run_filter.cxx 4.31 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
	if (nbytes < 0)
		throw MakeErrno("Read failed");

	return nbytes;
}

73 74 75 76 77 78 79 80 81 82
static size_t
WriteOrThrow(FileDescriptor fd, const void *buffer, size_t size)
{
	auto nbytes = fd.Write(buffer, size);
	if (nbytes < 0)
		throw MakeErrno("Write failed");

	return nbytes;
}

83
static void
84
FullWrite(FileDescriptor fd, ConstBuffer<uint8_t> src)
85
{
86 87
	while (!src.empty()) {
		size_t nbytes = WriteOrThrow(fd, src.data, src.size);
88
		if (nbytes == 0)
89
			throw std::runtime_error("Write failed");
90

91
		src.skip_front(nbytes);
92 93 94
	}
}

95 96 97 98 99 100
static void
FullWrite(FileDescriptor fd, ConstBuffer<void> src)
{
	FullWrite(fd, ConstBuffer<uint8_t>::FromVoid(src));
}

101
static size_t
102
ReadFrames(FileDescriptor fd, void *_buffer, size_t size, size_t frame_size)
103 104 105 106 107 108 109 110 111 112
{
	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;
113
		fd.FullRead(buffer + nbytes, rest);
114 115 116 117 118 119
		nbytes += rest;
	}

	return nbytes;
}

120
int main(int argc, char **argv)
121
try {
122
	if (argc < 3 || argc > 4) {
123 124
		fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
125 126
	}

127
	const FromNarrowPath config_path = argv[1];
128

129
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
130

131 132
	/* read configuration file (mpd.conf) */

133
	const auto config = AutoLoadConfigFile(config_path);
134 135 136

	/* parse the audio format */

137 138
	if (argc > 3)
		audio_format = ParseAudioFormat(argv[3], false);
139

140 141
	const size_t in_frame_size = audio_format.GetFrameSize();

142 143
	/* initialize the filter */

144
	auto prepared_filter = LoadFilter(config, argv[2]);
145 146 147

	/* open the filter */

148
	auto filter = prepared_filter->Open(audio_format);
149

150 151
	const AudioFormat out_audio_format = filter->GetOutAudioFormat();

152
	fprintf(stderr, "audio_format=%s\n",
153
		ToString(out_audio_format).c_str());
154 155 156

	/* play */

157 158 159
	FileDescriptor input_fd(STDIN_FILENO);
	FileDescriptor output_fd(STDOUT_FILENO);

160
	while (true) {
161 162
		char buffer[4096];

163
		ssize_t nbytes = ReadFrames(input_fd, buffer, sizeof(buffer),
164 165
					    in_frame_size);
		if (nbytes == 0)
166 167
			break;

168
		auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes});
169
		FullWrite(output_fd, dest);
170 171
	}

172 173 174 175 176 177 178
	while (true) {
		auto dest = filter->Flush();
		if (dest.IsNull())
			break;
		FullWrite(output_fd, dest);
	}

179 180
	/* cleanup and exit */

181
	return EXIT_SUCCESS;
182 183
} catch (...) {
	PrintException(std::current_exception());
184
	return EXIT_FAILURE;
185
}