~ubuntu-branches/ubuntu/trusty/mpd/trusty

« back to all changes in this revision

Viewing changes to src/decoder/wildmidi_decoder_plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2010 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
#include "config.h"
 
21
#include "decoder_api.h"
 
22
 
 
23
#include <glib.h>
 
24
 
 
25
#include <wildmidi_lib.h>
 
26
 
 
27
#undef G_LOG_DOMAIN
 
28
#define G_LOG_DOMAIN "wildmidi"
 
29
 
 
30
enum {
 
31
        WILDMIDI_SAMPLE_RATE = 48000,
 
32
};
 
33
 
 
34
static bool
 
35
wildmidi_init(const struct config_param *param)
 
36
{
 
37
        const char *config_file;
 
38
        int ret;
 
39
 
 
40
        config_file = config_get_block_string(param, "config_file",
 
41
                                              "/etc/timidity/timidity.cfg");
 
42
        if (!g_file_test(config_file, G_FILE_TEST_IS_REGULAR)) {
 
43
                g_debug("configuration file does not exist: %s", config_file);
 
44
                return false;
 
45
        }
 
46
 
 
47
        ret = WildMidi_Init(config_file, WILDMIDI_SAMPLE_RATE, 0);
 
48
        return ret == 0;
 
49
}
 
50
 
 
51
static void
 
52
wildmidi_finish(void)
 
53
{
 
54
        WildMidi_Shutdown();
 
55
}
 
56
 
 
57
static void
 
58
wildmidi_file_decode(struct decoder *decoder, const char *path_fs)
 
59
{
 
60
        static const struct audio_format audio_format = {
 
61
                .sample_rate = WILDMIDI_SAMPLE_RATE,
 
62
                .format = SAMPLE_FORMAT_S16,
 
63
                .channels = 2,
 
64
        };
 
65
        midi *wm;
 
66
        const struct _WM_Info *info;
 
67
        enum decoder_command cmd;
 
68
 
 
69
        wm = WildMidi_Open(path_fs);
 
70
        if (wm == NULL)
 
71
                return;
 
72
 
 
73
        info = WildMidi_GetInfo(wm);
 
74
        if (info == NULL) {
 
75
                WildMidi_Close(wm);
 
76
                return;
 
77
        }
 
78
 
 
79
        decoder_initialized(decoder, &audio_format, true,
 
80
                            info->approx_total_samples / WILDMIDI_SAMPLE_RATE);
 
81
 
 
82
        do {
 
83
                char buffer[4096];
 
84
                int len;
 
85
 
 
86
                info = WildMidi_GetInfo(wm);
 
87
                if (info == NULL)
 
88
                        break;
 
89
 
 
90
                len = WildMidi_GetOutput(wm, buffer, sizeof(buffer));
 
91
                if (len <= 0)
 
92
                        break;
 
93
 
 
94
                cmd = decoder_data(decoder, NULL, buffer, len, 0);
 
95
 
 
96
                if (cmd == DECODE_COMMAND_SEEK) {
 
97
                        unsigned long seek_where = WILDMIDI_SAMPLE_RATE *
 
98
                                decoder_seek_where(decoder);
 
99
 
 
100
#ifdef HAVE_WILDMIDI_SAMPLED_SEEK
 
101
                        WildMidi_SampledSeek(wm, &seek_where);
 
102
#else
 
103
                        WildMidi_FastSeek(wm, &seek_where);
 
104
#endif
 
105
                        decoder_command_finished(decoder);
 
106
                        cmd = DECODE_COMMAND_NONE;
 
107
                }
 
108
 
 
109
        } while (cmd == DECODE_COMMAND_NONE);
 
110
 
 
111
        WildMidi_Close(wm);
 
112
}
 
113
 
 
114
static struct tag *
 
115
wildmidi_tag_dup(const char *path_fs)
 
116
{
 
117
        midi *wm = WildMidi_Open(path_fs);
 
118
        if (wm == NULL)
 
119
                return NULL;
 
120
 
 
121
        const struct _WM_Info *info = WildMidi_GetInfo(wm);
 
122
        if (info == NULL) {
 
123
                WildMidi_Close(wm);
 
124
                return NULL;
 
125
        }
 
126
 
 
127
        struct tag *tag = tag_new();
 
128
        tag->time = info->approx_total_samples / WILDMIDI_SAMPLE_RATE;
 
129
 
 
130
        WildMidi_Close(wm);
 
131
 
 
132
        return tag;
 
133
}
 
134
 
 
135
static const char *const wildmidi_suffixes[] = {
 
136
        "mid",
 
137
        NULL
 
138
};
 
139
 
 
140
const struct decoder_plugin wildmidi_decoder_plugin = {
 
141
        .name = "wildmidi",
 
142
        .init = wildmidi_init,
 
143
        .finish = wildmidi_finish,
 
144
        .file_decode = wildmidi_file_decode,
 
145
        .tag_dup = wildmidi_tag_dup,
 
146
        .suffixes = wildmidi_suffixes,
 
147
};