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 #define STB_VORBIS_MAX_CHANNELS 2
36 #include "stb/stb_vorbis.h"
39 #pragma GCC pop_options
42 #define SFX_MAX_SYSTEMS 32
43 #define AUDIO_FLAG_LOOP 0x1
44 #define AUDIO_FLAG_ONESHOT 0x2
45 #define AUDIO_FLAG_SPACIAL_3D 0x4
47 #define FADEOUT_LENGTH 1100
48 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
50 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
52 enum audio_source_mode
55 k_audio_source_compressed
,
58 typedef struct audio_clip audio_clip
;
62 enum audio_source_mode source_mode
;
66 u32 len
; /* decompressed: sample count,
67 compressed: file size */
70 typedef struct audio_mix_info audio_mix_info
;
80 typedef struct audio_player audio_player
;
83 aatree_ptr active_entity
; /* non-nil if currently playing */
91 typedef struct audio_entity audio_entity
;
100 u32 fadeout
, fadeout_current
;
107 * play again: if already playing, leave in queue while it fadeouts
108 * oneshot: create a ghost entity
112 static struct vg_audio_system
114 ma_device miniaudio_device
;
115 ma_device_config miniaudio_dconfig
;
117 void *mem
, *decode_mem
;
124 vg_mutex mux_checker
,
127 /* Audio engine, thread 1 */
128 struct active_audio_player
134 aatree_pool_node pool_node
;
137 stb_vorbis
*vorbis_handle
;
138 stb_vorbis_alloc vorbis_alloc
;
140 active_players
[ SFX_MAX_SYSTEMS
];
142 aatree active_pool_info
; /* note: just using the pool */
143 aatree_ptr active_pool_head
;
145 /* System queue, and access from thread 0 */
146 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
149 char performance_info
[128],
150 performance_sub0
[64],
151 performance_sub1
[64];
158 double perf_ms_decode
,
161 u32 perf_measurements
;
165 static void *audio_alloc( u32 size
)
167 u32 new_current
= vg_audio
.mem_current
+ size
;
168 if( new_current
> vg_audio
.mem_total
)
170 vg_error( "audio pool over budget!\n" );
171 free( vg_audio
.mem
);
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 int 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
= malloc( 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_error( "ma_device failed to initialize" );
275 if( ma_device_start( device
) != MA_SUCCESS
)
277 ma_device_uninit( device
);
278 vg_error( "ma_device failed to start" );
283 vg_success( "Ready\n" );
287 static void vg_audio_free(void * nothing
)
289 ma_device
*device
= &vg_audio
.miniaudio_device
;
290 ma_device_uninit( device
);
292 free( vg_audio
.mem
);
294 vg_mutex_free( &vg_audio
.mux_checker
);
295 vg_mutex_free( &vg_audio
.mux_sync
);
302 static aatree_ptr
audio_alloc_entity_internal(void)
304 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
305 &vg_audio
.active_pool_head
);
307 if( playerid
== AATREE_PTR_NIL
)
308 return AATREE_PTR_NIL
;
310 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
316 static void audio_entity_free_internal( aatree_ptr id
)
318 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
321 /* Notify player that we've finished */
322 if( aap
->ent
.player
)
323 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
326 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
327 &vg_audio
.active_pool_head
);
330 static void *audio_entity_vorbis_ptr( aatree_ptr entid
)
332 u8
*buf
= (u8
*)vg_audio
.decode_mem
,
333 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
338 static void audio_entity_start( audio_entity
*src
)
340 aatree_ptr entid
= audio_alloc_entity_internal();
341 if( entid
== AATREE_PTR_NIL
)
344 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
346 ent
->info
= src
->info
;
347 ent
->name
= src
->info
.source
->path
;
349 ent
->player
= src
->player
;
352 ent
->fadeout_current
= 0;
354 /* Notify main player we are dequeud and playing */
357 src
->player
->enqued
= 0;
358 src
->player
->active_entity
= entid
;
361 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
363 /* Setup vorbis decoder */
364 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
366 stb_vorbis_alloc alloc
= {
367 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
368 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
372 stb_vorbis
*decoder
= stb_vorbis_open_memory(
373 src
->info
.source
->data
, src
->info
.source
->len
, &err
, &alloc
);
377 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
378 src
->info
.source
->path
, err
);
380 audio_entity_free_internal( entid
);
385 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
388 aap
->vorbis_handle
= decoder
;
392 ent
->length
= src
->info
.source
->len
;
397 * Read everything from the queue
399 static void audio_system_enque(void)
401 /* Process incoming sound queue */
405 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
407 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
412 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
414 audio_entity_start( src
);
418 /* Otherwise try start fadeout but dont remove from queue */
420 aatree_ptr entid
= src
->player
->active_entity
;
421 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
424 ent
->fadeout
= FADEOUT_LENGTH
;
425 ent
->fadeout_current
= FADEOUT_LENGTH
;
428 vg_audio
.entity_queue
[ wr
++ ] = *src
;
433 audio_entity_start( src
);
437 vg_audio
.queue_len
= wr
;
439 /* Localize others memory */
440 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
442 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
446 if( aap
->ent
.player
)
448 /* Only copy information in whilst not requeing */
449 if( aap
->ent
.player
->enqued
== 0 )
451 aap
->ent
.info
= aap
->ent
.player
->info
;
460 * Redistribute sound systems
462 static void audio_system_cleanup(void)
466 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
468 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
471 audio_entity
*src
= &aap
->ent
;
472 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
478 audio_entity_free_internal( i
);
487 * Get effective volume and pan from this entity
489 static void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
491 if( ent
->info
.vol
< 0.01f
)
493 *vol
= ent
->info
.vol
;
499 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
501 float dist2
= v3_length2( delta
);
503 if( dist2
< 0.0001f
)
510 float dist
= sqrtf( dist2
),
511 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
513 v3_muls( delta
, 1.0f
/dist
, delta
);
514 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
515 *vol
= 1.0f
/(attn
*attn
);
519 static void audio_decode_uncompressed_mono( float *src
, u32 count
, float *dst
)
521 for( u32 i
=0; i
<count
; i
++ )
523 dst
[ i
*2 + 0 ] = src
[i
];
524 dst
[ i
*2 + 1 ] = src
[i
];
529 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
532 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
536 c
= VG_MIN( 1, f
->channels
- 1 );
540 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
545 for( int j
=0; j
< k
; ++j
)
547 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
548 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
552 f
->channel_buffer_start
+= k
;
557 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
564 static void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
566 struct timespec time_start
, time_end
;
567 clock_gettime( CLOCK_REALTIME
, &time_start
);
569 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
570 audio_entity
*ent
= &aap
->ent
;
572 u32 remaining
= count
;
573 u32 cursor
= ent
->cur
;
578 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
579 remaining
-= samples_this_run
;
581 float *dst
= &buf
[ buffer_pos
* 2 ];
583 int source_mode
= ent
->info
.source
->source_mode
;
585 if( source_mode
== k_audio_source_mono
)
587 float *src
= &((float *)ent
->info
.source
->data
)[ cursor
];
588 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
590 else if( source_mode
== k_audio_source_compressed
)
592 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
597 if( read_samples
!= samples_this_run
)
599 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
603 cursor
+= samples_this_run
;
604 buffer_pos
+= samples_this_run
;
606 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
608 if( source_mode
== k_audio_source_compressed
)
610 stb_vorbis_seek_start( aap
->vorbis_handle
);
622 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
623 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
631 clock_gettime( CLOCK_REALTIME
, &time_end
);
632 vg_audio
.perf_ms_decode
+= vg_time_diff( time_start
, time_end
);
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 struct timespec time_start
, time_end
;
652 clock_gettime( CLOCK_REALTIME
, &time_start
);
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 clock_gettime( CLOCK_REALTIME
, &time_end
);
685 vg_audio
.perf_ms_mix
+= vg_time_diff( time_start
, time_end
);
689 * callback from miniaudio.h interface
691 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
692 const void *pInput
, ma_uint32 frame_count
)
694 struct timespec time_start
, time_end
;
695 clock_gettime( CLOCK_REALTIME
, &time_start
);
697 audio_system_enque();
700 float *pOut32F
= (float *)pOutBuf
;
701 for( int i
=0; i
<frame_count
*2; i
++ )
705 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
707 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
711 audio_entity_mix( i
, pOut32F
, frame_count
);
720 audio_system_cleanup();
722 /* TODO: what the hell is this? */
728 clock_gettime( CLOCK_REALTIME
, &time_end
);
730 double elapsed
= vg_time_diff( time_start
, time_end
),
731 budget
= ((double)frame_count
/ 44100.0) * 1000.0,
732 percent
= (elapsed
/budget
) * 100.0;
734 snprintf( vg_audio
.performance_info
, 127,
735 "%.2fms/%.2fms (%.1f%%) (%u frames)",
736 elapsed
, budget
, percent
, frame_count
);
738 vg_audio
.perf_measurements
++;
739 if( vg_audio
.perf_measurements
>= 30 )
741 double ms_decode
= vg_audio
.perf_ms_decode
* (1.0/30.0),
742 ms_mix
= vg_audio
.perf_ms_mix
* (1.0/30.0);
744 snprintf( vg_audio
.performance_sub0
, 63, "Decode %.2fms", ms_decode
);
745 snprintf( vg_audio
.performance_sub1
, 63, "mix %.2fms", ms_mix
);
747 vg_audio
.perf_ms_decode
= 0.0;
748 vg_audio
.perf_ms_mix
= 0.0;
750 vg_audio
.perf_measurements
= 0;
754 /* Decompress entire vorbis stream into buffer */
755 static float *audio_decompress_vorbis( const unsigned char *data
, int len
,
756 int channels
, u32
*samples
)
759 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
763 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
767 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
769 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
771 (float)length_samples
/ (44100.0f
*(float)channels
),
772 (float)(length_samples
*4*channels
) / 1024.0f
);
774 float *buffer
= audio_alloc( length_samples
* channels
* sizeof(float) );
777 stb_vorbis_close( pv
);
778 vg_error( "Failed to allocated memory for audio\n" );
782 int read_samples
= stb_vorbis_get_samples_float_interleaved(
783 pv
, channels
, buffer
, length_samples
* channels
);
785 if( read_samples
!= length_samples
)
787 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
788 length_samples
, read_samples
);
789 length_samples
= read_samples
;
792 stb_vorbis_close( pv
);
793 *samples
= length_samples
;
797 static int audio_clip_load( audio_clip
*clip
)
799 /* Load and decompress */
801 void *filedata
= vg_asset_read_s( clip
->path
, &file_len
);
805 vg_error( "OGG load failed (%s)\n", clip
->path
);
809 if( clip
->source_mode
== k_audio_source_mono
)
812 float *sound
= audio_decompress_vorbis( filedata
, file_len
, 1, &samples
);
816 float seconds
= (float)samples
/ 44100.0f
,
817 mb
= (float)(samples
*4) / (1024.0f
*1024.0f
);
819 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
820 clip
->path
, seconds
, mb
);
822 else if( clip
->source_mode
== k_audio_source_compressed
)
824 void *data
= audio_alloc( file_len
);
825 memcpy( data
, filedata
, file_len
);
828 clip
->len
= file_len
;
830 float mb
= (float)(file_len
) / (1024.0f
*1024.0f
);
831 vg_info( "Loaded audio clip[compressed] '%s' (%.1fmb)\n",
841 vg_error( "Unkown source mode (%u)\n", clip
->source_mode
);
848 static void audio_clip_loadn( audio_clip
*arr
, int count
)
850 for( int i
=0; i
<count
; i
++ )
851 audio_clip_load( &arr
[i
] );
854 /* Mark change to be uploaded through queue system */
855 static void audio_player_commit( audio_player
*sys
)
857 audio_require_lock();
859 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
861 vg_warn( "Audio commit queue full\n" );
867 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
872 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
873 ent
->info
= sys
->info
;
877 static void audio_require_init( audio_player
*player
)
882 vg_error( "Must init before playing! (%s)\n", player
->name
);
886 /* Play a clip using player. If its already playing something, it will
887 * fadeout quickly and start the next sound */
888 static void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
890 audio_require_lock();
891 audio_require_init( player
);
893 player
->info
.source
= clip
;
894 audio_player_commit( player
);
898 static void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
900 audio_require_lock();
901 audio_require_init( player
);
905 static void audio_play_oneshot( audio_clip
*clip
, float volume
)
907 audio_require_lock();
909 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
911 vg_warn( "Audio commit queue full\n" );
915 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
917 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
918 ent
->info
.pan
= 0.0f
;
919 ent
->info
.source
= clip
;
920 ent
->info
.vol
= volume
;
924 static void audio_player_init( audio_player
*player
)
926 player
->active_entity
= AATREE_PTR_NIL
;
935 * Safety enforced Get/set attributes
938 static int audio_player_is_playing( audio_player
*sys
)
940 audio_require_lock();
942 if( sys
->active_entity
!= AATREE_PTR_NIL
)
948 static void audio_player_set_position( audio_player
*sys
, v3f pos
)
950 audio_require_lock();
951 v3_copy( pos
, sys
->info
.world_position
);
954 static void audio_player_set_vol( audio_player
*sys
, float vol
)
956 audio_require_lock();
960 static float audio_player_get_vol( audio_player
*sys
)
962 audio_require_lock();
963 return sys
->info
.vol
;
966 static void audio_player_set_pan( audio_player
*sys
, float pan
)
968 audio_require_lock();
972 static float audio_player_get_pan( audio_player
*sys
)
974 audio_require_lock();
975 return sys
->info
.pan
;
978 static void audio_player_set_flags( audio_player
*sys
, u32 flags
)
980 audio_require_lock();
981 sys
->info
.flags
= flags
;
984 static u32
audio_player_get_flags( audio_player
*sys
)
986 audio_require_lock();
987 return sys
->info
.flags
;
995 static void audio_debug_ui( m4x4f mtx_pv
)
997 if( !vg_audio
.debug_ui
)
1004 u32 cursor
, flags
, length
;
1008 infos
[ SFX_MAX_SYSTEMS
];
1009 int num_systems
= 0;
1017 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
1019 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1024 audio_entity
*ent
= &aap
->ent
;
1025 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1027 snd
->name
= ent
->name
;
1028 snd
->cursor
= ent
->cur
;
1029 snd
->flags
= ent
->info
.flags
;
1030 snd
->length
= ent
->length
;
1031 snd
->vol
= ent
->info
.vol
*100.0f
;
1032 v3_copy( ent
->info
.world_position
, snd
->pos
);
1034 strcpy( perf
, vg_audio
.performance_info
);
1035 strcpy( psub0
, vg_audio
.performance_sub0
);
1036 strcpy( psub1
, vg_audio
.performance_sub1
);
1041 ui_global_ctx
.cursor
[0] = 10;
1042 ui_global_ctx
.cursor
[1] = 10;
1043 ui_global_ctx
.cursor
[2] = 150;
1044 ui_global_ctx
.cursor
[3] = 12;
1046 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1047 ui_global_ctx
.cursor
[1] += 20;
1049 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, psub0
, 1, 0 );
1050 ui_global_ctx
.cursor
[1] += 20;
1052 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, psub1
, 1, 0 );
1053 ui_global_ctx
.cursor
[1] += 20;
1055 float usage
= (float)vg_audio
.mem_current
/ (1024.0f
*1024.0f
),
1056 total
= (float)vg_audio
.mem_total
/ (1024.0f
*1024.0f
),
1057 percent
= (usage
/total
) * 100.0f
;
1058 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1060 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1061 ui_global_ctx
.cursor
[1] += 20;
1063 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1064 u32 overlap_length
= 0;
1066 /* Draw audio stack */
1067 for( int i
=0; i
<num_systems
; i
++ )
1069 struct sound_info
*inf
= &infos
[i
];
1071 ui_global_ctx
.cursor
[2] = 200;
1072 ui_global_ctx
.cursor
[3] = 18;
1074 u32 alpha
= 0xa0000000;
1076 ui_new_node( &ui_global_ctx
);
1078 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x00333333|alpha
);
1080 ui_px baseline
= ui_global_ctx
.cursor
[0],
1082 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1085 ui_global_ctx
.cursor
[2] = 2;
1086 ui_global_ctx
.cursor
[0] = c
;
1087 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0xffffffff );
1089 ui_global_ctx
.cursor
[0] = baseline
+ 2;
1090 ui_global_ctx
.cursor
[1] += 2;
1091 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1092 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1094 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1097 v3_copy( inf
->pos
, wpos
);
1099 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1101 if( wpos
[3] < 0.0f
)
1102 goto projected_behind
;
1104 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1105 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1108 wr
[0] = wpos
[0] * vg_window_x
;
1109 wr
[1] = (1.0f
-wpos
[1]) * vg_window_y
;
1113 for( int j
=0; j
<12; j
++ )
1116 for( int k
=0; k
<overlap_length
; k
++ )
1118 ui_px
*wk
= overlap_buffer
[k
];
1119 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1120 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1133 ui_text( &ui_global_ctx
, wr
, perf
, 1, 0 );
1135 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1141 ui_end_down( &ui_global_ctx
);
1142 ui_global_ctx
.cursor
[1] += 1;
1146 #endif /* VG_AUDIO_H */