much cleaner audio handling & extra controls
[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 441
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 int ch, end, cur, cur_lagged;
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 static int sfx_save( sfx_system *sys );
187
188 // Asynchronous load-to-system callback
189 void sfx_vorbis_a_to_c( sfx_bgload_t *loadinf )
190 {
191 struct sfx_vorbis_a_to_inf *inf = loadinf->user;
192
193 // Mark buffer for deallocation if autofree is set
194 if( inf->flags & SFX_A_FLAG_AUTOFREE )
195 inf->sys->replacement = loadinf->buffer;
196 else
197 inf->sys->source = loadinf->buffer;
198
199 inf->sys->end = loadinf->samples;
200
201 if( inf->flags & SFX_A_FLAG_AUTOSTART )
202 sfx_save( inf->sys );
203
204 free( loadinf->path );
205 free( loadinf );
206 free( inf );
207 }
208
209 // Asynchronous vorbis load into audio system
210 void sfx_vorbis_a_to( sfx_system *sys, const char *strFileName, int channels, u32 flags )
211 {
212 struct sfx_vorbis_a_to_inf *inf = malloc( sizeof( struct sfx_vorbis_a_to_inf ) );
213 inf->flags = flags;
214 inf->sys = sys;
215
216 sys->ch = channels;
217
218 if( !sfx_vorbis_a( strFileName, channels, sfx_vorbis_a_to_c, inf ) )
219 free( inf );
220 }
221
222 // 0
223 // ======================================================
224
225 // Mark change to be uploaded to queue system
226 static int sfx_save( sfx_system *sys )
227 {
228 MUTEX_LOCK( sfx_mux_t01 );
229
230 if( sfx_q_len >= SFX_MAX_SYSTEMS )
231 {
232 vg_error( "Warning: No free space in sound queue\n" );
233
234 MUTEX_UNLOCK( sfx_mux_t01 );
235 return 0;
236 }
237
238 // Mark change in queue
239 sfx_q[ sfx_q_len ++ ] = sys;
240
241 MUTEX_UNLOCK( sfx_mux_t01 );
242
243 return 1;
244 }
245
246 // Edit a volume float, has to be function wrapped because of mutex
247 static void sfx_vol_fset( sfx_vol_control *src, float to )
248 {
249 MUTEX_LOCK( sfx_mux_t01 );
250
251 src->val = to;
252
253 MUTEX_UNLOCK( sfx_mux_t01 );
254 }
255
256 // thread-safe get volume value
257 static float sfx_vol_fget( sfx_vol_control *src )
258 {
259 float val;
260
261 MUTEX_LOCK( sfx_mux_t01 );
262
263 val = src->val;
264
265 MUTEX_UNLOCK( sfx_mux_t01 );
266
267 return val;
268 }
269
270 // thread-safe set master volume
271 static void sfx_set_master( float to )
272 {
273 MUTEX_LOCK( sfx_mux_t01 );
274
275 g_master_volume = to;
276
277 MUTEX_UNLOCK( sfx_mux_t01 );
278 }
279
280 // thread-safe get master volume
281 static float sfx_get_master(void)
282 {
283 float val;
284
285 MUTEX_LOCK( sfx_mux_t01 );
286
287 val = g_master_volume;
288
289 MUTEX_UNLOCK( sfx_mux_t01 );
290
291 return val;
292 }
293
294 void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount );
295
296 // Miniaudio.h init
297 static void vg_audio_init(void)
298 {
299 g_aud_dconfig = ma_device_config_init( ma_device_type_playback );
300 g_aud_dconfig.playback.format = ma_format_f32;
301 g_aud_dconfig.playback.channels = 2;
302 g_aud_dconfig.sampleRate = 44100;
303 g_aud_dconfig.dataCallback = audio_mixer_callback;
304
305 g_aud_dconfig.pUserData = NULL;
306
307 vg_info( "Starting audio engine\n" );
308
309 if( ma_device_init( NULL, &g_aud_dconfig, &g_aud_device ) != MA_SUCCESS )
310 {
311 vg_exiterr( "ma_device failed to initialize" );
312 }
313 else
314 {
315 if( ma_device_start( &g_aud_device ) != MA_SUCCESS )
316 {
317 ma_device_uninit( &g_aud_device );
318 vg_exiterr( "ma_device failed to start" );
319 }
320 }
321 }
322
323 #ifndef VYGER_RELEASE
324 u32 num_sfx_sets = 0;
325 #endif
326
327 // Shutdown audio device
328 static void vg_audio_free(void)
329 {
330 ma_device_uninit( &g_aud_device );
331 }
332
333 // (debug) make sure we are shutting down safely
334 static void sfx_sys_chkerr(void)
335 {
336 #ifndef VYGER_RELEASE
337 if( num_sfx_sets )
338 {
339 vg_error( "Leaked %u sfx sets\n", num_sfx_sets );
340 }
341 #endif
342 }
343
344 // 1
345 // ======================================================
346
347 // Create and return slot for a sound
348 static sfx_system *sfx_alloc(void)
349 {
350 if( sfx_sys_len >= SFX_MAX_SYSTEMS )
351 {
352 return NULL;
353 }
354
355 // A conditional is done against this in localization step,
356 // Needs to be initialized.
357 sfx_sys[ sfx_sys_len ].source = NULL;
358
359 return sfx_sys + (sfx_sys_len++);
360 }
361
362 // Fetch samples into pcf
363 static void audio_mixer_getsamples( float *pcf, float *source, u32 cur, u32 ch )
364 {
365 if( ch == 2 )
366 {
367 pcf[0] = source[ cur*2+0 ];
368 pcf[1] = source[ cur*2+1 ];
369 }
370 else
371 {
372 pcf[0] = source[ cur ];
373 pcf[1] = source[ cur ];
374 }
375 }
376
377 // miniaudio.h interface
378 void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount )
379 {
380 // Process incoming sound queue
381 MUTEX_LOCK( sfx_mux_t01 );
382
383 while( sfx_q_len --> 0 )
384 {
385 sfx_system *src = sfx_q[sfx_q_len];
386 sfx_system *clone;
387
388 // This is a 'new' sound if thread_clone not set.
389 if( !src->thread_clone || src->flags & SFX_FLAG_ONESHOT )
390 {
391 src->thread_clone = sfx_alloc();
392 if( !src->thread_clone )
393 break;
394 }
395 else
396 {
397 // Modifying an active system spawns a small fadeout ghost system
398 sfx_system *ghost_system = sfx_alloc();
399
400 if( !ghost_system )
401 break;
402
403 ghost_system->source = src->source;
404 ghost_system->ch = src->ch;
405 ghost_system->end = src->end;
406 ghost_system->cur = src->cur_lagged;
407 ghost_system->flags = SFX_FLAG_GHOST;
408 ghost_system->fadeout = FADEOUT_LENGTH;
409 ghost_system->fadeout_current = FADEOUT_LENGTH;
410 ghost_system->vol_src = src->vol_src;
411 ghost_system->name = src->name;
412 ghost_system->thread_clone = src;
413 }
414
415 clone = src->thread_clone;
416
417 // run replacement routine if one is waiting (todo: what is this?)
418 if( src->replacement )
419 {
420 free( src->source );
421
422 src->source = src->replacement;
423 src->replacement = NULL;
424 }
425
426 // Localize data to thread 1's memory pool
427 clone->source = src->source;
428 clone->ch = src->ch;
429 clone->end = src->end;
430 clone->cur = src->cur;
431 clone->flags = src->flags;
432 clone->vol_src = src->vol_src;
433 clone->name = src->name;
434 clone->fadeout = src->fadeout;
435 clone->fadeout_current = src->fadeout_current;
436
437 // loopback pointer, mainly used for persistent sound handles
438 clone->thread_clone = src;
439 }
440
441 sfx_q_len = 0;
442
443 // Pull in volume sliders
444 for( int i = 0; i < sfx_sys_len; i ++ )
445 {
446 sfx_system *sys = sfx_sys + i;
447 sys->vol = sys->thread_clone->vol * g_master_volume;
448
449 if( sys->vol_src )
450 sys->vol *= sys->vol_src->val;
451 }
452
453 MUTEX_UNLOCK( sfx_mux_t01 );
454
455 // Clear buffer ( is necessary ? )
456 float *pOut32F = (float *)pOutBuf;
457 for( int i = 0; i < frameCount * 2; i ++ ){
458 pOut32F[i] = 0.f;
459 }
460
461 // Do something with local..
462 for( int i = 0; i < sfx_sys_len; i ++ )
463 {
464 sfx_system *sys = sfx_sys + i;
465
466 u32 cursor = sys->cur, buffer_pos = 0;
467
468 float avgvol = 0.f;
469 float pcf[2] = { 0.f, 0.0f };
470
471 u32 frames_write = frameCount;
472 float fadeout_divisor = 1.0f / (float)sys->fadeout;
473
474 while( frames_write )
475 {
476 u32 samples_this_run = vg_min( frames_write, sys->end - cursor );
477
478 if( sys->fadeout )
479 {
480 // Force this system to be removed
481 if( sys->fadeout_current == 0 )
482 {
483 sys->flags = SFX_FLAG_GHOST;
484 sys->cur = sys->end;
485 break;
486 }
487
488 samples_this_run = vg_min( samples_this_run, sys->fadeout_current );
489 }
490
491 for( u32 j = 0; j < samples_this_run; j ++ )
492 {
493 audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
494
495 float vol = sys->vol;
496
497 if( sys->fadeout )
498 {
499 vol *= (float)sys->fadeout_current * fadeout_divisor;
500 sys->fadeout_current --;
501 }
502
503 pOut32F[ buffer_pos*2+0 ] += pcf[0] * vol;
504 pOut32F[ buffer_pos*2+1 ] += pcf[1] * vol;
505
506 avgvol += fabs( pcf[0] * vol );
507 avgvol += fabs( pcf[1] * vol );
508
509 cursor ++;
510 buffer_pos ++;
511 }
512
513 frames_write -= samples_this_run;
514
515 if( sys->flags & SFX_FLAG_REPEAT )
516 {
517 if( frames_write )
518 {
519 cursor = 0;
520 continue;
521 }
522 }
523
524 sys->cur = cursor;
525 sys->cur_lagged = cursor;
526 sys->signal_average = avgvol / (float)(buffer_pos*2);
527
528 break;
529 }
530 }
531
532 // Redistribute sound systems
533 MUTEX_LOCK( sfx_mux_t01 );
534
535 unsigned int idx = 0, wr = 0;
536 while( idx != sfx_sys_len )
537 {
538 sfx_system *src = sfx_sys + idx;
539
540 // Keep only if cursor is before end or repeating
541 if( src->cur < src->end || (src->flags & SFX_FLAG_REPEAT) )
542 {
543 // Correct source pointer on persisitent originals since we shifted ID's
544 if( !(src->flags & SFX_FLAG_ONESHOT) )
545 src->thread_clone->thread_clone = sfx_sys + wr;
546
547 sfx_sys[ wr ++ ] = sfx_sys[ idx ];
548 }
549 else
550 {
551 // Clear link on persistent sources (done playing)
552 if( !(src->flags & SFX_FLAG_ONESHOT) )
553 src->thread_clone->thread_clone = NULL;
554 }
555
556 idx ++ ;
557 }
558 sfx_sys_len = wr;
559
560 MUTEX_UNLOCK( sfx_mux_t01 );
561
562 (void)pInput;
563 }
564
565 // Load strings into sfx_set's memory
566 // String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0"
567 void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync )
568 {
569 printf( "Init sfx set\n| start | end | length | name \n" );
570
571 dest->ch = (flags & SFX_FLAG_STEREO)? 2: 1;
572
573 dest->main = NULL;
574 dest->numsegments = 0;
575 char *source = strSources;
576
577 u32 total = 0;
578 int len;
579 while( (len = strlen( source )) )
580 {
581 u32 samples;
582 float *sound = sfx_vorbis( source, dest->ch, &samples );
583
584 if( !sound )
585 {
586 free( dest->main );
587 dest->numsegments = 0;
588 return;
589 }
590
591 total += samples;
592
593 float *nbuf = realloc( dest->main, total * dest->ch * sizeof(float) );
594
595 if( nbuf )
596 {
597 dest->main = nbuf;
598 memcpy( dest->main + (total-samples)*dest->ch, sound, samples*dest->ch*sizeof(float) );
599 free( sound );
600
601 dest->segments[ dest->numsegments*2+0 ] = total-samples;
602 dest->segments[ dest->numsegments*2+1 ] = total;
603
604 printf( "| %09u | %09u | %09u | %s\n", total-samples, total, samples, source );
605 }
606 else
607 {
608 vg_error( "realloc() failed\n" );
609 free( sound );
610 return;
611 }
612
613 source += len +1;
614 dest->numsegments ++;
615 }
616
617 vg_info( "finished, numsegments: %u\n", dest->numsegments );
618 }
619
620 void sfx_set_init( sfx_set *dest, char *sources )
621 {
622 #ifndef VYGER_RELEASE
623 num_sfx_sets ++;
624 #endif
625
626 if( !sources )
627 {
628 sfx_set_strings( dest, dest->sources, dest->flags, 0 );
629 }
630 else
631 {
632 sfx_set_strings( dest, sources, dest->flags, 0 );
633 }
634 }
635
636 // Pick a random sound from the buffer and play it into system
637 void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id )
638 {
639 if( !source->numsegments )
640 {
641 return;
642 }
643
644 int pick = (rand() % (max_id-min_id)) + min_id;
645
646 sys->fadeout = 0;
647 sys->source = source->main;
648 sys->cur = source->segments[ pick*2 + 0 ];
649 sys->end = source->segments[ pick*2 + 1 ];
650 sys->ch = source->ch;
651
652 sfx_save( sys );
653 }
654
655 static void sfx_system_fadeout( sfx_system *sys, u32 length_samples )
656 {
657 sys->fadeout_current = length_samples;
658 sys->fadeout = length_samples;
659
660 sfx_save( sys );
661 }
662
663 // Free set resources
664 void sfx_set_free( sfx_set *set )
665 {
666 #ifndef VYGER_RELEASE
667 num_sfx_sets --;
668 #endif
669 free( set->main );
670 }