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"
20 #include "vg/vg_stdint.h"
21 #include "vg/vg_platform.h"
25 #include "vg/vg_console.h"
26 #include "vg/vg_store.h"
31 #pragma GCC push_options
32 #pragma GCC optimize ("O3")
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38 #define STB_VORBIS_MAX_CHANNELS 2
39 #include "stb/stb_vorbis.h"
41 #pragma GCC diagnostic pop
44 #pragma GCC pop_options
47 #define SFX_MAX_SYSTEMS 32
48 #define AUDIO_FLAG_LOOP 0x1
49 #define AUDIO_FLAG_ONESHOT 0x2
50 #define AUDIO_FLAG_SPACIAL_3D 0x4
51 #define AUDIO_FLAG_AUTO_START 0x8
53 #define FADEOUT_LENGTH 1100
54 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
56 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
58 enum audio_source_mode
61 k_audio_source_compressed
,
64 typedef struct audio_clip audio_clip
;
68 enum audio_source_mode source_mode
;
72 u32 len
; /* decompressed: sample count,
73 compressed: file size */
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
;
123 void *mem
, *decode_mem
;
131 vg_mutex mux_checker
,
134 /* Audio engine, thread 1 */
135 struct active_audio_player
141 aatree_pool_node pool_node
;
144 stb_vorbis
*vorbis_handle
;
145 stb_vorbis_alloc vorbis_alloc
;
147 active_players
[ SFX_MAX_SYSTEMS
];
149 aatree active_pool_info
; /* note: just using the pool */
150 aatree_ptr active_pool_head
;
152 /* System queue, and access from thread 0 */
153 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
155 int debug_ui
, debug_ui_3d
;
162 static struct vg_profile
163 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
164 .name
= "[T2] audio_decode()"},
165 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
166 .name
= "[T2] audio_mix()"},
167 vg_prof_audio_decode
,
170 static void *audio_alloc( u32 size
)
172 u32 new_current
= vg_audio
.mem_current
+ size
;
173 if( new_current
> vg_audio
.mem_total
)
174 vg_fatal_exit_loop( "Audio pool ran out of memory" );
176 void *ptr
= vg_audio
.mem
+ vg_audio
.mem_current
;
177 vg_audio
.mem_current
= new_current
;
184 * These functions are called from the main thread and used to prevent bad
185 * access. TODO: They should be no-ops in release builds.
187 static int audio_lock_checker_load(void)
190 vg_mutex_lock( &vg_audio
.mux_checker
);
191 value
= vg_audio
.sync_locked
;
192 vg_mutex_unlock( &vg_audio
.mux_checker
);
196 static void audio_lock_checker_store( int value
)
198 vg_mutex_lock( &vg_audio
.mux_checker
);
199 vg_audio
.sync_locked
= value
;
200 vg_mutex_unlock( &vg_audio
.mux_checker
);
203 static void audio_require_lock(void)
205 if( audio_lock_checker_load() )
208 vg_error( "Modifying sound effects systems requires locking\n" );
212 static void audio_lock(void)
214 vg_mutex_lock( &vg_audio
.mux_sync
);
215 audio_lock_checker_store(1);
218 static void audio_unlock(void)
220 audio_lock_checker_store(0);
221 vg_mutex_unlock( &vg_audio
.mux_sync
);
225 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
226 const void *pInput
, ma_uint32 frameCount
);
228 static void vg_audio_init(void)
230 vg_mutex_init( &vg_audio
.mux_checker
);
231 vg_mutex_init( &vg_audio
.mux_sync
);
233 vg_convar_push( (struct vg_convar
){
234 .name
= "debug_audio",
235 .data
= &vg_audio
.debug_ui
,
236 .data_type
= k_convar_dtype_i32
,
237 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
241 vg_convar_push( (struct vg_convar
){
242 .name
= "debug_audio_clips",
243 .data
= &vg_audio
.debug_ui_3d
,
244 .data_type
= k_convar_dtype_i32
,
245 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
249 u32 decode_region
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
250 vg_audio
.mem_total
= 1024*1024*32;
251 vg_audio
.mem_current
= 0;
252 vg_audio
.mem
= vg_alloc( vg_audio
.mem_total
+ decode_region
);
253 vg_audio
.decode_mem
= &((u8
*)vg_audio
.mem
)[vg_audio
.mem_total
];
256 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
257 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
259 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
260 vg_audio
.active_pool_info
.p_cmp
= NULL
;
261 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
263 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
264 ma_device
*device
= &vg_audio
.miniaudio_device
;
266 *dconf
= ma_device_config_init( ma_device_type_playback
);
267 dconf
->playback
.format
= ma_format_f32
;
268 dconf
->playback
.channels
= 2;
269 dconf
->sampleRate
= 44100;
270 dconf
->dataCallback
= audio_mixer_callback
;
271 dconf
->periodSizeInFrames
= 441;
273 dconf
->pUserData
= NULL
;
275 vg_info( "Starting audio engine\n" );
277 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
279 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
283 if( ma_device_start( device
) != MA_SUCCESS
)
285 ma_device_uninit( device
);
286 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
290 vg_success( "Ready\n" );
293 static void vg_audio_free(void * nothing
)
295 ma_device
*device
= &vg_audio
.miniaudio_device
;
296 ma_device_uninit( device
);
298 vg_free( vg_audio
.mem
);
306 static aatree_ptr
audio_alloc_entity_internal(void)
308 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
309 &vg_audio
.active_pool_head
);
311 if( playerid
== AATREE_PTR_NIL
)
312 return AATREE_PTR_NIL
;
314 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
320 static void audio_entity_free_internal( aatree_ptr id
)
322 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
325 /* Notify player that we've finished */
326 if( aap
->ent
.player
)
327 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
330 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
331 &vg_audio
.active_pool_head
);
334 static void *audio_entity_vorbis_ptr( aatree_ptr entid
)
336 u8
*buf
= (u8
*)vg_audio
.decode_mem
,
337 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
342 static void audio_entity_start( audio_entity
*src
)
344 aatree_ptr entid
= audio_alloc_entity_internal();
345 if( entid
== AATREE_PTR_NIL
)
348 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
350 ent
->info
= src
->info
;
351 ent
->name
= src
->info
.source
->path
;
353 ent
->player
= src
->player
;
356 ent
->fadeout_current
= 0;
358 /* Notify main player we are dequeud and playing */
361 src
->player
->enqued
= 0;
362 src
->player
->active_entity
= entid
;
365 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
367 /* Setup vorbis decoder */
368 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
370 stb_vorbis_alloc alloc
= {
371 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
372 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
376 stb_vorbis
*decoder
= stb_vorbis_open_memory(
377 src
->info
.source
->data
, src
->info
.source
->len
, &err
, &alloc
);
381 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
382 src
->info
.source
->path
, err
);
384 audio_entity_free_internal( entid
);
389 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
392 aap
->vorbis_handle
= decoder
;
396 ent
->length
= src
->info
.source
->len
;
401 * Read everything from the queue
403 static void audio_system_enque(void)
405 /* Process incoming sound queue */
409 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
411 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
416 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
418 audio_entity_start( src
);
422 /* Otherwise try start fadeout but dont remove from queue */
424 aatree_ptr entid
= src
->player
->active_entity
;
425 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
428 ent
->fadeout
= FADEOUT_LENGTH
;
429 ent
->fadeout_current
= FADEOUT_LENGTH
;
432 vg_audio
.entity_queue
[ wr
++ ] = *src
;
437 audio_entity_start( src
);
441 vg_audio
.queue_len
= wr
;
443 /* Localize others memory */
444 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
446 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
450 if( aap
->ent
.player
)
452 /* Only copy information in whilst not requeing */
453 if( aap
->ent
.player
->enqued
== 0 )
455 aap
->ent
.info
= aap
->ent
.player
->info
;
464 * Redistribute sound systems
466 static void audio_system_cleanup(void)
470 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
472 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
475 audio_entity
*src
= &aap
->ent
;
476 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
482 audio_entity_free_internal( i
);
491 * Get effective volume and pan from this entity
493 static void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
495 if( ent
->info
.vol
< 0.01f
)
497 *vol
= ent
->info
.vol
;
503 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
505 float dist2
= v3_length2( delta
);
507 if( dist2
< 0.0001f
)
514 float dist
= sqrtf( dist2
),
515 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
517 v3_muls( delta
, 1.0f
/dist
, delta
);
518 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
519 *vol
= 1.0f
/(attn
*attn
);
523 static void audio_decode_uncompressed_mono( float *src
, u32 count
, float *dst
)
525 for( u32 i
=0; i
<count
; i
++ )
527 dst
[ i
*2 + 0 ] = src
[i
];
528 dst
[ i
*2 + 1 ] = src
[i
];
533 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
536 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
540 c
= VG_MIN( 1, f
->channels
- 1 );
544 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
549 for( int j
=0; j
< k
; ++j
)
551 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
552 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
556 f
->channel_buffer_start
+= k
;
561 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
568 static void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
570 vg_profile_begin( &_vg_prof_audio_decode
);
572 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
573 audio_entity
*ent
= &aap
->ent
;
575 u32 remaining
= count
;
576 u32 cursor
= ent
->cur
;
581 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
582 remaining
-= samples_this_run
;
584 float *dst
= &buf
[ buffer_pos
* 2 ];
586 int source_mode
= ent
->info
.source
->source_mode
;
588 if( source_mode
== k_audio_source_mono
)
590 float *src
= &((float *)ent
->info
.source
->data
)[ cursor
];
591 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
593 else if( source_mode
== k_audio_source_compressed
)
595 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
600 if( read_samples
!= samples_this_run
)
602 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
606 cursor
+= samples_this_run
;
607 buffer_pos
+= samples_this_run
;
609 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
611 if( source_mode
== k_audio_source_compressed
)
613 stb_vorbis_seek_start( aap
->vorbis_handle
);
625 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
626 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
633 vg_profile_end( &_vg_prof_audio_decode
);
636 static void audio_entity_mix( aatree_ptr id
, float *buffer
,
639 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
641 u32 cursor
= ent
->cur
, buffer_pos
= 0;
642 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
644 u32 frames_write
= frame_count
;
645 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
647 float vol
= ent
->info
.vol
,
650 audio_entity_get_samples( id
, frame_count
, pcf
);
652 vg_profile_begin( &_vg_prof_audio_mix
);
654 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
655 audio_entity_spacialize( ent
, &vol
, &pan
);
657 for( u32 j
=0; j
<frame_count
; j
++ )
659 float frame_vol
= vol
;
663 /* Force this system to be removed now */
664 if( ent
->fadeout_current
== 0 )
666 ent
->info
.flags
= 0x00;
667 ent
->cur
= ent
->length
;
671 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
672 ent
->fadeout_current
--;
678 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
679 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
684 vg_profile_end( &_vg_prof_audio_mix
);
688 * callback from miniaudio.h interface
690 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
691 const void *pInput
, ma_uint32 frame_count
)
693 struct timespec time_start
, time_end
;
694 clock_gettime( CLOCK_REALTIME
, &time_start
);
696 audio_system_enque();
699 float *pOut32F
= (float *)pOutBuf
;
700 for( int i
=0; i
<frame_count
*2; i
++ )
704 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
706 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
710 audio_entity_mix( i
, pOut32F
, frame_count
);
715 audio_system_cleanup();
717 /* TODO: what the hell is this? */
722 vg_profile_increment( &_vg_prof_audio_decode
);
723 vg_profile_increment( &_vg_prof_audio_mix
);
725 vg_prof_audio_mix
= _vg_prof_audio_mix
;
726 vg_prof_audio_decode
= _vg_prof_audio_decode
;
728 vg_audio
.samples_last
= frame_count
;
732 /* Decompress entire vorbis stream into buffer */
733 static float *audio_decompress_vorbis( const unsigned char *data
, int len
,
734 int channels
, u32
*samples
)
737 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
741 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
745 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
747 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
749 (float)length_samples
/ (44100.0f
*(float)channels
),
750 (float)(length_samples
*4*channels
) / 1024.0f
);
752 float *buffer
= audio_alloc( length_samples
* channels
* sizeof(float) );
755 stb_vorbis_close( pv
);
756 vg_error( "Failed to allocated memory for audio\n" );
760 int read_samples
= stb_vorbis_get_samples_float_interleaved(
761 pv
, channels
, buffer
, length_samples
* channels
);
763 if( read_samples
!= length_samples
)
765 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
766 length_samples
, read_samples
);
767 length_samples
= read_samples
;
770 stb_vorbis_close( pv
);
771 *samples
= length_samples
;
775 static int audio_clip_load( audio_clip
*clip
)
777 /* Load and decompress */
779 void *filedata
= vg_asset_read_s( clip
->path
, &file_len
);
783 vg_error( "OGG load failed (%s)\n", clip
->path
);
787 if( clip
->source_mode
== k_audio_source_mono
)
790 float *sound
= audio_decompress_vorbis( filedata
, file_len
, 1, &samples
);
794 float seconds
= (float)samples
/ 44100.0f
,
795 mb
= (float)(samples
*4) / (1024.0f
*1024.0f
);
797 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
798 clip
->path
, seconds
, mb
);
800 else if( clip
->source_mode
== k_audio_source_compressed
)
802 void *data
= audio_alloc( file_len
);
803 memcpy( data
, filedata
, file_len
);
806 clip
->len
= file_len
;
808 float mb
= (float)(file_len
) / (1024.0f
*1024.0f
);
809 vg_info( "Loaded audio clip[compressed] '%s' (%.1fmb)\n",
819 vg_error( "Unkown source mode (%u)\n", clip
->source_mode
);
826 static void audio_clip_loadn( audio_clip
*arr
, int count
)
828 for( int i
=0; i
<count
; i
++ )
829 audio_clip_load( &arr
[i
] );
832 /* Mark change to be uploaded through queue system */
833 static void audio_player_commit( audio_player
*sys
)
835 audio_require_lock();
837 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
839 vg_warn( "Audio commit queue full\n" );
845 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
850 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
851 ent
->info
= sys
->info
;
855 static void audio_require_init( audio_player
*player
)
860 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
863 static void audio_require_clip_loaded( audio_clip
*clip
)
868 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
871 /* Play a clip using player. If its already playing something, it will
872 * fadeout quickly and start the next sound */
873 static void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
875 audio_require_lock();
876 audio_require_init( player
);
877 audio_require_clip_loaded( clip
);
879 player
->info
.source
= clip
;
880 audio_player_commit( player
);
884 static void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
886 audio_require_lock();
887 audio_require_init( player
);
891 static void audio_play_oneshot( audio_clip
*clip
, float volume
)
893 audio_require_lock();
894 audio_require_clip_loaded( clip
);
896 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
898 vg_warn( "Audio commit queue full\n" );
902 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
904 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
905 ent
->info
.pan
= 0.0f
;
906 ent
->info
.source
= clip
;
907 ent
->info
.vol
= volume
;
911 static void audio_player_init( audio_player
*player
)
913 player
->active_entity
= AATREE_PTR_NIL
;
922 * Safety enforced Get/set attributes
925 static int audio_player_is_playing( audio_player
*sys
)
927 audio_require_lock();
929 if( sys
->active_entity
!= AATREE_PTR_NIL
)
935 static void audio_player_set_position( audio_player
*sys
, v3f pos
)
937 audio_require_lock();
938 v3_copy( pos
, sys
->info
.world_position
);
941 static void audio_player_set_vol( audio_player
*sys
, float vol
)
943 audio_require_lock();
947 static float audio_player_get_vol( audio_player
*sys
)
949 audio_require_lock();
950 return sys
->info
.vol
;
953 static void audio_player_set_pan( audio_player
*sys
, float pan
)
955 audio_require_lock();
959 static float audio_player_get_pan( audio_player
*sys
)
961 audio_require_lock();
962 return sys
->info
.pan
;
965 static void audio_player_set_flags( audio_player
*sys
, u32 flags
)
967 audio_require_lock();
968 sys
->info
.flags
= flags
;
971 static u32
audio_player_get_flags( audio_player
*sys
)
973 audio_require_lock();
974 return sys
->info
.flags
;
982 static void audio_debug_ui( m4x4f mtx_pv
)
984 if( !vg_audio
.debug_ui
)
991 u32 cursor
, flags
, length
;
995 infos
[ SFX_MAX_SYSTEMS
];
1000 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
1002 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1007 audio_entity
*ent
= &aap
->ent
;
1008 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1010 snd
->name
= ent
->name
;
1011 snd
->cursor
= ent
->cur
;
1012 snd
->flags
= ent
->info
.flags
;
1013 snd
->length
= ent
->length
;
1014 snd
->vol
= ent
->info
.vol
*100.0f
;
1015 v3_copy( ent
->info
.world_position
, snd
->pos
);
1018 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1019 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1020 &vg_prof_audio_mix
}, 2,
1021 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1029 ui_global_ctx
.cursor
[0] = 258;
1030 ui_global_ctx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1031 ui_global_ctx
.cursor
[2] = 150;
1032 ui_global_ctx
.cursor
[3] = 12;
1034 float usage
= (float)vg_audio
.mem_current
/ (1024.0f
*1024.0f
),
1035 total
= (float)vg_audio
.mem_total
/ (1024.0f
*1024.0f
),
1036 percent
= (usage
/total
) * 100.0f
;
1037 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1039 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1040 ui_global_ctx
.cursor
[1] += 20;
1042 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1043 u32 overlap_length
= 0;
1045 if( !vg_audio
.debug_ui_3d
)
1048 /* Draw audio stack */
1049 for( int i
=0; i
<num_systems
; i
++ )
1051 struct sound_info
*inf
= &infos
[i
];
1053 ui_global_ctx
.cursor
[2] = 200;
1054 ui_global_ctx
.cursor
[3] = 18;
1056 u32 alpha
= 0xa0000000;
1058 ui_new_node( &ui_global_ctx
);
1060 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x00333333|alpha
);
1062 ui_px baseline
= ui_global_ctx
.cursor
[0],
1064 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1067 ui_global_ctx
.cursor
[2] = 2;
1068 ui_global_ctx
.cursor
[0] = c
;
1069 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0xffffffff );
1071 ui_global_ctx
.cursor
[0] = baseline
+ 2;
1072 ui_global_ctx
.cursor
[1] += 2;
1073 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1074 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1076 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1079 v3_copy( inf
->pos
, wpos
);
1081 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1083 if( wpos
[3] < 0.0f
)
1084 goto projected_behind
;
1086 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1087 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1090 wr
[0] = wpos
[0] * vg
.window_x
;
1091 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1095 for( int j
=0; j
<12; j
++ )
1098 for( int k
=0; k
<overlap_length
; k
++ )
1100 ui_px
*wk
= overlap_buffer
[k
];
1101 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1102 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1115 ui_text( &ui_global_ctx
, wr
, perf
, 1, 0 );
1117 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1123 ui_end_down( &ui_global_ctx
);
1124 ui_global_ctx
.cursor
[1] += 1;
1128 #endif /* VG_AUDIO_H */