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