Don't lock the device state when preparing a new effect state
[openal-soft.git] / alc / panning.cpp
blob83a410ba24e23f0ea7e664727c55e0c9b1a148e8
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)";
129 std::unique_ptr<FrontStablizer> CreateStablizer(const size_t outchans, const uint srate)
131 auto stablizer = FrontStablizer::Create(outchans);
133 /* Initialize band-splitting filter for the mid signal, with a crossover at
134 * 5khz (could be higher).
136 stablizer->MidFilter.init(5000.0f / static_cast<float>(srate));
137 for(auto &filter : stablizer->ChannelFilters)
138 filter = stablizer->MidFilter;
140 return stablizer;
143 void AllocChannels(ALCdevice *device, const size_t main_chans, const size_t real_chans)
145 TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans, real_chans);
147 /* Allocate extra channels for any post-filter output. */
148 const size_t num_chans{main_chans + real_chans};
150 TRACE("Allocating %zu channels, %zu bytes\n", num_chans,
151 num_chans*sizeof(device->MixBuffer[0]));
152 device->MixBuffer.resize(num_chans);
153 al::span<FloatBufferLine> buffer{device->MixBuffer};
155 device->Dry.Buffer = buffer.first(main_chans);
156 buffer = buffer.subspan(main_chans);
157 if(real_chans != 0)
159 device->RealOut.Buffer = buffer.first(real_chans);
160 buffer = buffer.subspan(real_chans);
162 else
163 device->RealOut.Buffer = device->Dry.Buffer;
167 using ChannelCoeffs = std::array<float,MaxAmbiChannels>;
168 enum DecoderMode : bool {
169 SingleBand = false,
170 DualBand = true
173 template<DecoderMode Mode, size_t N>
174 struct DecoderConfig;
176 template<size_t N>
177 struct DecoderConfig<SingleBand, N> {
178 uint8_t mOrder{};
179 bool mIs3D{};
180 std::array<Channel,N> mChannels{};
181 DevAmbiScaling mScaling{};
182 std::array<float,MaxAmbiOrder+1> mOrderGain{};
183 std::array<ChannelCoeffs,N> mCoeffs{};
186 template<size_t N>
187 struct DecoderConfig<DualBand, N> {
188 uint8_t mOrder{};
189 bool mIs3D{};
190 std::array<Channel,N> mChannels{};
191 DevAmbiScaling mScaling{};
192 std::array<float,MaxAmbiOrder+1> mOrderGain{};
193 std::array<ChannelCoeffs,N> mCoeffs{};
194 std::array<float,MaxAmbiOrder+1> mOrderGainLF{};
195 std::array<ChannelCoeffs,N> mCoeffsLF{};
198 template<>
199 struct DecoderConfig<DualBand, 0> {
200 uint8_t mOrder{};
201 bool mIs3D{};
202 al::span<const Channel> mChannels;
203 DevAmbiScaling mScaling{};
204 al::span<const float> mOrderGain;
205 al::span<const ChannelCoeffs> mCoeffs;
206 al::span<const float> mOrderGainLF;
207 al::span<const ChannelCoeffs> mCoeffsLF;
209 template<size_t N>
210 DecoderConfig& operator=(const DecoderConfig<SingleBand,N> &rhs) noexcept
212 mOrder = rhs.mOrder;
213 mIs3D = rhs.mIs3D;
214 mChannels = rhs.mChannels;
215 mScaling = rhs.mScaling;
216 mOrderGain = rhs.mOrderGain;
217 mCoeffs = rhs.mCoeffs;
218 mOrderGainLF = {};
219 mCoeffsLF = {};
220 return *this;
223 template<size_t N>
224 DecoderConfig& operator=(const DecoderConfig<DualBand,N> &rhs) noexcept
226 mOrder = rhs.mOrder;
227 mIs3D = rhs.mIs3D;
228 mChannels = rhs.mChannels;
229 mScaling = rhs.mScaling;
230 mOrderGain = rhs.mOrderGain;
231 mCoeffs = rhs.mCoeffs;
232 mOrderGainLF = rhs.mOrderGainLF;
233 mCoeffsLF = rhs.mCoeffsLF;
234 return *this;
237 explicit operator bool() const noexcept { return !mChannels.empty(); }
239 using DecoderView = DecoderConfig<DualBand, 0>;
242 void InitNearFieldCtrl(ALCdevice *device, const float ctrl_dist, const uint order, const bool is3d)
244 static const std::array<uint,MaxAmbiOrder+1> chans_per_order2d{{1, 2, 2, 2}};
245 static const std::array<uint,MaxAmbiOrder+1> chans_per_order3d{{1, 3, 5, 7}};
247 /* NFC is only used when AvgSpeakerDist is greater than 0. */
248 if(!device->getConfigValueBool("decoder", "nfc", false) || !(ctrl_dist > 0.0f))
249 return;
251 device->AvgSpeakerDist = std::clamp(ctrl_dist, 0.1f, 10.0f);
252 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
254 const float w1{SpeedOfSoundMetersPerSec /
255 (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
256 device->mNFCtrlFilter.init(w1);
258 auto iter = std::copy_n(is3d ? chans_per_order3d.begin() : chans_per_order2d.begin(), order+1u,
259 device->NumChannelsPerOrder.begin());
260 std::fill(iter, device->NumChannelsPerOrder.end(), 0u);
263 void InitDistanceComp(ALCdevice *device, const al::span<const Channel> channels,
264 const al::span<const float,MaxOutputChannels> dists)
266 const float maxdist{std::accumulate(dists.begin(), dists.end(), 0.0f,
267 [](const float a, const float b) noexcept -> float { return std::max(a, b); })};
269 if(!device->getConfigValueBool("decoder", "distance-comp", true) || !(maxdist > 0.0f))
270 return;
272 const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
274 struct DistCoeffs { uint Length{}; float Gain{}; };
275 std::vector<DistCoeffs> ChanDelay;
276 ChanDelay.reserve(device->RealOut.Buffer.size());
278 size_t total{0u};
279 for(size_t chidx{0};chidx < channels.size();++chidx)
281 const Channel ch{channels[chidx]};
282 const size_t idx{device->RealOut.ChannelIndex[ch]};
283 if(idx == InvalidChannelIndex)
284 continue;
286 const float distance{dists[chidx]};
288 /* Distance compensation only delays in steps of the sample rate. This
289 * is a bit less accurate since the delay time falls to the nearest
290 * sample time, but it's far simpler as it doesn't have to deal with
291 * phase offsets. This means at 48khz, for instance, the distance delay
292 * will be in steps of about 7 millimeters.
294 float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
295 if(delay > float{DistanceComp::MaxDelay-1})
297 ERR("Delay for channel %zu (%s) exceeds buffer length (%f > %d)\n", idx,
298 GetLabelFromChannel(ch), delay, DistanceComp::MaxDelay-1);
299 delay = float{DistanceComp::MaxDelay-1};
302 ChanDelay.resize(std::max(ChanDelay.size(), idx+1_uz));
303 ChanDelay[idx].Length = static_cast<uint>(delay);
304 ChanDelay[idx].Gain = distance / maxdist;
305 TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
306 ChanDelay[idx].Length, ChanDelay[idx].Gain);
308 /* Round up to the next 4th sample, so each channel buffer starts
309 * 16-byte aligned.
311 total += RoundUp(ChanDelay[idx].Length, 4);
314 if(total > 0)
316 auto chandelays = DistanceComp::Create(total);
317 auto chanbuffer = chandelays->mSamples.begin();
319 auto set_bufptr = [&chanbuffer](const DistCoeffs &data)
321 DistanceComp::ChanData ret{};
322 ret.Buffer = al::span{chanbuffer, data.Length};
323 ret.Gain = data.Gain;
324 chanbuffer += ptrdiff_t(RoundUp(data.Length, 4));
325 return ret;
327 std::transform(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
328 set_bufptr);
329 device->ChannelDelays = std::move(chandelays);
334 constexpr auto GetAmbiScales(DevAmbiScaling scaletype) noexcept
336 if(scaletype == DevAmbiScaling::FuMa) return al::span{AmbiScale::FromFuMa};
337 if(scaletype == DevAmbiScaling::SN3D) return al::span{AmbiScale::FromSN3D};
338 return al::span{AmbiScale::FromN3D};
341 constexpr auto GetAmbiLayout(DevAmbiLayout layouttype) noexcept
343 if(layouttype == DevAmbiLayout::FuMa) return al::span{AmbiIndex::FromFuMa};
344 return al::span{AmbiIndex::FromACN};
348 DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf,
349 DecoderConfig<DualBand,MaxOutputChannels> &decoder)
351 DecoderView ret{};
353 decoder.mOrder = (conf->ChanMask > Ambi3OrderMask) ? uint8_t{4} :
354 (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
355 (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
356 decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
358 switch(conf->CoeffScale)
360 case AmbDecScale::Unset: ASSUME(false); break;
361 case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
362 case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
363 case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
366 const auto hfordermin = std::min(conf->HFOrderGain.size(), decoder.mOrderGain.size());
367 std::copy_n(conf->HFOrderGain.begin(), hfordermin, decoder.mOrderGain.begin());
368 const auto lfordermin = std::min(conf->LFOrderGain.size(), decoder.mOrderGainLF.size());
369 std::copy_n(conf->LFOrderGain.begin(), lfordermin, decoder.mOrderGainLF.begin());
371 const auto num_coeffs = decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder)
372 : Ambi2DChannelsFromOrder(decoder.mOrder);
373 const auto idx_map = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::FromACN}
374 : al::span<const uint8_t>{AmbiIndex::FromACN2D};
375 const auto hfmatrix = conf->HFMatrix;
376 const auto lfmatrix = conf->LFMatrix;
378 uint chan_count{0};
379 for(auto &speaker : al::span{std::as_const(conf->Speakers)})
381 /* NOTE: AmbDec does not define any standard speaker names, however
382 * for this to work we have to by able to find the output channel
383 * the speaker definition corresponds to. Therefore, OpenAL Soft
384 * requires these channel labels to be recognized:
386 * LF = Front left
387 * RF = Front right
388 * LS = Side left
389 * RS = Side right
390 * LB = Back left
391 * RB = Back right
392 * CE = Front center
393 * CB = Back center
394 * LFT = Top front left
395 * RFT = Top front right
396 * LBT = Top back left
397 * RBT = Top back right
398 * LFB = Bottom front left
399 * RFB = Bottom front right
400 * LBB = Bottom back left
401 * RBB = Bottom back right
403 * Additionally, surround51 will acknowledge back speakers for side
404 * channels, to avoid issues with an ambdec expecting 5.1 to use the
405 * back channels.
407 Channel ch{};
408 if(speaker.Name == "LF"sv)
409 ch = FrontLeft;
410 else if(speaker.Name == "RF"sv)
411 ch = FrontRight;
412 else if(speaker.Name == "CE"sv)
413 ch = FrontCenter;
414 else if(speaker.Name == "LS"sv)
415 ch = SideLeft;
416 else if(speaker.Name == "RS"sv)
417 ch = SideRight;
418 else if(speaker.Name == "LB"sv)
419 ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
420 else if(speaker.Name == "RB"sv)
421 ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
422 else if(speaker.Name == "CB"sv)
423 ch = BackCenter;
424 else if(speaker.Name == "LFT"sv)
425 ch = TopFrontLeft;
426 else if(speaker.Name == "RFT"sv)
427 ch = TopFrontRight;
428 else if(speaker.Name == "LBT"sv)
429 ch = TopBackLeft;
430 else if(speaker.Name == "RBT"sv)
431 ch = TopBackRight;
432 else if(speaker.Name == "LFB"sv)
433 ch = BottomFrontLeft;
434 else if(speaker.Name == "RFB"sv)
435 ch = BottomFrontRight;
436 else if(speaker.Name == "LBB"sv)
437 ch = BottomBackLeft;
438 else if(speaker.Name == "RBB"sv)
439 ch = BottomBackRight;
440 else
442 int idx{};
443 char c{};
444 if(sscanf(speaker.Name.c_str(), "AUX%d%c", &idx, &c) != 1 || idx < 0
445 || idx >= MaxChannels-Aux0)
447 ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
448 continue;
450 ch = static_cast<Channel>(Aux0+idx);
453 decoder.mChannels[chan_count] = ch;
454 for(size_t dst{0};dst < num_coeffs;++dst)
456 const size_t src{idx_map[dst]};
457 decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
459 if(conf->FreqBands > 1)
461 for(size_t dst{0};dst < num_coeffs;++dst)
463 const size_t src{idx_map[dst]};
464 decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
467 ++chan_count;
470 if(chan_count > 0)
472 ret.mOrder = decoder.mOrder;
473 ret.mIs3D = decoder.mIs3D;
474 ret.mScaling = decoder.mScaling;
475 ret.mChannels = al::span{decoder.mChannels}.first(chan_count);
476 ret.mOrderGain = decoder.mOrderGain;
477 ret.mCoeffs = al::span{decoder.mCoeffs}.first(chan_count);
478 if(conf->FreqBands > 1)
480 ret.mOrderGainLF = decoder.mOrderGainLF;
481 ret.mCoeffsLF = al::span{decoder.mCoeffsLF}.first(chan_count);
484 return ret;
487 constexpr DecoderConfig<SingleBand, 1> MonoConfig{
488 0, false, {{FrontCenter}},
489 DevAmbiScaling::N3D,
490 {{1.0f}},
491 {{ {{1.0f}} }}
493 constexpr DecoderConfig<SingleBand, 2> StereoConfig{
494 1, false, {{FrontLeft, FrontRight}},
495 DevAmbiScaling::N3D,
496 {{1.0f, 1.0f}},
498 {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
499 {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
502 constexpr DecoderConfig<DualBand, 4> QuadConfig{
503 1, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
504 DevAmbiScaling::N3D,
505 /*HF*/{{1.41421356e+0f, 1.00000000e+0f}},
507 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
508 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
509 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
510 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
512 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
514 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
515 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
516 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
517 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
520 constexpr DecoderConfig<DualBand, 5> X51Config{
521 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
522 DevAmbiScaling::FuMa,
523 /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
525 {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
526 {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
527 {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
528 {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
529 {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
531 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
533 {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
534 {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
535 {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
536 {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
537 {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
540 constexpr DecoderConfig<SingleBand, 5> X61Config{
541 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
542 DevAmbiScaling::N3D,
543 {{1.0f, 1.0f, 1.0f}},
545 {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
546 {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
547 {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
548 {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
549 {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
552 constexpr DecoderConfig<DualBand, 6> X71Config{
553 2, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
554 DevAmbiScaling::N3D,
555 /*HF*/{{1.41421356e+0f, 1.22474487e+0f, 7.07106781e-1f}},
557 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
558 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
559 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
560 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
561 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
562 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
564 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
566 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
567 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
568 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
569 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
570 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
571 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
574 constexpr DecoderConfig<DualBand, 6> X3D71Config{
575 1, true, {{Aux0, SideLeft, FrontLeft, FrontRight, SideRight, Aux1}},
576 DevAmbiScaling::N3D,
577 /*HF*/{{1.73205081e+0f, 1.00000000e+0f}},
579 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
580 {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
581 {{1.666666667e-01f, 2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
582 {{1.666666667e-01f, -2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
583 {{1.666666667e-01f, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
584 {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
586 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
588 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
589 {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
590 {{1.666666667e-01f, 2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
591 {{1.666666667e-01f, -2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
592 {{1.666666667e-01f, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
593 {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
596 constexpr DecoderConfig<SingleBand, 10> X714Config{
597 1, true, {{FrontLeft, FrontRight, SideLeft, SideRight, BackLeft, BackRight, TopFrontLeft, TopFrontRight, TopBackLeft, TopBackRight }},
598 DevAmbiScaling::N3D,
599 {{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
601 {{1.27149251e-01f, 7.63047539e-02f, -3.64373750e-02f, 1.59700680e-01f}},
602 {{1.07005418e-01f, -7.67638760e-02f, -4.92129762e-02f, 1.29012797e-01f}},
603 {{1.26400196e-01f, 1.77494694e-01f, -3.71203389e-02f, 0.00000000e+00f}},
604 {{1.26396516e-01f, -1.77488059e-01f, -3.71297878e-02f, 0.00000000e+00f}},
605 {{1.06996956e-01f, 7.67615256e-02f, -4.92166307e-02f, -1.29001640e-01f}},
606 {{1.27145671e-01f, -7.63003471e-02f, -3.64353304e-02f, -1.59697510e-01f}},
607 {{8.80919747e-02f, 7.48940670e-02f, 9.08786244e-02f, 6.22527183e-02f}},
608 {{1.57880745e-01f, -7.28755272e-02f, 1.82364187e-01f, 8.74240284e-02f}},
609 {{1.57892225e-01f, 7.28944768e-02f, 1.82363474e-01f, -8.74301086e-02f}},
610 {{8.80892603e-02f, -7.48948724e-02f, 9.08779842e-02f, -6.22480443e-02f}},
613 constexpr DecoderConfig<DualBand, 14> X7144Config{
614 1, true, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight, TopBackLeft, TopFrontLeft, TopFrontRight, TopBackRight, BottomBackLeft, BottomFrontLeft, BottomFrontRight, BottomBackRight}},
615 DevAmbiScaling::N3D,
616 /*HF*/{{2.64575131e+0f, 1.52752523e+0f}},
618 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
619 {{7.14285714e-02f, 1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
620 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
621 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
622 {{7.14285714e-02f, -1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
623 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
624 {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, -5.88235294e-02f}},
625 {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
626 {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
627 {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, -5.88235294e-02f}},
628 {{7.14285714e-02f, 5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
629 {{7.14285714e-02f, 5.88235294e-02f, -1.25000000e-01f, 5.88235294e-02f}},
630 {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, 5.88235294e-02f}},
631 {{7.14285714e-02f, -5.88235294e-02f, -1.25000000e-01f, -5.88235294e-02f}},
633 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
635 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
636 {{7.14285714e-02f, 1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
637 {{7.14285714e-02f, 5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
638 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, 8.82352941e-02f}},
639 {{7.14285714e-02f, -1.01885342e-01f, 0.00000000e+00f, 0.00000000e+00f}},
640 {{7.14285714e-02f, -5.09426708e-02f, 0.00000000e+00f, -8.82352941e-02f}},
641 {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, -5.88235294e-02f}},
642 {{7.14285714e-02f, 5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
643 {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, 5.88235294e-02f}},
644 {{7.14285714e-02f, -5.88235294e-02f, 1.25000000e-01f, -5.88235294e-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}},
652 void InitPanning(ALCdevice *device, const bool hqdec=false, const bool stablize=false,
653 DecoderView decoder={})
655 if(!decoder)
657 switch(device->FmtChans)
659 case DevFmtMono: decoder = MonoConfig; break;
660 case DevFmtStereo: decoder = StereoConfig; break;
661 case DevFmtQuad: decoder = QuadConfig; break;
662 case DevFmtX51: decoder = X51Config; break;
663 case DevFmtX61: decoder = X61Config; break;
664 case DevFmtX71: decoder = X71Config; break;
665 case DevFmtX714: decoder = X714Config; break;
666 case DevFmtX7144: decoder = X7144Config; break;
667 case DevFmtX3D71: decoder = X3D71Config; break;
668 case DevFmtAmbi3D:
669 /* For DevFmtAmbi3D, the ambisonic order is already set. */
670 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
671 const auto acnmap = GetAmbiLayout(device->mAmbiLayout).first(count);
672 const auto n3dscale = GetAmbiScales(device->mAmbiScale);
674 std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
675 [n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
676 { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
677 AllocChannels(device, count, 0);
678 device->m2DMixing = false;
680 float avg_dist{};
681 if(auto distopt = device->configValue<float>("decoder", "speaker-dist"))
682 avg_dist = *distopt;
683 else if(auto delayopt = device->configValue<float>("decoder", "nfc-ref-delay"))
685 WARN("nfc-ref-delay is deprecated, use speaker-dist instead\n");
686 avg_dist = *delayopt * SpeedOfSoundMetersPerSec;
689 InitNearFieldCtrl(device, avg_dist, device->mAmbiOrder, true);
690 return;
694 const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
695 Ambi2DChannelsFromOrder(decoder.mOrder)};
696 const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
697 std::vector<ChannelDec> chancoeffs, chancoeffslf;
698 for(size_t i{0u};i < decoder.mChannels.size();++i)
700 const size_t idx{device->channelIdxByName(decoder.mChannels[i])};
701 if(idx == InvalidChannelIndex)
703 ERR("Failed to find %s channel in device\n",
704 GetLabelFromChannel(decoder.mChannels[i]));
705 continue;
708 auto ordermap = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::OrderFromChannel}
709 : al::span<const uint8_t>{AmbiIndex::OrderFrom2DChannel};
711 chancoeffs.resize(std::max(chancoeffs.size(), idx+1_zu), ChannelDec{});
712 al::span<const float,MaxAmbiChannels> src{decoder.mCoeffs[i]};
713 al::span<float,MaxAmbiChannels> dst{chancoeffs[idx]};
714 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
715 dst[ambichan] = src[ambichan] * decoder.mOrderGain[ordermap[ambichan]];
717 if(!dual_band)
718 continue;
720 chancoeffslf.resize(std::max(chancoeffslf.size(), idx+1_zu), ChannelDec{});
721 src = decoder.mCoeffsLF[i];
722 dst = chancoeffslf[idx];
723 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
724 dst[ambichan] = src[ambichan] * decoder.mOrderGainLF[ordermap[ambichan]];
727 /* For non-DevFmtAmbi3D, set the ambisonic order. */
728 device->mAmbiOrder = decoder.mOrder;
729 device->m2DMixing = !decoder.mIs3D;
731 const auto acnmap = decoder.mIs3D ? al::span{AmbiIndex::FromACN}.first(ambicount)
732 : al::span{AmbiIndex::FromACN2D}.first(ambicount);
733 const auto coeffscale = GetAmbiScales(decoder.mScaling);
734 std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
735 [coeffscale](const uint8_t &acn) noexcept
736 { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
737 AllocChannels(device, ambicount, device->channelsFromFmt());
739 std::unique_ptr<FrontStablizer> stablizer;
740 if(stablize)
742 /* Only enable the stablizer if the decoder does not output to the
743 * front-center channel.
745 const size_t cidx{device->RealOut.ChannelIndex[FrontCenter]};
746 bool hasfc{false};
747 if(cidx < chancoeffs.size())
749 for(const auto &coeff : chancoeffs[cidx])
750 hasfc |= coeff != 0.0f;
752 if(!hasfc && cidx < chancoeffslf.size())
754 for(const auto &coeff : chancoeffslf[cidx])
755 hasfc |= coeff != 0.0f;
757 if(!hasfc)
759 stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
760 TRACE("Front stablizer enabled\n");
764 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
765 !dual_band ? "single" : "dual",
766 (decoder.mOrder > 3) ? "fourth" :
767 (decoder.mOrder > 2) ? "third" :
768 (decoder.mOrder > 1) ? "second" : "first",
769 decoder.mIs3D ? " periphonic" : "");
770 device->AmbiDecoder = BFormatDec::Create(ambicount, chancoeffs, chancoeffslf,
771 device->mXOverFreq/static_cast<float>(device->Frequency), std::move(stablizer));
774 void InitHrtfPanning(ALCdevice *device)
776 static constexpr float Deg180{al::numbers::pi_v<float>};
777 static constexpr float Deg_90{Deg180 / 2.0f /* 90 degrees*/};
778 static constexpr float Deg_45{Deg_90 / 2.0f /* 45 degrees*/};
779 static constexpr float Deg135{Deg_45 * 3.0f /*135 degrees*/};
780 static constexpr float Deg_21{3.648638281e-01f /* 20~ 21 degrees*/};
781 static constexpr float Deg_32{5.535743589e-01f /* 31~ 32 degrees*/};
782 static constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
783 static constexpr float Deg_58{1.017221968e+00f /* 58~ 59 degrees*/};
784 static constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
785 static constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
786 static constexpr float Deg122{2.124370686e+00f /*121~122 degrees*/};
787 static constexpr std::array AmbiPoints1O{
788 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
789 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
790 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
791 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
792 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
793 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
794 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
795 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
797 static constexpr std::array AmbiPoints2O{
798 AngularPoint{EvRadians{-Deg_32}, AzRadians{ 0.0f}},
799 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_58}},
800 AngularPoint{EvRadians{ Deg_58}, AzRadians{ Deg_90}},
801 AngularPoint{EvRadians{ Deg_32}, AzRadians{ 0.0f}},
802 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg122}},
803 AngularPoint{EvRadians{-Deg_58}, AzRadians{-Deg_90}},
804 AngularPoint{EvRadians{-Deg_32}, AzRadians{ Deg180}},
805 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg122}},
806 AngularPoint{EvRadians{ Deg_58}, AzRadians{-Deg_90}},
807 AngularPoint{EvRadians{ Deg_32}, AzRadians{ Deg180}},
808 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_58}},
809 AngularPoint{EvRadians{-Deg_58}, AzRadians{ Deg_90}},
811 static constexpr std::array AmbiPoints3O{
812 AngularPoint{EvRadians{ Deg_69}, AzRadians{-Deg_90}},
813 AngularPoint{EvRadians{ Deg_69}, AzRadians{ Deg_90}},
814 AngularPoint{EvRadians{-Deg_69}, AzRadians{-Deg_90}},
815 AngularPoint{EvRadians{-Deg_69}, AzRadians{ Deg_90}},
816 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_69}},
817 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg111}},
818 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_69}},
819 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg111}},
820 AngularPoint{EvRadians{ Deg_21}, AzRadians{ 0.0f}},
821 AngularPoint{EvRadians{ Deg_21}, AzRadians{ Deg180}},
822 AngularPoint{EvRadians{-Deg_21}, AzRadians{ 0.0f}},
823 AngularPoint{EvRadians{-Deg_21}, AzRadians{ Deg180}},
824 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
825 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
826 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
827 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
828 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
829 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
830 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
831 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
833 static constexpr std::array AmbiMatrix1O{
834 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f},
835 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f},
836 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f},
837 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f},
838 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f},
839 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f},
840 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f},
841 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f},
843 static constexpr std::array AmbiMatrix2O{
844 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},
845 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},
846 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},
847 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},
848 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},
849 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},
850 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},
851 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},
852 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},
853 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},
854 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},
855 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},
857 static constexpr std::array AmbiMatrix3O{
858 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},
859 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},
860 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},
861 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},
862 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},
863 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},
864 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},
865 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},
866 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},
867 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},
868 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},
869 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},
870 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},
871 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},
872 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},
873 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},
874 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},
875 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},
876 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},
877 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},
879 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain1O{
880 /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
882 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain2O{
883 /*ENRGY*/ 1.825741858e+00f, 1.414213562e+00f, 7.302967433e-01f
884 /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
885 /*RMS 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f*/
887 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain3O{
888 /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
889 /*AMP*/ 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f
890 /*RMS 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f*/
893 static_assert(AmbiPoints1O.size() == AmbiMatrix1O.size(), "First-Order Ambisonic HRTF mismatch");
894 static_assert(AmbiPoints2O.size() == AmbiMatrix2O.size(), "Second-Order Ambisonic HRTF mismatch");
895 static_assert(AmbiPoints3O.size() == AmbiMatrix3O.size(), "Third-Order Ambisonic HRTF mismatch");
897 /* A 700hz crossover frequency provides tighter sound imaging at the sweet
898 * spot with ambisonic decoding, as the distance between the ears is closer
899 * to half this frequency wavelength, which is the optimal point where the
900 * response should change between optimizing phase vs volume. Normally this
901 * tighter imaging is at the cost of a smaller sweet spot, but since the
902 * listener is fixed in the center of the HRTF responses for the decoder,
903 * we don't have to worry about ever being out of the sweet spot.
905 * A better option here may be to have the head radius as part of the HRTF
906 * data set and calculate the optimal crossover frequency from that.
908 device->mXOverFreq = 700.0f;
910 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
911 * and it eases the CPU/memory load.
913 device->mRenderMode = RenderMode::Hrtf;
914 uint ambi_order{1};
915 if(auto modeopt = device->configValue<std::string>({}, "hrtf-mode"))
917 struct HrtfModeEntry {
918 std::string_view name;
919 RenderMode mode;
920 uint order;
922 constexpr std::array hrtf_modes{
923 HrtfModeEntry{"full"sv, RenderMode::Hrtf, 1},
924 HrtfModeEntry{"ambi1"sv, RenderMode::Normal, 1},
925 HrtfModeEntry{"ambi2"sv, RenderMode::Normal, 2},
926 HrtfModeEntry{"ambi3"sv, RenderMode::Normal, 3},
929 std::string_view mode{*modeopt};
930 if(al::case_compare(mode, "basic"sv) == 0)
932 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", modeopt->c_str(), "ambi2");
933 mode = "ambi2";
936 auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
937 { return al::case_compare(mode, entry.name) == 0; };
938 auto iter = std::find_if(hrtf_modes.begin(), hrtf_modes.end(), match_entry);
939 if(iter == hrtf_modes.end())
940 ERR("Unexpected hrtf-mode: %s\n", modeopt->c_str());
941 else
943 device->mRenderMode = iter->mode;
944 ambi_order = iter->order;
947 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
948 GetCounterSuffix(ambi_order), (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
949 device->mHrtfName.c_str());
951 bool perHrirMin{false};
952 auto AmbiPoints = al::span{AmbiPoints1O}.subspan(0);
953 auto AmbiMatrix = al::span{AmbiMatrix1O}.subspan(0);
954 auto AmbiOrderHFGain = al::span{AmbiOrderHFGain1O};
955 if(ambi_order >= 3)
957 perHrirMin = true;
958 AmbiPoints = AmbiPoints3O;
959 AmbiMatrix = AmbiMatrix3O;
960 AmbiOrderHFGain = AmbiOrderHFGain3O;
962 else if(ambi_order == 2)
964 AmbiPoints = AmbiPoints2O;
965 AmbiMatrix = AmbiMatrix2O;
966 AmbiOrderHFGain = AmbiOrderHFGain2O;
968 device->mAmbiOrder = ambi_order;
969 device->m2DMixing = false;
971 const size_t count{AmbiChannelsFromOrder(ambi_order)};
972 const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
973 std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
974 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; });
975 AllocChannels(device, count, device->channelsFromFmt());
977 HrtfStore *Hrtf{device->mHrtf.get()};
978 auto hrtfstate = DirectHrtfState::Create(count);
979 hrtfstate->build(Hrtf, device->mIrSize, perHrirMin, AmbiPoints, AmbiMatrix, device->mXOverFreq,
980 AmbiOrderHFGain);
981 device->mHrtfState = std::move(hrtfstate);
983 InitNearFieldCtrl(device, Hrtf->mFields[0].distance, ambi_order, true);
986 void InitUhjPanning(ALCdevice *device)
988 /* UHJ is always 2D first-order. */
989 static constexpr size_t count{Ambi2DChannelsFromOrder(1)};
991 device->mAmbiOrder = 1;
992 device->m2DMixing = true;
994 const auto acnmap = al::span{AmbiIndex::FromFuMa2D}.first<count>();
995 std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
996 [](const uint8_t &acn) noexcept -> BFChannelConfig
997 { return BFChannelConfig{1.0f/AmbiScale::FromUHJ[acn], acn}; });
998 AllocChannels(device, count, device->channelsFromFmt());
1001 } // namespace
1003 void aluInitRenderer(ALCdevice *device, int hrtf_id, std::optional<StereoEncoding> stereomode)
1005 /* Hold the HRTF the device last used, in case it's used again. */
1006 HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
1008 device->mHrtfState = nullptr;
1009 device->mHrtf = nullptr;
1010 device->mIrSize = 0;
1011 device->mHrtfName.clear();
1012 device->mXOverFreq = 400.0f;
1013 device->m2DMixing = false;
1014 device->mRenderMode = RenderMode::Normal;
1016 if(device->FmtChans != DevFmtStereo)
1018 old_hrtf = nullptr;
1019 if(stereomode && *stereomode == StereoEncoding::Hrtf)
1020 device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1022 const char *layout{nullptr};
1023 switch(device->FmtChans)
1025 case DevFmtQuad: layout = "quad"; break;
1026 case DevFmtX51: layout = "surround51"; break;
1027 case DevFmtX61: layout = "surround61"; break;
1028 case DevFmtX71: layout = "surround71"; break;
1029 case DevFmtX714: layout = "surround714"; break;
1030 case DevFmtX7144: layout = "surround7144"; break;
1031 case DevFmtX3D71: layout = "surround3d71"; break;
1032 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
1033 case DevFmtMono:
1034 case DevFmtStereo:
1035 case DevFmtAmbi3D:
1036 break;
1039 std::unique_ptr<DecoderConfig<DualBand,MaxOutputChannels>> decoder_store;
1040 DecoderView decoder{};
1041 std::array<float,MaxOutputChannels> speakerdists{};
1042 auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
1044 AmbDecConf conf{};
1045 if(auto err = conf.load(config))
1047 ERR("Failed to load layout file %s\n", config);
1048 ERR(" %s\n", err->c_str());
1049 return false;
1051 if(conf.Speakers.size() > MaxOutputChannels)
1053 ERR("Unsupported decoder speaker count %zu (max %zu)\n", conf.Speakers.size(),
1054 MaxOutputChannels);
1055 return false;
1057 if(conf.ChanMask > Ambi3OrderMask)
1059 ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
1060 Ambi3OrderMask);
1061 return false;
1064 TRACE("Using %s decoder: \"%s\"\n", DevFmtChannelsString(device->FmtChans),
1065 conf.Description.c_str());
1066 device->mXOverFreq = std::clamp(conf.XOverFreq, 100.0f, 1000.0f);
1068 decoder_store = std::make_unique<DecoderConfig<DualBand,MaxOutputChannels>>();
1069 decoder = MakeDecoderView(device, &conf, *decoder_store);
1071 const auto confspeakers = al::span{std::as_const(conf.Speakers)}
1072 .first(decoder.mChannels.size());
1073 std::transform(confspeakers.cbegin(), confspeakers.cend(), speakerdists.begin(),
1074 std::mem_fn(&AmbDecConf::SpeakerConf::Distance));
1075 return true;
1077 bool usingCustom{false};
1078 if(layout)
1080 if(auto decopt = device->configValue<std::string>("decoder", layout))
1081 usingCustom = load_config(decopt->c_str());
1083 if(!usingCustom && device->FmtChans != DevFmtAmbi3D)
1084 TRACE("Using built-in %s decoder\n", DevFmtChannelsString(device->FmtChans));
1086 /* Enable the stablizer only for formats that have front-left, front-
1087 * right, and front-center outputs.
1089 const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != InvalidChannelIndex
1090 && device->RealOut.ChannelIndex[FrontLeft] != InvalidChannelIndex
1091 && device->RealOut.ChannelIndex[FrontRight] != InvalidChannelIndex
1092 && device->getConfigValueBool({}, "front-stablizer", false)};
1093 const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", true)};
1094 InitPanning(device, hqdec, stablize, decoder);
1095 if(decoder)
1097 float accum_dist{0.0f}, spkr_count{0.0f};
1098 for(auto dist : speakerdists)
1100 if(dist > 0.0f)
1102 accum_dist += dist;
1103 spkr_count += 1.0f;
1107 const float avg_dist{(accum_dist > 0.0f && spkr_count > 0) ? accum_dist/spkr_count :
1108 device->configValue<float>("decoder", "speaker-dist").value_or(1.0f)};
1109 InitNearFieldCtrl(device, avg_dist, decoder.mOrder, decoder.mIs3D);
1111 if(spkr_count > 0)
1112 InitDistanceComp(device, decoder.mChannels, speakerdists);
1114 if(auto *ambidec{device->AmbiDecoder.get()})
1116 device->PostProcess = ambidec->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
1117 : &ALCdevice::ProcessAmbiDec;
1119 return;
1123 /* If HRTF is explicitly requested, or if there's no explicit request and
1124 * the device is headphones, try to enable it.
1126 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Hrtf
1127 || (!stereomode && device->Flags.test(DirectEar)))
1129 if(device->mHrtfList.empty())
1130 device->enumerateHrtfs();
1132 if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
1134 const std::string_view hrtfname{device->mHrtfList[static_cast<uint>(hrtf_id)]};
1135 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1137 device->mHrtf = std::move(hrtf);
1138 device->mHrtfName = hrtfname;
1142 if(!device->mHrtf)
1144 for(const std::string_view hrtfname : device->mHrtfList)
1146 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1148 device->mHrtf = std::move(hrtf);
1149 device->mHrtfName = hrtfname;
1150 break;
1155 if(device->mHrtf)
1157 old_hrtf = nullptr;
1159 HrtfStore *hrtf{device->mHrtf.get()};
1160 device->mIrSize = hrtf->mIrSize;
1161 if(auto hrtfsizeopt = device->configValue<uint>({}, "hrtf-size"))
1163 if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
1164 device->mIrSize = std::max(*hrtfsizeopt, MinIrLength);
1167 InitHrtfPanning(device);
1168 device->PostProcess = &ALCdevice::ProcessHrtf;
1169 device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
1170 return;
1173 old_hrtf = nullptr;
1175 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Uhj)
1177 switch(UhjEncodeQuality)
1179 case UhjQualityType::IIR:
1180 device->mUhjEncoder = std::make_unique<UhjEncoderIIR>();
1181 break;
1182 case UhjQualityType::FIR256:
1183 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength256>>();
1184 break;
1185 case UhjQualityType::FIR512:
1186 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength512>>();
1187 break;
1189 assert(device->mUhjEncoder != nullptr);
1191 TRACE("UHJ enabled\n");
1192 InitUhjPanning(device);
1193 device->PostProcess = &ALCdevice::ProcessUhj;
1194 return;
1197 device->mRenderMode = RenderMode::Pairwise;
1198 if(device->Type != DeviceType::Loopback)
1200 if(auto cflevopt = device->configValue<int>({}, "cf_level"))
1202 if(*cflevopt > 0 && *cflevopt <= 6)
1204 auto bs2b = std::make_unique<Bs2b::bs2b>();
1205 bs2b->set_params(*cflevopt, static_cast<int>(device->Frequency));
1206 device->Bs2b = std::move(bs2b);
1207 TRACE("BS2B enabled\n");
1208 InitPanning(device);
1209 device->PostProcess = &ALCdevice::ProcessBs2b;
1210 return;
1215 TRACE("Stereo rendering\n");
1216 InitPanning(device);
1217 device->PostProcess = &ALCdevice::ProcessAmbiDec;
1221 void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
1223 DeviceBase *device{context->mDevice};
1224 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
1226 slot->mWetBuffer.resize(count);
1228 const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
1229 const auto iter = std::transform(acnmap.cbegin(), acnmap.cend(), slot->Wet.AmbiMap.begin(),
1230 [](const uint8_t &acn) noexcept -> BFChannelConfig { return BFChannelConfig{1.0f, acn}; });
1231 std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
1232 slot->Wet.Buffer = slot->mWetBuffer;