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
51 #define AUDIO_FLAG_AUTO_START 0x8
52 #define AUDIO_FLAG_KILL 0x10
54 #define FADEOUT_LENGTH 1100
55 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
57 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
59 enum audio_source_mode
62 k_audio_source_compressed
,
65 typedef struct audio_clip audio_clip
;
69 enum audio_source_mode source_mode
;
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
;
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
];
153 int debug_ui
, debug_ui_3d
;
160 static struct vg_profile
161 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
162 .name
= "[T2] audio_decode()"},
163 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
164 .name
= "[T2] audio_mix()"},
165 vg_prof_audio_decode
,
169 * These functions are called from the main thread and used to prevent bad
170 * access. TODO: They should be no-ops in release builds.
172 VG_STATIC
int audio_lock_checker_load(void)
175 vg_mutex_lock( &vg_audio
.mux_checker
);
176 value
= vg_audio
.sync_locked
;
177 vg_mutex_unlock( &vg_audio
.mux_checker
);
181 VG_STATIC
void audio_lock_checker_store( int value
)
183 vg_mutex_lock( &vg_audio
.mux_checker
);
184 vg_audio
.sync_locked
= value
;
185 vg_mutex_unlock( &vg_audio
.mux_checker
);
188 VG_STATIC
void audio_require_lock(void)
190 if( audio_lock_checker_load() )
193 vg_error( "Modifying sound effects systems requires locking\n" );
197 VG_STATIC
void audio_lock(void)
199 vg_mutex_lock( &vg_audio
.mux_sync
);
200 audio_lock_checker_store(1);
203 VG_STATIC
void audio_unlock(void)
205 audio_lock_checker_store(0);
206 vg_mutex_unlock( &vg_audio
.mux_sync
);
210 VG_STATIC
void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
211 const void *pInput
, ma_uint32 frameCount
);
213 VG_STATIC
void vg_audio_init(void)
215 vg_mutex_init( &vg_audio
.mux_checker
);
216 vg_mutex_init( &vg_audio
.mux_sync
);
218 /* TODO: Move here? */
219 vg_convar_push( (struct vg_convar
){
220 .name
= "debug_audio",
221 .data
= &vg_audio
.debug_ui
,
222 .data_type
= k_convar_dtype_i32
,
223 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
227 /* allocate memory */
230 vg_audio
.audio_pool
=
231 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32 );
234 u32 decode_size
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
235 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
238 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
239 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
241 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
242 vg_audio
.active_pool_info
.p_cmp
= NULL
;
243 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
245 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
246 ma_device
*device
= &vg_audio
.miniaudio_device
;
248 *dconf
= ma_device_config_init( ma_device_type_playback
);
249 dconf
->playback
.format
= ma_format_f32
;
250 dconf
->playback
.channels
= 2;
251 dconf
->sampleRate
= 44100;
252 dconf
->dataCallback
= audio_mixer_callback
;
253 dconf
->periodSizeInFrames
= 441;
255 dconf
->pUserData
= NULL
;
257 vg_info( "Starting audio engine\n" );
259 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
261 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
265 if( ma_device_start( device
) != MA_SUCCESS
)
267 ma_device_uninit( device
);
268 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
272 vg_success( "Ready\n" );
275 VG_STATIC
void vg_audio_free(void * nothing
)
277 ma_device
*device
= &vg_audio
.miniaudio_device
;
278 ma_device_uninit( device
);
281 vg_free( vg_audio
.mem
);
290 static aatree_ptr
audio_alloc_entity_internal(void)
292 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
293 &vg_audio
.active_pool_head
);
295 if( playerid
== AATREE_PTR_NIL
)
296 return AATREE_PTR_NIL
;
298 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
304 VG_STATIC
void audio_entity_free_internal( aatree_ptr id
)
306 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
309 /* Notify player that we've finished */
310 if( aap
->ent
.player
)
311 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
314 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
315 &vg_audio
.active_pool_head
);
318 VG_STATIC
void *audio_entity_vorbis_ptr( aatree_ptr entid
)
320 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
321 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
326 VG_STATIC
void audio_entity_start( audio_entity
*src
)
328 aatree_ptr entid
= audio_alloc_entity_internal();
329 if( entid
== AATREE_PTR_NIL
)
332 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
334 ent
->info
= src
->info
;
335 ent
->name
= src
->info
.source
->path
;
337 ent
->player
= src
->player
;
340 ent
->fadeout_current
= 0;
342 /* Notify main player we are dequeud and playing */
345 src
->player
->enqued
= 0;
346 src
->player
->active_entity
= entid
;
349 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
351 /* Setup vorbis decoder */
352 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
354 stb_vorbis_alloc alloc
= {
355 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
356 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
360 stb_vorbis
*decoder
= stb_vorbis_open_memory(
361 src
->info
.source
->data
,
362 src
->info
.source
->size
, &err
, &alloc
);
366 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
367 src
->info
.source
->path
, err
);
369 audio_entity_free_internal( entid
);
374 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
377 aap
->vorbis_handle
= decoder
;
381 ent
->length
= src
->info
.source
->size
;
386 * Read everything from the queue
388 VG_STATIC
void audio_system_enque(void)
390 /* Process incoming sound queue */
394 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
396 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
401 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
403 audio_entity_start( src
);
407 /* Otherwise try start fadeout but dont remove from queue */
409 aatree_ptr entid
= src
->player
->active_entity
;
410 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
413 ent
->fadeout
= FADEOUT_LENGTH
;
414 ent
->fadeout_current
= FADEOUT_LENGTH
;
417 vg_audio
.entity_queue
[ wr
++ ] = *src
;
422 audio_entity_start( src
);
426 vg_audio
.queue_len
= wr
;
428 /* Localize others memory */
429 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
431 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
435 if( aap
->ent
.player
)
437 /* Only copy information in whilst not requeing */
438 if( aap
->ent
.player
->enqued
== 0 )
440 aap
->ent
.info
= aap
->ent
.player
->info
;
442 if( (aap
->ent
.info
.flags
& AUDIO_FLAG_KILL
) && !aap
->ent
.fadeout
)
444 aap
->ent
.fadeout
= FADEOUT_LENGTH
;
445 aap
->ent
.fadeout_current
= FADEOUT_LENGTH
;
455 * Redistribute sound systems
457 VG_STATIC
void audio_system_cleanup(void)
461 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
463 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
466 audio_entity
*src
= &aap
->ent
;
467 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
473 audio_entity_free_internal( i
);
482 * Get effective volume and pan from this entity
484 VG_STATIC
void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
486 if( ent
->info
.vol
< 0.01f
)
488 *vol
= ent
->info
.vol
;
494 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
496 float dist2
= v3_length2( delta
);
498 if( dist2
< 0.0001f
)
505 float dist
= sqrtf( dist2
),
506 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
508 v3_muls( delta
, 1.0f
/dist
, delta
);
509 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
510 *vol
= 1.0f
/(attn
*attn
);
514 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
516 for( u32 i
=0; i
<count
; i
++ )
518 dst
[ i
*2 + 0 ] = (float)src
[i
] * (1.0f
/32767.0f
);
519 dst
[ i
*2 + 1 ] = (float)src
[i
] * (1.0f
/32767.0f
);
524 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
527 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
531 c
= VG_MIN( 1, f
->channels
- 1 );
535 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
540 for( int j
=0; j
< k
; ++j
)
542 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
543 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
547 f
->channel_buffer_start
+= k
;
552 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
560 * ........ more wrecked code sorry!
563 stb_vorbis_get_samples_i16_interleaved_mono( stb_vorbis
*f
, i16
*buffer
,
567 c
= VG_MIN( 1, f
->channels
- 1 );
571 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
576 for( int j
=0; j
< k
; ++j
)
578 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
579 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
581 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
582 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
586 f
->channel_buffer_start
+= k
;
591 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
598 VG_STATIC
void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
600 vg_profile_begin( &_vg_prof_audio_decode
);
602 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
603 audio_entity
*ent
= &aap
->ent
;
605 u32 remaining
= count
;
606 u32 cursor
= ent
->cur
;
611 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
612 remaining
-= samples_this_run
;
614 float *dst
= &buf
[ buffer_pos
* 2 ];
616 int source_mode
= ent
->info
.source
->source_mode
;
618 if( source_mode
== k_audio_source_mono
)
620 i16
*src
= &((i16
*)ent
->info
.source
->data
)[ cursor
];
621 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
623 else if( source_mode
== k_audio_source_compressed
)
625 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
630 if( read_samples
!= samples_this_run
)
632 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
636 cursor
+= samples_this_run
;
637 buffer_pos
+= samples_this_run
;
639 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
641 if( source_mode
== k_audio_source_compressed
)
643 stb_vorbis_seek_start( aap
->vorbis_handle
);
655 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
656 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
663 vg_profile_end( &_vg_prof_audio_decode
);
666 VG_STATIC
void audio_entity_mix( aatree_ptr id
, float *buffer
,
669 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
671 u32 cursor
= ent
->cur
, buffer_pos
= 0;
672 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
674 u32 frames_write
= frame_count
;
675 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
677 float vol
= ent
->info
.vol
,
680 audio_entity_get_samples( id
, frame_count
, pcf
);
682 vg_profile_begin( &_vg_prof_audio_mix
);
684 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
685 audio_entity_spacialize( ent
, &vol
, &pan
);
687 for( u32 j
=0; j
<frame_count
; j
++ )
689 float frame_vol
= vol
;
693 /* Force this system to be removed now */
694 if( ent
->fadeout_current
== 0 )
696 ent
->info
.flags
= 0x00;
697 ent
->cur
= ent
->length
;
701 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
702 ent
->fadeout_current
--;
708 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
709 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
714 vg_profile_end( &_vg_prof_audio_mix
);
718 * callback from miniaudio.h interface
720 VG_STATIC
void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
721 const void *pInput
, ma_uint32 frame_count
)
723 struct timespec time_start
, time_end
;
724 clock_gettime( CLOCK_REALTIME
, &time_start
);
726 audio_system_enque();
729 float *pOut32F
= (float *)pOutBuf
;
730 for( int i
=0; i
<frame_count
*2; i
++ )
734 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
736 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
740 audio_entity_mix( i
, pOut32F
, frame_count
);
745 audio_system_cleanup();
747 /* TODO: what the hell is this? */
752 vg_profile_increment( &_vg_prof_audio_decode
);
753 vg_profile_increment( &_vg_prof_audio_mix
);
755 vg_prof_audio_mix
= _vg_prof_audio_mix
;
756 vg_prof_audio_decode
= _vg_prof_audio_decode
;
758 vg_audio
.samples_last
= frame_count
;
762 VG_STATIC
void audio_clip_load( audio_clip
*clip
)
764 if( clip
->source_mode
== k_audio_source_mono
)
766 vg_linear_clear( vg_mem
.scratch
);
768 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
770 stb_vorbis_alloc alloc
= {
771 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
772 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
776 stb_vorbis
*decoder
= stb_vorbis_open_memory(
777 filedata
, fsize
, &err
, &alloc
);
781 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
783 vg_fatal_exit_loop( "Vorbis decode error" );
786 /* only mono is supported in uncompressed */
787 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
788 data_size
= length_samples
* sizeof(i16
);
790 clip
->data
= vg_linear_alloc( vg_audio
.audio_pool
, data_size
);
791 clip
->size
= length_samples
;
793 int read_samples
= stb_vorbis_get_samples_i16_interleaved_mono(
794 decoder
, clip
->data
, length_samples
);
796 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
797 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
800 /* load in directly */
801 else if( clip
->source_mode
== k_audio_source_compressed
)
803 clip
->data
= vg_file_read( vg_audio
.audio_pool
, clip
->path
, &clip
->size
);
806 vg_fatal_exit_loop( "Audio failed to load" );
808 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
809 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
813 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
)
815 for( int i
=0; i
<count
; i
++ )
816 audio_clip_load( &arr
[i
] );
819 /* Mark change to be uploaded through queue system */
820 VG_STATIC
void audio_player_commit( audio_player
*sys
)
822 audio_require_lock();
824 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
826 vg_warn( "Audio commit queue full\n" );
832 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
837 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
838 ent
->info
= sys
->info
;
842 VG_STATIC
void audio_require_init( audio_player
*player
)
847 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
850 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
852 if( clip
->data
&& clip
->size
)
855 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
858 /* Play a clip using player. If its already playing something, it will
859 * fadeout quickly and start the next sound */
860 VG_STATIC
void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
862 audio_require_lock();
863 audio_require_init( player
);
864 audio_require_clip_loaded( clip
);
866 if( player
->info
.flags
& AUDIO_FLAG_KILL
)
868 vg_error( "Can't start audio clip on player that is/has disconnected" );
872 player
->info
.source
= clip
;
873 audio_player_commit( player
);
877 VG_STATIC
void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
879 audio_require_lock();
880 audio_require_init( player
);
884 VG_STATIC
void audio_play_oneshot( audio_clip
*clip
, float volume
)
886 audio_require_lock();
887 audio_require_clip_loaded( clip
);
889 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
891 vg_warn( "Audio commit queue full\n" );
895 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
897 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
898 ent
->info
.pan
= 0.0f
;
899 ent
->info
.source
= clip
;
900 ent
->info
.vol
= volume
;
904 VG_STATIC
void audio_player_init( audio_player
*player
)
906 player
->active_entity
= AATREE_PTR_NIL
;
915 * Safety enforced Get/set attributes
918 VG_STATIC
int audio_player_is_playing( audio_player
*sys
)
920 audio_require_lock();
922 if( sys
->active_entity
!= AATREE_PTR_NIL
)
928 VG_STATIC
void audio_player_set_position( audio_player
*sys
, v3f pos
)
930 audio_require_lock();
931 v3_copy( pos
, sys
->info
.world_position
);
934 VG_STATIC
void audio_player_set_vol( audio_player
*sys
, float vol
)
936 audio_require_lock();
940 VG_STATIC
float audio_player_get_vol( audio_player
*sys
)
942 audio_require_lock();
943 return sys
->info
.vol
;
946 VG_STATIC
void audio_player_set_pan( audio_player
*sys
, float pan
)
948 audio_require_lock();
952 VG_STATIC
float audio_player_get_pan( audio_player
*sys
)
954 audio_require_lock();
955 return sys
->info
.pan
;
958 VG_STATIC
void audio_player_set_flags( audio_player
*sys
, u32 flags
)
960 audio_require_lock();
961 sys
->info
.flags
= flags
;
964 VG_STATIC u32
audio_player_get_flags( audio_player
*sys
)
966 audio_require_lock();
967 return sys
->info
.flags
;
975 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
977 if( !vg_audio
.debug_ui
)
984 u32 cursor
, flags
, length
;
988 infos
[ SFX_MAX_SYSTEMS
];
993 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
995 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1000 audio_entity
*ent
= &aap
->ent
;
1001 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1003 snd
->name
= ent
->name
;
1004 snd
->cursor
= ent
->cur
;
1005 snd
->flags
= ent
->info
.flags
;
1006 snd
->length
= ent
->length
;
1007 snd
->vol
= ent
->info
.vol
*100.0f
;
1008 v3_copy( ent
->info
.world_position
, snd
->pos
);
1011 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1012 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1013 &vg_prof_audio_mix
}, 2,
1014 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1022 vg_uictx
.cursor
[0] = 258;
1023 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1024 vg_uictx
.cursor
[2] = 150;
1025 vg_uictx
.cursor
[3] = 12;
1027 float mb1
= 1024.0f
*1024.0f
,
1028 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1029 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1030 percent
= (usage
/total
) * 100.0f
;
1032 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1034 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1035 vg_uictx
.cursor
[1] += 20;
1037 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1038 u32 overlap_length
= 0;
1040 /* Draw audio stack */
1041 for( int i
=0; i
<num_systems
; i
++ )
1043 struct sound_info
*inf
= &infos
[i
];
1045 vg_uictx
.cursor
[2] = 200;
1046 vg_uictx
.cursor
[3] = 18;
1048 u32 alpha
= 0xa0000000;
1052 ui_fill_rect( vg_uictx
.cursor
, 0x00333333|alpha
);
1054 ui_px baseline
= vg_uictx
.cursor
[0],
1056 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1059 vg_uictx
.cursor
[2] = 2;
1060 vg_uictx
.cursor
[0] = c
;
1061 ui_fill_rect( vg_uictx
.cursor
, 0xffffffff );
1063 vg_uictx
.cursor
[0] = baseline
+ 2;
1064 vg_uictx
.cursor
[1] += 2;
1065 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1066 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1068 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1071 v3_copy( inf
->pos
, wpos
);
1073 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1075 if( wpos
[3] < 0.0f
)
1076 goto projected_behind
;
1078 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1079 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1082 wr
[0] = wpos
[0] * vg
.window_x
;
1083 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1087 for( int j
=0; j
<12; j
++ )
1090 for( int k
=0; k
<overlap_length
; k
++ )
1092 ui_px
*wk
= overlap_buffer
[k
];
1093 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1094 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1107 ui_text( wr
, perf
, 1, 0 );
1109 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1116 vg_uictx
.cursor
[1] += 1;
1120 #endif /* VG_AUDIO_H */