2013-04-08 23:32:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-04-10 10:05:16 +02:00
|
|
|
#include "config.h"
|
2013-04-08 23:32:53 +02:00
|
|
|
#include "GmeDecoderPlugin.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2010-04-10 10:05:16 +02:00
|
|
|
#include "audio_check.h"
|
2013-07-29 07:32:36 +02:00
|
|
|
#include "TagHandler.hxx"
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "util/UriUtil.hxx"
|
2013-04-08 23:32:53 +02:00
|
|
|
|
2010-04-10 10:05:16 +02:00
|
|
|
#include <glib.h>
|
|
|
|
#include <assert.h>
|
2010-10-14 17:11:59 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-04-10 10:05:16 +02:00
|
|
|
#include <gme/gme.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "gme"
|
|
|
|
|
2010-10-14 17:11:59 +02:00
|
|
|
#define SUBTUNE_PREFIX "tune_"
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
static constexpr unsigned GME_SAMPLE_RATE = 44100;
|
|
|
|
static constexpr unsigned GME_CHANNELS = 2;
|
|
|
|
static constexpr unsigned GME_BUFFER_FRAMES = 2048;
|
|
|
|
static constexpr unsigned GME_BUFFER_SAMPLES =
|
|
|
|
GME_BUFFER_FRAMES * GME_CHANNELS;
|
2010-05-31 09:45:52 +02:00
|
|
|
|
2010-10-14 17:11:59 +02:00
|
|
|
/**
|
|
|
|
* returns the file path stripped of any /tune_xxx.* subtune
|
|
|
|
* suffix
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_container_name(const char *path_fs)
|
|
|
|
{
|
|
|
|
const char *subtune_suffix = uri_get_suffix(path_fs);
|
|
|
|
char *path_container = g_strdup(path_fs);
|
2013-04-08 23:32:53 +02:00
|
|
|
char *pat = g_strconcat("*/" SUBTUNE_PREFIX "???.",
|
|
|
|
subtune_suffix, nullptr);
|
2010-10-14 17:11:59 +02:00
|
|
|
GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
|
|
|
|
g_free(pat);
|
|
|
|
if (!g_pattern_match(path_with_subtune,
|
2013-04-08 23:32:53 +02:00
|
|
|
strlen(path_container), path_container, nullptr)) {
|
2010-10-14 17:11:59 +02:00
|
|
|
g_pattern_spec_free(path_with_subtune);
|
|
|
|
return path_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ptr = g_strrstr(path_container, "/" SUBTUNE_PREFIX);
|
2013-04-08 23:32:53 +02:00
|
|
|
if (ptr != nullptr)
|
2010-10-14 17:11:59 +02:00
|
|
|
*ptr='\0';
|
|
|
|
|
|
|
|
g_pattern_spec_free(path_with_subtune);
|
|
|
|
return path_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns tune number from file.nsf/tune_xxx.* style path or 0 if no subtune
|
|
|
|
* is appended.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
get_song_num(const char *path_fs)
|
|
|
|
{
|
|
|
|
const char *subtune_suffix = uri_get_suffix(path_fs);
|
2013-04-08 23:32:53 +02:00
|
|
|
char *pat = g_strconcat("*/" SUBTUNE_PREFIX "???.",
|
|
|
|
subtune_suffix, nullptr);
|
2010-10-14 17:11:59 +02:00
|
|
|
GPatternSpec *path_with_subtune = g_pattern_spec_new(pat);
|
|
|
|
g_free(pat);
|
|
|
|
|
|
|
|
if (g_pattern_match(path_with_subtune,
|
2013-04-08 23:32:53 +02:00
|
|
|
strlen(path_fs), path_fs, nullptr)) {
|
2010-10-14 17:11:59 +02:00
|
|
|
char *sub = g_strrstr(path_fs, "/" SUBTUNE_PREFIX);
|
|
|
|
g_pattern_spec_free(path_with_subtune);
|
2013-04-08 23:32:53 +02:00
|
|
|
if (!sub)
|
2010-10-14 17:11:59 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
sub += strlen("/" SUBTUNE_PREFIX);
|
2013-04-08 23:32:53 +02:00
|
|
|
int song_num = strtol(sub, nullptr, 10);
|
2010-10-14 17:11:59 +02:00
|
|
|
|
|
|
|
return song_num - 1;
|
|
|
|
} else {
|
|
|
|
g_pattern_spec_free(path_with_subtune);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
gme_container_scan(const char *path_fs, const unsigned int tnum)
|
|
|
|
{
|
|
|
|
Music_Emu *emu;
|
2013-04-08 23:32:53 +02:00
|
|
|
const char *gme_err = gme_open_file(path_fs, &emu, GME_SAMPLE_RATE);
|
|
|
|
if (gme_err != nullptr) {
|
2010-10-14 17:11:59 +02:00
|
|
|
g_warning("%s", gme_err);
|
2013-04-08 23:32:53 +02:00
|
|
|
return nullptr;
|
2010-10-14 17:11:59 +02:00
|
|
|
}
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
const unsigned num_songs = gme_track_count(emu);
|
2010-10-14 17:11:59 +02:00
|
|
|
/* if it only contains a single tune, don't treat as container */
|
|
|
|
if (num_songs < 2)
|
2013-04-08 23:32:53 +02:00
|
|
|
return nullptr;
|
2010-10-14 17:11:59 +02:00
|
|
|
|
|
|
|
const char *subtune_suffix = uri_get_suffix(path_fs);
|
|
|
|
if (tnum <= num_songs){
|
|
|
|
char *subtune = g_strdup_printf(
|
|
|
|
SUBTUNE_PREFIX "%03u.%s", tnum, subtune_suffix);
|
|
|
|
return subtune;
|
|
|
|
} else
|
2013-04-08 23:32:53 +02:00
|
|
|
return nullptr;
|
2010-10-14 17:11:59 +02:00
|
|
|
}
|
|
|
|
|
2010-04-10 10:05:16 +02:00
|
|
|
static void
|
|
|
|
gme_file_decode(struct decoder *decoder, const char *path_fs)
|
|
|
|
{
|
2010-10-14 17:11:59 +02:00
|
|
|
char *path_container = get_container_name(path_fs);
|
2010-04-10 10:05:16 +02:00
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
Music_Emu *emu;
|
|
|
|
const char *gme_err =
|
|
|
|
gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
|
2010-10-14 17:11:59 +02:00
|
|
|
g_free(path_container);
|
2013-04-08 23:32:53 +02:00
|
|
|
if (gme_err != nullptr) {
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", gme_err);
|
|
|
|
return;
|
|
|
|
}
|
2010-10-14 17:11:59 +02:00
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
gme_info_t *ti;
|
|
|
|
const int song_num = get_song_num(path_fs);
|
|
|
|
gme_err = gme_track_info(emu, &ti, song_num);
|
|
|
|
if (gme_err != nullptr) {
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", gme_err);
|
|
|
|
gme_delete(emu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
const float song_len = ti->length > 0
|
|
|
|
? ti->length / 1000.0
|
|
|
|
: -1.0;
|
2010-04-10 10:05:16 +02:00
|
|
|
|
|
|
|
/* initialize the MPD decoder */
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
GError *error = nullptr;
|
|
|
|
struct audio_format audio_format;
|
2010-05-31 09:45:52 +02:00
|
|
|
if (!audio_format_init_checked(&audio_format, GME_SAMPLE_RATE,
|
|
|
|
SAMPLE_FORMAT_S16, GME_CHANNELS,
|
|
|
|
&error)) {
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
gme_free_info(ti);
|
|
|
|
gme_delete(emu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder_initialized(decoder, &audio_format, true, song_len);
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
gme_err = gme_start_track(emu, song_num);
|
|
|
|
if (gme_err != nullptr)
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", gme_err);
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
if (ti->length > 0)
|
2011-02-03 00:25:35 +01:00
|
|
|
gme_set_fade(emu, ti->length);
|
|
|
|
|
2010-04-10 10:05:16 +02:00
|
|
|
/* play */
|
2013-04-08 23:32:53 +02:00
|
|
|
enum decoder_command cmd;
|
2010-04-10 10:05:16 +02:00
|
|
|
do {
|
2013-04-08 23:32:53 +02:00
|
|
|
short buf[GME_BUFFER_SAMPLES];
|
2010-05-31 09:44:30 +02:00
|
|
|
gme_err = gme_play(emu, GME_BUFFER_SAMPLES, buf);
|
2013-04-08 23:32:53 +02:00
|
|
|
if (gme_err != nullptr) {
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", gme_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
cmd = decoder_data(decoder, nullptr, buf, sizeof(buf), 0);
|
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
2010-04-10 10:05:16 +02:00
|
|
|
float where = decoder_seek_where(decoder);
|
2013-04-08 23:45:21 +02:00
|
|
|
gme_err = gme_seek(emu, int(where * 1000));
|
2013-04-08 23:32:53 +02:00
|
|
|
if (gme_err != nullptr)
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", gme_err);
|
|
|
|
decoder_command_finished(decoder);
|
|
|
|
}
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
if (gme_track_ended(emu))
|
2010-04-10 10:05:16 +02:00
|
|
|
break;
|
2013-04-08 23:32:53 +02:00
|
|
|
} while (cmd != DECODE_COMMAND_STOP);
|
2010-04-10 10:05:16 +02:00
|
|
|
|
|
|
|
gme_free_info(ti);
|
|
|
|
gme_delete(emu);
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
gme_scan_file(const char *path_fs,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2010-04-10 10:05:16 +02:00
|
|
|
{
|
2013-04-08 23:32:53 +02:00
|
|
|
char *path_container = get_container_name(path_fs);
|
2010-04-10 10:05:16 +02:00
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
Music_Emu *emu;
|
|
|
|
const char *gme_err =
|
|
|
|
gme_open_file(path_container, &emu, GME_SAMPLE_RATE);
|
2010-10-14 17:11:59 +02:00
|
|
|
g_free(path_container);
|
2013-04-08 23:32:53 +02:00
|
|
|
if (gme_err != nullptr) {
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", gme_err);
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2010-04-10 10:05:16 +02:00
|
|
|
}
|
2013-04-08 23:32:53 +02:00
|
|
|
|
|
|
|
const int song_num = get_song_num(path_fs);
|
|
|
|
|
|
|
|
gme_info_t *ti;
|
|
|
|
gme_err = gme_track_info(emu, &ti, song_num);
|
|
|
|
if (gme_err != nullptr) {
|
2010-04-10 10:05:16 +02:00
|
|
|
g_warning("%s", gme_err);
|
|
|
|
gme_delete(emu);
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2010-04-10 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
assert(ti != nullptr);
|
2012-02-11 16:59:24 +01:00
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
if (ti->length > 0)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
|
|
|
ti->length / 100);
|
|
|
|
|
2013-04-08 23:32:53 +02:00
|
|
|
if (ti->song != nullptr) {
|
|
|
|
if (gme_track_count(emu) > 1) {
|
2012-02-11 16:59:24 +01:00
|
|
|
/* start numbering subtunes from 1 */
|
2013-04-08 23:32:53 +02:00
|
|
|
char *tag_title =
|
|
|
|
g_strdup_printf("%s (%d/%d)",
|
|
|
|
ti->song, song_num + 1,
|
|
|
|
gme_track_count(emu));
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_TITLE, tag_title);
|
2012-02-11 16:59:24 +01:00
|
|
|
g_free(tag_title);
|
2013-04-08 23:32:53 +02:00
|
|
|
} else
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_TITLE, ti->song);
|
2010-04-10 10:05:16 +02:00
|
|
|
}
|
2013-04-08 23:32:53 +02:00
|
|
|
|
|
|
|
if (ti->author != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_ARTIST, ti->author);
|
2013-04-08 23:32:53 +02:00
|
|
|
|
|
|
|
if (ti->game != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_ALBUM, ti->game);
|
2013-04-08 23:32:53 +02:00
|
|
|
|
|
|
|
if (ti->comment != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_COMMENT, ti->comment);
|
2013-04-08 23:32:53 +02:00
|
|
|
|
|
|
|
if (ti->copyright != nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx,
|
|
|
|
TAG_DATE, ti->copyright);
|
2010-04-10 10:05:16 +02:00
|
|
|
|
|
|
|
gme_free_info(ti);
|
|
|
|
gme_delete(emu);
|
2012-02-11 19:12:02 +01:00
|
|
|
|
|
|
|
return true;
|
2010-04-10 10:05:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const gme_suffixes[] = {
|
|
|
|
"ay", "gbs", "gym", "hes", "kss", "nsf",
|
|
|
|
"nsfe", "sap", "spc", "vgm", "vgz",
|
2013-04-08 23:32:53 +02:00
|
|
|
nullptr
|
2010-04-10 10:05:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern const struct decoder_plugin gme_decoder_plugin;
|
|
|
|
const struct decoder_plugin gme_decoder_plugin = {
|
2013-04-08 23:32:53 +02:00
|
|
|
"gme",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
gme_file_decode,
|
|
|
|
gme_scan_file,
|
|
|
|
nullptr,
|
|
|
|
gme_container_scan,
|
|
|
|
gme_suffixes,
|
|
|
|
nullptr,
|
2010-04-10 10:05:16 +02:00
|
|
|
};
|