1 /* Copyright (C) 2021-2023 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_FRAME_SIZE 512
44 #define AUDIO_MIX_FRAME_SIZE 256
46 #define AUDIO_CHANNELS 32
48 #define AUDIO_FILTERS 16
49 #define AUDIO_FLAG_LOOP 0x1
50 #define AUDIO_FLAG_SPACIAL_3D 0x4
51 #define AUDIO_FLAG_AUTO_START 0x8
53 /* Vorbis will ALWAYS use the maximum amount of channels it can */
54 //#define AUDIO_FLAG_MONO 0x100 NOTE: This is the default, so its not used
55 #define AUDIO_FLAG_STEREO 0x200
56 #define AUDIO_FLAG_VORBIS 0x400
58 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
59 #define AUDIO_MUTE_VOLUME 0.0f
60 #define AUDIO_BASE_VOLUME 1.0f
62 typedef struct audio_clip audio_clip
;
63 typedef struct audio_channel audio_channel
;
64 typedef struct audio_lfo audio_lfo
;
75 static struct vg_audio_system
77 SDL_AudioDeviceID sdl_output_device
;
86 SDL_mutex
*mux_checker
,
91 u32 time
, time_startframe
;
92 float sqrt_polynomial_coefficient
;
101 k_lfo_polynomial_bipolar
106 float polynomial_coefficient
;
109 u32 editble_state_write_mask
;
111 oscillators
[ AUDIO_LFOS
];
116 char name
[32]; /* only editable while allocated == 0 */
117 audio_clip
*source
; /* ... */
120 /* internal non-readable state
121 * -----------------------------*/
122 u32 cursor
, source_length
;
124 float volume_movement_start
,
130 stb_vorbis
*vorbis_handle
;
131 stb_vorbis_alloc vorbis_alloc
;
133 enum channel_activity
135 k_channel_activity_reset
, /* will advance if allocated==1, to wake */
136 k_channel_activity_wake
, /* will advance to either of next two */
137 k_channel_activity_alive
,
138 k_channel_activity_error
143 * editable structure, can be modified inside _lock and _unlock
144 * the edit mask tells which to copy into internal _, or to discard
145 * ----------------------------------------------------------------------
151 float volume
, /* current volume */
152 volume_target
, /* target volume */
160 v4f spacial_falloff
; /* xyz, range */
166 u32 editble_state_write_mask
;
168 channels
[ AUDIO_CHANNELS
];
170 /* System queue, and access from thread 0 */
171 int debug_ui
, debug_ui_3d
;
178 volume_target_internal
,
181 vg_audio
= { .volume_console
= 1.0f
};
183 #include "vg/vg_audio_dsp.h"
185 static struct vg_profile
186 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
187 .name
= "[T2] audio_decode()"},
188 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
189 .name
= "[T2] audio_mix()"},
190 _vg_prof_dsp
= {.mode
= k_profile_mode_accum
,
191 .name
= "[T2] dsp_process()"},
192 vg_prof_audio_decode
,
197 * These functions are called from the main thread and used to prevent bad
198 * access. TODO: They should be no-ops in release builds.
200 VG_STATIC
int audio_lock_checker_load(void)
203 SDL_LockMutex( vg_audio
.mux_checker
);
204 value
= vg_audio
.sync_locked
;
205 SDL_UnlockMutex( vg_audio
.mux_checker
);
209 VG_STATIC
void audio_lock_checker_store( int value
)
211 SDL_LockMutex( vg_audio
.mux_checker
);
212 vg_audio
.sync_locked
= value
;
213 SDL_UnlockMutex( vg_audio
.mux_checker
);
216 VG_STATIC
void audio_require_lock(void)
218 if( audio_lock_checker_load() )
221 vg_error( "Modifying sound effects systems requires locking\n" );
225 VG_STATIC
void audio_lock(void)
227 SDL_LockMutex( vg_audio
.mux_sync
);
228 audio_lock_checker_store(1);
231 VG_STATIC
void audio_unlock(void)
233 audio_lock_checker_store(0);
234 SDL_UnlockMutex( vg_audio
.mux_sync
);
237 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int frame_count
);
238 VG_STATIC
void vg_audio_init(void)
240 vg_audio
.mux_checker
= SDL_CreateMutex();
241 vg_audio
.mux_sync
= SDL_CreateMutex();
243 /* TODO: Move here? */
244 vg_var_push( (struct vg_var
){
245 .name
= "debug_audio",
246 .data
= &vg_audio
.debug_ui
,
247 .data_type
= k_var_dtype_i32
,
248 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
252 vg_var_push( (struct vg_var
){
254 .data
= &vg_audio
.volume_console
,
255 .data_type
= k_var_dtype_f32
,
256 .opt_f32
= { .min
=0.0f
, .max
=2.0f
, .clamp
=1 },
260 /* allocate memory */
263 vg_audio
.audio_pool
=
264 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32,
268 u32 decode_size
= AUDIO_DECODE_SIZE
* AUDIO_CHANNELS
;
269 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
273 SDL_AudioSpec spec_desired
, spec_got
;
274 spec_desired
.callback
= audio_mixer_callback
;
275 spec_desired
.channels
= 2;
276 spec_desired
.format
= AUDIO_F32
;
277 spec_desired
.freq
= 44100;
278 spec_desired
.padding
= 0;
279 spec_desired
.samples
= AUDIO_FRAME_SIZE
;
280 spec_desired
.silence
= 0;
281 spec_desired
.size
= 0;
282 spec_desired
.userdata
= NULL
;
284 vg_audio
.sdl_output_device
=
285 SDL_OpenAudioDevice( NULL
, 0, &spec_desired
, &spec_got
,0 );
287 if( vg_audio
.sdl_output_device
)
289 SDL_PauseAudioDevice( vg_audio
.sdl_output_device
, 0 );
294 "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
295 " Frequency: 44100 hz\n"
296 " Buffer size: 512\n"
298 " Format: s16 or f32\n" );
301 vg_success( "Ready\n" );
304 VG_STATIC
void vg_audio_free(void)
307 SDL_CloseAudioDevice( vg_audio
.sdl_output_device
);
314 #define AUDIO_EDIT_VOLUME_SLOPE 0x1
315 #define AUDIO_EDIT_VOLUME 0x2
316 #define AUDIO_EDIT_LFO_PERIOD 0x4
317 #define AUDIO_EDIT_LFO_WAVE 0x8
318 #define AUDIO_EDIT_LFO_ATTACHMENT 0x10
319 #define AUDIO_EDIT_SPACIAL 0x20
320 #define AUDIO_EDIT_OWNERSHIP 0x40
321 #define AUDIO_EDIT_SAMPLING_RATE 0x80
323 static audio_channel
*audio_request_channel( audio_clip
*clip
, u32 flags
)
325 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
327 audio_channel
*ch
= &vg_audio
.channels
[i
];
333 strcpy( ch
->name
, clip
->path
);
337 ch
->editable_state
.relinquished
= 0;
338 ch
->editable_state
.volume
= 1.0f
;
339 ch
->editable_state
.volume_target
= 1.0f
;
340 ch
->editable_state
.pan
= 0.0f
;
341 ch
->editable_state
.pan_target
= 0.0f
;
342 ch
->editable_state
.volume_rate
= 0;
343 ch
->editable_state
.pan_rate
= 0;
344 v4_copy((v4f
){0.0f
,0.0f
,0.0f
,1.0f
},ch
->editable_state
.spacial_falloff
);
345 ch
->editable_state
.lfo
= NULL
;
346 ch
->editable_state
.lfo_amount
= 0.0f
;
347 ch
->editable_state
.sampling_rate
= 1.0f
;
348 ch
->editble_state_write_mask
= 0x00;
356 static audio_channel
*audio_relinquish_channel( audio_channel
*ch
)
358 ch
->editable_state
.relinquished
= 1;
359 ch
->editble_state_write_mask
|= AUDIO_EDIT_OWNERSHIP
;
363 static void audio_channel_slope_volume( audio_channel
*ch
, float length
,
366 ch
->editable_state
.volume_target
= new_volume
;
367 ch
->editable_state
.volume_rate
= length
* 44100.0f
;
368 ch
->editble_state_write_mask
|= AUDIO_EDIT_VOLUME_SLOPE
;
371 static void audio_channel_set_sampling_rate( audio_channel
*ch
, float rate
)
373 ch
->editable_state
.sampling_rate
= rate
;
374 ch
->editble_state_write_mask
|= AUDIO_EDIT_SAMPLING_RATE
;
377 static void audio_channel_edit_volume( audio_channel
*ch
,
378 float new_volume
, int instant
)
382 ch
->editable_state
.volume
= new_volume
;
383 ch
->editble_state_write_mask
|= AUDIO_EDIT_VOLUME
;
387 audio_channel_slope_volume( ch
, 0.05f
, new_volume
);
391 static audio_channel
*audio_channel_fadeout( audio_channel
*ch
, float length
)
393 audio_channel_slope_volume( ch
, length
, 0.0f
);
394 return audio_relinquish_channel( ch
);
397 static void audio_channel_fadein( audio_channel
*ch
, float length
)
399 audio_channel_edit_volume( ch
, 0.0f
, 1 );
400 audio_channel_slope_volume( ch
, length
, 1.0f
);
403 static audio_channel
*audio_channel_crossfade( audio_channel
*ch
,
404 audio_clip
*new_clip
,
405 float length
, u32 flags
)
410 ch
= audio_channel_fadeout( ch
, length
);
412 audio_channel
*replacement
= audio_request_channel( new_clip
, flags
);
415 audio_channel_fadein( replacement
, length
);
420 static void audio_channel_sidechain_lfo( audio_channel
*ch
, int lfo_id
,
423 ch
->editable_state
.lfo
= &vg_audio
.oscillators
[ lfo_id
];
424 ch
->editable_state
.lfo_amount
= amount
;
425 ch
->editble_state_write_mask
|= AUDIO_EDIT_LFO_ATTACHMENT
;
428 static void audio_channel_set_spacial( audio_channel
*ch
, v3f co
, float range
)
430 if( ch
->flags
& AUDIO_FLAG_SPACIAL_3D
)
432 v3_copy( co
, ch
->editable_state
.spacial_falloff
);
433 ch
->editable_state
.spacial_falloff
[3] = 1.0f
/range
;
434 ch
->editble_state_write_mask
|= AUDIO_EDIT_SPACIAL
;
438 vg_warn( "Tried to set spacialization paramaters for 2D channel (%s)\n",
443 static int audio_oneshot_3d( audio_clip
*clip
, v3f position
,
444 float range
, float volume
)
446 audio_channel
*ch
= audio_request_channel( clip
, AUDIO_FLAG_SPACIAL_3D
);
450 audio_channel_set_spacial( ch
, position
, range
);
451 audio_channel_edit_volume( ch
, volume
, 1 );
452 ch
= audio_relinquish_channel( ch
);
460 static int audio_oneshot( audio_clip
*clip
, float volume
, float pan
)
462 audio_channel
*ch
= audio_request_channel( clip
, 0x00 );
466 audio_channel_edit_volume( ch
, volume
, 1 );
467 ch
= audio_relinquish_channel( ch
);
475 static void audio_set_lfo_wave( int id
, enum lfo_wave_type type
,
478 audio_lfo
*lfo
= &vg_audio
.oscillators
[ id
];
479 lfo
->editable_state
.polynomial_coefficient
= coefficient
;
480 lfo
->editable_state
.wave_type
= type
;
482 lfo
->editble_state_write_mask
|= AUDIO_EDIT_LFO_WAVE
;
485 static void audio_set_lfo_frequency( int id
, float freq
)
487 audio_lfo
*lfo
= &vg_audio
.oscillators
[ id
];
488 lfo
->editable_state
.period
= 44100.0f
/ freq
;
489 lfo
->editble_state_write_mask
|= AUDIO_EDIT_LFO_PERIOD
;
494 * -----------------------------------------------------------------------------
496 static int audio_channel_load_source( audio_channel
*ch
)
498 if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
500 /* Setup vorbis decoder */
501 u32 index
= ch
- vg_audio
.channels
;
503 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
504 *loc
= &buf
[AUDIO_DECODE_SIZE
*index
];
506 stb_vorbis_alloc alloc
= {
507 .alloc_buffer
= (char *)loc
,
508 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
512 stb_vorbis
*decoder
= stb_vorbis_open_memory(
514 ch
->source
->size
, &err
, &alloc
);
518 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
519 ch
->source
->path
, err
);
524 ch
->source_length
= stb_vorbis_stream_length_in_samples( decoder
);
525 ch
->vorbis_handle
= decoder
;
528 else if( ch
->source
->flags
& AUDIO_FLAG_STEREO
)
530 ch
->source_length
= ch
->source
->size
/ 2;
534 ch
->source_length
= ch
->source
->size
;
540 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
542 for( u32 i
=0; i
<count
; i
++ )
544 dst
[ i
*2 + 0 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
545 dst
[ i
*2 + 1 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
550 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
553 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
557 c
= VG_MIN( 1, f
->channels
- 1 );
561 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
566 for( int j
=0; j
< k
; ++j
)
568 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
569 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
573 f
->channel_buffer_start
+= k
;
578 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
586 * ........ more wrecked code sorry!
589 stb_vorbis_get_samples_i16_downmixed( stb_vorbis
*f
, i16
*buffer
, int len
)
592 c
= VG_MIN( 1, f
->channels
- 1 );
596 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
601 for( int j
=0; j
< k
; ++j
)
603 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
604 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
606 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
607 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
611 f
->channel_buffer_start
+= k
;
616 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
623 static inline float audio_lfo_pull_sample( audio_lfo
*lfo
)
627 if( lfo
->time
>= lfo
->_
.period
)
631 t
/= (float)lfo
->_
.period
;
633 if( lfo
->_
.wave_type
== k_lfo_polynomial_bipolar
)
650 return (( 2.0f
* lfo
->sqrt_polynomial_coefficient
* t
) /
651 /* --------------------------------------- */
652 ( 1.0f
+ lfo
->_
.polynomial_coefficient
* t
*t
)
662 static void audio_channel_get_samples( audio_channel
*ch
,
663 u32 count
, float *buf
)
665 vg_profile_begin( &_vg_prof_audio_decode
);
667 u32 remaining
= count
;
672 u32 samples_this_run
= VG_MIN( remaining
, ch
->source_length
-ch
->cursor
);
673 remaining
-= samples_this_run
;
675 float *dst
= &buf
[ buffer_pos
* 2 ];
677 if( ch
->source
->flags
& AUDIO_FLAG_STEREO
)
679 for( int i
=0;i
<samples_this_run
; i
++ )
685 else if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
687 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
692 if( read_samples
!= samples_this_run
)
694 vg_warn( "Invalid samples read (%s)\n", ch
->source
->path
);
696 for( int i
=0; i
<samples_this_run
; i
++ )
705 i16
*src_buffer
= ch
->source
->data
,
706 *src
= &src_buffer
[ch
->cursor
];
708 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
711 ch
->cursor
+= samples_this_run
;
712 buffer_pos
+= samples_this_run
;
714 if( (ch
->flags
& AUDIO_FLAG_LOOP
) && remaining
)
716 if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
717 stb_vorbis_seek_start( ch
->vorbis_handle
);
728 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
729 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
735 vg_profile_end( &_vg_prof_audio_decode
);
738 static void audio_channel_mix( audio_channel
*ch
, float *buffer
)
740 u32 buffer_length
= AUDIO_MIX_FRAME_SIZE
;
741 if( ch
->_
.sampling_rate
!= 1.0f
)
743 float l
= ceilf( (float)(AUDIO_MIX_FRAME_SIZE
) * ch
->_
.sampling_rate
);
747 float pcf
[ AUDIO_MIX_FRAME_SIZE
* 2 * 2 ];
749 audio_channel_get_samples( ch
, buffer_length
, pcf
);
751 float framevol_l
= 1.0f
,
754 if( ch
->flags
& AUDIO_FLAG_SPACIAL_3D
)
756 if( !vg_validf(vg_audio
.listener_pos
[0]) ||
757 !vg_validf(vg_audio
.listener_pos
[1]) ||
758 !vg_validf(vg_audio
.listener_pos
[2]) ||
759 !vg_validf(ch
->_
.spacial_falloff
[0]) ||
760 !vg_validf(ch
->_
.spacial_falloff
[1]) ||
761 !vg_validf(ch
->_
.spacial_falloff
[2]) )
763 vg_error( "NaN listener/world position (%s)\n", ch
->name
);
770 v3_sub( ch
->_
.spacial_falloff
, vg_audio
.listener_pos
, delta
);
772 float dist
= v3_length( delta
),
773 vol
= vg_maxf( 0.0f
, 1.0f
- ch
->_
.spacial_falloff
[3]*dist
);
775 v3_muls( delta
, 1.0f
/dist
, delta
);
776 float pan
= v3_dot( vg_audio
.listener_ears
, delta
);
777 vol
= powf( vol
, 5.0f
);
779 framevol_l
*= (vol
* 0.5f
) * (1.0f
- pan
);
780 framevol_r
*= (vol
* 0.5f
) * (1.0f
+ pan
);
783 vg_profile_begin( &_vg_prof_audio_mix
);
785 float volume_movement
= ch
->volume_movement
;
786 float const fvolume_rate
= vg_maxf( 1.0f
, ch
->_
.volume_rate
);
787 const float inv_volume_rate
= 1.0f
/fvolume_rate
;
789 float volume
= ch
->_
.volume
;
790 const float volume_start
= ch
->volume_movement_start
;
791 const float volume_target
= ch
->_
.volume_target
;
793 for( u32 j
=0; j
<AUDIO_MIX_FRAME_SIZE
; j
++ )
796 * there is some REALLY weird behaviour with minss,
797 * i cannot begin to guess what the cause is, but the bahaviour when
798 * the second argument is not 1.0 would seemingly tripple or up to
799 * eight times this routine.
801 * the times it would happen are when moving from empty space into areas
802 * with geometry. in the bvh for skate rift.
804 * it should be completely unrelated to this, but somehow -- it is
805 * effecting the speed of minss. and severely at that too.
808 volume_movement
+= 1.0f
;
809 float movement_t
= volume_movement
* inv_volume_rate
;
810 movement_t
= vg_minf( volume_movement
, 1.0f
);
811 volume
= vg_lerpf( volume_start
, volume_target
, movement_t
);
813 float vol_norm
= volume
* volume
;
816 vol_norm
*= 1.0f
+ audio_lfo_pull_sample(ch
->_
.lfo
) * ch
->_
.lfo_amount
;
818 float vol_l
= vol_norm
* framevol_l
,
819 vol_r
= vol_norm
* framevol_r
,
823 if( ch
->_
.sampling_rate
!= 1.0f
)
825 /* absolutely garbage resampling, but it will do
828 float sample_index
= ch
->_
.sampling_rate
* (float)j
;
829 float t
= vg_fractf( sample_index
);
831 u32 i0
= floorf( sample_index
),
834 sample_l
= pcf
[ i0
*2+0 ]*(1.0f
-t
) + pcf
[ i1
*2+0 ]*t
;
835 sample_r
= pcf
[ i0
*2+1 ]*(1.0f
-t
) + pcf
[ i1
*2+1 ]*t
;
839 sample_l
= pcf
[ j
*2+0 ];
840 sample_r
= pcf
[ j
*2+1 ];
843 buffer
[ j
*2+0 ] += sample_l
* vol_l
;
844 buffer
[ j
*2+1 ] += sample_r
* vol_r
;
847 ch
->volume_movement
+= AUDIO_MIX_FRAME_SIZE
;
848 ch
->volume_movement
= VG_MIN( ch
->volume_movement
, ch
->_
.volume_rate
);
849 ch
->_
.volume
= volume
;
851 vg_profile_end( &_vg_prof_audio_mix
);
854 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int byte_count
)
857 * Copy data and move edit flags to commit flags
858 * ------------------------------------------------------------- */
860 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
862 audio_channel
*ch
= &vg_audio
.channels
[i
];
867 /* process relinquishments */
868 if( (ch
->activity
!= k_channel_activity_reset
) && ch
->_
.relinquished
)
870 if( (ch
->cursor
>= ch
->source_length
&& !(ch
->flags
& AUDIO_FLAG_LOOP
))
871 || (ch
->_
.volume
== 0.0f
)
872 || (ch
->activity
== k_channel_activity_error
) )
874 ch
->_
.relinquished
= 0;
876 ch
->activity
= k_channel_activity_reset
;
881 /* process new channels */
882 if( ch
->activity
== k_channel_activity_reset
)
884 ch
->_
= ch
->editable_state
;
886 ch
->source_length
= 0;
887 ch
->activity
= k_channel_activity_wake
;
890 if( ch
->editble_state_write_mask
& AUDIO_EDIT_OWNERSHIP
)
891 ch
->_
.relinquished
= ch
->editable_state
.relinquished
;
893 ch
->editable_state
.relinquished
= ch
->_
.relinquished
;
896 if( ch
->editble_state_write_mask
& AUDIO_EDIT_VOLUME
)
897 ch
->_
.volume
= ch
->editable_state
.volume
;
899 ch
->editable_state
.volume
= ch
->_
.volume
;
902 if( ch
->editble_state_write_mask
& AUDIO_EDIT_VOLUME_SLOPE
)
904 ch
->volume_movement_start
= ch
->_
.volume
;
905 ch
->volume_movement
= 0;
907 ch
->_
.volume_target
= ch
->editable_state
.volume_target
;
908 ch
->_
.volume_rate
= ch
->editable_state
.volume_rate
;
912 ch
->editable_state
.volume_target
= ch
->_
.volume_target
;
913 ch
->editable_state
.volume_rate
= ch
->_
.volume_rate
;
917 if( ch
->editble_state_write_mask
& AUDIO_EDIT_SAMPLING_RATE
)
918 ch
->_
.sampling_rate
= ch
->editable_state
.sampling_rate
;
920 ch
->editable_state
.sampling_rate
= ch
->_
.sampling_rate
;
923 if( ch
->editble_state_write_mask
& AUDIO_EDIT_LFO_ATTACHMENT
)
925 ch
->_
.lfo
= ch
->editable_state
.lfo
;
926 ch
->_
.lfo_amount
= ch
->editable_state
.lfo_amount
;
930 ch
->editable_state
.lfo
= ch
->_
.lfo
;
931 ch
->editable_state
.lfo_amount
= ch
->_
.lfo_amount
;
935 if( ch
->editble_state_write_mask
& AUDIO_EDIT_SPACIAL
)
936 v4_copy( ch
->editable_state
.spacial_falloff
,ch
->_
.spacial_falloff
);
938 v4_copy( ch
->_
.spacial_falloff
,ch
->editable_state
.spacial_falloff
);
941 /* currently readonly, i guess */
942 ch
->editable_state
.pan_target
= ch
->_
.pan_target
;
943 ch
->editable_state
.pan
= ch
->_
.pan
;
944 ch
->editble_state_write_mask
= 0x00;
947 for( int i
=0; i
<AUDIO_LFOS
; i
++ )
949 audio_lfo
*lfo
= &vg_audio
.oscillators
[ i
];
951 if( lfo
->editble_state_write_mask
& AUDIO_EDIT_LFO_WAVE
)
953 lfo
->_
.wave_type
= lfo
->editable_state
.wave_type
;
955 if( lfo
->_
.wave_type
== k_lfo_polynomial_bipolar
)
957 lfo
->_
.polynomial_coefficient
=
958 lfo
->editable_state
.polynomial_coefficient
;
959 lfo
->sqrt_polynomial_coefficient
=
960 sqrtf(lfo
->_
.polynomial_coefficient
);
964 if( lfo
->editble_state_write_mask
& AUDIO_EDIT_LFO_PERIOD
)
969 t
/= (float)lfo
->_
.period
;
971 lfo
->_
.period
= lfo
->editable_state
.period
;
972 lfo
->time
= lfo
->_
.period
* t
;
977 lfo
->_
.period
= lfo
->editable_state
.period
;
981 lfo
->editble_state_write_mask
= 0x00;
984 dsp_update_tunings();
989 * ------------------------------------------------------------- */
990 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
992 audio_channel
*ch
= &vg_audio
.channels
[i
];
994 if( ch
->activity
== k_channel_activity_wake
)
996 if( audio_channel_load_source( ch
) )
997 ch
->activity
= k_channel_activity_alive
;
999 ch
->activity
= k_channel_activity_error
;
1005 * -------------------------------------------------------- */
1006 int frame_count
= byte_count
/(2*sizeof(float));
1009 float *pOut32F
= (float *)stream
;
1010 for( int i
=0; i
<frame_count
*2; i
++ )
1013 for( int i
=0; i
<AUDIO_LFOS
; i
++ )
1015 audio_lfo
*lfo
= &vg_audio
.oscillators
[i
];
1016 lfo
->time_startframe
= lfo
->time
;
1019 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
1021 audio_channel
*ch
= &vg_audio
.channels
[i
];
1023 if( ch
->activity
== k_channel_activity_alive
)
1026 ch
->_
.lfo
->time
= ch
->_
.lfo
->time_startframe
;
1028 u32 remaining
= frame_count
,
1033 audio_channel_mix( ch
, pOut32F
+subpos
);
1034 remaining
-= AUDIO_MIX_FRAME_SIZE
;
1035 subpos
+= AUDIO_MIX_FRAME_SIZE
*2;
1040 vg_profile_begin( &_vg_prof_dsp
);
1042 for( int i
=0; i
<frame_count
; i
++ )
1043 vg_dsp_process( pOut32F
+ i
*2, pOut32F
+ i
*2 );
1045 vg_profile_end( &_vg_prof_dsp
);
1048 * Relinquishing conditions
1049 * ------------------------------------------------------------------
1053 /* Profiling information
1054 * ----------------------------------------------- */
1055 vg_profile_increment( &_vg_prof_audio_decode
);
1056 vg_profile_increment( &_vg_prof_audio_mix
);
1057 vg_profile_increment( &_vg_prof_dsp
);
1059 vg_prof_audio_mix
= _vg_prof_audio_mix
;
1060 vg_prof_audio_decode
= _vg_prof_audio_decode
;
1061 vg_prof_audio_dsp
= _vg_prof_dsp
;
1063 vg_audio
.samples_last
= frame_count
;
1065 if( vg_audio
.debug_ui
)
1067 vg_dsp_update_texture();
1073 VG_STATIC
void audio_clip_load( audio_clip
*clip
, void *lin_alloc
)
1075 if( lin_alloc
== NULL
)
1076 lin_alloc
= vg_audio
.audio_pool
;
1079 /* load in directly */
1080 if( clip
->flags
& AUDIO_FLAG_VORBIS
)
1083 clip
->data
= vg_file_read( lin_alloc
, clip
->path
, &clip
->size
);
1087 vg_fatal_exit_loop( "Audio failed to load" );
1089 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
1090 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
1092 else if( clip
->flags
& AUDIO_FLAG_STEREO
)
1094 vg_fatal_exit_loop( "Unsupported format (Stereo uncompressed)" );
1098 vg_linear_clear( vg_mem
.scratch
);
1101 stb_vorbis_alloc alloc
= {
1102 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
1103 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
1106 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
1109 stb_vorbis
*decoder
= stb_vorbis_open_memory(
1110 filedata
, fsize
, &err
, &alloc
);
1114 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
1116 vg_fatal_exit_loop( "Vorbis decode error" );
1119 /* only mono is supported in uncompressed */
1120 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
1121 data_size
= length_samples
* sizeof(i16
);
1124 clip
->data
= vg_linear_alloc( lin_alloc
, vg_align8(data_size
) );
1125 clip
->size
= length_samples
;
1128 int read_samples
= stb_vorbis_get_samples_i16_downmixed(
1129 decoder
, clip
->data
, length_samples
);
1131 if( read_samples
!= length_samples
)
1132 vg_fatal_exit_loop( "Decode error" );
1134 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
1135 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip
->path
, mb
,
1140 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
)
1142 for( int i
=0; i
<count
; i
++ )
1143 audio_clip_load( &arr
[i
], lin_alloc
);
1146 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
1148 if( clip
->data
&& clip
->size
)
1152 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
1159 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
1161 if( !vg_audio
.debug_ui
)
1166 glBindTexture( GL_TEXTURE_2D
, vg_dsp
.view_texture
);
1167 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 256, 256,
1168 GL_RGBA
, GL_UNSIGNED_BYTE
,
1169 vg_dsp
.view_texture_buffer
);
1173 * -----------------------------------------------------------------------
1176 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1177 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1179 &vg_prof_audio_dsp
}, 3,
1180 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1187 vg_uictx
.cursor
[0] = 512 + 8;
1188 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12+12;
1189 vg_uictx
.cursor
[2] = 150;
1190 vg_uictx
.cursor
[3] = 12;
1192 ui_rect view_thing
= { 4, vg
.window_y
-512-4, 512, 512 };
1193 ui_push_image( view_thing
, vg_dsp
.view_texture
);
1195 float mb1
= 1024.0f
*1024.0f
,
1196 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1197 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1198 percent
= (usage
/total
) * 100.0f
;
1200 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1202 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1203 vg_uictx
.cursor
[1] += 20;
1205 ui_rect overlap_buffer
[ AUDIO_CHANNELS
];
1206 u32 overlap_length
= 0;
1208 /* Draw audio stack */
1209 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ )
1211 audio_channel
*ch
= &vg_audio
.channels
[i
];
1213 vg_uictx
.cursor
[2] = 400;
1214 vg_uictx
.cursor
[3] = 18;
1218 if( !ch
->allocated
)
1220 ui_fill_rect( vg_uictx
.cursor
, 0x50333333 );
1223 vg_uictx
.cursor
[1] += 1;
1227 const char *formats
[] =
1235 int format_index
= 0;
1237 if( ch
->source
->flags
& AUDIO_FLAG_STEREO
)
1239 else if( ch
->source
->flags
& AUDIO_FLAG_VORBIS
)
1244 snprintf( perf
, 127, "%02d %c%c%cD %s %4.2fv'%s'",
1246 (ch
->editable_state
.relinquished
)? 'r': ' ',
1249 formats
[format_index
],
1250 ch
->editable_state
.volume
,
1253 if( format_index
== 0 )
1255 ui_fill_rect( vg_uictx
.cursor
, 0xa00000ff );
1259 ui_fill_rect( vg_uictx
.cursor
, 0xa0333333 );
1262 vg_uictx
.cursor
[0] += 2;
1263 vg_uictx
.cursor
[1] += 2;
1264 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1267 vg_uictx
.cursor
[1] += 1;
1269 if( AUDIO_FLAG_SPACIAL_3D
)
1272 v3_copy( ch
->editable_state
.spacial_falloff
, wpos
);
1275 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1277 if( wpos
[3] > 0.0f
)
1279 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1280 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1283 wr
[0] = wpos
[0] * vg
.window_x
;
1284 wr
[1] = (1.0f
-wpos
[1]) * vg
.window_y
;
1288 for( int j
=0; j
<12; j
++ )
1291 for( int k
=0; k
<overlap_length
; k
++ )
1293 ui_px
*wk
= overlap_buffer
[k
];
1294 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1295 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1308 ui_text( wr
, perf
, 1, 0 );
1310 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1318 #endif /* VG_AUDIO_H */