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
36 /* Current data set limits defined by the makehrtf utility. */
37 #define MIN_IR_SIZE (8)
38 #define MAX_IR_SIZE (128)
39 #define MOD_IR_SIZE (8)
41 #define MIN_EV_COUNT (5)
42 #define MAX_EV_COUNT (128)
44 #define MIN_AZ_COUNT (1)
45 #define MAX_AZ_COUNT (128)
52 const ALubyte
*azCount
;
53 const ALushort
*evOffset
;
54 const ALshort
*coeffs
;
55 const ALubyte
*delays
;
61 static const ALchar magicMarker00
[8] = "MinPHR00";
62 static const ALchar magicMarker01
[8] = "MinPHR01";
64 /* First value for pass-through coefficients (remaining are 0), used for omni-
65 * directional sounds. */
66 static const ALfloat PassthruCoeff
= 32767.0f
* 0.707106781187f
/*sqrt(0.5)*/;
68 static struct Hrtf
*LoadedHrtfs
= NULL
;
70 /* Calculate the elevation indices given the polar elevation in radians.
71 * This will return two indices between 0 and (evcount - 1) and an
72 * interpolation factor between 0.0 and 1.0.
74 static void CalcEvIndices(ALuint evcount
, ALfloat ev
, ALuint
*evidx
, ALfloat
*evmu
)
76 ev
= (F_PI_2
+ ev
) * (evcount
-1) / F_PI
;
77 evidx
[0] = fastf2u(ev
);
78 evidx
[1] = minu(evidx
[0] + 1, evcount
-1);
79 *evmu
= ev
- evidx
[0];
82 /* Calculate the azimuth indices given the polar azimuth in radians. This
83 * will return two indices between 0 and (azcount - 1) and an interpolation
84 * factor between 0.0 and 1.0.
86 static void CalcAzIndices(ALuint azcount
, ALfloat az
, ALuint
*azidx
, ALfloat
*azmu
)
88 az
= (F_TAU
+ az
) * azcount
/ F_TAU
;
89 azidx
[0] = fastf2u(az
) % azcount
;
90 azidx
[1] = (azidx
[0] + 1) % azcount
;
91 *azmu
= az
- floorf(az
);
94 /* Calculates static HRIR coefficients and delays for the given polar
95 * elevation and azimuth in radians. Linear interpolation is used to
96 * increase the apparent resolution of the HRIR data set. The coefficients
97 * are also normalized and attenuated by the specified gain.
99 void GetLerpedHrtfCoeffs(const struct Hrtf
*Hrtf
, ALfloat elevation
, ALfloat azimuth
, ALfloat dirfact
, ALfloat gain
, ALfloat (*coeffs
)[2], ALuint
*delays
)
101 ALuint evidx
[2], lidx
[4], ridx
[4];
102 ALfloat mu
[3], blend
[4];
105 /* Claculate elevation indices and interpolation factor. */
106 CalcEvIndices(Hrtf
->evCount
, elevation
, evidx
, &mu
[2]);
110 ALuint azcount
= Hrtf
->azCount
[evidx
[i
]];
111 ALuint evoffset
= Hrtf
->evOffset
[evidx
[i
]];
114 /* Calculate azimuth indices and interpolation factor for this elevation. */
115 CalcAzIndices(azcount
, azimuth
, azidx
, &mu
[i
]);
117 /* Calculate a set of linear HRIR indices for left and right channels. */
118 lidx
[i
*2 + 0] = evoffset
+ azidx
[0];
119 lidx
[i
*2 + 1] = evoffset
+ azidx
[1];
120 ridx
[i
*2 + 0] = evoffset
+ ((azcount
-azidx
[0]) % azcount
);
121 ridx
[i
*2 + 1] = evoffset
+ ((azcount
-azidx
[1]) % azcount
);
124 /* Calculate 4 blending weights for 2D bilinear interpolation. */
125 blend
[0] = (1.0f
-mu
[0]) * (1.0f
-mu
[2]);
126 blend
[1] = ( mu
[0]) * (1.0f
-mu
[2]);
127 blend
[2] = (1.0f
-mu
[1]) * ( mu
[2]);
128 blend
[3] = ( mu
[1]) * ( mu
[2]);
130 /* Calculate the HRIR delays using linear interpolation. */
131 delays
[0] = fastf2u((Hrtf
->delays
[lidx
[0]]*blend
[0] + Hrtf
->delays
[lidx
[1]]*blend
[1] +
132 Hrtf
->delays
[lidx
[2]]*blend
[2] + Hrtf
->delays
[lidx
[3]]*blend
[3]) *
133 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
134 delays
[1] = fastf2u((Hrtf
->delays
[ridx
[0]]*blend
[0] + Hrtf
->delays
[ridx
[1]]*blend
[1] +
135 Hrtf
->delays
[ridx
[2]]*blend
[2] + Hrtf
->delays
[ridx
[3]]*blend
[3]) *
136 dirfact
+ 0.5f
) << HRTFDELAY_BITS
;
138 /* Calculate the sample offsets for the HRIR indices. */
139 lidx
[0] *= Hrtf
->irSize
;
140 lidx
[1] *= Hrtf
->irSize
;
141 lidx
[2] *= Hrtf
->irSize
;
142 lidx
[3] *= Hrtf
->irSize
;
143 ridx
[0] *= Hrtf
->irSize
;
144 ridx
[1] *= Hrtf
->irSize
;
145 ridx
[2] *= Hrtf
->irSize
;
146 ridx
[3] *= Hrtf
->irSize
;
148 /* Calculate the normalized and attenuated HRIR coefficients using linear
149 * interpolation when there is enough gain to warrant it. Zero the
150 * coefficients if gain is too low.
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(PassthruCoeff
, 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(PassthruCoeff
, c
, dirfact
) * gain
* (1.0f
/32767.0f
);
164 for(i
= 1;i
< Hrtf
->irSize
;i
++)
166 c
= (Hrtf
->coeffs
[lidx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[lidx
[1]+i
]*blend
[1] +
167 Hrtf
->coeffs
[lidx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[lidx
[3]+i
]*blend
[3]);
168 coeffs
[i
][0] = lerp(0.0f
, c
, dirfact
) * gain
* (1.0f
/32767.0f
);
169 c
= (Hrtf
->coeffs
[ridx
[0]+i
]*blend
[0] + Hrtf
->coeffs
[ridx
[1]+i
]*blend
[1] +
170 Hrtf
->coeffs
[ridx
[2]+i
]*blend
[2] + Hrtf
->coeffs
[ridx
[3]+i
]*blend
[3]);
171 coeffs
[i
][1] = lerp(0.0f
, c
, dirfact
) * gain
* (1.0f
/32767.0f
);
176 for(i
= 0;i
< Hrtf
->irSize
;i
++)
185 static struct Hrtf
*LoadHrtf00(FILE *f
, const_al_string filename
)
187 const ALubyte maxDelay
= HRTF_HISTORY_LENGTH
-1;
188 struct Hrtf
*Hrtf
= NULL
;
189 ALboolean failed
= AL_FALSE
;
190 ALuint rate
= 0, irCount
= 0;
193 ALubyte
*azCount
= NULL
;
194 ALushort
*evOffset
= NULL
;
195 ALshort
*coeffs
= NULL
;
196 ALubyte
*delays
= NULL
;
201 rate
|= fgetc(f
)<<16;
202 rate
|= fgetc(f
)<<24;
205 irCount
|= fgetc(f
)<<8;
208 irSize
|= fgetc(f
)<<8;
212 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
214 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
215 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
218 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
220 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
221 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
228 azCount
= malloc(sizeof(azCount
[0])*evCount
);
229 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
230 if(azCount
== NULL
|| evOffset
== NULL
)
232 ERR("Out of memory.\n");
238 evOffset
[0] = fgetc(f
);
239 evOffset
[0] |= fgetc(f
)<<8;
240 for(i
= 1;i
< evCount
;i
++)
242 evOffset
[i
] = fgetc(f
);
243 evOffset
[i
] |= fgetc(f
)<<8;
244 if(evOffset
[i
] <= evOffset
[i
-1])
246 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
247 i
, evOffset
[i
], evOffset
[i
-1]);
251 azCount
[i
-1] = evOffset
[i
] - evOffset
[i
-1];
252 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
254 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
255 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
259 if(irCount
<= evOffset
[i
-1])
261 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
262 i
-1, evOffset
[i
-1], irCount
);
266 azCount
[i
-1] = irCount
- evOffset
[i
-1];
267 if(azCount
[i
-1] < MIN_AZ_COUNT
|| azCount
[i
-1] > MAX_AZ_COUNT
)
269 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
270 i
-1, azCount
[i
-1], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
277 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
278 delays
= malloc(sizeof(delays
[0])*irCount
);
279 if(coeffs
== NULL
|| delays
== NULL
)
281 ERR("Out of memory.\n");
288 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
290 for(j
= 0;j
< irSize
;j
++)
294 coeff
|= fgetc(f
)<<8;
298 for(i
= 0;i
< irCount
;i
++)
300 delays
[i
] = fgetc(f
);
301 if(delays
[i
] > maxDelay
)
303 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
310 ERR("Premature end of data\n");
317 size_t total
= sizeof(struct Hrtf
);
318 total
+= sizeof(azCount
[0])*evCount
;
319 total
+= sizeof(evOffset
[0])*evCount
;
320 total
+= sizeof(coeffs
[0])*irSize
*irCount
;
321 total
+= sizeof(delays
[0])*irCount
;
322 total
+= al_string_length(filename
)+1;
324 Hrtf
= malloc(total
);
327 ERR("Out of memory.\n");
334 Hrtf
->sampleRate
= rate
;
335 Hrtf
->irSize
= irSize
;
336 Hrtf
->evCount
= evCount
;
337 Hrtf
->azCount
= ((ALubyte
*)(Hrtf
+1));
338 Hrtf
->evOffset
= ((ALushort
*)(Hrtf
->azCount
+ evCount
));
339 Hrtf
->coeffs
= ((ALshort
*)(Hrtf
->evOffset
+ evCount
));
340 Hrtf
->delays
= ((ALubyte
*)(Hrtf
->coeffs
+ irSize
*irCount
));
341 Hrtf
->filename
= ((char*)(Hrtf
->delays
+ irCount
));
344 memcpy((void*)Hrtf
->azCount
, azCount
, sizeof(azCount
[0])*evCount
);
345 memcpy((void*)Hrtf
->evOffset
, evOffset
, sizeof(evOffset
[0])*evCount
);
346 memcpy((void*)Hrtf
->coeffs
, coeffs
, sizeof(coeffs
[0])*irSize
*irCount
);
347 memcpy((void*)Hrtf
->delays
, delays
, sizeof(delays
[0])*irCount
);
348 memcpy((void*)Hrtf
->filename
, al_string_get_cstr(filename
), al_string_length(filename
)+1);
359 static struct Hrtf
*LoadHrtf01(FILE *f
, const_al_string filename
)
361 const ALubyte maxDelay
= HRTF_HISTORY_LENGTH
-1;
362 struct Hrtf
*Hrtf
= NULL
;
363 ALboolean failed
= AL_FALSE
;
364 ALuint rate
= 0, irCount
= 0;
365 ALubyte irSize
= 0, evCount
= 0;
366 ALubyte
*azCount
= NULL
;
367 ALushort
*evOffset
= NULL
;
368 ALshort
*coeffs
= NULL
;
369 ALubyte
*delays
= NULL
;
374 rate
|= fgetc(f
)<<16;
375 rate
|= fgetc(f
)<<24;
381 if(irSize
< MIN_IR_SIZE
|| irSize
> MAX_IR_SIZE
|| (irSize
%MOD_IR_SIZE
))
383 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
384 irSize
, MIN_IR_SIZE
, MAX_IR_SIZE
, MOD_IR_SIZE
);
387 if(evCount
< MIN_EV_COUNT
|| evCount
> MAX_EV_COUNT
)
389 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
390 evCount
, MIN_EV_COUNT
, MAX_EV_COUNT
);
397 azCount
= malloc(sizeof(azCount
[0])*evCount
);
398 evOffset
= malloc(sizeof(evOffset
[0])*evCount
);
399 if(azCount
== NULL
|| evOffset
== NULL
)
401 ERR("Out of memory.\n");
407 for(i
= 0;i
< evCount
;i
++)
409 azCount
[i
] = fgetc(f
);
410 if(azCount
[i
] < MIN_AZ_COUNT
|| azCount
[i
] > MAX_AZ_COUNT
)
412 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
413 i
, azCount
[i
], MIN_AZ_COUNT
, MAX_AZ_COUNT
);
422 irCount
= azCount
[0];
423 for(i
= 1;i
< evCount
;i
++)
425 evOffset
[i
] = evOffset
[i
-1] + azCount
[i
-1];
426 irCount
+= azCount
[i
];
429 coeffs
= malloc(sizeof(coeffs
[0])*irSize
*irCount
);
430 delays
= malloc(sizeof(delays
[0])*irCount
);
431 if(coeffs
== NULL
|| delays
== NULL
)
433 ERR("Out of memory.\n");
440 for(i
= 0;i
< irCount
*irSize
;i
+=irSize
)
442 for(j
= 0;j
< irSize
;j
++)
446 coeff
|= fgetc(f
)<<8;
450 for(i
= 0;i
< irCount
;i
++)
452 delays
[i
] = fgetc(f
);
453 if(delays
[i
] > maxDelay
)
455 ERR("Invalid delays[%d]: %d (%d)\n", i
, delays
[i
], maxDelay
);
462 ERR("Premature end of data\n");
469 size_t total
= sizeof(struct Hrtf
);
470 total
+= sizeof(azCount
[0])*evCount
;
471 total
+= sizeof(evOffset
[0])*evCount
;
472 total
+= sizeof(coeffs
[0])*irSize
*irCount
;
473 total
+= sizeof(delays
[0])*irCount
;
474 total
+= al_string_length(filename
)+1;
476 Hrtf
= malloc(total
);
479 ERR("Out of memory.\n");
486 Hrtf
->sampleRate
= rate
;
487 Hrtf
->irSize
= irSize
;
488 Hrtf
->evCount
= evCount
;
489 Hrtf
->azCount
= ((ALubyte
*)(Hrtf
+1));
490 Hrtf
->evOffset
= ((ALushort
*)(Hrtf
->azCount
+ evCount
));
491 Hrtf
->coeffs
= ((ALshort
*)(Hrtf
->evOffset
+ evCount
));
492 Hrtf
->delays
= ((ALubyte
*)(Hrtf
->coeffs
+ irSize
*irCount
));
493 Hrtf
->filename
= ((char*)(Hrtf
->delays
+ irCount
));
496 memcpy((void*)Hrtf
->azCount
, azCount
, sizeof(azCount
[0])*evCount
);
497 memcpy((void*)Hrtf
->evOffset
, evOffset
, sizeof(evOffset
[0])*evCount
);
498 memcpy((void*)Hrtf
->coeffs
, coeffs
, sizeof(coeffs
[0])*irSize
*irCount
);
499 memcpy((void*)Hrtf
->delays
, delays
, sizeof(delays
[0])*irCount
);
500 memcpy((void*)Hrtf
->filename
, al_string_get_cstr(filename
), al_string_length(filename
)+1);
511 static void AddFileEntry(vector_HrtfEntry
*list
, al_string
*filename
)
513 HrtfEntry entry
= { AL_STRING_INIT_STATIC(), NULL
};
518 name
= strrchr(al_string_get_cstr(*filename
), '/');
519 if(!name
) name
= strrchr(al_string_get_cstr(*filename
), '\\');
520 if(!name
) name
= al_string_get_cstr(*filename
);
523 entry
.hrtf
= LoadedHrtfs
;
526 if(al_string_cmp_cstr(*filename
, entry
.hrtf
->filename
) == 0)
528 entry
.hrtf
= entry
.hrtf
->next
;
533 struct Hrtf
*hrtf
= NULL
;
537 TRACE("Loading %s...\n", al_string_get_cstr(*filename
));
538 f
= al_fopen(al_string_get_cstr(*filename
), "rb");
541 ERR("Could not open %s\n", al_string_get_cstr(*filename
));
545 if(fread(magic
, 1, sizeof(magic
), f
) != sizeof(magic
))
546 ERR("Failed to read header from %s\n", al_string_get_cstr(*filename
));
549 if(memcmp(magic
, magicMarker00
, sizeof(magicMarker00
)) == 0)
551 TRACE("Detected data set format v0\n");
552 hrtf
= LoadHrtf00(f
, *filename
);
554 else if(memcmp(magic
, magicMarker01
, sizeof(magicMarker01
)) == 0)
556 TRACE("Detected data set format v1\n");
557 hrtf
= LoadHrtf01(f
, *filename
);
560 ERR("Invalid header in %s: \"%.8s\"\n", al_string_get_cstr(*filename
), magic
);
566 ERR("Failed to load %s\n", al_string_get_cstr(*filename
));
570 hrtf
->next
= LoadedHrtfs
;
572 TRACE("Loaded HRTF support for format: %s %uhz\n",
573 DevFmtChannelsString(DevFmtStereo
), hrtf
->sampleRate
);
577 /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
582 al_string_copy_cstr(&entry
.name
, name
);
586 snprintf(str
, sizeof(str
), " #%d", i
+1);
587 al_string_append_cstr(&entry
.name
, str
);
591 #define MATCH_NAME(i) (al_string_cmp(entry.name, (i)->name) == 0)
592 VECTOR_FIND_IF(iter
, HrtfEntry
, *list
, MATCH_NAME
);
594 } while(iter
!= VECTOR_ITER_END(*list
));
596 TRACE("Adding entry \"%s\" from file \"%s\"\n", al_string_get_cstr(entry
.name
),
597 al_string_get_cstr(*filename
));
598 VECTOR_PUSH_BACK(*list
, entry
);
601 al_string_deinit(filename
);
604 vector_HrtfEntry
EnumerateHrtf(const_al_string devname
)
606 vector_HrtfEntry list
= VECTOR_INIT_STATIC();
607 const char *fnamelist
= "%s.mhr";
609 ConfigValueStr(al_string_get_cstr(devname
), NULL
, "hrtf_tables", &fnamelist
);
610 while(fnamelist
&& *fnamelist
)
612 while(isspace(*fnamelist
) || *fnamelist
== ',')
614 if(*fnamelist
!= '\0')
616 const char *next
, *end
;
618 next
= strchr(fnamelist
, ',');
620 end
= fnamelist
+ strlen(fnamelist
);
624 while(end
!= fnamelist
&& isspace(*(end
-1)))
628 al_string fname
= AL_STRING_INIT_STATIC();
629 vector_al_string flist
;
631 al_string_append_range(&fname
, fnamelist
, end
);
633 flist
= SearchDataFiles(al_string_get_cstr(fname
), "openal/hrtf");
634 VECTOR_FOR_EACH_PARAMS(al_string
, flist
, AddFileEntry
, &list
);
635 VECTOR_DEINIT(flist
);
637 al_string_deinit(&fname
);
647 void FreeHrtfList(vector_HrtfEntry
*list
)
649 #define CLEAR_ENTRY(i) do { \
650 al_string_deinit(&(i)->name); \
652 VECTOR_FOR_EACH(HrtfEntry
, *list
, CLEAR_ENTRY
);
653 VECTOR_DEINIT(*list
);
658 ALuint
GetHrtfSampleRate(const struct Hrtf
*Hrtf
)
660 return Hrtf
->sampleRate
;
663 ALuint
GetHrtfIrSize(const struct Hrtf
*Hrtf
)
671 struct Hrtf
*Hrtf
= LoadedHrtfs
;
676 struct Hrtf
*next
= Hrtf
->next
;