1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
7 #include "vg/vg_stdint.h"
8 #include "vg/vg_platform.h"
12 #include "vg/vg_console.h"
13 #include "vg/vg_store.h"
19 #pragma GCC push_options
20 #pragma GCC optimize ("O3")
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
26 #define STB_VORBIS_MAX_CHANNELS 2
27 #include "submodules/stb/stb_vorbis.c"
34 #pragma GCC pop_options
35 #pragma GCC diagnostic pop
39 #define SFX_MAX_SYSTEMS 32
40 #define AUDIO_FLAG_LOOP 0x1
41 #define AUDIO_FLAG_ONESHOT 0x2
42 #define AUDIO_FLAG_SPACIAL_3D 0x4
43 #define AUDIO_FLAG_AUTO_START 0x8
44 #define AUDIO_FLAG_KILL 0x10
46 #define FADEOUT_LENGTH 1100
47 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
49 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
51 enum audio_source_mode
54 k_audio_source_compressed
,
57 typedef struct audio_clip audio_clip
;
61 enum audio_source_mode source_mode
;
67 typedef struct audio_mix_info audio_mix_info
;
77 typedef struct audio_player audio_player
;
80 aatree_ptr active_entity
; /* non-nil if currently playing */
88 typedef struct audio_entity audio_entity
;
97 u32 fadeout
, fadeout_current
;
104 * play again: if already playing, leave in queue while it fadeouts
105 * oneshot: create a ghost entity
109 static struct vg_audio_system
112 ma_device miniaudio_device
;
113 ma_device_config miniaudio_dconfig
;
115 SDL_AudioDeviceID sdl_output_device
;
124 SDL_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
];
148 int debug_ui
, debug_ui_3d
;
155 static struct vg_profile
156 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
157 .name
= "[T2] audio_decode()"},
158 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
159 .name
= "[T2] audio_mix()"},
160 vg_prof_audio_decode
,
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 VG_STATIC
int audio_lock_checker_load(void)
170 SDL_LockMutex( vg_audio
.mux_checker
);
171 value
= vg_audio
.sync_locked
;
172 SDL_UnlockMutex( vg_audio
.mux_checker
);
176 VG_STATIC
void audio_lock_checker_store( int value
)
178 SDL_LockMutex( vg_audio
.mux_checker
);
179 vg_audio
.sync_locked
= value
;
180 SDL_UnlockMutex( vg_audio
.mux_checker
);
183 VG_STATIC
void audio_require_lock(void)
185 if( audio_lock_checker_load() )
188 vg_error( "Modifying sound effects systems requires locking\n" );
192 VG_STATIC
void audio_lock(void)
194 SDL_LockMutex( vg_audio
.mux_sync
);
195 audio_lock_checker_store(1);
198 VG_STATIC
void audio_unlock(void)
200 audio_lock_checker_store(0);
201 SDL_UnlockMutex( vg_audio
.mux_sync
);
204 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int frame_count
);
205 VG_STATIC
void vg_audio_init(void)
207 vg_audio
.mux_checker
= SDL_CreateMutex();
208 vg_audio
.mux_sync
= SDL_CreateMutex();
210 /* TODO: Move here? */
211 vg_convar_push( (struct vg_convar
){
212 .name
= "debug_audio",
213 .data
= &vg_audio
.debug_ui
,
214 .data_type
= k_convar_dtype_i32
,
215 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
219 /* allocate memory */
222 vg_audio
.audio_pool
=
223 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32,
227 u32 decode_size
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
228 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
231 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
232 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
234 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
235 vg_audio
.active_pool_info
.p_cmp
= NULL
;
236 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
238 SDL_AudioSpec spec_desired
, spec_got
;
239 spec_desired
.callback
= audio_mixer_callback
;
240 spec_desired
.channels
= 2;
241 spec_desired
.format
= AUDIO_F32
;
242 spec_desired
.freq
= 44100;
243 spec_desired
.padding
= 0;
244 spec_desired
.samples
= 512;
245 spec_desired
.silence
= 0;
246 spec_desired
.size
= 0;
247 spec_desired
.userdata
= NULL
;
249 vg_audio
.sdl_output_device
=
250 SDL_OpenAudioDevice( NULL
, 0, &spec_desired
, &spec_got
,
251 SDL_AUDIO_ALLOW_SAMPLES_CHANGE
);
253 if( vg_audio
.sdl_output_device
)
255 SDL_PauseAudioDevice( vg_audio
.sdl_output_device
, 0 );
260 "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
261 " Frequency: 44100 hz\n"
262 " Buffer size: 512\n"
264 " Format: s16 or f32\n" );
267 vg_success( "Ready\n" );
270 VG_STATIC
void vg_audio_free(void * nothing
)
272 SDL_CloseAudioDevice( vg_audio
.sdl_output_device
);
279 static aatree_ptr
audio_alloc_entity_internal(void)
281 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
282 &vg_audio
.active_pool_head
);
284 if( playerid
== AATREE_PTR_NIL
)
285 return AATREE_PTR_NIL
;
287 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
293 VG_STATIC
void audio_entity_free_internal( aatree_ptr id
)
295 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
298 /* Notify player that we've finished */
299 if( aap
->ent
.player
)
300 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
303 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
304 &vg_audio
.active_pool_head
);
307 VG_STATIC
void *audio_entity_vorbis_ptr( aatree_ptr entid
)
309 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
310 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
315 VG_STATIC
void audio_entity_start( audio_entity
*src
)
317 aatree_ptr entid
= audio_alloc_entity_internal();
318 if( entid
== AATREE_PTR_NIL
)
321 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
323 ent
->info
= src
->info
;
324 ent
->name
= src
->info
.source
->path
;
326 ent
->player
= src
->player
;
329 ent
->fadeout_current
= 0;
331 /* Notify main player we are dequeud and playing */
334 src
->player
->enqued
= 0;
335 src
->player
->active_entity
= entid
;
338 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
340 /* Setup vorbis decoder */
341 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
343 stb_vorbis_alloc alloc
= {
344 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
345 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
349 stb_vorbis
*decoder
= stb_vorbis_open_memory(
350 src
->info
.source
->data
,
351 src
->info
.source
->size
, &err
, &alloc
);
355 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
356 src
->info
.source
->path
, err
);
358 audio_entity_free_internal( entid
);
363 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
366 aap
->vorbis_handle
= decoder
;
370 ent
->length
= src
->info
.source
->size
;
375 * Read everything from the queue
377 VG_STATIC
void audio_system_enque(void)
379 /* Process incoming sound queue */
383 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
385 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
390 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
392 audio_entity_start( src
);
396 /* Otherwise try start fadeout but dont remove from queue */
398 aatree_ptr entid
= src
->player
->active_entity
;
399 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
402 ent
->fadeout
= FADEOUT_LENGTH
;
403 ent
->fadeout_current
= FADEOUT_LENGTH
;
406 vg_audio
.entity_queue
[ wr
++ ] = *src
;
411 audio_entity_start( src
);
415 vg_audio
.queue_len
= wr
;
417 /* Localize others memory */
418 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
420 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
424 if( aap
->ent
.player
)
426 /* Only copy information in whilst not requeing */
427 if( aap
->ent
.player
->enqued
== 0 )
429 aap
->ent
.info
= aap
->ent
.player
->info
;
431 if( (aap
->ent
.info
.flags
& AUDIO_FLAG_KILL
) && !aap
->ent
.fadeout
)
433 aap
->ent
.fadeout
= FADEOUT_LENGTH
;
434 aap
->ent
.fadeout_current
= FADEOUT_LENGTH
;
444 * Redistribute sound systems
446 VG_STATIC
void audio_system_cleanup(void)
450 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
452 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
455 audio_entity
*src
= &aap
->ent
;
456 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
462 audio_entity_free_internal( i
);
471 * Get effective volume and pan from this entity
473 VG_STATIC
void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
475 if( ent
->info
.vol
< 0.01f
)
477 *vol
= ent
->info
.vol
;
483 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
485 float dist2
= v3_length2( delta
);
487 if( dist2
< 0.0001f
)
494 float dist
= sqrtf( dist2
),
495 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
497 v3_muls( delta
, 1.0f
/dist
, delta
);
498 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
499 *vol
= 1.0f
/(attn
*attn
);
503 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
505 for( u32 i
=0; i
<count
; i
++ )
507 dst
[ i
*2 + 0 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
508 dst
[ i
*2 + 1 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
513 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
516 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
520 c
= VG_MIN( 1, f
->channels
- 1 );
524 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
529 for( int j
=0; j
< k
; ++j
)
531 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
532 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
536 f
->channel_buffer_start
+= k
;
541 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
549 * ........ more wrecked code sorry!
552 stb_vorbis_get_samples_i16_downmixed( stb_vorbis
*f
, i16
*buffer
, int len
)
555 c
= VG_MIN( 1, f
->channels
- 1 );
559 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
564 for( int j
=0; j
< k
; ++j
)
566 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
567 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
569 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
570 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
574 f
->channel_buffer_start
+= k
;
579 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
586 VG_STATIC
void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
588 vg_profile_begin( &_vg_prof_audio_decode
);
590 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
591 audio_entity
*ent
= &aap
->ent
;
593 u32 remaining
= count
;
594 u32 cursor
= ent
->cur
;
599 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
600 remaining
-= samples_this_run
;
602 float *dst
= &buf
[ buffer_pos
* 2 ];
604 int source_mode
= ent
->info
.source
->source_mode
;
606 if( source_mode
== k_audio_source_mono
)
608 i16
*src_buffer
= ent
->info
.source
->data
,
609 *src
= &src_buffer
[cursor
];
611 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
613 else if( source_mode
== k_audio_source_compressed
)
615 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
620 if( read_samples
!= samples_this_run
)
622 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
626 cursor
+= samples_this_run
;
627 buffer_pos
+= samples_this_run
;
629 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
631 if( source_mode
== k_audio_source_compressed
)
633 stb_vorbis_seek_start( aap
->vorbis_handle
);
645 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
646 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
653 vg_profile_end( &_vg_prof_audio_decode
);
656 VG_STATIC
void audio_entity_mix( aatree_ptr id
, float *buffer
,
659 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
661 u32 cursor
= ent
->cur
, buffer_pos
= 0;
662 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
664 u32 frames_write
= frame_count
;
665 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
667 float vol
= ent
->info
.vol
,
670 audio_entity_get_samples( id
, frame_count
, pcf
);
672 vg_profile_begin( &_vg_prof_audio_mix
);
674 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
675 audio_entity_spacialize( ent
, &vol
, &pan
);
677 for( u32 j
=0; j
<frame_count
; j
++ )
679 float frame_vol
= vol
;
683 /* Force this system to be removed now */
684 if( ent
->fadeout_current
== 0 )
686 ent
->info
.flags
= 0x00;
687 ent
->cur
= ent
->length
;
691 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
692 ent
->fadeout_current
--;
698 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
699 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
704 vg_profile_end( &_vg_prof_audio_mix
);
707 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int byte_count
)
709 audio_system_enque();
711 int frame_count
= byte_count
/(2*sizeof(float));
714 float *pOut32F
= (float *)stream
;
715 for( int i
=0; i
<frame_count
*2; i
++ )
719 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
721 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
725 audio_entity_mix( i
, pOut32F
, frame_count
);
730 audio_system_cleanup();
733 vg_profile_increment( &_vg_prof_audio_decode
);
734 vg_profile_increment( &_vg_prof_audio_mix
);
736 vg_prof_audio_mix
= _vg_prof_audio_mix
;
737 vg_prof_audio_decode
= _vg_prof_audio_decode
;
739 vg_audio
.samples_last
= frame_count
;
743 VG_STATIC
void audio_clip_load( audio_clip
*clip
, void *lin_alloc
)
745 if( lin_alloc
== NULL
)
746 lin_alloc
= vg_audio
.audio_pool
;
748 if( clip
->source_mode
== k_audio_source_mono
)
750 vg_linear_clear( vg_mem
.scratch
);
753 stb_vorbis_alloc alloc
= {
754 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
755 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
758 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
761 stb_vorbis
*decoder
= stb_vorbis_open_memory(
762 filedata
, fsize
, &err
, &alloc
);
766 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
768 vg_fatal_exit_loop( "Vorbis decode error" );
771 /* only mono is supported in uncompressed */
772 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
773 data_size
= length_samples
* sizeof(i16
);
776 clip
->data
= vg_linear_alloc( lin_alloc
, data_size
);
777 clip
->size
= length_samples
;
780 int read_samples
= stb_vorbis_get_samples_i16_downmixed(
781 decoder
, clip
->data
, length_samples
);
783 if( read_samples
!= length_samples
)
784 vg_fatal_exit_loop( "Decode error" );
786 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
787 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip
->path
, mb
,
791 /* load in directly */
792 else if( clip
->source_mode
== k_audio_source_compressed
)
795 clip
->data
= vg_file_read( lin_alloc
, clip
->path
, &clip
->size
);
799 vg_fatal_exit_loop( "Audio failed to load" );
801 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
802 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
806 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
)
808 for( int i
=0; i
<count
; i
++ )
809 audio_clip_load( &arr
[i
], lin_alloc
);
812 /* Mark change to be uploaded through queue system */
813 VG_STATIC
void audio_player_commit( audio_player
*sys
)
815 audio_require_lock();
817 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
819 vg_warn( "Audio commit queue full\n" );
825 vg_warn( "[2] Audio commit spamming; already enqued (%s)\n", sys
->name
);
830 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
831 ent
->info
= sys
->info
;
835 VG_STATIC
void audio_require_init( audio_player
*player
)
840 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
843 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
845 if( clip
->data
&& clip
->size
)
848 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
851 /* Play a clip using player. If its already playing something, it will
852 * fadeout quickly and start the next sound */
853 VG_STATIC
void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
855 audio_require_lock();
856 audio_require_init( player
);
857 audio_require_clip_loaded( clip
);
859 if( player
->info
.flags
& AUDIO_FLAG_KILL
)
861 vg_error( "Can't start audio clip on player that is/has disconnected" );
867 vg_warn( "[1] Audio commit spamming; already enqued (%s)\n",
872 player
->info
.source
= clip
;
873 audio_player_commit( player
);
876 VG_STATIC
void audio_play_oneshot( audio_clip
*clip
, float volume
)
878 audio_require_lock();
879 audio_require_clip_loaded( clip
);
881 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
883 vg_warn( "Audio commit queue full\n" );
887 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
889 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
890 ent
->info
.pan
= 0.0f
;
891 ent
->info
.source
= clip
;
892 ent
->info
.vol
= volume
;
896 VG_STATIC
void audio_player_init( audio_player
*player
)
898 player
->active_entity
= AATREE_PTR_NIL
;
907 * Safety enforced Get/set attributes
910 VG_STATIC
int audio_player_is_playing( audio_player
*sys
)
912 audio_require_lock();
914 if( sys
->active_entity
!= AATREE_PTR_NIL
)
920 VG_STATIC
void audio_player_set_position( audio_player
*sys
, v3f pos
)
922 audio_require_lock();
923 v3_copy( pos
, sys
->info
.world_position
);
926 VG_STATIC
void audio_player_set_vol( audio_player
*sys
, float vol
)
928 audio_require_lock();
932 VG_STATIC
float audio_player_get_vol( audio_player
*sys
)
934 audio_require_lock();
935 return sys
->info
.vol
;
938 VG_STATIC
void audio_player_set_pan( audio_player
*sys
, float pan
)
940 audio_require_lock();
944 VG_STATIC
float audio_player_get_pan( audio_player
*sys
)
946 audio_require_lock();
947 return sys
->info
.pan
;
950 VG_STATIC
void audio_player_set_flags( audio_player
*sys
, u32 flags
)
952 audio_require_lock();
953 sys
->info
.flags
= flags
;
956 VG_STATIC u32
audio_player_get_flags( audio_player
*sys
)
958 audio_require_lock();
959 return sys
->info
.flags
;
967 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
969 if( !vg_audio
.debug_ui
)
976 u32 cursor
, flags
, length
;
980 infos
[ SFX_MAX_SYSTEMS
];
985 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
987 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
992 audio_entity
*ent
= &aap
->ent
;
993 struct sound_info
*snd
= &infos
[ num_systems
++ ];
995 snd
->name
= ent
->name
;
996 snd
->cursor
= ent
->cur
;
997 snd
->flags
= ent
->info
.flags
;
998 snd
->length
= ent
->length
;
999 snd
->vol
= ent
->info
.vol
*100.0f
;
1000 v3_copy( ent
->info
.world_position
, snd
->pos
);
1003 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1004 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1005 &vg_prof_audio_mix
}, 2,
1006 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1014 vg_uictx
.cursor
[0] = 258;
1015 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1016 vg_uictx
.cursor
[2] = 150;
1017 vg_uictx
.cursor
[3] = 12;
1019 float mb1
= 1024.0f
*1024.0f
,
1020 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1021 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1022 percent
= (usage
/total
) * 100.0f
;
1024 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1026 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1027 vg_uictx
.cursor
[1] += 20;
1029 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1030 u32 overlap_length
= 0;
1032 /* Draw audio stack */
1033 for( int i
=0; i
<num_systems
; i
++ )
1035 struct sound_info
*inf
= &infos
[i
];
1037 vg_uictx
.cursor
[2] = 200;
1038 vg_uictx
.cursor
[3] = 18;
1040 u32 alpha
= 0xa0000000;
1044 ui_fill_rect( vg_uictx
.cursor
, 0x00333333|alpha
);
1046 ui_px baseline
= vg_uictx
.cursor
[0],
1048 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1051 vg_uictx
.cursor
[2] = 2;
1052 vg_uictx
.cursor
[0] = c
;
1053 ui_fill_rect( vg_uictx
.cursor
, 0xffffffff );
1055 vg_uictx
.cursor
[0] = baseline
+ 2;
1056 vg_uictx
.cursor
[1] += 2;
1057 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1058 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1060 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1063 v3_copy( inf
->pos
, wpos
);
1065 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1067 if( wpos
[3] < 0.0f
)
1068 goto projected_behind
;
1070 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1071 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1074 wr
[0] = wpos
[0] * vg
.window_x
;
1075 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1079 for( int j
=0; j
<12; j
++ )
1082 for( int k
=0; k
<overlap_length
; k
++ )
1084 ui_px
*wk
= overlap_buffer
[k
];
1085 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1086 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1099 ui_text( wr
, perf
, 1, 0 );
1101 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1108 vg_uictx
.cursor
[1] += 1;
1112 #endif /* VG_AUDIO_H */