run_filter.cxx 3.72 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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
#ifdef HAVE_GLIB
37
#include <glib.h>
38
#endif
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
bool
48
mixer_set_volume(gcc_unused Mixer *mixer,
49
		 gcc_unused unsigned volume, gcc_unused Error &error)
50 51 52 53
{
	return true;
}

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

64 65
	Error error;
	Filter *filter = filter_configured_new(*param, error);
66
	if (filter == NULL) {
67
		LogError(error, "Failed to load filter");
68 69 70 71 72 73 74 75
		return NULL;
	}

	return filter;
}

int main(int argc, char **argv)
{
76
	struct audio_format_string af_string;
77
	Error error2;
78 79 80
	char buffer[4096];

	if (argc < 3 || argc > 4) {
81 82
		fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
		return EXIT_FAILURE;
83 84
	}

85 86
	const Path config_path = Path::FromFS(argv[1]);

87
	AudioFormat audio_format(44100, SampleFormat::S16, 2);
88

89 90
	/* initialize GLib */

91
#ifdef HAVE_GLIB
92
#if !GLIB_CHECK_VERSION(2,32,0)
93
	g_thread_init(NULL);
94
#endif
95 96
#endif

97 98 99
	/* read configuration file (mpd.conf) */

	config_global_init();
100 101
	if (!ReadConfigFile(config_path, error2))
		FatalError(error2);
102 103 104 105

	/* parse the audio format */

	if (argc > 3) {
106 107
		Error error;
		if (!audio_format_parse(audio_format, argv[3], false, error)) {
108 109
			LogError(error, "Failed to parse audio format");
			return EXIT_FAILURE;
110 111 112 113 114
		}
	}

	/* initialize the filter */

115
	Filter *filter = load_filter(argv[2]);
116
	if (filter == NULL)
117
		return EXIT_FAILURE;
118 119 120

	/* open the filter */

121 122
	Error error;
	const AudioFormat out_audio_format = filter->Open(audio_format, error);
123
	if (!out_audio_format.IsDefined()) {
124
		LogError(error, "Failed to open filter");
125
		delete filter;
126
		return EXIT_FAILURE;
127 128
	}

129 130
	fprintf(stderr, "audio_format=%s\n",
		audio_format_to_string(out_audio_format, &af_string));
131 132 133 134 135 136 137 138 139 140

	/* play */

	while (true) {
		ssize_t nbytes;

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

141 142 143
		auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes},
					      error);
		if (dest.IsNull()) {
144
			LogError(error, "filter/Filter failed");
145 146
			filter->Close();
			delete filter;
147
			return EXIT_FAILURE;
148 149
		}

150
		nbytes = write(1, dest.data, dest.size);
151
		if (nbytes < 0) {
152 153
			fprintf(stderr, "Failed to write: %s\n",
				strerror(errno));
154 155
			filter->Close();
			delete filter;
156 157 158 159 160 161
			return 1;
		}
	}

	/* cleanup and exit */

162 163
	filter->Close();
	delete filter;
164 165 166 167 168

	config_global_finish();

	return 0;
}