run_filter.cxx 3.23 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
#include "config/ConfigGlobal.hxx"
23
#include "fs/Path.hxx"
24
#include "AudioParser.hxx"
25
#include "AudioFormat.hxx"
26 27
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
28
#include "pcm/Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "mixer/MixerControl.hxx"
30
#include "util/Error.hxx"
31
#include "util/ConstBuffer.hxx"
32
#include "system/FatalError.hxx"
33
#include "Log.hxx"
34

35 36
#include <memory>

37 38
#include <assert.h>
#include <string.h>
39 40
#include <stdlib.h>
#include <stdio.h>
41 42 43
#include <errno.h>
#include <unistd.h>

44
bool
45
mixer_set_volume(gcc_unused Mixer *mixer,
46
		 gcc_unused unsigned volume, gcc_unused Error &error)
47 48 49 50
{
	return true;
}

51
static PreparedFilter *
52 53
load_filter(const char *name)
{
54 55
	const auto *param = config_find_block(ConfigBlockOption::AUDIO_FILTER,
					      "name", name);
56
	if (param == NULL) {
57
		fprintf(stderr, "No such configured filter: %s\n", name);
58
		return nullptr;
59 60
	}

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

int main(int argc, char **argv)
65
try {
66
	struct audio_format_string af_string;
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 80
	/* read configuration file (mpd.conf) */

	config_global_init();
81
	ReadConfigFile(config_path);
82 83 84 85

	/* parse the audio format */

	if (argc > 3) {
86 87
		Error error;
		if (!audio_format_parse(audio_format, argv[3], false, error)) {
88 89
			LogError(error, "Failed to parse audio format");
			return EXIT_FAILURE;
90 91 92 93 94
		}
	}

	/* initialize the filter */

95 96
	std::unique_ptr<PreparedFilter> prepared_filter(load_filter(argv[2]));
	if (!prepared_filter)
97
		return EXIT_FAILURE;
98 99 100

	/* open the filter */

101
	std::unique_ptr<Filter> filter(prepared_filter->Open(audio_format));
102

103 104
	const AudioFormat out_audio_format = filter->GetOutAudioFormat();

105 106
	fprintf(stderr, "audio_format=%s\n",
		audio_format_to_string(out_audio_format, &af_string));
107 108 109 110 111 112 113 114 115 116

	/* play */

	while (true) {
		ssize_t nbytes;

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

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

119
		nbytes = write(1, dest.data, dest.size);
120
		if (nbytes < 0) {
121 122
			fprintf(stderr, "Failed to write: %s\n",
				strerror(errno));
123 124 125 126 127 128 129 130
			return 1;
		}
	}

	/* cleanup and exit */

	config_global_finish();

131
	return EXIT_SUCCESS;
132
} catch (const std::exception &e) {
133 134
	LogError(e);
	return EXIT_FAILURE;
135
}