forked from libretro/libretro-lutro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
652 lines (529 loc) · 17.6 KB
/
audio.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
#include "audio.h"
#include "lutro.h"
#include "compat/strl.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <file/file_path.h>
#include <audio/conversion/float_to_s16.h>
#include <math.h>
#include <assert.h>
#include <errno.h>
/* TODO/FIXME - no sound on big-endian */
// any source which is playing must maintain a ref in lua, to avoid __gc.
typedef struct {
audio_Source* source;
int lua_ref;
} audioSourceWithRef;
static int num_sources = 0;
static audioSourceWithRef* sources_playing = NULL;
static float volume = 1.0;
#define CHANNELS 2
static int16_t saturate(mixer_presaturate_t in) {
if (in >= INT16_MAX) { return INT16_MAX; }
if (in <= INT16_MIN) { return INT16_MIN; }
return cvt_presaturate_to_int16(in);
}
int audio_sources_nullify_refs(const audio_Source* source)
{
int counted = 0;
if (!source) return 0;
// rather than crash, let's nullify any known references here,
// even if they're currently playing (they'll be cut to silence)
for(int i=0; i<num_sources; ++i)
{
audioSourceWithRef* srcref = &sources_playing[i];
if (srcref->source == source)
{
if (srcref->source->state != AUDIO_STOPPED)
++counted;
// do not free - the pointers in sources are lua user data
srcref->source = NULL;
srcref->lua_ref = LUA_NOREF;
}
}
return counted;
}
// unrefs stopped sounds.
// this is done periodically by lutro to avoid adding lua dependencies to the mixer.
void mixer_unref_stopped_sounds(lua_State* L)
{
// Loop over audio sources
for (int i = 0; i < num_sources; i++)
{
if (sources_playing[i].lua_ref >= 0)
{
audio_Source* source = sources_playing[i].source;
if (!source || source->state == AUDIO_STOPPED)
{
lua_getglobal(L, "refs_audio_playing");
luaL_unref(L, -1, sources_playing[i].lua_ref);
sources_playing[i].lua_ref = LUA_REFNIL;
}
}
if (sources_playing[i].lua_ref < 0)
sources_playing[i].source = NULL;
}
}
void lutro_audio_stop_all(void)
{
// Loop over audio sources
// no cleanup needed, __gc will handle it later after a call to mixer_unref_stopped_sounds()
for (int i = 0; i < num_sources; i++)
{
if (sources_playing[i].source)
sources_playing[i].source->state = AUDIO_STOPPED;
}
}
void mixer_render(int16_t *buffer)
{
static mixer_presaturate_t presaturateBuffer[AUDIO_FRAMES * CHANNELS];
memset(presaturateBuffer, 0, AUDIO_FRAMES * CHANNELS * sizeof(mixer_presaturate_t));
presaturate_buffer_desc bufdesc;
bufdesc.data = presaturateBuffer;
bufdesc.channels = CHANNELS;
bufdesc.samplelen = AUDIO_FRAMES;
// Loop over audio sources
for (int i = 0; i < num_sources; i++)
{
audio_Source* source = sources_playing[i].source;
if (!source)
continue;
if (source->state == AUDIO_STOPPED)
continue;
// options here are to premultiply source volumes with master volume, or apply master volume at the end of mixing
// during the saturation step. Each approach has its strengths and weaknesses and overall neither differs much when
// using float or double for presaturation buffer (see final saturation step below)
float srcvol = source->volume;
// Decoder Seek Position Note:
// It's unclear if a decoder can be shared by multiple sources, so always seek the decoder for each chunk.
// Our decoder APIs internally optimize away redundant seeks.
if (source->oggData)
{
decOgg_seek(source->oggData, source->sndpos);
bool finished = decOgg_decode(source->oggData, &bufdesc, srcvol, source->loop);
if (finished)
{
decOgg_seek(source->oggData, 0); // see notes above
source->state = AUDIO_STOPPED;
source->sndpos = 0;
}
source->sndpos = decOgg_sampleTell(source->oggData);
continue;
}
if (source->wavData)
{
decWav_seek(source->wavData, source->sndpos); // see notes above
bool finished = decWav_decode(source->wavData, &bufdesc, srcvol, source->loop);
if (finished)
{
decWav_seek(source->wavData, 0);
source->state = AUDIO_STOPPED;
}
source->sndpos = decWav_sampleTell(source->wavData);
continue;
}
if (source->sndta)
{
snd_SoundData* sndta = source->sndta;
int total_mixed = 0;
while (total_mixed < AUDIO_FRAMES)
{
int mixchunksz = AUDIO_FRAMES - total_mixed;
int remaining = sndta->numSamples - source->sndpos;
if (mixchunksz > remaining)
{
mixchunksz = remaining;
}
if (sndta->numChannels == 1)
{
for (int j = 0; j < mixchunksz; ++j, ++total_mixed, ++source->sndpos)
{
presaturateBuffer[(total_mixed*2) + 0] += sndta->data[source->sndpos] * srcvol;
presaturateBuffer[(total_mixed*2) + 1] += sndta->data[source->sndpos] * srcvol;
}
}
if (sndta->numChannels == 2)
{
for (int j = 0; j < mixchunksz; ++j, ++total_mixed, ++source->sndpos)
{
presaturateBuffer[(total_mixed*2) + 0] += sndta->data[(source->sndpos*2) + 0] * srcvol;
presaturateBuffer[(total_mixed*2) + 1] += sndta->data[(source->sndpos*2) + 1] * srcvol;
}
}
assert(source->sndpos <= sndta->numSamples);
assert(total_mixed <= AUDIO_FRAMES);
if (source->sndpos == sndta->numSamples)
{
source->sndpos = 0;
if (!source->loop)
{
source->state = AUDIO_STOPPED;
break;
}
}
}
}
}
// final saturation step - downsample.
float mastervol_and_scale_to_int16 = volume * 32767;
for (int j = 0; j < AUDIO_FRAMES * CHANNELS; j++)
{
buffer[j] = saturate(presaturateBuffer[j] * mastervol_and_scale_to_int16);
}
}
int lutro_audio_preload(lua_State *L)
{
static luaL_Reg gfx_funcs[] = {
{ "play", audio_play },
{ "stop", audio_stop },
{ "newSource", audio_newSource },
{ "getVolume", audio_getVolume },
{ "setVolume", audio_setVolume },
{NULL, NULL}
};
lutro_ensure_global_table(L, "lutro");
luaL_newlib(L, gfx_funcs);
lua_setfield(L, -2, "audio");
return 1;
}
void lutro_audio_init(lua_State* L)
{
num_sources = 0;
sources_playing = NULL;
volume = 1.0;
lua_newtable(L);
lua_setglobal(L, "refs_audio_playing");
}
void lutro_audio_deinit()
{
if (!sources_playing) return;
// lua owns most of our objects so there's only two proper ways to deinit audio:
// 1. assume luaState has been forcibly destroyed without its own cleanup.
// 2. run lua_close() and let it clean most of this up first.
lutro_audio_stop_all();
int counted = 0;
for (int i = 0; i < num_sources; i++)
{
if (sources_playing[i].source)
++counted;
}
if (counted)
{
fprintf(stderr, "Found %d leaked audio source references. Was lua_close() called first?\n", counted);
//assert(false);
return;
}
free(sources_playing);
sources_playing = NULL;
num_sources = 0;
}
static int find_empty_source_slot(audio_Source* self)
{
for(int i=0; i<num_sources; ++i)
{
// it's possible a stopped source is still in the sources_playing list, since cleanup
// operations are deferred. This is OK and expected, just use the slot that's still assigned...
if (!sources_playing[i].source || sources_playing[i].source == self)
return i;
}
return -1;
}
int audio_newSource(lua_State *L)
{
int n = lua_gettop(L);
if (n != 1 && n != 2)
return luaL_error(L, "lutro.audio.newSource requires 1 or 2 arguments, %d given.", n);
audio_Source* self = (audio_Source*)lua_newuserdata(L, sizeof(audio_Source));
self->oggData = NULL;
self->wavData = NULL;
self->sndta = NULL;
self->lua_ref_sndta = LUA_REFNIL;
void *p = lua_touserdata(L, 1);
if (p == NULL)
{
// when matching file paths, only lua string types are OK.
// lua_tostring will convert numbers into strings implicitly, which is not what we want.
luaL_checktype(L, 1, LUA_TSTRING); // explicit non-converted string type test
const char* path = lua_tostring(L, 1);
AssetPathInfo asset;
lutro_assetPath_init(&asset, path);
if (strstr(asset.ext, "ogg"))
{
self->oggData = malloc(sizeof(dec_OggData));
decOgg_init(self->oggData, asset.fullpath);
}
if (strstr(asset.ext, "wav"))
{
self->wavData = malloc(sizeof(dec_WavData));
decWav_init(self->wavData, asset.fullpath);
}
}
else
{
snd_SoundData* sndta = (snd_SoundData*)luaL_checkudata(L, 1, "SoundData");
self->sndta = sndta;
lua_pushvalue(L, 1); // push ref to SoundData parameter
self->lua_ref_sndta = luaL_ref(L, LUA_REGISTRYINDEX);
}
self->loop = false;
self->volume = 1.0;
self->sndpos = 0;
self->state = AUDIO_STOPPED;
if (luaL_newmetatable(L, "Source") != 0)
{
static luaL_Reg audio_funcs[] = {
{ "play", audio_play }, /* We can reuse audio_play and */
{ "stop", audio_stop }, /* audio_stop here. */
{ "setLooping", source_setLooping },
{ "isLooping", source_isLooping },
{ "isStopped", source_isStopped },
{ "isPaused", source_isPaused },
{ "isPlaying", source_isPlaying },
{ "setVolume", source_setVolume },
{ "getVolume", source_getVolume },
{ "seek", source_seek },
{ "tell", source_tell },
{ "setPitch", source_setPitch },
{ "getPitch", source_getPitch },
{ "__gc", source_gc },
{NULL, NULL}
};
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, source_gc);
lua_setfield( L, -2, "__gc" );
luaL_setfuncs(L, audio_funcs, 0);
}
lua_setmetatable(L, -2);
return 1;
}
int audio_setVolume(lua_State *L)
{
int n = lua_gettop(L);
if (n != 1)
return luaL_error(L, "lutro.audio.setVolume requires 1 argument, %d given.", n);
volume = (float)luaL_checknumber(L, 1);
return 0;
}
int audio_getVolume(lua_State *L)
{
lua_pushnumber(L, volume);
return 1;
}
int source_setLooping(lua_State *L)
{
int n = lua_gettop(L);
if (n != 2)
return luaL_error(L, "Source:setLooping requires 2 arguments, %d given.", n);
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
bool loop = lua_toboolean(L, 2);
self->loop = loop;
return 0;
}
int source_isLooping(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
lua_pushboolean(L, self->loop);
return 1;
}
int source_isStopped(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
lua_pushboolean(L, (self->state == AUDIO_STOPPED));
return 1;
}
int source_isPaused(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
lua_pushboolean(L, (self->state == AUDIO_PAUSED));
return 1;
}
int source_isPlaying(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
lua_pushboolean(L, (self->state == AUDIO_PLAYING));
return 1;
}
int source_setVolume(lua_State *L)
{
int n = lua_gettop(L);
if (n != 2)
return luaL_error(L, "Source:setVolume requires 2 arguments, %d given.", n);
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
self->volume = (float)luaL_checknumber(L, 2);
return 0;
}
int source_getVolume(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
lua_pushnumber(L, self->volume);
return 1;
}
int source_tell(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
//currently assuming samples vs seconds
//TODO: check if 2nd param is "seconds" or "samples"
// sndpos should always be accurate for any given source or stream.
lua_pushnumber(L, self->sndpos);
return 1;
}
int source_seek(lua_State *L)
{
int n = lua_gettop(L);
if (n != 3)
return luaL_error(L, "Source:seek requires 3 arguments, %d given.", n);
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
const char* type = lua_tostring(L, 3);
intmax_t npSamples = 0;
if (type)
{
if (strcmp(type, "seconds") == 0)
{
double npSeconds = luaL_checknumber(L, 2);
npSamples = npSeconds * 44100.0;
}
else if(strcmp(type, "samples") == 0)
{
npSamples = luaL_checkinteger(L, 2);
}
else
{
return luaL_error(L, "Source:seek third argument is invalid, '%s' given. Expected either 'seconds' or 'samples'.\n", type);
}
}
else
{
// assume samples if third paramter is unspecified
npSamples = luaL_checkinteger(L, 2);
}
if (self->wavData)
{
if (!decWav_seek(self->wavData, npSamples))
{
// TODO: it'd be nice to log with the full lua FILE(LINE): context that's normally prefixed by lua_error,
// in a manner that allows us to log it without stopping the system. --jstine
fprintf(stderr, "WAV decoder seek failed: %s\n", strerror(errno));
}
self->sndpos = decWav_sampleTell(self->wavData);
}
if (self->oggData)
{
decOgg_seek(self->oggData, npSamples);
self->sndpos = decWav_sampleTell(self->wavData);
}
if (self->sndta)
{
self->sndpos = npSamples;
if (self->sndpos > self->sndta->numSamples)
self->sndpos = self->sndta->numSamples;
}
if (npSamples != self->sndpos)
{
// the underlying media source will fixup the seek position...
fprintf(stderr, "warning: seek asked for sample pos %jd, got pos %jd\n", npSamples, self->sndpos);
}
return 0;
}
int source_setPitch(lua_State *L)
{
int n = lua_gettop(L);
if (n != 2)
return luaL_error(L, "Source:setPitch requires 2 arguments, %d given.", n);
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
self->pitch = (float)luaL_checknumber(L, 2);
return 0;
}
int source_getPitch(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
lua_pushnumber(L, self->pitch);
return 1;
}
int source_gc(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
// todo - add some info to help identify the offending leaker.
// (don't get carried away tho - this message is only really useful to lutro core devs since
// it indiciates a failure of our internal Lua/C glue)
int leaks = audio_sources_nullify_refs(self);
if (leaks)
{
fprintf(stderr, "source_gc: playing audio references were nullified.\n");
//assert(false);
}
luaL_unref(L, LUA_REGISTRYINDEX, self->lua_ref_sndta);
self->lua_ref_sndta = LUA_REFNIL;
if (self->wavData)
{
if (self->wavData->fp)
fclose(self->wavData->fp);
free(self->wavData);
}
if (self->oggData)
{
ov_clear(&self->oggData->vf);
free(self->oggData);
}
(void)self;
return 0;
}
// audio.play returns nothing, but source:play returns a boolean.
// it's OK enough for us to always return a boolean.
int audio_play(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
//play pos should not reset if play called again before finished
//as in Love2D, game code should explicitly call stop or seek(0, nil) before play to reset pos if already playing
if (self->state == AUDIO_PLAYING)
return 0; // nothing to do.
if (self->state == AUDIO_PAUSED)
{
self->state = AUDIO_PLAYING;
return 0;
}
assert(self->state == AUDIO_STOPPED);
self->state = AUDIO_PLAYING;
// add a ref to our playing audio registry. this blocks __gc until the ref(s) are removed.
int slot = find_empty_source_slot(self);
if (slot < 0)
{
slot = num_sources++;
sources_playing = (audioSourceWithRef*)realloc(sources_playing, num_sources * sizeof(audioSourceWithRef));
sources_playing[slot].lua_ref = LUA_REFNIL;
sources_playing[slot].source = NULL;
}
// assume that find_empty_source_slot with either return the current slot, or an empty one.
if (sources_playing[slot].lua_ref < 0)
{
lua_getglobal(L, "refs_audio_playing");
lua_pushvalue(L, 1); // push ref to Source parameter
sources_playing[slot].source = self;
sources_playing[slot].lua_ref = luaL_ref(L, -2);
}
else
{
// existing ref means it should be our same source already.
assert(sources_playing[slot].source == self);
}
// for now sources always succeed in lutro.
// the only reason for a source to fail in Love2D is because it has a limited number of mixer
// voices internally that it allows. Lutro has no hard limit on mixer voices.
lua_pushboolean(L, 1);
return 1;
}
// returns nothing.
int audio_stop(lua_State *L)
{
audio_Source* self = (audio_Source*)luaL_checkudata(L, 1, "Source");
if (self->state == AUDIO_STOPPED)
return 0;
self->sndpos = 0;
self->state = AUDIO_STOPPED;
// unref will be handled via periodic invocation of mixer_unref_stopped_sounds()
//lua_getglobal(L, "refs_audio_playing");
//luaL_unref(L, -1, self->lua_ref);
//self->lua_ref = LUA_REFNIL;
return 0;
}