1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
6 #define MA_NO_GENERATION
9 #include "dr_soft/miniaudio.h"
11 #include "vg/vg_stdint.h"
12 #include "vg/vg_platform.h"
16 #include "vg/vg_console.h"
17 #include "vg/vg_store.h"
21 #define STB_VORBIS_MAX_CHANNELS 2
22 #include "stb/stb_vorbis.h"
24 #define SFX_MAX_SYSTEMS 32
25 #define AUDIO_FLAG_LOOP 0x1
26 #define AUDIO_FLAG_ONESHOT 0x2
27 #define AUDIO_FLAG_SPACIAL_3D 0x4
29 #define FADEOUT_LENGTH 4410
30 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
32 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
34 enum audio_source_mode
37 k_audio_source_mono_compressed
,
38 k_audio_source_stereo_compressed
41 typedef struct audio_clip audio_clip
;
45 enum audio_source_mode source_mode
;
49 u32 len
; /* decompressed: sample count,
50 compressed: file size */
53 typedef struct audio_mix_info audio_mix_info
;
63 typedef struct audio_player audio_player
;
66 aatree_ptr active_entity
; /* non-nil if currently playing */
74 typedef struct audio_entity audio_entity
;
83 u32 fadeout
, fadeout_current
;
90 * play again: if already playing, leave in queue while it fadeouts
91 * oneshot: create a ghost entity
95 static struct vg_audio_system
97 ma_device miniaudio_device
;
98 ma_device_config miniaudio_dconfig
;
100 void *mem
, *decode_mem
;
106 MUTEX_TYPE mutex_checker
;
107 MUTEX_TYPE mutex_sync
;
109 /* Audio engine, thread 1 */
110 struct active_audio_player
116 aatree_pool_node pool_node
;
119 stb_vorbis
*vorbis_handle
;
120 stb_vorbis_alloc vorbis_alloc
;
122 active_players
[ SFX_MAX_SYSTEMS
];
124 aatree active_pool_info
; /* note: just using the pool */
125 aatree_ptr active_pool_head
;
127 /* System queue, and access from thread 0 */
128 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
131 char performance_info
[128];
139 static void *audio_alloc( u32 size
)
141 u32 new_current
= vg_audio
.mem_current
+ size
;
142 if( new_current
> vg_audio
.mem_total
)
144 vg_error( "audio pool over budget!\n" );
145 free( vg_audio
.mem
);
149 void *ptr
= vg_audio
.mem
+ vg_audio
.mem_current
;
150 vg_audio
.mem_current
= new_current
;
157 * These functions are called from the main thread and used to prevent bad
158 * access. TODO: They should be no-ops in release builds.
160 static int audio_lock_checker_load(void)
163 MUTEX_LOCK( vg_audio
.mutex_checker
);
164 value
= vg_audio
.sync_locked
;
165 MUTEX_UNLOCK( vg_audio
.mutex_checker
);
169 static void audio_lock_checker_store( int value
)
171 MUTEX_LOCK( vg_audio
.mutex_checker
);
172 vg_audio
.sync_locked
= value
;
173 MUTEX_UNLOCK( vg_audio
.mutex_checker
);
176 static void audio_require_lock(void)
178 if( audio_lock_checker_load() )
181 vg_exiterr( "Modifying sound effects systems requires locking\n" );
184 static void audio_lock(void)
186 MUTEX_LOCK( vg_audio
.mutex_sync
);
187 audio_lock_checker_store(1);
190 static void audio_unlock(void)
192 audio_lock_checker_store(0);
193 MUTEX_UNLOCK( vg_audio
.mutex_sync
);
197 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
198 const void *pInput
, ma_uint32 frameCount
);
200 static int vg_audio_init(void)
202 vg_convar_push( (struct vg_convar
){
203 .name
= "debug_audio",
204 .data
= &vg_audio
.debug_ui
,
205 .data_type
= k_convar_dtype_i32
,
206 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
210 u32 decode_region
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
211 vg_audio
.mem_total
= 1024*1024*32;
212 vg_audio
.mem_current
= 0;
213 vg_audio
.mem
= malloc( vg_audio
.mem_total
+ decode_region
);
214 vg_audio
.decode_mem
= &((u8
*)vg_audio
.mem
)[vg_audio
.mem_total
];
217 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
218 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
220 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
221 vg_audio
.active_pool_info
.p_cmp
= NULL
;
222 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
224 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
225 ma_device
*device
= &vg_audio
.miniaudio_device
;
227 *dconf
= ma_device_config_init( ma_device_type_playback
);
228 dconf
->playback
.format
= ma_format_f32
;
229 dconf
->playback
.channels
= 2;
230 dconf
->sampleRate
= 44100;
231 dconf
->dataCallback
= audio_mixer_callback
;
233 dconf
->pUserData
= NULL
;
235 vg_info( "Starting audio engine\n" );
237 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
239 vg_error( "ma_device failed to initialize" );
244 if( ma_device_start( device
) != MA_SUCCESS
)
246 ma_device_uninit( device
);
247 vg_error( "ma_device failed to start" );
255 static void vg_audio_free(void)
257 ma_device
*device
= &vg_audio
.miniaudio_device
;
258 ma_device_uninit( device
);
260 free( vg_audio
.mem
);
267 static aatree_ptr
audio_alloc_entity_internal(void)
269 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
270 &vg_audio
.active_pool_head
);
272 if( playerid
== AATREE_PTR_NIL
)
273 return AATREE_PTR_NIL
;
275 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
281 static void audio_entity_free_internal( aatree_ptr id
)
283 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
286 /* Notify player that we've finished */
287 if( aap
->ent
.player
)
288 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
291 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
292 &vg_audio
.active_pool_head
);
295 static void *audio_entity_vorbis_ptr( aatree_ptr entid
)
297 u8
*buf
= (u8
*)vg_audio
.decode_mem
,
298 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
303 static void audio_entity_start( audio_entity
*src
)
305 aatree_ptr entid
= audio_alloc_entity_internal();
306 if( entid
== AATREE_PTR_NIL
)
309 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
311 ent
->info
= src
->info
;
314 ent
->player
= src
->player
;
317 ent
->fadeout_current
= 0;
319 /* Notify main player we are dequeud and playing */
322 src
->player
->enqued
= 0;
323 src
->player
->active_entity
= entid
;
326 if( src
->info
.source
->source_mode
== k_audio_source_mono_compressed
||
327 src
->info
.source
->source_mode
== k_audio_source_stereo_compressed
)
329 /* Setup vorbis decoder */
330 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
332 stb_vorbis_alloc alloc
= {
333 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
334 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
338 stb_vorbis
*decoder
= stb_vorbis_open_memory(
339 src
->info
.source
->data
, src
->info
.source
->len
, &err
, &alloc
);
343 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
344 src
->info
.source
->path
, err
);
346 audio_entity_free_internal( entid
);
351 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
354 aap
->vorbis_handle
= decoder
;
358 ent
->length
= src
->info
.source
->len
;
363 * Read everything from the queue
365 static void audio_system_enque(void)
367 /* Process incoming sound queue */
371 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
373 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
378 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
380 audio_entity_start( src
);
384 /* Otherwise try start fadeout but dont remove from queue */
386 aatree_ptr entid
= src
->player
->active_entity
;
387 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
391 ent
->fadeout_current
= FADEOUT_LENGTH
;
394 vg_audio
.entity_queue
[ wr
++ ] = *src
;
399 audio_entity_start( src
);
403 vg_audio
.queue_len
= wr
;
405 /* Localize others memory */
406 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
408 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
412 if( aap
->ent
.player
)
414 /* Only copy information in whilst not requeing */
415 if( aap
->ent
.player
->enqued
== 0 )
417 aap
->ent
.info
= aap
->ent
.player
->info
;
426 * Redistribute sound systems
428 static void audio_system_cleanup(void)
432 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
434 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
437 audio_entity
*src
= &aap
->ent
;
438 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
444 audio_entity_free_internal( i
);
453 * Get effective volume and pan from this entity
455 static void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
458 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
460 float dist
= v3_length( delta
),
461 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
463 v3_muls( delta
, 1.0f
/dist
, delta
);
465 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
466 *vol
= 1.0f
/(attn
*attn
);
469 static void audio_decode_uncompressed_mono( float *src
, u32 count
, float *dst
)
471 for( u32 i
=0; i
<count
; i
++ )
473 dst
[ i
*2 + 0 ] = src
[i
];
474 dst
[ i
*2 + 1 ] = src
[i
];
478 static void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
480 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
481 audio_entity
*ent
= &aap
->ent
;
483 u32 remaining
= count
;
484 u32 cursor
= ent
->cur
;
489 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
490 remaining
-= samples_this_run
;
492 float *dst
= &buf
[ buffer_pos
* 2 ];
494 if( ent
->info
.source
->source_mode
== k_audio_source_mono
)
496 float *src
= &((float *)ent
->info
.source
->data
)[ cursor
];
497 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
499 else if( ent
->info
.source
->source_mode
== k_audio_source_mono_compressed
)
501 int read_samples
= stb_vorbis_get_samples_float_interleaved(
505 samples_this_run
* 2 );
507 if( read_samples
!= samples_this_run
)
509 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
513 cursor
+= samples_this_run
;
514 buffer_pos
+= samples_this_run
;
516 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
518 if( ent
->info
.source
->source_mode
== k_audio_source_mono_compressed
||
519 ent
->info
.source
->source_mode
== k_audio_source_stereo_compressed
)
521 stb_vorbis_seek_start( aap
->vorbis_handle
);
533 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
534 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
543 static void audio_entity_mix( aatree_ptr id
, float *buffer
,
546 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
548 u32 cursor
= ent
->cur
, buffer_pos
= 0;
549 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
551 u32 frames_write
= frame_count
;
552 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
554 float vol
= ent
->info
.vol
,
557 audio_entity_get_samples( id
, frame_count
, pcf
);
559 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
560 audio_entity_spacialize( ent
, &vol
, &pan
);
562 for( u32 j
=0; j
<frame_count
; j
++ )
564 float frame_vol
= vol
;
568 /* Force this system to be removed now */
569 if( ent
->fadeout_current
== 0 )
571 ent
->info
.flags
= 0x00;
572 ent
->cur
= ent
->length
;
576 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
577 ent
->fadeout_current
--;
583 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
584 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
590 static void vg_sleep_ms( long msec
)
594 ts
.tv_sec
= msec
/ 1000;
595 ts
.tv_nsec
= (msec
% 1000) * 1000000;
596 nanosleep( &ts
, &ts
);
600 * callback from miniaudio.h interface
602 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
603 const void *pInput
, ma_uint32 frame_count
)
605 struct timespec time_start
, time_end
;
606 clock_gettime( CLOCK_REALTIME
, &time_start
);
608 audio_system_enque();
611 float *pOut32F
= (float *)pOutBuf
;
612 for( int i
=0; i
<frame_count
*2; i
++ )
616 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
618 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
621 audio_entity_mix( i
, pOut32F
, frame_count
);
629 audio_system_cleanup();
631 /* TODO: what the hell is this? */
637 clock_gettime( CLOCK_REALTIME
, &time_end
);
639 double elapsed
= 1000.0*time_end
.tv_sec
+ 1e-6*time_end
.tv_nsec
640 - (1000.0*time_start
.tv_sec
+ 1e-6*time_start
.tv_nsec
),
641 budget
= ((double)frame_count
/ 44100.0) * 1000.0,
642 percent
= (elapsed
/budget
) * 100.0;
644 snprintf( vg_audio
.performance_info
, 127,
645 "%.1fms/%.1fms (%.1f%%) (%u frames)",
646 elapsed
, budget
, percent
, frame_count
);
649 /* Decompress entire vorbis stream into buffer */
650 static float *audio_decompress_vorbis( const unsigned char *data
, int len
,
651 int channels
, u32
*samples
)
654 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
658 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
662 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
664 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
666 (float)length_samples
/ (44100.0f
*(float)channels
),
667 (float)(length_samples
*4*channels
) / 1024.0f
);
669 float *buffer
= audio_alloc( length_samples
* channels
* sizeof(float) );
672 stb_vorbis_close( pv
);
676 int read_samples
= stb_vorbis_get_samples_float_interleaved(
677 pv
, channels
, buffer
, length_samples
* channels
);
679 if( read_samples
!= length_samples
)
681 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
682 length_samples
, read_samples
);
683 length_samples
= read_samples
;
686 stb_vorbis_close( pv
);
687 *samples
= length_samples
;
691 static int audio_clip_load( audio_clip
*clip
)
693 /* Load and decompress */
695 void *filedata
= vg_asset_read_s( clip
->path
, &file_len
);
699 vg_error( "OGG load failed (%s)\n", clip
->path
);
703 if( clip
->source_mode
== k_audio_source_mono
)
706 float *sound
= audio_decompress_vorbis( filedata
, file_len
, 1, &samples
);
710 float seconds
= (float)samples
/ 44100.0f
,
711 mb
= (float)(samples
*4) / (1024.0f
*1024.0f
);
713 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
714 clip
->path
, seconds
, mb
);
716 else if( clip
->source_mode
== k_audio_source_mono_compressed
)
718 void *data
= audio_alloc( file_len
);
719 memcpy( data
, filedata
, file_len
);
722 clip
->len
= file_len
;
724 float mb
= (float)(file_len
) / (1024.0f
*1024.0f
);
725 vg_info( "Loaded audio clip[mono_compressed] '%s' (%.1fmb)\n",
728 else if( clip
->source_mode
== k_audio_source_stereo_compressed
)
735 vg_error( "Source mode (%u) currently unsupported\n", clip
->source_mode
);
745 vg_error( "Unkown source mode (%u)\n", clip
->source_mode
);
752 static void audio_clip_loadn( audio_clip
*arr
, int count
)
754 for( int i
=0; i
<count
; i
++ )
755 audio_clip_load( &arr
[i
] );
762 static void audio_pack_play( audio_pack
*source
, audio_player
*sys
, int id
)
764 audio_require_lock();
767 sys
->fadeout_current
= 0;
768 sys
->source
= source
->data
;
769 sys
->cur
= source
->segments
[ id
*2 + 0 ];
770 sys
->end
= source
->segments
[ id
*2 + 1 ];
771 sys
->ch
= source
->ch
;
772 sys
->source_mode
= source
->source_mode
;
774 /* for diagnostics */
775 sys
->clip_start
= sys
->cur
;
776 sys
->clip_end
= sys
->end
;
777 sys
->buffer_length
= source
->segments
[ (source
->numsegments
-1)*2 + 1 ];
780 audio_player_push( sys
);
785 /* Mark change to be uploaded through queue system */
786 static void audio_player_commit( audio_player
*sys
)
788 audio_require_lock();
790 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
792 vg_warn( "Audio commit queue full\n" );
798 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
803 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
804 ent
->info
= sys
->info
;
806 sys
->active_entity
= AATREE_PTR_NIL
;
809 /* Play a clip using player. If its already playing something, it will
810 * fadeout quickly and start the next sound */
811 static void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
813 audio_require_lock();
815 player
->info
.source
= clip
;
816 audio_player_commit( player
);
819 static void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
829 * Safety enforced Get/set attributes
832 static void audio_player_set_position( audio_player
*sys
, v3f pos
)
834 audio_require_lock();
835 v3_copy( pos
, sys
->info
.world_position
);
838 static void audio_player_set_vol( audio_player
*sys
, float vol
)
840 audio_require_lock();
844 static float audio_player_get_vol( audio_player
*sys
)
846 audio_require_lock();
847 return sys
->info
.vol
;
850 static void audio_player_set_pan( audio_player
*sys
, float pan
)
852 audio_require_lock();
856 static float audio_player_get_pan( audio_player
*sys
)
858 audio_require_lock();
859 return sys
->info
.pan
;
862 static void audio_player_set_flags( audio_player
*sys
, u32 flags
)
864 audio_require_lock();
865 sys
->info
.flags
= flags
;
868 static u32
audio_player_get_flags( audio_player
*sys
)
870 audio_require_lock();
871 return sys
->info
.flags
;
879 static void audio_debug_ui(void)
881 if( !vg_audio
.debug_ui
)
888 u32 cursor
, flags
, length
;
891 infos
[ SFX_MAX_SYSTEMS
];
898 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
900 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
905 audio_entity
*ent
= &aap
->ent
;
906 struct sound_info
*snd
= &infos
[ num_systems
++ ];
908 snd
->name
= ent
->name
;
909 snd
->cursor
= ent
->cur
;
910 snd
->flags
= ent
->info
.flags
;
911 snd
->length
= ent
->length
;
912 snd
->vol
= ent
->info
.vol
*100.0f
;
914 strcpy( perf
, vg_audio
.performance_info
);
918 ui_global_ctx
.cursor
[0] = 10;
919 ui_global_ctx
.cursor
[1] = 10;
920 ui_global_ctx
.cursor
[2] = 150;
921 ui_global_ctx
.cursor
[3] = 12;
922 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
924 float usage
= (float)vg_audio
.mem_current
/ (1024.0f
*1024.0f
),
925 total
= (float)vg_audio
.mem_total
/ (1024.0f
*1024.0f
),
926 percent
= (usage
/total
) * 100.0f
;
927 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
929 ui_global_ctx
.cursor
[1] += 20;
930 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
932 ui_global_ctx
.cursor
[1] += 20;
934 /* Draw audio stack */
935 for( int i
=0; i
<num_systems
; i
++ )
937 struct sound_info
*inf
= &infos
[i
];
939 ui_global_ctx
.cursor
[2] = 150;
940 ui_global_ctx
.cursor
[3] = 12;
942 u32 alpha
= 0xa0000000;
944 ui_new_node( &ui_global_ctx
);
946 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x00333333|alpha
);
948 ui_px baseline
= ui_global_ctx
.cursor
[0],
950 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
953 ui_global_ctx
.cursor
[2] = 2;
954 ui_global_ctx
.cursor
[0] = c
;
955 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0xffffffff );
957 ui_global_ctx
.cursor
[0] = baseline
+ 2;
958 ui_global_ctx
.cursor
[1] += 2;
959 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
960 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
963 ui_end_down( &ui_global_ctx
);
964 ui_global_ctx
.cursor
[1] += 1;
968 #endif /* VG_AUDIO_H */