hrtf.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 "compat.h"
  30. /* Current data set limits defined by the makehrtf utility. */
  31. #define MIN_IR_SIZE (8)
  32. #define MAX_IR_SIZE (128)
  33. #define MOD_IR_SIZE (8)
  34. #define MIN_EV_COUNT (5)
  35. #define MAX_EV_COUNT (128)
  36. #define MIN_AZ_COUNT (1)
  37. #define MAX_AZ_COUNT (128)
  38. struct Hrtf {
  39. ALuint sampleRate;
  40. ALuint irSize;
  41. ALubyte evCount;
  42. const ALubyte *azCount;
  43. const ALushort *evOffset;
  44. const ALshort *coeffs;
  45. const ALubyte *delays;
  46. al_string filename;
  47. struct Hrtf *next;
  48. };
  49. static const ALchar magicMarker00[8] = "MinPHR00";
  50. static const ALchar magicMarker01[8] = "MinPHR01";
  51. /* First value for pass-through coefficients (remaining are 0), used for omni-
  52. * directional sounds. */
  53. static const ALfloat PassthruCoeff = 32767.0f * 0.707106781187f/*sqrt(0.5)*/;
  54. static struct Hrtf *LoadedHrtfs = NULL;
  55. /* Calculate the elevation indices given the polar elevation in radians.
  56. * This will return two indices between 0 and (evcount - 1) and an
  57. * interpolation factor between 0.0 and 1.0.
  58. */
  59. static void CalcEvIndices(ALuint evcount, ALfloat ev, ALuint *evidx, ALfloat *evmu)
  60. {
  61. ev = (F_PI_2 + ev) * (evcount-1) / F_PI;
  62. evidx[0] = fastf2u(ev);
  63. evidx[1] = minu(evidx[0] + 1, evcount-1);
  64. *evmu = ev - evidx[0];
  65. }
  66. /* Calculate the azimuth indices given the polar azimuth in radians. This
  67. * will return two indices between 0 and (azcount - 1) and an interpolation
  68. * factor between 0.0 and 1.0.
  69. */
  70. static void CalcAzIndices(ALuint azcount, ALfloat az, ALuint *azidx, ALfloat *azmu)
  71. {
  72. az = (F_TAU + az) * azcount / F_TAU;
  73. azidx[0] = fastf2u(az) % azcount;
  74. azidx[1] = (azidx[0] + 1) % azcount;
  75. *azmu = az - floorf(az);
  76. }
  77. /* Calculates static HRIR coefficients and delays for the given polar
  78. * elevation and azimuth in radians. Linear interpolation is used to
  79. * increase the apparent resolution of the HRIR data set. The coefficients
  80. * are also normalized and attenuated by the specified gain.
  81. */
  82. void GetLerpedHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat dirfact, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays)
  83. {
  84. ALuint evidx[2], lidx[4], ridx[4];
  85. ALfloat mu[3], blend[4];
  86. ALuint i;
  87. /* Claculate elevation indices and interpolation factor. */
  88. CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
  89. for(i = 0;i < 2;i++)
  90. {
  91. ALuint azcount = Hrtf->azCount[evidx[i]];
  92. ALuint evoffset = Hrtf->evOffset[evidx[i]];
  93. ALuint azidx[2];
  94. /* Calculate azimuth indices and interpolation factor for this elevation. */
  95. CalcAzIndices(azcount, azimuth, azidx, &mu[i]);
  96. /* Calculate a set of linear HRIR indices for left and right channels. */
  97. lidx[i*2 + 0] = evoffset + azidx[0];
  98. lidx[i*2 + 1] = evoffset + azidx[1];
  99. ridx[i*2 + 0] = evoffset + ((azcount-azidx[0]) % azcount);
  100. ridx[i*2 + 1] = evoffset + ((azcount-azidx[1]) % azcount);
  101. }
  102. /* Calculate 4 blending weights for 2D bilinear interpolation. */
  103. blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
  104. blend[1] = ( mu[0]) * (1.0f-mu[2]);
  105. blend[2] = (1.0f-mu[1]) * ( mu[2]);
  106. blend[3] = ( mu[1]) * ( mu[2]);
  107. /* Calculate the HRIR delays using linear interpolation. */
  108. delays[0] = fastf2u((Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
  109. Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3]) *
  110. dirfact + 0.5f) << HRTFDELAY_BITS;
  111. delays[1] = fastf2u((Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
  112. Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3]) *
  113. dirfact + 0.5f) << HRTFDELAY_BITS;
  114. /* Calculate the sample offsets for the HRIR indices. */
  115. lidx[0] *= Hrtf->irSize;
  116. lidx[1] *= Hrtf->irSize;
  117. lidx[2] *= Hrtf->irSize;
  118. lidx[3] *= Hrtf->irSize;
  119. ridx[0] *= Hrtf->irSize;
  120. ridx[1] *= Hrtf->irSize;
  121. ridx[2] *= Hrtf->irSize;
  122. ridx[3] *= Hrtf->irSize;
  123. /* Calculate the normalized and attenuated HRIR coefficients using linear
  124. * interpolation when there is enough gain to warrant it. Zero the
  125. * coefficients if gain is too low.
  126. */
  127. if(gain > 0.0001f)
  128. {
  129. ALfloat c;
  130. i = 0;
  131. c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
  132. Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
  133. coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
  134. c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
  135. Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
  136. coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
  137. for(i = 1;i < Hrtf->irSize;i++)
  138. {
  139. c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
  140. Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
  141. coeffs[i][0] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
  142. c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
  143. Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
  144. coeffs[i][1] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
  145. }
  146. }
  147. else
  148. {
  149. for(i = 0;i < Hrtf->irSize;i++)
  150. {
  151. coeffs[i][0] = 0.0f;
  152. coeffs[i][1] = 0.0f;
  153. }
  154. }
  155. }
  156. /* Calculates the moving HRIR target coefficients, target delays, and
  157. * stepping values for the given polar elevation and azimuth in radians.
  158. * Linear interpolation is used to increase the apparent resolution of the
  159. * HRIR data set. The coefficients are also normalized and attenuated by the
  160. * specified gain. Stepping resolution and count is determined using the
  161. * given delta factor between 0.0 and 1.0.
  162. */
  163. ALuint GetMovingHrtfCoeffs(const struct Hrtf *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat dirfact, ALfloat gain, ALfloat delta, ALint counter, ALfloat (*coeffs)[2], ALuint *delays, ALfloat (*coeffStep)[2], ALint *delayStep)
  164. {
  165. ALuint evidx[2], lidx[4], ridx[4];
  166. ALfloat mu[3], blend[4];
  167. ALfloat left, right;
  168. ALfloat steps;
  169. ALuint i;
  170. /* Claculate elevation indices and interpolation factor. */
  171. CalcEvIndices(Hrtf->evCount, elevation, evidx, &mu[2]);
  172. for(i = 0;i < 2;i++)
  173. {
  174. ALuint azcount = Hrtf->azCount[evidx[i]];
  175. ALuint evoffset = Hrtf->evOffset[evidx[i]];
  176. ALuint azidx[2];
  177. /* Calculate azimuth indices and interpolation factor for this elevation. */
  178. CalcAzIndices(azcount, azimuth, azidx, &mu[i]);
  179. /* Calculate a set of linear HRIR indices for left and right channels. */
  180. lidx[i*2 + 0] = evoffset + azidx[0];
  181. lidx[i*2 + 1] = evoffset + azidx[1];
  182. ridx[i*2 + 0] = evoffset + ((azcount-azidx[0]) % azcount);
  183. ridx[i*2 + 1] = evoffset + ((azcount-azidx[1]) % azcount);
  184. }
  185. // Calculate the stepping parameters.
  186. steps = maxf(floorf(delta*Hrtf->sampleRate + 0.5f), 1.0f);
  187. delta = 1.0f / steps;
  188. /* Calculate 4 blending weights for 2D bilinear interpolation. */
  189. blend[0] = (1.0f-mu[0]) * (1.0f-mu[2]);
  190. blend[1] = ( mu[0]) * (1.0f-mu[2]);
  191. blend[2] = (1.0f-mu[1]) * ( mu[2]);
  192. blend[3] = ( mu[1]) * ( mu[2]);
  193. /* Calculate the HRIR delays using linear interpolation. Then calculate
  194. * the delay stepping values using the target and previous running
  195. * delays.
  196. */
  197. left = (ALfloat)(delays[0] - (delayStep[0] * counter));
  198. right = (ALfloat)(delays[1] - (delayStep[1] * counter));
  199. delays[0] = fastf2u((Hrtf->delays[lidx[0]]*blend[0] + Hrtf->delays[lidx[1]]*blend[1] +
  200. Hrtf->delays[lidx[2]]*blend[2] + Hrtf->delays[lidx[3]]*blend[3]) *
  201. dirfact + 0.5f) << HRTFDELAY_BITS;
  202. delays[1] = fastf2u((Hrtf->delays[ridx[0]]*blend[0] + Hrtf->delays[ridx[1]]*blend[1] +
  203. Hrtf->delays[ridx[2]]*blend[2] + Hrtf->delays[ridx[3]]*blend[3]) *
  204. dirfact + 0.5f) << HRTFDELAY_BITS;
  205. delayStep[0] = fastf2i(delta * (delays[0] - left));
  206. delayStep[1] = fastf2i(delta * (delays[1] - right));
  207. /* Calculate the sample offsets for the HRIR indices. */
  208. lidx[0] *= Hrtf->irSize;
  209. lidx[1] *= Hrtf->irSize;
  210. lidx[2] *= Hrtf->irSize;
  211. lidx[3] *= Hrtf->irSize;
  212. ridx[0] *= Hrtf->irSize;
  213. ridx[1] *= Hrtf->irSize;
  214. ridx[2] *= Hrtf->irSize;
  215. ridx[3] *= Hrtf->irSize;
  216. /* Calculate the normalized and attenuated target HRIR coefficients using
  217. * linear interpolation when there is enough gain to warrant it. Zero
  218. * the target coefficients if gain is too low. Then calculate the
  219. * coefficient stepping values using the target and previous running
  220. * coefficients.
  221. */
  222. if(gain > 0.0001f)
  223. {
  224. ALfloat c;
  225. i = 0;
  226. left = coeffs[i][0] - (coeffStep[i][0] * counter);
  227. right = coeffs[i][1] - (coeffStep[i][1] * counter);
  228. c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
  229. Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
  230. coeffs[i][0] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
  231. c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
  232. Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
  233. coeffs[i][1] = lerp(PassthruCoeff, c, dirfact) * gain * (1.0f/32767.0f);
  234. coeffStep[i][0] = delta * (coeffs[i][0] - left);
  235. coeffStep[i][1] = delta * (coeffs[i][1] - right);
  236. for(i = 1;i < Hrtf->irSize;i++)
  237. {
  238. left = coeffs[i][0] - (coeffStep[i][0] * counter);
  239. right = coeffs[i][1] - (coeffStep[i][1] * counter);
  240. c = (Hrtf->coeffs[lidx[0]+i]*blend[0] + Hrtf->coeffs[lidx[1]+i]*blend[1] +
  241. Hrtf->coeffs[lidx[2]+i]*blend[2] + Hrtf->coeffs[lidx[3]+i]*blend[3]);
  242. coeffs[i][0] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
  243. c = (Hrtf->coeffs[ridx[0]+i]*blend[0] + Hrtf->coeffs[ridx[1]+i]*blend[1] +
  244. Hrtf->coeffs[ridx[2]+i]*blend[2] + Hrtf->coeffs[ridx[3]+i]*blend[3]);
  245. coeffs[i][1] = lerp(0.0f, c, dirfact) * gain * (1.0f/32767.0f);
  246. coeffStep[i][0] = delta * (coeffs[i][0] - left);
  247. coeffStep[i][1] = delta * (coeffs[i][1] - right);
  248. }
  249. }
  250. else
  251. {
  252. for(i = 0;i < Hrtf->irSize;i++)
  253. {
  254. left = coeffs[i][0] - (coeffStep[i][0] * counter);
  255. right = coeffs[i][1] - (coeffStep[i][1] * counter);
  256. coeffs[i][0] = 0.0f;
  257. coeffs[i][1] = 0.0f;
  258. coeffStep[i][0] = delta * -left;
  259. coeffStep[i][1] = delta * -right;
  260. }
  261. }
  262. /* The stepping count is the number of samples necessary for the HRIR to
  263. * complete its transition. The mixer will only apply stepping for this
  264. * many samples.
  265. */
  266. return fastf2u(steps);
  267. }
  268. /* Calculates HRTF coefficients for B-Format channels (only up to first-order).
  269. * Note that these will decode a B-Format output mix, which uses FuMa ordering
  270. * and scaling, not N3D!
  271. */
  272. void GetBFormatHrtfCoeffs(const struct Hrtf *Hrtf, const ALuint num_chans, ALfloat (**coeffs_list)[2], ALuint **delay_list)
  273. {
  274. ALuint elev_idx, azi_idx;
  275. ALfloat scale;
  276. ALuint i, c;
  277. assert(num_chans <= 4);
  278. for(c = 0;c < num_chans;c++)
  279. {
  280. ALfloat (*coeffs)[2] = coeffs_list[c];
  281. ALuint *delay = delay_list[c];
  282. for(i = 0;i < Hrtf->irSize;i++)
  283. {
  284. coeffs[i][0] = 0.0f;
  285. coeffs[i][1] = 0.0f;
  286. }
  287. delay[0] = 0;
  288. delay[1] = 0;
  289. }
  290. /* NOTE: HRTF coefficients are generated by combining all the HRIRs in the
  291. * dataset, with each entry scaled according to how much it contributes to
  292. * the given B-Format channel based on its direction (including negative
  293. * contributions!).
  294. */
  295. scale = 0.0f;
  296. for(elev_idx = 0;elev_idx < Hrtf->evCount;elev_idx++)
  297. {
  298. ALfloat elev = (ALfloat)elev_idx/(ALfloat)(Hrtf->evCount-1)*F_PI - F_PI_2;
  299. ALuint evoffset = Hrtf->evOffset[elev_idx];
  300. ALuint azcount = Hrtf->azCount[elev_idx];
  301. scale += (ALfloat)azcount;
  302. for(azi_idx = 0;azi_idx < azcount;azi_idx++)
  303. {
  304. ALuint lidx, ridx;
  305. ALfloat ambi_coeffs[4];
  306. ALfloat az, gain;
  307. ALfloat x, y, z;
  308. lidx = evoffset + azi_idx;
  309. ridx = evoffset + ((azcount-azi_idx) % azcount);
  310. az = (ALfloat)azi_idx / (ALfloat)azcount * F_TAU;
  311. if(az > F_PI) az -= F_TAU;
  312. x = cosf(-az) * cosf(elev);
  313. y = sinf(-az) * cosf(elev);
  314. z = sinf(elev);
  315. ambi_coeffs[0] = 1.414213562f;
  316. ambi_coeffs[1] = x;
  317. ambi_coeffs[2] = y;
  318. ambi_coeffs[3] = z;
  319. for(c = 0;c < num_chans;c++)
  320. {
  321. ALfloat (*coeffs)[2] = coeffs_list[c];
  322. ALuint *delay = delay_list[c];
  323. /* NOTE: Always include the total delay average since the
  324. * channels need to have matching delays. */
  325. delay[0] += Hrtf->delays[lidx];
  326. delay[1] += Hrtf->delays[ridx];
  327. gain = ambi_coeffs[c];
  328. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  329. continue;
  330. for(i = 0;i < Hrtf->irSize;i++)
  331. {
  332. coeffs[i][0] += Hrtf->coeffs[lidx*Hrtf->irSize + i]*(1.0f/32767.0f) * gain;
  333. coeffs[i][1] += Hrtf->coeffs[ridx*Hrtf->irSize + i]*(1.0f/32767.0f) * gain;
  334. }
  335. }
  336. }
  337. }
  338. scale = 1.0f/scale;
  339. for(c = 0;c < num_chans;c++)
  340. {
  341. ALfloat (*coeffs)[2] = coeffs_list[c];
  342. ALuint *delay = delay_list[c];
  343. for(i = 0;i < Hrtf->irSize;i++)
  344. {
  345. coeffs[i][0] *= scale;
  346. coeffs[i][1] *= scale;
  347. }
  348. delay[0] = minu((ALuint)((ALfloat)delay[0] * scale), HRTF_HISTORY_LENGTH-1);
  349. delay[0] <<= HRTFDELAY_BITS;
  350. delay[1] = minu((ALuint)((ALfloat)delay[1] * scale), HRTF_HISTORY_LENGTH-1);
  351. delay[1] <<= HRTFDELAY_BITS;
  352. }
  353. }
  354. static struct Hrtf *LoadHrtf00(FILE *f)
  355. {
  356. const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
  357. struct Hrtf *Hrtf = NULL;
  358. ALboolean failed = AL_FALSE;
  359. ALuint rate = 0, irCount = 0;
  360. ALushort irSize = 0;
  361. ALubyte evCount = 0;
  362. ALubyte *azCount = NULL;
  363. ALushort *evOffset = NULL;
  364. ALshort *coeffs = NULL;
  365. ALubyte *delays = NULL;
  366. ALuint i, j;
  367. rate = fgetc(f);
  368. rate |= fgetc(f)<<8;
  369. rate |= fgetc(f)<<16;
  370. rate |= fgetc(f)<<24;
  371. irCount = fgetc(f);
  372. irCount |= fgetc(f)<<8;
  373. irSize = fgetc(f);
  374. irSize |= fgetc(f)<<8;
  375. evCount = fgetc(f);
  376. if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
  377. {
  378. ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
  379. irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
  380. failed = AL_TRUE;
  381. }
  382. if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
  383. {
  384. ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
  385. evCount, MIN_EV_COUNT, MAX_EV_COUNT);
  386. failed = AL_TRUE;
  387. }
  388. if(failed)
  389. return NULL;
  390. azCount = malloc(sizeof(azCount[0])*evCount);
  391. evOffset = malloc(sizeof(evOffset[0])*evCount);
  392. if(azCount == NULL || evOffset == NULL)
  393. {
  394. ERR("Out of memory.\n");
  395. failed = AL_TRUE;
  396. }
  397. if(!failed)
  398. {
  399. evOffset[0] = fgetc(f);
  400. evOffset[0] |= fgetc(f)<<8;
  401. for(i = 1;i < evCount;i++)
  402. {
  403. evOffset[i] = fgetc(f);
  404. evOffset[i] |= fgetc(f)<<8;
  405. if(evOffset[i] <= evOffset[i-1])
  406. {
  407. ERR("Invalid evOffset: evOffset[%d]=%d (last=%d)\n",
  408. i, evOffset[i], evOffset[i-1]);
  409. failed = AL_TRUE;
  410. }
  411. azCount[i-1] = evOffset[i] - evOffset[i-1];
  412. if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
  413. {
  414. ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
  415. i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
  416. failed = AL_TRUE;
  417. }
  418. }
  419. if(irCount <= evOffset[i-1])
  420. {
  421. ERR("Invalid evOffset: evOffset[%d]=%d (irCount=%d)\n",
  422. i-1, evOffset[i-1], irCount);
  423. failed = AL_TRUE;
  424. }
  425. azCount[i-1] = irCount - evOffset[i-1];
  426. if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
  427. {
  428. ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
  429. i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
  430. failed = AL_TRUE;
  431. }
  432. }
  433. if(!failed)
  434. {
  435. coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
  436. delays = malloc(sizeof(delays[0])*irCount);
  437. if(coeffs == NULL || delays == NULL)
  438. {
  439. ERR("Out of memory.\n");
  440. failed = AL_TRUE;
  441. }
  442. }
  443. if(!failed)
  444. {
  445. for(i = 0;i < irCount*irSize;i+=irSize)
  446. {
  447. for(j = 0;j < irSize;j++)
  448. {
  449. ALshort coeff;
  450. coeff = fgetc(f);
  451. coeff |= fgetc(f)<<8;
  452. coeffs[i+j] = coeff;
  453. }
  454. }
  455. for(i = 0;i < irCount;i++)
  456. {
  457. delays[i] = fgetc(f);
  458. if(delays[i] > maxDelay)
  459. {
  460. ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
  461. failed = AL_TRUE;
  462. }
  463. }
  464. if(feof(f))
  465. {
  466. ERR("Premature end of data\n");
  467. failed = AL_TRUE;
  468. }
  469. }
  470. if(!failed)
  471. {
  472. Hrtf = malloc(sizeof(struct Hrtf));
  473. if(Hrtf == NULL)
  474. {
  475. ERR("Out of memory.\n");
  476. failed = AL_TRUE;
  477. }
  478. }
  479. if(!failed)
  480. {
  481. Hrtf->sampleRate = rate;
  482. Hrtf->irSize = irSize;
  483. Hrtf->evCount = evCount;
  484. Hrtf->azCount = azCount;
  485. Hrtf->evOffset = evOffset;
  486. Hrtf->coeffs = coeffs;
  487. Hrtf->delays = delays;
  488. AL_STRING_INIT(Hrtf->filename);
  489. Hrtf->next = NULL;
  490. return Hrtf;
  491. }
  492. free(azCount);
  493. free(evOffset);
  494. free(coeffs);
  495. free(delays);
  496. return NULL;
  497. }
  498. static struct Hrtf *LoadHrtf01(FILE *f)
  499. {
  500. const ALubyte maxDelay = HRTF_HISTORY_LENGTH-1;
  501. struct Hrtf *Hrtf = NULL;
  502. ALboolean failed = AL_FALSE;
  503. ALuint rate = 0, irCount = 0;
  504. ALubyte irSize = 0, evCount = 0;
  505. ALubyte *azCount = NULL;
  506. ALushort *evOffset = NULL;
  507. ALshort *coeffs = NULL;
  508. ALubyte *delays = NULL;
  509. ALuint i, j;
  510. rate = fgetc(f);
  511. rate |= fgetc(f)<<8;
  512. rate |= fgetc(f)<<16;
  513. rate |= fgetc(f)<<24;
  514. irSize = fgetc(f);
  515. evCount = fgetc(f);
  516. if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
  517. {
  518. ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
  519. irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
  520. failed = AL_TRUE;
  521. }
  522. if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
  523. {
  524. ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
  525. evCount, MIN_EV_COUNT, MAX_EV_COUNT);
  526. failed = AL_TRUE;
  527. }
  528. if(failed)
  529. return NULL;
  530. azCount = malloc(sizeof(azCount[0])*evCount);
  531. evOffset = malloc(sizeof(evOffset[0])*evCount);
  532. if(azCount == NULL || evOffset == NULL)
  533. {
  534. ERR("Out of memory.\n");
  535. failed = AL_TRUE;
  536. }
  537. if(!failed)
  538. {
  539. for(i = 0;i < evCount;i++)
  540. {
  541. azCount[i] = fgetc(f);
  542. if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
  543. {
  544. ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
  545. i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
  546. failed = AL_TRUE;
  547. }
  548. }
  549. }
  550. if(!failed)
  551. {
  552. evOffset[0] = 0;
  553. irCount = azCount[0];
  554. for(i = 1;i < evCount;i++)
  555. {
  556. evOffset[i] = evOffset[i-1] + azCount[i-1];
  557. irCount += azCount[i];
  558. }
  559. coeffs = malloc(sizeof(coeffs[0])*irSize*irCount);
  560. delays = malloc(sizeof(delays[0])*irCount);
  561. if(coeffs == NULL || delays == NULL)
  562. {
  563. ERR("Out of memory.\n");
  564. failed = AL_TRUE;
  565. }
  566. }
  567. if(!failed)
  568. {
  569. for(i = 0;i < irCount*irSize;i+=irSize)
  570. {
  571. for(j = 0;j < irSize;j++)
  572. {
  573. ALshort coeff;
  574. coeff = fgetc(f);
  575. coeff |= fgetc(f)<<8;
  576. coeffs[i+j] = coeff;
  577. }
  578. }
  579. for(i = 0;i < irCount;i++)
  580. {
  581. delays[i] = fgetc(f);
  582. if(delays[i] > maxDelay)
  583. {
  584. ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i], maxDelay);
  585. failed = AL_TRUE;
  586. }
  587. }
  588. if(feof(f))
  589. {
  590. ERR("Premature end of data\n");
  591. failed = AL_TRUE;
  592. }
  593. }
  594. if(!failed)
  595. {
  596. Hrtf = malloc(sizeof(struct Hrtf));
  597. if(Hrtf == NULL)
  598. {
  599. ERR("Out of memory.\n");
  600. failed = AL_TRUE;
  601. }
  602. }
  603. if(!failed)
  604. {
  605. Hrtf->sampleRate = rate;
  606. Hrtf->irSize = irSize;
  607. Hrtf->evCount = evCount;
  608. Hrtf->azCount = azCount;
  609. Hrtf->evOffset = evOffset;
  610. Hrtf->coeffs = coeffs;
  611. Hrtf->delays = delays;
  612. AL_STRING_INIT(Hrtf->filename);
  613. Hrtf->next = NULL;
  614. return Hrtf;
  615. }
  616. free(azCount);
  617. free(evOffset);
  618. free(coeffs);
  619. free(delays);
  620. return NULL;
  621. }
  622. static void AddFileEntry(vector_HrtfEntry *list, al_string *filename)
  623. {
  624. HrtfEntry entry = { AL_STRING_INIT_STATIC(), *filename, NULL };
  625. HrtfEntry *iter;
  626. const char *name;
  627. int i;
  628. name = strrchr(al_string_get_cstr(entry.filename), '/');
  629. if(!name) name = strrchr(al_string_get_cstr(entry.filename), '\\');
  630. if(!name) name = al_string_get_cstr(entry.filename);
  631. else ++name;
  632. entry.hrtf = LoadedHrtfs;
  633. while(entry.hrtf)
  634. {
  635. if(al_string_cmp(entry.filename, entry.hrtf->filename) == 0)
  636. break;
  637. entry.hrtf = entry.hrtf->next;
  638. }
  639. if(!entry.hrtf)
  640. {
  641. struct Hrtf *hrtf = NULL;
  642. ALchar magic[8];
  643. FILE *f;
  644. TRACE("Loading %s...\n", al_string_get_cstr(entry.filename));
  645. f = al_fopen(al_string_get_cstr(entry.filename), "rb");
  646. if(f == NULL)
  647. {
  648. ERR("Could not open %s\n", al_string_get_cstr(entry.filename));
  649. goto error;
  650. }
  651. if(fread(magic, 1, sizeof(magic), f) != sizeof(magic))
  652. ERR("Failed to read header from %s\n", al_string_get_cstr(entry.filename));
  653. else
  654. {
  655. if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
  656. {
  657. TRACE("Detected data set format v0\n");
  658. hrtf = LoadHrtf00(f);
  659. }
  660. else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
  661. {
  662. TRACE("Detected data set format v1\n");
  663. hrtf = LoadHrtf01(f);
  664. }
  665. else
  666. ERR("Invalid header in %s: \"%.8s\"\n", al_string_get_cstr(entry.filename), magic);
  667. }
  668. fclose(f);
  669. if(!hrtf)
  670. {
  671. ERR("Failed to load %s\n", al_string_get_cstr(entry.filename));
  672. goto error;
  673. }
  674. al_string_copy(&hrtf->filename, entry.filename);
  675. hrtf->next = LoadedHrtfs;
  676. LoadedHrtfs = hrtf;
  677. TRACE("Loaded HRTF support for format: %s %uhz\n",
  678. DevFmtChannelsString(DevFmtStereo), hrtf->sampleRate);
  679. entry.hrtf = hrtf;
  680. }
  681. /* TODO: Get a human-readable name from the HRTF data (possibly coming in a
  682. * format update). */
  683. i = 0;
  684. do {
  685. al_string_copy_cstr(&entry.name, name);
  686. if(i != 0)
  687. {
  688. char str[64];
  689. snprintf(str, sizeof(str), " #%d", i+1);
  690. al_string_append_cstr(&entry.name, str);
  691. }
  692. ++i;
  693. #define MATCH_NAME(i) (al_string_cmp(entry.name, (i)->name) == 0)
  694. VECTOR_FIND_IF(iter, HrtfEntry, *list, MATCH_NAME);
  695. #undef MATCH_NAME
  696. } while(iter != VECTOR_ITER_END(*list));
  697. TRACE("Adding entry \"%s\" from file \"%s\"\n", al_string_get_cstr(entry.name),
  698. al_string_get_cstr(entry.filename));
  699. VECTOR_PUSH_BACK(*list, entry);
  700. return;
  701. error:
  702. al_string_deinit(&entry.filename);
  703. }
  704. vector_HrtfEntry EnumerateHrtf(const_al_string devname)
  705. {
  706. vector_HrtfEntry list = VECTOR_INIT_STATIC();
  707. const char *fnamelist = "%s.mhr";
  708. ConfigValueStr(al_string_get_cstr(devname), NULL, "hrtf_tables", &fnamelist);
  709. while(fnamelist && *fnamelist)
  710. {
  711. while(isspace(*fnamelist) || *fnamelist == ',')
  712. fnamelist++;
  713. if(*fnamelist != '\0')
  714. {
  715. const char *next, *end;
  716. next = strchr(fnamelist, ',');
  717. if(!next)
  718. end = fnamelist + strlen(fnamelist);
  719. else
  720. end = next++;
  721. while(end != fnamelist && isspace(*(end-1)))
  722. --end;
  723. if(end != fnamelist)
  724. {
  725. al_string fname = AL_STRING_INIT_STATIC();
  726. vector_al_string flist;
  727. al_string_append_range(&fname, fnamelist, end);
  728. flist = SearchDataFiles(al_string_get_cstr(fname), "openal/hrtf");
  729. VECTOR_FOR_EACH_PARAMS(al_string, flist, AddFileEntry, &list);
  730. VECTOR_DEINIT(flist);
  731. al_string_deinit(&fname);
  732. }
  733. fnamelist = next;
  734. }
  735. }
  736. return list;
  737. }
  738. void FreeHrtfList(vector_HrtfEntry *list)
  739. {
  740. #define CLEAR_ENTRY(i) do { \
  741. al_string_deinit(&(i)->name); \
  742. al_string_deinit(&(i)->filename); \
  743. } while(0)
  744. VECTOR_FOR_EACH(HrtfEntry, *list, CLEAR_ENTRY);
  745. VECTOR_DEINIT(*list);
  746. #undef CLEAR_ENTRY
  747. }
  748. ALuint GetHrtfSampleRate(const struct Hrtf *Hrtf)
  749. {
  750. return Hrtf->sampleRate;
  751. }
  752. ALuint GetHrtfIrSize(const struct Hrtf *Hrtf)
  753. {
  754. return Hrtf->irSize;
  755. }
  756. void FreeHrtfs(void)
  757. {
  758. struct Hrtf *Hrtf = NULL;
  759. while((Hrtf=LoadedHrtfs) != NULL)
  760. {
  761. LoadedHrtfs = Hrtf->next;
  762. free((void*)Hrtf->azCount);
  763. free((void*)Hrtf->evOffset);
  764. free((void*)Hrtf->coeffs);
  765. free((void*)Hrtf->delays);
  766. al_string_deinit(&Hrtf->filename);
  767. free(Hrtf);
  768. }
  769. }