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
;
129 vg_mutex mux_checker
,
132 /* Audio engine, thread 1 */
133 struct active_audio_player
139 aatree_pool_node pool_node
;
142 stb_vorbis
*vorbis_handle
;
143 stb_vorbis_alloc vorbis_alloc
;
145 active_players
[ SFX_MAX_SYSTEMS
];
147 aatree active_pool_info
; /* note: just using the pool */
148 aatree_ptr active_pool_head
;
150 /* System queue, and access from thread 0 */
151 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
154 char performance_info
[128],
155 performance_sub0
[64],
156 performance_sub1
[64];
163 double perf_ms_decode
,
166 u32 perf_measurements
;
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 u32 decode_region
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
242 vg_audio
.mem_total
= 1024*1024*32;
243 vg_audio
.mem_current
= 0;
244 vg_audio
.mem
= vg_alloc( vg_audio
.mem_total
+ decode_region
);
245 vg_audio
.decode_mem
= &((u8
*)vg_audio
.mem
)[vg_audio
.mem_total
];
248 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
249 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
251 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
252 vg_audio
.active_pool_info
.p_cmp
= NULL
;
253 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
255 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
256 ma_device
*device
= &vg_audio
.miniaudio_device
;
258 *dconf
= ma_device_config_init( ma_device_type_playback
);
259 dconf
->playback
.format
= ma_format_f32
;
260 dconf
->playback
.channels
= 2;
261 dconf
->sampleRate
= 44100;
262 dconf
->dataCallback
= audio_mixer_callback
;
263 dconf
->periodSizeInFrames
= 441;
265 dconf
->pUserData
= NULL
;
267 vg_info( "Starting audio engine\n" );
269 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
271 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
275 if( ma_device_start( device
) != MA_SUCCESS
)
277 ma_device_uninit( device
);
278 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
282 vg_success( "Ready\n" );
285 static void vg_audio_free(void * nothing
)
287 ma_device
*device
= &vg_audio
.miniaudio_device
;
288 ma_device_uninit( device
);
290 vg_free( vg_audio
.mem
);
298 static aatree_ptr
audio_alloc_entity_internal(void)
300 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
301 &vg_audio
.active_pool_head
);
303 if( playerid
== AATREE_PTR_NIL
)
304 return AATREE_PTR_NIL
;
306 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
312 static void audio_entity_free_internal( aatree_ptr id
)
314 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
317 /* Notify player that we've finished */
318 if( aap
->ent
.player
)
319 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
322 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
323 &vg_audio
.active_pool_head
);
326 static void *audio_entity_vorbis_ptr( aatree_ptr entid
)
328 u8
*buf
= (u8
*)vg_audio
.decode_mem
,
329 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
334 static void audio_entity_start( audio_entity
*src
)
336 aatree_ptr entid
= audio_alloc_entity_internal();
337 if( entid
== AATREE_PTR_NIL
)
340 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
342 ent
->info
= src
->info
;
343 ent
->name
= src
->info
.source
->path
;
345 ent
->player
= src
->player
;
348 ent
->fadeout_current
= 0;
350 /* Notify main player we are dequeud and playing */
353 src
->player
->enqued
= 0;
354 src
->player
->active_entity
= entid
;
357 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
359 /* Setup vorbis decoder */
360 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
362 stb_vorbis_alloc alloc
= {
363 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
364 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
368 stb_vorbis
*decoder
= stb_vorbis_open_memory(
369 src
->info
.source
->data
, src
->info
.source
->len
, &err
, &alloc
);
373 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
374 src
->info
.source
->path
, err
);
376 audio_entity_free_internal( entid
);
381 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
384 aap
->vorbis_handle
= decoder
;
388 ent
->length
= src
->info
.source
->len
;
393 * Read everything from the queue
395 static void audio_system_enque(void)
397 /* Process incoming sound queue */
401 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
403 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
408 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
410 audio_entity_start( src
);
414 /* Otherwise try start fadeout but dont remove from queue */
416 aatree_ptr entid
= src
->player
->active_entity
;
417 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
420 ent
->fadeout
= FADEOUT_LENGTH
;
421 ent
->fadeout_current
= FADEOUT_LENGTH
;
424 vg_audio
.entity_queue
[ wr
++ ] = *src
;
429 audio_entity_start( src
);
433 vg_audio
.queue_len
= wr
;
435 /* Localize others memory */
436 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
438 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
442 if( aap
->ent
.player
)
444 /* Only copy information in whilst not requeing */
445 if( aap
->ent
.player
->enqued
== 0 )
447 aap
->ent
.info
= aap
->ent
.player
->info
;
456 * Redistribute sound systems
458 static void audio_system_cleanup(void)
462 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
464 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
467 audio_entity
*src
= &aap
->ent
;
468 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
474 audio_entity_free_internal( i
);
483 * Get effective volume and pan from this entity
485 static void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
487 if( ent
->info
.vol
< 0.01f
)
489 *vol
= ent
->info
.vol
;
495 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
497 float dist2
= v3_length2( delta
);
499 if( dist2
< 0.0001f
)
506 float dist
= sqrtf( dist2
),
507 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
509 v3_muls( delta
, 1.0f
/dist
, delta
);
510 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
511 *vol
= 1.0f
/(attn
*attn
);
515 static void audio_decode_uncompressed_mono( float *src
, u32 count
, float *dst
)
517 for( u32 i
=0; i
<count
; i
++ )
519 dst
[ i
*2 + 0 ] = src
[i
];
520 dst
[ i
*2 + 1 ] = src
[i
];
525 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
528 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
532 c
= VG_MIN( 1, f
->channels
- 1 );
536 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
541 for( int j
=0; j
< k
; ++j
)
543 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
544 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
548 f
->channel_buffer_start
+= k
;
553 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
560 static void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
562 struct timespec time_start
, time_end
;
563 clock_gettime( CLOCK_REALTIME
, &time_start
);
565 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
566 audio_entity
*ent
= &aap
->ent
;
568 u32 remaining
= count
;
569 u32 cursor
= ent
->cur
;
574 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
575 remaining
-= samples_this_run
;
577 float *dst
= &buf
[ buffer_pos
* 2 ];
579 int source_mode
= ent
->info
.source
->source_mode
;
581 if( source_mode
== k_audio_source_mono
)
583 float *src
= &((float *)ent
->info
.source
->data
)[ cursor
];
584 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
586 else if( source_mode
== k_audio_source_compressed
)
588 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
593 if( read_samples
!= samples_this_run
)
595 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
599 cursor
+= samples_this_run
;
600 buffer_pos
+= samples_this_run
;
602 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
604 if( source_mode
== k_audio_source_compressed
)
606 stb_vorbis_seek_start( aap
->vorbis_handle
);
618 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
619 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
627 clock_gettime( CLOCK_REALTIME
, &time_end
);
628 vg_audio
.perf_ms_decode
+= vg_time_diff( time_start
, time_end
);
631 static void audio_entity_mix( aatree_ptr id
, float *buffer
,
634 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
636 u32 cursor
= ent
->cur
, buffer_pos
= 0;
637 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
639 u32 frames_write
= frame_count
;
640 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
642 float vol
= ent
->info
.vol
,
645 audio_entity_get_samples( id
, frame_count
, pcf
);
647 struct timespec time_start
, time_end
;
648 clock_gettime( CLOCK_REALTIME
, &time_start
);
650 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
651 audio_entity_spacialize( ent
, &vol
, &pan
);
653 for( u32 j
=0; j
<frame_count
; j
++ )
655 float frame_vol
= vol
;
659 /* Force this system to be removed now */
660 if( ent
->fadeout_current
== 0 )
662 ent
->info
.flags
= 0x00;
663 ent
->cur
= ent
->length
;
667 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
668 ent
->fadeout_current
--;
674 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
675 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
680 clock_gettime( CLOCK_REALTIME
, &time_end
);
681 vg_audio
.perf_ms_mix
+= vg_time_diff( time_start
, time_end
);
685 * callback from miniaudio.h interface
687 static void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
688 const void *pInput
, ma_uint32 frame_count
)
690 struct timespec time_start
, time_end
;
691 clock_gettime( CLOCK_REALTIME
, &time_start
);
693 audio_system_enque();
696 float *pOut32F
= (float *)pOutBuf
;
697 for( int i
=0; i
<frame_count
*2; i
++ )
701 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
703 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
707 audio_entity_mix( i
, pOut32F
, frame_count
);
716 audio_system_cleanup();
718 /* TODO: what the hell is this? */
724 clock_gettime( CLOCK_REALTIME
, &time_end
);
726 double elapsed
= vg_time_diff( time_start
, time_end
),
727 budget
= ((double)frame_count
/ 44100.0) * 1000.0,
728 percent
= (elapsed
/budget
) * 100.0;
730 snprintf( vg_audio
.performance_info
, 127,
731 "%.2fms/%.2fms (%.1f%%) (%u frames)",
732 elapsed
, budget
, percent
, frame_count
);
734 vg_audio
.perf_measurements
++;
735 if( vg_audio
.perf_measurements
>= 30 )
737 double ms_decode
= vg_audio
.perf_ms_decode
* (1.0/30.0),
738 ms_mix
= vg_audio
.perf_ms_mix
* (1.0/30.0);
740 snprintf( vg_audio
.performance_sub0
, 63, "Decode %.2fms", ms_decode
);
741 snprintf( vg_audio
.performance_sub1
, 63, "mix %.2fms", ms_mix
);
743 vg_audio
.perf_ms_decode
= 0.0;
744 vg_audio
.perf_ms_mix
= 0.0;
746 vg_audio
.perf_measurements
= 0;
750 /* Decompress entire vorbis stream into buffer */
751 static float *audio_decompress_vorbis( const unsigned char *data
, int len
,
752 int channels
, u32
*samples
)
755 stb_vorbis
*pv
= stb_vorbis_open_memory( data
, len
, &err
, NULL
);
759 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err
);
763 u32 length_samples
= stb_vorbis_stream_length_in_samples( pv
);
765 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
767 (float)length_samples
/ (44100.0f
*(float)channels
),
768 (float)(length_samples
*4*channels
) / 1024.0f
);
770 float *buffer
= audio_alloc( length_samples
* channels
* sizeof(float) );
773 stb_vorbis_close( pv
);
774 vg_error( "Failed to allocated memory for audio\n" );
778 int read_samples
= stb_vorbis_get_samples_float_interleaved(
779 pv
, channels
, buffer
, length_samples
* channels
);
781 if( read_samples
!= length_samples
)
783 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
784 length_samples
, read_samples
);
785 length_samples
= read_samples
;
788 stb_vorbis_close( pv
);
789 *samples
= length_samples
;
793 static int audio_clip_load( audio_clip
*clip
)
795 /* Load and decompress */
797 void *filedata
= vg_asset_read_s( clip
->path
, &file_len
);
801 vg_error( "OGG load failed (%s)\n", clip
->path
);
805 if( clip
->source_mode
== k_audio_source_mono
)
808 float *sound
= audio_decompress_vorbis( filedata
, file_len
, 1, &samples
);
812 float seconds
= (float)samples
/ 44100.0f
,
813 mb
= (float)(samples
*4) / (1024.0f
*1024.0f
);
815 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
816 clip
->path
, seconds
, mb
);
818 else if( clip
->source_mode
== k_audio_source_compressed
)
820 void *data
= audio_alloc( file_len
);
821 memcpy( data
, filedata
, file_len
);
824 clip
->len
= file_len
;
826 float mb
= (float)(file_len
) / (1024.0f
*1024.0f
);
827 vg_info( "Loaded audio clip[compressed] '%s' (%.1fmb)\n",
837 vg_error( "Unkown source mode (%u)\n", clip
->source_mode
);
844 static void audio_clip_loadn( audio_clip
*arr
, int count
)
846 for( int i
=0; i
<count
; i
++ )
847 audio_clip_load( &arr
[i
] );
850 /* Mark change to be uploaded through queue system */
851 static void audio_player_commit( audio_player
*sys
)
853 audio_require_lock();
855 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
857 vg_warn( "Audio commit queue full\n" );
863 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
868 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
869 ent
->info
= sys
->info
;
873 static void audio_require_init( audio_player
*player
)
878 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
881 static void audio_require_clip_loaded( audio_clip
*clip
)
886 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
889 /* Play a clip using player. If its already playing something, it will
890 * fadeout quickly and start the next sound */
891 static void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
893 audio_require_lock();
894 audio_require_init( player
);
895 audio_require_clip_loaded( clip
);
897 player
->info
.source
= clip
;
898 audio_player_commit( player
);
902 static void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
904 audio_require_lock();
905 audio_require_init( player
);
909 static void audio_play_oneshot( audio_clip
*clip
, float volume
)
911 audio_require_lock();
912 audio_require_clip_loaded( clip
);
914 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
916 vg_warn( "Audio commit queue full\n" );
920 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
922 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
923 ent
->info
.pan
= 0.0f
;
924 ent
->info
.source
= clip
;
925 ent
->info
.vol
= volume
;
929 static void audio_player_init( audio_player
*player
)
931 player
->active_entity
= AATREE_PTR_NIL
;
940 * Safety enforced Get/set attributes
943 static int audio_player_is_playing( audio_player
*sys
)
945 audio_require_lock();
947 if( sys
->active_entity
!= AATREE_PTR_NIL
)
953 static void audio_player_set_position( audio_player
*sys
, v3f pos
)
955 audio_require_lock();
956 v3_copy( pos
, sys
->info
.world_position
);
959 static void audio_player_set_vol( audio_player
*sys
, float vol
)
961 audio_require_lock();
965 static float audio_player_get_vol( audio_player
*sys
)
967 audio_require_lock();
968 return sys
->info
.vol
;
971 static void audio_player_set_pan( audio_player
*sys
, float pan
)
973 audio_require_lock();
977 static float audio_player_get_pan( audio_player
*sys
)
979 audio_require_lock();
980 return sys
->info
.pan
;
983 static void audio_player_set_flags( audio_player
*sys
, u32 flags
)
985 audio_require_lock();
986 sys
->info
.flags
= flags
;
989 static u32
audio_player_get_flags( audio_player
*sys
)
991 audio_require_lock();
992 return sys
->info
.flags
;
1000 static void audio_debug_ui( m4x4f mtx_pv
)
1002 if( !vg_audio
.debug_ui
)
1009 u32 cursor
, flags
, length
;
1013 infos
[ SFX_MAX_SYSTEMS
];
1014 int num_systems
= 0;
1022 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
1024 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1029 audio_entity
*ent
= &aap
->ent
;
1030 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1032 snd
->name
= ent
->name
;
1033 snd
->cursor
= ent
->cur
;
1034 snd
->flags
= ent
->info
.flags
;
1035 snd
->length
= ent
->length
;
1036 snd
->vol
= ent
->info
.vol
*100.0f
;
1037 v3_copy( ent
->info
.world_position
, snd
->pos
);
1039 strcpy( perf
, vg_audio
.performance_info
);
1040 strcpy( psub0
, vg_audio
.performance_sub0
);
1041 strcpy( psub1
, vg_audio
.performance_sub1
);
1046 ui_global_ctx
.cursor
[0] = 10;
1047 ui_global_ctx
.cursor
[1] = 10;
1048 ui_global_ctx
.cursor
[2] = 150;
1049 ui_global_ctx
.cursor
[3] = 12;
1051 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1052 ui_global_ctx
.cursor
[1] += 20;
1054 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, psub0
, 1, 0 );
1055 ui_global_ctx
.cursor
[1] += 20;
1057 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, psub1
, 1, 0 );
1058 ui_global_ctx
.cursor
[1] += 20;
1060 float usage
= (float)vg_audio
.mem_current
/ (1024.0f
*1024.0f
),
1061 total
= (float)vg_audio
.mem_total
/ (1024.0f
*1024.0f
),
1062 percent
= (usage
/total
) * 100.0f
;
1063 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1065 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1066 ui_global_ctx
.cursor
[1] += 20;
1068 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1069 u32 overlap_length
= 0;
1071 /* Draw audio stack */
1072 for( int i
=0; i
<num_systems
; i
++ )
1074 struct sound_info
*inf
= &infos
[i
];
1076 ui_global_ctx
.cursor
[2] = 200;
1077 ui_global_ctx
.cursor
[3] = 18;
1079 u32 alpha
= 0xa0000000;
1081 ui_new_node( &ui_global_ctx
);
1083 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0x00333333|alpha
);
1085 ui_px baseline
= ui_global_ctx
.cursor
[0],
1087 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1090 ui_global_ctx
.cursor
[2] = 2;
1091 ui_global_ctx
.cursor
[0] = c
;
1092 ui_fill_rect( &ui_global_ctx
, ui_global_ctx
.cursor
, 0xffffffff );
1094 ui_global_ctx
.cursor
[0] = baseline
+ 2;
1095 ui_global_ctx
.cursor
[1] += 2;
1096 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1097 ui_text( &ui_global_ctx
, ui_global_ctx
.cursor
, perf
, 1, 0 );
1099 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1102 v3_copy( inf
->pos
, wpos
);
1104 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1106 if( wpos
[3] < 0.0f
)
1107 goto projected_behind
;
1109 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1110 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1113 wr
[0] = wpos
[0] * vg_window_x
;
1114 wr
[1] = (1.0f
-wpos
[1]) * vg_window_y
;
1118 for( int j
=0; j
<12; j
++ )
1121 for( int k
=0; k
<overlap_length
; k
++ )
1123 ui_px
*wk
= overlap_buffer
[k
];
1124 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1125 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1138 ui_text( &ui_global_ctx
, wr
, perf
, 1, 0 );
1140 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1146 ui_end_down( &ui_global_ctx
);
1147 ui_global_ctx
.cursor
[1] += 1;
1151 #endif /* VG_AUDIO_H */