/
sys_audiod_sdlmixer.c
561 lines (452 loc) · 13.8 KB
/
sys_audiod_sdlmixer.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2009 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2007-2009 Daniel Swanson <danij@dengine.net>
*
* 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 St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* sys_audiod_sdlmixer.h: SDL_mixer, for SFX, Ext and Mus interfaces.
*/
// HEADER FILES ------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#ifdef MACOSX
# include <SDL.h>
# include <SDL_mixer.h>
#else
# include <SDL.h>
# include <SDL_mixer.h>
#endif
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_misc.h"
#include "sys_audiod.h"
#include "sys_audiod_sfx.h"
#include "sys_audiod_mus.h"
// MACROS ------------------------------------------------------------------
#define DEFAULT_MIDI_COMMAND "" //"timidity"
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
int DS_SDLMixerInit(void);
void DS_SDLMixerShutdown(void);
void DS_SDLMixerEvent(int type);
int DS_SDLMixer_SFX_Init(void);
sfxbuffer_t* DS_SDLMixer_SFX_CreateBuffer(int flags, int bits, int rate);
void DS_SDLMixer_SFX_DestroyBuffer(sfxbuffer_t* buf);
void DS_SDLMixer_SFX_Load(sfxbuffer_t* buf, struct sfxsample_s* sample);
void DS_SDLMixer_SFX_Reset(sfxbuffer_t* buf);
void DS_SDLMixer_SFX_Play(sfxbuffer_t* buf);
void DS_SDLMixer_SFX_Stop(sfxbuffer_t* buf);
void DS_SDLMixer_SFX_Refresh(sfxbuffer_t* buf);
void DS_SDLMixer_SFX_Set(sfxbuffer_t* buf, int prop, float value);
void DS_SDLMixer_SFX_Setv(sfxbuffer_t* buf, int prop, float* values);
void DS_SDLMixer_SFX_Listener(int prop, float value);
void DS_SDLMixer_SFX_Listenerv(int prop, float* values);
// The music interface.
int DS_SDLMixer_Music_Init(void);
void DS_SDLMixer_Music_Update(void);
void DS_SDLMixer_Music_Set(int prop, float value);
int DS_SDLMixer_Music_Get(int prop, void* value);
void DS_SDLMixer_Music_Pause(int pause);
void DS_SDLMixer_Music_Stop(void);
int DS_SDLMixer_Music_PlayFile(const char* fileName, int looped);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
boolean sdlInitOk = false;
audiodriver_t audiod_sdlmixer = {
DS_SDLMixerInit,
DS_SDLMixerShutdown,
DS_SDLMixerEvent
};
audiointerface_sfx_t audiod_sdlmixer_sfx = {
DS_SDLMixer_SFX_Init,
DS_SDLMixer_SFX_CreateBuffer,
DS_SDLMixer_SFX_DestroyBuffer,
DS_SDLMixer_SFX_Load,
DS_SDLMixer_SFX_Reset,
DS_SDLMixer_SFX_Play,
DS_SDLMixer_SFX_Stop,
DS_SDLMixer_SFX_Refresh,
DS_SDLMixer_SFX_Set,
DS_SDLMixer_SFX_Setv,
DS_SDLMixer_SFX_Listener,
DS_SDLMixer_SFX_Listenerv
};
audiointerface_music_t audiod_sdlmixer_music = {
DS_SDLMixer_Music_Init,
DS_SDLMixer_Music_Update,
DS_SDLMixer_Music_Set,
DS_SDLMixer_Music_Get,
DS_SDLMixer_Music_Pause,
DS_SDLMixer_Music_Stop,
NULL,
NULL,
DS_SDLMixer_Music_PlayFile,
};
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static int numChannels;
static boolean* usedChannels;
static Mix_Music* currentMusic;
// CODE --------------------------------------------------------------------
/**
* This is the hook we ask SDL_mixer to call when music playback finishes.
*/
static void musicPlaybackFinished(void)
{
if(currentMusic)
{
Mix_FreeMusic(currentMusic);
currentMusic = NULL;
}
}
static int getFreeChannel(void)
{
int i;
for(i = 0; i < numChannels; ++i)
{
if(!usedChannels[i])
return i;
}
return -1;
}
/**
* @return Length of the buffer in milliseconds.
*/
static unsigned int getBufLength(sfxbuffer_t* buf)
{
if(!buf)
return 0;
return 1000 * buf->sample->numSamples / buf->freq;
}
int DS_SDLMixerInit(void)
{
int freq, channels;
uint16_t format;
SDL_version compVer;
const SDL_version* linkVer;
if(sdlInitOk)
return true;
if(SDL_InitSubSystem(SDL_INIT_AUDIO))
{
Con_Message("DS_SDLMixerInit: Error initializing SDL AUDIO\n %s\n", SDL_GetError());
return false;
}
SDL_MIXER_VERSION(&compVer);
linkVer = Mix_Linked_Version();
if(SDL_VERSIONNUM(linkVer->major, linkVer->minor, linkVer->patch) >
SDL_VERSIONNUM(compVer.major, compVer.minor, compVer.patch))
{
Con_Message("DS_SDLMixerInit: Warning, linked version of SDLMixer (%u.%u.%u) is "
"newer than expected (%u.%u.%u)\n", linkVer->major, linkVer->minor,
linkVer->patch, compVer.major, compVer.minor, compVer.patch);
}
if(Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024))
{
Con_Message("DS_SDLMixerInit: Failed opening mixer %s.\n", Mix_GetError());
return false;
}
Mix_QuerySpec(&freq, &format, &channels);
// Announce capabilites:
Con_Printf("SDLMixer Configuration:\n");
Con_Printf(" Output: %s\n", channels > 1? "stereo" : "mono");
Con_Printf(" Format: %#x (%#x)\n", format, (uint16_t) AUDIO_S16LSB);
Con_Printf(" Frequency: %iHz (%iHz)\n", freq, (int) MIX_DEFAULT_FREQUENCY);
Con_Printf(" Initial Channels: %i\n", MIX_CHANNELS);
// Prepare to play simultaneous sounds.
/*numChannels =*/ Mix_AllocateChannels(MIX_CHANNELS);
usedChannels = NULL;
// Everything is OK.
sdlInitOk = true;
return true;
}
void DS_SDLMixerShutdown(void)
{
if(!sdlInitOk)
return;
if(usedChannels)
free(usedChannels);
if(currentMusic)
Mix_FreeMusic(currentMusic);
currentMusic = NULL;
Mix_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
sdlInitOk = false;
}
void DS_SDLMixerEvent(int type)
{
// Not supported.
}
int DS_SDLMixer_SFX_Init(void)
{
// No extra init needed.
return sdlInitOk;
}
sfxbuffer_t* DS_SDLMixer_SFX_CreateBuffer(int flags, int bits, int rate)
{
sfxbuffer_t* buf;
// Create the buffer.
buf = Z_Calloc(sizeof(*buf), PU_STATIC, 0);
buf->bytes = bits / 8;
buf->rate = rate;
buf->flags = flags;
buf->freq = rate; // Modified by calls to Set(SFXBP_FREQUENCY).
// The cursor is used to keep track of the channel on which the sample
// is playing.
if(-1 == (buf->cursor = getFreeChannel()))
{
buf->cursor = numChannels++;
usedChannels = realloc(usedChannels, sizeof(usedChannels[0]) * numChannels);
// Make sure we have enough channels allocated.
Mix_AllocateChannels(numChannels);
Mix_UnregisterAllEffects(buf->cursor);
}
usedChannels[buf->cursor] = true;
return buf;
}
void DS_SDLMixer_SFX_DestroyBuffer(sfxbuffer_t* buf)
{
Mix_HaltChannel(buf->cursor);
usedChannels[buf->cursor] = false;
if(buf)
Z_Free(buf);
}
void DS_SDLMixer_SFX_Load(sfxbuffer_t* buf, struct sfxsample_s* sample)
{
static char localBuf[0x40000];
char* conv = NULL;
size_t size;
if(!buf || !sample)
return; // Wha?
// Does the buffer already have a sample loaded?
if(buf->sample)
{
// Is the same one?
if(buf->sample->id == sample->id)
return;
// Free the existing data.
buf->sample = NULL;
Mix_FreeChunk(buf->ptr);
}
size = 8 + 4 + 8 + 16 + 8 + sample->size;
if(size <= sizeof(localBuf))
{
conv = localBuf;
}
else
{
conv = malloc(size);
}
// Transfer the sample to SDL_mixer by converting it to WAVE format.
strcpy(conv, "RIFF");
*(Uint32 *) (conv + 4) = ULONG(4 + 8 + 16 + 8 + sample->size);
strcpy(conv + 8, "WAVE");
// Format chunk.
strcpy(conv + 12, "fmt ");
*(Uint32 *) (conv + 16) = ULONG(16);
/**
* WORD wFormatTag; // Format category
* WORD wChannels; // Number of channels
* uint dwSamplesPerSec; // Sampling rate
* uint dwAvgBytesPerSec; // For buffer estimation
* WORD wBlockAlign; // Data block size
* WORD wBitsPerSample; // Sample size
*/
*(Uint16 *) (conv + 20) = USHORT(1);
*(Uint16 *) (conv + 22) = USHORT(1);
*(Uint32 *) (conv + 24) = ULONG(sample->rate);
*(Uint32 *) (conv + 28) = ULONG(sample->rate * sample->bytesPer);
*(Uint16 *) (conv + 32) = USHORT(sample->bytesPer);
*(Uint16 *) (conv + 34) = USHORT(sample->bytesPer * 8);
// Data chunk.
strcpy(conv + 36, "data");
*(Uint32 *) (conv + 40) = ULONG(sample->size);
memcpy(conv + 44, sample->data, sample->size);
buf->ptr = Mix_LoadWAV_RW(SDL_RWFromMem(conv, 44 + sample->size), 1);
if(!buf->ptr)
{
Con_Message("DS_SDLMixer_SFX_Load: Warning, failed loading sample (%s).\n",
Mix_GetError());
}
if(conv != localBuf)
{
free(conv);
}
buf->sample = sample;
}
/**
* Stops the buffer and makes it forget about its sample.
*/
void DS_SDLMixer_SFX_Reset(sfxbuffer_t* buf)
{
if(!buf)
return;
DS_SDLMixer_SFX_Stop(buf);
buf->sample = NULL;
// Unallocate the resources of the source.
Mix_FreeChunk(buf->ptr);
buf->ptr = NULL;
}
void DS_SDLMixer_SFX_Play(sfxbuffer_t* buf)
{
// Playing is quite impossible without a sample.
if(!buf || !buf->sample)
return;
// Update the volume at which the sample will be played.
Mix_Volume(buf->cursor, buf->written);
Mix_PlayChannel(buf->cursor, buf->ptr, (buf->flags & SFXBF_REPEAT ? -1 : 0));
// Calculate the end time (milliseconds).
buf->endTime = Sys_GetRealTime() + getBufLength(buf);
// The buffer is now playing.
buf->flags |= SFXBF_PLAYING;
}
void DS_SDLMixer_SFX_Stop(sfxbuffer_t* buf)
{
if(!buf || !buf->sample)
return;
Mix_HaltChannel(buf->cursor);
//usedChannels[buf->cursor] = false;
buf->flags &= ~SFXBF_PLAYING;
}
void DS_SDLMixer_SFX_Refresh(sfxbuffer_t* buf)
{
unsigned int nowTime;
// Can only be done if there is a sample and the buffer is playing.
if(!buf || !buf->sample || !(buf->flags & SFXBF_PLAYING))
return;
nowTime = Sys_GetRealTime();
/**
* Have we passed the predicted end of sample?
* \note This test fails if the game has been running for about 50 days,
* since the millisecond counter overflows. It only affects sounds that
* are playing while the overflow happens, though.
*/
if(!(buf->flags & SFXBF_REPEAT) && nowTime >= buf->endTime)
{
// Time for the sound to stop.
buf->flags &= ~SFXBF_PLAYING;
}
}
void DS_SDLMixer_SFX_Set(sfxbuffer_t* buf, int prop, float value)
{
int right;
if(!buf)
return;
switch(prop)
{
case SFXBP_VOLUME:
// 'written' is used for storing the volume of the channel.
buf->written = (unsigned int) (value * MIX_MAX_VOLUME);
Mix_Volume(buf->cursor, buf->written);
break;
case SFXBP_PAN: // -1 ... +1
right = (int) ((value + 1) * 127);
Mix_SetPanning(buf->cursor, 254 - right, right);
break;
default:
break;
}
}
void DS_SDLMixer_SFX_Setv(sfxbuffer_t* buf, int prop, float* values)
{
// Not supported.
}
void DS_SDLMixer_SFX_Listener(int prop, float value)
{
// Not supported.
}
void SetEnvironment(float* rev)
{
// Not supported.
}
void DS_SDLMixer_SFX_Listenerv(int prop, float* values)
{
// Not supported.
}
int DS_SDLMixer_Music_Init(void)
{
Mix_HookMusicFinished(musicPlaybackFinished);
return sdlInitOk;
}
void DS_SDLMixer_Music_Update(void)
{
// Nothing to update.
}
void DS_SDLMixer_Music_Set(int prop, float value)
{
if(!sdlInitOk)
return;
switch(prop)
{
case MUSIP_VOLUME:
Mix_VolumeMusic((int) (MIX_MAX_VOLUME * value));
break;
default:
break;
}
}
int DS_SDLMixer_Music_Get(int prop, void* value)
{
if(!sdlInitOk)
return false;
switch(prop)
{
case MUSIP_ID:
strcpy(value, "SDLMixer/Music");
break;
case MUSIP_PLAYING:
return (currentMusic? true : false);
default:
return false;
}
return true;
}
void DS_SDLMixer_Music_Pause(int pause)
{
if(!sdlInitOk)
return;
if(pause)
Mix_PauseMusic();
else
Mix_ResumeMusic();
}
void DS_SDLMixer_Music_Stop(void)
{
if(!sdlInitOk)
return;
Mix_HaltMusic();
}
int DS_SDLMixer_Music_PlayFile(const char* filename, int looped)
{
if(!sdlInitOk)
return false;
Mix_SetMusicCMD(NULL);
// Free any previously loaded music.
if(currentMusic)
Mix_FreeMusic(currentMusic);
if(!(currentMusic = Mix_LoadMUS(filename)))
{
Con_Message("DS_SDLMixer_Music_PlayFile: Error %s.\n", Mix_GetError());
return false;
}
return !Mix_PlayMusic(currentMusic, looped ? -1 : 1);
}