-
Notifications
You must be signed in to change notification settings - Fork 1
/
gme_interface.cpp
237 lines (202 loc) · 5.72 KB
/
gme_interface.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <emscripten.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "gme.h"
#include "player_interface.h"
#include "xzdec.h"
#define SAMPLES_PER_FRAME 2
typedef struct
{
int offset;
int size;
} fileEntry;
typedef struct
{
Music_Emu *emu;
uint8_t *dataBuffer;
int dataBufferSize;
int voiceCount;
int sampleRate;
int trackCount;
int isGameMusicArchive;
fileEntry *entries;
} gmeContext;
int crPlayerContextSize()
{
return sizeof(gmeContext);
}
int crPlayerInitialize(void *context, int sampleRate)
{
gmeContext *gme = (gmeContext*)context;
gme->emu = NULL;
gme->dataBuffer = NULL;
gme->dataBufferSize = 0;
gme->voiceCount = 0;
gme->sampleRate = sampleRate;
gme->trackCount = 0;
gme->isGameMusicArchive = 0;
gme->entries = NULL;
return 1;
}
int crPlayerLoadFile(void *context, const char *filename, unsigned char *data,
int size)
{
gmeContext *gme = (gmeContext*)context;
int i;
/* check if the data is compressed with XZ */
if ((data[0] == 0xFD) &&
(data[1] == '7') &&
(data[2] == 'z') &&
(data[3] == 'X') &&
(data[4] == 'Z'))
{
/* compressed with XZ-embedded; decompress */
if (!xz_decompress(data, size, &gme->dataBuffer, &gme->dataBufferSize))
{
free(gme->dataBuffer);
return 0;
}
}
else
{
/* not compressed; move to internal buffer */
gme->dataBufferSize = size;
gme->dataBuffer = (uint8_t*)malloc(gme->dataBufferSize);
if (!gme->dataBuffer)
return 0;
memcpy(gme->dataBuffer, data, gme->dataBufferSize);
}
/* check if this is a .gamemusic file */
if (strncmp((char *)gme->dataBuffer, "Game Music Files", 16) == 0)
{
/* it's a .gamemusic file; parse out a table of offsets and sizes */
gme->isGameMusicArchive = 1;
gme->trackCount =
(gme->dataBuffer[16] << 24) |
(gme->dataBuffer[17] << 16) |
(gme->dataBuffer[18] << 8) |
(gme->dataBuffer[19] << 0);
gme->entries = (fileEntry*)malloc(gme->trackCount * sizeof(fileEntry));
if (!gme->entries)
{
return 0;
}
/* gather up the offsets */
for (i = 0; i < gme->trackCount; i++)
{
gme->entries[i].offset =
(gme->dataBuffer[20 + i * 4 + 0] << 24) |
(gme->dataBuffer[20 + i * 4 + 1] << 16) |
(gme->dataBuffer[20 + i * 4 + 2] << 8) |
(gme->dataBuffer[20 + i * 4 + 3] << 0);
}
/* derive the sizes based on the offsets */
for (i = 0; i < gme->trackCount - 1; i++)
{
gme->entries[i].size =
gme->entries[i + 1].offset - gme->entries[i].offset;
}
/* last size takes data size into account */
gme->entries[gme->trackCount - 1].size =
gme->dataBufferSize - gme->entries[gme->trackCount - 1].offset;
}
return 1;
}
int crPlayerSetTrack(void *context, int track)
{
gmeContext *gme = (gmeContext*)context;
gme_err_t status = NULL;
int trueTrack;
fileEntry *entry;
/* initialize the engine */
if (gme->isGameMusicArchive)
{
/* when it's time to start a track, it's always going to be
* track 0 (first and only) of a single-song file */
trueTrack = 0;
/* if a file is already being played, free it first */
if (gme->emu)
{
gme_delete(gme->emu);
gme->emu = NULL;
}
entry = &gme->entries[track];
status = gme_open_data(gme->dataBuffer + entry->offset, entry->size,
&gme->emu, gme->sampleRate);
if (status)
return 0;
}
else
{
trueTrack = track;
/* if the player isn't already open, do the initialization */
if (!gme->emu)
{
status = gme_open_data(gme->dataBuffer, gme->dataBufferSize,
&gme->emu, gme->sampleRate);
if (status)
return 0;
}
}
/* set the track */
status = gme_start_track(gme->emu, trueTrack);
if (!status)
{
gme_type_t type = gme_type(gme->emu);
gme->voiceCount = gme_voice_count(gme->emu);
/* return either mono or stereo depending on the file type */
if ((type == gme_gbs_type) ||
(type == gme_spc_type) ||
(type == gme_vgm_type))
return 2; /* stereo */
else
return 1; /* mono */
}
else
{
gme_delete(gme->emu);
gme->emu = NULL;
return 0;
}
}
int crPlayerGenerateStereoFrames(void *context, int16_t *samples, int frameCount)
{
gmeContext *gme = (gmeContext*)context;
gme_err_t status;
status = gme_play(gme->emu, frameCount * SAMPLES_PER_FRAME, samples);
return (status == NULL);
}
int crPlayerVoicesCanBeToggled(void *context)
{
return 1;
}
int crPlayerGetVoiceCount(void *context)
{
gmeContext *gme = (gmeContext*)context;
return gme->voiceCount;
}
const char *crPlayerGetVoiceName(void *context, int voice)
{
gmeContext *gme = (gmeContext*)context;
return gme_voice_name(gme->emu, voice);
}
void crPlayerSetVoiceState(void *context, int voice, int enabled)
{
gmeContext *gme = (gmeContext*)context;
if (voice < gme->voiceCount)
gme_mute_voice(gme->emu, voice, !enabled);
}
void crPlayerCleanup(void *context)
{
gmeContext *gme = (gmeContext*)context;
if (gme->dataBuffer)
free(gme->dataBuffer);
gme->dataBuffer = NULL;
if (gme->entries)
free(gme->entries);
gme->entries = NULL;
if (gme->emu)
gme_delete(gme->emu);
gme->emu = NULL;
}