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
52 #define FADEOUT_LENGTH 1100
53 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
55 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
57 enum audio_source_mode
60 k_audio_source_compressed
,
63 typedef struct audio_clip audio_clip
;
67 enum audio_source_mode source_mode
;
71 u32 len
; /* decompressed: sample count,
72 compressed: file size */
75 typedef struct audio_mix_info audio_mix_info
;
85 typedef struct audio_player audio_player
;
88 aatree_ptr active_entity
; /* non-nil if currently playing */
96 typedef struct audio_entity audio_entity
;
105 u32 fadeout
, fadeout_current
;
112 * play again: if already playing, leave in queue while it fadeouts
113 * oneshot: create a ghost entity
117 static struct vg_audio_system
119 ma_device miniaudio_device
;
120 ma_device_config miniaudio_dconfig
;
122 void *mem
, *decode_mem
;
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
,
169 static void *audio_alloc( u32 size
)
171 u32 new_current
= vg_audio
.mem_current
+ size
;
172 if( new_current
> vg_audio
.mem_total
)
173 vg_fatal_exit_loop( "Audio pool ran out of memory" );
175 void *ptr
= vg_audio
.mem
+ vg_audio
.mem_current
;
176 vg_audio
.mem_current
= new_current
;
183 * These functions are called from the main thread and used to prevent bad
184 * access. TODO: They should be no-ops in release builds.
186 static int audio_lock_checker_load(void)
189 vg_mutex_lock( &vg_audio
.mux_checker
);
190 value
= vg_audio
.sync_locked
;
191 vg_mutex_unlock( &vg_audio
.mux_checker
);
195 static void audio_lock_checker_store( int value
)
197 vg_mutex_lock( &vg_audio
.mux_checker
);
198 vg_audio
.sync_locked
= value
;
199 vg_mutex_unlock( &vg_audio
.mux_checker
);
202 static void audio_require_lock(void)
204 if( audio_lock_checker_load() )
207 vg_error( "Modifying sound effects systems requires locking\n" );
211 static void audio_lock(void)
213 vg_mutex_lock( &vg_audio
.mux_sync
);
214 audio_lock_checker_store(1);
217 static void audio_unlock(void)
219 audio_lock_checker_store(0);
220 vg_mutex_unlock( &vg_audio
.mux_sync
);
224 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
225 const void *pInput
, ma_uint32 frameCount
);
227 static void vg_audio_init(void)
229 vg_mutex_init( &vg_audio
.mux_checker
);
230 vg_mutex_init( &vg_audio
.mux_sync
);
232 vg_convar_push( (struct vg_convar
){
233 .name
= "debug_audio",
234 .data
= &vg_audio
.debug_ui
,
235 .data_type
= k_convar_dtype_i32
,
236 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
240 vg_convar_push( (struct vg_convar
){
241 .name
= "debug_audio_clips",
242 .data
= &vg_audio
.debug_ui_3d
,
243 .data_type
= k_convar_dtype_i32
,
244 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
248 u32 decode_region
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
249 vg_audio
.mem_total
= 1024*1024*32;
250 vg_audio
.mem_current
= 0;
251 vg_audio
.mem
= vg_alloc( vg_audio
.mem_total
+ decode_region
);
252 vg_audio
.decode_mem
= &((u8
*)vg_audio
.mem
)[vg_audio
.mem_total
];
255 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
256 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
258 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
259 vg_audio
.active_pool_info
.p_cmp
= NULL
;
260 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
262 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
263 ma_device
*device
= &vg_audio
.miniaudio_device
;
265 *dconf
= ma_device_config_init( ma_device_type_playback
);
266 dconf
->playback
.format
= ma_format_f32
;
267 dconf
->playback
.channels
= 2;
268 dconf
->sampleRate
= 44100;
269 dconf
->dataCallback
= audio_mixer_callback
;
270 dconf
->periodSizeInFrames
= 441;
272 dconf
->pUserData
= NULL
;
274 vg_info( "Starting audio engine\n" );
276 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
278 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
282 if( ma_device_start( device
) != MA_SUCCESS
)
284 ma_device_uninit( device
);
285 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
289 vg_success( "Ready\n" );
292 static void vg_audio_free(void * nothing
)
294 ma_device
*device
= &vg_audio
.miniaudio_device
;
295 ma_device_uninit( device
);
297 vg_free( vg_audio
.mem
);
305 static aatree_ptr
audio_alloc_entity_internal(void)
307 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
308 &vg_audio
.active_pool_head
);
310 if( playerid
== AATREE_PTR_NIL
)
311 return AATREE_PTR_NIL
;
313 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
319 static void audio_entity_free_internal( aatree_ptr id
)
321 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
324 /* Notify player that we've finished */
325 if( aap
->ent
.player
)
326 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
329 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
330 &vg_audio
.active_pool_head
);
333 static void *audio_entity_vorbis_ptr( aatree_ptr entid
)
335 u8
*buf
= (u8
*)vg_audio
.decode_mem
,
336 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
341 static void audio_entity_start( audio_entity
*src
)
343 aatree_ptr entid
= audio_alloc_entity_internal();
344 if( entid
== AATREE_PTR_NIL
)
347 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
349 ent
->info
= src
->info
;
350 ent
->name
= src
->info
.source
->path
;
352 ent
->player
= src
->player
;
355 ent
->fadeout_current
= 0;
357 /* Notify main player we are dequeud and playing */
360 src
->player
->enqued
= 0;
361 src
->player
->active_entity
= entid
;
364 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
366 /* Setup vorbis decoder */
367 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
369 stb_vorbis_alloc alloc
= {
370 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
371 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
375 stb_vorbis
*decoder
= stb_vorbis_open_memory(
376 src
->info
.source
->data
, src
->info
.source
->len
, &err
, &alloc
);
380 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
381 src
->info
.source
->path
, err
);
383 audio_entity_free_internal( entid
);
388 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
391 aap
->vorbis_handle
= decoder
;
395 ent
->length
= src
->info
.source
->len
;
400 * Read everything from the queue
402 static void audio_system_enque(void)
404 /* Process incoming sound queue */
408 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
410 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
415 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
417 audio_entity_start( src
);
421 /* Otherwise try start fadeout but dont remove from queue */
423 aatree_ptr entid
= src
->player
->active_entity
;
424 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
427 ent
->fadeout
= FADEOUT_LENGTH
;
428 ent
->fadeout_current
= FADEOUT_LENGTH
;
431 vg_audio
.entity_queue
[ wr
++ ] = *src
;
436 audio_entity_start( src
);
440 vg_audio
.queue_len
= wr
;
442 /* Localize others memory */
443 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
445 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
449 if( aap
->ent
.player
)
451 /* Only copy information in whilst not requeing */
452 if( aap
->ent
.player
->enqued
== 0 )
454 aap
->ent
.info
= aap
->ent
.player
->info
;
463 * Redistribute sound systems
465 static void audio_system_cleanup(void)
469 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
471 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
474 audio_entity
*src
= &aap
->ent
;
475 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
481 audio_entity_free_internal( i
);
490 * Get effective volume and pan from this entity
492 static void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
494 if( ent
->info
.vol
< 0.01f
)
496 *vol
= ent
->info
.vol
;
502 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
504 float dist2
= v3_length2( delta
);
506 if( dist2
< 0.0001f
)
513 float dist
= sqrtf( dist2
),
514 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
516 v3_muls( delta
, 1.0f
/dist
, delta
);
517 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
518 *vol
= 1.0f
/(attn
*attn
);
522 static void audio_decode_uncompressed_mono( float *src
, u32 count
, float *dst
)
524 for( u32 i
=0; i
<count
; i
++ )
526 dst
[ i
*2 + 0 ] = src
[i
];
527 dst
[ i
*2 + 1 ] = src
[i
];
532 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
535 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
539 c
= VG_MIN( 1, f
->channels
- 1 );
543 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
548 for( int j
=0; j
< k
; ++j
)
550 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
551 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
555 f
->channel_buffer_start
+= k
;
560 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
567 static void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
569 vg_profile_begin( &_vg_prof_audio_decode
);
571 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
572 audio_entity
*ent
= &aap
->ent
;
574 u32 remaining
= count
;
575 u32 cursor
= ent
->cur
;
580 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
581 remaining
-= samples_this_run
;
583 float *dst
= &buf
[ buffer_pos
* 2 ];
585 int source_mode
= ent
->info
.source
->source_mode
;
587 if( source_mode
== k_audio_source_mono
)
589 float *src
= &((float *)ent
->info
.source
->data
)[ cursor
];
590 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
592 else if( source_mode
== k_audio_source_compressed
)
594 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
599 if( read_samples
!= samples_this_run
)
601 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
605 cursor
+= samples_this_run
;
606 buffer_pos
+= samples_this_run
;
608 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
610 if( source_mode
== k_audio_source_compressed
)
612 stb_vorbis_seek_start( aap
->vorbis_handle
);
624 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
625 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
632 vg_profile_end( &_vg_prof_audio_decode
);
635 static void audio_entity_mix( aatree_ptr id
, float *buffer
,
638 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
640 u32 cursor
= ent
->cur
, buffer_pos
= 0;
641 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
643 u32 frames_write
= frame_count
;
644 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
646 float vol
= ent
->info
.vol
,
649 audio_entity_get_samples( id
, frame_count
, pcf
);
651 vg_profile_begin( &_vg_prof_audio_mix
);
653 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
654 audio_entity_spacialize( ent
, &vol
, &pan
);
656 for( u32 j
=0; j
<frame_count
; j
++ )
658 float frame_vol
= vol
;
662 /* Force this system to be removed now */
663 if( ent
->fadeout_current
== 0 )
665 ent
->info
.flags
= 0x00;
666 ent
->cur
= ent
->length
;
670 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
671 ent
->fadeout_current
--;
677 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
678 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
683 vg_profile_end( &_vg_prof_audio_mix
);
687 * callback from miniaudio.h interface
689 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
690 const void *pInput
, ma_uint32 frame_count
)
692 struct timespec time_start
, time_end
;
693 clock_gettime( CLOCK_REALTIME
, &time_start
);
695 audio_system_enque();
698 float *pOut32F
= (float *)pOutBuf
;
699 for( int i
=0; i
<frame_count
*2; i
++ )
703 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
705 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
709 audio_entity_mix( i
, pOut32F
, frame_count
);
714 audio_system_cleanup();
716 /* TODO: what the hell is this? */
721 vg_profile_increment( &_vg_prof_audio_decode
);
722 vg_profile_increment( &_vg_prof_audio_mix
);
724 vg_prof_audio_mix
= _vg_prof_audio_mix
;
725 vg_prof_audio_decode
= _vg_prof_audio_decode
;
727 vg_audio
.samples_last
= frame_count
;
731 /* Decompress entire vorbis stream into buffer */
732 static float *audio_decompress_vorbis( const unsigned char *data
, int len
,
733 int channels
, u32
*samples
)
736 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
740 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
744 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
746 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
748 (float)length_samples
/ (44100.0f
*(float)channels
),
749 (float)(length_samples
*4*channels
) / 1024.0f
);
751 float *buffer
= audio_alloc( length_samples
* channels
* sizeof(float) );
754 stb_vorbis_close( pv
);
755 vg_error( "Failed to allocated memory for audio\n" );
759 int read_samples
= stb_vorbis_get_samples_float_interleaved(
760 pv
, channels
, buffer
, length_samples
* channels
);
762 if( read_samples
!= length_samples
)
764 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
765 length_samples
, read_samples
);
766 length_samples
= read_samples
;
769 stb_vorbis_close( pv
);
770 *samples
= length_samples
;
774 static int audio_clip_load( audio_clip
*clip
)
776 /* Load and decompress */
778 void *filedata
= vg_asset_read_s( clip
->path
, &file_len
);
782 vg_error( "OGG load failed (%s)\n", clip
->path
);
786 if( clip
->source_mode
== k_audio_source_mono
)
789 float *sound
= audio_decompress_vorbis( filedata
, file_len
, 1, &samples
);
793 float seconds
= (float)samples
/ 44100.0f
,
794 mb
= (float)(samples
*4) / (1024.0f
*1024.0f
);
796 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
797 clip
->path
, seconds
, mb
);
799 else if( clip
->source_mode
== k_audio_source_compressed
)
801 void *data
= audio_alloc( file_len
);
802 memcpy( data
, filedata
, file_len
);
805 clip
->len
= file_len
;
807 float mb
= (float)(file_len
) / (1024.0f
*1024.0f
);
808 vg_info( "Loaded audio clip[compressed] '%s' (%.1fmb)\n",
818 vg_error( "Unkown source mode (%u)\n", clip
->source_mode
);
825 static void audio_clip_loadn( audio_clip
*arr
, int count
)
827 for( int i
=0; i
<count
; i
++ )
828 audio_clip_load( &arr
[i
] );
831 /* Mark change to be uploaded through queue system */
832 static void audio_player_commit( audio_player
*sys
)
834 audio_require_lock();
836 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
838 vg_warn( "Audio commit queue full\n" );
844 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
849 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
850 ent
->info
= sys
->info
;
854 static void audio_require_init( audio_player
*player
)
859 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
862 static void audio_require_clip_loaded( audio_clip
*clip
)
867 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
870 /* Play a clip using player. If its already playing something, it will
871 * fadeout quickly and start the next sound */
872 static void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
874 audio_require_lock();
875 audio_require_init( player
);
876 audio_require_clip_loaded( clip
);
878 player
->info
.source
= clip
;
879 audio_player_commit( player
);
883 static void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
885 audio_require_lock();
886 audio_require_init( player
);
890 static void audio_play_oneshot( audio_clip
*clip
, float volume
)
892 audio_require_lock();
893 audio_require_clip_loaded( clip
);
895 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
897 vg_warn( "Audio commit queue full\n" );
901 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
903 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
904 ent
->info
.pan
= 0.0f
;
905 ent
->info
.source
= clip
;
906 ent
->info
.vol
= volume
;
910 static void audio_player_init( audio_player
*player
)
912 player
->active_entity
= AATREE_PTR_NIL
;
921 * Safety enforced Get/set attributes
924 static int audio_player_is_playing( audio_player
*sys
)
926 audio_require_lock();
928 if( sys
->active_entity
!= AATREE_PTR_NIL
)
934 static void audio_player_set_position( audio_player
*sys
, v3f pos
)
936 audio_require_lock();
937 v3_copy( pos
, sys
->info
.world_position
);
940 static void audio_player_set_vol( audio_player
*sys
, float vol
)
942 audio_require_lock();
946 static float audio_player_get_vol( audio_player
*sys
)
948 audio_require_lock();
949 return sys
->info
.vol
;
952 static void audio_player_set_pan( audio_player
*sys
, float pan
)
954 audio_require_lock();
958 static float audio_player_get_pan( audio_player
*sys
)
960 audio_require_lock();
961 return sys
->info
.pan
;
964 static void audio_player_set_flags( audio_player
*sys
, u32 flags
)
966 audio_require_lock();
967 sys
->info
.flags
= flags
;
970 static u32
audio_player_get_flags( audio_player
*sys
)
972 audio_require_lock();
973 return sys
->info
.flags
;
981 static void audio_debug_ui( m4x4f mtx_pv
)
983 if( !vg_audio
.debug_ui
)
990 u32 cursor
, flags
, length
;
994 infos
[ SFX_MAX_SYSTEMS
];
999 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
1001 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1006 audio_entity
*ent
= &aap
->ent
;
1007 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1009 snd
->name
= ent
->name
;
1010 snd
->cursor
= ent
->cur
;
1011 snd
->flags
= ent
->info
.flags
;
1012 snd
->length
= ent
->length
;
1013 snd
->vol
= ent
->info
.vol
*100.0f
;
1014 v3_copy( ent
->info
.world_position
, snd
->pos
);
1017 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1018 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1019 &vg_prof_audio_mix
}, 2,
1020 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1028 ui_global_ctx
.cursor
[0] = 258;
1029 ui_global_ctx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1030 ui_global_ctx
.cursor
[2] = 150;
1031 ui_global_ctx
.cursor
[3] = 12;
1033 float usage
= (float)vg_audio
.mem_current
/ (1024.0f
*1024.0f
),
1034 total
= (float)vg_audio
.mem_total
/ (1024.0f
*1024.0f
),
1035 percent
= (usage
/total
) * 100.0f
;
1036 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1038 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1039 ui_global_ctx
.cursor
[1] += 20;
1041 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1042 u32 overlap_length
= 0;
1044 if( !vg_audio
.debug_ui_3d
)
1047 /* Draw audio stack */
1048 for( int i
=0; i
<num_systems
; i
++ )
1050 struct sound_info
*inf
= &infos
[i
];
1052 ui_global_ctx
.cursor
[2] = 200;
1053 ui_global_ctx
.cursor
[3] = 18;
1055 u32 alpha
= 0xa0000000;
1057 ui_new_node( &ui_global_ctx
);
1059 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x00333333|alpha
);
1061 ui_px baseline
= ui_global_ctx
.cursor
[0],
1063 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1066 ui_global_ctx
.cursor
[2] = 2;
1067 ui_global_ctx
.cursor
[0] = c
;
1068 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0xffffffff );
1070 ui_global_ctx
.cursor
[0] = baseline
+ 2;
1071 ui_global_ctx
.cursor
[1] += 2;
1072 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1073 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1075 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1078 v3_copy( inf
->pos
, wpos
);
1080 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1082 if( wpos
[3] < 0.0f
)
1083 goto projected_behind
;
1085 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1086 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1089 wr
[0] = wpos
[0] * vg
.window_x
;
1090 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1094 for( int j
=0; j
<12; j
++ )
1097 for( int k
=0; k
<overlap_length
; k
++ )
1099 ui_px
*wk
= overlap_buffer
[k
];
1100 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1101 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1114 ui_text( &ui_global_ctx
, wr
, perf
, 1, 0 );
1116 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1122 ui_end_down( &ui_global_ctx
);
1123 ui_global_ctx
.cursor
[1] += 1;
1127 #endif /* VG_AUDIO_H */