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