better audio queuing
[fishladder.git] / vg / vg_audio.h
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 #define MINIAUDIO_IMPLEMENTATION
4 #include "dr_soft/miniaudio.h"
5
6 #define STB_VORBIS_MAX_CHANNELS 2
7 #include "stb/stb_vorbis.h"
8
9 #define SFX_MAX_SYSTEMS 32
10 #define SFX_FLAG_ONESHOT 0x1
11 #define SFX_FLAG_STEREO 0x2
12 #define SFX_FLAG_REPEAT 0x4
13 #define SFX_FLAG_GHOST 0x8
14 #define FADEOUT_LENGTH 4410
15 #define FADEOUT_DIVISOR (1.f/(float)FADEOUT_LENGTH)
16
17 typedef struct sfx_vol_control sfx_vol_control;
18 typedef struct sfx_system sfx_system;
19
20 struct sfx_vol_control
21 {
22 float val;
23 const char *name;
24 };
25
26 struct sfx_system
27 {
28 // Source buffer start
29 float *source, *replacement;
30
31 // Modifiers
32 sfx_vol_control *vol_src;
33 float vol;
34
35 // Info
36 u32 ch, end, cur;
37 u32 flags;
38
39 // Effects
40 u32 fadeout, fadeout_current;
41
42 sfx_system *thread_clone; // Memory of this structure is copied into thread 2
43
44 // Diagnostic
45 float signal_average; // Current signal volume
46 const char *name;
47 };
48
49 // Set of up to 8 sound effects packed into one
50 typedef struct sfx_set sfx_set;
51 struct sfx_set
52 {
53 float *main;
54 char *sources;
55
56 u32 segments[16]; //from->to,from->to ...
57 u32 numsegments;
58 u32 ch;
59 u32 flags;
60 };
61
62 ma_device g_aud_device;
63 ma_device_config g_aud_dconfig;
64
65 // Thread 1 - audio engine ( spawned from miniaudio.h )
66 // ======================================================
67 sfx_system sfx_sys[SFX_MAX_SYSTEMS];
68 int sfx_sys_len = 0;
69
70 // Thread 0 - Critical transfer section
71 // ======================================================
72 MUTEX_TYPE sfx_mux_t01; // Resources share: 0 & 1
73
74 sfx_system *sfx_q[SFX_MAX_SYSTEMS]; // Stuff changed
75 int sfx_q_len = 0; // How much
76
77 // x / 2
78 // ======================================================
79
80 // g_vol_master is never directly acessed by users
81 float g_master_volume = 1.f;
82
83 // Decompress entire vorbis stream into buffer
84 static float *sfx_vorbis_stream( const unsigned char *data, int len, int channels, u32 *samples )
85 {
86 int err;
87 stb_vorbis *pv = stb_vorbis_open_memory( data, len, &err, NULL );
88
89 if( !pv )
90 {
91 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err );
92 return NULL;
93 }
94
95 u32 length_samples = stb_vorbis_stream_length_in_samples( pv );
96 float *buffer = (float *)malloc( length_samples * channels * sizeof( float ));
97
98 if( !buffer )
99 {
100 stb_vorbis_close( pv );
101 vg_error( "out of memory while allocating sound resource\n" );
102 return NULL;
103 }
104
105 int read_samples = stb_vorbis_get_samples_float_interleaved( pv, channels, buffer, length_samples * channels );
106 if( read_samples != length_samples )
107 {
108 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n", length_samples, read_samples );
109 length_samples = read_samples;
110 }
111
112 stb_vorbis_close( pv );
113 *samples = length_samples;
114 return buffer;
115 }
116
117 static float *sfx_vorbis( const char *strFileName, int channels, u32 *samples )
118 {
119 i64 len;
120 void *filedata = vg_asset_read_s( strFileName, &len );
121
122 if( filedata )
123 {
124 float *wav = sfx_vorbis_stream( filedata, len, channels, samples );
125 free( filedata );
126 return wav;
127 }
128 else
129 {
130 vg_error( "OGG load failed\n" );
131 return NULL;
132 }
133 }
134
135 typedef struct sfx_bgload sfx_bgload_t;
136 struct sfx_bgload
137 {
138 char *path;
139 u32 channels;
140
141 float *buffer;
142 u32 samples;
143
144 void *user;
145
146 void(*OnComplete)(sfx_bgload_t *inf);
147 };
148
149 // Thread worker for background load job
150 void *sfx_vorbis_a_t( void *_inf )
151 {
152 sfx_bgload_t *info = _inf;
153
154 // Load the ogg clip
155 info->buffer = sfx_vorbis( info->path, info->channels, &info->samples );
156 info->OnComplete( info );
157
158 return NULL;
159 }
160
161 // Asynchronous resource load
162 int sfx_vorbis_a( const char *path, int channels, void(*OnComplete)(sfx_bgload_t *inf), void *user )
163 {
164 vg_info( "background job started for: %s\n", path );
165
166 sfx_bgload_t *params = malloc( sizeof( sfx_bgload_t ) );
167 params->path = malloc( strlen( path ) + 1 );
168 strcpy( params->path, path );
169 params->OnComplete = OnComplete;
170 params->user = user;
171 params->channels = channels;
172
173 return vg_thread_run( sfx_vorbis_a_t, params );
174 }
175
176 // Asynchronous load-to-system callback
177 struct sfx_vorbis_a_to_inf
178 {
179 sfx_system *sys;
180 u32 flags;
181 };
182
183 #define SFX_A_FLAG_AUTOSTART 0x1
184 #define SFX_A_FLAG_AUTOFREE 0x2
185
186 /*
187 static int sfx_save( sfx_system *sys );
188
189 // Asynchronous load-to-system callback
190 void sfx_vorbis_a_to_c( sfx_bgload_t *loadinf )
191 {
192 struct sfx_vorbis_a_to_inf *inf = loadinf->user;
193
194 // Mark buffer for deallocation if autofree is set
195 if( inf->flags & SFX_A_FLAG_AUTOFREE )
196 inf->sys->replacement = loadinf->buffer;
197 else
198 inf->sys->source = loadinf->buffer;
199
200 inf->sys->end = loadinf->samples;
201
202 if( inf->flags & SFX_A_FLAG_AUTOSTART )
203 sfx_save( inf->sys );
204
205 free( loadinf->path );
206 free( loadinf );
207 free( inf );
208 }
209
210 // Asynchronous vorbis load into audio system
211 void sfx_vorbis_a_to( sfx_system *sys, const char *strFileName, int channels, u32 flags )
212 {
213 struct sfx_vorbis_a_to_inf *inf = malloc( sizeof( struct sfx_vorbis_a_to_inf ) );
214 inf->flags = flags;
215 inf->sys = sys;
216
217 sys->ch = channels;
218
219 if( !sfx_vorbis_a( strFileName, channels, sfx_vorbis_a_to_c, inf ) )
220 free( inf );
221 }*/
222
223 // 0
224 // ======================================================
225
226 static int sfx_begin_edit( sfx_system *sys )
227 {
228 MUTEX_LOCK( sfx_mux_t01 );
229
230 if( sfx_q_len >= SFX_MAX_SYSTEMS )
231 {
232 MUTEX_UNLOCK( sfx_mux_t01 );
233 vg_warn( "Warning: No free space in sound queue\n" );
234 return 0;
235 }
236
237 return 1;
238 }
239
240 // Mark change to be uploaded to queue system
241 static int sfx_save( sfx_system *sys )
242 {
243 // Mark change in queue
244 sfx_q[ sfx_q_len ++ ] = sys;
245
246 MUTEX_UNLOCK( sfx_mux_t01 );
247
248 return 1;
249 }
250
251 // Edit a volume float, has to be function wrapped because of mutex
252 static void sfx_vol_fset( sfx_vol_control *src, float to )
253 {
254 MUTEX_LOCK( sfx_mux_t01 );
255
256 src->val = to;
257
258 MUTEX_UNLOCK( sfx_mux_t01 );
259 }
260
261 // thread-safe get volume value
262 static float sfx_vol_fget( sfx_vol_control *src )
263 {
264 float val;
265
266 MUTEX_LOCK( sfx_mux_t01 );
267
268 val = src->val;
269
270 MUTEX_UNLOCK( sfx_mux_t01 );
271
272 return val;
273 }
274
275 // thread-safe set master volume
276 static void sfx_set_master( float to )
277 {
278 MUTEX_LOCK( sfx_mux_t01 );
279
280 g_master_volume = to;
281
282 MUTEX_UNLOCK( sfx_mux_t01 );
283 }
284
285 // thread-safe get master volume
286 static float sfx_get_master(void)
287 {
288 float val;
289
290 MUTEX_LOCK( sfx_mux_t01 );
291
292 val = g_master_volume;
293
294 MUTEX_UNLOCK( sfx_mux_t01 );
295
296 return val;
297 }
298
299 void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount );
300
301 // Miniaudio.h init
302 static void vg_audio_init(void)
303 {
304 g_aud_dconfig = ma_device_config_init( ma_device_type_playback );
305 g_aud_dconfig.playback.format = ma_format_f32;
306 g_aud_dconfig.playback.channels = 2;
307 g_aud_dconfig.sampleRate = 44100;
308 g_aud_dconfig.dataCallback = audio_mixer_callback;
309
310 g_aud_dconfig.pUserData = NULL;
311
312 vg_info( "Starting audio engine\n" );
313
314 if( ma_device_init( NULL, &g_aud_dconfig, &g_aud_device ) != MA_SUCCESS )
315 {
316 vg_exiterr( "ma_device failed to initialize" );
317 }
318 else
319 {
320 if( ma_device_start( &g_aud_device ) != MA_SUCCESS )
321 {
322 ma_device_uninit( &g_aud_device );
323 vg_exiterr( "ma_device failed to start" );
324 }
325 }
326 }
327
328 // Shutdown audio device
329 static void vg_audio_free(void)
330 {
331 ma_device_uninit( &g_aud_device );
332 }
333
334 // 1
335 // ======================================================
336
337 // Create and return slot for a sound
338 static sfx_system *sfx_alloc(void)
339 {
340 if( sfx_sys_len >= SFX_MAX_SYSTEMS )
341 return NULL;
342
343 // A conditional is done against this in localization step,
344 // Needs to be initialized.
345 sfx_sys[ sfx_sys_len ].source = NULL;
346
347 return sfx_sys + (sfx_sys_len++);
348 }
349
350 // Fetch samples into pcf
351 static void audio_mixer_getsamples( float *pcf, float *source, u32 cur, u32 ch )
352 {
353 if( ch == 2 )
354 {
355 pcf[0] = source[ cur*2+0 ];
356 pcf[1] = source[ cur*2+1 ];
357 }
358 else
359 {
360 pcf[0] = source[ cur ];
361 pcf[1] = source[ cur ];
362 }
363 }
364
365 // miniaudio.h interface
366 void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount )
367 {
368 // Process incoming sound queue
369 MUTEX_LOCK( sfx_mux_t01 );
370
371 while( sfx_q_len --> 0 )
372 {
373 sfx_system *src = sfx_q[sfx_q_len];
374 sfx_system *clone;
375
376 // This is a 'new' sound if thread_clone not set.
377 if( !src->thread_clone || (src->flags & SFX_FLAG_ONESHOT) )
378 {
379 clone = sfx_alloc();
380 if( !clone )
381 break;
382
383 src->thread_clone = clone;
384 }
385 else
386 {
387 clone = src->thread_clone;
388
389 // Modifying an active system's cursor spawns a small fadeout ghost system
390 if( clone->cur != src->cur )
391 {
392 sfx_system *ghost_system = sfx_alloc();
393
394 if( !ghost_system )
395 break;
396
397 ghost_system->source = clone->source;
398 ghost_system->ch = clone->ch;
399 ghost_system->end = clone->end;
400 ghost_system->cur = clone->cur;
401 ghost_system->flags = SFX_FLAG_GHOST;
402 ghost_system->fadeout = FADEOUT_LENGTH;
403 ghost_system->fadeout_current = FADEOUT_LENGTH;
404 ghost_system->vol_src = clone->vol_src;
405 ghost_system->name = clone->name;
406 ghost_system->thread_clone = src;
407 }
408 }
409
410 // run replacement routine if one is waiting (todo: what is this?)
411 if( src->replacement )
412 {
413 free( src->source );
414
415 src->source = src->replacement;
416 src->replacement = NULL;
417 }
418
419 // Localize data to thread 1's memory pool
420 clone->source = src->source;
421 clone->ch = src->ch;
422 clone->end = src->end;
423 clone->cur = src->cur;
424 clone->flags = src->flags;
425 clone->vol_src = src->vol_src;
426 clone->name = src->name;
427 clone->fadeout = src->fadeout;
428 clone->fadeout_current = src->fadeout_current;
429
430 // loopback pointer, mainly used for persistent sound handles
431 clone->thread_clone = src;
432 }
433
434 sfx_q_len = 0;
435
436 // Pull in volume sliders
437 for( int i = 0; i < sfx_sys_len; i ++ )
438 {
439 sfx_system *sys = sfx_sys + i;
440 sys->vol = sys->thread_clone->vol * g_master_volume;
441
442 if( sys->vol_src )
443 sys->vol *= sys->vol_src->val;
444 }
445
446 MUTEX_UNLOCK( sfx_mux_t01 );
447
448 // Clear buffer ( is necessary ? )
449 float *pOut32F = (float *)pOutBuf;
450 for( int i = 0; i < frameCount * 2; i ++ ){
451 pOut32F[i] = 0.f;
452 }
453
454 // Do something with local..
455 for( int i = 0; i < sfx_sys_len; i ++ )
456 {
457 sfx_system *sys = sfx_sys + i;
458
459 u32 cursor = sys->cur, buffer_pos = 0;
460 float avgvol = 0.f;
461 float pcf[2] = { 0.f, 0.0f };
462
463 u32 frames_write = frameCount;
464 float fadeout_divisor = 1.0f / (float)sys->fadeout;
465
466 while( frames_write )
467 {
468 u32 samples_this_run = VG_MIN( frames_write, sys->end - cursor );
469
470 if( sys->fadeout )
471 {
472 // Force this system to be removed
473 if( sys->fadeout_current == 0 )
474 {
475 sys->flags = SFX_FLAG_GHOST;
476 sys->cur = sys->end;
477 break;
478 }
479
480 samples_this_run = VG_MIN( samples_this_run, sys->fadeout_current );
481 }
482
483 for( u32 j = 0; j < samples_this_run; j ++ )
484 {
485 audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
486
487 float vol = sys->vol;
488
489 if( sys->fadeout )
490 {
491 vol *= (float)sys->fadeout_current * fadeout_divisor;
492 sys->fadeout_current --;
493 }
494
495 if( buffer_pos >= frameCount )
496 {
497 break;
498 }
499
500 pOut32F[ buffer_pos*2+0 ] += pcf[0] * vol;
501 pOut32F[ buffer_pos*2+1 ] += pcf[1] * vol;
502
503 avgvol += fabs( pcf[0] * vol );
504 avgvol += fabs( pcf[1] * vol );
505
506 cursor ++;
507 buffer_pos ++;
508 }
509
510 frames_write -= samples_this_run;
511
512 if( sys->flags & SFX_FLAG_REPEAT )
513 {
514 if( frames_write )
515 {
516 cursor = 0;
517 continue;
518 }
519 }
520
521 sys->cur = cursor;
522 sys->signal_average = avgvol / (float)(buffer_pos*2);
523
524 break;
525 }
526 }
527
528 // Redistribute sound systems
529 MUTEX_LOCK( sfx_mux_t01 );
530
531 u32 idx = 0, wr = 0;
532 while( idx != sfx_sys_len )
533 {
534 sfx_system *src = sfx_sys + idx;
535
536 // Keep only if cursor is before end or repeating
537 if( src->cur < src->end || (src->flags & SFX_FLAG_REPEAT) )
538 {
539 // Correct source pointer on persisitent originals since we shifted ID's
540 if( !(src->flags & (SFX_FLAG_ONESHOT|SFX_FLAG_GHOST)) )
541 {
542 src->thread_clone->thread_clone = sfx_sys + wr;
543 }
544
545 sfx_sys[ wr ++ ] = sfx_sys[ idx ];
546 }
547 else
548 {
549 // Clear link on persistent sources (done playing)
550 if( !(src->flags & (SFX_FLAG_ONESHOT|SFX_FLAG_GHOST)) )
551 src->thread_clone->thread_clone = NULL;
552 }
553
554 idx ++ ;
555 }
556 sfx_sys_len = wr;
557
558 MUTEX_UNLOCK( sfx_mux_t01 );
559
560 (void)pInput;
561 }
562
563 // Load strings into sfx_set's memory
564 // String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0"
565 static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync )
566 {
567 printf( "Init sfx set\n| start | end | length | name \n" );
568
569 dest->ch = (flags & SFX_FLAG_STEREO)? 2: 1;
570
571 dest->main = NULL;
572 dest->numsegments = 0;
573 char *source = strSources;
574
575 u32 total = 0;
576 int len;
577 while( (len = strlen( source )) )
578 {
579 u32 samples;
580 float *sound = sfx_vorbis( source, dest->ch, &samples );
581
582 if( !sound )
583 {
584 free( dest->main );
585 dest->numsegments = 0;
586 return;
587 }
588
589 total += samples;
590
591 float *nbuf = realloc( dest->main, total * dest->ch * sizeof(float) );
592
593 if( nbuf )
594 {
595 dest->main = nbuf;
596 memcpy( dest->main + (total-samples)*dest->ch, sound, samples*dest->ch*sizeof(float) );
597 free( sound );
598
599 dest->segments[ dest->numsegments*2+0 ] = total-samples;
600 dest->segments[ dest->numsegments*2+1 ] = total;
601
602 printf( "| %09u | %09u | %09u | %s\n", total-samples, total, samples, source );
603 }
604 else
605 {
606 vg_error( "realloc() failed\n" );
607 free( sound );
608 return;
609 }
610
611 source += len +1;
612 dest->numsegments ++;
613 }
614
615 vg_info( "finished, numsegments: %u\n", dest->numsegments );
616 }
617
618 static void sfx_set_init( sfx_set *dest, char *sources )
619 {
620 if( !sources )
621 sfx_set_strings( dest, dest->sources, dest->flags, 0 );
622 else
623 sfx_set_strings( dest, sources, dest->flags, 0 );
624 }
625
626 // Pick a random sound from the buffer and play it into system
627 static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id )
628 {
629 if( !source->numsegments )
630 return;
631
632 int pick = (rand() % (max_id-min_id)) + min_id;
633
634 if( sfx_begin_edit( sys ) )
635 {
636 sys->fadeout = 0;
637 sys->source = source->main;
638 sys->cur = source->segments[ pick*2 + 0 ];
639 sys->end = source->segments[ pick*2 + 1 ];
640 sys->ch = source->ch;
641
642 sfx_save( sys );
643 }
644 }
645
646 static void sfx_system_fadeout( sfx_system *sys, u32 length_samples )
647 {
648 if( sfx_begin_edit( sys ) )
649 {
650 sys->fadeout_current = length_samples;
651 sys->fadeout = length_samples;
652
653 if( sys->thread_clone )
654 sys->cur = sys->thread_clone->cur;
655
656 sfx_save( sys );
657 }
658 }
659
660 // Free set resources
661 static void sfx_set_free( sfx_set *set )
662 {
663 free( set->main );
664 }