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
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)
50 const ALubyte
*azCount
;
51 const ALushort
*evOffset
;
52 const ALshort
*coeffs
;
53 const ALubyte
*delays
;
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 (evcount - 1) and an
65 * interpolation factor between 0.0 and 1.0.
67 static void CalcEvIndices(ALuint evcount
, ALfloat ev
, ALuint
*evidx
, ALfloat
*evmu
)
69 ev
= (F_PI_2
+ ev
) * (evcount
-1) / F_PI
;
70 evidx
[0] = fastf2u(ev
);
71 evidx
[1] = minu(evidx
[0] + 1, 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 (azcount - 1) and an interpolation
77 * factor between 0.0 and 1.0.
79 static void CalcAzIndices(ALuint azcount
, ALfloat az
, ALuint
*azidx
, ALfloat
*azmu
)
81 az
= (F_2PI
+ az
) * azcount
/ (F_2PI
);
82 azidx
[0] = fastf2u(az
) % azcount
;
83 azidx
[1] = (azidx
[0] + 1) % azcount
;
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
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 angle change only when there is enough gain to notice it.
103 if(gainChange
> 0.0001f
|| newGain
> 0.0001f
)
105 // No angle change when the directions are equal or degenerate (when
106 // both have zero length).
107 if(newdir
[0] != olddir
[0] || newdir
[1] != olddir
[1] || newdir
[2] != olddir
[2])
109 ALfloat dotp
= olddir
[0]*newdir
[0] + olddir
[1]*newdir
[1] + olddir
[2]*newdir
[2];
110 angleChange
= acosf(clampf(dotp
, -1.0f
, 1.0f
)) / F_PI
;
114 // Use the largest of the two changes for the delta factor, and apply a
115 // significance shaping function to it.
116 change
= maxf(angleChange
* 25.0f
, gainChange
) * 2.0f
;
117 return minf(change
, 1.0f
);
120 /* Calculates static HRIR coefficients and delays for the given polar
121 * elevation and azimuth in radians. Linear interpolation is used to
122 * increase the apparent resolution of the HRIR data set. The coefficients
123 * are also normalized and attenuated by the specified gain.
125 void GetLerpedHrtfCoeffs(const struct Hrtf
*Hrtf
, ALfloat elevation
, ALfloat azimuth
, ALfloat dirfact
, ALfloat gain
, ALfloat (*coeffs
)[2], ALuint
*delays
)
127 ALuint evidx
[2], lidx
[4], ridx
[4];
128 ALfloat mu
[3], blend
[4];
131 /* Claculate elevation indices and interpolation factor. */
132 CalcEvIndices(Hrtf
->evCount
, elevation
, evidx
, &mu
[2]);
136 ALuint azcount
= Hrtf
->azCount
[evidx
[i
]];
137 ALuint evoffset
= Hrtf
->evOffset
[evidx
[i
]];
140 /* Calculate azimuth indices and interpolation factor for this elevation. */
141 CalcAzIndices(azcount
, azimuth
, azidx
, &mu
[i
]);
143 /* Calculate a set of linear HRIR indices for left and right channels. */
144 lidx
[i
*2 + 0] = evoffset
+ azidx
[0];
145 lidx
[i
*2 + 1] = evoffset
+ azidx
[1];
146 ridx
[i
*2 + 0] = evoffset
+ ((azcount
-azidx
[0]) % azcount
);
147 ridx
[i
*2 + 1] = evoffset
+ ((azcount
-azidx
[1]) % azcount
);
150 /* Calculate 4 blending weights for 2D bilinear interpolation. */
151 blend
[0] = (1.0f
-mu
[0]) * (1.0f
-mu
[2]);
152 blend
[1] = ( mu
[0]) * (1.0f
-mu
[2]);
153 blend
[2] = (1.0f
-mu
[1]) * ( mu
[2]);
154 blend
[3] = ( mu
[1]) * ( mu
[2]);
156 /* Calculate the HRIR delays using linear interpolation. */
157 delays
[0] = fastf2u((Hrtf
->delays
[lidx
[0]]*blend
[0] + Hrtf
->delays
[lidx
[1]]*blend
[1] +
158 Hrtf
->delays
[lidx
[2]]*blend
[2] + Hrtf
->delays
[lidx
[3]]*blend
[3]) *
159 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
160 delays
[1] = fastf2u((Hrtf
->delays
[ridx
[0]]*blend
[0] + Hrtf
->delays
[ridx
[1]]*blend
[1] +
161 Hrtf
->delays
[ridx
[2]]*blend
[2] + Hrtf
->delays
[ridx
[3]]*blend
[3]) *
162 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
164 /* Calculate the sample offsets for the HRIR indices. */
165 lidx
[0] *= Hrtf
->irSize
;
166 lidx
[1] *= Hrtf
->irSize
;
167 lidx
[2] *= Hrtf
->irSize
;
168 lidx
[3] *= Hrtf
->irSize
;
169 ridx
[0] *= Hrtf
->irSize
;
170 ridx
[1] *= Hrtf
->irSize
;
171 ridx
[2] *= Hrtf
->irSize
;
172 ridx
[3] *= Hrtf
->irSize
;
174 /* Calculate the normalized and attenuated HRIR coefficients using linear
175 * interpolation when there is enough gain to warrant it. Zero the
176 * coefficients if gain is too low.
182 gain
*= 1.0f
/32767.0f
;
185 c
= (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
186 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]);
187 coeffs
[i
][0] = lerp(1.0f
, c
, dirfact
) * gain
;
188 c
= (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
189 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]);
190 coeffs
[i
][1] = lerp(1.0f
, c
, dirfact
) * gain
;
192 for(i
= 1;i
< Hrtf
->irSize
;i
++)
194 c
= (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
195 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]);
196 coeffs
[i
][0] = lerp(0.0f
, c
, dirfact
) * gain
;
197 c
= (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
198 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]);
199 coeffs
[i
][1] = lerp(0.0f
, c
, dirfact
) * gain
;
204 for(i
= 0;i
< Hrtf
->irSize
;i
++)
212 /* Calculates the moving HRIR target coefficients, target delays, and
213 * stepping values for the given polar elevation and azimuth in radians.
214 * Linear interpolation is used to increase the apparent resolution of the
215 * HRIR data set. The coefficients are also normalized and attenuated by the
216 * specified gain. Stepping resolution and count is determined using the
217 * given delta factor between 0.0 and 1.0.
219 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
)
221 ALuint evidx
[2], lidx
[4], ridx
[4];
222 ALfloat mu
[3], blend
[4];
227 /* Claculate elevation indices and interpolation factor. */
228 CalcEvIndices(Hrtf
->evCount
, elevation
, evidx
, &mu
[2]);
232 ALuint azcount
= Hrtf
->azCount
[evidx
[i
]];
233 ALuint evoffset
= Hrtf
->evOffset
[evidx
[i
]];
236 /* Calculate azimuth indices and interpolation factor for this elevation. */
237 CalcAzIndices(azcount
, azimuth
, azidx
, &mu
[i
]);
239 /* Calculate a set of linear HRIR indices for left and right channels. */
240 lidx
[i
*2 + 0] = evoffset
+ azidx
[0];
241 lidx
[i
*2 + 1] = evoffset
+ azidx
[1];
242 ridx
[i
*2 + 0] = evoffset
+ ((azcount
-azidx
[0]) % azcount
);
243 ridx
[i
*2 + 1] = evoffset
+ ((azcount
-azidx
[1]) % azcount
);
246 // Calculate the stepping parameters.
247 delta
= maxf(floorf(delta
*(Hrtf
->sampleRate
*0.015f
) + 0.5f
), 1.0f
);
250 /* Calculate 4 blending weights for 2D bilinear interpolation. */
251 blend
[0] = (1.0f
-mu
[0]) * (1.0f
-mu
[2]);
252 blend
[1] = ( mu
[0]) * (1.0f
-mu
[2]);
253 blend
[2] = (1.0f
-mu
[1]) * ( mu
[2]);
254 blend
[3] = ( mu
[1]) * ( mu
[2]);
256 /* Calculate the HRIR delays using linear interpolation. Then calculate
257 * the delay stepping values using the target and previous running
260 left
= (ALfloat
)(delays
[0] - (delayStep
[0] * counter
));
261 right
= (ALfloat
)(delays
[1] - (delayStep
[1] * counter
));
263 delays
[0] = fastf2u((Hrtf
->delays
[lidx
[0]]*blend
[0] + Hrtf
->delays
[lidx
[1]]*blend
[1] +
264 Hrtf
->delays
[lidx
[2]]*blend
[2] + Hrtf
->delays
[lidx
[3]]*blend
[3]) *
265 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
266 delays
[1] = fastf2u((Hrtf
->delays
[ridx
[0]]*blend
[0] + Hrtf
->delays
[ridx
[1]]*blend
[1] +
267 Hrtf
->delays
[ridx
[2]]*blend
[2] + Hrtf
->delays
[ridx
[3]]*blend
[3]) *
268 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
270 delayStep
[0] = fastf2i(step
* (delays
[0] - left
));
271 delayStep
[1] = fastf2i(step
* (delays
[1] - right
));
273 /* Calculate the sample offsets for the HRIR indices. */
274 lidx
[0] *= Hrtf
->irSize
;
275 lidx
[1] *= Hrtf
->irSize
;
276 lidx
[2] *= Hrtf
->irSize
;
277 lidx
[3] *= Hrtf
->irSize
;
278 ridx
[0] *= Hrtf
->irSize
;
279 ridx
[1] *= Hrtf
->irSize
;
280 ridx
[2] *= Hrtf
->irSize
;
281 ridx
[3] *= Hrtf
->irSize
;
283 /* Calculate the normalized and attenuated target HRIR coefficients using
284 * linear interpolation when there is enough gain to warrant it. Zero
285 * the target coefficients if gain is too low. Then calculate the
286 * coefficient stepping values using the target and previous running
293 gain
*= 1.0f
/32767.0f
;
296 left
= coeffs
[i
][0] - (coeffStep
[i
][0] * counter
);
297 right
= coeffs
[i
][1] - (coeffStep
[i
][1] * counter
);
299 c
= (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
300 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]);
301 coeffs
[i
][0] = lerp(1.0f
, c
, dirfact
) * gain
;
302 c
= (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
303 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]);
304 coeffs
[i
][1] = lerp(1.0f
, c
, dirfact
) * gain
;
306 coeffStep
[i
][0] = step
* (coeffs
[i
][0] - left
);
307 coeffStep
[i
][1] = step
* (coeffs
[i
][1] - right
);
309 for(i
= 1;i
< Hrtf
->irSize
;i
++)
311 left
= coeffs
[i
][0] - (coeffStep
[i
][0] * counter
);
312 right
= coeffs
[i
][1] - (coeffStep
[i
][1] * counter
);
314 c
= (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
315 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]);
316 coeffs
[i
][0] = lerp(0.0f
, c
, dirfact
) * gain
;
317 c
= (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
318 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]);
319 coeffs
[i
][1] = lerp(0.0f
, c
, dirfact
) * gain
;
321 coeffStep
[i
][0] = step
* (coeffs
[i
][0] - left
);
322 coeffStep
[i
][1] = step
* (coeffs
[i
][1] - right
);
327 for(i
= 0;i
< Hrtf
->irSize
;i
++)
329 left
= coeffs
[i
][0] - (coeffStep
[i
][0] * counter
);
330 right
= coeffs
[i
][1] - (coeffStep
[i
][1] * counter
);
335 coeffStep
[i
][0] = step
* -left
;
336 coeffStep
[i
][1] = step
* -right
;
340 /* The stepping count is the number of samples necessary for the HRIR to
341 * complete its transition. The mixer will only apply stepping for this
344 return fastf2u(delta
);
348 static struct Hrtf
*LoadHrtf00(FILE *f
, ALuint deviceRate
)
350 const ALubyte maxDelay
= SRC_HISTORY_LENGTH
-1;
351 struct Hrtf
*Hrtf
= NULL
;
352 ALboolean failed
= AL_FALSE
;
353 ALuint rate
= 0, irCount
= 0;
356 ALubyte
*azCount
= NULL
;
357 ALushort
*evOffset
= NULL
;
358 ALshort
*coeffs
= NULL
;
359 ALubyte
*delays
= NULL
;
364 rate
|= fgetc(f
)<<16;
365 rate
|= fgetc(f
)<<24;
368 irCount
|= fgetc(f
)<<8;
371 irSize
|= fgetc(f
)<<8;
375 if(rate
!= deviceRate
)
377 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
381 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
383 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
384 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
387 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
389 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
390 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
397 azCount
= malloc(sizeof(azCount
[0])*evCount
);
398 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
399 if(azCount
== NULL
|| evOffset
== NULL
)
401 ERR("Out of memory.\n");
407 evOffset
[0] = fgetc(f
);
408 evOffset
[0] |= fgetc(f
)<<8;
409 for(i
= 1;i
< evCount
;i
++)
411 evOffset
[i
] = fgetc(f
);
412 evOffset
[i
] |= fgetc(f
)<<8;
413 if(evOffset
[i
] <= evOffset
[i
-1])
415 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
416 i
, evOffset
[i
], evOffset
[i
-1]);
420 azCount
[i
-1] = evOffset
[i
] - evOffset
[i
-1];
421 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
423 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
424 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
428 if(irCount
<= evOffset
[i
-1])
430 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
431 i
-1, evOffset
[i
-1], irCount
);
435 azCount
[i
-1] = irCount
- evOffset
[i
-1];
436 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
438 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
439 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
446 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
447 delays
= malloc(sizeof(delays
[0])*irCount
);
448 if(coeffs
== NULL
|| delays
== NULL
)
450 ERR("Out of memory.\n");
457 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
459 for(j
= 0;j
< irSize
;j
++)
463 coeff
|= fgetc(f
)<<8;
467 for(i
= 0;i
< irCount
;i
++)
469 delays
[i
] = fgetc(f
);
470 if(delays
[i
] > maxDelay
)
472 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
479 ERR("Premature end of data\n");
486 Hrtf
= malloc(sizeof(struct Hrtf
));
489 ERR("Out of memory.\n");
496 Hrtf
->sampleRate
= rate
;
497 Hrtf
->irSize
= irSize
;
498 Hrtf
->evCount
= evCount
;
499 Hrtf
->azCount
= azCount
;
500 Hrtf
->evOffset
= evOffset
;
501 Hrtf
->coeffs
= coeffs
;
502 Hrtf
->delays
= delays
;
515 static struct Hrtf
*LoadHrtf01(FILE *f
, ALuint deviceRate
)
517 const ALubyte maxDelay
= SRC_HISTORY_LENGTH
-1;
518 struct Hrtf
*Hrtf
= NULL
;
519 ALboolean failed
= AL_FALSE
;
520 ALuint rate
= 0, irCount
= 0;
521 ALubyte irSize
= 0, evCount
= 0;
522 ALubyte
*azCount
= NULL
;
523 ALushort
*evOffset
= NULL
;
524 ALshort
*coeffs
= NULL
;
525 ALubyte
*delays
= NULL
;
530 rate
|= fgetc(f
)<<16;
531 rate
|= fgetc(f
)<<24;
537 if(rate
!= deviceRate
)
539 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
543 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
545 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
546 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
549 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
551 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
552 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
559 azCount
= malloc(sizeof(azCount
[0])*evCount
);
560 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
561 if(azCount
== NULL
|| evOffset
== NULL
)
563 ERR("Out of memory.\n");
569 for(i
= 0;i
< evCount
;i
++)
571 azCount
[i
] = fgetc(f
);
572 if(azCount
[i
] < MIN_AZ_COUNT
|| azCount
[i
] > MAX_AZ_COUNT
)
574 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
575 i
, azCount
[i
], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
584 irCount
= azCount
[0];
585 for(i
= 1;i
< evCount
;i
++)
587 evOffset
[i
] = evOffset
[i
-1] + azCount
[i
-1];
588 irCount
+= azCount
[i
];
591 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
592 delays
= malloc(sizeof(delays
[0])*irCount
);
593 if(coeffs
== NULL
|| delays
== NULL
)
595 ERR("Out of memory.\n");
602 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
604 for(j
= 0;j
< irSize
;j
++)
608 coeff
|= fgetc(f
)<<8;
612 for(i
= 0;i
< irCount
;i
++)
614 delays
[i
] = fgetc(f
);
615 if(delays
[i
] > maxDelay
)
617 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
624 ERR("Premature end of data\n");
631 Hrtf
= malloc(sizeof(struct Hrtf
));
634 ERR("Out of memory.\n");
641 Hrtf
->sampleRate
= rate
;
642 Hrtf
->irSize
= irSize
;
643 Hrtf
->evCount
= evCount
;
644 Hrtf
->azCount
= azCount
;
645 Hrtf
->evOffset
= evOffset
;
646 Hrtf
->coeffs
= coeffs
;
647 Hrtf
->delays
= delays
;
660 static struct Hrtf
*LoadHrtf(ALuint deviceRate
)
662 const char *fnamelist
= "default-%r.mhr";
664 ConfigValueStr(NULL
, "hrtf_tables", &fnamelist
);
665 while(*fnamelist
!= '\0')
667 struct Hrtf
*Hrtf
= NULL
;
668 char fname
[PATH_MAX
];
675 while(isspace(*fnamelist
) || *fnamelist
== ',')
678 while(*(fnamelist
=next
) != '\0' && *fnamelist
!= ',')
680 next
= strpbrk(fnamelist
, "%,");
681 while(fnamelist
!= next
&& *fnamelist
&& i
< sizeof(fname
))
682 fname
[i
++] = *(fnamelist
++);
684 if(!next
|| *next
== ',')
691 int wrote
= snprintf(&fname
[i
], sizeof(fname
)-i
, "%u", deviceRate
);
692 i
+= minu(wrote
, sizeof(fname
)-i
);
695 else if(*next
== '%')
697 if(i
< sizeof(fname
))
702 ERR("Invalid marker '%%%c'\n", *next
);
704 i
= minu(i
, sizeof(fname
)-1);
706 while(i
> 0 && isspace(fname
[i
-1]))
713 TRACE("Loading %s...\n", fname
);
714 f
= OpenDataFile(fname
, "openal/hrtf");
717 ERR("Could not open %s\n", fname
);
721 if(fread(magic
, 1, sizeof(magic
), f
) != sizeof(magic
))
722 ERR("Failed to read header from %s\n", fname
);
725 if(memcmp(magic
, magicMarker00
, sizeof(magicMarker00
)) == 0)
727 TRACE("Detected data set format v0\n");
728 Hrtf
= LoadHrtf00(f
, deviceRate
);
730 else if(memcmp(magic
, magicMarker01
, sizeof(magicMarker01
)) == 0)
732 TRACE("Detected data set format v1\n");
733 Hrtf
= LoadHrtf01(f
, deviceRate
);
736 ERR("Invalid header in %s: \"%.8s\"\n", fname
, magic
);
744 Hrtf
->next
= LoadedHrtfs
;
746 TRACE("Loaded HRTF support for format: %s %uhz\n",
747 DevFmtChannelsString(DevFmtStereo
), Hrtf
->sampleRate
);
751 ERR("Failed to load %s\n", fname
);
757 const struct Hrtf
*GetHrtf(enum DevFmtChannels chans
, ALCuint srate
)
759 if(chans
== DevFmtStereo
)
761 struct Hrtf
*Hrtf
= LoadedHrtfs
;
764 if(srate
== Hrtf
->sampleRate
)
769 Hrtf
= LoadHrtf(srate
);
773 ERR("Incompatible format: %s %uhz\n", DevFmtChannelsString(chans
), srate
);
777 ALCboolean
FindHrtfFormat(enum DevFmtChannels
*chans
, ALCuint
*srate
)
779 const struct Hrtf
*hrtf
= LoadedHrtfs
;
782 if(*srate
== hrtf
->sampleRate
)
789 hrtf
= LoadHrtf(*srate
);
790 if(hrtf
== NULL
) return ALC_FALSE
;
793 *chans
= DevFmtStereo
;
794 *srate
= hrtf
->sampleRate
;
800 struct Hrtf
*Hrtf
= NULL
;
802 while((Hrtf
=LoadedHrtfs
) != NULL
)
804 LoadedHrtfs
= Hrtf
->next
;
805 free((void*)Hrtf
->azCount
);
806 free((void*)Hrtf
->evOffset
);
807 free((void*)Hrtf
->coeffs
);
808 free((void*)Hrtf
->delays
);
813 ALuint
GetHrtfIrSize (const struct Hrtf
*Hrtf
)