aboutsummaryrefslogtreecommitdiffstats
path: root/src/decoder/gme_decoder_plugin.c
blob: 336fcb87f00e74fb3dd1ecf22a33255343fbaf25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "config.h"
#include "../decoder_api.h"
#include "audio_check.h"
#include <glib.h>
#include <assert.h>
#include <gme/gme.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "gme"

#define GME_BUF_SIZE	4096

static void
gme_file_decode(struct decoder *decoder, const char *path_fs)
{
	int sample_rate = 44100;
	int track = 0; /* index of track to play */
	float song_len;
	Music_Emu *emu;
	gme_info_t *ti;
	struct audio_format audio_format;
	enum decoder_command cmd;
	short buf[GME_BUF_SIZE];
	const char* gme_err;

	if((gme_err = gme_open_file(path_fs, &emu, sample_rate)) != NULL){
		g_warning("%s", gme_err);
		return;
	}
	if((gme_err = gme_track_info(emu, &ti, 0)) != NULL){
		g_warning("%s", gme_err);
		gme_delete(emu);
		return;
	}

	if(ti->length > 0)
		song_len = ti->length / 1000.0;
	else song_len = -1;

	/* initialize the MPD decoder */

	GError *error = NULL;
	if(!audio_format_init_checked(&audio_format, sample_rate, SAMPLE_FORMAT_S16,
					  2, &error)){
		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);

	if((gme_err = gme_start_track(emu, track)) != NULL)
		g_warning("%s", gme_err);

	/* play */
	do {
		if((gme_err = gme_play(emu, GME_BUF_SIZE, buf)) != NULL){
			g_warning("%s", gme_err);
			return;
		}
		cmd = decoder_data(decoder, NULL, buf, sizeof(buf), 0);

		if(cmd == DECODE_COMMAND_SEEK) {
			float where = decoder_seek_where(decoder);
			if((gme_err = gme_seek(emu, (int)where*1000)) != NULL)
				g_warning("%s", gme_err);
			decoder_command_finished(decoder);
		}

		if(gme_track_ended(emu))
			break;
	} while(cmd != DECODE_COMMAND_STOP);

	gme_free_info(ti);
	gme_delete(emu);
}

static struct tag *
gme_tag_dup(const char *path_fs)
{
	int sample_rate = 44100;
	Music_Emu *emu;
	gme_info_t *ti;
	const char* gme_err;

	if((gme_err = gme_open_file(path_fs, &emu, sample_rate)) != NULL){
		g_warning("%s", gme_err);
		return NULL;
	}
	if((gme_err = gme_track_info(emu, &ti, 0)) != NULL){
		g_warning("%s", gme_err);
		gme_delete(emu);
		return NULL;
	}

	struct tag *tag = tag_new();
	if(ti != NULL){
		if(ti->length > 0)
			tag->time = ti->length / 1000;
		if(ti->song != NULL)
			tag_add_item(tag, TAG_TITLE, ti->song);
		if(ti->author != NULL)
			tag_add_item(tag, TAG_ARTIST, ti->author);
		if(ti->comment != NULL)
			tag_add_item(tag, TAG_COMMENT, ti->comment);
		if(ti->copyright != NULL)
			tag_add_item(tag, TAG_DATE, ti->copyright);
	}

	gme_free_info(ti);
	gme_delete(emu);
	return tag;
}

static const char *const gme_suffixes[] = {
	"ay", "gbs", "gym", "hes", "kss", "nsf",
	"nsfe", "sap", "spc", "vgm", "vgz",
	NULL
};

extern const struct decoder_plugin gme_decoder_plugin;
const struct decoder_plugin gme_decoder_plugin = {
	.name = "gme",
	.file_decode = gme_file_decode,
	.tag_dup = gme_tag_dup,
	.suffixes = gme_suffixes,
};