-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudioc.c
More file actions
361 lines (317 loc) · 13.7 KB
/
audioc.c
File metadata and controls
361 lines (317 loc) · 13.7 KB
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
/* Dust Ultimate Game Library (DUGL)
Copyright (C) 2025 Fakhri Feki
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 3 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, see <https://www.gnu.org/licenses/>.
contact: libdugl(at)hotmail.com */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "DUGL.h"
#include "intrndugl.h"
SDL_AudioSpec wantAudio, haveAudio;
SDL_AudioDeviceID audDevId = 0;
#define MAX_SIMULTANOUS_DSOUNDS 64
DSoundState playingSounds[MAX_SIMULTANOUS_DSOUNDS];
DSoundState queuedSounds[MAX_SIMULTANOUS_DSOUNDS];
DSoundState replaceSounds[MAX_SIMULTANOUS_DSOUNDS];
unsigned int countPlayingSounds = 0;
void DGaudioCallback( void *userData, Uint8 * stream, int len ) {
int remainLen, mixLen, requiredLen;
int idx = 0, HandledSounds = 0;
// silence buffer
SDL_memset4( stream, 0, len/4 );
if (countPlayingSounds == 0)
return;
// mix sounds
for (; idx<MAX_SIMULTANOUS_DSOUNDS && HandledSounds < countPlayingSounds; idx++) {
if (playingSounds[idx].sound != NULL) {
// replace if required
if (replaceSounds[idx].sound != NULL) {
if(replaceSounds[idx].queueInheritEvents) {
replaceSounds[idx].OnFinish = playingSounds[idx].OnFinish;
replaceSounds[idx].OnLoop = playingSounds[idx].OnLoop;
replaceSounds[idx].OnQueue = playingSounds[idx].OnQueue;
}
playingSounds[idx] = replaceSounds[idx];
replaceSounds[idx].sound = NULL;
}
// mix
if (!playingSounds[idx].paused) {
remainLen = playingSounds[idx].sound->length - playingSounds[idx].position;
mixLen = (remainLen >= (unsigned int)(len)) ? len : remainLen;
if (mixLen > 0)
SDL_MixAudioFormat(stream,
(Uint8 *)&playingSounds[idx].sound->data[playingSounds[idx].position],
haveAudio.format,
mixLen, (int)playingSounds[idx].volume);
// sound finished
if (remainLen <= len) {
requiredLen = len - remainLen; // bytes not filled yet in stream
if (playingSounds[idx].loop) {
if (!playingSounds[idx].stopEvents && playingSounds[idx].OnLoop != NULL)
playingSounds[idx].OnLoop(idx);
if (requiredLen > 0 && playingSounds[idx].sound != NULL && !playingSounds[idx].paused) {
if (requiredLen > playingSounds[idx].sound->length)
requiredLen = playingSounds[idx].sound->length;
playingSounds[idx].position = requiredLen;
SDL_MixAudioFormat(&stream[remainLen],
(Uint8 *)&playingSounds[idx].sound->data[0],
haveAudio.format,
requiredLen, (int)playingSounds[idx].volume);
} else {
if (playingSounds[idx].sound != NULL)
playingSounds[idx].position = 0;
}
} else {
if (queuedSounds[idx].sound != NULL) { // queued ?
daudioEventCallBack OnQueue = playingSounds[idx].OnQueue;
// inherit current playing sound events ?
if(queuedSounds[idx].queueInheritEvents) {
queuedSounds[idx].OnFinish = playingSounds[idx].OnFinish;
queuedSounds[idx].OnLoop = playingSounds[idx].OnLoop;
queuedSounds[idx].OnQueue = playingSounds[idx].OnQueue;
}
playingSounds[idx] = queuedSounds[idx];
if (requiredLen > 0 && !queuedSounds[idx].paused) {
SDL_MixAudioFormat(&stream[remainLen],
(Uint8 *)&playingSounds[idx].sound->data[0],
haveAudio.format,
requiredLen, (int)playingSounds[idx].volume);
playingSounds[idx].position = requiredLen;
} else {
playingSounds[idx].position = 0;
}
queuedSounds[idx].sound = NULL;
if (!playingSounds[idx].stopEvents && OnQueue != NULL)
OnQueue(idx);
} else {
playingSounds[idx].sound = NULL;
countPlayingSounds--;
if (!playingSounds[idx].stopEvents && playingSounds[idx].OnFinish != NULL)
playingSounds[idx].OnFinish(idx);
}
}
} else {
playingSounds[idx].position += len;
}
}
HandledSounds++;
}
}
}
bool InstallDAudio(int frequency, bool stereo) {
SDL_memset(&wantAudio, 0, sizeof(wantAudio));
wantAudio.freq = (frequency >= 11025 && frequency <= 48000) ? frequency : 44000;
wantAudio.format = AUDIO_S16SYS;
wantAudio.channels = (stereo) ? 2 : 1;
wantAudio.samples = 1024*2;
wantAudio.callback = DGaudioCallback;
audDevId = SDL_OpenAudioDevice(NULL, 0, &wantAudio, &haveAudio, SDL_AUDIO_ALLOW_CHANNELS_CHANGE | SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
if (audDevId == 0)
return false;
SDL_PauseAudioDevice(audDevId, 1); // paused at startup
SDL_memset(playingSounds, 0, sizeof(DSoundState)*MAX_SIMULTANOUS_DSOUNDS);
SDL_memset(queuedSounds, 0, sizeof(DSoundState)*MAX_SIMULTANOUS_DSOUNDS);
SDL_memset(replaceSounds, 0, sizeof(DSoundState)*MAX_SIMULTANOUS_DSOUNDS);
countPlayingSounds = 0;
return true;
}
void UninstallDAudio() {
if (audDevId != 0) {
SDL_PauseAudioDevice(audDevId, 1);
SDL_CloseAudioDevice(audDevId);
audDevId = 0;
SDL_memset(playingSounds, 0, sizeof(DSoundState)*MAX_SIMULTANOUS_DSOUNDS);
SDL_memset(queuedSounds, 0, sizeof(DSoundState)*MAX_SIMULTANOUS_DSOUNDS);
SDL_memset(replaceSounds, 0, sizeof(DSoundState)*MAX_SIMULTANOUS_DSOUNDS);
countPlayingSounds = 0;
}
}
void PlayDAudio() {
SDL_PauseAudioDevice(audDevId, 0);
}
void PauseDAudio() {
SDL_PauseAudioDevice(audDevId, 1);
}
void LockDAudio() {
SDL_UnlockAudioDevice(audDevId);
}
void UnlockDAudio() {
SDL_UnlockAudioDevice(audDevId);
}
unsigned int PlayDSound(DSound *sound, unsigned char volume, bool loop, bool paused) {
LockDAudio();
int freeIdx = 0;
if (countPlayingSounds == MAX_SIMULTANOUS_DSOUNDS) {
UnlockDAudio();
return NO_SOUND_ID;
}
// mix sounds
for (; freeIdx<MAX_SIMULTANOUS_DSOUNDS && playingSounds[freeIdx].sound != NULL; freeIdx++);
// shouldn't happen
if (freeIdx >= MAX_SIMULTANOUS_DSOUNDS) {
UnlockDAudio();
return NO_SOUND_ID;
}
SDL_memset(&playingSounds[freeIdx], 0, sizeof(DSoundState));
playingSounds[freeIdx].sound = sound;
playingSounds[freeIdx].volume = (((unsigned int)volume+1)*SDL_MIX_MAXVOLUME)>>8;
playingSounds[freeIdx].loop = loop;
playingSounds[freeIdx].position = 0;
playingSounds[freeIdx].paused = paused;
countPlayingSounds++;
UnlockDAudio();
return freeIdx;
}
void StopDSound(unsigned int soundID) {
if (soundID < MAX_SIMULTANOUS_DSOUNDS && playingSounds[soundID].sound != NULL) {
LockDAudio();
playingSounds[soundID].sound = NULL;
countPlayingSounds--;
UnlockDAudio();
}
}
void SetVolumeDSound(unsigned int soundID, unsigned char volume) {
if (soundID < MAX_SIMULTANOUS_DSOUNDS && playingSounds[soundID].sound != NULL) {
playingSounds[soundID].volume = (((unsigned int)volume+1)*SDL_MIX_MAXVOLUME)>>8;
}
}
void SetPausedDSound(unsigned int soundID, bool paused) {
if (soundID < MAX_SIMULTANOUS_DSOUNDS && playingSounds[soundID].sound != NULL) {
playingSounds[soundID].paused = paused;
}
}
void SetLoopingDSound(unsigned int soundID, bool loop) {
if (soundID < MAX_SIMULTANOUS_DSOUNDS && playingSounds[soundID].sound != NULL) {
playingSounds[soundID].loop = loop;
}
}
void SetStopEventsDSound(unsigned int soundID, bool stopEvents) {
if (soundID < MAX_SIMULTANOUS_DSOUNDS && playingSounds[soundID].sound != NULL) {
playingSounds[soundID].stopEvents = stopEvents;
}
}
DSoundState* GetPlayingStateDSound(unsigned int soundID) {
if (soundID<MAX_SIMULTANOUS_DSOUNDS) {
return &playingSounds[soundID];
}
return NULL;
}
void SetEventsDSound(unsigned int soundID, daudioEventCallBack OnFinish, daudioEventCallBack OnLoop, daudioEventCallBack OnQueue) {
if (soundID < MAX_SIMULTANOUS_DSOUNDS) {
LockDAudio();
if (playingSounds[soundID].sound != NULL) {
playingSounds[soundID].OnFinish = OnFinish;
playingSounds[soundID].OnLoop = OnLoop;
playingSounds[soundID].OnQueue = OnQueue;
}
UnlockDAudio();
}
}
bool QueueDSound(unsigned int soundID, DSound *sound, unsigned char volume, bool loop, bool paused, bool inheritEvents) {
bool res = false;
LockDAudio();
if (playingSounds[soundID].sound != NULL && queuedSounds[soundID].sound == NULL) {
SDL_memset(&queuedSounds[soundID], 0, sizeof(DSoundState));
queuedSounds[soundID].sound = sound;
queuedSounds[soundID].volume = (((unsigned int)volume+1)*SDL_MIX_MAXVOLUME)>>8;
queuedSounds[soundID].loop = loop;
queuedSounds[soundID].position = 0;
queuedSounds[soundID].paused = paused;
queuedSounds[soundID].queueInheritEvents = inheritEvents;
res = true;
}
UnlockDAudio();
return res;
}
bool ReplaceDSound(unsigned int *soundID, DSound *sound, unsigned char volume, bool loop, bool paused, bool inheritEvents) {
bool res = false;
LockDAudio();
if (*soundID < MAX_SIMULTANOUS_DSOUNDS && playingSounds[*soundID].sound != NULL) {
SDL_memset(&replaceSounds[*soundID], 0, sizeof(DSoundState));
replaceSounds[*soundID].sound = sound;
replaceSounds[*soundID].volume = (((unsigned int)volume+1)*SDL_MIX_MAXVOLUME)>>8;
replaceSounds[*soundID].loop = loop;
replaceSounds[*soundID].position = 0;
replaceSounds[*soundID].paused = paused;
replaceSounds[*soundID].queueInheritEvents = inheritEvents;
res = true;
} else { // no sound playing ? add new playing sound
*soundID = PlayDSound(sound, volume, loop, paused);
}
UnlockDAudio();
return res;
}
bool IsQueuedDSound(unsigned int soundID) {
bool res = false;
if (soundID < MAX_SIMULTANOUS_DSOUNDS) {
LockDAudio();
res = (queuedSounds[soundID].sound != NULL);
UnlockDAudio();
}
return res;
}
bool IsPlayingDSound(unsigned int soundID) {
bool res = false;
if (soundID < MAX_SIMULTANOUS_DSOUNDS) {
LockDAudio();
res = (playingSounds[soundID].sound != NULL);
UnlockDAudio();
}
return res;
}
unsigned int GetPlayingDSoundCount() {
return countPlayingSounds;
}
DSound *DgLoadWAV(char *filename) {
DSound *sound = NULL;
SDL_AudioSpec wav_spec;
Uint32 wavLength = 0;
Uint8 *wavBuffer = NULL;
SDL_AudioCVT cvt;
if (audDevId != 0) {
if (SDL_LoadWAV(filename, &wav_spec, &wavBuffer, &wavLength) != NULL) {
// convert to current audio device format
SDL_BuildAudioCVT(&cvt, wav_spec.format, wav_spec.channels, wav_spec.freq, haveAudio.format, haveAudio.channels, haveAudio.freq);
if (cvt.needed) {
cvt.buf =(Uint8*)SDL_malloc(wavLength*((Uint32)cvt.len_mult));
if (cvt.buf != NULL) {
cvt.len = wavLength;
SDL_memcpy(cvt.buf, wavBuffer, wavLength);
if (SDL_ConvertAudio(&cvt) == 0) {
sound = (DSound*)SDL_malloc(sizeof(DSound) + cvt.len_cvt);
if (sound != NULL) {
sound->data = &((Uint8 *)sound)[sizeof(DSound)];
sound->length = cvt.len_cvt;
SDL_memcpy(sound->data, cvt.buf, cvt.len_cvt);
}
}
SDL_free(cvt.buf);
}
} else { // copy data as-is, no conversion needed
sound = (DSound*)SDL_malloc(sizeof(DSound) + wavLength);
if (sound != NULL) {
sound->data = &((Uint8 *)sound)[sizeof(DSound)];
sound->length = wavLength;
SDL_memcpy(sound->data, wavBuffer, wavLength);
}
}
SDL_FreeWAV(wavBuffer);
}
}
return sound;
}
void DestroyDSound(DSound *sound) {
sound->length = 0;
sound->data = NULL;
SDL_free(sound);
}