Use ALsizei for some count/length variables
[openal-soft.git] / Alc / hrtf.c
blobd1cfe6b0c672f06f943bc3af35770c2b8eae198f
1 /**
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <ctype.h>
26 #include "AL/al.h"
27 #include "AL/alc.h"
28 #include "alMain.h"
29 #include "alSource.h"
30 #include "alu.h"
31 #include "bformatdec.h"
32 #include "hrtf.h"
33 #include "alconfig.h"
35 #include "compat.h"
36 #include "almalloc.h"
39 /* Current data set limits defined by the makehrtf utility. */
40 #define MIN_IR_SIZE (8)
41 #define MAX_IR_SIZE (512)
42 #define MOD_IR_SIZE (8)
44 #define MIN_FD_COUNT (1)
45 #define MAX_FD_COUNT (16)
47 #define MIN_FD_DISTANCE (50)
48 #define MAX_FD_DISTANCE (2500)
50 #define MIN_EV_COUNT (5)
51 #define MAX_EV_COUNT (128)
53 #define MIN_AZ_COUNT (1)
54 #define MAX_AZ_COUNT (128)
56 #define MAX_HRIR_DELAY (HRTF_HISTORY_LENGTH-1)
58 struct HrtfEntry {
59 struct HrtfEntry *next;
60 struct Hrtf *handle;
61 char filename[];
64 static const ALchar magicMarker00[8] = "MinPHR00";
65 static const ALchar magicMarker01[8] = "MinPHR01";
66 static const ALchar magicMarker02[8] = "MinPHR02";
68 /* First value for pass-through coefficients (remaining are 0), used for omni-
69 * directional sounds. */
70 static const ALfloat PassthruCoeff = 0.707106781187f/*sqrt(0.5)*/;
72 static ATOMIC_FLAG LoadedHrtfLock = ATOMIC_FLAG_INIT;
73 static struct HrtfEntry *LoadedHrtfs = NULL;
76 /* Calculate the elevation index given the polar elevation in radians. This
77 * will return an index between 0 and (evcount - 1). Assumes the FPU is in
78 * round-to-zero mode.
80 static ALsizei CalcEvIndex(ALsizei evcount, ALfloat ev, ALfloat *mu)
82 ALsizei idx;
83 ev = (F_PI_2+ev) * (evcount-1) / F_PI;
84 idx = mini(fastf2i(ev), evcount-1);
86 *mu = ev - idx;
87 return idx;
90 /* Calculate the azimuth index given the polar azimuth in radians. This will
91 * return an index between 0 and (azcount - 1). Assumes the FPU is in round-to-
92 * zero mode.
94 static ALsizei CalcAzIndex(ALsizei azcount, ALfloat az, ALfloat *mu)
96 ALsizei idx;
97 az = (F_TAU+az) * azcount / F_TAU;
99 idx = fastf2i(az) % azcount;
100 *mu = az - floorf(az);
101 return idx;
104 /* Calculates static HRIR coefficients and delays for the given polar elevation
105 * and azimuth in radians. The coefficients are normalized.
107 void GetHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat spread,
108 ALfloat (*restrict coeffs)[2], ALsizei *delays)
110 ALsizei evidx, azidx, idx[4];
111 ALsizei evoffset;
112 ALfloat emu, amu[2];
113 ALfloat blend[4];
114 ALfloat dirfact;
115 ALsizei i, c;
117 dirfact = 1.0f - (spread / F_TAU);
119 /* Claculate the lower elevation index. */
120 evidx = CalcEvIndex(Hrtf->evCount, elevation, &emu);
121 evoffset = Hrtf->evOffset[evidx];
123 /* Calculate lower azimuth index. */
124 azidx= CalcAzIndex(Hrtf->azCount[evidx], azimuth, &amu[0]);
126 /* Calculate the lower HRIR indices. */
127 idx[0] = evoffset + azidx;
128 idx[1] = evoffset + ((azidx+1) % Hrtf->azCount[evidx]);
129 if(evidx < Hrtf->evCount-1)
131 /* Increment elevation to the next (upper) index. */
132 evidx++;
133 evoffset = Hrtf->evOffset[evidx];
135 /* Calculate upper azimuth index. */
136 azidx = CalcAzIndex(Hrtf->azCount[evidx], azimuth, &amu[1]);
138 /* Calculate the upper HRIR indices. */
139 idx[2] = evoffset + azidx;
140 idx[3] = evoffset + ((azidx+1) % Hrtf->azCount[evidx]);
142 else
144 /* If the lower elevation is the top index, the upper elevation is the
145 * same as the lower.
147 amu[1] = amu[0];
148 idx[2] = idx[0];
149 idx[3] = idx[1];
152 /* Calculate bilinear blending weights, attenuated according to the
153 * directional panning factor.
155 blend[0] = (1.0f-emu) * (1.0f-amu[0]) * dirfact;
156 blend[1] = (1.0f-emu) * ( amu[0]) * dirfact;
157 blend[2] = ( emu) * (1.0f-amu[1]) * dirfact;
158 blend[3] = ( emu) * ( amu[1]) * dirfact;
160 /* Calculate the blended HRIR delays. */
161 delays[0] = fastf2i(
162 Hrtf->delays[idx[0]][0]*blend[0] + Hrtf->delays[idx[1]][0]*blend[1] +
163 Hrtf->delays[idx[2]][0]*blend[2] + Hrtf->delays[idx[3]][0]*blend[3] + 0.5f
165 delays[1] = fastf2i(
166 Hrtf->delays[idx[0]][1]*blend[0] + Hrtf->delays[idx[1]][1]*blend[1] +
167 Hrtf->delays[idx[2]][1]*blend[2] + Hrtf->delays[idx[3]][1]*blend[3] + 0.5f
170 /* Calculate the sample offsets for the HRIR indices. */
171 idx[0] *= Hrtf->irSize;
172 idx[1] *= Hrtf->irSize;
173 idx[2] *= Hrtf->irSize;
174 idx[3] *= Hrtf->irSize;
176 coeffs = ASSUME_ALIGNED(coeffs, 16);
177 /* Calculate the blended HRIR coefficients. */
178 coeffs[0][0] = PassthruCoeff * (1.0f-dirfact);
179 coeffs[0][1] = PassthruCoeff * (1.0f-dirfact);
180 for(i = 1;i < Hrtf->irSize;i++)
182 coeffs[i][0] = 0.0f;
183 coeffs[i][1] = 0.0f;
185 for(c = 0;c < 4;c++)
187 const ALfloat (*restrict srccoeffs)[2] = ASSUME_ALIGNED(Hrtf->coeffs+idx[c], 16);
188 for(i = 0;i < Hrtf->irSize;i++)
190 coeffs[i][0] += srccoeffs[i][0] * blend[c];
191 coeffs[i][1] += srccoeffs[i][1] * blend[c];
197 void BuildBFormatHrtf(const struct Hrtf *Hrtf, DirectHrtfState *state, ALsizei NumChannels, const ALfloat (*restrict AmbiPoints)[2], const ALfloat (*restrict AmbiMatrix)[2][MAX_AMBI_COEFFS], ALsizei AmbiCount)
199 /* Set this to 2 for dual-band HRTF processing. May require a higher quality
200 * band-splitter, or better calculation of the new IR length to deal with the
201 * tail generated by the filter.
203 #define NUM_BANDS 2
204 BandSplitter splitter;
205 ALsizei idx[HRTF_AMBI_MAX_CHANNELS];
206 ALsizei min_delay = HRTF_HISTORY_LENGTH;
207 ALfloat temps[3][HRIR_LENGTH];
208 ALsizei max_length = 0;
209 ALsizei i, c, b;
211 for(c = 0;c < AmbiCount;c++)
213 ALuint evidx, azidx;
214 ALuint evoffset;
215 ALuint azcount;
217 /* Calculate elevation index. */
218 evidx = (ALsizei)floorf((F_PI_2 + AmbiPoints[c][0]) *
219 (Hrtf->evCount-1)/F_PI + 0.5f);
220 evidx = mini(evidx, Hrtf->evCount-1);
222 azcount = Hrtf->azCount[evidx];
223 evoffset = Hrtf->evOffset[evidx];
225 /* Calculate azimuth index for this elevation. */
226 azidx = (ALsizei)floorf((F_TAU+AmbiPoints[c][1]) *
227 azcount/F_TAU + 0.5f) % azcount;
229 /* Calculate indices for left and right channels. */
230 idx[c] = evoffset + azidx;
232 min_delay = mini(min_delay, mini(Hrtf->delays[idx[c]][0], Hrtf->delays[idx[c]][1]));
235 memset(temps, 0, sizeof(temps));
236 bandsplit_init(&splitter, 400.0f / (ALfloat)Hrtf->sampleRate);
237 for(c = 0;c < AmbiCount;c++)
239 const ALfloat (*fir)[2] = &Hrtf->coeffs[idx[c] * Hrtf->irSize];
240 ALsizei ldelay = Hrtf->delays[idx[c]][0] - min_delay;
241 ALsizei rdelay = Hrtf->delays[idx[c]][1] - min_delay;
243 max_length = maxi(max_length,
244 mini(maxi(ldelay, rdelay) + Hrtf->irSize, HRIR_LENGTH)
247 if(NUM_BANDS == 1)
249 for(i = 0;i < NumChannels;++i)
251 ALsizei lidx = ldelay, ridx = rdelay;
252 ALsizei j = 0;
253 while(lidx < HRIR_LENGTH && ridx < HRIR_LENGTH && j < Hrtf->irSize)
255 state->Chan[i].Coeffs[lidx++][0] += fir[j][0] * AmbiMatrix[c][0][i];
256 state->Chan[i].Coeffs[ridx++][1] += fir[j][1] * AmbiMatrix[c][0][i];
257 j++;
261 else
263 /* Band-split left HRIR into low and high frequency responses. */
264 bandsplit_clear(&splitter);
265 for(i = 0;i < Hrtf->irSize;i++)
266 temps[2][i] = fir[i][0];
267 bandsplit_process(&splitter, temps[0], temps[1], temps[2], HRIR_LENGTH);
269 /* Apply left ear response with delay. */
270 for(i = 0;i < NumChannels;++i)
272 for(b = 0;b < NUM_BANDS;b++)
274 ALsizei lidx = ldelay;
275 ALsizei j = 0;
276 while(lidx < HRIR_LENGTH)
277 state->Chan[i].Coeffs[lidx++][0] += temps[b][j++] * AmbiMatrix[c][b][i];
281 /* Band-split right HRIR into low and high frequency responses. */
282 bandsplit_clear(&splitter);
283 for(i = 0;i < Hrtf->irSize;i++)
284 temps[2][i] = fir[i][1];
285 bandsplit_process(&splitter, temps[0], temps[1], temps[2], HRIR_LENGTH);
287 /* Apply right ear response with delay. */
288 for(i = 0;i < NumChannels;++i)
290 for(b = 0;b < NUM_BANDS;b++)
292 ALsizei ridx = rdelay;
293 ALsizei j = 0;
294 while(ridx < HRIR_LENGTH)
295 state->Chan[i].Coeffs[ridx++][1] += temps[b][j++] * AmbiMatrix[c][b][i];
300 /* Round up to the next IR size multiple. */
301 max_length += MOD_IR_SIZE-1;
302 max_length -= max_length%MOD_IR_SIZE;
304 TRACE("Skipped min delay: %d, new combined length: %d\n", min_delay, max_length);
305 state->IrSize = max_length;
306 #undef NUM_BANDS
310 static struct Hrtf *CreateHrtfStore(ALuint rate, ALsizei irSize,
311 ALfloat distance, ALsizei evCount, ALsizei irCount, const ALubyte *azCount,
312 const ALushort *evOffset, const ALfloat (*coeffs)[2], const ALubyte (*delays)[2],
313 const char *filename)
315 struct Hrtf *Hrtf;
316 size_t total;
318 total = sizeof(struct Hrtf);
319 total += sizeof(Hrtf->azCount[0])*evCount;
320 total = RoundUp(total, sizeof(ALushort)); /* Align for ushort fields */
321 total += sizeof(Hrtf->evOffset[0])*evCount;
322 total = RoundUp(total, 16); /* Align for coefficients using SIMD */
323 total += sizeof(Hrtf->coeffs[0])*irSize*irCount;
324 total += sizeof(Hrtf->delays[0])*irCount;
326 Hrtf = al_calloc(16, total);
327 if(Hrtf == NULL)
328 ERR("Out of memory allocating storage for %s.\n", filename);
329 else
331 uintptr_t offset = sizeof(struct Hrtf);
332 char *base = (char*)Hrtf;
333 ALushort *_evOffset;
334 ALubyte *_azCount;
335 ALubyte (*_delays)[2];
336 ALfloat (*_coeffs)[2];
337 ALsizei i;
339 InitRef(&Hrtf->ref, 0);
340 Hrtf->sampleRate = rate;
341 Hrtf->irSize = irSize;
342 Hrtf->distance = distance;
343 Hrtf->evCount = evCount;
345 /* Set up pointers to storage following the main HRTF struct. */
346 _azCount = (ALubyte*)(base + offset);
347 offset += sizeof(_azCount[0])*evCount;
349 offset = RoundUp(offset, sizeof(ALushort)); /* Align for ushort fields */
350 _evOffset = (ALushort*)(base + offset);
351 offset += sizeof(_evOffset[0])*evCount;
353 offset = RoundUp(offset, 16); /* Align for coefficients using SIMD */
354 _coeffs = (ALfloat(*)[2])(base + offset);
355 offset += sizeof(_coeffs[0])*irSize*irCount;
357 _delays = (ALubyte(*)[2])(base + offset);
358 offset += sizeof(_delays[0])*irCount;
360 assert(offset == total);
362 /* Copy input data to storage. */
363 for(i = 0;i < evCount;i++) _azCount[i] = azCount[i];
364 for(i = 0;i < evCount;i++) _evOffset[i] = evOffset[i];
365 for(i = 0;i < irSize*irCount;i++)
367 _coeffs[i][0] = coeffs[i][0];
368 _coeffs[i][1] = coeffs[i][1];
370 for(i = 0;i < irCount;i++)
372 _delays[i][0] = delays[i][0];
373 _delays[i][1] = delays[i][1];
376 /* Finally, assign the storage pointers. */
377 Hrtf->azCount = _azCount;
378 Hrtf->evOffset = _evOffset;
379 Hrtf->coeffs = _coeffs;
380 Hrtf->delays = _delays;
383 return Hrtf;
386 static ALubyte GetLE_ALubyte(const ALubyte **data, size_t *len)
388 ALubyte ret = (*data)[0];
389 *data += 1; *len -= 1;
390 return ret;
393 static ALshort GetLE_ALshort(const ALubyte **data, size_t *len)
395 ALshort ret = (*data)[0] | ((*data)[1]<<8);
396 *data += 2; *len -= 2;
397 return ret;
400 static ALushort GetLE_ALushort(const ALubyte **data, size_t *len)
402 ALushort ret = (*data)[0] | ((*data)[1]<<8);
403 *data += 2; *len -= 2;
404 return ret;
407 static ALint GetLE_ALint24(const ALubyte **data, size_t *len)
409 ALint ret = (*data)[0] | ((*data)[1]<<8) | ((*data)[2]<<16);
410 *data += 3; *len -= 3;
411 return (ret^0x800000) - 0x800000;
414 static ALuint GetLE_ALuint(const ALubyte **data, size_t *len)
416 ALuint ret = (*data)[0] | ((*data)[1]<<8) | ((*data)[2]<<16) | ((*data)[3]<<24);
417 *data += 4; *len -= 4;
418 return ret;
421 static const ALubyte *Get_ALubytePtr(const ALubyte **data, size_t *len, size_t size)
423 const ALubyte *ret = *data;
424 *data += size; *len -= size;
425 return ret;
428 static struct Hrtf *LoadHrtf00(const ALubyte *data, size_t datalen, const char *filename)
430 struct Hrtf *Hrtf = NULL;
431 ALboolean failed = AL_FALSE;
432 ALuint rate = 0;
433 ALushort irCount = 0;
434 ALushort irSize = 0;
435 ALubyte evCount = 0;
436 ALubyte *azCount = NULL;
437 ALushort *evOffset = NULL;
438 ALfloat (*coeffs)[2] = NULL;
439 ALubyte (*delays)[2] = NULL;
440 ALsizei i, j;
442 if(datalen < 9)
444 ERR("Unexpected end of %s data (req %d, rem "SZFMT")\n", filename, 9, datalen);
445 return NULL;
448 rate = GetLE_ALuint(&data, &datalen);
450 irCount = GetLE_ALushort(&data, &datalen);
452 irSize = GetLE_ALushort(&data, &datalen);
454 evCount = GetLE_ALubyte(&data, &datalen);
456 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
458 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
459 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
460 failed = AL_TRUE;
462 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
464 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
465 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
466 failed = AL_TRUE;
468 if(failed)
469 return NULL;
471 if(datalen < evCount*2u)
473 ERR("Unexpected end of %s data (req %d, rem "SZFMT")\n", filename, evCount*2, datalen);
474 return NULL;
477 azCount = malloc(sizeof(azCount[0])*evCount);
478 evOffset = malloc(sizeof(evOffset[0])*evCount);
479 if(azCount == NULL || evOffset == NULL)
481 ERR("Out of memory.\n");
482 failed = AL_TRUE;
485 if(!failed)
487 evOffset[0] = GetLE_ALushort(&data, &datalen);
488 for(i = 1;i < evCount;i++)
490 evOffset[i] = GetLE_ALushort(&data, &datalen);
491 if(evOffset[i] <= evOffset[i-1])
493 ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
494 i, evOffset[i], evOffset[i-1]);
495 failed = AL_TRUE;
498 azCount[i-1] = evOffset[i] - evOffset[i-1];
499 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
501 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
502 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
503 failed = AL_TRUE;
506 if(irCount <= evOffset[i-1])
508 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
509 i-1, evOffset[i-1], irCount);
510 failed = AL_TRUE;
513 azCount[i-1] = irCount - evOffset[i-1];
514 if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
516 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
517 i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
518 failed = AL_TRUE;
522 if(!failed)
524 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
525 delays = malloc(sizeof(delays[0])*irCount);
526 if(coeffs == NULL || delays == NULL)
528 ERR("Out of memory.\n");
529 failed = AL_TRUE;
533 if(!failed)
535 size_t reqsize = 2*irSize*irCount + irCount;
536 if(datalen < reqsize)
538 ERR("Unexpected end of %s data (req "SZFMT", rem "SZFMT")\n",
539 filename, reqsize, datalen);
540 failed = AL_TRUE;
544 if(!failed)
546 for(i = 0;i < irCount;i++)
548 for(j = 0;j < irSize;j++)
549 coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
552 for(i = 0;i < irCount;i++)
554 delays[i][0] = GetLE_ALubyte(&data, &datalen);
555 if(delays[i][0] > MAX_HRIR_DELAY)
557 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
558 failed = AL_TRUE;
563 if(!failed)
565 /* Mirror the left ear responses to the right ear. */
566 for(i = 0;i < evCount;i++)
568 ALushort evoffset = evOffset[i];
569 ALubyte azcount = azCount[i];
570 for(j = 0;j < azcount;j++)
572 ALsizei lidx = evoffset + j;
573 ALsizei ridx = evoffset + ((azcount-j) % azcount);
574 ALsizei k;
576 for(k = 0;k < irSize;k++)
577 coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
578 delays[ridx][1] = delays[lidx][0];
582 Hrtf = CreateHrtfStore(rate, irSize, 0.0f, evCount, irCount, azCount,
583 evOffset, coeffs, delays, filename);
586 free(azCount);
587 free(evOffset);
588 free(coeffs);
589 free(delays);
590 return Hrtf;
593 static struct Hrtf *LoadHrtf01(const ALubyte *data, size_t datalen, const char *filename)
595 struct Hrtf *Hrtf = NULL;
596 ALboolean failed = AL_FALSE;
597 ALuint rate = 0;
598 ALushort irCount = 0;
599 ALushort irSize = 0;
600 ALubyte evCount = 0;
601 const ALubyte *azCount = NULL;
602 ALushort *evOffset = NULL;
603 ALfloat (*coeffs)[2] = NULL;
604 ALubyte (*delays)[2] = NULL;
605 ALsizei i, j;
607 if(datalen < 6)
609 ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, 6, datalen);
610 return NULL;
613 rate = GetLE_ALuint(&data, &datalen);
615 irSize = GetLE_ALubyte(&data, &datalen);
617 evCount = GetLE_ALubyte(&data, &datalen);
619 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
621 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
622 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
623 failed = AL_TRUE;
625 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
627 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
628 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
629 failed = AL_TRUE;
631 if(failed)
632 return NULL;
634 if(datalen < evCount)
636 ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, evCount, datalen);
637 return NULL;
640 azCount = Get_ALubytePtr(&data, &datalen, evCount);
642 evOffset = malloc(sizeof(evOffset[0])*evCount);
643 if(azCount == NULL || evOffset == NULL)
645 ERR("Out of memory.\n");
646 failed = AL_TRUE;
649 if(!failed)
651 for(i = 0;i < evCount;i++)
653 if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
655 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
656 i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
657 failed = AL_TRUE;
662 if(!failed)
664 evOffset[0] = 0;
665 irCount = azCount[0];
666 for(i = 1;i < evCount;i++)
668 evOffset[i] = evOffset[i-1] + azCount[i-1];
669 irCount += azCount[i];
672 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
673 delays = malloc(sizeof(delays[0])*irCount);
674 if(coeffs == NULL || delays == NULL)
676 ERR("Out of memory.\n");
677 failed = AL_TRUE;
681 if(!failed)
683 size_t reqsize = 2*irSize*irCount + irCount;
684 if(datalen < reqsize)
686 ERR("Unexpected end of %s data (req "SZFMT", rem "SZFMT"\n",
687 filename, reqsize, datalen);
688 failed = AL_TRUE;
692 if(!failed)
694 for(i = 0;i < irCount;i++)
696 for(j = 0;j < irSize;j++)
697 coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
700 for(i = 0;i < irCount;i++)
702 delays[i][0] = GetLE_ALubyte(&data, &datalen);
703 if(delays[i][0] > MAX_HRIR_DELAY)
705 ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
706 failed = AL_TRUE;
711 if(!failed)
713 /* Mirror the left ear responses to the right ear. */
714 for(i = 0;i < evCount;i++)
716 ALushort evoffset = evOffset[i];
717 ALubyte azcount = azCount[i];
718 for(j = 0;j < azcount;j++)
720 ALsizei lidx = evoffset + j;
721 ALsizei ridx = evoffset + ((azcount-j) % azcount);
722 ALsizei k;
724 for(k = 0;k < irSize;k++)
725 coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
726 delays[ridx][1] = delays[lidx][0];
730 Hrtf = CreateHrtfStore(rate, irSize, 0.0f, evCount, irCount, azCount,
731 evOffset, coeffs, delays, filename);
734 free(evOffset);
735 free(coeffs);
736 free(delays);
737 return Hrtf;
740 #define SAMPLETYPE_S16 0
741 #define SAMPLETYPE_S24 1
743 #define CHANTYPE_LEFTONLY 0
744 #define CHANTYPE_LEFTRIGHT 1
746 static struct Hrtf *LoadHrtf02(const ALubyte *data, size_t datalen, const char *filename)
748 struct Hrtf *Hrtf = NULL;
749 ALboolean failed = AL_FALSE;
750 ALuint rate = 0;
751 ALubyte sampleType;
752 ALubyte channelType;
753 ALushort irCount = 0;
754 ALushort irSize = 0;
755 ALubyte fdCount = 0;
756 ALushort distance = 0;
757 ALubyte evCount = 0;
758 const ALubyte *azCount = NULL;
759 ALushort *evOffset = NULL;
760 ALfloat (*coeffs)[2] = NULL;
761 ALubyte (*delays)[2] = NULL;
762 ALsizei i, j;
764 if(datalen < 8)
766 ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, 8, datalen);
767 return NULL;
770 rate = GetLE_ALuint(&data, &datalen);
771 sampleType = GetLE_ALubyte(&data, &datalen);
772 channelType = GetLE_ALubyte(&data, &datalen);
774 irSize = GetLE_ALubyte(&data, &datalen);
776 fdCount = GetLE_ALubyte(&data, &datalen);
778 if(sampleType > SAMPLETYPE_S24)
780 ERR("Unsupported sample type: %d\n", sampleType);
781 failed = AL_TRUE;
783 if(channelType > CHANTYPE_LEFTRIGHT)
785 ERR("Unsupported channel type: %d\n", channelType);
786 failed = AL_TRUE;
789 if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
791 ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
792 irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
793 failed = AL_TRUE;
795 if(fdCount != 1)
797 ERR("Multiple field-depths not supported: fdCount=%d (%d to %d)\n",
798 evCount, MIN_FD_COUNT, MAX_FD_COUNT);
799 failed = AL_TRUE;
801 if(failed)
802 return NULL;
804 for(i = 0;i < fdCount;i++)
806 if(datalen < 3)
808 ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, 3, datalen);
809 return NULL;
812 distance = GetLE_ALushort(&data, &datalen);
813 if(distance < MIN_FD_DISTANCE || distance > MAX_FD_DISTANCE)
815 ERR("Unsupported field distance: distance=%d (%dmm to %dmm)\n",
816 distance, MIN_FD_DISTANCE, MAX_FD_DISTANCE);
817 failed = AL_TRUE;
820 evCount = GetLE_ALubyte(&data, &datalen);
821 if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
823 ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
824 evCount, MIN_EV_COUNT, MAX_EV_COUNT);
825 failed = AL_TRUE;
827 if(failed)
828 return NULL;
830 if(datalen < evCount)
832 ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, evCount, datalen);
833 return NULL;
836 azCount = Get_ALubytePtr(&data, &datalen, evCount);
837 for(j = 0;j < evCount;j++)
839 if(azCount[j] < MIN_AZ_COUNT || azCount[j] > MAX_AZ_COUNT)
841 ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
842 j, azCount[j], MIN_AZ_COUNT, MAX_AZ_COUNT);
843 failed = AL_TRUE;
847 if(failed)
848 return NULL;
850 evOffset = malloc(sizeof(evOffset[0])*evCount);
851 if(azCount == NULL || evOffset == NULL)
853 ERR("Out of memory.\n");
854 failed = AL_TRUE;
857 if(!failed)
859 evOffset[0] = 0;
860 irCount = azCount[0];
861 for(i = 1;i < evCount;i++)
863 evOffset[i] = evOffset[i-1] + azCount[i-1];
864 irCount += azCount[i];
867 coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
868 delays = malloc(sizeof(delays[0])*irCount);
869 if(coeffs == NULL || delays == NULL)
871 ERR("Out of memory.\n");
872 failed = AL_TRUE;
876 if(!failed)
878 size_t reqsize = 2*irSize*irCount + irCount;
879 if(datalen < reqsize)
881 ERR("Unexpected end of %s data (req "SZFMT", rem "SZFMT"\n",
882 filename, reqsize, datalen);
883 failed = AL_TRUE;
887 if(!failed)
889 if(channelType == CHANTYPE_LEFTONLY)
891 if(sampleType == SAMPLETYPE_S16)
892 for(i = 0;i < irCount;i++)
894 for(j = 0;j < irSize;j++)
895 coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
897 else if(sampleType == SAMPLETYPE_S24)
898 for(i = 0;i < irCount;i++)
900 for(j = 0;j < irSize;j++)
901 coeffs[i*irSize + j][0] = GetLE_ALint24(&data, &datalen) / 8388608.0f;
904 for(i = 0;i < irCount;i++)
906 delays[i][0] = GetLE_ALubyte(&data, &datalen);
907 if(delays[i][0] > MAX_HRIR_DELAY)
909 ERR("Invalid delays[%d][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
910 failed = AL_TRUE;
914 else if(channelType == CHANTYPE_LEFTRIGHT)
916 if(sampleType == SAMPLETYPE_S16)
917 for(i = 0;i < irCount;i++)
919 for(j = 0;j < irSize;j++)
921 coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
922 coeffs[i*irSize + j][1] = GetLE_ALshort(&data, &datalen) / 32768.0f;
925 else if(sampleType == SAMPLETYPE_S24)
926 for(i = 0;i < irCount;i++)
928 for(j = 0;j < irSize;j++)
930 coeffs[i*irSize + j][0] = GetLE_ALint24(&data, &datalen) / 8388608.0f;
931 coeffs[i*irSize + j][1] = GetLE_ALint24(&data, &datalen) / 8388608.0f;
935 for(i = 0;i < irCount;i++)
937 delays[i][0] = GetLE_ALubyte(&data, &datalen);
938 if(delays[i][0] > MAX_HRIR_DELAY)
940 ERR("Invalid delays[%d][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
941 failed = AL_TRUE;
943 delays[i][1] = GetLE_ALubyte(&data, &datalen);
944 if(delays[i][1] > MAX_HRIR_DELAY)
946 ERR("Invalid delays[%d][1]: %d (%d)\n", i, delays[i][1], MAX_HRIR_DELAY);
947 failed = AL_TRUE;
953 if(!failed)
955 if(channelType == CHANTYPE_LEFTONLY)
957 /* Mirror the left ear responses to the right ear. */
958 for(i = 0;i < evCount;i++)
960 ALushort evoffset = evOffset[i];
961 ALubyte azcount = azCount[i];
962 for(j = 0;j < azcount;j++)
964 ALsizei lidx = evoffset + j;
965 ALsizei ridx = evoffset + ((azcount-j) % azcount);
966 ALsizei k;
968 for(k = 0;k < irSize;k++)
969 coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
970 delays[ridx][1] = delays[lidx][0];
975 Hrtf = CreateHrtfStore(rate, irSize,
976 (ALfloat)distance / 1000.0f, evCount, irCount, azCount, evOffset,
977 coeffs, delays, filename
981 free(evOffset);
982 free(coeffs);
983 free(delays);
984 return Hrtf;
988 static void AddFileEntry(vector_EnumeratedHrtf *list, const_al_string filename)
990 EnumeratedHrtf entry = { AL_STRING_INIT_STATIC(), NULL };
991 struct HrtfEntry *loaded_entry;
992 const EnumeratedHrtf *iter;
993 const char *name;
994 const char *ext;
995 int i;
997 /* Check if this file has already been loaded globally. */
998 loaded_entry = LoadedHrtfs;
999 while(loaded_entry)
1001 if(alstr_cmp_cstr(filename, loaded_entry->filename) == 0)
1003 /* Check if this entry has already been added to the list. */
1004 #define MATCH_ENTRY(i) (loaded_entry == (i)->hrtf)
1005 VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_ENTRY);
1006 if(iter != VECTOR_END(*list))
1008 TRACE("Skipping duplicate file entry %s\n", alstr_get_cstr(filename));
1009 return;
1011 #undef MATCH_FNAME
1013 break;
1015 loaded_entry = loaded_entry->next;
1018 if(!loaded_entry)
1020 TRACE("Got new file \"%s\"\n", alstr_get_cstr(filename));
1022 loaded_entry = al_calloc(DEF_ALIGN,
1023 FAM_SIZE(struct HrtfEntry, filename, alstr_length(filename)+1)
1025 loaded_entry->next = LoadedHrtfs;
1026 loaded_entry->handle = NULL;
1027 strcpy(loaded_entry->filename, alstr_get_cstr(filename));
1028 LoadedHrtfs = loaded_entry;
1031 /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
1032 * format update). */
1033 name = strrchr(alstr_get_cstr(filename), '/');
1034 if(!name) name = strrchr(alstr_get_cstr(filename), '\\');
1035 if(!name) name = alstr_get_cstr(filename);
1036 else ++name;
1038 ext = strrchr(name, '.');
1040 i = 0;
1041 do {
1042 if(!ext)
1043 alstr_copy_cstr(&entry.name, name);
1044 else
1045 alstr_copy_range(&entry.name, name, ext);
1046 if(i != 0)
1048 char str[64];
1049 snprintf(str, sizeof(str), " #%d", i+1);
1050 alstr_append_cstr(&entry.name, str);
1052 ++i;
1054 #define MATCH_NAME(i) (alstr_cmp(entry.name, (i)->name) == 0)
1055 VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_NAME);
1056 #undef MATCH_NAME
1057 } while(iter != VECTOR_END(*list));
1058 entry.hrtf = loaded_entry;
1060 TRACE("Adding entry \"%s\" from file \"%s\"\n", alstr_get_cstr(entry.name),
1061 alstr_get_cstr(filename));
1062 VECTOR_PUSH_BACK(*list, entry);
1065 /* Unfortunate that we have to duplicate AddFileEntry to take a memory buffer
1066 * for input instead of opening the given filename.
1068 static void AddBuiltInEntry(vector_EnumeratedHrtf *list, const_al_string filename, ALuint residx)
1070 EnumeratedHrtf entry = { AL_STRING_INIT_STATIC(), NULL };
1071 struct HrtfEntry *loaded_entry;
1072 struct Hrtf *hrtf = NULL;
1073 const EnumeratedHrtf *iter;
1074 const char *name;
1075 const char *ext;
1076 int i;
1078 loaded_entry = LoadedHrtfs;
1079 while(loaded_entry)
1081 if(alstr_cmp_cstr(filename, loaded_entry->filename) == 0)
1083 #define MATCH_ENTRY(i) (loaded_entry == (i)->hrtf)
1084 VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_ENTRY);
1085 if(iter != VECTOR_END(*list))
1087 TRACE("Skipping duplicate file entry %s\n", alstr_get_cstr(filename));
1088 return;
1090 #undef MATCH_FNAME
1092 break;
1094 loaded_entry = loaded_entry->next;
1097 if(!loaded_entry)
1099 size_t namelen = alstr_length(filename)+32;
1101 TRACE("Got new file \"%s\"\n", alstr_get_cstr(filename));
1103 loaded_entry = al_calloc(DEF_ALIGN,
1104 FAM_SIZE(struct HrtfEntry, filename, namelen)
1106 loaded_entry->next = LoadedHrtfs;
1107 loaded_entry->handle = hrtf;
1108 snprintf(loaded_entry->filename, namelen, "!%u_%s",
1109 residx, alstr_get_cstr(filename));
1110 LoadedHrtfs = loaded_entry;
1113 /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
1114 * format update). */
1115 name = strrchr(alstr_get_cstr(filename), '/');
1116 if(!name) name = strrchr(alstr_get_cstr(filename), '\\');
1117 if(!name) name = alstr_get_cstr(filename);
1118 else ++name;
1120 ext = strrchr(name, '.');
1122 i = 0;
1123 do {
1124 if(!ext)
1125 alstr_copy_cstr(&entry.name, name);
1126 else
1127 alstr_copy_range(&entry.name, name, ext);
1128 if(i != 0)
1130 char str[64];
1131 snprintf(str, sizeof(str), " #%d", i+1);
1132 alstr_append_cstr(&entry.name, str);
1134 ++i;
1136 #define MATCH_NAME(i) (alstr_cmp(entry.name, (i)->name) == 0)
1137 VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_NAME);
1138 #undef MATCH_NAME
1139 } while(iter != VECTOR_END(*list));
1140 entry.hrtf = loaded_entry;
1142 TRACE("Adding built-in entry \"%s\"\n", alstr_get_cstr(entry.name));
1143 VECTOR_PUSH_BACK(*list, entry);
1147 #define IDR_DEFAULT_44100_MHR 1
1148 #define IDR_DEFAULT_48000_MHR 2
1150 #ifndef ALSOFT_EMBED_HRTF_DATA
1152 static const ALubyte *GetResource(int UNUSED(name), size_t *size)
1154 *size = 0;
1155 return NULL;
1158 #else
1160 #include "default-44100.mhr.h"
1161 #include "default-48000.mhr.h"
1163 static const ALubyte *GetResource(int name, size_t *size)
1165 if(name == IDR_DEFAULT_44100_MHR)
1167 *size = sizeof(hrtf_default_44100);
1168 return hrtf_default_44100;
1170 if(name == IDR_DEFAULT_48000_MHR)
1172 *size = sizeof(hrtf_default_48000);
1173 return hrtf_default_48000;
1175 *size = 0;
1176 return NULL;
1178 #endif
1180 vector_EnumeratedHrtf EnumerateHrtf(const_al_string devname)
1182 vector_EnumeratedHrtf list = VECTOR_INIT_STATIC();
1183 const char *defaulthrtf = "";
1184 const char *pathlist = "";
1185 bool usedefaults = true;
1187 if(ConfigValueStr(alstr_get_cstr(devname), NULL, "hrtf-paths", &pathlist))
1189 al_string pname = AL_STRING_INIT_STATIC();
1190 while(pathlist && *pathlist)
1192 const char *next, *end;
1194 while(isspace(*pathlist) || *pathlist == ',')
1195 pathlist++;
1196 if(*pathlist == '\0')
1197 continue;
1199 next = strchr(pathlist, ',');
1200 if(next)
1201 end = next++;
1202 else
1204 end = pathlist + strlen(pathlist);
1205 usedefaults = false;
1208 while(end != pathlist && isspace(*(end-1)))
1209 --end;
1210 if(end != pathlist)
1212 vector_al_string flist;
1213 size_t i;
1215 alstr_copy_range(&pname, pathlist, end);
1217 flist = SearchDataFiles(".mhr", alstr_get_cstr(pname));
1218 for(i = 0;i < VECTOR_SIZE(flist);i++)
1219 AddFileEntry(&list, VECTOR_ELEM(flist, i));
1220 VECTOR_FOR_EACH(al_string, flist, alstr_reset);
1221 VECTOR_DEINIT(flist);
1224 pathlist = next;
1227 alstr_reset(&pname);
1229 else if(ConfigValueExists(alstr_get_cstr(devname), NULL, "hrtf_tables"))
1230 ERR("The hrtf_tables option is deprecated, please use hrtf-paths instead.\n");
1232 if(usedefaults)
1234 al_string ename = AL_STRING_INIT_STATIC();
1235 vector_al_string flist;
1236 const ALubyte *rdata;
1237 size_t rsize, i;
1239 flist = SearchDataFiles(".mhr", "openal/hrtf");
1240 for(i = 0;i < VECTOR_SIZE(flist);i++)
1241 AddFileEntry(&list, VECTOR_ELEM(flist, i));
1242 VECTOR_FOR_EACH(al_string, flist, alstr_reset);
1243 VECTOR_DEINIT(flist);
1245 rdata = GetResource(IDR_DEFAULT_44100_MHR, &rsize);
1246 if(rdata != NULL && rsize > 0)
1248 alstr_copy_cstr(&ename, "Built-In 44100hz");
1249 AddBuiltInEntry(&list, ename, IDR_DEFAULT_44100_MHR);
1252 rdata = GetResource(IDR_DEFAULT_48000_MHR, &rsize);
1253 if(rdata != NULL && rsize > 0)
1255 alstr_copy_cstr(&ename, "Built-In 48000hz");
1256 AddBuiltInEntry(&list, ename, IDR_DEFAULT_48000_MHR);
1258 alstr_reset(&ename);
1261 if(VECTOR_SIZE(list) > 1 && ConfigValueStr(alstr_get_cstr(devname), NULL, "default-hrtf", &defaulthrtf))
1263 const EnumeratedHrtf *iter;
1264 /* Find the preferred HRTF and move it to the front of the list. */
1265 #define FIND_ENTRY(i) (alstr_cmp_cstr((i)->name, defaulthrtf) == 0)
1266 VECTOR_FIND_IF(iter, const EnumeratedHrtf, list, FIND_ENTRY);
1267 #undef FIND_ENTRY
1268 if(iter == VECTOR_END(list))
1269 WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf);
1270 else if(iter != VECTOR_BEGIN(list))
1272 EnumeratedHrtf entry = *iter;
1273 memmove(&VECTOR_ELEM(list,1), &VECTOR_ELEM(list,0),
1274 (iter-VECTOR_BEGIN(list))*sizeof(EnumeratedHrtf));
1275 VECTOR_ELEM(list,0) = entry;
1279 return list;
1282 void FreeHrtfList(vector_EnumeratedHrtf *list)
1284 #define CLEAR_ENTRY(i) alstr_reset(&(i)->name)
1285 VECTOR_FOR_EACH(EnumeratedHrtf, *list, CLEAR_ENTRY);
1286 VECTOR_DEINIT(*list);
1287 #undef CLEAR_ENTRY
1290 struct Hrtf *GetLoadedHrtf(struct HrtfEntry *entry)
1292 struct Hrtf *hrtf = NULL;
1293 struct FileMapping fmap;
1294 const ALubyte *rdata;
1295 const char *name;
1296 ALuint residx;
1297 size_t rsize;
1298 char ch;
1300 while(ATOMIC_FLAG_TEST_AND_SET(&LoadedHrtfLock, almemory_order_seq_cst))
1301 althrd_yield();
1303 if(entry->handle)
1305 hrtf = entry->handle;
1306 Hrtf_IncRef(hrtf);
1307 goto done;
1310 fmap.ptr = NULL;
1311 fmap.len = 0;
1312 if(sscanf(entry->filename, "!%u%c", &residx, &ch) == 2 && ch == '_')
1314 name = strchr(entry->filename, ch)+1;
1316 TRACE("Loading %s...\n", name);
1317 rdata = GetResource(residx, &rsize);
1318 if(rdata == NULL || rsize == 0)
1320 ERR("Could not get resource %u, %s\n", residx, name);
1321 goto done;
1324 else
1326 name = entry->filename;
1328 TRACE("Loading %s...\n", entry->filename);
1329 fmap = MapFileToMem(entry->filename);
1330 if(fmap.ptr == NULL)
1332 ERR("Could not open %s\n", entry->filename);
1333 goto done;
1336 rdata = fmap.ptr;
1337 rsize = fmap.len;
1340 if(rsize < sizeof(magicMarker02))
1341 ERR("%s data is too short ("SZFMT" bytes)\n", name, rsize);
1342 else if(memcmp(rdata, magicMarker02, sizeof(magicMarker02)) == 0)
1344 TRACE("Detected data set format v2\n");
1345 hrtf = LoadHrtf02(rdata+sizeof(magicMarker02),
1346 rsize-sizeof(magicMarker02), name
1349 else if(memcmp(rdata, magicMarker01, sizeof(magicMarker01)) == 0)
1351 TRACE("Detected data set format v1\n");
1352 hrtf = LoadHrtf01(rdata+sizeof(magicMarker01),
1353 rsize-sizeof(magicMarker01), name
1356 else if(memcmp(rdata, magicMarker00, sizeof(magicMarker00)) == 0)
1358 TRACE("Detected data set format v0\n");
1359 hrtf = LoadHrtf00(rdata+sizeof(magicMarker00),
1360 rsize-sizeof(magicMarker00), name
1363 else
1364 ERR("Invalid header in %s: \"%.8s\"\n", name, (const char*)rdata);
1365 if(fmap.ptr)
1366 UnmapFileMem(&fmap);
1368 if(!hrtf)
1370 ERR("Failed to load %s\n", name);
1371 goto done;
1373 entry->handle = hrtf;
1374 Hrtf_IncRef(hrtf);
1376 TRACE("Loaded HRTF support for format: %s %uhz\n",
1377 DevFmtChannelsString(DevFmtStereo), hrtf->sampleRate);
1379 done:
1380 ATOMIC_FLAG_CLEAR(&LoadedHrtfLock, almemory_order_seq_cst);
1381 return hrtf;
1385 void Hrtf_IncRef(struct Hrtf *hrtf)
1387 uint ref = IncrementRef(&hrtf->ref);
1388 TRACEREF("%p increasing refcount to %u\n", hrtf, ref);
1391 void Hrtf_DecRef(struct Hrtf *hrtf)
1393 struct HrtfEntry *Hrtf;
1394 uint ref = DecrementRef(&hrtf->ref);
1395 TRACEREF("%p decreasing refcount to %u\n", hrtf, ref);
1396 if(ref == 0)
1398 while(ATOMIC_FLAG_TEST_AND_SET(&LoadedHrtfLock, almemory_order_seq_cst))
1399 althrd_yield();
1401 Hrtf = LoadedHrtfs;
1402 while(Hrtf != NULL)
1404 /* Need to double-check that it's still unused, as another device
1405 * could've reacquired this HRTF after its reference went to 0 and
1406 * before the lock was taken.
1408 if(hrtf == Hrtf->handle && ReadRef(&hrtf->ref) == 0)
1410 al_free(Hrtf->handle);
1411 Hrtf->handle = NULL;
1412 TRACE("Unloaded unused HRTF %s\n", Hrtf->filename);
1414 Hrtf = Hrtf->next;
1417 ATOMIC_FLAG_CLEAR(&LoadedHrtfLock, almemory_order_seq_cst);
1422 void FreeHrtfs(void)
1424 struct HrtfEntry *Hrtf = LoadedHrtfs;
1425 LoadedHrtfs = NULL;
1427 while(Hrtf != NULL)
1429 struct HrtfEntry *next = Hrtf->next;
1430 al_free(Hrtf->handle);
1431 al_free(Hrtf);
1432 Hrtf = next;