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
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 /* 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];
102 /* Claculate elevation indices and interpolation factor. */
103 CalcEvIndices(Hrtf
->evCount
, elevation
, evidx
, &mu
[2]);
107 ALuint azcount
= Hrtf
->azCount
[evidx
[i
]];
108 ALuint evoffset
= Hrtf
->evOffset
[evidx
[i
]];
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.
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
);
173 for(i
= 0;i
< Hrtf
->irSize
;i
++)
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];
196 /* Claculate elevation indices and interpolation factor. */
197 CalcEvIndices(Hrtf
->evCount
, elevation
, evidx
, &mu
[2]);
201 ALuint azcount
= Hrtf
->azCount
[evidx
[i
]];
202 ALuint evoffset
= Hrtf
->evOffset
[evidx
[i
]];
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
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
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
);
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
);
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
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;
323 ALubyte
*azCount
= NULL
;
324 ALushort
*evOffset
= NULL
;
325 ALshort
*coeffs
= NULL
;
326 ALubyte
*delays
= NULL
;
331 rate
|= fgetc(f
)<<16;
332 rate
|= fgetc(f
)<<24;
335 irCount
|= fgetc(f
)<<8;
338 irSize
|= fgetc(f
)<<8;
342 if(rate
!= deviceRate
)
344 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
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
);
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
);
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");
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]);
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
);
395 if(irCount
<= evOffset
[i
-1])
397 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
398 i
-1, evOffset
[i
-1], irCount
);
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
);
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");
424 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
426 for(j
= 0;j
< irSize
;j
++)
430 coeff
|= fgetc(f
)<<8;
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
);
446 ERR("Premature end of data\n");
453 Hrtf
= malloc(sizeof(struct Hrtf
));
456 ERR("Out of memory.\n");
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
;
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
;
497 rate
|= fgetc(f
)<<16;
498 rate
|= fgetc(f
)<<24;
504 if(rate
!= deviceRate
)
506 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
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
);
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
);
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");
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
);
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");
569 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
571 for(j
= 0;j
< irSize
;j
++)
575 coeff
|= fgetc(f
)<<8;
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
);
591 ERR("Premature end of data\n");
598 Hrtf
= malloc(sizeof(struct Hrtf
));
601 ERR("Out of memory.\n");
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
;
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
];
642 while(isspace(*fnamelist
) || *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
== ',')
658 int wrote
= snprintf(&fname
[i
], sizeof(fname
)-i
, "%u", deviceRate
);
659 i
+= minu(wrote
, sizeof(fname
)-i
);
662 else if(*next
== '%')
664 if(i
< sizeof(fname
))
669 ERR("Invalid marker '%%%c'\n", *next
);
671 i
= minu(i
, sizeof(fname
)-1);
673 while(i
> 0 && isspace(fname
[i
-1]))
680 TRACE("Loading %s...\n", fname
);
681 f
= OpenDataFile(fname
, "openal/hrtf");
684 ERR("Could not open %s\n", fname
);
688 if(fread(magic
, 1, sizeof(magic
), f
) != sizeof(magic
))
689 ERR("Failed to read header from %s\n", fname
);
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
);
703 ERR("Invalid header in %s: \"%.8s\"\n", fname
, magic
);
711 Hrtf
->next
= LoadedHrtfs
;
713 TRACE("Loaded HRTF support for format: %s %uhz\n",
714 DevFmtChannelsString(DevFmtStereo
), Hrtf
->sampleRate
);
718 ERR("Failed to load %s\n", fname
);
724 const struct Hrtf
*GetHrtf(enum DevFmtChannels chans
, ALCuint srate
)
726 if(chans
== DevFmtStereo
)
728 struct Hrtf
*Hrtf
= LoadedHrtfs
;
731 if(srate
== Hrtf
->sampleRate
)
736 Hrtf
= LoadHrtf(srate
);
740 ERR("Incompatible format: %s %uhz\n", DevFmtChannelsString(chans
), srate
);
744 ALCboolean
FindHrtfFormat(enum DevFmtChannels
*chans
, ALCuint
*srate
)
746 const struct Hrtf
*hrtf
= LoadedHrtfs
;
749 if(*srate
== hrtf
->sampleRate
)
756 hrtf
= LoadHrtf(*srate
);
757 if(hrtf
== NULL
) return ALC_FALSE
;
760 *chans
= DevFmtStereo
;
761 *srate
= hrtf
->sampleRate
;
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
);
780 ALuint
GetHrtfIrSize (const struct Hrtf
*Hrtf
)