Add a cast for MSVC
[openal-soft.git] / Alc / hrtf.c
blob9f1386aa07b19aeaa02bdde06723da7ffc317875
1 /**
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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <ctype.h>
26 #include "AL/al.h"
27 #include "AL/alc.h"
28 #include "alMain.h"
29 #include "alSource.h"
30 #include "alu.h"
31 #include "hrtf.h"
34 /* Current data set limits defined by the makehrtf utility. */
35 #define MIN_IR_SIZE (8)
36 #define MAX_IR_SIZE (128)
37 #define MOD_IR_SIZE (8)
39 #define MIN_EV_COUNT (5)
40 #define MAX_EV_COUNT (128)
42 #define MIN_AZ_COUNT (1)
43 #define MAX_AZ_COUNT (128)
45 struct Hrtf {
46 ALuint sampleRate;
47 ALuint irSize;
48 ALubyte evCount;
50 const ALubyte *azCount;
51 const ALushort *evOffset;
52 const ALshort *coeffs;
53 const ALubyte *delays;
55 struct Hrtf *next;
58 static const ALchar magicMarker00[8] = "MinPHR00";
59 static const ALchar magicMarker01[8] = "MinPHR01";
61 /* First value for pass-through coefficients (remaining are 0), used for omni-
62 * directional sounds. */
63 static const ALfloat PassthruCoeff = 32767.0f * 0.707106781187f/*sqrt(0.5)*/;
65 static struct Hrtf *LoadedHrtfs = NULL;
67 /* Calculate the elevation indices given the polar elevation in radians.
68 * This will return two indices between 0 and (evcount - 1) and an
69 * interpolation factor between 0.0 and 1.0.
71 static void CalcEvIndices(ALuint evcount, ALfloat ev, ALuint *evidx, ALfloat *evmu)
73 ev = (F_PI_2 + ev) * (evcount-1) / F_PI;
74 evidx[0] = fastf2u(ev);
75 evidx[1] = minu(evidx[0] + 1, evcount-1);
76 *evmu = ev - evidx[0];
79 /* Calculate the azimuth indices given the polar azimuth in radians. This
80 * will return two indices between 0 and (azcount - 1) and an interpolation
81 * factor between 0.0 and 1.0.
83 static void CalcAzIndices(ALuint azcount, ALfloat az, ALuint *azidx, ALfloat *azmu)
85 az = (F_2PI + az) * azcount / (F_2PI);
86 azidx[0] = fastf2u(az) % azcount;
87 azidx[1] = (azidx[0] + 1) % azcount;
88 *azmu = az - floorf(az);
91 /* Calculates the normalized HRTF transition factor (delta) from the changes
92 * in gain and listener to source angle between updates. The result is a
93 * normalized delta factor that can be used to calculate moving HRIR stepping
94 * values.
96 ALfloat CalcHrtfDelta(ALfloat oldGain, ALfloat newGain, const ALfloat olddir[3], const ALfloat newdir[3])
98 ALfloat gainChange, angleChange, change;
100 // Calculate the normalized dB gain change.
101 newGain = maxf(newGain, 0.0001f);
102 oldGain = maxf(oldGain, 0.0001f);
103 gainChange = fabsf(log10f(newGain / oldGain) / log10f(0.0001f));
105 // Calculate the angle change only when there is enough gain to notice it.
106 angleChange = 0.0f;
107 if(gainChange > 0.0001f || newGain > 0.0001f)
109 // No angle change when the directions are equal or degenerate (when
110 // both have zero length).
111 if(newdir[0] != olddir[0] || newdir[1] != olddir[1] || newdir[2] != olddir[2])
113 ALfloat dotp = olddir[0]*newdir[0] + olddir[1]*newdir[1] + olddir[2]*newdir[2];
114 angleChange = acosf(clampf(dotp, -1.0f, 1.0f)) / F_PI;
118 // Use the largest of the two changes for the delta factor, and apply a
119 // significance shaping function to it.
120 change = maxf(angleChange * 25.0f, gainChange) * 2.0f;
121 return minf(change, 1.0f);
124 /* Calculates static HRIR coefficients and delays for the given polar
125 * elevation and azimuth in radians. Linear interpolation is used to
126 * increase the apparent resolution of the HRIR data set. The coefficients
127 * are also normalized and attenuated by the specified gain.
129 void GetLerpedHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat dirfact, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays)
131 ALuint evidx[2], lidx[4], ridx[4];
132 ALfloat mu[3], blend[4];
133 ALuint i;
135 /* Claculate elevation indices and interpolation factor. */
136 CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
138 for(i = 0;i < 2;i++)
140 ALuint azcount = Hrtf->azCount[evidx[i]];
141 ALuint evoffset = Hrtf->evOffset[evidx[i]];
142 ALuint azidx[2];
144 /* Calculate azimuth indices and interpolation factor for this elevation. */
145 CalcAzIndices(azcount, azimuth, azidx, &mu[i]);
147 /* Calculate a set of linear HRIR indices for left and right channels. */
148 lidx[i*2 + 0] = evoffset + azidx[0];
149 lidx[i*2 + 1] = evoffset + azidx[1];
150 ridx[i*2 + 0] = evoffset + ((azcount-azidx[0]) % azcount);
151 ridx[i*2 + 1] = evoffset + ((azcount-azidx[1]) % azcount);
154 /* Calculate 4 blending weights for 2D bilinear interpolation. */
155 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
156 blend[1] = ( mu[0]) * (1.0f-mu[2]);
157 blend[2] = (1.0f-mu[1]) * ( mu[2]);
158 blend[3] = ( mu[1]) * ( mu[2]);
160 /* Calculate the HRIR delays using linear interpolation. */
161 delays[0] = fastf2u((Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
162 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3]) *
163 dirfact + 0.5f) << HRTFDELAY_BITS;
164 delays[1] = fastf2u((Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
165 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3]) *
166 dirfact + 0.5f) << HRTFDELAY_BITS;
168 /* Calculate the sample offsets for the HRIR indices. */
169 lidx[0] *= Hrtf->irSize;
170 lidx[1] *= Hrtf->irSize;
171 lidx[2] *= Hrtf->irSize;
172 lidx[3] *= Hrtf->irSize;
173 ridx[0] *= Hrtf->irSize;
174 ridx[1] *= Hrtf->irSize;
175 ridx[2] *= Hrtf->irSize;
176 ridx[3] *= Hrtf->irSize;
178 /* Calculate the normalized and attenuated HRIR coefficients using linear
179 * interpolation when there is enough gain to warrant it. Zero the
180 * coefficients if gain is too low.
182 if(gain > 0.0001f)
184 ALfloat c;
186 gain *= 1.0f/32767.0f;
188 i = 0;
189 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
190 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
191 coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain;
192 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
193 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
194 coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain;
196 for(i = 1;i < Hrtf->irSize;i++)
198 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
199 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
200 coeffs[i][0] = lerp(0.0f, c, dirfact) * gain;
201 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
202 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
203 coeffs[i][1] = lerp(0.0f, c, dirfact) * gain;
206 else
208 for(i = 0;i < Hrtf->irSize;i++)
210 coeffs[i][0] = 0.0f;
211 coeffs[i][1] = 0.0f;
216 /* Calculates the moving HRIR target coefficients, target delays, and
217 * stepping values for the given polar elevation and azimuth in radians.
218 * Linear interpolation is used to increase the apparent resolution of the
219 * HRIR data set. The coefficients are also normalized and attenuated by the
220 * specified gain. Stepping resolution and count is determined using the
221 * given delta factor between 0.0 and 1.0.
223 ALuint GetMovingHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat dirfact, ALfloat gain, ALfloat delta, ALint counter, ALfloat (*coeffs)[2], ALuint *delays, ALfloat (*coeffStep)[2], ALint *delayStep)
225 ALuint evidx[2], lidx[4], ridx[4];
226 ALfloat mu[3], blend[4];
227 ALfloat left, right;
228 ALfloat step;
229 ALuint i;
231 /* Claculate elevation indices and interpolation factor. */
232 CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
234 for(i = 0;i < 2;i++)
236 ALuint azcount = Hrtf->azCount[evidx[i]];
237 ALuint evoffset = Hrtf->evOffset[evidx[i]];
238 ALuint azidx[2];
240 /* Calculate azimuth indices and interpolation factor for this elevation. */
241 CalcAzIndices(azcount, azimuth, azidx, &mu[i]);
243 /* Calculate a set of linear HRIR indices for left and right channels. */
244 lidx[i*2 + 0] = evoffset + azidx[0];
245 lidx[i*2 + 1] = evoffset + azidx[1];
246 ridx[i*2 + 0] = evoffset + ((azcount-azidx[0]) % azcount);
247 ridx[i*2 + 1] = evoffset + ((azcount-azidx[1]) % azcount);
250 // Calculate the stepping parameters.
251 delta = maxf(floorf(delta*(Hrtf->sampleRate*0.015f) + 0.5f), 1.0f);
252 step = 1.0f / delta;
254 /* Calculate 4 blending weights for 2D bilinear interpolation. */
255 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
256 blend[1] = ( mu[0]) * (1.0f-mu[2]);
257 blend[2] = (1.0f-mu[1]) * ( mu[2]);
258 blend[3] = ( mu[1]) * ( mu[2]);
260 /* Calculate the HRIR delays using linear interpolation. Then calculate
261 * the delay stepping values using the target and previous running
262 * delays.
264 left = (ALfloat)(delays[0] - (delayStep[0] * counter));
265 right = (ALfloat)(delays[1] - (delayStep[1] * counter));
267 delays[0] = fastf2u((Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
268 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3]) *
269 dirfact + 0.5f) << HRTFDELAY_BITS;
270 delays[1] = fastf2u((Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
271 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3]) *
272 dirfact + 0.5f) << HRTFDELAY_BITS;
274 delayStep[0] = fastf2i(step * (delays[0] - left));
275 delayStep[1] = fastf2i(step * (delays[1] - right));
277 /* Calculate the sample offsets for the HRIR indices. */
278 lidx[0] *= Hrtf->irSize;
279 lidx[1] *= Hrtf->irSize;
280 lidx[2] *= Hrtf->irSize;
281 lidx[3] *= Hrtf->irSize;
282 ridx[0] *= Hrtf->irSize;
283 ridx[1] *= Hrtf->irSize;
284 ridx[2] *= Hrtf->irSize;
285 ridx[3] *= Hrtf->irSize;
287 /* Calculate the normalized and attenuated target HRIR coefficients using
288 * linear interpolation when there is enough gain to warrant it. Zero
289 * the target coefficients if gain is too low. Then calculate the
290 * coefficient stepping values using the target and previous running
291 * coefficients.
293 if(gain > 0.0001f)
295 ALfloat c;
297 gain *= 1.0f/32767.0f;
299 i = 0;
300 left = coeffs[i][0] - (coeffStep[i][0] * counter);
301 right = coeffs[i][1] - (coeffStep[i][1] * counter);
303 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
304 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
305 coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain;
306 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
307 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
308 coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain;
310 coeffStep[i][0] = step * (coeffs[i][0] - left);
311 coeffStep[i][1] = step * (coeffs[i][1] - right);
313 for(i = 1;i < Hrtf->irSize;i++)
315 left = coeffs[i][0] - (coeffStep[i][0] * counter);
316 right = coeffs[i][1] - (coeffStep[i][1] * counter);
318 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
319 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
320 coeffs[i][0] = lerp(0.0f, c, dirfact) * gain;
321 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
322 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
323 coeffs[i][1] = lerp(0.0f, c, dirfact) * gain;
325 coeffStep[i][0] = step * (coeffs[i][0] - left);
326 coeffStep[i][1] = step * (coeffs[i][1] - right);
329 else
331 for(i = 0;i < Hrtf->irSize;i++)
333 left = coeffs[i][0] - (coeffStep[i][0] * counter);
334 right = coeffs[i][1] - (coeffStep[i][1] * counter);
336 coeffs[i][0] = 0.0f;
337 coeffs[i][1] = 0.0f;
339 coeffStep[i][0] = step * -left;
340 coeffStep[i][1] = step * -right;
344 /* The stepping count is the number of samples necessary for the HRIR to
345 * complete its transition. The mixer will only apply stepping for this
346 * many samples.
348 return fastf2u(delta);
352 static struct Hrtf *LoadHrtf00(FILE *f, ALuint deviceRate)
354 const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
355 struct Hrtf *Hrtf = NULL;
356 ALboolean failed = AL_FALSE;
357 ALuint rate = 0, irCount = 0;
358 ALushort irSize = 0;
359 ALubyte evCount = 0;
360 ALubyte *azCount = NULL;
361 ALushort *evOffset = NULL;
362 ALshort *coeffs = NULL;
363 ALubyte *delays = NULL;
364 ALuint i, j;
366 rate = fgetc(f);
367 rate |= fgetc(f)<<8;
368 rate |= fgetc(f)<<16;
369 rate |= fgetc(f)<<24;
371 irCount = fgetc(f);
372 irCount |= fgetc(f)<<8;
374 irSize = fgetc(f);
375 irSize |= fgetc(f)<<8;
377 evCount = fgetc(f);
379 if(rate != deviceRate)
381 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
382 rate, deviceRate);
383 failed = AL_TRUE;
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);
389 failed = AL_TRUE;
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);
395 failed = AL_TRUE;
398 if(failed)
399 return NULL;
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");
406 failed = AL_TRUE;
409 if(!failed)
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]);
421 failed = AL_TRUE;
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);
429 failed = AL_TRUE;
432 if(irCount <= evOffset[i-1])
434 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
435 i-1, evOffset[i-1], irCount);
436 failed = AL_TRUE;
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);
444 failed = AL_TRUE;
448 if(!failed)
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");
455 failed = AL_TRUE;
459 if(!failed)
461 for(i = 0;i < irCount*irSize;i+=irSize)
463 for(j = 0;j < irSize;j++)
465 ALshort coeff;
466 coeff = fgetc(f);
467 coeff |= fgetc(f)<<8;
468 coeffs[i+j] = coeff;
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);
477 failed = AL_TRUE;
481 if(feof(f))
483 ERR("Premature end of data\n");
484 failed = AL_TRUE;
488 if(!failed)
490 Hrtf = malloc(sizeof(struct Hrtf));
491 if(Hrtf == NULL)
493 ERR("Out of memory.\n");
494 failed = AL_TRUE;
498 if(!failed)
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;
507 Hrtf->next = NULL;
508 return Hrtf;
511 free(azCount);
512 free(evOffset);
513 free(coeffs);
514 free(delays);
515 return NULL;
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;
530 ALuint i, j;
532 rate = fgetc(f);
533 rate |= fgetc(f)<<8;
534 rate |= fgetc(f)<<16;
535 rate |= fgetc(f)<<24;
537 irSize = fgetc(f);
539 evCount = fgetc(f);
541 if(rate != deviceRate)
543 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
544 rate, deviceRate);
545 failed = AL_TRUE;
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);
551 failed = AL_TRUE;
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);
557 failed = AL_TRUE;
560 if(failed)
561 return NULL;
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");
568 failed = AL_TRUE;
571 if(!failed)
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);
580 failed = AL_TRUE;
585 if(!failed)
587 evOffset[0] = 0;
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");
600 failed = AL_TRUE;
604 if(!failed)
606 for(i = 0;i < irCount*irSize;i+=irSize)
608 for(j = 0;j < irSize;j++)
610 ALshort coeff;
611 coeff = fgetc(f);
612 coeff |= fgetc(f)<<8;
613 coeffs[i+j] = coeff;
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);
622 failed = AL_TRUE;
626 if(feof(f))
628 ERR("Premature end of data\n");
629 failed = AL_TRUE;
633 if(!failed)
635 Hrtf = malloc(sizeof(struct Hrtf));
636 if(Hrtf == NULL)
638 ERR("Out of memory.\n");
639 failed = AL_TRUE;
643 if(!failed)
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;
652 Hrtf->next = NULL;
653 return Hrtf;
656 free(azCount);
657 free(evOffset);
658 free(coeffs);
659 free(delays);
660 return NULL;
664 static struct Hrtf *LoadHrtf(ALuint deviceRate)
666 const char *fnamelist = "default-%r.mhr";
668 ConfigValueStr(NULL, "hrtf_tables", &fnamelist);
669 while(*fnamelist != '\0')
671 struct Hrtf *Hrtf = NULL;
672 char fname[PATH_MAX];
673 const char *next;
674 ALchar magic[8];
675 ALuint i;
676 FILE *f;
678 i = 0;
679 while(isspace(*fnamelist) || *fnamelist == ',')
680 fnamelist++;
681 next = fnamelist;
682 while(*(fnamelist=next) != '\0' && *fnamelist != ',')
684 next = strpbrk(fnamelist, "%,");
685 while(fnamelist != next && *fnamelist && i < sizeof(fname))
686 fname[i++] = *(fnamelist++);
688 if(!next || *next == ',')
689 break;
691 /* *next == '%' */
692 next++;
693 if(*next == 'r')
695 int wrote = snprintf(&fname[i], sizeof(fname)-i, "%u", deviceRate);
696 i += minu(wrote, sizeof(fname)-i);
697 next++;
699 else if(*next == '%')
701 if(i < sizeof(fname))
702 fname[i++] = '%';
703 next++;
705 else
706 ERR("Invalid marker '%%%c'\n", *next);
708 i = minu(i, sizeof(fname)-1);
709 fname[i] = '\0';
710 while(i > 0 && isspace(fname[i-1]))
711 i--;
712 fname[i] = '\0';
714 if(fname[0] == '\0')
715 continue;
717 TRACE("Loading %s...\n", fname);
718 f = OpenDataFile(fname, "openal/hrtf");
719 if(f == NULL)
721 ERR("Could not open %s\n", fname);
722 continue;
725 if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
726 ERR("Failed to read header from %s\n", fname);
727 else
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);
739 else
740 ERR("Invalid header in %s: \"%.8s\"\n", fname, magic);
743 fclose(f);
744 f = NULL;
746 if(Hrtf)
748 Hrtf->next = LoadedHrtfs;
749 LoadedHrtfs = Hrtf;
750 TRACE("Loaded HRTF support for format: %s %uhz\n",
751 DevFmtChannelsString(DevFmtStereo), Hrtf->sampleRate);
752 return Hrtf;
755 ERR("Failed to load %s\n", fname);
758 return NULL;
761 const struct Hrtf *GetHrtf(enum DevFmtChannels chans, ALCuint srate)
763 if(chans == DevFmtStereo)
765 struct Hrtf *Hrtf = LoadedHrtfs;
766 while(Hrtf != NULL)
768 if(srate == Hrtf->sampleRate)
769 return Hrtf;
770 Hrtf = Hrtf->next;
773 Hrtf = LoadHrtf(srate);
774 if(Hrtf != NULL)
775 return Hrtf;
777 ERR("Incompatible format: %s %uhz\n", DevFmtChannelsString(chans), srate);
778 return NULL;
781 ALCboolean FindHrtfFormat(enum DevFmtChannels *chans, ALCuint *srate)
783 const struct Hrtf *hrtf = LoadedHrtfs;
784 while(hrtf != NULL)
786 if(*srate == hrtf->sampleRate)
787 break;
788 hrtf = hrtf->next;
791 if(hrtf == NULL)
793 hrtf = LoadHrtf(*srate);
794 if(hrtf == NULL) return ALC_FALSE;
797 *chans = DevFmtStereo;
798 *srate = hrtf->sampleRate;
799 return ALC_TRUE;
802 void FreeHrtfs(void)
804 struct Hrtf *Hrtf = NULL;
806 while((Hrtf=LoadedHrtfs) != NULL)
808 LoadedHrtfs = Hrtf->next;
809 free((void*)Hrtf->azCount);
810 free((void*)Hrtf->evOffset);
811 free((void*)Hrtf->coeffs);
812 free((void*)Hrtf->delays);
813 free(Hrtf);
817 ALuint GetHrtfIrSize (const struct Hrtf *Hrtf)
819 return Hrtf->irSize;