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
37 #define PATH_MAX MAX_PATH
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)
60 const ALubyte
*azCount
;
61 const ALushort
*evOffset
;
62 const ALshort
*coeffs
;
63 const ALubyte
*delays
;
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
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.
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];
143 // Claculate elevation indices and interpolation factor.
144 CalcEvIndices(Hrtf
, elevation
, evidx
, &mu
[2]);
146 // Calculate azimuth indices and interpolation factor for the first
148 CalcAzIndices(Hrtf
, evidx
[0], azimuth
, azidx
, &mu
[0]);
150 // Calculate the first set of linear HRIR indices for left and right
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
159 CalcAzIndices(Hrtf
, evidx
[1], azimuth
, azidx
, &mu
[1]);
161 // Calculate the second set of linear HRIR indices for left and right
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.
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
;
213 for(i
= 0;i
< Hrtf
->irSize
;i
++)
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];
237 // Claculate elevation indices and interpolation factor.
238 CalcEvIndices(Hrtf
, elevation
, evidx
, &mu
[2]);
240 // Calculate azimuth indices and interpolation factor for the first
242 CalcAzIndices(Hrtf
, evidx
[0], azimuth
, azidx
, &mu
[0]);
244 // Calculate the first set of linear HRIR indices for left and right
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
253 CalcAzIndices(Hrtf
, evidx
[1], azimuth
, azidx
, &mu
[1]);
255 // Calculate the second set of linear HRIR indices for left and right
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
);
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
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
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
);
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
);
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
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;
357 ALubyte
*azCount
= NULL
;
358 ALushort
*evOffset
= NULL
;
359 ALshort
*coeffs
= NULL
;
360 ALubyte
*delays
= NULL
;
365 rate
|= fgetc(f
)<<16;
366 rate
|= fgetc(f
)<<24;
369 irCount
|= fgetc(f
)<<8;
372 irSize
|= fgetc(f
)<<8;
376 if(rate
!= deviceRate
)
378 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
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
);
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
);
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");
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]);
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
);
429 if(irCount
<= evOffset
[i
-1])
431 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
432 i
-1, evOffset
[i
-1], irCount
);
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
);
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");
458 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
460 for(j
= 0;j
< irSize
;j
++)
464 coeff
|= fgetc(f
)<<8;
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
);
480 ERR("Premature end of data\n");
487 Hrtf
= malloc(sizeof(struct Hrtf
));
490 ERR("Out of memory.\n");
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
;
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
;
531 rate
|= fgetc(f
)<<16;
532 rate
|= fgetc(f
)<<24;
538 if(rate
!= deviceRate
)
540 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
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
);
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
);
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");
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
);
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");
603 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
605 for(j
= 0;j
< irSize
;j
++)
609 coeff
|= fgetc(f
)<<8;
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
);
625 ERR("Premature end of data\n");
632 Hrtf
= malloc(sizeof(struct Hrtf
));
635 ERR("Out of memory.\n");
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
;
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
];
676 while(isspace(*fnamelist
) || *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
== ',')
692 int wrote
= snprintf(&fname
[i
], sizeof(fname
)-i
, "%u", deviceRate
);
693 i
+= minu(wrote
, sizeof(fname
)-i
);
696 else if(*next
== '%')
698 if(i
< sizeof(fname
))
703 ERR("Invalid marker '%%%c'\n", *next
);
705 i
= minu(i
, sizeof(fname
)-1);
707 while(i
> 0 && isspace(fname
[i
-1]))
714 TRACE("Loading %s...\n", fname
);
715 f
= OpenDataFile(fname
, "openal/hrtf");
718 ERR("Could not open %s\n", fname
);
722 if(fread(magic
, 1, sizeof(magic
), f
) != sizeof(magic
))
723 ERR("Failed to read header from %s\n", fname
);
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
);
737 ERR("Invalid header in %s: \"%.8s\"\n", fname
, magic
);
745 Hrtf
->next
= LoadedHrtfs
;
747 TRACE("Loaded HRTF support for format: %s %uhz\n",
748 DevFmtChannelsString(DevFmtStereo
), Hrtf
->sampleRate
);
752 ERR("Failed to load %s\n", fname
);
758 const struct Hrtf
*GetHrtf(ALCdevice
*device
)
760 if(device
->FmtChans
== DevFmtStereo
)
762 struct Hrtf
*Hrtf
= LoadedHrtfs
;
765 if(device
->Frequency
== Hrtf
->sampleRate
)
770 Hrtf
= LoadHrtf(device
->Frequency
);
774 ERR("Incompatible format: %s %uhz\n",
775 DevFmtChannelsString(device
->FmtChans
), device
->Frequency
);
779 ALCboolean
FindHrtfFormat(const ALCdevice
*device
, enum DevFmtChannels
*chans
, ALCuint
*srate
)
781 const struct Hrtf
*hrtf
= LoadedHrtfs
;
784 if(device
->Frequency
== hrtf
->sampleRate
)
791 hrtf
= LoadHrtf(device
->Frequency
);
792 if(hrtf
== NULL
) return ALC_FALSE
;
795 *chans
= DevFmtStereo
;
796 *srate
= hrtf
->sampleRate
;
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
);
815 ALuint
GetHrtfIrSize (const struct Hrtf
*Hrtf
)