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

« back to all changes in this revision

Viewing changes to src/decoder/AdPlugDecoderPlugin.cxx

  • 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-2013 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 "AdPlugDecoderPlugin.h"
 
22
#include "tag/TagHandler.hxx"
 
23
#include "DecoderAPI.hxx"
 
24
#include "CheckAudioFormat.hxx"
 
25
#include "util/Error.hxx"
 
26
#include "util/Macros.hxx"
 
27
#include "Log.hxx"
 
28
 
 
29
#include <adplug/adplug.h>
 
30
#include <adplug/emuopl.h>
 
31
 
 
32
#include <assert.h>
 
33
 
 
34
static unsigned sample_rate;
 
35
 
 
36
static bool
 
37
adplug_init(const config_param &param)
 
38
{
 
39
        Error error;
 
40
 
 
41
        sample_rate = param.GetBlockValue("sample_rate", 48000u);
 
42
        if (!audio_check_sample_rate(sample_rate, error)) {
 
43
                LogError(error);
 
44
                return false;
 
45
        }
 
46
 
 
47
        return true;
 
48
}
 
49
 
 
50
static void
 
51
adplug_file_decode(Decoder &decoder, const char *path_fs)
 
52
{
 
53
        CEmuopl opl(sample_rate, true, true);
 
54
        opl.init();
 
55
 
 
56
        CPlayer *player = CAdPlug::factory(path_fs, &opl);
 
57
        if (player == nullptr)
 
58
                return;
 
59
 
 
60
        const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
 
61
        assert(audio_format.IsValid());
 
62
 
 
63
        decoder_initialized(decoder, audio_format, false,
 
64
                player->songlength() / 1000.);
 
65
 
 
66
        int16_t buffer[2048];
 
67
        const unsigned frames_per_buffer = ARRAY_SIZE(buffer) / 2;
 
68
        DecoderCommand cmd;
 
69
 
 
70
        do {
 
71
                if (!player->update())
 
72
                        break;
 
73
 
 
74
                opl.update(buffer, frames_per_buffer);
 
75
                cmd = decoder_data(decoder, nullptr,
 
76
                                   buffer, sizeof(buffer),
 
77
                                   0);
 
78
        } while (cmd == DecoderCommand::NONE);
 
79
 
 
80
        delete player;
 
81
}
 
82
 
 
83
static void
 
84
adplug_scan_tag(TagType type, const std::string &value,
 
85
                const struct tag_handler *handler, void *handler_ctx)
 
86
{
 
87
        if (!value.empty())
 
88
                tag_handler_invoke_tag(handler, handler_ctx,
 
89
                                       type, value.c_str());
 
90
}
 
91
 
 
92
static bool
 
93
adplug_scan_file(const char *path_fs,
 
94
                 const struct tag_handler *handler, void *handler_ctx)
 
95
{
 
96
        CEmuopl opl(sample_rate, true, true);
 
97
        opl.init();
 
98
 
 
99
        CPlayer *player = CAdPlug::factory(path_fs, &opl);
 
100
        if (player == nullptr)
 
101
                return false;
 
102
 
 
103
        tag_handler_invoke_duration(handler, handler_ctx,
 
104
                                    player->songlength() / 1000);
 
105
 
 
106
        if (handler->tag != nullptr) {
 
107
                adplug_scan_tag(TAG_TITLE, player->gettitle(),
 
108
                                handler, handler_ctx);
 
109
                adplug_scan_tag(TAG_ARTIST, player->getauthor(),
 
110
                                handler, handler_ctx);
 
111
                adplug_scan_tag(TAG_COMMENT, player->getdesc(),
 
112
                                handler, handler_ctx);
 
113
        }
 
114
 
 
115
        delete player;
 
116
        return true;
 
117
}
 
118
 
 
119
static const char *const adplug_suffixes[] = {
 
120
        "amd",
 
121
        "d00",
 
122
        "hsc",
 
123
        "laa",
 
124
        "rad",
 
125
        "raw",
 
126
        "sa2",
 
127
        nullptr
 
128
};
 
129
 
 
130
const struct DecoderPlugin adplug_decoder_plugin = {
 
131
        "adplug",
 
132
        adplug_init,
 
133
        nullptr,
 
134
        nullptr,
 
135
        adplug_file_decode,
 
136
        adplug_scan_file,
 
137
        nullptr,
 
138
        nullptr,
 
139
        adplug_suffixes,
 
140
        nullptr,
 
141
};