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
|
/*
* Simple-SDL2-Audio
*
* Copyright 2016 Jake Besworth
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <SDL2/SDL.h>
#include "audio.h"
/*
* Native WAVE format
*
* On some GNU/Linux you can identify a files properties using:
* mplayer -identify music.wav
*
* On some GNU/Linux to convert any music to this or another specified format use:
* ffmpeg -i in.mp3 -acodec pcm_s16le -ac 2 -ar 48000 out.wav
*/
/* SDL_AudioFormat of files, such as s16 little endian */
#define AUDIO_FORMAT AUDIO_S16LSB
/* Frequency of the file */
#define AUDIO_FREQUENCY 48000
/* 1 mono, 2 stereo, 4 quad, 6 (5.1) */
#define AUDIO_CHANNELS 2
/* Specifies a unit of audio data to be used at a time. Must be a power of 2 */
#define AUDIO_SAMPLES 4096
/* Max number of sounds that can be in the audio queue at anytime, stops too much mixing */
#define AUDIO_MAX_SOUNDS 25
/* Flags OR'd together, which specify how SDL should behave when a device cannot offer a specific feature
* If flag is set, SDL will change the format in the actual audio file structure (as opposed to gDevice->want)
*
* Note: If you're having issues with Emscripten / EMCC play around with these flags
*
* 0 Allow no changes
* SDL_AUDIO_ALLOW_FREQUENCY_CHANGE Allow frequency changes (e.g. AUDIO_FREQUENCY is 48k, but allow files to play at 44.1k
* SDL_AUDIO_ALLOW_FORMAT_CHANGE Allow Format change (e.g. AUDIO_FORMAT may be S32LSB, but allow wave files of S16LSB to play)
* SDL_AUDIO_ALLOW_CHANNELS_CHANGE Allow any number of channels (e.g. AUDIO_CHANNELS being 2, allow actual 1)
* SDL_AUDIO_ALLOW_ANY_CHANGE Allow all changes above
*/
#define SDL_AUDIO_ALLOW_CHANGES SDL_AUDIO_ALLOW_ANY_CHANGE
/*
* Definition for the game global sound device
*
*/
typedef struct privateAudioDevice
{
SDL_AudioDeviceID device;
SDL_AudioSpec want;
uint8_t audioEnabled;
} PrivateAudioDevice;
/* File scope variables to persist data */
static PrivateAudioDevice * gDevice;
static uint32_t gSoundCount;
/*
* Add a music to the queue, addAudio wrapper for music due to fade
*
* @param new New Audio to add
*
*/
static void addMusic(Audio * root, Audio * news);
/*
* Wrapper function for playMusic, playSound, playMusicFromMemory, playSoundFromMemory
*
* @param filename Provide a filename to load WAV from, or NULL if using FromMemory
* @param audio Provide an Audio object if copying from memory, or NULL if using a filename
* @param sound 1 if looping (music), 0 otherwise (sound)
* @param volume See playSound for explanation
*
*/
static inline void playAudio(const char * filename, Audio * audio, uint8_t loop, int volume);
/*
* Add a sound to the end of the queue
*
* @param root Root of queue
* @param new New Audio to add
*
*/
static void addAudio(Audio * root, Audio * news);
/*
* Audio callback function for OpenAudioDevice
*
* @param userdata Points to linked list of sounds to play, first being a placeholder
* @param stream Stream to mix sound into
* @param len Length of sound to play
*
*/
static inline void audioCallback(void * userdata, uint8_t * stream, int len);
void playSound(const char * filename, int volume)
{
playAudio(filename, NULL, 0, volume);
}
void playMusic(const char * filename, int volume)
{
playAudio(filename, NULL, 1, volume);
}
void playSoundFromMemory(Audio * audio, int volume)
{
playAudio(NULL, audio, 0, volume);
}
void playMusicFromMemory(Audio * audio, int volume)
{
playAudio(NULL, audio, 1, volume);
}
void initAudio(void)
{
Audio * global;
gDevice = (PrivateAudioDevice*)calloc(1, sizeof(PrivateAudioDevice));
gSoundCount = 0;
if(gDevice == NULL)
{
fprintf(stderr, "[%s: %d]Fatal Error: Memory c-allocation error\n", __FILE__, __LINE__);
return;
}
gDevice->audioEnabled = 0;
if(!(SDL_WasInit(SDL_INIT_AUDIO) & SDL_INIT_AUDIO))
{
fprintf(stderr, "[%s: %d]Error: SDL_INIT_AUDIO not initialized\n", __FILE__, __LINE__);
return;
}
SDL_memset(&(gDevice->want), 0, sizeof(gDevice->want));
(gDevice->want).freq = AUDIO_FREQUENCY;
(gDevice->want).format = AUDIO_FORMAT;
(gDevice->want).channels = AUDIO_CHANNELS;
(gDevice->want).samples = AUDIO_SAMPLES;
(gDevice->want).callback = audioCallback;
(gDevice->want).userdata = calloc(1, sizeof(Audio));
global = (Audio*)(gDevice->want).userdata;
if(global == NULL)
{
fprintf(stderr, "[%s: %d]Error: Memory allocation error\n", __FILE__, __LINE__);
return;
}
global->buffer = NULL;
global->next = NULL;
/* want.userdata = new; */
if((gDevice->device = SDL_OpenAudioDevice(NULL, 0, &(gDevice->want), NULL, SDL_AUDIO_ALLOW_CHANGES)) == 0)
{
fprintf(stderr, "[%s: %d]Warning: failed to open audio device: %s\n", __FILE__, __LINE__, SDL_GetError());
}
else
{
/* Set audio device enabled global flag */
gDevice->audioEnabled = 1;
/* Unpause active audio stream */
unpauseAudio();
}
}
void endAudio(void)
{
if(gDevice->audioEnabled)
{
pauseAudio();
freeAudio((Audio *) (gDevice->want).userdata);
/* Close down audio */
SDL_CloseAudioDevice(gDevice->device);
}
free(gDevice);
}
void pauseAudio(void)
{
if(gDevice->audioEnabled)
{
SDL_PauseAudioDevice(gDevice->device, 1);
}
}
void unpauseAudio(void)
{
if(gDevice->audioEnabled)
{
SDL_PauseAudioDevice(gDevice->device, 0);
}
}
void freeAudio(Audio * audio)
{
Audio * temp;
while(audio != NULL)
{
if(audio->free == 1)
{
SDL_FreeWAV(audio->bufferTrue);
}
temp = audio;
audio = audio->next;
free(temp);
}
}
Audio * createAudio(const char * filename, uint8_t loop, int volume)
{
Audio * news = (Audio*)calloc(1, sizeof(Audio));
if(news == NULL)
{
fprintf(stderr, "[%s: %d]Error: Memory allocation error\n", __FILE__, __LINE__);
return NULL;
}
if(filename == NULL)
{
fprintf(stderr, "[%s: %d]Warning: filename NULL: %s\n", __FILE__, __LINE__, filename);
return NULL;
}
news->next = NULL;
news->loop = loop;
news->fade = 0;
news->free = 1;
news->volume = volume;
if(SDL_LoadWAV(filename, &(news->audio), &(news->bufferTrue), &(news->lengthTrue)) == NULL)
{
fprintf(stderr, "[%s: %d]Warning: failed to open wave file: %s error: %s\n", __FILE__, __LINE__, filename, SDL_GetError());
free(news);
return NULL;
}
news->buffer = news->bufferTrue;
news->length = news->lengthTrue;
(news->audio).callback = NULL;
(news->audio).userdata = NULL;
return news;
}
static inline void playAudio(const char * filename, Audio * audio, uint8_t loop, int volume)
{
Audio * news;
/* Check if audio is enabled */
if(!gDevice->audioEnabled)
{
return;
}
/* If sound, check if under max number of sounds allowed, else don't play */
if(loop == 0)
{
if(gSoundCount >= AUDIO_MAX_SOUNDS)
{
return;
}
else
{
gSoundCount++;
}
}
/* Load from filename or from Memory */
if(filename != NULL)
{
/* Create new music sound with loop */
news = createAudio(filename, loop, volume);
}
else if(audio != NULL)
{
news = (Audio*)malloc(sizeof(Audio));
if(news == NULL)
{
fprintf(stderr, "[%s: %d]Fatal Error: Memory allocation error\n", __FILE__, __LINE__);
return;
}
memcpy(news, audio, sizeof(Audio));
news->volume = volume;
news->loop = loop;
news->free = 0;
}
else
{
fprintf(stderr, "[%s: %d]Warning: filename and Audio parameters NULL\n", __FILE__, __LINE__);
return;
}
/* Lock callback function */
SDL_LockAudioDevice(gDevice->device);
if(loop == 1)
{
addMusic((Audio *) (gDevice->want).userdata, news);
}
else
{
addAudio((Audio *) (gDevice->want).userdata, news);
}
SDL_UnlockAudioDevice(gDevice->device);
}
static void addMusic(Audio * root, Audio * news)
{
uint8_t musicFound = 0;
Audio * rootNext = root->next;
/* Find any existing musics, 0, 1 or 2 and fade them out */
while(rootNext != NULL)
{
/* Phase out any current music */
if(rootNext->loop == 1 && rootNext->fade == 0)
{
if(musicFound)
{
rootNext->length = 0;
rootNext->volume = 0;
}
rootNext->fade = 1;
}
/* Set flag to remove any queued up music in favour of new music */
else if(rootNext->loop == 1 && rootNext->fade == 1)
{
musicFound = 1;
}
rootNext = rootNext->next;
}
addAudio(root, news);
}
static inline void audioCallback(void * userdata, uint8_t * stream, int len)
{
Audio * audio = (Audio *) userdata;
Audio * previous = audio;
int tempLength;
uint8_t music = 0;
/* Silence the main buffer */
SDL_memset(stream, 0, len);
/* First one is place holder */
audio = audio->next;
while(audio != NULL)
{
if(audio->length > 0)
{
if(audio->fade == 1 && audio->loop == 1)
{
music = 1;
if(audio->volume > 0)
{
audio->volume--;
}
else
{
audio->length = 0;
}
}
if(music && audio->loop == 1 && audio->fade == 0)
{
tempLength = 0;
}
else
{
tempLength = ((uint32_t) len > audio->length) ? audio->length : (uint32_t) len;
}
SDL_MixAudioFormat(stream, audio->buffer, AUDIO_FORMAT, tempLength, audio->volume);
audio->buffer += tempLength;
audio->length -= tempLength;
previous = audio;
audio = audio->next;
}
else if(audio->loop == 1 && audio->fade == 0)
{
audio->buffer = audio->bufferTrue;
audio->length = audio->lengthTrue;
}
else
{
previous->next = audio->next;
if(audio->loop == 0)
{
gSoundCount--;
}
audio->next = NULL;
freeAudio(audio);
audio = previous->next;
}
}
}
static void addAudio(Audio * root, Audio * news)
{
if(root == NULL)
{
return;
}
while(root->next != NULL)
{
root = root->next;
}
root->next = news;
}
|