Add options to reverse local X and Y coordinates
[openal-soft.git] / alc / panning.cpp
blob3431a3f9f8fee71b2a675af6319ec03b3c5831d6
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 MaxChannels: break;
94 return "(unknown)";
98 std::unique_ptr<FrontStablizer> CreateStablizer(const size_t outchans, const uint srate)
100 auto stablizer = FrontStablizer::Create(outchans);
101 for(auto &buf : stablizer->DelayBuf)
102 std::fill(buf.begin(), buf.end(), 0.0f);
104 /* Initialize band-splitting filter for the mid signal, with a crossover at
105 * 5khz (could be higher).
107 stablizer->MidFilter.init(5000.0f / static_cast<float>(srate));
109 return stablizer;
112 void AllocChannels(ALCdevice *device, const size_t main_chans, const size_t real_chans)
114 TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans, real_chans);
116 /* Allocate extra channels for any post-filter output. */
117 const size_t num_chans{main_chans + real_chans};
119 TRACE("Allocating %zu channels, %zu bytes\n", num_chans,
120 num_chans*sizeof(device->MixBuffer[0]));
121 device->MixBuffer.resize(num_chans);
122 al::span<FloatBufferLine> buffer{device->MixBuffer};
124 device->Dry.Buffer = buffer.first(main_chans);
125 buffer = buffer.subspan(main_chans);
126 if(real_chans != 0)
128 device->RealOut.Buffer = buffer.first(real_chans);
129 buffer = buffer.subspan(real_chans);
131 else
132 device->RealOut.Buffer = device->Dry.Buffer;
136 using ChannelCoeffs = std::array<float,MaxAmbiChannels>;
137 enum DecoderMode : bool {
138 SingleBand = false,
139 DualBand = true
142 template<DecoderMode Mode, size_t N>
143 struct DecoderConfig;
145 template<size_t N>
146 struct DecoderConfig<SingleBand, N> {
147 uint8_t mOrder{};
148 bool mIs3D{};
149 std::array<Channel,N> mChannels{};
150 DevAmbiScaling mScaling{};
151 std::array<float,MaxAmbiOrder+1> mOrderGain{};
152 std::array<ChannelCoeffs,N> mCoeffs{};
155 template<size_t N>
156 struct DecoderConfig<DualBand, N> {
157 uint8_t mOrder{};
158 bool mIs3D{};
159 std::array<Channel,N> mChannels{};
160 DevAmbiScaling mScaling{};
161 std::array<float,MaxAmbiOrder+1> mOrderGain{};
162 std::array<ChannelCoeffs,N> mCoeffs{};
163 std::array<float,MaxAmbiOrder+1> mOrderGainLF{};
164 std::array<ChannelCoeffs,N> mCoeffsLF{};
167 template<>
168 struct DecoderConfig<DualBand, 0> {
169 uint8_t mOrder{};
170 bool mIs3D{};
171 al::span<const Channel> mChannels;
172 DevAmbiScaling mScaling{};
173 al::span<const float> mOrderGain;
174 al::span<const ChannelCoeffs> mCoeffs;
175 al::span<const float> mOrderGainLF;
176 al::span<const ChannelCoeffs> mCoeffsLF;
178 template<size_t N>
179 DecoderConfig& operator=(const DecoderConfig<SingleBand,N> &rhs) noexcept
181 mOrder = rhs.mOrder;
182 mIs3D = rhs.mIs3D;
183 mChannels = rhs.mChannels;
184 mScaling = rhs.mScaling;
185 mOrderGain = rhs.mOrderGain;
186 mCoeffs = rhs.mCoeffs;
187 mOrderGainLF = {};
188 mCoeffsLF = {};
189 return *this;
192 template<size_t N>
193 DecoderConfig& operator=(const DecoderConfig<DualBand,N> &rhs) noexcept
195 mOrder = rhs.mOrder;
196 mIs3D = rhs.mIs3D;
197 mChannels = rhs.mChannels;
198 mScaling = rhs.mScaling;
199 mOrderGain = rhs.mOrderGain;
200 mCoeffs = rhs.mCoeffs;
201 mOrderGainLF = rhs.mOrderGainLF;
202 mCoeffsLF = rhs.mCoeffsLF;
203 return *this;
206 using DecoderView = DecoderConfig<DualBand, 0>;
209 void InitNearFieldCtrl(ALCdevice *device, float ctrl_dist, uint order, bool is3d)
211 static const uint chans_per_order2d[MaxAmbiOrder+1]{ 1, 2, 2, 2 };
212 static const uint chans_per_order3d[MaxAmbiOrder+1]{ 1, 3, 5, 7 };
214 /* NFC is only used when AvgSpeakerDist is greater than 0. */
215 if(!device->getConfigValueBool("decoder", "nfc", 0) || !(ctrl_dist > 0.0f))
216 return;
218 device->AvgSpeakerDist = clampf(ctrl_dist, 0.1f, 10.0f);
219 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
221 const float w1{SpeedOfSoundMetersPerSec /
222 (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
223 device->mNFCtrlFilter.init(w1);
225 auto iter = std::copy_n(is3d ? chans_per_order3d : chans_per_order2d, order+1u,
226 std::begin(device->NumChannelsPerOrder));
227 std::fill(iter, std::end(device->NumChannelsPerOrder), 0u);
230 void InitDistanceComp(ALCdevice *device, const al::span<const Channel> channels,
231 const al::span<const float,MAX_OUTPUT_CHANNELS> dists)
233 const float maxdist{std::accumulate(std::begin(dists), std::end(dists), 0.0f, maxf)};
235 if(!device->getConfigValueBool("decoder", "distance-comp", 1) || !(maxdist > 0.0f))
236 return;
238 const auto distSampleScale = static_cast<float>(device->Frequency) / SpeedOfSoundMetersPerSec;
239 std::vector<DistanceComp::ChanData> ChanDelay;
240 ChanDelay.reserve(device->RealOut.Buffer.size());
241 size_t total{0u};
242 for(size_t chidx{0};chidx < channels.size();++chidx)
244 const Channel ch{channels[chidx]};
245 const uint idx{device->RealOut.ChannelIndex[ch]};
246 if(idx == INVALID_CHANNEL_INDEX)
247 continue;
249 const float distance{dists[chidx]};
251 /* Distance compensation only delays in steps of the sample rate. This
252 * is a bit less accurate since the delay time falls to the nearest
253 * sample time, but it's far simpler as it doesn't have to deal with
254 * phase offsets. This means at 48khz, for instance, the distance delay
255 * will be in steps of about 7 millimeters.
257 float delay{std::floor((maxdist - distance)*distSampleScale + 0.5f)};
258 if(delay > float{MAX_DELAY_LENGTH-1})
260 ERR("Delay for channel %u (%s) exceeds buffer length (%f > %d)\n", idx,
261 GetLabelFromChannel(ch), delay, MAX_DELAY_LENGTH-1);
262 delay = float{MAX_DELAY_LENGTH-1};
265 ChanDelay.resize(maxz(ChanDelay.size(), idx+1));
266 ChanDelay[idx].Length = static_cast<uint>(delay);
267 ChanDelay[idx].Gain = distance / maxdist;
268 TRACE("Channel %s distance comp: %u samples, %f gain\n", GetLabelFromChannel(ch),
269 ChanDelay[idx].Length, ChanDelay[idx].Gain);
271 /* Round up to the next 4th sample, so each channel buffer starts
272 * 16-byte aligned.
274 total += RoundUp(ChanDelay[idx].Length, 4);
277 if(total > 0)
279 auto chandelays = DistanceComp::Create(total);
281 ChanDelay[0].Buffer = chandelays->mSamples.data();
282 auto set_bufptr = [](const DistanceComp::ChanData &last, const DistanceComp::ChanData &cur)
283 -> DistanceComp::ChanData
285 DistanceComp::ChanData ret{cur};
286 ret.Buffer = last.Buffer + RoundUp(last.Length, 4);
287 return ret;
289 std::partial_sum(ChanDelay.begin(), ChanDelay.end(), chandelays->mChannels.begin(),
290 set_bufptr);
291 device->ChannelDelays = std::move(chandelays);
296 inline auto& GetAmbiScales(DevAmbiScaling scaletype) noexcept
298 if(scaletype == DevAmbiScaling::FuMa) return AmbiScale::FromFuMa();
299 if(scaletype == DevAmbiScaling::SN3D) return AmbiScale::FromSN3D();
300 return AmbiScale::FromN3D();
303 inline auto& GetAmbiLayout(DevAmbiLayout layouttype) noexcept
305 if(layouttype == DevAmbiLayout::FuMa) return AmbiIndex::FromFuMa();
306 return AmbiIndex::FromACN();
310 DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf,
311 DecoderConfig<DualBand, MAX_OUTPUT_CHANNELS> &decoder)
313 DecoderView ret{};
315 decoder.mOrder = (conf->ChanMask > Ambi2OrderMask) ? uint8_t{3} :
316 (conf->ChanMask > Ambi1OrderMask) ? uint8_t{2} : uint8_t{1};
317 decoder.mIs3D = (conf->ChanMask&AmbiPeriphonicMask) != 0;
319 switch(conf->CoeffScale)
321 case AmbDecScale::N3D: decoder.mScaling = DevAmbiScaling::N3D; break;
322 case AmbDecScale::SN3D: decoder.mScaling = DevAmbiScaling::SN3D; break;
323 case AmbDecScale::FuMa: decoder.mScaling = DevAmbiScaling::FuMa; break;
326 std::copy_n(std::begin(conf->HFOrderGain),
327 std::min(al::size(conf->HFOrderGain), al::size(decoder.mOrderGain)),
328 std::begin(decoder.mOrderGain));
329 std::copy_n(std::begin(conf->LFOrderGain),
330 std::min(al::size(conf->LFOrderGain), al::size(decoder.mOrderGainLF)),
331 std::begin(decoder.mOrderGainLF));
333 std::array<uint8_t,MaxAmbiChannels> idx_map{};
334 if(decoder.mIs3D)
336 uint flags{conf->ChanMask};
337 auto elem = idx_map.begin();
338 while(flags)
340 int acn{al::countr_zero(flags)};
341 flags &= ~(1u<<acn);
343 *elem = static_cast<uint8_t>(acn);
344 ++elem;
347 else
349 uint flags{conf->ChanMask};
350 auto elem = idx_map.begin();
351 while(flags)
353 int acn{al::countr_zero(flags)};
354 flags &= ~(1u<<acn);
356 switch(acn)
358 case 0: *elem = 0; break;
359 case 1: *elem = 1; break;
360 case 3: *elem = 2; break;
361 case 4: *elem = 3; break;
362 case 8: *elem = 4; break;
363 case 9: *elem = 5; break;
364 case 15: *elem = 6; break;
365 default: return ret;
367 ++elem;
370 const auto num_coeffs = static_cast<uint>(al::popcount(conf->ChanMask));
371 const auto hfmatrix = conf->HFMatrix;
372 const auto lfmatrix = conf->LFMatrix;
374 uint chan_count{0};
375 using const_speaker_span = al::span<const AmbDecConf::SpeakerConf>;
376 for(auto &speaker : const_speaker_span{conf->Speakers.get(), conf->NumSpeakers})
378 /* NOTE: AmbDec does not define any standard speaker names, however
379 * for this to work we have to by able to find the output channel
380 * the speaker definition corresponds to. Therefore, OpenAL Soft
381 * requires these channel labels to be recognized:
383 * LF = Front left
384 * RF = Front right
385 * LS = Side left
386 * RS = Side right
387 * LB = Back left
388 * RB = Back right
389 * CE = Front center
390 * CB = Back center
392 * Additionally, surround51 will acknowledge back speakers for side
393 * channels, to avoid issues with an ambdec expecting 5.1 to use the
394 * back channels.
396 Channel ch{};
397 if(speaker.Name == "LF")
398 ch = FrontLeft;
399 else if(speaker.Name == "RF")
400 ch = FrontRight;
401 else if(speaker.Name == "CE")
402 ch = FrontCenter;
403 else if(speaker.Name == "LS")
404 ch = SideLeft;
405 else if(speaker.Name == "RS")
406 ch = SideRight;
407 else if(speaker.Name == "LB")
408 ch = (device->FmtChans == DevFmtX51) ? SideLeft : BackLeft;
409 else if(speaker.Name == "RB")
410 ch = (device->FmtChans == DevFmtX51) ? SideRight : BackRight;
411 else if(speaker.Name == "CB")
412 ch = BackCenter;
413 else
415 ERR("AmbDec speaker label \"%s\" not recognized\n", speaker.Name.c_str());
416 continue;
419 decoder.mChannels[chan_count] = ch;
420 for(size_t src{0};src < num_coeffs;++src)
422 const size_t dst{idx_map[src]};
423 decoder.mCoeffs[chan_count][dst] = hfmatrix[chan_count][src];
425 if(conf->FreqBands > 1)
427 for(size_t src{0};src < num_coeffs;++src)
429 const size_t dst{idx_map[src]};
430 decoder.mCoeffsLF[chan_count][dst] = lfmatrix[chan_count][src];
433 ++chan_count;
436 if(chan_count > 0)
438 ret.mOrder = decoder.mOrder;
439 ret.mIs3D = decoder.mIs3D;
440 ret.mScaling = decoder.mScaling;
441 ret.mChannels = {decoder.mChannels.data(), chan_count};
442 ret.mOrderGain = decoder.mOrderGain;
443 ret.mCoeffs = {decoder.mCoeffs.data(), chan_count};
444 if(conf->FreqBands > 1)
446 ret.mOrderGainLF = decoder.mOrderGainLF;
447 ret.mCoeffsLF = {decoder.mCoeffsLF.data(), chan_count};
450 return ret;
453 constexpr DecoderConfig<SingleBand, 1> MonoConfig{
454 0, false, {{FrontCenter}},
455 DevAmbiScaling::N3D,
456 {{1.0f}},
457 {{ {{1.0f}} }}
459 constexpr DecoderConfig<SingleBand, 2> StereoConfig{
460 1, false, {{FrontLeft, FrontRight}},
461 DevAmbiScaling::N3D,
462 {{1.0f, 1.0f}},
464 {{5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f}},
465 {{5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f}},
468 constexpr DecoderConfig<DualBand, 4> QuadConfig{
469 2, false, {{BackLeft, FrontLeft, FrontRight, BackRight}},
470 DevAmbiScaling::N3D,
471 /*HF*/{{1.15470054e+0f, 1.00000000e+0f, 5.77350269e-1f}},
473 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
474 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
475 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
476 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
478 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
480 {{2.50000000e-1f, 2.04124145e-1f, -2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
481 {{2.50000000e-1f, 2.04124145e-1f, 2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
482 {{2.50000000e-1f, -2.04124145e-1f, 2.04124145e-1f, -1.29099445e-1f, 0.00000000e+0f}},
483 {{2.50000000e-1f, -2.04124145e-1f, -2.04124145e-1f, 1.29099445e-1f, 0.00000000e+0f}},
486 constexpr DecoderConfig<DualBand, 5> X51Config{
487 2, false, {{SideLeft, FrontLeft, FrontCenter, FrontRight, SideRight}},
488 DevAmbiScaling::FuMa,
489 /*HF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
491 {{5.67316000e-1f, 4.22920000e-1f, -3.15495000e-1f, -6.34490000e-2f, -2.92380000e-2f}},
492 {{3.68584000e-1f, 2.72349000e-1f, 3.21616000e-1f, 1.92645000e-1f, 4.82600000e-2f}},
493 {{1.83579000e-1f, 0.00000000e+0f, 1.99588000e-1f, 0.00000000e+0f, 9.62820000e-2f}},
494 {{3.68584000e-1f, -2.72349000e-1f, 3.21616000e-1f, -1.92645000e-1f, 4.82600000e-2f}},
495 {{5.67316000e-1f, -4.22920000e-1f, -3.15495000e-1f, 6.34490000e-2f, -2.92380000e-2f}},
497 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
499 {{4.90109850e-1f, 3.77305010e-1f, -3.73106990e-1f, -1.25914530e-1f, 1.45133000e-2f}},
500 {{1.49085730e-1f, 3.03561680e-1f, 1.53290060e-1f, 2.45112480e-1f, -1.50753130e-1f}},
501 {{1.37654920e-1f, 0.00000000e+0f, 4.49417940e-1f, 0.00000000e+0f, 2.57844070e-1f}},
502 {{1.49085730e-1f, -3.03561680e-1f, 1.53290060e-1f, -2.45112480e-1f, -1.50753130e-1f}},
503 {{4.90109850e-1f, -3.77305010e-1f, -3.73106990e-1f, 1.25914530e-1f, 1.45133000e-2f}},
506 constexpr DecoderConfig<SingleBand, 5> X61Config{
507 2, false, {{SideLeft, FrontLeft, FrontRight, SideRight, BackCenter}},
508 DevAmbiScaling::N3D,
509 {{1.0f, 1.0f, 1.0f}},
511 {{2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f}},
512 {{1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f}},
513 {{1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f}},
514 {{2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f}},
515 {{2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f}},
518 constexpr DecoderConfig<DualBand, 6> X71Config{
519 3, false, {{BackLeft, SideLeft, FrontLeft, FrontRight, SideRight, BackRight}},
520 DevAmbiScaling::N3D,
521 /*HF*/{{1.22474487e+0f, 1.13151672e+0f, 8.66025404e-1f, 4.68689571e-1f}},
523 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
524 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, -7.96819073e-2f, 0.00000000e+0f}},
525 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
526 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
527 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, 7.96819073e-2f, 0.00000000e+0f}},
528 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
530 /*LF*/{{1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f, 1.00000000e+0f}},
532 {{1.66666667e-1f, 9.62250449e-2f, -1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
533 {{1.66666667e-1f, 1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, -7.96819073e-2f, 0.00000000e+0f}},
534 {{1.66666667e-1f, 9.62250449e-2f, 1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, 7.96819073e-2f, 0.00000000e+0f}},
535 {{1.66666667e-1f, -9.62250449e-2f, 1.66666667e-1f, -1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
536 {{1.66666667e-1f, -1.92450090e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.72132593e-1f, 7.96819073e-2f, 0.00000000e+0f}},
537 {{1.66666667e-1f, -9.62250449e-2f, -1.66666667e-1f, 1.49071198e-1f, 8.60662966e-2f, -7.96819073e-2f, 0.00000000e+0f}},
541 void InitPanning(ALCdevice *device, const bool hqdec=false, const bool stablize=false,
542 DecoderView decoder={})
544 if(!decoder.mOrder)
546 switch(device->FmtChans)
548 case DevFmtMono: decoder = MonoConfig; break;
549 case DevFmtStereo: decoder = StereoConfig; break;
550 case DevFmtQuad: decoder = QuadConfig; break;
551 case DevFmtX51: decoder = X51Config; break;
552 case DevFmtX61: decoder = X61Config; break;
553 case DevFmtX71: decoder = X71Config; break;
554 case DevFmtAmbi3D:
555 auto&& acnmap = GetAmbiLayout(device->mAmbiLayout);
556 auto&& n3dscale = GetAmbiScales(device->mAmbiScale);
558 /* For DevFmtAmbi3D, the ambisonic order is already set. */
559 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
560 std::transform(acnmap.begin(), acnmap.begin()+count, std::begin(device->Dry.AmbiMap),
561 [&n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
562 { return BFChannelConfig{1.0f/n3dscale[acn], acn}; });
563 AllocChannels(device, count, 0);
565 float nfc_delay{device->configValue<float>("decoder", "nfc-ref-delay").value_or(0.0f)};
566 if(nfc_delay > 0.0f)
567 InitNearFieldCtrl(device, nfc_delay * SpeedOfSoundMetersPerSec, device->mAmbiOrder,
568 true);
569 return;
573 const bool dual_band{hqdec && !decoder.mCoeffsLF.empty()};
574 al::vector<ChannelDec> chancoeffs, chancoeffslf;
575 for(size_t i{0u};i < decoder.mChannels.size();++i)
577 const uint idx{GetChannelIdxByName(device->RealOut, decoder.mChannels[i])};
578 if(idx == INVALID_CHANNEL_INDEX)
580 ERR("Failed to find %s channel in device\n",
581 GetLabelFromChannel(decoder.mChannels[i]));
582 continue;
585 chancoeffs.resize(maxz(chancoeffs.size(), idx+1u), ChannelDec{});
586 al::span<float,MaxAmbiChannels> coeffs{chancoeffs[idx]};
587 size_t ambichan{0};
588 for(uint o{0};o < decoder.mOrder+1u;++o)
590 const float order_gain{decoder.mOrderGain[o]};
591 const size_t order_max{decoder.mIs3D ? AmbiChannelsFromOrder(o) :
592 Ambi2DChannelsFromOrder(o)};
593 for(;ambichan < order_max;++ambichan)
594 coeffs[ambichan] = decoder.mCoeffs[i][ambichan] * order_gain;
596 if(!dual_band)
597 continue;
599 chancoeffslf.resize(maxz(chancoeffslf.size(), idx+1u), ChannelDec{});
600 coeffs = chancoeffslf[idx];
601 ambichan = 0;
602 for(uint o{0};o < decoder.mOrder+1u;++o)
604 const float order_gain{decoder.mOrderGainLF[o]};
605 const size_t order_max{decoder.mIs3D ? AmbiChannelsFromOrder(o) :
606 Ambi2DChannelsFromOrder(o)};
607 for(;ambichan < order_max;++ambichan)
608 coeffs[ambichan] = decoder.mCoeffsLF[i][ambichan] * order_gain;
612 /* For non-DevFmtAmbi3D, set the ambisonic order. */
613 device->mAmbiOrder = decoder.mOrder;
615 const size_t ambicount{decoder.mIs3D ? AmbiChannelsFromOrder(decoder.mOrder) :
616 Ambi2DChannelsFromOrder(decoder.mOrder)};
617 const al::span<const uint8_t> acnmap{decoder.mIs3D ? AmbiIndex::FromACN().data() :
618 AmbiIndex::FromACN2D().data(), ambicount};
619 auto&& coeffscale = GetAmbiScales(decoder.mScaling);
620 std::transform(acnmap.begin(), acnmap.end(), std::begin(device->Dry.AmbiMap),
621 [&coeffscale](const uint8_t &acn) noexcept
622 { return BFChannelConfig{1.0f/coeffscale[acn], acn}; });
623 AllocChannels(device, ambicount, device->channelsFromFmt());
625 std::unique_ptr<FrontStablizer> stablizer;
626 if(stablize)
628 /* Only enable the stablizer if the decoder does not output to the
629 * front-center channel.
631 const auto cidx = device->RealOut.ChannelIndex[FrontCenter];
632 bool hasfc{false};
633 if(cidx < chancoeffs.size())
635 for(const auto &coeff : chancoeffs[cidx])
636 hasfc |= coeff != 0.0f;
638 if(!hasfc && cidx < chancoeffslf.size())
640 for(const auto &coeff : chancoeffslf[cidx])
641 hasfc |= coeff != 0.0f;
643 if(!hasfc)
645 stablizer = CreateStablizer(device->channelsFromFmt(), device->Frequency);
646 TRACE("Front stablizer enabled\n");
650 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
651 !dual_band ? "single" : "dual",
652 (decoder.mOrder > 2) ? "third" :
653 (decoder.mOrder > 1) ? "second" : "first",
654 decoder.mIs3D ? " periphonic" : "");
655 device->AmbiDecoder = BFormatDec::Create(ambicount, chancoeffs, chancoeffslf,
656 device->mXOverFreq/static_cast<float>(device->Frequency), std::move(stablizer));
659 void InitHrtfPanning(ALCdevice *device)
661 constexpr float Deg180{al::numbers::pi_v<float>};
662 constexpr float Deg_90{Deg180 / 2.0f /* 90 degrees*/};
663 constexpr float Deg_45{Deg_90 / 2.0f /* 45 degrees*/};
664 constexpr float Deg135{Deg_45 * 3.0f /*135 degrees*/};
665 constexpr float Deg_35{6.154797087e-01f /* 35~ 36 degrees*/};
666 constexpr float Deg_69{1.205932499e+00f /* 69~ 70 degrees*/};
667 constexpr float Deg111{1.935660155e+00f /*110~111 degrees*/};
668 constexpr float Deg_21{3.648638281e-01f /* 20~ 21 degrees*/};
669 static const AngularPoint AmbiPoints1O[]{
670 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
671 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
672 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
673 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
674 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
675 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
676 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
677 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
678 }, AmbiPoints2O[]{
679 { EvRadians{ 0.0f}, AzRadians{ 0.0f} },
680 { EvRadians{ 0.0f}, AzRadians{ Deg180} },
681 { EvRadians{ 0.0f}, AzRadians{-Deg_90} },
682 { EvRadians{ 0.0f}, AzRadians{ Deg_90} },
683 { EvRadians{ Deg_90}, AzRadians{ 0.0f} },
684 { EvRadians{-Deg_90}, AzRadians{ 0.0f} },
685 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
686 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
687 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
688 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
689 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
690 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
691 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
692 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
693 }, AmbiPoints3O[]{
694 { EvRadians{ Deg_69}, AzRadians{-Deg_90} },
695 { EvRadians{ Deg_69}, AzRadians{ Deg_90} },
696 { EvRadians{-Deg_69}, AzRadians{-Deg_90} },
697 { EvRadians{-Deg_69}, AzRadians{ Deg_90} },
698 { EvRadians{ 0.0f}, AzRadians{-Deg_69} },
699 { EvRadians{ 0.0f}, AzRadians{-Deg111} },
700 { EvRadians{ 0.0f}, AzRadians{ Deg_69} },
701 { EvRadians{ 0.0f}, AzRadians{ Deg111} },
702 { EvRadians{ Deg_21}, AzRadians{ 0.0f} },
703 { EvRadians{ Deg_21}, AzRadians{ Deg180} },
704 { EvRadians{-Deg_21}, AzRadians{ 0.0f} },
705 { EvRadians{-Deg_21}, AzRadians{ Deg180} },
706 { EvRadians{ Deg_35}, AzRadians{-Deg_45} },
707 { EvRadians{ Deg_35}, AzRadians{-Deg135} },
708 { EvRadians{ Deg_35}, AzRadians{ Deg_45} },
709 { EvRadians{ Deg_35}, AzRadians{ Deg135} },
710 { EvRadians{-Deg_35}, AzRadians{-Deg_45} },
711 { EvRadians{-Deg_35}, AzRadians{-Deg135} },
712 { EvRadians{-Deg_35}, AzRadians{ Deg_45} },
713 { EvRadians{-Deg_35}, AzRadians{ Deg135} },
715 static const float AmbiMatrix1O[][MaxAmbiChannels]{
716 { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
717 { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
718 { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
719 { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
720 { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
721 { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
722 { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
723 { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
724 }, AmbiMatrix2O[][MaxAmbiChannels]{
725 { 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, },
726 { 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, },
727 { 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, },
728 { 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, },
729 { 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, },
730 { 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, },
731 { 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, },
732 { 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, },
733 { 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, },
734 { 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, },
735 { 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, },
736 { 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, },
737 { 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, },
738 { 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, },
739 }, AmbiMatrix3O[][MaxAmbiChannels]{
740 { 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, },
741 { 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, },
742 { 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, },
743 { 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, },
744 { 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, },
745 { 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, },
746 { 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, },
747 { 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, },
748 { 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, },
749 { 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, },
750 { 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, },
751 { 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, },
752 { 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, },
753 { 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, },
754 { 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, },
755 { 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, },
756 { 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, },
757 { 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, },
758 { 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, },
759 { 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, },
761 static const float AmbiOrderHFGain1O[MaxAmbiOrder+1]{
762 /*ENRGY*/ 2.000000000e+00f, 1.154700538e+00f
763 }, AmbiOrderHFGain2O[MaxAmbiOrder+1]{
764 /*ENRGY 2.357022604e+00f, 1.825741858e+00f, 9.428090416e-01f*/
765 /*AMP 1.000000000e+00f, 7.745966692e-01f, 4.000000000e-01f*/
766 /*RMS*/ 9.128709292e-01f, 7.071067812e-01f, 3.651483717e-01f
767 }, AmbiOrderHFGain3O[MaxAmbiOrder+1]{
768 /*ENRGY 1.865086714e+00f, 1.606093894e+00f, 1.142055301e+00f, 5.683795528e-01f*/
769 /*AMP 1.000000000e+00f, 8.611363116e-01f, 6.123336207e-01f, 3.047469850e-01f*/
770 /*RMS*/ 8.340921354e-01f, 7.182670250e-01f, 5.107426573e-01f, 2.541870634e-01f
773 static_assert(al::size(AmbiPoints1O) == al::size(AmbiMatrix1O), "First-Order Ambisonic HRTF mismatch");
774 static_assert(al::size(AmbiPoints2O) == al::size(AmbiMatrix2O), "Second-Order Ambisonic HRTF mismatch");
775 static_assert(al::size(AmbiPoints3O) == al::size(AmbiMatrix3O), "Third-Order Ambisonic HRTF mismatch");
777 /* A 700hz crossover frequency provides tighter sound imaging at the sweet
778 * spot with ambisonic decoding, as the distance between the ears is closer
779 * to half this frequency wavelength, which is the optimal point where the
780 * response should change between optimizing phase vs volume. Normally this
781 * tighter imaging is at the cost of a smaller sweet spot, but since the
782 * listener is fixed in the center of the HRTF responses for the decoder,
783 * we don't have to worry about ever being out of the sweet spot.
785 * A better option here may be to have the head radius as part of the HRTF
786 * data set and calculate the optimal crossover frequency from that.
788 device->mXOverFreq = 700.0f;
790 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
791 * and it eases the CPU/memory load.
793 device->mRenderMode = RenderMode::Hrtf;
794 uint ambi_order{1};
795 if(auto modeopt = device->configValue<std::string>(nullptr, "hrtf-mode"))
797 struct HrtfModeEntry {
798 char name[8];
799 RenderMode mode;
800 uint order;
802 static const HrtfModeEntry hrtf_modes[]{
803 { "full", RenderMode::Hrtf, 1 },
804 { "ambi1", RenderMode::Normal, 1 },
805 { "ambi2", RenderMode::Normal, 2 },
806 { "ambi3", RenderMode::Normal, 3 },
809 const char *mode{modeopt->c_str()};
810 if(al::strcasecmp(mode, "basic") == 0)
812 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2");
813 mode = "ambi2";
816 auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
817 { return al::strcasecmp(mode, entry.name) == 0; };
818 auto iter = std::find_if(std::begin(hrtf_modes), std::end(hrtf_modes), match_entry);
819 if(iter == std::end(hrtf_modes))
820 ERR("Unexpected hrtf-mode: %s\n", mode);
821 else
823 device->mRenderMode = iter->mode;
824 ambi_order = iter->order;
827 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
828 (((ambi_order%100)/10) == 1) ? "th" :
829 ((ambi_order%10) == 1) ? "st" :
830 ((ambi_order%10) == 2) ? "nd" :
831 ((ambi_order%10) == 3) ? "rd" : "th",
832 (device->mRenderMode == RenderMode::Hrtf) ? "+ Full " : "",
833 device->mHrtfName.c_str());
835 al::span<const AngularPoint> AmbiPoints{AmbiPoints1O};
836 const float (*AmbiMatrix)[MaxAmbiChannels]{AmbiMatrix1O};
837 al::span<const float,MaxAmbiOrder+1> AmbiOrderHFGain{AmbiOrderHFGain1O};
838 if(ambi_order >= 3)
840 AmbiPoints = AmbiPoints3O;
841 AmbiMatrix = AmbiMatrix3O;
842 AmbiOrderHFGain = AmbiOrderHFGain3O;
844 else if(ambi_order == 2)
846 AmbiPoints = AmbiPoints2O;
847 AmbiMatrix = AmbiMatrix2O;
848 AmbiOrderHFGain = AmbiOrderHFGain2O;
850 device->mAmbiOrder = ambi_order;
852 const size_t count{AmbiChannelsFromOrder(ambi_order)};
853 std::transform(AmbiIndex::FromACN().begin(), AmbiIndex::FromACN().begin()+count,
854 std::begin(device->Dry.AmbiMap),
855 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
857 AllocChannels(device, count, device->channelsFromFmt());
859 HrtfStore *Hrtf{device->mHrtf.get()};
860 auto hrtfstate = DirectHrtfState::Create(count);
861 hrtfstate->build(Hrtf, device->mIrSize, AmbiPoints, AmbiMatrix, device->mXOverFreq,
862 AmbiOrderHFGain);
863 device->mHrtfState = std::move(hrtfstate);
865 InitNearFieldCtrl(device, Hrtf->field[0].distance, ambi_order, true);
868 void InitUhjPanning(ALCdevice *device)
870 /* UHJ is always 2D first-order. */
871 constexpr size_t count{Ambi2DChannelsFromOrder(1)};
873 device->mAmbiOrder = 1;
875 auto acnmap_begin = AmbiIndex::FromFuMa().begin();
876 std::transform(acnmap_begin, acnmap_begin + count, std::begin(device->Dry.AmbiMap),
877 [](const uint8_t &acn) noexcept -> BFChannelConfig
878 { return BFChannelConfig{1.0f/AmbiScale::FromUHJ()[acn], acn}; });
879 AllocChannels(device, count, device->channelsFromFmt());
882 } // namespace
884 void aluInitRenderer(ALCdevice *device, int hrtf_id, al::optional<StereoEncoding> stereomode)
886 /* Hold the HRTF the device last used, in case it's used again. */
887 HrtfStorePtr old_hrtf{std::move(device->mHrtf)};
889 device->mHrtfState = nullptr;
890 device->mHrtf = nullptr;
891 device->mIrSize = 0;
892 device->mHrtfName.clear();
893 device->mXOverFreq = 400.0f;
894 device->mRenderMode = RenderMode::Normal;
896 if(device->FmtChans != DevFmtStereo)
898 old_hrtf = nullptr;
899 if(stereomode && *stereomode == StereoEncoding::Hrtf)
900 device->mHrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
902 const char *layout{nullptr};
903 switch(device->FmtChans)
905 case DevFmtQuad: layout = "quad"; break;
906 case DevFmtX51: layout = "surround51"; break;
907 case DevFmtX61: layout = "surround61"; break;
908 case DevFmtX71: layout = "surround71"; break;
909 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
910 case DevFmtMono:
911 case DevFmtStereo:
912 case DevFmtAmbi3D:
913 break;
916 std::unique_ptr<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>> decoder_store;
917 DecoderView decoder{};
918 float speakerdists[MaxChannels]{};
919 auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config)
921 AmbDecConf conf{};
922 if(auto err = conf.load(config))
924 ERR("Failed to load layout file %s\n", config);
925 ERR(" %s\n", err->c_str());
927 else if(conf.NumSpeakers > MAX_OUTPUT_CHANNELS)
928 ERR("Unsupported decoder speaker count %zu (max %d)\n", conf.NumSpeakers,
929 MAX_OUTPUT_CHANNELS);
930 else if(conf.ChanMask > Ambi3OrderMask)
931 ERR("Unsupported decoder channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
932 Ambi3OrderMask);
933 else
935 device->mXOverFreq = clampf(conf.XOverFreq, 100.0f, 1000.0f);
937 decoder_store = std::make_unique<DecoderConfig<DualBand,MAX_OUTPUT_CHANNELS>>();
938 decoder = MakeDecoderView(device, &conf, *decoder_store);
939 for(size_t i{0};i < decoder.mChannels.size();++i)
940 speakerdists[i] = conf.Speakers[i].Distance;
943 if(layout)
945 if(auto decopt = device->configValue<std::string>("decoder", layout))
946 load_config(decopt->c_str());
949 /* Enable the stablizer only for formats that have front-left, front-
950 * right, and front-center outputs.
952 const bool stablize{device->RealOut.ChannelIndex[FrontCenter] != INVALID_CHANNEL_INDEX
953 && device->RealOut.ChannelIndex[FrontLeft] != INVALID_CHANNEL_INDEX
954 && device->RealOut.ChannelIndex[FrontRight] != INVALID_CHANNEL_INDEX
955 && device->getConfigValueBool(nullptr, "front-stablizer", 0) != 0};
956 const bool hqdec{device->getConfigValueBool("decoder", "hq-mode", 1) != 0};
957 InitPanning(device, hqdec, stablize, decoder);
958 if(decoder.mOrder > 0)
960 float accum_dist{0.0f}, spkr_count{0.0f};
961 for(auto dist : speakerdists)
963 if(dist > 0.0f)
965 accum_dist += dist;
966 spkr_count += 1.0f;
969 if(spkr_count > 0)
971 InitNearFieldCtrl(device, accum_dist / spkr_count, decoder.mOrder, decoder.mIs3D);
972 InitDistanceComp(device, decoder.mChannels, speakerdists);
975 if(auto *ambidec{device->AmbiDecoder.get()})
977 device->PostProcess = ambidec->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
978 : &ALCdevice::ProcessAmbiDec;
980 return;
984 /* If there's no request for HRTF or UHJ and the device is headphones, or
985 * if HRTF is explicitly requested, try to enable it.
987 if((!stereomode && device->Flags.test(DirectEar))
988 || (stereomode && *stereomode == StereoEncoding::Hrtf))
990 if(device->mHrtfList.empty())
991 device->enumerateHrtfs();
993 if(hrtf_id >= 0 && static_cast<uint>(hrtf_id) < device->mHrtfList.size())
995 const std::string &hrtfname = device->mHrtfList[static_cast<uint>(hrtf_id)];
996 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
998 device->mHrtf = std::move(hrtf);
999 device->mHrtfName = hrtfname;
1003 if(!device->mHrtf)
1005 for(const auto &hrtfname : device->mHrtfList)
1007 if(HrtfStorePtr hrtf{GetLoadedHrtf(hrtfname, device->Frequency)})
1009 device->mHrtf = std::move(hrtf);
1010 device->mHrtfName = hrtfname;
1011 break;
1016 if(device->mHrtf)
1018 old_hrtf = nullptr;
1020 HrtfStore *hrtf{device->mHrtf.get()};
1021 device->mIrSize = hrtf->irSize;
1022 if(auto hrtfsizeopt = device->configValue<uint>(nullptr, "hrtf-size"))
1024 if(*hrtfsizeopt > 0 && *hrtfsizeopt < device->mIrSize)
1025 device->mIrSize = maxu(*hrtfsizeopt, MinIrLength);
1028 InitHrtfPanning(device);
1029 device->PostProcess = &ALCdevice::ProcessHrtf;
1030 device->mHrtfStatus = ALC_HRTF_ENABLED_SOFT;
1031 return;
1034 old_hrtf = nullptr;
1036 if(stereomode && *stereomode == StereoEncoding::Uhj)
1038 device->mUhjEncoder = std::make_unique<UhjEncoder>();
1039 TRACE("UHJ enabled\n");
1040 InitUhjPanning(device);
1041 device->PostProcess = &ALCdevice::ProcessUhj;
1042 return;
1045 device->mRenderMode = RenderMode::Pairwise;
1046 if(device->Type != DeviceType::Loopback)
1048 if(auto cflevopt = device->configValue<int>(nullptr, "cf_level"))
1050 if(*cflevopt > 0 && *cflevopt <= 6)
1052 device->Bs2b = std::make_unique<bs2b>();
1053 bs2b_set_params(device->Bs2b.get(), *cflevopt,
1054 static_cast<int>(device->Frequency));
1055 TRACE("BS2B enabled\n");
1056 InitPanning(device);
1057 device->PostProcess = &ALCdevice::ProcessBs2b;
1058 return;
1063 TRACE("Stereo rendering\n");
1064 InitPanning(device);
1065 device->PostProcess = &ALCdevice::ProcessAmbiDec;
1069 void aluInitEffectPanning(EffectSlot *slot, ALCcontext *context)
1071 DeviceBase *device{context->mDevice};
1072 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
1074 auto wetbuffer_iter = context->mWetBuffers.end();
1075 if(slot->mWetBuffer)
1077 /* If the effect slot already has a wet buffer attached, allocate a new
1078 * one in its place.
1080 wetbuffer_iter = context->mWetBuffers.begin();
1081 for(;wetbuffer_iter != context->mWetBuffers.end();++wetbuffer_iter)
1083 if(wetbuffer_iter->get() == slot->mWetBuffer)
1085 slot->mWetBuffer = nullptr;
1086 slot->Wet.Buffer = {};
1088 *wetbuffer_iter = WetBufferPtr{new(FamCount(count)) WetBuffer{count}};
1090 break;
1094 if(wetbuffer_iter == context->mWetBuffers.end())
1096 /* Otherwise, search for an unused wet buffer. */
1097 wetbuffer_iter = context->mWetBuffers.begin();
1098 for(;wetbuffer_iter != context->mWetBuffers.end();++wetbuffer_iter)
1100 if(!(*wetbuffer_iter)->mInUse)
1101 break;
1103 if(wetbuffer_iter == context->mWetBuffers.end())
1105 /* Otherwise, allocate a new one to use. */
1106 context->mWetBuffers.emplace_back(WetBufferPtr{new(FamCount(count)) WetBuffer{count}});
1107 wetbuffer_iter = context->mWetBuffers.end()-1;
1110 WetBuffer *wetbuffer{slot->mWetBuffer = wetbuffer_iter->get()};
1111 wetbuffer->mInUse = true;
1113 auto acnmap_begin = AmbiIndex::FromACN().begin();
1114 auto iter = std::transform(acnmap_begin, acnmap_begin + count, slot->Wet.AmbiMap.begin(),
1115 [](const uint8_t &acn) noexcept -> BFChannelConfig
1116 { return BFChannelConfig{1.0f, acn}; });
1117 std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
1118 slot->Wet.Buffer = wetbuffer->mBuffer;