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