This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
forked from Jay-Jay-OPL/OPL-Daily-Builds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsound.c
559 lines (452 loc) · 14.4 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
/*
Copyright 2022, Thanks to SP193
Licenced under Academic Free License version 3.0
Review OpenPS2Loader README & LICENSE files for further details.
*/
#include <audsrv.h>
#include <vorbis/vorbisfile.h>
#include "include/sound.h"
#include "include/opl.h"
#include "include/ioman.h"
#include "include/themes.h"
// Silence unused variable warnings from vorbisfile.h
static ov_callbacks OV_CALLBACKS_NOCLOSE __attribute__((unused));
static ov_callbacks OV_CALLBACKS_STREAMONLY __attribute__((unused));
static ov_callbacks OV_CALLBACKS_STREAMONLY_NOCLOSE __attribute__((unused));
/*-- Theme Sound Effects ----------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------*/
extern unsigned char boot_adp[];
extern unsigned int size_boot_adp;
extern unsigned char cancel_adp[];
extern unsigned int size_cancel_adp;
extern unsigned char confirm_adp[];
extern unsigned int size_confirm_adp;
extern unsigned char cursor_adp[];
extern unsigned int size_cursor_adp;
extern unsigned char message_adp[];
extern unsigned int size_message_adp;
extern unsigned char transition_adp[];
extern unsigned int size_transition_adp;
struct sfxEffect
{
const char *name;
void *buffer;
int size;
int builtin;
int duration_ms;
};
static struct sfxEffect sfx_files[SFX_COUNT] = {
{"boot.adp"},
{"cancel.adp"},
{"confirm.adp"},
{"cursor.adp"},
{"message.adp"},
{"transition.adp"},
};
static struct audsrv_adpcm_t sfx[SFX_COUNT];
static int audio_initialized = 0;
// Returns 0 if the specified file was read. The sfxEffect structure will not be updated unless the file is successfully read.
static int sfxRead(const char *full_path, struct sfxEffect *sfx)
{
int adpcm;
void *buffer;
int ret, size;
LOG("SFX: sfxRead('%s')\n", full_path);
adpcm = open(full_path, O_RDONLY);
if (adpcm < 0) {
LOG("SFX: %s: Failed to open adpcm file %s\n", __FUNCTION__, full_path);
return -ENOENT;
}
size = lseek(adpcm, 0, SEEK_END);
lseek(adpcm, 0L, SEEK_SET);
buffer = memalign(64, size);
if (buffer == NULL) {
LOG("SFX: Failed to allocate memory for SFX\n");
close(adpcm);
return -ENOMEM;
}
ret = read(adpcm, buffer, size);
close(adpcm);
if (ret != size) {
LOG("SFX: Failed to read SFX: %d (expected %d)\n", ret, size);
free(buffer);
return -EIO;
}
sfx->buffer = buffer;
sfx->size = size;
sfx->builtin = 0;
return 0;
}
static int sfxCalculateSoundDuration(int nSamples)
{
float sampleRate = 44100; // 44.1kHz
// Return duration in milliseconds
return (nSamples / sampleRate) * 1000;
}
static void sfxInitDefaults(void)
{
sfx_files[SFX_BOOT].buffer = boot_adp;
sfx_files[SFX_BOOT].size = size_boot_adp;
sfx_files[SFX_BOOT].builtin = 1;
sfx_files[SFX_CANCEL].buffer = cancel_adp;
sfx_files[SFX_CANCEL].size = size_cancel_adp;
sfx_files[SFX_CANCEL].builtin = 1;
sfx_files[SFX_CONFIRM].buffer = confirm_adp;
sfx_files[SFX_CONFIRM].size = size_confirm_adp;
sfx_files[SFX_CONFIRM].builtin = 1;
sfx_files[SFX_CURSOR].buffer = cursor_adp;
sfx_files[SFX_CURSOR].size = size_cursor_adp;
sfx_files[SFX_CURSOR].builtin = 1;
sfx_files[SFX_MESSAGE].buffer = message_adp;
sfx_files[SFX_MESSAGE].size = size_message_adp;
sfx_files[SFX_MESSAGE].builtin = 1;
sfx_files[SFX_TRANSITION].buffer = transition_adp;
sfx_files[SFX_TRANSITION].size = size_transition_adp;
sfx_files[SFX_TRANSITION].builtin = 1;
}
// Returns 0 (AUDSRV_ERR_NOERROR) if the sound was loaded successfully.
static int sfxLoad(struct sfxEffect *sfxData, audsrv_adpcm_t *sfx)
{
int ret;
// Calculate duration based on number of samples
sfxData->duration_ms = sfxCalculateSoundDuration(((u32 *)sfxData->buffer)[3]);
// Estimate duration based on filesize, if the ADPCM header was 0
if (sfxData->duration_ms == 0)
sfxData->duration_ms = sfxData->size / 47;
ret = audsrv_load_adpcm(sfx, sfxData->buffer, sfxData->size);
if (sfxData->builtin == 0) {
free(sfxData->buffer);
sfxData->buffer = NULL; // Mark the buffer as freed.
}
return ret;
}
// Returns number of audio files successfully loaded, < 0 if an unrecoverable error occurred.
int sfxInit(int bootSnd)
{
char sound_path[256];
char full_path[256];
int ret, loaded;
int thmSfxEnabled = 0;
int i = 1;
if (!audio_initialized) {
LOG("SFX: %s: ERROR: not initialized!\n", __FUNCTION__);
return -1;
}
audsrv_adpcm_init();
sfxInitDefaults();
audioSetVolume();
// Check default theme is not current theme
int themeID = thmGetGuiValue();
if (themeID != 0) {
// Get theme path for sfx
char *thmPath = thmGetFilePath(themeID);
snprintf(sound_path, sizeof(sound_path), "%ssound", thmPath);
// Check for custom sfx folder
DIR *dir = opendir(sound_path);
if (dir != NULL) {
thmSfxEnabled = 1;
closedir(dir);
}
}
loaded = 0;
i = bootSnd ? 0 : 1;
for (; i < SFX_COUNT; i++) {
if (thmSfxEnabled) {
snprintf(full_path, sizeof(full_path), "%s/%s", sound_path, sfx_files[i].name);
ret = sfxRead(full_path, &sfx_files[i]);
if (ret != 0) {
LOG("SFX: %s could not be loaded. Using default sound %d.\n", full_path, ret);
}
} else
snprintf(full_path, sizeof(full_path), "builtin/%s", sfx_files[i].name);
ret = sfxLoad(&sfx_files[i], &sfx[i]);
if (ret == 0) {
LOG("SFX: Loaded %s, size=%d, duration=%dms\n", full_path, sfx_files[i].size, sfx_files[i].duration_ms);
loaded++;
} else {
LOG("SFX: failed to load %s, error %d\n", full_path, ret);
}
}
return loaded;
}
int sfxGetSoundDuration(int id)
{
if (!audio_initialized) {
LOG("SFX: %s: ERROR: not initialized!\n", __FUNCTION__);
return 0;
}
return sfx_files[id].duration_ms;
}
void sfxPlay(int id)
{
if (!audio_initialized) {
LOG("SFX: %s: ERROR: not initialized!\n", __FUNCTION__);
return;
}
if (gEnableSFX) {
audsrv_ch_play_adpcm(id, &sfx[id]);
}
}
/*-- Theme Background Music -------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------*/
#define BGM_RING_BUFFER_COUNT 16
#define BGM_RING_BUFFER_SIZE 4096
#define BGM_THREAD_BASE_PRIO 0x40
#define BGM_THREAD_STACK_SIZE 0x1000
extern void *_gp;
static int bgmThreadID, bgmIoThreadID;
static int outSema, inSema;
static unsigned char terminateFlag, bgmIsPlaying;
static unsigned char rdPtr, wrPtr;
static char bgmBuffer[BGM_RING_BUFFER_COUNT][BGM_RING_BUFFER_SIZE];
static volatile unsigned char bgmThreadRunning, bgmIoThreadRunning;
static u8 bgmThreadStack[BGM_THREAD_STACK_SIZE] __attribute__((aligned(16)));
static u8 bgmIoThreadStack[BGM_THREAD_STACK_SIZE] __attribute__((aligned(16)));
static OggVorbis_File *vorbisFile;
static void bgmThread(void *arg)
{
bgmThreadRunning = 1;
while (!terminateFlag) {
SleepThread();
while (PollSema(outSema) == outSema) {
audsrv_wait_audio(BGM_RING_BUFFER_SIZE);
audsrv_play_audio(bgmBuffer[rdPtr], BGM_RING_BUFFER_SIZE);
rdPtr = (rdPtr + 1) % BGM_RING_BUFFER_COUNT;
SignalSema(inSema);
}
}
audsrv_stop_audio();
rdPtr = 0;
wrPtr = 0;
bgmThreadRunning = 0;
bgmIsPlaying = 0;
}
static void bgmIoThread(void *arg)
{
int partsToRead, decodeTotal, bitStream, i;
bgmIoThreadRunning = 1;
do {
WaitSema(inSema);
partsToRead = 1;
while ((wrPtr + partsToRead < BGM_RING_BUFFER_COUNT) && (PollSema(inSema) == inSema))
partsToRead++;
decodeTotal = BGM_RING_BUFFER_SIZE;
int bufferPtr = 0;
do {
int ret = ov_read(vorbisFile, bgmBuffer[wrPtr] + bufferPtr, decodeTotal, 0, 2, 1, &bitStream);
if (ret > 0) {
bufferPtr += ret;
decodeTotal -= ret;
} else if (ret < 0) {
LOG("BGM: I/O error while reading.\n");
terminateFlag = 1;
break;
} else if (ret == 0)
ov_pcm_seek(vorbisFile, 0);
} while (decodeTotal > 0);
wrPtr = (wrPtr + partsToRead) % BGM_RING_BUFFER_COUNT;
for (i = 0; i < partsToRead; i++)
SignalSema(outSema);
WakeupThread(bgmThreadID);
} while (!terminateFlag && gEnableBGM);
bgmIoThreadRunning = 0;
terminateFlag = 1;
WakeupThread(bgmThreadID);
}
static int bgmLoad(void)
{
FILE *bgmFile;
char bgmPath[256];
vorbisFile = malloc(sizeof(OggVorbis_File));
memset(vorbisFile, 0, sizeof(OggVorbis_File));
int themeID = thmGetGuiValue();
if (themeID != 0) {
char *thmPath = thmGetFilePath(themeID);
snprintf(bgmPath, sizeof(bgmPath), "%ssound/bgm.ogg", thmPath);
} else
snprintf(bgmPath, sizeof(bgmPath), gDefaultBGMPath);
bgmFile = fopen(bgmPath, "rb");
if (bgmFile == NULL) {
LOG("BGM: Failed to open Ogg file %s\n", bgmPath);
return -ENOENT;
}
if (ov_open_callbacks(bgmFile, vorbisFile, NULL, 0, OV_CALLBACKS_DEFAULT) < 0) {
LOG("BGM: Input does not appear to be an Ogg bitstream.\n");
return -ENOENT;
}
return 0;
}
static int bgmInit(void)
{
ee_thread_t thread;
ee_sema_t sema;
int result;
terminateFlag = 0;
rdPtr = 0;
wrPtr = 0;
bgmThreadRunning = 0;
bgmIoThreadRunning = 0;
sema.max_count = BGM_RING_BUFFER_COUNT;
sema.init_count = BGM_RING_BUFFER_COUNT;
sema.attr = 0;
sema.option = (u32) "bgm-in-sema";
inSema = CreateSema(&sema);
if (inSema >= 0) {
sema.max_count = BGM_RING_BUFFER_COUNT;
sema.init_count = 0;
sema.attr = 0;
sema.option = (u32) "bgm-out-sema";
outSema = CreateSema(&sema);
if (outSema < 0) {
DeleteSema(inSema);
return outSema;
}
} else
return inSema;
thread.func = &bgmThread;
thread.stack = bgmThreadStack;
thread.stack_size = sizeof(bgmThreadStack);
thread.gp_reg = &_gp;
thread.initial_priority = BGM_THREAD_BASE_PRIO;
thread.attr = 0;
thread.option = 0;
// BGM thread will start in DORMANT state.
bgmThreadID = CreateThread(&thread);
if (bgmThreadID >= 0) {
thread.func = &bgmIoThread;
thread.stack = bgmIoThreadStack;
thread.stack_size = sizeof(bgmIoThreadStack);
thread.gp_reg = &_gp;
thread.initial_priority = BGM_THREAD_BASE_PRIO + 1;
thread.attr = 0;
thread.option = 0;
// BGM I/O thread will start in DORMANT state.
bgmIoThreadID = CreateThread(&thread);
if (bgmIoThreadID >= 0) {
result = 0;
} else {
DeleteSema(inSema);
DeleteSema(outSema);
DeleteThread(bgmThreadID);
result = bgmIoThreadID;
}
} else {
result = bgmThreadID;
DeleteSema(inSema);
DeleteSema(outSema);
}
return result;
}
static void bgmDeinit(void)
{
DeleteSema(inSema);
DeleteSema(outSema);
DeleteThread(bgmThreadID);
DeleteThread(bgmIoThreadID);
// Vorbisfile takes care of fclose.
ov_clear(vorbisFile);
free(vorbisFile);
vorbisFile = NULL;
}
static void bgmShutdownDelayCallback(s32 alarm_id, u16 time, void *common)
{
iWakeupThread((int)common);
}
void bgmStart(void)
{
struct audsrv_fmt_t audsrvFmt;
if (!audio_initialized) {
LOG("BGM: %s: ERROR: not initialized!\n", __FUNCTION__);
return;
}
int ret = bgmInit();
if (ret >= 0) {
if (bgmLoad() != 0) {
bgmDeinit();
return;
}
vorbis_info *vi = ov_info(vorbisFile, -1);
ov_pcm_seek(vorbisFile, 0);
audsrvFmt.channels = vi->channels;
audsrvFmt.freq = vi->rate;
audsrvFmt.bits = 16;
audsrv_set_format(&audsrvFmt);
bgmIsPlaying = 1;
StartThread(bgmIoThreadID, NULL);
StartThread(bgmThreadID, NULL);
}
}
void bgmStop(void)
{
int threadId;
if (!audio_initialized) {
LOG("BGM: %s: ERROR: not initialized!\n", __FUNCTION__);
return;
}
LOG("BGM: terminating threads...\n");
terminateFlag = 1;
WakeupThread(bgmThreadID);
threadId = GetThreadId();
while (bgmIoThreadRunning) {
SetAlarm(200 * 16, &bgmShutdownDelayCallback, (void *)threadId);
SleepThread();
}
while (bgmThreadRunning) {
SetAlarm(200 * 16, &bgmShutdownDelayCallback, (void *)threadId);
SleepThread();
}
bgmDeinit();
LOG("BGM: stopped.\n");
}
int isBgmPlaying(void)
{
int ret = (int)bgmIsPlaying;
return ret;
}
// HACK: BGM stutters while perfroming certain tasks, mute during these operations and unmute once completed.
void bgmMute(void)
{
if (audio_initialized)
audsrv_set_volume(0);
}
void bgmUnMute(void)
{
if (audio_initialized)
audsrv_set_volume(gBGMVolume);
}
/*-- General Audio ------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------*/
void audioInit(void)
{
if (!audio_initialized) {
if (audsrv_init() != 0) {
LOG("AUDIO: Failed to initialize audsrv\n");
LOG("AUDIO: Audsrv returned error string: %s\n", audsrv_get_error_string());
return;
}
audio_initialized = 1;
}
}
void audioEnd(void)
{
if (!audio_initialized) {
LOG("AUDIO: %s: ERROR: not initialized!\n", __FUNCTION__);
return;
}
if (gEnableBGM && isBgmPlaying())
bgmStop();
audsrv_quit();
audio_initialized = 0;
}
void audioSetVolume(void)
{
int i;
if (!audio_initialized) {
LOG("AUDIO: %s: ERROR: not initialized!\n", __FUNCTION__);
return;
}
for (i = 1; i < SFX_COUNT; i++)
audsrv_adpcm_set_volume(i, gSFXVolume);
audsrv_adpcm_set_volume(0, gBootSndVolume);
audsrv_set_volume(gBGMVolume);
}