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