-
Notifications
You must be signed in to change notification settings - Fork 3
/
sndwav.c
432 lines (335 loc) · 12 KB
/
sndwav.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <kos/mutex.h>
#include <kos/thread.h>
#include <dc/sound/stream.h>
#include "sndwav.h"
#include "libwav.h"
/* Keep track of things from the Driver side */
#define SNDDRV_STATUS_NULL 0x00
#define SNDDRV_STATUS_READY 0x01
#define SNDDRV_STATUS_DONE 0x02
/* Keep track of things from the Decoder side */
#define SNDDEC_STATUS_NULL 0x00
#define SNDDEC_STATUS_READY 0x01
#define SNDDEC_STATUS_STREAMING 0x02
#define SNDDEC_STATUS_PAUSING 0x03
#define SNDDEC_STATUS_STOPPING 0x04
#define SNDDEC_STATUS_RESUMING 0x05
typedef void *(*snddrv_cb)(snd_stream_hnd_t, int, int*);
typedef struct {
/* The buffer on the AICA side */
snd_stream_hnd_t shnd;
/* We either read the wav data from a file or
we read from a buffer */
file_t wave_file;
const uint8_t *wave_buf;
/* Contains the buffer that we are going to send
to the AICA in the callback. Should be 32-byte
aligned */
uint8_t *drv_buf;
/* Status of the stream that can be started, stopped
paused, ready. etc */
volatile int status;
snddrv_cb callback;
uint32_t loop;
uint32_t vol; /* 0-255 */
uint32_t format; /* Wave format */
uint32_t channels; /* 1-Mono/2-Stereo */
uint32_t sample_rate; /* 44100Hz */
uint32_t sample_size; /* 4/8/16-Bit */
/* Offset into the file or buffer where the audio
data starts */
uint32_t data_offset;
/* The length of the audio data */
uint32_t data_length;
/* Used only in reading wav data from a buffer
and not a file */
uint32_t buf_offset;
} snddrv_hnd;
static snddrv_hnd streams[SND_STREAM_MAX];
static volatile int sndwav_status = SNDDRV_STATUS_NULL;
static kthread_t *audio_thread;
static mutex_t stream_mutex = MUTEX_INITIALIZER;
static void *sndwav_thread(void *param);
static void *wav_file_callback(snd_stream_hnd_t hnd, int req, int *done);
static void *wav_buf_callback(snd_stream_hnd_t hnd, int req, int *done);
int wav_init(void) {
int i;
if(snd_stream_init() < 0)
return 0;
for(i = 0; i < SND_STREAM_MAX; i++) {
streams[i].shnd = SND_STREAM_INVALID;
streams[i].vol = 255;
streams[i].status = SNDDEC_STATUS_NULL;
streams[i].callback = NULL;
}
audio_thread = thd_create(0, sndwav_thread, NULL);
if(audio_thread != NULL) {
sndwav_status = SNDDRV_STATUS_READY;
return 1;
}
else {
return 0;
}
}
void wav_shutdown(void) {
int i;
sndwav_status = SNDDRV_STATUS_DONE;
thd_join(audio_thread, NULL);
for(i = 0; i < SND_STREAM_MAX; i++) {
wav_destroy(i);
}
}
void wav_destroy(wav_stream_hnd_t hnd) {
if(streams[hnd].shnd == SND_STREAM_INVALID)
return;
mutex_lock(&stream_mutex);
snd_stream_destroy(streams[hnd].shnd);
streams[hnd].shnd = SND_STREAM_INVALID;
streams[hnd].status = SNDDEC_STATUS_NULL;
streams[hnd].vol = 255;
streams[hnd].callback = NULL;
if(streams[hnd].wave_file != FILEHND_INVALID)
fs_close(streams[hnd].wave_file);
if(streams[hnd].drv_buf) {
free(streams[hnd].drv_buf);
streams[hnd].drv_buf = NULL;
}
mutex_unlock(&stream_mutex);
}
wav_stream_hnd_t wav_create(const char *filename, int loop) {
int fn_len;
file_t file;
WavFileInfo info;
wav_stream_hnd_t index;
if(filename == NULL)
return SND_STREAM_INVALID;
file = fs_open(filename, O_RDONLY);
if(file == FILEHND_INVALID)
return SND_STREAM_INVALID;
index = snd_stream_alloc(wav_file_callback, SND_STREAM_BUFFER_MAX);
if(index == SND_STREAM_INVALID) {
fs_close(file);
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
fn_len = strlen(filename);
/* Check for ".raw" or ".pcm" extension */
if (fn_len >= 4 &&
((strcmp(&filename[fn_len - 4], ".raw") == 0) ||
(strcmp(&filename[fn_len - 4], ".pcm") == 0))) {
wav_get_info_cdda(file, &info);
}
/* Check for ".adpcm" extension */
else if (fn_len >= 6 && strcmp(&filename[fn_len - 6], ".adpcm") == 0) {
wav_get_info_adpcm(file, &info);
}
/* Default case: handle other file types */
else if(!wav_get_info_file(file, &info)) {
fs_close(file);
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
streams[index].drv_buf = memalign(32, SND_STREAM_BUFFER_MAX);
if(streams[index].drv_buf == NULL) {
fs_close(file);
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
streams[index].shnd = index;
streams[index].wave_file = file;
streams[index].loop = loop;
streams[index].callback = wav_file_callback;
streams[index].format = info.format;
streams[index].channels = info.channels;
streams[index].sample_rate = info.sample_rate;
streams[index].sample_size = info.sample_size;
streams[index].data_length = info.data_length;
streams[index].data_offset = info.data_offset;
fs_seek(streams[index].wave_file, streams[index].data_offset, SEEK_SET);
streams[index].status = SNDDEC_STATUS_READY;
return index;
}
wav_stream_hnd_t wav_create_fd(file_t file, int loop) {
WavFileInfo info;
wav_stream_hnd_t index;
if(file == FILEHND_INVALID)
return SND_STREAM_INVALID;
index = snd_stream_alloc(wav_file_callback, SND_STREAM_BUFFER_MAX);
if(index == SND_STREAM_INVALID) {
fs_close(file);
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
if(!wav_get_info_file(file, &info)) {
fs_close(file);
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
streams[index].drv_buf = memalign(32, SND_STREAM_BUFFER_MAX);
if(streams[index].drv_buf == NULL) {
fs_close(file);
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
streams[index].shnd = index;
streams[index].wave_file = file;
streams[index].loop = loop;
streams[index].callback = wav_file_callback;
streams[index].format = info.format;
streams[index].channels = info.channels;
streams[index].sample_rate = info.sample_rate;
streams[index].sample_size = info.sample_size;
streams[index].data_length = info.data_length;
streams[index].data_offset = info.data_offset;
fs_seek(streams[index].wave_file, streams[index].data_offset, SEEK_SET);
streams[index].status = SNDDEC_STATUS_READY;
return index;
}
wav_stream_hnd_t wav_create_buf(const uint8_t *buf, int loop) {
WavFileInfo info;
wav_stream_hnd_t index;
if(buf == NULL)
return SND_STREAM_INVALID;
index = snd_stream_alloc(wav_file_callback, SND_STREAM_BUFFER_MAX);
if(index == SND_STREAM_INVALID) {
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
if(!wav_get_info_buffer(buf, &info)) {
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
streams[index].drv_buf = memalign(32, SND_STREAM_BUFFER_MAX);
if(streams[index].drv_buf == NULL) {
snd_stream_destroy(index);
return SND_STREAM_INVALID;
}
streams[index].shnd = index;
streams[index].wave_buf = buf;
streams[index].loop = loop;
streams[index].callback = wav_buf_callback;
streams[index].format = info.format;
streams[index].channels = info.channels;
streams[index].sample_rate = info.sample_rate;
streams[index].sample_size = info.sample_size;
streams[index].data_length = info.data_length;
streams[index].data_offset = info.data_offset;
streams[index].buf_offset = info.data_offset;
streams[index].status = SNDDEC_STATUS_READY;
return index;
}
void wav_play(wav_stream_hnd_t hnd) {
if(streams[hnd].status == SNDDEC_STATUS_STREAMING)
return;
streams[hnd].status = SNDDEC_STATUS_RESUMING;
}
void wav_pause(wav_stream_hnd_t hnd) {
if(streams[hnd].status == SNDDEC_STATUS_READY ||
streams[hnd].status == SNDDEC_STATUS_PAUSING)
return;
streams[hnd].status = SNDDEC_STATUS_PAUSING;
}
void wav_stop(wav_stream_hnd_t hnd) {
if(streams[hnd].status == SNDDEC_STATUS_READY ||
streams[hnd].status == SNDDEC_STATUS_STOPPING)
return;
streams[hnd].status = SNDDEC_STATUS_STOPPING;
}
void wav_volume(wav_stream_hnd_t hnd, int vol) {
if(streams[hnd].shnd == SND_STREAM_INVALID)
return;
if(vol > 255)
vol = 255;
if(vol < 0)
vol = 0;
streams[hnd].vol = vol;
snd_stream_volume(streams[hnd].shnd, streams[hnd].vol);
}
int wav_is_playing(wav_stream_hnd_t hnd) {
return streams[hnd].status == SNDDEC_STATUS_STREAMING;
}
void wav_add_filter(wav_stream_hnd_t hnd, wav_filter filter, void *obj) {
snd_stream_filter_add(streams[hnd].shnd, filter, obj);
}
void wav_remove_filter(wav_stream_hnd_t hnd, wav_filter filter, void *obj) {
snd_stream_filter_remove(streams[hnd].shnd, filter, obj);
}
static void *sndwav_thread(void *param) {
(void)param;
int i;
while(sndwav_status != SNDDRV_STATUS_DONE) {
mutex_lock(&stream_mutex);
for(i = 0; i < SND_STREAM_MAX; i++) {
switch(streams[i].status) {
case SNDDEC_STATUS_RESUMING:
if(streams[i].sample_size == 16) {
snd_stream_start(streams[i].shnd, streams[i].sample_rate, streams[i].channels - 1);
} else if(streams[i].sample_size == 8) {
snd_stream_start_pcm8(streams[i].shnd, streams[i].sample_rate, streams[i].channels - 1);
} else if(streams[i].sample_size == 4) {
snd_stream_start_adpcm(streams[i].shnd, streams[i].sample_rate, streams[i].channels - 1);
}
streams[i].status = SNDDEC_STATUS_STREAMING;
break;
case SNDDEC_STATUS_PAUSING:
snd_stream_stop(streams[i].shnd);
streams[i].status = SNDDEC_STATUS_READY;
break;
case SNDDEC_STATUS_STOPPING:
snd_stream_stop(streams[i].shnd);
if(streams[i].wave_file != FILEHND_INVALID)
fs_seek(streams[i].wave_file, streams[i].data_offset, SEEK_SET);
else
streams[i].buf_offset = streams[i].data_offset;
streams[i].status = SNDDEC_STATUS_READY;
break;
case SNDDEC_STATUS_STREAMING:
snd_stream_poll(streams[i].shnd);
break;
case SNDDEC_STATUS_READY:
default:
break;
}
}
mutex_unlock(&stream_mutex);
thd_sleep(50);
}
return NULL;
}
static void *wav_file_callback(snd_stream_hnd_t hnd, int req, int* done) {
int read = fs_read(streams[hnd].wave_file, streams[hnd].drv_buf, req);
if(read != req) {
fs_seek(streams[hnd].wave_file, streams[hnd].data_offset, SEEK_SET);
if(streams[hnd].loop) {
fs_read(streams[hnd].wave_file, streams[hnd].drv_buf, req);
}
else {
snd_stream_stop(streams[hnd].shnd);
streams[hnd].status = SNDDEC_STATUS_READY;
return NULL;
}
}
*done = req;
return streams[hnd].drv_buf;
}
static void *wav_buf_callback(snd_stream_hnd_t hnd, int req, int* done) {
if((streams[hnd].data_length-(streams[hnd].buf_offset - streams[hnd].data_offset)) >= req)
memcpy(streams[hnd].drv_buf, streams[hnd].wave_buf+streams[hnd].buf_offset, req);
else {
streams[hnd].buf_offset = streams[hnd].data_offset;
if(streams[hnd].loop) {
memcpy(streams[hnd].drv_buf, streams[hnd].wave_buf+streams[hnd].buf_offset, req);
}
else {
snd_stream_stop(streams[hnd].shnd);
streams[hnd].status = SNDDEC_STATUS_READY;
return NULL;
}
}
streams[hnd].buf_offset += *done = req;
return streams[hnd].drv_buf;
}