Include strings.h if it exists for strncasecmp
[openal-soft.git] / Alc / hrtf.c
bloba0f8833b19aaae58323b2eb07e908c0c2713be08
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 static struct Hrtf *LoadedHrtfs = NULL;
63 /* Calculate the elevation indices given the polar elevation in radians.
64 * This will return two indices between 0 and (Hrtf->evCount - 1) and an
65 * interpolation factor between 0.0 and 1.0.
67 static void CalcEvIndices(const struct Hrtf *Hrtf, ALfloat ev, ALuint *evidx, ALfloat *evmu)
69 ev = (F_PI_2 + ev) * (Hrtf->evCount-1) / F_PI;
70 evidx[0] = fastf2u(ev);
71 evidx[1] = minu(evidx[0] + 1, Hrtf->evCount-1);
72 *evmu = ev - evidx[0];
75 /* Calculate the azimuth indices given the polar azimuth in radians. This
76 * will return two indices between 0 and (Hrtf->azCount[ei] - 1) and an
77 * interpolation factor between 0.0 and 1.0.
79 static void CalcAzIndices(const struct Hrtf *Hrtf, ALuint evidx, ALfloat az, ALuint *azidx, ALfloat *azmu)
81 az = (F_2PI + az) * Hrtf->azCount[evidx] / (F_2PI);
82 azidx[0] = fastf2u(az) % Hrtf->azCount[evidx];
83 azidx[1] = (azidx[0] + 1) % Hrtf->azCount[evidx];
84 *azmu = az - floorf(az);
87 /* Calculates the normalized HRTF transition factor (delta) from the changes
88 * in gain and listener to source angle between updates. The result is a
89 * normalized delta factor that can be used to calculate moving HRIR stepping
90 * values.
92 ALfloat CalcHrtfDelta(ALfloat oldGain, ALfloat newGain, const ALfloat olddir[3], const ALfloat newdir[3])
94 ALfloat gainChange, angleChange, change;
96 // Calculate the normalized dB gain change.
97 newGain = maxf(newGain, 0.0001f);
98 oldGain = maxf(oldGain, 0.0001f);
99 gainChange = fabsf(log10f(newGain / oldGain) / log10f(0.0001f));
101 // Calculate the normalized listener to source angle change when there is
102 // enough gain to notice it.
103 angleChange = 0.0f;
104 if(gainChange > 0.0001f || newGain > 0.0001f)
106 // No angle change when the directions are equal or degenerate (when
107 // both have zero length).
108 if(newdir[0]-olddir[0] || newdir[1]-olddir[1] || newdir[2]-olddir[2])
109 angleChange = acosf(olddir[0]*newdir[0] +
110 olddir[1]*newdir[1] +
111 olddir[2]*newdir[2]) / F_PI;
115 // Use the largest of the two changes for the delta factor, and apply a
116 // significance shaping function to it.
117 change = maxf(angleChange * 25.0f, gainChange) * 2.0f;
118 return minf(change, 1.0f);
121 /* Calculates static HRIR coefficients and delays for the given polar
122 * elevation and azimuth in radians. Linear interpolation is used to
123 * increase the apparent resolution of the HRIR data set. The coefficients
124 * are also normalized and attenuated by the specified gain.
126 void GetLerpedHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays)
128 ALuint evidx[2], azidx[2];
129 ALuint lidx[4], ridx[4];
130 ALfloat mu[3], blend[4];
131 ALuint i;
133 // Claculate elevation indices and interpolation factor.
134 CalcEvIndices(Hrtf, elevation, evidx, &mu[2]);
136 // Calculate azimuth indices and interpolation factor for the first
137 // elevation.
138 CalcAzIndices(Hrtf, evidx[0], azimuth, azidx, &mu[0]);
140 // Calculate the first set of linear HRIR indices for left and right
141 // channels.
142 lidx[0] = Hrtf->evOffset[evidx[0]] + azidx[0];
143 lidx[1] = Hrtf->evOffset[evidx[0]] + azidx[1];
144 ridx[0] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[0]) % Hrtf->azCount[evidx[0]]);
145 ridx[1] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[1]) % Hrtf->azCount[evidx[0]]);
147 // Calculate azimuth indices and interpolation factor for the second
148 // elevation.
149 CalcAzIndices(Hrtf, evidx[1], azimuth, azidx, &mu[1]);
151 // Calculate the second set of linear HRIR indices for left and right
152 // channels.
153 lidx[2] = Hrtf->evOffset[evidx[1]] + azidx[0];
154 lidx[3] = Hrtf->evOffset[evidx[1]] + azidx[1];
155 ridx[2] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[0]) % Hrtf->azCount[evidx[1]]);
156 ridx[3] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[1]) % Hrtf->azCount[evidx[1]]);
158 /* Calculate 4 blending weights for 2D bilinear interpolation. */
159 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
160 blend[1] = ( mu[0]) * (1.0f-mu[2]);
161 blend[2] = (1.0f-mu[1]) * ( mu[2]);
162 blend[3] = ( mu[1]) * ( mu[2]);
164 /* Calculate the HRIR delays using linear interpolation. */
165 delays[0] = fastf2u(Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
166 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3] +
167 0.5f) << HRTFDELAY_BITS;
168 delays[1] = fastf2u(Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
169 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3] +
170 0.5f) << HRTFDELAY_BITS;
172 /* Calculate the sample offsets for the HRIR indices. */
173 lidx[0] *= Hrtf->irSize;
174 lidx[1] *= Hrtf->irSize;
175 lidx[2] *= Hrtf->irSize;
176 lidx[3] *= Hrtf->irSize;
177 ridx[0] *= Hrtf->irSize;
178 ridx[1] *= Hrtf->irSize;
179 ridx[2] *= Hrtf->irSize;
180 ridx[3] *= Hrtf->irSize;
182 /* Calculate the normalized and attenuated HRIR coefficients using linear
183 * interpolation when there is enough gain to warrant it. Zero the
184 * coefficients if gain is too low.
186 if(gain > 0.0001f)
188 gain *= 1.0f/32767.0f;
189 for(i = 0;i < Hrtf->irSize;i++)
191 coeffs[i][0] = (Hrtf->coeffs[lidx[0]+i]*blend[0] +
192 Hrtf->coeffs[lidx[1]+i]*blend[1] +
193 Hrtf->coeffs[lidx[2]+i]*blend[2] +
194 Hrtf->coeffs[lidx[3]+i]*blend[3]) * gain;
195 coeffs[i][1] = (Hrtf->coeffs[ridx[0]+i]*blend[0] +
196 Hrtf->coeffs[ridx[1]+i]*blend[1] +
197 Hrtf->coeffs[ridx[2]+i]*blend[2] +
198 Hrtf->coeffs[ridx[3]+i]*blend[3]) * gain;
201 else
203 for(i = 0;i < Hrtf->irSize;i++)
205 coeffs[i][0] = 0.0f;
206 coeffs[i][1] = 0.0f;
211 /* Calculates the moving HRIR target coefficients, target delays, and
212 * stepping values for the given polar elevation and azimuth in radians.
213 * Linear interpolation is used to increase the apparent resolution of the
214 * HRIR data set. The coefficients are also normalized and attenuated by the
215 * specified gain. Stepping resolution and count is determined using the
216 * given delta factor between 0.0 and 1.0.
218 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)
220 ALuint evidx[2], azidx[2];
221 ALuint lidx[4], ridx[4];
222 ALfloat mu[3], blend[4];
223 ALfloat left, right;
224 ALfloat step;
225 ALuint i;
227 // Claculate elevation indices and interpolation factor.
228 CalcEvIndices(Hrtf, elevation, evidx, &mu[2]);
230 // Calculate azimuth indices and interpolation factor for the first
231 // elevation.
232 CalcAzIndices(Hrtf, evidx[0], azimuth, azidx, &mu[0]);
234 // Calculate the first set of linear HRIR indices for left and right
235 // channels.
236 lidx[0] = Hrtf->evOffset[evidx[0]] + azidx[0];
237 lidx[1] = Hrtf->evOffset[evidx[0]] + azidx[1];
238 ridx[0] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[0]) % Hrtf->azCount[evidx[0]]);
239 ridx[1] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[1]) % Hrtf->azCount[evidx[0]]);
241 // Calculate azimuth indices and interpolation factor for the second
242 // elevation.
243 CalcAzIndices(Hrtf, evidx[1], azimuth, azidx, &mu[1]);
245 // Calculate the second set of linear HRIR indices for left and right
246 // channels.
247 lidx[2] = Hrtf->evOffset[evidx[1]] + azidx[0];
248 lidx[3] = Hrtf->evOffset[evidx[1]] + azidx[1];
249 ridx[2] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[0]) % Hrtf->azCount[evidx[1]]);
250 ridx[3] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[1]) % Hrtf->azCount[evidx[1]]);
252 // Calculate the stepping parameters.
253 delta = maxf(floorf(delta*(Hrtf->sampleRate*0.015f) + 0.5f), 1.0f);
254 step = 1.0f / delta;
256 /* Calculate 4 blending weights for 2D bilinear interpolation. */
257 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
258 blend[1] = ( mu[0]) * (1.0f-mu[2]);
259 blend[2] = (1.0f-mu[1]) * ( mu[2]);
260 blend[3] = ( mu[1]) * ( mu[2]);
262 /* Calculate the HRIR delays using linear interpolation. Then calculate
263 * the delay stepping values using the target and previous running
264 * delays.
266 left = (ALfloat)(delays[0] - (delayStep[0] * counter));
267 right = (ALfloat)(delays[1] - (delayStep[1] * counter));
269 delays[0] = fastf2u(Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
270 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3] +
271 0.5f) << HRTFDELAY_BITS;
272 delays[1] = fastf2u(Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
273 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3] +
274 0.5f) << HRTFDELAY_BITS;
276 delayStep[0] = fastf2i(step * (delays[0] - left));
277 delayStep[1] = fastf2i(step * (delays[1] - right));
279 /* Calculate the sample offsets for the HRIR indices. */
280 lidx[0] *= Hrtf->irSize;
281 lidx[1] *= Hrtf->irSize;
282 lidx[2] *= Hrtf->irSize;
283 lidx[3] *= Hrtf->irSize;
284 ridx[0] *= Hrtf->irSize;
285 ridx[1] *= Hrtf->irSize;
286 ridx[2] *= Hrtf->irSize;
287 ridx[3] *= Hrtf->irSize;
289 /* Calculate the normalized and attenuated target HRIR coefficients using
290 * linear interpolation when there is enough gain to warrant it. Zero
291 * the target coefficients if gain is too low. Then calculate the
292 * coefficient stepping values using the target and previous running
293 * coefficients.
295 if(gain > 0.0001f)
297 gain *= 1.0f/32767.0f;
298 for(i = 0;i < HRIR_LENGTH;i++)
300 left = coeffs[i][0] - (coeffStep[i][0] * counter);
301 right = coeffs[i][1] - (coeffStep[i][1] * counter);
303 coeffs[i][0] = (Hrtf->coeffs[lidx[0]+i]*blend[0] +
304 Hrtf->coeffs[lidx[1]+i]*blend[1] +
305 Hrtf->coeffs[lidx[2]+i]*blend[2] +
306 Hrtf->coeffs[lidx[3]+i]*blend[3]) * gain;
307 coeffs[i][1] = (Hrtf->coeffs[ridx[0]+i]*blend[0] +
308 Hrtf->coeffs[ridx[1]+i]*blend[1] +
309 Hrtf->coeffs[ridx[2]+i]*blend[2] +
310 Hrtf->coeffs[ridx[3]+i]*blend[3]) * gain;
312 coeffStep[i][0] = step * (coeffs[i][0] - left);
313 coeffStep[i][1] = step * (coeffs[i][1] - right);
316 else
318 for(i = 0;i < HRIR_LENGTH;i++)
320 left = coeffs[i][0] - (coeffStep[i][0] * counter);
321 right = coeffs[i][1] - (coeffStep[i][1] * counter);
323 coeffs[i][0] = 0.0f;
324 coeffs[i][1] = 0.0f;
326 coeffStep[i][0] = step * -left;
327 coeffStep[i][1] = step * -right;
331 /* The stepping count is the number of samples necessary for the HRIR to
332 * complete its transition. The mixer will only apply stepping for this
333 * many samples.
335 return fastf2u(delta);
339 static struct Hrtf *LoadHrtf00(FILE *f, ALuint deviceRate)
341 const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
342 struct Hrtf *Hrtf = NULL;
343 ALboolean failed = AL_FALSE;
344 ALuint rate = 0, irCount = 0;
345 ALushort irSize = 0;
346 ALubyte evCount = 0;
347 ALubyte *azCount = NULL;
348 ALushort *evOffset = NULL;
349 ALshort *coeffs = NULL;
350 ALubyte *delays = NULL;
351 ALuint i, j;
353 rate = fgetc(f);
354 rate |= fgetc(f)<<8;
355 rate |= fgetc(f)<<16;
356 rate |= fgetc(f)<<24;
358 irCount = fgetc(f);
359 irCount |= fgetc(f)<<8;
361 irSize = fgetc(f);
362 irSize |= fgetc(f)<<8;
364 evCount = fgetc(f);
366 if(rate != deviceRate)
368 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
369 rate, deviceRate);
370 failed = AL_TRUE;
372 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
374 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
375 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
376 failed = AL_TRUE;
378 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
380 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
381 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
382 failed = AL_TRUE;
385 if(failed)
386 return NULL;
388 azCount = malloc(sizeof(azCount[0])*evCount);
389 evOffset = malloc(sizeof(evOffset[0])*evCount);
390 if(azCount == NULL || evOffset == NULL)
392 ERR("Out of memory.\n");
393 failed = AL_TRUE;
396 if(!failed)
398 evOffset[0] = fgetc(f);
399 evOffset[0] |= fgetc(f)<<8;
400 for(i = 1;i < evCount;i++)
402 evOffset[i] = fgetc(f);
403 evOffset[i] |= fgetc(f)<<8;
404 if(evOffset[i] <= evOffset[i-1])
406 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
407 i, evOffset[i], evOffset[i-1]);
408 failed = AL_TRUE;
411 azCount[i-1] = evOffset[i] - evOffset[i-1];
412 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
414 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
415 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
416 failed = AL_TRUE;
419 if(irCount <= evOffset[i-1])
421 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
422 i-1, evOffset[i-1], irCount);
423 failed = AL_TRUE;
426 azCount[i-1] = irCount - evOffset[i-1];
427 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
429 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
430 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
431 failed = AL_TRUE;
435 if(!failed)
437 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
438 delays = malloc(sizeof(delays[0])*irCount);
439 if(coeffs == NULL || delays == NULL)
441 ERR("Out of memory.\n");
442 failed = AL_TRUE;
446 if(!failed)
448 for(i = 0;i < irCount*irSize;i+=irSize)
450 for(j = 0;j < irSize;j++)
452 ALshort coeff;
453 coeff = fgetc(f);
454 coeff |= fgetc(f)<<8;
455 coeffs[i+j] = coeff;
458 for(i = 0;i < irCount;i++)
460 delays[i] = fgetc(f);
461 if(delays[i] > maxDelay)
463 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
464 failed = AL_TRUE;
468 if(feof(f))
470 ERR("Premature end of data\n");
471 failed = AL_TRUE;
475 if(!failed)
477 Hrtf = malloc(sizeof(struct Hrtf));
478 if(Hrtf == NULL)
480 ERR("Out of memory.\n");
481 failed = AL_TRUE;
485 if(!failed)
487 Hrtf->sampleRate = rate;
488 Hrtf->irSize = irSize;
489 Hrtf->evCount = evCount;
490 Hrtf->azCount = azCount;
491 Hrtf->evOffset = evOffset;
492 Hrtf->coeffs = coeffs;
493 Hrtf->delays = delays;
494 Hrtf->next = NULL;
495 return Hrtf;
498 free(azCount);
499 free(evOffset);
500 free(coeffs);
501 free(delays);
502 return NULL;
506 static struct Hrtf *LoadHrtf01(FILE *f, ALuint deviceRate)
508 const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
509 struct Hrtf *Hrtf = NULL;
510 ALboolean failed = AL_FALSE;
511 ALuint rate = 0, irCount = 0;
512 ALubyte irSize = 0, evCount = 0;
513 ALubyte *azCount = NULL;
514 ALushort *evOffset = NULL;
515 ALshort *coeffs = NULL;
516 ALubyte *delays = NULL;
517 ALuint i, j;
519 rate = fgetc(f);
520 rate |= fgetc(f)<<8;
521 rate |= fgetc(f)<<16;
522 rate |= fgetc(f)<<24;
524 irSize = fgetc(f);
526 evCount = fgetc(f);
528 if(rate != deviceRate)
530 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
531 rate, deviceRate);
532 failed = AL_TRUE;
534 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
536 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
537 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
538 failed = AL_TRUE;
540 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
542 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
543 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
544 failed = AL_TRUE;
547 if(failed)
548 return NULL;
550 azCount = malloc(sizeof(azCount[0])*evCount);
551 evOffset = malloc(sizeof(evOffset[0])*evCount);
552 if(azCount == NULL || evOffset == NULL)
554 ERR("Out of memory.\n");
555 failed = AL_TRUE;
558 if(!failed)
560 for(i = 0;i < evCount;i++)
562 azCount[i] = fgetc(f);
563 if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
565 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
566 i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
567 failed = AL_TRUE;
572 if(!failed)
574 evOffset[0] = 0;
575 irCount = azCount[0];
576 for(i = 1;i < evCount;i++)
578 evOffset[i] = evOffset[i-1] + azCount[i-1];
579 irCount += azCount[i];
582 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
583 delays = malloc(sizeof(delays[0])*irCount);
584 if(coeffs == NULL || delays == NULL)
586 ERR("Out of memory.\n");
587 failed = AL_TRUE;
591 if(!failed)
593 for(i = 0;i < irCount*irSize;i+=irSize)
595 for(j = 0;j < irSize;j++)
597 ALshort coeff;
598 coeff = fgetc(f);
599 coeff |= fgetc(f)<<8;
600 coeffs[i+j] = coeff;
603 for(i = 0;i < irCount;i++)
605 delays[i] = fgetc(f);
606 if(delays[i] > maxDelay)
608 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
609 failed = AL_TRUE;
613 if(feof(f))
615 ERR("Premature end of data\n");
616 failed = AL_TRUE;
620 if(!failed)
622 Hrtf = malloc(sizeof(struct Hrtf));
623 if(Hrtf == NULL)
625 ERR("Out of memory.\n");
626 failed = AL_TRUE;
630 if(!failed)
632 Hrtf->sampleRate = rate;
633 Hrtf->irSize = irSize;
634 Hrtf->evCount = evCount;
635 Hrtf->azCount = azCount;
636 Hrtf->evOffset = evOffset;
637 Hrtf->coeffs = coeffs;
638 Hrtf->delays = delays;
639 Hrtf->next = NULL;
640 return Hrtf;
643 free(azCount);
644 free(evOffset);
645 free(coeffs);
646 free(delays);
647 return NULL;
651 static struct Hrtf *LoadHrtf(ALuint deviceRate)
653 const char *fnamelist = "default-%r.mhr";
655 ConfigValueStr(NULL, "hrtf_tables", &fnamelist);
656 while(*fnamelist != '\0')
658 struct Hrtf *Hrtf = NULL;
659 char fname[PATH_MAX];
660 const char *next;
661 ALchar magic[8];
662 ALuint i;
663 FILE *f;
665 i = 0;
666 while(isspace(*fnamelist) || *fnamelist == ',')
667 fnamelist++;
668 next = fnamelist;
669 while(*(fnamelist=next) != '\0' && *fnamelist != ',')
671 next = strpbrk(fnamelist, "%,");
672 while(fnamelist != next && *fnamelist && i < sizeof(fname))
673 fname[i++] = *(fnamelist++);
675 if(!next || *next == ',')
676 break;
678 /* *next == '%' */
679 next++;
680 if(*next == 'r')
682 int wrote = snprintf(&fname[i], sizeof(fname)-i, "%u", deviceRate);
683 i += minu(wrote, sizeof(fname)-i);
684 next++;
686 else if(*next == '%')
688 if(i < sizeof(fname))
689 fname[i++] = '%';
690 next++;
692 else
693 ERR("Invalid marker '%%%c'\n", *next);
695 i = minu(i, sizeof(fname)-1);
696 fname[i] = '\0';
697 while(i > 0 && isspace(fname[i-1]))
698 i--;
699 fname[i] = '\0';
701 if(fname[0] == '\0')
702 continue;
704 TRACE("Loading %s...\n", fname);
705 f = OpenDataFile(fname, "openal/hrtf");
706 if(f == NULL)
708 ERR("Could not open %s\n", fname);
709 continue;
712 if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
713 ERR("Failed to read header from %s\n", fname);
714 else
716 if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
718 TRACE("Detected data set format v0\n");
719 Hrtf = LoadHrtf00(f, deviceRate);
721 else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
723 TRACE("Detected data set format v1\n");
724 Hrtf = LoadHrtf01(f, deviceRate);
726 else
727 ERR("Invalid header in %s: \"%.8s\"\n", fname, magic);
730 fclose(f);
731 f = NULL;
733 if(Hrtf)
735 Hrtf->next = LoadedHrtfs;
736 LoadedHrtfs = Hrtf;
737 TRACE("Loaded HRTF support for format: %s %uhz\n",
738 DevFmtChannelsString(DevFmtStereo), Hrtf->sampleRate);
739 return Hrtf;
742 ERR("Failed to load %s\n", fname);
745 return NULL;
748 const struct Hrtf *GetHrtf(ALCdevice *device)
750 if(device->FmtChans == DevFmtStereo)
752 struct Hrtf *Hrtf = LoadedHrtfs;
753 while(Hrtf != NULL)
755 if(device->Frequency == Hrtf->sampleRate)
756 return Hrtf;
757 Hrtf = Hrtf->next;
760 Hrtf = LoadHrtf(device->Frequency);
761 if(Hrtf != NULL)
762 return Hrtf;
764 ERR("Incompatible format: %s %uhz\n",
765 DevFmtChannelsString(device->FmtChans), device->Frequency);
766 return NULL;
769 ALCboolean FindHrtfFormat(const ALCdevice *device, enum DevFmtChannels *chans, ALCuint *srate)
771 const struct Hrtf *hrtf = LoadedHrtfs;
772 while(hrtf != NULL)
774 if(device->Frequency == hrtf->sampleRate)
775 break;
776 hrtf = hrtf->next;
779 if(hrtf == NULL)
781 hrtf = LoadHrtf(device->Frequency);
782 if(hrtf == NULL) return ALC_FALSE;
785 *chans = DevFmtStereo;
786 *srate = hrtf->sampleRate;
787 return ALC_TRUE;
790 void FreeHrtfs(void)
792 struct Hrtf *Hrtf = NULL;
794 while((Hrtf=LoadedHrtfs) != NULL)
796 LoadedHrtfs = Hrtf->next;
797 free((void*)Hrtf->azCount);
798 free((void*)Hrtf->evOffset);
799 free((void*)Hrtf->coeffs);
800 free((void*)Hrtf->delays);
801 free(Hrtf);
805 ALuint GetHrtfIrSize (const struct Hrtf *Hrtf)
807 return Hrtf->irSize;