-
Notifications
You must be signed in to change notification settings - Fork 2
/
xsound.c
479 lines (433 loc) · 11.2 KB
/
xsound.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
/**
* This file belongs to the 'xlab' game engine.
* Copyright 2009 xfacter
* Copyright 2016 wickles
* This work is licensed under the LGPLv3
* subject to all terms as reproduced in the included LICENSE file.
*/
#include <stdio.h>
#include <pspaudiolib.h>
#include <pspaudio.h>
#include "xmem.h"
#include "xmath.h"
#include "xsound.h"
typedef struct sound_instance {
u16 playstate;
u32 playptr;
u32 playptr_fraction;
u32 rateratio;
u16 loop;
u16 panmode;
float pan;
float volume;
xSoundBuffer* buffer;
xSound3dSource* src;
} sound_instance;
int num_sounds = 0;
sound_instance* sounds = NULL;
static void sound_callback(void *buf, unsigned int reqn, void *pdata)
{
if (sounds == NULL) return;
s16* buffer = buf;
int n;
for (n = 0; n < reqn; n++)
{
int out_right = 0;
int out_left = 0;
int i;
for (i = 0; i < num_sounds; i++)
{
sound_instance* inst = &sounds[i];
if (inst->playstate != X_SOUND_PLAY)
continue;
u32 fraction = inst->playptr_fraction + inst->rateratio;
inst->playptr += fraction >> 16;
inst->playptr_fraction = fraction & 0xffff;
if (inst->volume <= 0.0f)
continue;
if (inst->playptr >= inst->buffer->samplecount)
{
if (inst->loop == X_SOUND_LOOP)
{
inst->playptr = 0;
inst->playptr_fraction = 0;
}
else
{
xSoundSetState(i, X_SOUND_STOP);
continue;
}
}
int index = inst->buffer->channels * inst->playptr;
if (inst->buffer->samplesize == 1)
{
s8* data8 = (s8*)inst->buffer->data;
if (inst->buffer->channels == 1)
{
out_left += (int)(data8[index]*256 * inst->volume * (1.0f - inst->pan));
out_right += (int)(data8[index]*256 * inst->volume * inst->pan);
}
else
{
if (inst->panmode == X_SOUND_COMBINED)
{
out_left += (int)((data8[index+0] + data8[index+1])*256*0.5f * inst->volume * (1.0f - inst->pan));
out_right += (int)((data8[index+0] + data8[index+1])*256*0.5f * inst->volume * inst->pan);
}
else
{
out_left += (int)(data8[index+0]*256 * inst->volume * (1.0f - inst->pan));
out_right += (int)(data8[index+1]*256 * inst->volume * inst->pan);
}
}
}
else
{
s16* data16 = (s16*)inst->buffer->data;
if (inst->buffer->channels == 1)
{
out_left += (int)(data16[index] * inst->volume * (1.0f - inst->pan));
out_right += (int)(data16[index] * inst->volume * inst->pan);
}
else
{
if (inst->panmode == X_SOUND_COMBINED)
{
out_left += (int)((data16[index+0] + data16[index+1])*0.5f * inst->volume * (1.0f - inst->pan));
out_right += (int)((data16[index+0] + data16[index+1])*0.5f * inst->volume * inst->pan);
}
else
{
out_left += (int)(data16[index+0] * inst->volume * (1.0f - inst->pan));
out_right += (int)(data16[index+1] * inst->volume * inst->pan);
}
}
}
}
if (out_left < -32768) out_left = -32768;
else if (out_left > 32767) out_left = 32767;
if (out_right < -32768) out_right = -32768;
else if (out_right > 32767) out_right = 32767;
*(buffer++) = (s16)out_left;
*(buffer++) = (s16)out_right;
}
}
int xSoundInit(int max_sounds)
{
X_LOG("Attempting to initialize sounds...");
if (sounds != NULL) return 1;
sounds = (sound_instance*)x_malloc(max_sounds*sizeof(sound_instance));
if (sounds == NULL) return 1;
num_sounds = max_sounds;
xSoundSetStateAll(X_SOUND_STOP, 0);
pspAudioInit();
pspAudioSetChannelCallback(X_SOUND_CHANNEL, sound_callback, 0);
pspAudioSetVolume(X_SOUND_CHANNEL, PSP_VOLUME_MAX, PSP_VOLUME_MAX);
X_LOG("Successfully initialized sounds.");
return 0;
}
void xSoundEnd()
{
X_LOG("Attempting to end sounds...");
if (sounds == NULL) return;
pspAudioEnd();
num_sounds = 0;
x_free(sounds);
sounds = NULL;
X_LOG("Successfully ended sounds.");
}
void xSoundGlobalVolume(float volume)
{
if (volume < 0.0f) volume = 0.0f;
if (volume > 1.0f) volume = 1.0f;
int vol = (int)(volume*PSP_VOLUME_MAX);
pspAudioSetVolume(X_SOUND_CHANNEL, vol, vol);
}
typedef struct {
u32 ChunkID;
u32 ChunkSize;
u32 Format;
} riff_chunk;
typedef struct {
u32 Subchunk1ID;
u32 Subchunk1Size;
u16 AudioFormat;
u16 NumChannels;
u32 SampleRate;
u32 ByteRate;
u16 BlockAlign;
u16 BitsPerSample;
} wave_chunk;
typedef struct {
u32 Subchunk2ID;
u32 Subchunk2Size;
} data_chunk;
#define WAV_HEAD_RIFF ('R'<<0|'I'<<8|'F'<<16|'F'<<24) /* "RIFF" (0x46464952) */
#define WAV_HEAD_FORMAT ('W'<<0|'A'<<8|'V'<<16|'E'<<24) /* "WAVE" (0x45564157) */
#define WAV_HEAD_SUB1ID ('f'<<0|'m'<<8|'t'<<16|' '<<24) /* "fmt " (0x20746d66) */
#define WAV_HEAD_SUB2ID ('d'<<0|'a'<<8|'t'<<16|'a'<<24) /* "data" (0x61746164) */
#define WAV_PCM (1)
xSoundBuffer* xSoundLoadBufferWav(char* filename)
{
if (filename == NULL) return NULL;
X_LOG("Attempting to load WAV buffer \"%s\"...", filename);
xSoundBuffer* buf = (xSoundBuffer*)x_malloc(sizeof(xSoundBuffer));
if (buf == NULL) return NULL;
buf->data = NULL;
FILE* file = fopen(filename, "rb");
if (file == NULL)
{
xSoundFreeBuffer(buf);
return NULL;
}
riff_chunk riff_c;
fread(&riff_c, sizeof(riff_chunk), 1, file);
wave_chunk wave_c;
fread(&wave_c, sizeof(wave_chunk), 1, file);
data_chunk data_c;
fseek(file, wave_c.Subchunk1Size-16, SEEK_CUR);
fread(&data_c, sizeof(data_chunk), 1, file);
X_LOG("WAV: ChunkID: 0x%08x, ChunkSize: %u, Format: 0x%08x, Subchunk1ID: 0x%08x, Subchunk1Size: %u \
AudioFormat: %u, NumChannels: %u, SampleRate: %u, ByteRate: %u, BlockAlign: %u, BitsPerSample: %u, Subchunk2ID: 0x%08x, Subchunk2Size: %u",
filename, riff_c.ChunkID, riff_c.ChunkSize, riff_c.Format, wave_c.Subchunk1ID, wave_c.Subchunk1Size,
wave_c.AudioFormat, wave_c.NumChannels, wave_c.SampleRate, wave_c.ByteRate, wave_c.BlockAlign, wave_c.BitsPerSample, data_c.Subchunk2ID, data_c.Subchunk2Size);
if (riff_c.ChunkID != WAV_HEAD_RIFF || riff_c.Format != WAV_HEAD_FORMAT ||
wave_c.AudioFormat != WAV_PCM || wave_c.Subchunk1ID != WAV_HEAD_SUB1ID ||
data_c.Subchunk2ID != WAV_HEAD_SUB2ID ||
(wave_c.NumChannels != 1 && wave_c.NumChannels != 2) ||
(wave_c.BitsPerSample != 8 && wave_c.BitsPerSample != 16))
{
fclose(file);
xSoundFreeBuffer(buf);
return NULL;
}
u32 datalength = data_c.Subchunk2Size;
buf->channels = wave_c.NumChannels;
buf->samplesize = wave_c.BitsPerSample/8;
buf->samplerate = wave_c.SampleRate;
buf->samplecount = datalength/(buf->channels*buf->samplesize);
buf->data = x_malloc(datalength);
if (buf->data == NULL)
{
fclose(file);
xSoundFreeBuffer(buf);
return NULL;
}
fread(buf->data, datalength, 1, file);
fclose(file);
buf->def_vol = 1.0f;
buf->def_pitch = 1.0f;
buf->def_pan = 0.0f;
buf->def_panmode = X_SOUND_SEPARATE;
buf->def_loop = X_SOUND_NO_LOOP;
X_LOG("Successfully load WAV buffer.");
return buf;
}
void xSoundFreeBuffer(xSoundBuffer* buf)
{
X_LOG("Freeing WAV buffer.");
if (buf != NULL)
{
if (buf->data != NULL)
{
x_free(buf->data);
}
x_free(buf);
}
}
int xSoundPlay(xSoundBuffer* buf)
{
if (buf == NULL || sounds == NULL) return -1;
int i;
for (i = 0; i < num_sounds; i++)
{
if (xSoundGetState(i) == X_SOUND_STOP)
{
sounds[i].playptr = 0;
sounds[i].playptr_fraction = 0;
sounds[i].src = NULL;
sounds[i].buffer = buf;
xSoundSetLoop(i, buf->def_loop);
xSoundSetPanMode(i, buf->def_panmode);
xSoundSetVolume(i, buf->def_vol);
xSoundSetPan(i, buf->def_pan);
xSoundSetPitch(i, buf->def_pitch);
sounds[i].playstate = X_SOUND_PLAY;
return i;
}
}
return -1;
}
int xSoundSetState(int ref, int state)
{
if (sounds == NULL || ref < 0) return 1;
switch (state)
{
case X_SOUND_STOP:
if (sounds[ref].playstate == X_SOUND_STOP)
{
return 1;
}
else
{
sounds[ref].playstate = X_SOUND_STOP;
return 0;
}
case X_SOUND_PLAY:
if (sounds[ref].playstate == X_SOUND_PAUSE)
{
sounds[ref].playstate = X_SOUND_PLAY;
return 0;
}
else
{
//return xSoundPlay(i);
return 1;
}
case X_SOUND_PAUSE:
if (sounds[ref].playstate == X_SOUND_PLAY)
{
sounds[ref].playstate = X_SOUND_PAUSE;
return 0;
}
else
{
return 1;
}
}
return 1;
}
int xSoundGetState(int ref)
{
if (sounds == NULL || ref < 0) return X_SOUND_STOP;
return sounds[ref].playstate;
}
int xSoundSetStateAll(int state, int skip_mask)
{
if (sounds == NULL) return 0;
int changed = 0;
int i;
for (i = 0; i < num_sounds; i++)
{
if (!(skip_mask & (1<<i)))
{
if (xSoundSetState(i, state) == 0)
{
changed += 1;
}
}
}
return changed;
}
void xSoundSetLoop(int ref, int loop)
{
if (sounds == NULL || ref < 0) return;
sounds[ref].loop = loop;
}
void xSoundSetPanMode(int ref, int panmode)
{
if (sounds == NULL || ref < 0) return;
sounds[ref].panmode = panmode;
}
void xSoundSetVolume(int ref, float volume)
{
if (sounds == NULL || ref < 0) return;
if (volume < 0.0f) volume = 0.0f;
if (volume > 1.0f) volume = 1.0f;
sounds[ref].volume = volume;
}
void xSoundSetPan(int ref, float pan)
{
if (sounds == NULL || ref < 0) return;
if (pan < -1.0f) pan = -1.0f;
if (pan > 1.0f) pan = 1.0f;
pan = (pan + 1.0f)*0.5f;
sounds[ref].pan = pan;
}
void xSoundSetPitch(int ref, float pitch)
{
if (sounds == NULL || ref < 0) return;
if (pitch < 0.0f) pitch = 0.0f;
u32 rateratio = (u32)(pitch*((sounds[ref].buffer->samplerate*0x4000)/11025));
/*
if (rateratio < (2000*0x4000)/11025) rateratio = (2000*0x4000)/11025;
if (rateratio > (100000*0x4000)/11025) rateratio = (100000*0x4000)/11025;
*/
sounds[ref].rateratio = rateratio;
}
float speed_of_sound = 343.3f;
xVector3f listener_pos = {0.0f, 0.0f, 0.0f};
xVector3f listener_right = {1.0f, 0.0f, 0.0f};
xVector3f listener_vel = {0.0f, 0.0f, 0.0f};
void xSound3dSpeedOfSound(float value)
{
speed_of_sound = value;
}
void xSound3dSetListener(ScePspFMatrix4* orientation, ScePspFVector3* vel)
{
if (orientation == NULL || vel == NULL) return;
listener_pos = *(xVector3f*)&orientation->w;
listener_right = *(xVector3f*)&orientation->x;
listener_vel = *(xVector3f*)vel;
}
void sound3d_update(int ref)
{
xVector3f dir;
xVec3Sub(&dir, (xVector3f*)&sounds[ref].src->pos, &listener_pos);
float dist_sq = xVec3SqLength(&dir);
if (dist_sq >= SQR(sounds[ref].src->radius))
{
xSoundSetVolume(ref, 0.0f);
}
else
{
//volume
xSoundSetVolume(ref, 1.0f - x_sqrtf(dist_sq)/sounds[ref].src->radius);
//pan
xVec3Normalize(&dir, &dir);
xSoundSetPan(ref, xVec3Dot(&dir, &listener_right));
//pitch
xVector3f rel_vel;
xVec3Sub(&rel_vel, (xVector3f*)&sounds[ref].src->vel, &listener_vel);
xSoundSetPitch(ref, speed_of_sound / (speed_of_sound + xVec3Dot(&dir, &rel_vel)));
}
}
int xSound3dPlay(xSoundBuffer* buf, xSound3dSource* s, int update)
{
if (buf == NULL || s == NULL || sounds == NULL) return -1;
int i;
for (i = 0; i < num_sounds; i++)
{
if (xSoundGetState(i) == X_SOUND_STOP)
{
sounds[i].playptr = 0;
sounds[i].playptr_fraction = 0;
sounds[i].src = s;
sounds[i].buffer = buf;
xSoundSetLoop(i, buf->def_loop);
xSoundSetPanMode(i, X_SOUND_COMBINED);
sound3d_update(i);
//take this out
if (sounds[i].volume > 0.0f || update)
{
if (!update)
sounds[i].src = NULL;
sounds[i].playstate = X_SOUND_PLAY;
return i;
}
}
}
return -1;
}
void xSound3dUpdate()
{
if (sounds == NULL) return;
int i;
for (i = 0; i < num_sounds; i++)
{
if (xSoundGetState(i) == X_SOUND_PLAY && sounds[i].src != NULL)
{
sound3d_update(i);
}
}
}