1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
9 #include "vg/vg_stdint.h"
10 #include "vg/vg_platform.h"
14 #include "vg/vg_console.h"
15 #include "vg/vg_store.h"
16 #include "vg/vg_profiler.h"
22 #pragma GCC push_options
23 #pragma GCC optimize ("O3")
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
29 #define STB_VORBIS_MAX_CHANNELS 2
30 #include "submodules/stb/stb_vorbis.c"
37 #pragma GCC pop_options
38 #pragma GCC diagnostic pop
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
46 #define AUDIO_FLAG_AUTO_START 0x8
47 #define AUDIO_FLAG_KILL 0x10
49 #define FADEOUT_LENGTH 1100
50 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
52 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
54 enum audio_source_mode
57 k_audio_source_compressed
,
60 typedef struct audio_clip audio_clip
;
64 enum audio_source_mode source_mode
;
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
115 ma_device miniaudio_device
;
116 ma_device_config miniaudio_dconfig
;
118 SDL_AudioDeviceID sdl_output_device
;
127 SDL_mutex
*mux_checker
,
130 /* Audio engine, thread 1 */
131 struct active_audio_player
137 aatree_pool_node pool_node
;
140 stb_vorbis
*vorbis_handle
;
141 stb_vorbis_alloc vorbis_alloc
;
143 active_players
[ SFX_MAX_SYSTEMS
];
145 aatree active_pool_info
; /* note: just using the pool */
146 aatree_ptr active_pool_head
;
148 /* System queue, and access from thread 0 */
149 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
151 int debug_ui
, debug_ui_3d
;
158 static struct vg_profile
159 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
160 .name
= "[T2] audio_decode()"},
161 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
162 .name
= "[T2] audio_mix()"},
163 vg_prof_audio_decode
,
167 * These functions are called from the main thread and used to prevent bad
168 * access. TODO: They should be no-ops in release builds.
170 VG_STATIC
int audio_lock_checker_load(void)
173 SDL_LockMutex( vg_audio
.mux_checker
);
174 value
= vg_audio
.sync_locked
;
175 SDL_UnlockMutex( vg_audio
.mux_checker
);
179 VG_STATIC
void audio_lock_checker_store( int value
)
181 SDL_LockMutex( vg_audio
.mux_checker
);
182 vg_audio
.sync_locked
= value
;
183 SDL_UnlockMutex( vg_audio
.mux_checker
);
186 VG_STATIC
void audio_require_lock(void)
188 if( audio_lock_checker_load() )
191 vg_error( "Modifying sound effects systems requires locking\n" );
195 VG_STATIC
void audio_lock(void)
197 SDL_LockMutex( vg_audio
.mux_sync
);
198 audio_lock_checker_store(1);
201 VG_STATIC
void audio_unlock(void)
203 audio_lock_checker_store(0);
204 SDL_UnlockMutex( vg_audio
.mux_sync
);
207 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int frame_count
);
208 VG_STATIC
void vg_audio_init(void)
210 vg_audio
.mux_checker
= SDL_CreateMutex();
211 vg_audio
.mux_sync
= SDL_CreateMutex();
213 /* TODO: Move here? */
214 vg_convar_push( (struct vg_convar
){
215 .name
= "debug_audio",
216 .data
= &vg_audio
.debug_ui
,
217 .data_type
= k_convar_dtype_i32
,
218 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
222 /* allocate memory */
225 vg_audio
.audio_pool
=
226 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32,
230 u32 decode_size
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
231 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
234 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
235 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
237 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
238 vg_audio
.active_pool_info
.p_cmp
= NULL
;
239 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
241 SDL_AudioSpec spec_desired
, spec_got
;
242 spec_desired
.callback
= audio_mixer_callback
;
243 spec_desired
.channels
= 2;
244 spec_desired
.format
= AUDIO_F32
;
245 spec_desired
.freq
= 44100;
246 spec_desired
.padding
= 0;
247 spec_desired
.samples
= 512;
248 spec_desired
.silence
= 0;
249 spec_desired
.size
= 0;
250 spec_desired
.userdata
= NULL
;
252 vg_audio
.sdl_output_device
=
253 SDL_OpenAudioDevice( NULL
, 0, &spec_desired
, &spec_got
,
254 SDL_AUDIO_ALLOW_SAMPLES_CHANGE
);
256 if( vg_audio
.sdl_output_device
)
258 SDL_PauseAudioDevice( vg_audio
.sdl_output_device
, 0 );
263 "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
264 " Frequency: 44100 hz\n"
265 " Buffer size: 512\n"
267 " Format: s16 or f32\n" );
270 vg_success( "Ready\n" );
273 VG_STATIC
void vg_audio_free(void)
275 SDL_CloseAudioDevice( vg_audio
.sdl_output_device
);
282 static aatree_ptr
audio_alloc_entity_internal(void)
284 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
285 &vg_audio
.active_pool_head
);
287 if( playerid
== AATREE_PTR_NIL
)
288 return AATREE_PTR_NIL
;
290 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
296 VG_STATIC
void audio_entity_free_internal( aatree_ptr id
)
298 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
301 /* Notify player that we've finished */
302 if( aap
->ent
.player
)
303 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
306 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
307 &vg_audio
.active_pool_head
);
310 VG_STATIC
void *audio_entity_vorbis_ptr( aatree_ptr entid
)
312 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
313 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
318 VG_STATIC
void audio_entity_start( audio_entity
*src
)
320 aatree_ptr entid
= audio_alloc_entity_internal();
321 if( entid
== AATREE_PTR_NIL
)
324 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
326 ent
->info
= src
->info
;
327 ent
->name
= src
->info
.source
->path
;
329 ent
->player
= src
->player
;
332 ent
->fadeout_current
= 0;
334 /* Notify main player we are dequeud and playing */
337 src
->player
->enqued
= 0;
338 src
->player
->active_entity
= entid
;
341 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
343 /* Setup vorbis decoder */
344 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
346 stb_vorbis_alloc alloc
= {
347 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
348 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
352 stb_vorbis
*decoder
= stb_vorbis_open_memory(
353 src
->info
.source
->data
,
354 src
->info
.source
->size
, &err
, &alloc
);
358 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
359 src
->info
.source
->path
, err
);
361 audio_entity_free_internal( entid
);
366 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
369 aap
->vorbis_handle
= decoder
;
373 ent
->length
= src
->info
.source
->size
;
378 * Read everything from the queue
380 VG_STATIC
void audio_system_enque(void)
382 /* Process incoming sound queue */
386 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
388 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
393 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
395 audio_entity_start( src
);
399 /* Otherwise try start fadeout but dont remove from queue */
401 aatree_ptr entid
= src
->player
->active_entity
;
402 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
405 ent
->fadeout
= FADEOUT_LENGTH
;
406 ent
->fadeout_current
= FADEOUT_LENGTH
;
409 vg_audio
.entity_queue
[ wr
++ ] = *src
;
414 audio_entity_start( src
);
418 vg_audio
.queue_len
= wr
;
420 /* Localize others memory */
421 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
423 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
427 if( aap
->ent
.player
)
429 /* Only copy information in whilst not requeing */
430 if( aap
->ent
.player
->enqued
== 0 )
432 aap
->ent
.info
= aap
->ent
.player
->info
;
434 if( (aap
->ent
.info
.flags
& AUDIO_FLAG_KILL
) && !aap
->ent
.fadeout
)
436 aap
->ent
.fadeout
= FADEOUT_LENGTH
;
437 aap
->ent
.fadeout_current
= FADEOUT_LENGTH
;
447 * Redistribute sound systems
449 VG_STATIC
void audio_system_cleanup(void)
453 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
455 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
458 audio_entity
*src
= &aap
->ent
;
459 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
465 audio_entity_free_internal( i
);
474 * Get effective volume and pan from this entity
476 VG_STATIC
void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
478 if( ent
->info
.vol
< 0.01f
)
480 *vol
= ent
->info
.vol
;
486 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
488 float dist2
= v3_length2( delta
);
490 if( dist2
< 0.0001f
)
497 float dist
= sqrtf( dist2
),
498 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
500 v3_muls( delta
, 1.0f
/dist
, delta
);
501 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
502 *vol
= 1.0f
/(attn
*attn
);
506 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
508 for( u32 i
=0; i
<count
; i
++ )
510 dst
[ i
*2 + 0 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
511 dst
[ i
*2 + 1 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
516 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
519 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
523 c
= VG_MIN( 1, f
->channels
- 1 );
527 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
532 for( int j
=0; j
< k
; ++j
)
534 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
535 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
539 f
->channel_buffer_start
+= k
;
544 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
552 * ........ more wrecked code sorry!
555 stb_vorbis_get_samples_i16_downmixed( stb_vorbis
*f
, i16
*buffer
, int len
)
558 c
= VG_MIN( 1, f
->channels
- 1 );
562 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
567 for( int j
=0; j
< k
; ++j
)
569 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
570 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
572 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
573 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
577 f
->channel_buffer_start
+= k
;
582 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
589 VG_STATIC
void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
591 vg_profile_begin( &_vg_prof_audio_decode
);
593 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
594 audio_entity
*ent
= &aap
->ent
;
596 u32 remaining
= count
;
597 u32 cursor
= ent
->cur
;
602 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
603 remaining
-= samples_this_run
;
605 float *dst
= &buf
[ buffer_pos
* 2 ];
607 int source_mode
= ent
->info
.source
->source_mode
;
609 if( source_mode
== k_audio_source_mono
)
611 i16
*src_buffer
= ent
->info
.source
->data
,
612 *src
= &src_buffer
[cursor
];
614 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
616 else if( source_mode
== k_audio_source_compressed
)
618 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
623 if( read_samples
!= samples_this_run
)
625 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
629 cursor
+= samples_this_run
;
630 buffer_pos
+= samples_this_run
;
632 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
634 if( source_mode
== k_audio_source_compressed
)
636 stb_vorbis_seek_start( aap
->vorbis_handle
);
648 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
649 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
656 vg_profile_end( &_vg_prof_audio_decode
);
659 VG_STATIC
void audio_entity_mix( aatree_ptr id
, float *buffer
,
662 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
664 u32 cursor
= ent
->cur
, buffer_pos
= 0;
665 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
667 u32 frames_write
= frame_count
;
668 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
670 float vol
= ent
->info
.vol
,
673 audio_entity_get_samples( id
, frame_count
, pcf
);
675 vg_profile_begin( &_vg_prof_audio_mix
);
677 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
678 audio_entity_spacialize( ent
, &vol
, &pan
);
680 for( u32 j
=0; j
<frame_count
; j
++ )
682 float frame_vol
= vol
;
686 /* Force this system to be removed now */
687 if( ent
->fadeout_current
== 0 )
689 ent
->info
.flags
= 0x00;
690 ent
->cur
= ent
->length
;
694 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
695 ent
->fadeout_current
--;
701 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
702 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
707 vg_profile_end( &_vg_prof_audio_mix
);
710 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int byte_count
)
712 audio_system_enque();
714 int frame_count
= byte_count
/(2*sizeof(float));
717 float *pOut32F
= (float *)stream
;
718 for( int i
=0; i
<frame_count
*2; i
++ )
722 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
724 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
728 audio_entity_mix( i
, pOut32F
, frame_count
);
733 audio_system_cleanup();
736 vg_profile_increment( &_vg_prof_audio_decode
);
737 vg_profile_increment( &_vg_prof_audio_mix
);
739 vg_prof_audio_mix
= _vg_prof_audio_mix
;
740 vg_prof_audio_decode
= _vg_prof_audio_decode
;
742 vg_audio
.samples_last
= frame_count
;
746 VG_STATIC
void audio_clip_load( audio_clip
*clip
, void *lin_alloc
)
748 if( lin_alloc
== NULL
)
749 lin_alloc
= vg_audio
.audio_pool
;
751 if( clip
->source_mode
== k_audio_source_mono
)
753 vg_linear_clear( vg_mem
.scratch
);
756 stb_vorbis_alloc alloc
= {
757 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
758 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
761 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
764 stb_vorbis
*decoder
= stb_vorbis_open_memory(
765 filedata
, fsize
, &err
, &alloc
);
769 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
771 vg_fatal_exit_loop( "Vorbis decode error" );
774 /* only mono is supported in uncompressed */
775 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
776 data_size
= length_samples
* sizeof(i16
);
779 clip
->data
= vg_linear_alloc( lin_alloc
, data_size
);
780 clip
->size
= length_samples
;
783 int read_samples
= stb_vorbis_get_samples_i16_downmixed(
784 decoder
, clip
->data
, length_samples
);
786 if( read_samples
!= length_samples
)
787 vg_fatal_exit_loop( "Decode error" );
789 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
790 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip
->path
, mb
,
794 /* load in directly */
795 else if( clip
->source_mode
== k_audio_source_compressed
)
798 clip
->data
= vg_file_read( lin_alloc
, clip
->path
, &clip
->size
);
802 vg_fatal_exit_loop( "Audio failed to load" );
804 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
805 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
809 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
)
811 for( int i
=0; i
<count
; i
++ )
812 audio_clip_load( &arr
[i
], lin_alloc
);
815 /* Mark change to be uploaded through queue system */
816 VG_STATIC
void audio_player_commit( audio_player
*sys
)
818 audio_require_lock();
820 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
822 vg_warn( "Audio commit queue full\n" );
828 vg_warn( "[2] Audio commit spamming; already enqued (%s)\n", sys
->name
);
833 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
834 ent
->info
= sys
->info
;
838 VG_STATIC
void audio_require_init( audio_player
*player
)
844 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
847 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
849 if( clip
->data
&& clip
->size
)
853 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
856 /* Play a clip using player. If its already playing something, it will
857 * fadeout quickly and start the next sound */
858 VG_STATIC
void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
860 audio_require_lock();
861 audio_require_init( player
);
862 audio_require_clip_loaded( clip
);
864 if( player
->info
.flags
& AUDIO_FLAG_KILL
)
866 vg_error( "Can't start audio clip on player that is/has disconnected" );
872 vg_warn( "[1] Audio commit spamming; already enqued (%s)\n",
877 player
->info
.source
= clip
;
878 audio_player_commit( player
);
881 VG_STATIC
void audio_play_oneshot( audio_clip
*clip
, float volume
)
883 audio_require_lock();
884 audio_require_clip_loaded( clip
);
886 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
888 vg_warn( "Audio commit queue full\n" );
892 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
894 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
895 ent
->info
.pan
= 0.0f
;
896 ent
->info
.source
= clip
;
897 ent
->info
.vol
= volume
;
901 VG_STATIC
void audio_player_init( audio_player
*player
)
903 player
->active_entity
= AATREE_PTR_NIL
;
912 * Safety enforced Get/set attributes
915 VG_STATIC
int audio_player_is_playing( audio_player
*sys
)
917 audio_require_lock();
919 if( sys
->active_entity
!= AATREE_PTR_NIL
)
925 VG_STATIC
void audio_player_set_position( audio_player
*sys
, v3f pos
)
927 audio_require_lock();
928 v3_copy( pos
, sys
->info
.world_position
);
931 VG_STATIC
void audio_player_set_vol( audio_player
*sys
, float vol
)
933 audio_require_lock();
937 VG_STATIC
float audio_player_get_vol( audio_player
*sys
)
939 audio_require_lock();
940 return sys
->info
.vol
;
943 VG_STATIC
void audio_player_set_pan( audio_player
*sys
, float pan
)
945 audio_require_lock();
949 VG_STATIC
float audio_player_get_pan( audio_player
*sys
)
951 audio_require_lock();
952 return sys
->info
.pan
;
955 VG_STATIC
void audio_player_set_flags( audio_player
*sys
, u32 flags
)
957 audio_require_lock();
958 sys
->info
.flags
= flags
;
961 VG_STATIC u32
audio_player_get_flags( audio_player
*sys
)
963 audio_require_lock();
964 return sys
->info
.flags
;
972 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
974 if( !vg_audio
.debug_ui
)
981 u32 cursor
, flags
, length
;
985 infos
[ SFX_MAX_SYSTEMS
];
990 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
992 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
997 audio_entity
*ent
= &aap
->ent
;
998 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1000 snd
->name
= ent
->name
;
1001 snd
->cursor
= ent
->cur
;
1002 snd
->flags
= ent
->info
.flags
;
1003 snd
->length
= ent
->length
;
1004 snd
->vol
= ent
->info
.vol
*100.0f
;
1005 v3_copy( ent
->info
.world_position
, snd
->pos
);
1008 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1009 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1010 &vg_prof_audio_mix
}, 2,
1011 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1019 vg_uictx
.cursor
[0] = 258;
1020 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1021 vg_uictx
.cursor
[2] = 150;
1022 vg_uictx
.cursor
[3] = 12;
1024 float mb1
= 1024.0f
*1024.0f
,
1025 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1026 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1027 percent
= (usage
/total
) * 100.0f
;
1029 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1031 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1032 vg_uictx
.cursor
[1] += 20;
1034 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1035 u32 overlap_length
= 0;
1037 /* Draw audio stack */
1038 for( int i
=0; i
<num_systems
; i
++ )
1040 struct sound_info
*inf
= &infos
[i
];
1042 vg_uictx
.cursor
[2] = 200;
1043 vg_uictx
.cursor
[3] = 18;
1045 u32 alpha
= 0xa0000000;
1049 ui_fill_rect( vg_uictx
.cursor
, 0x00333333|alpha
);
1051 ui_px baseline
= vg_uictx
.cursor
[0],
1053 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1056 vg_uictx
.cursor
[2] = 2;
1057 vg_uictx
.cursor
[0] = c
;
1058 ui_fill_rect( vg_uictx
.cursor
, 0xffffffff );
1060 vg_uictx
.cursor
[0] = baseline
+ 2;
1061 vg_uictx
.cursor
[1] += 2;
1062 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1063 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1065 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1068 v3_copy( inf
->pos
, wpos
);
1070 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1072 if( wpos
[3] < 0.0f
)
1073 goto projected_behind
;
1075 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1076 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1079 wr
[0] = wpos
[0] * vg
.window_x
;
1080 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1084 for( int j
=0; j
<12; j
++ )
1087 for( int k
=0; k
<overlap_length
; k
++ )
1089 ui_px
*wk
= overlap_buffer
[k
];
1090 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1091 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1104 ui_text( wr
, perf
, 1, 0 );
1106 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1113 vg_uictx
.cursor
[1] += 1;
1117 #endif /* VG_AUDIO_H */