silly mistake
[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 = new_volume;
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) * ch->_.lfo_amount;
796
797 float vol_l = vol_norm * framevol_l,
798 vol_r = vol_norm * framevol_r,
799 sample_l,
800 sample_r;
801
802 if( ch->_.sampling_rate != 1.0f )
803 {
804 /* absolutely garbage resampling, but it will do
805 */
806
807 float sample_index = ch->_.sampling_rate * (float)j;
808 float t = vg_fractf( sample_index );
809
810 u32 i0 = floorf( sample_index ),
811 i1 = i0+1;
812
813 sample_l = pcf[ i0*2+0 ]*(1.0f-t) + pcf[ i1*2+0 ]*t;
814 sample_r = pcf[ i0*2+1 ]*(1.0f-t) + pcf[ i1*2+1 ]*t;
815 }
816 else
817 {
818 sample_l = pcf[ j*2+0 ];
819 sample_r = pcf[ j*2+1 ];
820 }
821
822 buffer[ j*2+0 ] += sample_l * vol_l;
823 buffer[ j*2+1 ] += sample_r * vol_r;
824 }
825
826 vg_profile_end( &_vg_prof_audio_mix );
827 }
828
829 VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
830 {
831 /*
832 * Copy data and move edit flags to commit flags
833 * ------------------------------------------------------------- */
834 audio_lock();
835 for( int i=0; i<AUDIO_CHANNELS; i++ )
836 {
837 audio_channel *ch = &vg_audio.channels[i];
838
839 if( !ch->allocated )
840 continue;
841
842 /* process relinquishments */
843 if( (ch->activity != k_channel_activity_reset) && ch->_.relinquished )
844 {
845 if( (ch->cursor >= ch->source_length && !(ch->flags & AUDIO_FLAG_LOOP))
846 || (ch->_.volume == 0.0f)
847 || (ch->activity == k_channel_activity_error) )
848 {
849 ch->_.relinquished = 0;
850 ch->allocated = 0;
851 ch->activity = k_channel_activity_reset;
852 continue;
853 }
854 }
855
856 /* process new channels */
857 if( ch->activity == k_channel_activity_reset )
858 {
859 ch->_ = ch->editable_state;
860 ch->cursor = 0;
861 ch->source_length = 0;
862 ch->activity = k_channel_activity_wake;
863 }
864
865 if( ch->editble_state_write_mask & AUDIO_EDIT_OWNERSHIP )
866 ch->_.relinquished = ch->editable_state.relinquished;
867 else
868 ch->editable_state.relinquished = ch->_.relinquished;
869
870
871 if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME )
872 ch->_.volume = ch->editable_state.volume;
873 else
874 ch->editable_state.volume = ch->_.volume;
875
876
877 if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME_SLOPE )
878 {
879 ch->volume_movement_start = ch->_.volume;
880 ch->volume_movement = 0;
881
882 ch->_.volume_target = ch->editable_state.volume_target;
883 ch->_.volume_rate = ch->editable_state.volume_rate;
884 }
885 else
886 {
887 ch->editable_state.volume_target = ch->_.volume_target;
888 ch->editable_state.volume_rate = ch->_.volume_rate;
889 }
890
891
892 if( ch->editble_state_write_mask & AUDIO_EDIT_SAMPLING_RATE )
893 ch->_.sampling_rate = ch->editable_state.sampling_rate;
894 else
895 ch->editable_state.sampling_rate = ch->_.sampling_rate;
896
897
898 if( ch->editble_state_write_mask & AUDIO_EDIT_LFO_ATTACHMENT )
899 {
900 ch->_.lfo = ch->editable_state.lfo;
901 ch->_.lfo_amount = ch->editable_state.lfo_amount;
902 }
903 else
904 {
905 ch->editable_state.lfo = ch->_.lfo;
906 ch->editable_state.lfo_amount = ch->_.lfo_amount;
907 }
908
909
910 if( ch->editble_state_write_mask & AUDIO_EDIT_SPACIAL )
911 v4_copy( ch->editable_state.spacial_falloff,ch->_.spacial_falloff );
912 else
913 v4_copy( ch->_.spacial_falloff,ch->editable_state.spacial_falloff );
914
915
916 /* currently readonly, i guess */
917 ch->editable_state.pan_target = ch->_.pan_target;
918 ch->editable_state.pan = ch->_.pan;
919 ch->editble_state_write_mask = 0x00;
920 }
921
922 for( int i=0; i<AUDIO_LFOS; i++ )
923 {
924 audio_lfo *lfo = &vg_audio.oscillators[ i ];
925
926 if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_WAVE )
927 {
928 lfo->_.wave_type = lfo->editable_state.wave_type;
929
930 if( lfo->_.wave_type == k_lfo_polynomial_bipolar )
931 {
932 lfo->_.polynomial_coefficient =
933 lfo->editable_state.polynomial_coefficient;
934 lfo->sqrt_polynomial_coefficient =
935 sqrtf(lfo->_.polynomial_coefficient);
936 }
937 }
938
939 if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_PERIOD )
940 {
941 if( lfo->_.period )
942 {
943 float t = lfo->time;
944 t/= (float)lfo->_.period;
945
946 lfo->_.period = lfo->editable_state.period;
947 lfo->time = lfo->_.period * t;
948 }
949 else
950 {
951 lfo->time = 0;
952 lfo->_.period = lfo->editable_state.period;
953 }
954 }
955
956 lfo->editble_state_write_mask = 0x00;
957 }
958
959
960 audio_unlock();
961
962 /*
963 * Process spawns
964 * ------------------------------------------------------------- */
965 for( int i=0; i<AUDIO_CHANNELS; i++ )
966 {
967 audio_channel *ch = &vg_audio.channels[i];
968
969 if( ch->activity == k_channel_activity_wake )
970 {
971 if( audio_channel_load_source( ch ) )
972 ch->activity = k_channel_activity_alive;
973 else
974 ch->activity = k_channel_activity_error;
975 }
976 }
977
978 /*
979 * Mix everything
980 * -------------------------------------------------------- */
981 int frame_count = byte_count/(2*sizeof(float));
982
983 /* Clear buffer */
984 float *pOut32F = (float *)stream;
985 for( int i=0; i<frame_count*2; i ++ )
986 pOut32F[i] = 0.0f;
987
988 for( int i=0; i<AUDIO_LFOS; i++ )
989 {
990 audio_lfo *lfo = &vg_audio.oscillators[i];
991 lfo->time_startframe = lfo->time;
992 }
993
994 for( int i=0; i<AUDIO_CHANNELS; i ++ )
995 {
996 audio_channel *ch = &vg_audio.channels[i];
997
998 if( ch->activity == k_channel_activity_alive )
999 audio_channel_mix( ch, pOut32F, frame_count );
1000 }
1001
1002 /*
1003 * Relinquishing conditions
1004 * ------------------------------------------------------------------
1005 */
1006 audio_lock();
1007
1008 /* Profiling information
1009 * ----------------------------------------------- */
1010 vg_profile_increment( &_vg_prof_audio_decode );
1011 vg_profile_increment( &_vg_prof_audio_mix );
1012 vg_prof_audio_mix = _vg_prof_audio_mix;
1013 vg_prof_audio_decode = _vg_prof_audio_decode;
1014 vg_audio.samples_last = frame_count;
1015
1016 audio_unlock();
1017 }
1018
1019 VG_STATIC void audio_clip_load( audio_clip *clip, void *lin_alloc )
1020 {
1021 if( lin_alloc == NULL )
1022 lin_alloc = vg_audio.audio_pool;
1023
1024
1025 /* load in directly */
1026 if( clip->flags & AUDIO_FLAG_VORBIS )
1027 {
1028 audio_lock();
1029 clip->data = vg_file_read( lin_alloc, clip->path, &clip->size );
1030 audio_unlock();
1031
1032 if( !clip->data )
1033 vg_fatal_exit_loop( "Audio failed to load" );
1034
1035 float mb = (float)(clip->size) / (1024.0f*1024.0f);
1036 vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb );
1037 }
1038 else if( clip->flags & AUDIO_FLAG_STEREO )
1039 {
1040 vg_fatal_exit_loop( "Unsupported format (Stereo uncompressed)" );
1041 }
1042 else
1043 {
1044 vg_linear_clear( vg_mem.scratch );
1045 u32 fsize;
1046
1047 stb_vorbis_alloc alloc = {
1048 .alloc_buffer = vg_linear_alloc( vg_mem.scratch, AUDIO_DECODE_SIZE ),
1049 .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE
1050 };
1051
1052 void *filedata = vg_file_read( vg_mem.scratch, clip->path, &fsize );
1053
1054 int err;
1055 stb_vorbis *decoder = stb_vorbis_open_memory(
1056 filedata, fsize, &err, &alloc );
1057
1058 if( !decoder )
1059 {
1060 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
1061 clip->path, err );
1062 vg_fatal_exit_loop( "Vorbis decode error" );
1063 }
1064
1065 /* only mono is supported in uncompressed */
1066 u32 length_samples = stb_vorbis_stream_length_in_samples( decoder ),
1067 data_size = length_samples * sizeof(i16);
1068
1069 audio_lock();
1070 clip->data = vg_linear_alloc( lin_alloc, vg_align8(data_size) );
1071 clip->size = length_samples;
1072 audio_unlock();
1073
1074 int read_samples = stb_vorbis_get_samples_i16_downmixed(
1075 decoder, clip->data, length_samples );
1076
1077 if( read_samples != length_samples )
1078 vg_fatal_exit_loop( "Decode error" );
1079
1080 float mb = (float)(data_size) / (1024.0f*1024.0f);
1081 vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip->path, mb,
1082 length_samples );
1083 }
1084 }
1085
1086 VG_STATIC void audio_clip_loadn( audio_clip *arr, int count, void *lin_alloc )
1087 {
1088 for( int i=0; i<count; i++ )
1089 audio_clip_load( &arr[i], lin_alloc );
1090 }
1091
1092 VG_STATIC void audio_require_clip_loaded( audio_clip *clip )
1093 {
1094 if( clip->data && clip->size )
1095 return;
1096
1097 audio_unlock();
1098 vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
1099 }
1100
1101 /*
1102 * Debugging
1103 */
1104
1105 VG_STATIC void audio_debug_ui( m4x4f mtx_pv )
1106 {
1107 if( !vg_audio.debug_ui )
1108 return;
1109
1110 audio_lock();
1111
1112 /*
1113 * Profiler
1114 * -----------------------------------------------------------------------
1115 */
1116
1117 float budget = ((double)vg_audio.samples_last / 44100.0) * 1000.0;
1118 vg_profile_drawn( (struct vg_profile *[]){ &vg_prof_audio_decode,
1119 &vg_prof_audio_mix }, 2,
1120 budget, (ui_rect){ 4, VG_PROFILE_SAMPLE_COUNT*2 + 8,
1121 250, 0 }, 3 );
1122
1123
1124 char perf[128];
1125
1126 /* Draw UI */
1127 vg_uictx.cursor[0] = 258;
1128 vg_uictx.cursor[1] = VG_PROFILE_SAMPLE_COUNT*2+8+24+12;
1129 vg_uictx.cursor[2] = 150;
1130 vg_uictx.cursor[3] = 12;
1131
1132 float mb1 = 1024.0f*1024.0f,
1133 usage = vg_linear_get_cur( vg_audio.audio_pool ) / mb1,
1134 total = vg_linear_get_capacity( vg_audio.audio_pool ) / mb1,
1135 percent = (usage/total) * 100.0f;
1136
1137 snprintf( perf, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage, total, percent );
1138
1139 ui_text( vg_uictx.cursor, perf, 1, 0 );
1140 vg_uictx.cursor[1] += 20;
1141
1142 ui_rect overlap_buffer[ AUDIO_CHANNELS ];
1143 u32 overlap_length = 0;
1144
1145 /* Draw audio stack */
1146 for( int i=0; i<AUDIO_CHANNELS; i ++ )
1147 {
1148 audio_channel *ch = &vg_audio.channels[i];
1149
1150 vg_uictx.cursor[2] = 400;
1151 vg_uictx.cursor[3] = 18;
1152
1153 ui_new_node();
1154
1155 if( !ch->allocated )
1156 {
1157 ui_fill_rect( vg_uictx.cursor, 0x50333333 );
1158
1159 ui_end_down();
1160 vg_uictx.cursor[1] += 1;
1161 continue;
1162 }
1163
1164 const char *formats[] =
1165 {
1166 "------",
1167 "Mono ",
1168 "Stereo",
1169 "Vorbis"
1170 };
1171
1172 int format_index = 0;
1173
1174 if( ch->source->flags & AUDIO_FLAG_STEREO )
1175 format_index = 2;
1176 else if( ch->source->flags & AUDIO_FLAG_VORBIS )
1177 format_index = 3;
1178 else
1179 format_index = 1;
1180
1181 snprintf( perf, 127, "%02d %c%c%cD %s %4.2fv'%s'",
1182 i,
1183 (ch->editable_state.relinquished)? 'r': ' ',
1184 0? 'r': ' ',
1185 0? '3': '2',
1186 formats[format_index],
1187 ch->editable_state.volume,
1188 ch->name );
1189
1190 if( format_index == 0 )
1191 {
1192 ui_fill_rect( vg_uictx.cursor, 0xa00000ff );
1193 }
1194 else
1195 {
1196 ui_fill_rect( vg_uictx.cursor, 0xa0333333 );
1197 }
1198
1199 vg_uictx.cursor[0] += 2;
1200 vg_uictx.cursor[1] += 2;
1201 ui_text( vg_uictx.cursor, perf, 1, 0 );
1202
1203 ui_end_down();
1204 vg_uictx.cursor[1] += 1;
1205
1206 if( AUDIO_FLAG_SPACIAL_3D )
1207 {
1208 v4f wpos;
1209 v3_copy( ch->editable_state.spacial_falloff, wpos );
1210
1211 wpos[3] = 1.0f;
1212 m4x4_mulv( mtx_pv, wpos, wpos );
1213
1214 if( wpos[3] > 0.0f )
1215 {
1216 v2_muls( wpos, (1.0f/wpos[3]) * 0.5f, wpos );
1217 v2_add( wpos, (v2f){ 0.5f, 0.5f }, wpos );
1218
1219 ui_rect wr;
1220 wr[0] = wpos[0] * vg.window_x;
1221 wr[1] = (1.0f-wpos[1]) * vg.window_y;
1222 wr[2] = 100;
1223 wr[3] = 17;
1224
1225 for( int j=0; j<12; j++ )
1226 {
1227 int collide = 0;
1228 for( int k=0; k<overlap_length; k++ )
1229 {
1230 ui_px *wk = overlap_buffer[k];
1231 if( ((wr[0] <= wk[0]+wk[2]) && (wr[0]+wr[2] >= wk[0])) &&
1232 ((wr[1] <= wk[1]+wk[3]) && (wr[1]+wr[3] >= wk[1])) )
1233 {
1234 collide = 1;
1235 break;
1236 }
1237 }
1238
1239 if( !collide )
1240 break;
1241 else
1242 wr[1] += 18;
1243 }
1244
1245 ui_text( wr, perf, 1, 0 );
1246
1247 ui_rect_copy( wr, overlap_buffer[ overlap_length ++ ] );
1248 }
1249 }
1250 }
1251
1252 audio_unlock();
1253 }
1254
1255 #endif /* VG_AUDIO_H */