2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by Chris Robinson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
38 /* Current data set limits defined by the makehrtf utility. */
39 #define MIN_IR_SIZE (8)
40 #define MAX_IR_SIZE (128)
41 #define MOD_IR_SIZE (8)
43 #define MIN_EV_COUNT (5)
44 #define MAX_EV_COUNT (128)
46 #define MIN_AZ_COUNT (1)
47 #define MAX_AZ_COUNT (128)
54 const ALubyte
*azCount
;
55 const ALushort
*evOffset
;
56 const ALshort
*coeffs
;
57 const ALubyte
*delays
;
62 static const ALchar magicMarker00
[8] = "MinPHR00";
63 static const ALchar magicMarker01
[8] = "MinPHR01";
65 /* Define the default HRTF:
66 * ALubyte defaultAzCount [DefaultHrtf.evCount]
67 * ALushort defaultEvOffset [DefaultHrtf.evCount]
68 * ALshort defaultCoeffs [DefaultHrtf.irCount * defaultHrtf.irSize]
69 * ALubyte defaultDelays [DefaultHrtf.irCount]
71 * struct Hrtf DefaultHrtf
73 #include "hrtf_tables.inc"
75 static struct Hrtf
*LoadedHrtfs
= NULL
;
77 /* Calculate the elevation indices given the polar elevation in radians.
78 * This will return two indices between 0 and (Hrtf->evCount - 1) and an
79 * interpolation factor between 0.0 and 1.0.
81 static void CalcEvIndices(const struct Hrtf
*Hrtf
, ALfloat ev
, ALuint
*evidx
, ALfloat
*evmu
)
83 ev
= (F_PI_2
+ ev
) * (Hrtf
->evCount
-1) / F_PI
;
84 evidx
[0] = fastf2u(ev
);
85 evidx
[1] = minu(evidx
[0] + 1, Hrtf
->evCount
-1);
86 *evmu
= ev
- evidx
[0];
89 /* Calculate the azimuth indices given the polar azimuth in radians. This
90 * will return two indices between 0 and (Hrtf->azCount[ei] - 1) and an
91 * interpolation factor between 0.0 and 1.0.
93 static void CalcAzIndices(const struct Hrtf
*Hrtf
, ALuint evidx
, ALfloat az
, ALuint
*azidx
, ALfloat
*azmu
)
95 az
= (F_PI
*2.0f
+ az
) * Hrtf
->azCount
[evidx
] / (F_PI
*2.0f
);
96 azidx
[0] = fastf2u(az
) % Hrtf
->azCount
[evidx
];
97 azidx
[1] = (azidx
[0] + 1) % Hrtf
->azCount
[evidx
];
98 *azmu
= az
- floorf(az
);
101 /* Calculates the normalized HRTF transition factor (delta) from the changes
102 * in gain and listener to source angle between updates. The result is a
103 * normalized delta factor that can be used to calculate moving HRIR stepping
106 ALfloat
CalcHrtfDelta(ALfloat oldGain
, ALfloat newGain
, const ALfloat olddir
[3], const ALfloat newdir
[3])
108 ALfloat gainChange
, angleChange
, change
;
110 // Calculate the normalized dB gain change.
111 newGain
= maxf(newGain
, 0.0001f
);
112 oldGain
= maxf(oldGain
, 0.0001f
);
113 gainChange
= fabsf(log10f(newGain
/ oldGain
) / log10f(0.0001f
));
115 // Calculate the normalized listener to source angle change when there is
116 // enough gain to notice it.
118 if(gainChange
> 0.0001f
|| newGain
> 0.0001f
)
120 // No angle change when the directions are equal or degenerate (when
121 // both have zero length).
122 if(newdir
[0]-olddir
[0] || newdir
[1]-olddir
[1] || newdir
[2]-olddir
[2])
123 angleChange
= acosf(olddir
[0]*newdir
[0] +
124 olddir
[1]*newdir
[1] +
125 olddir
[2]*newdir
[2]) / F_PI
;
129 // Use the largest of the two changes for the delta factor, and apply a
130 // significance shaping function to it.
131 change
= maxf(angleChange
* 25.0f
, gainChange
) * 2.0f
;
132 return minf(change
, 1.0f
);
135 /* Calculates static HRIR coefficients and delays for the given polar
136 * elevation and azimuth in radians. Linear interpolation is used to
137 * increase the apparent resolution of the HRIR data set. The coefficients
138 * are also normalized and attenuated by the specified gain.
140 void GetLerpedHrtfCoeffs(const struct Hrtf
*Hrtf
, ALfloat elevation
, ALfloat azimuth
, ALfloat gain
, ALfloat (*coeffs
)[2], ALuint
*delays
)
142 ALuint evidx
[2], azidx
[2];
143 ALuint lidx
[4], ridx
[4];
144 ALfloat mu
[3], blend
[4];
147 // Claculate elevation indices and interpolation factor.
148 CalcEvIndices(Hrtf
, elevation
, evidx
, &mu
[2]);
150 // Calculate azimuth indices and interpolation factor for the first
152 CalcAzIndices(Hrtf
, evidx
[0], azimuth
, azidx
, &mu
[0]);
154 // Calculate the first set of linear HRIR indices for left and right
156 lidx
[0] = Hrtf
->evOffset
[evidx
[0]] + azidx
[0];
157 lidx
[1] = Hrtf
->evOffset
[evidx
[0]] + azidx
[1];
158 ridx
[0] = Hrtf
->evOffset
[evidx
[0]] + ((Hrtf
->azCount
[evidx
[0]]-azidx
[0]) % Hrtf
->azCount
[evidx
[0]]);
159 ridx
[1] = Hrtf
->evOffset
[evidx
[0]] + ((Hrtf
->azCount
[evidx
[0]]-azidx
[1]) % Hrtf
->azCount
[evidx
[0]]);
161 // Calculate azimuth indices and interpolation factor for the second
163 CalcAzIndices(Hrtf
, evidx
[1], azimuth
, azidx
, &mu
[1]);
165 // Calculate the second set of linear HRIR indices for left and right
167 lidx
[2] = Hrtf
->evOffset
[evidx
[1]] + azidx
[0];
168 lidx
[3] = Hrtf
->evOffset
[evidx
[1]] + azidx
[1];
169 ridx
[2] = Hrtf
->evOffset
[evidx
[1]] + ((Hrtf
->azCount
[evidx
[1]]-azidx
[0]) % Hrtf
->azCount
[evidx
[1]]);
170 ridx
[3] = Hrtf
->evOffset
[evidx
[1]] + ((Hrtf
->azCount
[evidx
[1]]-azidx
[1]) % Hrtf
->azCount
[evidx
[1]]);
172 /* Calculate 4 blending weights for 2D bilinear interpolation. */
173 blend
[0] = (1.0f
-mu
[0]) * (1.0f
-mu
[2]);
174 blend
[1] = ( mu
[0]) * (1.0f
-mu
[2]);
175 blend
[2] = (1.0f
-mu
[1]) * ( mu
[2]);
176 blend
[3] = ( mu
[1]) * ( mu
[2]);
178 /* Calculate the HRIR delays using linear interpolation. */
179 delays
[0] = fastf2u(Hrtf
->delays
[lidx
[0]]*blend
[0] + Hrtf
->delays
[lidx
[1]]*blend
[1] +
180 Hrtf
->delays
[lidx
[2]]*blend
[2] + Hrtf
->delays
[lidx
[3]]*blend
[3] +
181 0.5f
) << HRTFDELAY_BITS
;
182 delays
[1] = fastf2u(Hrtf
->delays
[ridx
[0]]*blend
[0] + Hrtf
->delays
[ridx
[1]]*blend
[1] +
183 Hrtf
->delays
[ridx
[2]]*blend
[2] + Hrtf
->delays
[ridx
[3]]*blend
[3] +
184 0.5f
) << HRTFDELAY_BITS
;
186 /* Calculate the sample offsets for the HRIR indices. */
187 lidx
[0] *= Hrtf
->irSize
;
188 lidx
[1] *= Hrtf
->irSize
;
189 lidx
[2] *= Hrtf
->irSize
;
190 lidx
[3] *= Hrtf
->irSize
;
191 ridx
[0] *= Hrtf
->irSize
;
192 ridx
[1] *= Hrtf
->irSize
;
193 ridx
[2] *= Hrtf
->irSize
;
194 ridx
[3] *= Hrtf
->irSize
;
196 /* Calculate the normalized and attenuated HRIR coefficients using linear
197 * interpolation when there is enough gain to warrant it. Zero the
198 * coefficients if gain is too low.
202 gain
*= 1.0f
/32767.0f
;
203 for(i
= 0;i
< Hrtf
->irSize
;i
++)
205 coeffs
[i
][0] = (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] +
206 Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
207 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] +
208 Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]) * gain
;
209 coeffs
[i
][1] = (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] +
210 Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
211 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] +
212 Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]) * gain
;
217 for(i
= 0;i
< Hrtf
->irSize
;i
++)
225 /* Calculates the moving HRIR target coefficients, target delays, and
226 * stepping values for the given polar elevation and azimuth in radians.
227 * Linear interpolation is used to increase the apparent resolution of the
228 * HRIR data set. The coefficients are also normalized and attenuated by the
229 * specified gain. Stepping resolution and count is determined using the
230 * given delta factor between 0.0 and 1.0.
232 ALuint
GetMovingHrtfCoeffs(const struct Hrtf
*Hrtf
, ALfloat elevation
, ALfloat azimuth
, ALfloat gain
, ALfloat delta
, ALint counter
, ALfloat (*coeffs
)[2], ALuint
*delays
, ALfloat (*coeffStep
)[2], ALint
*delayStep
)
234 ALuint evidx
[2], azidx
[2];
235 ALuint lidx
[4], ridx
[4];
236 ALfloat mu
[3], blend
[4];
241 // Claculate elevation indices and interpolation factor.
242 CalcEvIndices(Hrtf
, elevation
, evidx
, &mu
[2]);
244 // Calculate azimuth indices and interpolation factor for the first
246 CalcAzIndices(Hrtf
, evidx
[0], azimuth
, azidx
, &mu
[0]);
248 // Calculate the first set of linear HRIR indices for left and right
250 lidx
[0] = Hrtf
->evOffset
[evidx
[0]] + azidx
[0];
251 lidx
[1] = Hrtf
->evOffset
[evidx
[0]] + azidx
[1];
252 ridx
[0] = Hrtf
->evOffset
[evidx
[0]] + ((Hrtf
->azCount
[evidx
[0]]-azidx
[0]) % Hrtf
->azCount
[evidx
[0]]);
253 ridx
[1] = Hrtf
->evOffset
[evidx
[0]] + ((Hrtf
->azCount
[evidx
[0]]-azidx
[1]) % Hrtf
->azCount
[evidx
[0]]);
255 // Calculate azimuth indices and interpolation factor for the second
257 CalcAzIndices(Hrtf
, evidx
[1], azimuth
, azidx
, &mu
[1]);
259 // Calculate the second set of linear HRIR indices for left and right
261 lidx
[2] = Hrtf
->evOffset
[evidx
[1]] + azidx
[0];
262 lidx
[3] = Hrtf
->evOffset
[evidx
[1]] + azidx
[1];
263 ridx
[2] = Hrtf
->evOffset
[evidx
[1]] + ((Hrtf
->azCount
[evidx
[1]]-azidx
[0]) % Hrtf
->azCount
[evidx
[1]]);
264 ridx
[3] = Hrtf
->evOffset
[evidx
[1]] + ((Hrtf
->azCount
[evidx
[1]]-azidx
[1]) % Hrtf
->azCount
[evidx
[1]]);
266 // Calculate the stepping parameters.
267 delta
= maxf(floorf(delta
*(Hrtf
->sampleRate
*0.015f
) + 0.5f
), 1.0f
);
270 /* Calculate 4 blending weights for 2D bilinear interpolation. */
271 blend
[0] = (1.0f
-mu
[0]) * (1.0f
-mu
[2]);
272 blend
[1] = ( mu
[0]) * (1.0f
-mu
[2]);
273 blend
[2] = (1.0f
-mu
[1]) * ( mu
[2]);
274 blend
[3] = ( mu
[1]) * ( mu
[2]);
276 /* Calculate the HRIR delays using linear interpolation. Then calculate
277 * the delay stepping values using the target and previous running
280 left
= (ALfloat
)(delays
[0] - (delayStep
[0] * counter
));
281 right
= (ALfloat
)(delays
[1] - (delayStep
[1] * counter
));
283 delays
[0] = fastf2u(Hrtf
->delays
[lidx
[0]]*blend
[0] + Hrtf
->delays
[lidx
[1]]*blend
[1] +
284 Hrtf
->delays
[lidx
[2]]*blend
[2] + Hrtf
->delays
[lidx
[3]]*blend
[3] +
285 0.5f
) << HRTFDELAY_BITS
;
286 delays
[1] = fastf2u(Hrtf
->delays
[ridx
[0]]*blend
[0] + Hrtf
->delays
[ridx
[1]]*blend
[1] +
287 Hrtf
->delays
[ridx
[2]]*blend
[2] + Hrtf
->delays
[ridx
[3]]*blend
[3] +
288 0.5f
) << HRTFDELAY_BITS
;
290 delayStep
[0] = fastf2i(step
* (delays
[0] - left
));
291 delayStep
[1] = fastf2i(step
* (delays
[1] - right
));
293 /* Calculate the sample offsets for the HRIR indices. */
294 lidx
[0] *= Hrtf
->irSize
;
295 lidx
[1] *= Hrtf
->irSize
;
296 lidx
[2] *= Hrtf
->irSize
;
297 lidx
[3] *= Hrtf
->irSize
;
298 ridx
[0] *= Hrtf
->irSize
;
299 ridx
[1] *= Hrtf
->irSize
;
300 ridx
[2] *= Hrtf
->irSize
;
301 ridx
[3] *= Hrtf
->irSize
;
303 /* Calculate the normalized and attenuated target HRIR coefficients using
304 * linear interpolation when there is enough gain to warrant it. Zero
305 * the target coefficients if gain is too low. Then calculate the
306 * coefficient stepping values using the target and previous running
311 gain
*= 1.0f
/32767.0f
;
312 for(i
= 0;i
< HRIR_LENGTH
;i
++)
314 left
= coeffs
[i
][0] - (coeffStep
[i
][0] * counter
);
315 right
= coeffs
[i
][1] - (coeffStep
[i
][1] * counter
);
317 coeffs
[i
][0] = (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] +
318 Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
319 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] +
320 Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]) * gain
;
321 coeffs
[i
][1] = (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] +
322 Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
323 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] +
324 Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]) * gain
;
326 coeffStep
[i
][0] = step
* (coeffs
[i
][0] - left
);
327 coeffStep
[i
][1] = step
* (coeffs
[i
][1] - right
);
332 for(i
= 0;i
< HRIR_LENGTH
;i
++)
334 left
= coeffs
[i
][0] - (coeffStep
[i
][0] * counter
);
335 right
= coeffs
[i
][1] - (coeffStep
[i
][1] * counter
);
340 coeffStep
[i
][0] = step
* -left
;
341 coeffStep
[i
][1] = step
* -right
;
345 /* The stepping count is the number of samples necessary for the HRIR to
346 * complete its transition. The mixer will only apply stepping for this
349 return fastf2u(delta
);
353 static struct Hrtf
*LoadHrtf00(FILE *f
, ALuint deviceRate
)
355 const ALubyte maxDelay
= SRC_HISTORY_LENGTH
-1;
356 struct Hrtf
*Hrtf
= NULL
;
357 ALboolean failed
= AL_FALSE
;
358 ALuint rate
= 0, irCount
= 0;
359 ALushort irSize
= 0, evCount
= 0;
360 ALubyte
*azCount
= NULL
;
361 ALushort
*evOffset
= NULL
;
362 ALshort
*coeffs
= NULL
;
363 ALubyte
*delays
= NULL
;
368 rate
|= fgetc(f
)<<16;
369 rate
|= fgetc(f
)<<24;
372 irCount
|= fgetc(f
)<<8;
375 irSize
|= fgetc(f
)<<8;
379 if(rate
!= deviceRate
)
381 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
385 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
387 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
388 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
391 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
393 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
394 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
401 azCount
= malloc(sizeof(azCount
[0])*evCount
);
402 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
403 if(azCount
== NULL
|| evOffset
== NULL
)
405 ERR("Out of memory.\n");
411 evOffset
[0] = fgetc(f
);
412 evOffset
[0] |= fgetc(f
)<<8;
413 for(i
= 1;i
< evCount
;i
++)
415 evOffset
[i
] = fgetc(f
);
416 evOffset
[i
] |= fgetc(f
)<<8;
417 if(evOffset
[i
] <= evOffset
[i
-1])
419 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
420 i
, evOffset
[i
], evOffset
[i
-1]);
424 azCount
[i
-1] = evOffset
[i
] - evOffset
[i
-1];
425 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
427 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
428 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
432 if(irCount
<= evOffset
[i
-1])
434 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
435 i
-1, evOffset
[i
-1], irCount
);
439 azCount
[i
-1] = irCount
- evOffset
[i
-1];
440 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
442 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
443 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
450 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
451 delays
= malloc(sizeof(delays
[0])*irCount
);
452 if(coeffs
== NULL
|| delays
== NULL
)
454 ERR("Out of memory.\n");
461 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
463 for(j
= 0;j
< irSize
;j
++)
467 coeff
|= fgetc(f
)<<8;
471 for(i
= 0;i
< irCount
;i
++)
473 delays
[i
] = fgetc(f
);
474 if(delays
[i
] > maxDelay
)
476 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
483 ERR("Premature end of data\n");
490 Hrtf
= malloc(sizeof(struct Hrtf
));
493 ERR("Out of memory.\n");
500 Hrtf
->sampleRate
= rate
;
501 Hrtf
->irSize
= irSize
;
502 Hrtf
->evCount
= evCount
;
503 Hrtf
->azCount
= azCount
;
504 Hrtf
->evOffset
= evOffset
;
505 Hrtf
->coeffs
= coeffs
;
506 Hrtf
->delays
= delays
;
519 static struct Hrtf
*LoadHrtf01(FILE *f
, ALuint deviceRate
)
521 const ALubyte maxDelay
= SRC_HISTORY_LENGTH
-1;
522 struct Hrtf
*Hrtf
= NULL
;
523 ALboolean failed
= AL_FALSE
;
524 ALuint rate
= 0, irCount
= 0;
525 ALubyte irSize
= 0, evCount
= 0;
526 ALubyte
*azCount
= NULL
;
527 ALushort
*evOffset
= NULL
;
528 ALshort
*coeffs
= NULL
;
529 ALubyte
*delays
= NULL
;
534 rate
|= fgetc(f
)<<16;
535 rate
|= fgetc(f
)<<24;
541 if(rate
!= deviceRate
)
543 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
547 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
549 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
550 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
553 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
555 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
556 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
563 azCount
= malloc(sizeof(azCount
[0])*evCount
);
564 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
565 if(azCount
== NULL
|| evOffset
== NULL
)
567 ERR("Out of memory.\n");
573 for(i
= 0;i
< evCount
;i
++)
575 azCount
[i
] = fgetc(f
);
576 if(azCount
[i
] < MIN_AZ_COUNT
|| azCount
[i
] > MAX_AZ_COUNT
)
578 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
579 i
, azCount
[i
], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
588 irCount
= azCount
[0];
589 for(i
= 1;i
< evCount
;i
++)
591 evOffset
[i
] = evOffset
[i
-1] + azCount
[i
-1];
592 irCount
+= azCount
[i
];
595 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
596 delays
= malloc(sizeof(delays
[0])*irCount
);
597 if(coeffs
== NULL
|| delays
== NULL
)
599 ERR("Out of memory.\n");
606 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
608 for(j
= 0;j
< irSize
;j
++)
612 coeff
|= fgetc(f
)<<8;
616 for(i
= 0;i
< irCount
;i
++)
618 delays
[i
] = fgetc(f
);
619 if(delays
[i
] > maxDelay
)
621 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
628 ERR("Premature end of data\n");
635 Hrtf
= malloc(sizeof(struct Hrtf
));
638 ERR("Out of memory.\n");
645 Hrtf
->sampleRate
= rate
;
646 Hrtf
->irSize
= irSize
;
647 Hrtf
->evCount
= evCount
;
648 Hrtf
->azCount
= azCount
;
649 Hrtf
->evOffset
= evOffset
;
650 Hrtf
->coeffs
= coeffs
;
651 Hrtf
->delays
= delays
;
664 static struct Hrtf
*LoadHrtf(ALuint deviceRate
)
666 const char *fnamelist
= NULL
;
668 if(!ConfigValueStr(NULL
, "hrtf_tables", &fnamelist
))
670 while(*fnamelist
!= '\0')
672 struct Hrtf
*Hrtf
= NULL
;
673 char fname
[PATH_MAX
];
678 while(isspace(*fnamelist
) || *fnamelist
== ',')
681 while(*fnamelist
!= '\0' && *fnamelist
!= ',')
683 const char *next
= strpbrk(fnamelist
, "%,");
684 while(fnamelist
!= next
&& *fnamelist
&& i
< sizeof(fname
))
685 fname
[i
++] = *(fnamelist
++);
687 if(!next
|| *next
== ',')
694 int wrote
= snprintf(&fname
[i
], sizeof(fname
)-i
, "%u", deviceRate
);
695 i
+= minu(wrote
, sizeof(fname
)-i
);
698 else if(*next
== '%')
700 if(i
< sizeof(fname
))
705 ERR("Invalid marker '%%%c'\n", *next
);
708 i
= minu(i
, sizeof(fname
)-1);
710 while(i
> 0 && isspace(fname
[i
-1]))
717 TRACE("Loading %s...\n", fname
);
718 f
= fopen(fname
, "rb");
721 ERR("Could not open %s\n", fname
);
725 if(fread(magic
, 1, sizeof(magic
), f
) != sizeof(magic
))
726 ERR("Failed to read header from %s\n", fname
);
729 if(memcmp(magic
, magicMarker00
, sizeof(magicMarker00
)) == 0)
731 TRACE("Detected data set format v0\n");
732 Hrtf
= LoadHrtf00(f
, deviceRate
);
734 else if(memcmp(magic
, magicMarker01
, sizeof(magicMarker01
)) == 0)
736 TRACE("Detected data set format v1\n");
737 Hrtf
= LoadHrtf01(f
, deviceRate
);
740 ERR("Invalid header in %s: \"%.8s\"\n", fname
, magic
);
748 Hrtf
->next
= LoadedHrtfs
;
750 TRACE("Loaded HRTF support for format: %s %uhz\n",
751 DevFmtChannelsString(DevFmtStereo
), Hrtf
->sampleRate
);
755 ERR("Failed to load %s\n", fname
);
761 const struct Hrtf
*GetHrtf(ALCdevice
*device
)
763 if(device
->FmtChans
== DevFmtStereo
)
765 struct Hrtf
*Hrtf
= LoadedHrtfs
;
768 if(device
->Frequency
== Hrtf
->sampleRate
)
773 Hrtf
= LoadHrtf(device
->Frequency
);
777 if(device
->Frequency
== DefaultHrtf
.sampleRate
)
780 ERR("Incompatible format: %s %uhz\n",
781 DevFmtChannelsString(device
->FmtChans
), device
->Frequency
);
787 struct Hrtf
*Hrtf
= NULL
;
789 while((Hrtf
=LoadedHrtfs
) != NULL
)
791 LoadedHrtfs
= Hrtf
->next
;
792 free((void*)Hrtf
->azCount
);
793 free((void*)Hrtf
->evOffset
);
794 free((void*)Hrtf
->coeffs
);
795 free((void*)Hrtf
->delays
);
800 ALuint
GetHrtfIrSize (const struct Hrtf
*Hrtf
)