hrtf.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include "AL/al.h"
  24. #include "AL/alc.h"
  25. #include "alMain.h"
  26. #include "alSource.h"
  27. #include "alu.h"
  28. #include "hrtf.h"
  29. #include "alconfig.h"
  30. #include "filters/splitter.h"
  31. #include "compat.h"
  32. #include "almalloc.h"
  33. /* Current data set limits defined by the makehrtf utility. */
  34. #define MIN_IR_SIZE (8)
  35. #define MAX_IR_SIZE (512)
  36. #define MOD_IR_SIZE (8)
  37. #define MIN_FD_COUNT (1)
  38. #define MAX_FD_COUNT (16)
  39. #define MIN_FD_DISTANCE (50)
  40. #define MAX_FD_DISTANCE (2500)
  41. #define MIN_EV_COUNT (5)
  42. #define MAX_EV_COUNT (128)
  43. #define MIN_AZ_COUNT (1)
  44. #define MAX_AZ_COUNT (128)
  45. #define MAX_HRIR_DELAY (HRTF_HISTORY_LENGTH-1)
  46. struct HrtfEntry {
  47. struct HrtfEntry *next;
  48. struct Hrtf *handle;
  49. char filename[];
  50. };
  51. static const ALchar magicMarker00[8] = "MinPHR00";
  52. static const ALchar magicMarker01[8] = "MinPHR01";
  53. static const ALchar magicMarker02[8] = "MinPHR02";
  54. /* First value for pass-through coefficients (remaining are 0), used for omni-
  55. * directional sounds. */
  56. static const ALfloat PassthruCoeff = 0.707106781187f/*sqrt(0.5)*/;
  57. static ATOMIC_FLAG LoadedHrtfLock = ATOMIC_FLAG_INIT;
  58. static struct HrtfEntry *LoadedHrtfs = NULL;
  59. /* Calculate the elevation index given the polar elevation in radians. This
  60. * will return an index between 0 and (evcount - 1).
  61. */
  62. static ALsizei CalcEvIndex(ALsizei evcount, ALfloat ev, ALfloat *mu)
  63. {
  64. ALsizei idx;
  65. ev = (F_PI_2+ev) * (evcount-1) / F_PI;
  66. idx = float2int(ev);
  67. *mu = ev - idx;
  68. return mini(idx, evcount-1);
  69. }
  70. /* Calculate the azimuth index given the polar azimuth in radians. This will
  71. * return an index between 0 and (azcount - 1).
  72. */
  73. static ALsizei CalcAzIndex(ALsizei azcount, ALfloat az, ALfloat *mu)
  74. {
  75. ALsizei idx;
  76. az = (F_TAU+az) * azcount / F_TAU;
  77. idx = float2int(az);
  78. *mu = az - idx;
  79. return idx % azcount;
  80. }
  81. /* Calculates static HRIR coefficients and delays for the given polar elevation
  82. * and azimuth in radians. The coefficients are normalized.
  83. */
  84. void GetHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat spread,
  85. ALfloat (*restrict coeffs)[2], ALsizei *delays)
  86. {
  87. ALsizei evidx, azidx, idx[4];
  88. ALsizei evoffset;
  89. ALfloat emu, amu[2];
  90. ALfloat blend[4];
  91. ALfloat dirfact;
  92. ALsizei i, c;
  93. dirfact = 1.0f - (spread / F_TAU);
  94. /* Claculate the lower elevation index. */
  95. evidx = CalcEvIndex(Hrtf->evCount, elevation, &emu);
  96. evoffset = Hrtf->evOffset[evidx];
  97. /* Calculate lower azimuth index. */
  98. azidx= CalcAzIndex(Hrtf->azCount[evidx], azimuth, &amu[0]);
  99. /* Calculate the lower HRIR indices. */
  100. idx[0] = evoffset + azidx;
  101. idx[1] = evoffset + ((azidx+1) % Hrtf->azCount[evidx]);
  102. if(evidx < Hrtf->evCount-1)
  103. {
  104. /* Increment elevation to the next (upper) index. */
  105. evidx++;
  106. evoffset = Hrtf->evOffset[evidx];
  107. /* Calculate upper azimuth index. */
  108. azidx = CalcAzIndex(Hrtf->azCount[evidx], azimuth, &amu[1]);
  109. /* Calculate the upper HRIR indices. */
  110. idx[2] = evoffset + azidx;
  111. idx[3] = evoffset + ((azidx+1) % Hrtf->azCount[evidx]);
  112. }
  113. else
  114. {
  115. /* If the lower elevation is the top index, the upper elevation is the
  116. * same as the lower.
  117. */
  118. amu[1] = amu[0];
  119. idx[2] = idx[0];
  120. idx[3] = idx[1];
  121. }
  122. /* Calculate bilinear blending weights, attenuated according to the
  123. * directional panning factor.
  124. */
  125. blend[0] = (1.0f-emu) * (1.0f-amu[0]) * dirfact;
  126. blend[1] = (1.0f-emu) * ( amu[0]) * dirfact;
  127. blend[2] = ( emu) * (1.0f-amu[1]) * dirfact;
  128. blend[3] = ( emu) * ( amu[1]) * dirfact;
  129. /* Calculate the blended HRIR delays. */
  130. delays[0] = fastf2i(
  131. Hrtf->delays[idx[0]][0]*blend[0] + Hrtf->delays[idx[1]][0]*blend[1] +
  132. Hrtf->delays[idx[2]][0]*blend[2] + Hrtf->delays[idx[3]][0]*blend[3]
  133. );
  134. delays[1] = fastf2i(
  135. Hrtf->delays[idx[0]][1]*blend[0] + Hrtf->delays[idx[1]][1]*blend[1] +
  136. Hrtf->delays[idx[2]][1]*blend[2] + Hrtf->delays[idx[3]][1]*blend[3]
  137. );
  138. /* Calculate the sample offsets for the HRIR indices. */
  139. idx[0] *= Hrtf->irSize;
  140. idx[1] *= Hrtf->irSize;
  141. idx[2] *= Hrtf->irSize;
  142. idx[3] *= Hrtf->irSize;
  143. ASSUME(Hrtf->irSize >= MIN_IR_SIZE && (Hrtf->irSize%MOD_IR_SIZE) == 0);
  144. coeffs = ASSUME_ALIGNED(coeffs, 16);
  145. /* Calculate the blended HRIR coefficients. */
  146. coeffs[0][0] = PassthruCoeff * (1.0f-dirfact);
  147. coeffs[0][1] = PassthruCoeff * (1.0f-dirfact);
  148. for(i = 1;i < Hrtf->irSize;i++)
  149. {
  150. coeffs[i][0] = 0.0f;
  151. coeffs[i][1] = 0.0f;
  152. }
  153. for(c = 0;c < 4;c++)
  154. {
  155. const ALfloat (*restrict srccoeffs)[2] = ASSUME_ALIGNED(Hrtf->coeffs+idx[c], 16);
  156. for(i = 0;i < Hrtf->irSize;i++)
  157. {
  158. coeffs[i][0] += srccoeffs[i][0] * blend[c];
  159. coeffs[i][1] += srccoeffs[i][1] * blend[c];
  160. }
  161. }
  162. }
  163. void BuildBFormatHrtf(const struct Hrtf *Hrtf, DirectHrtfState *state, ALsizei NumChannels, const struct AngularPoint *AmbiPoints, const ALfloat (*restrict AmbiMatrix)[MAX_AMBI_COEFFS], ALsizei AmbiCount, const ALfloat *restrict AmbiOrderHFGain)
  164. {
  165. /* Set this to 2 for dual-band HRTF processing. May require a higher quality
  166. * band-splitter, or better calculation of the new IR length to deal with the
  167. * tail generated by the filter.
  168. */
  169. #define NUM_BANDS 2
  170. BandSplitter splitter;
  171. ALdouble (*tmpres)[HRIR_LENGTH][2];
  172. ALsizei *restrict idx;
  173. ALsizei min_delay = HRTF_HISTORY_LENGTH;
  174. ALsizei max_delay = 0;
  175. ALfloat temps[3][HRIR_LENGTH];
  176. ALsizei max_length;
  177. ALsizei i, c, b;
  178. idx = al_calloc(DEF_ALIGN, AmbiCount*sizeof(*idx));
  179. for(c = 0;c < AmbiCount;c++)
  180. {
  181. ALuint evidx, azidx;
  182. ALuint evoffset;
  183. ALuint azcount;
  184. /* Calculate elevation index. */
  185. evidx = (ALsizei)((F_PI_2+AmbiPoints[c].Elev) * (Hrtf->evCount-1) / F_PI + 0.5f);
  186. evidx = clampi(evidx, 0, Hrtf->evCount-1);
  187. azcount = Hrtf->azCount[evidx];
  188. evoffset = Hrtf->evOffset[evidx];
  189. /* Calculate azimuth index for this elevation. */
  190. azidx = (ALsizei)((F_TAU+AmbiPoints[c].Azim) * azcount / F_TAU + 0.5f) % azcount;
  191. /* Calculate indices for left and right channels. */
  192. idx[c] = evoffset + azidx;
  193. min_delay = mini(min_delay, mini(Hrtf->delays[idx[c]][0], Hrtf->delays[idx[c]][1]));
  194. max_delay = maxi(max_delay, maxi(Hrtf->delays[idx[c]][0], Hrtf->delays[idx[c]][1]));
  195. }
  196. tmpres = al_calloc(16, NumChannels * sizeof(*tmpres));
  197. memset(temps, 0, sizeof(temps));
  198. bandsplit_init(&splitter, 400.0f / (ALfloat)Hrtf->sampleRate);
  199. for(c = 0;c < AmbiCount;c++)
  200. {
  201. const ALfloat (*fir)[2] = &Hrtf->coeffs[idx[c] * Hrtf->irSize];
  202. ALsizei ldelay = Hrtf->delays[idx[c]][0] - min_delay;
  203. ALsizei rdelay = Hrtf->delays[idx[c]][1] - min_delay;
  204. if(NUM_BANDS == 1)
  205. {
  206. for(i = 0;i < NumChannels;++i)
  207. {
  208. ALdouble mult = (ALdouble)AmbiOrderHFGain[(ALsizei)sqrt(i)] * AmbiMatrix[c][i];
  209. ALsizei lidx = ldelay, ridx = rdelay;
  210. ALsizei j = 0;
  211. while(lidx < HRIR_LENGTH && ridx < HRIR_LENGTH && j < Hrtf->irSize)
  212. {
  213. tmpres[i][lidx++][0] += fir[j][0] * mult;
  214. tmpres[i][ridx++][1] += fir[j][1] * mult;
  215. j++;
  216. }
  217. }
  218. }
  219. else
  220. {
  221. /* Band-split left HRIR into low and high frequency responses. */
  222. bandsplit_clear(&splitter);
  223. for(i = 0;i < Hrtf->irSize;i++)
  224. temps[2][i] = fir[i][0];
  225. bandsplit_process(&splitter, temps[0], temps[1], temps[2], HRIR_LENGTH);
  226. /* Apply left ear response with delay. */
  227. for(i = 0;i < NumChannels;++i)
  228. {
  229. ALfloat hfgain = AmbiOrderHFGain[(ALsizei)sqrt(i)];
  230. for(b = 0;b < NUM_BANDS;b++)
  231. {
  232. ALdouble mult = AmbiMatrix[c][i] * (ALdouble)((b==0) ? hfgain : 1.0);
  233. ALsizei lidx = ldelay;
  234. ALsizei j = 0;
  235. while(lidx < HRIR_LENGTH)
  236. tmpres[i][lidx++][0] += temps[b][j++] * mult;
  237. }
  238. }
  239. /* Band-split right HRIR into low and high frequency responses. */
  240. bandsplit_clear(&splitter);
  241. for(i = 0;i < Hrtf->irSize;i++)
  242. temps[2][i] = fir[i][1];
  243. bandsplit_process(&splitter, temps[0], temps[1], temps[2], HRIR_LENGTH);
  244. /* Apply right ear response with delay. */
  245. for(i = 0;i < NumChannels;++i)
  246. {
  247. ALfloat hfgain = AmbiOrderHFGain[(ALsizei)sqrt(i)];
  248. for(b = 0;b < NUM_BANDS;b++)
  249. {
  250. ALdouble mult = AmbiMatrix[c][i] * (ALdouble)((b==0) ? hfgain : 1.0);
  251. ALsizei ridx = rdelay;
  252. ALsizei j = 0;
  253. while(ridx < HRIR_LENGTH)
  254. tmpres[i][ridx++][1] += temps[b][j++] * mult;
  255. }
  256. }
  257. }
  258. }
  259. for(i = 0;i < NumChannels;++i)
  260. {
  261. int idx;
  262. for(idx = 0;idx < HRIR_LENGTH;idx++)
  263. {
  264. state->Chan[i].Coeffs[idx][0] = (ALfloat)tmpres[i][idx][0];
  265. state->Chan[i].Coeffs[idx][1] = (ALfloat)tmpres[i][idx][1];
  266. }
  267. }
  268. al_free(tmpres);
  269. tmpres = NULL;
  270. al_free(idx);
  271. idx = NULL;
  272. if(NUM_BANDS == 1)
  273. max_length = mini(max_delay-min_delay + Hrtf->irSize, HRIR_LENGTH);
  274. else
  275. {
  276. /* Increase the IR size by 2/3rds to account for the tail generated by
  277. * the band-split filter.
  278. */
  279. const ALsizei irsize = mini(Hrtf->irSize*5/3, HRIR_LENGTH);
  280. max_length = mini(max_delay-min_delay + irsize, HRIR_LENGTH);
  281. }
  282. /* Round up to the next IR size multiple. */
  283. max_length += MOD_IR_SIZE-1;
  284. max_length -= max_length%MOD_IR_SIZE;
  285. TRACE("Skipped delay: %d, max delay: %d, new FIR length: %d\n",
  286. min_delay, max_delay-min_delay, max_length);
  287. state->IrSize = max_length;
  288. #undef NUM_BANDS
  289. }
  290. static struct Hrtf *CreateHrtfStore(ALuint rate, ALsizei irSize,
  291. ALfloat distance, ALsizei evCount, ALsizei irCount, const ALubyte *azCount,
  292. const ALushort *evOffset, const ALfloat (*coeffs)[2], const ALubyte (*delays)[2],
  293. const char *filename)
  294. {
  295. struct Hrtf *Hrtf;
  296. size_t total;
  297. total = sizeof(struct Hrtf);
  298. total += sizeof(Hrtf->azCount[0])*evCount;
  299. total = RoundUp(total, sizeof(ALushort)); /* Align for ushort fields */
  300. total += sizeof(Hrtf->evOffset[0])*evCount;
  301. total = RoundUp(total, 16); /* Align for coefficients using SIMD */
  302. total += sizeof(Hrtf->coeffs[0])*irSize*irCount;
  303. total += sizeof(Hrtf->delays[0])*irCount;
  304. Hrtf = al_calloc(16, total);
  305. if(Hrtf == NULL)
  306. ERR("Out of memory allocating storage for %s.\n", filename);
  307. else
  308. {
  309. uintptr_t offset = sizeof(struct Hrtf);
  310. char *base = (char*)Hrtf;
  311. ALushort *_evOffset;
  312. ALubyte *_azCount;
  313. ALubyte (*_delays)[2];
  314. ALfloat (*_coeffs)[2];
  315. ALsizei i;
  316. InitRef(&Hrtf->ref, 0);
  317. Hrtf->sampleRate = rate;
  318. Hrtf->irSize = irSize;
  319. Hrtf->distance = distance;
  320. Hrtf->evCount = evCount;
  321. /* Set up pointers to storage following the main HRTF struct. */
  322. _azCount = (ALubyte*)(base + offset);
  323. offset += sizeof(_azCount[0])*evCount;
  324. offset = RoundUp(offset, sizeof(ALushort)); /* Align for ushort fields */
  325. _evOffset = (ALushort*)(base + offset);
  326. offset += sizeof(_evOffset[0])*evCount;
  327. offset = RoundUp(offset, 16); /* Align for coefficients using SIMD */
  328. _coeffs = (ALfloat(*)[2])(base + offset);
  329. offset += sizeof(_coeffs[0])*irSize*irCount;
  330. _delays = (ALubyte(*)[2])(base + offset);
  331. offset += sizeof(_delays[0])*irCount;
  332. assert(offset == total);
  333. /* Copy input data to storage. */
  334. for(i = 0;i < evCount;i++) _azCount[i] = azCount[i];
  335. for(i = 0;i < evCount;i++) _evOffset[i] = evOffset[i];
  336. for(i = 0;i < irSize*irCount;i++)
  337. {
  338. _coeffs[i][0] = coeffs[i][0];
  339. _coeffs[i][1] = coeffs[i][1];
  340. }
  341. for(i = 0;i < irCount;i++)
  342. {
  343. _delays[i][0] = delays[i][0];
  344. _delays[i][1] = delays[i][1];
  345. }
  346. /* Finally, assign the storage pointers. */
  347. Hrtf->azCount = _azCount;
  348. Hrtf->evOffset = _evOffset;
  349. Hrtf->coeffs = _coeffs;
  350. Hrtf->delays = _delays;
  351. }
  352. return Hrtf;
  353. }
  354. static ALubyte GetLE_ALubyte(const ALubyte **data, size_t *len)
  355. {
  356. ALubyte ret = (*data)[0];
  357. *data += 1; *len -= 1;
  358. return ret;
  359. }
  360. static ALshort GetLE_ALshort(const ALubyte **data, size_t *len)
  361. {
  362. ALshort ret = (*data)[0] | ((*data)[1]<<8);
  363. *data += 2; *len -= 2;
  364. return ret;
  365. }
  366. static ALushort GetLE_ALushort(const ALubyte **data, size_t *len)
  367. {
  368. ALushort ret = (*data)[0] | ((*data)[1]<<8);
  369. *data += 2; *len -= 2;
  370. return ret;
  371. }
  372. static ALint GetLE_ALint24(const ALubyte **data, size_t *len)
  373. {
  374. ALint ret = (*data)[0] | ((*data)[1]<<8) | ((*data)[2]<<16);
  375. *data += 3; *len -= 3;
  376. return (ret^0x800000) - 0x800000;
  377. }
  378. static ALuint GetLE_ALuint(const ALubyte **data, size_t *len)
  379. {
  380. ALuint ret = (*data)[0] | ((*data)[1]<<8) | ((*data)[2]<<16) | ((*data)[3]<<24);
  381. *data += 4; *len -= 4;
  382. return ret;
  383. }
  384. static const ALubyte *Get_ALubytePtr(const ALubyte **data, size_t *len, size_t size)
  385. {
  386. const ALubyte *ret = *data;
  387. *data += size; *len -= size;
  388. return ret;
  389. }
  390. static struct Hrtf *LoadHrtf00(const ALubyte *data, size_t datalen, const char *filename)
  391. {
  392. struct Hrtf *Hrtf = NULL;
  393. ALboolean failed = AL_FALSE;
  394. ALuint rate = 0;
  395. ALushort irCount = 0;
  396. ALushort irSize = 0;
  397. ALubyte evCount = 0;
  398. ALubyte *azCount = NULL;
  399. ALushort *evOffset = NULL;
  400. ALfloat (*coeffs)[2] = NULL;
  401. ALubyte (*delays)[2] = NULL;
  402. ALsizei i, j;
  403. if(datalen < 9)
  404. {
  405. ERR("Unexpected end of %s data (req %d, rem "SZFMT")\n", filename, 9, datalen);
  406. return NULL;
  407. }
  408. rate = GetLE_ALuint(&data, &datalen);
  409. irCount = GetLE_ALushort(&data, &datalen);
  410. irSize = GetLE_ALushort(&data, &datalen);
  411. evCount = GetLE_ALubyte(&data, &datalen);
  412. if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
  413. {
  414. ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
  415. irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
  416. failed = AL_TRUE;
  417. }
  418. if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
  419. {
  420. ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
  421. evCount, MIN_EV_COUNT, MAX_EV_COUNT);
  422. failed = AL_TRUE;
  423. }
  424. if(failed)
  425. return NULL;
  426. if(datalen < evCount*2u)
  427. {
  428. ERR("Unexpected end of %s data (req %d, rem "SZFMT")\n", filename, evCount*2, datalen);
  429. return NULL;
  430. }
  431. azCount = malloc(sizeof(azCount[0])*evCount);
  432. evOffset = malloc(sizeof(evOffset[0])*evCount);
  433. if(azCount == NULL || evOffset == NULL)
  434. {
  435. ERR("Out of memory.\n");
  436. failed = AL_TRUE;
  437. }
  438. if(!failed)
  439. {
  440. evOffset[0] = GetLE_ALushort(&data, &datalen);
  441. for(i = 1;i < evCount;i++)
  442. {
  443. evOffset[i] = GetLE_ALushort(&data, &datalen);
  444. if(evOffset[i] <= evOffset[i-1])
  445. {
  446. ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
  447. i, evOffset[i], evOffset[i-1]);
  448. failed = AL_TRUE;
  449. }
  450. azCount[i-1] = evOffset[i] - evOffset[i-1];
  451. if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
  452. {
  453. ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
  454. i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
  455. failed = AL_TRUE;
  456. }
  457. }
  458. if(irCount <= evOffset[i-1])
  459. {
  460. ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
  461. i-1, evOffset[i-1], irCount);
  462. failed = AL_TRUE;
  463. }
  464. azCount[i-1] = irCount - evOffset[i-1];
  465. if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
  466. {
  467. ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
  468. i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
  469. failed = AL_TRUE;
  470. }
  471. }
  472. if(!failed)
  473. {
  474. coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
  475. delays = malloc(sizeof(delays[0])*irCount);
  476. if(coeffs == NULL || delays == NULL)
  477. {
  478. ERR("Out of memory.\n");
  479. failed = AL_TRUE;
  480. }
  481. }
  482. if(!failed)
  483. {
  484. size_t reqsize = 2*irSize*irCount + irCount;
  485. if(datalen < reqsize)
  486. {
  487. ERR("Unexpected end of %s data (req "SZFMT", rem "SZFMT")\n",
  488. filename, reqsize, datalen);
  489. failed = AL_TRUE;
  490. }
  491. }
  492. if(!failed)
  493. {
  494. for(i = 0;i < irCount;i++)
  495. {
  496. for(j = 0;j < irSize;j++)
  497. coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
  498. }
  499. for(i = 0;i < irCount;i++)
  500. {
  501. delays[i][0] = GetLE_ALubyte(&data, &datalen);
  502. if(delays[i][0] > MAX_HRIR_DELAY)
  503. {
  504. ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
  505. failed = AL_TRUE;
  506. }
  507. }
  508. }
  509. if(!failed)
  510. {
  511. /* Mirror the left ear responses to the right ear. */
  512. for(i = 0;i < evCount;i++)
  513. {
  514. ALushort evoffset = evOffset[i];
  515. ALubyte azcount = azCount[i];
  516. for(j = 0;j < azcount;j++)
  517. {
  518. ALsizei lidx = evoffset + j;
  519. ALsizei ridx = evoffset + ((azcount-j) % azcount);
  520. ALsizei k;
  521. for(k = 0;k < irSize;k++)
  522. coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
  523. delays[ridx][1] = delays[lidx][0];
  524. }
  525. }
  526. Hrtf = CreateHrtfStore(rate, irSize, 0.0f, evCount, irCount, azCount,
  527. evOffset, coeffs, delays, filename);
  528. }
  529. free(azCount);
  530. free(evOffset);
  531. free(coeffs);
  532. free(delays);
  533. return Hrtf;
  534. }
  535. static struct Hrtf *LoadHrtf01(const ALubyte *data, size_t datalen, const char *filename)
  536. {
  537. struct Hrtf *Hrtf = NULL;
  538. ALboolean failed = AL_FALSE;
  539. ALuint rate = 0;
  540. ALushort irCount = 0;
  541. ALushort irSize = 0;
  542. ALubyte evCount = 0;
  543. const ALubyte *azCount = NULL;
  544. ALushort *evOffset = NULL;
  545. ALfloat (*coeffs)[2] = NULL;
  546. ALubyte (*delays)[2] = NULL;
  547. ALsizei i, j;
  548. if(datalen < 6)
  549. {
  550. ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, 6, datalen);
  551. return NULL;
  552. }
  553. rate = GetLE_ALuint(&data, &datalen);
  554. irSize = GetLE_ALubyte(&data, &datalen);
  555. evCount = GetLE_ALubyte(&data, &datalen);
  556. if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
  557. {
  558. ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
  559. irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
  560. failed = AL_TRUE;
  561. }
  562. if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
  563. {
  564. ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
  565. evCount, MIN_EV_COUNT, MAX_EV_COUNT);
  566. failed = AL_TRUE;
  567. }
  568. if(failed)
  569. return NULL;
  570. if(datalen < evCount)
  571. {
  572. ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, evCount, datalen);
  573. return NULL;
  574. }
  575. azCount = Get_ALubytePtr(&data, &datalen, evCount);
  576. evOffset = malloc(sizeof(evOffset[0])*evCount);
  577. if(azCount == NULL || evOffset == NULL)
  578. {
  579. ERR("Out of memory.\n");
  580. failed = AL_TRUE;
  581. }
  582. if(!failed)
  583. {
  584. for(i = 0;i < evCount;i++)
  585. {
  586. if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
  587. {
  588. ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
  589. i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
  590. failed = AL_TRUE;
  591. }
  592. }
  593. }
  594. if(!failed)
  595. {
  596. evOffset[0] = 0;
  597. irCount = azCount[0];
  598. for(i = 1;i < evCount;i++)
  599. {
  600. evOffset[i] = evOffset[i-1] + azCount[i-1];
  601. irCount += azCount[i];
  602. }
  603. coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
  604. delays = malloc(sizeof(delays[0])*irCount);
  605. if(coeffs == NULL || delays == NULL)
  606. {
  607. ERR("Out of memory.\n");
  608. failed = AL_TRUE;
  609. }
  610. }
  611. if(!failed)
  612. {
  613. size_t reqsize = 2*irSize*irCount + irCount;
  614. if(datalen < reqsize)
  615. {
  616. ERR("Unexpected end of %s data (req "SZFMT", rem "SZFMT"\n",
  617. filename, reqsize, datalen);
  618. failed = AL_TRUE;
  619. }
  620. }
  621. if(!failed)
  622. {
  623. for(i = 0;i < irCount;i++)
  624. {
  625. for(j = 0;j < irSize;j++)
  626. coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
  627. }
  628. for(i = 0;i < irCount;i++)
  629. {
  630. delays[i][0] = GetLE_ALubyte(&data, &datalen);
  631. if(delays[i][0] > MAX_HRIR_DELAY)
  632. {
  633. ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
  634. failed = AL_TRUE;
  635. }
  636. }
  637. }
  638. if(!failed)
  639. {
  640. /* Mirror the left ear responses to the right ear. */
  641. for(i = 0;i < evCount;i++)
  642. {
  643. ALushort evoffset = evOffset[i];
  644. ALubyte azcount = azCount[i];
  645. for(j = 0;j < azcount;j++)
  646. {
  647. ALsizei lidx = evoffset + j;
  648. ALsizei ridx = evoffset + ((azcount-j) % azcount);
  649. ALsizei k;
  650. for(k = 0;k < irSize;k++)
  651. coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
  652. delays[ridx][1] = delays[lidx][0];
  653. }
  654. }
  655. Hrtf = CreateHrtfStore(rate, irSize, 0.0f, evCount, irCount, azCount,
  656. evOffset, coeffs, delays, filename);
  657. }
  658. free(evOffset);
  659. free(coeffs);
  660. free(delays);
  661. return Hrtf;
  662. }
  663. #define SAMPLETYPE_S16 0
  664. #define SAMPLETYPE_S24 1
  665. #define CHANTYPE_LEFTONLY 0
  666. #define CHANTYPE_LEFTRIGHT 1
  667. static struct Hrtf *LoadHrtf02(const ALubyte *data, size_t datalen, const char *filename)
  668. {
  669. struct Hrtf *Hrtf = NULL;
  670. ALboolean failed = AL_FALSE;
  671. ALuint rate = 0;
  672. ALubyte sampleType;
  673. ALubyte channelType;
  674. ALushort irCount = 0;
  675. ALushort irSize = 0;
  676. ALubyte fdCount = 0;
  677. ALushort distance = 0;
  678. ALubyte evCount = 0;
  679. const ALubyte *azCount = NULL;
  680. ALushort *evOffset = NULL;
  681. ALfloat (*coeffs)[2] = NULL;
  682. ALubyte (*delays)[2] = NULL;
  683. ALsizei i, j;
  684. if(datalen < 8)
  685. {
  686. ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, 8, datalen);
  687. return NULL;
  688. }
  689. rate = GetLE_ALuint(&data, &datalen);
  690. sampleType = GetLE_ALubyte(&data, &datalen);
  691. channelType = GetLE_ALubyte(&data, &datalen);
  692. irSize = GetLE_ALubyte(&data, &datalen);
  693. fdCount = GetLE_ALubyte(&data, &datalen);
  694. if(sampleType > SAMPLETYPE_S24)
  695. {
  696. ERR("Unsupported sample type: %d\n", sampleType);
  697. failed = AL_TRUE;
  698. }
  699. if(channelType > CHANTYPE_LEFTRIGHT)
  700. {
  701. ERR("Unsupported channel type: %d\n", channelType);
  702. failed = AL_TRUE;
  703. }
  704. if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
  705. {
  706. ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
  707. irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
  708. failed = AL_TRUE;
  709. }
  710. if(fdCount != 1)
  711. {
  712. ERR("Multiple field-depths not supported: fdCount=%d (%d to %d)\n",
  713. evCount, MIN_FD_COUNT, MAX_FD_COUNT);
  714. failed = AL_TRUE;
  715. }
  716. if(failed)
  717. return NULL;
  718. for(i = 0;i < fdCount;i++)
  719. {
  720. if(datalen < 3)
  721. {
  722. ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, 3, datalen);
  723. return NULL;
  724. }
  725. distance = GetLE_ALushort(&data, &datalen);
  726. if(distance < MIN_FD_DISTANCE || distance > MAX_FD_DISTANCE)
  727. {
  728. ERR("Unsupported field distance: distance=%d (%dmm to %dmm)\n",
  729. distance, MIN_FD_DISTANCE, MAX_FD_DISTANCE);
  730. failed = AL_TRUE;
  731. }
  732. evCount = GetLE_ALubyte(&data, &datalen);
  733. if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
  734. {
  735. ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
  736. evCount, MIN_EV_COUNT, MAX_EV_COUNT);
  737. failed = AL_TRUE;
  738. }
  739. if(failed)
  740. return NULL;
  741. if(datalen < evCount)
  742. {
  743. ERR("Unexpected end of %s data (req %d, rem "SZFMT"\n", filename, evCount, datalen);
  744. return NULL;
  745. }
  746. azCount = Get_ALubytePtr(&data, &datalen, evCount);
  747. for(j = 0;j < evCount;j++)
  748. {
  749. if(azCount[j] < MIN_AZ_COUNT || azCount[j] > MAX_AZ_COUNT)
  750. {
  751. ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
  752. j, azCount[j], MIN_AZ_COUNT, MAX_AZ_COUNT);
  753. failed = AL_TRUE;
  754. }
  755. }
  756. }
  757. if(failed)
  758. return NULL;
  759. evOffset = malloc(sizeof(evOffset[0])*evCount);
  760. if(azCount == NULL || evOffset == NULL)
  761. {
  762. ERR("Out of memory.\n");
  763. failed = AL_TRUE;
  764. }
  765. if(!failed)
  766. {
  767. evOffset[0] = 0;
  768. irCount = azCount[0];
  769. for(i = 1;i < evCount;i++)
  770. {
  771. evOffset[i] = evOffset[i-1] + azCount[i-1];
  772. irCount += azCount[i];
  773. }
  774. coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
  775. delays = malloc(sizeof(delays[0])*irCount);
  776. if(coeffs == NULL || delays == NULL)
  777. {
  778. ERR("Out of memory.\n");
  779. failed = AL_TRUE;
  780. }
  781. }
  782. if(!failed)
  783. {
  784. size_t reqsize = 2*irSize*irCount + irCount;
  785. if(datalen < reqsize)
  786. {
  787. ERR("Unexpected end of %s data (req "SZFMT", rem "SZFMT"\n",
  788. filename, reqsize, datalen);
  789. failed = AL_TRUE;
  790. }
  791. }
  792. if(!failed)
  793. {
  794. if(channelType == CHANTYPE_LEFTONLY)
  795. {
  796. if(sampleType == SAMPLETYPE_S16)
  797. for(i = 0;i < irCount;i++)
  798. {
  799. for(j = 0;j < irSize;j++)
  800. coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
  801. }
  802. else if(sampleType == SAMPLETYPE_S24)
  803. for(i = 0;i < irCount;i++)
  804. {
  805. for(j = 0;j < irSize;j++)
  806. coeffs[i*irSize + j][0] = GetLE_ALint24(&data, &datalen) / 8388608.0f;
  807. }
  808. for(i = 0;i < irCount;i++)
  809. {
  810. delays[i][0] = GetLE_ALubyte(&data, &datalen);
  811. if(delays[i][0] > MAX_HRIR_DELAY)
  812. {
  813. ERR("Invalid delays[%d][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
  814. failed = AL_TRUE;
  815. }
  816. }
  817. }
  818. else if(channelType == CHANTYPE_LEFTRIGHT)
  819. {
  820. if(sampleType == SAMPLETYPE_S16)
  821. for(i = 0;i < irCount;i++)
  822. {
  823. for(j = 0;j < irSize;j++)
  824. {
  825. coeffs[i*irSize + j][0] = GetLE_ALshort(&data, &datalen) / 32768.0f;
  826. coeffs[i*irSize + j][1] = GetLE_ALshort(&data, &datalen) / 32768.0f;
  827. }
  828. }
  829. else if(sampleType == SAMPLETYPE_S24)
  830. for(i = 0;i < irCount;i++)
  831. {
  832. for(j = 0;j < irSize;j++)
  833. {
  834. coeffs[i*irSize + j][0] = GetLE_ALint24(&data, &datalen) / 8388608.0f;
  835. coeffs[i*irSize + j][1] = GetLE_ALint24(&data, &datalen) / 8388608.0f;
  836. }
  837. }
  838. for(i = 0;i < irCount;i++)
  839. {
  840. delays[i][0] = GetLE_ALubyte(&data, &datalen);
  841. if(delays[i][0] > MAX_HRIR_DELAY)
  842. {
  843. ERR("Invalid delays[%d][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
  844. failed = AL_TRUE;
  845. }
  846. delays[i][1] = GetLE_ALubyte(&data, &datalen);
  847. if(delays[i][1] > MAX_HRIR_DELAY)
  848. {
  849. ERR("Invalid delays[%d][1]: %d (%d)\n", i, delays[i][1], MAX_HRIR_DELAY);
  850. failed = AL_TRUE;
  851. }
  852. }
  853. }
  854. }
  855. if(!failed)
  856. {
  857. if(channelType == CHANTYPE_LEFTONLY)
  858. {
  859. /* Mirror the left ear responses to the right ear. */
  860. for(i = 0;i < evCount;i++)
  861. {
  862. ALushort evoffset = evOffset[i];
  863. ALubyte azcount = azCount[i];
  864. for(j = 0;j < azcount;j++)
  865. {
  866. ALsizei lidx = evoffset + j;
  867. ALsizei ridx = evoffset + ((azcount-j) % azcount);
  868. ALsizei k;
  869. for(k = 0;k < irSize;k++)
  870. coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
  871. delays[ridx][1] = delays[lidx][0];
  872. }
  873. }
  874. }
  875. Hrtf = CreateHrtfStore(rate, irSize,
  876. (ALfloat)distance / 1000.0f, evCount, irCount, azCount, evOffset,
  877. coeffs, delays, filename
  878. );
  879. }
  880. free(evOffset);
  881. free(coeffs);
  882. free(delays);
  883. return Hrtf;
  884. }
  885. static void AddFileEntry(vector_EnumeratedHrtf *list, const_al_string filename)
  886. {
  887. EnumeratedHrtf entry = { AL_STRING_INIT_STATIC(), NULL };
  888. struct HrtfEntry *loaded_entry;
  889. const EnumeratedHrtf *iter;
  890. const char *name;
  891. const char *ext;
  892. int i;
  893. /* Check if this file has already been loaded globally. */
  894. loaded_entry = LoadedHrtfs;
  895. while(loaded_entry)
  896. {
  897. if(alstr_cmp_cstr(filename, loaded_entry->filename) == 0)
  898. {
  899. /* Check if this entry has already been added to the list. */
  900. #define MATCH_ENTRY(i) (loaded_entry == (i)->hrtf)
  901. VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_ENTRY);
  902. #undef MATCH_ENTRY
  903. if(iter != VECTOR_END(*list))
  904. {
  905. TRACE("Skipping duplicate file entry %s\n", alstr_get_cstr(filename));
  906. return;
  907. }
  908. break;
  909. }
  910. loaded_entry = loaded_entry->next;
  911. }
  912. if(!loaded_entry)
  913. {
  914. TRACE("Got new file \"%s\"\n", alstr_get_cstr(filename));
  915. loaded_entry = al_calloc(DEF_ALIGN,
  916. FAM_SIZE(struct HrtfEntry, filename, alstr_length(filename)+1)
  917. );
  918. loaded_entry->next = LoadedHrtfs;
  919. loaded_entry->handle = NULL;
  920. strcpy(loaded_entry->filename, alstr_get_cstr(filename));
  921. LoadedHrtfs = loaded_entry;
  922. }
  923. /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
  924. * format update). */
  925. name = strrchr(alstr_get_cstr(filename), '/');
  926. if(!name) name = strrchr(alstr_get_cstr(filename), '\\');
  927. if(!name) name = alstr_get_cstr(filename);
  928. else ++name;
  929. ext = strrchr(name, '.');
  930. i = 0;
  931. do {
  932. if(!ext)
  933. alstr_copy_cstr(&entry.name, name);
  934. else
  935. alstr_copy_range(&entry.name, name, ext);
  936. if(i != 0)
  937. {
  938. char str[64];
  939. snprintf(str, sizeof(str), " #%d", i+1);
  940. alstr_append_cstr(&entry.name, str);
  941. }
  942. ++i;
  943. #define MATCH_NAME(i) (alstr_cmp(entry.name, (i)->name) == 0)
  944. VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_NAME);
  945. #undef MATCH_NAME
  946. } while(iter != VECTOR_END(*list));
  947. entry.hrtf = loaded_entry;
  948. TRACE("Adding entry \"%s\" from file \"%s\"\n", alstr_get_cstr(entry.name),
  949. alstr_get_cstr(filename));
  950. VECTOR_PUSH_BACK(*list, entry);
  951. }
  952. /* Unfortunate that we have to duplicate AddFileEntry to take a memory buffer
  953. * for input instead of opening the given filename.
  954. */
  955. static void AddBuiltInEntry(vector_EnumeratedHrtf *list, const_al_string filename, ALuint residx)
  956. {
  957. EnumeratedHrtf entry = { AL_STRING_INIT_STATIC(), NULL };
  958. struct HrtfEntry *loaded_entry;
  959. struct Hrtf *hrtf = NULL;
  960. const EnumeratedHrtf *iter;
  961. const char *name;
  962. const char *ext;
  963. int i;
  964. loaded_entry = LoadedHrtfs;
  965. while(loaded_entry)
  966. {
  967. if(alstr_cmp_cstr(filename, loaded_entry->filename) == 0)
  968. {
  969. #define MATCH_ENTRY(i) (loaded_entry == (i)->hrtf)
  970. VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_ENTRY);
  971. #undef MATCH_ENTRY
  972. if(iter != VECTOR_END(*list))
  973. {
  974. TRACE("Skipping duplicate file entry %s\n", alstr_get_cstr(filename));
  975. return;
  976. }
  977. break;
  978. }
  979. loaded_entry = loaded_entry->next;
  980. }
  981. if(!loaded_entry)
  982. {
  983. size_t namelen = alstr_length(filename)+32;
  984. TRACE("Got new file \"%s\"\n", alstr_get_cstr(filename));
  985. loaded_entry = al_calloc(DEF_ALIGN,
  986. FAM_SIZE(struct HrtfEntry, filename, namelen)
  987. );
  988. loaded_entry->next = LoadedHrtfs;
  989. loaded_entry->handle = hrtf;
  990. snprintf(loaded_entry->filename, namelen, "!%u_%s",
  991. residx, alstr_get_cstr(filename));
  992. LoadedHrtfs = loaded_entry;
  993. }
  994. /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
  995. * format update). */
  996. name = strrchr(alstr_get_cstr(filename), '/');
  997. if(!name) name = strrchr(alstr_get_cstr(filename), '\\');
  998. if(!name) name = alstr_get_cstr(filename);
  999. else ++name;
  1000. ext = strrchr(name, '.');
  1001. i = 0;
  1002. do {
  1003. if(!ext)
  1004. alstr_copy_cstr(&entry.name, name);
  1005. else
  1006. alstr_copy_range(&entry.name, name, ext);
  1007. if(i != 0)
  1008. {
  1009. char str[64];
  1010. snprintf(str, sizeof(str), " #%d", i+1);
  1011. alstr_append_cstr(&entry.name, str);
  1012. }
  1013. ++i;
  1014. #define MATCH_NAME(i) (alstr_cmp(entry.name, (i)->name) == 0)
  1015. VECTOR_FIND_IF(iter, const EnumeratedHrtf, *list, MATCH_NAME);
  1016. #undef MATCH_NAME
  1017. } while(iter != VECTOR_END(*list));
  1018. entry.hrtf = loaded_entry;
  1019. TRACE("Adding built-in entry \"%s\"\n", alstr_get_cstr(entry.name));
  1020. VECTOR_PUSH_BACK(*list, entry);
  1021. }
  1022. #define IDR_DEFAULT_44100_MHR 1
  1023. #define IDR_DEFAULT_48000_MHR 2
  1024. #ifndef ALSOFT_EMBED_HRTF_DATA
  1025. static const ALubyte *GetResource(int UNUSED(name), size_t *size)
  1026. {
  1027. *size = 0;
  1028. return NULL;
  1029. }
  1030. #else
  1031. #include "default-44100.mhr.h"
  1032. #include "default-48000.mhr.h"
  1033. static const ALubyte *GetResource(int name, size_t *size)
  1034. {
  1035. if(name == IDR_DEFAULT_44100_MHR)
  1036. {
  1037. *size = sizeof(hrtf_default_44100);
  1038. return hrtf_default_44100;
  1039. }
  1040. if(name == IDR_DEFAULT_48000_MHR)
  1041. {
  1042. *size = sizeof(hrtf_default_48000);
  1043. return hrtf_default_48000;
  1044. }
  1045. *size = 0;
  1046. return NULL;
  1047. }
  1048. #endif
  1049. vector_EnumeratedHrtf EnumerateHrtf(const_al_string devname)
  1050. {
  1051. vector_EnumeratedHrtf list = VECTOR_INIT_STATIC();
  1052. const char *defaulthrtf = "";
  1053. const char *pathlist = "";
  1054. bool usedefaults = true;
  1055. if(ConfigValueStr(alstr_get_cstr(devname), NULL, "hrtf-paths", &pathlist))
  1056. {
  1057. al_string pname = AL_STRING_INIT_STATIC();
  1058. while(pathlist && *pathlist)
  1059. {
  1060. const char *next, *end;
  1061. while(isspace(*pathlist) || *pathlist == ',')
  1062. pathlist++;
  1063. if(*pathlist == '\0')
  1064. continue;
  1065. next = strchr(pathlist, ',');
  1066. if(next)
  1067. end = next++;
  1068. else
  1069. {
  1070. end = pathlist + strlen(pathlist);
  1071. usedefaults = false;
  1072. }
  1073. while(end != pathlist && isspace(*(end-1)))
  1074. --end;
  1075. if(end != pathlist)
  1076. {
  1077. vector_al_string flist;
  1078. size_t i;
  1079. alstr_copy_range(&pname, pathlist, end);
  1080. flist = SearchDataFiles(".mhr", alstr_get_cstr(pname));
  1081. for(i = 0;i < VECTOR_SIZE(flist);i++)
  1082. AddFileEntry(&list, VECTOR_ELEM(flist, i));
  1083. VECTOR_FOR_EACH(al_string, flist, alstr_reset);
  1084. VECTOR_DEINIT(flist);
  1085. }
  1086. pathlist = next;
  1087. }
  1088. alstr_reset(&pname);
  1089. }
  1090. else if(ConfigValueExists(alstr_get_cstr(devname), NULL, "hrtf_tables"))
  1091. ERR("The hrtf_tables option is deprecated, please use hrtf-paths instead.\n");
  1092. if(usedefaults)
  1093. {
  1094. al_string ename = AL_STRING_INIT_STATIC();
  1095. vector_al_string flist;
  1096. const ALubyte *rdata;
  1097. size_t rsize, i;
  1098. flist = SearchDataFiles(".mhr", "openal/hrtf");
  1099. for(i = 0;i < VECTOR_SIZE(flist);i++)
  1100. AddFileEntry(&list, VECTOR_ELEM(flist, i));
  1101. VECTOR_FOR_EACH(al_string, flist, alstr_reset);
  1102. VECTOR_DEINIT(flist);
  1103. rdata = GetResource(IDR_DEFAULT_44100_MHR, &rsize);
  1104. if(rdata != NULL && rsize > 0)
  1105. {
  1106. alstr_copy_cstr(&ename, "Built-In 44100hz");
  1107. AddBuiltInEntry(&list, ename, IDR_DEFAULT_44100_MHR);
  1108. }
  1109. rdata = GetResource(IDR_DEFAULT_48000_MHR, &rsize);
  1110. if(rdata != NULL && rsize > 0)
  1111. {
  1112. alstr_copy_cstr(&ename, "Built-In 48000hz");
  1113. AddBuiltInEntry(&list, ename, IDR_DEFAULT_48000_MHR);
  1114. }
  1115. alstr_reset(&ename);
  1116. }
  1117. if(VECTOR_SIZE(list) > 1 && ConfigValueStr(alstr_get_cstr(devname), NULL, "default-hrtf", &defaulthrtf))
  1118. {
  1119. const EnumeratedHrtf *iter;
  1120. /* Find the preferred HRTF and move it to the front of the list. */
  1121. #define FIND_ENTRY(i) (alstr_cmp_cstr((i)->name, defaulthrtf) == 0)
  1122. VECTOR_FIND_IF(iter, const EnumeratedHrtf, list, FIND_ENTRY);
  1123. #undef FIND_ENTRY
  1124. if(iter == VECTOR_END(list))
  1125. WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf);
  1126. else if(iter != VECTOR_BEGIN(list))
  1127. {
  1128. EnumeratedHrtf entry = *iter;
  1129. memmove(&VECTOR_ELEM(list,1), &VECTOR_ELEM(list,0),
  1130. (iter-VECTOR_BEGIN(list))*sizeof(EnumeratedHrtf));
  1131. VECTOR_ELEM(list,0) = entry;
  1132. }
  1133. }
  1134. return list;
  1135. }
  1136. void FreeHrtfList(vector_EnumeratedHrtf *list)
  1137. {
  1138. #define CLEAR_ENTRY(i) alstr_reset(&(i)->name)
  1139. VECTOR_FOR_EACH(EnumeratedHrtf, *list, CLEAR_ENTRY);
  1140. VECTOR_DEINIT(*list);
  1141. #undef CLEAR_ENTRY
  1142. }
  1143. struct Hrtf *GetLoadedHrtf(struct HrtfEntry *entry)
  1144. {
  1145. struct Hrtf *hrtf = NULL;
  1146. struct FileMapping fmap;
  1147. const ALubyte *rdata;
  1148. const char *name;
  1149. ALuint residx;
  1150. size_t rsize;
  1151. char ch;
  1152. while(ATOMIC_FLAG_TEST_AND_SET(&LoadedHrtfLock, almemory_order_seq_cst))
  1153. althrd_yield();
  1154. if(entry->handle)
  1155. {
  1156. hrtf = entry->handle;
  1157. Hrtf_IncRef(hrtf);
  1158. goto done;
  1159. }
  1160. fmap.ptr = NULL;
  1161. fmap.len = 0;
  1162. if(sscanf(entry->filename, "!%u%c", &residx, &ch) == 2 && ch == '_')
  1163. {
  1164. name = strchr(entry->filename, ch)+1;
  1165. TRACE("Loading %s...\n", name);
  1166. rdata = GetResource(residx, &rsize);
  1167. if(rdata == NULL || rsize == 0)
  1168. {
  1169. ERR("Could not get resource %u, %s\n", residx, name);
  1170. goto done;
  1171. }
  1172. }
  1173. else
  1174. {
  1175. name = entry->filename;
  1176. TRACE("Loading %s...\n", entry->filename);
  1177. fmap = MapFileToMem(entry->filename);
  1178. if(fmap.ptr == NULL)
  1179. {
  1180. ERR("Could not open %s\n", entry->filename);
  1181. goto done;
  1182. }
  1183. rdata = fmap.ptr;
  1184. rsize = fmap.len;
  1185. }
  1186. if(rsize < sizeof(magicMarker02))
  1187. ERR("%s data is too short ("SZFMT" bytes)\n", name, rsize);
  1188. else if(memcmp(rdata, magicMarker02, sizeof(magicMarker02)) == 0)
  1189. {
  1190. TRACE("Detected data set format v2\n");
  1191. hrtf = LoadHrtf02(rdata+sizeof(magicMarker02),
  1192. rsize-sizeof(magicMarker02), name
  1193. );
  1194. }
  1195. else if(memcmp(rdata, magicMarker01, sizeof(magicMarker01)) == 0)
  1196. {
  1197. TRACE("Detected data set format v1\n");
  1198. hrtf = LoadHrtf01(rdata+sizeof(magicMarker01),
  1199. rsize-sizeof(magicMarker01), name
  1200. );
  1201. }
  1202. else if(memcmp(rdata, magicMarker00, sizeof(magicMarker00)) == 0)
  1203. {
  1204. TRACE("Detected data set format v0\n");
  1205. hrtf = LoadHrtf00(rdata+sizeof(magicMarker00),
  1206. rsize-sizeof(magicMarker00), name
  1207. );
  1208. }
  1209. else
  1210. ERR("Invalid header in %s: \"%.8s\"\n", name, (const char*)rdata);
  1211. if(fmap.ptr)
  1212. UnmapFileMem(&fmap);
  1213. if(!hrtf)
  1214. {
  1215. ERR("Failed to load %s\n", name);
  1216. goto done;
  1217. }
  1218. entry->handle = hrtf;
  1219. Hrtf_IncRef(hrtf);
  1220. TRACE("Loaded HRTF support for format: %s %uhz\n",
  1221. DevFmtChannelsString(DevFmtStereo), hrtf->sampleRate);
  1222. done:
  1223. ATOMIC_FLAG_CLEAR(&LoadedHrtfLock, almemory_order_seq_cst);
  1224. return hrtf;
  1225. }
  1226. void Hrtf_IncRef(struct Hrtf *hrtf)
  1227. {
  1228. uint ref = IncrementRef(&hrtf->ref);
  1229. TRACEREF("%p increasing refcount to %u\n", hrtf, ref);
  1230. }
  1231. void Hrtf_DecRef(struct Hrtf *hrtf)
  1232. {
  1233. struct HrtfEntry *Hrtf;
  1234. uint ref = DecrementRef(&hrtf->ref);
  1235. TRACEREF("%p decreasing refcount to %u\n", hrtf, ref);
  1236. if(ref == 0)
  1237. {
  1238. while(ATOMIC_FLAG_TEST_AND_SET(&LoadedHrtfLock, almemory_order_seq_cst))
  1239. althrd_yield();
  1240. Hrtf = LoadedHrtfs;
  1241. while(Hrtf != NULL)
  1242. {
  1243. /* Need to double-check that it's still unused, as another device
  1244. * could've reacquired this HRTF after its reference went to 0 and
  1245. * before the lock was taken.
  1246. */
  1247. if(hrtf == Hrtf->handle && ReadRef(&hrtf->ref) == 0)
  1248. {
  1249. al_free(Hrtf->handle);
  1250. Hrtf->handle = NULL;
  1251. TRACE("Unloaded unused HRTF %s\n", Hrtf->filename);
  1252. }
  1253. Hrtf = Hrtf->next;
  1254. }
  1255. ATOMIC_FLAG_CLEAR(&LoadedHrtfLock, almemory_order_seq_cst);
  1256. }
  1257. }
  1258. void FreeHrtfs(void)
  1259. {
  1260. struct HrtfEntry *Hrtf = LoadedHrtfs;
  1261. LoadedHrtfs = NULL;
  1262. while(Hrtf != NULL)
  1263. {
  1264. struct HrtfEntry *next = Hrtf->next;
  1265. al_free(Hrtf->handle);
  1266. al_free(Hrtf);
  1267. Hrtf = next;
  1268. }
  1269. }