55 SH[1] = 0.488603 * dir.
y;
56 SH[2] = 0.488603 * dir.
z;
57 SH[3] = 0.488603 * dir.
x;
58 SH[4] = 1.092548 * dir.
x * dir.
y;
59 SH[5] = 1.092548 * dir.
y * dir.
z;
60 SH[6] = 0.315392 * (3 * dir.
z * dir.
z - 1);
61 SH[7] = 1.092548 * (dir.
x * dir.
z);
62 SH[8] = 0.546274 * (dir.
x * dir.
x - dir.
y * dir.
y);
74 int complex_len = (padded_len / 2) + 1;
77 for (
int i = 0; i <
SH_MAX; i++)
113 FILE * f = fopen(filename,
"rb");
120 for (
int i = 0; i <
SH_MAX; i++)
160 fftwf_complex * prev_ir_left = (*node->
prev_ir)[0][i];
161 fftwf_complex * prev_ir_right = (*node->
prev_ir)[1][i];
167 node->
PREV_SUM_L[j][0] +=
X[j][0] * prev_ir_left[j][0] -
X[j][1] * prev_ir_left[j][1];
168 node->
PREV_SUM_L[j][1] +=
X[j][0] * prev_ir_left[j][1] +
X[j][1] * prev_ir_left[j][0];
170 node->
PREV_SUM_R[j][0] +=
X[j][0] * prev_ir_right[j][0] -
X[j][1] * prev_ir_right[j][1];
171 node->
PREV_SUM_R[j][1] +=
X[j][0] * prev_ir_right[j][1] +
X[j][1] * prev_ir_right[j][0];
178 fftwf_complex * ir_left = (*node->
ir)[0][i];
179 fftwf_complex * ir_right = (*node->
ir)[1][i];
184 node->
SUM_L[j][0] +=
X[j][0] * ir_left[j][0] -
X[j][1] * ir_left[j][1];
185 node->
SUM_L[j][1] +=
X[j][0] * ir_left[j][1] +
X[j][1] * ir_left[j][0];
187 node->
SUM_R[j][0] +=
X[j][0] * ir_right[j][0] -
X[j][1] * ir_right[j][1];
188 node->
SUM_R[j][1] +=
X[j][0] * ir_right[j][1] +
X[j][1] * ir_right[j][0];
237 const static float s_c3 = 0.94617469575f;
238 const static float s_c4 = -0.31539156525f;
239 const static float s_c5 = 0.54627421529f;
241 const static float s_c_scale = 1.0 / 0.91529123286551084f;
242 const static float s_c_scale_inv = 0.91529123286551084f;
244 const static float s_rc2 = 1.5853309190550713f * s_c_scale;
245 const static float s_c4_div_c3 = s_c4 / s_c3;
246 const static float s_c4_div_c3_x2 = (s_c4 / s_c3) * 2.0;
248 const static float s_scale_dst2 = s_c3 * s_c_scale_inv;
249 const static float s_scale_dst4 = s_c5 * s_c_scale_inv;
252 memcpy(src, SH,
sizeof(
float) *
SH_MAX);
255 OUT[1] =
rot.b1 * src[1] -
rot.b2 * src[2] +
rot.b0 * src[3];
256 OUT[2] = -
rot.c1 * src[1] +
rot.c2 * src[2] -
rot.c0 * src[3];
257 OUT[3] =
rot.a1 * src[1] -
rot.a2 * src[2] +
rot.a0 * src[3];
261 float sh0 = src[7] + src[8] + src[8] - src[5];
262 float sh1 = src[4] + s_rc2 * src[6] + src[7] + src[8];
267 float r2x =
rot.a0 +
rot.a1;
268 float r2y =
rot.b0 +
rot.b1;
269 float r2z =
rot.c0 +
rot.c1;
271 float r3x =
rot.a0 +
rot.a2;
272 float r3y =
rot.b0 +
rot.b2;
273 float r3z =
rot.c0 +
rot.c2;
275 float r4x =
rot.a1 +
rot.a2;
276 float r4y =
rot.b1 +
rot.b2;
277 float r4z =
rot.c1 +
rot.c2;
279 float sh0_x = sh0 *
rot.a0;
280 float sh0_y = sh0 *
rot.b0;
281 float d0 = sh0_x *
rot.b0;
282 float d1 = sh0_y *
rot.c0;
283 float d2 = sh0 * (
rot.c0 *
rot.c0 + s_c4_div_c3);
284 float d3 = sh0_x *
rot.c0;
285 float d4 = sh0_x *
rot.a0 - sh0_y *
rot.b0;
287 float sh1_x = sh1 *
rot.a2;
288 float sh1_y = sh1 *
rot.b2;
289 d0 += sh1_x *
rot.b2;
290 d1 += sh1_y *
rot.c2;
291 d2 += sh1 * (
rot.c2 *
rot.c2 + s_c4_div_c3);
292 d3 += sh1_x *
rot.c2;
293 d4 += sh1_x *
rot.a2 - sh1_y *
rot.b2;
295 float sh2_x = sh2 * r2x;
296 float sh2_y = sh2 * r2y;
299 d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2);
301 d4 += sh2_x * r2x - sh2_y * r2y;
303 float sh3_x = sh3 * r3x;
304 float sh3_y = sh3 * r3y;
307 d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2);
309 d4 += sh3_x * r3x - sh3_y * r3y;
311 float sh4_x = sh4 * r4x;
312 float sh4_y = sh4 * r4y;
315 d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2);
317 d4 += sh4_x * r4x - sh4_y * r4y;
321 OUT[6] = d2 * s_scale_dst2;
323 OUT[8] = d4 * s_scale_dst4;
371 for (
int sh = 0; sh <
SH_MAX; sh++)
378 for (
int p = 0; p < probe->
ir_length; p++)
383 for (
int sh = 0; sh <
SH_MAX; sh++)
393 for (
int sh = 0; sh <
SH_MAX; sh++)
413 int fade_samples = 128;
460 SDL_UnlockMutex(td->
mutex);
517 td->
mutex = SDL_CreateMutex();
538 td->
t = SDL_CreateThread(
hrtf_worker,
"HRTF Worker Thread", td);
539 SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW);
546 SDL_LockMutex(td->
mutex);
550 SDL_UnlockMutex(td->
mutex);
552 SDL_WaitThread(td->
t, NULL);
556 SDL_DestroyMutex(td->
mutex);
562 fftwf_free(td->
tables[i][0][j]);
563 fftwf_free(td->
tables[i][1][j]);
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
Camera cam
This is currently the player cam.
Spring neck
Neck Spring (target based on pelvis).
SceneData scene_data
SceneData that is more-so scene specific.
Skeleton skeleton
Player Skeleton.
bool LOADED_AUDIO_PV_FLAG[4]
Flags that indicate if the slot is loaded.
Globals GLOBALS
Engine Globals.
APV LOADED_AUDIO_PV[4]
Load slots for Audio ProbeVolumes.
#define PARTITION_BUFFERS
The amount of audio partition back buffers.
vec3 pos
Position of the spring.
Camera * cam
Player Camera.
fftwf_plan bwd_plan
Inverse plan used when convolving IR with current audio stream.
float * PREV_LEFT_OUT
Contains the output of the inverse FFT of the previous IR with current audio stream.
float * IR_ROT[9]
The IR with rotated SH.
fftwf_complex * STATIC_HRTF_R_FWD[9]
The forward FFT of the static HRTF loaded from HRTF.bin.
fftwf_complex * PREV_SUM_L
The complex multiply of the previous IR with the partitions.
PartitionTable tables[4]
The partitions for overlap-add quad-buffered.
fftwf_plan fwd_plan
Forward plan used when convolving IR with current audio stream.
PartitionTable * FRONT
The current partition presented to the audio thread.
fftwf_plan IR_bwd_plan
Inverse plan used when convolving IR with HRTF.
vec3 last_pos
The last job's position.
AudioProbe * audio_probes
The probes in the volume, loaded in from binary.
PartitionTable * ir
The pointer to the new IR to convolve with.
float * PREV_RIGHT_OUT
Contains the output of the inverse FFT of the previous IR with current audio stream.
int probes[4]
The indices of the AudioProbes.
int complex_ir_len
(padded_len / 2) + 1
bool new_probe_ready
If a new job is ready.
fftwf_complex * PREV_SUM_R
The complex multiply of the previous IR with the partitions.
float(* final_ir)[9]
The baked IR samples by 9 SH.
fftwf_complex * MONO_FWD
Contains the output of the forward FFT of IR with current audio stream.
bool stop_requested
Used to quit gracefully.
fftwf_plan static_fwd_plan
Unusued - was used when parsing in sofas.
float * HRTF_IR_IN
Buffer used when splitting IR into partitions.
fftwf_complex * IR_SUM_L_FWD
Used to accumulate the complex multiply of IR with the HRTF.
fftwf_complex * IR_ROT_FWD[9]
The forward FFT of the rotated IR.
float * STATIC_HRTF_L[9]
The static HRTF loaded from HRTF.bin.
float * RIGHT_OUT
Contains the output of the inverse FFT of the new IR with current audio stream.
int padded_len
Currently 1024.
int complex_len
(padded_len / 2) + 1
float * IR_SUM_R
Used to hold the output of the inverse FFT of the IR with the HRTF.
float * IR_SUM_L
Used to hold the output of the inverse FFT of the IR with the HRTF.
fftwf_complex * SUM_R
The complex multiply of the new IR with the partitions.
SDL_mutex * mutex
The mutex used when signalling a job.
size_t ir_length
The amount of IR samples for this probe.
fftwf_plan IR_fwd_plan
Forward plan used when convolving IR with HRTF.
SDL_mutex * aprobe_mutex
Used when loading new APVs to avoid concurrency with the HRTF thread.
float weights[4]
The weights that correspond to each probe's value.
vec3 last_fwd
The last job's direction.
fftwf_complex * SUM_L
The complex multiply of the new IR with the partitions.
int complex_len
(padded_len / 2) + 1
int ir_len
MAX_IR_SAMPLES + hrtf_length - 1 and rounded up to next power of 2.
PartitionTable * prev_ir
The pointer to the previous IR we were convolving with.
int APV_id
Which APV these probes belong to.
float * STATIC_HRTF_R[9]
The static HRTF loaded from HRTF.bin.
fftwf_complex * IR_SUM_R_FWD
Used to accumulate the complex multiply of IR with the HRTF.
fftwf_complex * AUDIO_HISTORY[96]
Contains the history of MONO_FWD up to NUM_PARTITIONS_MAX.
SDL_SpinLock ir_swap_lock
The spinlock used when presenting an IR to the audio thread.
int block_size
Currently 512.
int count
The amount of probes that were sampled.
fftwf_complex * STATIC_HRTF_L_FWD[9]
The forward FFT of the static HRTF loaded from HRTF.bin.
fftwf_plan fwd_plan
The plan used when splitting IR into partitions.
int padded_len
Currently 1024.
int history_head
Contains the current position in the audio history buffer.
AudioProbeWeights w
Current job's weights.
float * LEFT_OUT
Contains the output of the inverse FFT of the new IR with current audio stream.
SDL_cond * work_cond
The job signal.
SDL_Thread * t
The thread object.
int final_ir_len
MAX_SAMPLES + hrtf_length - 1.
#define AUDIO_PROBE_BINS
The amount of bins (to reduce max sample size).
#define NUM_PARTITIONS
Amount of max partitions to be used for overlap-add.
#define MAX_IR_SAMPLES
Maximum IR sample length.
void evaluate_sh(vec3 dir, float SH[SH_MAX])
Evaluates 9 spherical harmonics based on direction.
void fft_channels(ConvolverNode *node, bool crossfading)
Applies an impulse response filter via discrete convolution.
void update_player_ir(PartitionTable *OUT)
Rotates neighboring IRs to the Player and then partitions them for convolution in the audio thread.
void delete_hrtf_worker_thread(HRTFThreadData *td)
Cleans up the worker thread that convolves the audio probes with the HRTF.
PartitionTableChannel PartitionTable[2]
Type for two channels (stereo) for overlap-add.
void load_hrtf_from_binary(const char *filename)
Loads a stored HRTF from engine binary.
void allocate_audio_scratch_hrtf(int hrtf_length)
Allocates AUDIO_SCRATCH based on HRTF size.
fftwf_complex * PartitionTableChannel[95]
Type for one FFT partiton for overlap-add.
void rotate_sh(float *OUT, float SH[SH_MAX], mat3 rot)
Converts a position to local OBB space and returns the coord as if it was a texture from .
#define NUM_PARTITIONS_MAX
Amount of max partitions to be used for overlap-add for audio probes + 1 for the tail trick.
int SDLCALL hrtf_worker(void *params)
Main thread loop for HRTFThreadData.
void init_hrtf_worker_thread(HRTFThreadData *td)
Initializes the worker thread that convolves the audio probes with the HRTF.
Baked IR samples loaded in from binary into APVs from ProbeVolumes.
The IR CONVOLVER_NODE used to process frames in the audio thread.
The data for the thread that prepares IRs by rotating then convolving them with an HRTF before it pre...
Important globals for the entire engine and initialization functions that are mainly called on engine...
AudioScratchBuffers AUDIO_SCRATCH
ConvolverNode CONVOLVER_NODE
Player player
The Player data.
mat3 rotation_matrix_r(vec3 right, vec3 up, vec3 forward)
int round_up_power_of_two(int a, int max)