ModplugDecoderPlugin.cxx 4.53 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "ModplugDecoderPlugin.hxx"
21
#include "ModCommon.hxx"
22
#include "../DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "input/InputStream.hxx"
24
#include "tag/Handler.hxx"
25
#include "util/Domain.hxx"
26
#include "util/RuntimeError.hxx"
27
#include "util/StringView.hxx"
28
#include "Log.hxx"
29

30 31 32 33 34
#ifdef _WIN32
/* assume ModPlug is built as static library on Windows; without
   this, linking to the static library would fail */
#define MODPLUG_STATIC
#endif
35 36 37

#include <libmodplug/modplug.h>

38
#include <cassert>
39

40
static constexpr Domain modplug_domain("modplug");
41

42
static constexpr size_t MODPLUG_FRAME_SIZE = 4096;
43

44
static int modplug_loop_count;
45
static unsigned char modplug_resampling_mode;
46 47

static bool
48
modplug_decoder_init(const ConfigBlock &block)
49
{
50 51 52 53 54 55 56 57 58 59 60 61 62 63
	const char* modplug_resampling_mode_value = block.GetBlockValue("resampling_mode", "fir");
	if (strcmp(modplug_resampling_mode_value, "nearest") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_NEAREST;
	} else if (strcmp(modplug_resampling_mode_value, "linear") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_LINEAR;
	} else if (strcmp(modplug_resampling_mode_value, "spline") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_SPLINE;
	} else if (strcmp(modplug_resampling_mode_value, "fir") == 0) {
		modplug_resampling_mode = MODPLUG_RESAMPLE_FIR;
	} else {
		throw FormatRuntimeError("Invalid resampling mode in line %d: %s",
				block.line, modplug_resampling_mode_value);
	}

64
	modplug_loop_count = block.GetBlockValue("loop_count", 0);
65
	if (modplug_loop_count < -1)
66 67
		throw FormatRuntimeError("Invalid loop count in line %d: %i",
					 block.line, modplug_loop_count);
68 69 70 71

	return true;
}

72
static ModPlugFile *
73
LoadModPlugFile(DecoderClient *client, InputStream &is)
74
{
75
	const auto buffer = mod_loadfile(&modplug_domain, client, is);
76
	if (buffer.IsNull()) {
77 78 79 80
		LogWarning(modplug_domain, "could not load stream");
		return nullptr;
	}

81
	ModPlugFile *f = ModPlug_Load(buffer.data(), buffer.size());
82 83 84
	return f;
}

85
static void
86
mod_decode(DecoderClient &client, InputStream &is)
87 88
{
	ModPlug_Settings settings;
89
	int ret;
90 91 92 93
	char audio_buffer[MODPLUG_FRAME_SIZE];

	ModPlug_GetSettings(&settings);
	/* alter setting */
94
	settings.mResamplingMode = modplug_resampling_mode;
95 96 97
	settings.mChannels = 2;
	settings.mBits = 16;
	settings.mFrequency = 44100;
98
	settings.mLoopCount = modplug_loop_count;
99 100 101
	/* insert more setting changes here */
	ModPlug_SetSettings(&settings);

102
	ModPlugFile *f = LoadModPlugFile(&client, is);
103
	if (f == nullptr) {
104
		LogWarning(modplug_domain, "could not decode stream");
105 106 107
		return;
	}

108 109
	static constexpr AudioFormat audio_format(44100, SampleFormat::S16, 2);
	assert(audio_format.IsValid());
110

111 112
	client.Ready(audio_format, is.IsSeekable(),
		     SongTime::FromMS(ModPlug_GetLength(f)));
113

114
	DecoderCommand cmd;
115 116
	do {
		ret = ModPlug_Read(f, audio_buffer, MODPLUG_FRAME_SIZE);
117
		if (ret <= 0)
118 119
			break;

120 121 122
		cmd = client.SubmitData(nullptr,
					audio_buffer, ret,
					0);
123

124
		if (cmd == DecoderCommand::SEEK) {
125 126
			ModPlug_Seek(f, client.GetSeekTime().ToMS());
			client.CommandFinished();
127 128
		}

129
	} while (cmd != DecoderCommand::STOP);
130 131 132 133

	ModPlug_Unload(f);
}

134
static bool
135
modplug_scan_stream(InputStream &is, TagHandler &handler) noexcept
136
{
137
	ModPlugFile *f = LoadModPlugFile(nullptr, is);
138
	if (f == nullptr)
139
		return false;
140

141
	handler.OnDuration(SongTime::FromMS(ModPlug_GetLength(f)));
142

143
	const char *title = ModPlug_GetName(f);
144
	if (title != nullptr)
145
		handler.OnTag(TAG_TITLE, title);
146 147 148

	ModPlug_Unload(f);

149
	return true;
150 151 152 153 154 155
}

static const char *const mod_suffixes[] = {
	"669", "amf", "ams", "dbm", "dfm", "dsm", "far", "it",
	"med", "mdl", "mod", "mtm", "mt2", "okt", "s3m", "stm",
	"ult", "umx", "xm",
156
	nullptr
157 158
};

159 160 161 162
constexpr DecoderPlugin modplug_decoder_plugin =
	DecoderPlugin("modplug", mod_decode, modplug_scan_stream)
	.WithInit(modplug_decoder_init)
	.WithSuffixes(mod_suffixes);