panning.cpp 59 KB

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