Remove unused channel enums
[openal-soft.git] / Alc / hrtf.c
blob1e371fa4457b9d1ab7b0b32f8a81fa567b2d3479
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 i = 0;
187 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
188 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
189 coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
190 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
191 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
192 coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
194 for(i = 1;i < Hrtf->irSize;i++)
196 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
197 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
198 coeffs[i][0] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
199 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
200 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
201 coeffs[i][1] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
204 else
206 for(i = 0;i < Hrtf->irSize;i++)
208 coeffs[i][0] = 0.0f;
209 coeffs[i][1] = 0.0f;
214 /* Calculates the moving HRIR target coefficients, target delays, and
215 * stepping values for the given polar elevation and azimuth in radians.
216 * Linear interpolation is used to increase the apparent resolution of the
217 * HRIR data set. The coefficients are also normalized and attenuated by the
218 * specified gain. Stepping resolution and count is determined using the
219 * given delta factor between 0.0 and 1.0.
221 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)
223 ALuint evidx[2], lidx[4], ridx[4];
224 ALfloat mu[3], blend[4];
225 ALfloat left, right;
226 ALfloat step;
227 ALuint i;
229 /* Claculate elevation indices and interpolation factor. */
230 CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
232 for(i = 0;i < 2;i++)
234 ALuint azcount = Hrtf->azCount[evidx[i]];
235 ALuint evoffset = Hrtf->evOffset[evidx[i]];
236 ALuint azidx[2];
238 /* Calculate azimuth indices and interpolation factor for this elevation. */
239 CalcAzIndices(azcount, azimuth, azidx, &mu[i]);
241 /* Calculate a set of linear HRIR indices for left and right channels. */
242 lidx[i*2 + 0] = evoffset + azidx[0];
243 lidx[i*2 + 1] = evoffset + azidx[1];
244 ridx[i*2 + 0] = evoffset + ((azcount-azidx[0]) % azcount);
245 ridx[i*2 + 1] = evoffset + ((azcount-azidx[1]) % azcount);
248 // Calculate the stepping parameters.
249 delta = maxf(floorf(delta*(Hrtf->sampleRate*0.015f) + 0.5f), 1.0f);
250 step = 1.0f / delta;
252 /* Calculate 4 blending weights for 2D bilinear interpolation. */
253 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
254 blend[1] = ( mu[0]) * (1.0f-mu[2]);
255 blend[2] = (1.0f-mu[1]) * ( mu[2]);
256 blend[3] = ( mu[1]) * ( mu[2]);
258 /* Calculate the HRIR delays using linear interpolation. Then calculate
259 * the delay stepping values using the target and previous running
260 * delays.
262 left = (ALfloat)(delays[0] - (delayStep[0] * counter));
263 right = (ALfloat)(delays[1] - (delayStep[1] * counter));
265 delays[0] = fastf2u((Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
266 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3]) *
267 dirfact + 0.5f) << HRTFDELAY_BITS;
268 delays[1] = fastf2u((Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
269 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3]) *
270 dirfact + 0.5f) << HRTFDELAY_BITS;
272 delayStep[0] = fastf2i(step * (delays[0] - left));
273 delayStep[1] = fastf2i(step * (delays[1] - right));
275 /* Calculate the sample offsets for the HRIR indices. */
276 lidx[0] *= Hrtf->irSize;
277 lidx[1] *= Hrtf->irSize;
278 lidx[2] *= Hrtf->irSize;
279 lidx[3] *= Hrtf->irSize;
280 ridx[0] *= Hrtf->irSize;
281 ridx[1] *= Hrtf->irSize;
282 ridx[2] *= Hrtf->irSize;
283 ridx[3] *= Hrtf->irSize;
285 /* Calculate the normalized and attenuated target HRIR coefficients using
286 * linear interpolation when there is enough gain to warrant it. Zero
287 * the target coefficients if gain is too low. Then calculate the
288 * coefficient stepping values using the target and previous running
289 * coefficients.
291 if(gain > 0.0001f)
293 ALfloat c;
295 i = 0;
296 left = coeffs[i][0] - (coeffStep[i][0] * counter);
297 right = coeffs[i][1] - (coeffStep[i][1] * counter);
299 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
300 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
301 coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);;
302 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
303 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
304 coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);;
306 coeffStep[i][0] = step * (coeffs[i][0] - left);
307 coeffStep[i][1] = step * (coeffs[i][1] - right);
309 for(i = 1;i < Hrtf->irSize;i++)
311 left = coeffs[i][0] - (coeffStep[i][0] * counter);
312 right = coeffs[i][1] - (coeffStep[i][1] * counter);
314 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
315 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
316 coeffs[i][0] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);;
317 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
318 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
319 coeffs[i][1] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);;
321 coeffStep[i][0] = step * (coeffs[i][0] - left);
322 coeffStep[i][1] = step * (coeffs[i][1] - right);
325 else
327 for(i = 0;i < Hrtf->irSize;i++)
329 left = coeffs[i][0] - (coeffStep[i][0] * counter);
330 right = coeffs[i][1] - (coeffStep[i][1] * counter);
332 coeffs[i][0] = 0.0f;
333 coeffs[i][1] = 0.0f;
335 coeffStep[i][0] = step * -left;
336 coeffStep[i][1] = step * -right;
340 /* The stepping count is the number of samples necessary for the HRIR to
341 * complete its transition. The mixer will only apply stepping for this
342 * many samples.
344 return fastf2u(delta);
348 static struct Hrtf *LoadHrtf00(FILE *f, ALuint deviceRate)
350 const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
351 struct Hrtf *Hrtf = NULL;
352 ALboolean failed = AL_FALSE;
353 ALuint rate = 0, irCount = 0;
354 ALushort irSize = 0;
355 ALubyte evCount = 0;
356 ALubyte *azCount = NULL;
357 ALushort *evOffset = NULL;
358 ALshort *coeffs = NULL;
359 ALubyte *delays = NULL;
360 ALuint i, j;
362 rate = fgetc(f);
363 rate |= fgetc(f)<<8;
364 rate |= fgetc(f)<<16;
365 rate |= fgetc(f)<<24;
367 irCount = fgetc(f);
368 irCount |= fgetc(f)<<8;
370 irSize = fgetc(f);
371 irSize |= fgetc(f)<<8;
373 evCount = fgetc(f);
375 if(rate != deviceRate)
377 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
378 rate, deviceRate);
379 failed = AL_TRUE;
381 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
383 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
384 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
385 failed = AL_TRUE;
387 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
389 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
390 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
391 failed = AL_TRUE;
394 if(failed)
395 return NULL;
397 azCount = malloc(sizeof(azCount[0])*evCount);
398 evOffset = malloc(sizeof(evOffset[0])*evCount);
399 if(azCount == NULL || evOffset == NULL)
401 ERR("Out of memory.\n");
402 failed = AL_TRUE;
405 if(!failed)
407 evOffset[0] = fgetc(f);
408 evOffset[0] |= fgetc(f)<<8;
409 for(i = 1;i < evCount;i++)
411 evOffset[i] = fgetc(f);
412 evOffset[i] |= fgetc(f)<<8;
413 if(evOffset[i] <= evOffset[i-1])
415 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
416 i, evOffset[i], evOffset[i-1]);
417 failed = AL_TRUE;
420 azCount[i-1] = evOffset[i] - evOffset[i-1];
421 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
423 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
424 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
425 failed = AL_TRUE;
428 if(irCount <= evOffset[i-1])
430 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
431 i-1, evOffset[i-1], irCount);
432 failed = AL_TRUE;
435 azCount[i-1] = irCount - evOffset[i-1];
436 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
438 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
439 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
440 failed = AL_TRUE;
444 if(!failed)
446 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
447 delays = malloc(sizeof(delays[0])*irCount);
448 if(coeffs == NULL || delays == NULL)
450 ERR("Out of memory.\n");
451 failed = AL_TRUE;
455 if(!failed)
457 for(i = 0;i < irCount*irSize;i+=irSize)
459 for(j = 0;j < irSize;j++)
461 ALshort coeff;
462 coeff = fgetc(f);
463 coeff |= fgetc(f)<<8;
464 coeffs[i+j] = coeff;
467 for(i = 0;i < irCount;i++)
469 delays[i] = fgetc(f);
470 if(delays[i] > maxDelay)
472 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
473 failed = AL_TRUE;
477 if(feof(f))
479 ERR("Premature end of data\n");
480 failed = AL_TRUE;
484 if(!failed)
486 Hrtf = malloc(sizeof(struct Hrtf));
487 if(Hrtf == NULL)
489 ERR("Out of memory.\n");
490 failed = AL_TRUE;
494 if(!failed)
496 Hrtf->sampleRate = rate;
497 Hrtf->irSize = irSize;
498 Hrtf->evCount = evCount;
499 Hrtf->azCount = azCount;
500 Hrtf->evOffset = evOffset;
501 Hrtf->coeffs = coeffs;
502 Hrtf->delays = delays;
503 Hrtf->next = NULL;
504 return Hrtf;
507 free(azCount);
508 free(evOffset);
509 free(coeffs);
510 free(delays);
511 return NULL;
515 static struct Hrtf *LoadHrtf01(FILE *f, ALuint deviceRate)
517 const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
518 struct Hrtf *Hrtf = NULL;
519 ALboolean failed = AL_FALSE;
520 ALuint rate = 0, irCount = 0;
521 ALubyte irSize = 0, evCount = 0;
522 ALubyte *azCount = NULL;
523 ALushort *evOffset = NULL;
524 ALshort *coeffs = NULL;
525 ALubyte *delays = NULL;
526 ALuint i, j;
528 rate = fgetc(f);
529 rate |= fgetc(f)<<8;
530 rate |= fgetc(f)<<16;
531 rate |= fgetc(f)<<24;
533 irSize = fgetc(f);
535 evCount = fgetc(f);
537 if(rate != deviceRate)
539 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
540 rate, deviceRate);
541 failed = AL_TRUE;
543 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
545 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
546 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
547 failed = AL_TRUE;
549 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
551 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
552 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
553 failed = AL_TRUE;
556 if(failed)
557 return NULL;
559 azCount = malloc(sizeof(azCount[0])*evCount);
560 evOffset = malloc(sizeof(evOffset[0])*evCount);
561 if(azCount == NULL || evOffset == NULL)
563 ERR("Out of memory.\n");
564 failed = AL_TRUE;
567 if(!failed)
569 for(i = 0;i < evCount;i++)
571 azCount[i] = fgetc(f);
572 if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
574 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
575 i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
576 failed = AL_TRUE;
581 if(!failed)
583 evOffset[0] = 0;
584 irCount = azCount[0];
585 for(i = 1;i < evCount;i++)
587 evOffset[i] = evOffset[i-1] + azCount[i-1];
588 irCount += azCount[i];
591 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
592 delays = malloc(sizeof(delays[0])*irCount);
593 if(coeffs == NULL || delays == NULL)
595 ERR("Out of memory.\n");
596 failed = AL_TRUE;
600 if(!failed)
602 for(i = 0;i < irCount*irSize;i+=irSize)
604 for(j = 0;j < irSize;j++)
606 ALshort coeff;
607 coeff = fgetc(f);
608 coeff |= fgetc(f)<<8;
609 coeffs[i+j] = coeff;
612 for(i = 0;i < irCount;i++)
614 delays[i] = fgetc(f);
615 if(delays[i] > maxDelay)
617 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
618 failed = AL_TRUE;
622 if(feof(f))
624 ERR("Premature end of data\n");
625 failed = AL_TRUE;
629 if(!failed)
631 Hrtf = malloc(sizeof(struct Hrtf));
632 if(Hrtf == NULL)
634 ERR("Out of memory.\n");
635 failed = AL_TRUE;
639 if(!failed)
641 Hrtf->sampleRate = rate;
642 Hrtf->irSize = irSize;
643 Hrtf->evCount = evCount;
644 Hrtf->azCount = azCount;
645 Hrtf->evOffset = evOffset;
646 Hrtf->coeffs = coeffs;
647 Hrtf->delays = delays;
648 Hrtf->next = NULL;
649 return Hrtf;
652 free(azCount);
653 free(evOffset);
654 free(coeffs);
655 free(delays);
656 return NULL;
660 static struct Hrtf *LoadHrtf(ALuint deviceRate)
662 const char *fnamelist = "default-%r.mhr";
664 ConfigValueStr(NULL, "hrtf_tables", &fnamelist);
665 while(*fnamelist != '\0')
667 struct Hrtf *Hrtf = NULL;
668 char fname[PATH_MAX];
669 const char *next;
670 ALchar magic[8];
671 ALuint i;
672 FILE *f;
674 i = 0;
675 while(isspace(*fnamelist) || *fnamelist == ',')
676 fnamelist++;
677 next = fnamelist;
678 while(*(fnamelist=next) != '\0' && *fnamelist != ',')
680 next = strpbrk(fnamelist, "%,");
681 while(fnamelist != next && *fnamelist && i < sizeof(fname))
682 fname[i++] = *(fnamelist++);
684 if(!next || *next == ',')
685 break;
687 /* *next == '%' */
688 next++;
689 if(*next == 'r')
691 int wrote = snprintf(&fname[i], sizeof(fname)-i, "%u", deviceRate);
692 i += minu(wrote, sizeof(fname)-i);
693 next++;
695 else if(*next == '%')
697 if(i < sizeof(fname))
698 fname[i++] = '%';
699 next++;
701 else
702 ERR("Invalid marker '%%%c'\n", *next);
704 i = minu(i, sizeof(fname)-1);
705 fname[i] = '\0';
706 while(i > 0 && isspace(fname[i-1]))
707 i--;
708 fname[i] = '\0';
710 if(fname[0] == '\0')
711 continue;
713 TRACE("Loading %s...\n", fname);
714 f = OpenDataFile(fname, "openal/hrtf");
715 if(f == NULL)
717 ERR("Could not open %s\n", fname);
718 continue;
721 if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
722 ERR("Failed to read header from %s\n", fname);
723 else
725 if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
727 TRACE("Detected data set format v0\n");
728 Hrtf = LoadHrtf00(f, deviceRate);
730 else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
732 TRACE("Detected data set format v1\n");
733 Hrtf = LoadHrtf01(f, deviceRate);
735 else
736 ERR("Invalid header in %s: \"%.8s\"\n", fname, magic);
739 fclose(f);
740 f = NULL;
742 if(Hrtf)
744 Hrtf->next = LoadedHrtfs;
745 LoadedHrtfs = Hrtf;
746 TRACE("Loaded HRTF support for format: %s %uhz\n",
747 DevFmtChannelsString(DevFmtStereo), Hrtf->sampleRate);
748 return Hrtf;
751 ERR("Failed to load %s\n", fname);
754 return NULL;
757 const struct Hrtf *GetHrtf(enum DevFmtChannels chans, ALCuint srate)
759 if(chans == DevFmtStereo)
761 struct Hrtf *Hrtf = LoadedHrtfs;
762 while(Hrtf != NULL)
764 if(srate == Hrtf->sampleRate)
765 return Hrtf;
766 Hrtf = Hrtf->next;
769 Hrtf = LoadHrtf(srate);
770 if(Hrtf != NULL)
771 return Hrtf;
773 ERR("Incompatible format: %s %uhz\n", DevFmtChannelsString(chans), srate);
774 return NULL;
777 ALCboolean FindHrtfFormat(enum DevFmtChannels *chans, ALCuint *srate)
779 const struct Hrtf *hrtf = LoadedHrtfs;
780 while(hrtf != NULL)
782 if(*srate == hrtf->sampleRate)
783 break;
784 hrtf = hrtf->next;
787 if(hrtf == NULL)
789 hrtf = LoadHrtf(*srate);
790 if(hrtf == NULL) return ALC_FALSE;
793 *chans = DevFmtStereo;
794 *srate = hrtf->sampleRate;
795 return ALC_TRUE;
798 void FreeHrtfs(void)
800 struct Hrtf *Hrtf = NULL;
802 while((Hrtf=LoadedHrtfs) != NULL)
804 LoadedHrtfs = Hrtf->next;
805 free((void*)Hrtf->azCount);
806 free((void*)Hrtf->evOffset);
807 free((void*)Hrtf->coeffs);
808 free((void*)Hrtf->delays);
809 free(Hrtf);
813 ALuint GetHrtfIrSize (const struct Hrtf *Hrtf)
815 return Hrtf->irSize;