AdPlugDecoderPlugin.cxx 3.41 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
22
#include "tag/TagHandler.hxx"
23
#include "../DecoderAPI.hxx"
24
#include "CheckAudioFormat.hxx"
25
#include "fs/Path.hxx"
26
#include "util/Error.hxx"
27
#include "util/Domain.hxx"
28
#include "util/Macros.hxx"
29
#include "Log.hxx"
30 31 32 33 34 35

#include <adplug/adplug.h>
#include <adplug/emuopl.h>

#include <assert.h>

36 37
static constexpr Domain adplug_domain("adplug");

38 39 40
static unsigned sample_rate;

static bool
41
adplug_init(const ConfigBlock &block)
42
{
43 44 45
	FormatDebug(adplug_domain, "adplug %s",
		    CAdPlug::get_version().c_str());

46
	Error error;
47

48
	sample_rate = block.GetBlockValue("sample_rate", 48000u);
49
	if (!audio_check_sample_rate(sample_rate, error)) {
50
		LogError(error);
51 52 53 54 55 56 57
		return false;
	}

	return true;
}

static void
58
adplug_file_decode(Decoder &decoder, Path path_fs)
59 60 61 62
{
	CEmuopl opl(sample_rate, true, true);
	opl.init();

63
	CPlayer *player = CAdPlug::factory(path_fs.c_str(), &opl);
64 65 66
	if (player == nullptr)
		return;

67 68
	const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
	assert(audio_format.IsValid());
69

70
	decoder_initialized(decoder, audio_format, false,
71
			    SongTime::FromMS(player->songlength()));
72

73
	DecoderCommand cmd;
74 75 76 77 78

	do {
		if (!player->update())
			break;

79 80
		int16_t buffer[2048];
		constexpr unsigned frames_per_buffer = ARRAY_SIZE(buffer) / 2;
81
		opl.update(buffer, frames_per_buffer);
82
		cmd = decoder_data(decoder, nullptr,
83 84
				   buffer, sizeof(buffer),
				   0);
85
	} while (cmd == DecoderCommand::NONE);
86 87 88 89 90

	delete player;
}

static void
91
adplug_scan_tag(TagType type, const std::string &value,
92 93 94 95 96 97 98 99
		const struct tag_handler *handler, void *handler_ctx)
{
	if (!value.empty())
		tag_handler_invoke_tag(handler, handler_ctx,
				       type, value.c_str());
}

static bool
100
adplug_scan_file(Path path_fs,
101 102 103 104 105
		 const struct tag_handler *handler, void *handler_ctx)
{
	CEmuopl opl(sample_rate, true, true);
	opl.init();

106
	CPlayer *player = CAdPlug::factory(path_fs.c_str(), &opl);
107 108 109 110
	if (player == nullptr)
		return false;

	tag_handler_invoke_duration(handler, handler_ctx,
111
				    SongTime::FromMS(player->songlength()));
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	if (handler->tag != nullptr) {
		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
};

137
const struct DecoderPlugin adplug_decoder_plugin = {
138 139 140 141 142 143 144 145 146 147 148
	"adplug",
	adplug_init,
	nullptr,
	nullptr,
	adplug_file_decode,
	adplug_scan_file,
	nullptr,
	nullptr,
	adplug_suffixes,
	nullptr,
};