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"
32 #pragma GCC push_options
33 #pragma GCC optimize ("O3")
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40 #define STB_VORBIS_MAX_CHANNELS 2
41 #include "stb/stb_vorbis.h"
43 #pragma GCC diagnostic pop
47 #pragma GCC pop_options
51 #define SFX_MAX_SYSTEMS 32
52 #define AUDIO_FLAG_LOOP 0x1
53 #define AUDIO_FLAG_ONESHOT 0x2
54 #define AUDIO_FLAG_SPACIAL_3D 0x4
55 #define AUDIO_FLAG_AUTO_START 0x8
56 #define AUDIO_FLAG_KILL 0x10
58 #define FADEOUT_LENGTH 1100
59 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
61 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
63 enum audio_source_mode
66 k_audio_source_compressed
,
69 typedef struct audio_clip audio_clip
;
73 enum audio_source_mode source_mode
;
79 typedef struct audio_mix_info audio_mix_info
;
89 typedef struct audio_player audio_player
;
92 aatree_ptr active_entity
; /* non-nil if currently playing */
100 typedef struct audio_entity audio_entity
;
103 audio_player
*player
;
109 u32 fadeout
, fadeout_current
;
116 * play again: if already playing, leave in queue while it fadeouts
117 * oneshot: create a ghost entity
121 static struct vg_audio_system
123 ma_device miniaudio_device
;
124 ma_device_config miniaudio_dconfig
;
133 vg_mutex mux_checker
,
136 /* Audio engine, thread 1 */
137 struct active_audio_player
143 aatree_pool_node pool_node
;
146 stb_vorbis
*vorbis_handle
;
147 stb_vorbis_alloc vorbis_alloc
;
149 active_players
[ SFX_MAX_SYSTEMS
];
151 aatree active_pool_info
; /* note: just using the pool */
152 aatree_ptr active_pool_head
;
154 /* System queue, and access from thread 0 */
155 audio_entity entity_queue
[SFX_MAX_SYSTEMS
];
157 int debug_ui
, debug_ui_3d
;
164 static struct vg_profile
165 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
166 .name
= "[T2] audio_decode()"},
167 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
168 .name
= "[T2] audio_mix()"},
169 vg_prof_audio_decode
,
173 * These functions are called from the main thread and used to prevent bad
174 * access. TODO: They should be no-ops in release builds.
176 VG_STATIC
int audio_lock_checker_load(void)
179 vg_mutex_lock( &vg_audio
.mux_checker
);
180 value
= vg_audio
.sync_locked
;
181 vg_mutex_unlock( &vg_audio
.mux_checker
);
185 VG_STATIC
void audio_lock_checker_store( int value
)
187 vg_mutex_lock( &vg_audio
.mux_checker
);
188 vg_audio
.sync_locked
= value
;
189 vg_mutex_unlock( &vg_audio
.mux_checker
);
192 VG_STATIC
void audio_require_lock(void)
194 if( audio_lock_checker_load() )
197 vg_error( "Modifying sound effects systems requires locking\n" );
201 VG_STATIC
void audio_lock(void)
203 vg_mutex_lock( &vg_audio
.mux_sync
);
204 audio_lock_checker_store(1);
207 VG_STATIC
void audio_unlock(void)
209 audio_lock_checker_store(0);
210 vg_mutex_unlock( &vg_audio
.mux_sync
);
214 VG_STATIC
void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
215 const void *pInput
, ma_uint32 frameCount
);
217 VG_STATIC
void vg_audio_init(void)
219 vg_mutex_init( &vg_audio
.mux_checker
);
220 vg_mutex_init( &vg_audio
.mux_sync
);
222 /* TODO: Move here? */
223 vg_convar_push( (struct vg_convar
){
224 .name
= "debug_audio",
225 .data
= &vg_audio
.debug_ui
,
226 .data_type
= k_convar_dtype_i32
,
227 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
231 /* allocate memory */
234 vg_audio
.audio_pool
=
235 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32,
239 u32 decode_size
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
240 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
243 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
244 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
246 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
247 vg_audio
.active_pool_info
.p_cmp
= NULL
;
248 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
250 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
251 ma_device
*device
= &vg_audio
.miniaudio_device
;
253 *dconf
= ma_device_config_init( ma_device_type_playback
);
254 dconf
->playback
.format
= ma_format_f32
;
255 dconf
->playback
.channels
= 2;
256 dconf
->sampleRate
= 44100;
257 dconf
->dataCallback
= audio_mixer_callback
;
258 dconf
->periodSizeInFrames
= 441;
260 dconf
->pUserData
= NULL
;
262 vg_info( "Starting audio engine\n" );
264 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
266 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
270 if( ma_device_start( device
) != MA_SUCCESS
)
272 ma_device_uninit( device
);
273 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
277 vg_success( "Ready\n" );
280 VG_STATIC
void vg_audio_free(void * nothing
)
282 ma_device
*device
= &vg_audio
.miniaudio_device
;
283 ma_device_uninit( device
);
286 vg_free( vg_audio
.mem
);
295 static aatree_ptr
audio_alloc_entity_internal(void)
297 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
298 &vg_audio
.active_pool_head
);
300 if( playerid
== AATREE_PTR_NIL
)
301 return AATREE_PTR_NIL
;
303 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
309 VG_STATIC
void audio_entity_free_internal( aatree_ptr id
)
311 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
314 /* Notify player that we've finished */
315 if( aap
->ent
.player
)
316 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
319 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
320 &vg_audio
.active_pool_head
);
323 VG_STATIC
void *audio_entity_vorbis_ptr( aatree_ptr entid
)
325 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
326 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
331 VG_STATIC
void audio_entity_start( audio_entity
*src
)
333 aatree_ptr entid
= audio_alloc_entity_internal();
334 if( entid
== AATREE_PTR_NIL
)
337 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
339 ent
->info
= src
->info
;
340 ent
->name
= src
->info
.source
->path
;
342 ent
->player
= src
->player
;
345 ent
->fadeout_current
= 0;
347 /* Notify main player we are dequeud and playing */
350 src
->player
->enqued
= 0;
351 src
->player
->active_entity
= entid
;
354 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
356 /* Setup vorbis decoder */
357 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
359 stb_vorbis_alloc alloc
= {
360 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
361 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
365 stb_vorbis
*decoder
= stb_vorbis_open_memory(
366 src
->info
.source
->data
,
367 src
->info
.source
->size
, &err
, &alloc
);
371 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
372 src
->info
.source
->path
, err
);
374 audio_entity_free_internal( entid
);
379 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
382 aap
->vorbis_handle
= decoder
;
386 ent
->length
= src
->info
.source
->size
;
391 * Read everything from the queue
393 VG_STATIC
void audio_system_enque(void)
395 /* Process incoming sound queue */
399 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
401 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
406 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
408 audio_entity_start( src
);
412 /* Otherwise try start fadeout but dont remove from queue */
414 aatree_ptr entid
= src
->player
->active_entity
;
415 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
418 ent
->fadeout
= FADEOUT_LENGTH
;
419 ent
->fadeout_current
= FADEOUT_LENGTH
;
422 vg_audio
.entity_queue
[ wr
++ ] = *src
;
427 audio_entity_start( src
);
431 vg_audio
.queue_len
= wr
;
433 /* Localize others memory */
434 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
436 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
440 if( aap
->ent
.player
)
442 /* Only copy information in whilst not requeing */
443 if( aap
->ent
.player
->enqued
== 0 )
445 aap
->ent
.info
= aap
->ent
.player
->info
;
447 if( (aap
->ent
.info
.flags
& AUDIO_FLAG_KILL
) && !aap
->ent
.fadeout
)
449 aap
->ent
.fadeout
= FADEOUT_LENGTH
;
450 aap
->ent
.fadeout_current
= FADEOUT_LENGTH
;
460 * Redistribute sound systems
462 VG_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 VG_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 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
521 for( u32 i
=0; i
<count
; i
++ )
523 dst
[ i
*2 + 0 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
524 dst
[ i
*2 + 1 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
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
))
565 * ........ more wrecked code sorry!
568 stb_vorbis_get_samples_i16_downmixed( stb_vorbis
*f
, i16
*buffer
, int len
)
571 c
= VG_MIN( 1, f
->channels
- 1 );
575 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
580 for( int j
=0; j
< k
; ++j
)
582 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
583 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
585 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
586 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
590 f
->channel_buffer_start
+= k
;
595 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
602 VG_STATIC
void audio_entity_get_samples( aatree_ptr id
, u32 count
, float *buf
)
604 vg_profile_begin( &_vg_prof_audio_decode
);
606 struct active_audio_player
*aap
= &vg_audio
.active_players
[id
];
607 audio_entity
*ent
= &aap
->ent
;
609 u32 remaining
= count
;
610 u32 cursor
= ent
->cur
;
615 u32 samples_this_run
= VG_MIN( remaining
, ent
->length
- cursor
);
616 remaining
-= samples_this_run
;
618 float *dst
= &buf
[ buffer_pos
* 2 ];
620 int source_mode
= ent
->info
.source
->source_mode
;
622 if( source_mode
== k_audio_source_mono
)
624 i16
*src_buffer
= ent
->info
.source
->data
,
625 *src
= &src_buffer
[cursor
];
627 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
629 else if( source_mode
== k_audio_source_compressed
)
631 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
636 if( read_samples
!= samples_this_run
)
638 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
642 cursor
+= samples_this_run
;
643 buffer_pos
+= samples_this_run
;
645 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
647 if( source_mode
== k_audio_source_compressed
)
649 stb_vorbis_seek_start( aap
->vorbis_handle
);
661 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
662 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
669 vg_profile_end( &_vg_prof_audio_decode
);
672 VG_STATIC
void audio_entity_mix( aatree_ptr id
, float *buffer
,
675 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
677 u32 cursor
= ent
->cur
, buffer_pos
= 0;
678 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
680 u32 frames_write
= frame_count
;
681 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
683 float vol
= ent
->info
.vol
,
686 audio_entity_get_samples( id
, frame_count
, pcf
);
688 vg_profile_begin( &_vg_prof_audio_mix
);
690 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
691 audio_entity_spacialize( ent
, &vol
, &pan
);
693 for( u32 j
=0; j
<frame_count
; j
++ )
695 float frame_vol
= vol
;
699 /* Force this system to be removed now */
700 if( ent
->fadeout_current
== 0 )
702 ent
->info
.flags
= 0x00;
703 ent
->cur
= ent
->length
;
707 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
708 ent
->fadeout_current
--;
714 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
715 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
720 vg_profile_end( &_vg_prof_audio_mix
);
724 * callback from miniaudio.h interface
726 VG_STATIC
void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
727 const void *pInput
, ma_uint32 frame_count
)
729 struct timespec time_start
, time_end
;
730 clock_gettime( CLOCK_REALTIME
, &time_start
);
732 audio_system_enque();
735 float *pOut32F
= (float *)pOutBuf
;
736 for( int i
=0; i
<frame_count
*2; i
++ )
740 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
742 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
746 audio_entity_mix( i
, pOut32F
, frame_count
);
751 audio_system_cleanup();
753 /* TODO: what the hell is this? */
758 vg_profile_increment( &_vg_prof_audio_decode
);
759 vg_profile_increment( &_vg_prof_audio_mix
);
761 vg_prof_audio_mix
= _vg_prof_audio_mix
;
762 vg_prof_audio_decode
= _vg_prof_audio_decode
;
764 vg_audio
.samples_last
= frame_count
;
768 VG_STATIC
void audio_clip_load( audio_clip
*clip
, void *lin_alloc
)
770 if( lin_alloc
== NULL
)
771 lin_alloc
= vg_audio
.audio_pool
;
773 if( clip
->source_mode
== k_audio_source_mono
)
775 vg_linear_clear( vg_mem
.scratch
);
778 stb_vorbis_alloc alloc
= {
779 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
780 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
783 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
786 stb_vorbis
*decoder
= stb_vorbis_open_memory(
787 filedata
, fsize
, &err
, &alloc
);
791 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
793 vg_fatal_exit_loop( "Vorbis decode error" );
796 /* only mono is supported in uncompressed */
797 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
798 data_size
= length_samples
* sizeof(i16
);
801 clip
->data
= vg_linear_alloc( lin_alloc
, data_size
);
802 clip
->size
= length_samples
;
805 int read_samples
= stb_vorbis_get_samples_i16_downmixed(
806 decoder
, clip
->data
, length_samples
);
808 if( read_samples
!= length_samples
)
809 vg_fatal_exit_loop( "Decode error" );
811 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
812 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip
->path
, mb
,
816 /* load in directly */
817 else if( clip
->source_mode
== k_audio_source_compressed
)
820 clip
->data
= vg_file_read( lin_alloc
, clip
->path
, &clip
->size
);
824 vg_fatal_exit_loop( "Audio failed to load" );
826 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
827 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
831 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
)
833 for( int i
=0; i
<count
; i
++ )
834 audio_clip_load( &arr
[i
], lin_alloc
);
837 /* Mark change to be uploaded through queue system */
838 VG_STATIC
void audio_player_commit( audio_player
*sys
)
840 audio_require_lock();
842 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
844 vg_warn( "Audio commit queue full\n" );
850 vg_warn( "[2] Audio commit spamming; already enqued (%s)\n", sys
->name
);
855 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
856 ent
->info
= sys
->info
;
860 VG_STATIC
void audio_require_init( audio_player
*player
)
865 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
868 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
870 if( clip
->data
&& clip
->size
)
873 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
876 /* Play a clip using player. If its already playing something, it will
877 * fadeout quickly and start the next sound */
878 VG_STATIC
void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
880 audio_require_lock();
881 audio_require_init( player
);
882 audio_require_clip_loaded( clip
);
884 if( player
->info
.flags
& AUDIO_FLAG_KILL
)
886 vg_error( "Can't start audio clip on player that is/has disconnected" );
892 vg_warn( "[1] Audio commit spamming; already enqued (%s)\n",
897 player
->info
.source
= clip
;
898 audio_player_commit( player
);
902 VG_STATIC
void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
904 audio_require_lock();
905 audio_require_init( player
);
909 VG_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 VG_STATIC
void audio_player_init( audio_player
*player
)
931 player
->active_entity
= AATREE_PTR_NIL
;
940 * Safety enforced Get/set attributes
943 VG_STATIC
int audio_player_is_playing( audio_player
*sys
)
945 audio_require_lock();
947 if( sys
->active_entity
!= AATREE_PTR_NIL
)
953 VG_STATIC
void audio_player_set_position( audio_player
*sys
, v3f pos
)
955 audio_require_lock();
956 v3_copy( pos
, sys
->info
.world_position
);
959 VG_STATIC
void audio_player_set_vol( audio_player
*sys
, float vol
)
961 audio_require_lock();
965 VG_STATIC
float audio_player_get_vol( audio_player
*sys
)
967 audio_require_lock();
968 return sys
->info
.vol
;
971 VG_STATIC
void audio_player_set_pan( audio_player
*sys
, float pan
)
973 audio_require_lock();
977 VG_STATIC
float audio_player_get_pan( audio_player
*sys
)
979 audio_require_lock();
980 return sys
->info
.pan
;
983 VG_STATIC
void audio_player_set_flags( audio_player
*sys
, u32 flags
)
985 audio_require_lock();
986 sys
->info
.flags
= flags
;
989 VG_STATIC u32
audio_player_get_flags( audio_player
*sys
)
991 audio_require_lock();
992 return sys
->info
.flags
;
1000 VG_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;
1018 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
1020 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1025 audio_entity
*ent
= &aap
->ent
;
1026 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1028 snd
->name
= ent
->name
;
1029 snd
->cursor
= ent
->cur
;
1030 snd
->flags
= ent
->info
.flags
;
1031 snd
->length
= ent
->length
;
1032 snd
->vol
= ent
->info
.vol
*100.0f
;
1033 v3_copy( ent
->info
.world_position
, snd
->pos
);
1036 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1037 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1038 &vg_prof_audio_mix
}, 2,
1039 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1047 vg_uictx
.cursor
[0] = 258;
1048 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1049 vg_uictx
.cursor
[2] = 150;
1050 vg_uictx
.cursor
[3] = 12;
1052 float mb1
= 1024.0f
*1024.0f
,
1053 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1054 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1055 percent
= (usage
/total
) * 100.0f
;
1057 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1059 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1060 vg_uictx
.cursor
[1] += 20;
1062 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1063 u32 overlap_length
= 0;
1065 /* Draw audio stack */
1066 for( int i
=0; i
<num_systems
; i
++ )
1068 struct sound_info
*inf
= &infos
[i
];
1070 vg_uictx
.cursor
[2] = 200;
1071 vg_uictx
.cursor
[3] = 18;
1073 u32 alpha
= 0xa0000000;
1077 ui_fill_rect( vg_uictx
.cursor
, 0x00333333|alpha
);
1079 ui_px baseline
= vg_uictx
.cursor
[0],
1081 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1084 vg_uictx
.cursor
[2] = 2;
1085 vg_uictx
.cursor
[0] = c
;
1086 ui_fill_rect( vg_uictx
.cursor
, 0xffffffff );
1088 vg_uictx
.cursor
[0] = baseline
+ 2;
1089 vg_uictx
.cursor
[1] += 2;
1090 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1091 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1093 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1096 v3_copy( inf
->pos
, wpos
);
1098 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1100 if( wpos
[3] < 0.0f
)
1101 goto projected_behind
;
1103 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1104 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1107 wr
[0] = wpos
[0] * vg
.window_x
;
1108 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1112 for( int j
=0; j
<12; j
++ )
1115 for( int k
=0; k
<overlap_length
; k
++ )
1117 ui_px
*wk
= overlap_buffer
[k
];
1118 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1119 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1132 ui_text( wr
, perf
, 1, 0 );
1134 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1141 vg_uictx
.cursor
[1] += 1;
1145 #endif /* VG_AUDIO_H */