1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 #define MINIAUDIO_IMPLEMENTATION
4 #include "dr_soft/miniaudio.h"
6 #define STB_VORBIS_MAX_CHANNELS 2
7 #include "stb/stb_vorbis.h"
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)
17 typedef struct sfx_vol_control sfx_vol_control
;
18 typedef struct sfx_system sfx_system
;
20 struct sfx_vol_control
28 // Source buffer start
29 float *source
, *replacement
;
31 u32 clip_start
, clip_end
, buffer_length
;
34 sfx_vol_control
*vol_src
;
42 u32 fadeout
, fadeout_current
;
44 sfx_system
*thread_clone
; // Memory of this structure is copied into thread 2
47 float signal_average
; // Current signal volume
51 // Set of up to 8 sound effects packed into one
52 typedef struct sfx_set sfx_set
;
58 u32 segments
[16]; //from->to,from->to ...
64 ma_device g_aud_device
;
65 ma_device_config g_aud_dconfig
;
67 // Thread 1 - audio engine ( spawned from miniaudio.h )
68 // ======================================================
69 sfx_system sfx_sys
[SFX_MAX_SYSTEMS
];
72 // Thread 0 - Critical transfer section
73 // ======================================================
74 MUTEX_TYPE sfx_mux_t01
; // Resources share: 0 & 1
76 sfx_system
*sfx_q
[SFX_MAX_SYSTEMS
]; // Stuff changed
77 int sfx_q_len
= 0; // How much
80 // ======================================================
82 // g_vol_master is never directly acessed by users
83 float g_master_volume
= 1.f
;
85 // Decompress entire vorbis stream into buffer
86 static float *sfx_vorbis_stream( const unsigned char *data
, int len
, int channels
, u32
*samples
)
89 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
93 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
97 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
98 float *buffer
= (float *)malloc( length_samples
* channels
* sizeof( float ));
102 stb_vorbis_close( pv
);
103 vg_error( "out of memory while allocating sound resource\n" );
107 int read_samples
= stb_vorbis_get_samples_float_interleaved( pv
, channels
, buffer
, length_samples
* channels
);
108 if( read_samples
!= length_samples
)
110 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n", length_samples
, read_samples
);
111 length_samples
= read_samples
;
114 stb_vorbis_close( pv
);
115 *samples
= length_samples
;
119 static float *sfx_vorbis( const char *strFileName
, int channels
, u32
*samples
)
122 void *filedata
= vg_asset_read_s( strFileName
, &len
);
126 float *wav
= sfx_vorbis_stream( filedata
, len
, channels
, samples
);
132 vg_error( "OGG load failed\n" );
137 typedef struct sfx_bgload sfx_bgload_t
;
148 void(*OnComplete
)(sfx_bgload_t
*inf
);
151 // Thread worker for background load job
152 void *sfx_vorbis_a_t( void *_inf
)
154 sfx_bgload_t
*info
= _inf
;
157 info
->buffer
= sfx_vorbis( info
->path
, info
->channels
, &info
->samples
);
158 info
->OnComplete( info
);
163 // Asynchronous resource load
164 int sfx_vorbis_a( const char *path
, int channels
, void(*OnComplete
)(sfx_bgload_t
*inf
), void *user
)
166 vg_info( "background job started for: %s\n", path
);
168 sfx_bgload_t
*params
= malloc( sizeof( sfx_bgload_t
) );
169 params
->path
= malloc( strlen( path
) + 1 );
170 strcpy( params
->path
, path
);
171 params
->OnComplete
= OnComplete
;
173 params
->channels
= channels
;
175 return vg_thread_run( sfx_vorbis_a_t
, params
);
178 // Asynchronous load-to-system callback
179 struct sfx_vorbis_a_to_inf
185 #define SFX_A_FLAG_AUTOSTART 0x1
186 #define SFX_A_FLAG_AUTOFREE 0x2
189 static int sfx_save( sfx_system *sys );
191 // Asynchronous load-to-system callback
192 void sfx_vorbis_a_to_c( sfx_bgload_t *loadinf )
194 struct sfx_vorbis_a_to_inf *inf = loadinf->user;
196 // Mark buffer for deallocation if autofree is set
197 if( inf->flags & SFX_A_FLAG_AUTOFREE )
198 inf->sys->replacement = loadinf->buffer;
200 inf->sys->source = loadinf->buffer;
202 inf->sys->end = loadinf->samples;
204 if( inf->flags & SFX_A_FLAG_AUTOSTART )
205 sfx_save( inf->sys );
207 free( loadinf->path );
212 // Asynchronous vorbis load into audio system
213 void sfx_vorbis_a_to( sfx_system *sys, const char *strFileName, int channels, u32 flags )
215 struct sfx_vorbis_a_to_inf *inf = malloc( sizeof( struct sfx_vorbis_a_to_inf ) );
221 if( !sfx_vorbis_a( strFileName, channels, sfx_vorbis_a_to_c, inf ) )
226 // ======================================================
228 static int sfx_begin_edit( sfx_system
*sys
)
230 MUTEX_LOCK( sfx_mux_t01
);
232 if( sfx_q_len
>= SFX_MAX_SYSTEMS
)
234 MUTEX_UNLOCK( sfx_mux_t01
);
235 vg_warn( "Warning: No free space in sound queue\n" );
242 // Mark change to be uploaded to queue system
243 static int sfx_save( sfx_system
*sys
)
245 // Mark change in queue
246 sfx_q
[ sfx_q_len
++ ] = sys
;
248 MUTEX_UNLOCK( sfx_mux_t01
);
253 // Edit a volume float, has to be function wrapped because of mutex
254 static void sfx_vol_fset( sfx_vol_control
*src
, float to
)
256 MUTEX_LOCK( sfx_mux_t01
);
260 MUTEX_UNLOCK( sfx_mux_t01
);
263 // thread-safe get volume value
264 static float sfx_vol_fget( sfx_vol_control
*src
)
268 MUTEX_LOCK( sfx_mux_t01
);
272 MUTEX_UNLOCK( sfx_mux_t01
);
277 // thread-safe set master volume
278 static void sfx_set_master( float to
)
280 MUTEX_LOCK( sfx_mux_t01
);
282 g_master_volume
= to
;
284 MUTEX_UNLOCK( sfx_mux_t01
);
287 // thread-safe get master volume
288 static float sfx_get_master(void)
292 MUTEX_LOCK( sfx_mux_t01
);
294 val
= g_master_volume
;
296 MUTEX_UNLOCK( sfx_mux_t01
);
301 void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
, const void *pInput
, ma_uint32 frameCount
);
304 static void vg_audio_init(void)
306 g_aud_dconfig
= ma_device_config_init( ma_device_type_playback
);
307 g_aud_dconfig
.playback
.format
= ma_format_f32
;
308 g_aud_dconfig
.playback
.channels
= 2;
309 g_aud_dconfig
.sampleRate
= 44100;
310 g_aud_dconfig
.dataCallback
= audio_mixer_callback
;
312 g_aud_dconfig
.pUserData
= NULL
;
314 vg_info( "Starting audio engine\n" );
316 if( ma_device_init( NULL
, &g_aud_dconfig
, &g_aud_device
) != MA_SUCCESS
)
318 vg_exiterr( "ma_device failed to initialize" );
322 if( ma_device_start( &g_aud_device
) != MA_SUCCESS
)
324 ma_device_uninit( &g_aud_device
);
325 vg_exiterr( "ma_device failed to start" );
330 // Shutdown audio device
331 static void vg_audio_free(void)
333 ma_device_uninit( &g_aud_device
);
337 // ======================================================
339 // Create and return slot for a sound
340 static sfx_system
*sfx_alloc(void)
342 if( sfx_sys_len
>= SFX_MAX_SYSTEMS
)
345 // A conditional is done against this in localization step,
346 // Needs to be initialized.
347 sfx_sys
[ sfx_sys_len
].source
= NULL
;
349 return sfx_sys
+ (sfx_sys_len
++);
352 // Fetch samples into pcf
353 static void audio_mixer_getsamples( float *pcf
, float *source
, u32 cur
, u32 ch
)
357 pcf
[0] = source
[ cur
*2+0 ];
358 pcf
[1] = source
[ cur
*2+1 ];
362 pcf
[0] = source
[ cur
];
363 pcf
[1] = source
[ cur
];
367 // miniaudio.h interface
368 void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
, const void *pInput
, ma_uint32 frameCount
)
370 // Process incoming sound queue
371 MUTEX_LOCK( sfx_mux_t01
);
373 while( sfx_q_len
--> 0 )
375 sfx_system
*src
= sfx_q
[sfx_q_len
];
378 // This is a 'new' sound if thread_clone not set.
379 if( !src
->thread_clone
|| (src
->flags
& SFX_FLAG_ONESHOT
) )
385 src
->thread_clone
= clone
;
389 clone
= src
->thread_clone
;
391 // Modifying an active system's cursor spawns a small fadeout ghost system
392 if( clone
->cur
!= src
->cur
)
394 sfx_system
*ghost_system
= sfx_alloc();
399 ghost_system
->source
= clone
->source
;
400 ghost_system
->ch
= clone
->ch
;
401 ghost_system
->end
= clone
->end
;
402 ghost_system
->cur
= clone
->cur
;
403 ghost_system
->flags
= SFX_FLAG_GHOST
;
404 ghost_system
->fadeout
= FADEOUT_LENGTH
;
405 ghost_system
->fadeout_current
= FADEOUT_LENGTH
;
406 ghost_system
->vol_src
= clone
->vol_src
;
407 ghost_system
->name
= clone
->name
;
408 ghost_system
->thread_clone
= src
;
412 // run replacement routine if one is waiting (todo: what is this?)
413 if( src
->replacement
)
417 src
->source
= src
->replacement
;
418 src
->replacement
= NULL
;
421 // Localize data to thread 1's memory pool
422 clone
->source
= src
->source
;
424 clone
->end
= src
->end
;
425 clone
->cur
= src
->cur
;
426 clone
->flags
= src
->flags
;
427 clone
->vol_src
= src
->vol_src
;
428 clone
->name
= src
->name
;
429 clone
->fadeout
= src
->fadeout
;
430 clone
->fadeout_current
= src
->fadeout_current
;
432 // loopback pointer, mainly used for persistent sound handles
433 clone
->thread_clone
= src
;
438 // Pull in volume sliders
439 for( int i
= 0; i
< sfx_sys_len
; i
++ )
441 sfx_system
*sys
= sfx_sys
+ i
;
442 sys
->vol
= sys
->thread_clone
->vol
* g_master_volume
;
445 sys
->vol
*= sys
->vol_src
->val
;
448 MUTEX_UNLOCK( sfx_mux_t01
);
450 // Clear buffer ( is necessary ? )
451 float *pOut32F
= (float *)pOutBuf
;
452 for( int i
= 0; i
< frameCount
* 2; i
++ ){
456 // Do something with local..
457 for( int i
= 0; i
< sfx_sys_len
; i
++ )
459 sfx_system
*sys
= sfx_sys
+ i
;
461 u32 cursor
= sys
->cur
, buffer_pos
= 0;
463 float pcf
[2] = { 0.f
, 0.0f
};
465 u32 frames_write
= frameCount
;
466 float fadeout_divisor
= 1.0f
/ (float)sys
->fadeout
;
468 while( frames_write
)
470 u32 samples_this_run
= VG_MIN( frames_write
, sys
->end
- cursor
);
474 // Force this system to be removed
475 if( sys
->fadeout_current
== 0 )
477 sys
->flags
= SFX_FLAG_GHOST
;
482 samples_this_run
= VG_MIN( samples_this_run
, sys
->fadeout_current
);
485 for( u32 j
= 0; j
< samples_this_run
; j
++ )
487 audio_mixer_getsamples( pcf
, sys
->source
, cursor
, sys
->ch
);
489 float vol
= sys
->vol
;
493 vol
*= (float)sys
->fadeout_current
* fadeout_divisor
;
494 sys
->fadeout_current
--;
497 if( buffer_pos
>= frameCount
)
502 pOut32F
[ buffer_pos
*2+0 ] += pcf
[0] * vol
;
503 pOut32F
[ buffer_pos
*2+1 ] += pcf
[1] * vol
;
505 avgvol
+= fabs( pcf
[0] * vol
);
506 avgvol
+= fabs( pcf
[1] * vol
);
512 frames_write
-= samples_this_run
;
514 if( sys
->flags
& SFX_FLAG_REPEAT
)
524 sys
->signal_average
= avgvol
/ (float)(buffer_pos
*2);
530 // Redistribute sound systems
531 MUTEX_LOCK( sfx_mux_t01
);
534 while( idx
!= sfx_sys_len
)
536 sfx_system
*src
= sfx_sys
+ idx
;
538 // Keep only if cursor is before end or repeating
539 if( src
->cur
< src
->end
|| (src
->flags
& SFX_FLAG_REPEAT
) )
541 // Correct source pointer on persisitent originals since we shifted ID's
542 if( !(src
->flags
& (SFX_FLAG_ONESHOT
|SFX_FLAG_GHOST
)) )
544 src
->thread_clone
->thread_clone
= sfx_sys
+ wr
;
547 sfx_sys
[ wr
++ ] = sfx_sys
[ idx
];
551 // Clear link on persistent sources (done playing)
552 if( !(src
->flags
& (SFX_FLAG_ONESHOT
|SFX_FLAG_GHOST
)) )
553 src
->thread_clone
->thread_clone
= NULL
;
560 MUTEX_UNLOCK( sfx_mux_t01
);
565 // Load strings into sfx_set's memory
566 // String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0"
567 static void sfx_set_strings( sfx_set
*dest
, char *strSources
, u32 flags
, int bAsync
)
569 printf( "Init sfx set\n| start | end | length | name \n" );
571 dest
->ch
= (flags
& SFX_FLAG_STEREO
)? 2: 1;
574 dest
->numsegments
= 0;
575 char *source
= strSources
;
579 while( (len
= strlen( source
)) )
582 float *sound
= sfx_vorbis( source
, dest
->ch
, &samples
);
587 dest
->numsegments
= 0;
593 float *nbuf
= realloc( dest
->main
, total
* dest
->ch
* sizeof(float) );
598 memcpy( dest
->main
+ (total
-samples
)*dest
->ch
, sound
, samples
*dest
->ch
*sizeof(float) );
601 dest
->segments
[ dest
->numsegments
*2+0 ] = total
-samples
;
602 dest
->segments
[ dest
->numsegments
*2+1 ] = total
;
604 printf( "| %09u | %09u | %09u | %s\n", total
-samples
, total
, samples
, source
);
608 vg_error( "realloc() failed\n" );
614 dest
->numsegments
++;
617 vg_info( "finished, numsegments: %u\n", dest
->numsegments
);
620 static void sfx_set_init( sfx_set
*dest
, char *sources
)
623 sfx_set_strings( dest
, dest
->sources
, dest
->flags
, 0 );
625 sfx_set_strings( dest
, sources
, dest
->flags
, 0 );
628 // Pick a random sound from the buffer and play it into system
629 static void sfx_set_playrnd( sfx_set
*source
, sfx_system
*sys
, int min_id
, int max_id
)
631 if( !source
->numsegments
)
634 int pick
= (rand() % (max_id
-min_id
)) + min_id
;
636 if( sfx_begin_edit( sys
) )
639 sys
->source
= source
->main
;
640 sys
->cur
= source
->segments
[ pick
*2 + 0 ];
641 sys
->end
= source
->segments
[ pick
*2 + 1 ];
642 sys
->ch
= source
->ch
;
645 sys
->clip_start
= sys
->cur
;
646 sys
->clip_end
= sys
->end
;
647 sys
->buffer_length
= source
->segments
[ (source
->numsegments
-1)*2 + 1 ];
653 static void sfx_system_fadeout( sfx_system
*sys
, u32 length_samples
)
655 if( sfx_begin_edit( sys
) )
659 sys
->fadeout_current
= length_samples
;
660 sys
->fadeout
= length_samples
;
662 if( sys
->thread_clone
)
663 sys
->cur
= sys
->thread_clone
->cur
;
669 // Sound was not initialized
670 MUTEX_UNLOCK( sfx_mux_t01
);
675 // Free set resources
676 static void sfx_set_free( sfx_set
*set
)