tune reverb
[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, debug_dsp;
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 = "debug_dsp",
279 .data = &vg_audio.debug_dsp,
280 .data_type = k_var_dtype_i32,
281 .opt_i32 = { .min=0, .max=1, .clamp=1 },
282 .persistent = 1
283 });
284
285 vg_var_push( (struct vg_var){
286 .name = "volume",
287 .data = &vg_audio.external_global_volume,
288 .data_type = k_var_dtype_f32,
289 .opt_f32 = { .min=0.0f, .max=2.0f, .clamp=1 },
290 .persistent = 1
291 });
292
293 /* allocate memory */
294
295 /* 32mb fixed */
296 vg_audio.audio_pool =
297 vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32,
298 VG_MEMORY_SYSTEM );
299
300 /* fixed */
301 u32 decode_size = AUDIO_DECODE_SIZE * AUDIO_CHANNELS;
302 vg_audio.decode_buffer = vg_linear_alloc( vg_mem.rtmemory, decode_size );
303
304 vg_dsp_init();
305
306 SDL_AudioSpec spec_desired, spec_got;
307 spec_desired.callback = audio_mixer_callback;
308 spec_desired.channels = 2;
309 spec_desired.format = AUDIO_F32;
310 spec_desired.freq = 44100;
311 spec_desired.padding = 0;
312 spec_desired.samples = AUDIO_FRAME_SIZE;
313 spec_desired.silence = 0;
314 spec_desired.size = 0;
315 spec_desired.userdata = NULL;
316
317 vg_audio.sdl_output_device =
318 SDL_OpenAudioDevice( NULL, 0, &spec_desired, &spec_got,0 );
319
320 if( vg_audio.sdl_output_device ){
321 SDL_PauseAudioDevice( vg_audio.sdl_output_device, 0 );
322 }
323 else{
324 vg_fatal_exit_loop(
325 "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
326 " Frequency: 44100 hz\n"
327 " Buffer size: 512\n"
328 " Channels: 2\n"
329 " Format: s16 or f32\n" );
330 }
331
332 vg_success( "Ready\n" );
333 }
334
335 VG_STATIC void vg_audio_free(void)
336 {
337 vg_dsp_free();
338 SDL_CloseAudioDevice( vg_audio.sdl_output_device );
339 }
340
341 /*
342 * thread 1
343 */
344
345 #define AUDIO_EDIT_VOLUME_SLOPE 0x1
346 #define AUDIO_EDIT_VOLUME 0x2
347 #define AUDIO_EDIT_LFO_PERIOD 0x4
348 #define AUDIO_EDIT_LFO_WAVE 0x8
349 #define AUDIO_EDIT_LFO_ATTACHMENT 0x10
350 #define AUDIO_EDIT_SPACIAL 0x20
351 #define AUDIO_EDIT_OWNERSHIP 0x40
352 #define AUDIO_EDIT_SAMPLING_RATE 0x80
353
354 static void audio_channel_init( audio_channel *ch, audio_clip *clip, u32 flags )
355 {
356 ch->group = 0;
357 ch->source = clip;
358 ch->flags = flags;
359 ch->colour = 0x00333333;
360
361 if( (ch->source->flags & AUDIO_FLAG_FORMAT) == k_audio_format_bird )
362 strcpy( ch->name, "[array]" );
363 else
364 strncpy( ch->name, clip->path, 31 );
365
366 ch->allocated = 1;
367
368 ch->editable_state.relinquished = 0;
369 ch->editable_state.volume = 1.0f;
370 ch->editable_state.volume_target = 1.0f;
371 ch->editable_state.pan = 0.0f;
372 ch->editable_state.pan_target = 0.0f;
373 ch->editable_state.volume_rate = 0;
374 ch->editable_state.pan_rate = 0;
375 v4_copy((v4f){0.0f,0.0f,0.0f,1.0f},ch->editable_state.spacial_falloff);
376 ch->editable_state.lfo = NULL;
377 ch->editable_state.lfo_amount = 0.0f;
378 ch->editable_state.sampling_rate = 1.0f;
379 ch->editble_state_write_mask = 0x00;
380 }
381
382 static void audio_channel_group( audio_channel *ch, u32 group )
383 {
384 ch->group = group;
385 ch->colour = ((group * 29986577) & 0x00ffffff) | 0xff000000;
386 }
387
388 static audio_channel *audio_get_first_idle_channel(void)
389 {
390 for( int i=0; i<AUDIO_CHANNELS; i++ ){
391 audio_channel *ch = &vg_audio.channels[i];
392
393 if( !ch->allocated ){
394 return ch;
395 }
396 }
397
398 return NULL;
399 }
400
401 static audio_channel *audio_get_group_idle_channel( u32 group, u32 max_count )
402 {
403 u32 count = 0;
404 audio_channel *dest = NULL;
405
406 for( int i=0; i<AUDIO_CHANNELS; i++ ){
407 audio_channel *ch = &vg_audio.channels[i];
408
409 if( ch->allocated ){
410 if( ch->group == group ){
411 count ++;
412 }
413 }
414 else{
415 if( !dest )
416 dest = ch;
417 }
418 }
419
420 if( dest && (count < max_count) ){
421 return dest;
422 }
423
424 return NULL;
425 }
426
427 static audio_channel *audio_get_group_first_active_channel( u32 group )
428 {
429 for( int i=0; i<AUDIO_CHANNELS; i++ ){
430 audio_channel *ch = &vg_audio.channels[i];
431 if( ch->allocated && (ch->group == group) )
432 return ch;
433 }
434 return NULL;
435 }
436
437 static int audio_channel_finished( audio_channel *ch )
438 {
439 if( ch->readable_activity == k_channel_activity_end )
440 return 1;
441 else
442 return 0;
443 }
444
445 static audio_channel *audio_relinquish_channel( audio_channel *ch )
446 {
447 ch->editable_state.relinquished = 1;
448 ch->editble_state_write_mask |= AUDIO_EDIT_OWNERSHIP;
449 return NULL;
450 }
451
452 static void audio_channel_slope_volume( audio_channel *ch, float length,
453 float new_volume )
454 {
455 ch->editable_state.volume_target = new_volume;
456 ch->editable_state.volume_rate = length * 44100.0f;
457 ch->editble_state_write_mask |= AUDIO_EDIT_VOLUME_SLOPE;
458 }
459
460 static void audio_channel_set_sampling_rate( audio_channel *ch, float rate )
461 {
462 ch->editable_state.sampling_rate = rate;
463 ch->editble_state_write_mask |= AUDIO_EDIT_SAMPLING_RATE;
464 }
465
466 static void audio_channel_edit_volume( audio_channel *ch,
467 float new_volume, int instant )
468 {
469 if( instant ){
470 ch->editable_state.volume = new_volume;
471 ch->editble_state_write_mask |= AUDIO_EDIT_VOLUME;
472 }
473 else{
474 audio_channel_slope_volume( ch, 0.05f, new_volume );
475 }
476 }
477
478 static audio_channel *audio_channel_fadeout( audio_channel *ch, float length )
479 {
480 audio_channel_slope_volume( ch, length, 0.0f );
481 return audio_relinquish_channel( ch );
482 }
483
484 static void audio_channel_fadein( audio_channel *ch, float length )
485 {
486 audio_channel_edit_volume( ch, 0.0f, 1 );
487 audio_channel_slope_volume( ch, length, 1.0f );
488 }
489
490 static audio_channel *audio_channel_crossfade( audio_channel *ch,
491 audio_clip *new_clip,
492 float length, u32 flags )
493 {
494 u32 cursor = 0;
495
496 if( ch )
497 ch = audio_channel_fadeout( ch, length );
498
499 audio_channel *replacement = audio_get_first_idle_channel();
500
501 if( replacement ){
502 audio_channel_init( replacement, new_clip, flags );
503 audio_channel_fadein( replacement, length );
504 }
505
506 return replacement;
507 }
508
509 static void audio_channel_sidechain_lfo( audio_channel *ch, int lfo_id,
510 float amount )
511 {
512 ch->editable_state.lfo = &vg_audio.oscillators[ lfo_id ];
513 ch->editable_state.lfo_amount = amount;
514 ch->editble_state_write_mask |= AUDIO_EDIT_LFO_ATTACHMENT;
515 }
516
517 static void audio_channel_set_spacial( audio_channel *ch, v3f co, float range )
518 {
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_channel *ch = audio_get_first_idle_channel();
539
540 if( ch ){
541 audio_channel_init( ch, clip, AUDIO_FLAG_SPACIAL_3D );
542 audio_channel_set_spacial( ch, position, range );
543 audio_channel_edit_volume( ch, volume, 1 );
544 ch = audio_relinquish_channel( ch );
545
546 return 1;
547 }
548 else
549 return 0;
550 }
551
552 static int audio_oneshot( audio_clip *clip, float volume, float pan )
553 {
554 audio_channel *ch = audio_get_first_idle_channel();
555
556 if( ch ){
557 audio_channel_init( ch, clip, 0x00 );
558 audio_channel_edit_volume( ch, volume, 1 );
559 ch = audio_relinquish_channel( ch );
560
561 return 1;
562 }
563 else
564 return 0;
565 }
566
567 static void audio_set_lfo_wave( int id, enum lfo_wave_type type,
568 float coefficient )
569 {
570 audio_lfo *lfo = &vg_audio.oscillators[ id ];
571 lfo->editable_state.polynomial_coefficient = coefficient;
572 lfo->editable_state.wave_type = type;
573
574 lfo->editble_state_write_mask |= AUDIO_EDIT_LFO_WAVE;
575 }
576
577 static void audio_set_lfo_frequency( int id, float freq )
578 {
579 audio_lfo *lfo = &vg_audio.oscillators[ id ];
580 lfo->editable_state.period = 44100.0f / freq;
581 lfo->editble_state_write_mask |= AUDIO_EDIT_LFO_PERIOD;
582 }
583
584
585 /*
586 * Committers
587 * -----------------------------------------------------------------------------
588 */
589 static int audio_channel_load_source( audio_channel *ch )
590 {
591 u32 format = ch->source->flags & AUDIO_FLAG_FORMAT;
592
593 if( format == k_audio_format_vorbis ){
594 /* Setup vorbis decoder */
595 u32 index = ch - vg_audio.channels;
596
597 u8 *buf = (u8*)vg_audio.decode_buffer,
598 *loc = &buf[AUDIO_DECODE_SIZE*index];
599
600 stb_vorbis_alloc alloc = {
601 .alloc_buffer = (char *)loc,
602 .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE
603 };
604
605 int err;
606 stb_vorbis *decoder = stb_vorbis_open_memory(
607 ch->source->data,
608 ch->source->size, &err, &alloc );
609
610 if( !decoder ){
611 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
612 ch->source->path, err );
613 return 0;
614 }
615 else{
616 ch->source_length = stb_vorbis_stream_length_in_samples( decoder );
617 ch->vorbis_handle = decoder;
618 }
619 }
620 else if( format == k_audio_format_bird ){
621 u32 index = ch - vg_audio.channels;
622
623 u8 *buf = (u8*)vg_audio.decode_buffer;
624 struct synth_bird *loc = (void *)&buf[AUDIO_DECODE_SIZE*index];
625
626 memcpy( loc, ch->source->data, ch->source->size );
627 synth_bird_reset( loc );
628
629 ch->bird_handle = loc;
630 ch->source_length = synth_bird_get_length_in_samples( loc );
631 }
632 else if( format == k_audio_format_stereo ){
633 ch->source_length = ch->source->size / 2;
634 }
635 else{
636 ch->source_length = ch->source->size;
637 }
638
639 return 1;
640 }
641
642 VG_STATIC void audio_decode_uncompressed_mono( i16 *src, u32 count, float *dst )
643 {
644 for( u32 i=0; i<count; i++ ){
645 dst[ i*2 + 0 ] = ((float)src[i]) * (1.0f/32767.0f);
646 dst[ i*2 + 1 ] = ((float)src[i]) * (1.0f/32767.0f);
647 }
648 }
649
650 /*
651 * adapted from stb_vorbis.h, since the original does not handle mono->stereo
652 */
653 VG_STATIC int
654 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
655 int len )
656 {
657 int n = 0,
658 c = VG_MIN( 1, f->channels - 1 );
659
660 while( n < len ) {
661 int k = f->channel_buffer_end - f->channel_buffer_start;
662
663 if( n+k >= len )
664 k = len - n;
665
666 for( int j=0; j < k; ++j ) {
667 *buffer++ = f->channel_buffers[ 0 ][f->channel_buffer_start+j];
668 *buffer++ = f->channel_buffers[ c ][f->channel_buffer_start+j];
669 }
670
671 n += k;
672 f->channel_buffer_start += k;
673
674 if( n == len )
675 break;
676
677 if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
678 break;
679 }
680
681 return n;
682 }
683
684 /*
685 * ........ more wrecked code sorry!
686 */
687 VG_STATIC int
688 stb_vorbis_get_samples_i16_downmixed( stb_vorbis *f, i16 *buffer, int len )
689 {
690 int n = 0,
691 c = VG_MIN( 1, f->channels - 1 );
692
693 while( n < len ) {
694 int k = f->channel_buffer_end - f->channel_buffer_start;
695
696 if( n+k >= len )
697 k = len - n;
698
699 for( int j=0; j < k; ++j ) {
700 float sl = f->channel_buffers[ 0 ][f->channel_buffer_start+j],
701 sr = f->channel_buffers[ c ][f->channel_buffer_start+j];
702
703 *buffer++ = vg_clampf( 0.5f*(sl+sr), -1.0f, 1.0f ) * 32767.0f;
704 //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
705 }
706
707 n += k;
708 f->channel_buffer_start += k;
709
710 if( n == len )
711 break;
712
713 if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
714 break;
715 }
716
717 return n;
718 }
719
720 static inline float audio_lfo_pull_sample( audio_lfo *lfo )
721 {
722 lfo->time ++;
723
724 if( lfo->time >= lfo->_.period )
725 lfo->time = 0;
726
727 float t = lfo->time;
728 t /= (float)lfo->_.period;
729
730 if( lfo->_.wave_type == k_lfo_polynomial_bipolar ){
731 /*
732 * #
733 * # #
734 * # #
735 * # #
736 * ### # ###
737 * ## #
738 * # #
739 * # #
740 * ##
741 */
742
743 t *= 2.0f;
744 t -= 1.0f;
745
746 return (( 2.0f * lfo->sqrt_polynomial_coefficient * t ) /
747 /* --------------------------------------- */
748 ( 1.0f + lfo->_.polynomial_coefficient * t*t )
749
750 ) * (1.0f-fabsf(t));
751 }
752 else{
753 return 0.0f;
754 }
755 }
756
757 static void audio_channel_get_samples( audio_channel *ch,
758 u32 count, float *buf )
759 {
760 vg_profile_begin( &_vg_prof_audio_decode );
761
762 u32 remaining = count;
763 u32 buffer_pos = 0;
764
765 u32 format = ch->source->flags & AUDIO_FLAG_FORMAT;
766
767 while( remaining ){
768 u32 samples_this_run = VG_MIN(remaining, ch->source_length - ch->cursor);
769 remaining -= samples_this_run;
770
771 float *dst = &buf[ buffer_pos * 2 ];
772
773 if( format == k_audio_format_stereo ){
774 for( int i=0;i<samples_this_run; i++ ){
775 dst[i*2+0] = 0.0f;
776 dst[i*2+1] = 0.0f;
777 }
778 }
779 else if( format == k_audio_format_vorbis ){
780 int read_samples = stb_vorbis_get_samples_float_interleaved_stereo(
781 ch->vorbis_handle,
782 dst,
783 samples_this_run );
784
785 if( read_samples != samples_this_run ){
786 vg_warn( "Invalid samples read (%s)\n", ch->source->path );
787
788 for( int i=0; i<samples_this_run; i++ ){
789 dst[i*2+0] = 0.0f;
790 dst[i*2+1] = 0.0f;
791 }
792 }
793 }
794 else if( format == k_audio_format_bird ){
795 synth_bird_generate_samples( ch->bird_handle, dst, samples_this_run );
796 }
797 else{
798 i16 *src_buffer = ch->source->data,
799 *src = &src_buffer[ch->cursor];
800
801 audio_decode_uncompressed_mono( src, samples_this_run, dst );
802 }
803
804 ch->cursor += samples_this_run;
805 buffer_pos += samples_this_run;
806
807 if( (ch->flags & AUDIO_FLAG_LOOP) && remaining ){
808 if( format == k_audio_format_vorbis )
809 stb_vorbis_seek_start( ch->vorbis_handle );
810 else if( format == k_audio_format_bird )
811 synth_bird_reset( ch->bird_handle );
812
813 ch->cursor = 0;
814 continue;
815 }
816 else
817 break;
818 }
819
820 while( remaining ){
821 buf[ buffer_pos*2 + 0 ] = 0.0f;
822 buf[ buffer_pos*2 + 1 ] = 0.0f;
823 buffer_pos ++;
824
825 remaining --;
826 }
827
828 vg_profile_end( &_vg_prof_audio_decode );
829 }
830
831 static void audio_channel_mix( audio_channel *ch, float *buffer )
832 {
833 float framevol_l = vg_audio.internal_global_volume,
834 framevol_r = vg_audio.internal_global_volume;
835
836 float frame_samplerate = ch->_.sampling_rate;
837
838 if( ch->flags & AUDIO_FLAG_SPACIAL_3D ){
839 v3f delta;
840 v3_sub( ch->_.spacial_falloff, vg_audio.internal_listener_pos, delta );
841
842 float dist = v3_length( delta ),
843 vol = vg_maxf( 0.0f, 1.0f - ch->_.spacial_falloff[3]*dist );
844
845 if( dist <= 0.01f ){
846
847 }
848 else{
849 v3_muls( delta, 1.0f/dist, delta );
850 float pan = v3_dot( vg_audio.internal_listener_ears, delta );
851 vol = powf( vol, 5.0f );
852
853 framevol_l *= (vol * 0.5f) * (1.0f - pan);
854 framevol_r *= (vol * 0.5f) * (1.0f + pan);
855
856 if( !(ch->source->flags & AUDIO_FLAG_NO_DOPPLER) ){
857 const float vs = 323.0f;
858
859 float dv = v3_dot(delta,vg_audio.internal_listener_velocity);
860 float doppler = (vs+dv)/vs;
861 doppler = vg_clampf( doppler, 0.6f, 1.4f );
862
863 if( fabsf(doppler-1.0f) > 0.01f )
864 frame_samplerate *= doppler;
865 }
866 }
867
868 if( !vg_validf( framevol_l ) ) vg_fatal_exit_loop( "NaN left channel" );
869 if( !vg_validf( framevol_r ) ) vg_fatal_exit_loop( "NaN right channel" );
870 if( !vg_validf( frame_samplerate ) )
871 vg_fatal_exit_loop( "NaN sample rate" );
872 }
873
874 u32 buffer_length = AUDIO_MIX_FRAME_SIZE;
875 if( frame_samplerate != 1.0f ){
876 float l = ceilf( (float)(AUDIO_MIX_FRAME_SIZE) * frame_samplerate );
877 buffer_length = l+1;
878 }
879
880 float pcf[ AUDIO_MIX_FRAME_SIZE * 2 * 2 ];
881
882 audio_channel_get_samples( ch, buffer_length, pcf );
883
884 vg_profile_begin( &_vg_prof_audio_mix );
885
886 float volume_movement = ch->volume_movement;
887 float const fvolume_rate = vg_maxf( 1.0f, ch->_.volume_rate );
888 const float inv_volume_rate = 1.0f/fvolume_rate;
889
890 float volume = ch->_.volume;
891 const float volume_start = ch->volume_movement_start;
892 const float volume_target = ch->_.volume_target;
893
894 for( u32 j=0; j<AUDIO_MIX_FRAME_SIZE; j++ ){
895 volume_movement += 1.0f;
896 float movement_t = volume_movement * inv_volume_rate;
897 movement_t = vg_minf( movement_t, 1.0f );
898 volume = vg_lerpf( volume_start, volume_target, movement_t );
899
900 float vol_norm = volume * volume;
901
902 if( ch->_.lfo )
903 vol_norm *= 1.0f + audio_lfo_pull_sample(ch->_.lfo) * ch->_.lfo_amount;
904
905 float vol_l = vol_norm * framevol_l,
906 vol_r = vol_norm * framevol_r,
907 sample_l,
908 sample_r;
909
910 if( frame_samplerate != 1.0f ){
911 /* absolutely garbage resampling, but it will do
912 */
913
914 float sample_index = frame_samplerate * (float)j;
915 float t = vg_fractf( sample_index );
916
917 u32 i0 = floorf( sample_index ),
918 i1 = i0+1;
919
920 sample_l = pcf[ i0*2+0 ]*(1.0f-t) + pcf[ i1*2+0 ]*t;
921 sample_r = pcf[ i0*2+1 ]*(1.0f-t) + pcf[ i1*2+1 ]*t;
922 }
923 else{
924 sample_l = pcf[ j*2+0 ];
925 sample_r = pcf[ j*2+1 ];
926 }
927
928 buffer[ j*2+0 ] += sample_l * vol_l;
929 buffer[ j*2+1 ] += sample_r * vol_r;
930 }
931
932 ch->volume_movement += AUDIO_MIX_FRAME_SIZE;
933 ch->volume_movement = VG_MIN( ch->volume_movement, ch->_.volume_rate );
934 ch->_.volume = volume;
935
936 vg_profile_end( &_vg_prof_audio_mix );
937 }
938
939 VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
940 {
941 /*
942 * Copy data and move edit flags to commit flags
943 * ------------------------------------------------------------- */
944 audio_lock();
945
946 v3_copy( vg_audio.external_listener_pos, vg_audio.internal_listener_pos );
947 v3_copy( vg_audio.external_listener_ears, vg_audio.internal_listener_ears );
948 v3_copy( vg_audio.external_lister_velocity,
949 vg_audio.internal_listener_velocity );
950 vg_audio.internal_global_volume = vg_audio.external_global_volume;
951
952 for( int i=0; i<AUDIO_CHANNELS; i++ ){
953 audio_channel *ch = &vg_audio.channels[i];
954
955 if( !ch->allocated )
956 continue;
957
958 if( ch->activity == k_channel_activity_alive ){
959 if( (ch->cursor >= ch->source_length) &&
960 !(ch->flags & AUDIO_FLAG_LOOP) )
961 {
962 ch->activity = k_channel_activity_end;
963 }
964 }
965
966 /* process relinquishments */
967 if( (ch->activity != k_channel_activity_reset) && ch->_.relinquished ){
968 if( (ch->activity == k_channel_activity_end)
969 || (ch->_.volume == 0.0f)
970 || (ch->activity == k_channel_activity_error) )
971 {
972 ch->_.relinquished = 0;
973 ch->allocated = 0;
974 ch->activity = k_channel_activity_reset;
975 continue;
976 }
977 }
978
979 /* process new channels */
980 if( ch->activity == k_channel_activity_reset ){
981 ch->_ = ch->editable_state;
982 ch->cursor = 0;
983 ch->source_length = 0;
984 ch->activity = k_channel_activity_wake;
985 }
986
987 if( ch->editble_state_write_mask & AUDIO_EDIT_OWNERSHIP )
988 ch->_.relinquished = ch->editable_state.relinquished;
989 else
990 ch->editable_state.relinquished = ch->_.relinquished;
991
992
993 if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME ){
994 ch->_.volume = ch->editable_state.volume;
995 ch->_.volume_target = ch->editable_state.volume;
996 }
997 else{
998 ch->editable_state.volume = ch->_.volume;
999 }
1000
1001
1002 if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME_SLOPE ){
1003 ch->volume_movement_start = ch->_.volume;
1004 ch->volume_movement = 0;
1005
1006 ch->_.volume_target = ch->editable_state.volume_target;
1007 ch->_.volume_rate = ch->editable_state.volume_rate;
1008 }
1009 else{
1010 ch->editable_state.volume_target = ch->_.volume_target;
1011 ch->editable_state.volume_rate = ch->_.volume_rate;
1012 }
1013
1014
1015 if( ch->editble_state_write_mask & AUDIO_EDIT_SAMPLING_RATE )
1016 ch->_.sampling_rate = ch->editable_state.sampling_rate;
1017 else
1018 ch->editable_state.sampling_rate = ch->_.sampling_rate;
1019
1020
1021 if( ch->editble_state_write_mask & AUDIO_EDIT_LFO_ATTACHMENT ){
1022 ch->_.lfo = ch->editable_state.lfo;
1023 ch->_.lfo_amount = ch->editable_state.lfo_amount;
1024 }
1025 else{
1026 ch->editable_state.lfo = ch->_.lfo;
1027 ch->editable_state.lfo_amount = ch->_.lfo_amount;
1028 }
1029
1030
1031 if( ch->editble_state_write_mask & AUDIO_EDIT_SPACIAL )
1032 v4_copy( ch->editable_state.spacial_falloff,ch->_.spacial_falloff );
1033 else
1034 v4_copy( ch->_.spacial_falloff,ch->editable_state.spacial_falloff );
1035
1036
1037 /* currently readonly, i guess */
1038 ch->editable_state.pan_target = ch->_.pan_target;
1039 ch->editable_state.pan = ch->_.pan;
1040 ch->editble_state_write_mask = 0x00;
1041 }
1042
1043 for( int i=0; i<AUDIO_LFOS; i++ ){
1044 audio_lfo *lfo = &vg_audio.oscillators[ i ];
1045
1046 if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_WAVE ){
1047 lfo->_.wave_type = lfo->editable_state.wave_type;
1048
1049 if( lfo->_.wave_type == k_lfo_polynomial_bipolar ){
1050 lfo->_.polynomial_coefficient =
1051 lfo->editable_state.polynomial_coefficient;
1052 lfo->sqrt_polynomial_coefficient =
1053 sqrtf(lfo->_.polynomial_coefficient);
1054 }
1055 }
1056
1057 if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_PERIOD ){
1058 if( lfo->_.period ){
1059 float t = lfo->time;
1060 t/= (float)lfo->_.period;
1061
1062 lfo->_.period = lfo->editable_state.period;
1063 lfo->time = lfo->_.period * t;
1064 }
1065 else{
1066 lfo->time = 0;
1067 lfo->_.period = lfo->editable_state.period;
1068 }
1069 }
1070
1071 lfo->editble_state_write_mask = 0x00;
1072 }
1073
1074 dsp_update_tunings();
1075 audio_unlock();
1076
1077 /*
1078 * Process spawns
1079 * ------------------------------------------------------------- */
1080 for( int i=0; i<AUDIO_CHANNELS; i++ ){
1081 audio_channel *ch = &vg_audio.channels[i];
1082
1083 if( ch->activity == k_channel_activity_wake ){
1084 if( audio_channel_load_source( ch ) )
1085 ch->activity = k_channel_activity_alive;
1086 else
1087 ch->activity = k_channel_activity_error;
1088 }
1089 }
1090
1091 /*
1092 * Mix everything
1093 * -------------------------------------------------------- */
1094 int frame_count = byte_count/(2*sizeof(float));
1095
1096 /* Clear buffer */
1097 float *pOut32F = (float *)stream;
1098 for( int i=0; i<frame_count*2; i ++ )
1099 pOut32F[i] = 0.0f;
1100
1101 for( int i=0; i<AUDIO_LFOS; i++ ){
1102 audio_lfo *lfo = &vg_audio.oscillators[i];
1103 lfo->time_startframe = lfo->time;
1104 }
1105
1106 for( int i=0; i<AUDIO_CHANNELS; i ++ ){
1107 audio_channel *ch = &vg_audio.channels[i];
1108
1109 if( ch->activity == k_channel_activity_alive ){
1110 if( ch->_.lfo )
1111 ch->_.lfo->time = ch->_.lfo->time_startframe;
1112
1113 u32 remaining = frame_count,
1114 subpos = 0;
1115
1116 while( remaining ){
1117 audio_channel_mix( ch, pOut32F+subpos );
1118 remaining -= AUDIO_MIX_FRAME_SIZE;
1119 subpos += AUDIO_MIX_FRAME_SIZE*2;
1120 }
1121 }
1122 }
1123
1124 vg_profile_begin( &_vg_prof_dsp );
1125
1126 for( int i=0; i<frame_count; i++ )
1127 vg_dsp_process( pOut32F + i*2, pOut32F + i*2 );
1128
1129 vg_profile_end( &_vg_prof_dsp );
1130
1131 audio_lock();
1132
1133 for( int i=0; i<AUDIO_CHANNELS; i ++ ){
1134 audio_channel *ch = &vg_audio.channels[i];
1135 ch->readable_activity = ch->activity;
1136 }
1137
1138 /* Profiling information
1139 * ----------------------------------------------- */
1140 vg_profile_increment( &_vg_prof_audio_decode );
1141 vg_profile_increment( &_vg_prof_audio_mix );
1142 vg_profile_increment( &_vg_prof_dsp );
1143
1144 vg_prof_audio_mix = _vg_prof_audio_mix;
1145 vg_prof_audio_decode = _vg_prof_audio_decode;
1146 vg_prof_audio_dsp = _vg_prof_dsp;
1147
1148 vg_audio.samples_last = frame_count;
1149
1150 if( vg_audio.debug_dsp ){
1151 vg_dsp_update_texture();
1152 }
1153
1154 audio_unlock();
1155 }
1156
1157 VG_STATIC void audio_clip_load( audio_clip *clip, void *lin_alloc )
1158 {
1159 if( lin_alloc == NULL )
1160 lin_alloc = vg_audio.audio_pool;
1161
1162 /* load in directly */
1163 u32 format = clip->flags & AUDIO_FLAG_FORMAT;
1164
1165 /* TODO: This contains audio_lock() and unlock, but i don't know why
1166 * can probably remove them. Low priority to check this */
1167
1168 /* TODO: packed files for vorbis etc, should take from data if its not not
1169 * NULL when we get the clip
1170 */
1171
1172 if( format == k_audio_format_vorbis ){
1173 if( !clip->path ){
1174 vg_fatal_exit_loop( "No path specified, embeded vorbis unsupported" );
1175 }
1176
1177 audio_lock();
1178 clip->data = vg_file_read( lin_alloc, clip->path, &clip->size );
1179 audio_unlock();
1180
1181 if( !clip->data )
1182 vg_fatal_exit_loop( "Audio failed to load" );
1183
1184 float mb = (float)(clip->size) / (1024.0f*1024.0f);
1185 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb );
1186 }
1187 else if( format == k_audio_format_stereo ){
1188 vg_fatal_exit_loop( "Unsupported format (Stereo uncompressed)" );
1189 }
1190 else if( format == k_audio_format_bird ){
1191 if( !clip->data ){
1192 vg_fatal_exit_loop( "No data, external birdsynth unsupported" );
1193 }
1194
1195 u32 total_size = clip->size + sizeof(struct synth_bird);
1196 total_size -= sizeof(struct synth_bird_settings);
1197 total_size = vg_align8( total_size );
1198
1199 if( total_size > AUDIO_DECODE_SIZE )
1200 vg_fatal_exit_loop( "Bird coding too long\n" );
1201
1202 struct synth_bird *bird = vg_linear_alloc( lin_alloc, total_size );
1203 memcpy( &bird->settings, clip->data, clip->size );
1204
1205 clip->data = bird;
1206 clip->size = total_size;
1207
1208 vg_info( "Loaded bird synthesis pattern (%u bytes)\n", total_size );
1209 }
1210 else{
1211 if( !clip->path ){
1212 vg_fatal_exit_loop( "No path specified, embeded mono unsupported" );
1213 }
1214
1215 vg_linear_clear( vg_mem.scratch );
1216 u32 fsize;
1217
1218 stb_vorbis_alloc alloc = {
1219 .alloc_buffer = vg_linear_alloc( vg_mem.scratch, AUDIO_DECODE_SIZE ),
1220 .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE
1221 };
1222
1223 void *filedata = vg_file_read( vg_mem.scratch, clip->path, &fsize );
1224
1225 int err;
1226 stb_vorbis *decoder = stb_vorbis_open_memory(
1227 filedata, fsize, &err, &alloc );
1228
1229 if( !decoder ){
1230 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
1231 clip->path, err );
1232 vg_fatal_exit_loop( "Vorbis decode error" );
1233 }
1234
1235 /* only mono is supported in uncompressed */
1236 u32 length_samples = stb_vorbis_stream_length_in_samples( decoder ),
1237 data_size = length_samples * sizeof(i16);
1238
1239 audio_lock();
1240 clip->data = vg_linear_alloc( lin_alloc, vg_align8(data_size) );
1241 clip->size = length_samples;
1242 audio_unlock();
1243
1244 int read_samples = stb_vorbis_get_samples_i16_downmixed(
1245 decoder, clip->data, length_samples );
1246
1247 if( read_samples != length_samples )
1248 vg_fatal_exit_loop( "Decode error" );
1249
1250 float mb = (float)(data_size) / (1024.0f*1024.0f);
1251 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip->path, mb,
1252 length_samples );
1253 }
1254 }
1255
1256 VG_STATIC void audio_clip_loadn( audio_clip *arr, int count, void *lin_alloc )
1257 {
1258 for( int i=0; i<count; i++ )
1259 audio_clip_load( &arr[i], lin_alloc );
1260 }
1261
1262 VG_STATIC void audio_require_clip_loaded( audio_clip *clip )
1263 {
1264 if( clip->data && clip->size )
1265 return;
1266
1267 audio_unlock();
1268 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
1269 }
1270
1271 /*
1272 * Debugging
1273 */
1274
1275 VG_STATIC void audio_debug_ui( m4x4f mtx_pv )
1276 {
1277 if( !vg_audio.debug_ui )
1278 return;
1279
1280 audio_lock();
1281
1282 glBindTexture( GL_TEXTURE_2D, vg_dsp.view_texture );
1283 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 256, 256,
1284 GL_RGBA, GL_UNSIGNED_BYTE,
1285 vg_dsp.view_texture_buffer );
1286
1287 /*
1288 * Profiler
1289 * -----------------------------------------------------------------------
1290 */
1291
1292 float budget = ((double)vg_audio.samples_last / 44100.0) * 1000.0;
1293 vg_profile_drawn( (struct vg_profile *[]){ &vg_prof_audio_decode,
1294 &vg_prof_audio_mix,
1295 &vg_prof_audio_dsp}, 3,
1296 budget, (ui_rect){ 4, VG_PROFILE_SAMPLE_COUNT*2 + 8,
1297 512, 0 }, 3 );
1298
1299
1300 char perf[128];
1301
1302 /* Draw UI */
1303 vg_uictx.cursor[0] = 512 + 8;
1304 vg_uictx.cursor[1] = VG_PROFILE_SAMPLE_COUNT*2+8+24+12+12;
1305 vg_uictx.cursor[2] = 150;
1306 vg_uictx.cursor[3] = 12;
1307
1308 if( vg_audio.debug_dsp ){
1309 ui_rect view_thing = { 4, vg.window_y-512-4, 512, 512 };
1310 ui_push_image( view_thing, vg_dsp.view_texture );
1311 }
1312
1313 float mb1 = 1024.0f*1024.0f,
1314 usage = vg_linear_get_cur( vg_audio.audio_pool ) / mb1,
1315 total = vg_linear_get_capacity( vg_audio.audio_pool ) / mb1,
1316 percent = (usage/total) * 100.0f;
1317
1318 snprintf( perf, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage, total, percent );
1319
1320 ui_text( vg_uictx.cursor, perf, 1, 0 );
1321 vg_uictx.cursor[1] += 20;
1322
1323 ui_rect overlap_buffer[ AUDIO_CHANNELS ];
1324 u32 overlap_length = 0;
1325
1326 /* Draw audio stack */
1327 for( int i=0; i<AUDIO_CHANNELS; i ++ ){
1328 audio_channel *ch = &vg_audio.channels[i];
1329
1330 vg_uictx.cursor[2] = 400;
1331 vg_uictx.cursor[3] = 18;
1332
1333 ui_new_node();
1334
1335 if( !ch->allocated ){
1336 ui_fill_rect( vg_uictx.cursor, 0x50333333 );
1337
1338 ui_end_down();
1339 vg_uictx.cursor[1] += 1;
1340 continue;
1341 }
1342
1343 const char *formats[] =
1344 {
1345 " mono ",
1346 " stereo ",
1347 " vorbis ",
1348 " none0 ",
1349 " none1 ",
1350 " none2 ",
1351 " none3 ",
1352 " none4 ",
1353 "synth:bird",
1354 " none5 ",
1355 " none6 ",
1356 " none7 ",
1357 " none8 ",
1358 " none9 ",
1359 " none10 ",
1360 " none11 ",
1361 };
1362
1363 const char *activties[] =
1364 {
1365 "reset",
1366 "wake ",
1367 "alive",
1368 "end ",
1369 "error"
1370 };
1371
1372 u32 format_index = (ch->source->flags & AUDIO_FLAG_FORMAT)>>9;
1373
1374 snprintf( perf, 127, "%02d %c%c%cD %s [%s] %4.2fv'%s'",
1375 i,
1376 (ch->editable_state.relinquished)? 'r': '_',
1377 0? 'r': '_',
1378 0? '3': '2',
1379 formats[format_index],
1380 activties[ch->readable_activity],
1381 ch->editable_state.volume,
1382 ch->name );
1383
1384 ui_fill_rect( vg_uictx.cursor, 0xa0000000 | ch->colour );
1385
1386 vg_uictx.cursor[0] += 2;
1387 vg_uictx.cursor[1] += 2;
1388 ui_text( vg_uictx.cursor, perf, 1, 0 );
1389
1390 ui_end_down();
1391 vg_uictx.cursor[1] += 1;
1392
1393 if( AUDIO_FLAG_SPACIAL_3D ){
1394 v4f wpos;
1395 v3_copy( ch->editable_state.spacial_falloff, wpos );
1396
1397 wpos[3] = 1.0f;
1398 m4x4_mulv( mtx_pv, wpos, wpos );
1399
1400 if( wpos[3] > 0.0f ){
1401 v2_muls( wpos, (1.0f/wpos[3]) * 0.5f, wpos );
1402 v2_add( wpos, (v2f){ 0.5f, 0.5f }, wpos );
1403
1404 ui_rect wr;
1405 wr[0] = vg_clampf(wpos[0] * vg.window_x, -32000.0f,32000.0f);
1406 wr[1] = vg_clampf((1.0f-wpos[1]) * vg.window_y,-32000.0f,32000.0f);
1407 wr[2] = 100;
1408 wr[3] = 17;
1409
1410 for( int j=0; j<12; j++ ){
1411 int collide = 0;
1412 for( int k=0; k<overlap_length; k++ ){
1413 ui_px *wk = overlap_buffer[k];
1414 if( ((wr[0] <= wk[0]+wk[2]) && (wr[0]+wr[2] >= wk[0])) &&
1415 ((wr[1] <= wk[1]+wk[3]) && (wr[1]+wr[3] >= wk[1])) )
1416 {
1417 collide = 1;
1418 break;
1419 }
1420 }
1421
1422 if( !collide )
1423 break;
1424 else
1425 wr[1] += 18;
1426 }
1427
1428 ui_text( wr, perf, 1, 0 );
1429
1430 ui_rect_copy( wr, overlap_buffer[ overlap_length ++ ] );
1431 }
1432 }
1433 }
1434
1435 audio_unlock();
1436 }
1437
1438 #endif /* VG_AUDIO_H */