run_filter.cxx 3.79 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "ConfigGlue.hxx"
21
#include "fs/Path.hxx"
22
#include "fs/NarrowPath.hxx"
23
#include "filter/LoadOne.hxx"
24 25
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
26 27
#include "pcm/AudioParser.hxx"
#include "pcm/AudioFormat.hxx"
28
#include "pcm/Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "mixer/MixerControl.hxx"
30
#include "system/Error.hxx"
31
#include "io/FileDescriptor.hxx"
32
#include "util/ConstBuffer.hxx"
33
#include "util/StringBuffer.hxx"
34
#include "util/RuntimeError.hxx"
35
#include "util/PrintException.hxx"
36

37
#include <cassert>
38
#include <memory>
39
#include <stdexcept>
40

41
#include <string.h>
42 43
#include <stdlib.h>
#include <stdio.h>
44

45
void
Rosen Penev's avatar
Rosen Penev committed
46 47
mixer_set_volume([[maybe_unused]] Mixer *mixer,
		 [[maybe_unused]] unsigned volume)
48 49 50
{
}

51
static std::unique_ptr<PreparedFilter>
52
LoadFilter(const ConfigData &config, const char *name)
53
{
54 55
	const auto *param = config.FindBlock(ConfigBlockOption::AUDIO_FILTER,
					     "name", name);
56
	if (param == nullptr)
57 58
		throw FormatRuntimeError("No such configured filter: %s",
					 name);
59

60
	return filter_configured_new(*param);
61 62
}

63
static size_t
64
ReadOrThrow(FileDescriptor fd, void *buffer, size_t size)
65
{
66
	auto nbytes = fd.Read(buffer, size);
67 68 69 70 71 72 73
	if (nbytes < 0)
		throw MakeErrno("Read failed");

	return nbytes;
}

static size_t
74
ReadFrames(FileDescriptor fd, void *_buffer, size_t size, size_t frame_size)
75 76 77 78 79 80 81 82 83 84
{
	auto buffer = (uint8_t *)_buffer;

	size = (size / frame_size) * frame_size;

	size_t nbytes = ReadOrThrow(fd, buffer, size);

	const size_t modulo = nbytes % frame_size;
	if (modulo > 0) {
		size_t rest = frame_size - modulo;
85
		fd.FullRead(buffer + nbytes, rest);
86 87 88 89 90 91
		nbytes += rest;
	}

	return nbytes;
}

92
int main(int argc, char **argv)
93
try {
94
	if (argc < 3 || argc > 4) {
95 96
		fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
97 98
	}

99
	const FromNarrowPath config_path = argv[1];
100

101
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
102

103 104
	/* read configuration file (mpd.conf) */

105
	const auto config = AutoLoadConfigFile(config_path);
106 107 108

	/* parse the audio format */

109 110
	if (argc > 3)
		audio_format = ParseAudioFormat(argv[3], false);
111

112 113
	const size_t in_frame_size = audio_format.GetFrameSize();

114 115
	/* initialize the filter */

116
	auto prepared_filter = LoadFilter(config, argv[2]);
117 118 119

	/* open the filter */

120
	auto filter = prepared_filter->Open(audio_format);
121

122 123
	const AudioFormat out_audio_format = filter->GetOutAudioFormat();

124
	fprintf(stderr, "audio_format=%s\n",
125
		ToString(out_audio_format).c_str());
126 127 128

	/* play */

129 130 131
	FileDescriptor input_fd(STDIN_FILENO);
	FileDescriptor output_fd(STDOUT_FILENO);

132
	while (true) {
133 134
		char buffer[4096];

135
		ssize_t nbytes = ReadFrames(input_fd, buffer, sizeof(buffer),
136 137
					    in_frame_size);
		if (nbytes == 0)
138 139
			break;

140
		auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes});
141
		output_fd.FullWrite(dest.data, dest.size);
142 143
	}

144 145 146 147
	while (true) {
		auto dest = filter->Flush();
		if (dest.IsNull())
			break;
148
		output_fd.FullWrite(dest.data, dest.size);
149 150
	}

151 152
	/* cleanup and exit */

153
	return EXIT_SUCCESS;
154 155
} catch (...) {
	PrintException(std::current_exception());
156
	return EXIT_FAILURE;
157
}