/
music_mikmod.c
488 lines (435 loc) · 13.4 KB
1
2
/*
SDL_mixer: An audio mixer library based on the SDL library
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifdef MUSIC_MOD_MIKMOD
/* This file supports MOD tracker music streams */
26
27
#include "SDL_loadso.h"
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
#include "music_mikmod.h"
#include "mikmod.h"
/* libmikmod >= 3.3.2 constified several funcs */
#if (LIBMIKMOD_VERSION < 0x030302)
#define MIKMOD3_CONST
#else
#define MIKMOD3_CONST const
#endif
typedef struct {
int loaded;
void *handle;
void (*MikMod_Exit)(void);
CHAR* (*MikMod_InfoDriver)(void);
CHAR* (*MikMod_InfoLoader)(void);
int (*MikMod_Init)(MIKMOD3_CONST CHAR*);
void (*MikMod_RegisterAllLoaders)(void);
void (*MikMod_RegisterDriver)(struct MDRIVER*);
int* MikMod_errno;
MIKMOD3_CONST char* (*MikMod_strerror)(int);
void (*MikMod_free)(void*);
BOOL (*Player_Active)(void);
void (*Player_Free)(MODULE*);
MODULE* (*Player_LoadGeneric)(MREADER*,int,BOOL);
void (*Player_SetPosition)(UWORD);
void (*Player_SetVolume)(SWORD);
void (*Player_Start)(MODULE*);
void (*Player_Stop)(void);
ULONG (*VC_WriteBytes)(SBYTE*,ULONG);
struct MDRIVER* drv_nos;
UWORD* md_device;
UWORD* md_mixfreq;
UWORD* md_mode;
UBYTE* md_musicvolume;
UBYTE* md_pansep;
UBYTE* md_reverb;
UBYTE* md_sndfxvolume;
UBYTE* md_volume;
} mikmod_loader;
static mikmod_loader mikmod = {
0, NULL
};
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifdef MIKMOD_DYNAMIC
#define FUNCTION_LOADER(FUNC, SIG) \
mikmod.FUNC = (SIG) SDL_LoadFunction(mikmod.handle, #FUNC); \
if (mikmod.FUNC == NULL) { SDL_UnloadObject(mikmod.handle); return -1; }
#define VARIABLE_LOADER(NAME, SIG) \
mikmod.NAME = (SIG) SDL_LoadFunction(mikmod.handle, #NAME); \
if (mikmod.NAME == NULL) { SDL_UnloadObject(mikmod.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
mikmod.FUNC = FUNC;
#define VARIABLE_LOADER(NAME, SIG) \
mikmod.NAME = &NAME;
#endif
89
90
91
92
static int MIKMOD_Load()
{
if (mikmod.loaded == 0) {
93
94
#ifdef MIKMOD_DYNAMIC
mikmod.handle = SDL_LoadObject(MIKMOD_DYNAMIC);
95
96
97
if (mikmod.handle == NULL) {
return -1;
}
98
#elif defined(__MACOSX__)
99
100
101
102
103
104
105
extern void Player_Start(MODULE*) __attribute__((weak_import));
if (Player_Start == NULL)
{
/* Missing weakly linked framework */
Mix_SetError("Missing mikmod.framework");
return -1;
}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#endif
FUNCTION_LOADER(MikMod_Exit, void (*)(void))
FUNCTION_LOADER(MikMod_InfoDriver, CHAR* (*)(void))
FUNCTION_LOADER(MikMod_InfoLoader, CHAR* (*)(void))
FUNCTION_LOADER(MikMod_Init, int (*)(MIKMOD3_CONST CHAR*))
FUNCTION_LOADER(MikMod_RegisterAllLoaders, void (*)(void))
FUNCTION_LOADER(MikMod_RegisterDriver, void (*)(struct MDRIVER*))
VARIABLE_LOADER(MikMod_errno, int*)
FUNCTION_LOADER(MikMod_strerror, MIKMOD3_CONST char* (*)(int))
#ifdef MIKMOD_DYNAMIC
mikmod.MikMod_free = (void (*)(void*)) SDL_LoadFunction(mikmod.handle, "MikMod_free");
if (!mikmod.MikMod_free) {
/* libmikmod 3.1 and earlier doesn't have it */
mikmod.MikMod_free = free;
}
#else
122
123
#if (LIBMIKMOD_VERSION < 0x030200) || !defined(DMODE_NOISEREDUCTION)
/* libmikmod 3.2.0-beta2 or older */
124
125
126
127
mikmod.MikMod_free = free;
#else
mikmod.MikMod_free = MikMod_free;
#endif
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#endif /* MIKMOD_DYNAMIC */
FUNCTION_LOADER(Player_Active, BOOL (*)(void))
FUNCTION_LOADER(Player_Free, void (*)(MODULE*))
FUNCTION_LOADER(Player_LoadGeneric, MODULE* (*)(MREADER*,int,BOOL))
FUNCTION_LOADER(Player_SetPosition, void (*)(UWORD))
FUNCTION_LOADER(Player_SetVolume, void (*)(SWORD))
FUNCTION_LOADER(Player_Start, void (*)(MODULE*))
FUNCTION_LOADER(Player_Stop, void (*)(void))
FUNCTION_LOADER(VC_WriteBytes, ULONG (*)(SBYTE*,ULONG))
VARIABLE_LOADER(drv_nos, MDRIVER*)
VARIABLE_LOADER(md_device, UWORD*)
VARIABLE_LOADER(md_mixfreq, UWORD*)
VARIABLE_LOADER(md_mode, UWORD*)
VARIABLE_LOADER(md_musicvolume, UBYTE*)
VARIABLE_LOADER(md_pansep, UBYTE*)
VARIABLE_LOADER(md_reverb, UBYTE*)
VARIABLE_LOADER(md_sndfxvolume, UBYTE*)
VARIABLE_LOADER(md_volume, UBYTE*)
146
147
148
149
150
151
152
153
154
155
156
157
}
++mikmod.loaded;
return 0;
}
static void MIKMOD_Unload()
{
if (mikmod.loaded == 0) {
return;
}
if (mikmod.loaded == 1) {
158
159
160
#ifdef MIKMOD_DYNAMIC
SDL_UnloadObject(mikmod.handle);
#endif
161
162
163
164
165
}
--mikmod.loaded;
}
166
167
168
169
170
171
172
173
174
typedef struct
{
int play_count;
int volume;
MODULE *module;
SDL_AudioStream *stream;
SBYTE *buffer;
ULONG buffer_size;
} MIKMOD_Music;
175
176
177
178
static int MIKMOD_Seek(void *context, double position);
static void MIKMOD_Delete(void *context);
179
180
181
182
183
184
185
186
187
/* Initialize the MOD player, with the given mixer settings
This function returns 0, or -1 if there was an error.
*/
static int MIKMOD_Open(const SDL_AudioSpec *spec)
{
CHAR *list;
/* Set the MikMod music format */
188
189
190
191
192
193
if (spec->format == AUDIO_S8 || spec->format == AUDIO_U8) {
/* MIKMOD audio format is AUDIO_U8 */
*mikmod.md_mode = 0;
} else {
/* MIKMOD audio format is AUDIO_S16SYS */
*mikmod.md_mode = DMODE_16BITS;
194
195
196
197
198
199
200
201
202
203
204
205
206
207
}
if (spec->channels > 1) {
*mikmod.md_mode |= DMODE_STEREO;
}
*mikmod.md_mixfreq = spec->freq;
*mikmod.md_device = 0;
*mikmod.md_volume = 96;
*mikmod.md_musicvolume = 128;
*mikmod.md_sndfxvolume = 128;
*mikmod.md_pansep = 128;
*mikmod.md_reverb = 0;
*mikmod.md_mode |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND;
list = mikmod.MikMod_InfoDriver();
208
if (list) {
209
mikmod.MikMod_free(list);
210
} else {
211
mikmod.MikMod_RegisterDriver(mikmod.drv_nos);
212
}
213
214
list = mikmod.MikMod_InfoLoader();
215
if (list) {
216
mikmod.MikMod_free(list);
217
} else {
218
mikmod.MikMod_RegisterAllLoaders();
219
}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
if (mikmod.MikMod_Init(NULL)) {
Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
return -1;
}
return 0;
}
/* Uninitialize the music players */
static void MIKMOD_Close(void)
{
if (mikmod.MikMod_Exit) {
mikmod.MikMod_Exit();
}
}
typedef struct
{
MREADER mr;
/* struct MREADER in libmikmod <= 3.2.0-beta2
* doesn't have iobase members. adding them here
* so that if we compile against 3.2.0-beta2, we
* can still run OK against 3.2.0b3 and newer. */
long iobase, prev_iobase;
Sint64 offset;
Sint64 eof;
SDL_RWops *src;
} LMM_MREADER;
int LMM_Seek(struct MREADER *mr,long to,int dir)
{
251
Sint64 offset = to;
252
253
254
255
256
257
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
if (dir == SEEK_SET) {
offset += lmmmr->offset;
if (offset < lmmmr->offset)
return -1;
}
258
return (SDL_RWseek(lmmmr->src, offset, dir) < lmmmr->offset)? -1 : 0;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
}
long LMM_Tell(struct MREADER *mr)
{
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
return (long)(SDL_RWtell(lmmmr->src) - lmmmr->offset);
}
BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
{
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
return SDL_RWread(lmmmr->src, buf, sz, 1);
}
int LMM_Get(struct MREADER *mr)
{
unsigned char c;
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
if (SDL_RWread(lmmmr->src, &c, 1, 1)) {
return c;
}
return EOF;
}
BOOL LMM_Eof(struct MREADER *mr)
{
Sint64 offset;
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
offset = LMM_Tell(mr);
return offset >= lmmmr->eof;
}
MODULE *MikMod_LoadSongRW(SDL_RWops *src, int maxchan)
{
LMM_MREADER lmmmr = {
{ LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof },
0,
0,
0
};
lmmmr.offset = SDL_RWtell(src);
SDL_RWseek(src, 0, RW_SEEK_END);
lmmmr.eof = SDL_RWtell(src);
SDL_RWseek(src, lmmmr.offset, RW_SEEK_SET);
lmmmr.src = src;
return mikmod.Player_LoadGeneric((MREADER*)&lmmmr, maxchan, 0);
}
/* Load a MOD stream from an SDL_RWops object */
void *MIKMOD_CreateFromRW(SDL_RWops *src, int freesrc)
{
305
306
307
MIKMOD_Music *music;
SDL_AudioFormat format;
Uint8 channels;
308
309
310
311
312
313
314
315
316
317
music = (MIKMOD_Music *)SDL_calloc(1, sizeof(*music));
if (!music) {
SDL_OutOfMemory();
return NULL;
}
music->volume = MIX_MAX_VOLUME;
music->module = MikMod_LoadSongRW(src, 64);
if (!music->module) {
318
Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
319
MIKMOD_Delete(music);
320
321
322
return NULL;
}
323
/* Allow implicit looping, disable fade out and other flags. */
324
325
326
music->module->extspd = 1;
music->module->panflag = 1;
music->module->wrap = 0;
327
328
music->module->loop = 1;
music->module->fadeout = 0;
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
if ((*mikmod.md_mode & DMODE_16BITS) == DMODE_16BITS) {
format = AUDIO_S16SYS;
} else {
format = AUDIO_U8;
}
if ((*mikmod.md_mode & DMODE_STEREO) == DMODE_STEREO) {
channels = 2;
} else {
channels = 1;
}
music->stream = SDL_NewAudioStream(format, channels, *mikmod.md_mixfreq,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
MIKMOD_Delete(music);
return NULL;
}
music->buffer_size = music_spec.samples * (SDL_AUDIO_BITSIZE(format) / 8) * channels;
music->buffer = (SBYTE *)SDL_malloc(music->buffer_size);
if (!music->buffer) {
SDL_OutOfMemory();
MIKMOD_Delete(music);
return NULL;
}
355
356
357
if (freesrc) {
SDL_RWclose(src);
}
358
return music;
359
360
361
362
363
}
/* Set the volume for a MOD stream */
static void MIKMOD_SetVolume(void *context, int volume)
{
364
365
MIKMOD_Music *music = (MIKMOD_Music *)context;
music->volume = volume;
366
367
368
369
mikmod.Player_SetVolume((SWORD)volume);
}
/* Start playback of a given MOD stream */
370
static int MIKMOD_Play(void *context, int play_count)
371
{
372
373
MIKMOD_Music *music = (MIKMOD_Music *)context;
music->play_count = play_count;
374
music->module->initvolume = music->volume;
375
376
mikmod.Player_Start(music->module);
return MIKMOD_Seek(music, 0.0);
377
378
379
380
381
382
383
384
385
}
/* Return non-zero if a stream is currently playing */
static SDL_bool MIKMOD_IsPlaying(void *context)
{
return mikmod.Player_Active() ? SDL_TRUE : SDL_FALSE;
}
/* Play some of a stream previously started with MOD_play() */
386
static int MIKMOD_GetSome(void *context, void *data, int bytes, SDL_bool *done)
387
{
388
389
390
391
392
393
MIKMOD_Music *music = (MIKMOD_Music *)context;
int filled;
filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
394
395
}
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
return 0;
}
/* This never fails, and always writes a full buffer */
mikmod.VC_WriteBytes(music->buffer, music->buffer_size);
if (SDL_AudioStreamPut(music->stream, music->buffer, music->buffer_size) < 0) {
return -1;
}
/* Check to see if we're done now */
if (!mikmod.Player_Active()) {
if (music->play_count == 1) {
music->play_count = 0;
SDL_AudioStreamFlush(music->stream);
} else {
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (MIKMOD_Play(music, play_count) < 0) {
return -1;
}
422
423
424
425
}
}
return 0;
}
426
427
428
429
static int MIKMOD_GetAudio(void *context, void *data, int bytes)
{
return music_pcm_getaudio(context, data, bytes, MIX_MAX_VOLUME, MIKMOD_GetSome);
}
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* Jump (seek) to a given position (time is in seconds) */
static int MIKMOD_Seek(void *context, double position)
{
mikmod.Player_SetPosition((UWORD)position);
return 0;
}
/* Stop playback of a stream previously started with MOD_play() */
static void MIKMOD_Stop(void *context)
{
mikmod.Player_Stop();
}
/* Close the given MOD stream */
static void MIKMOD_Delete(void *context)
{
447
448
449
450
451
452
453
454
455
456
457
458
MIKMOD_Music *music = (MIKMOD_Music *)context;
if (music->module) {
mikmod.Player_Free(music->module);
}
if (music->stream) {
SDL_FreeAudioStream(music->stream);
}
if (music->buffer) {
SDL_free(music->buffer);
}
SDL_free(music);
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
}
Mix_MusicInterface Mix_MusicInterface_MIKMOD =
{
"MIKMOD",
MIX_MUSIC_MIKMOD,
MUS_MOD,
SDL_FALSE,
SDL_FALSE,
MIKMOD_Load,
MIKMOD_Open,
MIKMOD_CreateFromRW,
NULL, /* CreateFromFile */
MIKMOD_SetVolume,
MIKMOD_Play,
MIKMOD_IsPlaying,
MIKMOD_GetAudio,
MIKMOD_Seek,
NULL, /* Pause */
NULL, /* Resume */
480
MIKMOD_Stop,
481
482
483
484
485
486
487
488
MIKMOD_Delete,
MIKMOD_Close,
MIKMOD_Unload,
};
#endif /* MUSIC_MOD_MIKMOD */
/* vi: set ts=4 sw=4 expandtab: */