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
;
59 /* Calculate the elevation index given the polar elevation in radians. This
60 * will return an index between 0 and (evcount - 1). Assumes the FPU is in
63 static ALuint
CalcEvIndex(ALuint evcount
, ALfloat ev
)
65 ev
= (F_PI_2
+ ev
) * (evcount
-1) / F_PI
;
66 return minu(fastf2u(ev
+ 0.5f
), evcount
-1);
69 /* Calculate the azimuth index given the polar azimuth in radians. This will
70 * return an index between 0 and (azcount - 1). Assumes the FPU is in round-to-
73 static ALuint
CalcAzIndex(ALuint azcount
, ALfloat az
)
75 az
= (F_TAU
+ az
) * azcount
/ F_TAU
;
76 return fastf2u(az
+ 0.5f
) % azcount
;
79 /* Calculates static HRIR coefficients and delays for the given polar elevation
80 * and azimuth in radians. The coefficients are normalized and attenuated by
83 void GetHrtfCoeffs(const struct Hrtf
*Hrtf
, ALfloat elevation
, ALfloat azimuth
, ALfloat spread
, ALfloat gain
, ALfloat (*coeffs
)[2], ALuint
*delays
)
85 ALuint evidx
, azidx
, lidx
, ridx
;
86 ALuint azcount
, evoffset
;
90 dirfact
= 1.0f
- (spread
/ F_TAU
);
92 /* Claculate elevation index. */
93 evidx
= CalcEvIndex(Hrtf
->evCount
, elevation
);
94 azcount
= Hrtf
->azCount
[evidx
];
95 evoffset
= Hrtf
->evOffset
[evidx
];
97 /* Calculate azimuth index. */
98 azidx
= CalcAzIndex(Hrtf
->azCount
[evidx
], azimuth
);
100 /* Calculate the HRIR indices for left and right channels. */
101 lidx
= evoffset
+ azidx
;
102 ridx
= evoffset
+ ((azcount
-azidx
) % azcount
);
104 /* Calculate the HRIR delays. */
105 delays
[0] = fastf2u(Hrtf
->delays
[lidx
]*dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
106 delays
[1] = fastf2u(Hrtf
->delays
[ridx
]*dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
108 /* Calculate the sample offsets for the HRIR indices. */
109 lidx
*= Hrtf
->irSize
;
110 ridx
*= Hrtf
->irSize
;
112 /* Calculate the normalized and attenuated HRIR coefficients. Zero the
113 * coefficients if gain is too low.
120 coeffs
[i
][0] = lerp(PassthruCoeff
, Hrtf
->coeffs
[lidx
+i
], dirfact
)*gain
;
121 coeffs
[i
][1] = lerp(PassthruCoeff
, Hrtf
->coeffs
[ridx
+i
], dirfact
)*gain
;
122 for(i
= 1;i
< Hrtf
->irSize
;i
++)
124 coeffs
[i
][0] = Hrtf
->coeffs
[lidx
+i
]*gain
* dirfact
;
125 coeffs
[i
][1] = Hrtf
->coeffs
[ridx
+i
]*gain
* dirfact
;
130 for(i
= 0;i
< Hrtf
->irSize
;i
++)
139 ALuint
BuildBFormatHrtf(const struct Hrtf
*Hrtf
, ALfloat (*coeffs
)[HRIR_LENGTH
][2], ALuint NumChannels
)
141 static const struct {
144 } Ambi3DPoints
[14] = {
145 { DEG2RAD( 90.0f
), DEG2RAD( 0.0f
) },
146 { DEG2RAD( 35.0f
), DEG2RAD( -45.0f
) },
147 { DEG2RAD( 35.0f
), DEG2RAD( 45.0f
) },
148 { DEG2RAD( 35.0f
), DEG2RAD( 135.0f
) },
149 { DEG2RAD( 35.0f
), DEG2RAD(-135.0f
) },
150 { DEG2RAD( 0.0f
), DEG2RAD( 0.0f
) },
151 { DEG2RAD( 0.0f
), DEG2RAD( 90.0f
) },
152 { DEG2RAD( 0.0f
), DEG2RAD( 180.0f
) },
153 { DEG2RAD( 0.0f
), DEG2RAD( -90.0f
) },
154 { DEG2RAD(-35.0f
), DEG2RAD( -45.0f
) },
155 { DEG2RAD(-35.0f
), DEG2RAD( 45.0f
) },
156 { DEG2RAD(-35.0f
), DEG2RAD( 135.0f
) },
157 { DEG2RAD(-35.0f
), DEG2RAD(-135.0f
) },
158 { DEG2RAD(-90.0f
), DEG2RAD( 0.0f
) },
160 static const ALfloat Ambi3DMatrix
[14][2][MAX_AMBI_COEFFS
] = {
161 { { 0.078851598f
, 0.000000000f
, 0.070561967f
, 0.000000000f
}, { 0.0714285714f
, 0.0000000000f
, 0.1237180798f
, 0.0000000000f
} },
162 { { 0.124051278f
, 0.059847972f
, 0.059847972f
, 0.059847972f
}, { 0.0714285714f
, 0.0714285714f
, 0.0714285714f
, 0.0714285714f
} },
163 { { 0.124051278f
, -0.059847972f
, 0.059847972f
, 0.059847972f
}, { 0.0714285714f
, -0.0714285714f
, 0.0714285714f
, 0.0714285714f
} },
164 { { 0.124051278f
, -0.059847972f
, 0.059847972f
, -0.059847972f
}, { 0.0714285714f
, -0.0714285714f
, 0.0714285714f
, -0.0714285714f
} },
165 { { 0.124051278f
, 0.059847972f
, 0.059847972f
, -0.059847972f
}, { 0.0714285714f
, 0.0714285714f
, 0.0714285714f
, -0.0714285714f
} },
166 { { 0.078851598f
, 0.000000000f
, 0.000000000f
, 0.070561967f
}, { 0.0714285714f
, 0.0000000000f
, 0.0000000000f
, 0.1237180798f
} },
167 { { 0.078851598f
, -0.070561967f
, 0.000000000f
, 0.000000000f
}, { 0.0714285714f
, -0.1237180798f
, 0.0000000000f
, 0.0000000000f
} },
168 { { 0.078851598f
, 0.000000000f
, 0.000000000f
, -0.070561967f
}, { 0.0714285714f
, 0.0000000000f
, 0.0000000000f
, -0.1237180798f
} },
169 { { 0.078851598f
, 0.070561967f
, 0.000000000f
, 0.000000000f
}, { 0.0714285714f
, 0.1237180798f
, 0.0000000000f
, 0.0000000000f
} },
170 { { 0.124051278f
, 0.059847972f
, -0.059847972f
, 0.059847972f
}, { 0.0714285714f
, 0.0714285714f
, -0.0714285714f
, 0.0714285714f
} },
171 { { 0.124051278f
, -0.059847972f
, -0.059847972f
, 0.059847972f
}, { 0.0714285714f
, -0.0714285714f
, -0.0714285714f
, 0.0714285714f
} },
172 { { 0.124051278f
, -0.059847972f
, -0.059847972f
, -0.059847972f
}, { 0.0714285714f
, -0.0714285714f
, -0.0714285714f
, -0.0714285714f
} },
173 { { 0.124051278f
, 0.059847972f
, -0.059847972f
, -0.059847972f
}, { 0.0714285714f
, 0.0714285714f
, -0.0714285714f
, -0.0714285714f
} },
174 { { 0.078851598f
, 0.000000000f
, -0.070561967f
, 0.000000000f
}, { 0.0714285714f
, 0.0000000000f
, -0.1237180798f
, 0.0000000000f
} },
176 #define AMBIHF_GAIN 1.0f
177 #define AMBILF_GAIN 0.553914423f /* -5.13dB */
179 /* Change this to 2 for dual-band HRTF processing. May require a higher quality
180 * band-splitter, or better calculation of the new IR length to deal with the
181 * tail generated by the filter.
184 BandSplitter splitter
;
185 ALfloat temps
[3][HRIR_LENGTH
];
186 ALuint lidx
[14], ridx
[14];
187 ALuint min_delay
= HRTF_HISTORY_LENGTH
;
188 ALuint max_length
= 0;
191 assert(NumChannels
== 4);
193 for(c
= 0;c
< COUNTOF(Ambi3DPoints
);c
++)
199 /* Calculate elevation index. */
200 evidx
= (ALuint
)floorf((F_PI_2
+ Ambi3DPoints
[c
].elevation
) *
201 (Hrtf
->evCount
-1)/F_PI
+ 0.5f
);
202 evidx
= minu(evidx
, Hrtf
->evCount
-1);
204 azcount
= Hrtf
->azCount
[evidx
];
205 evoffset
= Hrtf
->evOffset
[evidx
];
207 /* Calculate azimuth index for this elevation. */
208 azidx
= (ALuint
)floorf((F_TAU
+Ambi3DPoints
[c
].azimuth
) *
209 azcount
/F_TAU
+ 0.5f
) % azcount
;
211 /* Calculate indices for left and right channels. */
212 lidx
[c
] = evoffset
+ azidx
;
213 ridx
[c
] = evoffset
+ ((azcount
-azidx
) % azcount
);
215 min_delay
= minu(min_delay
, minu(Hrtf
->delays
[lidx
[c
]], Hrtf
->delays
[ridx
[c
]]));
218 memset(temps
, 0, sizeof(temps
));
219 bandsplit_init(&splitter
, 400.0f
/ (ALfloat
)Hrtf
->sampleRate
);
220 for(c
= 0;c
< COUNTOF(Ambi3DMatrix
);c
++)
225 /* Convert the left FIR from shorts to float */
226 fir
= &Hrtf
->coeffs
[lidx
[c
] * Hrtf
->irSize
];
229 for(i
= 0;i
< Hrtf
->irSize
;i
++)
230 temps
[0][i
] = fir
[i
] / 32767.0f
;
234 /* Band-split left HRIR into low and high frequency responses. */
235 bandsplit_clear(&splitter
);
236 for(i
= 0;i
< Hrtf
->irSize
;i
++)
237 temps
[2][i
] = fir
[i
] / 32767.0f
;
238 bandsplit_process(&splitter
, temps
[0], temps
[1], temps
[2], HRIR_LENGTH
);
239 /* Scale the low and high frequency responses. */
240 for(i
= 0;i
< HRIR_LENGTH
;i
++)
241 temps
[0][i
] *= AMBIHF_GAIN
;
242 for(i
= 0;i
< HRIR_LENGTH
;i
++)
243 temps
[1][i
] *= AMBILF_GAIN
;
246 /* Add to the left output coefficients with the specified delay. */
247 delay
= Hrtf
->delays
[lidx
[c
]] - min_delay
;
248 for(i
= 0;i
< NumChannels
;++i
)
250 for(b
= 0;b
< NUM_BANDS
;b
++)
253 for(j
= delay
;j
< HRIR_LENGTH
;++j
)
254 coeffs
[i
][j
][0] += temps
[b
][k
++] * Ambi3DMatrix
[c
][b
][i
];
257 max_length
= maxu(max_length
, minu(delay
+ Hrtf
->irSize
, HRIR_LENGTH
));
259 /* Convert the right FIR from shorts to float */
260 fir
= &Hrtf
->coeffs
[ridx
[c
] * Hrtf
->irSize
];
263 for(i
= 0;i
< Hrtf
->irSize
;i
++)
264 temps
[0][i
] = fir
[i
] / 32767.0f
;
268 /* Band-split right 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
);
273 /* Scale the low and high frequency responses. */
274 for(i
= 0;i
< HRIR_LENGTH
;i
++)
275 temps
[0][i
] *= AMBIHF_GAIN
;
276 for(i
= 0;i
< HRIR_LENGTH
;i
++)
277 temps
[1][i
] *= AMBILF_GAIN
;
280 /* Add to the right output coefficients with the specified delay. */
281 delay
= Hrtf
->delays
[ridx
[c
]] - min_delay
;
282 for(i
= 0;i
< NumChannels
;++i
)
284 for(b
= 0;b
< NUM_BANDS
;b
++)
287 for(j
= delay
;j
< HRIR_LENGTH
;++j
)
288 coeffs
[i
][j
][1] += temps
[b
][k
++] * Ambi3DMatrix
[c
][b
][i
];
291 max_length
= maxu(max_length
, minu(delay
+ Hrtf
->irSize
, HRIR_LENGTH
));
293 TRACE("Skipped min delay: %u, new combined length: %u\n", min_delay
, max_length
);
300 static struct Hrtf
*LoadHrtf00(const ALubyte
*data
, size_t datalen
, const_al_string filename
)
302 const ALubyte maxDelay
= HRTF_HISTORY_LENGTH
-1;
303 struct Hrtf
*Hrtf
= NULL
;
304 ALboolean failed
= AL_FALSE
;
305 ALuint rate
= 0, irCount
= 0;
308 ALubyte
*azCount
= NULL
;
309 ALushort
*evOffset
= NULL
;
310 ALshort
*coeffs
= NULL
;
311 const ALubyte
*delays
= NULL
;
316 ERR("Unexpected end of %s data (req %d, rem "SZFMT
")\n",
317 al_string_get_cstr(filename
), 9, datalen
);
322 rate
|= *(data
++)<<8;
323 rate
|= *(data
++)<<16;
324 rate
|= *(data
++)<<24;
328 irCount
|= *(data
++)<<8;
332 irSize
|= *(data
++)<<8;
338 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
340 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
341 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
344 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
346 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
347 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
353 if(datalen
< evCount
*2)
355 ERR("Unexpected end of %s data (req %d, rem "SZFMT
")\n",
356 al_string_get_cstr(filename
), evCount
*2, datalen
);
360 azCount
= malloc(sizeof(azCount
[0])*evCount
);
361 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
362 if(azCount
== NULL
|| evOffset
== NULL
)
364 ERR("Out of memory.\n");
370 evOffset
[0] = *(data
++);
371 evOffset
[0] |= *(data
++)<<8;
373 for(i
= 1;i
< evCount
;i
++)
375 evOffset
[i
] = *(data
++);
376 evOffset
[i
] |= *(data
++)<<8;
378 if(evOffset
[i
] <= evOffset
[i
-1])
380 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
381 i
, evOffset
[i
], evOffset
[i
-1]);
385 azCount
[i
-1] = evOffset
[i
] - evOffset
[i
-1];
386 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
388 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
389 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
393 if(irCount
<= evOffset
[i
-1])
395 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
396 i
-1, evOffset
[i
-1], irCount
);
400 azCount
[i
-1] = irCount
- evOffset
[i
-1];
401 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
403 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
404 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
411 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
414 ERR("Out of memory.\n");
421 size_t reqsize
= 2*irSize
*irCount
+ irCount
;
422 if(datalen
< reqsize
)
424 ERR("Unexpected end of %s data (req "SZFMT
", rem "SZFMT
")\n",
425 al_string_get_cstr(filename
), reqsize
, datalen
);
432 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
434 for(j
= 0;j
< irSize
;j
++)
436 coeffs
[i
+j
] = *(data
++);
437 coeffs
[i
+j
] |= *(data
++)<<8;
445 for(i
= 0;i
< irCount
;i
++)
447 if(delays
[i
] > maxDelay
)
449 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
457 size_t total
= sizeof(struct Hrtf
);
458 total
+= sizeof(azCount
[0])*evCount
;
459 total
= (total
+1)&~1; /* Align for (u)short fields */
460 total
+= sizeof(evOffset
[0])*evCount
;
461 total
+= sizeof(coeffs
[0])*irSize
*irCount
;
462 total
+= sizeof(delays
[0])*irCount
;
463 total
+= al_string_length(filename
)+1;
465 Hrtf
= al_calloc(16, total
);
468 ERR("Out of memory.\n");
475 char *base
= (char*)Hrtf
;
476 uintptr_t offset
= sizeof(*Hrtf
);
478 Hrtf
->sampleRate
= rate
;
479 Hrtf
->irSize
= irSize
;
480 Hrtf
->evCount
= evCount
;
481 Hrtf
->azCount
= ((ALubyte
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->azCount
[0]);
482 offset
= (offset
+1)&~1; /* Align for (u)short fields */
483 Hrtf
->evOffset
= ((ALushort
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->evOffset
[0]);
484 Hrtf
->coeffs
= ((ALshort
*)(base
+ offset
)); offset
+= irSize
*irCount
*sizeof(Hrtf
->coeffs
[0]);
485 Hrtf
->delays
= ((ALubyte
*)(base
+ offset
)); offset
+= irCount
*sizeof(Hrtf
->delays
[0]);
486 Hrtf
->filename
= ((char*)(base
+ offset
));
489 memcpy((void*)Hrtf
->azCount
, azCount
, sizeof(azCount
[0])*evCount
);
490 memcpy((void*)Hrtf
->evOffset
, evOffset
, sizeof(evOffset
[0])*evCount
);
491 memcpy((void*)Hrtf
->coeffs
, coeffs
, sizeof(coeffs
[0])*irSize
*irCount
);
492 memcpy((void*)Hrtf
->delays
, delays
, sizeof(delays
[0])*irCount
);
493 memcpy((void*)Hrtf
->filename
, al_string_get_cstr(filename
), al_string_length(filename
)+1);
502 static struct Hrtf
*LoadHrtf01(const ALubyte
*data
, size_t datalen
, const_al_string filename
)
504 const ALubyte maxDelay
= HRTF_HISTORY_LENGTH
-1;
505 struct Hrtf
*Hrtf
= NULL
;
506 ALboolean failed
= AL_FALSE
;
507 ALuint rate
= 0, irCount
= 0;
508 ALubyte irSize
= 0, evCount
= 0;
509 const ALubyte
*azCount
= NULL
;
510 ALushort
*evOffset
= NULL
;
511 ALshort
*coeffs
= NULL
;
512 const ALubyte
*delays
= NULL
;
517 ERR("Unexpected end of %s data (req %d, rem "SZFMT
"\n",
518 al_string_get_cstr(filename
), 6, datalen
);
523 rate
|= *(data
++)<<8;
524 rate
|= *(data
++)<<16;
525 rate
|= *(data
++)<<24;
534 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
536 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
537 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
540 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
542 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
543 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
549 if(datalen
< evCount
)
551 ERR("Unexpected end of %s data (req %d, rem "SZFMT
"\n",
552 al_string_get_cstr(filename
), evCount
, datalen
);
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 if(azCount
[i
] < MIN_AZ_COUNT
|| azCount
[i
] > MAX_AZ_COUNT
)
573 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
574 i
, azCount
[i
], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
583 irCount
= azCount
[0];
584 for(i
= 1;i
< evCount
;i
++)
586 evOffset
[i
] = evOffset
[i
-1] + azCount
[i
-1];
587 irCount
+= azCount
[i
];
590 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
593 ERR("Out of memory.\n");
600 size_t reqsize
= 2*irSize
*irCount
+ irCount
;
601 if(datalen
< reqsize
)
603 ERR("Unexpected end of %s data (req "SZFMT
", rem "SZFMT
"\n",
604 al_string_get_cstr(filename
), reqsize
, datalen
);
611 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
613 for(j
= 0;j
< irSize
;j
++)
617 coeff
|= *(data
++)<<8;
626 for(i
= 0;i
< irCount
;i
++)
628 if(delays
[i
] > maxDelay
)
630 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
638 size_t total
= sizeof(struct Hrtf
);
639 total
+= sizeof(azCount
[0])*evCount
;
640 total
= (total
+1)&~1; /* Align for (u)short fields */
641 total
+= sizeof(evOffset
[0])*evCount
;
642 total
+= sizeof(coeffs
[0])*irSize
*irCount
;
643 total
+= sizeof(delays
[0])*irCount
;
644 total
+= al_string_length(filename
)+1;
646 Hrtf
= al_calloc(16, total
);
649 ERR("Out of memory.\n");
656 char *base
= (char*)Hrtf
;
657 uintptr_t offset
= sizeof(*Hrtf
);
659 Hrtf
->sampleRate
= rate
;
660 Hrtf
->irSize
= irSize
;
661 Hrtf
->evCount
= evCount
;
662 Hrtf
->azCount
= ((ALubyte
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->azCount
[0]);
663 offset
= (offset
+1)&~1; /* Align for (u)short fields */
664 Hrtf
->evOffset
= ((ALushort
*)(base
+ offset
)); offset
+= evCount
*sizeof(Hrtf
->evOffset
[0]);
665 Hrtf
->coeffs
= ((ALshort
*)(base
+ offset
)); offset
+= irSize
*irCount
*sizeof(Hrtf
->coeffs
[0]);
666 Hrtf
->delays
= ((ALubyte
*)(base
+ offset
)); offset
+= irCount
*sizeof(Hrtf
->delays
[0]);
667 Hrtf
->filename
= ((char*)(base
+ offset
));
670 memcpy((void*)Hrtf
->azCount
, azCount
, sizeof(azCount
[0])*evCount
);
671 memcpy((void*)Hrtf
->evOffset
, evOffset
, sizeof(evOffset
[0])*evCount
);
672 memcpy((void*)Hrtf
->coeffs
, coeffs
, sizeof(coeffs
[0])*irSize
*irCount
);
673 memcpy((void*)Hrtf
->delays
, delays
, sizeof(delays
[0])*irCount
);
674 memcpy((void*)Hrtf
->filename
, al_string_get_cstr(filename
), al_string_length(filename
)+1);
682 static void AddFileEntry(vector_HrtfEntry
*list
, al_string
*filename
)
684 HrtfEntry entry
= { AL_STRING_INIT_STATIC(), NULL
};
685 struct Hrtf
*hrtf
= NULL
;
686 const HrtfEntry
*iter
;
687 struct FileMapping fmap
;
692 #define MATCH_FNAME(i) (al_string_cmp_cstr(*filename, (i)->hrtf->filename) == 0)
693 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_FNAME
);
694 if(iter
!= VECTOR_END(*list
))
696 TRACE("Skipping duplicate file entry %s\n", al_string_get_cstr(*filename
));
701 entry
.hrtf
= LoadedHrtfs
;
704 if(al_string_cmp_cstr(*filename
, entry
.hrtf
->filename
) == 0)
706 TRACE("Skipping load of already-loaded file %s\n", al_string_get_cstr(*filename
));
709 entry
.hrtf
= entry
.hrtf
->next
;
712 TRACE("Loading %s...\n", al_string_get_cstr(*filename
));
713 fmap
= MapFileToMem(al_string_get_cstr(*filename
));
716 ERR("Could not open %s\n", al_string_get_cstr(*filename
));
720 if(fmap
.len
< sizeof(magicMarker01
))
721 ERR("%s data is too short ("SZFMT
" bytes)\n", al_string_get_cstr(*filename
), fmap
.len
);
722 else if(memcmp(fmap
.ptr
, magicMarker01
, sizeof(magicMarker01
)) == 0)
724 TRACE("Detected data set format v1\n");
725 hrtf
= LoadHrtf01((const ALubyte
*)fmap
.ptr
+sizeof(magicMarker01
),
726 fmap
.len
-sizeof(magicMarker01
), *filename
729 else if(memcmp(fmap
.ptr
, magicMarker00
, sizeof(magicMarker00
)) == 0)
731 TRACE("Detected data set format v0\n");
732 hrtf
= LoadHrtf00((const ALubyte
*)fmap
.ptr
+sizeof(magicMarker00
),
733 fmap
.len
-sizeof(magicMarker00
), *filename
737 ERR("Invalid header in %s: \"%.8s\"\n", al_string_get_cstr(*filename
), (const char*)fmap
.ptr
);
742 ERR("Failed to load %s\n", al_string_get_cstr(*filename
));
746 hrtf
->next
= LoadedHrtfs
;
748 TRACE("Loaded HRTF support for format: %s %uhz\n",
749 DevFmtChannelsString(DevFmtStereo
), hrtf
->sampleRate
);
753 /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
755 name
= strrchr(al_string_get_cstr(*filename
), '/');
756 if(!name
) name
= strrchr(al_string_get_cstr(*filename
), '\\');
757 if(!name
) name
= al_string_get_cstr(*filename
);
760 ext
= strrchr(name
, '.');
765 al_string_copy_cstr(&entry
.name
, name
);
767 al_string_copy_range(&entry
.name
, name
, ext
);
771 snprintf(str
, sizeof(str
), " #%d", i
+1);
772 al_string_append_cstr(&entry
.name
, str
);
776 #define MATCH_NAME(i) (al_string_cmp(entry.name, (i)->name) == 0)
777 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_NAME
);
779 } while(iter
!= VECTOR_END(*list
));
781 TRACE("Adding entry \"%s\" from file \"%s\"\n", al_string_get_cstr(entry
.name
),
782 al_string_get_cstr(*filename
));
783 VECTOR_PUSH_BACK(*list
, entry
);
786 al_string_deinit(filename
);
789 /* Unfortunate that we have to duplicate AddFileEntry to take a memory buffer
790 * for input instead of opening the given filename.
792 static void AddBuiltInEntry(vector_HrtfEntry
*list
, const ALubyte
*data
, size_t datalen
, al_string
*filename
)
794 HrtfEntry entry
= { AL_STRING_INIT_STATIC(), NULL
};
795 struct Hrtf
*hrtf
= NULL
;
796 const HrtfEntry
*iter
;
799 #define MATCH_FNAME(i) (al_string_cmp_cstr(*filename, (i)->hrtf->filename) == 0)
800 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_FNAME
);
801 if(iter
!= VECTOR_END(*list
))
803 TRACE("Skipping duplicate file entry %s\n", al_string_get_cstr(*filename
));
808 entry
.hrtf
= LoadedHrtfs
;
811 if(al_string_cmp_cstr(*filename
, entry
.hrtf
->filename
) == 0)
813 TRACE("Skipping load of already-loaded file %s\n", al_string_get_cstr(*filename
));
816 entry
.hrtf
= entry
.hrtf
->next
;
819 TRACE("Loading %s...\n", al_string_get_cstr(*filename
));
820 if(datalen
< sizeof(magicMarker01
))
822 ERR("%s data is too short ("SZFMT
" bytes)\n", al_string_get_cstr(*filename
), datalen
);
826 if(memcmp(data
, magicMarker01
, sizeof(magicMarker01
)) == 0)
828 TRACE("Detected data set format v1\n");
829 hrtf
= LoadHrtf01(data
+sizeof(magicMarker01
),
830 datalen
-sizeof(magicMarker01
), *filename
833 else if(memcmp(data
, magicMarker00
, sizeof(magicMarker00
)) == 0)
835 TRACE("Detected data set format v0\n");
836 hrtf
= LoadHrtf00(data
+sizeof(magicMarker00
),
837 datalen
-sizeof(magicMarker00
), *filename
841 ERR("Invalid header in %s: \"%.8s\"\n", al_string_get_cstr(*filename
), data
);
845 ERR("Failed to load %s\n", al_string_get_cstr(*filename
));
849 hrtf
->next
= LoadedHrtfs
;
851 TRACE("Loaded HRTF support for format: %s %uhz\n",
852 DevFmtChannelsString(DevFmtStereo
), hrtf
->sampleRate
);
858 al_string_copy(&entry
.name
, *filename
);
862 snprintf(str
, sizeof(str
), " #%d", i
+1);
863 al_string_append_cstr(&entry
.name
, str
);
867 #define MATCH_NAME(i) (al_string_cmp(entry.name, (i)->name) == 0)
868 VECTOR_FIND_IF(iter
, const HrtfEntry
, *list
, MATCH_NAME
);
870 } while(iter
!= VECTOR_END(*list
));
872 TRACE("Adding built-in entry \"%s\"\n", al_string_get_cstr(entry
.name
));
873 VECTOR_PUSH_BACK(*list
, entry
);
876 al_string_deinit(filename
);
880 #ifndef ALSOFT_EMBED_HRTF_DATA
881 #define IDR_DEFAULT_44100_MHR 1
882 #define IDR_DEFAULT_48000_MHR 2
884 static const ALubyte
*GetResource(int UNUSED(name
), size_t *size
)
891 #include "hrtf_res.h"
894 static const ALubyte
*GetResource(int name
, size_t *size
)
901 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
| GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
902 (LPCWSTR
)GetResource
, &handle
904 rc
= FindResourceW(handle
, MAKEINTRESOURCEW(name
), MAKEINTRESOURCEW(MHRTYPE
));
905 res
= LoadResource(handle
, rc
);
907 *size
= SizeofResource(handle
, rc
);
908 return LockResource(res
);
911 #elif defined(__APPLE__)
913 #include <Availability.h>
914 #include <mach-o/getsect.h>
915 #include <mach-o/ldsyms.h>
917 static const ALubyte
*GetResource(int name
, size_t *size
)
919 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
920 /* NOTE: OSX 10.7 and up need to call getsectiondata(&_mh_dylib_header, ...). However, that
921 * call requires 10.7.
923 if(name
== IDR_DEFAULT_44100_MHR
)
924 return getsectiondata(&_mh_dylib_header
, "binary", "default_44100", size
);
925 if(name
== IDR_DEFAULT_48000_MHR
)
926 return getsectiondata(&_mh_dylib_header
, "binary", "default_48000", size
);
928 if(name
== IDR_DEFAULT_44100_MHR
)
929 return getsectdata("binary", "default_44100", size
);
930 if(name
== IDR_DEFAULT_48000_MHR
)
931 return getsectdata("binary", "default_48000", size
);
939 extern const ALubyte _binary_default_44100_mhr_start
[] HIDDEN_DECL
;
940 extern const ALubyte _binary_default_44100_mhr_end
[] HIDDEN_DECL
;
941 extern const ALubyte _binary_default_44100_mhr_size
[] HIDDEN_DECL
;
943 extern const ALubyte _binary_default_48000_mhr_start
[] HIDDEN_DECL
;
944 extern const ALubyte _binary_default_48000_mhr_end
[] HIDDEN_DECL
;
945 extern const ALubyte _binary_default_48000_mhr_size
[] HIDDEN_DECL
;
947 static const ALubyte
*GetResource(int name
, size_t *size
)
949 if(name
== IDR_DEFAULT_44100_MHR
)
951 /* Make sure all symbols are referenced, to ensure the compiler won't
952 * ignore the declarations and lose the visibility attribute used to
953 * hide them (would be nice if ld or objcopy could automatically mark
954 * them as hidden when generating them, but apparently they can't).
956 const void *volatile ptr
=_binary_default_44100_mhr_size
;
958 *size
= _binary_default_44100_mhr_end
- _binary_default_44100_mhr_start
;
959 return _binary_default_44100_mhr_start
;
961 if(name
== IDR_DEFAULT_48000_MHR
)
963 const void *volatile ptr
=_binary_default_48000_mhr_size
;
965 *size
= _binary_default_48000_mhr_end
- _binary_default_48000_mhr_start
;
966 return _binary_default_48000_mhr_start
;
974 vector_HrtfEntry
EnumerateHrtf(const_al_string devname
)
976 vector_HrtfEntry list
= VECTOR_INIT_STATIC();
977 const char *defaulthrtf
= "";
978 const char *pathlist
= "";
979 bool usedefaults
= true;
981 if(ConfigValueStr(al_string_get_cstr(devname
), NULL
, "hrtf-paths", &pathlist
))
983 while(pathlist
&& *pathlist
)
985 const char *next
, *end
;
987 while(isspace(*pathlist
) || *pathlist
== ',')
989 if(*pathlist
== '\0')
992 next
= strchr(pathlist
, ',');
997 end
= pathlist
+ strlen(pathlist
);
1001 while(end
!= pathlist
&& isspace(*(end
-1)))
1005 al_string pname
= AL_STRING_INIT_STATIC();
1006 vector_al_string flist
;
1008 al_string_append_range(&pname
, pathlist
, end
);
1010 flist
= SearchDataFiles(".mhr", al_string_get_cstr(pname
));
1011 VECTOR_FOR_EACH_PARAMS(al_string
, flist
, AddFileEntry
, &list
);
1012 VECTOR_DEINIT(flist
);
1014 al_string_deinit(&pname
);
1020 else if(ConfigValueExists(al_string_get_cstr(devname
), NULL
, "hrtf_tables"))
1021 ERR("The hrtf_tables option is deprecated, please use hrtf-paths instead.\n");
1025 vector_al_string flist
;
1026 const ALubyte
*rdata
;
1029 flist
= SearchDataFiles(".mhr", "openal/hrtf");
1030 VECTOR_FOR_EACH_PARAMS(al_string
, flist
, AddFileEntry
, &list
);
1031 VECTOR_DEINIT(flist
);
1033 rdata
= GetResource(IDR_DEFAULT_44100_MHR
, &rsize
);
1034 if(rdata
!= NULL
&& rsize
> 0)
1036 al_string ename
= AL_STRING_INIT_STATIC();
1037 al_string_copy_cstr(&ename
, "Built-In 44100hz");
1038 AddBuiltInEntry(&list
, rdata
, rsize
, &ename
);
1041 rdata
= GetResource(IDR_DEFAULT_48000_MHR
, &rsize
);
1042 if(rdata
!= NULL
&& rsize
> 0)
1044 al_string ename
= AL_STRING_INIT_STATIC();
1045 al_string_copy_cstr(&ename
, "Built-In 48000hz");
1046 AddBuiltInEntry(&list
, rdata
, rsize
, &ename
);
1050 if(VECTOR_SIZE(list
) > 1 && ConfigValueStr(al_string_get_cstr(devname
), NULL
, "default-hrtf", &defaulthrtf
))
1052 const HrtfEntry
*iter
;
1053 /* Find the preferred HRTF and move it to the front of the list. */
1054 #define FIND_ENTRY(i) (al_string_cmp_cstr((i)->name, defaulthrtf) == 0)
1055 VECTOR_FIND_IF(iter
, const HrtfEntry
, list
, FIND_ENTRY
);
1057 if(iter
== VECTOR_END(list
))
1058 WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf
);
1059 else if(iter
!= VECTOR_BEGIN(list
))
1061 HrtfEntry entry
= *iter
;
1062 memmove(&VECTOR_ELEM(list
,1), &VECTOR_ELEM(list
,0),
1063 (iter
-VECTOR_BEGIN(list
))*sizeof(HrtfEntry
));
1064 VECTOR_ELEM(list
,0) = entry
;
1071 void FreeHrtfList(vector_HrtfEntry
*list
)
1073 #define CLEAR_ENTRY(i) do { \
1074 al_string_deinit(&(i)->name); \
1076 VECTOR_FOR_EACH(HrtfEntry
, *list
, CLEAR_ENTRY
);
1077 VECTOR_DEINIT(*list
);
1082 void FreeHrtfs(void)
1084 struct Hrtf
*Hrtf
= LoadedHrtfs
;
1089 struct Hrtf
*next
= Hrtf
->next
;