Use a boolean check instead of a function pointer
[openal-soft.git] / alc / panning.cpp
blobd0afd5779eadd44e255026dcd66a04422aa5663b
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 <chrono>
26 #include <cmath>
27 #include <cstdio>
28 #include <cstring>
29 #include <functional>
30 #include <iterator>
31 #include <memory>
32 #include <new>
33 #include <numeric>
34 #include <string>
36 #include "AL/al.h"
37 #include "AL/alc.h"
38 #include "AL/alext.h"
40 #include "al/auxeffectslot.h"
41 #include "albit.h"
42 #include "alconfig.h"
43 #include "alc/context.h"
44 #include "almalloc.h"
45 #include "alnumbers.h"
46 #include "alnumeric.h"
47 #include "aloptional.h"
48 #include "alspan.h"
49 #include "alstring.h"
50 #include "alu.h"
51 #include "core/ambdec.h"
52 #include "core/ambidefs.h"
53 #include "core/bformatdec.h"
54 #include "core/bs2b.h"
55 #include "core/devformat.h"
56 #include "core/front_stablizer.h"
57 #include "core/hrtf.h"
58 #include "core/logging.h"
59 #include "core/uhjfilter.h"
60 #include "device.h"
61 #include "opthelpers.h"
64 namespace {
66 using namespace std::placeholders;
67 using std::chrono::seconds;
68 using std::chrono::nanoseconds;
70 inline const char *GetLabelFromChannel(Channel channel)
72 switch(channel)
74 case FrontLeft: return "front-left";
75 case FrontRight: return "front-right";
76 case FrontCenter: return "front-center";
77 case LFE: return "lfe";
78 case BackLeft: return "back-left";
79 case BackRight: return "back-right";
80 case BackCenter: return "back-center";
81 case SideLeft: return "side-left";
82 case SideRight: return "side-right";
84 case TopFrontLeft: return "top-front-left";
85 case TopFrontCenter: return "top-front-center";
86 case TopFrontRight: return "top-front-right";
87 case TopCenter: return "top-center";
88 case TopBackLeft: return "top-back-left";
89 case TopBackCenter: return "top-back-center";
90 case TopBackRight: return "top-back-right";
92 case Aux0: return "Aux0";
93 case Aux1: return "Aux1";
94 case Aux2: return "Aux2";
95 case Aux3: return "Aux3";
96 case Aux4: return "Aux4";
97 case Aux5: return "Aux5";
98 case Aux6: return "Aux6";
99 case Aux7: return "Aux7";
100 case Aux8: return "Aux8";
101 case Aux9: return "Aux9";
102 case Aux10: return "Aux10";
103 case Aux11: return "Aux11";
104 case Aux12: return "Aux12";
105 case Aux13: return "Aux13";
106 case Aux14: return "Aux14";
107 case Aux15: return "Aux15";
109 case MaxChannels: break;
111 return "(unknown)";
115 std::unique_ptr<FrontStablizer> CreateStablizer(const size_t outchans, const uint srate)
117 auto stablizer = FrontStablizer::Create(outchans);
118 for(auto &buf : stablizer->DelayBuf)
119 std::fill(buf.begin(), buf.end(), 0.0f);
121 /* Initialize band-splitting filter for the mid signal, with a crossover at
122 * 5khz (could be higher).
124 stablizer->MidFilter.init(5000.0f / static_cast<float>(srate));
126 return stablizer;
129 void AllocChannels(ALCdevice *device, const size_t main_chans, const size_t real_chans)
131 TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans, real_chans);
133 /* Allocate extra channels for any post-filter output. */
134 const size_t num_chans{main_chans + real_chans};
136 TRACE("Allocating %zu channels, %zu bytes\n", num_chans,
137 num_chans*sizeof(device->MixBuffer[0]));
138 device->MixBuffer.resize(num_chans);
139 al::span<FloatBufferLine> buffer{device->MixBuffer};
141 device->Dry.Buffer = buffer.first(main_chans);
142 buffer = buffer.subspan(main_chans);
143 if(real_chans != 0)
145 device->RealOut.Buffer = buffer.first(real_chans);
146 buffer = buffer.subspan(real_chans);
148 else
149 device->RealOut.Buffer = device->Dry.Buffer;
153 using ChannelCoeffs = std::array<float,MaxAmbiChannels>;
154 enum DecoderMode : bool {
155 SingleBand = false,
156 DualBand = true
159 template<DecoderMode Mode, size_t N>
160 struct DecoderConfig;
162 template<size_t N>
163 struct DecoderConfig<SingleBand, N> {
164 uint8_t mOrder{};
165 bool mIs3D{};
166 std::array<Channel,N> mChannels{};
167 DevAmbiScaling mScaling{};
168 std::array<float,MaxAmbiOrder+1> mOrderGain{};
169 std::array<ChannelCoeffs,N> mCoeffs{};
172 template<size_t N>
173 struct DecoderConfig<DualBand, N> {
174 uint8_t mOrder{};
175 bool mIs3D{};
176 std::array<Channel,N> mChannels{};
177 DevAmbiScaling mScaling{};
178 std::array<float,MaxAmbiOrder+1> mOrderGain{};
179 std::array<ChannelCoeffs,N> mCoeffs{};
180 std::array<float,MaxAmbiOrder+1> mOrderGainLF{};
181 std::array<ChannelCoeffs,N> mCoeffsLF{};
184 template<>
185 struct DecoderConfig<DualBand, 0> {
186 uint8_t mOrder{};
187 bool mIs3D{};
188 al::span<const Channel> mChannels;
189 DevAmbiScaling mScaling{};
190 al::span<const float> mOrderGain;
191 al::span<const ChannelCoeffs> mCoeffs;
192 al::span<const float> mOrderGainLF;
193 al::span<const ChannelCoeffs> mCoeffsLF;
195 template<size_t N>
196 DecoderConfig& operator=(const DecoderConfig<SingleBand,N> &rhs) noexcept
198 mOrder = rhs.mOrder;
199 mIs3D = rhs.mIs3D;
200 mChannels = rhs.mChannels;
201 mScaling = rhs.mScaling;
202 mOrderGain = rhs.mOrderGain;
203 mCoeffs = rhs.mCoeffs;
204 mOrderGainLF = {};
205 mCoeffsLF = {};
206 return *this;
209 template<size_t N>
210 DecoderConfig& operator=(const DecoderConfig<DualBand,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 = rhs.mOrderGainLF;
219 mCoeffsLF = rhs.mCoeffsLF;
220 return *this;
223 explicit operator bool() const noexcept { return mOrder != 0; }
225 using DecoderView = DecoderConfig<DualBand, 0>;
228 void InitNearFieldCtrl(ALCdevice *device, float ctrl_dist, uint order, bool is3d)
230 static const uint chans_per_order2d[MaxAmbiOrder+1]{ 1, 2, 2, 2 };
231 static const uint chans_per_order3d[MaxAmbiOrder+1]{ 1, 3, 5, 7 };
233 /* NFC is only used when AvgSpeakerDist is greater than 0. */
234 if(!device->getConfigValueBool("decoder", "nfc", 0) || !(ctrl_dist > 0.0f))
235 return;
237 device->AvgSpeakerDist = clampf(ctrl_dist, 0.1f, 10.0f);
238 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
240 const float w1{SpeedOfSoundMetersPerSec /
241 (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
242 device->mNFCtrlFilter.init(w1);
244 auto iter = std::copy_n(is3d ? chans_per_order3d : chans_per_order2d, order+1u,
245 std::begin(device->NumChannelsPerOrder));
246 std::fill(iter, std::end(device->NumChannelsPerOrder), 0u);
249 void InitDistanceComp(ALCdevice *device, const al::span<const Channel> channels,
250 const al::span<const float,MAX_OUTPUT_CHANNELS> dists)
252 const float maxdist{std::accumulate(std::begin(dists), std::end(dists), 0.0f, maxf)};
254 if(!device->getConfigValueBool("decoder", "distance-comp", 1) || !(maxdist > 0.0f))
255 return;
257 const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
258 std::vector<DistanceComp::ChanData> ChanDelay;
259 ChanDelay.reserve(device->RealOut.Buffer.size());
260 size_t total{0u};
261 for(size_t chidx{0};chidx < channels.size();++chidx)
263 const Channel ch{channels[chidx]};
264 const uint idx{device->RealOut.ChannelIndex[ch]};
265 if(idx == INVALID_CHANNEL_INDEX)
266 continue;
268 const float distance{dists[chidx]};
270 /* Distance compensation only delays in steps of the sample rate. This
271 * is a bit less accurate since the delay time falls to the nearest
272 * sample time, but it's far simpler as it doesn't have to deal with
273 * phase offsets. This means at 48khz, for instance, the distance delay
274 * will be in steps of about 7 millimeters.
276 float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
277 if(delay > float{MAX_DELAY_LENGTH-1})
279 ERR("Delay for channel %u (%s) exceeds buffer length (%f > %d)\n", idx,
280 GetLabelFromChannel(ch), delay, MAX_DELAY_LENGTH-1);
281 delay = float{MAX_DELAY_LENGTH-1};
284 ChanDelay.resize(maxz(ChanDelay.size(), idx+1));
285 ChanDelay[idx].Length = static_cast<uint>(delay);
286 ChanDelay[idx].Gain = distance / maxdist;
287 TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
288 ChanDelay[idx].Length, ChanDelay[idx].Gain);
290 /* Round up to the next 4th sample, so each channel buffer starts
291 * 16-byte aligned.
293 total += RoundUp(ChanDelay[idx].Length, 4);
296 if(total > 0)
298 auto chandelays = DistanceComp::Create(total);
300 ChanDelay[0].Buffer = chandelays->mSamples.data();
301 auto set_bufptr = [](const DistanceComp::ChanData &last, const DistanceComp::ChanData &cur)
302 -> DistanceComp::ChanData
304 DistanceComp::ChanData ret{cur};
305 ret.Buffer = last.Buffer + RoundUp(last.Length, 4);
306 return ret;
308 std::partial_sum(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
309 set_bufptr);
310 device->ChannelDelays = std::move(chandelays);
315 inline auto& GetAmbiScales(DevAmbiScaling scaletype) noexcept
317 if(scaletype == DevAmbiScaling::FuMa) return AmbiScale::FromFuMa();
318 if(scaletype == DevAmbiScaling::SN3D) return AmbiScale::FromSN3D();
319 return AmbiScale::FromN3D();
322 inline auto& GetAmbiLayout(DevAmbiLayout layouttype) noexcept
324 if(layouttype == DevAmbiLayout::FuMa) return AmbiIndex::FromFuMa();
325 return AmbiIndex::FromACN();
329 DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf,
330 DecoderConfig<DualBand, MAX_OUTPUT_CHANNELS> &decoder)
332 DecoderView ret{};
334 decoder.mOrder = (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
335 (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
336 decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
338 switch(conf->CoeffScale)
340 case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
341 case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
342 case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
345 std::copy_n(std::begin(conf->HFOrderGain),
346 std::min(al::size(conf->HFOrderGain), al::size(decoder.mOrderGain)),
347 std::begin(decoder.mOrderGain));
348 std::copy_n(std::begin(conf->LFOrderGain),
349 std::min(al::size(conf->LFOrderGain), al::size(decoder.mOrderGainLF)),
350 std::begin(decoder.mOrderGainLF));
352 std::array<uint8_t,MaxAmbiChannels> idx_map{};
353 if(decoder.mIs3D)
355 uint flags{conf->ChanMask};
356 auto elem = idx_map.begin();
357 while(flags)
359 int acn{al::countr_zero(flags)};
360 flags &= ~(1u<<acn);
362 *elem = static_cast<uint8_t>(acn);
363 ++elem;
366 else
368 uint flags{conf->ChanMask};
369 auto elem = idx_map.begin();
370 while(flags)
372 int acn{al::countr_zero(flags)};
373 flags &= ~(1u<<acn);
375 switch(acn)
377 case 0: *elem = 0; break;
378 case 1: *elem = 1; break;
379 case 3: *elem = 2; break;
380 case 4: *elem = 3; break;
381 case 8: *elem = 4; break;
382 case 9: *elem = 5; break;
383 case 15: *elem = 6; break;
384 default: return ret;
386 ++elem;
389 const auto num_coeffs = static_cast<uint>(al::popcount(conf->ChanMask));
390 const auto hfmatrix = conf->HFMatrix;
391 const auto lfmatrix = conf->LFMatrix;
393 uint chan_count{0};
394 using const_speaker_span = al::span<const AmbDecConf::SpeakerConf>;
395 for(auto &speaker : const_speaker_span{conf->Speakers.get(), conf->NumSpeakers})
397 /* NOTE: AmbDec does not define any standard speaker names, however
398 * for this to work we have to by able to find the output channel
399 * the speaker definition corresponds to. Therefore, OpenAL Soft
400 * requires these channel labels to be recognized:
402 * LF = Front left
403 * RF = Front right
404 * LS = Side left
405 * RS = Side right
406 * LB = Back left
407 * RB = Back right
408 * CE = Front center
409 * CB = Back center
411 * Additionally, surround51 will acknowledge back speakers for side
412 * channels, to avoid issues with an ambdec expecting 5.1 to use the
413 * back channels.
415 Channel ch{};
416 if(speaker.Name == "LF")
417 ch = FrontLeft;
418 else if(speaker.Name == "RF")
419 ch = FrontRight;
420 else if(speaker.Name == "CE")
421 ch = FrontCenter;
422 else if(speaker.Name == "LS")
423 ch = SideLeft;
424 else if(speaker.Name == "RS")
425 ch = SideRight;
426 else if(speaker.Name == "LB")
427 ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
428 else if(speaker.Name == "RB")
429 ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
430 else if(speaker.Name == "CB")
431 ch = BackCenter;
432 else
434 int idx{};
435 char c{};
436 if(sscanf(speaker.Name.c_str(), "AUX%d%c", &idx, &c) != 1 || idx < 0
437 || idx >= MaxChannels-Aux0)
439 ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
440 continue;
442 ch = static_cast<Channel>(Aux0+idx);
445 decoder.mChannels[chan_count] = ch;
446 for(size_t src{0};src < num_coeffs;++src)
448 const size_t dst{idx_map[src]};
449 decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
451 if(conf->FreqBands > 1)
453 for(size_t src{0};src < num_coeffs;++src)
455 const size_t dst{idx_map[src]};
456 decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
459 ++chan_count;
462 if(chan_count > 0)
464 ret.mOrder = decoder.mOrder;
465 ret.mIs3D = decoder.mIs3D;
466 ret.mScaling = decoder.mScaling;
467 ret.mChannels = {decoder.mChannels.data(), chan_count};
468 ret.mOrderGain = decoder.mOrderGain;
469 ret.mCoeffs = {decoder.mCoeffs.data(), chan_count};
470 if(conf->FreqBands > 1)
472 ret.mOrderGainLF = decoder.mOrderGainLF;
473 ret.mCoeffsLF = {decoder.mCoeffsLF.data(), chan_count};
476 return ret;
479 constexpr DecoderConfig<SingleBand, 1> MonoConfig{
480 0, false, {{FrontCenter}},
481 DevAmbiScaling::N3D,
482 {{1.0f}},
483 {{ {{1.0f}} }}
485 constexpr DecoderConfig<SingleBand, 2> StereoConfig{
486 1, false, {{FrontLeft, FrontRight}},
487 DevAmbiScaling::N3D,
488 {{1.0f, 1.0f}},
490 {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
491 {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
494 constexpr DecoderConfig<DualBand, 4> QuadConfig{
495 2, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
496 DevAmbiScaling::N3D,
497 /*HF*/{{1.15470054e+0f, 1.00000000e+0f, 5.77350269e-1f}},
499 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
500 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
501 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
502 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
504 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
506 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
507 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
508 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
509 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
512 constexpr DecoderConfig<DualBand, 5> X51Config{
513 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
514 DevAmbiScaling::FuMa,
515 /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
517 {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
518 {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
519 {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
520 {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
521 {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
523 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
525 {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
526 {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
527 {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
528 {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
529 {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
532 constexpr DecoderConfig<SingleBand, 5> X61Config{
533 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
534 DevAmbiScaling::N3D,
535 {{1.0f, 1.0f, 1.0f}},
537 {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
538 {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
539 {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
540 {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
541 {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
544 constexpr DecoderConfig<DualBand, 6> X71Config{
545 3, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
546 DevAmbiScaling::N3D,
547 /*HF*/{{1.22474487e+0f, 1.13151672e+0f, 8.66025404e-1f, 4.68689571e-1f}},
549 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
550 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, -7.96819073e-2f, 0.00000000e+0f}},
551 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
552 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
553 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, 7.96819073e-2f, 0.00000000e+0f}},
554 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
556 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
558 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
559 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, -7.96819073e-2f, 0.00000000e+0f}},
560 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
561 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
562 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, 7.96819073e-2f, 0.00000000e+0f}},
563 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
566 constexpr DecoderConfig<DualBand, 6> X3D71Config{
567 1, true, {{Aux0, SideLeft, FrontLeft, FrontRight, SideRight, Aux1}},
568 DevAmbiScaling::N3D,
569 /*HF*/{{1.73205081e+0f, 1.00000000e+0f}},
571 {{1.66669447e-1f, 0.00000000e+0f, 2.36070520e-1f, -1.66153012e-1f}},
572 {{1.66669447e-1f, 2.04127551e-1f, -1.17487922e-1f, -1.66927066e-1f}},
573 {{1.66669447e-1f, 2.04127551e-1f, 1.17487922e-1f, 1.66927066e-1f}},
574 {{1.66669447e-1f, -2.04127551e-1f, 1.17487922e-1f, 1.66927066e-1f}},
575 {{1.66669447e-1f, -2.04127551e-1f, -1.17487922e-1f, -1.66927066e-1f}},
576 {{1.66669447e-1f, 0.00000000e+0f, -2.36070520e-1f, 1.66153012e-1f}},
578 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
580 {{1.66669447e-1f, 0.00000000e+0f, 2.36070520e-1f, -1.66153012e-1f}},
581 {{1.66669447e-1f, 2.04127551e-1f, -1.17487922e-1f, -1.66927066e-1f}},
582 {{1.66669447e-1f, 2.04127551e-1f, 1.17487922e-1f, 1.66927066e-1f}},
583 {{1.66669447e-1f, -2.04127551e-1f, 1.17487922e-1f, 1.66927066e-1f}},
584 {{1.66669447e-1f, -2.04127551e-1f, -1.17487922e-1f, -1.66927066e-1f}},
585 {{1.66669447e-1f, 0.00000000e+0f, -2.36070520e-1f, 1.66153012e-1f}},
589 void InitPanning(ALCdevice *device, const bool hqdec=false, const bool stablize=false,
590 DecoderView decoder={})
592 if(!decoder)
594 switch(device->FmtChans)
596 case DevFmtMono: decoder = MonoConfig; break;
597 case DevFmtStereo: decoder = StereoConfig; break;
598 case DevFmtQuad: decoder = QuadConfig; break;
599 case DevFmtX51: decoder = X51Config; break;
600 case DevFmtX61: decoder = X61Config; break;
601 case DevFmtX71: decoder = X71Config; break;
602 case DevFmtX3D71: decoder = X3D71Config; break;
603 case DevFmtAmbi3D:
604 auto&& acnmap = GetAmbiLayout(device->mAmbiLayout);
605 auto&& n3dscale = GetAmbiScales(device->mAmbiScale);
607 /* For DevFmtAmbi3D, the ambisonic order is already set. */
608 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
609 std::transform(acnmap.begin(), acnmap.begin()+count, std::begin(device->Dry.AmbiMap),
610 [&n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
611 { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
612 AllocChannels(device, count, 0);
614 float nfc_delay{device->configValue<float>("decoder", "nfc-ref-delay").value_or(0.0f)};
615 if(nfc_delay > 0.0f)
616 InitNearFieldCtrl(device, nfc_delay * SpeedOfSoundMetersPerSec, device->mAmbiOrder,
617 true);
618 return;
622 const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
623 al::vector<ChannelDec> chancoeffs, chancoeffslf;
624 for(size_t i{0u};i < decoder.mChannels.size();++i)
626 const uint idx{GetChannelIdxByName(device->RealOut, decoder.mChannels[i])};
627 if(idx == INVALID_CHANNEL_INDEX)
629 ERR("Failed to find %s channel in device\n",
630 GetLabelFromChannel(decoder.mChannels[i]));
631 continue;
634 chancoeffs.resize(maxz(chancoeffs.size(), idx+1u), ChannelDec{});
635 al::span<float,MaxAmbiChannels> coeffs{chancoeffs[idx]};
636 size_t ambichan{0};
637 for(uint o{0};o < decoder.mOrder+1u;++o)
639 const float order_gain{decoder.mOrderGain[o]};
640 const size_t order_max{decoder.mIs3D ? AmbiChannelsFromOrder(o) :
641 Ambi2DChannelsFromOrder(o)};
642 for(;ambichan < order_max;++ambichan)
643 coeffs[ambichan] = decoder.mCoeffs[i][ambichan] * order_gain;
645 if(!dual_band)
646 continue;
648 chancoeffslf.resize(maxz(chancoeffslf.size(), idx+1u), ChannelDec{});
649 coeffs = chancoeffslf[idx];
650 ambichan = 0;
651 for(uint o{0};o < decoder.mOrder+1u;++o)
653 const float order_gain{decoder.mOrderGainLF[o]};
654 const size_t order_max{decoder.mIs3D ? AmbiChannelsFromOrder(o) :
655 Ambi2DChannelsFromOrder(o)};
656 for(;ambichan < order_max;++ambichan)
657 coeffs[ambichan] = decoder.mCoeffsLF[i][ambichan] * order_gain;
661 /* For non-DevFmtAmbi3D, set the ambisonic order. */
662 device->mAmbiOrder = decoder.mOrder;
664 const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
665 Ambi2DChannelsFromOrder(decoder.mOrder)};
666 const al::span<const uint8_t> acnmap{decoder.mIs3D ? AmbiIndex::FromACN().data() :
667 AmbiIndex::FromACN2D().data(), ambicount};
668 auto&& coeffscale = GetAmbiScales(decoder.mScaling);
669 std::transform(acnmap.begin(), acnmap.end(), std::begin(device->Dry.AmbiMap),
670 [&coeffscale](const uint8_t &acn) noexcept
671 { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
672 AllocChannels(device, ambicount, device->channelsFromFmt());
674 std::unique_ptr<FrontStablizer> stablizer;
675 if(stablize)
677 /* Only enable the stablizer if the decoder does not output to the
678 * front-center channel.
680 const auto cidx = device->RealOut.ChannelIndex[FrontCenter];
681 bool hasfc{false};
682 if(cidx < chancoeffs.size())
684 for(const auto &coeff : chancoeffs[cidx])
685 hasfc |= coeff != 0.0f;
687 if(!hasfc && cidx < chancoeffslf.size())
689 for(const auto &coeff : chancoeffslf[cidx])
690 hasfc |= coeff != 0.0f;
692 if(!hasfc)
694 stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
695 TRACE("Front stablizer enabled\n");
699 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
700 !dual_band ? "single" : "dual",
701 (decoder.mOrder > 2) ? "third" :
702 (decoder.mOrder > 1) ? "second" : "first",
703 decoder.mIs3D ? " periphonic" : "");
704 device->AmbiDecoder = BFormatDec::Create(ambicount, chancoeffs, chancoeffslf,
705 device->mXOverFreq/static_cast<float>(device->Frequency), std::move(stablizer));
708 void InitHrtfPanning(ALCdevice *device)
710 constexpr float Deg180{al::numbers::pi_v<float>};
711 constexpr float Deg_90{Deg180 / 2.0f /* 90 degrees*/};
712 constexpr float Deg_45{Deg_90 / 2.0f /* 45 degrees*/};
713 constexpr float Deg135{Deg_45 * 3.0f /*135 degrees*/};
714 constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
715 constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
716 constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
717 constexpr float Deg_21{3.648638281e-01f /* 20~ 21 degrees*/};
718 static const AngularPoint AmbiPoints1O[]{
719 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
720 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
721 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
722 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
723 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
724 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
725 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
726 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
727 }, AmbiPoints2O[]{
728 { EvRadians{ 0.0f}, AzRadians{ 0.0f} },
729 { EvRadians{ 0.0f}, AzRadians{ Deg180} },
730 { EvRadians{ 0.0f}, AzRadians{-Deg_90} },
731 { EvRadians{ 0.0f}, AzRadians{ Deg_90} },
732 { EvRadians{ Deg_90}, AzRadians{ 0.0f} },
733 { EvRadians{-Deg_90}, AzRadians{ 0.0f} },
734 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
735 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
736 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
737 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
738 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
739 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
740 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
741 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
742 }, AmbiPoints3O[]{
743 { EvRadians{ Deg_69}, AzRadians{-Deg_90} },
744 { EvRadians{ Deg_69}, AzRadians{ Deg_90} },
745 { EvRadians{-Deg_69}, AzRadians{-Deg_90} },
746 { EvRadians{-Deg_69}, AzRadians{ Deg_90} },
747 { EvRadians{ 0.0f}, AzRadians{-Deg_69} },
748 { EvRadians{ 0.0f}, AzRadians{-Deg111} },
749 { EvRadians{ 0.0f}, AzRadians{ Deg_69} },
750 { EvRadians{ 0.0f}, AzRadians{ Deg111} },
751 { EvRadians{ Deg_21}, AzRadians{ 0.0f} },
752 { EvRadians{ Deg_21}, AzRadians{ Deg180} },
753 { EvRadians{-Deg_21}, AzRadians{ 0.0f} },
754 { EvRadians{-Deg_21}, AzRadians{ Deg180} },
755 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
756 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
757 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
758 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
759 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
760 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
761 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
762 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
764 static const float AmbiMatrix1O[][MaxAmbiChannels]{
765 { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
766 { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
767 { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
768 { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
769 { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
770 { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
771 { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
772 { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
773 }, AmbiMatrix2O[][MaxAmbiChannels]{
774 { 7.142857143e-02f, 0.000000000e+00f, 0.000000000e+00f, 1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, 1.290994449e-01f, },
775 { 7.142857143e-02f, 0.000000000e+00f, 0.000000000e+00f, -1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, 1.290994449e-01f, },
776 { 7.142857143e-02f, 1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, -1.290994449e-01f, },
777 { 7.142857143e-02f, -1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -7.453559925e-02f, 0.000000000e+00f, -1.290994449e-01f, },
778 { 7.142857143e-02f, 0.000000000e+00f, 1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 1.490711985e-01f, 0.000000000e+00f, 0.000000000e+00f, },
779 { 7.142857143e-02f, 0.000000000e+00f, -1.237179148e-01f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 1.490711985e-01f, 0.000000000e+00f, 0.000000000e+00f, },
780 { 7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, 9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
781 { 7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, -9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
782 { 7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, 7.142857143e-02f, -9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
783 { 7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, 9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
784 { 7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, 9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
785 { 7.142857143e-02f, 7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, -9.682458366e-02f, -9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
786 { 7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, 7.142857143e-02f, -9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, -9.682458366e-02f, 0.000000000e+00f, },
787 { 7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, -7.142857143e-02f, 9.682458366e-02f, 9.682458366e-02f, 0.000000000e+00f, 9.682458366e-02f, 0.000000000e+00f, },
788 }, AmbiMatrix3O[][MaxAmbiChannels]{
789 { 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, },
790 { 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, },
791 { 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, },
792 { 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, },
793 { 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, },
794 { 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, },
795 { 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, },
796 { 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, },
797 { 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, },
798 { 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, },
799 { 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, },
800 { 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, },
801 { 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, },
802 { 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, },
803 { 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, },
804 { 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, },
805 { 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, },
806 { 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, },
807 { 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, },
808 { 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, },
810 static const float AmbiOrderHFGain1O[MaxAmbiOrder+1]{
811 /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
812 }, AmbiOrderHFGain2O[MaxAmbiOrder+1]{
813 /*ENRGY 2.357022604e+00f, 1.825741858e+00f, 9.428090416e-01f*/
814 /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
815 /*RMS*/ 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f
816 }, AmbiOrderHFGain3O[MaxAmbiOrder+1]{
817 /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
818 /*AMP 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f*/
819 /*RMS*/ 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f
822 static_assert(al::size(AmbiPoints1O) == al::size(AmbiMatrix1O), "First-Order Ambisonic HRTF mismatch");
823 static_assert(al::size(AmbiPoints2O) == al::size(AmbiMatrix2O), "Second-Order Ambisonic HRTF mismatch");
824 static_assert(al::size(AmbiPoints3O) == al::size(AmbiMatrix3O), "Third-Order Ambisonic HRTF mismatch");
826 /* A 700hz crossover frequency provides tighter sound imaging at the sweet
827 * spot with ambisonic decoding, as the distance between the ears is closer
828 * to half this frequency wavelength, which is the optimal point where the
829 * response should change between optimizing phase vs volume. Normally this
830 * tighter imaging is at the cost of a smaller sweet spot, but since the
831 * listener is fixed in the center of the HRTF responses for the decoder,
832 * we don't have to worry about ever being out of the sweet spot.
834 * A better option here may be to have the head radius as part of the HRTF
835 * data set and calculate the optimal crossover frequency from that.
837 device->mXOverFreq = 700.0f;
839 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
840 * and it eases the CPU/memory load.
842 device->mRenderMode = RenderMode::Hrtf;
843 uint ambi_order{1};
844 if(auto modeopt = device->configValue<std::string>(nullptr, "hrtf-mode"))
846 struct HrtfModeEntry {
847 char name[8];
848 RenderMode mode;
849 uint order;
851 static const HrtfModeEntry hrtf_modes[]{
852 { "full", RenderMode::Hrtf, 1 },
853 { "ambi1", RenderMode::Normal, 1 },
854 { "ambi2", RenderMode::Normal, 2 },
855 { "ambi3", RenderMode::Normal, 3 },
858 const char *mode{modeopt->c_str()};
859 if(al::strcasecmp(mode, "basic") == 0)
861 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2");
862 mode = "ambi2";
865 auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
866 { return al::strcasecmp(mode, entry.name) == 0; };
867 auto iter = std::find_if(std::begin(hrtf_modes), std::end(hrtf_modes), match_entry);
868 if(iter == std::end(hrtf_modes))
869 ERR("Unexpected hrtf-mode: %s\n", mode);
870 else
872 device->mRenderMode = iter->mode;
873 ambi_order = iter->order;
876 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
877 (((ambi_order%100)/10) == 1) ? "th" :
878 ((ambi_order%10) == 1) ? "st" :
879 ((ambi_order%10) == 2) ? "nd" :
880 ((ambi_order%10) == 3) ? "rd" : "th",
881 (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
882 device->mHrtfName.c_str());
884 al::span<const AngularPoint> AmbiPoints{AmbiPoints1O};
885 const float (*AmbiMatrix)[MaxAmbiChannels]{AmbiMatrix1O};
886 al::span<const float,MaxAmbiOrder+1> AmbiOrderHFGain{AmbiOrderHFGain1O};
887 if(ambi_order >= 3)
889 AmbiPoints = AmbiPoints3O;
890 AmbiMatrix = AmbiMatrix3O;
891 AmbiOrderHFGain = AmbiOrderHFGain3O;
893 else if(ambi_order == 2)
895 AmbiPoints = AmbiPoints2O;
896 AmbiMatrix = AmbiMatrix2O;
897 AmbiOrderHFGain = AmbiOrderHFGain2O;
899 device->mAmbiOrder = ambi_order;
901 const size_t count{AmbiChannelsFromOrder(ambi_order)};
902 std::transform(AmbiIndex::FromACN().begin(), AmbiIndex::FromACN().begin()+count,
903 std::begin(device->Dry.AmbiMap),
904 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
906 AllocChannels(device, count, device->channelsFromFmt());
908 HrtfStore *Hrtf{device->mHrtf.get()};
909 auto hrtfstate = DirectHrtfState::Create(count);
910 hrtfstate->build(Hrtf, device->mIrSize, AmbiPoints, AmbiMatrix, device->mXOverFreq,
911 AmbiOrderHFGain);
912 device->mHrtfState = std::move(hrtfstate);
914 InitNearFieldCtrl(device, Hrtf->field[0].distance, ambi_order, true);
917 void InitUhjPanning(ALCdevice *device)
919 /* UHJ is always 2D first-order. */
920 constexpr size_t count{Ambi2DChannelsFromOrder(1)};
922 device->mAmbiOrder = 1;
924 auto acnmap_begin = AmbiIndex::FromFuMa().begin();
925 std::transform(acnmap_begin, acnmap_begin + count, std::begin(device->Dry.AmbiMap),
926 [](const uint8_t &acn) noexcept -> BFChannelConfig
927 { return BFChannelConfig{1.0f/AmbiScale::FromUHJ()[acn], acn}; });
928 AllocChannels(device, count, device->channelsFromFmt());
931 } // namespace
933 void aluInitRenderer(ALCdevice *device, int hrtf_id, al::optional<StereoEncoding> stereomode)
935 /* Hold the HRTF the device last used, in case it's used again. */
936 HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
938 device->mHrtfState = nullptr;
939 device->mHrtf = nullptr;
940 device->mIrSize = 0;
941 device->mHrtfName.clear();
942 device->mXOverFreq = 400.0f;
943 device->mRenderMode = RenderMode::Normal;
945 if(device->FmtChans != DevFmtStereo)
947 old_hrtf = nullptr;
948 if(stereomode && *stereomode == StereoEncoding::Hrtf)
949 device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
951 const char *layout{nullptr};
952 switch(device->FmtChans)
954 case DevFmtQuad: layout = "quad"; break;
955 case DevFmtX51: layout = "surround51"; break;
956 case DevFmtX61: layout = "surround61"; break;
957 case DevFmtX71: layout = "surround71"; break;
958 case DevFmtX3D71: layout = "surround3d71"; break;
959 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
960 case DevFmtMono:
961 case DevFmtStereo:
962 case DevFmtAmbi3D:
963 break;
966 std::unique_ptr<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>> decoder_store;
967 DecoderView decoder{};
968 float speakerdists[MAX_OUTPUT_CHANNELS]{};
969 auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
971 AmbDecConf conf{};
972 if(auto err = conf.load(config))
974 ERR("Failed to load layout file %s\n", config);
975 ERR(" %s\n", err->c_str());
977 else if(conf.NumSpeakers > MAX_OUTPUT_CHANNELS)
978 ERR("Unsupported decoder speaker count %zu (max %d)\n", conf.NumSpeakers,
979 MAX_OUTPUT_CHANNELS);
980 else if(conf.ChanMask > Ambi3OrderMask)
981 ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
982 Ambi3OrderMask);
983 else
985 device->mXOverFreq = clampf(conf.XOverFreq, 100.0f, 1000.0f);
987 decoder_store = std::make_unique<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>>();
988 decoder = MakeDecoderView(device, &conf, *decoder_store);
989 for(size_t i{0};i < decoder.mChannels.size();++i)
990 speakerdists[i] = conf.Speakers[i].Distance;
993 if(layout)
995 if(auto decopt = device->configValue<std::string>("decoder", layout))
996 load_config(decopt->c_str());
999 /* Enable the stablizer only for formats that have front-left, front-
1000 * right, and front-center outputs.
1002 const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != INVALID_CHANNEL_INDEX
1003 && device->RealOut.ChannelIndex[FrontLeft] != INVALID_CHANNEL_INDEX
1004 && device->RealOut.ChannelIndex[FrontRight] != INVALID_CHANNEL_INDEX
1005 && device->getConfigValueBool(nullptr, "front-stablizer", 0) != 0};
1006 const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", 1) != 0};
1007 InitPanning(device, hqdec, stablize, decoder);
1008 if(decoder.mOrder > 0)
1010 float accum_dist{0.0f}, spkr_count{0.0f};
1011 for(auto dist : speakerdists)
1013 if(dist > 0.0f)
1015 accum_dist += dist;
1016 spkr_count += 1.0f;
1019 if(spkr_count > 0)
1021 InitNearFieldCtrl(device, accum_dist / spkr_count, decoder.mOrder, decoder.mIs3D);
1022 InitDistanceComp(device, decoder.mChannels, speakerdists);
1025 if(auto *ambidec{device->AmbiDecoder.get()})
1027 device->PostProcess = ambidec->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
1028 : &ALCdevice::ProcessAmbiDec;
1030 return;
1034 /* If HRTF is explicitly requested, or if there's no explicit request and
1035 * the device is headphones, try to enable it.
1037 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Hrtf
1038 || (!stereomode && device->Flags.test(DirectEar)))
1040 if(device->mHrtfList.empty())
1041 device->enumerateHrtfs();
1043 if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
1045 const std::string &hrtfname = device->mHrtfList[static_cast<uint>(hrtf_id)];
1046 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1048 device->mHrtf = std::move(hrtf);
1049 device->mHrtfName = hrtfname;
1053 if(!device->mHrtf)
1055 for(const auto &hrtfname : device->mHrtfList)
1057 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1059 device->mHrtf = std::move(hrtf);
1060 device->mHrtfName = hrtfname;
1061 break;
1066 if(device->mHrtf)
1068 old_hrtf = nullptr;
1070 HrtfStore *hrtf{device->mHrtf.get()};
1071 device->mIrSize = hrtf->irSize;
1072 if(auto hrtfsizeopt = device->configValue<uint>(nullptr, "hrtf-size"))
1074 if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
1075 device->mIrSize = maxu(*hrtfsizeopt, MinIrLength);
1078 InitHrtfPanning(device);
1079 device->PostProcess = &ALCdevice::ProcessHrtf;
1080 device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
1081 return;
1084 old_hrtf = nullptr;
1086 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Uhj)
1088 device->mUhjEncoder = std::make_unique<UhjEncoder>();
1089 TRACE("UHJ enabled\n");
1090 InitUhjPanning(device);
1091 device->PostProcess = &ALCdevice::ProcessUhj;
1092 return;
1095 device->mRenderMode = RenderMode::Pairwise;
1096 if(device->Type != DeviceType::Loopback)
1098 if(auto cflevopt = device->configValue<int>(nullptr, "cf_level"))
1100 if(*cflevopt > 0 && *cflevopt <= 6)
1102 device->Bs2b = std::make_unique<bs2b>();
1103 bs2b_set_params(device->Bs2b.get(), *cflevopt,
1104 static_cast<int>(device->Frequency));
1105 TRACE("BS2B enabled\n");
1106 InitPanning(device);
1107 device->PostProcess = &ALCdevice::ProcessBs2b;
1108 return;
1113 TRACE("Stereo rendering\n");
1114 InitPanning(device);
1115 device->PostProcess = &ALCdevice::ProcessAmbiDec;
1119 void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
1121 DeviceBase *device{context->mDevice};
1122 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
1124 auto wetbuffer_iter = context->mWetBuffers.end();
1125 if(slot->mWetBuffer)
1127 /* If the effect slot already has a wet buffer attached, allocate a new
1128 * one in its place.
1130 wetbuffer_iter = context->mWetBuffers.begin();
1131 for(;wetbuffer_iter != context->mWetBuffers.end();++wetbuffer_iter)
1133 if(wetbuffer_iter->get() == slot->mWetBuffer)
1135 slot->mWetBuffer = nullptr;
1136 slot->Wet.Buffer = {};
1138 *wetbuffer_iter = WetBufferPtr{new(FamCount(count)) WetBuffer{count}};
1140 break;
1144 if(wetbuffer_iter == context->mWetBuffers.end())
1146 /* Otherwise, search for an unused wet buffer. */
1147 wetbuffer_iter = context->mWetBuffers.begin();
1148 for(;wetbuffer_iter != context->mWetBuffers.end();++wetbuffer_iter)
1150 if(!(*wetbuffer_iter)->mInUse)
1151 break;
1153 if(wetbuffer_iter == context->mWetBuffers.end())
1155 /* Otherwise, allocate a new one to use. */
1156 context->mWetBuffers.emplace_back(WetBufferPtr{new(FamCount(count)) WetBuffer{count}});
1157 wetbuffer_iter = context->mWetBuffers.end()-1;
1160 WetBuffer *wetbuffer{slot->mWetBuffer = wetbuffer_iter->get()};
1161 wetbuffer->mInUse = true;
1163 auto acnmap_begin = AmbiIndex::FromACN().begin();
1164 auto iter = std::transform(acnmap_begin, acnmap_begin + count, slot->Wet.AmbiMap.begin(),
1165 [](const uint8_t &acn) noexcept -> BFChannelConfig
1166 { return BFChannelConfig{1.0f, acn}; });
1167 std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
1168 slot->Wet.Buffer = wetbuffer->mBuffer;