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