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
40 #include "al/auxeffectslot.h"
43 #include "alcontext.h"
45 #include "alnumeric.h"
46 #include "aloptional.h"
52 #include "bformatdec.h"
54 #include "devformat.h"
55 #include "front_stablizer.h"
58 #include "math_defs.h"
59 #include "opthelpers.h"
60 #include "uhjfilter.h"
63 constexpr std::array
<float,MAX_AMBI_CHANNELS
> AmbiScale::FromN3D
;
64 constexpr std::array
<float,MAX_AMBI_CHANNELS
> AmbiScale::FromSN3D
;
65 constexpr std::array
<float,MAX_AMBI_CHANNELS
> AmbiScale::FromFuMa
;
66 constexpr std::array
<uint8_t,MAX_AMBI_CHANNELS
> AmbiIndex::FromFuMa
;
67 constexpr std::array
<uint8_t,MAX_AMBI2D_CHANNELS
> AmbiIndex::FromFuMa2D
;
68 constexpr std::array
<uint8_t,MAX_AMBI_CHANNELS
> AmbiIndex::FromACN
;
69 constexpr std::array
<uint8_t,MAX_AMBI2D_CHANNELS
> AmbiIndex::From2D
;
70 constexpr std::array
<uint8_t,MAX_AMBI_CHANNELS
> AmbiIndex::OrderFromChannel
;
71 constexpr std::array
<uint8_t,MAX_AMBI2D_CHANNELS
> AmbiIndex::OrderFrom2DChannel
;
76 using namespace std::placeholders
;
77 using std::chrono::seconds
;
78 using std::chrono::nanoseconds
;
80 inline const char *GetLabelFromChannel(Channel channel
)
84 case FrontLeft
: return "front-left";
85 case FrontRight
: return "front-right";
86 case FrontCenter
: return "front-center";
87 case LFE
: return "lfe";
88 case BackLeft
: return "back-left";
89 case BackRight
: return "back-right";
90 case BackCenter
: return "back-center";
91 case SideLeft
: return "side-left";
92 case SideRight
: return "side-right";
94 case TopFrontLeft
: return "top-front-left";
95 case TopFrontCenter
: return "top-front-center";
96 case TopFrontRight
: return "top-front-right";
97 case TopCenter
: return "top-center";
98 case TopBackLeft
: return "top-back-left";
99 case TopBackCenter
: return "top-back-center";
100 case TopBackRight
: return "top-back-right";
102 case MaxChannels
: break;
108 std::unique_ptr
<FrontStablizer
> CreateStablizer(const size_t outchans
, const ALuint srate
)
110 auto stablizer
= FrontStablizer::Create(outchans
);
111 for(auto &buf
: stablizer
->DelayBuf
)
112 std::fill(buf
.begin(), buf
.end(), 0.0f
);
114 /* Initialize band-splitting filter for the mid signal, with a crossover at
115 * 5khz (could be higher).
117 stablizer
->MidFilter
.init(5000.0f
/ static_cast<float>(srate
));
122 void AllocChannels(ALCdevice
*device
, const size_t main_chans
, const size_t real_chans
)
124 TRACE("Channel config, Main: %zu, Real: %zu\n", main_chans
, real_chans
);
126 /* Allocate extra channels for any post-filter output. */
127 const size_t num_chans
{main_chans
+ real_chans
};
129 TRACE("Allocating %zu channels, %zu bytes\n", num_chans
,
130 num_chans
*sizeof(device
->MixBuffer
[0]));
131 device
->MixBuffer
.resize(num_chans
);
132 al::span
<FloatBufferLine
> buffer
{device
->MixBuffer
};
134 device
->Dry
.Buffer
= buffer
.first(main_chans
);
135 buffer
= buffer
.subspan(main_chans
);
138 device
->RealOut
.Buffer
= buffer
.first(real_chans
);
139 buffer
= buffer
.subspan(real_chans
);
142 device
->RealOut
.Buffer
= device
->Dry
.Buffer
;
148 float Config
[MAX_AMBI2D_CHANNELS
];
151 bool MakeSpeakerMap(ALCdevice
*device
, const AmbDecConf
*conf
, ALuint (&speakermap
)[MAX_OUTPUT_CHANNELS
])
153 auto map_spkr
= [device
](const AmbDecConf::SpeakerConf
&speaker
) -> ALuint
155 /* NOTE: AmbDec does not define any standard speaker names, however
156 * for this to work we have to by able to find the output channel
157 * the speaker definition corresponds to. Therefore, OpenAL Soft
158 * requires these channel labels to be recognized:
169 * Additionally, surround51 will acknowledge back speakers for side
170 * channels, and surround51rear will acknowledge side speakers for
171 * back channels, to avoid issues with an ambdec expecting 5.1 to
172 * use the side channels when the device is configured for back,
176 if(speaker
.Name
== "LF")
178 else if(speaker
.Name
== "RF")
180 else if(speaker
.Name
== "CE")
182 else if(speaker
.Name
== "LS")
184 if(device
->FmtChans
== DevFmtX51Rear
)
189 else if(speaker
.Name
== "RS")
191 if(device
->FmtChans
== DevFmtX51Rear
)
196 else if(speaker
.Name
== "LB")
198 if(device
->FmtChans
== DevFmtX51
)
203 else if(speaker
.Name
== "RB")
205 if(device
->FmtChans
== DevFmtX51
)
210 else if(speaker
.Name
== "CB")
214 ERR("AmbDec speaker label \"%s\" not recognized\n", speaker
.Name
.c_str());
215 return INVALID_CHANNEL_INDEX
;
217 const ALuint chidx
{GetChannelIdxByName(device
->RealOut
, ch
)};
218 if(chidx
== INVALID_CHANNEL_INDEX
)
219 ERR("Failed to lookup AmbDec speaker label %s\n", speaker
.Name
.c_str());
222 std::transform(conf
->Speakers
.begin(), conf
->Speakers
.end(), std::begin(speakermap
), map_spkr
);
223 /* Return success if no invalid entries are found. */
224 auto spkrmap_end
= std::begin(speakermap
) + conf
->Speakers
.size();
225 return std::find(std::begin(speakermap
), spkrmap_end
, INVALID_CHANNEL_INDEX
) == spkrmap_end
;
229 void InitNearFieldCtrl(ALCdevice
*device
, float ctrl_dist
, ALuint order
, bool is3d
)
231 static const ALuint chans_per_order2d
[MAX_AMBI_ORDER
+1]{ 1, 2, 2, 2 };
232 static const ALuint chans_per_order3d
[MAX_AMBI_ORDER
+1]{ 1, 3, 5, 7 };
234 /* NFC is only used when AvgSpeakerDist is greater than 0. */
235 const char *devname
{device
->DeviceName
.c_str()};
236 if(!GetConfigValueBool(devname
, "decoder", "nfc", 0) || !(ctrl_dist
> 0.0f
))
239 device
->AvgSpeakerDist
= clampf(ctrl_dist
, 0.1f
, 10.0f
);
240 TRACE("Using near-field reference distance: %.2f meters\n", device
->AvgSpeakerDist
);
242 auto iter
= std::copy_n(is3d
? chans_per_order3d
: chans_per_order2d
, order
+1u,
243 std::begin(device
->NumChannelsPerOrder
));
244 std::fill(iter
, std::end(device
->NumChannelsPerOrder
), 0u);
247 void InitDistanceComp(ALCdevice
*device
, const AmbDecConf
*conf
,
248 const ALuint (&speakermap
)[MAX_OUTPUT_CHANNELS
])
250 auto get_max
= std::bind(maxf
, _1
,
251 std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance
), _2
));
252 const float maxdist
{std::accumulate(conf
->Speakers
.begin(), conf
->Speakers
.end(), 0.0f
,
255 const char *devname
{device
->DeviceName
.c_str()};
256 if(!GetConfigValueBool(devname
, "decoder", "distance-comp", 1) || !(maxdist
> 0.0f
))
259 const auto distSampleScale
= static_cast<float>(device
->Frequency
) / SpeedOfSoundMetersPerSec
;
260 const auto ChanDelay
= device
->ChannelDelay
.as_span();
262 for(size_t i
{0u};i
< conf
->Speakers
.size();i
++)
264 const AmbDecConf::SpeakerConf
&speaker
= conf
->Speakers
[i
];
265 const ALuint chan
{speakermap
[i
]};
267 /* Distance compensation only delays in steps of the sample rate. This
268 * is a bit less accurate since the delay time falls to the nearest
269 * sample time, but it's far simpler as it doesn't have to deal with
270 * phase offsets. This means at 48khz, for instance, the distance delay
271 * will be in steps of about 7 millimeters.
273 float delay
{std::floor((maxdist
- speaker
.Distance
)*distSampleScale
+ 0.5f
)};
274 if(delay
> float{MAX_DELAY_LENGTH
-1})
276 ERR("Delay for speaker \"%s\" exceeds buffer length (%f > %d)\n",
277 speaker
.Name
.c_str(), delay
, MAX_DELAY_LENGTH
-1);
278 delay
= float{MAX_DELAY_LENGTH
-1};
281 ChanDelay
[chan
].Length
= static_cast<ALuint
>(delay
);
282 ChanDelay
[chan
].Gain
= speaker
.Distance
/ maxdist
;
283 TRACE("Channel %u \"%s\" distance compensation: %u samples, %f gain\n", chan
,
284 speaker
.Name
.c_str(), ChanDelay
[chan
].Length
, ChanDelay
[chan
].Gain
);
286 /* Round up to the next 4th sample, so each channel buffer starts
289 total
+= RoundUp(ChanDelay
[chan
].Length
, 4);
294 device
->ChannelDelay
.setSampleCount(total
);
295 ChanDelay
[0].Buffer
= device
->ChannelDelay
.getSamples();
296 auto set_bufptr
= [](const DistanceComp::DistData
&last
, const DistanceComp::DistData
&cur
) -> DistanceComp::DistData
298 DistanceComp::DistData ret
{cur
};
299 ret
.Buffer
= last
.Buffer
+ RoundUp(last
.Length
, 4);
302 std::partial_sum(ChanDelay
.begin(), ChanDelay
.end(), ChanDelay
.begin(), set_bufptr
);
307 auto GetAmbiScales(DevAmbiScaling scaletype
) noexcept
-> const std::array
<float,MAX_AMBI_CHANNELS
>&
309 if(scaletype
== DevAmbiScaling::FuMa
) return AmbiScale::FromFuMa
;
310 if(scaletype
== DevAmbiScaling::SN3D
) return AmbiScale::FromSN3D
;
311 return AmbiScale::FromN3D
;
314 auto GetAmbiLayout(DevAmbiLayout layouttype
) noexcept
-> const std::array
<uint8_t,MAX_AMBI_CHANNELS
>&
316 if(layouttype
== DevAmbiLayout::FuMa
) return AmbiIndex::FromFuMa
;
317 return AmbiIndex::FromACN
;
321 using ChannelCoeffs
= std::array
<float,MAX_AMBI2D_CHANNELS
>;
322 enum DecoderMode
: bool {
327 template<DecoderMode Mode
, size_t N
>
328 struct DecoderConfig
;
331 struct DecoderConfig
<SingleBand
, N
> {
333 std::array
<Channel
,N
> mChannels
;
334 std::array
<float,MAX_AMBI_ORDER
+1> mOrderGain
;
335 std::array
<ChannelCoeffs
,N
> mCoeffs
;
339 struct DecoderConfig
<DualBand
, N
> {
341 std::array
<Channel
,N
> mChannels
;
342 std::array
<float,MAX_AMBI_ORDER
+1> mOrderGain
;
343 std::array
<ChannelCoeffs
,N
> mCoeffs
;
344 std::array
<float,MAX_AMBI_ORDER
+1> mOrderGainLF
;
345 std::array
<ChannelCoeffs
,N
> mCoeffsLF
;
349 struct DecoderConfig
<DualBand
, 0> {
351 al::span
<const Channel
> mChannels
;
352 al::span
<const float> mOrderGain
;
353 al::span
<const ChannelCoeffs
> mCoeffs
;
354 al::span
<const float> mOrderGainLF
;
355 al::span
<const ChannelCoeffs
> mCoeffsLF
;
358 DecoderConfig
& operator=(const DecoderConfig
<SingleBand
,N
> &rhs
) noexcept
361 mChannels
= rhs
.mChannels
;
362 mOrderGain
= rhs
.mOrderGain
;
363 mCoeffs
= rhs
.mCoeffs
;
370 DecoderConfig
& operator=(const DecoderConfig
<DualBand
,N
> &rhs
) noexcept
373 mChannels
= rhs
.mChannels
;
374 mOrderGain
= rhs
.mOrderGain
;
375 mCoeffs
= rhs
.mCoeffs
;
376 mOrderGainLF
= rhs
.mOrderGainLF
;
377 mCoeffsLF
= rhs
.mCoeffsLF
;
381 using DecoderView
= DecoderConfig
<DualBand
, 0>;
383 constexpr DecoderConfig
<SingleBand
, 1> MonoConfig
{
388 constexpr DecoderConfig
<SingleBand
, 2> StereoConfig
{
389 1, {{FrontLeft
, FrontRight
}},
392 {{5.00000000e-1f
, 2.88675135e-1f
, 5.52305643e-2f
}},
393 {{5.00000000e-1f
, -2.88675135e-1f
, 5.52305643e-2f
}},
396 constexpr DecoderConfig
<DualBand
, 4> QuadConfig
{
397 2, {{BackLeft
, FrontLeft
, FrontRight
, BackRight
}},
398 /*HF*/{{1.15470054e+0f
, 1.00000000e+0f
, 5.77350269e-1f
}},
400 {{2.50000000e-1f
, 2.04124145e-1f
, -2.04124145e-1f
, -1.29099445e-1f
, 0.00000000e+0f
}},
401 {{2.50000000e-1f
, 2.04124145e-1f
, 2.04124145e-1f
, 1.29099445e-1f
, 0.00000000e+0f
}},
402 {{2.50000000e-1f
, -2.04124145e-1f
, 2.04124145e-1f
, -1.29099445e-1f
, 0.00000000e+0f
}},
403 {{2.50000000e-1f
, -2.04124145e-1f
, -2.04124145e-1f
, 1.29099445e-1f
, 0.00000000e+0f
}},
405 /*LF*/{{1.00000000e+0f
, 1.00000000e+0f
, 1.00000000e+0f
}},
407 {{2.50000000e-1f
, 2.04124145e-1f
, -2.04124145e-1f
, -1.29099445e-1f
, 0.00000000e+0f
}},
408 {{2.50000000e-1f
, 2.04124145e-1f
, 2.04124145e-1f
, 1.29099445e-1f
, 0.00000000e+0f
}},
409 {{2.50000000e-1f
, -2.04124145e-1f
, 2.04124145e-1f
, -1.29099445e-1f
, 0.00000000e+0f
}},
410 {{2.50000000e-1f
, -2.04124145e-1f
, -2.04124145e-1f
, 1.29099445e-1f
, 0.00000000e+0f
}},
413 constexpr DecoderConfig
<SingleBand
, 4> X51Config
{
414 2, {{SideLeft
, FrontLeft
, FrontRight
, SideRight
}},
415 {{1.0f
, 1.0f
, 1.0f
}},
417 {{3.33000782e-1f
, 1.89084803e-1f
, -2.00042375e-1f
, -2.12307769e-2f
, -1.14579885e-2f
}},
418 {{1.88542860e-1f
, 1.27709292e-1f
, 1.66295695e-1f
, 7.30571517e-2f
, 2.10901184e-2f
}},
419 {{1.88542860e-1f
, -1.27709292e-1f
, 1.66295695e-1f
, -7.30571517e-2f
, 2.10901184e-2f
}},
420 {{3.33000782e-1f
, -1.89084803e-1f
, -2.00042375e-1f
, 2.12307769e-2f
, -1.14579885e-2f
}},
423 constexpr DecoderConfig
<SingleBand
, 4> X51RearConfig
{
424 2, {{BackLeft
, FrontLeft
, FrontRight
, BackRight
}},
425 {{1.0f
, 1.0f
, 1.0f
}},
427 {{3.33000782e-1f
, 1.89084803e-1f
, -2.00042375e-1f
, -2.12307769e-2f
, -1.14579885e-2f
}},
428 {{1.88542860e-1f
, 1.27709292e-1f
, 1.66295695e-1f
, 7.30571517e-2f
, 2.10901184e-2f
}},
429 {{1.88542860e-1f
, -1.27709292e-1f
, 1.66295695e-1f
, -7.30571517e-2f
, 2.10901184e-2f
}},
430 {{3.33000782e-1f
, -1.89084803e-1f
, -2.00042375e-1f
, 2.12307769e-2f
, -1.14579885e-2f
}},
433 constexpr DecoderConfig
<SingleBand
, 5> X61Config
{
434 2, {{SideLeft
, FrontLeft
, FrontRight
, SideRight
, BackCenter
}},
435 {{1.0f
, 1.0f
, 1.0f
}},
437 {{2.04460341e-1f
, 2.17177926e-1f
, -4.39996780e-2f
, -2.60790269e-2f
, -6.87239792e-2f
}},
438 {{1.58923161e-1f
, 9.21772680e-2f
, 1.59658796e-1f
, 6.66278083e-2f
, 3.84686854e-2f
}},
439 {{1.58923161e-1f
, -9.21772680e-2f
, 1.59658796e-1f
, -6.66278083e-2f
, 3.84686854e-2f
}},
440 {{2.04460341e-1f
, -2.17177926e-1f
, -4.39996780e-2f
, 2.60790269e-2f
, -6.87239792e-2f
}},
441 {{2.50001688e-1f
, 0.00000000e+0f
, -2.50000094e-1f
, 0.00000000e+0f
, 6.05133395e-2f
}},
444 constexpr DecoderConfig
<DualBand
, 6> X71Config
{
445 3, {{BackLeft
, SideLeft
, FrontLeft
, FrontRight
, SideRight
, BackRight
}},
446 /*HF*/{{1.22474487e+0f
, 1.13151672e+0f
, 8.66025404e-1f
, 4.68689571e-1f
}},
448 {{1.66666667e-1f
, 9.62250449e-2f
, -1.66666667e-1f
, -1.49071198e-1f
, 8.60662966e-2f
, 7.96819073e-2f
, 0.00000000e+0f
}},
449 {{1.66666667e-1f
, 1.92450090e-1f
, 0.00000000e+0f
, 0.00000000e+0f
, -1.72132593e-1f
, -7.96819073e-2f
, 0.00000000e+0f
}},
450 {{1.66666667e-1f
, 9.62250449e-2f
, 1.66666667e-1f
, 1.49071198e-1f
, 8.60662966e-2f
, 7.96819073e-2f
, 0.00000000e+0f
}},
451 {{1.66666667e-1f
, -9.62250449e-2f
, 1.66666667e-1f
, -1.49071198e-1f
, 8.60662966e-2f
, -7.96819073e-2f
, 0.00000000e+0f
}},
452 {{1.66666667e-1f
, -1.92450090e-1f
, 0.00000000e+0f
, 0.00000000e+0f
, -1.72132593e-1f
, 7.96819073e-2f
, 0.00000000e+0f
}},
453 {{1.66666667e-1f
, -9.62250449e-2f
, -1.66666667e-1f
, 1.49071198e-1f
, 8.60662966e-2f
, -7.96819073e-2f
, 0.00000000e+0f
}},
455 /*LF*/{{1.00000000e+0f
, 1.00000000e+0f
, 1.00000000e+0f
, 1.00000000e+0f
}},
457 {{1.66666667e-1f
, 9.62250449e-2f
, -1.66666667e-1f
, -1.49071198e-1f
, 8.60662966e-2f
, 7.96819073e-2f
, 0.00000000e+0f
}},
458 {{1.66666667e-1f
, 1.92450090e-1f
, 0.00000000e+0f
, 0.00000000e+0f
, -1.72132593e-1f
, -7.96819073e-2f
, 0.00000000e+0f
}},
459 {{1.66666667e-1f
, 9.62250449e-2f
, 1.66666667e-1f
, 1.49071198e-1f
, 8.60662966e-2f
, 7.96819073e-2f
, 0.00000000e+0f
}},
460 {{1.66666667e-1f
, -9.62250449e-2f
, 1.66666667e-1f
, -1.49071198e-1f
, 8.60662966e-2f
, -7.96819073e-2f
, 0.00000000e+0f
}},
461 {{1.66666667e-1f
, -1.92450090e-1f
, 0.00000000e+0f
, 0.00000000e+0f
, -1.72132593e-1f
, 7.96819073e-2f
, 0.00000000e+0f
}},
462 {{1.66666667e-1f
, -9.62250449e-2f
, -1.66666667e-1f
, 1.49071198e-1f
, 8.60662966e-2f
, -7.96819073e-2f
, 0.00000000e+0f
}},
466 void InitPanning(ALCdevice
*device
, const bool hqdec
=false, const bool stablize
=false)
468 DecoderView decoder
{};
469 switch(device
->FmtChans
)
472 decoder
= MonoConfig
;
475 decoder
= StereoConfig
;
478 decoder
= QuadConfig
;
484 decoder
= X51RearConfig
;
496 if(device
->FmtChans
== DevFmtAmbi3D
)
498 const char *devname
{device
->DeviceName
.c_str()};
499 const std::array
<uint8_t,MAX_AMBI_CHANNELS
> &acnmap
= GetAmbiLayout(device
->mAmbiLayout
);
500 const std::array
<float,MAX_AMBI_CHANNELS
> &n3dscale
= GetAmbiScales(device
->mAmbiScale
);
502 /* For DevFmtAmbi3D, the ambisonic order is already set. */
503 const size_t count
{AmbiChannelsFromOrder(device
->mAmbiOrder
)};
504 std::transform(acnmap
.begin(), acnmap
.begin()+count
, std::begin(device
->Dry
.AmbiMap
),
505 [&n3dscale
](const uint8_t &acn
) noexcept
-> BFChannelConfig
506 { return BFChannelConfig
{1.0f
/n3dscale
[acn
], acn
}; }
508 AllocChannels(device
, count
, 0);
510 float nfc_delay
{ConfigValueFloat(devname
, "decoder", "nfc-ref-delay").value_or(0.0f
)};
512 InitNearFieldCtrl(device
, nfc_delay
* SpeedOfSoundMetersPerSec
, device
->mAmbiOrder
,
517 const bool dual_band
{hqdec
&& !decoder
.mCoeffsLF
.empty()};
518 al::vector
<ChannelDec
> chancoeffs
, chancoeffslf
;
519 for(size_t i
{0u};i
< decoder
.mChannels
.size();++i
)
521 const ALuint idx
{GetChannelIdxByName(device
->RealOut
, decoder
.mChannels
[i
])};
522 if(idx
== INVALID_CHANNEL_INDEX
)
524 ERR("Failed to find %s channel in device\n",
525 GetLabelFromChannel(decoder
.mChannels
[i
]));
529 chancoeffs
.resize(maxz(chancoeffs
.size(), idx
+1u), ChannelDec
{});
530 al::span
<float,MAX_AMBI_CHANNELS
> coeffs
{chancoeffs
[idx
]};
532 for(ALuint o
{0};o
<= decoder
.mOrder
;++o
)
534 size_t count
{o
? 2u : 1u};
536 coeffs
[start
] = decoder
.mCoeffs
[i
][start
] * decoder
.mOrderGain
[o
];
543 chancoeffslf
.resize(maxz(chancoeffslf
.size(), idx
+1u), ChannelDec
{});
544 coeffs
= chancoeffslf
[idx
];
546 for(ALuint o
{0};o
<= decoder
.mOrder
;++o
)
548 size_t count
{o
? 2u : 1u};
550 coeffs
[start
] = decoder
.mCoeffsLF
[i
][start
] * decoder
.mOrderGainLF
[o
];
556 /* For non-DevFmtAmbi3D, set the ambisonic order. */
557 device
->mAmbiOrder
= decoder
.mOrder
;
559 /* Built-in speaker decoders are always 2D. */
560 const size_t ambicount
{Ambi2DChannelsFromOrder(decoder
.mOrder
)};
561 std::transform(AmbiIndex::From2D
.begin(), AmbiIndex::From2D
.begin()+ambicount
,
562 std::begin(device
->Dry
.AmbiMap
),
563 [](const uint8_t &index
) noexcept
{ return BFChannelConfig
{1.0f
, index
}; }
565 AllocChannels(device
, ambicount
, device
->channelsFromFmt());
567 std::unique_ptr
<FrontStablizer
> stablizer
;
570 /* Only enable the stablizer if the decoder does not output to the
571 * front-center channel.
573 const auto cidx
= device
->RealOut
.ChannelIndex
[FrontCenter
];
575 if(cidx
< chancoeffs
.size())
577 for(const auto &coeff
: chancoeffs
[cidx
])
578 hasfc
|= coeff
!= 0.0f
;
580 if(!hasfc
&& cidx
< chancoeffslf
.size())
582 for(const auto &coeff
: chancoeffslf
[cidx
])
583 hasfc
|= coeff
!= 0.0f
;
587 stablizer
= CreateStablizer(device
->channelsFromFmt(), device
->Frequency
);
588 TRACE("Front stablizer enabled\n");
592 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
593 !dual_band
? "single" : "dual",
594 (decoder
.mOrder
> 2) ? "third" :
595 (decoder
.mOrder
> 1) ? "second" : "first",
597 device
->AmbiDecoder
= BFormatDec::Create(ambicount
, chancoeffs
, chancoeffslf
,
598 std::move(stablizer
));
602 void InitCustomPanning(ALCdevice
*device
, const bool hqdec
, const bool stablize
,
603 const AmbDecConf
*conf
, const ALuint (&speakermap
)[MAX_OUTPUT_CHANNELS
])
605 if(!hqdec
&& conf
->FreqBands
!= 1)
606 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
609 const ALuint order
{(conf
->ChanMask
> AMBI_2ORDER_MASK
) ? 3u :
610 (conf
->ChanMask
> AMBI_1ORDER_MASK
) ? 2u : 1u};
611 device
->mAmbiOrder
= order
;
614 if((conf
->ChanMask
&AMBI_PERIPHONIC_MASK
))
616 count
= AmbiChannelsFromOrder(order
);
617 std::transform(AmbiIndex::FromACN
.begin(), AmbiIndex::FromACN
.begin()+count
,
618 std::begin(device
->Dry
.AmbiMap
),
619 [](const uint8_t &index
) noexcept
{ return BFChannelConfig
{1.0f
, index
}; }
624 count
= Ambi2DChannelsFromOrder(order
);
625 std::transform(AmbiIndex::From2D
.begin(), AmbiIndex::From2D
.begin()+count
,
626 std::begin(device
->Dry
.AmbiMap
),
627 [](const uint8_t &index
) noexcept
{ return BFChannelConfig
{1.0f
, index
}; }
630 AllocChannels(device
, count
, device
->channelsFromFmt());
632 std::unique_ptr
<FrontStablizer
> stablizer
;
635 /* Only enable the stablizer if the decoder does not output to the
636 * front-center channel.
639 for(;cidx
< conf
->Speakers
.size();++cidx
)
641 if(speakermap
[cidx
] == FrontCenter
)
645 if(cidx
< conf
->LFMatrix
.size())
647 for(const auto &coeff
: conf
->LFMatrix
[cidx
])
648 hasfc
|= coeff
!= 0.0f
;
650 if(!hasfc
&& cidx
< conf
->HFMatrix
.size())
652 for(const auto &coeff
: conf
->HFMatrix
[cidx
])
653 hasfc
|= coeff
!= 0.0f
;
657 stablizer
= CreateStablizer(device
->channelsFromFmt(), device
->Frequency
);
658 TRACE("Front stablizer enabled\n");
662 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
663 (!hqdec
|| conf
->FreqBands
== 1) ? "single" : "dual",
664 (conf
->ChanMask
> AMBI_2ORDER_MASK
) ? "third" :
665 (conf
->ChanMask
> AMBI_1ORDER_MASK
) ? "second" : "first",
666 (conf
->ChanMask
&AMBI_PERIPHONIC_MASK
) ? " periphonic" : ""
668 device
->AmbiDecoder
= BFormatDec::Create(conf
, hqdec
, count
, device
->Frequency
, speakermap
,
669 std::move(stablizer
));
671 auto accum_spkr_dist
= std::bind(std::plus
<float>{}, _1
,
672 std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance
), _2
));
673 const float avg_dist
{
674 std::accumulate(conf
->Speakers
.begin(), conf
->Speakers
.end(), 0.0f
, accum_spkr_dist
) /
675 static_cast<float>(conf
->Speakers
.size())};
676 InitNearFieldCtrl(device
, avg_dist
, order
, !!(conf
->ChanMask
&AMBI_PERIPHONIC_MASK
));
678 InitDistanceComp(device
, conf
, speakermap
);
681 void InitHrtfPanning(ALCdevice
*device
)
683 constexpr float PI
{al::MathDefs
<float>::Pi()};
684 constexpr float PI_2
{PI
/ 2.0f
};
685 constexpr float PI_4
{PI_2
/ 2.0f
};
686 constexpr float PI3_4
{PI_4
* 3.0f
};
687 static const float CornerElev
{static_cast<float>(std::atan2(1.0, std::sqrt(2.0)))};
688 static const AngularPoint AmbiPoints1O
[]{
689 { EvRadians
{ CornerElev
}, AzRadians
{ -PI_4
} },
690 { EvRadians
{ CornerElev
}, AzRadians
{-PI3_4
} },
691 { EvRadians
{ CornerElev
}, AzRadians
{ PI_4
} },
692 { EvRadians
{ CornerElev
}, AzRadians
{ PI3_4
} },
693 { EvRadians
{-CornerElev
}, AzRadians
{ -PI_4
} },
694 { EvRadians
{-CornerElev
}, AzRadians
{-PI3_4
} },
695 { EvRadians
{-CornerElev
}, AzRadians
{ PI_4
} },
696 { EvRadians
{-CornerElev
}, AzRadians
{ PI3_4
} },
698 { EvRadians
{ -CornerElev
}, AzRadians
{ -PI_4
} },
699 { EvRadians
{ -CornerElev
}, AzRadians
{ -PI3_4
} },
700 { EvRadians
{ CornerElev
}, AzRadians
{ -PI3_4
} },
701 { EvRadians
{ CornerElev
}, AzRadians
{ PI3_4
} },
702 { EvRadians
{ CornerElev
}, AzRadians
{ PI_4
} },
703 { EvRadians
{ -CornerElev
}, AzRadians
{ PI_4
} },
704 { EvRadians
{ -CornerElev
}, AzRadians
{ PI3_4
} },
705 { EvRadians
{ CornerElev
}, AzRadians
{ -PI_4
} },
706 { EvRadians
{-1.205932499e+00f
}, AzRadians
{ -PI_2
} },
707 { EvRadians
{ 1.205932499e+00f
}, AzRadians
{ PI_2
} },
708 { EvRadians
{-1.205932499e+00f
}, AzRadians
{ PI_2
} },
709 { EvRadians
{ 1.205932499e+00f
}, AzRadians
{ -PI_2
} },
710 { EvRadians
{ 0.0f
}, AzRadians
{-1.205932499e+00f
} },
711 { EvRadians
{ 0.0f
}, AzRadians
{-1.935660155e+00f
} },
712 { EvRadians
{ 0.0f
}, AzRadians
{ 1.205932499e+00f
} },
713 { EvRadians
{ 0.0f
}, AzRadians
{ 1.935660155e+00f
} },
714 { EvRadians
{-3.648638281e-01f
}, AzRadians
{ PI
} },
715 { EvRadians
{ 3.648638281e-01f
}, AzRadians
{ PI
} },
716 { EvRadians
{ 3.648638281e-01f
}, AzRadians
{ 0.0f
} },
717 { EvRadians
{-3.648638281e-01f
}, AzRadians
{ 0.0f
} },
719 static const float AmbiMatrix1O
[][MAX_AMBI_CHANNELS
]{
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 { 1.250000000e-01f
, 1.250000000e-01f
, -1.250000000e-01f
, 1.250000000e-01f
},
725 { 1.250000000e-01f
, 1.250000000e-01f
, -1.250000000e-01f
, -1.250000000e-01f
},
726 { 1.250000000e-01f
, -1.250000000e-01f
, -1.250000000e-01f
, 1.250000000e-01f
},
727 { 1.250000000e-01f
, -1.250000000e-01f
, -1.250000000e-01f
, -1.250000000e-01f
},
728 }, AmbiMatrix2O
[][MAX_AMBI_CHANNELS
]{
729 { 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
},
730 { 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
},
731 { 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
},
732 { 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
},
733 { 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
},
734 { 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
},
735 { 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
},
736 { 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
},
737 { 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
},
738 { 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
},
739 { 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
},
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
},
741 { 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
},
742 { 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
},
743 { 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
},
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
},
745 { 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
},
746 { 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
},
747 { 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
},
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
},
750 static const float AmbiOrderHFGain1O
[MAX_AMBI_ORDER
+1]{
751 2.000000000e+00f
, 1.154700538e+00f
752 }, AmbiOrderHFGain2O
[MAX_AMBI_ORDER
+1]{
753 2.357022604e+00f
, 1.825741858e+00f
, 9.428090416e-01f
756 static_assert(al::size(AmbiPoints1O
) == al::size(AmbiMatrix1O
), "First-Order Ambisonic HRTF mismatch");
757 static_assert(al::size(AmbiPoints2O
) == al::size(AmbiMatrix2O
), "Second-Order Ambisonic HRTF mismatch");
759 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
760 * and it eases the CPU/memory load.
762 device
->mRenderMode
= RenderMode::Hrtf
;
763 ALuint ambi_order
{1};
764 if(auto modeopt
= ConfigValueStr(device
->DeviceName
.c_str(), nullptr, "hrtf-mode"))
766 struct HrtfModeEntry
{
771 static const HrtfModeEntry hrtf_modes
[]{
772 { "full", RenderMode::Hrtf
, 1 },
773 { "ambi1", RenderMode::Normal
, 1 },
774 { "ambi2", RenderMode::Normal
, 2 },
777 const char *mode
{modeopt
->c_str()};
778 if(al::strcasecmp(mode
, "basic") == 0 || al::strcasecmp(mode
, "ambi3") == 0)
780 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode
, "ambi2");
784 auto match_entry
= [mode
](const HrtfModeEntry
&entry
) -> bool
785 { return al::strcasecmp(mode
, entry
.name
) == 0; };
786 auto iter
= std::find_if(std::begin(hrtf_modes
), std::end(hrtf_modes
), match_entry
);
787 if(iter
== std::end(hrtf_modes
))
788 ERR("Unexpected hrtf-mode: %s\n", mode
);
791 device
->mRenderMode
= iter
->mode
;
792 ambi_order
= iter
->order
;
795 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order
,
796 (((ambi_order
%100)/10) == 1) ? "th" :
797 ((ambi_order
%10) == 1) ? "st" :
798 ((ambi_order
%10) == 2) ? "nd" :
799 ((ambi_order
%10) == 3) ? "rd" : "th",
800 (device
->mRenderMode
== RenderMode::Hrtf
) ? "+ Full " : "",
801 device
->HrtfName
.c_str());
803 al::span
<const AngularPoint
> AmbiPoints
{AmbiPoints1O
};
804 const float (*AmbiMatrix
)[MAX_AMBI_CHANNELS
]{AmbiMatrix1O
};
805 al::span
<const float,MAX_AMBI_ORDER
+1> AmbiOrderHFGain
{AmbiOrderHFGain1O
};
808 AmbiPoints
= AmbiPoints2O
;
809 AmbiMatrix
= AmbiMatrix2O
;
810 AmbiOrderHFGain
= AmbiOrderHFGain2O
;
812 device
->mAmbiOrder
= ambi_order
;
814 const size_t count
{AmbiChannelsFromOrder(ambi_order
)};
815 std::transform(AmbiIndex::FromACN
.begin(), AmbiIndex::FromACN
.begin()+count
,
816 std::begin(device
->Dry
.AmbiMap
),
817 [](const uint8_t &index
) noexcept
{ return BFChannelConfig
{1.0f
, index
}; }
819 AllocChannels(device
, count
, device
->channelsFromFmt());
821 HrtfStore
*Hrtf
{device
->mHrtf
.get()};
822 auto hrtfstate
= DirectHrtfState::Create(count
);
823 hrtfstate
->build(Hrtf
, AmbiPoints
, AmbiMatrix
, AmbiOrderHFGain
);
824 device
->mHrtfState
= std::move(hrtfstate
);
826 InitNearFieldCtrl(device
, Hrtf
->field
[0].distance
, ambi_order
, true);
829 void InitUhjPanning(ALCdevice
*device
)
831 /* UHJ is always 2D first-order. */
832 constexpr size_t count
{Ambi2DChannelsFromOrder(1)};
834 device
->mAmbiOrder
= 1;
836 auto acnmap_end
= AmbiIndex::FromFuMa
.begin() + count
;
837 std::transform(AmbiIndex::FromFuMa
.begin(), acnmap_end
, std::begin(device
->Dry
.AmbiMap
),
838 [](const uint8_t &acn
) noexcept
-> BFChannelConfig
839 { return BFChannelConfig
{1.0f
/AmbiScale::FromFuMa
[acn
], acn
}; }
841 AllocChannels(device
, count
, device
->channelsFromFmt());
846 void aluInitRenderer(ALCdevice
*device
, int hrtf_id
, HrtfRequestMode hrtf_appreq
,
847 HrtfRequestMode hrtf_userreq
)
849 /* Hold the HRTF the device last used, in case it's used again. */
850 HrtfStorePtr old_hrtf
{std::move(device
->mHrtf
)};
852 device
->mHrtfState
= nullptr;
853 device
->mHrtf
= nullptr;
854 device
->HrtfName
.clear();
855 device
->mRenderMode
= RenderMode::Normal
;
857 if(device
->FmtChans
!= DevFmtStereo
)
860 if(hrtf_appreq
== Hrtf_Enable
)
861 device
->HrtfStatus
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
863 const char *layout
{nullptr};
864 switch(device
->FmtChans
)
866 case DevFmtQuad
: layout
= "quad"; break;
867 case DevFmtX51
: /* fall-through */
868 case DevFmtX51Rear
: layout
= "surround51"; break;
869 case DevFmtX61
: layout
= "surround61"; break;
870 case DevFmtX71
: layout
= "surround71"; break;
871 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
878 const char *devname
{device
->DeviceName
.c_str()};
879 ALuint speakermap
[MAX_OUTPUT_CHANNELS
];
880 AmbDecConf
*pconf
{nullptr};
884 if(auto decopt
= ConfigValueStr(devname
, "decoder", layout
))
886 if(!conf
.load(decopt
->c_str()))
887 ERR("Failed to load layout file %s\n", decopt
->c_str());
888 else if(conf
.Speakers
.size() > MAX_OUTPUT_CHANNELS
)
889 ERR("Unsupported speaker count %zu (max %d)\n", conf
.Speakers
.size(),
890 MAX_OUTPUT_CHANNELS
);
891 else if(conf
.ChanMask
> AMBI_3ORDER_MASK
)
892 ERR("Unsupported channel mask 0x%04x (max 0x%x)\n", conf
.ChanMask
,
894 else if(MakeSpeakerMap(device
, &conf
, speakermap
))
899 /* Enable the stablizer only for formats that have front-left, front-
900 * right, and front-center outputs.
902 const bool stablize
{device
->RealOut
.ChannelIndex
[FrontCenter
] != INVALID_CHANNEL_INDEX
903 && device
->RealOut
.ChannelIndex
[FrontLeft
] != INVALID_CHANNEL_INDEX
904 && device
->RealOut
.ChannelIndex
[FrontRight
] != INVALID_CHANNEL_INDEX
905 && GetConfigValueBool(devname
, nullptr, "front-stablizer", 0) != 0};
906 const bool hqdec
{GetConfigValueBool(devname
, "decoder", "hq-mode", 1) != 0};
908 InitPanning(device
, hqdec
, stablize
);
910 InitCustomPanning(device
, hqdec
, stablize
, pconf
, speakermap
);
911 if(auto *ambidec
{device
->AmbiDecoder
.get()})
913 device
->PostProcess
= ambidec
->hasStablizer() ? &ALCdevice::ProcessAmbiDecStablized
914 : &ALCdevice::ProcessAmbiDec
;
919 bool headphones
{device
->IsHeadphones
};
920 if(device
->Type
!= DeviceType::Loopback
)
922 if(auto modeopt
= ConfigValueStr(device
->DeviceName
.c_str(), nullptr, "stereo-mode"))
924 const char *mode
{modeopt
->c_str()};
925 if(al::strcasecmp(mode
, "headphones") == 0)
927 else if(al::strcasecmp(mode
, "speakers") == 0)
929 else if(al::strcasecmp(mode
, "auto") != 0)
930 ERR("Unexpected stereo-mode: %s\n", mode
);
934 if(hrtf_userreq
== Hrtf_Default
)
936 bool usehrtf
= (headphones
&& hrtf_appreq
!= Hrtf_Disable
) ||
937 (hrtf_appreq
== Hrtf_Enable
);
938 if(!usehrtf
) goto no_hrtf
;
940 device
->HrtfStatus
= ALC_HRTF_ENABLED_SOFT
;
941 if(headphones
&& hrtf_appreq
!= Hrtf_Disable
)
942 device
->HrtfStatus
= ALC_HRTF_HEADPHONES_DETECTED_SOFT
;
946 if(hrtf_userreq
!= Hrtf_Enable
)
948 if(hrtf_appreq
== Hrtf_Enable
)
949 device
->HrtfStatus
= ALC_HRTF_DENIED_SOFT
;
952 device
->HrtfStatus
= ALC_HRTF_REQUIRED_SOFT
;
955 if(device
->HrtfList
.empty())
956 device
->HrtfList
= EnumerateHrtf(device
->DeviceName
.c_str());
958 if(hrtf_id
>= 0 && static_cast<ALuint
>(hrtf_id
) < device
->HrtfList
.size())
960 const char *devname
{device
->DeviceName
.c_str()};
961 const std::string
&hrtfname
= device
->HrtfList
[static_cast<ALuint
>(hrtf_id
)];
962 if(HrtfStorePtr hrtf
{GetLoadedHrtf(hrtfname
, devname
, device
->Frequency
)})
964 device
->mHrtf
= std::move(hrtf
);
965 device
->HrtfName
= hrtfname
;
971 const char *devname
{device
->DeviceName
.c_str()};
972 auto find_hrtf
= [device
,devname
](const std::string
&hrtfname
) -> bool
974 HrtfStorePtr hrtf
{GetLoadedHrtf(hrtfname
, devname
, device
->Frequency
)};
975 if(!hrtf
) return false;
976 device
->mHrtf
= std::move(hrtf
);
977 device
->HrtfName
= hrtfname
;
980 (void)std::find_if(device
->HrtfList
.cbegin(), device
->HrtfList
.cend(), find_hrtf
);
987 InitHrtfPanning(device
);
988 device
->PostProcess
= &ALCdevice::ProcessHrtf
;
991 device
->HrtfStatus
= ALC_HRTF_UNSUPPORTED_FORMAT_SOFT
;
996 device
->mRenderMode
= RenderMode::Pairwise
;
998 if(device
->Type
!= DeviceType::Loopback
)
1000 if(auto cflevopt
= ConfigValueInt(device
->DeviceName
.c_str(), nullptr, "cf_level"))
1002 if(*cflevopt
> 0 && *cflevopt
<= 6)
1004 device
->Bs2b
= std::make_unique
<bs2b
>();
1005 bs2b_set_params(device
->Bs2b
.get(), *cflevopt
,
1006 static_cast<int>(device
->Frequency
));
1007 TRACE("BS2B enabled\n");
1008 InitPanning(device
);
1009 device
->PostProcess
= &ALCdevice::ProcessBs2b
;
1015 if(auto encopt
= ConfigValueStr(device
->DeviceName
.c_str(), nullptr, "stereo-encoding"))
1017 const char *mode
{encopt
->c_str()};
1018 if(al::strcasecmp(mode
, "uhj") == 0)
1019 device
->mRenderMode
= RenderMode::Normal
;
1020 else if(al::strcasecmp(mode
, "panpot") != 0)
1021 ERR("Unexpected stereo-encoding: %s\n", mode
);
1023 if(device
->mRenderMode
== RenderMode::Normal
)
1025 device
->Uhj_Encoder
= std::make_unique
<Uhj2Encoder
>();
1026 TRACE("UHJ enabled\n");
1027 InitUhjPanning(device
);
1028 device
->PostProcess
= &ALCdevice::ProcessUhj
;
1032 TRACE("Stereo rendering\n");
1033 InitPanning(device
);
1034 device
->PostProcess
= &ALCdevice::ProcessAmbiDec
;
1038 void aluInitEffectPanning(ALeffectslot
*slot
, ALCcontext
*context
)
1040 ALCdevice
*device
{context
->mDevice
.get()};
1041 const size_t count
{AmbiChannelsFromOrder(device
->mAmbiOrder
)};
1043 auto wetbuffer_iter
= context
->mWetBuffers
.end();
1044 if(slot
->mWetBuffer
)
1046 /* If the effect slot already has a wet buffer attached, allocate a new
1049 wetbuffer_iter
= context
->mWetBuffers
.begin();
1050 for(;wetbuffer_iter
!= context
->mWetBuffers
.end();++wetbuffer_iter
)
1052 if(wetbuffer_iter
->get() == slot
->mWetBuffer
)
1054 slot
->mWetBuffer
= nullptr;
1055 slot
->Wet
.Buffer
= {};
1057 *wetbuffer_iter
= WetBufferPtr
{new(FamCount(count
)) WetBuffer
{count
}};
1063 if(wetbuffer_iter
== context
->mWetBuffers
.end())
1065 /* Otherwise, search for an unused wet buffer. */
1066 wetbuffer_iter
= context
->mWetBuffers
.begin();
1067 for(;wetbuffer_iter
!= context
->mWetBuffers
.end();++wetbuffer_iter
)
1069 if(!(*wetbuffer_iter
)->mInUse
)
1072 if(wetbuffer_iter
== context
->mWetBuffers
.end())
1074 /* Otherwise, allocate a new one to use. */
1075 context
->mWetBuffers
.emplace_back(WetBufferPtr
{new(FamCount(count
)) WetBuffer
{count
}});
1076 wetbuffer_iter
= context
->mWetBuffers
.end()-1;
1079 WetBuffer
*wetbuffer
{slot
->mWetBuffer
= wetbuffer_iter
->get()};
1080 wetbuffer
->mInUse
= true;
1082 auto acnmap_end
= AmbiIndex::FromACN
.begin() + count
;
1083 auto iter
= std::transform(AmbiIndex::FromACN
.begin(), acnmap_end
, slot
->Wet
.AmbiMap
.begin(),
1084 [](const uint8_t &acn
) noexcept
-> BFChannelConfig
1085 { return BFChannelConfig
{1.0f
, acn
}; });
1086 std::fill(iter
, slot
->Wet
.AmbiMap
.end(), BFChannelConfig
{});
1087 slot
->Wet
.Buffer
= wetbuffer
->mBuffer
;
1091 std::array
<float,MAX_AMBI_CHANNELS
> CalcAmbiCoeffs(const float y
, const float z
, const float x
,
1094 std::array
<float,MAX_AMBI_CHANNELS
> coeffs
;
1097 coeffs
[0] = 1.0f
; /* ACN 0 = 1 */
1099 coeffs
[1] = 1.732050808f
* y
; /* ACN 1 = sqrt(3) * Y */
1100 coeffs
[2] = 1.732050808f
* z
; /* ACN 2 = sqrt(3) * Z */
1101 coeffs
[3] = 1.732050808f
* x
; /* ACN 3 = sqrt(3) * X */
1103 const float xx
{x
*x
}, yy
{y
*y
}, zz
{z
*z
};
1104 coeffs
[4] = 3.872983346f
* x
* y
; /* ACN 4 = sqrt(15) * X * Y */
1105 coeffs
[5] = 3.872983346f
* y
* z
; /* ACN 5 = sqrt(15) * Y * Z */
1106 coeffs
[6] = 1.118033989f
* (3.0f
*zz
- 1.0f
); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
1107 coeffs
[7] = 3.872983346f
* x
* z
; /* ACN 7 = sqrt(15) * X * Z */
1108 coeffs
[8] = 1.936491673f
* (xx
- yy
); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
1110 coeffs
[9] = 2.091650066f
* y
* (3.0f
*xx
- yy
); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
1111 coeffs
[10] = 10.246950766f
* z
* x
* y
; /* ACN 10 = sqrt(105) * Z * X * Y */
1112 coeffs
[11] = 1.620185175f
* y
* (5.0f
*zz
- 1.0f
); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
1113 coeffs
[12] = 1.322875656f
* z
* (5.0f
*zz
- 3.0f
); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
1114 coeffs
[13] = 1.620185175f
* x
* (5.0f
*zz
- 1.0f
); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
1115 coeffs
[14] = 5.123475383f
* z
* (xx
- yy
); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
1116 coeffs
[15] = 2.091650066f
* x
* (xx
- 3.0f
*yy
); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
1118 /* ACN 16 = sqrt(35)*3/2 * X * Y * (X*X - Y*Y) */
1119 /* ACN 17 = sqrt(35/2)*3/2 * (3*X*X - Y*Y) * Y * Z */
1120 /* ACN 18 = sqrt(5)*3/2 * X * Y * (7*Z*Z - 1) */
1121 /* ACN 19 = sqrt(5/2)*3/2 * Y * Z * (7*Z*Z - 3) */
1122 /* ACN 20 = 3/8 * (35*Z*Z*Z*Z - 30*Z*Z + 3) */
1123 /* ACN 21 = sqrt(5/2)*3/2 * X * Z * (7*Z*Z - 3) */
1124 /* ACN 22 = sqrt(5)*3/4 * (X*X - Y*Y) * (7*Z*Z - 1) */
1125 /* ACN 23 = sqrt(35/2)*3/2 * (X*X - 3*Y*Y) * X * Z */
1126 /* ACN 24 = sqrt(35)*3/8 * (X*X*X*X - 6*X*X*Y*Y + Y*Y*Y*Y) */
1130 /* Implement the spread by using a spherical source that subtends the
1131 * angle spread. See:
1132 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
1134 * When adjusted for N3D normalization instead of SN3D, these
1137 * ZH0 = -sqrt(pi) * (-1+ca);
1138 * ZH1 = 0.5*sqrt(pi) * sa*sa;
1139 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
1140 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
1141 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
1142 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
1144 * The gain of the source is compensated for size, so that the
1145 * loudness doesn't depend on the spread. Thus:
1148 * ZH1 = 0.5f * (ca+1.0f);
1149 * ZH2 = 0.5f * (ca+1.0f)*ca;
1150 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
1151 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
1152 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
1154 const float ca
{std::cos(spread
* 0.5f
)};
1155 /* Increase the source volume by up to +3dB for a full spread. */
1156 const float scale
{std::sqrt(1.0f
+ spread
/al::MathDefs
<float>::Tau())};
1158 const float ZH0_norm
{scale
};
1159 const float ZH1_norm
{scale
* 0.5f
* (ca
+1.f
)};
1160 const float ZH2_norm
{scale
* 0.5f
* (ca
+1.f
)*ca
};
1161 const float ZH3_norm
{scale
* 0.125f
* (ca
+1.f
)*(5.f
*ca
*ca
-1.f
)};
1164 coeffs
[0] *= ZH0_norm
;
1166 coeffs
[1] *= ZH1_norm
;
1167 coeffs
[2] *= ZH1_norm
;
1168 coeffs
[3] *= ZH1_norm
;
1170 coeffs
[4] *= ZH2_norm
;
1171 coeffs
[5] *= ZH2_norm
;
1172 coeffs
[6] *= ZH2_norm
;
1173 coeffs
[7] *= ZH2_norm
;
1174 coeffs
[8] *= ZH2_norm
;
1176 coeffs
[9] *= ZH3_norm
;
1177 coeffs
[10] *= ZH3_norm
;
1178 coeffs
[11] *= ZH3_norm
;
1179 coeffs
[12] *= ZH3_norm
;
1180 coeffs
[13] *= ZH3_norm
;
1181 coeffs
[14] *= ZH3_norm
;
1182 coeffs
[15] *= ZH3_norm
;
1188 void ComputePanGains(const MixParams
*mix
, const float*RESTRICT coeffs
, const float ingain
,
1189 const al::span
<float,MAX_OUTPUT_CHANNELS
> gains
)
1191 auto ambimap
= mix
->AmbiMap
.cbegin();
1193 auto iter
= std::transform(ambimap
, ambimap
+mix
->Buffer
.size(), gains
.begin(),
1194 [coeffs
,ingain
](const BFChannelConfig
&chanmap
) noexcept
-> float
1195 { return chanmap
.Scale
* coeffs
[chanmap
.Index
] * ingain
; }
1197 std::fill(iter
, gains
.end(), 0.0f
);