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., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
18
* Or go to http://www.gnu.org/copyleft/lgpl.html
32
static const ALchar magicMarker[8] = "MinPHR00";
34
#define HRIR_COUNT 828
37
static const ALushort evOffset[ELEV_COUNT] = { 0, 1, 13, 37, 73, 118, 174, 234, 306, 378, 450, 522, 594, 654, 710, 755, 791, 815, 827 };
38
static const ALubyte azCount[ELEV_COUNT] = { 1, 12, 24, 36, 45, 56, 60, 72, 72, 72, 72, 72, 60, 56, 45, 36, 24, 12, 1 };
41
static const struct Hrtf {
43
ALshort coeffs[HRIR_COUNT][HRIR_LENGTH];
44
ALubyte delays[HRIR_COUNT];
47
#include "hrtf_tables.inc"
50
static struct Hrtf *LoadedHrtfs = NULL;
51
static ALuint NumLoadedHrtfs = 0;
54
// Calculate the elevation indices given the polar elevation in radians.
55
// This will return two indices between 0 and (ELEV_COUNT-1) and an
56
// interpolation factor between 0.0 and 1.0.
57
static void CalcEvIndices(ALfloat ev, ALuint *evidx, ALfloat *evmu)
59
ev = (F_PI_2 + ev) * (ELEV_COUNT-1) / F_PI;
60
evidx[0] = fastf2u(ev);
61
evidx[1] = minu(evidx[0] + 1, ELEV_COUNT-1);
62
*evmu = ev - evidx[0];
65
// Calculate the azimuth indices given the polar azimuth in radians. This
66
// will return two indices between 0 and (azCount [ei] - 1) and an
67
// interpolation factor between 0.0 and 1.0.
68
static void CalcAzIndices(ALuint evidx, ALfloat az, ALuint *azidx, ALfloat *azmu)
70
az = (F_PI*2.0f + az) * azCount[evidx] / (F_PI*2.0f);
71
azidx[0] = fastf2u(az) % azCount[evidx];
72
azidx[1] = (azidx[0] + 1) % azCount[evidx];
73
*azmu = az - aluFloor(az);
76
// Calculates the normalized HRTF transition factor (delta) from the changes
77
// in gain and listener to source angle between updates. The result is a
78
// normalized delta factor than can be used to calculate moving HRIR stepping
80
ALfloat CalcHrtfDelta(ALfloat oldGain, ALfloat newGain, const ALfloat olddir[3], const ALfloat newdir[3])
82
ALfloat gainChange, angleChange, change;
84
// Calculate the normalized dB gain change.
85
newGain = maxf(newGain, 0.0001f);
86
oldGain = maxf(oldGain, 0.0001f);
87
gainChange = aluFabs(aluLog10(newGain / oldGain) / aluLog10(0.0001f));
89
// Calculate the normalized listener to source angle change when there is
90
// enough gain to notice it.
92
if(gainChange > 0.0001f || newGain > 0.0001f)
94
// No angle change when the directions are equal or degenerate (when
95
// both have zero length).
96
if(newdir[0]-olddir[0] || newdir[1]-olddir[1] || newdir[2]-olddir[2])
97
angleChange = aluAcos(olddir[0]*newdir[0] +
99
olddir[2]*newdir[2]) / F_PI;
103
// Use the largest of the two changes for the delta factor, and apply a
104
// significance shaping function to it.
105
change = maxf(angleChange, gainChange) * 2.0f;
106
return minf(change, 1.0f);
109
// Calculates static HRIR coefficients and delays for the given polar
110
// elevation and azimuth in radians. Linear interpolation is used to
111
// increase the apparent resolution of the HRIR dataset. The coefficients
112
// are also normalized and attenuated by the specified gain.
113
void GetLerpedHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays)
115
ALuint evidx[2], azidx[2];
117
ALuint lidx[4], ridx[4];
120
// Claculate elevation indices and interpolation factor.
121
CalcEvIndices(elevation, evidx, &mu[2]);
123
// Calculate azimuth indices and interpolation factor for the first
125
CalcAzIndices(evidx[0], azimuth, azidx, &mu[0]);
127
// Calculate the first set of linear HRIR indices for left and right
129
lidx[0] = evOffset[evidx[0]] + azidx[0];
130
lidx[1] = evOffset[evidx[0]] + azidx[1];
131
ridx[0] = evOffset[evidx[0]] + ((azCount[evidx[0]]-azidx[0]) % azCount[evidx[0]]);
132
ridx[1] = evOffset[evidx[0]] + ((azCount[evidx[0]]-azidx[1]) % azCount[evidx[0]]);
134
// Calculate azimuth indices and interpolation factor for the second
136
CalcAzIndices(evidx[1], azimuth, azidx, &mu[1]);
138
// Calculate the second set of linear HRIR indices for left and right
140
lidx[2] = evOffset[evidx[1]] + azidx[0];
141
lidx[3] = evOffset[evidx[1]] + azidx[1];
142
ridx[2] = evOffset[evidx[1]] + ((azCount[evidx[1]]-azidx[0]) % azCount[evidx[1]]);
143
ridx[3] = evOffset[evidx[1]] + ((azCount[evidx[1]]-azidx[1]) % azCount[evidx[1]]);
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.
150
gain *= 1.0f/32767.0f;
151
for(i = 0;i < HRIR_LENGTH;i++)
153
coeffs[i][0] = lerp(lerp(Hrtf->coeffs[lidx[0]][i], Hrtf->coeffs[lidx[1]][i], mu[0]),
154
lerp(Hrtf->coeffs[lidx[2]][i], Hrtf->coeffs[lidx[3]][i], mu[1]),
156
coeffs[i][1] = lerp(lerp(Hrtf->coeffs[ridx[0]][i], Hrtf->coeffs[ridx[1]][i], mu[0]),
157
lerp(Hrtf->coeffs[ridx[2]][i], Hrtf->coeffs[ridx[3]][i], mu[1]),
163
for(i = 0;i < HRIR_LENGTH;i++)
170
// Calculate the HRIR delays using linear interpolation.
171
delays[0] = fastf2u(lerp(lerp(Hrtf->delays[lidx[0]], Hrtf->delays[lidx[1]], mu[0]),
172
lerp(Hrtf->delays[lidx[2]], Hrtf->delays[lidx[3]], mu[1]),
174
delays[1] = fastf2u(lerp(lerp(Hrtf->delays[ridx[0]], Hrtf->delays[ridx[1]], mu[0]),
175
lerp(Hrtf->delays[ridx[2]], Hrtf->delays[ridx[3]], mu[1]),
179
// Calculates the moving HRIR target coefficients, target delays, and
180
// stepping values for the given polar elevation and azimuth in radians.
181
// Linear interpolation is used to increase the apparent resolution of the
182
// HRIR dataset. The coefficients are also normalized and attenuated by the
183
// specified gain. Stepping resolution and count is determined using the
184
// given delta factor between 0.0 and 1.0.
185
ALuint GetMovingHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat gain, ALfloat delta, ALint counter, ALfloat (*coeffs)[2], ALuint *delays, ALfloat (*coeffStep)[2], ALint *delayStep)
187
ALuint evidx[2], azidx[2];
188
ALuint lidx[4], ridx[4];
194
// Claculate elevation indices and interpolation factor.
195
CalcEvIndices(elevation, evidx, &mu[2]);
197
// Calculate azimuth indices and interpolation factor for the first
199
CalcAzIndices(evidx[0], azimuth, azidx, &mu[0]);
201
// Calculate the first set of linear HRIR indices for left and right
203
lidx[0] = evOffset[evidx[0]] + azidx[0];
204
lidx[1] = evOffset[evidx[0]] + azidx[1];
205
ridx[0] = evOffset[evidx[0]] + ((azCount[evidx[0]]-azidx[0]) % azCount[evidx[0]]);
206
ridx[1] = evOffset[evidx[0]] + ((azCount[evidx[0]]-azidx[1]) % azCount[evidx[0]]);
208
// Calculate azimuth indices and interpolation factor for the second
210
CalcAzIndices(evidx[1], azimuth, azidx, &mu[1]);
212
// Calculate the second set of linear HRIR indices for left and right
214
lidx[2] = evOffset[evidx[1]] + azidx[0];
215
lidx[3] = evOffset[evidx[1]] + azidx[1];
216
ridx[2] = evOffset[evidx[1]] + ((azCount[evidx[1]]-azidx[0]) % azCount[evidx[1]]);
217
ridx[3] = evOffset[evidx[1]] + ((azCount[evidx[1]]-azidx[1]) % azCount[evidx[1]]);
219
// Calculate the stepping parameters.
220
delta = maxf(aluFloor(delta*(Hrtf->sampleRate*0.015f) + 0.5f), 1.0f);
223
// Calculate the normalized and attenuated target HRIR coefficients using
224
// linear interpolation when there is enough gain to warrant it. Zero
225
// the target coefficients if gain is too low. Then calculate the
226
// coefficient stepping values using the target and previous running
230
gain *= 1.0f/32767.0f;
231
for(i = 0;i < HRIR_LENGTH;i++)
233
left = coeffs[i][0] - (coeffStep[i][0] * counter);
234
right = coeffs[i][1] - (coeffStep[i][1] * counter);
236
coeffs[i][0] = lerp(lerp(Hrtf->coeffs[lidx[0]][i], Hrtf->coeffs[lidx[1]][i], mu[0]),
237
lerp(Hrtf->coeffs[lidx[2]][i], Hrtf->coeffs[lidx[3]][i], mu[1]),
239
coeffs[i][1] = lerp(lerp(Hrtf->coeffs[ridx[0]][i], Hrtf->coeffs[ridx[1]][i], mu[0]),
240
lerp(Hrtf->coeffs[ridx[2]][i], Hrtf->coeffs[ridx[3]][i], mu[1]),
243
coeffStep[i][0] = step * (coeffs[i][0] - left);
244
coeffStep[i][1] = step * (coeffs[i][1] - right);
249
for(i = 0;i < HRIR_LENGTH;i++)
251
left = coeffs[i][0] - (coeffStep[i][0] * counter);
252
right = coeffs[i][1] - (coeffStep[i][1] * counter);
257
coeffStep[i][0] = step * -left;
258
coeffStep[i][1] = step * -right;
262
// Calculate the HRIR delays using linear interpolation. Then calculate
263
// the delay stepping values using the target and previous running
265
left = (ALfloat)(delays[0] - (delayStep[0] * counter));
266
right = (ALfloat)(delays[1] - (delayStep[1] * counter));
268
delays[0] = fastf2u(lerp(lerp(Hrtf->delays[lidx[0]], Hrtf->delays[lidx[1]], mu[0]),
269
lerp(Hrtf->delays[lidx[2]], Hrtf->delays[lidx[3]], mu[1]),
271
delays[1] = fastf2u(lerp(lerp(Hrtf->delays[ridx[0]], Hrtf->delays[ridx[1]], mu[0]),
272
lerp(Hrtf->delays[ridx[2]], Hrtf->delays[ridx[3]], mu[1]),
275
delayStep[0] = fastf2i(step * (delays[0] - left));
276
delayStep[1] = fastf2i(step * (delays[1] - right));
278
// The stepping count is the number of samples necessary for the HRIR to
279
// complete its transition. The mixer will only apply stepping for this
281
return fastf2u(delta);
284
const struct Hrtf *GetHrtf(ALCdevice *device)
286
if(device->FmtChans == DevFmtStereo)
289
for(i = 0;i < NumLoadedHrtfs;i++)
291
if(device->Frequency == LoadedHrtfs[i].sampleRate)
292
return &LoadedHrtfs[i];
294
if(device->Frequency == DefaultHrtf.sampleRate)
297
ERR("Incompatible format: %s %uhz\n",
298
DevFmtChannelsString(device->FmtChans), device->Frequency);
304
char *fnamelist=NULL, *next=NULL;
307
if(ConfigValueStr(NULL, "hrtf_tables", &val))
308
next = fnamelist = strdup(val);
311
const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
320
next = strchr(fname, ',');
332
while(isspace(*next) || *next == ',')
338
TRACE("Loading %s\n", fname);
339
f = fopen(fname, "rb");
342
ERR("Could not open %s\n", fname);
347
if(fread(magic, 1, sizeof(magicMarker), f) != sizeof(magicMarker))
349
ERR("Failed to read magic marker\n");
352
else if(memcmp(magic, magicMarker, sizeof(magicMarker)) != 0)
355
ERR("Invalid magic marker: \"%s\"\n", magic);
361
ALushort hrirCount, hrirSize;
364
newdata.sampleRate = fgetc(f);
365
newdata.sampleRate |= fgetc(f)<<8;
366
newdata.sampleRate |= fgetc(f)<<16;
367
newdata.sampleRate |= fgetc(f)<<24;
369
hrirCount = fgetc(f);
370
hrirCount |= fgetc(f)<<8;
373
hrirSize |= fgetc(f)<<8;
377
if(hrirCount != HRIR_COUNT || hrirSize != HRIR_LENGTH || evCount != ELEV_COUNT)
379
ERR("Unsupported value: hrirCount=%d (%d), hrirSize=%d (%d), evCount=%d (%d)\n",
380
hrirCount, HRIR_COUNT, hrirSize, HRIR_LENGTH, evCount, ELEV_COUNT);
387
for(i = 0;i < ELEV_COUNT;i++)
391
offset |= fgetc(f)<<8;
392
if(offset != evOffset[i])
394
ERR("Unsupported evOffset[%d] value: %d (%d)\n", i, offset, evOffset[i]);
402
for(i = 0;i < HRIR_COUNT;i++)
404
for(j = 0;j < HRIR_LENGTH;j++)
408
coeff |= fgetc(f)<<8;
409
newdata.coeffs[i][j] = coeff;
412
for(i = 0;i < HRIR_COUNT;i++)
416
newdata.delays[i] = delay;
419
ERR("Invalid delay[%d]: %d (%d)\n", i, delay, maxDelay);
426
ERR("Premature end of data\n");
436
void *temp = realloc(LoadedHrtfs, (NumLoadedHrtfs+1)*sizeof(LoadedHrtfs[0]));
440
TRACE("Loaded HRTF support for format: %s %uhz\n",
441
DevFmtChannelsString(DevFmtStereo), newdata.sampleRate);
442
LoadedHrtfs[NumLoadedHrtfs++] = newdata;
446
ERR("Failed to load %s\n", fname);