Replace a couple loops with algorithms
[openal-soft.git] / alc / panning.cpp
blobc6d91812ac2b93e711be676eaee15b2c9c34f93f
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 inline 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 Aux0: return "Aux0";
102 case Aux1: return "Aux1";
103 case Aux2: return "Aux2";
104 case Aux3: return "Aux3";
105 case Aux4: return "Aux4";
106 case Aux5: return "Aux5";
107 case Aux6: return "Aux6";
108 case Aux7: return "Aux7";
109 case Aux8: return "Aux8";
110 case Aux9: return "Aux9";
111 case Aux10: return "Aux10";
112 case Aux11: return "Aux11";
113 case Aux12: return "Aux12";
114 case Aux13: return "Aux13";
115 case Aux14: return "Aux14";
116 case Aux15: return "Aux15";
118 case MaxChannels: break;
120 return "(unknown)";
124 std::unique_ptr<FrontStablizer> CreateStablizer(const size_t outchans, const uint srate)
126 auto stablizer = FrontStablizer::Create(outchans);
128 /* Initialize band-splitting filter for the mid signal, with a crossover at
129 * 5khz (could be higher).
131 stablizer->MidFilter.init(5000.0f / static_cast<float>(srate));
132 for(auto &filter : stablizer->ChannelFilters)
133 filter = stablizer->MidFilter;
135 return stablizer;
138 void AllocChannels(ALCdevice *device, const size_t main_chans, const size_t real_chans)
140 TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans, real_chans);
142 /* Allocate extra channels for any post-filter output. */
143 const size_t num_chans{main_chans + real_chans};
145 TRACE("Allocating %zu channels, %zu bytes\n", num_chans,
146 num_chans*sizeof(device->MixBuffer[0]));
147 device->MixBuffer.resize(num_chans);
148 al::span<FloatBufferLine> buffer{device->MixBuffer};
150 device->Dry.Buffer = buffer.first(main_chans);
151 buffer = buffer.subspan(main_chans);
152 if(real_chans != 0)
154 device->RealOut.Buffer = buffer.first(real_chans);
155 buffer = buffer.subspan(real_chans);
157 else
158 device->RealOut.Buffer = device->Dry.Buffer;
162 using ChannelCoeffs = std::array<float,MaxAmbiChannels>;
163 enum DecoderMode : bool {
164 SingleBand = false,
165 DualBand = true
168 template<DecoderMode Mode, size_t N>
169 struct DecoderConfig;
171 template<size_t N>
172 struct DecoderConfig<SingleBand, N> {
173 uint8_t mOrder{};
174 bool mIs3D{};
175 std::array<Channel,N> mChannels{};
176 DevAmbiScaling mScaling{};
177 std::array<float,MaxAmbiOrder+1> mOrderGain{};
178 std::array<ChannelCoeffs,N> mCoeffs{};
181 template<size_t N>
182 struct DecoderConfig<DualBand, N> {
183 uint8_t mOrder{};
184 bool mIs3D{};
185 std::array<Channel,N> mChannels{};
186 DevAmbiScaling mScaling{};
187 std::array<float,MaxAmbiOrder+1> mOrderGain{};
188 std::array<ChannelCoeffs,N> mCoeffs{};
189 std::array<float,MaxAmbiOrder+1> mOrderGainLF{};
190 std::array<ChannelCoeffs,N> mCoeffsLF{};
193 template<>
194 struct DecoderConfig<DualBand, 0> {
195 uint8_t mOrder{};
196 bool mIs3D{};
197 al::span<const Channel> mChannels;
198 DevAmbiScaling mScaling{};
199 al::span<const float> mOrderGain;
200 al::span<const ChannelCoeffs> mCoeffs;
201 al::span<const float> mOrderGainLF;
202 al::span<const ChannelCoeffs> mCoeffsLF;
204 template<size_t N>
205 DecoderConfig& operator=(const DecoderConfig<SingleBand,N> &rhs) noexcept
207 mOrder = rhs.mOrder;
208 mIs3D = rhs.mIs3D;
209 mChannels = rhs.mChannels;
210 mScaling = rhs.mScaling;
211 mOrderGain = rhs.mOrderGain;
212 mCoeffs = rhs.mCoeffs;
213 mOrderGainLF = {};
214 mCoeffsLF = {};
215 return *this;
218 template<size_t N>
219 DecoderConfig& operator=(const DecoderConfig<DualBand,N> &rhs) noexcept
221 mOrder = rhs.mOrder;
222 mIs3D = rhs.mIs3D;
223 mChannels = rhs.mChannels;
224 mScaling = rhs.mScaling;
225 mOrderGain = rhs.mOrderGain;
226 mCoeffs = rhs.mCoeffs;
227 mOrderGainLF = rhs.mOrderGainLF;
228 mCoeffsLF = rhs.mCoeffsLF;
229 return *this;
232 explicit operator bool() const noexcept { return !mChannels.empty(); }
234 using DecoderView = DecoderConfig<DualBand, 0>;
237 void InitNearFieldCtrl(ALCdevice *device, const float ctrl_dist, const uint order, const bool is3d)
239 static const std::array<uint,MaxAmbiOrder+1> chans_per_order2d{{1, 2, 2, 2}};
240 static const std::array<uint,MaxAmbiOrder+1> chans_per_order3d{{1, 3, 5, 7}};
242 /* NFC is only used when AvgSpeakerDist is greater than 0. */
243 if(!device->getConfigValueBool("decoder", "nfc", false) || !(ctrl_dist > 0.0f))
244 return;
246 device->AvgSpeakerDist = std::clamp(ctrl_dist, 0.1f, 10.0f);
247 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
249 const float w1{SpeedOfSoundMetersPerSec /
250 (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
251 device->mNFCtrlFilter.init(w1);
253 auto iter = std::copy_n(is3d ? chans_per_order3d.begin() : chans_per_order2d.begin(), order+1u,
254 device->NumChannelsPerOrder.begin());
255 std::fill(iter, device->NumChannelsPerOrder.end(), 0u);
258 void InitDistanceComp(ALCdevice *device, const al::span<const Channel> channels,
259 const al::span<const float,MaxOutputChannels> dists)
261 const float maxdist{std::accumulate(dists.begin(), dists.end(), 0.0f,
262 [](const float a, const float b) noexcept -> float { return std::max(a, b); })};
264 if(!device->getConfigValueBool("decoder", "distance-comp", true) || !(maxdist > 0.0f))
265 return;
267 const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
269 struct DistCoeffs { uint Length{}; float Gain{}; };
270 std::vector<DistCoeffs> ChanDelay;
271 ChanDelay.reserve(device->RealOut.Buffer.size());
273 size_t total{0u};
274 for(size_t chidx{0};chidx < channels.size();++chidx)
276 const Channel ch{channels[chidx]};
277 const size_t idx{device->RealOut.ChannelIndex[ch]};
278 if(idx == InvalidChannelIndex)
279 continue;
281 const float distance{dists[chidx]};
283 /* Distance compensation only delays in steps of the sample rate. This
284 * is a bit less accurate since the delay time falls to the nearest
285 * sample time, but it's far simpler as it doesn't have to deal with
286 * phase offsets. This means at 48khz, for instance, the distance delay
287 * will be in steps of about 7 millimeters.
289 float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
290 if(delay > float{DistanceComp::MaxDelay-1})
292 ERR("Delay for channel %zu (%s) exceeds buffer length (%f > %d)\n", idx,
293 GetLabelFromChannel(ch), delay, DistanceComp::MaxDelay-1);
294 delay = float{DistanceComp::MaxDelay-1};
297 ChanDelay.resize(std::max(ChanDelay.size(), idx+1_uz));
298 ChanDelay[idx].Length = static_cast<uint>(delay);
299 ChanDelay[idx].Gain = distance / maxdist;
300 TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
301 ChanDelay[idx].Length, ChanDelay[idx].Gain);
303 /* Round up to the next 4th sample, so each channel buffer starts
304 * 16-byte aligned.
306 total += RoundUp(ChanDelay[idx].Length, 4);
309 if(total > 0)
311 auto chandelays = DistanceComp::Create(total);
312 auto chanbuffer = chandelays->mSamples.begin();
314 auto set_bufptr = [&chanbuffer](const DistCoeffs &data)
316 DistanceComp::ChanData ret{};
317 ret.Buffer = al::span{chanbuffer, data.Length};
318 ret.Gain = data.Gain;
319 chanbuffer += ptrdiff_t(RoundUp(data.Length, 4));
320 return ret;
322 std::transform(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
323 set_bufptr);
324 device->ChannelDelays = std::move(chandelays);
329 constexpr auto GetAmbiScales(DevAmbiScaling scaletype) noexcept
331 if(scaletype == DevAmbiScaling::FuMa) return al::span{AmbiScale::FromFuMa};
332 if(scaletype == DevAmbiScaling::SN3D) return al::span{AmbiScale::FromSN3D};
333 return al::span{AmbiScale::FromN3D};
336 constexpr auto GetAmbiLayout(DevAmbiLayout layouttype) noexcept
338 if(layouttype == DevAmbiLayout::FuMa) return al::span{AmbiIndex::FromFuMa};
339 return al::span{AmbiIndex::FromACN};
343 DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf,
344 DecoderConfig<DualBand,MaxOutputChannels> &decoder)
346 DecoderView ret{};
348 decoder.mOrder = (conf->ChanMask > Ambi3OrderMask) ? uint8_t{4} :
349 (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
350 (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
351 decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
353 switch(conf->CoeffScale)
355 case AmbDecScale::Unset: ASSUME(false); break;
356 case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
357 case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
358 case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
361 const auto hfordermin = std::min(conf->HFOrderGain.size(), decoder.mOrderGain.size());
362 std::copy_n(conf->HFOrderGain.begin(), hfordermin, decoder.mOrderGain.begin());
363 const auto lfordermin = std::min(conf->LFOrderGain.size(), decoder.mOrderGainLF.size());
364 std::copy_n(conf->LFOrderGain.begin(), lfordermin, decoder.mOrderGainLF.begin());
366 const auto num_coeffs = decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder)
367 : Ambi2DChannelsFromOrder(decoder.mOrder);
368 const auto idx_map = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::FromACN}
369 : al::span<const uint8_t>{AmbiIndex::FromACN2D};
370 const auto hfmatrix = conf->HFMatrix;
371 const auto lfmatrix = conf->LFMatrix;
373 uint chan_count{0};
374 for(auto &speaker : al::span{std::as_const(conf->Speakers)})
376 /* NOTE: AmbDec does not define any standard speaker names, however
377 * for this to work we have to by able to find the output channel
378 * the speaker definition corresponds to. Therefore, OpenAL Soft
379 * requires these channel labels to be recognized:
381 * LF = Front left
382 * RF = Front right
383 * LS = Side left
384 * RS = Side right
385 * LB = Back left
386 * RB = Back right
387 * CE = Front center
388 * CB = Back center
389 * LFT = Top front left
390 * RFT = Top front right
391 * LBT = Top back left
392 * RBT = Top back right
394 * Additionally, surround51 will acknowledge back speakers for side
395 * channels, to avoid issues with an ambdec expecting 5.1 to use the
396 * back channels.
398 Channel ch{};
399 if(speaker.Name == "LF")
400 ch = FrontLeft;
401 else if(speaker.Name == "RF")
402 ch = FrontRight;
403 else if(speaker.Name == "CE")
404 ch = FrontCenter;
405 else if(speaker.Name == "LS")
406 ch = SideLeft;
407 else if(speaker.Name == "RS")
408 ch = SideRight;
409 else if(speaker.Name == "LB")
410 ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
411 else if(speaker.Name == "RB")
412 ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
413 else if(speaker.Name == "CB")
414 ch = BackCenter;
415 else if(speaker.Name == "LFT")
416 ch = TopFrontLeft;
417 else if(speaker.Name == "RFT")
418 ch = TopFrontRight;
419 else if(speaker.Name == "LBT")
420 ch = TopBackLeft;
421 else if(speaker.Name == "RBT")
422 ch = TopBackRight;
423 else
425 int idx{};
426 char c{};
427 if(sscanf(speaker.Name.c_str(), "AUX%d%c", &idx, &c) != 1 || idx < 0
428 || idx >= MaxChannels-Aux0)
430 ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
431 continue;
433 ch = static_cast<Channel>(Aux0+idx);
436 decoder.mChannels[chan_count] = ch;
437 for(size_t dst{0};dst < num_coeffs;++dst)
439 const size_t src{idx_map[dst]};
440 decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
442 if(conf->FreqBands > 1)
444 for(size_t dst{0};dst < num_coeffs;++dst)
446 const size_t src{idx_map[dst]};
447 decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
450 ++chan_count;
453 if(chan_count > 0)
455 ret.mOrder = decoder.mOrder;
456 ret.mIs3D = decoder.mIs3D;
457 ret.mScaling = decoder.mScaling;
458 ret.mChannels = al::span{decoder.mChannels}.first(chan_count);
459 ret.mOrderGain = decoder.mOrderGain;
460 ret.mCoeffs = al::span{decoder.mCoeffs}.first(chan_count);
461 if(conf->FreqBands > 1)
463 ret.mOrderGainLF = decoder.mOrderGainLF;
464 ret.mCoeffsLF = al::span{decoder.mCoeffsLF}.first(chan_count);
467 return ret;
470 constexpr DecoderConfig<SingleBand, 1> MonoConfig{
471 0, false, {{FrontCenter}},
472 DevAmbiScaling::N3D,
473 {{1.0f}},
474 {{ {{1.0f}} }}
476 constexpr DecoderConfig<SingleBand, 2> StereoConfig{
477 1, false, {{FrontLeft, FrontRight}},
478 DevAmbiScaling::N3D,
479 {{1.0f, 1.0f}},
481 {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
482 {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
485 constexpr DecoderConfig<DualBand, 4> QuadConfig{
486 1, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
487 DevAmbiScaling::N3D,
488 /*HF*/{{1.41421356e+0f, 1.00000000e+0f}},
490 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
491 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
492 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
493 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
495 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
497 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
498 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
499 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
500 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
503 constexpr DecoderConfig<DualBand, 5> X51Config{
504 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
505 DevAmbiScaling::FuMa,
506 /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
508 {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
509 {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
510 {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
511 {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
512 {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
514 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
516 {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
517 {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
518 {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
519 {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
520 {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
523 constexpr DecoderConfig<SingleBand, 5> X61Config{
524 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
525 DevAmbiScaling::N3D,
526 {{1.0f, 1.0f, 1.0f}},
528 {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
529 {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
530 {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
531 {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
532 {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
535 constexpr DecoderConfig<DualBand, 6> X71Config{
536 2, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
537 DevAmbiScaling::N3D,
538 /*HF*/{{1.41421356e+0f, 1.22474487e+0f, 7.07106781e-1f}},
540 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
541 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
542 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
543 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
544 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
545 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
547 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
549 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
550 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
551 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
552 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
553 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
554 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
557 constexpr DecoderConfig<DualBand, 6> X3D71Config{
558 1, true, {{Aux0, SideLeft, FrontLeft, FrontRight, SideRight, Aux1}},
559 DevAmbiScaling::N3D,
560 /*HF*/{{1.73205081e+0f, 1.00000000e+0f}},
562 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
563 {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
564 {{1.666666667e-01f, 2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
565 {{1.666666667e-01f, -2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
566 {{1.666666667e-01f, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
567 {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
569 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
571 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
572 {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
573 {{1.666666667e-01f, 2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
574 {{1.666666667e-01f, -2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
575 {{1.666666667e-01f, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
576 {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
579 constexpr DecoderConfig<SingleBand, 10> X714Config{
580 1, true, {{FrontLeft, FrontRight, SideLeft, SideRight, BackLeft, BackRight, TopFrontLeft, TopFrontRight, TopBackLeft, TopBackRight }},
581 DevAmbiScaling::N3D,
582 {{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
584 {{1.27149251e-01f, 7.63047539e-02f, -3.64373750e-02f, 1.59700680e-01f}},
585 {{1.07005418e-01f, -7.67638760e-02f, -4.92129762e-02f, 1.29012797e-01f}},
586 {{1.26400196e-01f, 1.77494694e-01f, -3.71203389e-02f, 0.00000000e+00f}},
587 {{1.26396516e-01f, -1.77488059e-01f, -3.71297878e-02f, 0.00000000e+00f}},
588 {{1.06996956e-01f, 7.67615256e-02f, -4.92166307e-02f, -1.29001640e-01f}},
589 {{1.27145671e-01f, -7.63003471e-02f, -3.64353304e-02f, -1.59697510e-01f}},
590 {{8.80919747e-02f, 7.48940670e-02f, 9.08786244e-02f, 6.22527183e-02f}},
591 {{1.57880745e-01f, -7.28755272e-02f, 1.82364187e-01f, 8.74240284e-02f}},
592 {{1.57892225e-01f, 7.28944768e-02f, 1.82363474e-01f, -8.74301086e-02f}},
593 {{8.80892603e-02f, -7.48948724e-02f, 9.08779842e-02f, -6.22480443e-02f}},
597 void InitPanning(ALCdevice *device, const bool hqdec=false, const bool stablize=false,
598 DecoderView decoder={})
600 if(!decoder)
602 switch(device->FmtChans)
604 case DevFmtMono: decoder = MonoConfig; break;
605 case DevFmtStereo: decoder = StereoConfig; break;
606 case DevFmtQuad: decoder = QuadConfig; break;
607 case DevFmtX51: decoder = X51Config; break;
608 case DevFmtX61: decoder = X61Config; break;
609 case DevFmtX71: decoder = X71Config; break;
610 case DevFmtX714: decoder = X714Config; break;
611 case DevFmtX3D71: decoder = X3D71Config; break;
612 case DevFmtAmbi3D:
613 /* For DevFmtAmbi3D, the ambisonic order is already set. */
614 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
615 const auto acnmap = GetAmbiLayout(device->mAmbiLayout).first(count);
616 const auto n3dscale = GetAmbiScales(device->mAmbiScale);
618 std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
619 [n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
620 { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
621 AllocChannels(device, count, 0);
622 device->m2DMixing = false;
624 float avg_dist{};
625 if(auto distopt = device->configValue<float>("decoder", "speaker-dist"))
626 avg_dist = *distopt;
627 else if(auto delayopt = device->configValue<float>("decoder", "nfc-ref-delay"))
629 WARN("nfc-ref-delay is deprecated, use speaker-dist instead\n");
630 avg_dist = *delayopt * SpeedOfSoundMetersPerSec;
633 InitNearFieldCtrl(device, avg_dist, device->mAmbiOrder, true);
634 return;
638 const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
639 Ambi2DChannelsFromOrder(decoder.mOrder)};
640 const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
641 std::vector<ChannelDec> chancoeffs, chancoeffslf;
642 for(size_t i{0u};i < decoder.mChannels.size();++i)
644 const size_t idx{device->channelIdxByName(decoder.mChannels[i])};
645 if(idx == InvalidChannelIndex)
647 ERR("Failed to find %s channel in device\n",
648 GetLabelFromChannel(decoder.mChannels[i]));
649 continue;
652 auto ordermap = decoder.mIs3D ? al::span<const uint8_t>{AmbiIndex::OrderFromChannel}
653 : al::span<const uint8_t>{AmbiIndex::OrderFrom2DChannel};
655 chancoeffs.resize(std::max(chancoeffs.size(), idx+1_zu), ChannelDec{});
656 al::span<const float,MaxAmbiChannels> src{decoder.mCoeffs[i]};
657 al::span<float,MaxAmbiChannels> dst{chancoeffs[idx]};
658 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
659 dst[ambichan] = src[ambichan] * decoder.mOrderGain[ordermap[ambichan]];
661 if(!dual_band)
662 continue;
664 chancoeffslf.resize(std::max(chancoeffslf.size(), idx+1_zu), ChannelDec{});
665 src = decoder.mCoeffsLF[i];
666 dst = chancoeffslf[idx];
667 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
668 dst[ambichan] = src[ambichan] * decoder.mOrderGainLF[ordermap[ambichan]];
671 /* For non-DevFmtAmbi3D, set the ambisonic order. */
672 device->mAmbiOrder = decoder.mOrder;
673 device->m2DMixing = !decoder.mIs3D;
675 const auto acnmap = decoder.mIs3D ? al::span{AmbiIndex::FromACN}.first(ambicount)
676 : al::span{AmbiIndex::FromACN2D}.first(ambicount);
677 const auto coeffscale = GetAmbiScales(decoder.mScaling);
678 std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
679 [coeffscale](const uint8_t &acn) noexcept
680 { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
681 AllocChannels(device, ambicount, device->channelsFromFmt());
683 std::unique_ptr<FrontStablizer> stablizer;
684 if(stablize)
686 /* Only enable the stablizer if the decoder does not output to the
687 * front-center channel.
689 const size_t cidx{device->RealOut.ChannelIndex[FrontCenter]};
690 bool hasfc{false};
691 if(cidx < chancoeffs.size())
693 for(const auto &coeff : chancoeffs[cidx])
694 hasfc |= coeff != 0.0f;
696 if(!hasfc && cidx < chancoeffslf.size())
698 for(const auto &coeff : chancoeffslf[cidx])
699 hasfc |= coeff != 0.0f;
701 if(!hasfc)
703 stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
704 TRACE("Front stablizer enabled\n");
708 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
709 !dual_band ? "single" : "dual",
710 (decoder.mOrder > 3) ? "fourth" :
711 (decoder.mOrder > 2) ? "third" :
712 (decoder.mOrder > 1) ? "second" : "first",
713 decoder.mIs3D ? " periphonic" : "");
714 device->AmbiDecoder = BFormatDec::Create(ambicount, chancoeffs, chancoeffslf,
715 device->mXOverFreq/static_cast<float>(device->Frequency), std::move(stablizer));
718 void InitHrtfPanning(ALCdevice *device)
720 static constexpr float Deg180{al::numbers::pi_v<float>};
721 static constexpr float Deg_90{Deg180 / 2.0f /* 90 degrees*/};
722 static constexpr float Deg_45{Deg_90 / 2.0f /* 45 degrees*/};
723 static constexpr float Deg135{Deg_45 * 3.0f /*135 degrees*/};
724 static constexpr float Deg_21{3.648638281e-01f /* 20~ 21 degrees*/};
725 static constexpr float Deg_32{5.535743589e-01f /* 31~ 32 degrees*/};
726 static constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
727 static constexpr float Deg_58{1.017221968e+00f /* 58~ 59 degrees*/};
728 static constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
729 static constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
730 static constexpr float Deg122{2.124370686e+00f /*121~122 degrees*/};
731 static constexpr std::array AmbiPoints1O{
732 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
733 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
734 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
735 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
736 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
737 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
738 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
739 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
741 static constexpr std::array AmbiPoints2O{
742 AngularPoint{EvRadians{-Deg_32}, AzRadians{ 0.0f}},
743 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_58}},
744 AngularPoint{EvRadians{ Deg_58}, AzRadians{ Deg_90}},
745 AngularPoint{EvRadians{ Deg_32}, AzRadians{ 0.0f}},
746 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg122}},
747 AngularPoint{EvRadians{-Deg_58}, AzRadians{-Deg_90}},
748 AngularPoint{EvRadians{-Deg_32}, AzRadians{ Deg180}},
749 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg122}},
750 AngularPoint{EvRadians{ Deg_58}, AzRadians{-Deg_90}},
751 AngularPoint{EvRadians{ Deg_32}, AzRadians{ Deg180}},
752 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_58}},
753 AngularPoint{EvRadians{-Deg_58}, AzRadians{ Deg_90}},
755 static constexpr std::array AmbiPoints3O{
756 AngularPoint{EvRadians{ Deg_69}, AzRadians{-Deg_90}},
757 AngularPoint{EvRadians{ Deg_69}, AzRadians{ Deg_90}},
758 AngularPoint{EvRadians{-Deg_69}, AzRadians{-Deg_90}},
759 AngularPoint{EvRadians{-Deg_69}, AzRadians{ Deg_90}},
760 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg_69}},
761 AngularPoint{EvRadians{ 0.0f}, AzRadians{-Deg111}},
762 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg_69}},
763 AngularPoint{EvRadians{ 0.0f}, AzRadians{ Deg111}},
764 AngularPoint{EvRadians{ Deg_21}, AzRadians{ 0.0f}},
765 AngularPoint{EvRadians{ Deg_21}, AzRadians{ Deg180}},
766 AngularPoint{EvRadians{-Deg_21}, AzRadians{ 0.0f}},
767 AngularPoint{EvRadians{-Deg_21}, AzRadians{ Deg180}},
768 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg_45}},
769 AngularPoint{EvRadians{ Deg_35}, AzRadians{-Deg135}},
770 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg_45}},
771 AngularPoint{EvRadians{ Deg_35}, AzRadians{ Deg135}},
772 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg_45}},
773 AngularPoint{EvRadians{-Deg_35}, AzRadians{-Deg135}},
774 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg_45}},
775 AngularPoint{EvRadians{-Deg_35}, AzRadians{ Deg135}},
777 static constexpr std::array AmbiMatrix1O{
778 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f},
779 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f},
780 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f},
781 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f},
782 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f},
783 ChannelCoeffs{1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f},
784 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f},
785 ChannelCoeffs{1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f},
787 static constexpr std::array AmbiMatrix2O{
788 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},
789 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},
790 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},
791 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},
792 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},
793 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},
794 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},
795 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},
796 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},
797 ChannelCoeffs{8.333333333e-02f, 0.000000000e+00f, 7.588274978e-02f, -1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, -1.591525047e-02f, -1.443375673e-01f, 1.167715449e-01f},
798 ChannelCoeffs{8.333333333e-02f, 1.227808683e-01f, 0.000000000e+00f, 7.588274978e-02f, 1.443375673e-01f, 0.000000000e+00f, -9.316949906e-02f, 0.000000000e+00f, -7.216878365e-02f},
799 ChannelCoeffs{8.333333333e-02f, -7.588274978e-02f, -1.227808683e-01f, 0.000000000e+00f, 0.000000000e+00f, 1.443375673e-01f, 1.090847495e-01f, 0.000000000e+00f, -4.460276122e-02f},
801 static constexpr std::array AmbiMatrix3O{
802 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},
803 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},
804 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},
805 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},
806 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},
807 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},
808 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},
809 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},
810 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},
811 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},
812 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},
813 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},
814 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},
815 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},
816 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},
817 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},
818 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},
819 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},
820 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},
821 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},
823 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain1O{
824 /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
826 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain2O{
827 /*ENRGY*/ 1.825741858e+00f, 1.414213562e+00f, 7.302967433e-01f
828 /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
829 /*RMS 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f*/
831 static constexpr std::array<float,MaxAmbiOrder+1> AmbiOrderHFGain3O{
832 /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
833 /*AMP*/ 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f
834 /*RMS 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f*/
837 static_assert(AmbiPoints1O.size() == AmbiMatrix1O.size(), "First-Order Ambisonic HRTF mismatch");
838 static_assert(AmbiPoints2O.size() == AmbiMatrix2O.size(), "Second-Order Ambisonic HRTF mismatch");
839 static_assert(AmbiPoints3O.size() == AmbiMatrix3O.size(), "Third-Order Ambisonic HRTF mismatch");
841 /* A 700hz crossover frequency provides tighter sound imaging at the sweet
842 * spot with ambisonic decoding, as the distance between the ears is closer
843 * to half this frequency wavelength, which is the optimal point where the
844 * response should change between optimizing phase vs volume. Normally this
845 * tighter imaging is at the cost of a smaller sweet spot, but since the
846 * listener is fixed in the center of the HRTF responses for the decoder,
847 * we don't have to worry about ever being out of the sweet spot.
849 * A better option here may be to have the head radius as part of the HRTF
850 * data set and calculate the optimal crossover frequency from that.
852 device->mXOverFreq = 700.0f;
854 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
855 * and it eases the CPU/memory load.
857 device->mRenderMode = RenderMode::Hrtf;
858 uint ambi_order{1};
859 if(auto modeopt = device->configValue<std::string>({}, "hrtf-mode"))
861 struct HrtfModeEntry {
862 std::string_view name;
863 RenderMode mode;
864 uint order;
866 constexpr std::array hrtf_modes{
867 HrtfModeEntry{"full"sv, RenderMode::Hrtf, 1},
868 HrtfModeEntry{"ambi1"sv, RenderMode::Normal, 1},
869 HrtfModeEntry{"ambi2"sv, RenderMode::Normal, 2},
870 HrtfModeEntry{"ambi3"sv, RenderMode::Normal, 3},
873 std::string_view mode{*modeopt};
874 if(al::case_compare(mode, "basic"sv) == 0)
876 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", modeopt->c_str(), "ambi2");
877 mode = "ambi2";
880 auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
881 { return al::case_compare(mode, entry.name) == 0; };
882 auto iter = std::find_if(hrtf_modes.begin(), hrtf_modes.end(), match_entry);
883 if(iter == hrtf_modes.end())
884 ERR("Unexpected hrtf-mode: %s\n", modeopt->c_str());
885 else
887 device->mRenderMode = iter->mode;
888 ambi_order = iter->order;
891 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
892 GetCounterSuffix(ambi_order), (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
893 device->mHrtfName.c_str());
895 bool perHrirMin{false};
896 auto AmbiPoints = al::span{AmbiPoints1O}.subspan(0);
897 auto AmbiMatrix = al::span{AmbiMatrix1O}.subspan(0);
898 auto AmbiOrderHFGain = al::span{AmbiOrderHFGain1O};
899 if(ambi_order >= 3)
901 perHrirMin = true;
902 AmbiPoints = AmbiPoints3O;
903 AmbiMatrix = AmbiMatrix3O;
904 AmbiOrderHFGain = AmbiOrderHFGain3O;
906 else if(ambi_order == 2)
908 AmbiPoints = AmbiPoints2O;
909 AmbiMatrix = AmbiMatrix2O;
910 AmbiOrderHFGain = AmbiOrderHFGain2O;
912 device->mAmbiOrder = ambi_order;
913 device->m2DMixing = false;
915 const size_t count{AmbiChannelsFromOrder(ambi_order)};
916 const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
917 std::transform(acnmap.begin(), acnmap.end(), device->Dry.AmbiMap.begin(),
918 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; });
919 AllocChannels(device, count, device->channelsFromFmt());
921 HrtfStore *Hrtf{device->mHrtf.get()};
922 auto hrtfstate = DirectHrtfState::Create(count);
923 hrtfstate->build(Hrtf, device->mIrSize, perHrirMin, AmbiPoints, AmbiMatrix, device->mXOverFreq,
924 AmbiOrderHFGain);
925 device->mHrtfState = std::move(hrtfstate);
927 InitNearFieldCtrl(device, Hrtf->mFields[0].distance, ambi_order, true);
930 void InitUhjPanning(ALCdevice *device)
932 /* UHJ is always 2D first-order. */
933 static constexpr size_t count{Ambi2DChannelsFromOrder(1)};
935 device->mAmbiOrder = 1;
936 device->m2DMixing = true;
938 const auto acnmap = al::span{AmbiIndex::FromFuMa2D}.first<count>();
939 std::transform(acnmap.cbegin(), acnmap.cend(), device->Dry.AmbiMap.begin(),
940 [](const uint8_t &acn) noexcept -> BFChannelConfig
941 { return BFChannelConfig{1.0f/AmbiScale::FromUHJ[acn], acn}; });
942 AllocChannels(device, count, device->channelsFromFmt());
945 } // namespace
947 void aluInitRenderer(ALCdevice *device, int hrtf_id, std::optional<StereoEncoding> stereomode)
949 /* Hold the HRTF the device last used, in case it's used again. */
950 HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
952 device->mHrtfState = nullptr;
953 device->mHrtf = nullptr;
954 device->mIrSize = 0;
955 device->mHrtfName.clear();
956 device->mXOverFreq = 400.0f;
957 device->m2DMixing = false;
958 device->mRenderMode = RenderMode::Normal;
960 if(device->FmtChans != DevFmtStereo)
962 old_hrtf = nullptr;
963 if(stereomode && *stereomode == StereoEncoding::Hrtf)
964 device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
966 const char *layout{nullptr};
967 switch(device->FmtChans)
969 case DevFmtQuad: layout = "quad"; break;
970 case DevFmtX51: layout = "surround51"; break;
971 case DevFmtX61: layout = "surround61"; break;
972 case DevFmtX71: layout = "surround71"; break;
973 case DevFmtX714: layout = "surround714"; break;
974 case DevFmtX3D71: layout = "surround3d71"; break;
975 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
976 case DevFmtMono:
977 case DevFmtStereo:
978 case DevFmtAmbi3D:
979 break;
982 std::unique_ptr<DecoderConfig<DualBand,MaxOutputChannels>> decoder_store;
983 DecoderView decoder{};
984 std::array<float,MaxOutputChannels> speakerdists{};
985 auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
987 AmbDecConf conf{};
988 if(auto err = conf.load(config))
990 ERR("Failed to load layout file %s\n", config);
991 ERR(" %s\n", err->c_str());
992 return false;
994 if(conf.Speakers.size() > MaxOutputChannels)
996 ERR("Unsupported decoder speaker count %zu (max %zu)\n", conf.Speakers.size(),
997 MaxOutputChannels);
998 return false;
1000 if(conf.ChanMask > Ambi3OrderMask)
1002 ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
1003 Ambi3OrderMask);
1004 return false;
1007 TRACE("Using %s decoder: \"%s\"\n", DevFmtChannelsString(device->FmtChans),
1008 conf.Description.c_str());
1009 device->mXOverFreq = std::clamp(conf.XOverFreq, 100.0f, 1000.0f);
1011 decoder_store = std::make_unique<DecoderConfig<DualBand,MaxOutputChannels>>();
1012 decoder = MakeDecoderView(device, &conf, *decoder_store);
1014 const auto confspeakers = al::span{std::as_const(conf.Speakers)}
1015 .first(decoder.mChannels.size());
1016 std::transform(confspeakers.cbegin(), confspeakers.cend(), speakerdists.begin(),
1017 std::mem_fn(&AmbDecConf::SpeakerConf::Distance));
1018 return true;
1020 bool usingCustom{false};
1021 if(layout)
1023 if(auto decopt = device->configValue<std::string>("decoder", layout))
1024 usingCustom = load_config(decopt->c_str());
1026 if(!usingCustom && device->FmtChans != DevFmtAmbi3D)
1027 TRACE("Using built-in %s decoder\n", DevFmtChannelsString(device->FmtChans));
1029 /* Enable the stablizer only for formats that have front-left, front-
1030 * right, and front-center outputs.
1032 const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != InvalidChannelIndex
1033 && device->RealOut.ChannelIndex[FrontLeft] != InvalidChannelIndex
1034 && device->RealOut.ChannelIndex[FrontRight] != InvalidChannelIndex
1035 && device->getConfigValueBool({}, "front-stablizer", false)};
1036 const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", true)};
1037 InitPanning(device, hqdec, stablize, decoder);
1038 if(decoder)
1040 float accum_dist{0.0f}, spkr_count{0.0f};
1041 for(auto dist : speakerdists)
1043 if(dist > 0.0f)
1045 accum_dist += dist;
1046 spkr_count += 1.0f;
1050 const float avg_dist{(accum_dist > 0.0f && spkr_count > 0) ? accum_dist/spkr_count :
1051 device->configValue<float>("decoder", "speaker-dist").value_or(1.0f)};
1052 InitNearFieldCtrl(device, avg_dist, decoder.mOrder, decoder.mIs3D);
1054 if(spkr_count > 0)
1055 InitDistanceComp(device, decoder.mChannels, speakerdists);
1057 if(auto *ambidec{device->AmbiDecoder.get()})
1059 device->PostProcess = ambidec->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
1060 : &ALCdevice::ProcessAmbiDec;
1062 return;
1066 /* If HRTF is explicitly requested, or if there's no explicit request and
1067 * the device is headphones, try to enable it.
1069 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Hrtf
1070 || (!stereomode && device->Flags.test(DirectEar)))
1072 if(device->mHrtfList.empty())
1073 device->enumerateHrtfs();
1075 if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
1077 const std::string_view hrtfname{device->mHrtfList[static_cast<uint>(hrtf_id)]};
1078 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1080 device->mHrtf = std::move(hrtf);
1081 device->mHrtfName = hrtfname;
1085 if(!device->mHrtf)
1087 for(const std::string_view hrtfname : device->mHrtfList)
1089 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1091 device->mHrtf = std::move(hrtf);
1092 device->mHrtfName = hrtfname;
1093 break;
1098 if(device->mHrtf)
1100 old_hrtf = nullptr;
1102 HrtfStore *hrtf{device->mHrtf.get()};
1103 device->mIrSize = hrtf->mIrSize;
1104 if(auto hrtfsizeopt = device->configValue<uint>({}, "hrtf-size"))
1106 if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
1107 device->mIrSize = std::max(*hrtfsizeopt, MinIrLength);
1110 InitHrtfPanning(device);
1111 device->PostProcess = &ALCdevice::ProcessHrtf;
1112 device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
1113 return;
1116 old_hrtf = nullptr;
1118 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Uhj)
1120 switch(UhjEncodeQuality)
1122 case UhjQualityType::IIR:
1123 device->mUhjEncoder = std::make_unique<UhjEncoderIIR>();
1124 break;
1125 case UhjQualityType::FIR256:
1126 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength256>>();
1127 break;
1128 case UhjQualityType::FIR512:
1129 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength512>>();
1130 break;
1132 assert(device->mUhjEncoder != nullptr);
1134 TRACE("UHJ enabled\n");
1135 InitUhjPanning(device);
1136 device->PostProcess = &ALCdevice::ProcessUhj;
1137 return;
1140 device->mRenderMode = RenderMode::Pairwise;
1141 if(device->Type != DeviceType::Loopback)
1143 if(auto cflevopt = device->configValue<int>({}, "cf_level"))
1145 if(*cflevopt > 0 && *cflevopt <= 6)
1147 auto bs2b = std::make_unique<Bs2b::bs2b>();
1148 bs2b->set_params(*cflevopt, static_cast<int>(device->Frequency));
1149 device->Bs2b = std::move(bs2b);
1150 TRACE("BS2B enabled\n");
1151 InitPanning(device);
1152 device->PostProcess = &ALCdevice::ProcessBs2b;
1153 return;
1158 TRACE("Stereo rendering\n");
1159 InitPanning(device);
1160 device->PostProcess = &ALCdevice::ProcessAmbiDec;
1164 void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
1166 DeviceBase *device{context->mDevice};
1167 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
1169 slot->mWetBuffer.resize(count);
1171 const auto acnmap = al::span{AmbiIndex::FromACN}.first(count);
1172 const auto iter = std::transform(acnmap.cbegin(), acnmap.cend(), slot->Wet.AmbiMap.begin(),
1173 [](const uint8_t &acn) noexcept -> BFChannelConfig { return BFChannelConfig{1.0f, acn}; });
1174 std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
1175 slot->Wet.Buffer = slot->mWetBuffer;