forked from belek666/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sound.c
236 lines (194 loc) · 5.82 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
/*-- Theme Sound Effects ----------------------------------------------------------------------------------------------
---- SP193 wrote the code, I just made small changes. ---------------------------------------------------------------*/
#include <audsrv.h>
#include "include/sound.h"
#include "include/opl.h"
#include "include/ioman.h"
#include "include/themes.h"
//default sfx
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;
};
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];
//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)
{
FILE* adpcm;
void *buffer;
int ret, size;
LOG("sfxRead('%s')\n", full_path);
adpcm = fopen(full_path, "rb");
if (adpcm == NULL)
{
LOG("Failed to open adpcm file %s\n", full_path);
return -ENOENT;
}
fseek(adpcm, 0, SEEK_END);
size = ftell(adpcm);
rewind(adpcm);
buffer = memalign(64, size);
if (buffer == NULL)
{
LOG("Failed to allocate memory for SFX\n");
fclose(adpcm);
return -ENOMEM;
}
ret = fread(buffer, 1, size, adpcm);
fclose(adpcm);
if(ret != size)
{
LOG("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 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;
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;
}
static int getFadeDelay(char *sound_path)
{
FILE* bootSnd;
char boot_path[256];
int len;
int logoFadeTime = 1400; //fade time from sound call to fade to main in milliseconds
int byteRate = 176400 / 1000; //sample rate * channels * bits per sample /8 (/1000 to get in milliseconds)
snprintf(boot_path, sizeof(boot_path), "%s/%s", sound_path, sfx_files[SFX_BOOT].name);
bootSnd = fopen(boot_path, "rb");
if (bootSnd == NULL)
{
LOG("Failed to open adpcm file %s\n", boot_path);
return -ENOENT;
}
fseek(bootSnd, 12, SEEK_SET);
fread(&len, 4, 1, bootSnd);
rewind(bootSnd);
fclose(bootSnd);
gFadeDelay = len / byteRate - logoFadeTime;
return 0;
}
void sfxVolume(void)
{
int i;
for (i = 1; i < SFX_COUNT; i++)
{
audsrv_adpcm_set_volume(i, gSFXVolume);
}
audsrv_adpcm_set_volume(0, gBootSndVolume);
}
//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;
audsrv_adpcm_init();
sfxInitDefaults();
sfxVolume();
//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);
}
}
//boot sound only needs to be read/loaded at init
if (bootSnd) {
i = 0;
ret = getFadeDelay(sound_path);
if (ret != 0)
gFadeDelay = 1200;
}
loaded = 0;
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);
}
}
ret = sfxLoad(&sfx_files[i], &sfx[i]);
if (ret == 0)
{
loaded++;
}
else
{
LOG("SFX: failed to load %s, error %d\n", full_path, ret);
}
}
return loaded;
}
void sfxPlay(int id)
{
if (gEnableSFX) {
audsrv_ch_play_adpcm(id, &sfx[id]);
}
}