mpd/src/decoder/plugins/AdPlugDecoderPlugin.cxx

143 lines
3.3 KiB
C++
Raw Normal View History

2012-09-25 22:03:44 +02:00
/*
2016-02-26 17:54:05 +01:00
* Copyright 2003-2016 The Music Player Daemon Project
2012-09-25 22:03:44 +02:00
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "AdPlugDecoderPlugin.h"
2013-09-05 18:22:02 +02:00
#include "tag/TagHandler.hxx"
2014-01-24 00:02:24 +01:00
#include "../DecoderAPI.hxx"
2013-07-29 07:50:08 +02:00
#include "CheckAudioFormat.hxx"
#include "fs/Path.hxx"
2014-09-21 13:22:56 +02:00
#include "util/Domain.hxx"
#include "util/Macros.hxx"
#include "Log.hxx"
2012-09-25 22:03:44 +02:00
#include <adplug/adplug.h>
#include <adplug/emuopl.h>
#include <assert.h>
2014-09-21 13:22:56 +02:00
static constexpr Domain adplug_domain("adplug");
2012-09-25 22:03:44 +02:00
static unsigned sample_rate;
static bool
adplug_init(const ConfigBlock &block)
2012-09-25 22:03:44 +02:00
{
2014-09-21 13:22:56 +02:00
FormatDebug(adplug_domain, "adplug %s",
CAdPlug::get_version().c_str());
sample_rate = block.GetBlockValue("sample_rate", 48000u);
CheckSampleRate(sample_rate);
2012-09-25 22:03:44 +02:00
return true;
}
static void
adplug_file_decode(DecoderClient &client, Path path_fs)
2012-09-25 22:03:44 +02:00
{
CEmuopl opl(sample_rate, true, true);
opl.init();
CPlayer *player = CAdPlug::factory(path_fs.c_str(), &opl);
2012-09-25 22:03:44 +02:00
if (player == nullptr)
return;
2013-08-03 21:00:50 +02:00
const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
assert(audio_format.IsValid());
2012-09-25 22:03:44 +02:00
client.Ready(audio_format, false,
SongTime::FromMS(player->songlength()));
2012-09-25 22:03:44 +02:00
DecoderCommand cmd;
2012-09-25 22:03:44 +02:00
do {
if (!player->update())
break;
int16_t buffer[2048];
constexpr unsigned frames_per_buffer = ARRAY_SIZE(buffer) / 2;
2012-09-25 22:03:44 +02:00
opl.update(buffer, frames_per_buffer);
cmd = decoder_data(client, nullptr,
2012-09-25 22:03:44 +02:00
buffer, sizeof(buffer),
0);
} while (cmd == DecoderCommand::NONE);
2012-09-25 22:03:44 +02:00
delete player;
}
static void
adplug_scan_tag(TagType type, const std::string &value,
2016-02-23 10:10:13 +01:00
const TagHandler &handler, void *handler_ctx)
2012-09-25 22:03:44 +02:00
{
if (!value.empty())
tag_handler_invoke_tag(handler, handler_ctx,
type, value.c_str());
}
static bool
adplug_scan_file(Path path_fs,
2016-02-23 10:10:13 +01:00
const TagHandler &handler, void *handler_ctx)
2012-09-25 22:03:44 +02:00
{
CEmuopl opl(sample_rate, true, true);
opl.init();
CPlayer *player = CAdPlug::factory(path_fs.c_str(), &opl);
2012-09-25 22:03:44 +02:00
if (player == nullptr)
return false;
tag_handler_invoke_duration(handler, handler_ctx,
SongTime::FromMS(player->songlength()));
2012-09-25 22:03:44 +02:00
2016-02-23 10:10:13 +01:00
if (handler.tag != nullptr) {
2012-09-25 22:03:44 +02:00
adplug_scan_tag(TAG_TITLE, player->gettitle(),
handler, handler_ctx);
adplug_scan_tag(TAG_ARTIST, player->getauthor(),
handler, handler_ctx);
adplug_scan_tag(TAG_COMMENT, player->getdesc(),
handler, handler_ctx);
}
delete player;
return true;
}
static const char *const adplug_suffixes[] = {
"amd",
"d00",
"hsc",
"laa",
"rad",
"raw",
"sa2",
nullptr
};
const struct DecoderPlugin adplug_decoder_plugin = {
2012-09-25 22:03:44 +02:00
"adplug",
adplug_init,
nullptr,
nullptr,
adplug_file_decode,
adplug_scan_file,
nullptr,
nullptr,
adplug_suffixes,
nullptr,
};