~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to test/run_convert.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
18
 
 */
19
 
 
20
 
/*
21
 
 * This program is a command line interface to MPD's PCM conversion
22
 
 * library (pcm_convert.c).
23
 
 *
24
 
 */
25
 
 
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"
32
 
#include "stdbin.h"
33
 
 
34
 
#include <glib.h>
35
 
 
36
 
#include <assert.h>
37
 
#include <stddef.h>
38
 
#include <unistd.h>
39
 
 
40
 
static void
41
 
my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
42
 
            const gchar *message, G_GNUC_UNUSED gpointer user_data)
43
 
{
44
 
        if (log_domain != NULL)
45
 
                g_printerr("%s: %s\n", log_domain, message);
46
 
        else
47
 
                g_printerr("%s\n", message);
48
 
}
49
 
 
50
 
const char *
51
 
config_get_string(G_GNUC_UNUSED const char *name, const char *default_value)
52
 
{
53
 
        return default_value;
54
 
}
55
 
 
56
 
int main(int argc, char **argv)
57
 
{
58
 
        GError *error = NULL;
59
 
        struct audio_format in_audio_format, out_audio_format;
60
 
        struct pcm_convert_state state;
61
 
        const void *output;
62
 
        ssize_t nbytes;
63
 
        size_t length;
64
 
 
65
 
        if (argc != 3) {
66
 
                g_printerr("Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT\n");
67
 
                return 1;
68
 
        }
69
 
 
70
 
        g_log_set_default_handler(my_log_func, NULL);
71
 
 
72
 
        if (!audio_format_parse(&in_audio_format, argv[1],
73
 
                                false, &error)) {
74
 
                g_printerr("Failed to parse audio format: %s\n",
75
 
                           error->message);
76
 
                return 1;
77
 
        }
78
 
 
79
 
        struct audio_format out_audio_format_mask;
80
 
        if (!audio_format_parse(&out_audio_format_mask, argv[2],
81
 
                                true, &error)) {
82
 
                g_printerr("Failed to parse audio format: %s\n",
83
 
                           error->message);
84
 
                return 1;
85
 
        }
86
 
 
87
 
        out_audio_format = in_audio_format;
88
 
        audio_format_mask_apply(&out_audio_format, &out_audio_format_mask);
89
 
 
90
 
        const size_t in_frame_size = audio_format_frame_size(&in_audio_format);
91
 
 
92
 
        pcm_convert_init(&state);
93
 
 
94
 
        struct fifo_buffer *buffer = fifo_buffer_new(4096);
95
 
 
96
 
        while (true) {
97
 
                void *p = fifo_buffer_write(buffer, &length);
98
 
                assert(p != NULL);
99
 
 
100
 
                nbytes = read(0, p, length);
101
 
                if (nbytes <= 0)
102
 
                        break;
103
 
 
104
 
                fifo_buffer_append(buffer, nbytes);
105
 
 
106
 
                const void *src = fifo_buffer_read(buffer, &length);
107
 
                assert(src != NULL);
108
 
 
109
 
                length -= length % in_frame_size;
110
 
                if (length == 0)
111
 
                        continue;
112
 
 
113
 
                fifo_buffer_consume(buffer, length);
114
 
 
115
 
                output = pcm_convert(&state, &in_audio_format, src, length,
116
 
                                     &out_audio_format, &length, &error);
117
 
                if (output == NULL) {
118
 
                        g_printerr("Failed to convert: %s\n", error->message);
119
 
                        return 2;
120
 
                }
121
 
 
122
 
                G_GNUC_UNUSED ssize_t ignored = write(1, output, length);
123
 
        }
124
 
 
125
 
        pcm_convert_deinit(&state);
126
 
}