AdPlugDecoderPlugin.cxx 3.1 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "AdPlugDecoderPlugin.h"
21
#include "tag/Handler.hxx"
22
#include "../DecoderAPI.hxx"
23
#include "pcm/CheckAudioFormat.hxx"
24
#include "fs/Path.hxx"
25
#include "util/Domain.hxx"
26
#include "util/StringView.hxx"
27
#include "Log.hxx"
28 29 30 31

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

32
#include <cassert>
33

34 35
static constexpr Domain adplug_domain("adplug");

36 37 38
static unsigned sample_rate;

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

44
	sample_rate = block.GetPositiveValue("sample_rate", 48000U);
45
	CheckSampleRate(sample_rate);
46 47 48 49 50

	return true;
}

static void
51
adplug_file_decode(DecoderClient &client, Path path_fs)
52 53 54 55
{
	CEmuopl opl(sample_rate, true, true);
	opl.init();

56
	CPlayer *player = CAdPlug::factory(path_fs.c_str(), &opl);
57 58 59
	if (player == nullptr)
		return;

60 61
	const AudioFormat audio_format(sample_rate, SampleFormat::S16, 2);
	assert(audio_format.IsValid());
62

63 64
	client.Ready(audio_format, false,
		     SongTime::FromMS(player->songlength()));
65

66
	DecoderCommand cmd;
67 68 69 70 71

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

72
		int16_t buffer[2048];
73
		constexpr unsigned frames_per_buffer = std::size(buffer) / 2;
74
		opl.update(buffer, frames_per_buffer);
75 76 77
		cmd = client.SubmitData(nullptr,
					buffer, sizeof(buffer),
					0);
78
	} while (cmd == DecoderCommand::NONE);
79 80 81 82 83

	delete player;
}

static void
84
adplug_scan_tag(TagType type, const std::string &value,
85
		TagHandler &handler) noexcept
86 87
{
	if (!value.empty())
88
		handler.OnTag(type, {value.data(), value.size()});
89 90 91
}

static bool
92
adplug_scan_file(Path path_fs, TagHandler &handler) noexcept
93 94 95 96
{
	CEmuopl opl(sample_rate, true, true);
	opl.init();

97
	CPlayer *player = CAdPlug::factory(path_fs.c_str(), &opl);
98 99 100
	if (player == nullptr)
		return false;

101
	handler.OnDuration(SongTime::FromMS(player->songlength()));
102

103
	if (handler.WantTag()) {
104
		adplug_scan_tag(TAG_TITLE, player->gettitle(),
105
				handler);
106
		adplug_scan_tag(TAG_ARTIST, player->getauthor(),
107
				handler);
108
		adplug_scan_tag(TAG_COMMENT, player->getdesc(),
109
				handler);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	}

	delete player;
	return true;
}

static const char *const adplug_suffixes[] = {
	"amd",
	"d00",
	"hsc",
	"laa",
	"rad",
	"raw",
	"sa2",
	nullptr
};

127 128 129 130
constexpr DecoderPlugin adplug_decoder_plugin =
	DecoderPlugin("adplug", adplug_file_decode, adplug_scan_file)
	.WithInit(adplug_init)
	.WithSuffixes(adplug_suffixes);