hrtf.c 43 KB

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