-
Notifications
You must be signed in to change notification settings - Fork 0
/
music_mod.c
346 lines (305 loc) · 7.57 KB
/
music_mod.c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
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.
*/
/* $Id: music_mod.c 4211 2008-12-08 00:27:32Z slouken $ */
#ifdef MOD_MUSIC
/* This file supports MOD tracker music streams */
#include "SDL_mixer.h"
#include "dynamic_mod.h"
#include "music_mod.h"
#include "mikmod.h"
#define SDL_SURROUND
#ifdef SDL_SURROUND
#define MAX_OUTPUT_CHANNELS 6
#else
#define MAX_OUTPUT_CHANNELS 2
#endif
/* Reference for converting mikmod output to 4/6 channels */
static int current_output_channels;
static Uint16 current_output_format;
static int music_swap8;
static int music_swap16;
/* Initialize the MOD player, with the given mixer settings
This function returns 0, or -1 if there was an error.
*/
int MOD_init(SDL_AudioSpec *mixerfmt)
{
CHAR *list;
if ( !Mix_Init(MIX_INIT_MOD) ) {
return -1;
}
/* Set the MikMod music format */
music_swap8 = 0;
music_swap16 = 0;
switch (mixerfmt->format) {
case AUDIO_U8:
case AUDIO_S8: {
if ( mixerfmt->format == AUDIO_S8 ) {
music_swap8 = 1;
}
*mikmod.md_mode = 0;
}
break;
case AUDIO_S16LSB:
case AUDIO_S16MSB: {
/* See if we need to correct MikMod mixing */
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
if ( mixerfmt->format == AUDIO_S16MSB ) {
#else
if ( mixerfmt->format == AUDIO_S16LSB ) {
#endif
music_swap16 = 1;
}
*mikmod.md_mode = DMODE_16BITS;
}
break;
default: {
Mix_SetError("Unknown hardware audio format");
return -1;
}
}
current_output_channels = mixerfmt->channels;
current_output_format = mixerfmt->format;
if ( mixerfmt->channels > 1 ) {
if ( mixerfmt->channels > MAX_OUTPUT_CHANNELS ) {
Mix_SetError("Hardware uses more channels than mixerfmt");
return -1;
}
*mikmod.md_mode |= DMODE_STEREO;
}
*mikmod.md_mixfreq = mixerfmt->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();
if ( list )
free(list);
else
mikmod.MikMod_RegisterDriver(mikmod.drv_nos);
list = mikmod.MikMod_InfoLoader();
if ( list )
free(list);
else
mikmod.MikMod_RegisterAllLoaders();
if ( mikmod.MikMod_Init(NULL) ) {
Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
return -1;
}
return 0;
}
/* Uninitialize the music players */
void MOD_exit(void)
{
if (mikmod.MikMod_Exit) {
mikmod.MikMod_Exit();
}
}
/* Set the volume for a MOD stream */
void MOD_setvolume(MODULE *music, int volume)
{
mikmod.Player_SetVolume((SWORD)volume);
}
typedef struct
{
MREADER mr;
long offset;
long eof;
SDL_RWops *rw;
} LMM_MREADER;
BOOL LMM_Seek(struct MREADER *mr,long to,int dir)
{
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
if ( dir == SEEK_SET ) {
to += lmmmr->offset;
}
return (SDL_RWseek(lmmmr->rw, to, dir) < lmmmr->offset);
}
long LMM_Tell(struct MREADER *mr)
{
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
return SDL_RWtell(lmmmr->rw) - lmmmr->offset;
}
BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
{
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
return SDL_RWread(lmmmr->rw, buf, sz, 1);
}
int LMM_Get(struct MREADER *mr)
{
unsigned char c;
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
if ( SDL_RWread(lmmmr->rw, &c, 1, 1) ) {
return c;
}
return EOF;
}
BOOL LMM_Eof(struct MREADER *mr)
{
long offset;
LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
offset = LMM_Tell(mr);
return offset >= lmmmr->eof;
}
MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan)
{
LMM_MREADER lmmmr = {
{ LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof },
0,
0,
0
};
lmmmr.offset = SDL_RWtell(rw);
SDL_RWseek(rw, 0, RW_SEEK_END);
lmmmr.eof = SDL_RWtell(rw);
SDL_RWseek(rw, lmmmr.offset, RW_SEEK_SET);
lmmmr.rw = rw;
return mikmod.Player_LoadGeneric((MREADER*)&lmmmr, maxchan, 0);
}
/* Load a MOD stream from an SDL_RWops object */
MODULE *MOD_new_RW(SDL_RWops *rw, int freerw)
{
MODULE *module;
/* Make sure the mikmod library is loaded */
if ( !Mix_Init(MIX_INIT_MOD) ) {
if ( freerw ) {
SDL_RWclose(rw);
}
return NULL;
}
module = MikMod_LoadSongRW(rw,64);
if (!module) {
Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
if ( freerw ) {
SDL_RWclose(rw);
}
return NULL;
}
/* Stop implicit looping, fade out and other flags. */
module->extspd = 1;
module->panflag = 1;
module->wrap = 0;
module->loop = 0;
#if 0 /* Don't set fade out by default - unfortunately there's no real way
to query the status of the song or set trigger actions. Hum. */
module->fadeout = 1;
#endif
if ( freerw ) {
SDL_RWclose(rw);
}
return module;
}
/* Start playback of a given MOD stream */
void MOD_play(MODULE *music)
{
mikmod.Player_Start(music);
}
/* Return non-zero if a stream is currently playing */
int MOD_playing(MODULE *music)
{
return mikmod.Player_Active();
}
/* Play some of a stream previously started with MOD_play() */
int MOD_playAudio(MODULE *music, Uint8 *stream, int len)
{
if (current_output_channels > 2) {
int small_len = 2 * len / current_output_channels;
int i;
Uint8 *src, *dst;
mikmod.VC_WriteBytes((SBYTE *)stream, small_len);
/* and extend to len by copying channels */
src = stream + small_len;
dst = stream + len;
switch (current_output_format & 0xFF) {
case 8:
for ( i=small_len/2; i; --i ) {
src -= 2;
dst -= current_output_channels;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[0];
dst[3] = src[1];
if (current_output_channels == 6) {
dst[4] = src[0];
dst[5] = src[1];
}
}
break;
case 16:
for ( i=small_len/4; i; --i ) {
src -= 4;
dst -= 2 * current_output_channels;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
dst[4] = src[0];
dst[5] = src[1];
dst[6] = src[2];
dst[7] = src[3];
if (current_output_channels == 6) {
dst[8] = src[0];
dst[9] = src[1];
dst[10] = src[2];
dst[11] = src[3];
}
}
break;
}
} else {
mikmod.VC_WriteBytes((SBYTE *)stream, len);
}
if ( music_swap8 ) {
Uint8 *dst;
int i;
dst = stream;
for ( i=len; i; --i ) {
*dst++ ^= 0x80;
}
} else
if ( music_swap16 ) {
Uint8 *dst, tmp;
int i;
dst = stream;
for ( i=(len/2); i; --i ) {
tmp = dst[0];
dst[0] = dst[1];
dst[1] = tmp;
dst += 2;
}
}
return 0;
}
/* Stop playback of a stream previously started with MOD_play() */
void MOD_stop(MODULE *music)
{
mikmod.Player_Stop();
}
/* Close the given MOD stream */
void MOD_delete(MODULE *music)
{
mikmod.Player_Free(music);
}
/* Jump (seek) to a given position (time is in seconds) */
void MOD_jump_to_time(MODULE *music, double time)
{
mikmod.Player_SetPosition((UWORD)time);
}
#endif /* MOD_MUSIC */