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