Update a couple comments
[openal-soft.git] / Alc / hrtf.c
blob13aef6d6fe415498af314379a7465fdbf9342cf2
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 /* Calculates HRTF coefficients for B-Format channels (only up to first-order). */
316 void GetBFormatHrtfCoeffs(const struct Hrtf *Hrtf, const ALuint num_chans, ALfloat (**coeffs_list)[2], ALuint **delay_list)
318 ALuint elev_idx, azi_idx;
319 ALfloat scale;
320 ALuint i, c;
322 assert(num_chans <= 4);
324 for(c = 0;c < num_chans;c++)
326 ALfloat (*coeffs)[2] = coeffs_list[c];
327 ALuint *delay = delay_list[c];
329 for(i = 0;i < Hrtf->irSize;i++)
331 coeffs[i][0] = 0.0f;
332 coeffs[i][1] = 0.0f;
334 delay[0] = 0;
335 delay[1] = 0;
338 /* NOTE: HRTF coefficients are generated by combining all the HRIRs in the
339 * dataset, with each entry scaled according to how much it contributes to
340 * the given B-Format channel based on its direction (including negative
341 * contributions!).
343 scale = 0.0f;
344 for(elev_idx = 0;elev_idx < Hrtf->evCount;elev_idx++)
346 ALfloat elev = (ALfloat)elev_idx/(ALfloat)(Hrtf->evCount-1)*F_PI - F_PI_2;
347 ALuint evoffset = Hrtf->evOffset[elev_idx];
348 ALuint azcount = Hrtf->azCount[elev_idx];
350 scale += (ALfloat)azcount;
352 for(azi_idx = 0;azi_idx < azcount;azi_idx++)
354 ALuint lidx, ridx;
355 ALfloat ambi_coeffs[4];
356 ALfloat az, gain;
357 ALfloat x, y, z;
359 lidx = evoffset + azi_idx;
360 ridx = evoffset + ((azcount-azi_idx) % azcount);
362 az = (ALfloat)azi_idx / (ALfloat)azcount * F_2PI;
363 if(az > F_PI) az -= F_2PI;
365 x = cosf(-az) * cosf(elev);
366 y = sinf(-az) * cosf(elev);
367 z = sinf(elev);
369 ambi_coeffs[0] = 1.4142f;
370 ambi_coeffs[1] = x; /* X */
371 ambi_coeffs[2] = y; /* Y */
372 ambi_coeffs[3] = z; /* Z */
374 for(c = 0;c < num_chans;c++)
376 ALfloat (*coeffs)[2] = coeffs_list[c];
377 ALuint *delay = delay_list[c];
379 /* NOTE: Always include the total delay average since the
380 * channels need to have matching delays. */
381 delay[0] += Hrtf->delays[lidx];
382 delay[1] += Hrtf->delays[ridx];
384 gain = ambi_coeffs[c];
385 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
386 continue;
388 for(i = 0;i < Hrtf->irSize;i++)
390 coeffs[i][0] += Hrtf->coeffs[lidx*Hrtf->irSize + i]*(1.0f/32767.0f) * gain;
391 coeffs[i][1] += Hrtf->coeffs[ridx*Hrtf->irSize + i]*(1.0f/32767.0f) * gain;
397 scale = 1.0f/scale;
399 for(c = 0;c < num_chans;c++)
401 ALfloat (*coeffs)[2] = coeffs_list[c];
402 ALuint *delay = delay_list[c];
404 for(i = 0;i < Hrtf->irSize;i++)
406 coeffs[i][0] *= scale;
407 coeffs[i][1] *= scale;
409 delay[0] = minu((ALuint)((ALfloat)delay[0] * scale), HRTF_HISTORY_LENGTH-1);
410 delay[0] <<= HRTFDELAY_BITS;
411 delay[1] = minu((ALuint)((ALfloat)delay[1] * scale), HRTF_HISTORY_LENGTH-1);
412 delay[1] <<= HRTFDELAY_BITS;
417 static struct Hrtf *LoadHrtf00(FILE *f, ALuint deviceRate)
419 const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
420 struct Hrtf *Hrtf = NULL;
421 ALboolean failed = AL_FALSE;
422 ALuint rate = 0, irCount = 0;
423 ALushort irSize = 0;
424 ALubyte evCount = 0;
425 ALubyte *azCount = NULL;
426 ALushort *evOffset = NULL;
427 ALshort *coeffs = NULL;
428 ALubyte *delays = NULL;
429 ALuint i, j;
431 rate = fgetc(f);
432 rate |= fgetc(f)<<8;
433 rate |= fgetc(f)<<16;
434 rate |= fgetc(f)<<24;
436 irCount = fgetc(f);
437 irCount |= fgetc(f)<<8;
439 irSize = fgetc(f);
440 irSize |= fgetc(f)<<8;
442 evCount = fgetc(f);
444 if(rate != deviceRate)
446 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
447 rate, deviceRate);
448 failed = AL_TRUE;
450 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
452 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
453 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
454 failed = AL_TRUE;
456 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
458 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
459 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
460 failed = AL_TRUE;
463 if(failed)
464 return NULL;
466 azCount = malloc(sizeof(azCount[0])*evCount);
467 evOffset = malloc(sizeof(evOffset[0])*evCount);
468 if(azCount == NULL || evOffset == NULL)
470 ERR("Out of memory.\n");
471 failed = AL_TRUE;
474 if(!failed)
476 evOffset[0] = fgetc(f);
477 evOffset[0] |= fgetc(f)<<8;
478 for(i = 1;i < evCount;i++)
480 evOffset[i] = fgetc(f);
481 evOffset[i] |= fgetc(f)<<8;
482 if(evOffset[i] <= evOffset[i-1])
484 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
485 i, evOffset[i], evOffset[i-1]);
486 failed = AL_TRUE;
489 azCount[i-1] = evOffset[i] - evOffset[i-1];
490 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
492 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
493 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
494 failed = AL_TRUE;
497 if(irCount <= evOffset[i-1])
499 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
500 i-1, evOffset[i-1], irCount);
501 failed = AL_TRUE;
504 azCount[i-1] = irCount - evOffset[i-1];
505 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
507 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
508 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
509 failed = AL_TRUE;
513 if(!failed)
515 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
516 delays = malloc(sizeof(delays[0])*irCount);
517 if(coeffs == NULL || delays == NULL)
519 ERR("Out of memory.\n");
520 failed = AL_TRUE;
524 if(!failed)
526 for(i = 0;i < irCount*irSize;i+=irSize)
528 for(j = 0;j < irSize;j++)
530 ALshort coeff;
531 coeff = fgetc(f);
532 coeff |= fgetc(f)<<8;
533 coeffs[i+j] = coeff;
536 for(i = 0;i < irCount;i++)
538 delays[i] = fgetc(f);
539 if(delays[i] > maxDelay)
541 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
542 failed = AL_TRUE;
546 if(feof(f))
548 ERR("Premature end of data\n");
549 failed = AL_TRUE;
553 if(!failed)
555 Hrtf = malloc(sizeof(struct Hrtf));
556 if(Hrtf == NULL)
558 ERR("Out of memory.\n");
559 failed = AL_TRUE;
563 if(!failed)
565 Hrtf->sampleRate = rate;
566 Hrtf->irSize = irSize;
567 Hrtf->evCount = evCount;
568 Hrtf->azCount = azCount;
569 Hrtf->evOffset = evOffset;
570 Hrtf->coeffs = coeffs;
571 Hrtf->delays = delays;
572 Hrtf->next = NULL;
573 return Hrtf;
576 free(azCount);
577 free(evOffset);
578 free(coeffs);
579 free(delays);
580 return NULL;
584 static struct Hrtf *LoadHrtf01(FILE *f, ALuint deviceRate)
586 const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
587 struct Hrtf *Hrtf = NULL;
588 ALboolean failed = AL_FALSE;
589 ALuint rate = 0, irCount = 0;
590 ALubyte irSize = 0, evCount = 0;
591 ALubyte *azCount = NULL;
592 ALushort *evOffset = NULL;
593 ALshort *coeffs = NULL;
594 ALubyte *delays = NULL;
595 ALuint i, j;
597 rate = fgetc(f);
598 rate |= fgetc(f)<<8;
599 rate |= fgetc(f)<<16;
600 rate |= fgetc(f)<<24;
602 irSize = fgetc(f);
604 evCount = fgetc(f);
606 if(rate != deviceRate)
608 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
609 rate, deviceRate);
610 failed = AL_TRUE;
612 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
614 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
615 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
616 failed = AL_TRUE;
618 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
620 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
621 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
622 failed = AL_TRUE;
625 if(failed)
626 return NULL;
628 azCount = malloc(sizeof(azCount[0])*evCount);
629 evOffset = malloc(sizeof(evOffset[0])*evCount);
630 if(azCount == NULL || evOffset == NULL)
632 ERR("Out of memory.\n");
633 failed = AL_TRUE;
636 if(!failed)
638 for(i = 0;i < evCount;i++)
640 azCount[i] = fgetc(f);
641 if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
643 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
644 i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
645 failed = AL_TRUE;
650 if(!failed)
652 evOffset[0] = 0;
653 irCount = azCount[0];
654 for(i = 1;i < evCount;i++)
656 evOffset[i] = evOffset[i-1] + azCount[i-1];
657 irCount += azCount[i];
660 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
661 delays = malloc(sizeof(delays[0])*irCount);
662 if(coeffs == NULL || delays == NULL)
664 ERR("Out of memory.\n");
665 failed = AL_TRUE;
669 if(!failed)
671 for(i = 0;i < irCount*irSize;i+=irSize)
673 for(j = 0;j < irSize;j++)
675 ALshort coeff;
676 coeff = fgetc(f);
677 coeff |= fgetc(f)<<8;
678 coeffs[i+j] = coeff;
681 for(i = 0;i < irCount;i++)
683 delays[i] = fgetc(f);
684 if(delays[i] > maxDelay)
686 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
687 failed = AL_TRUE;
691 if(feof(f))
693 ERR("Premature end of data\n");
694 failed = AL_TRUE;
698 if(!failed)
700 Hrtf = malloc(sizeof(struct Hrtf));
701 if(Hrtf == NULL)
703 ERR("Out of memory.\n");
704 failed = AL_TRUE;
708 if(!failed)
710 Hrtf->sampleRate = rate;
711 Hrtf->irSize = irSize;
712 Hrtf->evCount = evCount;
713 Hrtf->azCount = azCount;
714 Hrtf->evOffset = evOffset;
715 Hrtf->coeffs = coeffs;
716 Hrtf->delays = delays;
717 Hrtf->next = NULL;
718 return Hrtf;
721 free(azCount);
722 free(evOffset);
723 free(coeffs);
724 free(delays);
725 return NULL;
729 static struct Hrtf *LoadHrtf(ALuint deviceRate)
731 const char *fnamelist = "default-%r.mhr";
733 ConfigValueStr(NULL, "hrtf_tables", &fnamelist);
734 while(*fnamelist != '\0')
736 struct Hrtf *Hrtf = NULL;
737 char fname[PATH_MAX];
738 const char *next;
739 ALchar magic[8];
740 ALuint i;
741 FILE *f;
743 i = 0;
744 while(isspace(*fnamelist) || *fnamelist == ',')
745 fnamelist++;
746 next = fnamelist;
747 while(*(fnamelist=next) != '\0' && *fnamelist != ',')
749 next = strpbrk(fnamelist, "%,");
750 while(fnamelist != next && *fnamelist && i < sizeof(fname))
751 fname[i++] = *(fnamelist++);
753 if(!next || *next == ',')
754 break;
756 /* *next == '%' */
757 next++;
758 if(*next == 'r')
760 int wrote = snprintf(&fname[i], sizeof(fname)-i, "%u", deviceRate);
761 i += minu(wrote, sizeof(fname)-i);
762 next++;
764 else if(*next == '%')
766 if(i < sizeof(fname))
767 fname[i++] = '%';
768 next++;
770 else
771 ERR("Invalid marker '%%%c'\n", *next);
773 i = minu(i, sizeof(fname)-1);
774 fname[i] = '\0';
775 while(i > 0 && isspace(fname[i-1]))
776 i--;
777 fname[i] = '\0';
779 if(fname[0] == '\0')
780 continue;
782 TRACE("Loading %s...\n", fname);
783 f = OpenDataFile(fname, "openal/hrtf");
784 if(f == NULL)
786 ERR("Could not open %s\n", fname);
787 continue;
790 if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
791 ERR("Failed to read header from %s\n", fname);
792 else
794 if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
796 TRACE("Detected data set format v0\n");
797 Hrtf = LoadHrtf00(f, deviceRate);
799 else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
801 TRACE("Detected data set format v1\n");
802 Hrtf = LoadHrtf01(f, deviceRate);
804 else
805 ERR("Invalid header in %s: \"%.8s\"\n", fname, magic);
808 fclose(f);
809 f = NULL;
811 if(Hrtf)
813 Hrtf->next = LoadedHrtfs;
814 LoadedHrtfs = Hrtf;
815 TRACE("Loaded HRTF support for format: %s %uhz\n",
816 DevFmtChannelsString(DevFmtStereo), Hrtf->sampleRate);
817 return Hrtf;
820 ERR("Failed to load %s\n", fname);
823 return NULL;
826 const struct Hrtf *GetHrtf(enum DevFmtChannels chans, ALCuint srate)
828 if(chans == DevFmtStereo)
830 struct Hrtf *Hrtf = LoadedHrtfs;
831 while(Hrtf != NULL)
833 if(srate == Hrtf->sampleRate)
834 return Hrtf;
835 Hrtf = Hrtf->next;
838 Hrtf = LoadHrtf(srate);
839 if(Hrtf != NULL)
840 return Hrtf;
842 ERR("Incompatible format: %s %uhz\n", DevFmtChannelsString(chans), srate);
843 return NULL;
846 ALCboolean FindHrtfFormat(enum DevFmtChannels *chans, ALCuint *srate)
848 const struct Hrtf *hrtf = LoadedHrtfs;
849 while(hrtf != NULL)
851 if(*srate == hrtf->sampleRate)
852 break;
853 hrtf = hrtf->next;
856 if(hrtf == NULL)
858 hrtf = LoadHrtf(*srate);
859 if(hrtf == NULL) return ALC_FALSE;
862 *chans = DevFmtStereo;
863 *srate = hrtf->sampleRate;
864 return ALC_TRUE;
867 void FreeHrtfs(void)
869 struct Hrtf *Hrtf = NULL;
871 while((Hrtf=LoadedHrtfs) != NULL)
873 LoadedHrtfs = Hrtf->next;
874 free((void*)Hrtf->azCount);
875 free((void*)Hrtf->evOffset);
876 free((void*)Hrtf->coeffs);
877 free((void*)Hrtf->delays);
878 free(Hrtf);
882 ALuint GetHrtfIrSize (const struct Hrtf *Hrtf)
884 return Hrtf->irSize;