1#ifndef _VAE_HRTF_LOADER
2#define _VAE_HRTF_LOADER
4#include "../vae_types.hpp"
5#include "../vae_util.hpp"
6#include "../vae_config.hpp"
7#include "../pod/vae_hrtf.hpp"
9#include "../../wrapped/vae_fs.hpp"
12 #include "../../../external/headeronly/json.h"
13 #include "../../../external/headeronly/json.c"
16#include "../../../external/glm/glm/gtc/matrix_transform.hpp"
20namespace vae {
namespace core {
22 static void*
allocate(
size_t size,
int zero,
void* context) {
24 void* ptr =
reinterpret_cast<void*
>(allocator.
allocate(size));
33 allocator.
deallocate(
reinterpret_cast<char*
>(ptr), 0);
42 const char* encoded = path;
47 VAE_DEBUG(
"Started loading HRTF %s", path)
58 length = jsonText.
size();
59 encoded = jsonText.
c_str();
68 json_settings settings = { };
75 json = json_parse_ex(&settings, encoded, length, 0);
77 if (json ==
nullptr) {
81 json_value& data = (*json);
83 hrtf.
rate = sampleRate;
87 float((
double) data[
"up"][0]),
88 float((
double) data[
"up"][1]),
89 float((
double) data[
"up"][2]),
93 float((
double) data[
"front"][0]),
94 float((
double) data[
"front"][1]),
95 float((
double) data[
"front"][2]),
102 glm::mat4x4 matchCoord = glm::lookAt(
108 Vec3 up1 = (matchCoord * glm::vec4(up, 1.f));
109 Vec3 front1 = (matchCoord * glm::vec4(front, 1.f));
112 auto& positions = data[
"positions"].u.array;
113 const Size positionCount = positions.length;
116 const bool needsResample = hrtf.
originalRate != sampleRate;
119 VAE_ERROR(
"Can't open HRTF, resampling needed!")
123 Size maxIrLength = 0;
126 for (
Size i = 0; i < positionCount; 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++) {
136 for (
Size j = 0; j < irLength; j++) {
137 p.
ir[c][0][j] = (double) *(irSamples[c].u.array.values[j]);
146 json_value_free_ex(&settings, json);
149 VAE_DEBUG(
"Finished loading HRTF %s", path)
bool resize(const Size length, uchar channels)
! Will not keep the contents! Resizes the buffer to the desired length and channel count.
const char * c_str() const
void append(const String &str)
Result load(const char *path, Size length, const char *rootPath, const Size sampleRate, HRTF &hrtf)
static void * allocate(size_t size, int zero, void *context)
static void deallocate(void *ptr, void *context)
T max(const T &v1, const T &v2)
Contains Typedefinitions and basic structures use by the public API and internally.
unsigned int Size
How the elements are addressed in the heapbuffer.
Result
Return Types for most engine functions.
@ FileOpenError
File system could not load file.
@ BankFormatError
Generic bank loading error.
Listener uses additional up vector.
AudioBuffer ir[2]
Impulse response needed for the convolution.
HeapBuffer< Position > positions
Allocator used for all heapbuffers in VAE.
T * allocate(size_t n) noexcept
void deallocate(T *ptr, std::size_t n) noexcept
#define VAE_ERROR(msg,...)
#define VAE_DEBUG(msg,...)
#define VAE_PROFILER_SCOPE_NAMED(name)
Profiles a scope and names it.
#define VAE_PROFILER_SCOPE()
Profiles a scope.