Don't return a large-ish array on the stack
[openal-soft.git] / alc / panning.cpp
blobd118f99cf9263d4af695b926e4d5f8a0faa87402
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 <cassert>
26 #include <chrono>
27 #include <cmath>
28 #include <cstdio>
29 #include <cstring>
30 #include <functional>
31 #include <iterator>
32 #include <memory>
33 #include <new>
34 #include <numeric>
35 #include <string>
37 #include "AL/al.h"
38 #include "AL/alc.h"
39 #include "AL/alext.h"
41 #include "al/auxeffectslot.h"
42 #include "albit.h"
43 #include "alconfig.h"
44 #include "alc/context.h"
45 #include "almalloc.h"
46 #include "alnumbers.h"
47 #include "alnumeric.h"
48 #include "aloptional.h"
49 #include "alspan.h"
50 #include "alstring.h"
51 #include "alu.h"
52 #include "core/ambdec.h"
53 #include "core/ambidefs.h"
54 #include "core/bformatdec.h"
55 #include "core/bs2b.h"
56 #include "core/devformat.h"
57 #include "core/front_stablizer.h"
58 #include "core/hrtf.h"
59 #include "core/logging.h"
60 #include "core/uhjfilter.h"
61 #include "device.h"
62 #include "opthelpers.h"
65 namespace {
67 using namespace std::placeholders;
68 using std::chrono::seconds;
69 using std::chrono::nanoseconds;
71 inline const char *GetLabelFromChannel(Channel channel)
73 switch(channel)
75 case FrontLeft: return "front-left";
76 case FrontRight: return "front-right";
77 case FrontCenter: return "front-center";
78 case LFE: return "lfe";
79 case BackLeft: return "back-left";
80 case BackRight: return "back-right";
81 case BackCenter: return "back-center";
82 case SideLeft: return "side-left";
83 case SideRight: return "side-right";
85 case TopFrontLeft: return "top-front-left";
86 case TopFrontCenter: return "top-front-center";
87 case TopFrontRight: return "top-front-right";
88 case TopCenter: return "top-center";
89 case TopBackLeft: return "top-back-left";
90 case TopBackCenter: return "top-back-center";
91 case TopBackRight: return "top-back-right";
93 case Aux0: return "Aux0";
94 case Aux1: return "Aux1";
95 case Aux2: return "Aux2";
96 case Aux3: return "Aux3";
97 case Aux4: return "Aux4";
98 case Aux5: return "Aux5";
99 case Aux6: return "Aux6";
100 case Aux7: return "Aux7";
101 case Aux8: return "Aux8";
102 case Aux9: return "Aux9";
103 case Aux10: return "Aux10";
104 case Aux11: return "Aux11";
105 case Aux12: return "Aux12";
106 case Aux13: return "Aux13";
107 case Aux14: return "Aux14";
108 case Aux15: return "Aux15";
110 case MaxChannels: break;
112 return "(unknown)";
116 std::unique_ptr<FrontStablizer> CreateStablizer(const size_t outchans, const uint srate)
118 auto stablizer = FrontStablizer::Create(outchans);
120 /* Initialize band-splitting filter for the mid signal, with a crossover at
121 * 5khz (could be higher).
123 stablizer->MidFilter.init(5000.0f / static_cast<float>(srate));
124 for(auto &filter : stablizer->ChannelFilters)
125 filter = stablizer->MidFilter;
127 return stablizer;
130 void AllocChannels(ALCdevice *device, const size_t main_chans, const size_t real_chans)
132 TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans, real_chans);
134 /* Allocate extra channels for any post-filter output. */
135 const size_t num_chans{main_chans + real_chans};
137 TRACE("Allocating %zu channels, %zu bytes\n", num_chans,
138 num_chans*sizeof(device->MixBuffer[0]));
139 device->MixBuffer.resize(num_chans);
140 al::span<FloatBufferLine> buffer{device->MixBuffer};
142 device->Dry.Buffer = buffer.first(main_chans);
143 buffer = buffer.subspan(main_chans);
144 if(real_chans != 0)
146 device->RealOut.Buffer = buffer.first(real_chans);
147 buffer = buffer.subspan(real_chans);
149 else
150 device->RealOut.Buffer = device->Dry.Buffer;
154 using ChannelCoeffs = std::array<float,MaxAmbiChannels>;
155 enum DecoderMode : bool {
156 SingleBand = false,
157 DualBand = true
160 template<DecoderMode Mode, size_t N>
161 struct DecoderConfig;
163 template<size_t N>
164 struct DecoderConfig<SingleBand, N> {
165 uint8_t mOrder{};
166 bool mIs3D{};
167 std::array<Channel,N> mChannels{};
168 DevAmbiScaling mScaling{};
169 std::array<float,MaxAmbiOrder+1> mOrderGain{};
170 std::array<ChannelCoeffs,N> mCoeffs{};
173 template<size_t N>
174 struct DecoderConfig<DualBand, N> {
175 uint8_t mOrder{};
176 bool mIs3D{};
177 std::array<Channel,N> mChannels{};
178 DevAmbiScaling mScaling{};
179 std::array<float,MaxAmbiOrder+1> mOrderGain{};
180 std::array<ChannelCoeffs,N> mCoeffs{};
181 std::array<float,MaxAmbiOrder+1> mOrderGainLF{};
182 std::array<ChannelCoeffs,N> mCoeffsLF{};
185 template<>
186 struct DecoderConfig<DualBand, 0> {
187 uint8_t mOrder{};
188 bool mIs3D{};
189 al::span<const Channel> mChannels;
190 DevAmbiScaling mScaling{};
191 al::span<const float> mOrderGain;
192 al::span<const ChannelCoeffs> mCoeffs;
193 al::span<const float> mOrderGainLF;
194 al::span<const ChannelCoeffs> mCoeffsLF;
196 template<size_t N>
197 DecoderConfig& operator=(const DecoderConfig<SingleBand,N> &rhs) noexcept
199 mOrder = rhs.mOrder;
200 mIs3D = rhs.mIs3D;
201 mChannels = rhs.mChannels;
202 mScaling = rhs.mScaling;
203 mOrderGain = rhs.mOrderGain;
204 mCoeffs = rhs.mCoeffs;
205 mOrderGainLF = {};
206 mCoeffsLF = {};
207 return *this;
210 template<size_t N>
211 DecoderConfig& operator=(const DecoderConfig<DualBand,N> &rhs) noexcept
213 mOrder = rhs.mOrder;
214 mIs3D = rhs.mIs3D;
215 mChannels = rhs.mChannels;
216 mScaling = rhs.mScaling;
217 mOrderGain = rhs.mOrderGain;
218 mCoeffs = rhs.mCoeffs;
219 mOrderGainLF = rhs.mOrderGainLF;
220 mCoeffsLF = rhs.mCoeffsLF;
221 return *this;
224 explicit operator bool() const noexcept { return !mChannels.empty(); }
226 using DecoderView = DecoderConfig<DualBand, 0>;
229 void InitNearFieldCtrl(ALCdevice *device, float ctrl_dist, uint order, bool is3d)
231 static const uint chans_per_order2d[MaxAmbiOrder+1]{ 1, 2, 2, 2 };
232 static const uint chans_per_order3d[MaxAmbiOrder+1]{ 1, 3, 5, 7 };
234 /* NFC is only used when AvgSpeakerDist is greater than 0. */
235 if(!device->getConfigValueBool("decoder", "nfc", false) || !(ctrl_dist > 0.0f))
236 return;
238 device->AvgSpeakerDist = clampf(ctrl_dist, 0.1f, 10.0f);
239 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
241 const float w1{SpeedOfSoundMetersPerSec /
242 (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
243 device->mNFCtrlFilter.init(w1);
245 auto iter = std::copy_n(is3d ? chans_per_order3d : chans_per_order2d, order+1u,
246 std::begin(device->NumChannelsPerOrder));
247 std::fill(iter, std::end(device->NumChannelsPerOrder), 0u);
250 void InitDistanceComp(ALCdevice *device, const al::span<const Channel> channels,
251 const al::span<const float,MAX_OUTPUT_CHANNELS> dists)
253 const float maxdist{std::accumulate(std::begin(dists), std::end(dists), 0.0f, maxf)};
255 if(!device->getConfigValueBool("decoder", "distance-comp", true) || !(maxdist > 0.0f))
256 return;
258 const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
259 std::vector<DistanceComp::ChanData> ChanDelay;
260 ChanDelay.reserve(device->RealOut.Buffer.size());
261 size_t total{0u};
262 for(size_t chidx{0};chidx < channels.size();++chidx)
264 const Channel ch{channels[chidx]};
265 const uint idx{device->RealOut.ChannelIndex[ch]};
266 if(idx == InvalidChannelIndex)
267 continue;
269 const float distance{dists[chidx]};
271 /* Distance compensation only delays in steps of the sample rate. This
272 * is a bit less accurate since the delay time falls to the nearest
273 * sample time, but it's far simpler as it doesn't have to deal with
274 * phase offsets. This means at 48khz, for instance, the distance delay
275 * will be in steps of about 7 millimeters.
277 float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
278 if(delay > float{DistanceComp::MaxDelay-1})
280 ERR("Delay for channel %u (%s) exceeds buffer length (%f > %d)\n", idx,
281 GetLabelFromChannel(ch), delay, DistanceComp::MaxDelay-1);
282 delay = float{DistanceComp::MaxDelay-1};
285 ChanDelay.resize(maxz(ChanDelay.size(), idx+1));
286 ChanDelay[idx].Length = static_cast<uint>(delay);
287 ChanDelay[idx].Gain = distance / maxdist;
288 TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
289 ChanDelay[idx].Length, ChanDelay[idx].Gain);
291 /* Round up to the next 4th sample, so each channel buffer starts
292 * 16-byte aligned.
294 total += RoundUp(ChanDelay[idx].Length, 4);
297 if(total > 0)
299 auto chandelays = DistanceComp::Create(total);
301 ChanDelay[0].Buffer = chandelays->mSamples.data();
302 auto set_bufptr = [](const DistanceComp::ChanData &last, const DistanceComp::ChanData &cur)
303 -> DistanceComp::ChanData
305 DistanceComp::ChanData ret{cur};
306 ret.Buffer = last.Buffer + RoundUp(last.Length, 4);
307 return ret;
309 std::partial_sum(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
310 set_bufptr);
311 device->ChannelDelays = std::move(chandelays);
316 inline auto& GetAmbiScales(DevAmbiScaling scaletype) noexcept
318 if(scaletype == DevAmbiScaling::FuMa) return AmbiScale::FromFuMa();
319 if(scaletype == DevAmbiScaling::SN3D) return AmbiScale::FromSN3D();
320 return AmbiScale::FromN3D();
323 inline auto& GetAmbiLayout(DevAmbiLayout layouttype) noexcept
325 if(layouttype == DevAmbiLayout::FuMa) return AmbiIndex::FromFuMa();
326 return AmbiIndex::FromACN();
330 DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf,
331 DecoderConfig<DualBand, MAX_OUTPUT_CHANNELS> &decoder)
333 DecoderView ret{};
335 decoder.mOrder = (conf->ChanMask > Ambi3OrderMask) ? uint8_t{4} :
336 (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
337 (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
338 decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
340 switch(conf->CoeffScale)
342 case AmbDecScale::Unset: ASSUME(false); break;
343 case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
344 case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
345 case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
348 std::copy_n(std::begin(conf->HFOrderGain),
349 std::min(al::size(conf->HFOrderGain), al::size(decoder.mOrderGain)),
350 std::begin(decoder.mOrderGain));
351 std::copy_n(std::begin(conf->LFOrderGain),
352 std::min(al::size(conf->LFOrderGain), al::size(decoder.mOrderGainLF)),
353 std::begin(decoder.mOrderGainLF));
355 const auto num_coeffs = decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder)
356 : Ambi2DChannelsFromOrder(decoder.mOrder);
357 const auto idx_map = decoder.mIs3D ? AmbiIndex::FromACN().data()
358 : AmbiIndex::FromACN2D().data();
359 const auto hfmatrix = conf->HFMatrix;
360 const auto lfmatrix = conf->LFMatrix;
362 uint chan_count{0};
363 using const_speaker_span = al::span<const AmbDecConf::SpeakerConf>;
364 for(auto &speaker : const_speaker_span{conf->Speakers.get(), conf->NumSpeakers})
366 /* NOTE: AmbDec does not define any standard speaker names, however
367 * for this to work we have to by able to find the output channel
368 * the speaker definition corresponds to. Therefore, OpenAL Soft
369 * requires these channel labels to be recognized:
371 * LF = Front left
372 * RF = Front right
373 * LS = Side left
374 * RS = Side right
375 * LB = Back left
376 * RB = Back right
377 * CE = Front center
378 * CB = Back center
379 * LFT = Top front left
380 * RFT = Top front right
381 * LBT = Top back left
382 * RBT = Top back right
384 * Additionally, surround51 will acknowledge back speakers for side
385 * channels, to avoid issues with an ambdec expecting 5.1 to use the
386 * back channels.
388 Channel ch{};
389 if(speaker.Name == "LF")
390 ch = FrontLeft;
391 else if(speaker.Name == "RF")
392 ch = FrontRight;
393 else if(speaker.Name == "CE")
394 ch = FrontCenter;
395 else if(speaker.Name == "LS")
396 ch = SideLeft;
397 else if(speaker.Name == "RS")
398 ch = SideRight;
399 else if(speaker.Name == "LB")
400 ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
401 else if(speaker.Name == "RB")
402 ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
403 else if(speaker.Name == "CB")
404 ch = BackCenter;
405 else if(speaker.Name == "LFT")
406 ch = TopFrontLeft;
407 else if(speaker.Name == "RFT")
408 ch = TopFrontRight;
409 else if(speaker.Name == "LBT")
410 ch = TopBackLeft;
411 else if(speaker.Name == "RBT")
412 ch = TopBackRight;
413 else
415 int idx{};
416 char c{};
417 if(sscanf(speaker.Name.c_str(), "AUX%d%c", &idx, &c) != 1 || idx < 0
418 || idx >= MaxChannels-Aux0)
420 ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
421 continue;
423 ch = static_cast<Channel>(Aux0+idx);
426 decoder.mChannels[chan_count] = ch;
427 for(size_t dst{0};dst < num_coeffs;++dst)
429 const size_t src{idx_map[dst]};
430 decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
432 if(conf->FreqBands > 1)
434 for(size_t dst{0};dst < num_coeffs;++dst)
436 const size_t src{idx_map[dst]};
437 decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
440 ++chan_count;
443 if(chan_count > 0)
445 ret.mOrder = decoder.mOrder;
446 ret.mIs3D = decoder.mIs3D;
447 ret.mScaling = decoder.mScaling;
448 ret.mChannels = {decoder.mChannels.data(), chan_count};
449 ret.mOrderGain = decoder.mOrderGain;
450 ret.mCoeffs = {decoder.mCoeffs.data(), chan_count};
451 if(conf->FreqBands > 1)
453 ret.mOrderGainLF = decoder.mOrderGainLF;
454 ret.mCoeffsLF = {decoder.mCoeffsLF.data(), chan_count};
457 return ret;
460 constexpr DecoderConfig<SingleBand, 1> MonoConfig{
461 0, false, {{FrontCenter}},
462 DevAmbiScaling::N3D,
463 {{1.0f}},
464 {{ {{1.0f}} }}
466 constexpr DecoderConfig<SingleBand, 2> StereoConfig{
467 1, false, {{FrontLeft, FrontRight}},
468 DevAmbiScaling::N3D,
469 {{1.0f, 1.0f}},
471 {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
472 {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
475 constexpr DecoderConfig<DualBand, 4> QuadConfig{
476 1, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
477 DevAmbiScaling::N3D,
478 /*HF*/{{1.41421356e+0f, 1.00000000e+0f}},
480 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
481 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
482 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
483 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
485 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
487 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f}},
488 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f}},
489 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f}},
490 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f}},
493 constexpr DecoderConfig<DualBand, 5> X51Config{
494 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
495 DevAmbiScaling::FuMa,
496 /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
498 {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
499 {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
500 {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
501 {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
502 {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
504 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
506 {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
507 {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
508 {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
509 {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
510 {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
513 constexpr DecoderConfig<SingleBand, 5> X61Config{
514 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
515 DevAmbiScaling::N3D,
516 {{1.0f, 1.0f, 1.0f}},
518 {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
519 {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
520 {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
521 {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
522 {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
525 constexpr DecoderConfig<DualBand, 6> X71Config{
526 2, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
527 DevAmbiScaling::N3D,
528 /*HF*/{{1.41421356e+0f, 1.22474487e+0f, 7.07106781e-1f}},
530 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
531 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
532 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
533 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
534 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
535 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
537 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
539 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
540 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
541 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
542 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f}},
543 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f}},
544 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f}},
547 constexpr DecoderConfig<DualBand, 6> X3D71Config{
548 1, true, {{Aux0, SideLeft, FrontLeft, FrontRight, SideRight, Aux1}},
549 DevAmbiScaling::N3D,
550 /*HF*/{{1.73205081e+0f, 1.00000000e+0f}},
552 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
553 {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
554 {{1.666666667e-01f, 2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
555 {{1.666666667e-01f, -2.033043281e-01f, 1.175581508e-01f, 1.678904388e-01f}},
556 {{1.666666667e-01f, -2.033043281e-01f, -1.175581508e-01f, -1.678904388e-01f}},
557 {{1.666666667e-01f, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
559 /*LF*/{{1.00000000e+0f, 1.00000000e+0f}},
561 {{1.666666667e-01f, 0.000000000e+00f, 2.356640879e-01f, -1.667265410e-01f}},
562 {{1.666666667e-01f, 2.033043281e-01f, -1.175581508e-01f, -1.678904388e-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, 0.000000000e+00f, -2.356640879e-01f, 1.667265410e-01f}},
569 constexpr DecoderConfig<SingleBand, 10> X714Config{
570 1, true, {{FrontLeft, FrontRight, SideLeft, SideRight, BackLeft, BackRight, TopFrontLeft, TopFrontRight, TopBackLeft, TopBackRight }},
571 DevAmbiScaling::N3D,
572 {{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
574 {{1.27149251e-01f, 7.63047539e-02f, -3.64373750e-02f, 1.59700680e-01f}},
575 {{1.07005418e-01f, -7.67638760e-02f, -4.92129762e-02f, 1.29012797e-01f}},
576 {{1.26400196e-01f, 1.77494694e-01f, -3.71203389e-02f, 0.00000000e+00f}},
577 {{1.26396516e-01f, -1.77488059e-01f, -3.71297878e-02f, 0.00000000e+00f}},
578 {{1.06996956e-01f, 7.67615256e-02f, -4.92166307e-02f, -1.29001640e-01f}},
579 {{1.27145671e-01f, -7.63003471e-02f, -3.64353304e-02f, -1.59697510e-01f}},
580 {{8.80919747e-02f, 7.48940670e-02f, 9.08786244e-02f, 6.22527183e-02f}},
581 {{1.57880745e-01f, -7.28755272e-02f, 1.82364187e-01f, 8.74240284e-02f}},
582 {{1.57892225e-01f, 7.28944768e-02f, 1.82363474e-01f, -8.74301086e-02f}},
583 {{8.80892603e-02f, -7.48948724e-02f, 9.08779842e-02f, -6.22480443e-02f}},
587 void InitPanning(ALCdevice *device, const bool hqdec=false, const bool stablize=false,
588 DecoderView decoder={})
590 if(!decoder)
592 switch(device->FmtChans)
594 case DevFmtMono: decoder = MonoConfig; break;
595 case DevFmtStereo: decoder = StereoConfig; break;
596 case DevFmtQuad: decoder = QuadConfig; break;
597 case DevFmtX51: decoder = X51Config; break;
598 case DevFmtX61: decoder = X61Config; break;
599 case DevFmtX71: decoder = X71Config; break;
600 case DevFmtX714: decoder = X714Config; break;
601 case DevFmtX3D71: decoder = X3D71Config; break;
602 case DevFmtAmbi3D:
603 auto&& acnmap = GetAmbiLayout(device->mAmbiLayout);
604 auto&& n3dscale = GetAmbiScales(device->mAmbiScale);
606 /* For DevFmtAmbi3D, the ambisonic order is already set. */
607 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
608 std::transform(acnmap.begin(), acnmap.begin()+count, std::begin(device->Dry.AmbiMap),
609 [&n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
610 { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
611 AllocChannels(device, count, 0);
612 device->m2DMixing = false;
614 float avg_dist{};
615 if(auto distopt = device->configValue<float>("decoder", "speaker-dist"))
616 avg_dist = *distopt;
617 else if(auto delayopt = device->configValue<float>("decoder", "nfc-ref-delay"))
619 WARN("nfc-ref-delay is deprecated, use speaker-dist instead\n");
620 avg_dist = *delayopt * SpeedOfSoundMetersPerSec;
623 InitNearFieldCtrl(device, avg_dist, device->mAmbiOrder, true);
624 return;
628 const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
629 Ambi2DChannelsFromOrder(decoder.mOrder)};
630 const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
631 al::vector<ChannelDec> chancoeffs, chancoeffslf;
632 for(size_t i{0u};i < decoder.mChannels.size();++i)
634 const uint idx{device->channelIdxByName(decoder.mChannels[i])};
635 if(idx == InvalidChannelIndex)
637 ERR("Failed to find %s channel in device\n",
638 GetLabelFromChannel(decoder.mChannels[i]));
639 continue;
642 auto ordermap = decoder.mIs3D ? AmbiIndex::OrderFromChannel().data()
643 : AmbiIndex::OrderFrom2DChannel().data();
645 chancoeffs.resize(maxz(chancoeffs.size(), idx+1u), ChannelDec{});
646 al::span<const float,MaxAmbiChannels> src{decoder.mCoeffs[i]};
647 al::span<float,MaxAmbiChannels> dst{chancoeffs[idx]};
648 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
649 dst[ambichan] = src[ambichan] * decoder.mOrderGain[ordermap[ambichan]];
651 if(!dual_band)
652 continue;
654 chancoeffslf.resize(maxz(chancoeffslf.size(), idx+1u), ChannelDec{});
655 src = decoder.mCoeffsLF[i];
656 dst = chancoeffslf[idx];
657 for(size_t ambichan{0};ambichan < ambicount;++ambichan)
658 dst[ambichan] = src[ambichan] * decoder.mOrderGainLF[ordermap[ambichan]];
661 /* For non-DevFmtAmbi3D, set the ambisonic order. */
662 device->mAmbiOrder = decoder.mOrder;
663 device->m2DMixing = !decoder.mIs3D;
665 const al::span<const uint8_t> acnmap{decoder.mIs3D ? AmbiIndex::FromACN().data() :
666 AmbiIndex::FromACN2D().data(), ambicount};
667 auto&& coeffscale = GetAmbiScales(decoder.mScaling);
668 std::transform(acnmap.begin(), acnmap.end(), std::begin(device->Dry.AmbiMap),
669 [&coeffscale](const uint8_t &acn) noexcept
670 { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
671 AllocChannels(device, ambicount, device->channelsFromFmt());
673 std::unique_ptr<FrontStablizer> stablizer;
674 if(stablize)
676 /* Only enable the stablizer if the decoder does not output to the
677 * front-center channel.
679 const auto cidx = device->RealOut.ChannelIndex[FrontCenter];
680 bool hasfc{false};
681 if(cidx < chancoeffs.size())
683 for(const auto &coeff : chancoeffs[cidx])
684 hasfc |= coeff != 0.0f;
686 if(!hasfc && cidx < chancoeffslf.size())
688 for(const auto &coeff : chancoeffslf[cidx])
689 hasfc |= coeff != 0.0f;
691 if(!hasfc)
693 stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
694 TRACE("Front stablizer enabled\n");
698 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
699 !dual_band ? "single" : "dual",
700 (decoder.mOrder > 3) ? "fourth" :
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_21{3.648638281e-01f /* 20~ 21 degrees*/};
715 constexpr float Deg_32{5.535743589e-01f /* 31~ 32 degrees*/};
716 constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
717 constexpr float Deg_58{1.017221968e+00f /* 58~ 59 degrees*/};
718 constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
719 constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
720 constexpr float Deg122{2.124370686e+00f /*121~122 degrees*/};
721 static const AngularPoint AmbiPoints1O[]{
722 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
723 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
724 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
725 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
726 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
727 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
728 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
729 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
730 }, AmbiPoints2O[]{
731 { EvRadians{-Deg_32}, AzRadians{ 0.0f} },
732 { EvRadians{ 0.0f}, AzRadians{ Deg_58} },
733 { EvRadians{ Deg_58}, AzRadians{ Deg_90} },
734 { EvRadians{ Deg_32}, AzRadians{ 0.0f} },
735 { EvRadians{ 0.0f}, AzRadians{ Deg122} },
736 { EvRadians{-Deg_58}, AzRadians{-Deg_90} },
737 { EvRadians{-Deg_32}, AzRadians{ Deg180} },
738 { EvRadians{ 0.0f}, AzRadians{-Deg122} },
739 { EvRadians{ Deg_58}, AzRadians{-Deg_90} },
740 { EvRadians{ Deg_32}, AzRadians{ Deg180} },
741 { EvRadians{ 0.0f}, AzRadians{-Deg_58} },
742 { EvRadians{-Deg_58}, AzRadians{ Deg_90} },
743 }, AmbiPoints3O[]{
744 { EvRadians{ Deg_69}, AzRadians{-Deg_90} },
745 { EvRadians{ Deg_69}, AzRadians{ Deg_90} },
746 { EvRadians{-Deg_69}, AzRadians{-Deg_90} },
747 { EvRadians{-Deg_69}, AzRadians{ Deg_90} },
748 { EvRadians{ 0.0f}, AzRadians{-Deg_69} },
749 { EvRadians{ 0.0f}, AzRadians{-Deg111} },
750 { EvRadians{ 0.0f}, AzRadians{ Deg_69} },
751 { EvRadians{ 0.0f}, AzRadians{ Deg111} },
752 { EvRadians{ Deg_21}, AzRadians{ 0.0f} },
753 { EvRadians{ Deg_21}, AzRadians{ Deg180} },
754 { EvRadians{-Deg_21}, AzRadians{ 0.0f} },
755 { EvRadians{-Deg_21}, AzRadians{ Deg180} },
756 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
757 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
758 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
759 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
760 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
761 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
762 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
763 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
765 static const float AmbiMatrix1O[][MaxAmbiChannels]{
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 { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
774 }, AmbiMatrix2O[][MaxAmbiChannels]{
775 { 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, },
776 { 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, },
777 { 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, },
778 { 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, },
779 { 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, },
780 { 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, },
781 { 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, },
782 { 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, },
783 { 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, },
784 { 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, },
785 { 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, },
786 { 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, },
787 }, AmbiMatrix3O[][MaxAmbiChannels]{
788 { 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, },
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, 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, },
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, 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, },
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, 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, },
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, },
809 static const float AmbiOrderHFGain1O[MaxAmbiOrder+1]{
810 /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
811 }, AmbiOrderHFGain2O[MaxAmbiOrder+1]{
812 /*ENRGY*/ 1.825741858e+00f, 1.414213562e+00f, 7.302967433e-01f
813 /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
814 /*RMS 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f*/
815 }, AmbiOrderHFGain3O[MaxAmbiOrder+1]{
816 /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
817 /*AMP*/ 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f
818 /*RMS 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f*/
821 static_assert(al::size(AmbiPoints1O) == al::size(AmbiMatrix1O), "First-Order Ambisonic HRTF mismatch");
822 static_assert(al::size(AmbiPoints2O) == al::size(AmbiMatrix2O), "Second-Order Ambisonic HRTF mismatch");
823 static_assert(al::size(AmbiPoints3O) == al::size(AmbiMatrix3O), "Third-Order Ambisonic HRTF mismatch");
825 /* A 700hz crossover frequency provides tighter sound imaging at the sweet
826 * spot with ambisonic decoding, as the distance between the ears is closer
827 * to half this frequency wavelength, which is the optimal point where the
828 * response should change between optimizing phase vs volume. Normally this
829 * tighter imaging is at the cost of a smaller sweet spot, but since the
830 * listener is fixed in the center of the HRTF responses for the decoder,
831 * we don't have to worry about ever being out of the sweet spot.
833 * A better option here may be to have the head radius as part of the HRTF
834 * data set and calculate the optimal crossover frequency from that.
836 device->mXOverFreq = 700.0f;
838 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
839 * and it eases the CPU/memory load.
841 device->mRenderMode = RenderMode::Hrtf;
842 uint ambi_order{1};
843 if(auto modeopt = device->configValue<std::string>(nullptr, "hrtf-mode"))
845 struct HrtfModeEntry {
846 char name[8];
847 RenderMode mode;
848 uint order;
850 static const HrtfModeEntry hrtf_modes[]{
851 { "full", RenderMode::Hrtf, 1 },
852 { "ambi1", RenderMode::Normal, 1 },
853 { "ambi2", RenderMode::Normal, 2 },
854 { "ambi3", RenderMode::Normal, 3 },
857 const char *mode{modeopt->c_str()};
858 if(al::strcasecmp(mode, "basic") == 0)
860 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2");
861 mode = "ambi2";
864 auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
865 { return al::strcasecmp(mode, entry.name) == 0; };
866 auto iter = std::find_if(std::begin(hrtf_modes), std::end(hrtf_modes), match_entry);
867 if(iter == std::end(hrtf_modes))
868 ERR("Unexpected hrtf-mode: %s\n", mode);
869 else
871 device->mRenderMode = iter->mode;
872 ambi_order = iter->order;
875 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
876 (((ambi_order%100)/10) == 1) ? "th" :
877 ((ambi_order%10) == 1) ? "st" :
878 ((ambi_order%10) == 2) ? "nd" :
879 ((ambi_order%10) == 3) ? "rd" : "th",
880 (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
881 device->mHrtfName.c_str());
883 bool perHrirMin{false};
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 perHrirMin = true;
890 AmbiPoints = AmbiPoints3O;
891 AmbiMatrix = AmbiMatrix3O;
892 AmbiOrderHFGain = AmbiOrderHFGain3O;
894 else if(ambi_order == 2)
896 AmbiPoints = AmbiPoints2O;
897 AmbiMatrix = AmbiMatrix2O;
898 AmbiOrderHFGain = AmbiOrderHFGain2O;
900 device->mAmbiOrder = ambi_order;
901 device->m2DMixing = false;
903 const size_t count{AmbiChannelsFromOrder(ambi_order)};
904 std::transform(AmbiIndex::FromACN().begin(), AmbiIndex::FromACN().begin()+count,
905 std::begin(device->Dry.AmbiMap),
906 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
908 AllocChannels(device, count, device->channelsFromFmt());
910 HrtfStore *Hrtf{device->mHrtf.get()};
911 auto hrtfstate = DirectHrtfState::Create(count);
912 hrtfstate->build(Hrtf, device->mIrSize, perHrirMin, AmbiPoints, AmbiMatrix, device->mXOverFreq,
913 AmbiOrderHFGain);
914 device->mHrtfState = std::move(hrtfstate);
916 InitNearFieldCtrl(device, Hrtf->mFields[0].distance, ambi_order, true);
919 void InitUhjPanning(ALCdevice *device)
921 /* UHJ is always 2D first-order. */
922 constexpr size_t count{Ambi2DChannelsFromOrder(1)};
924 device->mAmbiOrder = 1;
925 device->m2DMixing = true;
927 auto acnmap_begin = AmbiIndex::FromFuMa2D().begin();
928 std::transform(acnmap_begin, acnmap_begin + count, std::begin(device->Dry.AmbiMap),
929 [](const uint8_t &acn) noexcept -> BFChannelConfig
930 { return BFChannelConfig{1.0f/AmbiScale::FromUHJ()[acn], acn}; });
931 AllocChannels(device, count, device->channelsFromFmt());
934 } // namespace
936 void aluInitRenderer(ALCdevice *device, int hrtf_id, al::optional<StereoEncoding> stereomode)
938 /* Hold the HRTF the device last used, in case it's used again. */
939 HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
941 device->mHrtfState = nullptr;
942 device->mHrtf = nullptr;
943 device->mIrSize = 0;
944 device->mHrtfName.clear();
945 device->mXOverFreq = 400.0f;
946 device->m2DMixing = false;
947 device->mRenderMode = RenderMode::Normal;
949 if(device->FmtChans != DevFmtStereo)
951 old_hrtf = nullptr;
952 if(stereomode && *stereomode == StereoEncoding::Hrtf)
953 device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
955 const char *layout{nullptr};
956 switch(device->FmtChans)
958 case DevFmtQuad: layout = "quad"; break;
959 case DevFmtX51: layout = "surround51"; break;
960 case DevFmtX61: layout = "surround61"; break;
961 case DevFmtX71: layout = "surround71"; break;
962 case DevFmtX714: layout = "surround714"; break;
963 case DevFmtX3D71: layout = "surround3d71"; break;
964 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
965 case DevFmtMono:
966 case DevFmtStereo:
967 case DevFmtAmbi3D:
968 break;
971 std::unique_ptr<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>> decoder_store;
972 DecoderView decoder{};
973 float speakerdists[MAX_OUTPUT_CHANNELS]{};
974 auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
976 AmbDecConf conf{};
977 if(auto err = conf.load(config))
979 ERR("Failed to load layout file %s\n", config);
980 ERR(" %s\n", err->c_str());
982 else if(conf.NumSpeakers > MAX_OUTPUT_CHANNELS)
983 ERR("Unsupported decoder speaker count %zu (max %d)\n", conf.NumSpeakers,
984 MAX_OUTPUT_CHANNELS);
985 else if(conf.ChanMask > Ambi3OrderMask)
986 ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
987 Ambi3OrderMask);
988 else
990 device->mXOverFreq = clampf(conf.XOverFreq, 100.0f, 1000.0f);
992 decoder_store = std::make_unique<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>>();
993 decoder = MakeDecoderView(device, &conf, *decoder_store);
994 for(size_t i{0};i < decoder.mChannels.size();++i)
995 speakerdists[i] = conf.Speakers[i].Distance;
998 if(layout)
1000 if(auto decopt = device->configValue<std::string>("decoder", layout))
1001 load_config(decopt->c_str());
1004 /* Enable the stablizer only for formats that have front-left, front-
1005 * right, and front-center outputs.
1007 const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != InvalidChannelIndex
1008 && device->RealOut.ChannelIndex[FrontLeft] != InvalidChannelIndex
1009 && device->RealOut.ChannelIndex[FrontRight] != InvalidChannelIndex
1010 && device->getConfigValueBool(nullptr, "front-stablizer", false) != 0};
1011 const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", true) != 0};
1012 InitPanning(device, hqdec, stablize, decoder);
1013 if(decoder)
1015 float accum_dist{0.0f}, spkr_count{0.0f};
1016 for(auto dist : speakerdists)
1018 if(dist > 0.0f)
1020 accum_dist += dist;
1021 spkr_count += 1.0f;
1025 const float avg_dist{(accum_dist > 0.0f && spkr_count > 0) ? accum_dist/spkr_count :
1026 device->configValue<float>("decoder", "speaker-dist").value_or(1.0f)};
1027 InitNearFieldCtrl(device, avg_dist, decoder.mOrder, decoder.mIs3D);
1029 if(spkr_count > 0)
1030 InitDistanceComp(device, decoder.mChannels, speakerdists);
1032 if(auto *ambidec{device->AmbiDecoder.get()})
1034 device->PostProcess = ambidec->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
1035 : &ALCdevice::ProcessAmbiDec;
1037 return;
1041 /* If HRTF is explicitly requested, or if there's no explicit request and
1042 * the device is headphones, try to enable it.
1044 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Hrtf
1045 || (!stereomode && device->Flags.test(DirectEar)))
1047 if(device->mHrtfList.empty())
1048 device->enumerateHrtfs();
1050 if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
1052 const std::string &hrtfname = device->mHrtfList[static_cast<uint>(hrtf_id)];
1053 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1055 device->mHrtf = std::move(hrtf);
1056 device->mHrtfName = hrtfname;
1060 if(!device->mHrtf)
1062 for(const auto &hrtfname : device->mHrtfList)
1064 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1066 device->mHrtf = std::move(hrtf);
1067 device->mHrtfName = hrtfname;
1068 break;
1073 if(device->mHrtf)
1075 old_hrtf = nullptr;
1077 HrtfStore *hrtf{device->mHrtf.get()};
1078 device->mIrSize = hrtf->mIrSize;
1079 if(auto hrtfsizeopt = device->configValue<uint>(nullptr, "hrtf-size"))
1081 if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
1082 device->mIrSize = maxu(*hrtfsizeopt, MinIrLength);
1085 InitHrtfPanning(device);
1086 device->PostProcess = &ALCdevice::ProcessHrtf;
1087 device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
1088 return;
1091 old_hrtf = nullptr;
1093 if(stereomode.value_or(StereoEncoding::Default) == StereoEncoding::Uhj)
1095 switch(UhjEncodeQuality)
1097 case UhjQualityType::IIR:
1098 device->mUhjEncoder = std::make_unique<UhjEncoderIIR>();
1099 break;
1100 case UhjQualityType::FIR256:
1101 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength256>>();
1102 break;
1103 case UhjQualityType::FIR512:
1104 device->mUhjEncoder = std::make_unique<UhjEncoder<UhjLength512>>();
1105 break;
1107 assert(device->mUhjEncoder != nullptr);
1109 TRACE("UHJ enabled\n");
1110 InitUhjPanning(device);
1111 device->PostProcess = &ALCdevice::ProcessUhj;
1112 return;
1115 device->mRenderMode = RenderMode::Pairwise;
1116 if(device->Type != DeviceType::Loopback)
1118 if(auto cflevopt = device->configValue<int>(nullptr, "cf_level"))
1120 if(*cflevopt > 0 && *cflevopt <= 6)
1122 device->Bs2b = std::make_unique<bs2b>();
1123 bs2b_set_params(device->Bs2b.get(), *cflevopt,
1124 static_cast<int>(device->Frequency));
1125 TRACE("BS2B enabled\n");
1126 InitPanning(device);
1127 device->PostProcess = &ALCdevice::ProcessBs2b;
1128 return;
1133 TRACE("Stereo rendering\n");
1134 InitPanning(device);
1135 device->PostProcess = &ALCdevice::ProcessAmbiDec;
1139 void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
1141 DeviceBase *device{context->mDevice};
1142 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
1144 slot->mWetBuffer.resize(count);
1146 auto acnmap_begin = AmbiIndex::FromACN().begin();
1147 auto iter = std::transform(acnmap_begin, acnmap_begin + count, slot->Wet.AmbiMap.begin(),
1148 [](const uint8_t &acn) noexcept -> BFChannelConfig
1149 { return BFChannelConfig{1.0f, acn}; });
1150 std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
1151 slot->Wet.Buffer = slot->mWetBuffer;