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
];
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 u32 decode_region
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
241 vg_audio
.mem_total
= 1024*1024*32;
242 vg_audio
.mem_current
= 0;
243 vg_audio
.mem
= vg_alloc( vg_audio
.mem_total
+ decode_region
);
244 vg_audio
.decode_mem
= &((u8
*)vg_audio
.mem
)[vg_audio
.mem_total
];
247 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
248 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
250 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
251 vg_audio
.active_pool_info
.p_cmp
= NULL
;
252 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
254 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
255 ma_device
*device
= &vg_audio
.miniaudio_device
;
257 *dconf
= ma_device_config_init( ma_device_type_playback
);
258 dconf
->playback
.format
= ma_format_f32
;
259 dconf
->playback
.channels
= 2;
260 dconf
->sampleRate
= 44100;
261 dconf
->dataCallback
= audio_mixer_callback
;
262 dconf
->periodSizeInFrames
= 441;
264 dconf
->pUserData
= NULL
;
266 vg_info( "Starting audio engine\n" );
268 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
270 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
274 if( ma_device_start( device
) != MA_SUCCESS
)
276 ma_device_uninit( device
);
277 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
281 vg_success( "Ready\n" );
284 static void vg_audio_free(void * nothing
)
286 ma_device
*device
= &vg_audio
.miniaudio_device
;
287 ma_device_uninit( device
);
289 vg_free( vg_audio
.mem
);
297 static aatree_ptr
audio_alloc_entity_internal(void)
299 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
300 &vg_audio
.active_pool_head
);
302 if( playerid
== AATREE_PTR_NIL
)
303 return AATREE_PTR_NIL
;
305 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
311 static void audio_entity_free_internal( aatree_ptr id
)
313 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
316 /* Notify player that we've finished */
317 if( aap
->ent
.player
)
318 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
321 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
322 &vg_audio
.active_pool_head
);
325 static void *audio_entity_vorbis_ptr( aatree_ptr entid
)
327 u8
*buf
= (u8
*)vg_audio
.decode_mem
,
328 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
333 static void audio_entity_start( audio_entity
*src
)
335 aatree_ptr entid
= audio_alloc_entity_internal();
336 if( entid
== AATREE_PTR_NIL
)
339 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
341 ent
->info
= src
->info
;
342 ent
->name
= src
->info
.source
->path
;
344 ent
->player
= src
->player
;
347 ent
->fadeout_current
= 0;
349 /* Notify main player we are dequeud and playing */
352 src
->player
->enqued
= 0;
353 src
->player
->active_entity
= entid
;
356 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
358 /* Setup vorbis decoder */
359 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
361 stb_vorbis_alloc alloc
= {
362 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
363 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
367 stb_vorbis
*decoder
= stb_vorbis_open_memory(
368 src
->info
.source
->data
, src
->info
.source
->len
, &err
, &alloc
);
372 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
373 src
->info
.source
->path
, err
);
375 audio_entity_free_internal( entid
);
380 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
383 aap
->vorbis_handle
= decoder
;
387 ent
->length
= src
->info
.source
->len
;
392 * Read everything from the queue
394 static void audio_system_enque(void)
396 /* Process incoming sound queue */
400 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
402 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
407 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
409 audio_entity_start( src
);
413 /* Otherwise try start fadeout but dont remove from queue */
415 aatree_ptr entid
= src
->player
->active_entity
;
416 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
419 ent
->fadeout
= FADEOUT_LENGTH
;
420 ent
->fadeout_current
= FADEOUT_LENGTH
;
423 vg_audio
.entity_queue
[ wr
++ ] = *src
;
428 audio_entity_start( src
);
432 vg_audio
.queue_len
= wr
;
434 /* Localize others memory */
435 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
437 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
441 if( aap
->ent
.player
)
443 /* Only copy information in whilst not requeing */
444 if( aap
->ent
.player
->enqued
== 0 )
446 aap
->ent
.info
= aap
->ent
.player
->info
;
455 * Redistribute sound systems
457 static void audio_system_cleanup(void)
461 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
463 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
466 audio_entity
*src
= &aap
->ent
;
467 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
473 audio_entity_free_internal( i
);
482 * Get effective volume and pan from this entity
484 static void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
486 if( ent
->info
.vol
< 0.01f
)
488 *vol
= ent
->info
.vol
;
494 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
496 float dist2
= v3_length2( delta
);
498 if( dist2
< 0.0001f
)
505 float dist
= sqrtf( dist2
),
506 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
508 v3_muls( delta
, 1.0f
/dist
, delta
);
509 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
510 *vol
= 1.0f
/(attn
*attn
);
514 static void audio_decode_uncompressed_mono( float *src
, u32 count
, float *dst
)
516 for( u32 i
=0; i
<count
; i
++ )
518 dst
[ i
*2 + 0 ] = src
[i
];
519 dst
[ i
*2 + 1 ] = src
[i
];
524 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
527 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
531 c
= VG_MIN( 1, f
->channels
- 1 );
535 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
540 for( int j
=0; j
< k
; ++j
)
542 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
543 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
547 f
->channel_buffer_start
+= k
;
552 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
559 static void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
561 vg_profile_begin( &_vg_prof_audio_decode
);
563 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
564 audio_entity
*ent
= &aap
->ent
;
566 u32 remaining
= count
;
567 u32 cursor
= ent
->cur
;
572 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
573 remaining
-= samples_this_run
;
575 float *dst
= &buf
[ buffer_pos
* 2 ];
577 int source_mode
= ent
->info
.source
->source_mode
;
579 if( source_mode
== k_audio_source_mono
)
581 float *src
= &((float *)ent
->info
.source
->data
)[ cursor
];
582 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
584 else if( source_mode
== k_audio_source_compressed
)
586 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
591 if( read_samples
!= samples_this_run
)
593 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
597 cursor
+= samples_this_run
;
598 buffer_pos
+= samples_this_run
;
600 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
602 if( source_mode
== k_audio_source_compressed
)
604 stb_vorbis_seek_start( aap
->vorbis_handle
);
616 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
617 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
624 vg_profile_end( &_vg_prof_audio_decode
);
627 static void audio_entity_mix( aatree_ptr id
, float *buffer
,
630 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
632 u32 cursor
= ent
->cur
, buffer_pos
= 0;
633 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
635 u32 frames_write
= frame_count
;
636 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
638 float vol
= ent
->info
.vol
,
641 audio_entity_get_samples( id
, frame_count
, pcf
);
643 vg_profile_begin( &_vg_prof_audio_mix
);
645 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
646 audio_entity_spacialize( ent
, &vol
, &pan
);
648 for( u32 j
=0; j
<frame_count
; j
++ )
650 float frame_vol
= vol
;
654 /* Force this system to be removed now */
655 if( ent
->fadeout_current
== 0 )
657 ent
->info
.flags
= 0x00;
658 ent
->cur
= ent
->length
;
662 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
663 ent
->fadeout_current
--;
669 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
670 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
675 vg_profile_end( &_vg_prof_audio_mix
);
679 * callback from miniaudio.h interface
681 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
682 const void *pInput
, ma_uint32 frame_count
)
684 struct timespec time_start
, time_end
;
685 clock_gettime( CLOCK_REALTIME
, &time_start
);
687 audio_system_enque();
690 float *pOut32F
= (float *)pOutBuf
;
691 for( int i
=0; i
<frame_count
*2; i
++ )
695 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
697 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
701 audio_entity_mix( i
, pOut32F
, frame_count
);
706 audio_system_cleanup();
708 /* TODO: what the hell is this? */
713 vg_profile_increment( &_vg_prof_audio_decode
);
714 vg_profile_increment( &_vg_prof_audio_mix
);
716 vg_prof_audio_mix
= _vg_prof_audio_mix
;
717 vg_prof_audio_decode
= _vg_prof_audio_decode
;
719 vg_audio
.samples_last
= frame_count
;
723 /* Decompress entire vorbis stream into buffer */
724 static float *audio_decompress_vorbis( const unsigned char *data
, int len
,
725 int channels
, u32
*samples
)
728 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
732 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
736 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
738 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
740 (float)length_samples
/ (44100.0f
*(float)channels
),
741 (float)(length_samples
*4*channels
) / 1024.0f
);
743 float *buffer
= audio_alloc( length_samples
* channels
* sizeof(float) );
746 stb_vorbis_close( pv
);
747 vg_error( "Failed to allocated memory for audio\n" );
751 int read_samples
= stb_vorbis_get_samples_float_interleaved(
752 pv
, channels
, buffer
, length_samples
* channels
);
754 if( read_samples
!= length_samples
)
756 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
757 length_samples
, read_samples
);
758 length_samples
= read_samples
;
761 stb_vorbis_close( pv
);
762 *samples
= length_samples
;
766 static int audio_clip_load( audio_clip
*clip
)
768 /* Load and decompress */
770 void *filedata
= vg_asset_read_s( clip
->path
, &file_len
);
774 vg_error( "OGG load failed (%s)\n", clip
->path
);
778 if( clip
->source_mode
== k_audio_source_mono
)
781 float *sound
= audio_decompress_vorbis( filedata
, file_len
, 1, &samples
);
785 float seconds
= (float)samples
/ 44100.0f
,
786 mb
= (float)(samples
*4) / (1024.0f
*1024.0f
);
788 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
789 clip
->path
, seconds
, mb
);
791 else if( clip
->source_mode
== k_audio_source_compressed
)
793 void *data
= audio_alloc( file_len
);
794 memcpy( data
, filedata
, file_len
);
797 clip
->len
= file_len
;
799 float mb
= (float)(file_len
) / (1024.0f
*1024.0f
);
800 vg_info( "Loaded audio clip[compressed] '%s' (%.1fmb)\n",
810 vg_error( "Unkown source mode (%u)\n", clip
->source_mode
);
817 static void audio_clip_loadn( audio_clip
*arr
, int count
)
819 for( int i
=0; i
<count
; i
++ )
820 audio_clip_load( &arr
[i
] );
823 /* Mark change to be uploaded through queue system */
824 static void audio_player_commit( audio_player
*sys
)
826 audio_require_lock();
828 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
830 vg_warn( "Audio commit queue full\n" );
836 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
841 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
842 ent
->info
= sys
->info
;
846 static void audio_require_init( audio_player
*player
)
851 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
854 static void audio_require_clip_loaded( audio_clip
*clip
)
859 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
862 /* Play a clip using player. If its already playing something, it will
863 * fadeout quickly and start the next sound */
864 static void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
866 audio_require_lock();
867 audio_require_init( player
);
868 audio_require_clip_loaded( clip
);
870 player
->info
.source
= clip
;
871 audio_player_commit( player
);
875 static void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
877 audio_require_lock();
878 audio_require_init( player
);
882 static void audio_play_oneshot( audio_clip
*clip
, float volume
)
884 audio_require_lock();
885 audio_require_clip_loaded( clip
);
887 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
889 vg_warn( "Audio commit queue full\n" );
893 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
895 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
896 ent
->info
.pan
= 0.0f
;
897 ent
->info
.source
= clip
;
898 ent
->info
.vol
= volume
;
902 static void audio_player_init( audio_player
*player
)
904 player
->active_entity
= AATREE_PTR_NIL
;
913 * Safety enforced Get/set attributes
916 static int audio_player_is_playing( audio_player
*sys
)
918 audio_require_lock();
920 if( sys
->active_entity
!= AATREE_PTR_NIL
)
926 static void audio_player_set_position( audio_player
*sys
, v3f pos
)
928 audio_require_lock();
929 v3_copy( pos
, sys
->info
.world_position
);
932 static void audio_player_set_vol( audio_player
*sys
, float vol
)
934 audio_require_lock();
938 static float audio_player_get_vol( audio_player
*sys
)
940 audio_require_lock();
941 return sys
->info
.vol
;
944 static void audio_player_set_pan( audio_player
*sys
, float pan
)
946 audio_require_lock();
950 static float audio_player_get_pan( audio_player
*sys
)
952 audio_require_lock();
953 return sys
->info
.pan
;
956 static void audio_player_set_flags( audio_player
*sys
, u32 flags
)
958 audio_require_lock();
959 sys
->info
.flags
= flags
;
962 static u32
audio_player_get_flags( audio_player
*sys
)
964 audio_require_lock();
965 return sys
->info
.flags
;
973 static void audio_debug_ui( m4x4f mtx_pv
)
975 if( !vg_audio
.debug_ui
)
982 u32 cursor
, flags
, length
;
986 infos
[ SFX_MAX_SYSTEMS
];
991 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
993 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
998 audio_entity
*ent
= &aap
->ent
;
999 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1001 snd
->name
= ent
->name
;
1002 snd
->cursor
= ent
->cur
;
1003 snd
->flags
= ent
->info
.flags
;
1004 snd
->length
= ent
->length
;
1005 snd
->vol
= ent
->info
.vol
*100.0f
;
1006 v3_copy( ent
->info
.world_position
, snd
->pos
);
1009 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1010 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1011 &vg_prof_audio_mix
}, 2,
1012 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1020 ui_global_ctx
.cursor
[0] = 258;
1021 ui_global_ctx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1022 ui_global_ctx
.cursor
[2] = 150;
1023 ui_global_ctx
.cursor
[3] = 12;
1025 float usage
= (float)vg_audio
.mem_current
/ (1024.0f
*1024.0f
),
1026 total
= (float)vg_audio
.mem_total
/ (1024.0f
*1024.0f
),
1027 percent
= (usage
/total
) * 100.0f
;
1028 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1030 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1031 ui_global_ctx
.cursor
[1] += 20;
1033 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1034 u32 overlap_length
= 0;
1036 /* Draw audio stack */
1037 for( int i
=0; i
<num_systems
; i
++ )
1039 struct sound_info
*inf
= &infos
[i
];
1041 ui_global_ctx
.cursor
[2] = 200;
1042 ui_global_ctx
.cursor
[3] = 18;
1044 u32 alpha
= 0xa0000000;
1046 ui_new_node( &ui_global_ctx
);
1048 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x00333333|alpha
);
1050 ui_px baseline
= ui_global_ctx
.cursor
[0],
1052 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1055 ui_global_ctx
.cursor
[2] = 2;
1056 ui_global_ctx
.cursor
[0] = c
;
1057 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0xffffffff );
1059 ui_global_ctx
.cursor
[0] = baseline
+ 2;
1060 ui_global_ctx
.cursor
[1] += 2;
1061 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1062 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1064 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1067 v3_copy( inf
->pos
, wpos
);
1069 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1071 if( wpos
[3] < 0.0f
)
1072 goto projected_behind
;
1074 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1075 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1078 wr
[0] = wpos
[0] * vg
.window_x
;
1079 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1083 for( int j
=0; j
<12; j
++ )
1086 for( int k
=0; k
<overlap_length
; k
++ )
1088 ui_px
*wk
= overlap_buffer
[k
];
1089 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1090 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1103 ui_text( &ui_global_ctx
, wr
, perf
, 1, 0 );
1105 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1111 ui_end_down( &ui_global_ctx
);
1112 ui_global_ctx
.cursor
[1] += 1;
1116 #endif /* VG_AUDIO_H */