panning.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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 <algorithm>
  22. #include <array>
  23. #include <bitset>
  24. #include <cassert>
  25. #include <chrono>
  26. #include <cmath>
  27. #include <cstddef>
  28. #include <cstdio>
  29. #include <cstdint>
  30. #include <functional>
  31. #include <memory>
  32. #include <numeric>
  33. #include <optional>
  34. #include <string>
  35. #include <string_view>
  36. #include <utility>
  37. #include <vector>
  38. #include "AL/alc.h"
  39. #include "AL/alext.h"
  40. #include "alc/context.h"
  41. #include "alnumbers.h"
  42. #include "alnumeric.h"
  43. #include "alspan.h"
  44. #include "alstring.h"
  45. #include "alu.h"
  46. #include "core/ambdec.h"
  47. #include "core/ambidefs.h"
  48. #include "core/bformatdec.h"
  49. #include "core/bufferline.h"
  50. #include "core/bs2b.h"
  51. #include "core/context.h"
  52. #include "core/devformat.h"
  53. #include "core/device.h"
  54. #include "core/effectslot.h"
  55. #include "core/filters/nfc.h"
  56. #include "core/filters/splitter.h"
  57. #include "core/front_stablizer.h"
  58. #include "core/hrtf.h"
  59. #include "core/logging.h"
  60. #include "core/mixer/hrtfdefs.h"
  61. #include "core/uhjfilter.h"
  62. #include "device.h"
  63. #include "flexarray.h"
  64. #include "intrusive_ptr.h"
  65. #include "opthelpers.h"
  66. #include "vector.h"
  67. namespace {
  68. using namespace std::string_view_literals;
  69. using std::chrono::seconds;
  70. using std::chrono::nanoseconds;
  71. const char *GetLabelFromChannel(Channel channel)
  72. {
  73. switch(channel)
  74. {
  75. case FrontLeft: return "front-left";
  76. case FrontRight: return "front-right";
  77. case FrontCenter: return "front-center";
  78. case LFE: return "lfe";
  79. case BackLeft: return "back-left";
  80. case BackRight: return "back-right";
  81. case BackCenter: return "back-center";
  82. case SideLeft: return "side-left";
  83. case SideRight: return "side-right";
  84. case TopFrontLeft: return "top-front-left";
  85. case TopFrontCenter: return "top-front-center";
  86. case TopFrontRight: return "top-front-right";
  87. case TopCenter: return "top-center";
  88. case TopBackLeft: return "top-back-left";
  89. case TopBackCenter: return "top-back-center";
  90. case TopBackRight: return "top-back-right";
  91. case BottomFrontLeft: return "bottom-front-left";
  92. case BottomFrontRight: return "bottom-front-right";
  93. case BottomBackLeft: return "bottom-back-left";
  94. case BottomBackRight: return "bottom-back-right";
  95. case Aux0: return "Aux0";
  96. case Aux1: return "Aux1";
  97. case Aux2: return "Aux2";
  98. case Aux3: return "Aux3";
  99. case Aux4: return "Aux4";
  100. case Aux5: return "Aux5";
  101. case Aux6: return "Aux6";
  102. case Aux7: return "Aux7";
  103. case Aux8: return "Aux8";
  104. case Aux9: return "Aux9";
  105. case Aux10: return "Aux10";
  106. case Aux11: return "Aux11";
  107. case Aux12: return "Aux12";
  108. case Aux13: return "Aux13";
  109. case Aux14: return "Aux14";
  110. case Aux15: return "Aux15";
  111. case MaxChannels: break;
  112. }
  113. return "(unknown)";
  114. }
  115. std::unique_ptr<FrontStablizer> CreateStablizer(const size_t outchans, const uint srate)
  116. {
  117. auto stablizer = FrontStablizer::Create(outchans);
  118. /* Initialize band-splitting filter for the mid signal, with a crossover at
  119. * 5khz (could be higher).
  120. */
  121. stablizer->MidFilter.init(5000.0f / static_cast<float>(srate));
  122. for(auto &filter : stablizer->ChannelFilters)
  123. filter = stablizer->MidFilter;
  124. return stablizer;
  125. }
  126. void AllocChannels(ALCdevice *device, const size_t main_chans, const size_t real_chans)
  127. {
  128. TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans, real_chans);
  129. /* Allocate extra channels for any post-filter output. */
  130. const size_t num_chans{main_chans + real_chans};
  131. TRACE("Allocating %zu channels, %zu bytes\n", num_chans,
  132. num_chans*sizeof(device->MixBuffer[0]));
  133. device->MixBuffer.resize(num_chans);
  134. al::span<FloatBufferLine> buffer{device->MixBuffer};
  135. device->Dry.Buffer = buffer.first(main_chans);
  136. buffer = buffer.subspan(main_chans);
  137. if(real_chans != 0)
  138. {
  139. device->RealOut.Buffer = buffer.first(real_chans);
  140. buffer = buffer.subspan(real_chans);
  141. }
  142. else
  143. device->RealOut.Buffer = device->Dry.Buffer;
  144. }
  145. using ChannelCoeffs = std::array<float,MaxAmbiChannels>;
  146. enum DecoderMode : bool {
  147. SingleBand = false,
  148. DualBand = true
  149. };
  150. template<DecoderMode Mode, size_t N>
  151. struct DecoderConfig;
  152. template<size_t N>
  153. struct DecoderConfig<SingleBand, N> {
  154. uint8_t mOrder{};
  155. bool mIs3D{};
  156. std::array<Channel,N> mChannels{};
  157. DevAmbiScaling mScaling{};
  158. std::array<float,MaxAmbiOrder+1> mOrderGain{};
  159. std::array<ChannelCoeffs,N> mCoeffs{};
  160. };
  161. template<size_t N>
  162. struct DecoderConfig<DualBand, N> {
  163. uint8_t mOrder{};
  164. bool mIs3D{};
  165. std::array<Channel,N> mChannels{};
  166. DevAmbiScaling mScaling{};
  167. std::array<float,MaxAmbiOrder+1> mOrderGain{};
  168. std::array<ChannelCoeffs,N> mCoeffs{};
  169. std::array<float,MaxAmbiOrder+1> mOrderGainLF{};
  170. std::array<ChannelCoeffs,N> mCoeffsLF{};
  171. };
  172. template<>
  173. struct DecoderConfig<DualBand, 0> {
  174. uint8_t mOrder{};
  175. bool mIs3D{};
  176. al::span<const Channel> mChannels;
  177. DevAmbiScaling mScaling{};
  178. al::span<const float> mOrderGain;
  179. al::span<const ChannelCoeffs> mCoeffs;
  180. al::span<const float> mOrderGainLF;
  181. al::span<const ChannelCoeffs> mCoeffsLF;
  182. template<size_t N>
  183. DecoderConfig& operator=(const DecoderConfig<SingleBand,N> &rhs) noexcept
  184. {
  185. mOrder = rhs.mOrder;
  186. mIs3D = rhs.mIs3D;
  187. mChannels = rhs.mChannels;
  188. mScaling = rhs.mScaling;
  189. mOrderGain = rhs.mOrderGain;
  190. mCoeffs = rhs.mCoeffs;
  191. mOrderGainLF = {};
  192. mCoeffsLF = {};
  193. return *this;
  194. }
  195. template<size_t N>
  196. DecoderConfig& operator=(const DecoderConfig<DualBand,N> &rhs) noexcept
  197. {
  198. mOrder = rhs.mOrder;
  199. mIs3D = rhs.mIs3D;
  200. mChannels = rhs.mChannels;
  201. mScaling = rhs.mScaling;
  202. mOrderGain = rhs.mOrderGain;
  203. mCoeffs = rhs.mCoeffs;
  204. mOrderGainLF = rhs.mOrderGainLF;
  205. mCoeffsLF = rhs.mCoeffsLF;
  206. return *this;
  207. }
  208. explicit operator bool() const noexcept { return !mChannels.empty(); }
  209. };
  210. using DecoderView = DecoderConfig<DualBand, 0>;
  211. void InitNearFieldCtrl(ALCdevice *device, const float ctrl_dist, const uint order, const bool is3d)
  212. {
  213. static const std::array<uint,MaxAmbiOrder+1> chans_per_order2d{{1, 2, 2, 2}};
  214. static const std::array<uint,MaxAmbiOrder+1> chans_per_order3d{{1, 3, 5, 7}};
  215. /* NFC is only used when AvgSpeakerDist is greater than 0. */
  216. if(!device->getConfigValueBool("decoder", "nfc", false) || !(ctrl_dist > 0.0f))
  217. return;
  218. device->AvgSpeakerDist = std::clamp(ctrl_dist, 0.1f, 10.0f);
  219. TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
  220. const float w1{SpeedOfSoundMetersPerSec /
  221. (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
  222. device->mNFCtrlFilter.init(w1);
  223. auto iter = std::copy_n(is3d ? chans_per_order3d.begin() : chans_per_order2d.begin(), order+1u,
  224. device->NumChannelsPerOrder.begin());
  225. std::fill(iter, device->NumChannelsPerOrder.end(), 0u);
  226. }
  227. void InitDistanceComp(ALCdevice *device, const al::span<const Channel> channels,
  228. const al::span<const float,MaxOutputChannels> dists)
  229. {
  230. const float maxdist{std::accumulate(dists.begin(), dists.end(), 0.0f,
  231. [](const float a, const float b) noexcept -> float { return std::max(a, b); })};
  232. if(!device->getConfigValueBool("decoder", "distance-comp", true) || !(maxdist > 0.0f))
  233. return;
  234. const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
  235. struct DistCoeffs { uint Length{}; float Gain{}; };
  236. std::vector<DistCoeffs> ChanDelay;
  237. ChanDelay.reserve(device->RealOut.Buffer.size());
  238. size_t total{0u};
  239. for(size_t chidx{0};chidx < channels.size();++chidx)
  240. {
  241. const Channel ch{channels[chidx]};
  242. const size_t idx{device->RealOut.ChannelIndex[ch]};
  243. if(idx == InvalidChannelIndex)
  244. continue;
  245. const float distance{dists[chidx]};
  246. /* Distance compensation only delays in steps of the sample rate. This
  247. * is a bit less accurate since the delay time falls to the nearest
  248. * sample time, but it's far simpler as it doesn't have to deal with
  249. * phase offsets. This means at 48khz, for instance, the distance delay
  250. * will be in steps of about 7 millimeters.
  251. */
  252. float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
  253. if(delay > float{DistanceComp::MaxDelay-1})
  254. {
  255. ERR("Delay for channel %zu (%s) exceeds buffer length (%f > %d)\n", idx,
  256. GetLabelFromChannel(ch), delay, DistanceComp::MaxDelay-1);
  257. delay = float{DistanceComp::MaxDelay-1};
  258. }
  259. ChanDelay.resize(std::max(ChanDelay.size(), idx+1_uz));
  260. ChanDelay[idx].Length = static_cast<uint>(delay);
  261. ChanDelay[idx].Gain = distance / maxdist;
  262. TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
  263. ChanDelay[idx].Length, ChanDelay[idx].Gain);
  264. /* Round up to the next 4th sample, so each channel buffer starts
  265. * 16-byte aligned.
  266. */
  267. total += RoundUp(ChanDelay[idx].Length, 4);
  268. }
  269. if(total > 0)
  270. {
  271. auto chandelays = DistanceComp::Create(total);
  272. auto chanbuffer = chandelays->mSamples.begin();
  273. auto set_bufptr = [&chanbuffer](const DistCoeffs &data)
  274. {
  275. DistanceComp::ChanData ret{};
  276. ret.Buffer = al::span{chanbuffer, data.Length};
  277. ret.Gain = data.Gain;
  278. chanbuffer += ptrdiff_t(RoundUp(data.Length, 4));
  279. return ret;
  280. };
  281. std::transform(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
  282. set_bufptr);
  283. device->ChannelDelays = std::move(chandelays);
  284. }
  285. }
  286. constexpr auto GetAmbiScales(DevAmbiScaling scaletype) noexcept
  287. {
  288. if(scaletype == DevAmbiScaling::FuMa) return al::span{AmbiScale::FromFuMa};
  289. if(scaletype == DevAmbiScaling::SN3D) return al::span{AmbiScale::FromSN3D};
  290. return al::span{AmbiScale::FromN3D};
  291. }
  292. constexpr auto GetAmbiLayout(DevAmbiLayout layouttype) noexcept
  293. {
  294. if(layouttype == DevAmbiLayout::FuMa) return al::span{AmbiIndex::FromFuMa};
  295. return al::span{AmbiIndex::FromACN};
  296. }
  297. DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf,
  298. DecoderConfig<DualBand,MaxOutputChannels> &decoder)
  299. {
  300. DecoderView ret{};
  301. decoder.mOrder = (conf->ChanMask > Ambi3OrderMask) ? uint8_t{4} :
  302. (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
  303. (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
  304. decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
  305. switch(conf->CoeffScale)
  306. {
  307. case AmbDecScale::Unset: ASSUME(false); break;
  308. case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
  309. case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
  310. case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
  311. }
  312. const auto hfordermin = std::min(conf->HFOrderGain.size(), decoder.mOrderGain.size());
  313. std::copy_n(conf->HFOrderGain.begin(), hfordermin, decoder.mOrderGain.begin());
  314. const auto lfordermin = std::min(conf->LFOrderGain.size(), decoder.mOrderGainLF.size());
  315. std::copy_n(conf->LFOrderGain.begin(), lfordermin, decoder.mOrderGainLF.begin());
  316. const auto num_coeffs = decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder)
  317. : Ambi2DChannelsFromOrder(decoder.mOrder);
  318. const auto idx_map = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::FromACN}
  319. : al::span<const uint8_t>{AmbiIndex::FromACN2D};
  320. const auto hfmatrix = conf->HFMatrix;
  321. const auto lfmatrix = conf->LFMatrix;
  322. uint chan_count{0};
  323. for(auto &speaker : al::span{std::as_const(conf->Speakers)})
  324. {
  325. /* NOTE: AmbDec does not define any standard speaker names, however
  326. * for this to work we have to by able to find the output channel
  327. * the speaker definition corresponds to. Therefore, OpenAL Soft
  328. * requires these channel labels to be recognized:
  329. *
  330. * LF = Front left
  331. * RF = Front right
  332. * LS = Side left
  333. * RS = Side right
  334. * LB = Back left
  335. * RB = Back right
  336. * CE = Front center
  337. * CB = Back center
  338. * LFT = Top front left
  339. * RFT = Top front right
  340. * LBT = Top back left
  341. * RBT = Top back right
  342. * LFB = Bottom front left
  343. * RFB = Bottom front right
  344. * LBB = Bottom back left
  345. * RBB = Bottom back right
  346. *
  347. * Additionally, surround51 will acknowledge back speakers for side
  348. * channels, to avoid issues with an ambdec expecting 5.1 to use the
  349. * back channels.
  350. */
  351. Channel ch{};
  352. if(speaker.Name == "LF"sv)
  353. ch = FrontLeft;
  354. else if(speaker.Name == "RF"sv)
  355. ch = FrontRight;
  356. else if(speaker.Name == "CE"sv)
  357. ch = FrontCenter;
  358. else if(speaker.Name == "LS"sv)
  359. ch = SideLeft;
  360. else if(speaker.Name == "RS"sv)
  361. ch = SideRight;
  362. else if(speaker.Name == "LB"sv)
  363. ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
  364. else if(speaker.Name == "RB"sv)
  365. ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
  366. else if(speaker.Name == "CB"sv)
  367. ch = BackCenter;
  368. else if(speaker.Name == "LFT"sv)
  369. ch = TopFrontLeft;
  370. else if(speaker.Name == "RFT"sv)
  371. ch = TopFrontRight;
  372. else if(speaker.Name == "LBT"sv)
  373. ch = TopBackLeft;
  374. else if(speaker.Name == "RBT"sv)
  375. ch = TopBackRight;
  376. else if(speaker.Name == "LFB"sv)
  377. ch = BottomFrontLeft;
  378. else if(speaker.Name == "RFB"sv)
  379. ch = BottomFrontRight;
  380. else if(speaker.Name == "LBB"sv)
  381. ch = BottomBackLeft;
  382. else if(speaker.Name == "RBB"sv)
  383. ch = BottomBackRight;
  384. else
  385. {
  386. int idx{};
  387. char c{};
  388. if(sscanf(speaker.Name.c_str(), "AUX%d%c", &idx, &c) != 1 || idx < 0
  389. || idx >= MaxChannels-Aux0)
  390. {
  391. ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
  392. continue;
  393. }
  394. ch = static_cast<Channel>(Aux0+idx);
  395. }
  396. decoder.mChannels[chan_count] = ch;
  397. for(size_t dst{0};dst < num_coeffs;++dst)
  398. {
  399. const size_t src{idx_map[dst]};
  400. decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
  401. }
  402. if(conf->FreqBands > 1)
  403. {
  404. for(size_t dst{0};dst < num_coeffs;++dst)
  405. {
  406. const size_t src{idx_map[dst]};
  407. decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
  408. }
  409. }
  410. ++chan_count;
  411. }
  412. if(chan_count > 0)
  413. {
  414. ret.mOrder = decoder.mOrder;
  415. ret.mIs3D = decoder.mIs3D;
  416. ret.mScaling = decoder.mScaling;
  417. ret.mChannels = al::span{decoder.mChannels}.first(chan_count);
  418. ret.mOrderGain = decoder.mOrderGain;
  419. ret.mCoeffs = al::span{decoder.mCoeffs}.first(chan_count);
  420. if(conf->FreqBands > 1)
  421. {
  422. ret.mOrderGainLF = decoder.mOrderGainLF;
  423. ret.mCoeffsLF = al::span{decoder.mCoeffsLF}.first(chan_count);
  424. }
  425. }
  426. return ret;
  427. }
  428. constexpr DecoderConfig<SingleBand, 1> MonoConfig{
  429. 0, false, {{FrontCenter}},
  430. DevAmbiScaling::N3D,
  431. {{1.0f}},
  432. {{ {{1.0f}} }}
  433. };
  434. constexpr DecoderConfig<SingleBand, 2> StereoConfig{
  435. 1, false, {{FrontLeft, FrontRight}},
  436. DevAmbiScaling::N3D,
  437. {{1.0f, 1.0f}},
  438. {{
  439. {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
  440. {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
  441. }}
  442. };
  443. constexpr DecoderConfig<DualBand, 4> QuadConfig{
  444. 1, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
  445. DevAmbiScaling::N3D,
  446. /*HF*/{{1.41421356e+0f, 1.00000000e+0f}},
  447. {{
  448. {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
  449. {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
  450. {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
  451. {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
  452. }},
  453. /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
  454. {{
  455. {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
  456. {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
  457. {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
  458. {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
  459. }}
  460. };
  461. constexpr DecoderConfig<DualBand, 5> X51Config{
  462. 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
  463. DevAmbiScaling::FuMa,
  464. /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  465. {{
  466. {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
  467. {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
  468. {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
  469. {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
  470. {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
  471. }},
  472. /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  473. {{
  474. {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
  475. {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
  476. {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
  477. {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
  478. {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
  479. }}
  480. };
  481. constexpr DecoderConfig<SingleBand, 5> X61Config{
  482. 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
  483. DevAmbiScaling::N3D,
  484. {{1.0f, 1.0f, 1.0f}},
  485. {{
  486. {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
  487. {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
  488. {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
  489. {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
  490. {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
  491. }}
  492. };
  493. constexpr DecoderConfig<DualBand, 6> X71Config{
  494. 2, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
  495. DevAmbiScaling::N3D,
  496. /*HF*/{{1.41421356e+0f, 1.22474487e+0f, 7.07106781e-1f}},
  497. {{
  498. {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
  499. {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
  500. {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
  501. {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
  502. {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
  503. {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
  504. }},
  505. /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  506. {{
  507. {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
  508. {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
  509. {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
  510. {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
  511. {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
  512. {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
  513. }}
  514. };
  515. constexpr DecoderConfig<DualBand, 6> X3D71Config{
  516. 1, true, {{Aux0, SideLeft, FrontLeft, FrontRight, SideRight, Aux1}},
  517. DevAmbiScaling::N3D,
  518. /*HF*/{{1.73205081e+0f, 1.00000000e+0f}},
  519. {{
  520. {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
  521. {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
  522. {{1.666666667e-01f, 2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
  523. {{1.666666667e-01f, -2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
  524. {{1.666666667e-01f, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
  525. {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
  526. }},
  527. /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
  528. {{
  529. {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
  530. {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
  531. {{1.666666667e-01f, 2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
  532. {{1.666666667e-01f, -2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
  533. {{1.666666667e-01f, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
  534. {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
  535. }}
  536. };
  537. constexpr DecoderConfig<SingleBand, 10> X714Config{
  538. 1, true, {{FrontLeft, FrontRight, SideLeft, SideRight, BackLeft, BackRight, TopFrontLeft, TopFrontRight, TopBackLeft, TopBackRight }},
  539. DevAmbiScaling::N3D,
  540. {{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  541. {{
  542. {{1.27149251e-01f, 7.63047539e-02f, -3.64373750e-02f, 1.59700680e-01f}},
  543. {{1.07005418e-01f, -7.67638760e-02f, -4.92129762e-02f, 1.29012797e-01f}},
  544. {{1.26400196e-01f, 1.77494694e-01f, -3.71203389e-02f, 0.00000000e+00f}},
  545. {{1.26396516e-01f, -1.77488059e-01f, -3.71297878e-02f, 0.00000000e+00f}},
  546. {{1.06996956e-01f, 7.67615256e-02f, -4.92166307e-02f, -1.29001640e-01f}},
  547. {{1.27145671e-01f, -7.63003471e-02f, -3.64353304e-02f, -1.59697510e-01f}},
  548. {{8.80919747e-02f, 7.48940670e-02f, 9.08786244e-02f, 6.22527183e-02f}},
  549. {{1.57880745e-01f, -7.28755272e-02f, 1.82364187e-01f, 8.74240284e-02f}},
  550. {{1.57892225e-01f, 7.28944768e-02f, 1.82363474e-01f, -8.74301086e-02f}},
  551. {{8.80892603e-02f, -7.48948724e-02f, 9.08779842e-02f, -6.22480443e-02f}},
  552. }}
  553. };
  554. constexpr DecoderConfig<DualBand, 14> X7144Config{
  555. 1, true, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight, TopBackLeft, TopFrontLeft, TopFrontRight, TopBackRight, BottomBackLeft, BottomFrontLeft, BottomFrontRight, BottomBackRight}},
  556. DevAmbiScaling::N3D,
  557. /*HF*/{{2.64575131e+0f, 1.52752523e+0f}},
  558. {{
  559. {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
  560. {{7.14285714e-02f, 1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
  561. {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
  562. {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
  563. {{7.14285714e-02f, -1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
  564. {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
  565. {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, -5.88235294e-02f}},
  566. {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
  567. {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
  568. {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, -5.88235294e-02f}},
  569. {{7.14285714e-02f, 5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
  570. {{7.14285714e-02f, 5.88235294e-02f, -1.25000000e-01f, 5.88235294e-02f}},
  571. {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, 5.88235294e-02f}},
  572. {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
  573. }},
  574. /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
  575. {{
  576. {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
  577. {{7.14285714e-02f, 1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
  578. {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
  579. {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
  580. {{7.14285714e-02f, -1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
  581. {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
  582. {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, -5.88235294e-02f}},
  583. {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
  584. {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
  585. {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, -5.88235294e-02f}},
  586. {{7.14285714e-02f, 5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
  587. {{7.14285714e-02f, 5.88235294e-02f, -1.25000000e-01f, 5.88235294e-02f}},
  588. {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, 5.88235294e-02f}},
  589. {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
  590. }}
  591. };
  592. void InitPanning(ALCdevice *device, const bool hqdec=false, const bool stablize=false,
  593. DecoderView decoder={})
  594. {
  595. if(!decoder)
  596. {
  597. switch(device->FmtChans)
  598. {
  599. case DevFmtMono: decoder = MonoConfig; break;
  600. case DevFmtStereo: decoder = StereoConfig; break;
  601. case DevFmtQuad: decoder = QuadConfig; break;
  602. case DevFmtX51: decoder = X51Config; break;
  603. case DevFmtX61: decoder = X61Config; break;
  604. case DevFmtX71: decoder = X71Config; break;
  605. case DevFmtX714: decoder = X714Config; break;
  606. case DevFmtX7144: decoder = X7144Config; break;
  607. case DevFmtX3D71: decoder = X3D71Config; break;
  608. case DevFmtAmbi3D:
  609. /* For DevFmtAmbi3D, the ambisonic order is already set. */
  610. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  611. const auto acnmap = GetAmbiLayout(device->mAmbiLayout).first(count);
  612. const auto n3dscale = GetAmbiScales(device->mAmbiScale);
  613. std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
  614. [n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
  615. { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
  616. AllocChannels(device, count, 0);
  617. device->m2DMixing = false;
  618. float avg_dist{};
  619. if(auto distopt = device->configValue<float>("decoder", "speaker-dist"))
  620. avg_dist = *distopt;
  621. else if(auto delayopt = device->configValue<float>("decoder", "nfc-ref-delay"))
  622. {
  623. WARN("nfc-ref-delay is deprecated, use speaker-dist instead\n");
  624. avg_dist = *delayopt * SpeedOfSoundMetersPerSec;
  625. }
  626. InitNearFieldCtrl(device, avg_dist, device->mAmbiOrder, true);
  627. return;
  628. }
  629. }
  630. const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
  631. Ambi2DChannelsFromOrder(decoder.mOrder)};
  632. const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
  633. std::vector<ChannelDec> chancoeffs, chancoeffslf;
  634. for(size_t i{0u};i < decoder.mChannels.size();++i)
  635. {
  636. const size_t idx{device->channelIdxByName(decoder.mChannels[i])};
  637. if(idx == InvalidChannelIndex)
  638. {
  639. ERR("Failed to find %s channel in device\n",
  640. GetLabelFromChannel(decoder.mChannels[i]));
  641. continue;
  642. }
  643. auto ordermap = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::OrderFromChannel}
  644. : al::span<const uint8_t>{AmbiIndex::OrderFrom2DChannel};
  645. chancoeffs.resize(std::max(chancoeffs.size(), idx+1_zu), ChannelDec{});
  646. al::span<const float,MaxAmbiChannels> src{decoder.mCoeffs[i]};
  647. al::span<float,MaxAmbiChannels> dst{chancoeffs[idx]};
  648. for(size_t ambichan{0};ambichan < ambicount;++ambichan)
  649. dst[ambichan] = src[ambichan] * decoder.mOrderGain[ordermap[ambichan]];
  650. if(!dual_band)
  651. continue;
  652. chancoeffslf.resize(std::max(chancoeffslf.size(), idx+1_zu), ChannelDec{});
  653. src = decoder.mCoeffsLF[i];
  654. dst = chancoeffslf[idx];
  655. for(size_t ambichan{0};ambichan < ambicount;++ambichan)
  656. dst[ambichan] = src[ambichan] * decoder.mOrderGainLF[ordermap[ambichan]];
  657. }
  658. /* For non-DevFmtAmbi3D, set the ambisonic order. */
  659. device->mAmbiOrder = decoder.mOrder;
  660. device->m2DMixing = !decoder.mIs3D;
  661. const auto acnmap = decoder.mIs3D ? al::span{AmbiIndex::FromACN}.first(ambicount)
  662. : al::span{AmbiIndex::FromACN2D}.first(ambicount);
  663. const auto coeffscale = GetAmbiScales(decoder.mScaling);
  664. std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
  665. [coeffscale](const uint8_t &acn) noexcept
  666. { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
  667. AllocChannels(device, ambicount, device->channelsFromFmt());
  668. std::unique_ptr<FrontStablizer> stablizer;
  669. if(stablize)
  670. {
  671. /* Only enable the stablizer if the decoder does not output to the
  672. * front-center channel.
  673. */
  674. const size_t cidx{device->RealOut.ChannelIndex[FrontCenter]};
  675. bool hasfc{false};
  676. if(cidx < chancoeffs.size())
  677. {
  678. for(const auto &coeff : chancoeffs[cidx])
  679. hasfc |= coeff != 0.0f;
  680. }
  681. if(!hasfc && cidx < chancoeffslf.size())
  682. {
  683. for(const auto &coeff : chancoeffslf[cidx])
  684. hasfc |= coeff != 0.0f;
  685. }
  686. if(!hasfc)
  687. {
  688. stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
  689. TRACE("Front stablizer enabled\n");
  690. }
  691. }
  692. TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
  693. !dual_band ? "single" : "dual",
  694. (decoder.mOrder > 3) ? "fourth" :
  695. (decoder.mOrder > 2) ? "third" :
  696. (decoder.mOrder > 1) ? "second" : "first",
  697. decoder.mIs3D ? " periphonic" : "");
  698. device->AmbiDecoder = BFormatDec::Create(ambicount, chancoeffs, chancoeffslf,
  699. device->mXOverFreq/static_cast<float>(device->Frequency), std::move(stablizer));
  700. }
  701. void InitHrtfPanning(ALCdevice *device)
  702. {
  703. static constexpr float Deg180{al::numbers::pi_v<float>};
  704. static constexpr float Deg_90{Deg180 / 2.0f /* 90 degrees*/};
  705. static constexpr float Deg_45{Deg_90 / 2.0f /* 45 degrees*/};
  706. static constexpr float Deg135{Deg_45 * 3.0f /*135 degrees*/};
  707. static constexpr float Deg_21{3.648638281e-01f /* 20~ 21 degrees*/};
  708. static constexpr float Deg_32{5.535743589e-01f /* 31~ 32 degrees*/};
  709. static constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
  710. static constexpr float Deg_58{1.017221968e+00f /* 58~ 59 degrees*/};
  711. static constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
  712. static constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
  713. static constexpr float Deg122{2.124370686e+00f /*121~122 degrees*/};
  714. static constexpr std::array AmbiPoints1O{
  715. AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
  716. AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
  717. AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
  718. AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
  719. AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
  720. AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
  721. AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
  722. AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
  723. };
  724. static constexpr std::array AmbiPoints2O{
  725. AngularPoint{EvRadians{-Deg_32}, AzRadians{ 0.0f}},
  726. AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_58}},
  727. AngularPoint{EvRadians{ Deg_58}, AzRadians{ Deg_90}},
  728. AngularPoint{EvRadians{ Deg_32}, AzRadians{ 0.0f}},
  729. AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg122}},
  730. AngularPoint{EvRadians{-Deg_58}, AzRadians{-Deg_90}},
  731. AngularPoint{EvRadians{-Deg_32}, AzRadians{ Deg180}},
  732. AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg122}},
  733. AngularPoint{EvRadians{ Deg_58}, AzRadians{-Deg_90}},
  734. AngularPoint{EvRadians{ Deg_32}, AzRadians{ Deg180}},
  735. AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_58}},
  736. AngularPoint{EvRadians{-Deg_58}, AzRadians{ Deg_90}},
  737. };
  738. static constexpr std::array AmbiPoints3O{
  739. AngularPoint{EvRadians{ Deg_69}, AzRadians{-Deg_90}},
  740. AngularPoint{EvRadians{ Deg_69}, AzRadians{ Deg_90}},
  741. AngularPoint{EvRadians{-Deg_69}, AzRadians{-Deg_90}},
  742. AngularPoint{EvRadians{-Deg_69}, AzRadians{ Deg_90}},
  743. AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_69}},
  744. AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg111}},
  745. AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_69}},
  746. AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg111}},
  747. AngularPoint{EvRadians{ Deg_21}, AzRadians{ 0.0f}},
  748. AngularPoint{EvRadians{ Deg_21}, AzRadians{ Deg180}},
  749. AngularPoint{EvRadians{-Deg_21}, AzRadians{ 0.0f}},
  750. AngularPoint{EvRadians{-Deg_21}, AzRadians{ Deg180}},
  751. AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
  752. AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
  753. AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
  754. AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
  755. AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
  756. AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
  757. AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
  758. AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
  759. };
  760. static constexpr std::array AmbiMatrix1O{
  761. ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f},
  762. ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f},
  763. ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f},
  764. ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f},
  765. ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f},
  766. ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f},
  767. ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f},
  768. ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f},
  769. };
  770. static constexpr std::array AmbiMatrix2O{
  771. ChannelCoeffs{8.333333333e-02f, 0.000000000e+00f, -7.588274978e-02f, 1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, -1.591525047e-02f, -1.443375673e-01f, 1.167715449e-01f},
  772. ChannelCoeffs{8.333333333e-02f, -1.227808683e-01f, 0.000000000e+00f, 7.588274978e-02f, -1.443375673e-01f, 0.000000000e+00f, -9.316949906e-02f, 0.000000000e+00f, -7.216878365e-02f},
  773. ChannelCoeffs{8.333333333e-02f, -7.588274978e-02f, 1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, -1.443375673e-01f, 1.090847495e-01f, 0.000000000e+00f, -4.460276122e-02f},
  774. ChannelCoeffs{8.333333333e-02f, 0.000000000e+00f, 7.588274978e-02f, 1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, -1.591525047e-02f, 1.443375673e-01f, 1.167715449e-01f},
  775. ChannelCoeffs{8.333333333e-02f, -1.227808683e-01f, 0.000000000e+00f, -7.588274978e-02f, 1.443375673e-01f, 0.000000000e+00f, -9.316949906e-02f, 0.000000000e+00f, -7.216878365e-02f},
  776. ChannelCoeffs{8.333333333e-02f, 7.588274978e-02f, -1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, -1.443375673e-01f, 1.090847495e-01f, 0.000000000e+00f, -4.460276122e-02f},
  777. ChannelCoeffs{8.333333333e-02f, 0.000000000e+00f, -7.588274978e-02f, -1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, -1.591525047e-02f, 1.443375673e-01f, 1.167715449e-01f},
  778. ChannelCoeffs{8.333333333e-02f, 1.227808683e-01f, 0.000000000e+00f, -7.588274978e-02f, -1.443375673e-01f, 0.000000000e+00f, -9.316949906e-02f, 0.000000000e+00f, -7.216878365e-02f},
  779. ChannelCoeffs{8.333333333e-02f, 7.588274978e-02f, 1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, 1.443375673e-01f, 1.090847495e-01f, 0.000000000e+00f, -4.460276122e-02f},
  780. ChannelCoeffs{8.333333333e-02f, 0.000000000e+00f, 7.588274978e-02f, -1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, -1.591525047e-02f, -1.443375673e-01f, 1.167715449e-01f},
  781. ChannelCoeffs{8.333333333e-02f, 1.227808683e-01f, 0.000000000e+00f, 7.588274978e-02f, 1.443375673e-01f, 0.000000000e+00f, -9.316949906e-02f, 0.000000000e+00f, -7.216878365e-02f},
  782. ChannelCoeffs{8.333333333e-02f, -7.588274978e-02f, -1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, 1.443375673e-01f, 1.090847495e-01f, 0.000000000e+00f, -4.460276122e-02f},
  783. };
  784. static constexpr std::array AmbiMatrix3O{
  785. ChannelCoeffs{5.000000000e-02f, 3.090169944e-02f, 8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, 6.454972244e-02f, 9.045084972e-02f, 0.000000000e+00f, -1.232790000e-02f, -1.256118221e-01f, 0.000000000e+00f, 1.126112056e-01f, 7.944389175e-02f, 0.000000000e+00f, 2.421151497e-02f, 0.000000000e+00f},
  786. ChannelCoeffs{5.000000000e-02f, -3.090169944e-02f, 8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, -6.454972244e-02f, 9.045084972e-02f, 0.000000000e+00f, -1.232790000e-02f, 1.256118221e-01f, 0.000000000e+00f, -1.126112056e-01f, 7.944389175e-02f, 0.000000000e+00f, 2.421151497e-02f, 0.000000000e+00f},
  787. ChannelCoeffs{5.000000000e-02f, 3.090169944e-02f, -8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, -6.454972244e-02f, 9.045084972e-02f, 0.000000000e+00f, -1.232790000e-02f, -1.256118221e-01f, 0.000000000e+00f, 1.126112056e-01f, -7.944389175e-02f, 0.000000000e+00f, -2.421151497e-02f, 0.000000000e+00f},
  788. ChannelCoeffs{5.000000000e-02f, -3.090169944e-02f, -8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, 6.454972244e-02f, 9.045084972e-02f, 0.000000000e+00f, -1.232790000e-02f, 1.256118221e-01f, 0.000000000e+00f, -1.126112056e-01f, -7.944389175e-02f, 0.000000000e+00f, -2.421151497e-02f, 0.000000000e+00f},
  789. ChannelCoeffs{5.000000000e-02f, 8.090169944e-02f, 0.000000000e+00f, 3.090169944e-02f, 6.454972244e-02f, 0.000000000e+00f, -5.590169944e-02f, 0.000000000e+00f, -7.216878365e-02f, -7.763237543e-02f, 0.000000000e+00f, -2.950836627e-02f, 0.000000000e+00f, -1.497759251e-01f, 0.000000000e+00f, -7.763237543e-02f},
  790. ChannelCoeffs{5.000000000e-02f, 8.090169944e-02f, 0.000000000e+00f, -3.090169944e-02f, -6.454972244e-02f, 0.000000000e+00f, -5.590169944e-02f, 0.000000000e+00f, -7.216878365e-02f, -7.763237543e-02f, 0.000000000e+00f, -2.950836627e-02f, 0.000000000e+00f, 1.497759251e-01f, 0.000000000e+00f, 7.763237543e-02f},
  791. ChannelCoeffs{5.000000000e-02f, -8.090169944e-02f, 0.000000000e+00f, 3.090169944e-02f, -6.454972244e-02f, 0.000000000e+00f, -5.590169944e-02f, 0.000000000e+00f, -7.216878365e-02f, 7.763237543e-02f, 0.000000000e+00f, 2.950836627e-02f, 0.000000000e+00f, -1.497759251e-01f, 0.000000000e+00f, -7.763237543e-02f},
  792. ChannelCoeffs{5.000000000e-02f, -8.090169944e-02f, 0.000000000e+00f, -3.090169944e-02f, 6.454972244e-02f, 0.000000000e+00f, -5.590169944e-02f, 0.000000000e+00f, -7.216878365e-02f, 7.763237543e-02f, 0.000000000e+00f, 2.950836627e-02f, 0.000000000e+00f, 1.497759251e-01f, 0.000000000e+00f, 7.763237543e-02f},
  793. ChannelCoeffs{5.000000000e-02f, 0.000000000e+00f, 3.090169944e-02f, 8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, -3.454915028e-02f, 6.454972244e-02f, 8.449668365e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 3.034486645e-02f, -6.779013272e-02f, 1.659481923e-01f, 4.797944664e-02f},
  794. ChannelCoeffs{5.000000000e-02f, 0.000000000e+00f, 3.090169944e-02f, -8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, -3.454915028e-02f, -6.454972244e-02f, 8.449668365e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 3.034486645e-02f, 6.779013272e-02f, 1.659481923e-01f, -4.797944664e-02f},
  795. ChannelCoeffs{5.000000000e-02f, 0.000000000e+00f, -3.090169944e-02f, 8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, -3.454915028e-02f, -6.454972244e-02f, 8.449668365e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -3.034486645e-02f, -6.779013272e-02f, -1.659481923e-01f, 4.797944664e-02f},
  796. ChannelCoeffs{5.000000000e-02f, 0.000000000e+00f, -3.090169944e-02f, -8.090169944e-02f, 0.000000000e+00f, 0.000000000e+00f, -3.454915028e-02f, 6.454972244e-02f, 8.449668365e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -3.034486645e-02f, 6.779013272e-02f, -1.659481923e-01f, -4.797944664e-02f},
  797. ChannelCoeffs{5.000000000e-02f, 5.000000000e-02f, 5.000000000e-02f, 5.000000000e-02f, 6.454972244e-02f, 6.454972244e-02f, 0.000000000e+00f, 6.454972244e-02f, 0.000000000e+00f, 1.016220987e-01f, 6.338656910e-02f, -1.092600649e-02f, -7.364853795e-02f, 1.011266756e-01f, -7.086833869e-02f, -1.482646439e-02f},
  798. ChannelCoeffs{5.000000000e-02f, 5.000000000e-02f, 5.000000000e-02f, -5.000000000e-02f, -6.454972244e-02f, 6.454972244e-02f, 0.000000000e+00f, -6.454972244e-02f, 0.000000000e+00f, 1.016220987e-01f, -6.338656910e-02f, -1.092600649e-02f, -7.364853795e-02f, -1.011266756e-01f, -7.086833869e-02f, 1.482646439e-02f},
  799. ChannelCoeffs{5.000000000e-02f, -5.000000000e-02f, 5.000000000e-02f, 5.000000000e-02f, -6.454972244e-02f, -6.454972244e-02f, 0.000000000e+00f, 6.454972244e-02f, 0.000000000e+00f, -1.016220987e-01f, -6.338656910e-02f, 1.092600649e-02f, -7.364853795e-02f, 1.011266756e-01f, -7.086833869e-02f, -1.482646439e-02f},
  800. ChannelCoeffs{5.000000000e-02f, -5.000000000e-02f, 5.000000000e-02f, -5.000000000e-02f, 6.454972244e-02f, -6.454972244e-02f, 0.000000000e+00f, -6.454972244e-02f, 0.000000000e+00f, -1.016220987e-01f, 6.338656910e-02f, 1.092600649e-02f, -7.364853795e-02f, -1.011266756e-01f, -7.086833869e-02f, 1.482646439e-02f},
  801. ChannelCoeffs{5.000000000e-02f, 5.000000000e-02f, -5.000000000e-02f, 5.000000000e-02f, 6.454972244e-02f, -6.454972244e-02f, 0.000000000e+00f, -6.454972244e-02f, 0.000000000e+00f, 1.016220987e-01f, -6.338656910e-02f, -1.092600649e-02f, 7.364853795e-02f, 1.011266756e-01f, 7.086833869e-02f, -1.482646439e-02f},
  802. ChannelCoeffs{5.000000000e-02f, 5.000000000e-02f, -5.000000000e-02f, -5.000000000e-02f, -6.454972244e-02f, -6.454972244e-02f, 0.000000000e+00f, 6.454972244e-02f, 0.000000000e+00f, 1.016220987e-01f, 6.338656910e-02f, -1.092600649e-02f, 7.364853795e-02f, -1.011266756e-01f, 7.086833869e-02f, 1.482646439e-02f},
  803. ChannelCoeffs{5.000000000e-02f, -5.000000000e-02f, -5.000000000e-02f, 5.000000000e-02f, -6.454972244e-02f, 6.454972244e-02f, 0.000000000e+00f, -6.454972244e-02f, 0.000000000e+00f, -1.016220987e-01f, 6.338656910e-02f, 1.092600649e-02f, 7.364853795e-02f, 1.011266756e-01f, 7.086833869e-02f, -1.482646439e-02f},
  804. ChannelCoeffs{5.000000000e-02f, -5.000000000e-02f, -5.000000000e-02f, -5.000000000e-02f, 6.454972244e-02f, 6.454972244e-02f, 0.000000000e+00f, 6.454972244e-02f, 0.000000000e+00f, -1.016220987e-01f, -6.338656910e-02f, 1.092600649e-02f, 7.364853795e-02f, -1.011266756e-01f, 7.086833869e-02f, 1.482646439e-02f},
  805. };
  806. static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain1O{
  807. /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
  808. };
  809. static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain2O{
  810. /*ENRGY*/ 1.825741858e+00f, 1.414213562e+00f, 7.302967433e-01f
  811. /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
  812. /*RMS 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f*/
  813. };
  814. static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain3O{
  815. /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
  816. /*AMP*/ 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f
  817. /*RMS 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f*/
  818. };
  819. static_assert(AmbiPoints1O.size() == AmbiMatrix1O.size(), "First-Order Ambisonic HRTF mismatch");
  820. static_assert(AmbiPoints2O.size() == AmbiMatrix2O.size(), "Second-Order Ambisonic HRTF mismatch");
  821. static_assert(AmbiPoints3O.size() == AmbiMatrix3O.size(), "Third-Order Ambisonic HRTF mismatch");
  822. /* A 700hz crossover frequency provides tighter sound imaging at the sweet
  823. * spot with ambisonic decoding, as the distance between the ears is closer
  824. * to half this frequency wavelength, which is the optimal point where the
  825. * response should change between optimizing phase vs volume. Normally this
  826. * tighter imaging is at the cost of a smaller sweet spot, but since the
  827. * listener is fixed in the center of the HRTF responses for the decoder,
  828. * we don't have to worry about ever being out of the sweet spot.
  829. *
  830. * A better option here may be to have the head radius as part of the HRTF
  831. * data set and calculate the optimal crossover frequency from that.
  832. */
  833. device->mXOverFreq = 700.0f;
  834. /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
  835. * and it eases the CPU/memory load.
  836. */
  837. device->mRenderMode = RenderMode::Hrtf;
  838. uint ambi_order{1};
  839. if(auto modeopt = device->configValue<std::string>({}, "hrtf-mode"))
  840. {
  841. struct HrtfModeEntry {
  842. std::string_view name;
  843. RenderMode mode;
  844. uint order;
  845. };
  846. constexpr std::array hrtf_modes{
  847. HrtfModeEntry{"full"sv, RenderMode::Hrtf, 1},
  848. HrtfModeEntry{"ambi1"sv, RenderMode::Normal, 1},
  849. HrtfModeEntry{"ambi2"sv, RenderMode::Normal, 2},
  850. HrtfModeEntry{"ambi3"sv, RenderMode::Normal, 3},
  851. };
  852. std::string_view mode{*modeopt};
  853. if(al::case_compare(mode, "basic"sv) == 0)
  854. {
  855. ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", modeopt->c_str(), "ambi2");
  856. mode = "ambi2";
  857. }
  858. auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
  859. { return al::case_compare(mode, entry.name) == 0; };
  860. auto iter = std::find_if(hrtf_modes.begin(), hrtf_modes.end(), match_entry);
  861. if(iter == hrtf_modes.end())
  862. ERR("Unexpected hrtf-mode: %s\n", modeopt->c_str());
  863. else
  864. {
  865. device->mRenderMode = iter->mode;
  866. ambi_order = iter->order;
  867. }
  868. }
  869. TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
  870. GetCounterSuffix(ambi_order), (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
  871. device->mHrtfName.c_str());
  872. bool perHrirMin{false};
  873. auto AmbiPoints = al::span{AmbiPoints1O}.subspan(0);
  874. auto AmbiMatrix = al::span{AmbiMatrix1O}.subspan(0);
  875. auto AmbiOrderHFGain = al::span{AmbiOrderHFGain1O};
  876. if(ambi_order >= 3)
  877. {
  878. perHrirMin = true;
  879. AmbiPoints = AmbiPoints3O;
  880. AmbiMatrix = AmbiMatrix3O;
  881. AmbiOrderHFGain = AmbiOrderHFGain3O;
  882. }
  883. else if(ambi_order == 2)
  884. {
  885. AmbiPoints = AmbiPoints2O;
  886. AmbiMatrix = AmbiMatrix2O;
  887. AmbiOrderHFGain = AmbiOrderHFGain2O;
  888. }
  889. device->mAmbiOrder = ambi_order;
  890. device->m2DMixing = false;
  891. const size_t count{AmbiChannelsFromOrder(ambi_order)};
  892. const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
  893. std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
  894. [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; });
  895. AllocChannels(device, count, device->channelsFromFmt());
  896. HrtfStore *Hrtf{device->mHrtf.get()};
  897. auto hrtfstate = DirectHrtfState::Create(count);
  898. hrtfstate->build(Hrtf, device->mIrSize, perHrirMin, AmbiPoints, AmbiMatrix, device->mXOverFreq,
  899. AmbiOrderHFGain);
  900. device->mHrtfState = std::move(hrtfstate);
  901. InitNearFieldCtrl(device, Hrtf->mFields[0].distance, ambi_order, true);
  902. }
  903. void InitUhjPanning(ALCdevice *device)
  904. {
  905. /* UHJ is always 2D first-order. */
  906. static constexpr size_t count{Ambi2DChannelsFromOrder(1)};
  907. device->mAmbiOrder = 1;
  908. device->m2DMixing = true;
  909. const auto acnmap = al::span{AmbiIndex::FromFuMa2D}.first<count>();
  910. std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
  911. [](const uint8_t &acn) noexcept -> BFChannelConfig
  912. { return BFChannelConfig{1.0f/AmbiScale::FromUHJ[acn], acn}; });
  913. AllocChannels(device, count, device->channelsFromFmt());
  914. }
  915. } // namespace
  916. void aluInitRenderer(ALCdevice *device, int hrtf_id, std::optional<StereoEncoding> stereomode)
  917. {
  918. /* Hold the HRTF the device last used, in case it's used again. */
  919. HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
  920. device->mHrtfState = nullptr;
  921. device->mHrtf = nullptr;
  922. device->mIrSize = 0;
  923. device->mHrtfName.clear();
  924. device->mXOverFreq = 400.0f;
  925. device->m2DMixing = false;
  926. device->mRenderMode = RenderMode::Normal;
  927. if(device->FmtChans != DevFmtStereo)
  928. {
  929. old_hrtf = nullptr;
  930. if(stereomode && *stereomode == StereoEncoding::Hrtf)
  931. device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  932. const char *layout{nullptr};
  933. switch(device->FmtChans)
  934. {
  935. case DevFmtQuad: layout = "quad"; break;
  936. case DevFmtX51: layout = "surround51"; break;
  937. case DevFmtX61: layout = "surround61"; break;
  938. case DevFmtX71: layout = "surround71"; break;
  939. case DevFmtX714: layout = "surround714"; break;
  940. case DevFmtX7144: layout = "surround7144"; break;
  941. case DevFmtX3D71: layout = "surround3d71"; break;
  942. /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
  943. case DevFmtMono:
  944. case DevFmtStereo:
  945. case DevFmtAmbi3D:
  946. break;
  947. }
  948. std::unique_ptr<DecoderConfig<DualBand,MaxOutputChannels>> decoder_store;
  949. DecoderView decoder{};
  950. std::array<float,MaxOutputChannels> speakerdists{};
  951. auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
  952. {
  953. AmbDecConf conf{};
  954. if(auto err = conf.load(config))
  955. {
  956. ERR("Failed to load layout file %s\n", config);
  957. ERR(" %s\n", err->c_str());
  958. return false;
  959. }
  960. if(conf.Speakers.size() > MaxOutputChannels)
  961. {
  962. ERR("Unsupported decoder speaker count %zu (max %zu)\n", conf.Speakers.size(),
  963. MaxOutputChannels);
  964. return false;
  965. }
  966. if(conf.ChanMask > Ambi3OrderMask)
  967. {
  968. ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
  969. Ambi3OrderMask);
  970. return false;
  971. }
  972. TRACE("Using %s decoder: \"%s\"\n", DevFmtChannelsString(device->FmtChans),
  973. conf.Description.c_str());
  974. device->mXOverFreq = std::clamp(conf.XOverFreq, 100.0f, 1000.0f);
  975. decoder_store = std::make_unique<DecoderConfig<DualBand,MaxOutputChannels>>();
  976. decoder = MakeDecoderView(device, &conf, *decoder_store);
  977. const auto confspeakers = al::span{std::as_const(conf.Speakers)}
  978. .first(decoder.mChannels.size());
  979. std::transform(confspeakers.cbegin(), confspeakers.cend(), speakerdists.begin(),
  980. std::mem_fn(&AmbDecConf::SpeakerConf::Distance));
  981. return true;
  982. };
  983. bool usingCustom{false};
  984. if(layout)
  985. {
  986. if(auto decopt = device->configValue<std::string>("decoder", layout))
  987. usingCustom = load_config(decopt->c_str());
  988. }
  989. if(!usingCustom && device->FmtChans != DevFmtAmbi3D)
  990. TRACE("Using built-in %s decoder\n", DevFmtChannelsString(device->FmtChans));
  991. /* Enable the stablizer only for formats that have front-left, front-
  992. * right, and front-center outputs.
  993. */
  994. const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != InvalidChannelIndex
  995. && device->RealOut.ChannelIndex[FrontLeft] != InvalidChannelIndex
  996. && device->RealOut.ChannelIndex[FrontRight] != InvalidChannelIndex
  997. && device->getConfigValueBool({}, "front-stablizer", false)};
  998. const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", true)};
  999. InitPanning(device, hqdec, stablize, decoder);
  1000. if(decoder)
  1001. {
  1002. float accum_dist{0.0f}, spkr_count{0.0f};
  1003. for(auto dist : speakerdists)
  1004. {
  1005. if(dist > 0.0f)
  1006. {
  1007. accum_dist += dist;
  1008. spkr_count += 1.0f;
  1009. }
  1010. }
  1011. const float avg_dist{(accum_dist > 0.0f && spkr_count > 0) ? accum_dist/spkr_count :
  1012. device->configValue<float>("decoder", "speaker-dist").value_or(1.0f)};
  1013. InitNearFieldCtrl(device, avg_dist, decoder.mOrder, decoder.mIs3D);
  1014. if(spkr_count > 0)
  1015. InitDistanceComp(device, decoder.mChannels, speakerdists);
  1016. }
  1017. if(auto *ambidec{device->AmbiDecoder.get()})
  1018. {
  1019. device->PostProcess = ambidec->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
  1020. : &ALCdevice::ProcessAmbiDec;
  1021. }
  1022. return;
  1023. }
  1024. /* If HRTF is explicitly requested, or if there's no explicit request and
  1025. * the device is headphones, try to enable it.
  1026. */
  1027. if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Hrtf
  1028. || (!stereomode && device->Flags.test(DirectEar)))
  1029. {
  1030. if(device->mHrtfList.empty())
  1031. device->enumerateHrtfs();
  1032. if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
  1033. {
  1034. const std::string_view hrtfname{device->mHrtfList[static_cast<uint>(hrtf_id)]};
  1035. if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
  1036. {
  1037. device->mHrtf = std::move(hrtf);
  1038. device->mHrtfName = hrtfname;
  1039. }
  1040. }
  1041. if(!device->mHrtf)
  1042. {
  1043. for(const std::string_view hrtfname : device->mHrtfList)
  1044. {
  1045. if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
  1046. {
  1047. device->mHrtf = std::move(hrtf);
  1048. device->mHrtfName = hrtfname;
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. if(device->mHrtf)
  1054. {
  1055. old_hrtf = nullptr;
  1056. HrtfStore *hrtf{device->mHrtf.get()};
  1057. device->mIrSize = hrtf->mIrSize;
  1058. if(auto hrtfsizeopt = device->configValue<uint>({}, "hrtf-size"))
  1059. {
  1060. if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
  1061. device->mIrSize = std::max(*hrtfsizeopt, MinIrLength);
  1062. }
  1063. InitHrtfPanning(device);
  1064. device->PostProcess = &ALCdevice::ProcessHrtf;
  1065. device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
  1066. return;
  1067. }
  1068. }
  1069. old_hrtf = nullptr;
  1070. if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Uhj)
  1071. {
  1072. switch(UhjEncodeQuality)
  1073. {
  1074. case UhjQualityType::IIR:
  1075. device->mUhjEncoder = std::make_unique<UhjEncoderIIR>();
  1076. break;
  1077. case UhjQualityType::FIR256:
  1078. device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength256>>();
  1079. break;
  1080. case UhjQualityType::FIR512:
  1081. device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength512>>();
  1082. break;
  1083. }
  1084. assert(device->mUhjEncoder != nullptr);
  1085. TRACE("UHJ enabled\n");
  1086. InitUhjPanning(device);
  1087. device->PostProcess = &ALCdevice::ProcessUhj;
  1088. return;
  1089. }
  1090. device->mRenderMode = RenderMode::Pairwise;
  1091. if(device->Type != DeviceType::Loopback)
  1092. {
  1093. if(auto cflevopt = device->configValue<int>({}, "cf_level"))
  1094. {
  1095. if(*cflevopt > 0 && *cflevopt <= 6)
  1096. {
  1097. auto bs2b = std::make_unique<Bs2b::bs2b>();
  1098. bs2b->set_params(*cflevopt, static_cast<int>(device->Frequency));
  1099. device->Bs2b = std::move(bs2b);
  1100. TRACE("BS2B enabled\n");
  1101. InitPanning(device);
  1102. device->PostProcess = &ALCdevice::ProcessBs2b;
  1103. return;
  1104. }
  1105. }
  1106. }
  1107. TRACE("Stereo rendering\n");
  1108. InitPanning(device);
  1109. device->PostProcess = &ALCdevice::ProcessAmbiDec;
  1110. }
  1111. void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
  1112. {
  1113. DeviceBase *device{context->mDevice};
  1114. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  1115. slot->mWetBuffer.resize(count);
  1116. const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
  1117. const auto iter = std::transform(acnmap.cbegin(), acnmap.cend(), slot->Wet.AmbiMap.begin(),
  1118. [](const uint8_t &acn) noexcept -> BFChannelConfig { return BFChannelConfig{1.0f, acn}; });
  1119. std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
  1120. slot->Wet.Buffer = slot->mWetBuffer;
  1121. }