~ubuntu-branches/ubuntu/utopic/openal-soft/utopic-proposed

« back to all changes in this revision

Viewing changes to Alc/hrtf.c

  • Committer: Package Import Robot
  • Author(s): Bret Curtis
  • Date: 2014-07-16 08:35:25 UTC
  • mfrom: (0.2.10)
  • Revision ID: package-import@ubuntu.com-20140716083525-5rldbuk4mo211l1a
Tags: 1:1.15.1-1
* Added openal-info binary. (Closes: 659198)
* Added makehrtf binary.
* Added 'audio' to short description. (Closes: 598064) 
* Added FLAGS fixes for cmake in rules for hardening.
* Added man pages for the two binaries.
* New upstream release. (Closes: 731159)
* Removed libsndio-dlopen-change.patch, no longer required.
* Removed no-fpuextended.patch, macros no longer used.
* Removed need for lintian overrides.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include "AL/alc.h"
28
28
#include "alMain.h"
29
29
#include "alSource.h"
30
 
 
31
 
 
32
 
static const ALchar magicMarker[8] = "MinPHR00";
33
 
 
34
 
#define HRIR_COUNT 828
35
 
#define ELEV_COUNT 19
36
 
 
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 };
39
 
 
40
 
 
41
 
static const struct Hrtf {
 
30
#include "alu.h"
 
31
 
 
32
 
 
33
#ifndef PATH_MAX
 
34
#define PATH_MAX 4096
 
35
#endif
 
36
 
 
37
 
 
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)
 
42
 
 
43
#define MIN_EV_COUNT                 (5)
 
44
#define MAX_EV_COUNT                 (128)
 
45
 
 
46
#define MIN_AZ_COUNT                 (1)
 
47
#define MAX_AZ_COUNT                 (128)
 
48
 
 
49
struct Hrtf {
42
50
    ALuint sampleRate;
43
 
    ALshort coeffs[HRIR_COUNT][HRIR_LENGTH];
44
 
    ALubyte delays[HRIR_COUNT];
45
 
} DefaultHrtf = {
46
 
    44100,
 
51
    ALuint irSize;
 
52
    ALubyte evCount;
 
53
 
 
54
    const ALubyte *azCount;
 
55
    const ALushort *evOffset;
 
56
    const ALshort *coeffs;
 
57
    const ALubyte *delays;
 
58
 
 
59
    struct Hrtf *next;
 
60
};
 
61
 
 
62
static const ALchar magicMarker00[8] = "MinPHR00";
 
63
static const ALchar magicMarker01[8] = "MinPHR01";
 
64
 
 
65
/* Define the default HRTF:
 
66
 *  ALubyte  defaultAzCount  [DefaultHrtf.evCount]
 
67
 *  ALushort defaultEvOffset [DefaultHrtf.evCount]
 
68
 *  ALshort  defaultCoeffs   [DefaultHrtf.irCount * defaultHrtf.irSize]
 
69
 *  ALubyte  defaultDelays   [DefaultHrtf.irCount]
 
70
 *
 
71
 *  struct Hrtf DefaultHrtf
 
72
 */
47
73
#include "hrtf_tables.inc"
48
 
};
49
74
 
50
75
static struct Hrtf *LoadedHrtfs = NULL;
51
 
static ALuint NumLoadedHrtfs = 0;
52
 
 
53
 
 
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)
 
76
 
 
77
/* Calculate the elevation indices given the polar elevation in radians.
 
78
 * This will return two indices between 0 and (Hrtf->evCount - 1) and an
 
79
 * interpolation factor between 0.0 and 1.0.
 
80
 */
 
81
static void CalcEvIndices(const struct Hrtf *Hrtf, ALfloat ev, ALuint *evidx, ALfloat *evmu)
58
82
{
59
 
    ev = (F_PI_2 + ev) * (ELEV_COUNT-1) / F_PI;
 
83
    ev = (F_PI_2 + ev) * (Hrtf->evCount-1) / F_PI;
60
84
    evidx[0] = fastf2u(ev);
61
 
    evidx[1] = minu(evidx[0] + 1, ELEV_COUNT-1);
 
85
    evidx[1] = minu(evidx[0] + 1, Hrtf->evCount-1);
62
86
    *evmu = ev - evidx[0];
63
87
}
64
88
 
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)
 
89
/* Calculate the azimuth indices given the polar azimuth in radians.  This
 
90
 * will return two indices between 0 and (Hrtf->azCount[ei] - 1) and an
 
91
 * interpolation factor between 0.0 and 1.0.
 
92
 */
 
93
static void CalcAzIndices(const struct Hrtf *Hrtf, ALuint evidx, ALfloat az, ALuint *azidx, ALfloat *azmu)
69
94
{
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);
 
95
    az = (F_PI*2.0f + az) * Hrtf->azCount[evidx] / (F_PI*2.0f);
 
96
    azidx[0] = fastf2u(az) % Hrtf->azCount[evidx];
 
97
    azidx[1] = (azidx[0] + 1) % Hrtf->azCount[evidx];
 
98
    *azmu = az - floorf(az);
74
99
}
75
100
 
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
79
 
// values.
 
101
/* Calculates the normalized HRTF transition factor (delta) from the changes
 
102
 * in gain and listener to source angle between updates.  The result is a
 
103
 * normalized delta factor that can be used to calculate moving HRIR stepping
 
104
 * values.
 
105
 */
80
106
ALfloat CalcHrtfDelta(ALfloat oldGain, ALfloat newGain, const ALfloat olddir[3], const ALfloat newdir[3])
81
107
{
82
108
    ALfloat gainChange, angleChange, change;
84
110
    // Calculate the normalized dB gain change.
85
111
    newGain = maxf(newGain, 0.0001f);
86
112
    oldGain = maxf(oldGain, 0.0001f);
87
 
    gainChange = aluFabs(aluLog10(newGain / oldGain) / aluLog10(0.0001f));
 
113
    gainChange = fabsf(log10f(newGain / oldGain) / log10f(0.0001f));
88
114
 
89
115
    // Calculate the normalized listener to source angle change when there is
90
116
    // enough gain to notice it.
94
120
        // No angle change when the directions are equal or degenerate (when
95
121
        // both have zero length).
96
122
        if(newdir[0]-olddir[0] || newdir[1]-olddir[1] || newdir[2]-olddir[2])
97
 
            angleChange = aluAcos(olddir[0]*newdir[0] +
98
 
                                  olddir[1]*newdir[1] +
99
 
                                  olddir[2]*newdir[2]) / F_PI;
 
123
            angleChange = acosf(olddir[0]*newdir[0] +
 
124
                                olddir[1]*newdir[1] +
 
125
                                olddir[2]*newdir[2]) / F_PI;
100
126
 
101
127
    }
102
128
 
103
129
    // Use the largest of the two changes for the delta factor, and apply a
104
130
    // significance shaping function to it.
105
 
    change = maxf(angleChange, gainChange) * 2.0f;
 
131
    change = maxf(angleChange * 25.0f, gainChange) * 2.0f;
106
132
    return minf(change, 1.0f);
107
133
}
108
134
 
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.
 
135
/* Calculates static HRIR coefficients and delays for the given polar
 
136
 * elevation and azimuth in radians.  Linear interpolation is used to
 
137
 * increase the apparent resolution of the HRIR data set.  The coefficients
 
138
 * are also normalized and attenuated by the specified gain.
 
139
 */
113
140
void GetLerpedHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays)
114
141
{
115
142
    ALuint evidx[2], azidx[2];
116
 
    ALfloat mu[3];
117
143
    ALuint lidx[4], ridx[4];
 
144
    ALfloat mu[3], blend[4];
118
145
    ALuint i;
119
146
 
120
147
    // Claculate elevation indices and interpolation factor.
121
 
    CalcEvIndices(elevation, evidx, &mu[2]);
 
148
    CalcEvIndices(Hrtf, elevation, evidx, &mu[2]);
122
149
 
123
150
    // Calculate azimuth indices and interpolation factor for the first
124
151
    // elevation.
125
 
    CalcAzIndices(evidx[0], azimuth, azidx, &mu[0]);
 
152
    CalcAzIndices(Hrtf, evidx[0], azimuth, azidx, &mu[0]);
126
153
 
127
154
    // Calculate the first set of linear HRIR indices for left and right
128
155
    // channels.
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]]);
 
156
    lidx[0] = Hrtf->evOffset[evidx[0]] + azidx[0];
 
157
    lidx[1] = Hrtf->evOffset[evidx[0]] + azidx[1];
 
158
    ridx[0] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[0]) % Hrtf->azCount[evidx[0]]);
 
159
    ridx[1] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[1]) % Hrtf->azCount[evidx[0]]);
133
160
 
134
161
    // Calculate azimuth indices and interpolation factor for the second
135
162
    // elevation.
136
 
    CalcAzIndices(evidx[1], azimuth, azidx, &mu[1]);
 
163
    CalcAzIndices(Hrtf, evidx[1], azimuth, azidx, &mu[1]);
137
164
 
138
165
    // Calculate the second set of linear HRIR indices for left and right
139
166
    // channels.
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]]);
144
 
 
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.
 
167
    lidx[2] = Hrtf->evOffset[evidx[1]] + azidx[0];
 
168
    lidx[3] = Hrtf->evOffset[evidx[1]] + azidx[1];
 
169
    ridx[2] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[0]) % Hrtf->azCount[evidx[1]]);
 
170
    ridx[3] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[1]) % Hrtf->azCount[evidx[1]]);
 
171
 
 
172
    /* Calculate 4 blending weights for 2D bilinear interpolation. */
 
173
    blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
 
174
    blend[1] = (     mu[0]) * (1.0f-mu[2]);
 
175
    blend[2] = (1.0f-mu[1]) * (     mu[2]);
 
176
    blend[3] = (     mu[1]) * (     mu[2]);
 
177
 
 
178
    /* Calculate the HRIR delays using linear interpolation. */
 
179
    delays[0] = fastf2u(Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
 
180
                        Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3] +
 
181
                        0.5f) << HRTFDELAY_BITS;
 
182
    delays[1] = fastf2u(Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
 
183
                        Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3] +
 
184
                        0.5f) << HRTFDELAY_BITS;
 
185
 
 
186
    /* Calculate the sample offsets for the HRIR indices. */
 
187
    lidx[0] *= Hrtf->irSize;
 
188
    lidx[1] *= Hrtf->irSize;
 
189
    lidx[2] *= Hrtf->irSize;
 
190
    lidx[3] *= Hrtf->irSize;
 
191
    ridx[0] *= Hrtf->irSize;
 
192
    ridx[1] *= Hrtf->irSize;
 
193
    ridx[2] *= Hrtf->irSize;
 
194
    ridx[3] *= Hrtf->irSize;
 
195
 
 
196
    /* Calculate the normalized and attenuated HRIR coefficients using linear
 
197
     * interpolation when there is enough gain to warrant it.  Zero the
 
198
     * coefficients if gain is too low.
 
199
     */
148
200
    if(gain > 0.0001f)
149
201
    {
150
202
        gain *= 1.0f/32767.0f;
151
 
        for(i = 0;i < HRIR_LENGTH;i++)
 
203
        for(i = 0;i < Hrtf->irSize;i++)
152
204
        {
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]),
155
 
                                mu[2]) * gain;
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]),
158
 
                                mu[2]) * gain;
 
205
            coeffs[i][0] = (Hrtf->coeffs[lidx[0]+i]*blend[0] +
 
206
                            Hrtf->coeffs[lidx[1]+i]*blend[1] +
 
207
                            Hrtf->coeffs[lidx[2]+i]*blend[2] +
 
208
                            Hrtf->coeffs[lidx[3]+i]*blend[3]) * gain;
 
209
            coeffs[i][1] = (Hrtf->coeffs[ridx[0]+i]*blend[0] +
 
210
                            Hrtf->coeffs[ridx[1]+i]*blend[1] +
 
211
                            Hrtf->coeffs[ridx[2]+i]*blend[2] +
 
212
                            Hrtf->coeffs[ridx[3]+i]*blend[3]) * gain;
159
213
        }
160
214
    }
161
215
    else
162
216
    {
163
 
        for(i = 0;i < HRIR_LENGTH;i++)
 
217
        for(i = 0;i < Hrtf->irSize;i++)
164
218
        {
165
219
            coeffs[i][0] = 0.0f;
166
220
            coeffs[i][1] = 0.0f;
167
221
        }
168
222
    }
169
 
 
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]),
173
 
                             mu[2]) * 65536.0f);
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]),
176
 
                             mu[2]) * 65536.0f);
177
223
}
178
224
 
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.
 
225
/* Calculates the moving HRIR target coefficients, target delays, and
 
226
 * stepping values for the given polar elevation and azimuth in radians.
 
227
 * Linear interpolation is used to increase the apparent resolution of the
 
228
 * HRIR data set.  The coefficients are also normalized and attenuated by the
 
229
 * specified gain.  Stepping resolution and count is determined using the
 
230
 * given delta factor between 0.0 and 1.0.
 
231
 */
185
232
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)
186
233
{
187
234
    ALuint evidx[2], azidx[2];
188
235
    ALuint lidx[4], ridx[4];
 
236
    ALfloat mu[3], blend[4];
189
237
    ALfloat left, right;
190
 
    ALfloat mu[3];
191
238
    ALfloat step;
192
239
    ALuint i;
193
240
 
194
241
    // Claculate elevation indices and interpolation factor.
195
 
    CalcEvIndices(elevation, evidx, &mu[2]);
 
242
    CalcEvIndices(Hrtf, elevation, evidx, &mu[2]);
196
243
 
197
244
    // Calculate azimuth indices and interpolation factor for the first
198
245
    // elevation.
199
 
    CalcAzIndices(evidx[0], azimuth, azidx, &mu[0]);
 
246
    CalcAzIndices(Hrtf, evidx[0], azimuth, azidx, &mu[0]);
200
247
 
201
248
    // Calculate the first set of linear HRIR indices for left and right
202
249
    // channels.
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]]);
 
250
    lidx[0] = Hrtf->evOffset[evidx[0]] + azidx[0];
 
251
    lidx[1] = Hrtf->evOffset[evidx[0]] + azidx[1];
 
252
    ridx[0] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[0]) % Hrtf->azCount[evidx[0]]);
 
253
    ridx[1] = Hrtf->evOffset[evidx[0]] + ((Hrtf->azCount[evidx[0]]-azidx[1]) % Hrtf->azCount[evidx[0]]);
207
254
 
208
255
    // Calculate azimuth indices and interpolation factor for the second
209
256
    // elevation.
210
 
    CalcAzIndices(evidx[1], azimuth, azidx, &mu[1]);
 
257
    CalcAzIndices(Hrtf, evidx[1], azimuth, azidx, &mu[1]);
211
258
 
212
259
    // Calculate the second set of linear HRIR indices for left and right
213
260
    // channels.
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]]);
 
261
    lidx[2] = Hrtf->evOffset[evidx[1]] + azidx[0];
 
262
    lidx[3] = Hrtf->evOffset[evidx[1]] + azidx[1];
 
263
    ridx[2] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[0]) % Hrtf->azCount[evidx[1]]);
 
264
    ridx[3] = Hrtf->evOffset[evidx[1]] + ((Hrtf->azCount[evidx[1]]-azidx[1]) % Hrtf->azCount[evidx[1]]);
218
265
 
219
266
    // Calculate the stepping parameters.
220
 
    delta = maxf(aluFloor(delta*(Hrtf->sampleRate*0.015f) + 0.5f), 1.0f);
 
267
    delta = maxf(floorf(delta*(Hrtf->sampleRate*0.015f) + 0.5f), 1.0f);
221
268
    step = 1.0f / delta;
222
269
 
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
227
 
    // coefficients.
 
270
    /* Calculate 4 blending weights for 2D bilinear interpolation. */
 
271
    blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
 
272
    blend[1] = (     mu[0]) * (1.0f-mu[2]);
 
273
    blend[2] = (1.0f-mu[1]) * (     mu[2]);
 
274
    blend[3] = (     mu[1]) * (     mu[2]);
 
275
 
 
276
    /* Calculate the HRIR delays using linear interpolation.  Then calculate
 
277
     * the delay stepping values using the target and previous running
 
278
     * delays.
 
279
     */
 
280
    left = (ALfloat)(delays[0] - (delayStep[0] * counter));
 
281
    right = (ALfloat)(delays[1] - (delayStep[1] * counter));
 
282
 
 
283
    delays[0] = fastf2u(Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
 
284
                        Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3] +
 
285
                        0.5f) << HRTFDELAY_BITS;
 
286
    delays[1] = fastf2u(Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
 
287
                        Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3] +
 
288
                        0.5f) << HRTFDELAY_BITS;
 
289
 
 
290
    delayStep[0] = fastf2i(step * (delays[0] - left));
 
291
    delayStep[1] = fastf2i(step * (delays[1] - right));
 
292
 
 
293
    /* Calculate the sample offsets for the HRIR indices. */
 
294
    lidx[0] *= Hrtf->irSize;
 
295
    lidx[1] *= Hrtf->irSize;
 
296
    lidx[2] *= Hrtf->irSize;
 
297
    lidx[3] *= Hrtf->irSize;
 
298
    ridx[0] *= Hrtf->irSize;
 
299
    ridx[1] *= Hrtf->irSize;
 
300
    ridx[2] *= Hrtf->irSize;
 
301
    ridx[3] *= Hrtf->irSize;
 
302
 
 
303
    /* Calculate the normalized and attenuated target HRIR coefficients using
 
304
     * linear interpolation when there is enough gain to warrant it.  Zero
 
305
     * the target coefficients if gain is too low.  Then calculate the
 
306
     * coefficient stepping values using the target and previous running
 
307
     * coefficients.
 
308
     */
228
309
    if(gain > 0.0001f)
229
310
    {
230
311
        gain *= 1.0f/32767.0f;
233
314
            left = coeffs[i][0] - (coeffStep[i][0] * counter);
234
315
            right = coeffs[i][1] - (coeffStep[i][1] * counter);
235
316
 
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]),
238
 
                                mu[2]) * gain;
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]),
241
 
                                mu[2]) * gain;
 
317
            coeffs[i][0] = (Hrtf->coeffs[lidx[0]+i]*blend[0] +
 
318
                            Hrtf->coeffs[lidx[1]+i]*blend[1] +
 
319
                            Hrtf->coeffs[lidx[2]+i]*blend[2] +
 
320
                            Hrtf->coeffs[lidx[3]+i]*blend[3]) * gain;
 
321
            coeffs[i][1] = (Hrtf->coeffs[ridx[0]+i]*blend[0] +
 
322
                            Hrtf->coeffs[ridx[1]+i]*blend[1] +
 
323
                            Hrtf->coeffs[ridx[2]+i]*blend[2] +
 
324
                            Hrtf->coeffs[ridx[3]+i]*blend[3]) * gain;
242
325
 
243
326
            coeffStep[i][0] = step * (coeffs[i][0] - left);
244
327
            coeffStep[i][1] = step * (coeffs[i][1] - right);
259
342
        }
260
343
    }
261
344
 
262
 
    // Calculate the HRIR delays using linear interpolation.  Then calculate
263
 
    // the delay stepping values using the target and previous running
264
 
    // delays.
265
 
    left = (ALfloat)(delays[0] - (delayStep[0] * counter));
266
 
    right = (ALfloat)(delays[1] - (delayStep[1] * counter));
267
 
 
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]),
270
 
                             mu[2]) * 65536.0f);
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]),
273
 
                             mu[2]) * 65536.0f);
274
 
 
275
 
    delayStep[0] = fastf2i(step * (delays[0] - left));
276
 
    delayStep[1] = fastf2i(step * (delays[1] - right));
277
 
 
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
280
 
    // many samples.
 
345
    /* The stepping count is the number of samples necessary for the HRIR to
 
346
     * complete its transition.  The mixer will only apply stepping for this
 
347
     * many samples.
 
348
     */
281
349
    return fastf2u(delta);
282
350
}
283
351
 
284
 
const struct Hrtf *GetHrtf(ALCdevice *device)
285
 
{
286
 
    if(device->FmtChans == DevFmtStereo)
287
 
    {
 
352
 
 
353
static struct Hrtf *LoadHrtf00(FILE *f, ALuint deviceRate)
 
354
{
 
355
    const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
 
356
    struct Hrtf *Hrtf = NULL;
 
357
    ALboolean failed = AL_FALSE;
 
358
    ALuint rate = 0, irCount = 0;
 
359
    ALushort irSize = 0;
 
360
    ALubyte evCount = 0;
 
361
    ALubyte *azCount = NULL;
 
362
    ALushort *evOffset = NULL;
 
363
    ALshort *coeffs = NULL;
 
364
    ALubyte *delays = NULL;
 
365
    ALuint i, j;
 
366
 
 
367
    rate  = fgetc(f);
 
368
    rate |= fgetc(f)<<8;
 
369
    rate |= fgetc(f)<<16;
 
370
    rate |= fgetc(f)<<24;
 
371
 
 
372
    irCount  = fgetc(f);
 
373
    irCount |= fgetc(f)<<8;
 
374
 
 
375
    irSize  = fgetc(f);
 
376
    irSize |= fgetc(f)<<8;
 
377
 
 
378
    evCount = fgetc(f);
 
379
 
 
380
    if(rate != deviceRate)
 
381
    {
 
382
        ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
 
383
            rate, deviceRate);
 
384
        failed = AL_TRUE;
 
385
    }
 
386
    if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
 
387
    {
 
388
        ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
 
389
            irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
 
390
        failed = AL_TRUE;
 
391
    }
 
392
    if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
 
393
    {
 
394
        ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
 
395
            evCount, MIN_EV_COUNT, MAX_EV_COUNT);
 
396
        failed = AL_TRUE;
 
397
    }
 
398
 
 
399
    if(failed)
 
400
        return NULL;
 
401
 
 
402
    azCount = malloc(sizeof(azCount[0])*evCount);
 
403
    evOffset = malloc(sizeof(evOffset[0])*evCount);
 
404
    if(azCount == NULL || evOffset == NULL)
 
405
    {
 
406
        ERR("Out of memory.\n");
 
407
        failed = AL_TRUE;
 
408
    }
 
409
 
 
410
    if(!failed)
 
411
    {
 
412
        evOffset[0]  = fgetc(f);
 
413
        evOffset[0] |= fgetc(f)<<8;
 
414
        for(i = 1;i < evCount;i++)
 
415
        {
 
416
            evOffset[i]  = fgetc(f);
 
417
            evOffset[i] |= fgetc(f)<<8;
 
418
            if(evOffset[i] <= evOffset[i-1])
 
419
            {
 
420
                ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
 
421
                    i, evOffset[i], evOffset[i-1]);
 
422
                failed = AL_TRUE;
 
423
            }
 
424
 
 
425
            azCount[i-1] = evOffset[i] - evOffset[i-1];
 
426
            if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
 
427
            {
 
428
                ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
 
429
                    i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
 
430
                failed = AL_TRUE;
 
431
            }
 
432
        }
 
433
        if(irCount <= evOffset[i-1])
 
434
        {
 
435
            ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
 
436
                i-1, evOffset[i-1], irCount);
 
437
            failed = AL_TRUE;
 
438
        }
 
439
 
 
440
        azCount[i-1] = irCount - evOffset[i-1];
 
441
        if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
 
442
        {
 
443
            ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
 
444
                i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
 
445
            failed = AL_TRUE;
 
446
        }
 
447
    }
 
448
 
 
449
    if(!failed)
 
450
    {
 
451
        coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
 
452
        delays = malloc(sizeof(delays[0])*irCount);
 
453
        if(coeffs == NULL || delays == NULL)
 
454
        {
 
455
            ERR("Out of memory.\n");
 
456
            failed = AL_TRUE;
 
457
        }
 
458
    }
 
459
 
 
460
    if(!failed)
 
461
    {
 
462
        for(i = 0;i < irCount*irSize;i+=irSize)
 
463
        {
 
464
            for(j = 0;j < irSize;j++)
 
465
            {
 
466
                ALshort coeff;
 
467
                coeff  = fgetc(f);
 
468
                coeff |= fgetc(f)<<8;
 
469
                coeffs[i+j] = coeff;
 
470
            }
 
471
        }
 
472
        for(i = 0;i < irCount;i++)
 
473
        {
 
474
            delays[i] = fgetc(f);
 
475
            if(delays[i] > maxDelay)
 
476
            {
 
477
                ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
 
478
                failed = AL_TRUE;
 
479
            }
 
480
        }
 
481
 
 
482
        if(feof(f))
 
483
        {
 
484
            ERR("Premature end of data\n");
 
485
            failed = AL_TRUE;
 
486
        }
 
487
    }
 
488
 
 
489
    if(!failed)
 
490
    {
 
491
        Hrtf = malloc(sizeof(struct Hrtf));
 
492
        if(Hrtf == NULL)
 
493
        {
 
494
            ERR("Out of memory.\n");
 
495
            failed = AL_TRUE;
 
496
        }
 
497
    }
 
498
 
 
499
    if(!failed)
 
500
    {
 
501
        Hrtf->sampleRate = rate;
 
502
        Hrtf->irSize = irSize;
 
503
        Hrtf->evCount = evCount;
 
504
        Hrtf->azCount = azCount;
 
505
        Hrtf->evOffset = evOffset;
 
506
        Hrtf->coeffs = coeffs;
 
507
        Hrtf->delays = delays;
 
508
        Hrtf->next = NULL;
 
509
        return Hrtf;
 
510
    }
 
511
 
 
512
    free(azCount);
 
513
    free(evOffset);
 
514
    free(coeffs);
 
515
    free(delays);
 
516
    return NULL;
 
517
}
 
518
 
 
519
 
 
520
static struct Hrtf *LoadHrtf01(FILE *f, ALuint deviceRate)
 
521
{
 
522
    const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
 
523
    struct Hrtf *Hrtf = NULL;
 
524
    ALboolean failed = AL_FALSE;
 
525
    ALuint rate = 0, irCount = 0;
 
526
    ALubyte irSize = 0, evCount = 0;
 
527
    ALubyte *azCount = NULL;
 
528
    ALushort *evOffset = NULL;
 
529
    ALshort *coeffs = NULL;
 
530
    ALubyte *delays = NULL;
 
531
    ALuint i, j;
 
532
 
 
533
    rate  = fgetc(f);
 
534
    rate |= fgetc(f)<<8;
 
535
    rate |= fgetc(f)<<16;
 
536
    rate |= fgetc(f)<<24;
 
537
 
 
538
    irSize = fgetc(f);
 
539
 
 
540
    evCount = fgetc(f);
 
541
 
 
542
    if(rate != deviceRate)
 
543
    {
 
544
        ERR("HRIR rate does not match device rate: rate=%d (%d)\n",
 
545
                rate, deviceRate);
 
546
        failed = AL_TRUE;
 
547
    }
 
548
    if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
 
549
    {
 
550
        ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
 
551
            irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
 
552
        failed = AL_TRUE;
 
553
    }
 
554
    if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
 
555
    {
 
556
        ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
 
557
            evCount, MIN_EV_COUNT, MAX_EV_COUNT);
 
558
        failed = AL_TRUE;
 
559
    }
 
560
 
 
561
    if(failed)
 
562
        return NULL;
 
563
 
 
564
    azCount = malloc(sizeof(azCount[0])*evCount);
 
565
    evOffset = malloc(sizeof(evOffset[0])*evCount);
 
566
    if(azCount == NULL || evOffset == NULL)
 
567
    {
 
568
        ERR("Out of memory.\n");
 
569
        failed = AL_TRUE;
 
570
    }
 
571
 
 
572
    if(!failed)
 
573
    {
 
574
        for(i = 0;i < evCount;i++)
 
575
        {
 
576
            azCount[i] = fgetc(f);
 
577
            if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
 
578
            {
 
579
                ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
 
580
                    i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
 
581
                failed = AL_TRUE;
 
582
            }
 
583
        }
 
584
    }
 
585
 
 
586
    if(!failed)
 
587
    {
 
588
        evOffset[0] = 0;
 
589
        irCount = azCount[0];
 
590
        for(i = 1;i < evCount;i++)
 
591
        {
 
592
            evOffset[i] = evOffset[i-1] + azCount[i-1];
 
593
            irCount += azCount[i];
 
594
        }
 
595
 
 
596
        coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
 
597
        delays = malloc(sizeof(delays[0])*irCount);
 
598
        if(coeffs == NULL || delays == NULL)
 
599
        {
 
600
            ERR("Out of memory.\n");
 
601
            failed = AL_TRUE;
 
602
        }
 
603
    }
 
604
 
 
605
    if(!failed)
 
606
    {
 
607
        for(i = 0;i < irCount*irSize;i+=irSize)
 
608
        {
 
609
            for(j = 0;j < irSize;j++)
 
610
            {
 
611
                ALshort coeff;
 
612
                coeff  = fgetc(f);
 
613
                coeff |= fgetc(f)<<8;
 
614
                coeffs[i+j] = coeff;
 
615
            }
 
616
        }
 
617
        for(i = 0;i < irCount;i++)
 
618
        {
 
619
            delays[i] = fgetc(f);
 
620
            if(delays[i] > maxDelay)
 
621
            {
 
622
                ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
 
623
                failed = AL_TRUE;
 
624
            }
 
625
        }
 
626
 
 
627
        if(feof(f))
 
628
        {
 
629
            ERR("Premature end of data\n");
 
630
            failed = AL_TRUE;
 
631
        }
 
632
    }
 
633
 
 
634
    if(!failed)
 
635
    {
 
636
        Hrtf = malloc(sizeof(struct Hrtf));
 
637
        if(Hrtf == NULL)
 
638
        {
 
639
            ERR("Out of memory.\n");
 
640
            failed = AL_TRUE;
 
641
        }
 
642
    }
 
643
 
 
644
    if(!failed)
 
645
    {
 
646
        Hrtf->sampleRate = rate;
 
647
        Hrtf->irSize = irSize;
 
648
        Hrtf->evCount = evCount;
 
649
        Hrtf->azCount = azCount;
 
650
        Hrtf->evOffset = evOffset;
 
651
        Hrtf->coeffs = coeffs;
 
652
        Hrtf->delays = delays;
 
653
        Hrtf->next = NULL;
 
654
        return Hrtf;
 
655
    }
 
656
 
 
657
    free(azCount);
 
658
    free(evOffset);
 
659
    free(coeffs);
 
660
    free(delays);
 
661
    return NULL;
 
662
}
 
663
 
 
664
 
 
665
static struct Hrtf *LoadHrtf(ALuint deviceRate)
 
666
{
 
667
    const char *fnamelist = NULL;
 
668
 
 
669
    if(!ConfigValueStr(NULL, "hrtf_tables", &fnamelist))
 
670
        return NULL;
 
671
    while(*fnamelist != '\0')
 
672
    {
 
673
        struct Hrtf *Hrtf = NULL;
 
674
        char fname[PATH_MAX];
 
675
        ALchar magic[8];
288
676
        ALuint i;
289
 
        for(i = 0;i < NumLoadedHrtfs;i++)
290
 
        {
291
 
            if(device->Frequency == LoadedHrtfs[i].sampleRate)
292
 
                return &LoadedHrtfs[i];
293
 
        }
294
 
        if(device->Frequency == DefaultHrtf.sampleRate)
295
 
            return &DefaultHrtf;
296
 
    }
297
 
    ERR("Incompatible format: %s %uhz\n",
298
 
        DevFmtChannelsString(device->FmtChans), device->Frequency);
299
 
    return NULL;
300
 
}
301
 
 
302
 
void InitHrtf(void)
303
 
{
304
 
    char *fnamelist=NULL, *next=NULL;
305
 
    const char *val;
306
 
 
307
 
    if(ConfigValueStr(NULL, "hrtf_tables", &val))
308
 
        next = fnamelist = strdup(val);
309
 
    while(next && *next)
310
 
    {
311
 
        const ALubyte maxDelay = SRC_HISTORY_LENGTH-1;
312
 
        struct Hrtf newdata;
313
 
        ALboolean failed;
314
 
        ALchar magic[9];
315
 
        ALsizei i, j;
316
 
        char *fname;
317
677
        FILE *f;
318
678
 
319
 
        fname = next;
320
 
        next = strchr(fname, ',');
321
 
        if(next)
 
679
        while(isspace(*fnamelist) || *fnamelist == ',')
 
680
            fnamelist++;
 
681
        i = 0;
 
682
        while(*fnamelist != '\0' && *fnamelist != ',')
322
683
        {
323
 
            while(next != fname)
324
 
            {
325
 
                next--;
326
 
                if(!isspace(*next))
327
 
                {
328
 
                    *(next++) = '\0';
329
 
                    break;
330
 
                }
331
 
            }
332
 
            while(isspace(*next) || *next == ',')
333
 
                next++;
 
684
            const char *next = strpbrk(fnamelist, "%,");
 
685
            while(fnamelist != next && *fnamelist && i < sizeof(fname))
 
686
                fname[i++] = *(fnamelist++);
 
687
 
 
688
            if(!next || *next == ',')
 
689
                break;
 
690
 
 
691
            /* *next == '%' */
 
692
            next++;
 
693
            if(*next == 'r')
 
694
            {
 
695
                int wrote = snprintf(&fname[i], sizeof(fname)-i, "%u", deviceRate);
 
696
                i += minu(wrote, sizeof(fname)-i);
 
697
                next++;
 
698
            }
 
699
            else if(*next == '%')
 
700
            {
 
701
                if(i < sizeof(fname))
 
702
                    fname[i++] = '%';
 
703
                next++;
 
704
            }
 
705
            else
 
706
                ERR("Invalid marker '%%%c'\n", *next);
 
707
            fnamelist = next;
334
708
        }
 
709
        i = minu(i, sizeof(fname)-1);
 
710
        fname[i] = '\0';
 
711
        while(i > 0 && isspace(fname[i-1]))
 
712
            i--;
 
713
        fname[i] = '\0';
335
714
 
336
 
        if(!fname[0])
 
715
        if(fname[0] == '\0')
337
716
            continue;
338
 
        TRACE("Loading %s\n", fname);
 
717
 
 
718
        TRACE("Loading %s...\n", fname);
339
719
        f = fopen(fname, "rb");
340
720
        if(f == NULL)
341
721
        {
343
723
            continue;
344
724
        }
345
725
 
346
 
        failed = AL_FALSE;
347
 
        if(fread(magic, 1, sizeof(magicMarker), f) != sizeof(magicMarker))
348
 
        {
349
 
            ERR("Failed to read magic marker\n");
350
 
            failed = AL_TRUE;
351
 
        }
352
 
        else if(memcmp(magic, magicMarker, sizeof(magicMarker)) != 0)
353
 
        {
354
 
            magic[8] = 0;
355
 
            ERR("Invalid magic marker: \"%s\"\n", magic);
356
 
            failed = AL_TRUE;
357
 
        }
358
 
 
359
 
        if(!failed)
360
 
        {
361
 
            ALushort hrirCount, hrirSize;
362
 
            ALubyte  evCount;
363
 
 
364
 
            newdata.sampleRate  = fgetc(f);
365
 
            newdata.sampleRate |= fgetc(f)<<8;
366
 
            newdata.sampleRate |= fgetc(f)<<16;
367
 
            newdata.sampleRate |= fgetc(f)<<24;
368
 
 
369
 
            hrirCount  = fgetc(f);
370
 
            hrirCount |= fgetc(f)<<8;
371
 
 
372
 
            hrirSize  = fgetc(f);
373
 
            hrirSize |= fgetc(f)<<8;
374
 
 
375
 
            evCount = fgetc(f);
376
 
 
377
 
            if(hrirCount != HRIR_COUNT || hrirSize != HRIR_LENGTH || evCount != ELEV_COUNT)
378
 
            {
379
 
                ERR("Unsupported value: hrirCount=%d (%d), hrirSize=%d (%d), evCount=%d (%d)\n",
380
 
                    hrirCount, HRIR_COUNT, hrirSize, HRIR_LENGTH, evCount, ELEV_COUNT);
381
 
                failed = AL_TRUE;
382
 
            }
383
 
        }
384
 
 
385
 
        if(!failed)
386
 
        {
387
 
            for(i = 0;i < ELEV_COUNT;i++)
388
 
            {
389
 
                ALushort offset;
390
 
                offset  = fgetc(f);
391
 
                offset |= fgetc(f)<<8;
392
 
                if(offset != evOffset[i])
393
 
                {
394
 
                    ERR("Unsupported evOffset[%d] value: %d (%d)\n", i, offset, evOffset[i]);
395
 
                    failed = AL_TRUE;
396
 
                }
397
 
            }
398
 
        }
399
 
 
400
 
        if(!failed)
401
 
        {
402
 
            for(i = 0;i < HRIR_COUNT;i++)
403
 
            {
404
 
                for(j = 0;j < HRIR_LENGTH;j++)
405
 
                {
406
 
                    ALshort coeff;
407
 
                    coeff  = fgetc(f);
408
 
                    coeff |= fgetc(f)<<8;
409
 
                    newdata.coeffs[i][j] = coeff;
410
 
                }
411
 
            }
412
 
            for(i = 0;i < HRIR_COUNT;i++)
413
 
            {
414
 
                ALubyte delay;
415
 
                delay = fgetc(f);
416
 
                newdata.delays[i] = delay;
417
 
                if(delay > maxDelay)
418
 
                {
419
 
                    ERR("Invalid delay[%d]: %d (%d)\n", i, delay, maxDelay);
420
 
                    failed = AL_TRUE;
421
 
                }
422
 
            }
423
 
 
424
 
            if(feof(f))
425
 
            {
426
 
                ERR("Premature end of data\n");
427
 
                failed = AL_TRUE;
428
 
            }
 
726
        if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
 
727
            ERR("Failed to read header from %s\n", fname);
 
728
        else
 
729
        {
 
730
            if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
 
731
            {
 
732
                TRACE("Detected data set format v0\n");
 
733
                Hrtf = LoadHrtf00(f, deviceRate);
 
734
            }
 
735
            else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
 
736
            {
 
737
                TRACE("Detected data set format v1\n");
 
738
                Hrtf = LoadHrtf01(f, deviceRate);
 
739
            }
 
740
            else
 
741
                ERR("Invalid header in %s: \"%.8s\"\n", fname, magic);
429
742
        }
430
743
 
431
744
        fclose(f);
432
745
        f = NULL;
433
746
 
434
 
        if(!failed)
435
 
        {
436
 
            void *temp = realloc(LoadedHrtfs, (NumLoadedHrtfs+1)*sizeof(LoadedHrtfs[0]));
437
 
            if(temp != NULL)
438
 
            {
439
 
                LoadedHrtfs = temp;
440
 
                TRACE("Loaded HRTF support for format: %s %uhz\n",
441
 
                      DevFmtChannelsString(DevFmtStereo), newdata.sampleRate);
442
 
                LoadedHrtfs[NumLoadedHrtfs++] = newdata;
443
 
            }
444
 
        }
445
 
        else
446
 
            ERR("Failed to load %s\n", fname);
447
 
    }
448
 
    free(fnamelist);
449
 
    fnamelist = NULL;
450
 
}
451
 
 
452
 
void FreeHrtf(void)
453
 
{
454
 
    NumLoadedHrtfs = 0;
455
 
    free(LoadedHrtfs);
456
 
    LoadedHrtfs = NULL;
 
747
        if(Hrtf)
 
748
        {
 
749
            Hrtf->next = LoadedHrtfs;
 
750
            LoadedHrtfs = Hrtf;
 
751
            TRACE("Loaded HRTF support for format: %s %uhz\n",
 
752
                  DevFmtChannelsString(DevFmtStereo), Hrtf->sampleRate);
 
753
            return Hrtf;
 
754
        }
 
755
 
 
756
        ERR("Failed to load %s\n", fname);
 
757
    }
 
758
 
 
759
    return NULL;
 
760
}
 
761
 
 
762
const struct Hrtf *GetHrtf(ALCdevice *device)
 
763
{
 
764
    if(device->FmtChans == DevFmtStereo)
 
765
    {
 
766
        struct Hrtf *Hrtf = LoadedHrtfs;
 
767
        while(Hrtf != NULL)
 
768
        {
 
769
            if(device->Frequency == Hrtf->sampleRate)
 
770
                return Hrtf;
 
771
            Hrtf = Hrtf->next;
 
772
        }
 
773
 
 
774
        Hrtf = LoadHrtf(device->Frequency);
 
775
        if(Hrtf != NULL)
 
776
            return Hrtf;
 
777
 
 
778
        if(device->Frequency == DefaultHrtf.sampleRate)
 
779
            return &DefaultHrtf;
 
780
    }
 
781
    ERR("Incompatible format: %s %uhz\n",
 
782
        DevFmtChannelsString(device->FmtChans), device->Frequency);
 
783
    return NULL;
 
784
}
 
785
 
 
786
void FreeHrtfs(void)
 
787
{
 
788
    struct Hrtf *Hrtf = NULL;
 
789
 
 
790
    while((Hrtf=LoadedHrtfs) != NULL)
 
791
    {
 
792
        LoadedHrtfs = Hrtf->next;
 
793
        free((void*)Hrtf->azCount);
 
794
        free((void*)Hrtf->evOffset);
 
795
        free((void*)Hrtf->coeffs);
 
796
        free((void*)Hrtf->delays);
 
797
        free(Hrtf);
 
798
    }
 
799
}
 
800
 
 
801
ALuint GetHrtfIrSize (const struct Hrtf *Hrtf)
 
802
{
 
803
    return Hrtf->irSize;
457
804
}