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 );
238 u32 decode_size
= AUDIO_DECODE_SIZE
* SFX_MAX_SYSTEMS
;
239 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
242 vg_audio
.active_pool_info
.base
= vg_audio
.active_players
;
243 vg_audio
.active_pool_info
.offset
= offsetof(struct active_audio_player
,
245 vg_audio
.active_pool_info
.stride
= sizeof(struct active_audio_player
);
246 vg_audio
.active_pool_info
.p_cmp
= NULL
;
247 aatree_init_pool( &vg_audio
.active_pool_info
, SFX_MAX_SYSTEMS
);
249 ma_device_config
*dconf
= &vg_audio
.miniaudio_dconfig
;
250 ma_device
*device
= &vg_audio
.miniaudio_device
;
252 *dconf
= ma_device_config_init( ma_device_type_playback
);
253 dconf
->playback
.format
= ma_format_f32
;
254 dconf
->playback
.channels
= 2;
255 dconf
->sampleRate
= 44100;
256 dconf
->dataCallback
= audio_mixer_callback
;
257 dconf
->periodSizeInFrames
= 441;
259 dconf
->pUserData
= NULL
;
261 vg_info( "Starting audio engine\n" );
263 if( ma_device_init( NULL
, dconf
, device
) != MA_SUCCESS
)
265 vg_fatal_exit_loop( "(audio) ma_device failed to initialize" );
269 if( ma_device_start( device
) != MA_SUCCESS
)
271 ma_device_uninit( device
);
272 vg_fatal_exit_loop( "(audio) ma_device failed to start" );
276 vg_success( "Ready\n" );
279 VG_STATIC
void vg_audio_free(void * nothing
)
281 ma_device
*device
= &vg_audio
.miniaudio_device
;
282 ma_device_uninit( device
);
285 vg_free( vg_audio
.mem
);
294 static aatree_ptr
audio_alloc_entity_internal(void)
296 aatree_ptr playerid
= aatree_pool_alloc( &vg_audio
.active_pool_info
,
297 &vg_audio
.active_pool_head
);
299 if( playerid
== AATREE_PTR_NIL
)
300 return AATREE_PTR_NIL
;
302 struct active_audio_player
*aap
= &vg_audio
.active_players
[ playerid
];
308 VG_STATIC
void audio_entity_free_internal( aatree_ptr id
)
310 struct active_audio_player
*aap
= &vg_audio
.active_players
[ id
];
313 /* Notify player that we've finished */
314 if( aap
->ent
.player
)
315 aap
->ent
.player
->active_entity
= AATREE_PTR_NIL
;
318 aatree_pool_free( &vg_audio
.active_pool_info
, id
,
319 &vg_audio
.active_pool_head
);
322 VG_STATIC
void *audio_entity_vorbis_ptr( aatree_ptr entid
)
324 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
325 *loc
= &buf
[AUDIO_DECODE_SIZE
*entid
];
330 VG_STATIC
void audio_entity_start( audio_entity
*src
)
332 aatree_ptr entid
= audio_alloc_entity_internal();
333 if( entid
== AATREE_PTR_NIL
)
336 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
338 ent
->info
= src
->info
;
339 ent
->name
= src
->info
.source
->path
;
341 ent
->player
= src
->player
;
344 ent
->fadeout_current
= 0;
346 /* Notify main player we are dequeud and playing */
349 src
->player
->enqued
= 0;
350 src
->player
->active_entity
= entid
;
353 if( src
->info
.source
->source_mode
== k_audio_source_compressed
)
355 /* Setup vorbis decoder */
356 struct active_audio_player
*aap
= &vg_audio
.active_players
[ entid
];
358 stb_vorbis_alloc alloc
= {
359 .alloc_buffer
= (char *)audio_entity_vorbis_ptr( entid
),
360 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
364 stb_vorbis
*decoder
= stb_vorbis_open_memory(
365 src
->info
.source
->data
,
366 src
->info
.source
->size
, &err
, &alloc
);
370 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
371 src
->info
.source
->path
, err
);
373 audio_entity_free_internal( entid
);
378 ent
->length
= stb_vorbis_stream_length_in_samples( decoder
);
381 aap
->vorbis_handle
= decoder
;
385 ent
->length
= src
->info
.source
->size
;
390 * Read everything from the queue
392 VG_STATIC
void audio_system_enque(void)
394 /* Process incoming sound queue */
398 for( int i
=0; i
<vg_audio
.queue_len
; i
++ )
400 audio_entity
*src
= &vg_audio
.entity_queue
[ i
];
405 if( src
->player
->active_entity
== AATREE_PTR_NIL
)
407 audio_entity_start( src
);
411 /* Otherwise try start fadeout but dont remove from queue */
413 aatree_ptr entid
= src
->player
->active_entity
;
414 audio_entity
*ent
= &vg_audio
.active_players
[ entid
].ent
;
417 ent
->fadeout
= FADEOUT_LENGTH
;
418 ent
->fadeout_current
= FADEOUT_LENGTH
;
421 vg_audio
.entity_queue
[ wr
++ ] = *src
;
426 audio_entity_start( src
);
430 vg_audio
.queue_len
= wr
;
432 /* Localize others memory */
433 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
435 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
439 if( aap
->ent
.player
)
441 /* Only copy information in whilst not requeing */
442 if( aap
->ent
.player
->enqued
== 0 )
444 aap
->ent
.info
= aap
->ent
.player
->info
;
446 if( (aap
->ent
.info
.flags
& AUDIO_FLAG_KILL
) && !aap
->ent
.fadeout
)
448 aap
->ent
.fadeout
= FADEOUT_LENGTH
;
449 aap
->ent
.fadeout_current
= FADEOUT_LENGTH
;
459 * Redistribute sound systems
461 VG_STATIC
void audio_system_cleanup(void)
465 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
467 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
470 audio_entity
*src
= &aap
->ent
;
471 if( src
->cur
< src
->length
|| (src
->info
.flags
& AUDIO_FLAG_LOOP
))
477 audio_entity_free_internal( i
);
486 * Get effective volume and pan from this entity
488 VG_STATIC
void audio_entity_spacialize( audio_entity
*ent
, float *vol
, float *pan
)
490 if( ent
->info
.vol
< 0.01f
)
492 *vol
= ent
->info
.vol
;
498 v3_sub( ent
->info
.world_position
, vg_audio
.listener_pos
, delta
);
500 float dist2
= v3_length2( delta
);
502 if( dist2
< 0.0001f
)
509 float dist
= sqrtf( dist2
),
510 attn
= (dist
/ ent
->info
.vol
) +1.0f
;
512 v3_muls( delta
, 1.0f
/dist
, delta
);
513 *pan
= v3_dot( vg_audio
.listener_ears
, delta
);
514 *vol
= 1.0f
/(attn
*attn
);
518 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
520 for( u32 i
=0; i
<count
; i
++ )
522 dst
[ i
*2 + 0 ] = (float)src
[i
] * (1.0f
/32767.0f
);
523 dst
[ i
*2 + 1 ] = (float)src
[i
] * (1.0f
/32767.0f
);
528 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
531 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
535 c
= VG_MIN( 1, f
->channels
- 1 );
539 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
544 for( int j
=0; j
< k
; ++j
)
546 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
547 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
551 f
->channel_buffer_start
+= k
;
556 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
564 * ........ more wrecked code sorry!
567 stb_vorbis_get_samples_i16_interleaved_mono( stb_vorbis
*f
, i16
*buffer
,
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
= &((i16
*)ent
->info
.source
->data
)[ cursor
];
625 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
627 else if( source_mode
== k_audio_source_compressed
)
629 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
634 if( read_samples
!= samples_this_run
)
636 vg_warn( "Invalid samples read (%s)\n", ent
->info
.source
->path
);
640 cursor
+= samples_this_run
;
641 buffer_pos
+= samples_this_run
;
643 if( (ent
->info
.flags
& AUDIO_FLAG_LOOP
) && remaining
)
645 if( source_mode
== k_audio_source_compressed
)
647 stb_vorbis_seek_start( aap
->vorbis_handle
);
659 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
660 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
667 vg_profile_end( &_vg_prof_audio_decode
);
670 VG_STATIC
void audio_entity_mix( aatree_ptr id
, float *buffer
,
673 audio_entity
*ent
= &vg_audio
.active_players
[id
].ent
;
675 u32 cursor
= ent
->cur
, buffer_pos
= 0;
676 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
678 u32 frames_write
= frame_count
;
679 float fadeout_divisor
= 1.0f
/ (float)ent
->fadeout
;
681 float vol
= ent
->info
.vol
,
684 audio_entity_get_samples( id
, frame_count
, pcf
);
686 vg_profile_begin( &_vg_prof_audio_mix
);
688 if( ent
->info
.flags
& AUDIO_FLAG_SPACIAL_3D
)
689 audio_entity_spacialize( ent
, &vol
, &pan
);
691 for( u32 j
=0; j
<frame_count
; j
++ )
693 float frame_vol
= vol
;
697 /* Force this system to be removed now */
698 if( ent
->fadeout_current
== 0 )
700 ent
->info
.flags
= 0x00;
701 ent
->cur
= ent
->length
;
705 frame_vol
*= (float)ent
->fadeout_current
* fadeout_divisor
;
706 ent
->fadeout_current
--;
712 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * frame_vol
* sl
;
713 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * frame_vol
* sr
;
718 vg_profile_end( &_vg_prof_audio_mix
);
722 * callback from miniaudio.h interface
724 VG_STATIC
void audio_mixer_callback( ma_device
*pDevice
, void *pOutBuf
,
725 const void *pInput
, ma_uint32 frame_count
)
727 struct timespec time_start
, time_end
;
728 clock_gettime( CLOCK_REALTIME
, &time_start
);
730 audio_system_enque();
733 float *pOut32F
= (float *)pOutBuf
;
734 for( int i
=0; i
<frame_count
*2; i
++ )
738 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
740 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
744 audio_entity_mix( i
, pOut32F
, frame_count
);
749 audio_system_cleanup();
751 /* TODO: what the hell is this? */
756 vg_profile_increment( &_vg_prof_audio_decode
);
757 vg_profile_increment( &_vg_prof_audio_mix
);
759 vg_prof_audio_mix
= _vg_prof_audio_mix
;
760 vg_prof_audio_decode
= _vg_prof_audio_decode
;
762 vg_audio
.samples_last
= frame_count
;
766 VG_STATIC
void audio_clip_load( audio_clip
*clip
)
768 if( clip
->source_mode
== k_audio_source_mono
)
770 vg_linear_clear( vg_mem
.scratch
);
772 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
774 stb_vorbis_alloc alloc
= {
775 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
776 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
780 stb_vorbis
*decoder
= stb_vorbis_open_memory(
781 filedata
, fsize
, &err
, &alloc
);
785 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
787 vg_fatal_exit_loop( "Vorbis decode error" );
790 /* only mono is supported in uncompressed */
791 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
792 data_size
= length_samples
* sizeof(i16
);
794 clip
->data
= vg_linear_alloc( vg_audio
.audio_pool
, data_size
);
795 clip
->size
= length_samples
;
797 int read_samples
= stb_vorbis_get_samples_i16_interleaved_mono(
798 decoder
, clip
->data
, length_samples
);
800 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
801 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
804 /* load in directly */
805 else if( clip
->source_mode
== k_audio_source_compressed
)
807 clip
->data
= vg_file_read( vg_audio
.audio_pool
, clip
->path
, &clip
->size
);
810 vg_fatal_exit_loop( "Audio failed to load" );
812 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
813 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
817 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
)
819 for( int i
=0; i
<count
; i
++ )
820 audio_clip_load( &arr
[i
] );
823 /* Mark change to be uploaded through queue system */
824 VG_STATIC
void audio_player_commit( audio_player
*sys
)
826 audio_require_lock();
828 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
830 vg_warn( "Audio commit queue full\n" );
836 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys
->name
);
841 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
842 ent
->info
= sys
->info
;
846 VG_STATIC
void audio_require_init( audio_player
*player
)
851 vg_fatal_exit_loop( "Must init audio player before playing! \n" );
854 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
856 if( clip
->data
&& clip
->size
)
859 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
862 /* Play a clip using player. If its already playing something, it will
863 * fadeout quickly and start the next sound */
864 VG_STATIC
void audio_player_playclip( audio_player
*player
, audio_clip
*clip
)
866 audio_require_lock();
867 audio_require_init( player
);
868 audio_require_clip_loaded( clip
);
870 if( player
->info
.flags
& AUDIO_FLAG_KILL
)
872 vg_error( "Can't start audio clip on player that is/has disconnected" );
876 player
->info
.source
= clip
;
877 audio_player_commit( player
);
881 VG_STATIC
void audio_player_playoneshot( audio_player
*player
, audio_clip
*clip
)
883 audio_require_lock();
884 audio_require_init( player
);
888 VG_STATIC
void audio_play_oneshot( audio_clip
*clip
, float volume
)
890 audio_require_lock();
891 audio_require_clip_loaded( clip
);
893 if( vg_audio
.queue_len
>= vg_list_size( vg_audio
.entity_queue
) )
895 vg_warn( "Audio commit queue full\n" );
899 audio_entity
*ent
= &vg_audio
.entity_queue
[ vg_audio
.queue_len
++ ];
901 ent
->info
.flags
= AUDIO_FLAG_ONESHOT
;
902 ent
->info
.pan
= 0.0f
;
903 ent
->info
.source
= clip
;
904 ent
->info
.vol
= volume
;
908 VG_STATIC
void audio_player_init( audio_player
*player
)
910 player
->active_entity
= AATREE_PTR_NIL
;
919 * Safety enforced Get/set attributes
922 VG_STATIC
int audio_player_is_playing( audio_player
*sys
)
924 audio_require_lock();
926 if( sys
->active_entity
!= AATREE_PTR_NIL
)
932 VG_STATIC
void audio_player_set_position( audio_player
*sys
, v3f pos
)
934 audio_require_lock();
935 v3_copy( pos
, sys
->info
.world_position
);
938 VG_STATIC
void audio_player_set_vol( audio_player
*sys
, float vol
)
940 audio_require_lock();
944 VG_STATIC
float audio_player_get_vol( audio_player
*sys
)
946 audio_require_lock();
947 return sys
->info
.vol
;
950 VG_STATIC
void audio_player_set_pan( audio_player
*sys
, float pan
)
952 audio_require_lock();
956 VG_STATIC
float audio_player_get_pan( audio_player
*sys
)
958 audio_require_lock();
959 return sys
->info
.pan
;
962 VG_STATIC
void audio_player_set_flags( audio_player
*sys
, u32 flags
)
964 audio_require_lock();
965 sys
->info
.flags
= flags
;
968 VG_STATIC u32
audio_player_get_flags( audio_player
*sys
)
970 audio_require_lock();
971 return sys
->info
.flags
;
979 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
981 if( !vg_audio
.debug_ui
)
988 u32 cursor
, flags
, length
;
992 infos
[ SFX_MAX_SYSTEMS
];
997 for( int i
=0; i
<SFX_MAX_SYSTEMS
; i
++ )
999 struct active_audio_player
*aap
= &vg_audio
.active_players
[i
];
1004 audio_entity
*ent
= &aap
->ent
;
1005 struct sound_info
*snd
= &infos
[ num_systems
++ ];
1007 snd
->name
= ent
->name
;
1008 snd
->cursor
= ent
->cur
;
1009 snd
->flags
= ent
->info
.flags
;
1010 snd
->length
= ent
->length
;
1011 snd
->vol
= ent
->info
.vol
*100.0f
;
1012 v3_copy( ent
->info
.world_position
, snd
->pos
);
1015 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1016 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1017 &vg_prof_audio_mix
}, 2,
1018 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1026 vg_uictx
.cursor
[0] = 258;
1027 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1028 vg_uictx
.cursor
[2] = 150;
1029 vg_uictx
.cursor
[3] = 12;
1031 float mb1
= 1024.0f
*1024.0f
,
1032 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1033 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1034 percent
= (usage
/total
) * 100.0f
;
1036 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1038 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1039 vg_uictx
.cursor
[1] += 20;
1041 ui_rect overlap_buffer
[ SFX_MAX_SYSTEMS
];
1042 u32 overlap_length
= 0;
1044 /* Draw audio stack */
1045 for( int i
=0; i
<num_systems
; i
++ )
1047 struct sound_info
*inf
= &infos
[i
];
1049 vg_uictx
.cursor
[2] = 200;
1050 vg_uictx
.cursor
[3] = 18;
1052 u32 alpha
= 0xa0000000;
1056 ui_fill_rect( vg_uictx
.cursor
, 0x00333333|alpha
);
1058 ui_px baseline
= vg_uictx
.cursor
[0],
1060 c
= baseline
+ ((float)inf
->cursor
/ (float)inf
->length
) * w
;
1063 vg_uictx
.cursor
[2] = 2;
1064 vg_uictx
.cursor
[0] = c
;
1065 ui_fill_rect( vg_uictx
.cursor
, 0xffffffff );
1067 vg_uictx
.cursor
[0] = baseline
+ 2;
1068 vg_uictx
.cursor
[1] += 2;
1069 snprintf( perf
, 127, "%s %.1f%%", infos
[i
].name
, infos
[i
].vol
);
1070 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1072 if( inf
->flags
& AUDIO_FLAG_SPACIAL_3D
)
1075 v3_copy( inf
->pos
, wpos
);
1077 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1079 if( wpos
[3] < 0.0f
)
1080 goto projected_behind
;
1082 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1083 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1086 wr
[0] = wpos
[0] * vg
.window_x
;
1087 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1091 for( int j
=0; j
<12; j
++ )
1094 for( int k
=0; k
<overlap_length
; k
++ )
1096 ui_px
*wk
= overlap_buffer
[k
];
1097 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1098 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1111 ui_text( wr
, perf
, 1, 0 );
1113 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1120 vg_uictx
.cursor
[1] += 1;
1124 #endif /* VG_AUDIO_H */