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 1100
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_compressed
,
40 typedef struct audio_clip audio_clip
;
44 enum audio_source_mode source_mode
;
48 u32 len
; /* decompressed: sample count,
49 compressed: file size */
52 typedef struct audio_mix_info audio_mix_info
;
62 typedef struct audio_player audio_player
;
65 aatree_ptr active_entity
; /* non-nil if currently playing */
73 typedef struct audio_entity audio_entity
;
82 u32 fadeout
, fadeout_current
;
89 * play again: if already playing, leave in queue while it fadeouts
90 * oneshot: create a ghost entity
94 static struct vg_audio_system
96 ma_device miniaudio_device
;
97 ma_device_config miniaudio_dconfig
;
99 void *mem
, *decode_mem
;
105 MUTEX_TYPE mutex_checker
;
106 MUTEX_TYPE mutex_sync
;
108 /* Audio engine, thread 1 */
109 struct active_audio_player
115 aatree_pool_node pool_node
;
118 stb_vorbis
*vorbis_handle
;
119 stb_vorbis_alloc vorbis_alloc
;
121 active_players
[ SFX_MAX_SYSTEMS
];
123 aatree active_pool_info
; /* note: just using the pool */
124 aatree_ptr active_pool_head
;
126 /* System queue, and access from thread 0 */
127 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
130 char performance_info
[128],
131 performance_sub0
[64],
132 performance_sub1
[64];
139 double perf_ms_decode
,
142 u32 perf_measurements
;
146 static void *audio_alloc( u32 size
)
148 u32 new_current
= vg_audio
.mem_current
+ size
;
149 if( new_current
> vg_audio
.mem_total
)
151 vg_error( "audio pool over budget!\n" );
152 free( vg_audio
.mem
);
156 void *ptr
= vg_audio
.mem
+ vg_audio
.mem_current
;
157 vg_audio
.mem_current
= new_current
;
164 * These functions are called from the main thread and used to prevent bad
165 * access. TODO: They should be no-ops in release builds.
167 static int audio_lock_checker_load(void)
170 MUTEX_LOCK( vg_audio
.mutex_checker
);
171 value
= vg_audio
.sync_locked
;
172 MUTEX_UNLOCK( vg_audio
.mutex_checker
);
176 static void audio_lock_checker_store( int value
)
178 MUTEX_LOCK( vg_audio
.mutex_checker
);
179 vg_audio
.sync_locked
= value
;
180 MUTEX_UNLOCK( vg_audio
.mutex_checker
);
183 static void audio_require_lock(void)
185 if( audio_lock_checker_load() )
188 vg_exiterr( "Modifying sound effects systems requires locking\n" );
191 static void audio_lock(void)
193 MUTEX_LOCK( vg_audio
.mutex_sync
);
194 audio_lock_checker_store(1);
197 static void audio_unlock(void)
199 audio_lock_checker_store(0);
200 MUTEX_UNLOCK( vg_audio
.mutex_sync
);
204 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
205 const void *pInput
, ma_uint32 frameCount
);
207 static int vg_audio_init(void)
209 vg_convar_push( (struct vg_convar
){
210 .name
= "debug_audio",
211 .data
= &vg_audio
.debug_ui
,
212 .data_type
= k_convar_dtype_i32
,
213 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
217 u32 decode_region
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
218 vg_audio
.mem_total
= 1024*1024*32;
219 vg_audio
.mem_current
= 0;
220 vg_audio
.mem
= malloc( vg_audio
.mem_total
+ decode_region
);
221 vg_audio
.decode_mem
= &((u8
*)vg_audio
.mem
)[vg_audio
.mem_total
];
224 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
225 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
227 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
228 vg_audio
.active_pool_info
.p_cmp
= NULL
;
229 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
231 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
232 ma_device
*device
= &vg_audio
.miniaudio_device
;
234 *dconf
= ma_device_config_init( ma_device_type_playback
);
235 dconf
->playback
.format
= ma_format_f32
;
236 dconf
->playback
.channels
= 2;
237 dconf
->sampleRate
= 44100;
238 dconf
->dataCallback
= audio_mixer_callback
;
239 dconf
->periodSizeInFrames
= 441;
241 dconf
->pUserData
= NULL
;
243 vg_info( "Starting audio engine\n" );
245 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
247 vg_error( "ma_device failed to initialize" );
252 if( ma_device_start( device
) != MA_SUCCESS
)
254 ma_device_uninit( device
);
255 vg_error( "ma_device failed to start" );
263 static void vg_audio_free(void)
265 ma_device
*device
= &vg_audio
.miniaudio_device
;
266 ma_device_uninit( device
);
268 free( vg_audio
.mem
);
275 static aatree_ptr
audio_alloc_entity_internal(void)
277 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
278 &vg_audio
.active_pool_head
);
280 if( playerid
== AATREE_PTR_NIL
)
281 return AATREE_PTR_NIL
;
283 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
289 static void audio_entity_free_internal( aatree_ptr id
)
291 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
294 /* Notify player that we've finished */
295 if( aap
->ent
.player
)
296 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
299 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
300 &vg_audio
.active_pool_head
);
303 static void *audio_entity_vorbis_ptr( aatree_ptr entid
)
305 u8
*buf
= (u8
*)vg_audio
.decode_mem
,
306 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
311 static void audio_entity_start( audio_entity
*src
)
313 aatree_ptr entid
= audio_alloc_entity_internal();
314 if( entid
== AATREE_PTR_NIL
)
317 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
319 ent
->info
= src
->info
;
320 ent
->name
= src
->info
.source
->path
;
322 ent
->player
= src
->player
;
325 ent
->fadeout_current
= 0;
327 /* Notify main player we are dequeud and playing */
330 src
->player
->enqued
= 0;
331 src
->player
->active_entity
= entid
;
334 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
336 /* Setup vorbis decoder */
337 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
339 stb_vorbis_alloc alloc
= {
340 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
341 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
345 stb_vorbis
*decoder
= stb_vorbis_open_memory(
346 src
->info
.source
->data
, src
->info
.source
->len
, &err
, &alloc
);
350 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
351 src
->info
.source
->path
, err
);
353 audio_entity_free_internal( entid
);
358 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
361 aap
->vorbis_handle
= decoder
;
365 ent
->length
= src
->info
.source
->len
;
370 * Read everything from the queue
372 static void audio_system_enque(void)
374 /* Process incoming sound queue */
378 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
380 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
385 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
387 audio_entity_start( src
);
391 /* Otherwise try start fadeout but dont remove from queue */
393 aatree_ptr entid
= src
->player
->active_entity
;
394 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
397 ent
->fadeout
= FADEOUT_LENGTH
;
398 ent
->fadeout_current
= FADEOUT_LENGTH
;
401 vg_audio
.entity_queue
[ wr
++ ] = *src
;
406 audio_entity_start( src
);
410 vg_audio
.queue_len
= wr
;
412 /* Localize others memory */
413 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
415 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
419 if( aap
->ent
.player
)
421 /* Only copy information in whilst not requeing */
422 if( aap
->ent
.player
->enqued
== 0 )
424 aap
->ent
.info
= aap
->ent
.player
->info
;
433 * Redistribute sound systems
435 static void audio_system_cleanup(void)
439 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
441 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
444 audio_entity
*src
= &aap
->ent
;
445 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
451 audio_entity_free_internal( i
);
460 * Get effective volume and pan from this entity
462 static void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
465 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
467 float dist
= v3_length( delta
),
468 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
470 v3_muls( delta
, 1.0f
/dist
, delta
);
472 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
473 *vol
= 1.0f
/(attn
*attn
);
476 static void audio_decode_uncompressed_mono( float *src
, u32 count
, float *dst
)
478 for( u32 i
=0; i
<count
; i
++ )
480 dst
[ i
*2 + 0 ] = src
[i
];
481 dst
[ i
*2 + 1 ] = src
[i
];
486 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
489 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
493 c
= VG_MIN( 1, f
->channels
- 1 );
497 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
502 for( int j
=0; j
< k
; ++j
)
504 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
505 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
509 f
->channel_buffer_start
+= k
;
514 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
521 static void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
523 struct timespec time_start
, time_end
;
524 clock_gettime( CLOCK_REALTIME
, &time_start
);
526 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
527 audio_entity
*ent
= &aap
->ent
;
529 u32 remaining
= count
;
530 u32 cursor
= ent
->cur
;
535 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
536 remaining
-= samples_this_run
;
538 float *dst
= &buf
[ buffer_pos
* 2 ];
540 int source_mode
= ent
->info
.source
->source_mode
;
542 if( source_mode
== k_audio_source_mono
)
544 float *src
= &((float *)ent
->info
.source
->data
)[ cursor
];
545 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
547 else if( source_mode
== k_audio_source_compressed
)
549 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
554 if( read_samples
!= samples_this_run
)
556 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
560 cursor
+= samples_this_run
;
561 buffer_pos
+= samples_this_run
;
563 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
565 if( source_mode
== k_audio_source_compressed
)
567 stb_vorbis_seek_start( aap
->vorbis_handle
);
579 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
580 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
588 clock_gettime( CLOCK_REALTIME
, &time_end
);
589 vg_audio
.perf_ms_decode
+= vg_time_diff( time_start
, time_end
);
592 static void audio_entity_mix( aatree_ptr id
, float *buffer
,
595 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
597 u32 cursor
= ent
->cur
, buffer_pos
= 0;
598 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
600 u32 frames_write
= frame_count
;
601 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
603 float vol
= ent
->info
.vol
,
606 audio_entity_get_samples( id
, frame_count
, pcf
);
608 struct timespec time_start
, time_end
;
609 clock_gettime( CLOCK_REALTIME
, &time_start
);
611 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
612 audio_entity_spacialize( ent
, &vol
, &pan
);
614 for( u32 j
=0; j
<frame_count
; j
++ )
616 float frame_vol
= vol
;
620 /* Force this system to be removed now */
621 if( ent
->fadeout_current
== 0 )
623 ent
->info
.flags
= 0x00;
624 ent
->cur
= ent
->length
;
628 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
629 ent
->fadeout_current
--;
635 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
636 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
641 clock_gettime( CLOCK_REALTIME
, &time_end
);
642 vg_audio
.perf_ms_mix
+= vg_time_diff( time_start
, time_end
);
646 * callback from miniaudio.h interface
648 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
649 const void *pInput
, ma_uint32 frame_count
)
651 struct timespec time_start
, time_end
;
652 clock_gettime( CLOCK_REALTIME
, &time_start
);
654 audio_system_enque();
657 float *pOut32F
= (float *)pOutBuf
;
658 for( int i
=0; i
<frame_count
*2; i
++ )
662 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
664 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
667 audio_entity_mix( i
, pOut32F
, frame_count
);
675 audio_system_cleanup();
677 /* TODO: what the hell is this? */
683 clock_gettime( CLOCK_REALTIME
, &time_end
);
685 double elapsed
= vg_time_diff( time_start
, time_end
),
686 budget
= ((double)frame_count
/ 44100.0) * 1000.0,
687 percent
= (elapsed
/budget
) * 100.0;
689 snprintf( vg_audio
.performance_info
, 127,
690 "%.2fms/%.2fms (%.1f%%) (%u frames)",
691 elapsed
, budget
, percent
, frame_count
);
693 vg_audio
.perf_measurements
++;
694 if( vg_audio
.perf_measurements
>= 30 )
696 double ms_decode
= vg_audio
.perf_ms_decode
* (1.0/30.0),
697 ms_mix
= vg_audio
.perf_ms_mix
* (1.0/30.0);
699 snprintf( vg_audio
.performance_sub0
, 63, "Decode %.2fms", ms_decode
);
700 snprintf( vg_audio
.performance_sub1
, 63, "mix %.2fms", ms_mix
);
702 vg_audio
.perf_ms_decode
= 0.0;
703 vg_audio
.perf_ms_mix
= 0.0;
705 vg_audio
.perf_measurements
= 0;
709 /* Decompress entire vorbis stream into buffer */
710 static float *audio_decompress_vorbis( const unsigned char *data
, int len
,
711 int channels
, u32
*samples
)
714 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
718 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
722 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
724 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
726 (float)length_samples
/ (44100.0f
*(float)channels
),
727 (float)(length_samples
*4*channels
) / 1024.0f
);
729 float *buffer
= audio_alloc( length_samples
* channels
* sizeof(float) );
732 stb_vorbis_close( pv
);
736 int read_samples
= stb_vorbis_get_samples_float_interleaved(
737 pv
, channels
, buffer
, length_samples
* channels
);
739 if( read_samples
!= length_samples
)
741 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
742 length_samples
, read_samples
);
743 length_samples
= read_samples
;
746 stb_vorbis_close( pv
);
747 *samples
= length_samples
;
751 static int audio_clip_load( audio_clip
*clip
)
753 /* Load and decompress */
755 void *filedata
= vg_asset_read_s( clip
->path
, &file_len
);
759 vg_error( "OGG load failed (%s)\n", clip
->path
);
763 if( clip
->source_mode
== k_audio_source_mono
)
766 float *sound
= audio_decompress_vorbis( filedata
, file_len
, 1, &samples
);
770 float seconds
= (float)samples
/ 44100.0f
,
771 mb
= (float)(samples
*4) / (1024.0f
*1024.0f
);
773 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
774 clip
->path
, seconds
, mb
);
776 else if( clip
->source_mode
== k_audio_source_compressed
)
778 void *data
= audio_alloc( file_len
);
779 memcpy( data
, filedata
, file_len
);
782 clip
->len
= file_len
;
784 float mb
= (float)(file_len
) / (1024.0f
*1024.0f
);
785 vg_info( "Loaded audio clip[compressed] '%s' (%.1fmb)\n",
795 vg_error( "Unkown source mode (%u)\n", clip
->source_mode
);
802 static void audio_clip_loadn( audio_clip
*arr
, int count
)
804 for( int i
=0; i
<count
; i
++ )
805 audio_clip_load( &arr
[i
] );
808 /* Mark change to be uploaded through queue system */
809 static void audio_player_commit( audio_player
*sys
)
811 audio_require_lock();
813 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
815 vg_warn( "Audio commit queue full\n" );
821 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
826 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
827 ent
->info
= sys
->info
;
831 static void audio_require_init( audio_player
*player
)
837 snprintf( err
, 127, "Must init before playing! (%s)\n", player
->name
);
841 /* Play a clip using player. If its already playing something, it will
842 * fadeout quickly and start the next sound */
843 static void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
845 audio_require_lock();
846 audio_require_init( player
);
848 player
->info
.source
= clip
;
849 audio_player_commit( player
);
853 static void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
855 audio_require_lock();
856 audio_require_init( player
);
860 static void audio_play_oneshot( audio_clip
*clip
, float volume
)
862 audio_require_lock();
864 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
866 vg_warn( "Audio commit queue full\n" );
870 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
872 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
873 ent
->info
.pan
= 0.0f
;
874 ent
->info
.source
= clip
;
875 ent
->info
.vol
= volume
;
879 static void audio_player_init( audio_player
*player
)
881 player
->active_entity
= AATREE_PTR_NIL
;
890 * Safety enforced Get/set attributes
893 static void audio_player_set_position( audio_player
*sys
, v3f pos
)
895 audio_require_lock();
896 v3_copy( pos
, sys
->info
.world_position
);
899 static void audio_player_set_vol( audio_player
*sys
, float vol
)
901 audio_require_lock();
905 static float audio_player_get_vol( audio_player
*sys
)
907 audio_require_lock();
908 return sys
->info
.vol
;
911 static void audio_player_set_pan( audio_player
*sys
, float pan
)
913 audio_require_lock();
917 static float audio_player_get_pan( audio_player
*sys
)
919 audio_require_lock();
920 return sys
->info
.pan
;
923 static void audio_player_set_flags( audio_player
*sys
, u32 flags
)
925 audio_require_lock();
926 sys
->info
.flags
= flags
;
929 static u32
audio_player_get_flags( audio_player
*sys
)
931 audio_require_lock();
932 return sys
->info
.flags
;
940 static void audio_debug_ui( m4x4f mtx_pv
)
942 if( !vg_audio
.debug_ui
)
949 u32 cursor
, flags
, length
;
953 infos
[ SFX_MAX_SYSTEMS
];
962 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
964 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
969 audio_entity
*ent
= &aap
->ent
;
970 struct sound_info
*snd
= &infos
[ num_systems
++ ];
972 snd
->name
= ent
->name
;
973 snd
->cursor
= ent
->cur
;
974 snd
->flags
= ent
->info
.flags
;
975 snd
->length
= ent
->length
;
976 snd
->vol
= ent
->info
.vol
*100.0f
;
977 v3_copy( ent
->info
.world_position
, snd
->pos
);
979 strcpy( perf
, vg_audio
.performance_info
);
980 strcpy( psub0
, vg_audio
.performance_sub0
);
981 strcpy( psub1
, vg_audio
.performance_sub1
);
986 ui_global_ctx
.cursor
[0] = 10;
987 ui_global_ctx
.cursor
[1] = 10;
988 ui_global_ctx
.cursor
[2] = 150;
989 ui_global_ctx
.cursor
[3] = 12;
991 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
992 ui_global_ctx
.cursor
[1] += 20;
994 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, psub0
, 1, 0 );
995 ui_global_ctx
.cursor
[1] += 20;
997 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, psub1
, 1, 0 );
998 ui_global_ctx
.cursor
[1] += 20;
1000 float usage
= (float)vg_audio
.mem_current
/ (1024.0f
*1024.0f
),
1001 total
= (float)vg_audio
.mem_total
/ (1024.0f
*1024.0f
),
1002 percent
= (usage
/total
) * 100.0f
;
1003 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1005 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1006 ui_global_ctx
.cursor
[1] += 20;
1008 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1009 u32 overlap_length
= 0;
1011 /* Draw audio stack */
1012 for( int i
=0; i
<num_systems
; i
++ )
1014 struct sound_info
*inf
= &infos
[i
];
1016 ui_global_ctx
.cursor
[2] = 200;
1017 ui_global_ctx
.cursor
[3] = 18;
1019 u32 alpha
= 0xa0000000;
1021 ui_new_node( &ui_global_ctx
);
1023 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x00333333|alpha
);
1025 ui_px baseline
= ui_global_ctx
.cursor
[0],
1027 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1030 ui_global_ctx
.cursor
[2] = 2;
1031 ui_global_ctx
.cursor
[0] = c
;
1032 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0xffffffff );
1034 ui_global_ctx
.cursor
[0] = baseline
+ 2;
1035 ui_global_ctx
.cursor
[1] += 2;
1036 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1037 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1039 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1042 v3_copy( inf
->pos
, wpos
);
1044 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1046 if( wpos
[3] < 0.0f
)
1047 goto projected_behind
;
1049 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1050 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1053 wr
[0] = wpos
[0] * vg_window_x
;
1054 wr
[1] = (1.0f
-wpos
[1]) * vg_window_y
;
1058 for( int j
=0; j
<12; j
++ )
1061 for( int k
=0; k
<overlap_length
; k
++ )
1063 ui_px
*wk
= overlap_buffer
[k
];
1064 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1065 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1078 ui_text( &ui_global_ctx
, wr
, perf
, 1, 0 );
1080 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1086 ui_end_down( &ui_global_ctx
);
1087 ui_global_ctx
.cursor
[1] += 1;
1091 #endif /* VG_AUDIO_H */