Untitled
Loading...
Searching...
No Matches
audio_ir.c
Go to the documentation of this file.
1
7#include "utilities/types.h"
8#include "load/init.h"
9#include "utilities/mathf.h"
10
11
17static void allocate_audio_scratch_hrtf(int hrtf_length);
18
42static void update_player_ir(PartitionTable * OUT);
43
49static int SDLCALL hrtf_worker(void * params);
50
51void evaluate_sh(vec3 dir, float SH[SH_MAX])
52{
53
54 SH[0] = 0.282095;
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);
63
64}
65
66
67static void allocate_audio_scratch_hrtf(int hrtf_length)
68{
69
70 HRTF_LENGTH = hrtf_length;
71 int len_hrtf = MAX_SAMPLES + hrtf_length - 1;
72 int padded_len = MAX_IR_SAMPLES + hrtf_length - 1;
73 padded_len = round_up_power_of_two(padded_len, 48);
74 int complex_len = (padded_len / 2) + 1;
75 AUDIO_SCRATCH.ir_len = padded_len;
76 AUDIO_SCRATCH.complex_ir_len = complex_len;
77 for (int i = 0; i < SH_MAX; i++)
78 {
79
80 AUDIO_SCRATCH.STATIC_HRTF_L[i] = fftwf_malloc(padded_len * sizeof(float));
81 AUDIO_SCRATCH.STATIC_HRTF_R[i] = fftwf_malloc(padded_len * sizeof(float));
82
83 AUDIO_SCRATCH.STATIC_HRTF_L_FWD[i] = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
84 AUDIO_SCRATCH.STATIC_HRTF_R_FWD[i] = fftwf_malloc(sizeof(fftwf_complex) * complex_len);
85 memset(AUDIO_SCRATCH.STATIC_HRTF_L[i], 0, sizeof(float) * padded_len);
86 memset(AUDIO_SCRATCH.STATIC_HRTF_R[i], 0, sizeof(float) * padded_len);
87 memset(AUDIO_SCRATCH.STATIC_HRTF_L_FWD[i], 0, sizeof(fftwf_complex) * complex_len);
88 memset(AUDIO_SCRATCH.STATIC_HRTF_R_FWD[i], 0, sizeof(fftwf_complex) * complex_len);
89 AUDIO_SCRATCH.IR_ROT[i] = fftwf_malloc(padded_len * sizeof(float));
90
91 AUDIO_SCRATCH.IR_ROT_FWD[i] = fftwf_malloc(AUDIO_SCRATCH.complex_ir_len * sizeof(fftwf_complex));
92 memset(AUDIO_SCRATCH.IR_ROT[i], 0, sizeof(float) * padded_len);
93 memset(AUDIO_SCRATCH.IR_ROT_FWD[i], 0, sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_ir_len);
94 }
95 AUDIO_SCRATCH.IR_SUM_L = fftwf_malloc(padded_len * sizeof(float));
96 AUDIO_SCRATCH.IR_SUM_R = fftwf_malloc(padded_len * sizeof(float));
97 AUDIO_SCRATCH.IR_SUM_L_FWD = fftwf_malloc(AUDIO_SCRATCH.complex_ir_len * sizeof(fftwf_complex));
98 AUDIO_SCRATCH.IR_SUM_R_FWD = fftwf_malloc(AUDIO_SCRATCH.complex_ir_len * sizeof(fftwf_complex));
99 memset(AUDIO_SCRATCH.IR_SUM_L, 0, sizeof(float) * padded_len);
100 memset(AUDIO_SCRATCH.IR_SUM_R, 0, sizeof(float) * padded_len);
101 memset(AUDIO_SCRATCH.IR_SUM_L_FWD, 0, sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_ir_len);
102 memset(AUDIO_SCRATCH.IR_SUM_R_FWD, 0, sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_ir_len);
103
104 AUDIO_SCRATCH.final_ir_len = len_hrtf;
105
106 AUDIO_SCRATCH.static_fwd_plan = fftwf_plan_dft_r2c_1d(padded_len, NULL, NULL, FFTW_ESTIMATE);
107 AUDIO_SCRATCH.IR_fwd_plan = fftwf_plan_dft_r2c_1d(padded_len, NULL, NULL, FFTW_ESTIMATE);
108 AUDIO_SCRATCH.IR_bwd_plan = fftwf_plan_dft_c2r_1d(padded_len, NULL, NULL, FFTW_ESTIMATE);
109}
110
111void load_hrtf_from_binary(const char * filename)
112{
113 FILE * f = fopen(filename, "rb");
114 if (!f) return;
115
116 fread(&HRTF_LENGTH, sizeof(int), 1, f);
117
119
120 for (int i = 0; i < SH_MAX; i++)
121 {
122 fread(AUDIO_SCRATCH.STATIC_HRTF_L[i], sizeof(float), AUDIO_SCRATCH.ir_len, f);
123 fread(AUDIO_SCRATCH.STATIC_HRTF_R[i], sizeof(float), AUDIO_SCRATCH.ir_len, f);
124 fread(AUDIO_SCRATCH.STATIC_HRTF_L_FWD[i], sizeof(fftwf_complex), AUDIO_SCRATCH.complex_ir_len, f);
125 fread(AUDIO_SCRATCH.STATIC_HRTF_R_FWD[i], sizeof(fftwf_complex), AUDIO_SCRATCH.complex_ir_len, f);
126 }
127
128 if (f) fclose(f);
129}
130
131
138void fft_channels(ConvolverNode * node, bool crossfading)
139{
140
141
142
143 fftwf_execute_dft_r2c(node->fwd_plan, node->LEFT_OUT, node->MONO_FWD);
144
145 memset(CONVOLVER_NODE.PREV_LEFT_OUT, 0, sizeof(float) * CONVOLVER_NODE.padded_len);
146 memset(CONVOLVER_NODE.PREV_RIGHT_OUT, 0, sizeof(float) * CONVOLVER_NODE.padded_len);
147 memset(CONVOLVER_NODE.LEFT_OUT, 0, sizeof(float) * CONVOLVER_NODE.padded_len);
148 memset(CONVOLVER_NODE.RIGHT_OUT, 0, sizeof(float) * CONVOLVER_NODE.padded_len);
149
150 node->history_head = (node->history_head + 1) % NUM_PARTITIONS_MAX;
151
152 memcpy(node->AUDIO_HISTORY[node->history_head], node->MONO_FWD, sizeof(fftwf_complex) * node->complex_len);
153
154 for (int i = 0; i < NUM_PARTITIONS; i++)
155 {
156
157 int history_idx = (node->history_head - i + NUM_PARTITIONS_MAX) % NUM_PARTITIONS_MAX;
158
159 fftwf_complex * X = node->AUDIO_HISTORY[history_idx];
160 fftwf_complex * prev_ir_left = (*node->prev_ir)[0][i];
161 fftwf_complex * prev_ir_right = (*node->prev_ir)[1][i];
162
163
164 for (int j = 0; j < node->complex_len; j++)
165 {
166
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];
169
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];
172
173 }
174
175 if (crossfading)
176 {
177
178 fftwf_complex * ir_left = (*node->ir)[0][i];
179 fftwf_complex * ir_right = (*node->ir)[1][i];
180
181 for (int j = 0; j < node->complex_len; j++)
182 {
183
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];
186
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];
189
190 }
191 }
192
193 }
194
195 fftwf_execute_dft_c2r(node->bwd_plan, node->PREV_SUM_L, node->PREV_LEFT_OUT);
196 fftwf_execute_dft_c2r(node->bwd_plan, node->PREV_SUM_R, node->PREV_RIGHT_OUT);
197
198 if (crossfading)
199 {
200
201 fftwf_execute_dft_c2r(node->bwd_plan, node->SUM_L, node->LEFT_OUT);
202 fftwf_execute_dft_c2r(node->bwd_plan, node->SUM_R, node->RIGHT_OUT);
203
204 }
205
206}
207
233// CODE FROM JOHN HABLE
234void rotate_sh(float * OUT, float SH[SH_MAX], mat3 rot)
235{
236
237 const static float s_c3 = 0.94617469575f;
238 const static float s_c4 = -0.31539156525f;
239 const static float s_c5 = 0.54627421529f;
240
241 const static float s_c_scale = 1.0 / 0.91529123286551084f;
242 const static float s_c_scale_inv = 0.91529123286551084f;
243
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;
247
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;
250
251 float src[SH_MAX];
252 memcpy(src, SH, sizeof(float) * SH_MAX);
253
254 OUT[0] = src[0];
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];
258
259
260
261 float sh0 = src[7] + src[8] + src[8] - src[5];
262 float sh1 = src[4] + s_rc2 * src[6] + src[7] + src[8];
263 float sh2 = src[4];
264 float sh3 = -src[7];
265 float sh4 = -src[5];
266
267 float r2x = rot.a0 + rot.a1;
268 float r2y = rot.b0 + rot.b1;
269 float r2z = rot.c0 + rot.c1;
270
271 float r3x = rot.a0 + rot.a2;
272 float r3y = rot.b0 + rot.b2;
273 float r3z = rot.c0 + rot.c2;
274
275 float r4x = rot.a1 + rot.a2;
276 float r4y = rot.b1 + rot.b2;
277 float r4z = rot.c1 + rot.c2;
278
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;
286
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;
294
295 float sh2_x = sh2 * r2x;
296 float sh2_y = sh2 * r2y;
297 d0 += sh2_x * r2y;
298 d1 += sh2_y * r2z;
299 d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2);
300 d3 += sh2_x * r2z;
301 d4 += sh2_x * r2x - sh2_y * r2y;
302
303 float sh3_x = sh3 * r3x;
304 float sh3_y = sh3 * r3y;
305 d0 += sh3_x * r3y;
306 d1 += sh3_y * r3z;
307 d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2);
308 d3 += sh3_x * r3z;
309 d4 += sh3_x * r3x - sh3_y * r3y;
310
311 float sh4_x = sh4 * r4x;
312 float sh4_y = sh4 * r4y;
313 d0 += sh4_x * r4y;
314 d1 += sh4_y * r4z;
315 d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2);
316 d3 += sh4_x * r4z;
317 d4 += sh4_x * r4x - sh4_y * r4y;
318
319 OUT[4] = d0;
320 OUT[5] = -d1;
321 OUT[6] = d2 * s_scale_dst2;
322 OUT[7] = -d3;
323 OUT[8] = d4 * s_scale_dst4;
324}
325
327{
328
329
330 SDL_LockMutex(AUDIO_SCRATCH.aprobe_mutex);
331 if (HRTF_TD.w.count == 0)
332 {
333 SDL_UnlockMutex(AUDIO_SCRATCH.aprobe_mutex);
334 return;
335
336 }
337 if (HRTF_TD.w.APV_id == -1)
338 {
339
340 SDL_UnlockMutex(AUDIO_SCRATCH.aprobe_mutex);
341 return;
342 }
344 {
345
346 SDL_UnlockMutex(AUDIO_SCRATCH.aprobe_mutex);
347 return;
348 }
349
352
353
354 PartitionTableChannel * chl = &((*OUT)[0]);
355 PartitionTableChannel * chr = &((*OUT)[1]);
356 for (int j = 0; j < NUM_PARTITIONS; j++)
357 {
358
359 memset((*chl)[j], 0, sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_len);
360 memset((*chr)[j], 0, sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_len);
361 }
362
363
364 memset(AUDIO_SCRATCH.IR_SUM_L, 0, sizeof(float) * AUDIO_SCRATCH.final_ir_len);
365 memset(AUDIO_SCRATCH.IR_SUM_R, 0, sizeof(float) * AUDIO_SCRATCH.final_ir_len);
366
367 int samples_per_bin = MAX_SAMPLES / AUDIO_PROBE_BINS;
368
370
371 for (int sh = 0; sh < SH_MAX; sh++)
372 {
373 memset(AUDIO_SCRATCH.IR_ROT[sh], 0, sizeof(float) * AUDIO_SCRATCH.ir_len);
374 }
375 for (int j = 0; j < HRTF_TD.w.count; j++)
376 {
378 for (int p = 0; p < probe->ir_length; p++)
379 {
380 float out[SH_MAX];
381 rotate_sh(out, probe->final_ir[p], rot);
382
383 for (int sh = 0; sh < SH_MAX; sh++)
384 {
385 AUDIO_SCRATCH.IR_ROT[sh][p] += out[sh] * HRTF_TD.w.weights[j];
386 }
387 }
388 }
389
390
391 memset(AUDIO_SCRATCH.IR_SUM_L_FWD, 0, sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_ir_len);
392 memset(AUDIO_SCRATCH.IR_SUM_R_FWD, 0, sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_ir_len);
393 for (int sh = 0; sh < SH_MAX; sh++)
394 {
395 fftwf_execute_dft_r2c(AUDIO_SCRATCH.IR_fwd_plan, AUDIO_SCRATCH.IR_ROT[sh], AUDIO_SCRATCH.IR_ROT_FWD[sh]);
396
397 for (int i = 0; i < AUDIO_SCRATCH.complex_ir_len; i++)
398 {
399
402
405 }
406 }
407 memset(AUDIO_SCRATCH.IR_SUM_L, 0, sizeof(float) * AUDIO_SCRATCH.ir_len);
408 memset(AUDIO_SCRATCH.IR_SUM_R, 0, sizeof(float) * AUDIO_SCRATCH.ir_len);
411
412 float scale = 1.0f / (float)AUDIO_SCRATCH.ir_len;
413 int fade_samples = 128;
414 int fade_start = AUDIO_SCRATCH.final_ir_len - fade_samples;
415 for (int k = 0; k < AUDIO_SCRATCH.final_ir_len; k++) {
416 AUDIO_SCRATCH.IR_SUM_L[k] *= scale;
417 AUDIO_SCRATCH.IR_SUM_R[k] *= scale;
418 }
419
420
421 SDL_UnlockMutex(AUDIO_SCRATCH.aprobe_mutex);
422 for (int i = 0; i < NUM_PARTITIONS; i++)
423 {
424
425
426
427 memset(AUDIO_SCRATCH.HRTF_IR_IN, 0, sizeof(float) * AUDIO_SCRATCH.padded_len);
428 for (int k = 0; k < AUDIO_SCRATCH.block_size; k++)
429 {
430 int ir_idx = (i * AUDIO_SCRATCH.block_size) + k;
431 if (ir_idx < AUDIO_SCRATCH.final_ir_len) {
433 }
434 }
435 fftwf_execute_dft_r2c(AUDIO_SCRATCH.fwd_plan, AUDIO_SCRATCH.HRTF_IR_IN, (*chl)[i]);
436
437 memset(AUDIO_SCRATCH.HRTF_IR_IN, 0, sizeof(float) * AUDIO_SCRATCH.padded_len);
438 for (int k = 0; k < AUDIO_SCRATCH.block_size; k++)
439 {
440 int ir_idx = (i * AUDIO_SCRATCH.block_size) + k;
441 if (ir_idx < AUDIO_SCRATCH.final_ir_len) {
443 }
444 }
445 fftwf_execute_dft_r2c(AUDIO_SCRATCH.fwd_plan, AUDIO_SCRATCH.HRTF_IR_IN, (*chr)[i]);
446 }
447
448}
449
450static int SDLCALL hrtf_worker(void * params)
451{
452
453 HRTFThreadData * td = (HRTFThreadData*)params;
454
455 while(1)
456 {
457
458 if (td->stop_requested)
459 {
460 SDL_UnlockMutex(td->mutex);
461 break;
462 }
463
464 if ((!td->new_probe_ready && !td->stop_requested))
465 {
466 SDL_CondWait(td->work_cond, td->mutex);
467 continue;
468 }
469
470
471 PartitionTable * t = NULL;
472 SDL_AtomicLock(&td->ir_swap_lock);
473 {
474 PartitionTable * f = td->FRONT;
477
478 for (int i = 0; i < PARTITION_BUFFERS; i++)
479 {
480 if (b != &td->tables[i] && f != &td->tables[i] && a != &td->tables[i])
481 {
482
483 t = &td->tables[i];
484 break;
485 }
486 }
487 }
488 SDL_AtomicUnlock(&td->ir_swap_lock);
489
490 if (t == NULL)
491 {
492 continue;
493 }
494
495 td->new_probe_ready = false;
497
498
499 SDL_AtomicLock(&td->ir_swap_lock);
500 {
501 td->FRONT = t;
502 }
503 SDL_AtomicUnlock(&td->ir_swap_lock);
504
505
506 }
507
508 return 1;
509
510}
511
513{
514
515 memset(td, 0, sizeof(HRTFThreadData));
516
517 td->mutex = SDL_CreateMutex();
518 td->work_cond = SDL_CreateCond();
519 td->ir_swap_lock = 0;
520
521 td->w.count = 0;
522
523 for (int i = 0; i < PARTITION_BUFFERS; i++)
524 {
525 for (int j = 0; j < NUM_PARTITIONS; j++)
526 {
527
528 td->tables[i][0][j] = fftwf_malloc(sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_len);
529 td->tables[i][1][j] = fftwf_malloc(sizeof(fftwf_complex) * AUDIO_SCRATCH.complex_len);
530
531 }
532 }
533
534
535 td->stop_requested = false;
536 td->new_probe_ready = false;
537
538 td->t = SDL_CreateThread(hrtf_worker, "HRTF Worker Thread", td);
539 SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW);
540
541}
542
544{
545
546 SDL_LockMutex(td->mutex);
547 td->stop_requested = true;
548 SDL_CondSignal(td->work_cond);
549
550 SDL_UnlockMutex(td->mutex);
551
552 SDL_WaitThread(td->t, NULL);
553
554 SDL_DestroyCond(td->work_cond);
555
556 SDL_DestroyMutex(td->mutex);
557
558 for (int i = 0; i < PARTITION_BUFFERS; i++)
559 {
560 for (int j = 0; j < NUM_PARTITIONS; j++)
561 {
562 fftwf_free(td->tables[i][0][j]);
563 fftwf_free(td->tables[i][1][j]);
564 }
565 }
566
567
568}
#define MAX_SAMPLES
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
Definition camera.c:1004
Camera cam
This is currently the player cam.
Definition main.c:322
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.
Definition main.c:310
vec3 forward
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 SH_MAX
#define MAX_IR_SAMPLES
Maximum IR sample length.
void evaluate_sh(vec3 dir, float SH[SH_MAX])
Evaluates 9 spherical harmonics based on direction.
Definition audio_ir.c:51
void fft_channels(ConvolverNode *node, bool crossfading)
Applies an impulse response filter via discrete convolution.
Definition audio_ir.c:138
void update_player_ir(PartitionTable *OUT)
Rotates neighboring IRs to the Player and then partitions them for convolution in the audio thread.
Definition audio_ir.c:326
void delete_hrtf_worker_thread(HRTFThreadData *td)
Cleans up the worker thread that convolves the audio probes with the HRTF.
Definition audio_ir.c:543
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.
Definition audio_ir.c:111
void allocate_audio_scratch_hrtf(int hrtf_length)
Allocates AUDIO_SCRATCH based on HRTF size.
Definition audio_ir.c:67
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 .
Definition audio_ir.c:234
#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.
Definition audio_ir.c:450
void init_hrtf_worker_thread(HRTFThreadData *td)
Initializes the worker thread that convolves the audio probes with the HRTF.
Definition audio_ir.c:512
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
Definition main.c:73
ConvolverNode CONVOLVER_NODE
Definition main.c:72
int HRTF_LENGTH
Definition main.c:71
HRTFThreadData HRTF_TD
Definition main.c:74
Player player
The Player data.
Definition main.c:287
mat3 rotation_matrix_r(vec3 right, vec3 up, vec3 forward)
Definition mathf.h:88
int round_up_power_of_two(int a, int max)
Definition mathf.h:11
float x
Definition types.h:12
float y
Definition types.h:12
float z
Definition types.h:12
@ X
Definition types.h:31
Definition types.h:20
Definition types.h:12