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