35b36c5f07a13aac6395a83200b4008bc1f3e6a0
[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 // Shutdown audio device
324 static void vg_audio_free(void)
325 {
326 ma_device_uninit( &g_aud_device );
327 }
328
329 // 1
330 // ======================================================
331
332 // Create and return slot for a sound
333 static sfx_system *sfx_alloc(void)
334 {
335 if( sfx_sys_len >= SFX_MAX_SYSTEMS )
336 return NULL;
337
338 // A conditional is done against this in localization step,
339 // Needs to be initialized.
340 sfx_sys[ sfx_sys_len ].source = NULL;
341
342 return sfx_sys + (sfx_sys_len++);
343 }
344
345 // Fetch samples into pcf
346 static void audio_mixer_getsamples( float *pcf, float *source, u32 cur, u32 ch )
347 {
348 if( ch == 2 )
349 {
350 pcf[0] = source[ cur*2+0 ];
351 pcf[1] = source[ cur*2+1 ];
352 }
353 else
354 {
355 pcf[0] = source[ cur ];
356 pcf[1] = source[ cur ];
357 }
358 }
359
360 // miniaudio.h interface
361 void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount )
362 {
363 // Process incoming sound queue
364 MUTEX_LOCK( sfx_mux_t01 );
365
366 while( sfx_q_len --> 0 )
367 {
368 sfx_system *src = sfx_q[sfx_q_len];
369 sfx_system *clone;
370
371 // This is a 'new' sound if thread_clone not set.
372 if( !src->thread_clone || src->flags & SFX_FLAG_ONESHOT )
373 {
374 src->thread_clone = sfx_alloc();
375 if( !src->thread_clone )
376 break;
377 }
378 else
379 {
380 // Modifying an active system spawns a small fadeout ghost system
381 sfx_system *ghost_system = sfx_alloc();
382
383 if( !ghost_system )
384 break;
385
386 ghost_system->source = src->source;
387 ghost_system->ch = src->ch;
388 ghost_system->end = src->end;
389 ghost_system->cur = src->cur_lagged;
390 ghost_system->flags = SFX_FLAG_GHOST;
391 ghost_system->fadeout = FADEOUT_LENGTH;
392 ghost_system->fadeout_current = FADEOUT_LENGTH;
393 ghost_system->vol_src = src->vol_src;
394 ghost_system->name = src->name;
395 ghost_system->thread_clone = src;
396 }
397
398 clone = src->thread_clone;
399
400 // run replacement routine if one is waiting (todo: what is this?)
401 if( src->replacement )
402 {
403 free( src->source );
404
405 src->source = src->replacement;
406 src->replacement = NULL;
407 }
408
409 // Localize data to thread 1's memory pool
410 clone->source = src->source;
411 clone->ch = src->ch;
412 clone->end = src->end;
413 clone->cur = src->cur;
414 clone->flags = src->flags;
415 clone->vol_src = src->vol_src;
416 clone->name = src->name;
417 clone->fadeout = src->fadeout;
418 clone->fadeout_current = src->fadeout_current;
419
420 // loopback pointer, mainly used for persistent sound handles
421 clone->thread_clone = src;
422 }
423
424 sfx_q_len = 0;
425
426 // Pull in volume sliders
427 for( int i = 0; i < sfx_sys_len; i ++ )
428 {
429 sfx_system *sys = sfx_sys + i;
430 sys->vol = sys->thread_clone->vol * g_master_volume;
431
432 if( sys->vol_src )
433 sys->vol *= sys->vol_src->val;
434 }
435
436 MUTEX_UNLOCK( sfx_mux_t01 );
437
438 // Clear buffer ( is necessary ? )
439 float *pOut32F = (float *)pOutBuf;
440 for( int i = 0; i < frameCount * 2; i ++ ){
441 pOut32F[i] = 0.f;
442 }
443
444 // Do something with local..
445 for( int i = 0; i < sfx_sys_len; i ++ )
446 {
447 sfx_system *sys = sfx_sys + i;
448
449 u32 cursor = sys->cur, buffer_pos = 0;
450
451 float avgvol = 0.f;
452 float pcf[2] = { 0.f, 0.0f };
453
454 u32 frames_write = frameCount;
455 float fadeout_divisor = 1.0f / (float)sys->fadeout;
456
457 while( frames_write )
458 {
459 u32 samples_this_run = vg_min( frames_write, sys->end - cursor );
460
461 if( sys->fadeout )
462 {
463 // Force this system to be removed
464 if( sys->fadeout_current == 0 )
465 {
466 sys->flags = SFX_FLAG_GHOST;
467 sys->cur = sys->end;
468 break;
469 }
470
471 samples_this_run = vg_min( samples_this_run, sys->fadeout_current );
472 }
473
474 for( u32 j = 0; j < samples_this_run; j ++ )
475 {
476 audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
477
478 float vol = sys->vol;
479
480 if( sys->fadeout )
481 {
482 vol *= (float)sys->fadeout_current * fadeout_divisor;
483 sys->fadeout_current --;
484 }
485
486 pOut32F[ buffer_pos*2+0 ] += pcf[0] * vol;
487 pOut32F[ buffer_pos*2+1 ] += pcf[1] * vol;
488
489 avgvol += fabs( pcf[0] * vol );
490 avgvol += fabs( pcf[1] * vol );
491
492 cursor ++;
493 buffer_pos ++;
494 }
495
496 frames_write -= samples_this_run;
497
498 if( sys->flags & SFX_FLAG_REPEAT )
499 {
500 if( frames_write )
501 {
502 cursor = 0;
503 continue;
504 }
505 }
506
507 sys->cur = cursor;
508 sys->cur_lagged = cursor;
509 sys->signal_average = avgvol / (float)(buffer_pos*2);
510
511 break;
512 }
513 }
514
515 // Redistribute sound systems
516 MUTEX_LOCK( sfx_mux_t01 );
517
518 unsigned int idx = 0, wr = 0;
519 while( idx != sfx_sys_len )
520 {
521 sfx_system *src = sfx_sys + idx;
522
523 // Keep only if cursor is before end or repeating
524 if( src->cur < src->end || (src->flags & SFX_FLAG_REPEAT) )
525 {
526 // Correct source pointer on persisitent originals since we shifted ID's
527 if( !(src->flags & SFX_FLAG_ONESHOT) )
528 src->thread_clone->thread_clone = sfx_sys + wr;
529
530 sfx_sys[ wr ++ ] = sfx_sys[ idx ];
531 }
532 else
533 {
534 // Clear link on persistent sources (done playing)
535 if( !(src->flags & SFX_FLAG_ONESHOT) )
536 src->thread_clone->thread_clone = NULL;
537 }
538
539 idx ++ ;
540 }
541 sfx_sys_len = wr;
542
543 MUTEX_UNLOCK( sfx_mux_t01 );
544
545 (void)pInput;
546 }
547
548 // Load strings into sfx_set's memory
549 // String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0"
550 static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync )
551 {
552 printf( "Init sfx set\n| start | end | length | name \n" );
553
554 dest->ch = (flags & SFX_FLAG_STEREO)? 2: 1;
555
556 dest->main = NULL;
557 dest->numsegments = 0;
558 char *source = strSources;
559
560 u32 total = 0;
561 int len;
562 while( (len = strlen( source )) )
563 {
564 u32 samples;
565 float *sound = sfx_vorbis( source, dest->ch, &samples );
566
567 if( !sound )
568 {
569 free( dest->main );
570 dest->numsegments = 0;
571 return;
572 }
573
574 total += samples;
575
576 float *nbuf = realloc( dest->main, total * dest->ch * sizeof(float) );
577
578 if( nbuf )
579 {
580 dest->main = nbuf;
581 memcpy( dest->main + (total-samples)*dest->ch, sound, samples*dest->ch*sizeof(float) );
582 free( sound );
583
584 dest->segments[ dest->numsegments*2+0 ] = total-samples;
585 dest->segments[ dest->numsegments*2+1 ] = total;
586
587 printf( "| %09u | %09u | %09u | %s\n", total-samples, total, samples, source );
588 }
589 else
590 {
591 vg_error( "realloc() failed\n" );
592 free( sound );
593 return;
594 }
595
596 source += len +1;
597 dest->numsegments ++;
598 }
599
600 vg_info( "finished, numsegments: %u\n", dest->numsegments );
601 }
602
603 static void sfx_set_init( sfx_set *dest, char *sources )
604 {
605 if( !sources )
606 sfx_set_strings( dest, dest->sources, dest->flags, 0 );
607 else
608 sfx_set_strings( dest, sources, dest->flags, 0 );
609 }
610
611 // Pick a random sound from the buffer and play it into system
612 static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id )
613 {
614 if( !source->numsegments )
615 return;
616
617 int pick = (rand() % (max_id-min_id)) + min_id;
618
619 sys->fadeout = 0;
620 sys->source = source->main;
621 sys->cur = source->segments[ pick*2 + 0 ];
622 sys->end = source->segments[ pick*2 + 1 ];
623 sys->ch = source->ch;
624
625 sfx_save( sys );
626 }
627
628 static void sfx_system_fadeout( sfx_system *sys, u32 length_samples )
629 {
630 sys->fadeout_current = length_samples;
631 sys->fadeout = length_samples;
632
633 sfx_save( sys );
634 }
635
636 // Free set resources
637 static void sfx_set_free( sfx_set *set )
638 {
639 free( set->main );
640 }