Untitled
Loading...
Searching...
No Matches
audio.c
Go to the documentation of this file.
1
6
7#include "audio/audio.h"
8#include "SDL2/SDL_mutex.h"
9#include "SDL2/SDL_thread.h"
10#include "core/camera.h"
11#include "load/init.h"
12#include "utilities/mathf.h"
13#include "miniaudio/miniaudio.h"
14#include "render/render.h"
15#include <string.h>
16#include "core/engine_types.h"
17#include "core/acceleration.h"
19#include "fftw3.h"
20#include <math.h>
21#include "audio/audio_ir.h"
22#include "utilities/utilities.h"
23#include <stdatomic.h>
24#include <stddef.h>
25#include <stdio.h>
26
27
29
30
31
36static void heap_swap(OpenNode *a, OpenNode *b);
37
43static void heap_push(Vector * h);
44
51static OpenNode heap_pop(Vector *h);
52
62static void reconstruct_path_aportal(AStarWorkspace * WS, int start, int end);
63
64
74static int a_star_aportal(AStarWorkspace * WS, int start, int end);
75
76
85static float calculate_path_volume_equal_power(float path_distance, float min_distance, float max_distance);
86
99static bool pathfind_audio_occlusion(vec3 listener_pos, int closest_l, vec3 emitter_pos, vec3 * out_pos, float * out_volume, float * distance);
100
101
117static vec3 pos_to_pv_local(OBB *obb, vec3 pos);
118
119
127static int query_probe_volume(vec3 pos);
128
135static void remove_audio_pv(APV * apv, bool * apb);
136
143static void load_audio_pv(APV * apv, ProbeVolume * pv);
144
151static void interpolate_nearest_aprobes(vec3 pos, AudioProbeWeights * out_w);
152
162static float process_biquad(Biquad* filter, float in_sample) ;
163
172static void init_bandpass(Biquad * filter, float sample_rate, float center_freq, float q);
173
199static void generate_synthetic_IR_tail(float* out_tail, float fade_start, AudioProbe * probe, float rms);
200
218static void compute_schroeder_edc(float* energy_histogram, float* edc_out);
219
220
237static float extract_rms(AudioProbe * probe, int fade_start, int window_size);
238
282static void extract_t60(float * edc_db, AudioProbe * probe, int band, float bin_duration_seconds, int last_sample);
283
284
410static void process_probe_chunk(int start, int count);
411
412#define SAMPLE_IDX(i, sh, b) \
413 (((i) * SH_MAX * AUDIO_NUM_BANDS) + ((sh) * AUDIO_NUM_BANDS) + (b)) \
414
415#define Y_00 0.28209479177387814f
416
417
419{
420
422 for (int i = 0; i < GLOBALS.audio_portals.count; i++)
423 {
424
425 AudioPortal * p = &portals[i];
426
428 init_vector(&p->adjacent_portals, 0, sizeof(int));
429
430 }
431
432 for (int i = 0; i < GLOBALS.audio_portals.count; i++)
433 {
434
435 AudioPortal * p0 = &portals[i];
436
437
438 for (int j = i+1; j < GLOBALS.audio_portals.count; j++)
439 {
440 AudioPortal * p1 = &portals[j];
441
442 vec3 diff = vec3_sub(p1->pos, p0->pos);
443 float dist = vec3_length(diff);
444 vec3 dir = vec3_scale(diff, 1.0f/dist);
445 RayHit hit = raycast_scene(p0->pos, dir, INFINITY, false);
446 if (hit.hit && hit.t < dist)
447 {
448 continue;
449 }
450 else
451 {
452 int * p0a = vector_expand(&p0->adjacent_portals, 1);
453 int * p1a = vector_expand(&p1->adjacent_portals, 1);
454 *p0a = j;
455 *p1a = i;
456 }
457 }
458 }
459 for (int i = 0; i < GLOBALS.audio_portals.count; i++)
460 {
461 AudioPortal * p = &portals[i];
462 int * adj = CAST_VEC(p->adjacent_portals, int);
463 for (int j = 0; j < p->adjacent_portals.count; j++)
464 {
465 printf("Portal %d Adjacent to %d\n", i, adj[j]);
466 PRINT_VEC3N(p->pos);
467 PRINT_VEC3N(portals[adj[j]].pos);
468
469 }
470 }
471}
472
473#define APORTAL_VISIBILITY_RADIUS 1.0f
474
476{
477
479 float closest_dist = INFINITY;
480 int closest_index = -1;
481 for (int i = 0; i < GLOBALS.audio_portals.count; i++)
482 {
483
484 AudioPortal * p = &portals[i];
485
486 vec3 diff = vec3_sub(p->pos, pos);
487 float dist = vec3_length(diff);
488 vec3 dir = vec3_scale(diff, 1.0f / dist);
489
490 RayHit hit = raycast_scene(pos, dir, INFINITY, false);
491
492 if (hit.hit && hit.t < dist)
493 {
494 continue;
495 }
496 else
497 {
498 if (closest_dist > dist)
499 {
500 closest_dist = dist;
501 closest_index = i;
502 }
503 }
504 }
505
506 return closest_index;
507
508}
509
510
511static void heap_swap(OpenNode *a, OpenNode *b) {
512 OpenNode tmp = *a; *a = *b; *b = tmp;
513}
514
515static void heap_push(Vector * h) {
516 int i = h->count - 1;
517 OpenNode * buf = CAST_VEC(*h, OpenNode);
518
519 while (i > 0) {
520 int parent = (i - 1) / 2;
521 if (buf[parent].f <= buf[i].f) break;
522 heap_swap(&buf[parent], &buf[i]);
523 i = parent;
524 }
525}
526
528
529 OpenNode * buf = CAST_VEC(*h, OpenNode);
530 OpenNode root = buf[0];
531 buf[0] = buf[--h->count];
532 int i = 0;
533 while (1) {
534 int left = 2*i + 1, right = 2*i + 2, smallest = i;
535 if (left < h->count && buf[left].f < buf[smallest].f) smallest = left;
536 if (right < h->count && buf[right].f < buf[smallest].f) smallest = right;
537 if (smallest == i) break;
538 heap_swap(&buf[i], &buf[smallest]);
539 i = smallest;
540 }
541 return root;
542}
543
544
545static void reconstruct_path_aportal(AStarWorkspace * WS, int start, int end)
546{
547
548 int current = end;
549 while (current != start)
550 {
551 if (WS->path_count >= MAX_PORTALS) break;
552 WS->path[WS->path_count++] = current;
553
554 int prev = WS->stack[end].came_from;
555 if (prev == -1)
556 {
557 printf("NO PARENT %d\n", current);
558 break;
559 }
560 current = prev;
561 }
562 if (WS->path_count < MAX_PORTALS - 1) WS->path[WS->path_count++] = start;
563
564 for (int i = 0; i < WS->path_count / 2; i++) {
565 int tmp = WS->path[i];
566 WS->path[i] = WS->path[WS->path_count-1-i];
567 WS->path[WS->path_count-1-i] = tmp;
568 }
569
570}
571
572
573
574
575
576static int a_star_aportal(AStarWorkspace * WS, int start, int end)
577{
578
579
580 WS->current_run++;
581 WS->path_count = 0;
582 init_vector(&WS->min_heap, 0, sizeof(OpenNode));
583
585
586 AudioPortal * p0 = &portals[start];
587
588 AudioPortal * p1 = &portals[end];
589
590 OpenNode * start_node = vector_expand(&WS->min_heap, 1);
591 start_node->i = start;
592 float f = vec3_length(vec3_sub(p0->pos, p1->pos));
593 start_node->f = f;
594 WS->stack[start].g = 0.0f;
595 WS->stack[start].f = f;
596 WS->stack[start].came_from = -1;
597 WS->stack[start].run_id = WS->current_run;
598 heap_push(&WS->min_heap);
599
600
601 while(WS->min_heap.count > 0)
602 {
603
604 OpenNode current = heap_pop(&WS->min_heap);
605 if (current.i == end)
606 {
607 reconstruct_path_aportal(WS, start, end);
608 return end;
609 }
610
611 AudioPortal * current_portal = &portals[current.i];
612 if (!current_portal->adjacent_portals.buffer) continue;
613 int * adj = CAST_VEC(current_portal->adjacent_portals, int);
614 for (int i = 0; i < current_portal->adjacent_portals.count; i++)
615 {
616
617 int neighbor = adj[i];
618
619 AudioPortal * neighbor_portal = &portals[neighbor];
620 float tenative_g = current.g + vec3_length(vec3_sub(current_portal->pos, neighbor_portal->pos));
621 float old_g = WS->stack[neighbor].g;
622 bool did_search = (WS->stack[neighbor].run_id == WS->current_run);
623
624 if (!did_search || tenative_g < old_g)
625 {
626 float f = tenative_g + vec3_length(vec3_sub(neighbor_portal->pos, p1->pos));
627
628 OpenNode * neighbor_node = vector_expand(&WS->min_heap, 1);
629 neighbor_node->i = neighbor;
630 neighbor_node->g = tenative_g;
631 neighbor_node->f = f;
632 neighbor_node->parent = current.i;
633
634 WS->stack[neighbor].came_from = current.i;
635 WS->stack[neighbor].f = f;
636 WS->stack[neighbor].g = tenative_g;
637 WS->stack[neighbor].run_id = WS->current_run;
638 heap_push(&WS->min_heap);
639
640 }
641 }
642 }
643
644 return -1;
645}
646
647
648
649
650static float calculate_path_volume_equal_power(float path_distance, float min_distance, float max_distance) {
651 if (path_distance <= min_distance) return 1.0f;
652 if (path_distance >= max_distance) return 0.0f;
653
654 float norm = (path_distance - min_distance) / (max_distance - min_distance);
655 float linear_factor = 1.0f - norm;
656
657 return linear_factor * linear_factor;
658}
659
660
661static bool pathfind_audio_occlusion(vec3 listener_pos, int closest_l, vec3 emitter_pos, vec3 * out_pos, float * out_volume, float * distance)
662{
663
664
665 Timer res_timer;
666 start_timer(&res_timer);
667 int closest_e = find_closest_visible_aportal(emitter_pos);
668
669
670 if (closest_l == -1 || closest_e == -1) return false;
671
672
673
674 int res = a_star_aportal(&AW, closest_l, closest_e);
675
676 double res_time = end_timer(&res_timer);
677
678 if (res == -1)
679 {
680 return false;
681 }
682
684
685
686 start_timer(&res_timer);
687 int final_path_pos = 0;
688 int actual_path_start = 0;
689 for (int i = 0; i < AW.path_count; i++)
690 {
691
692 AudioPortal * p = &portals[AW.path[i]];
693
694 vec3 diff = vec3_sub(p->pos, listener_pos);
695 float dist = vec3_length(diff);
696 vec3 dir = vec3_scale(diff, 1.0f / dist);
697
698 RayHit hit = raycast_scene(listener_pos, dir, INFINITY, false);
699
700 if (hit.hit && hit.t < dist)
701 {
702 break;
703 }
704 final_path_pos = AW.path[i];
705 actual_path_start = i;
706 }
707
708 float dist = 0.0f;
709 dist += vec3_length(vec3_sub(listener_pos, portals[final_path_pos].pos));
710 dist += vec3_length(vec3_sub(emitter_pos, portals[closest_e].pos));
711
712 for (int i = actual_path_start; i < AW.path_count-1; i++)
713 {
714
715 AudioPortal * p = &portals[AW.path[i]];
716 dist += vec3_length(vec3_sub(p->pos, portals[AW.path[i+1]].pos));
717
718 }
719
720 if (final_path_pos < 0)
721 {
722 printf("Somehow the final path pos was less than zero which means that the original aportal thats visible is not visible.\n");
723 return false;
724 }
725
726 *out_pos = portals[final_path_pos].pos;
727 *out_volume = calculate_path_volume_equal_power(dist, 0.2f, 20.0f);
728 *distance = dist;
729 double res_time2 = end_timer(&res_timer);
730
731 return true;
732
733}
734
735
736
738{
739
741 ap->pos = pos;
742 init_vector(&ap->adjacent_portals, 0, sizeof(int));
743}
744
746{
747
749 AudioPortal * ap = &aps[id];
750
753
754}
755
756
757
758
759void load_emitter(AudioEmitter * emitter, const char* filepath, bool is_loop, bool is_occludable, float base_volume)
760{
761 ma_sound_config sound_config = ma_sound_config_init();
762
763 sound_config.pFilePath = filepath;
764 sound_config.channelsIn = 1;
765 sound_config.channelsOut = 0;
766
767 ma_result res = ma_sound_init_from_file(&AUDIO_ENGINE, filepath, 0, NULL, NULL, &emitter->sound_obj);
768
769 if (res != MA_SUCCESS)
770 {
771 return;
772 }
773 emitter->is_loaded = true;
774 emitter->is_loop = is_loop;
775 emitter->is_occludable = is_occludable;
776 emitter->base_volume = base_volume;
777 strcpy(emitter->path, filepath);
778 ma_uint32 channels = ma_engine_get_channels(&AUDIO_ENGINE);
779 ma_uint32 sampleRate = ma_engine_get_sample_rate(&AUDIO_ENGINE);
780 emitter->current_cutoff = 20000.0f;
781
782 ma_result res2 = ma_node_attach_output_bus(&emitter->sound_obj, 0, &CONVOLVER_NODE, 0);
783 if (res2 != MA_SUCCESS)
784 {
785 printf("COULDNT BIND\n");
786 if (res2 == MA_INVALID_OPERATION)
787 {
788 printf("INVALID\n");
789 }
790 }
791
792 ma_sound_set_spatialization_enabled(&emitter->sound_obj, MA_TRUE);
793}
794
795
796// static void update_lpf_sound(AudioEmitter * emitter, bool occluded)
797// {
798// float cutoff = occluded ? 350.0f : 20000.0f;
799
800// emitter->current_cutoff += (cutoff - emitter->current_cutoff) * 10.0f * dt;
801
802
803// ma_lpf_config filterUpdate = ma_lpf_config_init(
804// emitter->lpf_node.lpf.format,
805// emitter->lpf_node.lpf.channels,
806// emitter->lpf_node.lpf.sampleRate,
807// emitter->current_cutoff,
808// LPF_ORDER
809// );
810
811// ma_result result = ma_lpf_node_reinit(&filterUpdate, &emitter->lpf_node);
812// }
813
814AudioEmitter * init_emitter(const char * filepath, bool is_loop, bool is_occludable, float base_volume)
815{
816
817 AudioEmitter * emitter = NULL;
818 for (int i = 0; i < MAX_AUDIO_EMITTERS; i++)
819 {
820 if (AUDIO_EMITTERS[i].free)
821 {
822 emitter = &AUDIO_EMITTERS[i];
823 break;
824 }
825 }
826 load_emitter(emitter, filepath, is_loop, is_occludable, base_volume);
827 emitter->free = false;
828 return emitter;
829}
830
831void unload_emitter(int id)
832{
833
834 AudioEmitter * e = &AUDIO_EMITTERS[id];
835 ma_sound_uninit(&e->sound_obj);
836 e->is_loaded = false;
837
838
839}
840
841void destroy_emitter(int id)
842{
843
844 AudioEmitter * e = &AUDIO_EMITTERS[id];
845
846 ma_sound_uninit(&e->sound_obj);
847 memset(e, 0, sizeof(AudioEmitter));
848 e->free = true;
849
850}
851
852
853void play_emitter(AudioEmitter * emitter, vec3 pos)
854{
855
856 if (!emitter->is_loaded) return;
857 emitter->pos = pos;
858 ma_sound_set_position(&emitter->sound_obj, pos.x, pos.y, pos.z);
859 if (emitter->is_loop)
860 {
861
862 if (ma_sound_is_looping(&emitter->sound_obj))
863 {
864 return;
865 }
866 else
867 {
868
869 ma_sound_set_looping(&emitter->sound_obj, MA_TRUE);
870 ma_sound_start(&emitter->sound_obj);
871 }
872 }
873 else
874 {
875 ma_sound_seek_to_pcm_frame(&emitter->sound_obj, 0);
876 ma_sound_start(&emitter->sound_obj);
877 }
878
879
880}
882{
883 ma_sound_seek_to_pcm_frame(&e->sound_obj, 0);
884 ma_sound_stop(&e->sound_obj);
885}
887
888 ma_engine_listener_set_position(&AUDIO_ENGINE, 0, player->skeleton.neck.pos.x, player->skeleton.neck.pos.y, player->skeleton.neck.pos.z);
889 ma_engine_listener_set_direction(&AUDIO_ENGINE, 0, player->cam->forward.x, player->cam->forward.y, player->cam->forward.z);
890 ma_engine_listener_set_world_up(&AUDIO_ENGINE, 0, player->cam->up.x, player->cam->up.y, player->cam->up.z);
891
892
893 for (int i = 0; i < MAX_AUDIO_EMITTERS; i++) {
894 AudioEmitter* emitter = &AUDIO_EMITTERS[i];
895 if (emitter->free) continue;
896 if (!ma_sound_is_playing(&emitter->sound_obj)) continue;
897
898 ma_sound_set_position(&emitter->sound_obj, emitter->virtual_pos.x, emitter->virtual_pos.y, emitter->virtual_pos.z);
899
900
901 ma_sound_set_min_distance(&emitter->sound_obj, 100000.0f);
902 ma_sound_set_max_distance(&emitter->sound_obj, 100000.0f);
903 ma_sound_set_volume(&emitter->sound_obj, emitter->current_volume);
904 }
905}
906
907
908void SDLAudioCallback(void* pUserData, Uint8* pStream, int len) {
909
910 ma_engine* pEngine = (ma_engine*)pUserData;
911
912 ma_uint64 frames_to_read = len / (sizeof(float) * 2);
913 memset(pStream, 0, len);
914
915 ma_engine_read_pcm_frames(pEngine, pStream, frames_to_read, NULL);
916}
917
918
919
920static vec3 pos_to_pv_local(OBB *obb, vec3 pos)
921{
922
923 vec3 local_pos = vec3_sub(pos, obb->center);
924 vec3 unrotated = mat3_mul_vec3(mat3_transpose(obb->rot), local_pos);
925 vec3 norm = vec3_div(unrotated, vec3_scale(obb->extents, 2.0f));
926 return vec3_add(norm, (vec3){0.5f, 0.5f, 0.5f});
927}
928
929int query_audio_probe(ProbeVolume * pv, vec3 pos, ivec3 * out_local)
930{
931
932 vec3 pv_local = pos_to_pv_local(&pv->obb, pos);
933 ivec3 pv_index = {clamp(pv_local.x * (pv->resolution.x), 0, pv->resolution.x-1), clamp(pv_local.y * (pv->resolution.y), 0, pv->resolution.y-1), clamp(pv_local.z * (pv->resolution.z), 0, pv->resolution.z-1)};
934 *out_local = pv_index;
935 return pv_index.z * pv->resolution.y * pv->resolution.x + pv_index.y * pv->resolution.x + pv_index.x;
936}
937
939{
941
942 for (int i = 0; i < GLOBALS.scene_data.audio_probe_volumes.count; i++)
943 {
944 ProbeVolume * pv = &pvs[i];
945 if (point_in_obb(&pv->obb, pos))
946 {
947 return i;
948 }
949 }
950 return -1;
951}
952
953static void remove_audio_pv(APV * apv, bool * apb)
954{
955 if (apv->audio_probes)
956 {
957 free(apv->audio_probes);
958 apv->audio_probes = NULL;
959 }
960 apv->pv_id = 0;
961 *apb = false;
962}
963
971
972static void load_audio_pv(APV * apv, ProbeVolume * pv)
973{
974
975 fseek(GLOBALS.scene_data.APV_FILE, pv->byte_offset, SEEK_SET);
976
977 apv->audio_probes = malloc(sizeof(AudioProbe) * pv->resolution.x * pv->resolution.y * pv->resolution.z);
978
979 for (int i = 0; i < pv->resolution.z * pv->resolution.y * pv->resolution.x; i++)
980 {
981
982 AudioProbe * p = &apv->audio_probes[i];
983
984 fread(p, sizeof(AudioProbe), 1, GLOBALS.scene_data.APV_FILE);
985 if (p->ir_length <= 0)
986 {
987 p->final_ir = NULL;
988 continue;
989 }
990 p->final_ir = malloc(sizeof(float) * SH_MAX * p->ir_length);
991 fread(p->final_ir, sizeof(float) * SH_MAX, p->ir_length, GLOBALS.scene_data.APV_FILE);
992
993 }
994
995
996}
997
999{
1000
1001 if (GLOBALS.scene_data.APV_FILE == NULL) return;
1002
1003 int current = query_probe_volume(player.skeleton.neck.pos);
1004 if (current == -1) return;
1005
1006 bool loaded = false;
1007 for (int i = 0; i < MAX_AUDIO_PROBE_VOLUMES; i++)
1008 {
1010 {
1011 loaded = true;
1012 break;
1013 }
1014 }
1015
1016 if (loaded)
1017 {
1018 return;
1019 }
1020
1021 int empty = -1;
1023 float farthest_dist = -INFINITY;
1024 int farthest_index = -1;
1025 for (int i = 0; i < MAX_AUDIO_PROBE_VOLUMES; i++)
1026 {
1028 {
1029 empty = i;
1030 break;
1031 }
1032 else
1033 {
1034
1036 vec3 diff = vec3_sub(player.skeleton.neck.pos, center);
1037 float dist = vec3_length(diff);
1038 if (dist > farthest_dist)
1039 {
1040 farthest_index = i;
1041 farthest_dist = dist;
1042 }
1043 }
1044 }
1045 if (empty == -1)
1046 {
1047 empty = farthest_index;
1048 }
1049
1050 SDL_LockMutex(AUDIO_SCRATCH.aprobe_mutex);
1051 APV * apv = &GLOBALS.scene_data.LOADED_AUDIO_PV[empty];
1052
1054
1055 ProbeVolume * pv = &pvs[current];
1056
1057 load_audio_pv(apv, pv);
1058
1059
1060 apv->pv_id = current;
1061
1063
1064 SDL_UnlockMutex(AUDIO_SCRATCH.aprobe_mutex);
1065
1066}
1067
1068
1069
1070/*
1071 * @todo Clean up the probe_world_position replacement
1072 */
1073// static vec3 probe_world_position(ProbeVolume * pv, int x, int y, int z)
1074// {
1075
1076// float fx = (x / (float)(pv->resolution.x - 1)) - 0.5f;
1077// float fy = (y / (float)(pv->resolution.y - 1)) - 0.5f;
1078// float fz = (z / (float)(pv->resolution.z - 1)) - 0.5f;
1079
1080// vec3 local_pos;
1081// local_pos.x = fx * (pv->obb.extents.x * 2.0f);
1082// local_pos.y = fy * (pv->obb.extents.y * 2.0f);
1083// local_pos.z = fz * (pv->obb.extents.z * 2.0f);
1084
1085// vec3 fwd; vec3 right; vec3 up;
1086// OBB_vectors(pv->obb.rot, &fwd, &right, &up);
1087
1088// vec3 world_pos = vec3_add(pv->obb.center,
1089// vec3_add(vec3_scale(fwd, local_pos.x),
1090// vec3_add(vec3_scale(right, local_pos.y),
1091// vec3_scale(up, local_pos.z))));
1092
1093// return world_pos;
1094
1095// }
1096
1097
1099{
1100
1102 weights.count = 0;
1103 weights.APV_id = -1;
1104 float weight_sum = 0.0f;
1105 int pv_idx = query_probe_volume(pos);
1106 if (pv_idx == -1)
1107 {
1108 *out_w = weights;
1109 return;
1110 }
1111 int apv_idx = -1;
1112 for (int i = 0; i < MAX_AUDIO_PROBE_VOLUMES; i++)
1113 {
1114 if (GLOBALS.scene_data.LOADED_AUDIO_PV_FLAG[i] == 0) continue;
1115 if (GLOBALS.scene_data.LOADED_AUDIO_PV[i].pv_id == pv_idx)
1116 {
1117 apv_idx = i;
1118 break;
1119 }
1120 }
1121 if (apv_idx == -1)
1122 {
1123 *out_w = weights;
1124 return;
1125 }
1126 weights.APV_id = apv_idx;
1127
1129 ivec3 local;
1130 int index = query_audio_probe(&pvs[pv_idx], pos, &local);
1131 DEBUG_CURRENT_APROBE = index;
1132
1133 ProbeVolume * pv = &pvs[pv_idx];
1134
1135 for (int z = -1; z <= 1; z++)
1136 for (int y = -1; y <= 1; y++)
1137 for (int x = -1; x <= 1; x++)
1138 {
1139
1140 int idx_x = local.x + x;
1141 int idx_y = local.y + y;
1142 int idx_z = local.z + z;
1143 if (idx_x < 0 || idx_x >= pv->resolution.x) continue;
1144 if (idx_y < 0 || idx_y >= pv->resolution.y) continue;
1145 if (idx_z < 0 || idx_z >= pv->resolution.z) continue;
1146 int current_index = index + z * pv->resolution.y * pv->resolution.x + y * pv->resolution.x + x;
1147 ivec3 probe_local = {local.x + x, local.y + y, local.z + z};
1148 vec3 probe_world = GLOBALS.scene_data.LOADED_AUDIO_PV[apv_idx].audio_probes[current_index].pos;
1149
1150 vec3 d = vec3_sub(probe_world, pos);
1151 float dist = vec3_length(d);
1152 vec3 dir = vec3_normalize(d);
1153
1154 RayHit hit = raycast_scene(pos, dir, INFINITY, false);
1155
1156 if (hit.hit && hit.t < dist)
1157 {
1158 continue;
1159 }
1160
1161 float w = 1.0f / powf(dist + 0.05f, 2.0f);
1162
1163 if (weights.count < MAX_INTERP_APROBES)
1164 {
1165 weights.probes[weights.count] = current_index;
1166 weights.weights[weights.count] = w;
1167
1168 weights.count++;
1169 weight_sum += w;
1170
1171 }
1172 else
1173 {
1174 int lowest = 0;
1175 float lowest_w = INFINITY;
1176 for (int i = 0; i < MAX_INTERP_APROBES; i++)
1177 {
1178
1179 if (weights.weights[i] < lowest_w)
1180 {
1181 lowest_w = weights.weights[i];
1182 lowest = i;
1183 }
1184
1185 }
1186
1187 if (lowest_w < w)
1188 {
1189 weights.probes[lowest] = current_index;
1190 weight_sum -= weights.weights[lowest];
1191 weights.weights[lowest] = w;
1192 weight_sum += w;
1193
1194 }
1195 }
1196 }
1197
1198 float inv_w = 1.0f / weight_sum;
1199 for (int i = 0; i < MAX_INTERP_APROBES; i++)
1200 {
1201 weights.weights[i] *= inv_w;
1202 }
1203
1204 if (weights.count == 0)
1205 {
1206 weights.probes[0] = index;
1207 weights.weights[0] = 1;
1208 weights.count = 1;
1209 }
1210
1211
1212 memcpy(out_w, &weights, sizeof(AudioProbeWeights));
1213
1214}
1216{
1217
1218 ivec3 local;
1221 SDL_LockMutex(HRTF_TD.mutex);
1223 {
1224 HRTF_TD.new_probe_ready = true;
1225 memcpy(&HRTF_TD.w, &w, sizeof(AudioProbeWeights));
1226 SDL_CondSignal(HRTF_TD.work_cond);
1227 }
1228 SDL_UnlockMutex(HRTF_TD.mutex);
1229}
1230
1231
1232static float process_biquad(Biquad* filter, float in_sample)
1233{
1234 float out_sample = (filter->b0 * in_sample
1235 + filter->b1 * filter->x1
1236 + filter->b2 * filter->x2
1237 - filter->a1 * filter->y1
1238 - filter->a2 * filter->y2) /* / filter->a0 */;
1239
1240 filter->x2 = filter->x1;
1241 filter->x1 = in_sample;
1242 filter->y2 = filter->y1;
1243 filter->y1 = out_sample;
1244
1245 return out_sample;
1246}
1247
1248
1249static void init_bandpass(Biquad * filter, float sample_rate, float center_freq, float q)
1250{
1251
1252 memset(filter, 0, sizeof(Biquad));
1253 float w0 = 2.0f * M_PI * center_freq / sample_rate;
1254 float alpha = sinf(w0) / (2.0f * q);
1255
1256 float a0 = 1.0f + alpha;
1257 filter->a0 = a0;
1258 filter->b0 = alpha / a0;
1259 filter->b1 = 0.0f;
1260 filter->b2 = -alpha / a0;
1261 filter->a1 = -2.0f * cosf(w0) / a0;
1262 filter->a2 = (1.0f - alpha) / a0;
1263
1264}
1265
1266static void generate_synthetic_IR_tail(float* out_tail, float fade_start, AudioProbe * probe, float rms)
1267{
1268
1269 Biquad filter[AUDIO_NUM_BANDS] = {0};
1270 float decay[AUDIO_NUM_BANDS] = {0};
1271 float envelope[AUDIO_NUM_BANDS] = {0};
1272
1273
1274 const float RT60_DECAY_CONST = -6.907755278982137f;
1275 for (int b = 0; b < AUDIO_NUM_BANDS; b++)
1276 {
1277
1278 init_bandpass(&filter[b], SAMPLE_RATE, AUDIO_BAND_FREQ[b], 0.707f);
1279
1280 float rt60_samples = MAX(0.01f, probe->rt60[b] * IR_SAMPLE_RATE);
1281
1282
1283 decay[b] = expf(RT60_DECAY_CONST / rt60_samples);
1284
1285 envelope[b] = rms / sqrtf(AUDIO_NUM_BANDS);
1286
1287 }
1288
1289 float unit_noise_scaler = 1.0f / 0.577350269f;
1290
1291 for (int i = fade_start; i < MAX_IR_SAMPLES; i++) {
1292
1293 float raw = ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
1294 raw *= unit_noise_scaler;
1295 float combined = 0.0f;
1296 for (int b = 0; b < AUDIO_NUM_BANDS; b++)
1297 {
1298
1299 float diff = process_biquad(&filter[b], raw * envelope[b]);
1300 combined += diff;
1301 envelope[b] *= decay[b];
1302 }
1303
1304 out_tail[i] = combined;
1305 }
1306}
1307
1308
1309
1310
1311// dont need bins since we have no bins so we can just do 1 * sample
1312static void compute_schroeder_edc(float* energy_histogram, float* edc_out) {
1313
1314 float accumulated_energy = 0.0f;
1315
1316 for (int i = MAX_EDC_SAMPLES - 1; i >= 0; i--) {
1317 accumulated_energy += energy_histogram[i];
1318 edc_out[i] = accumulated_energy;
1319 }
1320
1321 float max_energy = edc_out[0];
1322
1323 if (max_energy <= 0.0f) max_energy = 1e-12f;
1324
1325 for (int i = 0; i < MAX_EDC_SAMPLES; i++) {
1326
1327 if (edc_out[i] > 0.0f) {
1328
1329 edc_out[i] = 10.0f * log10f(edc_out[i] / max_energy);
1330
1331 } else {
1332
1333 edc_out[i] = -120.0f;
1334
1335 }
1336
1337 }
1338
1339}
1340
1341
1342static float extract_rms(AudioProbe * probe, int fade_start, int window_size)
1343{
1344
1345 int start = fade_start - window_size;
1346 float sum = 0.0f;
1347 int count = 0;
1348 for (int i = start; i < fade_start; i++)
1349 {
1350
1351 sum += probe->final_ir[i][0] * probe->final_ir[i][0];
1352
1353 count++;
1354 }
1355
1356 return (sqrtf(sum / count));
1357
1358}
1359
1360
1361
1362
1363static void extract_t60(float * edc_db, AudioProbe * probe, int band, float bin_duration_seconds, int last_sample) {
1364
1365 int start_idx = -1;
1366 int end_idx = -1;
1367
1368 for (int i = 0; i < last_sample; i++) {
1369 if (start_idx == -1 && edc_db[i] <= -5.0f) {
1370 start_idx = i;
1371 }
1372 if (end_idx == -1 && edc_db[i] <= -35.0f) {
1373 end_idx = i;
1374 break;
1375 }
1376 }
1377
1378 if (start_idx == -1 || end_idx == -1) {
1379 probe->rt60[band] = 0.0f;
1380 probe->slope[band] = 0.0f;
1381 return;
1382 }
1383
1384 float sum_x = 0.0f, sum_y = 0.0f, sum_xy = 0.0f, sum_xx = 0.0f;
1385 for (int i = start_idx; i <= end_idx; i++) {
1386 float x = i * bin_duration_seconds;
1387 float y = edc_db[i];
1388
1389 sum_x += x;
1390 sum_y += y;
1391 sum_xy += x * y;
1392 sum_xx += x * x;
1393 }
1394
1395 int n = end_idx - start_idx + 1;
1396 float denominator = (n * sum_xx) - (sum_x * sum_x);
1397 float slope = 0.0f;
1398 float t60 = 0.0f;
1399
1400 if (denominator != 0.0f) {
1401 slope = ((n * sum_xy) - (sum_x * sum_y)) / denominator;
1402
1403 t60 = -60.0f / slope;
1404 }
1405 probe->rt60[band] = t60;
1406 probe->slope[band] = slope;
1407
1408}
1409
1410
1411static void process_probe_chunk(int start, int count)
1412{
1413
1415 int BOUNCE_SSBO_SIZE = count * MAX_SAMPLES_PER_PROBE;
1416
1417 AudioSampleGPU * samples_init_data = calloc(BOUNCE_SSBO_SIZE, sizeof(AudioSampleGPU));
1418 GLuint bounce_ssbo;
1419 glGenBuffers(1, &bounce_ssbo);
1420 glBindBuffer(GL_SHADER_STORAGE_BUFFER, bounce_ssbo);
1421 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(AudioSampleGPU) * BOUNCE_SSBO_SIZE, samples_init_data, GL_MAP_READ_BIT);
1422 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, bounce_ssbo);
1423 free(samples_init_data);
1424
1425
1426 glUseProgram(AUDIO_PROBE_BAKE_SHADER);
1427 const int AUDIO_BATCHES = 256;
1428
1429 Timer gpu_timer;
1430 start_timer(&gpu_timer);
1431 for (int i = 0; i < MAX(EDITOR_SETTINGS.audio_probe_iterations / AUDIO_BATCHES, 1); i++)
1432 {
1433
1434 glUniform1ui(glGetUniformLocation(AUDIO_PROBE_BAKE_SHADER, "uProbeSize"), count);
1435 glUniform1ui(glGetUniformLocation(AUDIO_PROBE_BAKE_SHADER, "uProbeOffset"), start);
1436 glUniform1ui(glGetUniformLocation(AUDIO_PROBE_BAKE_SHADER, "uRayOffset"), i * AUDIO_BATCHES);
1437
1438 glUniform1ui(glGetUniformLocation(AUDIO_PROBE_BAKE_SHADER, "uIterations"), EDITOR_SETTINGS.audio_probe_iterations);
1439
1440 glUniform1ui(glGetUniformLocation(AUDIO_PROBE_BAKE_SHADER, "uBounces"), EDITOR_SETTINGS.audio_probe_bounces);
1441 glUniform1ui(glGetUniformLocation(AUDIO_PROBE_BAKE_SHADER, "uMaxSamplesPerProbe"), MAX_SAMPLES_PER_PROBE);
1442 glUniform1ui(glGetUniformLocation(AUDIO_PROBE_BAKE_SHADER, "uBatchSize"), MIN(AUDIO_BATCHES, EDITOR_SETTINGS.audio_probe_iterations));
1443
1444 glDispatchCompute((MIN(AUDIO_BATCHES, EDITOR_SETTINGS.audio_probe_iterations) + 15) / 16, (count + 15) / 16, 1);
1445 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
1446 glFinish();
1447
1448 }
1449
1450 glFinish();
1451 double gpu_time = end_timer(&gpu_timer);
1452
1453 Timer cpu_timer;
1454
1455 start_timer(&cpu_timer);
1456
1457
1458
1459 glBindBuffer(GL_SHADER_STORAGE_BUFFER, bounce_ssbo);
1460 AudioSampleGPU * asample_gpu = glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
1461 if (!asample_gpu)
1462 {
1463 printf("NO SAMPLES\n");
1464 }
1465
1466
1467
1468 float norm = 4.0f * M_PI / ((float)EDITOR_SETTINGS.audio_probe_iterations);
1469
1470 float * edc_in[6];
1471 float * edc_out[6];
1472
1473 for (int b = 0; b < AUDIO_NUM_BANDS; b++)
1474 {
1475
1476 edc_in[b] = malloc(MAX_EDC_SAMPLES * sizeof(float));
1477 edc_out[b] = malloc(MAX_EDC_SAMPLES * sizeof(float));
1478
1479 }
1480 float * late_reverb = malloc(MAX_IR_SAMPLES * sizeof(float));
1481
1483
1484 float * samples = malloc(MAX_IR_SAMPLES * SH_MAX * AUDIO_NUM_BANDS * sizeof(float));
1485 for (int i = 0; i < count; i++)
1486 {
1487 AudioProbe * aprobe = &GLOBALS.audio_probes[start + i];
1488
1489 memset(samples, 0, sizeof(float) * MAX_IR_SAMPLES * SH_MAX * AUDIO_NUM_BANDS);
1490
1491 for (int b = 0; b < AUDIO_NUM_BANDS; b++)
1492 {
1493
1494 memset(edc_in[b], 0, sizeof(float) * MAX_EDC_SAMPLES);
1495 memset(edc_out[b], 0, sizeof(float) * MAX_EDC_SAMPLES);
1496
1497 }
1498
1499 memset(late_reverb, 0, sizeof(float) * MAX_IR_SAMPLES);
1500
1501 int oob = 0;
1502 const float MAX_IR_TIME_SECONDS = (float)MAX_SAMPLES / (float)SAMPLE_RATE;
1503
1504 int last_sample[AUDIO_NUM_BANDS] = {0};
1505 for (int j = 0; j < EDITOR_SETTINGS.audio_probe_iterations; j++)
1506 {
1507 for (int k = 0; k < EDITOR_SETTINGS.audio_probe_bounces; k++)
1508 {
1509 int index = i * MAX_SAMPLES_PER_PROBE + j * EDITOR_SETTINGS.audio_probe_bounces + k;
1510 AudioSampleGPU * sample = &asample_gpu[index];
1511
1512 if (!sample->valid) continue;
1513 float sample_index = sample->time_of_flight * IR_SAMPLE_RATE;
1514 int i0 = (int)floorf(sample_index);
1515 float frac = sample_index - (float)i0;
1516
1517 if (i0 >= MAX_IR_SAMPLES - 1 || i0 < 0) continue;
1518
1519
1520 float SH[SH_MAX];
1521 vec3 tr_dir = {sample->arrival_dir.y, sample->arrival_dir.x, -sample->arrival_dir.z};
1522 evaluate_sh(tr_dir, SH);
1523
1524 for (int sh = 0; sh < SH_MAX; sh++)
1525 {
1526
1527 for (int band = 0; band < AUDIO_NUM_BANDS; band++)
1528 {
1529 samples[SAMPLE_IDX(i0, sh, band)] += SH[sh] * sample->energy[band] * norm;
1530 }
1531 }
1532
1533 int edc_sample_index = sample->time_of_flight * EDC_SAMPLE_RATE;
1534 if (edc_sample_index < 0 || edc_sample_index >= MAX_EDC_SAMPLES) continue;
1535 for (int band = 0; band < AUDIO_NUM_BANDS; band++)
1536 {
1537 edc_in[band][edc_sample_index] += sample->abs_energy[band] * norm;
1538 if (edc_sample_index > last_sample[band])
1539 {
1540 last_sample[band] = edc_sample_index;
1541 }
1542 }
1543 }
1544
1545 }
1546
1547 for (int b = 0; b < AUDIO_NUM_BANDS; b++)
1548 {
1549 compute_schroeder_edc(edc_in[b], edc_out[b]);
1550 extract_t60(edc_out[b], aprobe, b, 1.0f/MAX_SAMPLES, last_sample[b]);
1551 }
1552
1553
1554 int last = MAX_SAMPLES;
1555 aprobe->ir_length = 0;
1556 if (last <= 0) continue;
1557 aprobe->final_ir = malloc(last * sizeof(float) * SH_MAX);
1558 if (!aprobe->final_ir) break;
1559 memset(aprobe->final_ir, 0, sizeof(float) * SH_MAX * last);
1560 aprobe->ir_length = last;
1561
1562
1563 int fade_start = 800;
1564 int fade_end = 1200;
1565
1566
1567 int samples_per_bin = MAX_SAMPLES / AUDIO_PROBE_BINS;
1568 float seconds_per_bin = (float)AUDIO_PROBE_BINS / SAMPLE_RATE;
1569
1570 for (int k = 0; k < last; k++)
1571 {
1572 int bin_center_sample = k;
1573 int sign = 1;
1574 if (rand() % 2 == 0) sign = -1;
1575
1576 for (int b = 0; b < AUDIO_NUM_BANDS; b++)
1577 {
1578 float sh0_energy = samples[SAMPLE_IDX(k, 0, b)];
1579 if (sh0_energy <= 0.000001f) continue;
1580
1581 float total_energy = sh0_energy / Y_00;
1582
1583 float band_amplitude = sqrtf(total_energy) * sign;
1584
1585
1586 float sh_ratios[SH_MAX];
1587 for (int sh = 0; sh < SH_MAX; sh++)
1588 {
1589 sh_ratios[sh] = samples[SAMPLE_IDX(k, sh, b)] / sh0_energy;
1590 }
1591
1592 for (int m = 0; m < AUDIO_KERNEL_SIZE; m++)
1593 {
1594 int target_sample = k + m - AUDIO_KERNEL_CENTER;
1595 if (target_sample < 0) continue;
1596 if (target_sample >= last) break;
1597
1598 float filtered_sample = BAND_IMPULSES[b][m] * band_amplitude;
1599
1600 for (int sh = 0; sh < SH_MAX; sh++)
1601 {
1602 aprobe->final_ir[target_sample][sh] += filtered_sample * sh_ratios[sh];
1603 }
1604 }
1605 }
1606 }
1607
1608 float rms = extract_rms(aprobe, fade_start, 200);
1609
1610 generate_synthetic_IR_tail(late_reverb, fade_start, aprobe, rms);
1611
1612 for (int i = fade_start; i < last; i++)
1613 {
1614 float alpha = 1.0f;
1615 if (i < fade_end)
1616 {
1617 alpha = (float)(i - fade_start) / (fade_end - fade_start);
1618 }
1619
1620
1621 float er = cosf(alpha * 0.5 * M_PI);
1622 float lr = sinf(alpha * 0.5 * M_PI);
1623
1624 aprobe->final_ir[i][0] = (aprobe->final_ir[i][0] * er) + (late_reverb[i] * lr);
1625
1626 for (int sh = 1; sh < SH_MAX; sh++)
1627 {
1628
1629 float sh_decorrelated_noise = (((float)rand() / (float)RAND_MAX) * 2.0f - 1.0f) * 0.2f;
1630 float late_sh = late_reverb[i] * sh_decorrelated_noise;
1631
1632
1633 aprobe->final_ir[i][sh] = (aprobe->final_ir[i][sh] * er) + (late_sh * lr);
1634
1635
1636 }
1637
1638 }
1639 }
1640
1642 for (int b = 0; b < AUDIO_NUM_BANDS; b++)
1643 {
1644
1645 free(edc_in[b]);
1646 free(edc_out[b]);
1647
1648 }
1649 free(late_reverb);
1650 free(samples);
1651
1652
1653 glDeleteBuffers(1, &bounce_ssbo);
1654 double cpu_time = end_timer(&cpu_timer);
1655
1656 printf("CPU: %f GPU: %f\n", cpu_time, gpu_time);
1657
1658}
1659
1660
1662{
1663
1664 if (GLOBALS.scene_data.APV_FILE == NULL)
1665 {
1666 printf("NO APV FILE OPEN TO BAKE INTO\n");
1667 return;
1668 }
1669
1670 size_t byte_size = sizeof(AudioProbe) * pv->resolution.x * pv->resolution.y * pv->resolution.z;
1671 if (byte_size > pv->size && pv->size > 0)
1672 {
1673 printf("PV SIZE GREW, SKIPPING WRITE\n");
1674 return;
1675 }
1676
1677 SDL_LockMutex(AUDIO_SCRATCH.aprobe_mutex);
1678
1680 GLOBALS.audio_probes = calloc(pv->resolution.x * pv->resolution.y * pv->resolution.z, sizeof(AudioProbe));
1681 if (!GLOBALS.audio_probes)
1682 {
1683
1684 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, GLOBAL_INSTANCE_SSBO);
1685 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, INSTANCE_SSBO);
1686 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 8, TEXT_BOX_SSBO);
1687 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, RPROBE_SSBO);
1688 SDL_UnlockMutex(AUDIO_SCRATCH.aprobe_mutex);
1689 return;
1690 }
1691
1692 int APROBE_COUNT = pv->resolution.x * pv->resolution.y * pv->resolution.z;
1693 AudioProbeGPU * aprobe_init_data = calloc(APROBE_COUNT, sizeof(AudioProbeGPU));
1694
1695
1696 OBB obb = pv->obb;
1697
1698 vec3 step_x, step_y, step_z;
1699 PV_step_size(pv, &step_x, &step_y, &step_z);
1700
1701 vec3 pos = OBB_min_corner(&obb);
1702
1703 vec3 current_pos = pos;
1704
1705 for (int z = 0; z < pv->resolution.z; z++)
1706 {
1707 for (int y = 0; y < pv->resolution.y; y++)
1708 {
1709 for (int x = 0; x < pv->resolution.x; x++)
1710 {
1711 int index = z * pv->resolution.y * pv->resolution.x + y * pv->resolution.x + x;
1712 current_pos.x = pos.x + x * step_x.x + y * step_y.x + z * step_z.x;
1713 current_pos.y = pos.y + x * step_x.y + y * step_y.y + z * step_z.y;
1714 current_pos.z = pos.z + x * step_x.z + y * step_y.z + z * step_z.z;
1715 GLOBALS.audio_probes[index].pos = current_pos;
1716 aprobe_init_data[index].pos = GLOBALS.audio_probes[index].pos;
1717 }
1718 }
1719 }
1720
1722
1723 GLuint aprobe_ssbo;
1724 glGenBuffers(1, &aprobe_ssbo);
1725 glBindBuffer(GL_SHADER_STORAGE_BUFFER, aprobe_ssbo);
1726 glBufferStorage(GL_SHADER_STORAGE_BUFFER, sizeof(AudioProbeGPU) * APROBE_COUNT, aprobe_init_data, GL_MAP_READ_BIT);
1727 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, aprobe_ssbo);
1728 free(aprobe_init_data);
1729
1731 int BOUNCE_SSBO_SIZE = APROBE_COUNT * MAX_SAMPLES_PER_PROBE;
1732 size_t BOUNCE_SSBO_BYTE_SIZE = BOUNCE_SSBO_SIZE * sizeof(AudioSampleGPU);
1733 const size_t SAMPLE_SSBO_CHUNK_SIZE = 400000000;
1734 int chunk_count = MAX(BOUNCE_SSBO_BYTE_SIZE / SAMPLE_SSBO_CHUNK_SIZE, 1);
1735 int probes_per_chunk = APROBE_COUNT / chunk_count;
1736 for (int i = 0; i < chunk_count; i++)
1737 {
1738 size_t current_byte_size = SAMPLE_SSBO_CHUNK_SIZE;
1739 if (i == chunk_count - 1)
1740 {
1741 current_byte_size = BOUNCE_SSBO_BYTE_SIZE - chunk_count * SAMPLE_SSBO_CHUNK_SIZE;
1742 }
1743 int start = i * probes_per_chunk;
1744 process_probe_chunk(start, probes_per_chunk);
1745
1746 }
1747
1748
1749 glDeleteBuffers(1, &aprobe_ssbo);
1750
1752
1753 size_t byte_offset = 0;
1754 if (pv->size > 0)
1755 {
1756 byte_offset = pv->byte_offset;
1757 fseek(GLOBALS.scene_data.APV_FILE, byte_offset, SEEK_SET);
1758 }
1759 else
1760 {
1761 fseek(GLOBALS.scene_data.APV_FILE, 0, SEEK_END);
1762 byte_offset = ftell(GLOBALS.scene_data.APV_FILE);
1763 }
1764
1765 for (int i = 0; i < pv->resolution.z * pv->resolution.y * pv->resolution.x; i++)
1766 {
1767
1769 fwrite(p, sizeof(AudioProbe), 1, GLOBALS.scene_data.APV_FILE);
1770 fwrite(p->final_ir, sizeof(float) * SH_MAX, p->ir_length, GLOBALS.scene_data.APV_FILE);
1771
1772 }
1773
1774 pv->size = ftell(GLOBALS.scene_data.APV_FILE) - byte_offset;
1775 pv->byte_offset = byte_offset;
1776
1777 for (int i = 0; i < pv->resolution.z * pv->resolution.y * pv->resolution.x; i++)
1778 {
1780 }
1781 free(GLOBALS.audio_probes);
1782 GLOBALS.audio_probes = NULL;
1783
1784 SDL_UnlockMutex(AUDIO_SCRATCH.aprobe_mutex);
1785
1786}
1787
1788
1789
1790
1792{
1793
1795 for (int i = 0; i < MAX_AUDIO_EMITTERS; i++) {
1796 AudioEmitter* emitter = &AUDIO_EMITTERS[i];
1797 if (emitter->free) continue;
1798 if (!ma_sound_is_playing(&emitter->sound_obj)) continue;
1799
1800
1801 if (!emitter->is_occludable)
1802 {
1803 emitter->virtual_pos = emitter->pos;
1804 emitter->current_volume = emitter->base_volume;
1805 continue;
1806 }
1807
1808 vec3 diff = vec3_sub(emitter->pos, player->skeleton.neck.pos);
1809
1810 float len = vec3_length(diff);
1811
1812 vec3 dir = vec3_normalize(diff);
1813
1814 vec3 inv_dir = (vec3){1.0f/dir.x, 1.0f/dir.y, 1.0f/dir.z};
1815 RayHit hit = raycast_scene(player->skeleton.neck.pos, dir, INFINITY, false);
1816 if (hit.hit && hit.t < len)
1817 {
1818 vec3 vpos;
1819 float vol = 0;
1820 float dist = len;
1821 bool res = pathfind_audio_occlusion(player->skeleton.neck.pos, closest_l, emitter->pos, &vpos, &vol, &dist);
1822 if (res)
1823 {
1824
1825 emitter->virtual_pos = vpos;
1826 emitter->current_volume = vol;
1827 emitter->current_distance = dist;
1828 }
1829 else
1830 {
1831 emitter->virtual_pos = emitter->pos;
1832 emitter->current_volume = 0.0f;
1833 emitter->current_distance = dist;
1834 }
1835
1836 }
1837 else
1838 {
1839 emitter->virtual_pos = emitter->pos;
1840 emitter->current_volume = calculate_path_volume_equal_power(len, 0.2f, 20.0f);
1841 emitter->current_distance = len;
1842 }
1843 }
1844
1845
1846}
1847
1848
1849static void init_normalized_lpf(float cutoff_hz, float sample_rate, float* out_kernel)
1850{
1851 float sum = 0.0f;
1852
1853 for (int n = 0; n < AUDIO_KERNEL_SIZE; n++)
1854 {
1855 int k = n - AUDIO_KERNEL_CENTER;
1856
1857 float w = 0.54f - 0.46f * cosf((2.0f * M_PI * (float)n) / (float)(AUDIO_KERNEL_SIZE - 1));
1858
1859 if (k == 0)
1860 {
1861 out_kernel[n] = (2.0f * cutoff_hz / sample_rate) * w;
1862 }
1863 else
1864 {
1865 float x = 2.0f * M_PI * cutoff_hz * (float)k / sample_rate;
1866 out_kernel[n] = (sinf(x) / (M_PI * (float)k)) * w;
1867 }
1868
1869 sum += out_kernel[n];
1870 }
1871
1872 if (sum > 0.0f)
1873 {
1874 for (int n = 0; n < AUDIO_KERNEL_SIZE; n++)
1875 {
1876 out_kernel[n] /= sum;
1877 }
1878 }
1879}
1880
1881void init_band_kernels(float sample_rate, float f1_hz, float f2_hz, float f3_hz, float f4_hz, float f5_hz, float out_kernels[AUDIO_NUM_BANDS][AUDIO_KERNEL_SIZE])
1882{
1883 float lpf1[AUDIO_KERNEL_SIZE];
1884 float lpf2[AUDIO_KERNEL_SIZE];
1885 float lpf3[AUDIO_KERNEL_SIZE];
1886 float lpf4[AUDIO_KERNEL_SIZE];
1887 float lpf5[AUDIO_KERNEL_SIZE];
1888
1889 init_normalized_lpf(f1_hz, sample_rate, lpf1);
1890 init_normalized_lpf(f2_hz, sample_rate, lpf2);
1891 init_normalized_lpf(f3_hz, sample_rate, lpf3);
1892 init_normalized_lpf(f4_hz, sample_rate, lpf4);
1893 init_normalized_lpf(f5_hz, sample_rate, lpf5);
1894
1895 for (int n = 0; n < AUDIO_KERNEL_SIZE; n++)
1896 {
1897 out_kernels[0][n] = lpf1[n];
1898
1899 out_kernels[1][n] = lpf2[n] - lpf1[n];
1900
1901 out_kernels[2][n] = lpf3[n] - lpf2[n];
1902
1903 out_kernels[3][n] = lpf4[n] - lpf3[n];
1904 out_kernels[4][n] = lpf5[n] - lpf4[n];
1905
1906 float delta = (n == AUDIO_KERNEL_CENTER) ? 1.0f : 0.0f;
1907 out_kernels[5][n] = delta - lpf5[n];
1908 }
1909}
RayHit raycast_scene(vec3 start, vec3 dir, float initial_t, bool check_normal)
Raycasts through the scene and finds closest intersection to meshes.
Acceleration structure generation and how we traverse them.
void init_normalized_lpf(float cutoff_hz, float sample_rate, float *out_kernel)
Definition audio.c:1849
void init_band_kernels(float sample_rate, float f1_hz, float f2_hz, float f3_hz, float f4_hz, float f5_hz, float out_kernels[6][31])
Definition audio.c:1881
float BAND_IMPULSES[6][31]
Definition audio.c:28
#define SAMPLE_IDX(i, sh, b)
Definition audio.c:412
#define Y_00
Definition audio.c:415
Audio probe baking, portal creation, probe volumes, and emitter functions.
Audio IR processing, hrtf loading and parsing, and audio threading.
#define SAMPLE_RATE
#define MAX_SAMPLES
RayHit hit
float rand()
const float weights[5]
Functions for camera.
uniform uint n
Engine specific types and defines.
Spring neck
Neck Spring (target based on pelvis).
FILE * APV_FILE
FILE pointer to the opened binary file.
#define MAX_PORTALS
The maximum amount of portals that are allocated to AStarWorkspace.path.
SceneData scene_data
SceneData that is more-so scene specific.
int i
The index into the array of whatever is being traversed.
#define MAX_AUDIO_PROBE_VOLUMES
int came_from
The index into the array of traversed items that this node came from.
int run_id
Makes sure that no old stack items are used.
Skeleton skeleton
Player Skeleton.
float g
The g score: current score of the path.
bool LOADED_AUDIO_PV_FLAG[4]
Flags that indicate if the slot is loaded.
Vector audio_portals
AudioPortals.
AudioProbe * audio_probes
A scratch buffer used when baking AudioProbes.
int parent
The index of the parent of this neighbor.
Globals GLOBALS
Engine Globals.
Definition main.c:310
vec3 forward
APV LOADED_AUDIO_PV[4]
Load slots for Audio ProbeVolumes.
int path_count
The final path count.
Vector audio_probe_volumes
All of the AudioProbe% volumes, regardless of if they have been loaded yet.
float g
The g score: current score of the path.
Vector min_heap
OpenNode - the heap of nodes that are pushed and popped until there are none left or it reaches the e...
int current_run
The current run indicates what AStarStateStack entries we can use.
int * path
The path indices that are returned.
vec3 pos
Position of the spring.
float f
The f score: score from neighbor to end.
Camera * cam
Player Camera.
AStarStateStack * stack
The stack that tracks where each node connects so that it can be backtracked.
float f
The f score: score from neighbor to end.
The workspace for conducting a-star pathfinding/.
Biquad filter.
A node inside of the min_heap of an AStarWorkspace.
The Player.
double end_timer(Timer *t)
void start_timer(Timer *t)
void PV_step_size(ProbeVolume *pv, vec3 *step_x, vec3 *step_y, vec3 *step_z)
float abs_energy[6]
The energy per band with only absorption and no scattering.
float time_of_flight
The time that it takes for the sample to reach this position in total dist + dist to probe.
vec3 last_pos
The last job's position.
AudioProbe * audio_probes
The probes in the volume, loaded in from binary.
bool new_probe_ready
If a new job is ready.
float(* final_ir)[9]
The baked IR samples by 9 SH.
vec3 arrival_dir
The direction the ray was traveling to this position.
float rt60[6]
The RT60 from schroeder integration.
ivec3 resolution
The resolution in X, Y, Z.
size_t size
The byte size of this volume.
size_t byte_offset
The byte offset into the binary file storing this volume.
float energy[6]
The energy per band.
SDL_mutex * mutex
The mutex used when signalling a job.
int pv_id
Which ProbeVolume in SceneData.audio_probe_volumes.
OBB obb
The OBB that defines the size.
float slope[6]
The slope from EDC.
uint32_t valid
If it is occluded then it is not valid.
size_t ir_length
The amount of IR samples for this probe.
SDL_mutex * aprobe_mutex
Used when loading new APVs to avoid concurrency with the HRTF thread.
vec3 last_fwd
The last job's direction.
vec3 pos
The world position of this probe.
AudioProbeWeights w
Current job's weights.
SDL_cond * work_cond
The job signal.
#define AUDIO_PROBE_BINS
The amount of bins (to reduce max sample size).
#define SH_MAX
#define MAX_IR_SAMPLES
Maximum IR sample length.
#define IR_SAMPLE_RATE
Sample rate for baked audio IRs.
int query_probe_volume(vec3 pos)
Checks which ProbeVolume a position is inside.
Definition audio.c:938
void reset_audio_pvs()
Reset audio pvs, frees each slot in the global audio pv slots.
Definition audio.c:964
void evaluate_sh(vec3 dir, float SH[SH_MAX])
Evaluates 9 spherical harmonics based on direction.
Definition audio_ir.c:51
vec3 pos_to_pv_local(OBB *obb, vec3 pos)
Converts a position to local OBB space.
Definition audio.c:920
void load_audio_pv(APV *apv, ProbeVolume *pv)
Loads an APV into a slot.
Definition audio.c:972
void init_bandpass(Biquad *filter, float sample_rate, float center_freq, float q)
Initializes a bandpass filter for the synthetic IR.
Definition audio.c:1249
void extract_t60(float *edc_db, AudioProbe *probe, int band, float bin_duration_seconds, int last_sample)
Extracts the RT60 from an EDC for computing a synthetic IR.
Definition audio.c:1363
#define MAX_INTERP_APROBES
Max AudioProbes that can be sampled at once.
int query_audio_probe(ProbeVolume *pv, vec3 pos, ivec3 *out_local)
Queries which audio probe is closest to given position.
Definition audio.c:929
void bake_audio_probe_volume(ProbeVolume *pv)
Bakes an audio probe volume.
Definition audio.c:1661
float extract_rms(AudioProbe *probe, int fade_start, int window_size)
Extracts the RMS from a certain sample range before the synthetic IR.
Definition audio.c:1342
#define AUDIO_KERNEL_SIZE
Frequency kernel size for distributing samples during audio bake.
void update_player_audio_probe()
Determines if a new IR needs to be signaled from the background thread.
Definition audio.c:1215
void SDLAudioCallback(void *pUserData, Uint8 *pStream, int len)
Passes frames to miniaudio in the audio thread.
Definition audio.c:908
#define MAX_EDC_SAMPLES
Maximum EDC sample length.
void generate_synthetic_IR_tail(float *out_tail, float fade_start, AudioProbe *probe, float rms)
Generates a synthetic IR tail for a given AudioProbe based on its RT60.
Definition audio.c:1266
void remove_audio_pv(APV *apv, bool *apb)
Frees an APV from its slot.
Definition audio.c:953
void check_audio_pv()
Finds the closest audio PVs to the player and loads the closest ones into open slots based on distanc...
Definition audio.c:998
void compute_schroeder_edc(float *energy_histogram, float *edc_out)
Generates an Energy Decay Curve for input IR samples.
Definition audio.c:1312
void interpolate_nearest_aprobes(vec3 pos, AudioProbeWeights *out_w)
Interpolates between 4 closest AudioProbes within one singular ProbeVolume and weighs them based on d...
Definition audio.c:1098
#define AUDIO_NUM_BANDS
The amount of bands to use for audio probe baking.
const float AUDIO_BAND_FREQ[]
Audio frequencies for each band for audio probe baking.
#define EDC_SAMPLE_RATE
EDC sample rate for schroeder integration.
void process_probe_chunk(int start, int count)
Path traces from AudioProbes within a certain chunk of a ProbeVolume and processes samples per AudioP...
Definition audio.c:1411
#define AUDIO_KERNEL_CENTER
Center of the kernel for distributing samples during audio bake.
float process_biquad(Biquad *filter, float in_sample)
Processes a biquad filter over a sample.
Definition audio.c:1232
A ProbeVolume for AudioProbes.
Baked IR samples loaded in from binary into APVs from ProbeVolumes.
Simply stores the positions for audio probes during the bake.
Weights used for interpolating between audio probes.
Each ray's samples when baking AudioProbes, padded for GPU.
The ProbeVolumes for APVs and LPVs, controlled by OBBs.
bool is_occludable
If the AudioEmitter can be occluded and needs to go through the AudioPortal system.
Vector adjacent_portals
int - Vector of AudioPortals that can be seen from this one.
float current_distance
The current distance from listener.
float current_cutoff
The current low pass cutoff.
bool is_loop
If the AudioEmitter loops.
float current_volume
The current volume.
vec3 pos
The position of the AudioEmitter .
vec3 virtual_pos
The position that we spatialize the emitter at (for example: at the corner instead of behind the wall...
char path[256]
The path to the sound.
float base_volume
The starting volume.
vec3 pos
The position of the AudioPortal.
ma_sound sound_obj
The miniaudio sound obj.
bool is_loaded
If the emitter is loaded in miniaudio.
bool free
Indicates if this AudioEmitter is empty or can be replaced.
void end_emitter(AudioEmitter *e)
Stops playing an emitter.
Definition audio.c:881
AudioEmitter * init_emitter(const char *filepath, bool is_loop, bool is_occludable, float base_volume)
Initialized an audio emitter and sets up its audio object in miniaudio.
Definition audio.c:814
int find_closest_visible_aportal(vec3 pos)
Finds the closest audio portal for a star to travel through.
Definition audio.c:475
void heap_swap(OpenNode *a, OpenNode *b)
Swaps A and B.
Definition audio.c:511
void play_emitter(AudioEmitter *emitter, vec3 pos)
Starts playing an emitter.
Definition audio.c:853
void audio_update(Player *player)
Spatializes the audio from the player's position.
Definition audio.c:886
float calculate_path_volume_equal_power(float path_distance, float min_distance, float max_distance)
Calculates volume over a path.
Definition audio.c:650
void heap_push(Vector *h)
After a node has been pushed to the top of the stack, we reorder by f score.
Definition audio.c:515
void reconstruct_path_aportal(AStarWorkspace *WS, int start, int end)
Traverses the AStarStateStack and backtracks to find the entire path.
Definition audio.c:545
OpenNode heap_pop(Vector *h)
Pops the bottommost root off of the OpenNode Vector and then places the topmost node onto the bottom ...
Definition audio.c:527
void unload_emitter(int id)
Unloads an emitter from miniaudio.
Definition audio.c:831
#define MAX_AUDIO_EMITTERS
The maximum amount of AudioEmitters.
int a_star_aportal(AStarWorkspace *WS, int start, int end)
Conducts AStar between a start and end point in AudioPortals.
Definition audio.c:576
void load_emitter(AudioEmitter *emitter, const char *filepath, bool is_loop, bool is_occludable, float base_volume)
Loads an existing emitter into miniaudio.
Definition audio.c:759
bool pathfind_audio_occlusion(vec3 listener_pos, int closest_l, vec3 emitter_pos, vec3 *out_pos, float *out_volume, float *distance)
Finds closest AudioPortal to the AudioEmitter and calls AStar to pathfind, then calulcates the volume...
Definition audio.c:661
void destroy_emitter(int id)
Unloads an emitter from miniaudio and frees the emitter as well.
Definition audio.c:841
void init_new_audio_portal(vec3 pos)
Creates an audio portal by expanding the vector.
Definition audio.c:737
void remove_audio_portal(int id)
Removes an audio portal from the vector.
Definition audio.c:745
void update_audio_occlusion(Player *player)
Raycasts from player to audio sources to determine occlusion and performs a star if necessary.
Definition audio.c:1791
void recalculate_audio_portal_paths()
Recalculates the adjacency map of portals between each other.
Definition audio.c:418
An AudioEmitter that plays audio possibily from a certain position or at a certain time.
AudioPortals used for determining paths to transmit sound when emitter and listener are occluded.
int audio_probe_iterations
The amount of iterations (sample rays) to bake per AudioProbe.
int audio_probe_bounces
The amount of bounces to accept per AudioProbe iteration.
vec3 extents
Definition dynamic.frag:23
vec3 center
Definition dynamic.frag:15
mat3 rot
Definition types.h:49
Oriented Bounding Boxes.
Definition dynamic.frag:14
Important globals for the entire engine and initialization functions that are mainly called on engine...
AudioScratchBuffers AUDIO_SCRATCH
Definition main.c:73
ma_engine AUDIO_ENGINE
Definition main.c:66
GLuint GLOBAL_INSTANCE_SSBO
Definition main.c:166
int DEBUG_APROBE_RAY_COUNT
Definition main.c:52
AudioEmitter AUDIO_EMITTERS[1024]
Definition main.c:67
GLuint RPROBE_SSBO
Definition main.c:160
AStarWorkspace AW
Definition main.c:75
ConvolverNode CONVOLVER_NODE
Definition main.c:72
EditorSettings EDITOR_SETTINGS
Definition main.c:330
int DEBUG_LINE_COUNT
Definition main.c:282
GLuint TEXT_BOX_SSBO
Definition main.c:172
GLuint INSTANCE_SSBO
Definition main.c:171
GLuint AUDIO_PROBE_BAKE_SHADER
audio/audio_probe_bake.comp
Definition main.c:229
HRTFThreadData HRTF_TD
Definition main.c:74
Player player
The Player data.
Definition main.c:287
int DEBUG_CURRENT_APROBE
Definition main.c:54
float vec3_dist(vec3 a, vec3 b)
Definition mathf.h:318
#define MIN(a, b)
Definition mathf.h:435
vec3 vec3_normalize(vec3 v)
Definition mathf.h:46
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:248
vec3 vec3_div(vec3 a, vec3 b)
Definition mathf.h:306
vec3 vec3_sub(vec3 a, vec3 b)
Definition mathf.h:266
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:286
float clamp(float a, float b, float c)
Definition mathf.h:379
float vec3_length(vec3 v)
Definition mathf.h:328
#define MAX(a, b)
Definition mathf.h:433
const int samples
void bind_baking_ssbos()
Binds SSBOs required for baking.
Definition render.c:31
void unbind_baking_ssbos()
Unbinds SSBOs required for baking.
Definition render.c:42
The main render pass, data GPU uploads, and SSBO management.
float x
Definition types.h:12
int y
Definition types.h:15
float y
Definition types.h:12
int z
Definition types.h:15
float z
Definition types.h:12
size_t count
Definition types.h:209
int x
Definition types.h:15
void * buffer
Definition types.h:210
Definition types.h:333
Definition types.h:15
Definition types.h:12
void init_vector(Vector *v, size_t i, size_t type_size)
Definition utilities.c:74
void * vector_expand(Vector *v, size_t i)
Definition utilities.c:49
vec3 mat3_mul_vec3(mat3 m, vec3 v)
Definition utilities.h:498
#define CAST_VEC(vec, type)
Definition utilities.h:134
#define REMOVE_FROM_VEC(vec, index, type)
Definition utilities.h:276
vec3 OBB_min_corner(OBB *obb)
Definition utilities.h:607
#define PRINT_VEC3N(vec)
Definition utilities.h:305
mat3 mat3_transpose(mat3 in)
Definition utilities.h:545
bool point_in_obb(OBB *obb, vec3 pos)
Definition utilities.h:623
#define M_PI
Definition utilities.h:52