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

1119 lines
50 KiB

  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2010 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <algorithm>
  22. #include <array>
  23. #include <chrono>
  24. #include <cmath>
  25. #include <cstdio>
  26. #include <cstring>
  27. #include <functional>
  28. #include <iterator>
  29. #include <memory>
  30. #include <new>
  31. #include <numeric>
  32. #include <string>
  33. #include "AL/al.h"
  34. #include "AL/alc.h"
  35. #include "AL/alext.h"
  36. #include "al/auxeffectslot.h"
  37. #include "albit.h"
  38. #include "alconfig.h"
  39. #include "alc/context.h"
  40. #include "almalloc.h"
  41. #include "alnumbers.h"
  42. #include "alnumeric.h"
  43. #include "aloptional.h"
  44. #include "alspan.h"
  45. #include "alstring.h"
  46. #include "alu.h"
  47. #include "core/ambdec.h"
  48. #include "core/ambidefs.h"
  49. #include "core/bformatdec.h"
  50. #include "core/bs2b.h"
  51. #include "core/devformat.h"
  52. #include "core/front_stablizer.h"
  53. #include "core/hrtf.h"
  54. #include "core/logging.h"
  55. #include "core/uhjfilter.h"
  56. #include "device.h"
  57. #include "opthelpers.h"
  58. namespace {
  59. using namespace std::placeholders;
  60. using std::chrono::seconds;
  61. using std::chrono::nanoseconds;
  62. inline const char *GetLabelFromChannel(Channel channel)
  63. {
  64. switch(channel)
  65. {
  66. case FrontLeft: return "front-left";
  67. case FrontRight: return "front-right";
  68. case FrontCenter: return "front-center";
  69. case LFE: return "lfe";
  70. case BackLeft: return "back-left";
  71. case BackRight: return "back-right";
  72. case BackCenter: return "back-center";
  73. case SideLeft: return "side-left";
  74. case SideRight: return "side-right";
  75. case TopFrontLeft: return "top-front-left";
  76. case TopFrontCenter: return "top-front-center";
  77. case TopFrontRight: return "top-front-right";
  78. case TopCenter: return "top-center";
  79. case TopBackLeft: return "top-back-left";
  80. case TopBackCenter: return "top-back-center";
  81. case TopBackRight: return "top-back-right";
  82. case MaxChannels: break;
  83. }
  84. return "(unknown)";
  85. }
  86. std::unique_ptr<FrontStablizer> CreateStablizer(const size_t outchans, const uint srate)
  87. {
  88. auto stablizer = FrontStablizer::Create(outchans);
  89. for(auto &buf : stablizer->DelayBuf)
  90. std::fill(buf.begin(), buf.end(), 0.0f);
  91. /* Initialize band-splitting filter for the mid signal, with a crossover at
  92. * 5khz (could be higher).
  93. */
  94. stablizer->MidFilter.init(5000.0f / static_cast<float>(srate));
  95. return stablizer;
  96. }
  97. void AllocChannels(ALCdevice *device, const size_t main_chans, const size_t real_chans)
  98. {
  99. TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans, real_chans);
  100. /* Allocate extra channels for any post-filter output. */
  101. const size_t num_chans{main_chans + real_chans};
  102. TRACE("Allocating %zu channels, %zu bytes\n", num_chans,
  103. num_chans*sizeof(device->MixBuffer[0]));
  104. device->MixBuffer.resize(num_chans);
  105. al::span<FloatBufferLine> buffer{device->MixBuffer};
  106. device->Dry.Buffer = buffer.first(main_chans);
  107. buffer = buffer.subspan(main_chans);
  108. if(real_chans != 0)
  109. {
  110. device->RealOut.Buffer = buffer.first(real_chans);
  111. buffer = buffer.subspan(real_chans);
  112. }
  113. else
  114. device->RealOut.Buffer = device->Dry.Buffer;
  115. }
  116. using ChannelCoeffs = std::array<float,MaxAmbiChannels>;
  117. enum DecoderMode : bool {
  118. SingleBand = false,
  119. DualBand = true
  120. };
  121. template<DecoderMode Mode, size_t N>
  122. struct DecoderConfig;
  123. template<size_t N>
  124. struct DecoderConfig<SingleBand, N> {
  125. uint8_t mOrder{};
  126. bool mIs3D{};
  127. std::array<Channel,N> mChannels{};
  128. DevAmbiScaling mScaling{};
  129. std::array<float,MaxAmbiOrder+1> mOrderGain{};
  130. std::array<ChannelCoeffs,N> mCoeffs{};
  131. };
  132. template<size_t N>
  133. struct DecoderConfig<DualBand, N> {
  134. uint8_t mOrder{};
  135. bool mIs3D{};
  136. std::array<Channel,N> mChannels{};
  137. DevAmbiScaling mScaling{};
  138. std::array<float,MaxAmbiOrder+1> mOrderGain{};
  139. std::array<ChannelCoeffs,N> mCoeffs{};
  140. std::array<float,MaxAmbiOrder+1> mOrderGainLF{};
  141. std::array<ChannelCoeffs,N> mCoeffsLF{};
  142. };
  143. template<>
  144. struct DecoderConfig<DualBand, 0> {
  145. uint8_t mOrder{};
  146. bool mIs3D{};
  147. al::span<const Channel> mChannels;
  148. DevAmbiScaling mScaling{};
  149. al::span<const float> mOrderGain;
  150. al::span<const ChannelCoeffs> mCoeffs;
  151. al::span<const float> mOrderGainLF;
  152. al::span<const ChannelCoeffs> mCoeffsLF;
  153. template<size_t N>
  154. DecoderConfig& operator=(const DecoderConfig<SingleBand,N> &rhs) noexcept
  155. {
  156. mOrder = rhs.mOrder;
  157. mIs3D = rhs.mIs3D;
  158. mChannels = rhs.mChannels;
  159. mScaling = rhs.mScaling;
  160. mOrderGain = rhs.mOrderGain;
  161. mCoeffs = rhs.mCoeffs;
  162. mOrderGainLF = {};
  163. mCoeffsLF = {};
  164. return *this;
  165. }
  166. template<size_t N>
  167. DecoderConfig& operator=(const DecoderConfig<DualBand,N> &rhs) noexcept
  168. {
  169. mOrder = rhs.mOrder;
  170. mIs3D = rhs.mIs3D;
  171. mChannels = rhs.mChannels;
  172. mScaling = rhs.mScaling;
  173. mOrderGain = rhs.mOrderGain;
  174. mCoeffs = rhs.mCoeffs;
  175. mOrderGainLF = rhs.mOrderGainLF;
  176. mCoeffsLF = rhs.mCoeffsLF;
  177. return *this;
  178. }
  179. };
  180. using DecoderView = DecoderConfig<DualBand, 0>;
  181. void InitNearFieldCtrl(ALCdevice *device, float ctrl_dist, uint order, bool is3d)
  182. {
  183. static const uint chans_per_order2d[MaxAmbiOrder+1]{ 1, 2, 2, 2 };
  184. static const uint chans_per_order3d[MaxAmbiOrder+1]{ 1, 3, 5, 7 };
  185. /* NFC is only used when AvgSpeakerDist is greater than 0. */
  186. if(!device->getConfigValueBool("decoder", "nfc", 0) || !(ctrl_dist > 0.0f))
  187. return;
  188. device->AvgSpeakerDist = clampf(ctrl_dist, 0.1f, 10.0f);
  189. TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
  190. const float w1{SpeedOfSoundMetersPerSec /
  191. (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
  192. device->mNFCtrlFilter.init(w1);
  193. auto iter = std::copy_n(is3d ? chans_per_order3d : chans_per_order2d, order+1u,
  194. std::begin(device->NumChannelsPerOrder));
  195. std::fill(iter, std::end(device->NumChannelsPerOrder), 0u);
  196. }
  197. void InitDistanceComp(ALCdevice *device, const al::span<const Channel> channels,
  198. const al::span<const float,MAX_OUTPUT_CHANNELS> dists)
  199. {
  200. const float maxdist{std::accumulate(std::begin(dists), std::end(dists), 0.0f, maxf)};
  201. if(!device->getConfigValueBool("decoder", "distance-comp", 1) || !(maxdist > 0.0f))
  202. return;
  203. const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
  204. std::vector<DistanceComp::ChanData> ChanDelay;
  205. ChanDelay.reserve(device->RealOut.Buffer.size());
  206. size_t total{0u};
  207. for(size_t chidx{0};chidx < channels.size();++chidx)
  208. {
  209. const Channel ch{channels[chidx]};
  210. const uint idx{device->RealOut.ChannelIndex[ch]};
  211. if(idx == INVALID_CHANNEL_INDEX)
  212. continue;
  213. const float distance{dists[chidx]};
  214. /* Distance compensation only delays in steps of the sample rate. This
  215. * is a bit less accurate since the delay time falls to the nearest
  216. * sample time, but it's far simpler as it doesn't have to deal with
  217. * phase offsets. This means at 48khz, for instance, the distance delay
  218. * will be in steps of about 7 millimeters.
  219. */
  220. float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
  221. if(delay > float{MAX_DELAY_LENGTH-1})
  222. {
  223. ERR("Delay for channel %u (%s) exceeds buffer length (%f > %d)\n", idx,
  224. GetLabelFromChannel(ch), delay, MAX_DELAY_LENGTH-1);
  225. delay = float{MAX_DELAY_LENGTH-1};
  226. }
  227. ChanDelay.resize(maxz(ChanDelay.size(), idx+1));
  228. ChanDelay[idx].Length = static_cast<uint>(delay);
  229. ChanDelay[idx].Gain = distance / maxdist;
  230. TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
  231. ChanDelay[idx].Length, ChanDelay[idx].Gain);
  232. /* Round up to the next 4th sample, so each channel buffer starts
  233. * 16-byte aligned.
  234. */
  235. total += RoundUp(ChanDelay[idx].Length, 4);
  236. }
  237. if(total > 0)
  238. {
  239. auto chandelays = DistanceComp::Create(total);
  240. ChanDelay[0].Buffer = chandelays->mSamples.data();
  241. auto set_bufptr = [](const DistanceComp::ChanData &last, const DistanceComp::ChanData &cur)
  242. -> DistanceComp::ChanData
  243. {
  244. DistanceComp::ChanData ret{cur};
  245. ret.Buffer = last.Buffer + RoundUp(last.Length, 4);
  246. return ret;
  247. };
  248. std::partial_sum(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
  249. set_bufptr);
  250. device->ChannelDelays = std::move(chandelays);
  251. }
  252. }
  253. inline auto& GetAmbiScales(DevAmbiScaling scaletype) noexcept
  254. {
  255. if(scaletype == DevAmbiScaling::FuMa) return AmbiScale::FromFuMa();
  256. if(scaletype == DevAmbiScaling::SN3D) return AmbiScale::FromSN3D();
  257. return AmbiScale::FromN3D();
  258. }
  259. inline auto& GetAmbiLayout(DevAmbiLayout layouttype) noexcept
  260. {
  261. if(layouttype == DevAmbiLayout::FuMa) return AmbiIndex::FromFuMa();
  262. return AmbiIndex::FromACN();
  263. }
  264. DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf,
  265. DecoderConfig<DualBand, MAX_OUTPUT_CHANNELS> &decoder)
  266. {
  267. DecoderView ret{};
  268. decoder.mOrder = (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
  269. (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
  270. decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
  271. switch(conf->CoeffScale)
  272. {
  273. case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
  274. case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
  275. case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
  276. }
  277. std::copy_n(std::begin(conf->HFOrderGain),
  278. std::min(al::size(conf->HFOrderGain), al::size(decoder.mOrderGain)),
  279. std::begin(decoder.mOrderGain));
  280. std::copy_n(std::begin(conf->LFOrderGain),
  281. std::min(al::size(conf->LFOrderGain), al::size(decoder.mOrderGainLF)),
  282. std::begin(decoder.mOrderGainLF));
  283. std::array<uint8_t,MaxAmbiChannels> idx_map{};
  284. if(decoder.mIs3D)
  285. {
  286. uint flags{conf->ChanMask};
  287. auto elem = idx_map.begin();
  288. while(flags)
  289. {
  290. int acn{al::countr_zero(flags)};
  291. flags &= ~(1u<<acn);
  292. *elem = static_cast<uint8_t>(acn);
  293. ++elem;
  294. }
  295. }
  296. else
  297. {
  298. uint flags{conf->ChanMask};
  299. auto elem = idx_map.begin();
  300. while(flags)
  301. {
  302. int acn{al::countr_zero(flags)};
  303. flags &= ~(1u<<acn);
  304. switch(acn)
  305. {
  306. case 0: *elem = 0; break;
  307. case 1: *elem = 1; break;
  308. case 3: *elem = 2; break;
  309. case 4: *elem = 3; break;
  310. case 8: *elem = 4; break;
  311. case 9: *elem = 5; break;
  312. case 15: *elem = 6; break;
  313. default: return ret;
  314. }
  315. ++elem;
  316. }
  317. }
  318. const auto num_coeffs = static_cast<uint>(al::popcount(conf->ChanMask));
  319. const auto hfmatrix = conf->HFMatrix;
  320. const auto lfmatrix = conf->LFMatrix;
  321. uint chan_count{0};
  322. using const_speaker_span = al::span<const AmbDecConf::SpeakerConf>;
  323. for(auto &speaker : const_speaker_span{conf->Speakers.get(), conf->NumSpeakers})
  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. *
  339. * Additionally, surround51 will acknowledge back speakers for side
  340. * channels, to avoid issues with an ambdec expecting 5.1 to use the
  341. * back channels.
  342. */
  343. Channel ch{};
  344. if(speaker.Name == "LF")
  345. ch = FrontLeft;
  346. else if(speaker.Name == "RF")
  347. ch = FrontRight;
  348. else if(speaker.Name == "CE")
  349. ch = FrontCenter;
  350. else if(speaker.Name == "LS")
  351. ch = SideLeft;
  352. else if(speaker.Name == "RS")
  353. ch = SideRight;
  354. else if(speaker.Name == "LB")
  355. ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
  356. else if(speaker.Name == "RB")
  357. ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
  358. else if(speaker.Name == "CB")
  359. ch = BackCenter;
  360. else
  361. {
  362. ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
  363. continue;
  364. }
  365. decoder.mChannels[chan_count] = ch;
  366. for(size_t src{0};src < num_coeffs;++src)
  367. {
  368. const size_t dst{idx_map[src]};
  369. decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
  370. }
  371. if(conf->FreqBands > 1)
  372. {
  373. for(size_t src{0};src < num_coeffs;++src)
  374. {
  375. const size_t dst{idx_map[src]};
  376. decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
  377. }
  378. }
  379. ++chan_count;
  380. }
  381. if(chan_count > 0)
  382. {
  383. ret.mOrder = decoder.mOrder;
  384. ret.mIs3D = decoder.mIs3D;
  385. ret.mScaling = decoder.mScaling;
  386. ret.mChannels = {decoder.mChannels.data(), chan_count};
  387. ret.mOrderGain = decoder.mOrderGain;
  388. ret.mCoeffs = {decoder.mCoeffs.data(), chan_count};
  389. if(conf->FreqBands > 1)
  390. {
  391. ret.mOrderGainLF = decoder.mOrderGainLF;
  392. ret.mCoeffsLF = {decoder.mCoeffsLF.data(), chan_count};
  393. }
  394. }
  395. return ret;
  396. }
  397. constexpr DecoderConfig<SingleBand, 1> MonoConfig{
  398. 0, false, {{FrontCenter}},
  399. DevAmbiScaling::N3D,
  400. {{1.0f}},
  401. {{ {{1.0f}} }}
  402. };
  403. constexpr DecoderConfig<SingleBand, 2> StereoConfig{
  404. 1, false, {{FrontLeft, FrontRight}},
  405. DevAmbiScaling::N3D,
  406. {{1.0f, 1.0f}},
  407. {{
  408. {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
  409. {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
  410. }}
  411. };
  412. constexpr DecoderConfig<DualBand, 4> QuadConfig{
  413. 2, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
  414. DevAmbiScaling::N3D,
  415. /*HF*/{{1.15470054e+0f, 1.00000000e+0f, 5.77350269e-1f}},
  416. {{
  417. {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
  418. {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
  419. {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
  420. {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
  421. }},
  422. /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  423. {{
  424. {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
  425. {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
  426. {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
  427. {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
  428. }}
  429. };
  430. constexpr DecoderConfig<DualBand, 5> X51Config{
  431. 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
  432. DevAmbiScaling::FuMa,
  433. /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  434. {{
  435. {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
  436. {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
  437. {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
  438. {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
  439. {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
  440. }},
  441. /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  442. {{
  443. {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
  444. {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
  445. {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
  446. {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
  447. {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
  448. }}
  449. };
  450. constexpr DecoderConfig<SingleBand, 5> X61Config{
  451. 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
  452. DevAmbiScaling::N3D,
  453. {{1.0f, 1.0f, 1.0f}},
  454. {{
  455. {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
  456. {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
  457. {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
  458. {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
  459. {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
  460. }}
  461. };
  462. constexpr DecoderConfig<DualBand, 6> X71Config{
  463. 3, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
  464. DevAmbiScaling::N3D,
  465. /*HF*/{{1.22474487e+0f, 1.13151672e+0f, 8.66025404e-1f, 4.68689571e-1f}},
  466. {{
  467. {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
  468. {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, -7.96819073e-2f, 0.00000000e+0f}},
  469. {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
  470. {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
  471. {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, 7.96819073e-2f, 0.00000000e+0f}},
  472. {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
  473. }},
  474. /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
  475. {{
  476. {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
  477. {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, -7.96819073e-2f, 0.00000000e+0f}},
  478. {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
  479. {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
  480. {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, 7.96819073e-2f, 0.00000000e+0f}},
  481. {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
  482. }}
  483. };
  484. void InitPanning(ALCdevice *device, const bool hqdec=false, const bool stablize=false,
  485. DecoderView decoder={})
  486. {
  487. if(!decoder.mOrder)
  488. {
  489. switch(device->FmtChans)
  490. {
  491. case DevFmtMono: decoder = MonoConfig; break;
  492. case DevFmtStereo: decoder = StereoConfig; break;
  493. case DevFmtQuad: decoder = QuadConfig; break;
  494. case DevFmtX51: decoder = X51Config; break;
  495. case DevFmtX61: decoder = X61Config; break;
  496. case DevFmtX71: decoder = X71Config; break;
  497. case DevFmtAmbi3D:
  498. auto&& acnmap = GetAmbiLayout(device->mAmbiLayout);
  499. auto&& n3dscale = GetAmbiScales(device->mAmbiScale);
  500. /* For DevFmtAmbi3D, the ambisonic order is already set. */
  501. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  502. std::transform(acnmap.begin(), acnmap.begin()+count, std::begin(device->Dry.AmbiMap),
  503. [&n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
  504. { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
  505. AllocChannels(device, count, 0);
  506. float nfc_delay{device->configValue<float>("decoder", "nfc-ref-delay").value_or(0.0f)};
  507. if(nfc_delay > 0.0f)
  508. InitNearFieldCtrl(device, nfc_delay * SpeedOfSoundMetersPerSec, device->mAmbiOrder,
  509. true);
  510. return;
  511. }
  512. }
  513. const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
  514. al::vector<ChannelDec> chancoeffs, chancoeffslf;
  515. for(size_t i{0u};i < decoder.mChannels.size();++i)
  516. {
  517. const uint idx{GetChannelIdxByName(device->RealOut, decoder.mChannels[i])};
  518. if(idx == INVALID_CHANNEL_INDEX)
  519. {
  520. ERR("Failed to find %s channel in device\n",
  521. GetLabelFromChannel(decoder.mChannels[i]));
  522. continue;
  523. }
  524. chancoeffs.resize(maxz(chancoeffs.size(), idx+1u), ChannelDec{});
  525. al::span<float,MaxAmbiChannels> coeffs{chancoeffs[idx]};
  526. size_t ambichan{0};
  527. for(uint o{0};o < decoder.mOrder+1u;++o)
  528. {
  529. const float order_gain{decoder.mOrderGain[o]};
  530. const size_t order_max{decoder.mIs3D ? AmbiChannelsFromOrder(o) :
  531. Ambi2DChannelsFromOrder(o)};
  532. for(;ambichan < order_max;++ambichan)
  533. coeffs[ambichan] = decoder.mCoeffs[i][ambichan] * order_gain;
  534. }
  535. if(!dual_band)
  536. continue;
  537. chancoeffslf.resize(maxz(chancoeffslf.size(), idx+1u), ChannelDec{});
  538. coeffs = chancoeffslf[idx];
  539. ambichan = 0;
  540. for(uint o{0};o < decoder.mOrder+1u;++o)
  541. {
  542. const float order_gain{decoder.mOrderGainLF[o]};
  543. const size_t order_max{decoder.mIs3D ? AmbiChannelsFromOrder(o) :
  544. Ambi2DChannelsFromOrder(o)};
  545. for(;ambichan < order_max;++ambichan)
  546. coeffs[ambichan] = decoder.mCoeffsLF[i][ambichan] * order_gain;
  547. }
  548. }
  549. /* For non-DevFmtAmbi3D, set the ambisonic order. */
  550. device->mAmbiOrder = decoder.mOrder;
  551. const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
  552. Ambi2DChannelsFromOrder(decoder.mOrder)};
  553. const al::span<const uint8_t> acnmap{decoder.mIs3D ? AmbiIndex::FromACN().data() :
  554. AmbiIndex::FromACN2D().data(), ambicount};
  555. auto&& coeffscale = GetAmbiScales(decoder.mScaling);
  556. std::transform(acnmap.begin(), acnmap.end(), std::begin(device->Dry.AmbiMap),
  557. [&coeffscale](const uint8_t &acn) noexcept
  558. { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
  559. AllocChannels(device, ambicount, device->channelsFromFmt());
  560. std::unique_ptr<FrontStablizer> stablizer;
  561. if(stablize)
  562. {
  563. /* Only enable the stablizer if the decoder does not output to the
  564. * front-center channel.
  565. */
  566. const auto cidx = device->RealOut.ChannelIndex[FrontCenter];
  567. bool hasfc{false};
  568. if(cidx < chancoeffs.size())
  569. {
  570. for(const auto &coeff : chancoeffs[cidx])
  571. hasfc |= coeff != 0.0f;
  572. }
  573. if(!hasfc && cidx < chancoeffslf.size())
  574. {
  575. for(const auto &coeff : chancoeffslf[cidx])
  576. hasfc |= coeff != 0.0f;
  577. }
  578. if(!hasfc)
  579. {
  580. stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
  581. TRACE("Front stablizer enabled\n");
  582. }
  583. }
  584. TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
  585. !dual_band ? "single" : "dual",
  586. (decoder.mOrder > 2) ? "third" :
  587. (decoder.mOrder > 1) ? "second" : "first",
  588. decoder.mIs3D ? " periphonic" : "");
  589. device->AmbiDecoder = BFormatDec::Create(ambicount, chancoeffs, chancoeffslf,
  590. device->mXOverFreq/static_cast<float>(device->Frequency), std::move(stablizer));
  591. }
  592. void InitHrtfPanning(ALCdevice *device)
  593. {
  594. constexpr float Deg180{al::numbers::pi_v<float>};
  595. constexpr float Deg_90{Deg180 / 2.0f /* 90 degrees*/};
  596. constexpr float Deg_45{Deg_90 / 2.0f /* 45 degrees*/};
  597. constexpr float Deg135{Deg_45 * 3.0f /*135 degrees*/};
  598. constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
  599. constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
  600. constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
  601. constexpr float Deg_21{3.648638281e-01f /* 20~ 21 degrees*/};
  602. static const AngularPoint AmbiPoints1O[]{
  603. { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
  604. { EvRadians{ Deg_35}, AzRadians{-Deg135} },
  605. { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
  606. { EvRadians{ Deg_35}, AzRadians{ Deg135} },
  607. { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
  608. { EvRadians{-Deg_35}, AzRadians{-Deg135} },
  609. { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
  610. { EvRadians{-Deg_35}, AzRadians{ Deg135} },
  611. }, AmbiPoints2O[]{
  612. { EvRadians{ 0.0f}, AzRadians{ 0.0f} },
  613. { EvRadians{ 0.0f}, AzRadians{ Deg180} },
  614. { EvRadians{ 0.0f}, AzRadians{-Deg_90} },
  615. { EvRadians{ 0.0f}, AzRadians{ Deg_90} },
  616. { EvRadians{ Deg_90}, AzRadians{ 0.0f} },
  617. { EvRadians{-Deg_90}, AzRadians{ 0.0f} },
  618. { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
  619. { EvRadians{ Deg_35}, AzRadians{-Deg135} },
  620. { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
  621. { EvRadians{ Deg_35}, AzRadians{ Deg135} },
  622. { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
  623. { EvRadians{-Deg_35}, AzRadians{-Deg135} },
  624. { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
  625. { EvRadians{-Deg_35}, AzRadians{ Deg135} },
  626. }, AmbiPoints3O[]{
  627. { EvRadians{ Deg_69}, AzRadians{-Deg_90} },
  628. { EvRadians{ Deg_69}, AzRadians{ Deg_90} },
  629. { EvRadians{-Deg_69}, AzRadians{-Deg_90} },
  630. { EvRadians{-Deg_69}, AzRadians{ Deg_90} },
  631. { EvRadians{ 0.0f}, AzRadians{-Deg_69} },
  632. { EvRadians{ 0.0f}, AzRadians{-Deg111} },
  633. { EvRadians{ 0.0f}, AzRadians{ Deg_69} },
  634. { EvRadians{ 0.0f}, AzRadians{ Deg111} },
  635. { EvRadians{ Deg_21}, AzRadians{ 0.0f} },
  636. { EvRadians{ Deg_21}, AzRadians{ Deg180} },
  637. { EvRadians{-Deg_21}, AzRadians{ 0.0f} },
  638. { EvRadians{-Deg_21}, AzRadians{ Deg180} },
  639. { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
  640. { EvRadians{ Deg_35}, AzRadians{-Deg135} },
  641. { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
  642. { EvRadians{ Deg_35}, AzRadians{ Deg135} },
  643. { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
  644. { EvRadians{-Deg_35}, AzRadians{-Deg135} },
  645. { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
  646. { EvRadians{-Deg_35}, AzRadians{ Deg135} },
  647. };
  648. static const float AmbiMatrix1O[][MaxAmbiChannels]{
  649. { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
  650. { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
  651. { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
  652. { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
  653. { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
  654. { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
  655. { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
  656. { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
  657. }, AmbiMatrix2O[][MaxAmbiChannels]{
  658. { 7.142857143e-02f, 0.000000000e+00f, 0.000000000e+00f, 1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, 1.290994449e-01f, },
  659. { 7.142857143e-02f, 0.000000000e+00f, 0.000000000e+00f, -1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, 1.290994449e-01f, },
  660. { 7.142857143e-02f, 1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, -1.290994449e-01f, },
  661. { 7.142857143e-02f, -1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, -1.290994449e-01f, },
  662. { 7.142857143e-02f, 0.000000000e+00f, 1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 1.490711985e-01f, 0.000000000e+00f, 0.000000000e+00f, },
  663. { 7.142857143e-02f, 0.000000000e+00f, -1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 1.490711985e-01f, 0.000000000e+00f, 0.000000000e+00f, },
  664. { 7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, 9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
  665. { 7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, -9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
  666. { 7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, -9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
  667. { 7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, 9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
  668. { 7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, 9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
  669. { 7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, -9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
  670. { 7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, -9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
  671. { 7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, 9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
  672. }, AmbiMatrix3O[][MaxAmbiChannels]{
  673. { 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, },
  674. { 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, },
  675. { 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, },
  676. { 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, },
  677. { 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, },
  678. { 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, },
  679. { 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, },
  680. { 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, },
  681. { 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, },
  682. { 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, },
  683. { 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, },
  684. { 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, },
  685. { 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, },
  686. { 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, },
  687. { 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, },
  688. { 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, },
  689. { 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, },
  690. { 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, },
  691. { 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, },
  692. { 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, },
  693. };
  694. static const float AmbiOrderHFGain1O[MaxAmbiOrder+1]{
  695. /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
  696. }, AmbiOrderHFGain2O[MaxAmbiOrder+1]{
  697. /*ENRGY 2.357022604e+00f, 1.825741858e+00f, 9.428090416e-01f*/
  698. /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
  699. /*RMS*/ 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f
  700. }, AmbiOrderHFGain3O[MaxAmbiOrder+1]{
  701. /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
  702. /*AMP 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f*/
  703. /*RMS*/ 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f
  704. };
  705. static_assert(al::size(AmbiPoints1O) == al::size(AmbiMatrix1O), "First-Order Ambisonic HRTF mismatch");
  706. static_assert(al::size(AmbiPoints2O) == al::size(AmbiMatrix2O), "Second-Order Ambisonic HRTF mismatch");
  707. static_assert(al::size(AmbiPoints3O) == al::size(AmbiMatrix3O), "Third-Order Ambisonic HRTF mismatch");
  708. /* A 700hz crossover frequency provides tighter sound imaging at the sweet
  709. * spot with ambisonic decoding, as the distance between the ears is closer
  710. * to half this frequency wavelength, which is the optimal point where the
  711. * response should change between optimizing phase vs volume. Normally this
  712. * tighter imaging is at the cost of a smaller sweet spot, but since the
  713. * listener is fixed in the center of the HRTF responses for the decoder,
  714. * we don't have to worry about ever being out of the sweet spot.
  715. *
  716. * A better option here may be to have the head radius as part of the HRTF
  717. * data set and calculate the optimal crossover frequency from that.
  718. */
  719. device->mXOverFreq = 700.0f;
  720. /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
  721. * and it eases the CPU/memory load.
  722. */
  723. device->mRenderMode = RenderMode::Hrtf;
  724. uint ambi_order{1};
  725. if(auto modeopt = device->configValue<std::string>(nullptr, "hrtf-mode"))
  726. {
  727. struct HrtfModeEntry {
  728. char name[8];
  729. RenderMode mode;
  730. uint order;
  731. };
  732. static const HrtfModeEntry hrtf_modes[]{
  733. { "full", RenderMode::Hrtf, 1 },
  734. { "ambi1", RenderMode::Normal, 1 },
  735. { "ambi2", RenderMode::Normal, 2 },
  736. { "ambi3", RenderMode::Normal, 3 },
  737. };
  738. const char *mode{modeopt->c_str()};
  739. if(al::strcasecmp(mode, "basic") == 0)
  740. {
  741. ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2");
  742. mode = "ambi2";
  743. }
  744. auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
  745. { return al::strcasecmp(mode, entry.name) == 0; };
  746. auto iter = std::find_if(std::begin(hrtf_modes), std::end(hrtf_modes), match_entry);
  747. if(iter == std::end(hrtf_modes))
  748. ERR("Unexpected hrtf-mode: %s\n", mode);
  749. else
  750. {
  751. device->mRenderMode = iter->mode;
  752. ambi_order = iter->order;
  753. }
  754. }
  755. TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
  756. (((ambi_order%100)/10) == 1) ? "th" :
  757. ((ambi_order%10) == 1) ? "st" :
  758. ((ambi_order%10) == 2) ? "nd" :
  759. ((ambi_order%10) == 3) ? "rd" : "th",
  760. (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
  761. device->mHrtfName.c_str());
  762. al::span<const AngularPoint> AmbiPoints{AmbiPoints1O};
  763. const float (*AmbiMatrix)[MaxAmbiChannels]{AmbiMatrix1O};
  764. al::span<const float,MaxAmbiOrder+1> AmbiOrderHFGain{AmbiOrderHFGain1O};
  765. if(ambi_order >= 3)
  766. {
  767. AmbiPoints = AmbiPoints3O;
  768. AmbiMatrix = AmbiMatrix3O;
  769. AmbiOrderHFGain = AmbiOrderHFGain3O;
  770. }
  771. else if(ambi_order == 2)
  772. {
  773. AmbiPoints = AmbiPoints2O;
  774. AmbiMatrix = AmbiMatrix2O;
  775. AmbiOrderHFGain = AmbiOrderHFGain2O;
  776. }
  777. device->mAmbiOrder = ambi_order;
  778. const size_t count{AmbiChannelsFromOrder(ambi_order)};
  779. std::transform(AmbiIndex::FromACN().begin(), AmbiIndex::FromACN().begin()+count,
  780. std::begin(device->Dry.AmbiMap),
  781. [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
  782. );
  783. AllocChannels(device, count, device->channelsFromFmt());
  784. HrtfStore *Hrtf{device->mHrtf.get()};
  785. auto hrtfstate = DirectHrtfState::Create(count);
  786. hrtfstate->build(Hrtf, device->mIrSize, AmbiPoints, AmbiMatrix, device->mXOverFreq,
  787. AmbiOrderHFGain);
  788. device->mHrtfState = std::move(hrtfstate);
  789. InitNearFieldCtrl(device, Hrtf->field[0].distance, ambi_order, true);
  790. }
  791. void InitUhjPanning(ALCdevice *device)
  792. {
  793. /* UHJ is always 2D first-order. */
  794. constexpr size_t count{Ambi2DChannelsFromOrder(1)};
  795. device->mAmbiOrder = 1;
  796. auto acnmap_begin = AmbiIndex::FromFuMa().begin();
  797. std::transform(acnmap_begin, acnmap_begin + count, std::begin(device->Dry.AmbiMap),
  798. [](const uint8_t &acn) noexcept -> BFChannelConfig
  799. { return BFChannelConfig{1.0f/AmbiScale::FromUHJ()[acn], acn}; });
  800. AllocChannels(device, count, device->channelsFromFmt());
  801. }
  802. } // namespace
  803. void aluInitRenderer(ALCdevice *device, int hrtf_id, al::optional<StereoEncoding> stereomode)
  804. {
  805. /* Hold the HRTF the device last used, in case it's used again. */
  806. HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
  807. device->mHrtfState = nullptr;
  808. device->mHrtf = nullptr;
  809. device->mIrSize = 0;
  810. device->mHrtfName.clear();
  811. device->mXOverFreq = 400.0f;
  812. device->mRenderMode = RenderMode::Normal;
  813. if(device->FmtChans != DevFmtStereo)
  814. {
  815. old_hrtf = nullptr;
  816. if(stereomode && *stereomode == StereoEncoding::Hrtf)
  817. device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  818. const char *layout{nullptr};
  819. switch(device->FmtChans)
  820. {
  821. case DevFmtQuad: layout = "quad"; break;
  822. case DevFmtX51: layout = "surround51"; break;
  823. case DevFmtX61: layout = "surround61"; break;
  824. case DevFmtX71: layout = "surround71"; break;
  825. /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
  826. case DevFmtMono:
  827. case DevFmtStereo:
  828. case DevFmtAmbi3D:
  829. break;
  830. }
  831. std::unique_ptr<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>> decoder_store;
  832. DecoderView decoder{};
  833. float speakerdists[MaxChannels]{};
  834. auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
  835. {
  836. AmbDecConf conf{};
  837. if(auto err = conf.load(config))
  838. {
  839. ERR("Failed to load layout file %s\n", config);
  840. ERR(" %s\n", err->c_str());
  841. }
  842. else if(conf.NumSpeakers > MAX_OUTPUT_CHANNELS)
  843. ERR("Unsupported decoder speaker count %zu (max %d)\n", conf.NumSpeakers,
  844. MAX_OUTPUT_CHANNELS);
  845. else if(conf.ChanMask > Ambi3OrderMask)
  846. ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
  847. Ambi3OrderMask);
  848. else
  849. {
  850. device->mXOverFreq = clampf(conf.XOverFreq, 100.0f, 1000.0f);
  851. decoder_store = std::make_unique<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>>();
  852. decoder = MakeDecoderView(device, &conf, *decoder_store);
  853. for(size_t i{0};i < decoder.mChannels.size();++i)
  854. speakerdists[i] = conf.Speakers[i].Distance;
  855. }
  856. };
  857. if(layout)
  858. {
  859. if(auto decopt = device->configValue<std::string>("decoder", layout))
  860. load_config(decopt->c_str());
  861. }
  862. /* Enable the stablizer only for formats that have front-left, front-
  863. * right, and front-center outputs.
  864. */
  865. const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != INVALID_CHANNEL_INDEX
  866. && device->RealOut.ChannelIndex[FrontLeft] != INVALID_CHANNEL_INDEX
  867. && device->RealOut.ChannelIndex[FrontRight] != INVALID_CHANNEL_INDEX
  868. && device->getConfigValueBool(nullptr, "front-stablizer", 0) != 0};
  869. const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", 1) != 0};
  870. InitPanning(device, hqdec, stablize, decoder);
  871. if(decoder.mOrder > 0)
  872. {
  873. float accum_dist{0.0f}, spkr_count{0.0f};
  874. for(auto dist : speakerdists)
  875. {
  876. if(dist > 0.0f)
  877. {
  878. accum_dist += dist;
  879. spkr_count += 1.0f;
  880. }
  881. }
  882. if(spkr_count > 0)
  883. {
  884. InitNearFieldCtrl(device, accum_dist / spkr_count, decoder.mOrder, decoder.mIs3D);
  885. InitDistanceComp(device, decoder.mChannels, speakerdists);
  886. }
  887. }
  888. if(auto *ambidec{device->AmbiDecoder.get()})
  889. {
  890. device->PostProcess = ambidec->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
  891. : &ALCdevice::ProcessAmbiDec;
  892. }
  893. return;
  894. }
  895. /* If HRTF is explicitly requested, or if there's no explicit request and
  896. * the device is headphones, try to enable it.
  897. */
  898. if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Hrtf
  899. || (!stereomode && device->Flags.test(DirectEar)))
  900. {
  901. if(device->mHrtfList.empty())
  902. device->enumerateHrtfs();
  903. if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
  904. {
  905. const std::string &hrtfname = device->mHrtfList[static_cast<uint>(hrtf_id)];
  906. if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
  907. {
  908. device->mHrtf = std::move(hrtf);
  909. device->mHrtfName = hrtfname;
  910. }
  911. }
  912. if(!device->mHrtf)
  913. {
  914. for(const auto &hrtfname : device->mHrtfList)
  915. {
  916. if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
  917. {
  918. device->mHrtf = std::move(hrtf);
  919. device->mHrtfName = hrtfname;
  920. break;
  921. }
  922. }
  923. }
  924. if(device->mHrtf)
  925. {
  926. old_hrtf = nullptr;
  927. HrtfStore *hrtf{device->mHrtf.get()};
  928. device->mIrSize = hrtf->irSize;
  929. if(auto hrtfsizeopt = device->configValue<uint>(nullptr, "hrtf-size"))
  930. {
  931. if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
  932. device->mIrSize = maxu(*hrtfsizeopt, MinIrLength);
  933. }
  934. InitHrtfPanning(device);
  935. device->PostProcess = &ALCdevice::ProcessHrtf;
  936. device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
  937. return;
  938. }
  939. }
  940. old_hrtf = nullptr;
  941. if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Uhj)
  942. {
  943. device->mUhjEncoder = std::make_unique<UhjEncoder>();
  944. TRACE("UHJ enabled\n");
  945. InitUhjPanning(device);
  946. device->PostProcess = &ALCdevice::ProcessUhj;
  947. return;
  948. }
  949. device->mRenderMode = RenderMode::Pairwise;
  950. if(device->Type != DeviceType::Loopback)
  951. {
  952. if(auto cflevopt = device->configValue<int>(nullptr, "cf_level"))
  953. {
  954. if(*cflevopt > 0 && *cflevopt <= 6)
  955. {
  956. device->Bs2b = std::make_unique<bs2b>();
  957. bs2b_set_params(device->Bs2b.get(), *cflevopt,
  958. static_cast<int>(device->Frequency));
  959. TRACE("BS2B enabled\n");
  960. InitPanning(device);
  961. device->PostProcess = &ALCdevice::ProcessBs2b;
  962. return;
  963. }
  964. }
  965. }
  966. TRACE("Stereo rendering\n");
  967. InitPanning(device);
  968. device->PostProcess = &ALCdevice::ProcessAmbiDec;
  969. }
  970. void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
  971. {
  972. DeviceBase *device{context->mDevice};
  973. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  974. auto wetbuffer_iter = context->mWetBuffers.end();
  975. if(slot->mWetBuffer)
  976. {
  977. /* If the effect slot already has a wet buffer attached, allocate a new
  978. * one in its place.
  979. */
  980. wetbuffer_iter = context->mWetBuffers.begin();
  981. for(;wetbuffer_iter != context->mWetBuffers.end();++wetbuffer_iter)
  982. {
  983. if(wetbuffer_iter->get() == slot->mWetBuffer)
  984. {
  985. slot->mWetBuffer = nullptr;
  986. slot->Wet.Buffer = {};
  987. *wetbuffer_iter = WetBufferPtr{new(FamCount(count)) WetBuffer{count}};
  988. break;
  989. }
  990. }
  991. }
  992. if(wetbuffer_iter == context->mWetBuffers.end())
  993. {
  994. /* Otherwise, search for an unused wet buffer. */
  995. wetbuffer_iter = context->mWetBuffers.begin();
  996. for(;wetbuffer_iter != context->mWetBuffers.end();++wetbuffer_iter)
  997. {
  998. if(!(*wetbuffer_iter)->mInUse)
  999. break;
  1000. }
  1001. if(wetbuffer_iter == context->mWetBuffers.end())
  1002. {
  1003. /* Otherwise, allocate a new one to use. */
  1004. context->mWetBuffers.emplace_back(WetBufferPtr{new(FamCount(count)) WetBuffer{count}});
  1005. wetbuffer_iter = context->mWetBuffers.end()-1;
  1006. }
  1007. }
  1008. WetBuffer *wetbuffer{slot->mWetBuffer = wetbuffer_iter->get()};
  1009. wetbuffer->mInUse = true;
  1010. auto acnmap_begin = AmbiIndex::FromACN().begin();
  1011. auto iter = std::transform(acnmap_begin, acnmap_begin + count, slot->Wet.AmbiMap.begin(),
  1012. [](const uint8_t &acn) noexcept -> BFChannelConfig
  1013. { return BFChannelConfig{1.0f, acn}; });
  1014. std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
  1015. slot->Wet.Buffer = wetbuffer->mBuffer;
  1016. }