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

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

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

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

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

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

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

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

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

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

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

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

	/* parse the audio format */

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

	/* initialize the filter */

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

	/* open the filter */

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

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

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

	/* play */

	while (true) {
		ssize_t nbytes;

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

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

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

	/* cleanup and exit */

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