Add back the missing PATH_MAX fallback
[openal-soft.git] / Alc / hrtf.c
blobdc91e474e46d5dd8e5b454bb0b223d71a87ff250
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>
25 #include <limits.h>
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #include "alMain.h"
30 #include "alSource.h"
31 #include "alu.h"
32 #include "hrtf.h"
35 #ifndef PATH_MAX
36 #ifdef MAX_PATH
37 #define PATH_MAX MAX_PATH
38 #else
39 #define PATH_MAX 4096
40 #endif
41 #endif
44 /* Current data set limits defined by the makehrtf utility. */
45 #define MIN_IR_SIZE (8)
46 #define MAX_IR_SIZE (128)
47 #define MOD_IR_SIZE (8)
49 #define MIN_EV_COUNT (5)
50 #define MAX_EV_COUNT (128)
52 #define MIN_AZ_COUNT (1)
53 #define MAX_AZ_COUNT (128)
55 struct Hrtf {
56 ALuint sampleRate;
57 ALuint irSize;
58 ALubyte evCount;
60 const ALubyte *azCount;
61 const ALushort *evOffset;
62 const ALshort *coeffs;
63 const ALubyte *delays;
65 struct Hrtf *next;
68 static const ALchar magicMarker00[8] = "MinPHR00";
69 static const ALchar magicMarker01[8] = "MinPHR01";
71 static struct Hrtf *LoadedHrtfs = NULL;
73 /* Calculate the elevation indices given the polar elevation in radians.
74 * This will return two indices between 0 and (Hrtf->evCount - 1) and an
75 * interpolation factor between 0.0 and 1.0.
77 static void CalcEvIndices(const struct Hrtf *Hrtf, ALfloat ev, ALuint *evidx, ALfloat *evmu)
79 ev = (F_PI_2 + ev) * (Hrtf->evCount-1) / F_PI;
80 evidx[0] = fastf2u(ev);
81 evidx[1] = minu(evidx[0] + 1, Hrtf->evCount-1);
82 *evmu = ev - evidx[0];
85 /* Calculate the azimuth indices given the polar azimuth in radians. This
86 * will return two indices between 0 and (Hrtf->azCount[ei] - 1) and an
87 * interpolation factor between 0.0 and 1.0.
89 static void CalcAzIndices(const struct Hrtf *Hrtf, ALuint evidx, ALfloat az, ALuint *azidx, ALfloat *azmu)
91 az = (F_2PI + az) * Hrtf->azCount[evidx] / (F_2PI);
92 azidx[0] = fastf2u(az) % Hrtf->azCount[evidx];
93 azidx[1] = (azidx[0] + 1) % Hrtf->azCount[evidx];
94 *azmu = az - floorf(az);
97 /* Calculates the normalized HRTF transition factor (delta) from the changes
98 * in gain and listener to source angle between updates. The result is a
99 * normalized delta factor that can be used to calculate moving HRIR stepping
100 * values.
102 ALfloat CalcHrtfDelta(ALfloat oldGain, ALfloat newGain, const ALfloat olddir[3], const ALfloat newdir[3])
104 ALfloat gainChange, angleChange, change;
106 // Calculate the normalized dB gain change.
107 newGain = maxf(newGain, 0.0001f);
108 oldGain = maxf(oldGain, 0.0001f);
109 gainChange = fabsf(log10f(newGain / oldGain) / log10f(0.0001f));
111 // Calculate the normalized listener to source angle change when there is
112 // enough gain to notice it.
113 angleChange = 0.0f;
114 if(gainChange > 0.0001f || newGain > 0.0001f)
116 // No angle change when the directions are equal or degenerate (when
117 // both have zero length).
118 if(newdir[0]-olddir[0] || newdir[1]-olddir[1] || newdir[2]-olddir[2])
119 angleChange = acosf(olddir[0]*newdir[0] +
120 olddir[1]*newdir[1] +
121 olddir[2]*newdir[2]) / F_PI;
125 // Use the largest of the two changes for the delta factor, and apply a
126 // significance shaping function to it.
127 change = maxf(angleChange * 25.0f, gainChange) * 2.0f;
128 return minf(change, 1.0f);
131 /* Calculates static HRIR coefficients and delays for the given polar
132 * elevation and azimuth in radians. Linear interpolation is used to
133 * increase the apparent resolution of the HRIR data set. The coefficients
134 * are also normalized and attenuated by the specified gain.
136 void GetLerpedHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays)
138 ALuint evidx[2], azidx[2];
139 ALuint lidx[4], ridx[4];
140 ALfloat mu[3], blend[4];
141 ALuint i;
143 // Claculate elevation indices and interpolation factor.
144 CalcEvIndices(Hrtf, elevation, evidx, &mu[2]);
146 // Calculate azimuth indices and interpolation factor for the first
147 // elevation.
148 CalcAzIndices(Hrtf, evidx[0], azimuth, azidx, &mu[0]);
150 // Calculate the first set of linear HRIR indices for left and right
151 // channels.
152 lidx[0] = Hrtf->evOffset[evidx[0]] + azidx[0];
153 lidx[1] = Hrtf->evOffset[evidx[0]] + azidx[1];
154 ridx[0] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[0]) % Hrtf->azCount[evidx[0]]);
155 ridx[1] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[1]) % Hrtf->azCount[evidx[0]]);
157 // Calculate azimuth indices and interpolation factor for the second
158 // elevation.
159 CalcAzIndices(Hrtf, evidx[1], azimuth, azidx, &mu[1]);
161 // Calculate the second set of linear HRIR indices for left and right
162 // channels.
163 lidx[2] = Hrtf->evOffset[evidx[1]] + azidx[0];
164 lidx[3] = Hrtf->evOffset[evidx[1]] + azidx[1];
165 ridx[2] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[0]) % Hrtf->azCount[evidx[1]]);
166 ridx[3] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[1]) % Hrtf->azCount[evidx[1]]);
168 /* Calculate 4 blending weights for 2D bilinear interpolation. */
169 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
170 blend[1] = ( mu[0]) * (1.0f-mu[2]);
171 blend[2] = (1.0f-mu[1]) * ( mu[2]);
172 blend[3] = ( mu[1]) * ( mu[2]);
174 /* Calculate the HRIR delays using linear interpolation. */
175 delays[0] = fastf2u(Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
176 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3] +
177 0.5f) << HRTFDELAY_BITS;
178 delays[1] = fastf2u(Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
179 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3] +
180 0.5f) << HRTFDELAY_BITS;
182 /* Calculate the sample offsets for the HRIR indices. */
183 lidx[0] *= Hrtf->irSize;
184 lidx[1] *= Hrtf->irSize;
185 lidx[2] *= Hrtf->irSize;
186 lidx[3] *= Hrtf->irSize;
187 ridx[0] *= Hrtf->irSize;
188 ridx[1] *= Hrtf->irSize;
189 ridx[2] *= Hrtf->irSize;
190 ridx[3] *= Hrtf->irSize;
192 /* Calculate the normalized and attenuated HRIR coefficients using linear
193 * interpolation when there is enough gain to warrant it. Zero the
194 * coefficients if gain is too low.
196 if(gain > 0.0001f)
198 gain *= 1.0f/32767.0f;
199 for(i = 0;i < Hrtf->irSize;i++)
201 coeffs[i][0] = (Hrtf->coeffs[lidx[0]+i]*blend[0] +
202 Hrtf->coeffs[lidx[1]+i]*blend[1] +
203 Hrtf->coeffs[lidx[2]+i]*blend[2] +
204 Hrtf->coeffs[lidx[3]+i]*blend[3]) * gain;
205 coeffs[i][1] = (Hrtf->coeffs[ridx[0]+i]*blend[0] +
206 Hrtf->coeffs[ridx[1]+i]*blend[1] +
207 Hrtf->coeffs[ridx[2]+i]*blend[2] +
208 Hrtf->coeffs[ridx[3]+i]*blend[3]) * gain;
211 else
213 for(i = 0;i < Hrtf->irSize;i++)
215 coeffs[i][0] = 0.0f;
216 coeffs[i][1] = 0.0f;
221 /* Calculates the moving HRIR target coefficients, target delays, and
222 * stepping values for the given polar elevation and azimuth in radians.
223 * Linear interpolation is used to increase the apparent resolution of the
224 * HRIR data set. The coefficients are also normalized and attenuated by the
225 * specified gain. Stepping resolution and count is determined using the
226 * given delta factor between 0.0 and 1.0.
228 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)
230 ALuint evidx[2], azidx[2];
231 ALuint lidx[4], ridx[4];
232 ALfloat mu[3], blend[4];
233 ALfloat left, right;
234 ALfloat step;
235 ALuint i;
237 // Claculate elevation indices and interpolation factor.
238 CalcEvIndices(Hrtf, elevation, evidx, &mu[2]);
240 // Calculate azimuth indices and interpolation factor for the first
241 // elevation.
242 CalcAzIndices(Hrtf, evidx[0], azimuth, azidx, &mu[0]);
244 // Calculate the first set of linear HRIR indices for left and right
245 // channels.
246 lidx[0] = Hrtf->evOffset[evidx[0]] + azidx[0];
247 lidx[1] = Hrtf->evOffset[evidx[0]] + azidx[1];
248 ridx[0] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[0]) % Hrtf->azCount[evidx[0]]);
249 ridx[1] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[1]) % Hrtf->azCount[evidx[0]]);
251 // Calculate azimuth indices and interpolation factor for the second
252 // elevation.
253 CalcAzIndices(Hrtf, evidx[1], azimuth, azidx, &mu[1]);
255 // Calculate the second set of linear HRIR indices for left and right
256 // channels.
257 lidx[2] = Hrtf->evOffset[evidx[1]] + azidx[0];
258 lidx[3] = Hrtf->evOffset[evidx[1]] + azidx[1];
259 ridx[2] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[0]) % Hrtf->azCount[evidx[1]]);
260 ridx[3] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[1]) % Hrtf->azCount[evidx[1]]);
262 // Calculate the stepping parameters.
263 delta = maxf(floorf(delta*(Hrtf->sampleRate*0.015f) + 0.5f), 1.0f);
264 step = 1.0f / delta;
266 /* Calculate 4 blending weights for 2D bilinear interpolation. */
267 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
268 blend[1] = ( mu[0]) * (1.0f-mu[2]);
269 blend[2] = (1.0f-mu[1]) * ( mu[2]);
270 blend[3] = ( mu[1]) * ( mu[2]);
272 /* Calculate the HRIR delays using linear interpolation. Then calculate
273 * the delay stepping values using the target and previous running
274 * delays.
276 left = (ALfloat)(delays[0] - (delayStep[0] * counter));
277 right = (ALfloat)(delays[1] - (delayStep[1] * counter));
279 delays[0] = fastf2u(Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
280 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3] +
281 0.5f) << HRTFDELAY_BITS;
282 delays[1] = fastf2u(Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
283 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3] +
284 0.5f) << HRTFDELAY_BITS;
286 delayStep[0] = fastf2i(step * (delays[0] - left));
287 delayStep[1] = fastf2i(step * (delays[1] - right));
289 /* Calculate the sample offsets for the HRIR indices. */
290 lidx[0] *= Hrtf->irSize;
291 lidx[1] *= Hrtf->irSize;
292 lidx[2] *= Hrtf->irSize;
293 lidx[3] *= Hrtf->irSize;
294 ridx[0] *= Hrtf->irSize;
295 ridx[1] *= Hrtf->irSize;
296 ridx[2] *= Hrtf->irSize;
297 ridx[3] *= Hrtf->irSize;
299 /* Calculate the normalized and attenuated target HRIR coefficients using
300 * linear interpolation when there is enough gain to warrant it. Zero
301 * the target coefficients if gain is too low. Then calculate the
302 * coefficient stepping values using the target and previous running
303 * coefficients.
305 if(gain > 0.0001f)
307 gain *= 1.0f/32767.0f;
308 for(i = 0;i < HRIR_LENGTH;i++)
310 left = coeffs[i][0] - (coeffStep[i][0] * counter);
311 right = coeffs[i][1] - (coeffStep[i][1] * counter);
313 coeffs[i][0] = (Hrtf->coeffs[lidx[0]+i]*blend[0] +
314 Hrtf->coeffs[lidx[1]+i]*blend[1] +
315 Hrtf->coeffs[lidx[2]+i]*blend[2] +
316 Hrtf->coeffs[lidx[3]+i]*blend[3]) * gain;
317 coeffs[i][1] = (Hrtf->coeffs[ridx[0]+i]*blend[0] +
318 Hrtf->coeffs[ridx[1]+i]*blend[1] +
319 Hrtf->coeffs[ridx[2]+i]*blend[2] +
320 Hrtf->coeffs[ridx[3]+i]*blend[3]) * gain;
322 coeffStep[i][0] = step * (coeffs[i][0] - left);
323 coeffStep[i][1] = step * (coeffs[i][1] - right);
326 else
328 for(i = 0;i < HRIR_LENGTH;i++)
330 left = coeffs[i][0] - (coeffStep[i][0] * counter);
331 right = coeffs[i][1] - (coeffStep[i][1] * counter);
333 coeffs[i][0] = 0.0f;
334 coeffs[i][1] = 0.0f;
336 coeffStep[i][0] = step * -left;
337 coeffStep[i][1] = step * -right;
341 /* The stepping count is the number of samples necessary for the HRIR to
342 * complete its transition. The mixer will only apply stepping for this
343 * many samples.
345 return fastf2u(delta);
349 static struct Hrtf *LoadHrtf00(FILE *f, ALuint deviceRate)
351 const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
352 struct Hrtf *Hrtf = NULL;
353 ALboolean failed = AL_FALSE;
354 ALuint rate = 0, irCount = 0;
355 ALushort irSize = 0;
356 ALubyte evCount = 0;
357 ALubyte *azCount = NULL;
358 ALushort *evOffset = NULL;
359 ALshort *coeffs = NULL;
360 ALubyte *delays = NULL;
361 ALuint i, j;
363 rate = fgetc(f);
364 rate |= fgetc(f)<<8;
365 rate |= fgetc(f)<<16;
366 rate |= fgetc(f)<<24;
368 irCount = fgetc(f);
369 irCount |= fgetc(f)<<8;
371 irSize = fgetc(f);
372 irSize |= fgetc(f)<<8;
374 evCount = fgetc(f);
376 if(rate != deviceRate)
378 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
379 rate, deviceRate);
380 failed = AL_TRUE;
382 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
384 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
385 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
386 failed = AL_TRUE;
388 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
390 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
391 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
392 failed = AL_TRUE;
395 if(failed)
396 return NULL;
398 azCount = malloc(sizeof(azCount[0])*evCount);
399 evOffset = malloc(sizeof(evOffset[0])*evCount);
400 if(azCount == NULL || evOffset == NULL)
402 ERR("Out of memory.\n");
403 failed = AL_TRUE;
406 if(!failed)
408 evOffset[0] = fgetc(f);
409 evOffset[0] |= fgetc(f)<<8;
410 for(i = 1;i < evCount;i++)
412 evOffset[i] = fgetc(f);
413 evOffset[i] |= fgetc(f)<<8;
414 if(evOffset[i] <= evOffset[i-1])
416 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
417 i, evOffset[i], evOffset[i-1]);
418 failed = AL_TRUE;
421 azCount[i-1] = evOffset[i] - evOffset[i-1];
422 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
424 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
425 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
426 failed = AL_TRUE;
429 if(irCount <= evOffset[i-1])
431 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
432 i-1, evOffset[i-1], irCount);
433 failed = AL_TRUE;
436 azCount[i-1] = irCount - evOffset[i-1];
437 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
439 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
440 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
441 failed = AL_TRUE;
445 if(!failed)
447 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
448 delays = malloc(sizeof(delays[0])*irCount);
449 if(coeffs == NULL || delays == NULL)
451 ERR("Out of memory.\n");
452 failed = AL_TRUE;
456 if(!failed)
458 for(i = 0;i < irCount*irSize;i+=irSize)
460 for(j = 0;j < irSize;j++)
462 ALshort coeff;
463 coeff = fgetc(f);
464 coeff |= fgetc(f)<<8;
465 coeffs[i+j] = coeff;
468 for(i = 0;i < irCount;i++)
470 delays[i] = fgetc(f);
471 if(delays[i] > maxDelay)
473 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
474 failed = AL_TRUE;
478 if(feof(f))
480 ERR("Premature end of data\n");
481 failed = AL_TRUE;
485 if(!failed)
487 Hrtf = malloc(sizeof(struct Hrtf));
488 if(Hrtf == NULL)
490 ERR("Out of memory.\n");
491 failed = AL_TRUE;
495 if(!failed)
497 Hrtf->sampleRate = rate;
498 Hrtf->irSize = irSize;
499 Hrtf->evCount = evCount;
500 Hrtf->azCount = azCount;
501 Hrtf->evOffset = evOffset;
502 Hrtf->coeffs = coeffs;
503 Hrtf->delays = delays;
504 Hrtf->next = NULL;
505 return Hrtf;
508 free(azCount);
509 free(evOffset);
510 free(coeffs);
511 free(delays);
512 return NULL;
516 static struct Hrtf *LoadHrtf01(FILE *f, ALuint deviceRate)
518 const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
519 struct Hrtf *Hrtf = NULL;
520 ALboolean failed = AL_FALSE;
521 ALuint rate = 0, irCount = 0;
522 ALubyte irSize = 0, evCount = 0;
523 ALubyte *azCount = NULL;
524 ALushort *evOffset = NULL;
525 ALshort *coeffs = NULL;
526 ALubyte *delays = NULL;
527 ALuint i, j;
529 rate = fgetc(f);
530 rate |= fgetc(f)<<8;
531 rate |= fgetc(f)<<16;
532 rate |= fgetc(f)<<24;
534 irSize = fgetc(f);
536 evCount = fgetc(f);
538 if(rate != deviceRate)
540 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
541 rate, deviceRate);
542 failed = AL_TRUE;
544 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
546 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
547 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
548 failed = AL_TRUE;
550 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
552 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
553 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
554 failed = AL_TRUE;
557 if(failed)
558 return NULL;
560 azCount = malloc(sizeof(azCount[0])*evCount);
561 evOffset = malloc(sizeof(evOffset[0])*evCount);
562 if(azCount == NULL || evOffset == NULL)
564 ERR("Out of memory.\n");
565 failed = AL_TRUE;
568 if(!failed)
570 for(i = 0;i < evCount;i++)
572 azCount[i] = fgetc(f);
573 if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
575 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
576 i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
577 failed = AL_TRUE;
582 if(!failed)
584 evOffset[0] = 0;
585 irCount = azCount[0];
586 for(i = 1;i < evCount;i++)
588 evOffset[i] = evOffset[i-1] + azCount[i-1];
589 irCount += azCount[i];
592 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
593 delays = malloc(sizeof(delays[0])*irCount);
594 if(coeffs == NULL || delays == NULL)
596 ERR("Out of memory.\n");
597 failed = AL_TRUE;
601 if(!failed)
603 for(i = 0;i < irCount*irSize;i+=irSize)
605 for(j = 0;j < irSize;j++)
607 ALshort coeff;
608 coeff = fgetc(f);
609 coeff |= fgetc(f)<<8;
610 coeffs[i+j] = coeff;
613 for(i = 0;i < irCount;i++)
615 delays[i] = fgetc(f);
616 if(delays[i] > maxDelay)
618 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
619 failed = AL_TRUE;
623 if(feof(f))
625 ERR("Premature end of data\n");
626 failed = AL_TRUE;
630 if(!failed)
632 Hrtf = malloc(sizeof(struct Hrtf));
633 if(Hrtf == NULL)
635 ERR("Out of memory.\n");
636 failed = AL_TRUE;
640 if(!failed)
642 Hrtf->sampleRate = rate;
643 Hrtf->irSize = irSize;
644 Hrtf->evCount = evCount;
645 Hrtf->azCount = azCount;
646 Hrtf->evOffset = evOffset;
647 Hrtf->coeffs = coeffs;
648 Hrtf->delays = delays;
649 Hrtf->next = NULL;
650 return Hrtf;
653 free(azCount);
654 free(evOffset);
655 free(coeffs);
656 free(delays);
657 return NULL;
661 static struct Hrtf *LoadHrtf(ALuint deviceRate)
663 const char *fnamelist = "default-%r.mhr";
665 ConfigValueStr(NULL, "hrtf_tables", &fnamelist);
666 while(*fnamelist != '\0')
668 struct Hrtf *Hrtf = NULL;
669 char fname[PATH_MAX];
670 const char *next;
671 ALchar magic[8];
672 ALuint i;
673 FILE *f;
675 i = 0;
676 while(isspace(*fnamelist) || *fnamelist == ',')
677 fnamelist++;
678 next = fnamelist;
679 while(*(fnamelist=next) != '\0' && *fnamelist != ',')
681 next = strpbrk(fnamelist, "%,");
682 while(fnamelist != next && *fnamelist && i < sizeof(fname))
683 fname[i++] = *(fnamelist++);
685 if(!next || *next == ',')
686 break;
688 /* *next == '%' */
689 next++;
690 if(*next == 'r')
692 int wrote = snprintf(&fname[i], sizeof(fname)-i, "%u", deviceRate);
693 i += minu(wrote, sizeof(fname)-i);
694 next++;
696 else if(*next == '%')
698 if(i < sizeof(fname))
699 fname[i++] = '%';
700 next++;
702 else
703 ERR("Invalid marker '%%%c'\n", *next);
705 i = minu(i, sizeof(fname)-1);
706 fname[i] = '\0';
707 while(i > 0 && isspace(fname[i-1]))
708 i--;
709 fname[i] = '\0';
711 if(fname[0] == '\0')
712 continue;
714 TRACE("Loading %s...\n", fname);
715 f = OpenDataFile(fname, "openal/hrtf");
716 if(f == NULL)
718 ERR("Could not open %s\n", fname);
719 continue;
722 if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
723 ERR("Failed to read header from %s\n", fname);
724 else
726 if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
728 TRACE("Detected data set format v0\n");
729 Hrtf = LoadHrtf00(f, deviceRate);
731 else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
733 TRACE("Detected data set format v1\n");
734 Hrtf = LoadHrtf01(f, deviceRate);
736 else
737 ERR("Invalid header in %s: \"%.8s\"\n", fname, magic);
740 fclose(f);
741 f = NULL;
743 if(Hrtf)
745 Hrtf->next = LoadedHrtfs;
746 LoadedHrtfs = Hrtf;
747 TRACE("Loaded HRTF support for format: %s %uhz\n",
748 DevFmtChannelsString(DevFmtStereo), Hrtf->sampleRate);
749 return Hrtf;
752 ERR("Failed to load %s\n", fname);
755 return NULL;
758 const struct Hrtf *GetHrtf(ALCdevice *device)
760 if(device->FmtChans == DevFmtStereo)
762 struct Hrtf *Hrtf = LoadedHrtfs;
763 while(Hrtf != NULL)
765 if(device->Frequency == Hrtf->sampleRate)
766 return Hrtf;
767 Hrtf = Hrtf->next;
770 Hrtf = LoadHrtf(device->Frequency);
771 if(Hrtf != NULL)
772 return Hrtf;
774 ERR("Incompatible format: %s %uhz\n",
775 DevFmtChannelsString(device->FmtChans), device->Frequency);
776 return NULL;
779 ALCboolean FindHrtfFormat(const ALCdevice *device, enum DevFmtChannels *chans, ALCuint *srate)
781 const struct Hrtf *hrtf = LoadedHrtfs;
782 while(hrtf != NULL)
784 if(device->Frequency == hrtf->sampleRate)
785 break;
786 hrtf = hrtf->next;
789 if(hrtf == NULL)
791 hrtf = LoadHrtf(device->Frequency);
792 if(hrtf == NULL) return ALC_FALSE;
795 *chans = DevFmtStereo;
796 *srate = hrtf->sampleRate;
797 return ALC_TRUE;
800 void FreeHrtfs(void)
802 struct Hrtf *Hrtf = NULL;
804 while((Hrtf=LoadedHrtfs) != NULL)
806 LoadedHrtfs = Hrtf->next;
807 free((void*)Hrtf->azCount);
808 free((void*)Hrtf->evOffset);
809 free((void*)Hrtf->coeffs);
810 free((void*)Hrtf->delays);
811 free(Hrtf);
815 ALuint GetHrtfIrSize (const struct Hrtf *Hrtf)
817 return Hrtf->irSize;