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"
23 #pragma GCC push_options
24 #pragma GCC optimize ("O3")
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
30 #define STB_VORBIS_MAX_CHANNELS 2
31 #include "submodules/stb/stb_vorbis.c"
38 #pragma GCC pop_options
39 #pragma GCC diagnostic pop
43 #define AUDIO_CHANNELS 32
45 #define AUDIO_FLAG_LOOP 0x1
46 #define AUDIO_FLAG_SPACIAL_3D 0x2
48 /* Vorbis will ALWAYS use the maximum amount of channels it can */
49 //#define AUDIO_FLAG_MONO 0x100 NOTE: This is the default, so its not used
50 #define AUDIO_FLAG_STEREO 0x200
51 #define AUDIO_FLAG_VORBIS 0x400
53 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
54 #define AUDIO_MUTE_VOLUME 0.0f
55 #define AUDIO_BASE_VOLUME 1.0f
57 typedef struct audio_clip audio_clip
;
58 typedef struct audio_channel audio_channel
;
59 typedef struct audio_lfo audio_lfo
;
70 static struct vg_audio_system
72 SDL_AudioDeviceID sdl_output_device
;
81 SDL_mutex
*mux_checker
,
86 u32 time
, time_startframe
;
87 float sqrt_polynomial_coefficient
;
96 k_lfo_polynomial_bipolar
101 float polynomial_coefficient
;
104 u32 editble_state_write_mask
;
106 oscillators
[ AUDIO_LFOS
];
111 char name
[32]; /* only editable while allocated == 0 */
112 audio_clip
*source
; /* ... */
115 /* internal non-readable state
116 * -----------------------------*/
117 u32 cursor
, source_length
;
119 float volume_movement_start
,
125 stb_vorbis
*vorbis_handle
;
126 stb_vorbis_alloc vorbis_alloc
;
128 enum channel_activity
130 k_channel_activity_reset
, /* will advance if allocated==1, to wake */
131 k_channel_activity_wake
, /* will advance to either of next two */
132 k_channel_activity_alive
,
133 k_channel_activity_error
138 * editable structure, can be modified inside _lock and _unlock
139 * the edit mask tells which to copy into internal _, or to discard
140 * ----------------------------------------------------------------------
146 float volume
, /* current volume */
147 volume_target
, /* target volume */
154 v4f spacial_falloff
; /* xyz, range */
160 u32 editble_state_write_mask
;
162 channels
[ AUDIO_CHANNELS
];
164 /* System queue, and access from thread 0 */
165 int debug_ui
, debug_ui_3d
;
172 volume_target_internal
,
175 vg_audio
= { .volume_console
= 1.0f
};
178 static struct vg_profile
179 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
180 .name
= "[T2] audio_decode()"},
181 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
182 .name
= "[T2] audio_mix()"},
183 vg_prof_audio_decode
,
187 * These functions are called from the main thread and used to prevent bad
188 * access. TODO: They should be no-ops in release builds.
190 VG_STATIC
int audio_lock_checker_load(void)
193 SDL_LockMutex( vg_audio
.mux_checker
);
194 value
= vg_audio
.sync_locked
;
195 SDL_UnlockMutex( vg_audio
.mux_checker
);
199 VG_STATIC
void audio_lock_checker_store( int value
)
201 SDL_LockMutex( vg_audio
.mux_checker
);
202 vg_audio
.sync_locked
= value
;
203 SDL_UnlockMutex( vg_audio
.mux_checker
);
206 VG_STATIC
void audio_require_lock(void)
208 if( audio_lock_checker_load() )
211 vg_error( "Modifying sound effects systems requires locking\n" );
215 VG_STATIC
void audio_lock(void)
217 SDL_LockMutex( vg_audio
.mux_sync
);
218 audio_lock_checker_store(1);
221 VG_STATIC
void audio_unlock(void)
223 audio_lock_checker_store(0);
224 SDL_UnlockMutex( vg_audio
.mux_sync
);
227 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int frame_count
);
228 VG_STATIC
void vg_audio_init(void)
230 vg_audio
.mux_checker
= SDL_CreateMutex();
231 vg_audio
.mux_sync
= SDL_CreateMutex();
233 /* TODO: Move here? */
234 vg_var_push( (struct vg_var
){
235 .name
= "debug_audio",
236 .data
= &vg_audio
.debug_ui
,
237 .data_type
= k_var_dtype_i32
,
238 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
242 vg_var_push( (struct vg_var
){
244 .data
= &vg_audio
.volume_console
,
245 .data_type
= k_var_dtype_f32
,
246 .opt_f32
= { .min
=0.0f
, .max
=2.0f
, .clamp
=1 },
250 /* allocate memory */
253 vg_audio
.audio_pool
=
254 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32,
258 u32 decode_size
= AUDIO_DECODE_SIZE
* AUDIO_CHANNELS
;
259 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
261 SDL_AudioSpec spec_desired
, spec_got
;
262 spec_desired
.callback
= audio_mixer_callback
;
263 spec_desired
.channels
= 2;
264 spec_desired
.format
= AUDIO_F32
;
265 spec_desired
.freq
= 44100;
266 spec_desired
.padding
= 0;
267 spec_desired
.samples
= 512;
268 spec_desired
.silence
= 0;
269 spec_desired
.size
= 0;
270 spec_desired
.userdata
= NULL
;
272 vg_audio
.sdl_output_device
=
273 SDL_OpenAudioDevice( NULL
, 0, &spec_desired
, &spec_got
,
274 SDL_AUDIO_ALLOW_SAMPLES_CHANGE
);
276 if( vg_audio
.sdl_output_device
)
278 SDL_PauseAudioDevice( vg_audio
.sdl_output_device
, 0 );
283 "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
284 " Frequency: 44100 hz\n"
285 " Buffer size: 512\n"
287 " Format: s16 or f32\n" );
290 vg_success( "Ready\n" );
293 VG_STATIC
void vg_audio_free(void)
295 SDL_CloseAudioDevice( vg_audio
.sdl_output_device
);
302 #define AUDIO_EDIT_VOLUME_SLOPE 0x1
303 #define AUDIO_EDIT_VOLUME 0x2
304 #define AUDIO_EDIT_LFO_PERIOD 0x4
305 #define AUDIO_EDIT_LFO_WAVE 0x8
306 #define AUDIO_EDIT_LFO_ATTACHMENT 0x10
307 #define AUDIO_EDIT_SPACIAL 0x20
308 #define AUDIO_EDIT_OWNERSHIP 0x40
310 static audio_channel
*audio_request_channel( audio_clip
*clip
, u32 flags
)
312 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
314 audio_channel
*ch
= &vg_audio
.channels
[i
];
320 strcpy( ch
->name
, clip
->path
);
324 ch
->editable_state
.relinquished
= 0;
325 ch
->editable_state
.volume
= 1.0f
;
326 ch
->editable_state
.volume_target
= 1.0f
;
327 ch
->editable_state
.pan
= 0.0f
;
328 ch
->editable_state
.pan_target
= 0.0f
;
329 ch
->editable_state
.volume_rate
= 0;
330 ch
->editable_state
.pan_rate
= 0;
331 v4_copy((v4f
){0.0f
,0.0f
,0.0f
,1.0f
},ch
->editable_state
.spacial_falloff
);
332 ch
->editable_state
.lfo
= NULL
;
333 ch
->editable_state
.lfo_amount
= 0.0f
;
334 ch
->editble_state_write_mask
= 0x00;
342 static audio_channel
*audio_relinquish_channel( audio_channel
*ch
)
344 ch
->editable_state
.relinquished
= 1;
345 ch
->editble_state_write_mask
|= AUDIO_EDIT_OWNERSHIP
;
349 static audio_channel
*audio_channel_slope_volume( audio_channel
*ch
,
353 ch
->editable_state
.volume_target
= new_volume
;
354 ch
->editable_state
.volume_rate
= length
* 44100.0f
;
355 ch
->editble_state_write_mask
|= AUDIO_EDIT_VOLUME_SLOPE
;
360 static audio_channel
*audio_channel_edit_volume( audio_channel
*ch
,
361 float new_volume
, int instant
)
365 ch
->editable_state
.volume
= 0.0f
;
366 ch
->editble_state_write_mask
|= AUDIO_EDIT_VOLUME
;
371 return audio_channel_slope_volume( ch
, 0.05f
, new_volume
);
375 static audio_channel
*audio_channel_fadeout( audio_channel
*ch
, float length
)
377 ch
= audio_channel_slope_volume( ch
, length
, 0.0f
);
378 ch
= audio_relinquish_channel( ch
);
383 static audio_channel
*audio_channel_fadein( audio_channel
*ch
, float length
)
385 ch
= audio_channel_edit_volume( ch
, 0.0f
, 1 );
386 ch
= audio_channel_slope_volume( ch
, length
, 1.0f
);
390 static audio_channel
*audio_channel_crossfade( audio_channel
*ch
,
391 audio_clip
*new_clip
,
392 float length
, u32 flags
)
398 ch
= audio_channel_fadeout( ch
, length
);
401 audio_channel
*replacement
= audio_request_channel( new_clip
, flags
);
405 replacement
= audio_channel_fadein( replacement
, length
);
411 static audio_channel
*audio_channel_sidechain_lfo( audio_channel
*ch
,
412 int lfo_id
, float amount
)
414 ch
->editable_state
.lfo
= &vg_audio
.oscillators
[ lfo_id
];
415 ch
->editable_state
.lfo_amount
= amount
;
416 ch
->editble_state_write_mask
|= AUDIO_EDIT_LFO_ATTACHMENT
;
421 static audio_channel
*audio_channel_set_spacial( audio_channel
*ch
,
422 v3f co
, float range
)
424 if( ch
->flags
& AUDIO_FLAG_SPACIAL_3D
)
426 v3_copy( co
, ch
->editable_state
.spacial_falloff
);
427 ch
->editable_state
.spacial_falloff
[3] = 1.0f
/range
;
428 ch
->editble_state_write_mask
|= AUDIO_EDIT_SPACIAL
;
432 vg_warn( "Tried to set spacialization paramaters for 2D channel (%s)\n",
439 static audio_channel
*audio_oneshot_3d( audio_clip
*clip
, v3f position
,
440 float range
, float volume
)
442 audio_channel
*ch
= audio_request_channel( clip
, AUDIO_FLAG_SPACIAL_3D
);
446 ch
= audio_channel_set_spacial( ch
, position
, range
);
447 ch
= audio_channel_edit_volume( ch
, volume
, 1 );
448 ch
= audio_relinquish_channel( ch
);
454 static audio_channel
*audio_oneshot( audio_clip
*clip
, float volume
, float pan
)
456 audio_channel
*ch
= audio_request_channel( clip
, 0x00 );
460 ch
= audio_channel_edit_volume( ch
, volume
, 1 );
461 ch
= audio_relinquish_channel( ch
);
467 static void audio_set_lfo_wave( int id
, enum lfo_wave_type type
,
470 audio_lfo
*lfo
= &vg_audio
.oscillators
[ id
];
471 lfo
->editable_state
.polynomial_coefficient
= coefficient
;
472 lfo
->editable_state
.wave_type
= type
;
474 lfo
->editble_state_write_mask
|= AUDIO_EDIT_LFO_WAVE
;
477 static void audio_set_lfo_frequency( int id
, float freq
)
479 audio_lfo
*lfo
= &vg_audio
.oscillators
[ id
];
480 lfo
->editable_state
.period
= 44100.0f
/ freq
;
481 lfo
->editble_state_write_mask
|= AUDIO_EDIT_LFO_PERIOD
;
486 * -----------------------------------------------------------------------------
488 static int audio_channel_load_source( audio_channel
*ch
)
490 if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
492 /* Setup vorbis decoder */
493 u32 index
= ch
- vg_audio
.channels
;
495 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
496 *loc
= &buf
[AUDIO_DECODE_SIZE
*index
];
498 stb_vorbis_alloc alloc
= {
499 .alloc_buffer
= (char *)loc
,
500 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
504 stb_vorbis
*decoder
= stb_vorbis_open_memory(
506 ch
->source
->size
, &err
, &alloc
);
510 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
511 ch
->source
->path
, err
);
516 ch
->source_length
= stb_vorbis_stream_length_in_samples( decoder
);
517 ch
->vorbis_handle
= decoder
;
520 else if( ch
->source
->flags
& AUDIO_FLAG_STEREO
)
522 ch
->source_length
= ch
->source
->size
/ 2;
526 ch
->source_length
= ch
->source
->size
;
532 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
534 for( u32 i
=0; i
<count
; i
++ )
536 dst
[ i
*2 + 0 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
537 dst
[ i
*2 + 1 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
542 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
545 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
549 c
= VG_MIN( 1, f
->channels
- 1 );
553 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
558 for( int j
=0; j
< k
; ++j
)
560 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
561 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
565 f
->channel_buffer_start
+= k
;
570 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
578 * ........ more wrecked code sorry!
581 stb_vorbis_get_samples_i16_downmixed( stb_vorbis
*f
, i16
*buffer
, int len
)
584 c
= VG_MIN( 1, f
->channels
- 1 );
588 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
593 for( int j
=0; j
< k
; ++j
)
595 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
596 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
598 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
599 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
603 f
->channel_buffer_start
+= k
;
608 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
615 static float audio_lfo_pull_sample( audio_lfo
*lfo
)
619 if( lfo
->time
>= lfo
->_
.period
)
623 t
/= (float)lfo
->_
.period
;
625 if( lfo
->_
.wave_type
== k_lfo_polynomial_bipolar
)
642 return (( 2.0f
* lfo
->sqrt_polynomial_coefficient
* t
) /
643 /* --------------------------------------- */
644 ( 1.0f
+ lfo
->_
.polynomial_coefficient
* t
*t
)
654 static void audio_channel_get_samples( audio_channel
*ch
,
655 u32 count
, float *buf
)
657 vg_profile_begin( &_vg_prof_audio_decode
);
659 u32 remaining
= count
;
664 u32 samples_this_run
= VG_MIN( remaining
, ch
->source_length
-ch
->cursor
);
665 remaining
-= samples_this_run
;
667 float *dst
= &buf
[ buffer_pos
* 2 ];
669 if( ch
->source
->flags
& AUDIO_FLAG_STEREO
)
671 for( int i
=0;i
<samples_this_run
; i
++ )
677 else if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
679 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
684 if( read_samples
!= samples_this_run
)
686 vg_warn( "Invalid samples read (%s)\n", ch
->source
->path
);
688 for( int i
=0; i
<samples_this_run
; i
++ )
697 i16
*src_buffer
= ch
->source
->data
,
698 *src
= &src_buffer
[ch
->cursor
];
700 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
703 ch
->cursor
+= samples_this_run
;
704 buffer_pos
+= samples_this_run
;
706 if( (ch
->flags
& AUDIO_FLAG_LOOP
) && remaining
)
708 if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
709 stb_vorbis_seek_start( ch
->vorbis_handle
);
720 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
721 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
727 vg_profile_end( &_vg_prof_audio_decode
);
730 static void audio_channel_mix( audio_channel
*ch
,
731 float *buffer
, u32 frame_count
)
734 float *pcf
= alloca( frame_count
* 2 * sizeof(float) );
735 u32 frames_write
= frame_count
;
737 audio_channel_get_samples( ch
, frame_count
, pcf
);
738 vg_profile_begin( &_vg_prof_audio_mix
);
741 ch
->_
.lfo
->time
= ch
->_
.lfo
->time_startframe
;
743 float framevol_l
= 1.0f
,
746 if( ch
->flags
& AUDIO_FLAG_SPACIAL_3D
)
748 if( !vg_validf(vg_audio
.listener_pos
[0]) ||
749 !vg_validf(vg_audio
.listener_pos
[1]) ||
750 !vg_validf(vg_audio
.listener_pos
[2]) ||
751 !vg_validf(ch
->_
.spacial_falloff
[0]) ||
752 !vg_validf(ch
->_
.spacial_falloff
[1]) ||
753 !vg_validf(ch
->_
.spacial_falloff
[2]) )
755 vg_error( "NaN listener/world position (%s)\n", ch
->name
);
762 v3_sub( ch
->_
.spacial_falloff
, vg_audio
.listener_pos
, delta
);
764 float dist
= v3_length( delta
),
765 vol
= vg_maxf( 0.0f
, 1.0f
- ch
->_
.spacial_falloff
[3]*dist
);
767 v3_muls( delta
, 1.0f
/dist
, delta
);
768 float pan
= v3_dot( vg_audio
.listener_ears
, delta
);
769 vol
= powf( vol
, 5.0f
);
771 framevol_l
*= (vol
* 0.5f
) * (1.0f
- pan
);
772 framevol_r
*= (vol
* 0.5f
) * (1.0f
+ pan
);
775 for( u32 j
=0; j
<frame_count
; j
++ )
777 if( ch
->volume_movement
< ch
->_
.volume_rate
)
779 ch
->volume_movement
++;
781 float movement_t
= ch
->volume_movement
;
782 movement_t
/= (float)ch
->_
.volume_rate
;
784 ch
->_
.volume
= vg_lerpf( ch
->volume_movement_start
,
789 float vol_norm
= ch
->_
.volume
* ch
->_
.volume
;
792 vol_norm
*= 1.0f
+ audio_lfo_pull_sample( ch
->_
.lfo
)
795 float vol_l
= vol_norm
* framevol_l
,
796 vol_r
= vol_norm
* framevol_r
;
798 buffer
[ buffer_pos
*2+0 ] += pcf
[ buffer_pos
*2+0 ] * vol_l
;
799 buffer
[ buffer_pos
*2+1 ] += pcf
[ buffer_pos
*2+1 ] * vol_r
;
804 vg_profile_end( &_vg_prof_audio_mix
);
807 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int byte_count
)
810 * Copy data and move edit flags to commit flags
811 * ------------------------------------------------------------- */
813 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
815 audio_channel
*ch
= &vg_audio
.channels
[i
];
820 /* process relinquishments */
821 if( (ch
->activity
!= k_channel_activity_reset
) && ch
->_
.relinquished
)
823 if( (ch
->cursor
>= ch
->source_length
&& !(ch
->flags
& AUDIO_FLAG_LOOP
))
824 || (ch
->_
.volume
== 0.0f
)
825 || (ch
->activity
== k_channel_activity_error
) )
827 ch
->_
.relinquished
= 0;
829 ch
->activity
= k_channel_activity_reset
;
834 /* process new channels */
835 if( ch
->activity
== k_channel_activity_reset
)
837 ch
->_
= ch
->editable_state
;
839 ch
->source_length
= 0;
840 ch
->activity
= k_channel_activity_wake
;
843 if( ch
->editble_state_write_mask
& AUDIO_EDIT_OWNERSHIP
)
844 ch
->_
.relinquished
= ch
->editable_state
.relinquished
;
846 ch
->editable_state
.relinquished
= ch
->_
.relinquished
;
849 if( ch
->editble_state_write_mask
& AUDIO_EDIT_VOLUME
)
850 ch
->_
.volume
= ch
->editable_state
.volume
;
852 ch
->editable_state
.volume
= ch
->_
.volume
;
855 if( ch
->editble_state_write_mask
& AUDIO_EDIT_VOLUME_SLOPE
)
857 ch
->volume_movement_start
= ch
->_
.volume
;
858 ch
->volume_movement
= 0;
860 ch
->_
.volume_target
= ch
->editable_state
.volume_target
;
861 ch
->_
.volume_rate
= ch
->editable_state
.volume_rate
;
865 ch
->editable_state
.volume_target
= ch
->_
.volume_target
;
866 ch
->editable_state
.volume_rate
= ch
->_
.volume_rate
;
870 if( ch
->editble_state_write_mask
& AUDIO_EDIT_LFO_ATTACHMENT
)
872 ch
->_
.lfo
= ch
->editable_state
.lfo
;
873 ch
->_
.lfo_amount
= ch
->editable_state
.lfo_amount
;
877 ch
->editable_state
.lfo
= ch
->_
.lfo
;
878 ch
->editable_state
.lfo_amount
= ch
->_
.lfo_amount
;
882 if( ch
->editble_state_write_mask
& AUDIO_EDIT_SPACIAL
)
883 v4_copy( ch
->editable_state
.spacial_falloff
,ch
->_
.spacial_falloff
);
885 v4_copy( ch
->_
.spacial_falloff
,ch
->editable_state
.spacial_falloff
);
888 /* currently readonly, i guess */
889 ch
->editable_state
.pan_target
= ch
->_
.pan_target
;
890 ch
->editable_state
.pan
= ch
->_
.pan
;
891 ch
->editble_state_write_mask
= 0x00;
894 for( int i
=0; i
<AUDIO_LFOS
; i
++ )
896 audio_lfo
*lfo
= &vg_audio
.oscillators
[ i
];
898 if( lfo
->editble_state_write_mask
& AUDIO_EDIT_LFO_WAVE
)
900 lfo
->_
.wave_type
= lfo
->editable_state
.wave_type
;
902 if( lfo
->_
.wave_type
== k_lfo_polynomial_bipolar
)
904 lfo
->_
.polynomial_coefficient
=
905 lfo
->editable_state
.polynomial_coefficient
;
906 lfo
->sqrt_polynomial_coefficient
=
907 sqrtf(lfo
->_
.polynomial_coefficient
);
911 if( lfo
->editble_state_write_mask
& AUDIO_EDIT_LFO_PERIOD
)
916 t
/= (float)lfo
->_
.period
;
918 lfo
->_
.period
= lfo
->editable_state
.period
;
919 lfo
->time
= lfo
->_
.period
* t
;
924 lfo
->_
.period
= lfo
->editable_state
.period
;
928 lfo
->editble_state_write_mask
= 0x00;
936 * ------------------------------------------------------------- */
937 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
939 audio_channel
*ch
= &vg_audio
.channels
[i
];
941 if( ch
->activity
== k_channel_activity_wake
)
943 if( audio_channel_load_source( ch
) )
944 ch
->activity
= k_channel_activity_alive
;
946 ch
->activity
= k_channel_activity_error
;
952 * -------------------------------------------------------- */
953 int frame_count
= byte_count
/(2*sizeof(float));
956 float *pOut32F
= (float *)stream
;
957 for( int i
=0; i
<frame_count
*2; i
++ )
960 for( int i
=0; i
<AUDIO_LFOS
; i
++ )
962 audio_lfo
*lfo
= &vg_audio
.oscillators
[i
];
963 lfo
->time_startframe
= lfo
->time
;
966 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
968 audio_channel
*ch
= &vg_audio
.channels
[i
];
970 if( ch
->activity
== k_channel_activity_alive
)
971 audio_channel_mix( ch
, pOut32F
, frame_count
);
975 * Relinquishing conditions
976 * ------------------------------------------------------------------
980 /* Profiling information
981 * ----------------------------------------------- */
982 vg_profile_increment( &_vg_prof_audio_decode
);
983 vg_profile_increment( &_vg_prof_audio_mix
);
984 vg_prof_audio_mix
= _vg_prof_audio_mix
;
985 vg_prof_audio_decode
= _vg_prof_audio_decode
;
986 vg_audio
.samples_last
= frame_count
;
991 VG_STATIC
void audio_clip_load( audio_clip
*clip
, void *lin_alloc
)
993 if( lin_alloc
== NULL
)
994 lin_alloc
= vg_audio
.audio_pool
;
997 /* load in directly */
998 if( clip
->flags
& AUDIO_FLAG_VORBIS
)
1001 clip
->data
= vg_file_read( lin_alloc
, clip
->path
, &clip
->size
);
1005 vg_fatal_exit_loop( "Audio failed to load" );
1007 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
1008 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
1010 else if( clip
->flags
& AUDIO_FLAG_STEREO
)
1012 vg_fatal_exit_loop( "Unsupported format (Stereo uncompressed)" );
1016 vg_linear_clear( vg_mem
.scratch
);
1019 stb_vorbis_alloc alloc
= {
1020 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
1021 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
1024 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
1027 stb_vorbis
*decoder
= stb_vorbis_open_memory(
1028 filedata
, fsize
, &err
, &alloc
);
1032 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
1034 vg_fatal_exit_loop( "Vorbis decode error" );
1037 /* only mono is supported in uncompressed */
1038 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
1039 data_size
= length_samples
* sizeof(i16
);
1042 clip
->data
= vg_linear_alloc( lin_alloc
, vg_align8(data_size
) );
1043 clip
->size
= length_samples
;
1046 int read_samples
= stb_vorbis_get_samples_i16_downmixed(
1047 decoder
, clip
->data
, length_samples
);
1049 if( read_samples
!= length_samples
)
1050 vg_fatal_exit_loop( "Decode error" );
1052 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
1053 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip
->path
, mb
,
1058 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
)
1060 for( int i
=0; i
<count
; i
++ )
1061 audio_clip_load( &arr
[i
], lin_alloc
);
1064 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
1066 if( clip
->data
&& clip
->size
)
1070 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
1077 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
1079 if( !vg_audio
.debug_ui
)
1086 * -----------------------------------------------------------------------
1089 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1090 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1091 &vg_prof_audio_mix
}, 2,
1092 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1099 vg_uictx
.cursor
[0] = 258;
1100 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12;
1101 vg_uictx
.cursor
[2] = 150;
1102 vg_uictx
.cursor
[3] = 12;
1104 float mb1
= 1024.0f
*1024.0f
,
1105 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1106 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1107 percent
= (usage
/total
) * 100.0f
;
1109 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1111 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1112 vg_uictx
.cursor
[1] += 20;
1114 ui_rect overlap_buffer
[ AUDIO_CHANNELS
];
1115 u32 overlap_length
= 0;
1117 /* Draw audio stack */
1118 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
1120 audio_channel
*ch
= &vg_audio
.channels
[i
];
1122 vg_uictx
.cursor
[2] = 400;
1123 vg_uictx
.cursor
[3] = 18;
1127 if( !ch
->allocated
)
1129 ui_fill_rect( vg_uictx
.cursor
, 0x50333333 );
1132 vg_uictx
.cursor
[1] += 1;
1136 const char *formats
[] =
1144 int format_index
= 0;
1146 if( ch
->source
->flags
& AUDIO_FLAG_STEREO
)
1148 else if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
1153 snprintf( perf
, 127, "%02d %c%c%cD %s %4.2fv'%s'",
1155 (ch
->editable_state
.relinquished
)? 'r': ' ',
1158 formats
[format_index
],
1159 ch
->editable_state
.volume
,
1162 if( format_index
== 0 )
1164 ui_fill_rect( vg_uictx
.cursor
, 0xa00000ff );
1168 ui_fill_rect( vg_uictx
.cursor
, 0xa0333333 );
1171 vg_uictx
.cursor
[0] += 2;
1172 vg_uictx
.cursor
[1] += 2;
1173 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1176 vg_uictx
.cursor
[1] += 1;
1178 if( AUDIO_FLAG_SPACIAL_3D
)
1181 v3_copy( ch
->editable_state
.spacial_falloff
, wpos
);
1184 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1186 if( wpos
[3] > 0.0f
)
1188 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1189 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1192 wr
[0] = wpos
[0] * vg
.window_x
;
1193 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1197 for( int j
=0; j
<12; j
++ )
1200 for( int k
=0; k
<overlap_length
; k
++ )
1202 ui_px
*wk
= overlap_buffer
[k
];
1203 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1204 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1217 ui_text( wr
, perf
, 1, 0 );
1219 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1227 #endif /* VG_AUDIO_H */