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 /* 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
;
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
++)
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
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
++)
355 ALfloat ambi_coeffs
[4];
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
);
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
))
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
;
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;
425 ALubyte
*azCount
= NULL
;
426 ALushort
*evOffset
= NULL
;
427 ALshort
*coeffs
= NULL
;
428 ALubyte
*delays
= NULL
;
433 rate
|= fgetc(f
)<<16;
434 rate
|= fgetc(f
)<<24;
437 irCount
|= fgetc(f
)<<8;
440 irSize
|= fgetc(f
)<<8;
444 if(rate
!= deviceRate
)
446 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
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
);
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
);
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");
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]);
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
);
497 if(irCount
<= evOffset
[i
-1])
499 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
500 i
-1, evOffset
[i
-1], irCount
);
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
);
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");
526 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
528 for(j
= 0;j
< irSize
;j
++)
532 coeff
|= fgetc(f
)<<8;
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
);
548 ERR("Premature end of data\n");
555 Hrtf
= malloc(sizeof(struct Hrtf
));
558 ERR("Out of memory.\n");
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
;
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
;
599 rate
|= fgetc(f
)<<16;
600 rate
|= fgetc(f
)<<24;
606 if(rate
!= deviceRate
)
608 ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
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
);
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
);
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");
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
);
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");
671 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
673 for(j
= 0;j
< irSize
;j
++)
677 coeff
|= fgetc(f
)<<8;
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
);
693 ERR("Premature end of data\n");
700 Hrtf
= malloc(sizeof(struct Hrtf
));
703 ERR("Out of memory.\n");
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
;
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
];
744 while(isspace(*fnamelist
) || *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
== ',')
760 int wrote
= snprintf(&fname
[i
], sizeof(fname
)-i
, "%u", deviceRate
);
761 i
+= minu(wrote
, sizeof(fname
)-i
);
764 else if(*next
== '%')
766 if(i
< sizeof(fname
))
771 ERR("Invalid marker '%%%c'\n", *next
);
773 i
= minu(i
, sizeof(fname
)-1);
775 while(i
> 0 && isspace(fname
[i
-1]))
782 TRACE("Loading %s...\n", fname
);
783 f
= OpenDataFile(fname
, "openal/hrtf");
786 ERR("Could not open %s\n", fname
);
790 if(fread(magic
, 1, sizeof(magic
), f
) != sizeof(magic
))
791 ERR("Failed to read header from %s\n", fname
);
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
);
805 ERR("Invalid header in %s: \"%.8s\"\n", fname
, magic
);
813 Hrtf
->next
= LoadedHrtfs
;
815 TRACE("Loaded HRTF support for format: %s %uhz\n",
816 DevFmtChannelsString(DevFmtStereo
), Hrtf
->sampleRate
);
820 ERR("Failed to load %s\n", fname
);
826 const struct Hrtf
*GetHrtf(enum DevFmtChannels chans
, ALCuint srate
)
828 if(chans
== DevFmtStereo
)
830 struct Hrtf
*Hrtf
= LoadedHrtfs
;
833 if(srate
== Hrtf
->sampleRate
)
838 Hrtf
= LoadHrtf(srate
);
842 ERR("Incompatible format: %s %uhz\n", DevFmtChannelsString(chans
), srate
);
846 ALCboolean
FindHrtfFormat(enum DevFmtChannels
*chans
, ALCuint
*srate
)
848 const struct Hrtf
*hrtf
= LoadedHrtfs
;
851 if(*srate
== hrtf
->sampleRate
)
858 hrtf
= LoadHrtf(*srate
);
859 if(hrtf
== NULL
) return ALC_FALSE
;
862 *chans
= DevFmtStereo
;
863 *srate
= hrtf
->sampleRate
;
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
);
882 ALuint
GetHrtfIrSize (const struct Hrtf
*Hrtf
)