1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
6 #define MA_NO_GENERATION
13 #define MA_NO_NODE_GRAPH
14 #define MA_NO_RESOURCE_MANAGER
16 #include "dr_soft/miniaudio.h"
19 #include "vg/vg_stdint.h"
20 #include "vg/vg_platform.h"
24 #include "vg/vg_console.h"
25 #include "vg/vg_store.h"
31 #pragma GCC push_options
32 #pragma GCC optimize ("O3")
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38 #define STB_VORBIS_MAX_CHANNELS 2
39 #include "stb/stb_vorbis.h"
43 #pragma GCC pop_options
44 #pragma GCC diagnostic pop
48 #define SFX_MAX_SYSTEMS 32
49 #define AUDIO_FLAG_LOOP 0x1
50 #define AUDIO_FLAG_ONESHOT 0x2
51 #define AUDIO_FLAG_SPACIAL_3D 0x4
52 #define AUDIO_FLAG_AUTO_START 0x8
53 #define AUDIO_FLAG_KILL 0x10
55 #define FADEOUT_LENGTH 1100
56 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
58 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
60 enum audio_source_mode
63 k_audio_source_compressed
,
66 typedef struct audio_clip audio_clip
;
70 enum audio_source_mode source_mode
;
76 typedef struct audio_mix_info audio_mix_info
;
86 typedef struct audio_player audio_player
;
89 aatree_ptr active_entity
; /* non-nil if currently playing */
97 typedef struct audio_entity audio_entity
;
100 audio_player
*player
;
106 u32 fadeout
, fadeout_current
;
113 * play again: if already playing, leave in queue while it fadeouts
114 * oneshot: create a ghost entity
118 static struct vg_audio_system
120 ma_device miniaudio_device
;
121 ma_device_config miniaudio_dconfig
;
130 vg_mutex mux_checker
,
133 /* Audio engine, thread 1 */
134 struct active_audio_player
140 aatree_pool_node pool_node
;
143 stb_vorbis
*vorbis_handle
;
144 stb_vorbis_alloc vorbis_alloc
;
146 active_players
[ SFX_MAX_SYSTEMS
];
148 aatree active_pool_info
; /* note: just using the pool */
149 aatree_ptr active_pool_head
;
151 /* System queue, and access from thread 0 */
152 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
154 int debug_ui
, debug_ui_3d
;
161 static struct vg_profile
162 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
163 .name
= "[T2] audio_decode()"},
164 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
165 .name
= "[T2] audio_mix()"},
166 vg_prof_audio_decode
,
170 * These functions are called from the main thread and used to prevent bad
171 * access. TODO: They should be no-ops in release builds.
173 VG_STATIC
int audio_lock_checker_load(void)
176 vg_mutex_lock( &vg_audio
.mux_checker
);
177 value
= vg_audio
.sync_locked
;
178 vg_mutex_unlock( &vg_audio
.mux_checker
);
182 VG_STATIC
void audio_lock_checker_store( int value
)
184 vg_mutex_lock( &vg_audio
.mux_checker
);
185 vg_audio
.sync_locked
= value
;
186 vg_mutex_unlock( &vg_audio
.mux_checker
);
189 VG_STATIC
void audio_require_lock(void)
191 if( audio_lock_checker_load() )
194 vg_error( "Modifying sound effects systems requires locking\n" );
198 VG_STATIC
void audio_lock(void)
200 vg_mutex_lock( &vg_audio
.mux_sync
);
201 audio_lock_checker_store(1);
204 VG_STATIC
void audio_unlock(void)
206 audio_lock_checker_store(0);
207 vg_mutex_unlock( &vg_audio
.mux_sync
);
211 VG_STATIC
void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
212 const void *pInput
, ma_uint32 frameCount
);
214 VG_STATIC
void vg_audio_init(void)
216 vg_mutex_init( &vg_audio
.mux_checker
);
217 vg_mutex_init( &vg_audio
.mux_sync
);
219 /* TODO: Move here? */
220 vg_convar_push( (struct vg_convar
){
221 .name
= "debug_audio",
222 .data
= &vg_audio
.debug_ui
,
223 .data_type
= k_convar_dtype_i32
,
224 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
228 /* allocate memory */
231 vg_audio
.audio_pool
=
232 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32,
236 u32 decode_size
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
237 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
240 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
241 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
243 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
244 vg_audio
.active_pool_info
.p_cmp
= NULL
;
245 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
247 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
248 ma_device
*device
= &vg_audio
.miniaudio_device
;
250 *dconf
= ma_device_config_init( ma_device_type_playback
);
251 dconf
->playback
.format
= ma_format_f32
;
252 dconf
->playback
.channels
= 2;
253 dconf
->sampleRate
= 44100;
254 dconf
->dataCallback
= audio_mixer_callback
;
255 dconf
->periodSizeInFrames
= 441;
257 dconf
->pUserData
= NULL
;
259 vg_info( "Starting audio engine\n" );
261 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
263 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
267 if( ma_device_start( device
) != MA_SUCCESS
)
269 ma_device_uninit( device
);
270 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
274 vg_success( "Ready\n" );
277 VG_STATIC
void vg_audio_free(void * nothing
)
279 ma_device
*device
= &vg_audio
.miniaudio_device
;
280 ma_device_uninit( device
);
283 vg_free( vg_audio
.mem
);
292 static aatree_ptr
audio_alloc_entity_internal(void)
294 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
295 &vg_audio
.active_pool_head
);
297 if( playerid
== AATREE_PTR_NIL
)
298 return AATREE_PTR_NIL
;
300 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
306 VG_STATIC
void audio_entity_free_internal( aatree_ptr id
)
308 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
311 /* Notify player that we've finished */
312 if( aap
->ent
.player
)
313 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
316 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
317 &vg_audio
.active_pool_head
);
320 VG_STATIC
void *audio_entity_vorbis_ptr( aatree_ptr entid
)
322 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
323 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
328 VG_STATIC
void audio_entity_start( audio_entity
*src
)
330 aatree_ptr entid
= audio_alloc_entity_internal();
331 if( entid
== AATREE_PTR_NIL
)
334 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
336 ent
->info
= src
->info
;
337 ent
->name
= src
->info
.source
->path
;
339 ent
->player
= src
->player
;
342 ent
->fadeout_current
= 0;
344 /* Notify main player we are dequeud and playing */
347 src
->player
->enqued
= 0;
348 src
->player
->active_entity
= entid
;
351 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
353 /* Setup vorbis decoder */
354 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
356 stb_vorbis_alloc alloc
= {
357 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
358 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
362 stb_vorbis
*decoder
= stb_vorbis_open_memory(
363 src
->info
.source
->data
,
364 src
->info
.source
->size
, &err
, &alloc
);
368 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
369 src
->info
.source
->path
, err
);
371 audio_entity_free_internal( entid
);
376 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
379 aap
->vorbis_handle
= decoder
;
383 ent
->length
= src
->info
.source
->size
;
388 * Read everything from the queue
390 VG_STATIC
void audio_system_enque(void)
392 /* Process incoming sound queue */
396 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
398 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
403 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
405 audio_entity_start( src
);
409 /* Otherwise try start fadeout but dont remove from queue */
411 aatree_ptr entid
= src
->player
->active_entity
;
412 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
415 ent
->fadeout
= FADEOUT_LENGTH
;
416 ent
->fadeout_current
= FADEOUT_LENGTH
;
419 vg_audio
.entity_queue
[ wr
++ ] = *src
;
424 audio_entity_start( src
);
428 vg_audio
.queue_len
= wr
;
430 /* Localize others memory */
431 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
433 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
437 if( aap
->ent
.player
)
439 /* Only copy information in whilst not requeing */
440 if( aap
->ent
.player
->enqued
== 0 )
442 aap
->ent
.info
= aap
->ent
.player
->info
;
444 if( (aap
->ent
.info
.flags
& AUDIO_FLAG_KILL
) && !aap
->ent
.fadeout
)
446 aap
->ent
.fadeout
= FADEOUT_LENGTH
;
447 aap
->ent
.fadeout_current
= FADEOUT_LENGTH
;
457 * Redistribute sound systems
459 VG_STATIC
void audio_system_cleanup(void)
463 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
465 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
468 audio_entity
*src
= &aap
->ent
;
469 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
475 audio_entity_free_internal( i
);
484 * Get effective volume and pan from this entity
486 VG_STATIC
void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
488 if( ent
->info
.vol
< 0.01f
)
490 *vol
= ent
->info
.vol
;
496 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
498 float dist2
= v3_length2( delta
);
500 if( dist2
< 0.0001f
)
507 float dist
= sqrtf( dist2
),
508 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
510 v3_muls( delta
, 1.0f
/dist
, delta
);
511 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
512 *vol
= 1.0f
/(attn
*attn
);
516 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
518 for( u32 i
=0; i
<count
; i
++ )
520 dst
[ i
*2 + 0 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
521 dst
[ i
*2 + 1 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
526 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
529 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
533 c
= VG_MIN( 1, f
->channels
- 1 );
537 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
542 for( int j
=0; j
< k
; ++j
)
544 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
545 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
549 f
->channel_buffer_start
+= k
;
554 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
562 * ........ more wrecked code sorry!
565 stb_vorbis_get_samples_i16_downmixed( stb_vorbis
*f
, i16
*buffer
, int len
)
568 c
= VG_MIN( 1, f
->channels
- 1 );
572 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
577 for( int j
=0; j
< k
; ++j
)
579 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
580 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
582 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
583 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
587 f
->channel_buffer_start
+= k
;
592 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
599 VG_STATIC
void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
601 vg_profile_begin( &_vg_prof_audio_decode
);
603 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
604 audio_entity
*ent
= &aap
->ent
;
606 u32 remaining
= count
;
607 u32 cursor
= ent
->cur
;
612 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
613 remaining
-= samples_this_run
;
615 float *dst
= &buf
[ buffer_pos
* 2 ];
617 int source_mode
= ent
->info
.source
->source_mode
;
619 if( source_mode
== k_audio_source_mono
)
621 i16
*src_buffer
= ent
->info
.source
->data
,
622 *src
= &src_buffer
[cursor
];
624 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
626 else if( source_mode
== k_audio_source_compressed
)
628 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
633 if( read_samples
!= samples_this_run
)
635 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
639 cursor
+= samples_this_run
;
640 buffer_pos
+= samples_this_run
;
642 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
644 if( source_mode
== k_audio_source_compressed
)
646 stb_vorbis_seek_start( aap
->vorbis_handle
);
658 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
659 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
666 vg_profile_end( &_vg_prof_audio_decode
);
669 VG_STATIC
void audio_entity_mix( aatree_ptr id
, float *buffer
,
672 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
674 u32 cursor
= ent
->cur
, buffer_pos
= 0;
675 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
677 u32 frames_write
= frame_count
;
678 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
680 float vol
= ent
->info
.vol
,
683 audio_entity_get_samples( id
, frame_count
, pcf
);
685 vg_profile_begin( &_vg_prof_audio_mix
);
687 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
688 audio_entity_spacialize( ent
, &vol
, &pan
);
690 for( u32 j
=0; j
<frame_count
; j
++ )
692 float frame_vol
= vol
;
696 /* Force this system to be removed now */
697 if( ent
->fadeout_current
== 0 )
699 ent
->info
.flags
= 0x00;
700 ent
->cur
= ent
->length
;
704 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
705 ent
->fadeout_current
--;
711 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
712 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
717 vg_profile_end( &_vg_prof_audio_mix
);
721 * callback from miniaudio.h interface
723 VG_STATIC
void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
724 const void *pInput
, ma_uint32 frame_count
)
726 struct timespec time_start
, time_end
;
727 clock_gettime( CLOCK_REALTIME
, &time_start
);
729 audio_system_enque();
732 float *pOut32F
= (float *)pOutBuf
;
733 for( int i
=0; i
<frame_count
*2; i
++ )
737 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
739 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
743 audio_entity_mix( i
, pOut32F
, frame_count
);
748 audio_system_cleanup();
750 /* TODO: what the hell is this? */
755 vg_profile_increment( &_vg_prof_audio_decode
);
756 vg_profile_increment( &_vg_prof_audio_mix
);
758 vg_prof_audio_mix
= _vg_prof_audio_mix
;
759 vg_prof_audio_decode
= _vg_prof_audio_decode
;
761 vg_audio
.samples_last
= frame_count
;
765 VG_STATIC
void audio_clip_load( audio_clip
*clip
, void *lin_alloc
)
767 if( lin_alloc
== NULL
)
768 lin_alloc
= vg_audio
.audio_pool
;
770 if( clip
->source_mode
== k_audio_source_mono
)
772 vg_linear_clear( vg_mem
.scratch
);
775 stb_vorbis_alloc alloc
= {
776 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
777 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
780 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
783 stb_vorbis
*decoder
= stb_vorbis_open_memory(
784 filedata
, fsize
, &err
, &alloc
);
788 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
790 vg_fatal_exit_loop( "Vorbis decode error" );
793 /* only mono is supported in uncompressed */
794 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
795 data_size
= length_samples
* sizeof(i16
);
798 clip
->data
= vg_linear_alloc( lin_alloc
, data_size
);
799 clip
->size
= length_samples
;
802 int read_samples
= stb_vorbis_get_samples_i16_downmixed(
803 decoder
, clip
->data
, length_samples
);
805 if( read_samples
!= length_samples
)
806 vg_fatal_exit_loop( "Decode error" );
808 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
809 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip
->path
, mb
,
813 /* load in directly */
814 else if( clip
->source_mode
== k_audio_source_compressed
)
817 clip
->data
= vg_file_read( lin_alloc
, clip
->path
, &clip
->size
);
821 vg_fatal_exit_loop( "Audio failed to load" );
823 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
824 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
828 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
)
830 for( int i
=0; i
<count
; i
++ )
831 audio_clip_load( &arr
[i
], lin_alloc
);
834 /* Mark change to be uploaded through queue system */
835 VG_STATIC
void audio_player_commit( audio_player
*sys
)
837 audio_require_lock();
839 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
841 vg_warn( "Audio commit queue full\n" );
847 vg_warn( "[2] Audio commit spamming; already enqued (%s)\n", sys
->name
);
852 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
853 ent
->info
= sys
->info
;
857 VG_STATIC
void audio_require_init( audio_player
*player
)
862 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
865 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
867 if( clip
->data
&& clip
->size
)
870 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
873 /* Play a clip using player. If its already playing something, it will
874 * fadeout quickly and start the next sound */
875 VG_STATIC
void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
877 audio_require_lock();
878 audio_require_init( player
);
879 audio_require_clip_loaded( clip
);
881 if( player
->info
.flags
& AUDIO_FLAG_KILL
)
883 vg_error( "Can't start audio clip on player that is/has disconnected" );
889 vg_warn( "[1] Audio commit spamming; already enqued (%s)\n",
894 player
->info
.source
= clip
;
895 audio_player_commit( player
);
899 VG_STATIC
void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
901 audio_require_lock();
902 audio_require_init( player
);
906 VG_STATIC
void audio_play_oneshot( audio_clip
*clip
, float volume
)
908 audio_require_lock();
909 audio_require_clip_loaded( clip
);
911 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
913 vg_warn( "Audio commit queue full\n" );
917 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
919 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
920 ent
->info
.pan
= 0.0f
;
921 ent
->info
.source
= clip
;
922 ent
->info
.vol
= volume
;
926 VG_STATIC
void audio_player_init( audio_player
*player
)
928 player
->active_entity
= AATREE_PTR_NIL
;
937 * Safety enforced Get/set attributes
940 VG_STATIC
int audio_player_is_playing( audio_player
*sys
)
942 audio_require_lock();
944 if( sys
->active_entity
!= AATREE_PTR_NIL
)
950 VG_STATIC
void audio_player_set_position( audio_player
*sys
, v3f pos
)
952 audio_require_lock();
953 v3_copy( pos
, sys
->info
.world_position
);
956 VG_STATIC
void audio_player_set_vol( audio_player
*sys
, float vol
)
958 audio_require_lock();
962 VG_STATIC
float audio_player_get_vol( audio_player
*sys
)
964 audio_require_lock();
965 return sys
->info
.vol
;
968 VG_STATIC
void audio_player_set_pan( audio_player
*sys
, float pan
)
970 audio_require_lock();
974 VG_STATIC
float audio_player_get_pan( audio_player
*sys
)
976 audio_require_lock();
977 return sys
->info
.pan
;
980 VG_STATIC
void audio_player_set_flags( audio_player
*sys
, u32 flags
)
982 audio_require_lock();
983 sys
->info
.flags
= flags
;
986 VG_STATIC u32
audio_player_get_flags( audio_player
*sys
)
988 audio_require_lock();
989 return sys
->info
.flags
;
997 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
999 if( !vg_audio
.debug_ui
)
1006 u32 cursor
, flags
, length
;
1010 infos
[ SFX_MAX_SYSTEMS
];
1011 int num_systems
= 0;
1015 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
1017 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1022 audio_entity
*ent
= &aap
->ent
;
1023 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1025 snd
->name
= ent
->name
;
1026 snd
->cursor
= ent
->cur
;
1027 snd
->flags
= ent
->info
.flags
;
1028 snd
->length
= ent
->length
;
1029 snd
->vol
= ent
->info
.vol
*100.0f
;
1030 v3_copy( ent
->info
.world_position
, snd
->pos
);
1033 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1034 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1035 &vg_prof_audio_mix
}, 2,
1036 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1044 vg_uictx
.cursor
[0] = 258;
1045 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1046 vg_uictx
.cursor
[2] = 150;
1047 vg_uictx
.cursor
[3] = 12;
1049 float mb1
= 1024.0f
*1024.0f
,
1050 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1051 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1052 percent
= (usage
/total
) * 100.0f
;
1054 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1056 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1057 vg_uictx
.cursor
[1] += 20;
1059 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1060 u32 overlap_length
= 0;
1062 /* Draw audio stack */
1063 for( int i
=0; i
<num_systems
; i
++ )
1065 struct sound_info
*inf
= &infos
[i
];
1067 vg_uictx
.cursor
[2] = 200;
1068 vg_uictx
.cursor
[3] = 18;
1070 u32 alpha
= 0xa0000000;
1074 ui_fill_rect( vg_uictx
.cursor
, 0x00333333|alpha
);
1076 ui_px baseline
= vg_uictx
.cursor
[0],
1078 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1081 vg_uictx
.cursor
[2] = 2;
1082 vg_uictx
.cursor
[0] = c
;
1083 ui_fill_rect( vg_uictx
.cursor
, 0xffffffff );
1085 vg_uictx
.cursor
[0] = baseline
+ 2;
1086 vg_uictx
.cursor
[1] += 2;
1087 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1088 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1090 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1093 v3_copy( inf
->pos
, wpos
);
1095 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1097 if( wpos
[3] < 0.0f
)
1098 goto projected_behind
;
1100 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1101 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1104 wr
[0] = wpos
[0] * vg
.window_x
;
1105 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1109 for( int j
=0; j
<12; j
++ )
1112 for( int k
=0; k
<overlap_length
; k
++ )
1114 ui_px
*wk
= overlap_buffer
[k
];
1115 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1116 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1129 ui_text( wr
, perf
, 1, 0 );
1131 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1138 vg_uictx
.cursor
[1] += 1;
1142 #endif /* VG_AUDIO_H */