🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

981 lines
42 KiB

  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 <cmath>
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <cctype>
  25. #include <cassert>
  26. #include <cmath>
  27. #include <chrono>
  28. #include <numeric>
  29. #include <algorithm>
  30. #include <functional>
  31. #include "alMain.h"
  32. #include "alAuxEffectSlot.h"
  33. #include "alu.h"
  34. #include "alconfig.h"
  35. #include "ambdec.h"
  36. #include "bformatdec.h"
  37. #include "filters/splitter.h"
  38. #include "uhjfilter.h"
  39. #include "bs2b.h"
  40. #include "alspan.h"
  41. constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromN3D;
  42. constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromSN3D;
  43. constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromFuMa;
  44. constexpr std::array<int,MAX_AMBI_CHANNELS> AmbiIndex::FromFuMa;
  45. constexpr std::array<int,MAX_AMBI_CHANNELS> AmbiIndex::FromACN;
  46. constexpr std::array<int,MAX_AMBI2D_CHANNELS> AmbiIndex::From2D;
  47. constexpr std::array<int,MAX_AMBI_CHANNELS> AmbiIndex::From3D;
  48. namespace {
  49. using namespace std::placeholders;
  50. using std::chrono::seconds;
  51. using std::chrono::nanoseconds;
  52. inline const char *GetLabelFromChannel(Channel channel)
  53. {
  54. switch(channel)
  55. {
  56. case FrontLeft: return "front-left";
  57. case FrontRight: return "front-right";
  58. case FrontCenter: return "front-center";
  59. case LFE: return "lfe";
  60. case BackLeft: return "back-left";
  61. case BackRight: return "back-right";
  62. case BackCenter: return "back-center";
  63. case SideLeft: return "side-left";
  64. case SideRight: return "side-right";
  65. case UpperFrontLeft: return "upper-front-left";
  66. case UpperFrontRight: return "upper-front-right";
  67. case UpperBackLeft: return "upper-back-left";
  68. case UpperBackRight: return "upper-back-right";
  69. case LowerFrontLeft: return "lower-front-left";
  70. case LowerFrontRight: return "lower-front-right";
  71. case LowerBackLeft: return "lower-back-left";
  72. case LowerBackRight: return "lower-back-right";
  73. case Aux0: return "aux-0";
  74. case Aux1: return "aux-1";
  75. case Aux2: return "aux-2";
  76. case Aux3: return "aux-3";
  77. case Aux4: return "aux-4";
  78. case Aux5: return "aux-5";
  79. case Aux6: return "aux-6";
  80. case Aux7: return "aux-7";
  81. case Aux8: return "aux-8";
  82. case Aux9: return "aux-9";
  83. case Aux10: return "aux-10";
  84. case Aux11: return "aux-11";
  85. case Aux12: return "aux-12";
  86. case Aux13: return "aux-13";
  87. case Aux14: return "aux-14";
  88. case Aux15: return "aux-15";
  89. case MaxChannels: break;
  90. }
  91. return "(unknown)";
  92. }
  93. struct ChannelMap {
  94. Channel ChanName;
  95. ALfloat Config[MAX_AMBI2D_CHANNELS];
  96. };
  97. bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALsizei (&speakermap)[MAX_OUTPUT_CHANNELS])
  98. {
  99. auto map_spkr = [device](const AmbDecConf::SpeakerConf &speaker) -> ALsizei
  100. {
  101. /* NOTE: AmbDec does not define any standard speaker names, however
  102. * for this to work we have to by able to find the output channel
  103. * the speaker definition corresponds to. Therefore, OpenAL Soft
  104. * requires these channel labels to be recognized:
  105. *
  106. * LF = Front left
  107. * RF = Front right
  108. * LS = Side left
  109. * RS = Side right
  110. * LB = Back left
  111. * RB = Back right
  112. * CE = Front center
  113. * CB = Back center
  114. *
  115. * Additionally, surround51 will acknowledge back speakers for side
  116. * channels, and surround51rear will acknowledge side speakers for
  117. * back channels, to avoid issues with an ambdec expecting 5.1 to
  118. * use the side channels when the device is configured for back,
  119. * and vice-versa.
  120. */
  121. Channel ch{};
  122. if(speaker.Name == "LF")
  123. ch = FrontLeft;
  124. else if(speaker.Name == "RF")
  125. ch = FrontRight;
  126. else if(speaker.Name == "CE")
  127. ch = FrontCenter;
  128. else if(speaker.Name == "LS")
  129. {
  130. if(device->FmtChans == DevFmtX51Rear)
  131. ch = BackLeft;
  132. else
  133. ch = SideLeft;
  134. }
  135. else if(speaker.Name == "RS")
  136. {
  137. if(device->FmtChans == DevFmtX51Rear)
  138. ch = BackRight;
  139. else
  140. ch = SideRight;
  141. }
  142. else if(speaker.Name == "LB")
  143. {
  144. if(device->FmtChans == DevFmtX51)
  145. ch = SideLeft;
  146. else
  147. ch = BackLeft;
  148. }
  149. else if(speaker.Name == "RB")
  150. {
  151. if(device->FmtChans == DevFmtX51)
  152. ch = SideRight;
  153. else
  154. ch = BackRight;
  155. }
  156. else if(speaker.Name == "CB")
  157. ch = BackCenter;
  158. else
  159. {
  160. const char *name{speaker.Name.c_str()};
  161. unsigned int n;
  162. char c;
  163. if(sscanf(name, "AUX%u%c", &n, &c) == 1 && n < 16)
  164. ch = static_cast<Channel>(Aux0+n);
  165. else
  166. {
  167. ERR("AmbDec speaker label \"%s\" not recognized\n", name);
  168. return -1;
  169. }
  170. }
  171. const int chidx{GetChannelIdxByName(device->RealOut, ch)};
  172. if(chidx == -1)
  173. ERR("Failed to lookup AmbDec speaker label %s\n", speaker.Name.c_str());
  174. return chidx;
  175. };
  176. std::transform(conf->Speakers.begin(), conf->Speakers.end(), std::begin(speakermap), map_spkr);
  177. /* Return success if no invalid entries are found. */
  178. auto speakermap_end = std::begin(speakermap) + conf->Speakers.size();
  179. return std::find(std::begin(speakermap), speakermap_end, -1) == speakermap_end;
  180. }
  181. constexpr ChannelMap MonoCfg[1] = {
  182. { FrontCenter, { 1.0f } },
  183. }, StereoCfg[2] = {
  184. { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f } },
  185. { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f } },
  186. }, QuadCfg[4] = {
  187. { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, -2.04124145e-1f } },
  188. { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 2.04124145e-1f } },
  189. { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 2.04124145e-1f } },
  190. { BackRight, { 3.53553391e-1f, -2.04124145e-1f, -2.04124145e-1f } },
  191. }, X51SideCfg[4] = {
  192. { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, -2.00042375e-1f, -2.12307769e-2f, -1.14579885e-2f } },
  193. { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 1.66295695e-1f, 7.30571517e-2f, 2.10901184e-2f } },
  194. { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 1.66295695e-1f, -7.30571517e-2f, 2.10901184e-2f } },
  195. { SideRight, { 3.33000782e-1f, -1.89084803e-1f, -2.00042375e-1f, 2.12307769e-2f, -1.14579885e-2f } },
  196. }, X51RearCfg[4] = {
  197. { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, -2.00042375e-1f, -2.12307769e-2f, -1.14579885e-2f } },
  198. { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 1.66295695e-1f, 7.30571517e-2f, 2.10901184e-2f } },
  199. { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 1.66295695e-1f, -7.30571517e-2f, 2.10901184e-2f } },
  200. { BackRight, { 3.33000782e-1f, -1.89084803e-1f, -2.00042375e-1f, 2.12307769e-2f, -1.14579885e-2f } },
  201. }, X61Cfg[6] = {
  202. { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f } },
  203. { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f } },
  204. { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f } },
  205. { SideRight, { 2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f } },
  206. { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f } },
  207. }, X71Cfg[6] = {
  208. { BackLeft, { 2.04124145e-1f, 1.08880247e-1f, -1.88586120e-1f, -1.29099444e-1f, 7.45355993e-2f, 3.73460789e-2f, 0.00000000e+0f } },
  209. { SideLeft, { 2.04124145e-1f, 2.17760495e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.49071198e-1f, -3.73460789e-2f, 0.00000000e+0f } },
  210. { FrontLeft, { 2.04124145e-1f, 1.08880247e-1f, 1.88586120e-1f, 1.29099444e-1f, 7.45355993e-2f, 3.73460789e-2f, 0.00000000e+0f } },
  211. { FrontRight, { 2.04124145e-1f, -1.08880247e-1f, 1.88586120e-1f, -1.29099444e-1f, 7.45355993e-2f, -3.73460789e-2f, 0.00000000e+0f } },
  212. { SideRight, { 2.04124145e-1f, -2.17760495e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.49071198e-1f, 3.73460789e-2f, 0.00000000e+0f } },
  213. { BackRight, { 2.04124145e-1f, -1.08880247e-1f, -1.88586120e-1f, 1.29099444e-1f, 7.45355993e-2f, -3.73460789e-2f, 0.00000000e+0f } },
  214. };
  215. void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALsizei order, const ALsizei *RESTRICT chans_per_order)
  216. {
  217. /* NFC is only used when AvgSpeakerDist is greater than 0. */
  218. const char *devname{device->DeviceName.c_str()};
  219. if(!GetConfigValueBool(devname, "decoder", "nfc", 0) || !(ctrl_dist > 0.0f))
  220. return;
  221. device->AvgSpeakerDist = minf(ctrl_dist, 10.0f);
  222. TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
  223. auto iter = std::copy(chans_per_order, chans_per_order+order+1,
  224. std::begin(device->NumChannelsPerOrder));
  225. std::fill(iter, std::end(device->NumChannelsPerOrder), 0);
  226. }
  227. void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf, const ALsizei (&speakermap)[MAX_OUTPUT_CHANNELS])
  228. {
  229. const ALfloat maxdist{
  230. std::accumulate(conf->Speakers.begin(), conf->Speakers.end(), float{0.0f},
  231. std::bind(maxf, _1, std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance), _2))
  232. )
  233. };
  234. const char *devname{device->DeviceName.c_str()};
  235. if(!GetConfigValueBool(devname, "decoder", "distance-comp", 1) || !(maxdist > 0.0f))
  236. return;
  237. auto srate = static_cast<ALfloat>(device->Frequency);
  238. size_t total{0u};
  239. for(size_t i{0u};i < conf->Speakers.size();i++)
  240. {
  241. const AmbDecConf::SpeakerConf &speaker = conf->Speakers[i];
  242. const ALsizei chan{speakermap[i]};
  243. /* Distance compensation only delays in steps of the sample rate. This
  244. * is a bit less accurate since the delay time falls to the nearest
  245. * sample time, but it's far simpler as it doesn't have to deal with
  246. * phase offsets. This means at 48khz, for instance, the distance delay
  247. * will be in steps of about 7 millimeters.
  248. */
  249. const ALfloat delay{
  250. std::floor((maxdist - speaker.Distance)/SPEEDOFSOUNDMETRESPERSEC*srate + 0.5f)
  251. };
  252. if(delay >= static_cast<ALfloat>(MAX_DELAY_LENGTH))
  253. ERR("Delay for speaker \"%s\" exceeds buffer length (%f >= %d)\n",
  254. speaker.Name.c_str(), delay, MAX_DELAY_LENGTH);
  255. device->ChannelDelay[chan].Length = static_cast<ALsizei>(clampf(
  256. delay, 0.0f, static_cast<ALfloat>(MAX_DELAY_LENGTH-1)
  257. ));
  258. device->ChannelDelay[chan].Gain = speaker.Distance / maxdist;
  259. TRACE("Channel %u \"%s\" distance compensation: %d samples, %f gain\n", chan,
  260. speaker.Name.c_str(), device->ChannelDelay[chan].Length,
  261. device->ChannelDelay[chan].Gain
  262. );
  263. /* Round up to the next 4th sample, so each channel buffer starts
  264. * 16-byte aligned.
  265. */
  266. total += RoundUp(device->ChannelDelay[chan].Length, 4);
  267. }
  268. if(total > 0)
  269. {
  270. device->ChannelDelay.resize(total);
  271. device->ChannelDelay[0].Buffer = device->ChannelDelay.data();
  272. auto set_bufptr = [](const DistanceComp::DistData &last, const DistanceComp::DistData &cur) -> DistanceComp::DistData
  273. {
  274. DistanceComp::DistData ret{cur};
  275. ret.Buffer = last.Buffer + RoundUp(last.Length, 4);
  276. return ret;
  277. };
  278. std::partial_sum(device->ChannelDelay.begin(), device->ChannelDelay.end(),
  279. device->ChannelDelay.begin(), set_bufptr);
  280. }
  281. }
  282. auto GetAmbiScales(AmbiNorm scaletype) noexcept -> const std::array<float,MAX_AMBI_CHANNELS>&
  283. {
  284. if(scaletype == AmbiNorm::FuMa) return AmbiScale::FromFuMa;
  285. if(scaletype == AmbiNorm::SN3D) return AmbiScale::FromSN3D;
  286. return AmbiScale::FromN3D;
  287. }
  288. auto GetAmbiLayout(AmbiLayout layouttype) noexcept -> const std::array<int,MAX_AMBI_CHANNELS>&
  289. {
  290. if(layouttype == AmbiLayout::FuMa) return AmbiIndex::FromFuMa;
  291. return AmbiIndex::FromACN;
  292. }
  293. void InitPanning(ALCdevice *device)
  294. {
  295. al::span<const ChannelMap> chanmap;
  296. ALsizei coeffcount{};
  297. switch(device->FmtChans)
  298. {
  299. case DevFmtMono:
  300. chanmap = MonoCfg;
  301. coeffcount = 1;
  302. break;
  303. case DevFmtStereo:
  304. chanmap = StereoCfg;
  305. coeffcount = 3;
  306. break;
  307. case DevFmtQuad:
  308. chanmap = QuadCfg;
  309. coeffcount = 3;
  310. break;
  311. case DevFmtX51:
  312. chanmap = X51SideCfg;
  313. coeffcount = 5;
  314. break;
  315. case DevFmtX51Rear:
  316. chanmap = X51RearCfg;
  317. coeffcount = 5;
  318. break;
  319. case DevFmtX61:
  320. chanmap = X61Cfg;
  321. coeffcount = 5;
  322. break;
  323. case DevFmtX71:
  324. chanmap = X71Cfg;
  325. coeffcount = 7;
  326. break;
  327. case DevFmtAmbi3D:
  328. break;
  329. }
  330. if(device->FmtChans == DevFmtAmbi3D)
  331. {
  332. const char *devname{device->DeviceName.c_str()};
  333. const std::array<int,MAX_AMBI_CHANNELS> &acnmap = GetAmbiLayout(device->mAmbiLayout);
  334. const std::array<float,MAX_AMBI_CHANNELS> &n3dscale = GetAmbiScales(device->mAmbiScale);
  335. /* For DevFmtAmbi3D, the ambisonic order is already set. */
  336. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  337. std::transform(acnmap.begin(), acnmap.begin()+count, std::begin(device->Dry.AmbiMap),
  338. [&n3dscale](const ALsizei &acn) noexcept -> BFChannelConfig
  339. { return BFChannelConfig{1.0f/n3dscale[acn], acn}; }
  340. );
  341. device->Dry.NumChannels = static_cast<ALsizei>(count);
  342. ALfloat nfc_delay{0.0f};
  343. if(ConfigValueFloat(devname, "decoder", "nfc-ref-delay", &nfc_delay) && nfc_delay > 0.0f)
  344. {
  345. static constexpr ALsizei chans_per_order[MAX_AMBI_ORDER+1]{ 1, 3, 5, 7 };
  346. nfc_delay = clampf(nfc_delay, 0.001f, 1000.0f);
  347. InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC,
  348. device->mAmbiOrder, chans_per_order);
  349. }
  350. device->RealOut.NumChannels = 0;
  351. }
  352. else
  353. {
  354. ChannelDec chancoeffs[MAX_OUTPUT_CHANNELS]{};
  355. ALsizei idxmap[MAX_OUTPUT_CHANNELS]{};
  356. for(size_t i{0u};i < chanmap.size();++i)
  357. {
  358. const ALint idx{GetChannelIdxByName(device->RealOut, chanmap[i].ChanName)};
  359. if(idx < 0)
  360. {
  361. ERR("Failed to find %s channel in device\n",
  362. GetLabelFromChannel(chanmap[i].ChanName));
  363. continue;
  364. }
  365. idxmap[i] = idx;
  366. std::copy_n(chanmap[i].Config, coeffcount, chancoeffs[i]);
  367. }
  368. /* For non-DevFmtAmbi3D, set the ambisonic order given the mixing
  369. * channel count. Built-in speaker decoders are always 2D, so just
  370. * reverse that calculation.
  371. */
  372. device->mAmbiOrder = (coeffcount-1) / 2;
  373. std::transform(AmbiIndex::From2D.begin(), AmbiIndex::From2D.begin()+coeffcount,
  374. std::begin(device->Dry.AmbiMap),
  375. [](const ALsizei &index) noexcept { return BFChannelConfig{1.0f, index}; }
  376. );
  377. device->Dry.NumChannels = coeffcount;
  378. TRACE("Enabling %s-order%s ambisonic decoder\n",
  379. (coeffcount > 5) ? "third" :
  380. (coeffcount > 3) ? "second" : "first",
  381. ""
  382. );
  383. device->AmbiDecoder = al::make_unique<BFormatDec>(coeffcount,
  384. static_cast<ALsizei>(chanmap.size()), chancoeffs, idxmap);
  385. device->RealOut.NumChannels = device->channelsFromFmt();
  386. }
  387. }
  388. void InitCustomPanning(ALCdevice *device, bool hqdec, const AmbDecConf *conf, const ALsizei (&speakermap)[MAX_OUTPUT_CHANNELS])
  389. {
  390. static constexpr ALsizei chans_per_order2d[MAX_AMBI_ORDER+1] = { 1, 2, 2, 2 };
  391. static constexpr ALsizei chans_per_order3d[MAX_AMBI_ORDER+1] = { 1, 3, 5, 7 };
  392. if(!hqdec && conf->FreqBands != 1)
  393. ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
  394. conf->XOverFreq);
  395. ALsizei order{(conf->ChanMask > AMBI_2ORDER_MASK) ? 3 :
  396. (conf->ChanMask > AMBI_1ORDER_MASK) ? 2 : 1};
  397. device->mAmbiOrder = order;
  398. ALsizei count;
  399. if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
  400. {
  401. count = static_cast<ALsizei>(AmbiChannelsFromOrder(order));
  402. std::transform(AmbiIndex::From3D.begin(), AmbiIndex::From3D.begin()+count,
  403. std::begin(device->Dry.AmbiMap),
  404. [](const ALsizei &index) noexcept { return BFChannelConfig{1.0f, index}; }
  405. );
  406. }
  407. else
  408. {
  409. count = static_cast<ALsizei>(Ambi2DChannelsFromOrder(order));
  410. std::transform(AmbiIndex::From2D.begin(), AmbiIndex::From2D.begin()+count,
  411. std::begin(device->Dry.AmbiMap),
  412. [](const ALsizei &index) noexcept { return BFChannelConfig{1.0f, index}; }
  413. );
  414. }
  415. device->Dry.NumChannels = count;
  416. TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
  417. (!hqdec || conf->FreqBands == 1) ? "single" : "dual",
  418. (conf->ChanMask > AMBI_2ORDER_MASK) ? "third" :
  419. (conf->ChanMask > AMBI_1ORDER_MASK) ? "second" : "first",
  420. (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
  421. );
  422. device->AmbiDecoder = al::make_unique<BFormatDec>(conf, hqdec, count, device->Frequency,
  423. speakermap);
  424. device->RealOut.NumChannels = device->channelsFromFmt();
  425. auto accum_spkr_dist = std::bind(std::plus<float>{}, _1,
  426. std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance), _2));
  427. const ALfloat avg_dist{
  428. std::accumulate(conf->Speakers.begin(), conf->Speakers.end(), float{0.0f},
  429. accum_spkr_dist) / static_cast<ALfloat>(conf->Speakers.size())
  430. };
  431. InitNearFieldCtrl(device, avg_dist, order,
  432. (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? chans_per_order3d : chans_per_order2d);
  433. InitDistanceComp(device, conf, speakermap);
  434. }
  435. void InitHrtfPanning(ALCdevice *device)
  436. {
  437. /* NOTE: In degrees, and azimuth goes clockwise. */
  438. static constexpr AngularPoint AmbiPoints[]{
  439. { 35.264390f, -45.000000f },
  440. { 35.264390f, 45.000000f },
  441. { 35.264390f, 135.000000f },
  442. { 35.264390f, -135.000000f },
  443. { -35.264390f, -45.000000f },
  444. { -35.264390f, 45.000000f },
  445. { -35.264390f, 135.000000f },
  446. { -35.264390f, -135.000000f },
  447. { 0.000000f, -20.905157f },
  448. { 0.000000f, 20.905157f },
  449. { 0.000000f, 159.094843f },
  450. { 0.000000f, -159.094843f },
  451. { 20.905157f, -90.000000f },
  452. { -20.905157f, -90.000000f },
  453. { -20.905157f, 90.000000f },
  454. { 20.905157f, 90.000000f },
  455. { 69.094843f, 0.000000f },
  456. { -69.094843f, 0.000000f },
  457. { -69.094843f, 180.000000f },
  458. { 69.094843f, 180.000000f },
  459. };
  460. static constexpr ALfloat AmbiMatrix[][MAX_AMBI_CHANNELS]{
  461. { 5.00000000e-02f, 5.00000000e-02f, 5.00000000e-02f, 5.00000000e-02f, 6.45497224e-02f, 6.45497224e-02f, 0.00000000e+00f, 6.45497224e-02f, 0.00000000e+00f, 1.48264644e-02f, 6.33865691e-02f, 1.01126676e-01f, -7.36485380e-02f, -1.09260065e-02f, 7.08683387e-02f, -1.01622099e-01f },
  462. { 5.00000000e-02f, -5.00000000e-02f, 5.00000000e-02f, 5.00000000e-02f, -6.45497224e-02f, -6.45497224e-02f, 0.00000000e+00f, 6.45497224e-02f, 0.00000000e+00f, -1.48264644e-02f, -6.33865691e-02f, -1.01126676e-01f, -7.36485380e-02f, -1.09260065e-02f, 7.08683387e-02f, -1.01622099e-01f },
  463. { 5.00000000e-02f, -5.00000000e-02f, 5.00000000e-02f, -5.00000000e-02f, 6.45497224e-02f, -6.45497224e-02f, 0.00000000e+00f, -6.45497224e-02f, 0.00000000e+00f, -1.48264644e-02f, 6.33865691e-02f, -1.01126676e-01f, -7.36485380e-02f, 1.09260065e-02f, 7.08683387e-02f, 1.01622099e-01f },
  464. { 5.00000000e-02f, 5.00000000e-02f, 5.00000000e-02f, -5.00000000e-02f, -6.45497224e-02f, 6.45497224e-02f, 0.00000000e+00f, -6.45497224e-02f, 0.00000000e+00f, 1.48264644e-02f, -6.33865691e-02f, 1.01126676e-01f, -7.36485380e-02f, 1.09260065e-02f, 7.08683387e-02f, 1.01622099e-01f },
  465. { 5.00000000e-02f, 5.00000000e-02f, -5.00000000e-02f, 5.00000000e-02f, 6.45497224e-02f, -6.45497224e-02f, 0.00000000e+00f, -6.45497224e-02f, 0.00000000e+00f, 1.48264644e-02f, -6.33865691e-02f, 1.01126676e-01f, 7.36485380e-02f, -1.09260065e-02f, -7.08683387e-02f, -1.01622099e-01f },
  466. { 5.00000000e-02f, -5.00000000e-02f, -5.00000000e-02f, 5.00000000e-02f, -6.45497224e-02f, 6.45497224e-02f, 0.00000000e+00f, -6.45497224e-02f, 0.00000000e+00f, -1.48264644e-02f, 6.33865691e-02f, -1.01126676e-01f, 7.36485380e-02f, -1.09260065e-02f, -7.08683387e-02f, -1.01622099e-01f },
  467. { 5.00000000e-02f, -5.00000000e-02f, -5.00000000e-02f, -5.00000000e-02f, 6.45497224e-02f, 6.45497224e-02f, 0.00000000e+00f, 6.45497224e-02f, 0.00000000e+00f, -1.48264644e-02f, -6.33865691e-02f, -1.01126676e-01f, 7.36485380e-02f, 1.09260065e-02f, -7.08683387e-02f, 1.01622099e-01f },
  468. { 5.00000000e-02f, 5.00000000e-02f, -5.00000000e-02f, -5.00000000e-02f, -6.45497224e-02f, -6.45497224e-02f, 0.00000000e+00f, 6.45497224e-02f, 0.00000000e+00f, 1.48264644e-02f, 6.33865691e-02f, 1.01126676e-01f, 7.36485380e-02f, 1.09260065e-02f, -7.08683387e-02f, 1.01622099e-01f },
  469. { 5.00000000e-02f, 3.09016994e-02f, 0.00000000e+00f, 8.09016994e-02f, 6.45497224e-02f, 0.00000000e+00f, -5.59016994e-02f, 0.00000000e+00f, 7.21687836e-02f, 7.76323754e-02f, 0.00000000e+00f, -1.49775925e-01f, 0.00000000e+00f, -2.95083663e-02f, 0.00000000e+00f, 7.76323754e-02f },
  470. { 5.00000000e-02f, -3.09016994e-02f, 0.00000000e+00f, 8.09016994e-02f, -6.45497224e-02f, 0.00000000e+00f, -5.59016994e-02f, 0.00000000e+00f, 7.21687836e-02f, -7.76323754e-02f, 0.00000000e+00f, 1.49775925e-01f, 0.00000000e+00f, -2.95083663e-02f, 0.00000000e+00f, 7.76323754e-02f },
  471. { 5.00000000e-02f, -3.09016994e-02f, 0.00000000e+00f, -8.09016994e-02f, 6.45497224e-02f, 0.00000000e+00f, -5.59016994e-02f, 0.00000000e+00f, 7.21687836e-02f, -7.76323754e-02f, 0.00000000e+00f, 1.49775925e-01f, 0.00000000e+00f, 2.95083663e-02f, 0.00000000e+00f, -7.76323754e-02f },
  472. { 5.00000000e-02f, 3.09016994e-02f, 0.00000000e+00f, -8.09016994e-02f, -6.45497224e-02f, 0.00000000e+00f, -5.59016994e-02f, 0.00000000e+00f, 7.21687836e-02f, 7.76323754e-02f, 0.00000000e+00f, -1.49775925e-01f, 0.00000000e+00f, 2.95083663e-02f, 0.00000000e+00f, -7.76323754e-02f },
  473. { 5.00000000e-02f, 8.09016994e-02f, 3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, 6.45497224e-02f, -3.45491503e-02f, 0.00000000e+00f, -8.44966837e-02f, -4.79794466e-02f, 0.00000000e+00f, -6.77901327e-02f, 3.03448665e-02f, 0.00000000e+00f, -1.65948192e-01f, 0.00000000e+00f },
  474. { 5.00000000e-02f, 8.09016994e-02f, -3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, -6.45497224e-02f, -3.45491503e-02f, 0.00000000e+00f, -8.44966837e-02f, -4.79794466e-02f, 0.00000000e+00f, -6.77901327e-02f, -3.03448665e-02f, 0.00000000e+00f, 1.65948192e-01f, 0.00000000e+00f },
  475. { 5.00000000e-02f, -8.09016994e-02f, -3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, 6.45497224e-02f, -3.45491503e-02f, 0.00000000e+00f, -8.44966837e-02f, 4.79794466e-02f, 0.00000000e+00f, 6.77901327e-02f, -3.03448665e-02f, 0.00000000e+00f, 1.65948192e-01f, 0.00000000e+00f },
  476. { 5.00000000e-02f, -8.09016994e-02f, 3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, -6.45497224e-02f, -3.45491503e-02f, 0.00000000e+00f, -8.44966837e-02f, 4.79794466e-02f, 0.00000000e+00f, 6.77901327e-02f, 3.03448665e-02f, 0.00000000e+00f, -1.65948192e-01f, 0.00000000e+00f },
  477. { 5.00000000e-02f, 0.00000000e+00f, 8.09016994e-02f, 3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, 9.04508497e-02f, 6.45497224e-02f, 1.23279000e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 7.94438918e-02f, 1.12611206e-01f, -2.42115150e-02f, 1.25611822e-01f },
  478. { 5.00000000e-02f, 0.00000000e+00f, -8.09016994e-02f, 3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, 9.04508497e-02f, -6.45497224e-02f, 1.23279000e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -7.94438918e-02f, 1.12611206e-01f, 2.42115150e-02f, 1.25611822e-01f },
  479. { 5.00000000e-02f, 0.00000000e+00f, -8.09016994e-02f, -3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, 9.04508497e-02f, 6.45497224e-02f, 1.23279000e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -7.94438918e-02f, -1.12611206e-01f, 2.42115150e-02f, -1.25611822e-01f },
  480. { 5.00000000e-02f, 0.00000000e+00f, 8.09016994e-02f, -3.09016994e-02f, 0.00000000e+00f, 0.00000000e+00f, 9.04508497e-02f, -6.45497224e-02f, 1.23279000e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 7.94438918e-02f, -1.12611206e-01f, -2.42115150e-02f, -1.25611822e-01f }
  481. };
  482. static constexpr ALfloat AmbiOrderHFGainFOA[MAX_AMBI_ORDER+1]{
  483. 3.16227766e+00f, 1.82574186e+00f
  484. }, AmbiOrderHFGainHOA[MAX_AMBI_ORDER+1]{
  485. 2.35702260e+00f, 1.82574186e+00f, 9.42809042e-01f
  486. /*1.86508671e+00f, 1.60609389e+00f, 1.14205530e+00f, 5.68379553e-01f*/
  487. };
  488. static constexpr ALsizei ChansPerOrder[MAX_AMBI_ORDER+1]{ 1, 3, 5, 7 };
  489. const ALfloat *AmbiOrderHFGain{AmbiOrderHFGainFOA};
  490. static_assert(al::size(AmbiPoints) == al::size(AmbiMatrix), "Ambisonic HRTF mismatch");
  491. /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
  492. * and it eases the CPU/memory load.
  493. */
  494. ALsizei ambi_order{1};
  495. if(device->mRenderMode != HrtfRender)
  496. {
  497. ambi_order = 2;
  498. AmbiOrderHFGain = AmbiOrderHFGainHOA;
  499. }
  500. device->mAmbiOrder = ambi_order;
  501. const size_t count{AmbiChannelsFromOrder(ambi_order)};
  502. device->mHrtfState = DirectHrtfState::Create(count);
  503. std::transform(AmbiIndex::From3D.begin(), AmbiIndex::From3D.begin()+count,
  504. std::begin(device->Dry.AmbiMap),
  505. [](const ALsizei &index) noexcept { return BFChannelConfig{1.0f, index}; }
  506. );
  507. device->Dry.NumChannels = static_cast<ALsizei>(count);
  508. device->RealOut.NumChannels = device->channelsFromFmt();
  509. BuildBFormatHrtf(device->mHrtf, device->mHrtfState.get(), device->Dry.NumChannels, AmbiPoints,
  510. AmbiMatrix, al::size(AmbiPoints), AmbiOrderHFGain);
  511. HrtfEntry *Hrtf{device->mHrtf};
  512. InitNearFieldCtrl(device, Hrtf->field[0].distance, ambi_order, ChansPerOrder);
  513. }
  514. void InitUhjPanning(ALCdevice *device)
  515. {
  516. /* UHJ is always 2D first-order. */
  517. static constexpr size_t count{Ambi2DChannelsFromOrder(1)};
  518. device->mAmbiOrder = 1;
  519. auto acnmap_end = AmbiIndex::FromFuMa.begin() + count;
  520. std::transform(AmbiIndex::FromFuMa.begin(), acnmap_end, std::begin(device->Dry.AmbiMap),
  521. [](const ALsizei &acn) noexcept -> BFChannelConfig
  522. { return BFChannelConfig{1.0f/AmbiScale::FromFuMa[acn], acn}; }
  523. );
  524. device->Dry.NumChannels = count;
  525. device->RealOut.NumChannels = device->channelsFromFmt();
  526. }
  527. } // namespace
  528. void CalcAmbiCoeffs(const ALfloat y, const ALfloat z, const ALfloat x, const ALfloat spread,
  529. ALfloat (&coeffs)[MAX_AMBI_CHANNELS])
  530. {
  531. /* Zeroth-order */
  532. coeffs[0] = 1.0f; /* ACN 0 = 1 */
  533. /* First-order */
  534. coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
  535. coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
  536. coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
  537. /* Second-order */
  538. coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
  539. coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
  540. coeffs[6] = 1.118033989f * (z*z*3.0f - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
  541. coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
  542. coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
  543. /* Third-order */
  544. coeffs[9] = 2.091650066f * y * (x*x*3.0f - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
  545. coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
  546. coeffs[11] = 1.620185175f * y * (z*z*5.0f - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
  547. coeffs[12] = 1.322875656f * z * (z*z*5.0f - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
  548. coeffs[13] = 1.620185175f * x * (z*z*5.0f - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
  549. coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
  550. coeffs[15] = 2.091650066f * x * (x*x - y*y*3.0f); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
  551. /* Fourth-order */
  552. /* ACN 16 = sqrt(35)*3/2 * X * Y * (X*X - Y*Y) */
  553. /* ACN 17 = sqrt(35/2)*3/2 * (3*X*X - Y*Y) * Y * Z */
  554. /* ACN 18 = sqrt(5)*3/2 * X * Y * (7*Z*Z - 1) */
  555. /* ACN 19 = sqrt(5/2)*3/2 * Y * Z * (7*Z*Z - 3) */
  556. /* ACN 20 = 3/8 * (35*Z*Z*Z*Z - 30*Z*Z + 3) */
  557. /* ACN 21 = sqrt(5/2)*3/2 * X * Z * (7*Z*Z - 3) */
  558. /* ACN 22 = sqrt(5)*3/4 * (X*X - Y*Y) * (7*Z*Z - 1) */
  559. /* ACN 23 = sqrt(35/2)*3/2 * (X*X - 3*Y*Y) * X * Z */
  560. /* ACN 24 = sqrt(35)*3/8 * (X*X*X*X - 6*X*X*Y*Y + Y*Y*Y*Y) */
  561. if(spread > 0.0f)
  562. {
  563. /* Implement the spread by using a spherical source that subtends the
  564. * angle spread. See:
  565. * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
  566. *
  567. * When adjusted for N3D normalization instead of SN3D, these
  568. * calculations are:
  569. *
  570. * ZH0 = -sqrt(pi) * (-1+ca);
  571. * ZH1 = 0.5*sqrt(pi) * sa*sa;
  572. * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
  573. * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
  574. * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
  575. * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
  576. *
  577. * The gain of the source is compensated for size, so that the
  578. * loudness doesn't depend on the spread. Thus:
  579. *
  580. * ZH0 = 1.0f;
  581. * ZH1 = 0.5f * (ca+1.0f);
  582. * ZH2 = 0.5f * (ca+1.0f)*ca;
  583. * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
  584. * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
  585. * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
  586. */
  587. ALfloat ca = std::cos(spread * 0.5f);
  588. /* Increase the source volume by up to +3dB for a full spread. */
  589. ALfloat scale = std::sqrt(1.0f + spread/al::MathDefs<float>::Tau());
  590. ALfloat ZH0_norm = scale;
  591. ALfloat ZH1_norm = 0.5f * (ca+1.f) * scale;
  592. ALfloat ZH2_norm = 0.5f * (ca+1.f)*ca * scale;
  593. ALfloat ZH3_norm = 0.125f * (ca+1.f)*(5.f*ca*ca-1.f) * scale;
  594. /* Zeroth-order */
  595. coeffs[0] *= ZH0_norm;
  596. /* First-order */
  597. coeffs[1] *= ZH1_norm;
  598. coeffs[2] *= ZH1_norm;
  599. coeffs[3] *= ZH1_norm;
  600. /* Second-order */
  601. coeffs[4] *= ZH2_norm;
  602. coeffs[5] *= ZH2_norm;
  603. coeffs[6] *= ZH2_norm;
  604. coeffs[7] *= ZH2_norm;
  605. coeffs[8] *= ZH2_norm;
  606. /* Third-order */
  607. coeffs[9] *= ZH3_norm;
  608. coeffs[10] *= ZH3_norm;
  609. coeffs[11] *= ZH3_norm;
  610. coeffs[12] *= ZH3_norm;
  611. coeffs[13] *= ZH3_norm;
  612. coeffs[14] *= ZH3_norm;
  613. coeffs[15] *= ZH3_norm;
  614. }
  615. }
  616. void ComputePanGains(const MixParams *mix, const ALfloat *RESTRICT coeffs, ALfloat ingain, ALfloat (&gains)[MAX_OUTPUT_CHANNELS])
  617. {
  618. auto ambimap = mix->AmbiMap.cbegin();
  619. const ALsizei numchans{mix->NumChannels};
  620. ASSUME(numchans > 0);
  621. auto iter = std::transform(ambimap, ambimap+numchans, std::begin(gains),
  622. [coeffs,ingain](const BFChannelConfig &chanmap) noexcept -> ALfloat
  623. {
  624. ASSUME(chanmap.Index >= 0);
  625. return chanmap.Scale * coeffs[chanmap.Index] * ingain;
  626. }
  627. );
  628. std::fill(iter, std::end(gains), 0.0f);
  629. }
  630. void aluInitRenderer(ALCdevice *device, ALint hrtf_id, HrtfRequestMode hrtf_appreq, HrtfRequestMode hrtf_userreq)
  631. {
  632. /* Hold the HRTF the device last used, in case it's used again. */
  633. HrtfEntry *old_hrtf{device->mHrtf};
  634. device->mHrtfState = nullptr;
  635. device->mHrtf = nullptr;
  636. device->HrtfName.clear();
  637. device->mRenderMode = NormalRender;
  638. device->Dry.AmbiMap.fill(BFChannelConfig{});
  639. device->Dry.NumChannels = 0;
  640. std::fill(std::begin(device->NumChannelsPerOrder), std::end(device->NumChannelsPerOrder), 0);
  641. device->AvgSpeakerDist = 0.0f;
  642. device->ChannelDelay.clear();
  643. device->AmbiDecoder = nullptr;
  644. device->Stablizer = nullptr;
  645. if(device->FmtChans != DevFmtStereo)
  646. {
  647. if(old_hrtf)
  648. old_hrtf->DecRef();
  649. old_hrtf = nullptr;
  650. if(hrtf_appreq == Hrtf_Enable)
  651. device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  652. const char *layout{nullptr};
  653. switch(device->FmtChans)
  654. {
  655. case DevFmtQuad: layout = "quad"; break;
  656. case DevFmtX51: /* fall-through */
  657. case DevFmtX51Rear: layout = "surround51"; break;
  658. case DevFmtX61: layout = "surround61"; break;
  659. case DevFmtX71: layout = "surround71"; break;
  660. /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
  661. case DevFmtMono:
  662. case DevFmtStereo:
  663. case DevFmtAmbi3D:
  664. break;
  665. }
  666. const char *devname{device->DeviceName.c_str()};
  667. ALsizei speakermap[MAX_OUTPUT_CHANNELS];
  668. AmbDecConf *pconf{nullptr};
  669. AmbDecConf conf{};
  670. if(layout)
  671. {
  672. const char *fname;
  673. if(ConfigValueStr(devname, "decoder", layout, &fname))
  674. {
  675. if(!conf.load(fname))
  676. ERR("Failed to load layout file %s\n", fname);
  677. else if(conf.Speakers.size() > MAX_OUTPUT_CHANNELS)
  678. ERR("Unsupported speaker count %zu (max %d)\n", conf.Speakers.size(),
  679. MAX_OUTPUT_CHANNELS);
  680. else if(conf.ChanMask > AMBI_3ORDER_MASK)
  681. ERR("Unsupported channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
  682. AMBI_3ORDER_MASK);
  683. else if(MakeSpeakerMap(device, &conf, speakermap))
  684. pconf = &conf;
  685. }
  686. }
  687. if(!pconf)
  688. InitPanning(device);
  689. else
  690. {
  691. int hqdec{GetConfigValueBool(devname, "decoder", "hq-mode", 0)};
  692. InitCustomPanning(device, !!hqdec, pconf, speakermap);
  693. }
  694. /* Enable the stablizer only for formats that have front-left, front-
  695. * right, and front-center outputs.
  696. */
  697. switch(device->FmtChans)
  698. {
  699. case DevFmtX51:
  700. case DevFmtX51Rear:
  701. case DevFmtX61:
  702. case DevFmtX71:
  703. if(GetConfigValueBool(devname, nullptr, "front-stablizer", 0))
  704. {
  705. auto stablizer = al::make_unique<FrontStablizer>();
  706. /* Initialize band-splitting filters for the front-left and
  707. * front-right channels, with a crossover at 5khz (could be
  708. * higher).
  709. */
  710. const ALfloat scale{static_cast<ALfloat>(5000.0 / device->Frequency)};
  711. stablizer->LFilter.init(scale);
  712. stablizer->RFilter = stablizer->LFilter;
  713. /* Initialize an all-pass filter for the phase corrector. */
  714. stablizer->APFilter.init(scale);
  715. device->Stablizer = std::move(stablizer);
  716. /* NOTE: Don't know why this has to be "copied" into a local
  717. * static constexpr variable to avoid a reference on
  718. * FrontStablizer::DelayLength...
  719. */
  720. static constexpr size_t StablizerDelay{FrontStablizer::DelayLength};
  721. device->FixedLatency += nanoseconds{seconds{StablizerDelay}} / device->Frequency;
  722. }
  723. break;
  724. case DevFmtMono:
  725. case DevFmtStereo:
  726. case DevFmtQuad:
  727. case DevFmtAmbi3D:
  728. break;
  729. }
  730. TRACE("Front stablizer %s\n", device->Stablizer ? "enabled" : "disabled");
  731. return;
  732. }
  733. device->AmbiDecoder = nullptr;
  734. bool headphones{device->IsHeadphones != AL_FALSE};
  735. if(device->Type != Loopback)
  736. {
  737. const char *mode;
  738. if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-mode", &mode))
  739. {
  740. if(strcasecmp(mode, "headphones") == 0)
  741. headphones = true;
  742. else if(strcasecmp(mode, "speakers") == 0)
  743. headphones = false;
  744. else if(strcasecmp(mode, "auto") != 0)
  745. ERR("Unexpected stereo-mode: %s\n", mode);
  746. }
  747. }
  748. if(hrtf_userreq == Hrtf_Default)
  749. {
  750. bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
  751. (hrtf_appreq == Hrtf_Enable);
  752. if(!usehrtf) goto no_hrtf;
  753. device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
  754. if(headphones && hrtf_appreq != Hrtf_Disable)
  755. device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
  756. }
  757. else
  758. {
  759. if(hrtf_userreq != Hrtf_Enable)
  760. {
  761. if(hrtf_appreq == Hrtf_Enable)
  762. device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
  763. goto no_hrtf;
  764. }
  765. device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
  766. }
  767. if(device->HrtfList.empty())
  768. device->HrtfList = EnumerateHrtf(device->DeviceName.c_str());
  769. if(hrtf_id >= 0 && static_cast<size_t>(hrtf_id) < device->HrtfList.size())
  770. {
  771. const EnumeratedHrtf &entry = device->HrtfList[hrtf_id];
  772. HrtfEntry *hrtf{GetLoadedHrtf(entry.hrtf)};
  773. if(hrtf && hrtf->sampleRate == device->Frequency)
  774. {
  775. device->mHrtf = hrtf;
  776. device->HrtfName = entry.name;
  777. }
  778. else if(hrtf)
  779. hrtf->DecRef();
  780. }
  781. if(!device->mHrtf)
  782. {
  783. auto find_hrtf = [device](const EnumeratedHrtf &entry) -> bool
  784. {
  785. HrtfEntry *hrtf{GetLoadedHrtf(entry.hrtf)};
  786. if(!hrtf) return false;
  787. if(hrtf->sampleRate != device->Frequency)
  788. {
  789. hrtf->DecRef();
  790. return false;
  791. }
  792. device->mHrtf = hrtf;
  793. device->HrtfName = entry.name;
  794. return true;
  795. };
  796. std::find_if(device->HrtfList.cbegin(), device->HrtfList.cend(), find_hrtf);
  797. }
  798. if(device->mHrtf)
  799. {
  800. if(old_hrtf)
  801. old_hrtf->DecRef();
  802. old_hrtf = nullptr;
  803. device->mRenderMode = HrtfRender;
  804. const char *mode;
  805. if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf-mode", &mode))
  806. {
  807. if(strcasecmp(mode, "full") == 0)
  808. device->mRenderMode = HrtfRender;
  809. else if(strcasecmp(mode, "basic") == 0)
  810. device->mRenderMode = NormalRender;
  811. else
  812. ERR("Unexpected hrtf-mode: %s\n", mode);
  813. }
  814. TRACE("%s HRTF rendering enabled, using \"%s\"\n",
  815. ((device->mRenderMode == HrtfRender) ? "Full" : "Basic"), device->HrtfName.c_str()
  816. );
  817. InitHrtfPanning(device);
  818. return;
  819. }
  820. device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  821. no_hrtf:
  822. if(old_hrtf)
  823. old_hrtf->DecRef();
  824. old_hrtf = nullptr;
  825. device->mRenderMode = StereoPair;
  826. int bs2blevel{((headphones && hrtf_appreq != Hrtf_Disable) ||
  827. (hrtf_appreq == Hrtf_Enable)) ? 5 : 0};
  828. if(device->Type != Loopback)
  829. ConfigValueInt(device->DeviceName.c_str(), nullptr, "cf_level", &bs2blevel);
  830. if(bs2blevel > 0 && bs2blevel <= 6)
  831. {
  832. device->Bs2b = al::make_unique<bs2b>();
  833. bs2b_set_params(device->Bs2b.get(), bs2blevel, device->Frequency);
  834. TRACE("BS2B enabled\n");
  835. InitPanning(device);
  836. return;
  837. }
  838. const char *mode;
  839. if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-encoding", &mode))
  840. {
  841. if(strcasecmp(mode, "uhj") == 0)
  842. device->mRenderMode = NormalRender;
  843. else if(strcasecmp(mode, "panpot") != 0)
  844. ERR("Unexpected stereo-encoding: %s\n", mode);
  845. }
  846. if(device->mRenderMode == NormalRender)
  847. {
  848. device->Uhj_Encoder = al::make_unique<Uhj2Encoder>();
  849. TRACE("UHJ enabled\n");
  850. InitUhjPanning(device);
  851. return;
  852. }
  853. TRACE("Stereo rendering\n");
  854. InitPanning(device);
  855. }
  856. void aluInitEffectPanning(ALeffectslot *slot, ALCdevice *device)
  857. {
  858. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  859. slot->MixBuffer.resize(count);
  860. slot->MixBuffer.shrink_to_fit();
  861. auto acnmap_end = AmbiIndex::From3D.begin() + count;
  862. auto iter = std::transform(AmbiIndex::From3D.begin(), acnmap_end, slot->Wet.AmbiMap.begin(),
  863. [](const ALsizei &acn) noexcept -> BFChannelConfig
  864. { return BFChannelConfig{1.0f, acn}; }
  865. );
  866. std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
  867. slot->Wet.Buffer = &reinterpret_cast<ALfloat(&)[BUFFERSIZE]>(slot->MixBuffer[0]);
  868. slot->Wet.NumChannels = static_cast<ALsizei>(count);
  869. }