-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.c
747 lines (599 loc) · 12.9 KB
/
sound.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
#include "palcommon.h"
#include "common.h"
#include "sound.h"
#include "rixplay.h"
#include "util.h"
#ifdef PAL_HAS_NATIVEMIDI
#include "midi.h"
#endif
#ifdef PAL_HAS_MP3
#include "libmad/music_mad.h"
#endif
static BOOL gSndOpened = FALSE;
BOOL g_fNoSound = FALSE;
BOOL g_fNoMusic = FALSE;
#ifdef PAL_HAS_NATIVEMIDI
BOOL g_fUseMidi = FALSE;
#endif
static BOOL g_fUseWav = FALSE;
#ifdef __SYMBIAN32__
INT g_iVolume = SDL_MIX_MAXVOLUME * 0.1;
#endif
#ifdef PAL_CLASSIC
int g_iCurrChannel = 0;
#endif
typedef struct tagSNDPLAYER
{
FILE *mkf;
SDL_AudioSpec spec;
SDL_mutex *mtx;
LPBYTE buf[2], pos[2];
INT audio_len[2];
#ifdef PAL_HAS_CD
SDL_CD *pCD;
#endif
#ifdef PAL_HAS_MP3
mad_data *pMP3;
BOOL fMP3Loop;
INT iCurrentMP3;
SDL_mutex *lock;
#endif
} SNDPLAYER;
static SNDPLAYER gSndPlayer;
static SDL_AudioSpec *
SOUND_LoadVOCFromBuffer(
LPCBYTE lpVOC,
DWORD dwLen,
SDL_AudioSpec *lpSpec,
LPBYTE *lppBuffer
)
/*++
Purpose:
Load a VOC file in a buffer. Currently supports type 01 block only.
Parameters:
[IN] lpVOC - pointer to the buffer of the VOC file.
[IN] dwLen - length of the buffer of the VOC file.
[OUT] lpSpec - pointer to the SDL_AudioSpec structure, which contains
some basic information about the VOC file.
[OUT] lppBuffer - the output buffer.
Return value:
Pointer to the SDL_AudioSpec structure, NULL if failed.
--*/
{
INT freq, len, x, i, l;
SDL_RWops *rw;
if (g_fUseWav)
{
rw = SDL_RWFromConstMem(lpVOC, dwLen);
if (rw == NULL) return NULL;
len = dwLen;
SDL_LoadWAV_RW(rw, 1, lpSpec, lppBuffer, (Uint32 *)&len);
lpSpec->size = len;
return lpSpec;
}
else
{
//
// Skip header
//
lpVOC += 0x1B;
//
// Length is 3 bytes long
//
len = (lpVOC[0] | (lpVOC[1] << 8) | (lpVOC[2] << 16)) - 2;
lpVOC += 3;
//
// One byte for frequency
//
freq = 1000000 / (256 - *lpVOC);
#if 1
lpVOC += 2;
//
// Convert the sample manually, as SDL doesn't like "strange" sample rates.
//
x = (INT)(len * ((FLOAT)PAL_SAMPLE_RATE / freq));
*lppBuffer = (LPBYTE)calloc(1, x);
if (*lppBuffer == NULL)
{
return NULL;
}
for (i = 0; i < x; i++)
{
l = (INT)(i * (freq / (FLOAT)PAL_SAMPLE_RATE));
if (l >= len)
{
l = len - 1;
}
(*lppBuffer)[i] = lpVOC[l];
}
lpSpec->channels = 1;
lpSpec->format = AUDIO_U8;
lpSpec->freq = PAL_SAMPLE_RATE;
lpSpec->size = x;
#else
*lppBuffer = (unsigned char *)malloc(len);
if (*lppBuffer == NULL)
{
return NULL;
}
lpSpec->channels = 1;
lpSpec->format = AUDIO_U8;
lpSpec->freq = freq;
lpSpec->size = len;
lpVOC += 2;
memcpy(*lppBuffer, lpVOC, len);
#endif
return lpSpec;
}
}
static VOID SDLCALL
SOUND_FillAudio(
LPVOID udata,
LPBYTE stream,
INT len
)
/*++
Purpose:
SDL sound callback function.
Parameters:
[IN] udata - pointer to user-defined parameters (Not used).
[OUT] stream - pointer to the stream buffer.
[IN] len - Length of the buffer.
Return value:
None.
--*/
{
int i;
#if SDL_VERSION_ATLEAST(2,0,0)
memset(stream, 0, len);
#endif
//
// Play music
//
if (!g_fNoMusic)
{
#ifdef PAL_HAS_MP3
SDL_mutexP(gSndPlayer.lock);
if (gSndPlayer.pMP3 != NULL)
{
mad_getSamples(gSndPlayer.pMP3, stream, len);
if (!mad_isPlaying(gSndPlayer.pMP3) && gSndPlayer.fMP3Loop)
{
mad_seek(gSndPlayer.pMP3, 0);
mad_start(gSndPlayer.pMP3);
mad_getSamples(gSndPlayer.pMP3, stream, len);
}
}
SDL_mutexV(gSndPlayer.lock);
#endif
RIX_FillBuffer(stream, len);
}
//
// No current playing sound
//
if (g_fNoSound)
{
return;
}
SDL_mutexP(gSndPlayer.mtx);
for (i = 0; i < 2; i++)
{
//
// Only play if we have data left
//
if (gSndPlayer.buf[i] == NULL)
{
continue;
}
if (gSndPlayer.audio_len[i] == 0)
{
//
// Delete the audio buffer from memory
//
free(gSndPlayer.buf[i]);
gSndPlayer.buf[i] = NULL;
continue;
}
//
// Mix as much data as possible
//
len = (len > gSndPlayer.audio_len[i]) ? gSndPlayer.audio_len[i] : len;
#ifdef __SYMBIAN32__
SDL_MixAudio(stream, gSndPlayer.pos[i], len, g_iVolume);
#else
SDL_MixAudio(stream, gSndPlayer.pos[i], len, SDL_MIX_MAXVOLUME * 2 / 3);
#endif
gSndPlayer.pos[i] += len;
gSndPlayer.audio_len[i] -= len;
}
SDL_mutexV(gSndPlayer.mtx);
}
INT
SOUND_OpenAudio(
VOID
)
/*++
Purpose:
Initialize the audio subsystem.
Parameters:
None.
Return value:
0 if succeed, others if failed.
--*/
{
SDL_AudioSpec spec;
if (gSndOpened)
{
//
// Already opened
//
return -1;
}
gSndOpened = FALSE;
//
// Load the MKF file.
//
gSndPlayer.mkf = UTIL_OpenFile("voc.mkf");
if (gSndPlayer.mkf == NULL)
{
gSndPlayer.mkf = UTIL_OpenFile("sounds.mkf");
if (gSndPlayer.mkf == NULL)
{
return -2;
}
g_fUseWav = TRUE;
}
else
{
g_fUseWav = FALSE;
}
//
// Open the sound subsystem.
//
gSndPlayer.spec.freq = PAL_SAMPLE_RATE;
gSndPlayer.spec.format = AUDIO_S16;
gSndPlayer.spec.channels = PAL_CHANNELS;
gSndPlayer.spec.samples = 1024;
gSndPlayer.spec.callback = SOUND_FillAudio;
if (SDL_OpenAudio(&gSndPlayer.spec, &spec) < 0)
{
//
// Failed
//
return -3;
}
memcpy(&gSndPlayer.spec, &spec, sizeof(SDL_AudioSpec));
gSndPlayer.buf[0] = NULL;
gSndPlayer.pos[0] = NULL;
gSndPlayer.audio_len[0] = 0;
gSndPlayer.buf[1] = NULL;
gSndPlayer.pos[1] = NULL;
gSndPlayer.audio_len[1] = 0;
gSndPlayer.mtx = SDL_CreateMutex();
gSndOpened = TRUE;
//
// Initialize the music subsystem.
//
if (RIX_Init(va("%s%s", PAL_PREFIX, "mus.mkf")) < 0)
{
RIX_Init(va("%s%s", PAL_PREFIX, "MUS.MKF"));
}
#ifdef PAL_HAS_CD
//
// Initialize the CD audio.
//
{
int i;
gSndPlayer.pCD = NULL;
for (i = 0; i < SDL_CDNumDrives(); i++)
{
gSndPlayer.pCD = SDL_CDOpen(i);
if (gSndPlayer.pCD != NULL)
{
if (!CD_INDRIVE(SDL_CDStatus(gSndPlayer.pCD)))
{
SDL_CDClose(gSndPlayer.pCD);
gSndPlayer.pCD = NULL;
}
else
{
break;
}
}
}
}
#endif
#ifdef PAL_HAS_MP3
gSndPlayer.iCurrentMP3 = -1;
gSndPlayer.lock = SDL_CreateMutex();
#endif
//
// Let the callback function run so that musics will be played.
//
SDL_PauseAudio(0);
return 0;
}
#ifdef PSP
void
SOUND_ReloadVOC(
void
)
{
fclose(gSndPlayer.mkf);
gSndPlayer.mkf = UTIL_OpenFile("voc.mkf");
g_fUseWav = FALSE;
}
#endif
VOID
SOUND_CloseAudio(
VOID
)
/*++
Purpose:
Close the audio subsystem.
Parameters:
None.
Return value:
None.
--*/
{
SDL_mutexP(gSndPlayer.mtx);
SDL_CloseAudio();
if (gSndPlayer.buf[0] != NULL)
{
free(gSndPlayer.buf[0]);
gSndPlayer.buf[0] = NULL;
}
if (gSndPlayer.buf[1] != NULL)
{
free(gSndPlayer.buf[1]);
gSndPlayer.buf[1] = NULL;
}
if (gSndPlayer.mkf != NULL)
{
fclose(gSndPlayer.mkf);
gSndPlayer.mkf = NULL;
}
SDL_DestroyMutex(gSndPlayer.mtx);
#ifdef PAL_HAS_MP3
SDL_mutexP(gSndPlayer.lock);
if (gSndPlayer.pMP3 != NULL)
{
mad_stop(gSndPlayer.pMP3);
mad_closeFile(gSndPlayer.pMP3);
gSndPlayer.pMP3 = NULL;
}
SDL_DestroyMutex(gSndPlayer.lock);
#endif
RIX_Shutdown();
#ifdef PAL_HAS_CD
if (gSndPlayer.pCD != NULL)
{
SOUND_PlayCDA(-1);
SDL_CDClose(gSndPlayer.pCD);
}
#endif
#ifdef PAL_HAS_NATIVEMIDI
MIDI_Play(0, FALSE);
#endif
}
#ifdef __SYMBIAN32__
VOID
SOUND_AdjustVolume(
INT iDirectory
)
/*++
Purpose:
SDL sound volume adjust function.
Parameters:
[IN] iDirectory - value, Increase (>0) or decrease (<=0) 3% volume.
Return value:
None.
--*/
{
if (iDirectory > 0)
{
if (g_iVolume <= SDL_MIX_MAXVOLUME)
{
g_iVolume += SDL_MIX_MAXVOLUME * 0.03;
}
else
{
g_iVolume = SDL_MIX_MAXVOLUME;
}
}
else
{
if (g_iVolume > 0)
{
g_iVolume -= SDL_MIX_MAXVOLUME * 0.03;
}
else
{
g_iVolume = 0;
}
}
}
#endif
VOID
SOUND_PlayChannel(
INT iSoundNum,
INT iChannel
)
/*++
Purpose:
Play a sound in voc.mkf file.
Parameters:
[IN] iSoundNum - number of the sound.
[IN] iChannel - the number of channel (0 or 1).
Return value:
None.
--*/
{
SDL_AudioCVT wavecvt;
SDL_AudioSpec wavespec;
LPBYTE buf, bufdec;
UINT samplesize;
int len;
if (!gSndOpened || g_fNoSound)
{
return;
}
//
// Stop playing current sound.
//
SDL_mutexP(gSndPlayer.mtx);
if (gSndPlayer.buf[iChannel] != NULL)
{
LPBYTE p = gSndPlayer.buf[iChannel];
gSndPlayer.buf[iChannel] = NULL;
free(p);
}
SDL_mutexV(gSndPlayer.mtx);
if (iSoundNum < 0)
{
return;
}
//
// Get the length of the sound file.
//
len = PAL_MKFGetChunkSize(iSoundNum, gSndPlayer.mkf);
if (len <= 0)
{
return;
}
buf = (LPBYTE)calloc(len, 1);
if (buf == NULL)
{
return;
}
//
// Read the sound file from the MKF archive.
//
PAL_MKFReadChunk(buf, len, iSoundNum, gSndPlayer.mkf);
SOUND_LoadVOCFromBuffer(buf, len, &wavespec, &bufdec);
free(buf);
//
// Build the audio converter and create conversion buffers
//
if (SDL_BuildAudioCVT(&wavecvt, wavespec.format, wavespec.channels, wavespec.freq,
gSndPlayer.spec.format, gSndPlayer.spec.channels, gSndPlayer.spec.freq) < 0)
{
free(bufdec);
return;
}
samplesize = ((wavespec.format & 0xFF) / 8) * wavespec.channels;
wavecvt.len = wavespec.size & ~(samplesize - 1);
wavecvt.buf = (LPBYTE)malloc(wavecvt.len * wavecvt.len_mult);
if (wavecvt.buf == NULL)
{
free(bufdec);
return;
}
memcpy(wavecvt.buf, bufdec, wavespec.size);
if (g_fUseWav)
{
SDL_FreeWAV(bufdec);
}
else
{
free(bufdec);
}
//
// Run the audio converter
//
if (SDL_ConvertAudio(&wavecvt) < 0)
{
return;
}
SDL_mutexP(gSndPlayer.mtx);
if (gSndPlayer.buf[iChannel] != NULL)
{
LPBYTE p = gSndPlayer.buf[iChannel];
gSndPlayer.buf[iChannel] = NULL;
free(p);
}
gSndPlayer.buf[iChannel] = wavecvt.buf;
gSndPlayer.audio_len[iChannel] = wavecvt.len * wavecvt.len_mult;
gSndPlayer.pos[iChannel] = wavecvt.buf;
SDL_mutexV(gSndPlayer.mtx);
}
VOID
PAL_PlayMUS(
INT iNumRIX,
BOOL fLoop,
FLOAT flFadeTime
)
{
#ifdef PAL_HAS_NATIVEMIDI
if (g_fUseMidi)
{
MIDI_Play(iNumRIX, fLoop);
return;
}
#endif
#ifdef PAL_HAS_MP3
SDL_mutexP(gSndPlayer.lock);
if (gSndPlayer.pMP3 != NULL)
{
if (iNumRIX == gSndPlayer.iCurrentMP3 && !g_fNoMusic)
{
SDL_mutexV(gSndPlayer.lock);
return;
}
mad_stop(gSndPlayer.pMP3);
mad_closeFile(gSndPlayer.pMP3);
gSndPlayer.pMP3 = NULL;
}
SDL_mutexV(gSndPlayer.lock);
gSndPlayer.iCurrentMP3 = -1;
if (iNumRIX > 0)
{
SDL_mutexP(gSndPlayer.lock);
gSndPlayer.pMP3 = mad_openFile(va("%s/mp3/%.2d.mp3", PAL_PREFIX, iNumRIX), &gSndPlayer.spec);
if (gSndPlayer.pMP3 != NULL)
{
RIX_Play(0, FALSE, flFadeTime);
mad_start(gSndPlayer.pMP3);
gSndPlayer.fMP3Loop = fLoop;
gSndPlayer.iCurrentMP3 = iNumRIX;
SDL_mutexV(gSndPlayer.lock);
return;
}
SDL_mutexV(gSndPlayer.lock);
}
#endif
RIX_Play(iNumRIX, fLoop, flFadeTime);
}
BOOL
SOUND_PlayCDA(
INT iNumTrack
)
/*++
Purpose:
Play a CD Audio Track.
Parameters:
[IN] iNumTrack - number of the CD Audio Track.
Return value:
TRUE if the track can be played, FALSE if not.
--*/
{
#ifdef PAL_HAS_CD
if (gSndPlayer.pCD != NULL)
{
if (CD_INDRIVE(SDL_CDStatus(gSndPlayer.pCD)))
{
SDL_CDStop(gSndPlayer.pCD);
if (iNumTrack != -1)
{
PAL_PlayMUS(-1, FALSE, 0);
if (SDL_CDPlayTracks(gSndPlayer.pCD, iNumTrack - 1, 0, 1, 0) == 0)
{
return TRUE;
}
}
}
}
#endif
return FALSE;
}