configure.ac: Move WAVE Encoder to Encoder Plugins.
[mpd-mk.git] / test / run_convert.c
blobddfca0870c3dfa6dd38bab88c5ed28f302479a7c
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.
21 * This program is a command line interface to MPD's PCM conversion
22 * library (pcm_convert.c).
26 #include "config.h"
27 #include "audio_parser.h"
28 #include "audio_format.h"
29 #include "pcm_convert.h"
30 #include "conf.h"
31 #include "fifo_buffer.h"
33 #include <glib.h>
35 #include <assert.h>
36 #include <stddef.h>
37 #include <unistd.h>
39 static void
40 my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
41 const gchar *message, G_GNUC_UNUSED gpointer user_data)
43 if (log_domain != NULL)
44 g_printerr("%s: %s\n", log_domain, message);
45 else
46 g_printerr("%s\n", message);
49 const char *
50 config_get_string(G_GNUC_UNUSED const char *name, const char *default_value)
52 return default_value;
55 int main(int argc, char **argv)
57 GError *error = NULL;
58 struct audio_format in_audio_format, out_audio_format;
59 struct pcm_convert_state state;
60 const void *output;
61 ssize_t nbytes;
62 size_t length;
64 if (argc != 3) {
65 g_printerr("Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT\n");
66 return 1;
69 g_log_set_default_handler(my_log_func, NULL);
71 if (!audio_format_parse(&in_audio_format, argv[1],
72 false, &error)) {
73 g_printerr("Failed to parse audio format: %s\n",
74 error->message);
75 return 1;
78 if (!audio_format_parse(&out_audio_format, argv[2],
79 false, &error)) {
80 g_printerr("Failed to parse audio format: %s\n",
81 error->message);
82 return 1;
85 const size_t in_frame_size = audio_format_frame_size(&in_audio_format);
87 pcm_convert_init(&state);
89 struct fifo_buffer *buffer = fifo_buffer_new(4096);
91 while (true) {
92 void *p = fifo_buffer_write(buffer, &length);
93 assert(p != NULL);
95 nbytes = read(0, p, length);
96 if (nbytes <= 0)
97 break;
99 fifo_buffer_append(buffer, nbytes);
101 const void *src = fifo_buffer_read(buffer, &length);
102 assert(src != NULL);
104 length -= length % in_frame_size;
105 if (length == 0)
106 continue;
108 fifo_buffer_consume(buffer, length);
110 output = pcm_convert(&state, &in_audio_format, src, length,
111 &out_audio_format, &length, &error);
112 if (output == NULL) {
113 g_printerr("Failed to convert: %s\n", error->message);
114 return 2;
117 write(1, output, length);
120 pcm_convert_deinit(&state);