hrtf.c 25 KB

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