run_filter.cxx 3.57 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 22
#include "config/ConfigData.hxx"
#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 "stdbin.h"
31
#include "util/Error.hxx"
32
#include "util/ConstBuffer.hxx"
33
#include "system/FatalError.hxx"
34
#include "Log.hxx"
35 36 37

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

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

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

60 61
	Error error;
	Filter *filter = filter_configured_new(*param, error);
62
	if (filter == NULL) {
63
		LogError(error, "Failed to load filter");
64 65 66 67 68 69 70 71
		return NULL;
	}

	return filter;
}

int main(int argc, char **argv)
{
72
	struct audio_format_string af_string;
73
	Error error2;
74 75 76
	char buffer[4096];

	if (argc < 3 || argc > 4) {
77 78
		fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
79 80
	}

81 82
	const Path config_path = Path::FromFS(argv[1]);

83
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
84

85 86 87
	/* read configuration file (mpd.conf) */

	config_global_init();
88 89
	if (!ReadConfigFile(config_path, error2))
		FatalError(error2);
90 91 92 93

	/* parse the audio format */

	if (argc > 3) {
94 95
		Error error;
		if (!audio_format_parse(audio_format, argv[3], false, error)) {
96 97
			LogError(error, "Failed to parse audio format");
			return EXIT_FAILURE;
98 99 100 101 102
		}
	}

	/* initialize the filter */

103
	Filter *filter = load_filter(argv[2]);
104
	if (filter == NULL)
105
		return EXIT_FAILURE;
106 107 108

	/* open the filter */

109 110
	Error error;
	const AudioFormat out_audio_format = filter->Open(audio_format, error);
111
	if (!out_audio_format.IsDefined()) {
112
		LogError(error, "Failed to open filter");
113
		delete filter;
114
		return EXIT_FAILURE;
115 116
	}

117 118
	fprintf(stderr, "audio_format=%s\n",
		audio_format_to_string(out_audio_format, &af_string));
119 120 121 122 123 124 125 126 127 128

	/* play */

	while (true) {
		ssize_t nbytes;

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

129 130 131
		auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes},
					      error);
		if (dest.IsNull()) {
132
			LogError(error, "filter/Filter failed");
133 134
			filter->Close();
			delete filter;
135
			return EXIT_FAILURE;
136 137
		}

138
		nbytes = write(1, dest.data, dest.size);
139
		if (nbytes < 0) {
140 141
			fprintf(stderr, "Failed to write: %s\n",
				strerror(errno));
142 143
			filter->Close();
			delete filter;
144 145 146 147 148 149
			return 1;
		}
	}

	/* cleanup and exit */

150 151
	filter->Close();
	delete filter;
152 153 154 155 156

	config_global_finish();

	return 0;
}