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