run_filter.cxx 3.07 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2018 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 "config/Param.hxx"
21 22
#include "config/Data.hxx"
#include "config/File.hxx"
23
#include "config/Migrate.hxx"
24
#include "fs/Path.hxx"
25
#include "AudioParser.hxx"
26
#include "AudioFormat.hxx"
27
#include "filter/LoadOne.hxx"
28 29
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
30
#include "pcm/Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "mixer/MixerControl.hxx"
32
#include "util/ConstBuffer.hxx"
33
#include "util/StringBuffer.hxx"
34
#include "util/RuntimeError.hxx"
35
#include "util/PrintException.hxx"
36

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

40 41
#include <assert.h>
#include <string.h>
42 43
#include <stdlib.h>
#include <stdio.h>
44 45 46
#include <errno.h>
#include <unistd.h>

47
void
48
mixer_set_volume(gcc_unused Mixer *mixer,
49
		 gcc_unused unsigned volume)
50 51 52
{
}

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

62
	return filter_configured_new(*param);
63 64 65
}

int main(int argc, char **argv)
66
try {
67 68 69
	char buffer[4096];

	if (argc < 3 || argc > 4) {
70 71
		fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
72 73
	}

74 75
	const Path config_path = Path::FromFS(argv[1]);

76
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
77

78 79
	/* read configuration file (mpd.conf) */

80 81
	ConfigData config;
	ReadConfigFile(config, config_path);
82
	Migrate(config);
83 84 85

	/* parse the audio format */

86 87
	if (argc > 3)
		audio_format = ParseAudioFormat(argv[3], false);
88 89 90

	/* initialize the filter */

91
	auto prepared_filter = LoadFilter(config, argv[2]);
92 93 94

	/* open the filter */

95
	auto filter = prepared_filter->Open(audio_format);
96

97 98
	const AudioFormat out_audio_format = filter->GetOutAudioFormat();

99
	fprintf(stderr, "audio_format=%s\n",
100
		ToString(out_audio_format).c_str());
101 102 103 104 105 106 107 108 109 110

	/* play */

	while (true) {
		ssize_t nbytes;

		nbytes = read(0, buffer, sizeof(buffer));
		if (nbytes <= 0)
			break;

111
		auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes});
112

113
		nbytes = write(1, dest.data, dest.size);
114
		if (nbytes < 0) {
115 116
			fprintf(stderr, "Failed to write: %s\n",
				strerror(errno));
117 118 119 120 121 122
			return 1;
		}
	}

	/* cleanup and exit */

123
	return EXIT_SUCCESS;
124 125
} catch (...) {
	PrintException(std::current_exception());
126
	return EXIT_FAILURE;
127
}