panning.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2010 by authors.
  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 <math.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <assert.h>
  26. #include "alMain.h"
  27. #include "alAuxEffectSlot.h"
  28. #include "alu.h"
  29. #include "bool.h"
  30. #include "ambdec.h"
  31. #include "bformatdec.h"
  32. #include "uhjfilter.h"
  33. #include "bs2b.h"
  34. extern inline void CalcXYZCoeffs(ALfloat x, ALfloat y, ALfloat z, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
  35. #define ZERO_ORDER_SCALE 0.0f
  36. #define FIRST_ORDER_SCALE 1.0f
  37. #define SECOND_ORDER_SCALE (1.0f / 1.22474f)
  38. #define THIRD_ORDER_SCALE (1.0f / 1.30657f)
  39. static const ALuint FuMa2ACN[MAX_AMBI_COEFFS] = {
  40. 0, /* W */
  41. 3, /* X */
  42. 1, /* Y */
  43. 2, /* Z */
  44. 6, /* R */
  45. 7, /* S */
  46. 5, /* T */
  47. 8, /* U */
  48. 4, /* V */
  49. 12, /* K */
  50. 13, /* L */
  51. 11, /* M */
  52. 14, /* N */
  53. 10, /* O */
  54. 15, /* P */
  55. 9, /* Q */
  56. };
  57. static const ALuint ACN2ACN[MAX_AMBI_COEFFS] = {
  58. 0, 1, 2, 3, 4, 5, 6, 7,
  59. 8, 9, 10, 11, 12, 13, 14, 15
  60. };
  61. /* NOTE: These are scale factors as applied to Ambisonics content. Decoder
  62. * coefficients should be divided by these values to get proper N3D scalings.
  63. */
  64. static const ALfloat UnitScale[MAX_AMBI_COEFFS] = {
  65. 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  66. 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
  67. };
  68. static const ALfloat SN3D2N3DScale[MAX_AMBI_COEFFS] = {
  69. 1.000000000f, /* ACN 0 (W), sqrt(1) */
  70. 1.732050808f, /* ACN 1 (Y), sqrt(3) */
  71. 1.732050808f, /* ACN 2 (Z), sqrt(3) */
  72. 1.732050808f, /* ACN 3 (X), sqrt(3) */
  73. 2.236067978f, /* ACN 4 (V), sqrt(5) */
  74. 2.236067978f, /* ACN 5 (T), sqrt(5) */
  75. 2.236067978f, /* ACN 6 (R), sqrt(5) */
  76. 2.236067978f, /* ACN 7 (S), sqrt(5) */
  77. 2.236067978f, /* ACN 8 (U), sqrt(5) */
  78. 2.645751311f, /* ACN 9 (Q), sqrt(7) */
  79. 2.645751311f, /* ACN 10 (O), sqrt(7) */
  80. 2.645751311f, /* ACN 11 (M), sqrt(7) */
  81. 2.645751311f, /* ACN 12 (K), sqrt(7) */
  82. 2.645751311f, /* ACN 13 (L), sqrt(7) */
  83. 2.645751311f, /* ACN 14 (N), sqrt(7) */
  84. 2.645751311f, /* ACN 15 (P), sqrt(7) */
  85. };
  86. static const ALfloat FuMa2N3DScale[MAX_AMBI_COEFFS] = {
  87. 1.414213562f, /* ACN 0 (W), sqrt(2) */
  88. 1.732050808f, /* ACN 1 (Y), sqrt(3) */
  89. 1.732050808f, /* ACN 2 (Z), sqrt(3) */
  90. 1.732050808f, /* ACN 3 (X), sqrt(3) */
  91. 1.936491673f, /* ACN 4 (V), sqrt(15)/2 */
  92. 1.936491673f, /* ACN 5 (T), sqrt(15)/2 */
  93. 2.236067978f, /* ACN 6 (R), sqrt(5) */
  94. 1.936491673f, /* ACN 7 (S), sqrt(15)/2 */
  95. 1.936491673f, /* ACN 8 (U), sqrt(15)/2 */
  96. 2.091650066f, /* ACN 9 (Q), sqrt(35/8) */
  97. 1.972026594f, /* ACN 10 (O), sqrt(35)/3 */
  98. 2.231093404f, /* ACN 11 (M), sqrt(224/45) */
  99. 2.645751311f, /* ACN 12 (K), sqrt(7) */
  100. 2.231093404f, /* ACN 13 (L), sqrt(224/45) */
  101. 1.972026594f, /* ACN 14 (N), sqrt(35)/3 */
  102. 2.091650066f, /* ACN 15 (P), sqrt(35/8) */
  103. };
  104. void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
  105. {
  106. /* Convert from OpenAL coords to Ambisonics. */
  107. ALfloat x = -dir[2];
  108. ALfloat y = -dir[0];
  109. ALfloat z = dir[1];
  110. /* Zeroth-order */
  111. coeffs[0] = 1.0f; /* ACN 0 = 1 */
  112. /* First-order */
  113. coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
  114. coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
  115. coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
  116. /* Second-order */
  117. coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
  118. coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
  119. coeffs[6] = 1.118033989f * (3.0f*z*z - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
  120. coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
  121. coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
  122. /* Third-order */
  123. coeffs[9] = 2.091650066f * y * (3.0f*x*x - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
  124. coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
  125. coeffs[11] = 1.620185175f * y * (5.0f*z*z - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
  126. coeffs[12] = 1.322875656f * z * (5.0f*z*z - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
  127. coeffs[13] = 1.620185175f * x * (5.0f*z*z - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
  128. coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
  129. coeffs[15] = 2.091650066f * x * (x*x - 3.0f*y*y); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
  130. if(spread > 0.0f)
  131. {
  132. /* Implement the spread by using a spherical source that subtends the
  133. * angle spread. See:
  134. * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
  135. *
  136. * When adjusted for N3D normalization instead of SN3D, these
  137. * calculations are:
  138. *
  139. * ZH0 = -sqrt(pi) * (-1+ca);
  140. * ZH1 = 0.5*sqrt(pi) * sa*sa;
  141. * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
  142. * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
  143. * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
  144. * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
  145. *
  146. * The gain of the source is compensated for size, so that the
  147. * loundness doesn't depend on the spread. That is, the factors are
  148. * scaled so that ZH0 remains 1 regardless of the spread. Thus:
  149. *
  150. * ZH0 = 1.0f;
  151. * ZH1 = 0.5f * (ca+1.0f);
  152. * ZH2 = 0.5f * (ca+1.0f)*ca;
  153. * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
  154. * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
  155. * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
  156. */
  157. ALfloat ca = cosf(spread * 0.5f);
  158. ALfloat ZH0_norm = 1.0f;
  159. ALfloat ZH1_norm = 0.5f * (ca+1.f);
  160. ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca;
  161. ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f);
  162. /* Zeroth-order */
  163. coeffs[0] *= ZH0_norm;
  164. /* First-order */
  165. coeffs[1] *= ZH1_norm;
  166. coeffs[2] *= ZH1_norm;
  167. coeffs[3] *= ZH1_norm;
  168. /* Second-order */
  169. coeffs[4] *= ZH2_norm;
  170. coeffs[5] *= ZH2_norm;
  171. coeffs[6] *= ZH2_norm;
  172. coeffs[7] *= ZH2_norm;
  173. coeffs[8] *= ZH2_norm;
  174. /* Third-order */
  175. coeffs[9] *= ZH3_norm;
  176. coeffs[10] *= ZH3_norm;
  177. coeffs[11] *= ZH3_norm;
  178. coeffs[12] *= ZH3_norm;
  179. coeffs[13] *= ZH3_norm;
  180. coeffs[14] *= ZH3_norm;
  181. coeffs[15] *= ZH3_norm;
  182. }
  183. }
  184. void CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
  185. {
  186. ALfloat dir[3] = {
  187. sinf(azimuth) * cosf(elevation),
  188. sinf(elevation),
  189. -cosf(azimuth) * cosf(elevation)
  190. };
  191. CalcDirectionCoeffs(dir, spread, coeffs);
  192. }
  193. void ComputeAmbientGainsMC(const ChannelConfig *chancoeffs, ALuint numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
  194. {
  195. ALuint i;
  196. for(i = 0;i < numchans;i++)
  197. {
  198. // The W coefficients are based on a mathematical average of the
  199. // output. The square root of the base average provides for a more
  200. // perceptual average volume, better suited to non-directional gains.
  201. gains[i] = sqrtf(chancoeffs[i][0]) * ingain;
  202. }
  203. for(;i < MAX_OUTPUT_CHANNELS;i++)
  204. gains[i] = 0.0f;
  205. }
  206. void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALuint numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
  207. {
  208. ALfloat gain = 0.0f;
  209. ALuint i;
  210. for(i = 0;i < numchans;i++)
  211. {
  212. if(chanmap[i].Index == 0)
  213. gain += chanmap[i].Scale;
  214. }
  215. gains[0] = gain * 1.414213562f * ingain;
  216. for(i = 1;i < MAX_OUTPUT_CHANNELS;i++)
  217. gains[i] = 0.0f;
  218. }
  219. void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALuint numchans, ALuint numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
  220. {
  221. ALuint i, j;
  222. for(i = 0;i < numchans;i++)
  223. {
  224. float gain = 0.0f;
  225. for(j = 0;j < numcoeffs;j++)
  226. gain += chancoeffs[i][j]*coeffs[j];
  227. gains[i] = gain * ingain;
  228. }
  229. for(;i < MAX_OUTPUT_CHANNELS;i++)
  230. gains[i] = 0.0f;
  231. }
  232. void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALuint numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
  233. {
  234. ALuint i;
  235. for(i = 0;i < numchans;i++)
  236. gains[i] = chanmap[i].Scale * coeffs[chanmap[i].Index] * ingain;
  237. for(;i < MAX_OUTPUT_CHANNELS;i++)
  238. gains[i] = 0.0f;
  239. }
  240. void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALuint numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
  241. {
  242. ALuint i, j;
  243. for(i = 0;i < numchans;i++)
  244. {
  245. float gain = 0.0f;
  246. for(j = 0;j < 4;j++)
  247. gain += chancoeffs[i][j] * mtx[j];
  248. gains[i] = gain * ingain;
  249. }
  250. for(;i < MAX_OUTPUT_CHANNELS;i++)
  251. gains[i] = 0.0f;
  252. }
  253. void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALuint numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS])
  254. {
  255. ALuint i;
  256. for(i = 0;i < numchans;i++)
  257. gains[i] = chanmap[i].Scale * mtx[chanmap[i].Index] * ingain;
  258. for(;i < MAX_OUTPUT_CHANNELS;i++)
  259. gains[i] = 0.0f;
  260. }
  261. static inline const char *GetLabelFromChannel(enum Channel channel)
  262. {
  263. switch(channel)
  264. {
  265. case FrontLeft: return "front-left";
  266. case FrontRight: return "front-right";
  267. case FrontCenter: return "front-center";
  268. case LFE: return "lfe";
  269. case BackLeft: return "back-left";
  270. case BackRight: return "back-right";
  271. case BackCenter: return "back-center";
  272. case SideLeft: return "side-left";
  273. case SideRight: return "side-right";
  274. case UpperFrontLeft: return "upper-front-left";
  275. case UpperFrontRight: return "upper-front-right";
  276. case UpperBackLeft: return "upper-back-left";
  277. case UpperBackRight: return "upper-back-right";
  278. case LowerFrontLeft: return "lower-front-left";
  279. case LowerFrontRight: return "lower-front-right";
  280. case LowerBackLeft: return "lower-back-left";
  281. case LowerBackRight: return "lower-back-right";
  282. case Aux0: return "aux-0";
  283. case Aux1: return "aux-1";
  284. case Aux2: return "aux-2";
  285. case Aux3: return "aux-3";
  286. case Aux4: return "aux-4";
  287. case Aux5: return "aux-5";
  288. case Aux6: return "aux-6";
  289. case Aux7: return "aux-7";
  290. case Aux8: return "aux-8";
  291. case Aux9: return "aux-9";
  292. case Aux10: return "aux-10";
  293. case Aux11: return "aux-11";
  294. case Aux12: return "aux-12";
  295. case Aux13: return "aux-13";
  296. case Aux14: return "aux-14";
  297. case Aux15: return "aux-15";
  298. case InvalidChannel: break;
  299. }
  300. return "(unknown)";
  301. }
  302. typedef struct ChannelMap {
  303. enum Channel ChanName;
  304. ChannelConfig Config;
  305. } ChannelMap;
  306. static void SetChannelMap(const enum Channel *devchans, ChannelConfig *ambicoeffs,
  307. const ChannelMap *chanmap, size_t count, ALuint *outcount,
  308. ALboolean isfuma)
  309. {
  310. const ALuint *acnmap = isfuma ? FuMa2ACN : ACN2ACN;
  311. const ALfloat *n3dscale = isfuma ? FuMa2N3DScale : UnitScale;
  312. size_t j, k;
  313. ALuint i;
  314. for(i = 0;i < MAX_OUTPUT_CHANNELS && devchans[i] != InvalidChannel;i++)
  315. {
  316. if(devchans[i] == LFE)
  317. {
  318. for(j = 0;j < MAX_AMBI_COEFFS;j++)
  319. ambicoeffs[i][j] = 0.0f;
  320. continue;
  321. }
  322. for(j = 0;j < count;j++)
  323. {
  324. if(devchans[i] != chanmap[j].ChanName)
  325. continue;
  326. for(k = 0;k < MAX_AMBI_COEFFS;++k)
  327. {
  328. ALuint acn = acnmap[k];
  329. ambicoeffs[i][acn] = chanmap[j].Config[k] / n3dscale[acn];
  330. }
  331. break;
  332. }
  333. if(j == count)
  334. ERR("Failed to match %s channel (%u) in channel map\n", GetLabelFromChannel(devchans[i]), i);
  335. }
  336. *outcount = i;
  337. }
  338. static bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALuint speakermap[MAX_OUTPUT_CHANNELS])
  339. {
  340. ALuint i;
  341. for(i = 0;i < conf->NumSpeakers;i++)
  342. {
  343. int c = -1;
  344. /* NOTE: AmbDec does not define any standard speaker names, however
  345. * for this to work we have to by able to find the output channel
  346. * the speaker definition corresponds to. Therefore, OpenAL Soft
  347. * requires these channel labels to be recognized:
  348. *
  349. * LF = Front left
  350. * RF = Front right
  351. * LS = Side left
  352. * RS = Side right
  353. * LB = Back left
  354. * RB = Back right
  355. * CE = Front center
  356. * CB = Back center
  357. *
  358. * Additionally, surround51 will acknowledge back speakers for side
  359. * channels, and surround51rear will acknowledge side speakers for
  360. * back channels, to avoid issues with an ambdec expecting 5.1 to
  361. * use the side channels when the device is configured for back,
  362. * and vice-versa.
  363. */
  364. if(al_string_cmp_cstr(conf->Speakers[i].Name, "LF") == 0)
  365. c = GetChannelIdxByName(device->RealOut, FrontLeft);
  366. else if(al_string_cmp_cstr(conf->Speakers[i].Name, "RF") == 0)
  367. c = GetChannelIdxByName(device->RealOut, FrontRight);
  368. else if(al_string_cmp_cstr(conf->Speakers[i].Name, "CE") == 0)
  369. c = GetChannelIdxByName(device->RealOut, FrontCenter);
  370. else if(al_string_cmp_cstr(conf->Speakers[i].Name, "LS") == 0)
  371. {
  372. if(device->FmtChans == DevFmtX51Rear)
  373. c = GetChannelIdxByName(device->RealOut, BackLeft);
  374. else
  375. c = GetChannelIdxByName(device->RealOut, SideLeft);
  376. }
  377. else if(al_string_cmp_cstr(conf->Speakers[i].Name, "RS") == 0)
  378. {
  379. if(device->FmtChans == DevFmtX51Rear)
  380. c = GetChannelIdxByName(device->RealOut, BackRight);
  381. else
  382. c = GetChannelIdxByName(device->RealOut, SideRight);
  383. }
  384. else if(al_string_cmp_cstr(conf->Speakers[i].Name, "LB") == 0)
  385. {
  386. if(device->FmtChans == DevFmtX51)
  387. c = GetChannelIdxByName(device->RealOut, SideLeft);
  388. else
  389. c = GetChannelIdxByName(device->RealOut, BackLeft);
  390. }
  391. else if(al_string_cmp_cstr(conf->Speakers[i].Name, "RB") == 0)
  392. {
  393. if(device->FmtChans == DevFmtX51)
  394. c = GetChannelIdxByName(device->RealOut, SideRight);
  395. else
  396. c = GetChannelIdxByName(device->RealOut, BackRight);
  397. }
  398. else if(al_string_cmp_cstr(conf->Speakers[i].Name, "CB") == 0)
  399. c = GetChannelIdxByName(device->RealOut, BackCenter);
  400. else
  401. {
  402. const char *name = al_string_get_cstr(conf->Speakers[i].Name);
  403. unsigned int n;
  404. char ch;
  405. if(sscanf(name, "AUX%u%c", &n, &ch) == 1 && n < 16)
  406. c = GetChannelIdxByName(device->RealOut, Aux0+n);
  407. else
  408. {
  409. ERR("AmbDec speaker label \"%s\" not recognized\n", name);
  410. return false;
  411. }
  412. }
  413. if(c == -1)
  414. {
  415. ERR("Failed to lookup AmbDec speaker label %s\n",
  416. al_string_get_cstr(conf->Speakers[i].Name));
  417. return false;
  418. }
  419. speakermap[i] = c;
  420. }
  421. return true;
  422. }
  423. /* NOTE: These decoder coefficients are using FuMa channel ordering and
  424. * normalization, since that's what was produced by the Ambisonic Decoder
  425. * Toolbox. SetChannelMap will convert them to N3D.
  426. */
  427. static const ChannelMap MonoCfg[1] = {
  428. { FrontCenter, { 1.414213562f } },
  429. }, StereoCfg[2] = {
  430. { FrontLeft, { 0.707106781f, 0.0f, 0.5f, 0.0f } },
  431. { FrontRight, { 0.707106781f, 0.0f, -0.5f, 0.0f } },
  432. }, QuadCfg[4] = {
  433. { FrontLeft, { 0.353553f, 0.306186f, 0.306186f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, 0.125000f } },
  434. { FrontRight, { 0.353553f, 0.306186f, -0.306186f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, -0.125000f } },
  435. { BackLeft, { 0.353553f, -0.306186f, 0.306186f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, -0.125000f } },
  436. { BackRight, { 0.353553f, -0.306186f, -0.306186f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, 0.125000f } },
  437. }, X51SideCfg[5] = {
  438. { FrontLeft, { 0.208954f, 0.199518f, 0.223424f, 0.0f, 0.0f, 0.0f, 0.0f, -0.012543f, 0.144260f } },
  439. { FrontRight, { 0.208950f, 0.199514f, -0.223425f, 0.0f, 0.0f, 0.0f, 0.0f, -0.012544f, -0.144258f } },
  440. { FrontCenter, { 0.109403f, 0.168250f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.100431f, -0.000001f } },
  441. { SideLeft, { 0.470934f, -0.346484f, 0.327504f, 0.0f, 0.0f, 0.0f, 0.0f, -0.022188f, -0.041113f } },
  442. { SideRight, { 0.470936f, -0.346480f, -0.327507f, 0.0f, 0.0f, 0.0f, 0.0f, -0.022186f, 0.041114f } },
  443. }, X51RearCfg[5] = {
  444. { FrontLeft, { 0.208954f, 0.199518f, 0.223424f, 0.0f, 0.0f, 0.0f, 0.0f, -0.012543f, 0.144260f } },
  445. { FrontRight, { 0.208950f, 0.199514f, -0.223425f, 0.0f, 0.0f, 0.0f, 0.0f, -0.012544f, -0.144258f } },
  446. { FrontCenter, { 0.109403f, 0.168250f, -0.000002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.100431f, -0.000001f } },
  447. { BackLeft, { 0.470934f, -0.346484f, 0.327504f, 0.0f, 0.0f, 0.0f, 0.0f, -0.022188f, -0.041113f } },
  448. { BackRight, { 0.470936f, -0.346480f, -0.327507f, 0.0f, 0.0f, 0.0f, 0.0f, -0.022186f, 0.041114f } },
  449. }, X61Cfg[6] = {
  450. { FrontLeft, { 0.167065f, 0.200583f, 0.172695f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029855f, 0.186407f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, 0.068910f } },
  451. { FrontRight, { 0.167065f, 0.200583f, -0.172695f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029855f, -0.186407f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, -0.068910f } },
  452. { FrontCenter, { 0.109403f, 0.179490f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.142031f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.072024f, 0.000000f } },
  453. { BackCenter, { 0.353556f, -0.461940f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.165723f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, 0.000000f } },
  454. { SideLeft, { 0.289151f, -0.081301f, 0.401292f, 0.0f, 0.0f, 0.0f, 0.0f, -0.188208f, -0.071420f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.010099f, -0.032897f } },
  455. { SideRight, { 0.289151f, -0.081301f, -0.401292f, 0.0f, 0.0f, 0.0f, 0.0f, -0.188208f, 0.071420f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.010099f, 0.032897f } },
  456. }, X71Cfg[7] = {
  457. { FrontLeft, { 0.167065f, 0.200583f, 0.172695f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029855f, 0.186407f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, 0.068910f } },
  458. { FrontRight, { 0.167065f, 0.200583f, -0.172695f, 0.0f, 0.0f, 0.0f, 0.0f, 0.029855f, -0.186407f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.039241f, -0.068910f } },
  459. { FrontCenter, { 0.109403f, 0.179490f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.142031f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.072024f, 0.000000f } },
  460. { BackLeft, { 0.224752f, -0.295009f, 0.170325f, 0.0f, 0.0f, 0.0f, 0.0f, 0.105349f, -0.182473f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, 0.065799f } },
  461. { BackRight, { 0.224752f, -0.295009f, -0.170325f, 0.0f, 0.0f, 0.0f, 0.0f, 0.105349f, 0.182473f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, -0.065799f } },
  462. { SideLeft, { 0.224739f, 0.000000f, 0.340644f, 0.0f, 0.0f, 0.0f, 0.0f, -0.210697f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, -0.065795f } },
  463. { SideRight, { 0.224739f, 0.000000f, -0.340644f, 0.0f, 0.0f, 0.0f, 0.0f, -0.210697f, 0.000000f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000f, 0.065795f } },
  464. };
  465. static void InitPanning(ALCdevice *device)
  466. {
  467. const ChannelMap *chanmap = NULL;
  468. ALuint coeffcount = 0;
  469. ALfloat ambiscale;
  470. size_t count = 0;
  471. ALuint i, j;
  472. ambiscale = 1.0f;
  473. switch(device->FmtChans)
  474. {
  475. case DevFmtMono:
  476. count = COUNTOF(MonoCfg);
  477. chanmap = MonoCfg;
  478. ambiscale = ZERO_ORDER_SCALE;
  479. coeffcount = 1;
  480. break;
  481. case DevFmtStereo:
  482. count = COUNTOF(StereoCfg);
  483. chanmap = StereoCfg;
  484. ambiscale = FIRST_ORDER_SCALE;
  485. coeffcount = 4;
  486. break;
  487. case DevFmtQuad:
  488. count = COUNTOF(QuadCfg);
  489. chanmap = QuadCfg;
  490. ambiscale = SECOND_ORDER_SCALE;
  491. coeffcount = 9;
  492. break;
  493. case DevFmtX51:
  494. count = COUNTOF(X51SideCfg);
  495. chanmap = X51SideCfg;
  496. ambiscale = SECOND_ORDER_SCALE;
  497. coeffcount = 9;
  498. break;
  499. case DevFmtX51Rear:
  500. count = COUNTOF(X51RearCfg);
  501. chanmap = X51RearCfg;
  502. ambiscale = SECOND_ORDER_SCALE;
  503. coeffcount = 9;
  504. break;
  505. case DevFmtX61:
  506. count = COUNTOF(X61Cfg);
  507. chanmap = X61Cfg;
  508. ambiscale = THIRD_ORDER_SCALE;
  509. coeffcount = 16;
  510. break;
  511. case DevFmtX71:
  512. count = COUNTOF(X71Cfg);
  513. chanmap = X71Cfg;
  514. ambiscale = THIRD_ORDER_SCALE;
  515. coeffcount = 16;
  516. break;
  517. case DevFmtAmbi1:
  518. case DevFmtAmbi2:
  519. case DevFmtAmbi3:
  520. break;
  521. }
  522. if(device->FmtChans >= DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
  523. {
  524. const ALuint *acnmap = (device->AmbiFmt == AmbiFormat_FuMa) ? FuMa2ACN : ACN2ACN;
  525. const ALfloat *n3dscale = (device->AmbiFmt == AmbiFormat_FuMa) ? FuMa2N3DScale :
  526. (device->AmbiFmt == AmbiFormat_ACN_SN3D) ? SN3D2N3DScale :
  527. /*(device->AmbiFmt == AmbiFormat_ACN_N3D) ?*/ UnitScale;
  528. count = (device->FmtChans == DevFmtAmbi3) ? 16 :
  529. (device->FmtChans == DevFmtAmbi2) ? 9 :
  530. (device->FmtChans == DevFmtAmbi1) ? 4 : 1;
  531. for(i = 0;i < count;i++)
  532. {
  533. ALuint acn = acnmap[i];
  534. device->Dry.Ambi.Map[i].Scale = 1.0f/n3dscale[acn];
  535. device->Dry.Ambi.Map[i].Index = acn;
  536. }
  537. device->Dry.CoeffCount = 0;
  538. device->Dry.NumChannels = count;
  539. if(device->FmtChans == DevFmtAmbi1)
  540. {
  541. device->FOAOut.Ambi = device->Dry.Ambi;
  542. device->FOAOut.CoeffCount = device->Dry.CoeffCount;
  543. }
  544. else
  545. {
  546. /* FOA output is always ACN+N3D for higher-order ambisonic output.
  547. * The upsampler expects this and will convert it for output.
  548. */
  549. memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
  550. for(i = 0;i < 4;i++)
  551. {
  552. device->FOAOut.Ambi.Map[i].Scale = 1.0f;
  553. device->FOAOut.Ambi.Map[i].Index = i;
  554. }
  555. device->FOAOut.CoeffCount = 0;
  556. ambiup_reset(device->AmbiUp, device);
  557. }
  558. }
  559. else
  560. {
  561. SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs,
  562. chanmap, count, &device->Dry.NumChannels, AL_TRUE);
  563. device->Dry.CoeffCount = coeffcount;
  564. memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
  565. for(i = 0;i < device->Dry.NumChannels;i++)
  566. {
  567. device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0];
  568. for(j = 1;j < 4;j++)
  569. device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * ambiscale;
  570. }
  571. device->FOAOut.CoeffCount = 4;
  572. }
  573. }
  574. static void InitCustomPanning(ALCdevice *device, const AmbDecConf *conf, const ALuint speakermap[MAX_OUTPUT_CHANNELS])
  575. {
  576. ChannelMap chanmap[MAX_OUTPUT_CHANNELS];
  577. const ALfloat *coeff_scale = UnitScale;
  578. ALfloat ambiscale = 1.0f;
  579. ALuint i, j;
  580. if(conf->FreqBands != 1)
  581. ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
  582. conf->XOverFreq);
  583. if(conf->ChanMask > 0x1ff)
  584. ambiscale = THIRD_ORDER_SCALE;
  585. else if(conf->ChanMask > 0xf)
  586. ambiscale = SECOND_ORDER_SCALE;
  587. else if(conf->ChanMask > 0x1)
  588. ambiscale = FIRST_ORDER_SCALE;
  589. else
  590. ambiscale = 0.0f;
  591. if(conf->CoeffScale == ADS_SN3D)
  592. coeff_scale = SN3D2N3DScale;
  593. else if(conf->CoeffScale == ADS_FuMa)
  594. coeff_scale = FuMa2N3DScale;
  595. for(i = 0;i < conf->NumSpeakers;i++)
  596. {
  597. ALuint chan = speakermap[i];
  598. ALfloat gain;
  599. ALuint k = 0;
  600. for(j = 0;j < MAX_AMBI_COEFFS;j++)
  601. chanmap[i].Config[j] = 0.0f;
  602. chanmap[i].ChanName = device->RealOut.ChannelName[chan];
  603. for(j = 0;j < MAX_AMBI_COEFFS;j++)
  604. {
  605. if(j == 0) gain = conf->HFOrderGain[0];
  606. else if(j == 1) gain = conf->HFOrderGain[1];
  607. else if(j == 4) gain = conf->HFOrderGain[2];
  608. else if(j == 9) gain = conf->HFOrderGain[3];
  609. if((conf->ChanMask&(1<<j)))
  610. chanmap[i].Config[j] = conf->HFMatrix[i][k++] / coeff_scale[j] * gain;
  611. }
  612. }
  613. SetChannelMap(device->RealOut.ChannelName, device->Dry.Ambi.Coeffs, chanmap,
  614. conf->NumSpeakers, &device->Dry.NumChannels, AL_FALSE);
  615. device->Dry.CoeffCount = (conf->ChanMask > 0x1ff) ? 16 :
  616. (conf->ChanMask > 0xf) ? 9 : 4;
  617. memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
  618. for(i = 0;i < device->Dry.NumChannels;i++)
  619. {
  620. device->FOAOut.Ambi.Coeffs[i][0] = device->Dry.Ambi.Coeffs[i][0];
  621. for(j = 1;j < 4;j++)
  622. device->FOAOut.Ambi.Coeffs[i][j] = device->Dry.Ambi.Coeffs[i][j] * ambiscale;
  623. }
  624. device->FOAOut.CoeffCount = 4;
  625. }
  626. static void InitHQPanning(ALCdevice *device, const AmbDecConf *conf, const ALuint speakermap[MAX_OUTPUT_CHANNELS])
  627. {
  628. const char *devname;
  629. int decflags = 0;
  630. size_t count;
  631. ALuint i;
  632. devname = al_string_get_cstr(device->DeviceName);
  633. if(GetConfigValueBool(devname, "decoder", "distance-comp", 1))
  634. decflags |= BFDF_DistanceComp;
  635. if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
  636. {
  637. count = (conf->ChanMask > 0x1ff) ? 16 :
  638. (conf->ChanMask > 0xf) ? 9 : 4;
  639. for(i = 0;i < count;i++)
  640. {
  641. device->Dry.Ambi.Map[i].Scale = 1.0f;
  642. device->Dry.Ambi.Map[i].Index = i;
  643. }
  644. }
  645. else
  646. {
  647. static const int map[MAX_AMBI2D_COEFFS] = { 0, 1, 3, 4, 8, 9, 15 };
  648. count = (conf->ChanMask > 0x1ff) ? 7 :
  649. (conf->ChanMask > 0xf) ? 5 : 3;
  650. for(i = 0;i < count;i++)
  651. {
  652. device->Dry.Ambi.Map[i].Scale = 1.0f;
  653. device->Dry.Ambi.Map[i].Index = map[i];
  654. }
  655. }
  656. device->Dry.CoeffCount = 0;
  657. device->Dry.NumChannels = count;
  658. TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
  659. (conf->FreqBands == 1) ? "single" : "dual",
  660. (conf->ChanMask > 0xf) ? (conf->ChanMask > 0x1ff) ? "third" : "second" : "first",
  661. (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
  662. );
  663. bformatdec_reset(device->AmbiDecoder, conf, count, device->Frequency,
  664. speakermap, decflags);
  665. if(bformatdec_getOrder(device->AmbiDecoder) < 2)
  666. {
  667. device->FOAOut.Ambi = device->Dry.Ambi;
  668. device->FOAOut.CoeffCount = device->Dry.CoeffCount;
  669. }
  670. else
  671. {
  672. memset(&device->FOAOut.Ambi, 0, sizeof(device->FOAOut.Ambi));
  673. for(i = 0;i < 4;i++)
  674. {
  675. device->FOAOut.Ambi.Map[i].Scale = 1.0f;
  676. device->FOAOut.Ambi.Map[i].Index = i;
  677. }
  678. device->FOAOut.CoeffCount = 0;
  679. }
  680. }
  681. static void InitHrtfPanning(ALCdevice *device)
  682. {
  683. size_t count = 4;
  684. ALuint i;
  685. for(i = 0;i < count;i++)
  686. {
  687. device->Dry.Ambi.Map[i].Scale = 1.0f;
  688. device->Dry.Ambi.Map[i].Index = i;
  689. }
  690. device->Dry.CoeffCount = 0;
  691. device->Dry.NumChannels = count;
  692. device->FOAOut.Ambi = device->Dry.Ambi;
  693. device->FOAOut.CoeffCount = device->Dry.CoeffCount;
  694. memset(device->Hrtf.Coeffs, 0, sizeof(device->Hrtf.Coeffs));
  695. device->Hrtf.IrSize = BuildBFormatHrtf(device->Hrtf.Handle,
  696. device->Hrtf.Coeffs, device->Dry.NumChannels
  697. );
  698. /* Round up to the nearest multiple of 8 */
  699. device->Hrtf.IrSize = (device->Hrtf.IrSize+7)&~7;
  700. }
  701. static void InitUhjPanning(ALCdevice *device)
  702. {
  703. size_t count = 3;
  704. ALuint i;
  705. for(i = 0;i < count;i++)
  706. {
  707. ALuint acn = FuMa2ACN[i];
  708. device->Dry.Ambi.Map[i].Scale = 1.0f/FuMa2N3DScale[acn];
  709. device->Dry.Ambi.Map[i].Index = acn;
  710. }
  711. device->Dry.CoeffCount = 0;
  712. device->Dry.NumChannels = count;
  713. device->FOAOut.Ambi = device->Dry.Ambi;
  714. device->FOAOut.CoeffCount = device->Dry.CoeffCount;
  715. }
  716. void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq)
  717. {
  718. const char *mode;
  719. bool headphones;
  720. int bs2blevel;
  721. size_t i;
  722. device->Hrtf.Handle = NULL;
  723. al_string_clear(&device->Hrtf.Name);
  724. device->Render_Mode = NormalRender;
  725. memset(&device->Dry.Ambi, 0, sizeof(device->Dry.Ambi));
  726. device->Dry.CoeffCount = 0;
  727. device->Dry.NumChannels = 0;
  728. if(device->FmtChans != DevFmtStereo)
  729. {
  730. ALuint speakermap[MAX_OUTPUT_CHANNELS];
  731. const char *devname, *layout = NULL;
  732. AmbDecConf conf, *pconf = NULL;
  733. if(hrtf_appreq == Hrtf_Enable)
  734. device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  735. ambdec_init(&conf);
  736. devname = al_string_get_cstr(device->DeviceName);
  737. switch(device->FmtChans)
  738. {
  739. case DevFmtQuad: layout = "quad"; break;
  740. case DevFmtX51: layout = "surround51"; break;
  741. case DevFmtX51Rear: layout = "surround51rear"; break;
  742. case DevFmtX61: layout = "surround61"; break;
  743. case DevFmtX71: layout = "surround71"; break;
  744. /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
  745. case DevFmtMono:
  746. case DevFmtStereo:
  747. case DevFmtAmbi1:
  748. case DevFmtAmbi2:
  749. case DevFmtAmbi3:
  750. break;
  751. }
  752. if(layout)
  753. {
  754. const char *fname;
  755. if(ConfigValueStr(devname, "decoder", layout, &fname))
  756. {
  757. if(!ambdec_load(&conf, fname))
  758. ERR("Failed to load layout file %s\n", fname);
  759. else
  760. {
  761. if(conf.ChanMask > 0xffff)
  762. ERR("Unsupported channel mask 0x%04x (max 0xffff)\n", conf.ChanMask);
  763. else
  764. {
  765. if(MakeSpeakerMap(device, &conf, speakermap))
  766. pconf = &conf;
  767. }
  768. }
  769. }
  770. }
  771. if(pconf && GetConfigValueBool(devname, "decoder", "hq-mode", 0))
  772. {
  773. ambiup_free(device->AmbiUp);
  774. device->AmbiUp = NULL;
  775. if(!device->AmbiDecoder)
  776. device->AmbiDecoder = bformatdec_alloc();
  777. }
  778. else
  779. {
  780. bformatdec_free(device->AmbiDecoder);
  781. device->AmbiDecoder = NULL;
  782. if(device->FmtChans > DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
  783. {
  784. if(!device->AmbiUp)
  785. device->AmbiUp = ambiup_alloc();
  786. }
  787. else
  788. {
  789. ambiup_free(device->AmbiUp);
  790. device->AmbiUp = NULL;
  791. }
  792. }
  793. if(!pconf)
  794. InitPanning(device);
  795. else if(device->AmbiDecoder)
  796. InitHQPanning(device, pconf, speakermap);
  797. else
  798. InitCustomPanning(device, pconf, speakermap);
  799. ambdec_deinit(&conf);
  800. return;
  801. }
  802. ambiup_free(device->AmbiUp);
  803. device->AmbiUp = NULL;
  804. bformatdec_free(device->AmbiDecoder);
  805. device->AmbiDecoder = NULL;
  806. headphones = device->IsHeadphones;
  807. if(device->Type != Loopback)
  808. {
  809. const char *mode;
  810. if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "stereo-mode", &mode))
  811. {
  812. if(strcasecmp(mode, "headphones") == 0)
  813. headphones = true;
  814. else if(strcasecmp(mode, "speakers") == 0)
  815. headphones = false;
  816. else if(strcasecmp(mode, "auto") != 0)
  817. ERR("Unexpected stereo-mode: %s\n", mode);
  818. }
  819. }
  820. if(hrtf_userreq == Hrtf_Default)
  821. {
  822. bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
  823. (hrtf_appreq == Hrtf_Enable);
  824. if(!usehrtf) goto no_hrtf;
  825. device->Hrtf.Status = ALC_HRTF_ENABLED_SOFT;
  826. if(headphones && hrtf_appreq != Hrtf_Disable)
  827. device->Hrtf.Status = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
  828. }
  829. else
  830. {
  831. if(hrtf_userreq != Hrtf_Enable)
  832. {
  833. if(hrtf_appreq == Hrtf_Enable)
  834. device->Hrtf.Status = ALC_HRTF_DENIED_SOFT;
  835. goto no_hrtf;
  836. }
  837. device->Hrtf.Status = ALC_HRTF_REQUIRED_SOFT;
  838. }
  839. if(VECTOR_SIZE(device->Hrtf.List) == 0)
  840. {
  841. VECTOR_DEINIT(device->Hrtf.List);
  842. device->Hrtf.List = EnumerateHrtf(device->DeviceName);
  843. }
  844. if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->Hrtf.List))
  845. {
  846. const HrtfEntry *entry = &VECTOR_ELEM(device->Hrtf.List, hrtf_id);
  847. if(entry->hrtf->sampleRate == device->Frequency)
  848. {
  849. device->Hrtf.Handle = entry->hrtf;
  850. al_string_copy(&device->Hrtf.Name, entry->name);
  851. }
  852. }
  853. for(i = 0;!device->Hrtf.Handle && i < VECTOR_SIZE(device->Hrtf.List);i++)
  854. {
  855. const HrtfEntry *entry = &VECTOR_ELEM(device->Hrtf.List, i);
  856. if(entry->hrtf->sampleRate == device->Frequency)
  857. {
  858. device->Hrtf.Handle = entry->hrtf;
  859. al_string_copy(&device->Hrtf.Name, entry->name);
  860. }
  861. }
  862. if(device->Hrtf.Handle)
  863. {
  864. device->Render_Mode = HrtfRender;
  865. if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "hrtf-mode", &mode))
  866. {
  867. if(strcasecmp(mode, "full") == 0)
  868. device->Render_Mode = HrtfRender;
  869. else if(strcasecmp(mode, "basic") == 0)
  870. device->Render_Mode = NormalRender;
  871. else
  872. ERR("Unexpected hrtf-mode: %s\n", mode);
  873. }
  874. TRACE("HRTF enabled, \"%s\"\n", al_string_get_cstr(device->Hrtf.Name));
  875. InitHrtfPanning(device);
  876. return;
  877. }
  878. device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  879. no_hrtf:
  880. TRACE("HRTF disabled\n");
  881. bs2blevel = ((headphones && hrtf_appreq != Hrtf_Disable) ||
  882. (hrtf_appreq == Hrtf_Enable)) ? 5 : 0;
  883. if(device->Type != Loopback)
  884. ConfigValueInt(al_string_get_cstr(device->DeviceName), NULL, "cf_level", &bs2blevel);
  885. if(bs2blevel > 0 && bs2blevel <= 6)
  886. {
  887. device->Bs2b = al_calloc(16, sizeof(*device->Bs2b));
  888. bs2b_set_params(device->Bs2b, bs2blevel, device->Frequency);
  889. device->Render_Mode = StereoPair;
  890. TRACE("BS2B enabled\n");
  891. InitPanning(device);
  892. return;
  893. }
  894. TRACE("BS2B disabled\n");
  895. device->Render_Mode = NormalRender;
  896. if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "stereo-panning", &mode))
  897. {
  898. if(strcasecmp(mode, "paired") == 0)
  899. device->Render_Mode = StereoPair;
  900. else if(strcasecmp(mode, "uhj") != 0)
  901. ERR("Unexpected stereo-panning: %s\n", mode);
  902. }
  903. if(device->Render_Mode == NormalRender)
  904. {
  905. device->Uhj_Encoder = al_calloc(16, sizeof(Uhj2Encoder));
  906. TRACE("UHJ enabled\n");
  907. InitUhjPanning(device);
  908. return;
  909. }
  910. TRACE("UHJ disabled\n");
  911. InitPanning(device);
  912. }
  913. void aluInitEffectPanning(ALeffectslot *slot)
  914. {
  915. ALuint i;
  916. memset(slot->ChanMap, 0, sizeof(slot->ChanMap));
  917. slot->NumChannels = 0;
  918. for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
  919. {
  920. slot->ChanMap[i].Scale = 1.0f;
  921. slot->ChanMap[i].Index = i;
  922. }
  923. slot->NumChannels = i;
  924. }