Fix reverb with UHJ encoding
[openal-soft.git] / Alc / hrtf.c
blob49c9793b8329e815eaacdd083ca04e57e586e7d5
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 "hrtf.h"
33 #include "compat.h"
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)
47 struct Hrtf {
48 ALuint sampleRate;
49 ALuint irSize;
50 ALubyte evCount;
52 const ALubyte *azCount;
53 const ALushort *evOffset;
54 const ALshort *coeffs;
55 const ALubyte *delays;
57 const char *filename;
58 struct Hrtf *next;
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];
103 ALuint i;
105 /* Claculate elevation indices and interpolation factor. */
106 CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
108 for(i = 0;i < 2;i++)
110 ALuint azcount = Hrtf->azCount[evidx[i]];
111 ALuint evoffset = Hrtf->evOffset[evidx[i]];
112 ALuint azidx[2];
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.
152 if(gain > 0.0001f)
154 ALfloat c;
156 i = 0;
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);
174 else
176 for(i = 0;i < Hrtf->irSize;i++)
178 coeffs[i][0] = 0.0f;
179 coeffs[i][1] = 0.0f;
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;
191 ALushort irSize = 0;
192 ALubyte evCount = 0;
193 ALubyte *azCount = NULL;
194 ALushort *evOffset = NULL;
195 ALshort *coeffs = NULL;
196 ALubyte *delays = NULL;
197 ALuint i, j;
199 rate = fgetc(f);
200 rate |= fgetc(f)<<8;
201 rate |= fgetc(f)<<16;
202 rate |= fgetc(f)<<24;
204 irCount = fgetc(f);
205 irCount |= fgetc(f)<<8;
207 irSize = fgetc(f);
208 irSize |= fgetc(f)<<8;
210 evCount = fgetc(f);
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);
216 failed = AL_TRUE;
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);
222 failed = AL_TRUE;
225 if(failed)
226 return NULL;
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");
233 failed = AL_TRUE;
236 if(!failed)
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]);
248 failed = AL_TRUE;
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);
256 failed = AL_TRUE;
259 if(irCount <= evOffset[i-1])
261 ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
262 i-1, evOffset[i-1], irCount);
263 failed = AL_TRUE;
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);
271 failed = AL_TRUE;
275 if(!failed)
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");
282 failed = AL_TRUE;
286 if(!failed)
288 for(i = 0;i < irCount*irSize;i+=irSize)
290 for(j = 0;j < irSize;j++)
292 ALshort coeff;
293 coeff = fgetc(f);
294 coeff |= fgetc(f)<<8;
295 coeffs[i+j] = coeff;
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);
304 failed = AL_TRUE;
308 if(feof(f))
310 ERR("Premature end of data\n");
311 failed = AL_TRUE;
315 if(!failed)
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);
325 if(Hrtf == NULL)
327 ERR("Out of memory.\n");
328 failed = AL_TRUE;
332 if(!failed)
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));
342 Hrtf->next = NULL;
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);
351 free(azCount);
352 free(evOffset);
353 free(coeffs);
354 free(delays);
355 return Hrtf;
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;
370 ALuint i, j;
372 rate = fgetc(f);
373 rate |= fgetc(f)<<8;
374 rate |= fgetc(f)<<16;
375 rate |= fgetc(f)<<24;
377 irSize = fgetc(f);
379 evCount = fgetc(f);
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);
385 failed = AL_TRUE;
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);
391 failed = AL_TRUE;
394 if(failed)
395 return NULL;
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");
402 failed = AL_TRUE;
405 if(!failed)
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);
414 failed = AL_TRUE;
419 if(!failed)
421 evOffset[0] = 0;
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");
434 failed = AL_TRUE;
438 if(!failed)
440 for(i = 0;i < irCount*irSize;i+=irSize)
442 for(j = 0;j < irSize;j++)
444 ALshort coeff;
445 coeff = fgetc(f);
446 coeff |= fgetc(f)<<8;
447 coeffs[i+j] = coeff;
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);
456 failed = AL_TRUE;
460 if(feof(f))
462 ERR("Premature end of data\n");
463 failed = AL_TRUE;
467 if(!failed)
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);
477 if(Hrtf == NULL)
479 ERR("Out of memory.\n");
480 failed = AL_TRUE;
484 if(!failed)
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));
494 Hrtf->next = NULL;
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);
503 free(azCount);
504 free(evOffset);
505 free(coeffs);
506 free(delays);
507 return Hrtf;
511 static void AddFileEntry(vector_HrtfEntry *list, al_string *filename)
513 HrtfEntry entry = { AL_STRING_INIT_STATIC(), NULL };
514 struct Hrtf *hrtf = NULL;
515 const HrtfEntry *iter;
516 const char *name;
517 const char *ext;
518 ALchar magic[8];
519 FILE *f;
520 int i;
522 name = strrchr(al_string_get_cstr(*filename), '/');
523 if(!name) name = strrchr(al_string_get_cstr(*filename), '\\');
524 if(!name) name = al_string_get_cstr(*filename);
525 else ++name;
527 entry.hrtf = LoadedHrtfs;
528 while(entry.hrtf)
530 if(al_string_cmp_cstr(*filename, entry.hrtf->filename) == 0)
532 TRACE("Skipping duplicate file entry %s\n", al_string_get_cstr(*filename));
533 goto done;
535 entry.hrtf = entry.hrtf->next;
538 TRACE("Loading %s...\n", al_string_get_cstr(*filename));
539 f = al_fopen(al_string_get_cstr(*filename), "rb");
540 if(f == NULL)
542 ERR("Could not open %s\n", al_string_get_cstr(*filename));
543 goto done;
546 if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
547 ERR("Failed to read header from %s\n", al_string_get_cstr(*filename));
548 else
550 if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
552 TRACE("Detected data set format v0\n");
553 hrtf = LoadHrtf00(f, *filename);
555 else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
557 TRACE("Detected data set format v1\n");
558 hrtf = LoadHrtf01(f, *filename);
560 else
561 ERR("Invalid header in %s: \"%.8s\"\n", al_string_get_cstr(*filename), magic);
563 fclose(f);
565 if(!hrtf)
567 ERR("Failed to load %s\n", al_string_get_cstr(*filename));
568 goto done;
571 hrtf->next = LoadedHrtfs;
572 LoadedHrtfs = hrtf;
573 TRACE("Loaded HRTF support for format: %s %uhz\n",
574 DevFmtChannelsString(DevFmtStereo), hrtf->sampleRate);
575 entry.hrtf = hrtf;
577 /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
578 * format update). */
579 ext = strrchr(name, '.');
581 i = 0;
582 do {
583 if(!ext)
584 al_string_copy_cstr(&entry.name, name);
585 else
586 al_string_copy_range(&entry.name, name, ext);
587 if(i != 0)
589 char str[64];
590 snprintf(str, sizeof(str), " #%d", i+1);
591 al_string_append_cstr(&entry.name, str);
593 ++i;
595 #define MATCH_NAME(i) (al_string_cmp(entry.name, (i)->name) == 0)
596 VECTOR_FIND_IF(iter, const HrtfEntry, *list, MATCH_NAME);
597 #undef MATCH_NAME
598 } while(iter != VECTOR_ITER_END(*list));
600 TRACE("Adding entry \"%s\" from file \"%s\"\n", al_string_get_cstr(entry.name),
601 al_string_get_cstr(*filename));
602 VECTOR_PUSH_BACK(*list, entry);
604 done:
605 al_string_deinit(filename);
608 vector_HrtfEntry EnumerateHrtf(const_al_string devname)
610 vector_HrtfEntry list = VECTOR_INIT_STATIC();
611 const char *defaulthrtf = "";
612 const char *pathlist = "";
613 bool usedefaults = true;
615 if(ConfigValueStr(al_string_get_cstr(devname), NULL, "hrtf-paths", &pathlist))
617 while(pathlist && *pathlist)
619 const char *next, *end;
621 while(isspace(*pathlist) || *pathlist == ',')
622 pathlist++;
623 if(*pathlist == '\0')
624 continue;
626 next = strchr(pathlist, ',');
627 if(next)
628 end = next++;
629 else
631 end = pathlist + strlen(pathlist);
632 usedefaults = false;
635 while(end != pathlist && isspace(*(end-1)))
636 --end;
637 if(end != pathlist)
639 al_string pname = AL_STRING_INIT_STATIC();
640 vector_al_string flist;
642 al_string_append_range(&pname, pathlist, end);
644 flist = SearchDataFiles(".mhr", al_string_get_cstr(pname));
645 VECTOR_FOR_EACH_PARAMS(al_string, flist, AddFileEntry, &list);
646 VECTOR_DEINIT(flist);
648 al_string_deinit(&pname);
651 pathlist = next;
654 else if(ConfigValueExists(al_string_get_cstr(devname), NULL, "hrtf_tables"))
655 ERR("The hrtf_tables option is deprecated, please use hrtf-paths instead.\n");
657 if(usedefaults)
659 vector_al_string flist = SearchDataFiles(".mhr", "openal/hrtf");
660 VECTOR_FOR_EACH_PARAMS(al_string, flist, AddFileEntry, &list);
661 VECTOR_DEINIT(flist);
664 if(VECTOR_SIZE(list) > 1 && ConfigValueStr(al_string_get_cstr(devname), NULL, "default-hrtf", &defaulthrtf))
666 const HrtfEntry *iter;
667 /* Find the preferred HRTF and move it to the front of the list. */
668 #define FIND_ENTRY(i) (al_string_cmp_cstr((i)->name, defaulthrtf) == 0)
669 VECTOR_FIND_IF(iter, const HrtfEntry, list, FIND_ENTRY);
670 if(iter != VECTOR_ITER_END(list) && iter != VECTOR_ITER_BEGIN(list))
672 HrtfEntry entry = *iter;
673 memmove(&VECTOR_ELEM(list,1), &VECTOR_ELEM(list,0),
674 (iter-VECTOR_ITER_BEGIN(list))*sizeof(HrtfEntry));
675 VECTOR_ELEM(list,0) = entry;
677 else
678 WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf);
679 #undef FIND_ENTRY
682 return list;
685 void FreeHrtfList(vector_HrtfEntry *list)
687 #define CLEAR_ENTRY(i) do { \
688 al_string_deinit(&(i)->name); \
689 } while(0)
690 VECTOR_FOR_EACH(HrtfEntry, *list, CLEAR_ENTRY);
691 VECTOR_DEINIT(*list);
692 #undef CLEAR_ENTRY
696 ALuint GetHrtfSampleRate(const struct Hrtf *Hrtf)
698 return Hrtf->sampleRate;
701 ALuint GetHrtfIrSize(const struct Hrtf *Hrtf)
703 return Hrtf->irSize;
707 void FreeHrtfs(void)
709 struct Hrtf *Hrtf = LoadedHrtfs;
710 LoadedHrtfs = NULL;
712 while(Hrtf != NULL)
714 struct Hrtf *next = Hrtf->next;
715 free(Hrtf);
716 Hrtf = next;