Do up to 256 samples at a time with multi-step loops
[openal-soft.git] / Alc / hrtf.c
blob54e16cc7870a55bb24f9959a5451cc173dbc9c91
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 static HRIR coefficients and delays for the given polar
92 * elevation and azimuth in radians. Linear interpolation is used to
93 * increase the apparent resolution of the HRIR data set. The coefficients
94 * are also normalized and attenuated by the specified gain.
96 void GetLerpedHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat dirfact, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays)
98 ALuint evidx[2], lidx[4], ridx[4];
99 ALfloat mu[3], blend[4];
100 ALuint i;
102 /* Claculate elevation indices and interpolation factor. */
103 CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
105 for(i = 0;i < 2;i++)
107 ALuint azcount = Hrtf->azCount[evidx[i]];
108 ALuint evoffset = Hrtf->evOffset[evidx[i]];
109 ALuint azidx[2];
111 /* Calculate azimuth indices and interpolation factor for this elevation. */
112 CalcAzIndices(azcount, azimuth, azidx, &mu[i]);
114 /* Calculate a set of linear HRIR indices for left and right channels. */
115 lidx[i*2 + 0] = evoffset + azidx[0];
116 lidx[i*2 + 1] = evoffset + azidx[1];
117 ridx[i*2 + 0] = evoffset + ((azcount-azidx[0]) % azcount);
118 ridx[i*2 + 1] = evoffset + ((azcount-azidx[1]) % azcount);
121 /* Calculate 4 blending weights for 2D bilinear interpolation. */
122 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
123 blend[1] = ( mu[0]) * (1.0f-mu[2]);
124 blend[2] = (1.0f-mu[1]) * ( mu[2]);
125 blend[3] = ( mu[1]) * ( mu[2]);
127 /* Calculate the HRIR delays using linear interpolation. */
128 delays[0] = fastf2u((Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
129 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3]) *
130 dirfact + 0.5f) << HRTFDELAY_BITS;
131 delays[1] = fastf2u((Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
132 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3]) *
133 dirfact + 0.5f) << HRTFDELAY_BITS;
135 /* Calculate the sample offsets for the HRIR indices. */
136 lidx[0] *= Hrtf->irSize;
137 lidx[1] *= Hrtf->irSize;
138 lidx[2] *= Hrtf->irSize;
139 lidx[3] *= Hrtf->irSize;
140 ridx[0] *= Hrtf->irSize;
141 ridx[1] *= Hrtf->irSize;
142 ridx[2] *= Hrtf->irSize;
143 ridx[3] *= Hrtf->irSize;
145 /* Calculate the normalized and attenuated HRIR coefficients using linear
146 * interpolation when there is enough gain to warrant it. Zero the
147 * coefficients if gain is too low.
149 if(gain > 0.0001f)
151 ALfloat c;
153 i = 0;
154 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
155 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
156 coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
157 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
158 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
159 coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
161 for(i = 1;i < Hrtf->irSize;i++)
163 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
164 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
165 coeffs[i][0] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
166 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
167 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
168 coeffs[i][1] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
171 else
173 for(i = 0;i < Hrtf->irSize;i++)
175 coeffs[i][0] = 0.0f;
176 coeffs[i][1] = 0.0f;
181 /* Calculates the moving HRIR target coefficients, target delays, and
182 * stepping values for the given polar elevation and azimuth in radians.
183 * Linear interpolation is used to increase the apparent resolution of the
184 * HRIR data set. The coefficients are also normalized and attenuated by the
185 * specified gain. Stepping resolution and count is determined using the
186 * given delta factor between 0.0 and 1.0.
188 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)
190 ALuint evidx[2], lidx[4], ridx[4];
191 ALfloat mu[3], blend[4];
192 ALfloat left, right;
193 ALfloat steps;
194 ALuint i;
196 /* Claculate elevation indices and interpolation factor. */
197 CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
199 for(i = 0;i < 2;i++)
201 ALuint azcount = Hrtf->azCount[evidx[i]];
202 ALuint evoffset = Hrtf->evOffset[evidx[i]];
203 ALuint azidx[2];
205 /* Calculate azimuth indices and interpolation factor for this elevation. */
206 CalcAzIndices(azcount, azimuth, azidx, &mu[i]);
208 /* Calculate a set of linear HRIR indices for left and right channels. */
209 lidx[i*2 + 0] = evoffset + azidx[0];
210 lidx[i*2 + 1] = evoffset + azidx[1];
211 ridx[i*2 + 0] = evoffset + ((azcount-azidx[0]) % azcount);
212 ridx[i*2 + 1] = evoffset + ((azcount-azidx[1]) % azcount);
215 // Calculate the stepping parameters.
216 steps = maxf(floorf(delta*Hrtf->sampleRate + 0.5f), 1.0f);
217 delta = 1.0f / steps;
219 /* Calculate 4 blending weights for 2D bilinear interpolation. */
220 blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
221 blend[1] = ( mu[0]) * (1.0f-mu[2]);
222 blend[2] = (1.0f-mu[1]) * ( mu[2]);
223 blend[3] = ( mu[1]) * ( mu[2]);
225 /* Calculate the HRIR delays using linear interpolation. Then calculate
226 * the delay stepping values using the target and previous running
227 * delays.
229 left = (ALfloat)(delays[0] - (delayStep[0] * counter));
230 right = (ALfloat)(delays[1] - (delayStep[1] * counter));
232 delays[0] = fastf2u((Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
233 Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3]) *
234 dirfact + 0.5f) << HRTFDELAY_BITS;
235 delays[1] = fastf2u((Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
236 Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3]) *
237 dirfact + 0.5f) << HRTFDELAY_BITS;
239 delayStep[0] = fastf2i(delta * (delays[0] - left));
240 delayStep[1] = fastf2i(delta * (delays[1] - right));
242 /* Calculate the sample offsets for the HRIR indices. */
243 lidx[0] *= Hrtf->irSize;
244 lidx[1] *= Hrtf->irSize;
245 lidx[2] *= Hrtf->irSize;
246 lidx[3] *= Hrtf->irSize;
247 ridx[0] *= Hrtf->irSize;
248 ridx[1] *= Hrtf->irSize;
249 ridx[2] *= Hrtf->irSize;
250 ridx[3] *= Hrtf->irSize;
252 /* Calculate the normalized and attenuated target HRIR coefficients using
253 * linear interpolation when there is enough gain to warrant it. Zero
254 * the target coefficients if gain is too low. Then calculate the
255 * coefficient stepping values using the target and previous running
256 * coefficients.
258 if(gain > 0.0001f)
260 ALfloat c;
262 i = 0;
263 left = coeffs[i][0] - (coeffStep[i][0] * counter);
264 right = coeffs[i][1] - (coeffStep[i][1] * counter);
266 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
267 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
268 coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
269 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
270 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
271 coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
273 coeffStep[i][0] = delta * (coeffs[i][0] - left);
274 coeffStep[i][1] = delta * (coeffs[i][1] - right);
276 for(i = 1;i < Hrtf->irSize;i++)
278 left = coeffs[i][0] - (coeffStep[i][0] * counter);
279 right = coeffs[i][1] - (coeffStep[i][1] * counter);
281 c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
282 Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
283 coeffs[i][0] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
284 c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
285 Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
286 coeffs[i][1] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
288 coeffStep[i][0] = delta * (coeffs[i][0] - left);
289 coeffStep[i][1] = delta * (coeffs[i][1] - right);
292 else
294 for(i = 0;i < Hrtf->irSize;i++)
296 left = coeffs[i][0] - (coeffStep[i][0] * counter);
297 right = coeffs[i][1] - (coeffStep[i][1] * counter);
299 coeffs[i][0] = 0.0f;
300 coeffs[i][1] = 0.0f;
302 coeffStep[i][0] = delta * -left;
303 coeffStep[i][1] = delta * -right;
307 /* The stepping count is the number of samples necessary for the HRIR to
308 * complete its transition. The mixer will only apply stepping for this
309 * many samples.
311 return fastf2u(steps);
315 static struct Hrtf *LoadHrtf00(FILE *f, ALuint deviceRate)
317 const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
318 struct Hrtf *Hrtf = NULL;
319 ALboolean failed = AL_FALSE;
320 ALuint rate = 0, irCount = 0;
321 ALushort irSize = 0;
322 ALubyte evCount = 0;
323 ALubyte *azCount = NULL;
324 ALushort *evOffset = NULL;
325 ALshort *coeffs = NULL;
326 ALubyte *delays = NULL;
327 ALuint i, j;
329 rate = fgetc(f);
330 rate |= fgetc(f)<<8;
331 rate |= fgetc(f)<<16;
332 rate |= fgetc(f)<<24;
334 irCount = fgetc(f);
335 irCount |= fgetc(f)<<8;
337 irSize = fgetc(f);
338 irSize |= fgetc(f)<<8;
340 evCount = fgetc(f);
342 if(rate != deviceRate)
344 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
345 rate, deviceRate);
346 failed = AL_TRUE;
348 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
350 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
351 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
352 failed = AL_TRUE;
354 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
356 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
357 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
358 failed = AL_TRUE;
361 if(failed)
362 return NULL;
364 azCount = malloc(sizeof(azCount[0])*evCount);
365 evOffset = malloc(sizeof(evOffset[0])*evCount);
366 if(azCount == NULL || evOffset == NULL)
368 ERR("Out of memory.\n");
369 failed = AL_TRUE;
372 if(!failed)
374 evOffset[0] = fgetc(f);
375 evOffset[0] |= fgetc(f)<<8;
376 for(i = 1;i < evCount;i++)
378 evOffset[i] = fgetc(f);
379 evOffset[i] |= fgetc(f)<<8;
380 if(evOffset[i] <= evOffset[i-1])
382 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
383 i, evOffset[i], evOffset[i-1]);
384 failed = AL_TRUE;
387 azCount[i-1] = evOffset[i] - evOffset[i-1];
388 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
390 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
391 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
392 failed = AL_TRUE;
395 if(irCount <= evOffset[i-1])
397 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
398 i-1, evOffset[i-1], irCount);
399 failed = AL_TRUE;
402 azCount[i-1] = irCount - evOffset[i-1];
403 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
405 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
406 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
407 failed = AL_TRUE;
411 if(!failed)
413 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
414 delays = malloc(sizeof(delays[0])*irCount);
415 if(coeffs == NULL || delays == NULL)
417 ERR("Out of memory.\n");
418 failed = AL_TRUE;
422 if(!failed)
424 for(i = 0;i < irCount*irSize;i+=irSize)
426 for(j = 0;j < irSize;j++)
428 ALshort coeff;
429 coeff = fgetc(f);
430 coeff |= fgetc(f)<<8;
431 coeffs[i+j] = coeff;
434 for(i = 0;i < irCount;i++)
436 delays[i] = fgetc(f);
437 if(delays[i] > maxDelay)
439 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
440 failed = AL_TRUE;
444 if(feof(f))
446 ERR("Premature end of data\n");
447 failed = AL_TRUE;
451 if(!failed)
453 Hrtf = malloc(sizeof(struct Hrtf));
454 if(Hrtf == NULL)
456 ERR("Out of memory.\n");
457 failed = AL_TRUE;
461 if(!failed)
463 Hrtf->sampleRate = rate;
464 Hrtf->irSize = irSize;
465 Hrtf->evCount = evCount;
466 Hrtf->azCount = azCount;
467 Hrtf->evOffset = evOffset;
468 Hrtf->coeffs = coeffs;
469 Hrtf->delays = delays;
470 Hrtf->next = NULL;
471 return Hrtf;
474 free(azCount);
475 free(evOffset);
476 free(coeffs);
477 free(delays);
478 return NULL;
482 static struct Hrtf *LoadHrtf01(FILE *f, ALuint deviceRate)
484 const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
485 struct Hrtf *Hrtf = NULL;
486 ALboolean failed = AL_FALSE;
487 ALuint rate = 0, irCount = 0;
488 ALubyte irSize = 0, evCount = 0;
489 ALubyte *azCount = NULL;
490 ALushort *evOffset = NULL;
491 ALshort *coeffs = NULL;
492 ALubyte *delays = NULL;
493 ALuint i, j;
495 rate = fgetc(f);
496 rate |= fgetc(f)<<8;
497 rate |= fgetc(f)<<16;
498 rate |= fgetc(f)<<24;
500 irSize = fgetc(f);
502 evCount = fgetc(f);
504 if(rate != deviceRate)
506 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
507 rate, deviceRate);
508 failed = AL_TRUE;
510 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
512 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
513 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
514 failed = AL_TRUE;
516 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
518 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
519 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
520 failed = AL_TRUE;
523 if(failed)
524 return NULL;
526 azCount = malloc(sizeof(azCount[0])*evCount);
527 evOffset = malloc(sizeof(evOffset[0])*evCount);
528 if(azCount == NULL || evOffset == NULL)
530 ERR("Out of memory.\n");
531 failed = AL_TRUE;
534 if(!failed)
536 for(i = 0;i < evCount;i++)
538 azCount[i] = fgetc(f);
539 if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
541 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
542 i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
543 failed = AL_TRUE;
548 if(!failed)
550 evOffset[0] = 0;
551 irCount = azCount[0];
552 for(i = 1;i < evCount;i++)
554 evOffset[i] = evOffset[i-1] + azCount[i-1];
555 irCount += azCount[i];
558 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
559 delays = malloc(sizeof(delays[0])*irCount);
560 if(coeffs == NULL || delays == NULL)
562 ERR("Out of memory.\n");
563 failed = AL_TRUE;
567 if(!failed)
569 for(i = 0;i < irCount*irSize;i+=irSize)
571 for(j = 0;j < irSize;j++)
573 ALshort coeff;
574 coeff = fgetc(f);
575 coeff |= fgetc(f)<<8;
576 coeffs[i+j] = coeff;
579 for(i = 0;i < irCount;i++)
581 delays[i] = fgetc(f);
582 if(delays[i] > maxDelay)
584 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
585 failed = AL_TRUE;
589 if(feof(f))
591 ERR("Premature end of data\n");
592 failed = AL_TRUE;
596 if(!failed)
598 Hrtf = malloc(sizeof(struct Hrtf));
599 if(Hrtf == NULL)
601 ERR("Out of memory.\n");
602 failed = AL_TRUE;
606 if(!failed)
608 Hrtf->sampleRate = rate;
609 Hrtf->irSize = irSize;
610 Hrtf->evCount = evCount;
611 Hrtf->azCount = azCount;
612 Hrtf->evOffset = evOffset;
613 Hrtf->coeffs = coeffs;
614 Hrtf->delays = delays;
615 Hrtf->next = NULL;
616 return Hrtf;
619 free(azCount);
620 free(evOffset);
621 free(coeffs);
622 free(delays);
623 return NULL;
627 static struct Hrtf *LoadHrtf(ALuint deviceRate)
629 const char *fnamelist = "default-%r.mhr";
631 ConfigValueStr(NULL, "hrtf_tables", &fnamelist);
632 while(*fnamelist != '\0')
634 struct Hrtf *Hrtf = NULL;
635 char fname[PATH_MAX];
636 const char *next;
637 ALchar magic[8];
638 ALuint i;
639 FILE *f;
641 i = 0;
642 while(isspace(*fnamelist) || *fnamelist == ',')
643 fnamelist++;
644 next = fnamelist;
645 while(*(fnamelist=next) != '\0' && *fnamelist != ',')
647 next = strpbrk(fnamelist, "%,");
648 while(fnamelist != next && *fnamelist && i < sizeof(fname))
649 fname[i++] = *(fnamelist++);
651 if(!next || *next == ',')
652 break;
654 /* *next == '%' */
655 next++;
656 if(*next == 'r')
658 int wrote = snprintf(&fname[i], sizeof(fname)-i, "%u", deviceRate);
659 i += minu(wrote, sizeof(fname)-i);
660 next++;
662 else if(*next == '%')
664 if(i < sizeof(fname))
665 fname[i++] = '%';
666 next++;
668 else
669 ERR("Invalid marker '%%%c'\n", *next);
671 i = minu(i, sizeof(fname)-1);
672 fname[i] = '\0';
673 while(i > 0 && isspace(fname[i-1]))
674 i--;
675 fname[i] = '\0';
677 if(fname[0] == '\0')
678 continue;
680 TRACE("Loading %s...\n", fname);
681 f = OpenDataFile(fname, "openal/hrtf");
682 if(f == NULL)
684 ERR("Could not open %s\n", fname);
685 continue;
688 if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
689 ERR("Failed to read header from %s\n", fname);
690 else
692 if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
694 TRACE("Detected data set format v0\n");
695 Hrtf = LoadHrtf00(f, deviceRate);
697 else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
699 TRACE("Detected data set format v1\n");
700 Hrtf = LoadHrtf01(f, deviceRate);
702 else
703 ERR("Invalid header in %s: \"%.8s\"\n", fname, magic);
706 fclose(f);
707 f = NULL;
709 if(Hrtf)
711 Hrtf->next = LoadedHrtfs;
712 LoadedHrtfs = Hrtf;
713 TRACE("Loaded HRTF support for format: %s %uhz\n",
714 DevFmtChannelsString(DevFmtStereo), Hrtf->sampleRate);
715 return Hrtf;
718 ERR("Failed to load %s\n", fname);
721 return NULL;
724 const struct Hrtf *GetHrtf(enum DevFmtChannels chans, ALCuint srate)
726 if(chans == DevFmtStereo)
728 struct Hrtf *Hrtf = LoadedHrtfs;
729 while(Hrtf != NULL)
731 if(srate == Hrtf->sampleRate)
732 return Hrtf;
733 Hrtf = Hrtf->next;
736 Hrtf = LoadHrtf(srate);
737 if(Hrtf != NULL)
738 return Hrtf;
740 ERR("Incompatible format: %s %uhz\n", DevFmtChannelsString(chans), srate);
741 return NULL;
744 ALCboolean FindHrtfFormat(enum DevFmtChannels *chans, ALCuint *srate)
746 const struct Hrtf *hrtf = LoadedHrtfs;
747 while(hrtf != NULL)
749 if(*srate == hrtf->sampleRate)
750 break;
751 hrtf = hrtf->next;
754 if(hrtf == NULL)
756 hrtf = LoadHrtf(*srate);
757 if(hrtf == NULL) return ALC_FALSE;
760 *chans = DevFmtStereo;
761 *srate = hrtf->sampleRate;
762 return ALC_TRUE;
765 void FreeHrtfs(void)
767 struct Hrtf *Hrtf = NULL;
769 while((Hrtf=LoadedHrtfs) != NULL)
771 LoadedHrtfs = Hrtf->next;
772 free((void*)Hrtf->azCount);
773 free((void*)Hrtf->evOffset);
774 free((void*)Hrtf->coeffs);
775 free((void*)Hrtf->delays);
776 free(Hrtf);
780 ALuint GetHrtfIrSize (const struct Hrtf *Hrtf)
782 return Hrtf->irSize;