Use filebuf instead of ifstream in allafplay
[openal-soft.git] / alc / panning.cpp
blobf144186cbd0fa2b60acef390ba1007dd85f245c9
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(al::Device *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(al::Device *device, const float ctrl_dist, const uint order,
264 const bool is3d)
266 static const std::array<uint,MaxAmbiOrder+1> chans_per_order2d{{1, 2, 2, 2}};
267 static const std::array<uint,MaxAmbiOrder+1> chans_per_order3d{{1, 3, 5, 7}};
269 /* NFC is only used when AvgSpeakerDist is greater than 0. */
270 if(!device->getConfigValueBool("decoder", "nfc", false) || !(ctrl_dist > 0.0f))
271 return;
273 device->AvgSpeakerDist = std::clamp(ctrl_dist, 0.1f, 10.0f);
274 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
276 const float w1{SpeedOfSoundMetersPerSec /
277 (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
278 device->mNFCtrlFilter.init(w1);
280 auto iter = std::copy_n(is3d ? chans_per_order3d.begin() : chans_per_order2d.begin(), order+1u,
281 device->NumChannelsPerOrder.begin());
282 std::fill(iter, device->NumChannelsPerOrder.end(), 0u);
285 void InitDistanceComp(al::Device *device, const al::span<const Channel> channels,
286 const al::span<const float,MaxOutputChannels> dists)
288 const float maxdist{std::accumulate(dists.begin(), dists.end(), 0.0f,
289 [](const float a, const float b) noexcept -> float { return std::max(a, b); })};
291 if(!device->getConfigValueBool("decoder", "distance-comp", true) || !(maxdist > 0.0f))
292 return;
294 const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
296 struct DistCoeffs { uint Length{}; float Gain{}; };
297 std::vector<DistCoeffs> ChanDelay;
298 ChanDelay.reserve(device->RealOut.Buffer.size());
300 size_t total{0u};
301 for(size_t chidx{0};chidx < channels.size();++chidx)
303 const Channel ch{channels[chidx]};
304 const size_t idx{device->RealOut.ChannelIndex[ch]};
305 if(idx == InvalidChannelIndex)
306 continue;
308 const float distance{dists[chidx]};
310 /* Distance compensation only delays in steps of the sample rate. This
311 * is a bit less accurate since the delay time falls to the nearest
312 * sample time, but it's far simpler as it doesn't have to deal with
313 * phase offsets. This means at 48khz, for instance, the distance delay
314 * will be in steps of about 7 millimeters.
316 float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
317 if(delay > float{DistanceComp::MaxDelay-1})
319 ERR("Delay for channel %zu (%s) exceeds buffer length (%f > %d)\n", idx,
320 GetLabelFromChannel(ch), delay, DistanceComp::MaxDelay-1);
321 delay = float{DistanceComp::MaxDelay-1};
324 ChanDelay.resize(std::max(ChanDelay.size(), idx+1_uz));
325 ChanDelay[idx].Length = static_cast<uint>(delay);
326 ChanDelay[idx].Gain = distance / maxdist;
327 TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
328 ChanDelay[idx].Length, ChanDelay[idx].Gain);
330 /* Round up to the next 4th sample, so each channel buffer starts
331 * 16-byte aligned.
333 total += RoundUp(ChanDelay[idx].Length, 4);
336 if(total > 0)
338 auto chandelays = DistanceComp::Create(total);
339 auto chanbuffer = chandelays->mSamples.begin();
341 auto set_bufptr = [&chanbuffer](const DistCoeffs &data)
343 DistanceComp::ChanData ret{};
344 ret.Buffer = al::span{chanbuffer, data.Length};
345 ret.Gain = data.Gain;
346 chanbuffer += ptrdiff_t(RoundUp(data.Length, 4));
347 return ret;
349 std::transform(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
350 set_bufptr);
351 device->ChannelDelays = std::move(chandelays);
356 constexpr auto GetAmbiScales(DevAmbiScaling scaletype) noexcept
358 if(scaletype == DevAmbiScaling::FuMa) return al::span{AmbiScale::FromFuMa};
359 if(scaletype == DevAmbiScaling::SN3D) return al::span{AmbiScale::FromSN3D};
360 return al::span{AmbiScale::FromN3D};
363 constexpr auto GetAmbiLayout(DevAmbiLayout layouttype) noexcept
365 if(layouttype == DevAmbiLayout::FuMa) return al::span{AmbiIndex::FromFuMa};
366 return al::span{AmbiIndex::FromACN};
370 auto MakeDecoderView(al::Device *device, const AmbDecConf *conf,
371 DecoderConfig<DualBand,MaxOutputChannels> &decoder) -> DecoderView
373 DecoderView ret{};
375 decoder.mOrder = (conf->ChanMask > Ambi3OrderMask) ? uint8_t{4} :
376 (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
377 (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
378 decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
380 switch(conf->CoeffScale)
382 case AmbDecScale::Unset: ASSUME(false); break;
383 case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
384 case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
385 case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
388 const auto hfordermin = std::min(conf->HFOrderGain.size(), decoder.mOrderGain.size());
389 std::copy_n(conf->HFOrderGain.begin(), hfordermin, decoder.mOrderGain.begin());
390 const auto lfordermin = std::min(conf->LFOrderGain.size(), decoder.mOrderGainLF.size());
391 std::copy_n(conf->LFOrderGain.begin(), lfordermin, decoder.mOrderGainLF.begin());
393 const auto num_coeffs = decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder)
394 : Ambi2DChannelsFromOrder(decoder.mOrder);
395 const auto idx_map = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::FromACN}
396 : al::span<const uint8_t>{AmbiIndex::FromACN2D};
397 const auto hfmatrix = conf->HFMatrix;
398 const auto lfmatrix = conf->LFMatrix;
400 uint chan_count{0};
401 for(auto &speaker : al::span{std::as_const(conf->Speakers)})
403 /* NOTE: AmbDec does not define any standard speaker names, however
404 * for this to work we have to by able to find the output channel
405 * the speaker definition corresponds to. Therefore, OpenAL Soft
406 * requires these channel labels to be recognized:
408 * LF = Front left
409 * RF = Front right
410 * LS = Side left
411 * RS = Side right
412 * LB = Back left
413 * RB = Back right
414 * CE = Front center
415 * CB = Back center
416 * LFT = Top front left
417 * RFT = Top front right
418 * LBT = Top back left
419 * RBT = Top back right
420 * LFB = Bottom front left
421 * RFB = Bottom front right
422 * LBB = Bottom back left
423 * RBB = Bottom back right
425 * Additionally, surround51 will acknowledge back speakers for side
426 * channels, to avoid issues with an ambdec expecting 5.1 to use the
427 * back channels.
429 Channel ch{};
430 if(speaker.Name == "LF"sv)
431 ch = FrontLeft;
432 else if(speaker.Name == "RF"sv)
433 ch = FrontRight;
434 else if(speaker.Name == "CE"sv)
435 ch = FrontCenter;
436 else if(speaker.Name == "LS"sv)
437 ch = SideLeft;
438 else if(speaker.Name == "RS"sv)
439 ch = SideRight;
440 else if(speaker.Name == "LB"sv)
441 ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
442 else if(speaker.Name == "RB"sv)
443 ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
444 else if(speaker.Name == "CB"sv)
445 ch = BackCenter;
446 else if(speaker.Name == "LFT"sv)
447 ch = TopFrontLeft;
448 else if(speaker.Name == "RFT"sv)
449 ch = TopFrontRight;
450 else if(speaker.Name == "LBT"sv)
451 ch = TopBackLeft;
452 else if(speaker.Name == "RBT"sv)
453 ch = TopBackRight;
454 else if(speaker.Name == "LFB"sv)
455 ch = BottomFrontLeft;
456 else if(speaker.Name == "RFB"sv)
457 ch = BottomFrontRight;
458 else if(speaker.Name == "LBB"sv)
459 ch = BottomBackLeft;
460 else if(speaker.Name == "RBB"sv)
461 ch = BottomBackRight;
462 else
464 int idx{};
465 char c{};
466 if(sscanf(speaker.Name.c_str(), "AUX%d%c", &idx, &c) != 1 || idx < 0
467 || idx >= MaxChannels-Aux0)
469 ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
470 continue;
472 ch = static_cast<Channel>(Aux0+idx);
475 decoder.mChannels[chan_count] = ch;
476 for(size_t dst{0};dst < num_coeffs;++dst)
478 const size_t src{idx_map[dst]};
479 decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
481 if(conf->FreqBands > 1)
483 for(size_t dst{0};dst < num_coeffs;++dst)
485 const size_t src{idx_map[dst]};
486 decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
489 ++chan_count;
492 if(chan_count > 0)
494 ret.mOrder = decoder.mOrder;
495 ret.mIs3D = decoder.mIs3D;
496 ret.mScaling = decoder.mScaling;
497 ret.mChannels = al::span{decoder.mChannels}.first(chan_count);
498 ret.mOrderGain = decoder.mOrderGain;
499 ret.mCoeffs = al::span{decoder.mCoeffs}.first(chan_count);
500 if(conf->FreqBands > 1)
502 ret.mOrderGainLF = decoder.mOrderGainLF;
503 ret.mCoeffsLF = al::span{decoder.mCoeffsLF}.first(chan_count);
506 return ret;
509 constexpr DecoderConfig<SingleBand, 1> MonoConfig{
510 0, false, {{FrontCenter}},
511 DevAmbiScaling::N3D,
512 {{1.0f}},
513 {{ {{1.0f}} }}
515 constexpr DecoderConfig<SingleBand, 2> StereoConfig{
516 1, false, {{FrontLeft, FrontRight}},
517 DevAmbiScaling::N3D,
518 {{1.0f, 1.0f}},
520 {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
521 {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
524 constexpr DecoderConfig<DualBand, 4> QuadConfig{
525 1, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
526 DevAmbiScaling::N3D,
527 /*HF*/{{1.41421356e+0f, 1.00000000e+0f}},
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}},
532 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
534 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
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}},
539 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
542 constexpr DecoderConfig<DualBand, 5> X51Config{
543 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
544 DevAmbiScaling::FuMa,
545 /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
547 {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
548 {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
549 {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
550 {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
551 {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
553 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
555 {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
556 {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
557 {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
558 {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
559 {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
562 constexpr DecoderConfig<SingleBand, 5> X61Config{
563 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
564 DevAmbiScaling::N3D,
565 {{1.0f, 1.0f, 1.0f}},
567 {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
568 {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
569 {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
570 {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
571 {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
574 constexpr DecoderConfig<DualBand, 6> X71Config{
575 2, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
576 DevAmbiScaling::N3D,
577 /*HF*/{{1.41421356e+0f, 1.22474487e+0f, 7.07106781e-1f}},
579 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
580 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
581 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
582 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
583 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
584 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
586 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
588 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
589 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
590 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
591 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
592 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
593 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
596 constexpr DecoderConfig<DualBand, 6> X3D71Config{
597 1, true, {{Aux0, SideLeft, FrontLeft, FrontRight, SideRight, Aux1}},
598 DevAmbiScaling::N3D,
599 /*HF*/{{1.73205081e+0f, 1.00000000e+0f}},
601 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-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, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
606 {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
608 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
610 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-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, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
615 {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
618 constexpr DecoderConfig<SingleBand, 10> X714Config{
619 1, true, {{FrontLeft, FrontRight, SideLeft, SideRight, BackLeft, BackRight, TopFrontLeft, TopFrontRight, TopBackLeft, TopBackRight }},
620 DevAmbiScaling::N3D,
621 {{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
623 {{1.27149251e-01f, 7.63047539e-02f, -3.64373750e-02f, 1.59700680e-01f}},
624 {{1.07005418e-01f, -7.67638760e-02f, -4.92129762e-02f, 1.29012797e-01f}},
625 {{1.26400196e-01f, 1.77494694e-01f, -3.71203389e-02f, 0.00000000e+00f}},
626 {{1.26396516e-01f, -1.77488059e-01f, -3.71297878e-02f, 0.00000000e+00f}},
627 {{1.06996956e-01f, 7.67615256e-02f, -4.92166307e-02f, -1.29001640e-01f}},
628 {{1.27145671e-01f, -7.63003471e-02f, -3.64353304e-02f, -1.59697510e-01f}},
629 {{8.80919747e-02f, 7.48940670e-02f, 9.08786244e-02f, 6.22527183e-02f}},
630 {{1.57880745e-01f, -7.28755272e-02f, 1.82364187e-01f, 8.74240284e-02f}},
631 {{1.57892225e-01f, 7.28944768e-02f, 1.82363474e-01f, -8.74301086e-02f}},
632 {{8.80892603e-02f, -7.48948724e-02f, 9.08779842e-02f, -6.22480443e-02f}},
635 constexpr DecoderConfig<DualBand, 14> X7144Config{
636 1, true, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight, TopBackLeft, TopFrontLeft, TopFrontRight, TopBackRight, BottomBackLeft, BottomFrontLeft, BottomFrontRight, BottomBackRight}},
637 DevAmbiScaling::N3D,
638 /*HF*/{{2.64575131e+0f, 1.52752523e+0f}},
640 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
641 {{7.14285714e-02f, 1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
642 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
643 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
644 {{7.14285714e-02f, -1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
645 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, -8.82352941e-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}},
653 {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
655 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
657 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
658 {{7.14285714e-02f, 1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
659 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
660 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
661 {{7.14285714e-02f, -1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
662 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, -8.82352941e-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}},
670 {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
674 void InitPanning(al::Device *device, const bool hqdec=false, const bool stablize=false,
675 DecoderView decoder={})
677 if(!decoder)
679 switch(device->FmtChans)
681 case DevFmtMono: decoder = MonoConfig; break;
682 case DevFmtStereo: decoder = StereoConfig; break;
683 case DevFmtQuad: decoder = QuadConfig; break;
684 case DevFmtX51: decoder = X51Config; break;
685 case DevFmtX61: decoder = X61Config; break;
686 case DevFmtX71: decoder = X71Config; break;
687 case DevFmtX714: decoder = X714Config; break;
688 case DevFmtX7144: decoder = X7144Config; break;
689 case DevFmtX3D71: decoder = X3D71Config; break;
690 case DevFmtAmbi3D:
691 /* For DevFmtAmbi3D, the ambisonic order is already set. */
692 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
693 const auto acnmap = GetAmbiLayout(device->mAmbiLayout).first(count);
694 const auto n3dscale = GetAmbiScales(device->mAmbiScale);
696 std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
697 [n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
698 { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
699 AllocChannels(device, count, 0);
700 device->m2DMixing = false;
702 float avg_dist{};
703 if(auto distopt = device->configValue<float>("decoder", "speaker-dist"))
704 avg_dist = *distopt;
705 else if(auto delayopt = device->configValue<float>("decoder", "nfc-ref-delay"))
707 WARN("nfc-ref-delay is deprecated, use speaker-dist instead\n");
708 avg_dist = *delayopt * SpeedOfSoundMetersPerSec;
711 TRACE("%u%s order ambisonic output (%s layout, %s scaling)\n", device->mAmbiOrder,
712 GetCounterSuffix(device->mAmbiOrder), GetLayoutName(device->mAmbiLayout),
713 GetScalingName(device->mAmbiScale));
714 InitNearFieldCtrl(device, avg_dist, device->mAmbiOrder, true);
715 return;
719 const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
720 Ambi2DChannelsFromOrder(decoder.mOrder)};
721 const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
722 std::vector<ChannelDec> chancoeffs, chancoeffslf;
723 for(size_t i{0u};i < decoder.mChannels.size();++i)
725 const size_t idx{device->channelIdxByName(decoder.mChannels[i])};
726 if(idx == InvalidChannelIndex)
728 ERR("Failed to find %s channel in device\n",
729 GetLabelFromChannel(decoder.mChannels[i]));
730 continue;
733 auto ordermap = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::OrderFromChannel}
734 : al::span<const uint8_t>{AmbiIndex::OrderFrom2DChannel};
736 chancoeffs.resize(std::max(chancoeffs.size(), idx+1_zu), ChannelDec{});
737 al::span<const float,MaxAmbiChannels> src{decoder.mCoeffs[i]};
738 al::span<float,MaxAmbiChannels> dst{chancoeffs[idx]};
739 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
740 dst[ambichan] = src[ambichan] * decoder.mOrderGain[ordermap[ambichan]];
742 if(!dual_band)
743 continue;
745 chancoeffslf.resize(std::max(chancoeffslf.size(), idx+1_zu), ChannelDec{});
746 src = decoder.mCoeffsLF[i];
747 dst = chancoeffslf[idx];
748 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
749 dst[ambichan] = src[ambichan] * decoder.mOrderGainLF[ordermap[ambichan]];
752 /* For non-DevFmtAmbi3D, set the ambisonic order. */
753 device->mAmbiOrder = decoder.mOrder;
754 device->m2DMixing = !decoder.mIs3D;
756 const auto acnmap = decoder.mIs3D ? al::span{AmbiIndex::FromACN}.first(ambicount)
757 : al::span{AmbiIndex::FromACN2D}.first(ambicount);
758 const auto coeffscale = GetAmbiScales(decoder.mScaling);
759 std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
760 [coeffscale](const uint8_t &acn) noexcept
761 { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
762 AllocChannels(device, ambicount, device->channelsFromFmt());
764 std::unique_ptr<FrontStablizer> stablizer;
765 if(stablize)
767 /* Only enable the stablizer if the decoder does not output to the
768 * front-center channel.
770 const size_t cidx{device->RealOut.ChannelIndex[FrontCenter]};
771 bool hasfc{false};
772 if(cidx < chancoeffs.size())
774 for(const auto &coeff : chancoeffs[cidx])
775 hasfc |= coeff != 0.0f;
777 if(!hasfc && cidx < chancoeffslf.size())
779 for(const auto &coeff : chancoeffslf[cidx])
780 hasfc |= coeff != 0.0f;
782 if(!hasfc)
784 stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
785 TRACE("Front stablizer enabled\n");
789 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
790 !dual_band ? "single" : "dual",
791 (decoder.mOrder > 3) ? "fourth" :
792 (decoder.mOrder > 2) ? "third" :
793 (decoder.mOrder > 1) ? "second" : "first",
794 decoder.mIs3D ? " periphonic" : "");
795 device->AmbiDecoder = BFormatDec::Create(ambicount, chancoeffs, chancoeffslf,
796 device->mXOverFreq/static_cast<float>(device->Frequency), std::move(stablizer));
799 void InitHrtfPanning(al::Device *device)
801 static constexpr float Deg180{al::numbers::pi_v<float>};
802 static constexpr float Deg_90{Deg180 / 2.0f /* 90 degrees*/};
803 static constexpr float Deg_45{Deg_90 / 2.0f /* 45 degrees*/};
804 static constexpr float Deg135{Deg_45 * 3.0f /*135 degrees*/};
805 static constexpr float Deg_21{3.648638281e-01f /* 20~ 21 degrees*/};
806 static constexpr float Deg_32{5.535743589e-01f /* 31~ 32 degrees*/};
807 static constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
808 static constexpr float Deg_58{1.017221968e+00f /* 58~ 59 degrees*/};
809 static constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
810 static constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
811 static constexpr float Deg122{2.124370686e+00f /*121~122 degrees*/};
812 static constexpr std::array AmbiPoints1O{
813 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
814 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
815 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
816 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
817 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
818 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
819 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
820 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
822 static constexpr std::array AmbiPoints2O{
823 AngularPoint{EvRadians{-Deg_32}, AzRadians{ 0.0f}},
824 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_58}},
825 AngularPoint{EvRadians{ Deg_58}, AzRadians{ Deg_90}},
826 AngularPoint{EvRadians{ Deg_32}, AzRadians{ 0.0f}},
827 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg122}},
828 AngularPoint{EvRadians{-Deg_58}, AzRadians{-Deg_90}},
829 AngularPoint{EvRadians{-Deg_32}, AzRadians{ Deg180}},
830 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg122}},
831 AngularPoint{EvRadians{ Deg_58}, AzRadians{-Deg_90}},
832 AngularPoint{EvRadians{ Deg_32}, AzRadians{ Deg180}},
833 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_58}},
834 AngularPoint{EvRadians{-Deg_58}, AzRadians{ Deg_90}},
836 static constexpr std::array AmbiPoints3O{
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{-Deg_69}, AzRadians{ Deg_90}},
841 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_69}},
842 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg111}},
843 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_69}},
844 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg111}},
845 AngularPoint{EvRadians{ Deg_21}, AzRadians{ 0.0f}},
846 AngularPoint{EvRadians{ Deg_21}, AzRadians{ Deg180}},
847 AngularPoint{EvRadians{-Deg_21}, AzRadians{ 0.0f}},
848 AngularPoint{EvRadians{-Deg_21}, AzRadians{ Deg180}},
849 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
850 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
851 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
852 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
853 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
854 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
855 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
856 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
858 static constexpr std::array AmbiMatrix1O{
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},
866 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f},
868 static constexpr std::array AmbiMatrix2O{
869 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},
870 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},
871 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},
872 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},
873 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},
874 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},
875 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},
876 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},
877 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},
878 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},
879 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},
880 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},
882 static constexpr std::array AmbiMatrix3O{
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, -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},
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, -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},
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, 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},
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},
902 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},
904 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain1O{
905 /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
907 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain2O{
908 /*ENRGY*/ 1.825741858e+00f, 1.414213562e+00f, 7.302967433e-01f
909 /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
910 /*RMS 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f*/
912 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain3O{
913 /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
914 /*AMP*/ 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f
915 /*RMS 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f*/
918 static_assert(AmbiPoints1O.size() == AmbiMatrix1O.size(), "First-Order Ambisonic HRTF mismatch");
919 static_assert(AmbiPoints2O.size() == AmbiMatrix2O.size(), "Second-Order Ambisonic HRTF mismatch");
920 static_assert(AmbiPoints3O.size() == AmbiMatrix3O.size(), "Third-Order Ambisonic HRTF mismatch");
922 /* A 700hz crossover frequency provides tighter sound imaging at the sweet
923 * spot with ambisonic decoding, as the distance between the ears is closer
924 * to half this frequency wavelength, which is the optimal point where the
925 * response should change between optimizing phase vs volume. Normally this
926 * tighter imaging is at the cost of a smaller sweet spot, but since the
927 * listener is fixed in the center of the HRTF responses for the decoder,
928 * we don't have to worry about ever being out of the sweet spot.
930 * A better option here may be to have the head radius as part of the HRTF
931 * data set and calculate the optimal crossover frequency from that.
933 device->mXOverFreq = 700.0f;
935 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
936 * and it eases the CPU/memory load.
938 device->mRenderMode = RenderMode::Hrtf;
939 uint ambi_order{1};
940 if(auto modeopt = device->configValue<std::string>({}, "hrtf-mode"))
942 struct HrtfModeEntry {
943 std::string_view name;
944 RenderMode mode;
945 uint order;
947 constexpr std::array hrtf_modes{
948 HrtfModeEntry{"full"sv, RenderMode::Hrtf, 1},
949 HrtfModeEntry{"ambi1"sv, RenderMode::Normal, 1},
950 HrtfModeEntry{"ambi2"sv, RenderMode::Normal, 2},
951 HrtfModeEntry{"ambi3"sv, RenderMode::Normal, 3},
954 std::string_view mode{*modeopt};
955 if(al::case_compare(mode, "basic"sv) == 0)
957 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", modeopt->c_str(), "ambi2");
958 mode = "ambi2";
961 auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
962 { return al::case_compare(mode, entry.name) == 0; };
963 auto iter = std::find_if(hrtf_modes.begin(), hrtf_modes.end(), match_entry);
964 if(iter == hrtf_modes.end())
965 ERR("Unexpected hrtf-mode: %s\n", modeopt->c_str());
966 else
968 device->mRenderMode = iter->mode;
969 ambi_order = iter->order;
972 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
973 GetCounterSuffix(ambi_order), (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
974 device->mHrtfName.c_str());
976 bool perHrirMin{false};
977 auto AmbiPoints = al::span{AmbiPoints1O}.subspan(0);
978 auto AmbiMatrix = al::span{AmbiMatrix1O}.subspan(0);
979 auto AmbiOrderHFGain = al::span{AmbiOrderHFGain1O};
980 if(ambi_order >= 3)
982 perHrirMin = true;
983 AmbiPoints = AmbiPoints3O;
984 AmbiMatrix = AmbiMatrix3O;
985 AmbiOrderHFGain = AmbiOrderHFGain3O;
987 else if(ambi_order == 2)
989 AmbiPoints = AmbiPoints2O;
990 AmbiMatrix = AmbiMatrix2O;
991 AmbiOrderHFGain = AmbiOrderHFGain2O;
993 device->mAmbiOrder = ambi_order;
994 device->m2DMixing = false;
996 const size_t count{AmbiChannelsFromOrder(ambi_order)};
997 const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
998 std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
999 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; });
1000 AllocChannels(device, count, device->channelsFromFmt());
1002 HrtfStore *Hrtf{device->mHrtf.get()};
1003 auto hrtfstate = DirectHrtfState::Create(count);
1004 hrtfstate->build(Hrtf, device->mIrSize, perHrirMin, AmbiPoints, AmbiMatrix, device->mXOverFreq,
1005 AmbiOrderHFGain);
1006 device->mHrtfState = std::move(hrtfstate);
1008 InitNearFieldCtrl(device, Hrtf->mFields[0].distance, ambi_order, true);
1011 void InitUhjPanning(al::Device *device)
1013 /* UHJ is always 2D first-order. */
1014 static constexpr size_t count{Ambi2DChannelsFromOrder(1)};
1016 device->mAmbiOrder = 1;
1017 device->m2DMixing = true;
1019 const auto acnmap = al::span{AmbiIndex::FromFuMa2D}.first<count>();
1020 std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
1021 [](const uint8_t &acn) noexcept -> BFChannelConfig
1022 { return BFChannelConfig{1.0f/AmbiScale::FromUHJ[acn], acn}; });
1023 AllocChannels(device, count, device->channelsFromFmt());
1026 } // namespace
1028 void aluInitRenderer(al::Device *device, int hrtf_id, std::optional<StereoEncoding> stereomode)
1030 /* Hold the HRTF the device last used, in case it's used again. */
1031 HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
1033 device->mHrtfState = nullptr;
1034 device->mHrtf = nullptr;
1035 device->mIrSize = 0;
1036 device->mHrtfName.clear();
1037 device->mXOverFreq = 400.0f;
1038 device->m2DMixing = false;
1039 device->mRenderMode = RenderMode::Normal;
1041 if(device->FmtChans != DevFmtStereo)
1043 old_hrtf = nullptr;
1044 if(stereomode && *stereomode == StereoEncoding::Hrtf)
1045 device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1047 const char *layout{nullptr};
1048 switch(device->FmtChans)
1050 case DevFmtQuad: layout = "quad"; break;
1051 case DevFmtX51: layout = "surround51"; break;
1052 case DevFmtX61: layout = "surround61"; break;
1053 case DevFmtX71: layout = "surround71"; break;
1054 case DevFmtX714: layout = "surround714"; break;
1055 case DevFmtX7144: layout = "surround7144"; break;
1056 case DevFmtX3D71: layout = "surround3d71"; break;
1057 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1058 case DevFmtMono:
1059 case DevFmtStereo:
1060 case DevFmtAmbi3D:
1061 break;
1064 std::unique_ptr<DecoderConfig<DualBand,MaxOutputChannels>> decoder_store;
1065 DecoderView decoder{};
1066 std::array<float,MaxOutputChannels> speakerdists{};
1067 auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
1069 AmbDecConf conf{};
1070 if(auto err = conf.load(config))
1072 ERR("Failed to load layout file %s\n", config);
1073 ERR(" %s\n", err->c_str());
1074 return false;
1076 if(conf.Speakers.size() > MaxOutputChannels)
1078 ERR("Unsupported decoder speaker count %zu (max %zu)\n", conf.Speakers.size(),
1079 MaxOutputChannels);
1080 return false;
1082 if(conf.ChanMask > Ambi3OrderMask)
1084 ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
1085 Ambi3OrderMask);
1086 return false;
1089 TRACE("Using %s decoder: \"%s\"\n", DevFmtChannelsString(device->FmtChans),
1090 conf.Description.c_str());
1091 device->mXOverFreq = std::clamp(conf.XOverFreq, 100.0f, 1000.0f);
1093 decoder_store = std::make_unique<DecoderConfig<DualBand,MaxOutputChannels>>();
1094 decoder = MakeDecoderView(device, &conf, *decoder_store);
1096 const auto confspeakers = al::span{std::as_const(conf.Speakers)}
1097 .first(decoder.mChannels.size());
1098 std::transform(confspeakers.cbegin(), confspeakers.cend(), speakerdists.begin(),
1099 std::mem_fn(&AmbDecConf::SpeakerConf::Distance));
1100 return true;
1102 bool usingCustom{false};
1103 if(layout)
1105 if(auto decopt = device->configValue<std::string>("decoder", layout))
1106 usingCustom = load_config(decopt->c_str());
1108 if(!usingCustom && device->FmtChans != DevFmtAmbi3D)
1109 TRACE("Using built-in %s decoder\n", DevFmtChannelsString(device->FmtChans));
1111 /* Enable the stablizer only for formats that have front-left, front-
1112 * right, and front-center outputs.
1114 const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != InvalidChannelIndex
1115 && device->RealOut.ChannelIndex[FrontLeft] != InvalidChannelIndex
1116 && device->RealOut.ChannelIndex[FrontRight] != InvalidChannelIndex
1117 && device->getConfigValueBool({}, "front-stablizer", false)};
1118 const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", true)};
1119 InitPanning(device, hqdec, stablize, decoder);
1120 if(decoder)
1122 float accum_dist{0.0f}, spkr_count{0.0f};
1123 for(auto dist : speakerdists)
1125 if(dist > 0.0f)
1127 accum_dist += dist;
1128 spkr_count += 1.0f;
1132 const float avg_dist{(accum_dist > 0.0f && spkr_count > 0) ? accum_dist/spkr_count :
1133 device->configValue<float>("decoder", "speaker-dist").value_or(1.0f)};
1134 InitNearFieldCtrl(device, avg_dist, decoder.mOrder, decoder.mIs3D);
1136 if(spkr_count > 0)
1137 InitDistanceComp(device, decoder.mChannels, speakerdists);
1139 if(auto *ambidec{device->AmbiDecoder.get()})
1141 device->PostProcess = ambidec->hasStablizer() ? &al::Device::ProcessAmbiDecStablized
1142 : &al::Device::ProcessAmbiDec;
1144 return;
1148 /* If HRTF is explicitly requested, or if there's no explicit request and
1149 * the device is headphones, try to enable it.
1151 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Hrtf
1152 || (!stereomode && device->Flags.test(DirectEar)))
1154 if(device->mHrtfList.empty())
1155 device->enumerateHrtfs();
1157 if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
1159 const std::string_view hrtfname{device->mHrtfList[static_cast<uint>(hrtf_id)]};
1160 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1162 device->mHrtf = std::move(hrtf);
1163 device->mHrtfName = hrtfname;
1167 if(!device->mHrtf)
1169 for(const std::string_view hrtfname : device->mHrtfList)
1171 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1173 device->mHrtf = std::move(hrtf);
1174 device->mHrtfName = hrtfname;
1175 break;
1180 if(device->mHrtf)
1182 old_hrtf = nullptr;
1184 HrtfStore *hrtf{device->mHrtf.get()};
1185 device->mIrSize = hrtf->mIrSize;
1186 if(auto hrtfsizeopt = device->configValue<uint>({}, "hrtf-size"))
1188 if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
1189 device->mIrSize = std::max(*hrtfsizeopt, MinIrLength);
1192 InitHrtfPanning(device);
1193 device->PostProcess = &al::Device::ProcessHrtf;
1194 device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
1195 return;
1198 old_hrtf = nullptr;
1200 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Uhj)
1202 auto ftype = std::string_view{};
1203 switch(UhjEncodeQuality)
1205 case UhjQualityType::IIR:
1206 device->mUhjEncoder = std::make_unique<UhjEncoderIIR>();
1207 ftype = "IIR"sv;
1208 break;
1209 case UhjQualityType::FIR256:
1210 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength256>>();
1211 ftype = "FIR-256"sv;
1212 break;
1213 case UhjQualityType::FIR512:
1214 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength512>>();
1215 ftype = "FIR-512"sv;
1216 break;
1218 assert(device->mUhjEncoder != nullptr);
1220 TRACE("UHJ enabled (%.*s encoder)\n", al::sizei(ftype), ftype.data());
1221 InitUhjPanning(device);
1222 device->PostProcess = &al::Device::ProcessUhj;
1223 return;
1226 device->mRenderMode = RenderMode::Pairwise;
1227 if(device->Type != DeviceType::Loopback)
1229 if(auto cflevopt = device->configValue<int>({}, "cf_level"))
1231 if(*cflevopt > 0 && *cflevopt <= 6)
1233 auto bs2b = std::make_unique<Bs2b::bs2b>();
1234 bs2b->set_params(*cflevopt, static_cast<int>(device->Frequency));
1235 device->Bs2b = std::move(bs2b);
1236 TRACE("BS2B enabled\n");
1237 InitPanning(device);
1238 device->PostProcess = &al::Device::ProcessBs2b;
1239 return;
1244 TRACE("Stereo rendering\n");
1245 InitPanning(device);
1246 device->PostProcess = &al::Device::ProcessAmbiDec;
1250 void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
1252 DeviceBase *device{context->mDevice};
1253 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
1255 slot->mWetBuffer.resize(count);
1257 const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
1258 const auto iter = std::transform(acnmap.cbegin(), acnmap.cend(), slot->Wet.AmbiMap.begin(),
1259 [](const uint8_t &acn) noexcept -> BFChannelConfig { return BFChannelConfig{1.0f, acn}; });
1260 std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
1261 slot->Wet.Buffer = slot->mWetBuffer;