VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
vae::core::HRTFLoader Class Reference

#include <vae_hrtf_loader.hpp>

Collaboration diagram for vae::core::HRTFLoader:

Public Member Functions

Result load (const char *path, Size length, const char *rootPath, const Size sampleRate, HRTF &hrtf)
 

Static Private Member Functions

static void * allocate (size_t size, int zero, void *context)
 
static void deallocate (void *ptr, void *context)
 

Detailed Description

Definition at line 21 of file vae_hrtf_loader.hpp.

Member Function Documentation

◆ allocate()

static void * vae::core::HRTFLoader::allocate ( size_t  size,
int  zero,
void *  context 
)
inlinestaticprivate

Definition at line 22 of file vae_hrtf_loader.hpp.

22 {
23 memory::AllocatorFS<char> allocator;
24 void* ptr = reinterpret_cast<void*>(allocator.allocate(size));
25 if (zero) {
26 memset(ptr, 0, size);
27 }
28 return ptr;
29 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ deallocate()

static void vae::core::HRTFLoader::deallocate ( void *  ptr,
void *  context 
)
inlinestaticprivate

Definition at line 31 of file vae_hrtf_loader.hpp.

31 {
32 memory::AllocatorFS<char> allocator;
33 allocator.deallocate(reinterpret_cast<char*>(ptr), 0);
34 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ load()

Result vae::core::HRTFLoader::load ( const char *  path,
Size  length,
const char *  rootPath,
const Size  sampleRate,
HRTF hrtf 
)
inline

Open file and decode json

Definition at line 36 of file vae_hrtf_loader.hpp.

36 {
38
39 /**
40 * Open file and decode json
41 */
42 const char* encoded = path; // The plain json text
43
44 String jsonText;
45 if (length == 0) { // length 0 indicates the file is on disk
46 #ifndef VAE_NO_STDIO
47 VAE_DEBUG("Started loading HRTF %s", path)
49 PathString folder;
50 folder = rootPath;
51 folder.append(path);
52
53 fs::File file(folder.c_str());
54 jsonText.resize(file.size());
55 if (!file.readAll(jsonText.data())) {
57 }
58 length = jsonText.size();
59 encoded = jsonText.c_str();
60 #else
62 #endif
63 }
64
65
66
67
68 json_settings settings = { };
69 settings.mem_alloc = allocate;
70 settings.mem_free = deallocate;
71
72 json_value* json;
73 {
74 VAE_PROFILER_SCOPE_NAMED("HRTF Parse")
75 json = json_parse_ex(&settings, encoded, length, 0);
76 }
77 if (json == nullptr) {
78 VAE_ERROR("Failed to parse HRTF")
79 return Result::BankFormatError;
80 }
81 json_value& data = (*json);
82
83 hrtf.rate = sampleRate;
84 hrtf.originalRate = (json_int_t) data["samplerate"];
85
86 Vec3 up = {
87 float((double) data["up"][0]),
88 float((double) data["up"][1]),
89 float((double) data["up"][2]),
90 };
91
92 Vec3 front = {
93 float((double) data["front"][0]),
94 float((double) data["front"][1]),
95 float((double) data["front"][2]),
96 };
97
98 LocationOrientation ref;
99 Vec3 frontNeed = Vec3(ref.front.x, ref.front.y, ref.front.z);
100 Vec3 upNeed = Vec3(ref.up.x, ref.up.y, ref.up.z);
101
102 glm::mat4x4 matchCoord = glm::lookAt(
103 Vec3(0.f, 0.f, 0.f),
104 front,
105 up
106 );
107
108 Vec3 up1 = (matchCoord * glm::vec4(up, 1.f));
109 Vec3 front1 = (matchCoord * glm::vec4(front, 1.f));
110 // These should match upNeed
111
112 auto& positions = data["positions"].u.array;
113 const Size positionCount = positions.length;
114 hrtf.positions.resize(positionCount);
115
116 const bool needsResample = hrtf.originalRate != sampleRate;
117 if (needsResample) {
118 // TODO
119 VAE_ERROR("Can't open HRTF, resampling needed!")
120 return Result::GenericFailure;
121 }
122
123 Size maxIrLength = 0;
124 {
125 VAE_PROFILER_SCOPE_NAMED("HRTF Convert")
126 for (Size i = 0; i < positionCount; i++) {
127 HRTF::Position& p = hrtf.positions[i];
128 auto& pi = *positions.values[i];
129 glm::vec4 pos((double) pi["x"], (double)pi["y"], (double)pi["z"], 1.0);
130 p.pos = matchCoord * pos;
131 json_value irSamples[2] = { pi["left"], pi["right"]};
132 const Size irLength = irSamples[0].u.array.length;
133 maxIrLength = std::max(maxIrLength, irLength);
134 for (int c = 0; c < 2; c++) {
135 p.ir[c].resize(irLength, 1);
136 for (Size j = 0; j < irLength; j++) {
137 p.ir[c][0][j] = (double) *(irSamples[c].u.array.values[j]);
138 }
139 }
140 }
141 }
142
143 hrtf.irLength = maxIrLength;
144 {
145 VAE_PROFILER_SCOPE_NAMED("HRTF Dealloc")
146 json_value_free_ex(&settings, json);
147 }
148
149 VAE_DEBUG("Finished loading HRTF %s", path)
150
151 return Result::Success;
152 }
static void * allocate(size_t size, int zero, void *context)
static void deallocate(void *ptr, void *context)
constexpr int rate
Definition: main.cpp:19
T max(const T &v1, const T &v2)
Definition: TMath.hpp:21
glm::vec3 Vec3
Definition: vae_types.hpp:47
tklb::String< HeapBuffer< char > > PathString
Non optional string used for locations, maybe replaceable with a unique_ptr or something.
Definition: vae_types.hpp:95
unsigned int Size
How the elements are addressed in the heapbuffer.
Definition: vae.hpp:33
Result
Return Types for most engine functions.
Definition: vae.hpp:73
@ FileOpenError
File system could not load file.
HeapBuffer< char > String
Definition: string.cpp:4
Definition: string.cpp:6
#define VAE_ERROR(msg,...)
Definition: vae_logger.hpp:80
#define VAE_DEBUG(msg,...)
Definition: vae_logger.hpp:83
#define VAE_PROFILER_SCOPE_NAMED(name)
Profiles a scope and names it.
#define VAE_PROFILER_SCOPE()
Profiles a scope.
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: