option to force audio compression
[vg.git] / vg_audio.h
1 /* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved */
2
3 #ifndef VG_AUDIO_H
4 #define VG_AUDIO_H
5
6 #define VG_GAME
7
8 #include "vg/vg.h"
9 #include "vg/vg_stdint.h"
10 #include "vg/vg_platform.h"
11 #include "vg/vg_io.h"
12 #include "vg/vg_m.h"
13 #include "vg/vg_console.h"
14 #include "vg/vg_store.h"
15 #include "vg/vg_profiler.h"
16 #include "vg/vg_audio_synth_bird.h"
17
18 #ifdef __GNUC__
19 #ifndef __clang__
20 #pragma GCC push_options
21 #pragma GCC optimize ("O3")
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
24 #endif
25 #endif
26
27 #define STB_VORBIS_MAX_CHANNELS 2
28 #include "submodules/stb/stb_vorbis.c"
29 #undef L
30 #undef R
31 #undef C
32
33 #ifdef __GNUC__
34 #ifndef __clang__
35 #pragma GCC pop_options
36 #pragma GCC diagnostic pop
37 #endif
38 #endif
39
40 #define AUDIO_FRAME_SIZE 512
41 #define AUDIO_MIX_FRAME_SIZE 256
42
43 #define AUDIO_CHANNELS 32
44 #define AUDIO_LFOS 8
45 #define AUDIO_FILTERS 16
46 #define AUDIO_FLAG_LOOP 0x1
47 #define AUDIO_FLAG_NO_DOPPLER 0x2
48 #define AUDIO_FLAG_SPACIAL_3D 0x4
49 #define AUDIO_FLAG_AUTO_START 0x8
50 #define AUDIO_FLAG_FORMAT 0x1E00
51
52 enum audio_format
53 {
54 k_audio_format_mono = 0x000u,
55 k_audio_format_stereo = 0x200u,
56 k_audio_format_vorbis = 0x400u,
57 k_audio_format_none0 = 0x600u,
58 k_audio_format_none1 = 0x800u,
59 k_audio_format_none2 = 0xA00u,
60 k_audio_format_none3 = 0xC00u,
61 k_audio_format_none4 = 0xE00u,
62
63 k_audio_format_bird = 0x1000u,
64 k_audio_format_none5 = 0x1200u,
65 k_audio_format_none6 = 0x1400u,
66 k_audio_format_none7 = 0x1600u,
67 k_audio_format_none8 = 0x1800u,
68 k_audio_format_none9 = 0x1A00u,
69 k_audio_format_none10 = 0x1C00u,
70 k_audio_format_none11 = 0x1E00u,
71 };
72
73 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
74 #define AUDIO_MUTE_VOLUME 0.0f
75 #define AUDIO_BASE_VOLUME 1.0f
76
77 typedef struct audio_clip audio_clip;
78 typedef struct audio_channel audio_channel;
79 typedef struct audio_lfo audio_lfo;
80
81 struct audio_clip{
82 union { /* TODO oof.. */
83 u64 _p64_;
84 const char *path;
85 };
86
87 u32 flags;
88 u32 size;
89
90 union{
91 u64 _p64;
92 void *data;
93 };
94 };
95
96 struct vg_audio_system{
97 SDL_AudioDeviceID sdl_output_device;
98
99 void *audio_pool,
100 *decode_buffer;
101 u32 samples_last;
102
103 /* synchro */
104 int sync_locked;
105
106 SDL_SpinLock sl_checker,
107 sl_sync;
108
109 struct audio_lfo{
110 u32 time, time_startframe;
111 float sqrt_polynomial_coefficient;
112
113 struct{
114 enum lfo_wave_type{
115 k_lfo_triangle,
116 k_lfo_square,
117 k_lfo_saw,
118 k_lfo_polynomial_bipolar
119 }
120 wave_type;
121
122 u32 period;
123 float polynomial_coefficient;
124 }
125 _, editable_state;
126 u32 editble_state_write_mask;
127 }
128 oscillators[ AUDIO_LFOS ];
129
130 struct audio_channel{
131 int allocated;
132 u16 group;
133 u8 world_id;
134
135 char name[32]; /* only editable while allocated == 0 */
136 audio_clip *source; /* ... */
137 u32 flags; /* ... */
138 u32 colour; /* ... */
139
140 /* internal non-readable state
141 * -----------------------------*/
142 u32 cursor, source_length;
143
144 float volume_movement_start,
145 pan_movement_start;
146
147 u32 volume_movement,
148 pan_movement;
149
150 union{
151 struct synth_bird *bird_handle;
152 stb_vorbis *vorbis_handle;
153 };
154
155 stb_vorbis_alloc vorbis_alloc;
156
157 enum channel_activity{
158 k_channel_activity_reset, /* will advance if allocated==1, to wake */
159 k_channel_activity_wake, /* will advance to either of next two */
160 k_channel_activity_alive,
161 k_channel_activity_end,
162 k_channel_activity_error
163 }
164 activity,
165 readable_activity;
166
167 /*
168 * editable structure, can be modified inside _lock and _unlock
169 * the edit mask tells which to copy into internal _, or to discard
170 * ----------------------------------------------------------------------
171 */
172 struct channel_state{
173 int relinquished;
174
175 float volume, /* current volume */
176 volume_target, /* target volume */
177 pan,
178 pan_target,
179 sampling_rate;
180
181 u32 volume_rate,
182 pan_rate;
183
184 v4f spacial_falloff; /* xyz, range */
185
186 audio_lfo *lfo;
187 float lfo_amount;
188 }
189 _, editable_state;
190 u32 editble_state_write_mask;
191 }
192 channels[ AUDIO_CHANNELS ];
193
194 int debug_ui, debug_ui_3d, debug_dsp;
195
196 v3f internal_listener_pos,
197 internal_listener_ears,
198 internal_listener_velocity,
199
200 external_listener_pos,
201 external_listener_ears,
202 external_lister_velocity;
203
204 float internal_global_volume,
205 external_global_volume;
206 }
207 static vg_audio = { .external_global_volume = 1.0f };
208
209 #include "vg/vg_audio_dsp.h"
210
211 static struct vg_profile
212 _vg_prof_audio_decode = {.mode = k_profile_mode_accum,
213 .name = "[T2] audio_decode()"},
214 _vg_prof_audio_mix = {.mode = k_profile_mode_accum,
215 .name = "[T2] audio_mix()"},
216 _vg_prof_dsp = {.mode = k_profile_mode_accum,
217 .name = "[T2] dsp_process()"},
218 vg_prof_audio_decode,
219 vg_prof_audio_mix,
220 vg_prof_audio_dsp;
221
222 /*
223 * These functions are called from the main thread and used to prevent bad
224 * access. TODO: They should be no-ops in release builds.
225 */
226 static int audio_lock_checker_load(void)
227 {
228 int value;
229 SDL_AtomicLock( &vg_audio.sl_checker );
230 value = vg_audio.sync_locked;
231 SDL_AtomicUnlock( &vg_audio.sl_checker );
232 return value;
233 }
234
235 static void audio_lock_checker_store( int value )
236 {
237 SDL_AtomicLock( &vg_audio.sl_checker );
238 vg_audio.sync_locked = value;
239 SDL_AtomicUnlock( &vg_audio.sl_checker );
240 }
241
242 static void audio_require_lock(void)
243 {
244 if( audio_lock_checker_load() )
245 return;
246
247 vg_error( "Modifying sound effects systems requires locking\n" );
248 abort();
249 }
250
251 static void audio_lock(void)
252 {
253 SDL_AtomicLock( &vg_audio.sl_sync );
254 audio_lock_checker_store(1);
255 }
256
257 static void audio_unlock(void)
258 {
259 audio_lock_checker_store(0);
260 SDL_AtomicUnlock( &vg_audio.sl_sync );
261 }
262
263 static void audio_mixer_callback( void *user, u8 *stream, int frame_count );
264 static void vg_audio_init(void)
265 {
266 /* TODO: Move here? */
267 vg_console_reg_var( "debug_audio", &vg_audio.debug_ui,
268 k_var_dtype_i32, VG_VAR_CHEAT );
269 vg_console_reg_var( "debug_dsp", &vg_audio.debug_dsp,
270 k_var_dtype_i32, VG_VAR_CHEAT );
271 vg_console_reg_var( "volume", &vg_audio.external_global_volume,
272 k_var_dtype_f32, VG_VAR_PERSISTENT );
273
274 /* allocate memory */
275 /* 32mb fixed */
276 vg_audio.audio_pool =
277 vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32,
278 VG_MEMORY_SYSTEM );
279
280 /* fixed */
281 u32 decode_size = AUDIO_DECODE_SIZE * AUDIO_CHANNELS;
282 vg_audio.decode_buffer = vg_linear_alloc( vg_mem.rtmemory, decode_size );
283
284 vg_dsp_init();
285
286 SDL_AudioSpec spec_desired, spec_got;
287 spec_desired.callback = audio_mixer_callback;
288 spec_desired.channels = 2;
289 spec_desired.format = AUDIO_F32;
290 spec_desired.freq = 44100;
291 spec_desired.padding = 0;
292 spec_desired.samples = AUDIO_FRAME_SIZE;
293 spec_desired.silence = 0;
294 spec_desired.size = 0;
295 spec_desired.userdata = NULL;
296
297 vg_audio.sdl_output_device =
298 SDL_OpenAudioDevice( NULL, 0, &spec_desired, &spec_got,0 );
299
300 if( vg_audio.sdl_output_device ){
301 SDL_PauseAudioDevice( vg_audio.sdl_output_device, 0 );
302 }
303 else{
304 vg_fatal_error(
305 "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
306 " Frequency: 44100 hz\n"
307 " Buffer size: 512\n"
308 " Channels: 2\n"
309 " Format: s16 or f32\n" );
310 }
311 }
312
313 static void vg_audio_free(void)
314 {
315 vg_dsp_free();
316 SDL_CloseAudioDevice( vg_audio.sdl_output_device );
317 }
318
319 /*
320 * thread 1
321 */
322
323 #define AUDIO_EDIT_VOLUME_SLOPE 0x1
324 #define AUDIO_EDIT_VOLUME 0x2
325 #define AUDIO_EDIT_LFO_PERIOD 0x4
326 #define AUDIO_EDIT_LFO_WAVE 0x8
327 #define AUDIO_EDIT_LFO_ATTACHMENT 0x10
328 #define AUDIO_EDIT_SPACIAL 0x20
329 #define AUDIO_EDIT_OWNERSHIP 0x40
330 #define AUDIO_EDIT_SAMPLING_RATE 0x80
331
332 static void audio_channel_init( audio_channel *ch, audio_clip *clip, u32 flags )
333 {
334 audio_require_lock();
335 ch->group = 0;
336 ch->world_id = 0;
337 ch->source = clip;
338 ch->flags = flags;
339 ch->colour = 0x00333333;
340
341 if( (ch->source->flags & AUDIO_FLAG_FORMAT) == k_audio_format_bird )
342 strcpy( ch->name, "[array]" );
343 else
344 vg_strncpy( clip->path, ch->name, 32, k_strncpy_always_add_null );
345
346 ch->allocated = 1;
347
348 ch->editable_state.relinquished = 0;
349 ch->editable_state.volume = 1.0f;
350 ch->editable_state.volume_target = 1.0f;
351 ch->editable_state.pan = 0.0f;
352 ch->editable_state.pan_target = 0.0f;
353 ch->editable_state.volume_rate = 0;
354 ch->editable_state.pan_rate = 0;
355 v4_copy((v4f){0.0f,0.0f,0.0f,1.0f},ch->editable_state.spacial_falloff);
356 ch->editable_state.lfo = NULL;
357 ch->editable_state.lfo_amount = 0.0f;
358 ch->editable_state.sampling_rate = 1.0f;
359 ch->editble_state_write_mask = 0x00;
360 }
361
362 static void audio_channel_group( audio_channel *ch, u16 group )
363 {
364 audio_require_lock();
365 ch->group = group;
366 ch->colour = (((u32)group * 29986577) & 0x00ffffff) | 0xff000000;
367 }
368
369 static void audio_channel_world( audio_channel *ch, u8 world_id )
370 {
371 audio_require_lock();
372 ch->world_id = world_id;
373 }
374
375 static audio_channel *audio_get_first_idle_channel(void)
376 {
377 audio_require_lock();
378 for( int i=0; i<AUDIO_CHANNELS; i++ ){
379 audio_channel *ch = &vg_audio.channels[i];
380
381 if( !ch->allocated ){
382 return ch;
383 }
384 }
385
386 return NULL;
387 }
388
389 static audio_channel *audio_get_group_idle_channel( u16 group, u32 max_count )
390 {
391 audio_require_lock();
392 u32 count = 0;
393 audio_channel *dest = NULL;
394
395 for( int i=0; i<AUDIO_CHANNELS; i++ ){
396 audio_channel *ch = &vg_audio.channels[i];
397
398 if( ch->allocated ){
399 if( ch->group == group ){
400 count ++;
401 }
402 }
403 else{
404 if( !dest )
405 dest = ch;
406 }
407 }
408
409 if( dest && (count < max_count) ){
410 return dest;
411 }
412
413 return NULL;
414 }
415
416 static audio_channel *audio_get_group_first_active_channel( u16 group )
417 {
418 audio_require_lock();
419 for( int i=0; i<AUDIO_CHANNELS; i++ ){
420 audio_channel *ch = &vg_audio.channels[i];
421 if( ch->allocated && (ch->group == group) )
422 return ch;
423 }
424 return NULL;
425 }
426
427 static int audio_channel_finished( audio_channel *ch )
428 {
429 audio_require_lock();
430 if( ch->readable_activity == k_channel_activity_end )
431 return 1;
432 else
433 return 0;
434 }
435
436 static audio_channel *audio_relinquish_channel( audio_channel *ch )
437 {
438 audio_require_lock();
439 ch->editable_state.relinquished = 1;
440 ch->editble_state_write_mask |= AUDIO_EDIT_OWNERSHIP;
441 return NULL;
442 }
443
444 static void audio_channel_slope_volume( audio_channel *ch, float length,
445 float new_volume )
446 {
447 audio_require_lock();
448 ch->editable_state.volume_target = new_volume;
449 ch->editable_state.volume_rate = length * 44100.0f;
450 ch->editble_state_write_mask |= AUDIO_EDIT_VOLUME_SLOPE;
451 }
452
453 static void audio_channel_set_sampling_rate( audio_channel *ch, float rate )
454 {
455 audio_require_lock();
456 ch->editable_state.sampling_rate = rate;
457 ch->editble_state_write_mask |= AUDIO_EDIT_SAMPLING_RATE;
458 }
459
460 static void audio_channel_edit_volume( audio_channel *ch,
461 float new_volume, int instant )
462 {
463 audio_require_lock();
464 if( instant ){
465 ch->editable_state.volume = new_volume;
466 ch->editble_state_write_mask |= AUDIO_EDIT_VOLUME;
467 }
468 else{
469 audio_channel_slope_volume( ch, 0.05f, new_volume );
470 }
471 }
472
473 static audio_channel *audio_channel_fadeout( audio_channel *ch, float length )
474 {
475 audio_require_lock();
476 audio_channel_slope_volume( ch, length, 0.0f );
477 return audio_relinquish_channel( ch );
478 }
479
480 static void audio_channel_fadein( audio_channel *ch, float length )
481 {
482 audio_require_lock();
483 audio_channel_edit_volume( ch, 0.0f, 1 );
484 audio_channel_slope_volume( ch, length, 1.0f );
485 }
486
487 static audio_channel *audio_channel_crossfade( audio_channel *ch,
488 audio_clip *new_clip,
489 float length, u32 flags )
490 {
491 audio_require_lock();
492 u32 cursor = 0;
493
494 if( ch )
495 ch = audio_channel_fadeout( ch, length );
496
497 audio_channel *replacement = audio_get_first_idle_channel();
498
499 if( replacement ){
500 audio_channel_init( replacement, new_clip, flags );
501 audio_channel_fadein( replacement, length );
502 }
503
504 return replacement;
505 }
506
507 static void audio_channel_sidechain_lfo( audio_channel *ch, int lfo_id,
508 float amount )
509 {
510 audio_require_lock();
511 ch->editable_state.lfo = &vg_audio.oscillators[ lfo_id ];
512 ch->editable_state.lfo_amount = amount;
513 ch->editble_state_write_mask |= AUDIO_EDIT_LFO_ATTACHMENT;
514 }
515
516 static void audio_channel_set_spacial( audio_channel *ch, v3f co, float range )
517 {
518 audio_require_lock();
519 if( ch->flags & AUDIO_FLAG_SPACIAL_3D ){
520 v3_copy( co, ch->editable_state.spacial_falloff );
521
522 if( range == 0.0f )
523 ch->editable_state.spacial_falloff[3] = 1.0f;
524 else
525 ch->editable_state.spacial_falloff[3] = 1.0f/range;
526
527 ch->editble_state_write_mask |= AUDIO_EDIT_SPACIAL;
528 }
529 else{
530 vg_warn( "Tried to set spacialization paramaters for 2D channel (%s)\n",
531 ch->name );
532 }
533 }
534
535 static int audio_oneshot_3d( audio_clip *clip, v3f position,
536 float range, float volume )
537 {
538 audio_require_lock();
539 audio_channel *ch = audio_get_first_idle_channel();
540
541 if( ch ){
542 audio_channel_init( ch, clip, AUDIO_FLAG_SPACIAL_3D );
543 audio_channel_set_spacial( ch, position, range );
544 audio_channel_edit_volume( ch, volume, 1 );
545 ch = audio_relinquish_channel( ch );
546
547 return 1;
548 }
549 else
550 return 0;
551 }
552
553 static int audio_oneshot( audio_clip *clip, float volume, float pan )
554 {
555 audio_require_lock();
556 audio_channel *ch = audio_get_first_idle_channel();
557
558 if( ch ){
559 audio_channel_init( ch, clip, 0x00 );
560 audio_channel_edit_volume( ch, volume, 1 );
561 ch = audio_relinquish_channel( ch );
562
563 return 1;
564 }
565 else
566 return 0;
567 }
568
569 static void audio_set_lfo_wave( int id, enum lfo_wave_type type,
570 float coefficient )
571 {
572 audio_require_lock();
573 audio_lfo *lfo = &vg_audio.oscillators[ id ];
574 lfo->editable_state.polynomial_coefficient = coefficient;
575 lfo->editable_state.wave_type = type;
576
577 lfo->editble_state_write_mask |= AUDIO_EDIT_LFO_WAVE;
578 }
579
580 static void audio_set_lfo_frequency( int id, float freq )
581 {
582 audio_require_lock();
583 audio_lfo *lfo = &vg_audio.oscillators[ id ];
584 lfo->editable_state.period = 44100.0f / freq;
585 lfo->editble_state_write_mask |= AUDIO_EDIT_LFO_PERIOD;
586 }
587
588
589 /*
590 * Committers
591 * -----------------------------------------------------------------------------
592 */
593 static int audio_channel_load_source( audio_channel *ch )
594 {
595 u32 format = ch->source->flags & AUDIO_FLAG_FORMAT;
596
597 if( format == k_audio_format_vorbis ){
598 /* Setup vorbis decoder */
599 u32 index = ch - vg_audio.channels;
600
601 u8 *buf = (u8*)vg_audio.decode_buffer,
602 *loc = &buf[AUDIO_DECODE_SIZE*index];
603
604 stb_vorbis_alloc alloc = {
605 .alloc_buffer = (char *)loc,
606 .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE
607 };
608
609 int err;
610 stb_vorbis *decoder = stb_vorbis_open_memory(
611 ch->source->data,
612 ch->source->size, &err, &alloc );
613
614 if( !decoder ){
615 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
616 ch->source->path, err );
617 return 0;
618 }
619 else{
620 ch->source_length = stb_vorbis_stream_length_in_samples( decoder );
621 ch->vorbis_handle = decoder;
622 }
623 }
624 else if( format == k_audio_format_bird ){
625 u32 index = ch - vg_audio.channels;
626
627 u8 *buf = (u8*)vg_audio.decode_buffer;
628 struct synth_bird *loc = (void *)&buf[AUDIO_DECODE_SIZE*index];
629
630 memcpy( loc, ch->source->data, ch->source->size );
631 synth_bird_reset( loc );
632
633 ch->bird_handle = loc;
634 ch->source_length = synth_bird_get_length_in_samples( loc );
635 }
636 else if( format == k_audio_format_stereo ){
637 ch->source_length = ch->source->size / 2;
638 }
639 else{
640 ch->source_length = ch->source->size;
641 }
642
643 return 1;
644 }
645
646 static void audio_decode_uncompressed_mono( i16 *src, u32 count, float *dst )
647 {
648 for( u32 i=0; i<count; i++ ){
649 dst[ i*2 + 0 ] = ((float)src[i]) * (1.0f/32767.0f);
650 dst[ i*2 + 1 ] = ((float)src[i]) * (1.0f/32767.0f);
651 }
652 }
653
654 /*
655 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
656 */
657 static int
658 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
659 int len )
660 {
661 int n = 0,
662 c = VG_MIN( 1, f->channels - 1 );
663
664 while( n < len ) {
665 int k = f->channel_buffer_end - f->channel_buffer_start;
666
667 if( n+k >= len )
668 k = len - n;
669
670 for( int j=0; j < k; ++j ) {
671 *buffer++ = f->channel_buffers[ 0 ][f->channel_buffer_start+j];
672 *buffer++ = f->channel_buffers[ c ][f->channel_buffer_start+j];
673 }
674
675 n += k;
676 f->channel_buffer_start += k;
677
678 if( n == len )
679 break;
680
681 if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
682 break;
683 }
684
685 return n;
686 }
687
688 /*
689 * ........ more wrecked code sorry!
690 */
691 static int
692 stb_vorbis_get_samples_i16_downmixed( stb_vorbis *f, i16 *buffer, int len )
693 {
694 int n = 0,
695 c = VG_MIN( 1, f->channels - 1 );
696
697 while( n < len ) {
698 int k = f->channel_buffer_end - f->channel_buffer_start;
699
700 if( n+k >= len )
701 k = len - n;
702
703 for( int j=0; j < k; ++j ) {
704 float sl = f->channel_buffers[ 0 ][f->channel_buffer_start+j],
705 sr = f->channel_buffers[ c ][f->channel_buffer_start+j];
706
707 *buffer++ = vg_clampf( 0.5f*(sl+sr), -1.0f, 1.0f ) * 32767.0f;
708 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
709 }
710
711 n += k;
712 f->channel_buffer_start += k;
713
714 if( n == len )
715 break;
716
717 if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
718 break;
719 }
720
721 return n;
722 }
723
724 static inline float audio_lfo_pull_sample( audio_lfo *lfo )
725 {
726 lfo->time ++;
727
728 if( lfo->time >= lfo->_.period )
729 lfo->time = 0;
730
731 float t = lfo->time;
732 t /= (float)lfo->_.period;
733
734 if( lfo->_.wave_type == k_lfo_polynomial_bipolar ){
735 /*
736 * #
737 * # #
738 * # #
739 * # #
740 * ### # ###
741 * ## #
742 * # #
743 * # #
744 * ##
745 */
746
747 t *= 2.0f;
748 t -= 1.0f;
749
750 return (( 2.0f * lfo->sqrt_polynomial_coefficient * t ) /
751 /* --------------------------------------- */
752 ( 1.0f + lfo->_.polynomial_coefficient * t*t )
753
754 ) * (1.0f-fabsf(t));
755 }
756 else{
757 return 0.0f;
758 }
759 }
760
761 static void audio_channel_get_samples( audio_channel *ch,
762 u32 count, float *buf )
763 {
764 vg_profile_begin( &_vg_prof_audio_decode );
765
766 u32 remaining = count;
767 u32 buffer_pos = 0;
768
769 u32 format = ch->source->flags & AUDIO_FLAG_FORMAT;
770
771 while( remaining ){
772 u32 samples_this_run = VG_MIN(remaining, ch->source_length - ch->cursor);
773 remaining -= samples_this_run;
774
775 float *dst = &buf[ buffer_pos * 2 ];
776
777 if( format == k_audio_format_stereo ){
778 for( int i=0;i<samples_this_run; i++ ){
779 dst[i*2+0] = 0.0f;
780 dst[i*2+1] = 0.0f;
781 }
782 }
783 else if( format == k_audio_format_vorbis ){
784 int read_samples = stb_vorbis_get_samples_float_interleaved_stereo(
785 ch->vorbis_handle,
786 dst,
787 samples_this_run );
788
789 if( read_samples != samples_this_run ){
790 vg_warn( "Invalid samples read (%s)\n", ch->source->path );
791
792 for( int i=0; i<samples_this_run; i++ ){
793 dst[i*2+0] = 0.0f;
794 dst[i*2+1] = 0.0f;
795 }
796 }
797 }
798 else if( format == k_audio_format_bird ){
799 synth_bird_generate_samples( ch->bird_handle, dst, samples_this_run );
800 }
801 else{
802 i16 *src_buffer = ch->source->data,
803 *src = &src_buffer[ch->cursor];
804
805 audio_decode_uncompressed_mono( src, samples_this_run, dst );
806 }
807
808 ch->cursor += samples_this_run;
809 buffer_pos += samples_this_run;
810
811 if( (ch->flags & AUDIO_FLAG_LOOP) && remaining ){
812 if( format == k_audio_format_vorbis )
813 stb_vorbis_seek_start( ch->vorbis_handle );
814 else if( format == k_audio_format_bird )
815 synth_bird_reset( ch->bird_handle );
816
817 ch->cursor = 0;
818 continue;
819 }
820 else
821 break;
822 }
823
824 while( remaining ){
825 buf[ buffer_pos*2 + 0 ] = 0.0f;
826 buf[ buffer_pos*2 + 1 ] = 0.0f;
827 buffer_pos ++;
828
829 remaining --;
830 }
831
832 vg_profile_end( &_vg_prof_audio_decode );
833 }
834
835 static void audio_channel_mix( audio_channel *ch, float *buffer )
836 {
837 float framevol_l = vg_audio.internal_global_volume,
838 framevol_r = vg_audio.internal_global_volume;
839
840 float frame_samplerate = ch->_.sampling_rate;
841
842 if( ch->flags & AUDIO_FLAG_SPACIAL_3D ){
843 v3f delta;
844 v3_sub( ch->_.spacial_falloff, vg_audio.internal_listener_pos, delta );
845
846 float dist = v3_length( delta ),
847 vol = vg_maxf( 0.0f, 1.0f - ch->_.spacial_falloff[3]*dist );
848
849 if( dist <= 0.01f ){
850
851 }
852 else{
853 v3_muls( delta, 1.0f/dist, delta );
854 float pan = v3_dot( vg_audio.internal_listener_ears, delta );
855 vol = powf( vol, 5.0f );
856
857 framevol_l *= (vol * 0.5f) * (1.0f - pan);
858 framevol_r *= (vol * 0.5f) * (1.0f + pan);
859
860 if( !(ch->source->flags & AUDIO_FLAG_NO_DOPPLER) ){
861 const float vs = 323.0f;
862
863 float dv = v3_dot(delta,vg_audio.internal_listener_velocity);
864 float doppler = (vs+dv)/vs;
865 doppler = vg_clampf( doppler, 0.6f, 1.4f );
866
867 if( fabsf(doppler-1.0f) > 0.01f )
868 frame_samplerate *= doppler;
869 }
870 }
871
872 if( !vg_validf( framevol_l ) ||
873 !vg_validf( framevol_r ) ||
874 !vg_validf( frame_samplerate ) ){
875 vg_fatal_error( "Invalid sampling conditions.\n"
876 "This crash is to protect your ears.\n"
877 " channel: %p (%s)\n"
878 " sample_rate: %f\n"
879 " volume: L%f R%f\n"
880 " listener: %.2f %.2f %.2f [%.2f %.2f %.2f]\n",
881 ch, ch->name, frame_samplerate,
882 framevol_l, framevol_r,
883 vg_audio.internal_listener_pos[0],
884 vg_audio.internal_listener_pos[1],
885 vg_audio.internal_listener_pos[2],
886 vg_audio.internal_listener_ears[0],
887 vg_audio.internal_listener_ears[1],
888 vg_audio.internal_listener_ears[2]
889 );
890 }
891 }
892
893 u32 buffer_length = AUDIO_MIX_FRAME_SIZE;
894 if( frame_samplerate != 1.0f ){
895 float l = ceilf( (float)(AUDIO_MIX_FRAME_SIZE) * frame_samplerate );
896 buffer_length = l+1;
897 }
898
899 float pcf[ AUDIO_MIX_FRAME_SIZE * 2 * 2 ];
900
901 audio_channel_get_samples( ch, buffer_length, pcf );
902
903 vg_profile_begin( &_vg_prof_audio_mix );
904
905 float volume_movement = ch->volume_movement;
906 float const fvolume_rate = vg_maxf( 1.0f, ch->_.volume_rate );
907 const float inv_volume_rate = 1.0f/fvolume_rate;
908
909 float volume = ch->_.volume;
910 const float volume_start = ch->volume_movement_start;
911 const float volume_target = ch->_.volume_target;
912
913 for( u32 j=0; j<AUDIO_MIX_FRAME_SIZE; j++ ){
914 volume_movement += 1.0f;
915 float movement_t = volume_movement * inv_volume_rate;
916 movement_t = vg_minf( movement_t, 1.0f );
917 volume = vg_lerpf( volume_start, volume_target, movement_t );
918
919 float vol_norm = volume * volume;
920
921 if( ch->_.lfo )
922 vol_norm *= 1.0f + audio_lfo_pull_sample(ch->_.lfo) * ch->_.lfo_amount;
923
924 float vol_l = vol_norm * framevol_l,
925 vol_r = vol_norm * framevol_r,
926 sample_l,
927 sample_r;
928
929 if( frame_samplerate != 1.0f ){
930 /* absolutely garbage resampling, but it will do
931 */
932
933 float sample_index = frame_samplerate * (float)j;
934 float t = vg_fractf( sample_index );
935
936 u32 i0 = floorf( sample_index ),
937 i1 = i0+1;
938
939 sample_l = pcf[ i0*2+0 ]*(1.0f-t) + pcf[ i1*2+0 ]*t;
940 sample_r = pcf[ i0*2+1 ]*(1.0f-t) + pcf[ i1*2+1 ]*t;
941 }
942 else{
943 sample_l = pcf[ j*2+0 ];
944 sample_r = pcf[ j*2+1 ];
945 }
946
947 buffer[ j*2+0 ] += sample_l * vol_l;
948 buffer[ j*2+1 ] += sample_r * vol_r;
949 }
950
951 ch->volume_movement += AUDIO_MIX_FRAME_SIZE;
952 ch->volume_movement = VG_MIN( ch->volume_movement, ch->_.volume_rate );
953 ch->_.volume = volume;
954
955 vg_profile_end( &_vg_prof_audio_mix );
956 }
957
958 static void audio_mixer_callback( void *user, u8 *stream, int byte_count )
959 {
960 /*
961 * Copy data and move edit flags to commit flags
962 * ------------------------------------------------------------- */
963 audio_lock();
964
965 v3_copy( vg_audio.external_listener_pos, vg_audio.internal_listener_pos );
966 v3_copy( vg_audio.external_listener_ears, vg_audio.internal_listener_ears );
967 v3_copy( vg_audio.external_lister_velocity,
968 vg_audio.internal_listener_velocity );
969 vg_audio.internal_global_volume = vg_audio.external_global_volume;
970
971 for( int i=0; i<AUDIO_CHANNELS; i++ ){
972 audio_channel *ch = &vg_audio.channels[i];
973
974 if( !ch->allocated )
975 continue;
976
977 if( ch->activity == k_channel_activity_alive ){
978 if( (ch->cursor >= ch->source_length) &&
979 !(ch->flags & AUDIO_FLAG_LOOP) )
980 {
981 ch->activity = k_channel_activity_end;
982 }
983 }
984
985 /* process relinquishments */
986 if( (ch->activity != k_channel_activity_reset) && ch->_.relinquished ){
987 if( (ch->activity == k_channel_activity_end)
988 || (ch->_.volume == 0.0f)
989 || (ch->activity == k_channel_activity_error) )
990 {
991 ch->_.relinquished = 0;
992 ch->allocated = 0;
993 ch->activity = k_channel_activity_reset;
994 continue;
995 }
996 }
997
998 /* process new channels */
999 if( ch->activity == k_channel_activity_reset ){
1000 ch->_ = ch->editable_state;
1001 ch->cursor = 0;
1002 ch->source_length = 0;
1003 ch->activity = k_channel_activity_wake;
1004 }
1005
1006 if( ch->editble_state_write_mask & AUDIO_EDIT_OWNERSHIP )
1007 ch->_.relinquished = ch->editable_state.relinquished;
1008 else
1009 ch->editable_state.relinquished = ch->_.relinquished;
1010
1011
1012 if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME ){
1013 ch->_.volume = ch->editable_state.volume;
1014 ch->_.volume_target = ch->editable_state.volume;
1015 }
1016 else{
1017 ch->editable_state.volume = ch->_.volume;
1018 }
1019
1020
1021 if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME_SLOPE ){
1022 ch->volume_movement_start = ch->_.volume;
1023 ch->volume_movement = 0;
1024
1025 ch->_.volume_target = ch->editable_state.volume_target;
1026 ch->_.volume_rate = ch->editable_state.volume_rate;
1027 }
1028 else{
1029 ch->editable_state.volume_target = ch->_.volume_target;
1030 ch->editable_state.volume_rate = ch->_.volume_rate;
1031 }
1032
1033
1034 if( ch->editble_state_write_mask & AUDIO_EDIT_SAMPLING_RATE )
1035 ch->_.sampling_rate = ch->editable_state.sampling_rate;
1036 else
1037 ch->editable_state.sampling_rate = ch->_.sampling_rate;
1038
1039
1040 if( ch->editble_state_write_mask & AUDIO_EDIT_LFO_ATTACHMENT ){
1041 ch->_.lfo = ch->editable_state.lfo;
1042 ch->_.lfo_amount = ch->editable_state.lfo_amount;
1043 }
1044 else{
1045 ch->editable_state.lfo = ch->_.lfo;
1046 ch->editable_state.lfo_amount = ch->_.lfo_amount;
1047 }
1048
1049
1050 if( ch->editble_state_write_mask & AUDIO_EDIT_SPACIAL )
1051 v4_copy( ch->editable_state.spacial_falloff,ch->_.spacial_falloff );
1052 else
1053 v4_copy( ch->_.spacial_falloff,ch->editable_state.spacial_falloff );
1054
1055
1056 /* currently readonly, i guess */
1057 ch->editable_state.pan_target = ch->_.pan_target;
1058 ch->editable_state.pan = ch->_.pan;
1059 ch->editble_state_write_mask = 0x00;
1060 }
1061
1062 for( int i=0; i<AUDIO_LFOS; i++ ){
1063 audio_lfo *lfo = &vg_audio.oscillators[ i ];
1064
1065 if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_WAVE ){
1066 lfo->_.wave_type = lfo->editable_state.wave_type;
1067
1068 if( lfo->_.wave_type == k_lfo_polynomial_bipolar ){
1069 lfo->_.polynomial_coefficient =
1070 lfo->editable_state.polynomial_coefficient;
1071 lfo->sqrt_polynomial_coefficient =
1072 sqrtf(lfo->_.polynomial_coefficient);
1073 }
1074 }
1075
1076 if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_PERIOD ){
1077 if( lfo->_.period ){
1078 float t = lfo->time;
1079 t/= (float)lfo->_.period;
1080
1081 lfo->_.period = lfo->editable_state.period;
1082 lfo->time = lfo->_.period * t;
1083 }
1084 else{
1085 lfo->time = 0;
1086 lfo->_.period = lfo->editable_state.period;
1087 }
1088 }
1089
1090 lfo->editble_state_write_mask = 0x00;
1091 }
1092
1093 dsp_update_tunings();
1094 audio_unlock();
1095
1096 /*
1097 * Process spawns
1098 * ------------------------------------------------------------- */
1099 for( int i=0; i<AUDIO_CHANNELS; i++ ){
1100 audio_channel *ch = &vg_audio.channels[i];
1101
1102 if( ch->activity == k_channel_activity_wake ){
1103 if( audio_channel_load_source( ch ) )
1104 ch->activity = k_channel_activity_alive;
1105 else
1106 ch->activity = k_channel_activity_error;
1107 }
1108 }
1109
1110 /*
1111 * Mix everything
1112 * -------------------------------------------------------- */
1113 int frame_count = byte_count/(2*sizeof(float));
1114
1115 /* Clear buffer */
1116 float *pOut32F = (float *)stream;
1117 for( int i=0; i<frame_count*2; i ++ )
1118 pOut32F[i] = 0.0f;
1119
1120 for( int i=0; i<AUDIO_LFOS; i++ ){
1121 audio_lfo *lfo = &vg_audio.oscillators[i];
1122 lfo->time_startframe = lfo->time;
1123 }
1124
1125 for( int i=0; i<AUDIO_CHANNELS; i ++ ){
1126 audio_channel *ch = &vg_audio.channels[i];
1127
1128 if( ch->activity == k_channel_activity_alive ){
1129 if( ch->_.lfo )
1130 ch->_.lfo->time = ch->_.lfo->time_startframe;
1131
1132 u32 remaining = frame_count,
1133 subpos = 0;
1134
1135 while( remaining ){
1136 audio_channel_mix( ch, pOut32F+subpos );
1137 remaining -= AUDIO_MIX_FRAME_SIZE;
1138 subpos += AUDIO_MIX_FRAME_SIZE*2;
1139 }
1140 }
1141 }
1142
1143 vg_profile_begin( &_vg_prof_dsp );
1144
1145 for( int i=0; i<frame_count; i++ )
1146 vg_dsp_process( pOut32F + i*2, pOut32F + i*2 );
1147
1148 vg_profile_end( &_vg_prof_dsp );
1149
1150 audio_lock();
1151
1152 for( int i=0; i<AUDIO_CHANNELS; i ++ ){
1153 audio_channel *ch = &vg_audio.channels[i];
1154 ch->readable_activity = ch->activity;
1155 }
1156
1157 /* Profiling information
1158 * ----------------------------------------------- */
1159 vg_profile_increment( &_vg_prof_audio_decode );
1160 vg_profile_increment( &_vg_prof_audio_mix );
1161 vg_profile_increment( &_vg_prof_dsp );
1162
1163 vg_prof_audio_mix = _vg_prof_audio_mix;
1164 vg_prof_audio_decode = _vg_prof_audio_decode;
1165 vg_prof_audio_dsp = _vg_prof_dsp;
1166
1167 vg_audio.samples_last = frame_count;
1168
1169 if( vg_audio.debug_dsp ){
1170 vg_dsp_update_texture();
1171 }
1172
1173 audio_unlock();
1174 }
1175
1176 static void audio_clip_load( audio_clip *clip, void *lin_alloc )
1177 {
1178 if( lin_alloc == NULL )
1179 lin_alloc = vg_audio.audio_pool;
1180
1181 #ifdef VG_AUDIO_FORCE_COMPRESSED
1182
1183 if( (clip->flags & AUDIO_FLAG_FORMAT) != k_audio_format_bird ){
1184 clip->flags &= ~AUDIO_FLAG_FORMAT;
1185 clip->flags |= k_audio_format_vorbis;
1186 }
1187
1188 #endif
1189
1190 /* load in directly */
1191 u32 format = clip->flags & AUDIO_FLAG_FORMAT;
1192
1193 /* TODO: This contains audio_lock() and unlock, but i don't know why
1194 * can probably remove them. Low priority to check this */
1195
1196 /* TODO: packed files for vorbis etc, should take from data if its not not
1197 * NULL when we get the clip
1198 */
1199
1200 if( format == k_audio_format_vorbis ){
1201 if( !clip->path ){
1202 vg_fatal_error( "No path specified, embeded vorbis unsupported" );
1203 }
1204
1205 audio_lock();
1206 clip->data = vg_file_read( lin_alloc, clip->path, &clip->size );
1207 audio_unlock();
1208
1209 if( !clip->data )
1210 vg_fatal_error( "Audio failed to load" );
1211
1212 float mb = (float)(clip->size) / (1024.0f*1024.0f);
1213 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb );
1214 }
1215 else if( format == k_audio_format_stereo ){
1216 vg_fatal_error( "Unsupported format (Stereo uncompressed)" );
1217 }
1218 else if( format == k_audio_format_bird ){
1219 if( !clip->data ){
1220 vg_fatal_error( "No data, external birdsynth unsupported" );
1221 }
1222
1223 u32 total_size = clip->size + sizeof(struct synth_bird);
1224 total_size -= sizeof(struct synth_bird_settings);
1225 total_size = vg_align8( total_size );
1226
1227 if( total_size > AUDIO_DECODE_SIZE )
1228 vg_fatal_error( "Bird coding too long\n" );
1229
1230 struct synth_bird *bird = vg_linear_alloc( lin_alloc, total_size );
1231 memcpy( &bird->settings, clip->data, clip->size );
1232
1233 clip->data = bird;
1234 clip->size = total_size;
1235
1236 vg_info( "Loaded bird synthesis pattern (%u bytes)\n", total_size );
1237 }
1238 else{
1239 if( !clip->path ){
1240 vg_fatal_error( "No path specified, embeded mono unsupported" );
1241 }
1242
1243 vg_linear_clear( vg_mem.scratch );
1244 u32 fsize;
1245
1246 stb_vorbis_alloc alloc = {
1247 .alloc_buffer = vg_linear_alloc( vg_mem.scratch, AUDIO_DECODE_SIZE ),
1248 .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE
1249 };
1250
1251 void *filedata = vg_file_read( vg_mem.scratch, clip->path, &fsize );
1252
1253 int err;
1254 stb_vorbis *decoder = stb_vorbis_open_memory(
1255 filedata, fsize, &err, &alloc );
1256
1257 if( !decoder ){
1258 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
1259 clip->path, err );
1260 vg_fatal_error( "Vorbis decode error" );
1261 }
1262
1263 /* only mono is supported in uncompressed */
1264 u32 length_samples = stb_vorbis_stream_length_in_samples( decoder ),
1265 data_size = length_samples * sizeof(i16);
1266
1267 audio_lock();
1268 clip->data = vg_linear_alloc( lin_alloc, vg_align8(data_size) );
1269 clip->size = length_samples;
1270 audio_unlock();
1271
1272 int read_samples = stb_vorbis_get_samples_i16_downmixed(
1273 decoder, clip->data, length_samples );
1274
1275 if( read_samples != length_samples )
1276 vg_fatal_error( "Decode error" );
1277
1278 #if 0
1279 float mb = (float)(data_size) / (1024.0f*1024.0f);
1280 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip->path, mb,
1281 length_samples );
1282 #endif
1283 }
1284 }
1285
1286 static void audio_clip_loadn( audio_clip *arr, int count, void *lin_alloc )
1287 {
1288 for( int i=0; i<count; i++ )
1289 audio_clip_load( &arr[i], lin_alloc );
1290 }
1291
1292 static void audio_require_clip_loaded( audio_clip *clip )
1293 {
1294 if( clip->data && clip->size )
1295 return;
1296
1297 audio_unlock();
1298 vg_fatal_error( "Must load audio clip before playing! \n" );
1299 }
1300
1301 /*
1302 * Debugging
1303 */
1304
1305 static void audio_debug_ui( m4x4f mtx_pv )
1306 {
1307 if( !vg_audio.debug_ui )
1308 return;
1309
1310 audio_lock();
1311
1312 glBindTexture( GL_TEXTURE_2D, vg_dsp.view_texture );
1313 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 256, 256,
1314 GL_RGBA, GL_UNSIGNED_BYTE,
1315 vg_dsp.view_texture_buffer );
1316
1317 /*
1318 * Profiler
1319 * -----------------------------------------------------------------------
1320 */
1321
1322 float budget = ((double)vg_audio.samples_last / 44100.0) * 1000.0;
1323 vg_profile_drawn( (struct vg_profile *[]){ &vg_prof_audio_decode,
1324 &vg_prof_audio_mix,
1325 &vg_prof_audio_dsp}, 3,
1326 budget, (ui_rect){ 4, VG_PROFILE_SAMPLE_COUNT*2 + 8,
1327 512, 0 }, 3 );
1328
1329
1330 char perf[128];
1331
1332 /* Draw UI */
1333 ui_rect window = {
1334 0,
1335 0,
1336 800,
1337 AUDIO_CHANNELS * 18
1338 };
1339
1340 if( vg_audio.debug_dsp ){
1341 ui_rect view_thing = { 4, vg.window_y-512-4, 512, 512 };
1342 ui_image( view_thing, vg_dsp.view_texture );
1343 }
1344
1345 ui_rect overlap_buffer[ AUDIO_CHANNELS ];
1346 u32 overlap_length = 0;
1347
1348 /* Draw audio stack */
1349 for( int i=0; i<AUDIO_CHANNELS; i ++ ){
1350 audio_channel *ch = &vg_audio.channels[i];
1351
1352 ui_rect row;
1353 ui_split( window, k_ui_axis_h, 18, 1, row, window );
1354
1355 if( !ch->allocated ){
1356 ui_fill( row, 0x50333333 );
1357 continue;
1358 }
1359
1360 const char *formats[] =
1361 {
1362 " mono ",
1363 " stereo ",
1364 " vorbis ",
1365 " none0 ",
1366 " none1 ",
1367 " none2 ",
1368 " none3 ",
1369 " none4 ",
1370 "synth:bird",
1371 " none5 ",
1372 " none6 ",
1373 " none7 ",
1374 " none8 ",
1375 " none9 ",
1376 " none10 ",
1377 " none11 ",
1378 };
1379
1380 const char *activties[] =
1381 {
1382 "reset",
1383 "wake ",
1384 "alive",
1385 "end ",
1386 "error"
1387 };
1388
1389 u32 format_index = (ch->source->flags & AUDIO_FLAG_FORMAT)>>9;
1390
1391 snprintf( perf, 127, "%02d[%#04x.%#06x]%c%c%cD %s [%s] %4.2fv'%s'",
1392 i,
1393 ch->world_id, ch->group,
1394 (ch->editable_state.relinquished)? 'r': '_',
1395 0? 'r': '_',
1396 0? '3': '2',
1397 formats[format_index],
1398 activties[ch->readable_activity],
1399 ch->editable_state.volume,
1400 ch->name );
1401
1402 ui_fill( row, 0xa0000000 | ch->colour );
1403 ui_text( row, perf, 1, k_ui_align_middle_left, 0 );
1404
1405 if( AUDIO_FLAG_SPACIAL_3D ){
1406 v4f wpos;
1407 v3_copy( ch->editable_state.spacial_falloff, wpos );
1408
1409 wpos[3] = 1.0f;
1410 m4x4_mulv( mtx_pv, wpos, wpos );
1411
1412 if( wpos[3] > 0.0f ){
1413 v2_muls( wpos, (1.0f/wpos[3]) * 0.5f, wpos );
1414 v2_add( wpos, (v2f){ 0.5f, 0.5f }, wpos );
1415
1416 ui_rect wr;
1417 wr[0] = vg_clampf(wpos[0] * vg.window_x, -32000.0f,32000.0f);
1418 wr[1] = vg_clampf((1.0f-wpos[1]) * vg.window_y,-32000.0f,32000.0f);
1419 wr[2] = 1000;
1420 wr[3] = 17;
1421
1422 for( int j=0; j<12; j++ ){
1423 int collide = 0;
1424 for( int k=0; k<overlap_length; k++ ){
1425 ui_px *wk = overlap_buffer[k];
1426 if( ((wr[0] <= wk[0]+wk[2]) && (wr[0]+wr[2] >= wk[0])) &&
1427 ((wr[1] <= wk[1]+wk[3]) && (wr[1]+wr[3] >= wk[1])) )
1428 {
1429 collide = 1;
1430 break;
1431 }
1432 }
1433
1434 if( !collide )
1435 break;
1436 else
1437 wr[1] += 18;
1438 }
1439
1440 ui_text( wr, perf, 1, k_ui_align_middle_left, 0 );
1441 rect_copy( wr, overlap_buffer[ overlap_length ++ ] );
1442 }
1443 }
1444 }
1445
1446 audio_unlock();
1447 }
1448
1449 #endif /* VG_AUDIO_H */