configure.ac: Move WAVE Encoder to Encoder Plugins.
[mpd-mk.git] / test / run_filter.c
blobf9d628aebf1564fdb6bfedd610d94eb55184a334
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "conf.h"
22 #include "audio_parser.h"
23 #include "audio_format.h"
24 #include "filter_plugin.h"
25 #include "pcm_volume.h"
26 #include "idle.h"
27 #include "mixer_control.h"
29 #include <glib.h>
31 #include <assert.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <unistd.h>
36 void
37 idle_add(G_GNUC_UNUSED unsigned flags)
41 bool
42 mixer_set_volume(G_GNUC_UNUSED struct mixer *mixer,
43 G_GNUC_UNUSED unsigned volume, G_GNUC_UNUSED GError **error_r)
45 return true;
48 static void
49 my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
50 const gchar *message, G_GNUC_UNUSED gpointer user_data)
52 if (log_domain != NULL)
53 g_printerr("%s: %s\n", log_domain, message);
54 else
55 g_printerr("%s\n", message);
58 static const struct config_param *
59 find_named_config_block(const char *block, const char *name)
61 const struct config_param *param = NULL;
63 while ((param = config_get_next_param(block, param)) != NULL) {
64 const char *current_name =
65 config_get_block_string(param, "name", NULL);
66 if (current_name != NULL && strcmp(current_name, name) == 0)
67 return param;
70 return NULL;
73 static struct filter *
74 load_filter(const char *name)
76 const struct config_param *param;
77 struct filter *filter;
78 GError *error = NULL;
80 param = find_named_config_block("filter", name);
81 if (param == NULL) {
82 g_printerr("No such configured filter: %s\n", name);
83 return false;
86 filter = filter_configured_new(param, &error);
87 if (filter == NULL) {
88 g_printerr("Failed to load filter: %s\n", error->message);
89 g_error_free(error);
90 return NULL;
93 return filter;
96 int main(int argc, char **argv)
98 struct audio_format audio_format;
99 struct audio_format_string af_string;
100 bool success;
101 GError *error = NULL;
102 struct filter *filter;
103 const struct audio_format *out_audio_format;
104 char buffer[4096];
105 size_t frame_size;
107 if (argc < 3 || argc > 4) {
108 g_printerr("Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
109 return 1;
112 audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);
114 /* initialize GLib */
116 g_thread_init(NULL);
117 g_log_set_default_handler(my_log_func, NULL);
119 /* read configuration file (mpd.conf) */
121 config_global_init();
122 success = config_read_file(argv[1], &error);
123 if (!success) {
124 g_printerr("%s:", error->message);
125 g_error_free(error);
126 return 1;
129 /* parse the audio format */
131 if (argc > 3) {
132 success = audio_format_parse(&audio_format, argv[3],
133 false, &error);
134 if (!success) {
135 g_printerr("Failed to parse audio format: %s\n",
136 error->message);
137 g_error_free(error);
138 return 1;
142 /* initialize the filter */
144 filter = load_filter(argv[2]);
145 if (filter == NULL)
146 return 1;
148 /* open the filter */
150 out_audio_format = filter_open(filter, &audio_format, &error);
151 if (out_audio_format == NULL) {
152 g_printerr("Failed to open filter: %s\n", error->message);
153 g_error_free(error);
154 filter_free(filter);
155 return 1;
158 g_printerr("audio_format=%s\n",
159 audio_format_to_string(out_audio_format, &af_string));
161 frame_size = audio_format_frame_size(&audio_format);
163 /* play */
165 while (true) {
166 ssize_t nbytes;
167 size_t length;
168 const void *dest;
170 nbytes = read(0, buffer, sizeof(buffer));
171 if (nbytes <= 0)
172 break;
174 dest = filter_filter(filter, buffer, (size_t)nbytes,
175 &length, &error);
176 if (dest == NULL) {
177 g_printerr("Filter failed: %s\n", error->message);
178 filter_close(filter);
179 filter_free(filter);
180 return 1;
183 nbytes = write(1, dest, length);
184 if (nbytes < 0) {
185 g_printerr("Failed to write: %s\n", strerror(errno));
186 filter_close(filter);
187 filter_free(filter);
188 return 1;
192 /* cleanup and exit */
194 filter_close(filter);
195 filter_free(filter);
197 config_global_finish();
199 return 0;