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