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
31 #include "bformatdec.h"
38 /* Current data set limits defined by the makehrtf utility. */
39 #define MIN_IR_SIZE (8)
40 #define MAX_IR_SIZE (128)
41 #define MOD_IR_SIZE (8)
43 #define MIN_EV_COUNT (5)
44 #define MAX_EV_COUNT (128)
46 #define MIN_AZ_COUNT (1)
47 #define MAX_AZ_COUNT (128)
49 static const ALchar magicMarker00
[8] = "MinPHR00";
50 static const ALchar magicMarker01
[8] = "MinPHR01";
52 /* First value for pass-through coefficients (remaining are 0), used for omni-
53 * directional sounds. */
54 static const ALfloat PassthruCoeff
= 32767.0f
* 0.707106781187f
/*sqrt(0.5)*/;
56 static struct Hrtf
*LoadedHrtfs
= NULL
;
58 /* Calculate the elevation indices given the polar elevation in radians.
59 * This will return two indices between 0 and (evcount - 1) and an
60 * interpolation factor between 0.0 and 1.0.
62 static void CalcEvIndices(ALuint evcount
, ALfloat ev
, ALuint
*evidx
, ALfloat
*evmu
)
64 ev
= (F_PI_2
+ ev
) * (evcount
-1) / F_PI
;
65 evidx
[0] = fastf2u(ev
);
66 evidx
[1] = minu(evidx
[0] + 1, evcount
-1);
67 *evmu
= ev
- evidx
[0];
70 /* Calculate the azimuth indices given the polar azimuth in radians. This
71 * will return two indices between 0 and (azcount - 1) and an interpolation
72 * factor between 0.0 and 1.0.
74 static void CalcAzIndices(ALuint azcount
, ALfloat az
, ALuint
*azidx
, ALfloat
*azmu
)
76 az
= (F_TAU
+ az
) * azcount
/ F_TAU
;
77 azidx
[0] = fastf2u(az
) % azcount
;
78 azidx
[1] = (azidx
[0] + 1) % azcount
;
79 *azmu
= az
- floorf(az
);
82 /* Calculates static HRIR coefficients and delays for the given polar
83 * elevation and azimuth in radians. Linear interpolation is used to
84 * increase the apparent resolution of the HRIR data set. The coefficients
85 * are also normalized and attenuated by the specified gain.
87 void GetLerpedHrtfCoeffs(const struct Hrtf
*Hrtf
, ALfloat elevation
, ALfloat azimuth
, ALfloat spread
, ALfloat gain
, ALfloat (*coeffs
)[2], ALuint
*delays
)
89 ALuint evidx
[2], lidx
[4], ridx
[4];
90 ALfloat mu
[3], blend
[4];
94 dirfact
= 1.0f
- (spread
/ F_TAU
);
96 /* Claculate elevation indices and interpolation factor. */
97 CalcEvIndices(Hrtf
->evCount
, elevation
, evidx
, &mu
[2]);
101 ALuint azcount
= Hrtf
->azCount
[evidx
[i
]];
102 ALuint evoffset
= Hrtf
->evOffset
[evidx
[i
]];
105 /* Calculate azimuth indices and interpolation factor for this elevation. */
106 CalcAzIndices(azcount
, azimuth
, azidx
, &mu
[i
]);
108 /* Calculate a set of linear HRIR indices for left and right channels. */
109 lidx
[i
*2 + 0] = evoffset
+ azidx
[0];
110 lidx
[i
*2 + 1] = evoffset
+ azidx
[1];
111 ridx
[i
*2 + 0] = evoffset
+ ((azcount
-azidx
[0]) % azcount
);
112 ridx
[i
*2 + 1] = evoffset
+ ((azcount
-azidx
[1]) % azcount
);
115 /* Calculate 4 blending weights for 2D bilinear interpolation. */
116 blend
[0] = (1.0f
-mu
[0]) * (1.0f
-mu
[2]);
117 blend
[1] = ( mu
[0]) * (1.0f
-mu
[2]);
118 blend
[2] = (1.0f
-mu
[1]) * ( mu
[2]);
119 blend
[3] = ( mu
[1]) * ( mu
[2]);
121 /* Calculate the HRIR delays using linear interpolation. */
122 delays
[0] = fastf2u((Hrtf
->delays
[lidx
[0]]*blend
[0] + Hrtf
->delays
[lidx
[1]]*blend
[1] +
123 Hrtf
->delays
[lidx
[2]]*blend
[2] + Hrtf
->delays
[lidx
[3]]*blend
[3]) *
124 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
125 delays
[1] = fastf2u((Hrtf
->delays
[ridx
[0]]*blend
[0] + Hrtf
->delays
[ridx
[1]]*blend
[1] +
126 Hrtf
->delays
[ridx
[2]]*blend
[2] + Hrtf
->delays
[ridx
[3]]*blend
[3]) *
127 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
129 /* Calculate the sample offsets for the HRIR indices. */
130 lidx
[0] *= Hrtf
->irSize
;
131 lidx
[1] *= Hrtf
->irSize
;
132 lidx
[2] *= Hrtf
->irSize
;
133 lidx
[3] *= Hrtf
->irSize
;
134 ridx
[0] *= Hrtf
->irSize
;
135 ridx
[1] *= Hrtf
->irSize
;
136 ridx
[2] *= Hrtf
->irSize
;
137 ridx
[3] *= Hrtf
->irSize
;
139 /* Calculate the normalized and attenuated HRIR coefficients using linear
140 * interpolation when there is enough gain to warrant it. Zero the
141 * coefficients if gain is too low.
148 c
= (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
149 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]);
150 coeffs
[i
][0] = lerp(PassthruCoeff
, c
, dirfact
) * gain
* (1.0f
/32767.0f
);
151 c
= (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
152 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]);
153 coeffs
[i
][1] = lerp(PassthruCoeff
, c
, dirfact
) * gain
* (1.0f
/32767.0f
);
155 for(i
= 1;i
< Hrtf
->irSize
;i
++)
157 c
= (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
158 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]);
159 coeffs
[i
][0] = lerp(0.0f
, c
, dirfact
) * gain
* (1.0f
/32767.0f
);
160 c
= (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
161 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]);
162 coeffs
[i
][1] = lerp(0.0f
, c
, dirfact
) * gain
* (1.0f
/32767.0f
);
167 for(i
= 0;i
< Hrtf
->irSize
;i
++)
176 ALuint
BuildBFormatHrtf(const struct Hrtf
*Hrtf
, ALfloat (*coeffs
)[HRIR_LENGTH
][2], ALuint NumChannels
)
178 static const struct {
181 } Ambi3DPoints
[14] = {
182 { DEG2RAD( 90.0f
), DEG2RAD( 0.0f
) },
183 { DEG2RAD( 35.0f
), DEG2RAD( -45.0f
) },
184 { DEG2RAD( 35.0f
), DEG2RAD( 45.0f
) },
185 { DEG2RAD( 35.0f
), DEG2RAD( 135.0f
) },
186 { DEG2RAD( 35.0f
), DEG2RAD(-135.0f
) },
187 { DEG2RAD( 0.0f
), DEG2RAD( 0.0f
) },
188 { DEG2RAD( 0.0f
), DEG2RAD( 90.0f
) },
189 { DEG2RAD( 0.0f
), DEG2RAD( 180.0f
) },
190 { DEG2RAD( 0.0f
), DEG2RAD( -90.0f
) },
191 { DEG2RAD(-35.0f
), DEG2RAD( -45.0f
) },
192 { DEG2RAD(-35.0f
), DEG2RAD( 45.0f
) },
193 { DEG2RAD(-35.0f
), DEG2RAD( 135.0f
) },
194 { DEG2RAD(-35.0f
), DEG2RAD(-135.0f
) },
195 { DEG2RAD(-90.0f
), DEG2RAD( 0.0f
) },
197 static const ALfloat Ambi3DMatrix
[14][2][MAX_AMBI_COEFFS
] = {
198 { { 0.071428392f
, 0.000000000f
, 0.071428392f
, 0.000000000f
}, { 0.0269973975f
, 0.0000000000f
, 0.0467610443f
, 0.0000000000f
} },
199 { { 0.071428392f
, 0.041239332f
, 0.041239332f
, 0.041239332f
}, { 0.0269973975f
, 0.0269973975f
, 0.0269973975f
, 0.0269973975f
} },
200 { { 0.071428392f
, -0.041239332f
, 0.041239332f
, 0.041239332f
}, { 0.0269973975f
, -0.0269973975f
, 0.0269973975f
, 0.0269973975f
} },
201 { { 0.071428392f
, -0.041239332f
, 0.041239332f
, -0.041239332f
}, { 0.0269973975f
, -0.0269973975f
, 0.0269973975f
, -0.0269973975f
} },
202 { { 0.071428392f
, 0.041239332f
, 0.041239332f
, -0.041239332f
}, { 0.0269973975f
, 0.0269973975f
, 0.0269973975f
, -0.0269973975f
} },
203 { { 0.071428392f
, 0.000000000f
, 0.000000000f
, 0.071428392f
}, { 0.0269973975f
, 0.0000000000f
, 0.0000000000f
, 0.0467610443f
} },
204 { { 0.071428392f
, -0.071428392f
, 0.000000000f
, 0.000000000f
}, { 0.0269973975f
, -0.0467610443f
, 0.0000000000f
, 0.0000000000f
} },
205 { { 0.071428392f
, 0.000000000f
, 0.000000000f
, -0.071428392f
}, { 0.0269973975f
, 0.0000000000f
, 0.0000000000f
, -0.0467610443f
} },
206 { { 0.071428392f
, 0.071428392f
, 0.000000000f
, 0.000000000f
}, { 0.0269973975f
, 0.0467610443f
, 0.0000000000f
, 0.0000000000f
} },
207 { { 0.071428392f
, 0.041239332f
, -0.041239332f
, 0.041239332f
}, { 0.0269973975f
, 0.0269973975f
, -0.0269973975f
, 0.0269973975f
} },
208 { { 0.071428392f
, -0.041239332f
, -0.041239332f
, 0.041239332f
}, { 0.0269973975f
, -0.0269973975f
, -0.0269973975f
, 0.0269973975f
} },
209 { { 0.071428392f
, -0.041239332f
, -0.041239332f
, -0.041239332f
}, { 0.0269973975f
, -0.0269973975f
, -0.0269973975f
, -0.0269973975f
} },
210 { { 0.071428392f
, 0.041239332f
, -0.041239332f
, -0.041239332f
}, { 0.0269973975f
, 0.0269973975f
, -0.0269973975f
, -0.0269973975f
} },
211 { { 0.071428392f
, 0.000000000f
, -0.071428392f
, 0.000000000f
}, { 0.0269973975f
, 0.0000000000f
, -0.0467610443f
, 0.0000000000f
} },
213 /* Change this to 2 for dual-band HRTF processing. May require a higher quality
214 * band-splitter, or better calculation of the new IR length to deal with the
215 * tail generated by the filter.
218 BandSplitter splitter
;
219 ALfloat temps
[3][HRIR_LENGTH
];
220 ALuint lidx
[14], ridx
[14];
221 ALuint min_delay
= HRTF_HISTORY_LENGTH
;
222 ALuint max_length
= 0;
225 assert(NumChannels
== 4);
227 for(c
= 0;c
< COUNTOF(Ambi3DPoints
);c
++)
233 /* Calculate elevation index. */
234 evidx
= (ALuint
)floorf((F_PI_2
+ Ambi3DPoints
[c
].elevation
) *
235 (Hrtf
->evCount
-1)/F_PI
+ 0.5f
);
236 evidx
= minu(evidx
, Hrtf
->evCount
-1);
238 azcount
= Hrtf
->azCount
[evidx
];
239 evoffset
= Hrtf
->evOffset
[evidx
];
241 /* Calculate azimuth index for this elevation. */
242 azidx
= (ALuint
)floorf((F_TAU
+Ambi3DPoints
[c
].azimuth
) *
243 azcount
/F_TAU
+ 0.5f
) % azcount
;
245 /* Calculate indices for left and right channels. */
246 lidx
[c
] = evoffset
+ azidx
;
247 ridx
[c
] = evoffset
+ ((azcount
-azidx
) % azcount
);
249 min_delay
= minu(min_delay
, minu(Hrtf
->delays
[lidx
[c
]], Hrtf
->delays
[ridx
[c
]]));
252 memset(temps
, 0, sizeof(temps
));
253 bandsplit_init(&splitter
, 400.0f
/ (ALfloat
)Hrtf
->sampleRate
);
254 for(c
= 0;c
< COUNTOF(Ambi3DMatrix
);c
++)
259 /* Convert the left FIR from shorts to float */
260 fir
= &Hrtf
->coeffs
[lidx
[c
] * Hrtf
->irSize
];
263 for(i
= 0;i
< Hrtf
->irSize
;i
++)
264 temps
[0][i
] = fir
[i
] / 32767.0f
;
268 /* Band-split left HRIR into low and high frequency responses. */
269 bandsplit_clear(&splitter
);
270 for(i
= 0;i
< Hrtf
->irSize
;i
++)
271 temps
[2][i
] = fir
[i
] / 32767.0f
;
272 bandsplit_process(&splitter
, temps
[0], temps
[1], temps
[2], HRIR_LENGTH
);
275 /* Add to the left output coefficients with the specified delay. */
276 delay
= Hrtf
->delays
[lidx
[c
]] - min_delay
;
277 for(i
= 0;i
< NumChannels
;++i
)
279 for(b
= 0;b
< NUM_BANDS
;b
++)
282 for(j
= delay
;j
< HRIR_LENGTH
;++j
)
283 coeffs
[i
][j
][0] += temps
[b
][k
++] * Ambi3DMatrix
[c
][b
][i
];
286 max_length
= maxu(max_length
, minu(delay
+ Hrtf
->irSize
, HRIR_LENGTH
));
288 /* Convert the right FIR from shorts to float */
289 fir
= &Hrtf
->coeffs
[ridx
[c
] * Hrtf
->irSize
];
292 for(i
= 0;i
< Hrtf
->irSize
;i
++)
293 temps
[0][i
] = fir
[i
] / 32767.0f
;
297 /* Band-split right HRIR into low and high frequency responses. */
298 bandsplit_clear(&splitter
);
299 for(i
= 0;i
< Hrtf
->irSize
;i
++)
300 temps
[2][i
] = fir
[i
] / 32767.0f
;
301 bandsplit_process(&splitter
, temps
[0], temps
[1], temps
[2], HRIR_LENGTH
);
304 /* Add to the right output coefficients with the specified delay. */
305 delay
= Hrtf
->delays
[ridx
[c
]] - min_delay
;
306 for(i
= 0;i
< NumChannels
;++i
)
308 for(b
= 0;b
< NUM_BANDS
;b
++)
311 for(j
= delay
;j
< HRIR_LENGTH
;++j
)
312 coeffs
[i
][j
][1] += temps
[b
][k
++] * Ambi3DMatrix
[c
][b
][i
];
315 max_length
= maxu(max_length
, minu(delay
+ Hrtf
->irSize
, HRIR_LENGTH
));
317 TRACE("Skipped min delay: %u, new combined length: %u\n", min_delay
, max_length
);
324 static struct Hrtf
*LoadHrtf00(const ALubyte
*data
, size_t datalen
, const_al_string filename
)
326 const ALubyte maxDelay
= HRTF_HISTORY_LENGTH
-1;
327 struct Hrtf
*Hrtf
= NULL
;
328 ALboolean failed
= AL_FALSE
;
329 ALuint rate
= 0, irCount
= 0;
332 ALubyte
*azCount
= NULL
;
333 ALushort
*evOffset
= NULL
;
334 ALshort
*coeffs
= NULL
;
335 const ALubyte
*delays
= NULL
;
340 ERR("Unexpected end of %s data (req %d, rem "SZFMT
")\n",
341 al_string_get_cstr(filename
), 9, datalen
);
346 rate
|= *(data
++)<<8;
347 rate
|= *(data
++)<<16;
348 rate
|= *(data
++)<<24;
352 irCount
|= *(data
++)<<8;
356 irSize
|= *(data
++)<<8;
362 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
364 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
365 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
368 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
370 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
371 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
377 if(datalen
< evCount
*2)
379 ERR("Unexpected end of %s data (req %d, rem "SZFMT
")\n",
380 al_string_get_cstr(filename
), evCount
*2, datalen
);
384 azCount
= malloc(sizeof(azCount
[0])*evCount
);
385 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
386 if(azCount
== NULL
|| evOffset
== NULL
)
388 ERR("Out of memory.\n");
394 evOffset
[0] = *(data
++);
395 evOffset
[0] |= *(data
++)<<8;
397 for(i
= 1;i
< evCount
;i
++)
399 evOffset
[i
] = *(data
++);
400 evOffset
[i
] |= *(data
++)<<8;
402 if(evOffset
[i
] <= evOffset
[i
-1])
404 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
405 i
, evOffset
[i
], evOffset
[i
-1]);
409 azCount
[i
-1] = evOffset
[i
] - evOffset
[i
-1];
410 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
412 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
413 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
417 if(irCount
<= evOffset
[i
-1])
419 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
420 i
-1, evOffset
[i
-1], irCount
);
424 azCount
[i
-1] = irCount
- evOffset
[i
-1];
425 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
427 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
428 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
435 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
438 ERR("Out of memory.\n");
445 size_t reqsize
= 2*irSize
*irCount
+ irCount
;
446 if(datalen
< reqsize
)
448 ERR("Unexpected end of %s data (req "SZFMT
", rem "SZFMT
")\n",
449 al_string_get_cstr(filename
), reqsize
, datalen
);
456 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
458 for(j
= 0;j
< irSize
;j
++)
460 coeffs
[i
+j
] = *(data
++);
461 coeffs
[i
+j
] |= *(data
++)<<8;
469 for(i
= 0;i
< irCount
;i
++)
471 if(delays
[i
] > maxDelay
)
473 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
481 size_t total
= sizeof(struct Hrtf
);
482 total
+= sizeof(azCount
[0])*evCount
;
483 total
= (total
+1)&~1; /* Align for (u)short fields */
484 total
+= sizeof(evOffset
[0])*evCount
;
485 total
+= sizeof(coeffs
[0])*irSize
*irCount
;
486 total
+= sizeof(delays
[0])*irCount
;
487 total
+= al_string_length(filename
)+1;
489 Hrtf
= al_calloc(16, total
);
492 ERR("Out of memory.\n");
499 char *base
= (char*)Hrtf
;
500 uintptr_t offset
= sizeof(*Hrtf
);
502 Hrtf
->sampleRate
= rate
;
503 Hrtf
->irSize
= irSize
;
504 Hrtf
->evCount
= evCount
;
505 Hrtf
->azCount
= ((ALubyte
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->azCount
[0]);
506 offset
= (offset
+1)&~1; /* Align for (u)short fields */
507 Hrtf
->evOffset
= ((ALushort
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->evOffset
[0]);
508 Hrtf
->coeffs
= ((ALshort
*)(base
+ offset
)); offset
+= irSize
*irCount
*sizeof(Hrtf
->coeffs
[0]);
509 Hrtf
->delays
= ((ALubyte
*)(base
+ offset
)); offset
+= irCount
*sizeof(Hrtf
->delays
[0]);
510 Hrtf
->filename
= ((char*)(base
+ offset
));
513 memcpy((void*)Hrtf
->azCount
, azCount
, sizeof(azCount
[0])*evCount
);
514 memcpy((void*)Hrtf
->evOffset
, evOffset
, sizeof(evOffset
[0])*evCount
);
515 memcpy((void*)Hrtf
->coeffs
, coeffs
, sizeof(coeffs
[0])*irSize
*irCount
);
516 memcpy((void*)Hrtf
->delays
, delays
, sizeof(delays
[0])*irCount
);
517 memcpy((void*)Hrtf
->filename
, al_string_get_cstr(filename
), al_string_length(filename
)+1);
526 static struct Hrtf
*LoadHrtf01(const ALubyte
*data
, size_t datalen
, const_al_string filename
)
528 const ALubyte maxDelay
= HRTF_HISTORY_LENGTH
-1;
529 struct Hrtf
*Hrtf
= NULL
;
530 ALboolean failed
= AL_FALSE
;
531 ALuint rate
= 0, irCount
= 0;
532 ALubyte irSize
= 0, evCount
= 0;
533 const ALubyte
*azCount
= NULL
;
534 ALushort
*evOffset
= NULL
;
535 ALshort
*coeffs
= NULL
;
536 const ALubyte
*delays
= NULL
;
541 ERR("Unexpected end of %s data (req %d, rem "SZFMT
"\n",
542 al_string_get_cstr(filename
), 6, datalen
);
547 rate
|= *(data
++)<<8;
548 rate
|= *(data
++)<<16;
549 rate
|= *(data
++)<<24;
558 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
560 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
561 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
564 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
566 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
567 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
573 if(datalen
< evCount
)
575 ERR("Unexpected end of %s data (req %d, rem "SZFMT
"\n",
576 al_string_get_cstr(filename
), evCount
, datalen
);
584 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
585 if(azCount
== NULL
|| evOffset
== NULL
)
587 ERR("Out of memory.\n");
593 for(i
= 0;i
< evCount
;i
++)
595 if(azCount
[i
] < MIN_AZ_COUNT
|| azCount
[i
] > MAX_AZ_COUNT
)
597 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
598 i
, azCount
[i
], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
607 irCount
= azCount
[0];
608 for(i
= 1;i
< evCount
;i
++)
610 evOffset
[i
] = evOffset
[i
-1] + azCount
[i
-1];
611 irCount
+= azCount
[i
];
614 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
617 ERR("Out of memory.\n");
624 size_t reqsize
= 2*irSize
*irCount
+ irCount
;
625 if(datalen
< reqsize
)
627 ERR("Unexpected end of %s data (req "SZFMT
", rem "SZFMT
"\n",
628 al_string_get_cstr(filename
), reqsize
, datalen
);
635 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
637 for(j
= 0;j
< irSize
;j
++)
641 coeff
|= *(data
++)<<8;
650 for(i
= 0;i
< irCount
;i
++)
652 if(delays
[i
] > maxDelay
)
654 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
662 size_t total
= sizeof(struct Hrtf
);
663 total
+= sizeof(azCount
[0])*evCount
;
664 total
= (total
+1)&~1; /* Align for (u)short fields */
665 total
+= sizeof(evOffset
[0])*evCount
;
666 total
+= sizeof(coeffs
[0])*irSize
*irCount
;
667 total
+= sizeof(delays
[0])*irCount
;
668 total
+= al_string_length(filename
)+1;
670 Hrtf
= al_calloc(16, total
);
673 ERR("Out of memory.\n");
680 char *base
= (char*)Hrtf
;
681 uintptr_t offset
= sizeof(*Hrtf
);
683 Hrtf
->sampleRate
= rate
;
684 Hrtf
->irSize
= irSize
;
685 Hrtf
->evCount
= evCount
;
686 Hrtf
->azCount
= ((ALubyte
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->azCount
[0]);
687 offset
= (offset
+1)&~1; /* Align for (u)short fields */
688 Hrtf
->evOffset
= ((ALushort
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->evOffset
[0]);
689 Hrtf
->coeffs
= ((ALshort
*)(base
+ offset
)); offset
+= irSize
*irCount
*sizeof(Hrtf
->coeffs
[0]);
690 Hrtf
->delays
= ((ALubyte
*)(base
+ offset
)); offset
+= irCount
*sizeof(Hrtf
->delays
[0]);
691 Hrtf
->filename
= ((char*)(base
+ offset
));
694 memcpy((void*)Hrtf
->azCount
, azCount
, sizeof(azCount
[0])*evCount
);
695 memcpy((void*)Hrtf
->evOffset
, evOffset
, sizeof(evOffset
[0])*evCount
);
696 memcpy((void*)Hrtf
->coeffs
, coeffs
, sizeof(coeffs
[0])*irSize
*irCount
);
697 memcpy((void*)Hrtf
->delays
, delays
, sizeof(delays
[0])*irCount
);
698 memcpy((void*)Hrtf
->filename
, al_string_get_cstr(filename
), al_string_length(filename
)+1);
706 static void AddFileEntry(vector_HrtfEntry
*list
, al_string
*filename
)
708 HrtfEntry entry
= { AL_STRING_INIT_STATIC(), NULL
};
709 struct Hrtf
*hrtf
= NULL
;
710 const HrtfEntry
*iter
;
711 struct FileMapping fmap
;
716 #define MATCH_FNAME(i) (al_string_cmp_cstr(*filename, (i)->hrtf->filename) == 0)
717 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_FNAME
);
718 if(iter
!= VECTOR_END(*list
))
720 TRACE("Skipping duplicate file entry %s\n", al_string_get_cstr(*filename
));
725 entry
.hrtf
= LoadedHrtfs
;
728 if(al_string_cmp_cstr(*filename
, entry
.hrtf
->filename
) == 0)
730 TRACE("Skipping load of already-loaded file %s\n", al_string_get_cstr(*filename
));
733 entry
.hrtf
= entry
.hrtf
->next
;
736 TRACE("Loading %s...\n", al_string_get_cstr(*filename
));
737 fmap
= MapFileToMem(al_string_get_cstr(*filename
));
740 ERR("Could not open %s\n", al_string_get_cstr(*filename
));
744 if(fmap
.len
< sizeof(magicMarker01
))
745 ERR("%s data is too short ("SZFMT
" bytes)\n", al_string_get_cstr(*filename
), fmap
.len
);
746 else if(memcmp(fmap
.ptr
, magicMarker01
, sizeof(magicMarker01
)) == 0)
748 TRACE("Detected data set format v1\n");
749 hrtf
= LoadHrtf01((const ALubyte
*)fmap
.ptr
+sizeof(magicMarker01
),
750 fmap
.len
-sizeof(magicMarker01
), *filename
753 else if(memcmp(fmap
.ptr
, magicMarker00
, sizeof(magicMarker00
)) == 0)
755 TRACE("Detected data set format v0\n");
756 hrtf
= LoadHrtf00((const ALubyte
*)fmap
.ptr
+sizeof(magicMarker00
),
757 fmap
.len
-sizeof(magicMarker00
), *filename
761 ERR("Invalid header in %s: \"%.8s\"\n", al_string_get_cstr(*filename
), (const char*)fmap
.ptr
);
766 ERR("Failed to load %s\n", al_string_get_cstr(*filename
));
770 hrtf
->next
= LoadedHrtfs
;
772 TRACE("Loaded HRTF support for format: %s %uhz\n",
773 DevFmtChannelsString(DevFmtStereo
), hrtf
->sampleRate
);
777 /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
779 name
= strrchr(al_string_get_cstr(*filename
), '/');
780 if(!name
) name
= strrchr(al_string_get_cstr(*filename
), '\\');
781 if(!name
) name
= al_string_get_cstr(*filename
);
784 ext
= strrchr(name
, '.');
789 al_string_copy_cstr(&entry
.name
, name
);
791 al_string_copy_range(&entry
.name
, name
, ext
);
795 snprintf(str
, sizeof(str
), " #%d", i
+1);
796 al_string_append_cstr(&entry
.name
, str
);
800 #define MATCH_NAME(i) (al_string_cmp(entry.name, (i)->name) == 0)
801 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_NAME
);
803 } while(iter
!= VECTOR_END(*list
));
805 TRACE("Adding entry \"%s\" from file \"%s\"\n", al_string_get_cstr(entry
.name
),
806 al_string_get_cstr(*filename
));
807 VECTOR_PUSH_BACK(*list
, entry
);
810 al_string_deinit(filename
);
813 /* Unfortunate that we have to duplicate AddFileEntry to take a memory buffer
814 * for input instead of opening the given filename.
816 static void AddBuiltInEntry(vector_HrtfEntry
*list
, const ALubyte
*data
, size_t datalen
, al_string
*filename
)
818 HrtfEntry entry
= { AL_STRING_INIT_STATIC(), NULL
};
819 struct Hrtf
*hrtf
= NULL
;
820 const HrtfEntry
*iter
;
823 #define MATCH_FNAME(i) (al_string_cmp_cstr(*filename, (i)->hrtf->filename) == 0)
824 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_FNAME
);
825 if(iter
!= VECTOR_END(*list
))
827 TRACE("Skipping duplicate file entry %s\n", al_string_get_cstr(*filename
));
832 entry
.hrtf
= LoadedHrtfs
;
835 if(al_string_cmp_cstr(*filename
, entry
.hrtf
->filename
) == 0)
837 TRACE("Skipping load of already-loaded file %s\n", al_string_get_cstr(*filename
));
840 entry
.hrtf
= entry
.hrtf
->next
;
843 TRACE("Loading %s...\n", al_string_get_cstr(*filename
));
844 if(datalen
< sizeof(magicMarker01
))
846 ERR("%s data is too short ("SZFMT
" bytes)\n", al_string_get_cstr(*filename
), datalen
);
850 if(memcmp(data
, magicMarker01
, sizeof(magicMarker01
)) == 0)
852 TRACE("Detected data set format v1\n");
853 hrtf
= LoadHrtf01(data
+sizeof(magicMarker01
),
854 datalen
-sizeof(magicMarker01
), *filename
857 else if(memcmp(data
, magicMarker00
, sizeof(magicMarker00
)) == 0)
859 TRACE("Detected data set format v0\n");
860 hrtf
= LoadHrtf00(data
+sizeof(magicMarker00
),
861 datalen
-sizeof(magicMarker00
), *filename
865 ERR("Invalid header in %s: \"%.8s\"\n", al_string_get_cstr(*filename
), data
);
869 ERR("Failed to load %s\n", al_string_get_cstr(*filename
));
873 hrtf
->next
= LoadedHrtfs
;
875 TRACE("Loaded HRTF support for format: %s %uhz\n",
876 DevFmtChannelsString(DevFmtStereo
), hrtf
->sampleRate
);
882 al_string_copy(&entry
.name
, *filename
);
886 snprintf(str
, sizeof(str
), " #%d", i
+1);
887 al_string_append_cstr(&entry
.name
, str
);
891 #define MATCH_NAME(i) (al_string_cmp(entry.name, (i)->name) == 0)
892 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_NAME
);
894 } while(iter
!= VECTOR_END(*list
));
896 TRACE("Adding built-in entry \"%s\"\n", al_string_get_cstr(entry
.name
));
897 VECTOR_PUSH_BACK(*list
, entry
);
900 al_string_deinit(filename
);
904 #ifndef ALSOFT_EMBED_HRTF_DATA
905 #define IDR_DEFAULT_44100_MHR 0
906 #define IDR_DEFAULT_48000_MHR 1
908 static const ALubyte
*GetResource(int UNUSED(name
), size_t *size
)
915 #include "hrtf_res.h"
918 static const ALubyte
*GetResource(int name
, size_t *size
)
925 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
| GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
926 (LPCWSTR
)GetResource
, &handle
928 rc
= FindResourceW(handle
, MAKEINTRESOURCEW(name
), MAKEINTRESOURCEW(MHRTYPE
));
929 res
= LoadResource(handle
, rc
);
931 *size
= SizeofResource(handle
, rc
);
932 return LockResource(res
);
937 extern const ALubyte _binary_default_44100_mhr_start
[] HIDDEN_DECL
;
938 extern const ALubyte _binary_default_44100_mhr_end
[] HIDDEN_DECL
;
939 extern const ALubyte _binary_default_44100_mhr_size
[] HIDDEN_DECL
;
941 extern const ALubyte _binary_default_48000_mhr_start
[] HIDDEN_DECL
;
942 extern const ALubyte _binary_default_48000_mhr_end
[] HIDDEN_DECL
;
943 extern const ALubyte _binary_default_48000_mhr_size
[] HIDDEN_DECL
;
945 static const ALubyte
*GetResource(int name
, size_t *size
)
947 if(name
== IDR_DEFAULT_44100_MHR
)
949 /* Make sure all symbols are referenced, to ensure the compiler won't
950 * ignore the declarations and lose the visibility attribute used to
951 * hide them (would be nice if ld or objcopy could automatically mark
952 * them as hidden when generating them, but apparently they can't).
954 const void *volatile ptr
=_binary_default_44100_mhr_size
;
956 *size
= _binary_default_44100_mhr_end
- _binary_default_44100_mhr_start
;
957 return _binary_default_44100_mhr_start
;
959 if(name
== IDR_DEFAULT_48000_MHR
)
961 const void *volatile ptr
=_binary_default_48000_mhr_size
;
963 *size
= _binary_default_48000_mhr_end
- _binary_default_48000_mhr_start
;
964 return _binary_default_48000_mhr_start
;
972 vector_HrtfEntry
EnumerateHrtf(const_al_string devname
)
974 vector_HrtfEntry list
= VECTOR_INIT_STATIC();
975 const char *defaulthrtf
= "";
976 const char *pathlist
= "";
977 bool usedefaults
= true;
979 if(ConfigValueStr(al_string_get_cstr(devname
), NULL
, "hrtf-paths", &pathlist
))
981 while(pathlist
&& *pathlist
)
983 const char *next
, *end
;
985 while(isspace(*pathlist
) || *pathlist
== ',')
987 if(*pathlist
== '\0')
990 next
= strchr(pathlist
, ',');
995 end
= pathlist
+ strlen(pathlist
);
999 while(end
!= pathlist
&& isspace(*(end
-1)))
1003 al_string pname
= AL_STRING_INIT_STATIC();
1004 vector_al_string flist
;
1006 al_string_append_range(&pname
, pathlist
, end
);
1008 flist
= SearchDataFiles(".mhr", al_string_get_cstr(pname
));
1009 VECTOR_FOR_EACH_PARAMS(al_string
, flist
, AddFileEntry
, &list
);
1010 VECTOR_DEINIT(flist
);
1012 al_string_deinit(&pname
);
1018 else if(ConfigValueExists(al_string_get_cstr(devname
), NULL
, "hrtf_tables"))
1019 ERR("The hrtf_tables option is deprecated, please use hrtf-paths instead.\n");
1023 vector_al_string flist
;
1024 const ALubyte
*rdata
;
1027 flist
= SearchDataFiles(".mhr", "openal/hrtf");
1028 VECTOR_FOR_EACH_PARAMS(al_string
, flist
, AddFileEntry
, &list
);
1029 VECTOR_DEINIT(flist
);
1031 rdata
= GetResource(IDR_DEFAULT_44100_MHR
, &rsize
);
1032 if(rdata
!= NULL
&& rsize
> 0)
1034 al_string ename
= AL_STRING_INIT_STATIC();
1035 al_string_copy_cstr(&ename
, "Built-In 44100hz");
1036 AddBuiltInEntry(&list
, rdata
, rsize
, &ename
);
1039 rdata
= GetResource(IDR_DEFAULT_48000_MHR
, &rsize
);
1040 if(rdata
!= NULL
&& rsize
> 0)
1042 al_string ename
= AL_STRING_INIT_STATIC();
1043 al_string_copy_cstr(&ename
, "Built-In 48000hz");
1044 AddBuiltInEntry(&list
, rdata
, rsize
, &ename
);
1048 if(VECTOR_SIZE(list
) > 1 && ConfigValueStr(al_string_get_cstr(devname
), NULL
, "default-hrtf", &defaulthrtf
))
1050 const HrtfEntry
*iter
;
1051 /* Find the preferred HRTF and move it to the front of the list. */
1052 #define FIND_ENTRY(i) (al_string_cmp_cstr((i)->name, defaulthrtf) == 0)
1053 VECTOR_FIND_IF(iter
, const HrtfEntry
, list
, FIND_ENTRY
);
1055 if(iter
== VECTOR_END(list
))
1056 WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf
);
1057 else if(iter
!= VECTOR_BEGIN(list
))
1059 HrtfEntry entry
= *iter
;
1060 memmove(&VECTOR_ELEM(list
,1), &VECTOR_ELEM(list
,0),
1061 (iter
-VECTOR_BEGIN(list
))*sizeof(HrtfEntry
));
1062 VECTOR_ELEM(list
,0) = entry
;
1069 void FreeHrtfList(vector_HrtfEntry
*list
)
1071 #define CLEAR_ENTRY(i) do { \
1072 al_string_deinit(&(i)->name); \
1074 VECTOR_FOR_EACH(HrtfEntry
, *list
, CLEAR_ENTRY
);
1075 VECTOR_DEINIT(*list
);
1080 void FreeHrtfs(void)
1082 struct Hrtf
*Hrtf
= LoadedHrtfs
;
1087 struct Hrtf
*next
= Hrtf
->next
;