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"
17 #include "vg/vg_audio_synth_bird.h"
24 #pragma GCC push_options
25 #pragma GCC optimize ("O3")
26 #pragma GCC diagnostic push
27 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31 #define STB_VORBIS_MAX_CHANNELS 2
32 #include "submodules/stb/stb_vorbis.c"
39 #pragma GCC pop_options
40 #pragma GCC diagnostic pop
44 #define AUDIO_FRAME_SIZE 512
45 #define AUDIO_MIX_FRAME_SIZE 256
47 #define AUDIO_CHANNELS 32
49 #define AUDIO_FILTERS 16
50 #define AUDIO_FLAG_LOOP 0x1
51 #define AUDIO_FLAG_NO_DOPPLER 0x2
52 #define AUDIO_FLAG_SPACIAL_3D 0x4
53 #define AUDIO_FLAG_AUTO_START 0x8
54 #define AUDIO_FLAG_FORMAT 0x1E00
58 k_audio_format_mono
= 0x000u
,
59 k_audio_format_stereo
= 0x200u
,
60 k_audio_format_vorbis
= 0x400u
,
61 k_audio_format_none0
= 0x600u
,
62 k_audio_format_none1
= 0x800u
,
63 k_audio_format_none2
= 0xA00u
,
64 k_audio_format_none3
= 0xC00u
,
65 k_audio_format_none4
= 0xE00u
,
67 k_audio_format_bird
= 0x1000u
,
68 k_audio_format_none5
= 0x1200u
,
69 k_audio_format_none6
= 0x1400u
,
70 k_audio_format_none7
= 0x1600u
,
71 k_audio_format_none8
= 0x1800u
,
72 k_audio_format_none9
= 0x1A00u
,
73 k_audio_format_none10
= 0x1C00u
,
74 k_audio_format_none11
= 0x1E00u
,
77 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
78 #define AUDIO_MUTE_VOLUME 0.0f
79 #define AUDIO_BASE_VOLUME 1.0f
81 typedef struct audio_clip audio_clip
;
82 typedef struct audio_channel audio_channel
;
83 typedef struct audio_lfo audio_lfo
;
92 static struct vg_audio_system
{
93 SDL_AudioDeviceID sdl_output_device
;
102 SDL_SpinLock sl_checker
,
106 u32 time
, time_startframe
;
107 float sqrt_polynomial_coefficient
;
114 k_lfo_polynomial_bipolar
119 float polynomial_coefficient
;
122 u32 editble_state_write_mask
;
124 oscillators
[ AUDIO_LFOS
];
126 struct audio_channel
{
130 char name
[32]; /* only editable while allocated == 0 */
131 audio_clip
*source
; /* ... */
133 u32 colour
; /* ... */
135 /* internal non-readable state
136 * -----------------------------*/
137 u32 cursor
, source_length
;
139 float volume_movement_start
,
146 struct synth_bird
*bird_handle
;
147 stb_vorbis
*vorbis_handle
;
150 stb_vorbis_alloc vorbis_alloc
;
152 enum channel_activity
{
153 k_channel_activity_reset
, /* will advance if allocated==1, to wake */
154 k_channel_activity_wake
, /* will advance to either of next two */
155 k_channel_activity_alive
,
156 k_channel_activity_end
,
157 k_channel_activity_error
163 * editable structure, can be modified inside _lock and _unlock
164 * the edit mask tells which to copy into internal _, or to discard
165 * ----------------------------------------------------------------------
167 struct channel_state
{
170 float volume
, /* current volume */
171 volume_target
, /* target volume */
179 v4f spacial_falloff
; /* xyz, range */
185 u32 editble_state_write_mask
;
187 channels
[ AUDIO_CHANNELS
];
189 int debug_ui
, debug_ui_3d
, debug_dsp
;
191 v3f internal_listener_pos
,
192 internal_listener_ears
,
193 internal_listener_velocity
,
195 external_listener_pos
,
196 external_listener_ears
,
197 external_lister_velocity
;
199 float internal_global_volume
,
200 external_global_volume
;
202 vg_audio
= { .external_global_volume
= 1.0f
};
204 #include "vg/vg_audio_dsp.h"
206 static struct vg_profile
207 _vg_prof_audio_decode
= {.mode
= k_profile_mode_accum
,
208 .name
= "[T2] audio_decode()"},
209 _vg_prof_audio_mix
= {.mode
= k_profile_mode_accum
,
210 .name
= "[T2] audio_mix()"},
211 _vg_prof_dsp
= {.mode
= k_profile_mode_accum
,
212 .name
= "[T2] dsp_process()"},
213 vg_prof_audio_decode
,
218 * These functions are called from the main thread and used to prevent bad
219 * access. TODO: They should be no-ops in release builds.
221 VG_STATIC
int audio_lock_checker_load(void)
224 SDL_AtomicLock( &vg_audio
.sl_checker
);
225 value
= vg_audio
.sync_locked
;
226 SDL_AtomicUnlock( &vg_audio
.sl_checker
);
230 VG_STATIC
void audio_lock_checker_store( int value
)
232 SDL_AtomicLock( &vg_audio
.sl_checker
);
233 vg_audio
.sync_locked
= value
;
234 SDL_AtomicUnlock( &vg_audio
.sl_checker
);
237 VG_STATIC
void audio_require_lock(void)
239 if( audio_lock_checker_load() )
242 vg_error( "Modifying sound effects systems requires locking\n" );
246 VG_STATIC
void audio_lock(void)
248 SDL_AtomicLock( &vg_audio
.sl_sync
);
249 audio_lock_checker_store(1);
252 VG_STATIC
void audio_unlock(void)
254 audio_lock_checker_store(0);
255 SDL_AtomicUnlock( &vg_audio
.sl_sync
);
258 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int frame_count
);
259 VG_STATIC
void vg_audio_init(void)
261 /* TODO: Move here? */
262 vg_console_reg_var( "debug_audio", &vg_audio
.debug_ui
,
263 k_var_dtype_i32
, VG_VAR_CHEAT
);
264 vg_console_reg_var( "debug_dsp", &vg_audio
.debug_dsp
,
265 k_var_dtype_i32
, VG_VAR_CHEAT
);
266 vg_console_reg_var( "volume", &vg_audio
.external_global_volume
,
267 k_var_dtype_f32
, VG_VAR_PERSISTENT
);
269 /* allocate memory */
271 vg_audio
.audio_pool
=
272 vg_create_linear_allocator( vg_mem
.rtmemory
, 1024*1024*32,
276 u32 decode_size
= AUDIO_DECODE_SIZE
* AUDIO_CHANNELS
;
277 vg_audio
.decode_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, decode_size
);
281 SDL_AudioSpec spec_desired
, spec_got
;
282 spec_desired
.callback
= audio_mixer_callback
;
283 spec_desired
.channels
= 2;
284 spec_desired
.format
= AUDIO_F32
;
285 spec_desired
.freq
= 44100;
286 spec_desired
.padding
= 0;
287 spec_desired
.samples
= AUDIO_FRAME_SIZE
;
288 spec_desired
.silence
= 0;
289 spec_desired
.size
= 0;
290 spec_desired
.userdata
= NULL
;
292 vg_audio
.sdl_output_device
=
293 SDL_OpenAudioDevice( NULL
, 0, &spec_desired
, &spec_got
,0 );
295 if( vg_audio
.sdl_output_device
){
296 SDL_PauseAudioDevice( vg_audio
.sdl_output_device
, 0 );
300 "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
301 " Frequency: 44100 hz\n"
302 " Buffer size: 512\n"
304 " Format: s16 or f32\n" );
308 VG_STATIC
void vg_audio_free(void)
311 SDL_CloseAudioDevice( vg_audio
.sdl_output_device
);
318 #define AUDIO_EDIT_VOLUME_SLOPE 0x1
319 #define AUDIO_EDIT_VOLUME 0x2
320 #define AUDIO_EDIT_LFO_PERIOD 0x4
321 #define AUDIO_EDIT_LFO_WAVE 0x8
322 #define AUDIO_EDIT_LFO_ATTACHMENT 0x10
323 #define AUDIO_EDIT_SPACIAL 0x20
324 #define AUDIO_EDIT_OWNERSHIP 0x40
325 #define AUDIO_EDIT_SAMPLING_RATE 0x80
327 static void audio_channel_init( audio_channel
*ch
, audio_clip
*clip
, u32 flags
)
332 ch
->colour
= 0x00333333;
334 if( (ch
->source
->flags
& AUDIO_FLAG_FORMAT
) == k_audio_format_bird
)
335 strcpy( ch
->name
, "[array]" );
337 vg_strncpy( clip
->path
, ch
->name
, 32, k_strncpy_always_add_null
);
341 ch
->editable_state
.relinquished
= 0;
342 ch
->editable_state
.volume
= 1.0f
;
343 ch
->editable_state
.volume_target
= 1.0f
;
344 ch
->editable_state
.pan
= 0.0f
;
345 ch
->editable_state
.pan_target
= 0.0f
;
346 ch
->editable_state
.volume_rate
= 0;
347 ch
->editable_state
.pan_rate
= 0;
348 v4_copy((v4f
){0.0f
,0.0f
,0.0f
,1.0f
},ch
->editable_state
.spacial_falloff
);
349 ch
->editable_state
.lfo
= NULL
;
350 ch
->editable_state
.lfo_amount
= 0.0f
;
351 ch
->editable_state
.sampling_rate
= 1.0f
;
352 ch
->editble_state_write_mask
= 0x00;
355 static void audio_channel_group( audio_channel
*ch
, u32 group
)
358 ch
->colour
= ((group
* 29986577) & 0x00ffffff) | 0xff000000;
361 static audio_channel
*audio_get_first_idle_channel(void)
363 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
364 audio_channel
*ch
= &vg_audio
.channels
[i
];
366 if( !ch
->allocated
){
374 static audio_channel
*audio_get_group_idle_channel( u32 group
, u32 max_count
)
377 audio_channel
*dest
= NULL
;
379 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
380 audio_channel
*ch
= &vg_audio
.channels
[i
];
383 if( ch
->group
== group
){
393 if( dest
&& (count
< max_count
) ){
400 static audio_channel
*audio_get_group_first_active_channel( u32 group
)
402 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
403 audio_channel
*ch
= &vg_audio
.channels
[i
];
404 if( ch
->allocated
&& (ch
->group
== group
) )
410 static int audio_channel_finished( audio_channel
*ch
)
412 if( ch
->readable_activity
== k_channel_activity_end
)
418 static audio_channel
*audio_relinquish_channel( audio_channel
*ch
)
420 ch
->editable_state
.relinquished
= 1;
421 ch
->editble_state_write_mask
|= AUDIO_EDIT_OWNERSHIP
;
425 static void audio_channel_slope_volume( audio_channel
*ch
, float length
,
428 ch
->editable_state
.volume_target
= new_volume
;
429 ch
->editable_state
.volume_rate
= length
* 44100.0f
;
430 ch
->editble_state_write_mask
|= AUDIO_EDIT_VOLUME_SLOPE
;
433 static void audio_channel_set_sampling_rate( audio_channel
*ch
, float rate
)
435 ch
->editable_state
.sampling_rate
= rate
;
436 ch
->editble_state_write_mask
|= AUDIO_EDIT_SAMPLING_RATE
;
439 static void audio_channel_edit_volume( audio_channel
*ch
,
440 float new_volume
, int instant
)
443 ch
->editable_state
.volume
= new_volume
;
444 ch
->editble_state_write_mask
|= AUDIO_EDIT_VOLUME
;
447 audio_channel_slope_volume( ch
, 0.05f
, new_volume
);
451 static audio_channel
*audio_channel_fadeout( audio_channel
*ch
, float length
)
453 audio_channel_slope_volume( ch
, length
, 0.0f
);
454 return audio_relinquish_channel( ch
);
457 static void audio_channel_fadein( audio_channel
*ch
, float length
)
459 audio_channel_edit_volume( ch
, 0.0f
, 1 );
460 audio_channel_slope_volume( ch
, length
, 1.0f
);
463 static audio_channel
*audio_channel_crossfade( audio_channel
*ch
,
464 audio_clip
*new_clip
,
465 float length
, u32 flags
)
470 ch
= audio_channel_fadeout( ch
, length
);
472 audio_channel
*replacement
= audio_get_first_idle_channel();
475 audio_channel_init( replacement
, new_clip
, flags
);
476 audio_channel_fadein( replacement
, length
);
482 static void audio_channel_sidechain_lfo( audio_channel
*ch
, int lfo_id
,
485 ch
->editable_state
.lfo
= &vg_audio
.oscillators
[ lfo_id
];
486 ch
->editable_state
.lfo_amount
= amount
;
487 ch
->editble_state_write_mask
|= AUDIO_EDIT_LFO_ATTACHMENT
;
490 static void audio_channel_set_spacial( audio_channel
*ch
, v3f co
, float range
)
492 if( ch
->flags
& AUDIO_FLAG_SPACIAL_3D
){
493 v3_copy( co
, ch
->editable_state
.spacial_falloff
);
496 ch
->editable_state
.spacial_falloff
[3] = 1.0f
;
498 ch
->editable_state
.spacial_falloff
[3] = 1.0f
/range
;
500 ch
->editble_state_write_mask
|= AUDIO_EDIT_SPACIAL
;
503 vg_warn( "Tried to set spacialization paramaters for 2D channel (%s)\n",
508 static int audio_oneshot_3d( audio_clip
*clip
, v3f position
,
509 float range
, float volume
)
511 audio_channel
*ch
= audio_get_first_idle_channel();
514 audio_channel_init( ch
, clip
, AUDIO_FLAG_SPACIAL_3D
);
515 audio_channel_set_spacial( ch
, position
, range
);
516 audio_channel_edit_volume( ch
, volume
, 1 );
517 ch
= audio_relinquish_channel( ch
);
525 static int audio_oneshot( audio_clip
*clip
, float volume
, float pan
)
527 audio_channel
*ch
= audio_get_first_idle_channel();
530 audio_channel_init( ch
, clip
, 0x00 );
531 audio_channel_edit_volume( ch
, volume
, 1 );
532 ch
= audio_relinquish_channel( ch
);
540 static void audio_set_lfo_wave( int id
, enum lfo_wave_type type
,
543 audio_lfo
*lfo
= &vg_audio
.oscillators
[ id
];
544 lfo
->editable_state
.polynomial_coefficient
= coefficient
;
545 lfo
->editable_state
.wave_type
= type
;
547 lfo
->editble_state_write_mask
|= AUDIO_EDIT_LFO_WAVE
;
550 static void audio_set_lfo_frequency( int id
, float freq
)
552 audio_lfo
*lfo
= &vg_audio
.oscillators
[ id
];
553 lfo
->editable_state
.period
= 44100.0f
/ freq
;
554 lfo
->editble_state_write_mask
|= AUDIO_EDIT_LFO_PERIOD
;
560 * -----------------------------------------------------------------------------
562 static int audio_channel_load_source( audio_channel
*ch
)
564 u32 format
= ch
->source
->flags
& AUDIO_FLAG_FORMAT
;
566 if( format
== k_audio_format_vorbis
){
567 /* Setup vorbis decoder */
568 u32 index
= ch
- vg_audio
.channels
;
570 u8
*buf
= (u8
*)vg_audio
.decode_buffer
,
571 *loc
= &buf
[AUDIO_DECODE_SIZE
*index
];
573 stb_vorbis_alloc alloc
= {
574 .alloc_buffer
= (char *)loc
,
575 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
579 stb_vorbis
*decoder
= stb_vorbis_open_memory(
581 ch
->source
->size
, &err
, &alloc
);
584 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
585 ch
->source
->path
, err
);
589 ch
->source_length
= stb_vorbis_stream_length_in_samples( decoder
);
590 ch
->vorbis_handle
= decoder
;
593 else if( format
== k_audio_format_bird
){
594 u32 index
= ch
- vg_audio
.channels
;
596 u8
*buf
= (u8
*)vg_audio
.decode_buffer
;
597 struct synth_bird
*loc
= (void *)&buf
[AUDIO_DECODE_SIZE
*index
];
599 memcpy( loc
, ch
->source
->data
, ch
->source
->size
);
600 synth_bird_reset( loc
);
602 ch
->bird_handle
= loc
;
603 ch
->source_length
= synth_bird_get_length_in_samples( loc
);
605 else if( format
== k_audio_format_stereo
){
606 ch
->source_length
= ch
->source
->size
/ 2;
609 ch
->source_length
= ch
->source
->size
;
615 VG_STATIC
void audio_decode_uncompressed_mono( i16
*src
, u32 count
, float *dst
)
617 for( u32 i
=0; i
<count
; i
++ ){
618 dst
[ i
*2 + 0 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
619 dst
[ i
*2 + 1 ] = ((float)src
[i
]) * (1.0f
/32767.0f
);
624 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
627 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis
*f
, float *buffer
,
631 c
= VG_MIN( 1, f
->channels
- 1 );
634 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
639 for( int j
=0; j
< k
; ++j
) {
640 *buffer
++ = f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
];
641 *buffer
++ = f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
645 f
->channel_buffer_start
+= k
;
650 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
658 * ........ more wrecked code sorry!
661 stb_vorbis_get_samples_i16_downmixed( stb_vorbis
*f
, i16
*buffer
, int len
)
664 c
= VG_MIN( 1, f
->channels
- 1 );
667 int k
= f
->channel_buffer_end
- f
->channel_buffer_start
;
672 for( int j
=0; j
< k
; ++j
) {
673 float sl
= f
->channel_buffers
[ 0 ][f
->channel_buffer_start
+j
],
674 sr
= f
->channel_buffers
[ c
][f
->channel_buffer_start
+j
];
676 *buffer
++ = vg_clampf( 0.5f
*(sl
+sr
), -1.0f
, 1.0f
) * 32767.0f
;
677 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
681 f
->channel_buffer_start
+= k
;
686 if( !stb_vorbis_get_frame_float( f
, NULL
, NULL
))
693 static inline float audio_lfo_pull_sample( audio_lfo
*lfo
)
697 if( lfo
->time
>= lfo
->_
.period
)
701 t
/= (float)lfo
->_
.period
;
703 if( lfo
->_
.wave_type
== k_lfo_polynomial_bipolar
){
719 return (( 2.0f
* lfo
->sqrt_polynomial_coefficient
* t
) /
720 /* --------------------------------------- */
721 ( 1.0f
+ lfo
->_
.polynomial_coefficient
* t
*t
)
730 static void audio_channel_get_samples( audio_channel
*ch
,
731 u32 count
, float *buf
)
733 vg_profile_begin( &_vg_prof_audio_decode
);
735 u32 remaining
= count
;
738 u32 format
= ch
->source
->flags
& AUDIO_FLAG_FORMAT
;
741 u32 samples_this_run
= VG_MIN(remaining
, ch
->source_length
- ch
->cursor
);
742 remaining
-= samples_this_run
;
744 float *dst
= &buf
[ buffer_pos
* 2 ];
746 if( format
== k_audio_format_stereo
){
747 for( int i
=0;i
<samples_this_run
; i
++ ){
752 else if( format
== k_audio_format_vorbis
){
753 int read_samples
= stb_vorbis_get_samples_float_interleaved_stereo(
758 if( read_samples
!= samples_this_run
){
759 vg_warn( "Invalid samples read (%s)\n", ch
->source
->path
);
761 for( int i
=0; i
<samples_this_run
; i
++ ){
767 else if( format
== k_audio_format_bird
){
768 synth_bird_generate_samples( ch
->bird_handle
, dst
, samples_this_run
);
771 i16
*src_buffer
= ch
->source
->data
,
772 *src
= &src_buffer
[ch
->cursor
];
774 audio_decode_uncompressed_mono( src
, samples_this_run
, dst
);
777 ch
->cursor
+= samples_this_run
;
778 buffer_pos
+= samples_this_run
;
780 if( (ch
->flags
& AUDIO_FLAG_LOOP
) && remaining
){
781 if( format
== k_audio_format_vorbis
)
782 stb_vorbis_seek_start( ch
->vorbis_handle
);
783 else if( format
== k_audio_format_bird
)
784 synth_bird_reset( ch
->bird_handle
);
794 buf
[ buffer_pos
*2 + 0 ] = 0.0f
;
795 buf
[ buffer_pos
*2 + 1 ] = 0.0f
;
801 vg_profile_end( &_vg_prof_audio_decode
);
804 static void audio_channel_mix( audio_channel
*ch
, float *buffer
)
806 float framevol_l
= vg_audio
.internal_global_volume
,
807 framevol_r
= vg_audio
.internal_global_volume
;
809 float frame_samplerate
= ch
->_
.sampling_rate
;
811 if( ch
->flags
& AUDIO_FLAG_SPACIAL_3D
){
813 v3_sub( ch
->_
.spacial_falloff
, vg_audio
.internal_listener_pos
, delta
);
815 float dist
= v3_length( delta
),
816 vol
= vg_maxf( 0.0f
, 1.0f
- ch
->_
.spacial_falloff
[3]*dist
);
822 v3_muls( delta
, 1.0f
/dist
, delta
);
823 float pan
= v3_dot( vg_audio
.internal_listener_ears
, delta
);
824 vol
= powf( vol
, 5.0f
);
826 framevol_l
*= (vol
* 0.5f
) * (1.0f
- pan
);
827 framevol_r
*= (vol
* 0.5f
) * (1.0f
+ pan
);
829 if( !(ch
->source
->flags
& AUDIO_FLAG_NO_DOPPLER
) ){
830 const float vs
= 323.0f
;
832 float dv
= v3_dot(delta
,vg_audio
.internal_listener_velocity
);
833 float doppler
= (vs
+dv
)/vs
;
834 doppler
= vg_clampf( doppler
, 0.6f
, 1.4f
);
836 if( fabsf(doppler
-1.0f
) > 0.01f
)
837 frame_samplerate
*= doppler
;
841 if( !vg_validf( framevol_l
) ) vg_fatal_error( "NaN left channel" );
842 if( !vg_validf( framevol_r
) ) vg_fatal_error( "NaN right channel" );
843 if( !vg_validf( frame_samplerate
) )
844 vg_fatal_error( "NaN sample rate" );
847 u32 buffer_length
= AUDIO_MIX_FRAME_SIZE
;
848 if( frame_samplerate
!= 1.0f
){
849 float l
= ceilf( (float)(AUDIO_MIX_FRAME_SIZE
) * frame_samplerate
);
853 float pcf
[ AUDIO_MIX_FRAME_SIZE
* 2 * 2 ];
855 audio_channel_get_samples( ch
, buffer_length
, pcf
);
857 vg_profile_begin( &_vg_prof_audio_mix
);
859 float volume_movement
= ch
->volume_movement
;
860 float const fvolume_rate
= vg_maxf( 1.0f
, ch
->_
.volume_rate
);
861 const float inv_volume_rate
= 1.0f
/fvolume_rate
;
863 float volume
= ch
->_
.volume
;
864 const float volume_start
= ch
->volume_movement_start
;
865 const float volume_target
= ch
->_
.volume_target
;
867 for( u32 j
=0; j
<AUDIO_MIX_FRAME_SIZE
; j
++ ){
868 volume_movement
+= 1.0f
;
869 float movement_t
= volume_movement
* inv_volume_rate
;
870 movement_t
= vg_minf( movement_t
, 1.0f
);
871 volume
= vg_lerpf( volume_start
, volume_target
, movement_t
);
873 float vol_norm
= volume
* volume
;
876 vol_norm
*= 1.0f
+ audio_lfo_pull_sample(ch
->_
.lfo
) * ch
->_
.lfo_amount
;
878 float vol_l
= vol_norm
* framevol_l
,
879 vol_r
= vol_norm
* framevol_r
,
883 if( frame_samplerate
!= 1.0f
){
884 /* absolutely garbage resampling, but it will do
887 float sample_index
= frame_samplerate
* (float)j
;
888 float t
= vg_fractf( sample_index
);
890 u32 i0
= floorf( sample_index
),
893 sample_l
= pcf
[ i0
*2+0 ]*(1.0f
-t
) + pcf
[ i1
*2+0 ]*t
;
894 sample_r
= pcf
[ i0
*2+1 ]*(1.0f
-t
) + pcf
[ i1
*2+1 ]*t
;
897 sample_l
= pcf
[ j
*2+0 ];
898 sample_r
= pcf
[ j
*2+1 ];
901 buffer
[ j
*2+0 ] += sample_l
* vol_l
;
902 buffer
[ j
*2+1 ] += sample_r
* vol_r
;
905 ch
->volume_movement
+= AUDIO_MIX_FRAME_SIZE
;
906 ch
->volume_movement
= VG_MIN( ch
->volume_movement
, ch
->_
.volume_rate
);
907 ch
->_
.volume
= volume
;
909 vg_profile_end( &_vg_prof_audio_mix
);
912 VG_STATIC
void audio_mixer_callback( void *user
, u8
*stream
, int byte_count
)
915 * Copy data and move edit flags to commit flags
916 * ------------------------------------------------------------- */
919 v3_copy( vg_audio
.external_listener_pos
, vg_audio
.internal_listener_pos
);
920 v3_copy( vg_audio
.external_listener_ears
, vg_audio
.internal_listener_ears
);
921 v3_copy( vg_audio
.external_lister_velocity
,
922 vg_audio
.internal_listener_velocity
);
923 vg_audio
.internal_global_volume
= vg_audio
.external_global_volume
;
925 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
926 audio_channel
*ch
= &vg_audio
.channels
[i
];
931 if( ch
->activity
== k_channel_activity_alive
){
932 if( (ch
->cursor
>= ch
->source_length
) &&
933 !(ch
->flags
& AUDIO_FLAG_LOOP
) )
935 ch
->activity
= k_channel_activity_end
;
939 /* process relinquishments */
940 if( (ch
->activity
!= k_channel_activity_reset
) && ch
->_
.relinquished
){
941 if( (ch
->activity
== k_channel_activity_end
)
942 || (ch
->_
.volume
== 0.0f
)
943 || (ch
->activity
== k_channel_activity_error
) )
945 ch
->_
.relinquished
= 0;
947 ch
->activity
= k_channel_activity_reset
;
952 /* process new channels */
953 if( ch
->activity
== k_channel_activity_reset
){
954 ch
->_
= ch
->editable_state
;
956 ch
->source_length
= 0;
957 ch
->activity
= k_channel_activity_wake
;
960 if( ch
->editble_state_write_mask
& AUDIO_EDIT_OWNERSHIP
)
961 ch
->_
.relinquished
= ch
->editable_state
.relinquished
;
963 ch
->editable_state
.relinquished
= ch
->_
.relinquished
;
966 if( ch
->editble_state_write_mask
& AUDIO_EDIT_VOLUME
){
967 ch
->_
.volume
= ch
->editable_state
.volume
;
968 ch
->_
.volume_target
= ch
->editable_state
.volume
;
971 ch
->editable_state
.volume
= ch
->_
.volume
;
975 if( ch
->editble_state_write_mask
& AUDIO_EDIT_VOLUME_SLOPE
){
976 ch
->volume_movement_start
= ch
->_
.volume
;
977 ch
->volume_movement
= 0;
979 ch
->_
.volume_target
= ch
->editable_state
.volume_target
;
980 ch
->_
.volume_rate
= ch
->editable_state
.volume_rate
;
983 ch
->editable_state
.volume_target
= ch
->_
.volume_target
;
984 ch
->editable_state
.volume_rate
= ch
->_
.volume_rate
;
988 if( ch
->editble_state_write_mask
& AUDIO_EDIT_SAMPLING_RATE
)
989 ch
->_
.sampling_rate
= ch
->editable_state
.sampling_rate
;
991 ch
->editable_state
.sampling_rate
= ch
->_
.sampling_rate
;
994 if( ch
->editble_state_write_mask
& AUDIO_EDIT_LFO_ATTACHMENT
){
995 ch
->_
.lfo
= ch
->editable_state
.lfo
;
996 ch
->_
.lfo_amount
= ch
->editable_state
.lfo_amount
;
999 ch
->editable_state
.lfo
= ch
->_
.lfo
;
1000 ch
->editable_state
.lfo_amount
= ch
->_
.lfo_amount
;
1004 if( ch
->editble_state_write_mask
& AUDIO_EDIT_SPACIAL
)
1005 v4_copy( ch
->editable_state
.spacial_falloff
,ch
->_
.spacial_falloff
);
1007 v4_copy( ch
->_
.spacial_falloff
,ch
->editable_state
.spacial_falloff
);
1010 /* currently readonly, i guess */
1011 ch
->editable_state
.pan_target
= ch
->_
.pan_target
;
1012 ch
->editable_state
.pan
= ch
->_
.pan
;
1013 ch
->editble_state_write_mask
= 0x00;
1016 for( int i
=0; i
<AUDIO_LFOS
; i
++ ){
1017 audio_lfo
*lfo
= &vg_audio
.oscillators
[ i
];
1019 if( lfo
->editble_state_write_mask
& AUDIO_EDIT_LFO_WAVE
){
1020 lfo
->_
.wave_type
= lfo
->editable_state
.wave_type
;
1022 if( lfo
->_
.wave_type
== k_lfo_polynomial_bipolar
){
1023 lfo
->_
.polynomial_coefficient
=
1024 lfo
->editable_state
.polynomial_coefficient
;
1025 lfo
->sqrt_polynomial_coefficient
=
1026 sqrtf(lfo
->_
.polynomial_coefficient
);
1030 if( lfo
->editble_state_write_mask
& AUDIO_EDIT_LFO_PERIOD
){
1031 if( lfo
->_
.period
){
1032 float t
= lfo
->time
;
1033 t
/= (float)lfo
->_
.period
;
1035 lfo
->_
.period
= lfo
->editable_state
.period
;
1036 lfo
->time
= lfo
->_
.period
* t
;
1040 lfo
->_
.period
= lfo
->editable_state
.period
;
1044 lfo
->editble_state_write_mask
= 0x00;
1047 dsp_update_tunings();
1052 * ------------------------------------------------------------- */
1053 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
1054 audio_channel
*ch
= &vg_audio
.channels
[i
];
1056 if( ch
->activity
== k_channel_activity_wake
){
1057 if( audio_channel_load_source( ch
) )
1058 ch
->activity
= k_channel_activity_alive
;
1060 ch
->activity
= k_channel_activity_error
;
1066 * -------------------------------------------------------- */
1067 int frame_count
= byte_count
/(2*sizeof(float));
1070 float *pOut32F
= (float *)stream
;
1071 for( int i
=0; i
<frame_count
*2; i
++ )
1074 for( int i
=0; i
<AUDIO_LFOS
; i
++ ){
1075 audio_lfo
*lfo
= &vg_audio
.oscillators
[i
];
1076 lfo
->time_startframe
= lfo
->time
;
1079 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
1080 audio_channel
*ch
= &vg_audio
.channels
[i
];
1082 if( ch
->activity
== k_channel_activity_alive
){
1084 ch
->_
.lfo
->time
= ch
->_
.lfo
->time_startframe
;
1086 u32 remaining
= frame_count
,
1090 audio_channel_mix( ch
, pOut32F
+subpos
);
1091 remaining
-= AUDIO_MIX_FRAME_SIZE
;
1092 subpos
+= AUDIO_MIX_FRAME_SIZE
*2;
1097 vg_profile_begin( &_vg_prof_dsp
);
1099 for( int i
=0; i
<frame_count
; i
++ )
1100 vg_dsp_process( pOut32F
+ i
*2, pOut32F
+ i
*2 );
1102 vg_profile_end( &_vg_prof_dsp
);
1106 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
1107 audio_channel
*ch
= &vg_audio
.channels
[i
];
1108 ch
->readable_activity
= ch
->activity
;
1111 /* Profiling information
1112 * ----------------------------------------------- */
1113 vg_profile_increment( &_vg_prof_audio_decode
);
1114 vg_profile_increment( &_vg_prof_audio_mix
);
1115 vg_profile_increment( &_vg_prof_dsp
);
1117 vg_prof_audio_mix
= _vg_prof_audio_mix
;
1118 vg_prof_audio_decode
= _vg_prof_audio_decode
;
1119 vg_prof_audio_dsp
= _vg_prof_dsp
;
1121 vg_audio
.samples_last
= frame_count
;
1123 if( vg_audio
.debug_dsp
){
1124 vg_dsp_update_texture();
1130 VG_STATIC
void audio_clip_load( audio_clip
*clip
, void *lin_alloc
)
1132 if( lin_alloc
== NULL
)
1133 lin_alloc
= vg_audio
.audio_pool
;
1135 /* load in directly */
1136 u32 format
= clip
->flags
& AUDIO_FLAG_FORMAT
;
1138 /* TODO: This contains audio_lock() and unlock, but i don't know why
1139 * can probably remove them. Low priority to check this */
1141 /* TODO: packed files for vorbis etc, should take from data if its not not
1142 * NULL when we get the clip
1145 if( format
== k_audio_format_vorbis
){
1147 vg_fatal_error( "No path specified, embeded vorbis unsupported" );
1151 clip
->data
= vg_file_read( lin_alloc
, clip
->path
, &clip
->size
);
1155 vg_fatal_error( "Audio failed to load" );
1157 float mb
= (float)(clip
->size
) / (1024.0f
*1024.0f
);
1158 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip
->path
, mb
);
1160 else if( format
== k_audio_format_stereo
){
1161 vg_fatal_error( "Unsupported format (Stereo uncompressed)" );
1163 else if( format
== k_audio_format_bird
){
1165 vg_fatal_error( "No data, external birdsynth unsupported" );
1168 u32 total_size
= clip
->size
+ sizeof(struct synth_bird
);
1169 total_size
-= sizeof(struct synth_bird_settings
);
1170 total_size
= vg_align8( total_size
);
1172 if( total_size
> AUDIO_DECODE_SIZE
)
1173 vg_fatal_error( "Bird coding too long\n" );
1175 struct synth_bird
*bird
= vg_linear_alloc( lin_alloc
, total_size
);
1176 memcpy( &bird
->settings
, clip
->data
, clip
->size
);
1179 clip
->size
= total_size
;
1181 vg_info( "Loaded bird synthesis pattern (%u bytes)\n", total_size
);
1185 vg_fatal_error( "No path specified, embeded mono unsupported" );
1188 vg_linear_clear( vg_mem
.scratch
);
1191 stb_vorbis_alloc alloc
= {
1192 .alloc_buffer
= vg_linear_alloc( vg_mem
.scratch
, AUDIO_DECODE_SIZE
),
1193 .alloc_buffer_length_in_bytes
= AUDIO_DECODE_SIZE
1196 void *filedata
= vg_file_read( vg_mem
.scratch
, clip
->path
, &fsize
);
1199 stb_vorbis
*decoder
= stb_vorbis_open_memory(
1200 filedata
, fsize
, &err
, &alloc
);
1203 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
1205 vg_fatal_error( "Vorbis decode error" );
1208 /* only mono is supported in uncompressed */
1209 u32 length_samples
= stb_vorbis_stream_length_in_samples( decoder
),
1210 data_size
= length_samples
* sizeof(i16
);
1213 clip
->data
= vg_linear_alloc( lin_alloc
, vg_align8(data_size
) );
1214 clip
->size
= length_samples
;
1217 int read_samples
= stb_vorbis_get_samples_i16_downmixed(
1218 decoder
, clip
->data
, length_samples
);
1220 if( read_samples
!= length_samples
)
1221 vg_fatal_error( "Decode error" );
1223 float mb
= (float)(data_size
) / (1024.0f
*1024.0f
);
1224 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip
->path
, mb
,
1229 VG_STATIC
void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
)
1231 for( int i
=0; i
<count
; i
++ )
1232 audio_clip_load( &arr
[i
], lin_alloc
);
1235 VG_STATIC
void audio_require_clip_loaded( audio_clip
*clip
)
1237 if( clip
->data
&& clip
->size
)
1241 vg_fatal_error( "Must load audio clip before playing! \n" );
1248 VG_STATIC
void audio_debug_ui( m4x4f mtx_pv
)
1250 if( !vg_audio
.debug_ui
)
1255 glBindTexture( GL_TEXTURE_2D
, vg_dsp
.view_texture
);
1256 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 256, 256,
1257 GL_RGBA
, GL_UNSIGNED_BYTE
,
1258 vg_dsp
.view_texture_buffer
);
1262 * -----------------------------------------------------------------------
1265 float budget
= ((double)vg_audio
.samples_last
/ 44100.0) * 1000.0;
1266 vg_profile_drawn( (struct vg_profile
*[]){ &vg_prof_audio_decode
,
1268 &vg_prof_audio_dsp
}, 3,
1269 budget
, (ui_rect
){ 4, VG_PROFILE_SAMPLE_COUNT
*2 + 8,
1276 vg_uictx
.cursor
[0] = 512 + 8;
1277 vg_uictx
.cursor
[1] = VG_PROFILE_SAMPLE_COUNT
*2+8+24+12+12;
1278 vg_uictx
.cursor
[2] = 150;
1279 vg_uictx
.cursor
[3] = 12;
1281 if( vg_audio
.debug_dsp
){
1282 ui_rect view_thing
= { 4, vg
.window_y
-512-4, 512, 512 };
1283 ui_push_image( view_thing
, vg_dsp
.view_texture
);
1286 float mb1
= 1024.0f
*1024.0f
,
1287 usage
= vg_linear_get_cur( vg_audio
.audio_pool
) / mb1
,
1288 total
= vg_linear_get_capacity( vg_audio
.audio_pool
) / mb1
,
1289 percent
= (usage
/total
) * 100.0f
;
1291 snprintf( perf
, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage
, total
, percent
);
1293 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1294 vg_uictx
.cursor
[1] += 20;
1296 ui_rect overlap_buffer
[ AUDIO_CHANNELS
];
1297 u32 overlap_length
= 0;
1299 /* Draw audio stack */
1300 for( int i
=0; i
<AUDIO_CHANNELS
; i
++ ){
1301 audio_channel
*ch
= &vg_audio
.channels
[i
];
1303 vg_uictx
.cursor
[2] = 400;
1304 vg_uictx
.cursor
[3] = 18;
1308 if( !ch
->allocated
){
1309 ui_fill_rect( vg_uictx
.cursor
, 0x50333333 );
1312 vg_uictx
.cursor
[1] += 1;
1316 const char *formats
[] =
1336 const char *activties
[] =
1345 u32 format_index
= (ch
->source
->flags
& AUDIO_FLAG_FORMAT
)>>9;
1347 snprintf( perf
, 127, "%02d %c%c%cD %s [%s] %4.2fv'%s'",
1349 (ch
->editable_state
.relinquished
)? 'r': '_',
1352 formats
[format_index
],
1353 activties
[ch
->readable_activity
],
1354 ch
->editable_state
.volume
,
1357 ui_fill_rect( vg_uictx
.cursor
, 0xa0000000 | ch
->colour
);
1359 vg_uictx
.cursor
[0] += 2;
1360 vg_uictx
.cursor
[1] += 2;
1361 ui_text( vg_uictx
.cursor
, perf
, 1, 0 );
1364 vg_uictx
.cursor
[1] += 1;
1366 if( AUDIO_FLAG_SPACIAL_3D
){
1368 v3_copy( ch
->editable_state
.spacial_falloff
, wpos
);
1371 m4x4_mulv( mtx_pv
, wpos
, wpos
);
1373 if( wpos
[3] > 0.0f
){
1374 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
1375 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
1378 wr
[0] = vg_clampf(wpos
[0] * vg
.window_x
, -32000.0f
,32000.0f
);
1379 wr
[1] = vg_clampf((1.0f
-wpos
[1]) * vg
.window_y
,-32000.0f
,32000.0f
);
1383 for( int j
=0; j
<12; j
++ ){
1385 for( int k
=0; k
<overlap_length
; k
++ ){
1386 ui_px
*wk
= overlap_buffer
[k
];
1387 if( ((wr
[0] <= wk
[0]+wk
[2]) && (wr
[0]+wr
[2] >= wk
[0])) &&
1388 ((wr
[1] <= wk
[1]+wk
[3]) && (wr
[1]+wr
[3] >= wk
[1])) )
1401 ui_text( wr
, perf
, 1, 0 );
1403 ui_rect_copy( wr
, overlap_buffer
[ overlap_length
++ ] );
1411 #endif /* VG_AUDIO_H */