Limit HRTF ambisonic decoding to second-order
[openal-soft.git] / alc / panning.cpp
blob1288197d38cf824bb14bcd9ebcf4ad11a29b2381
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 "alcmain.h"
42 #include "alconfig.h"
43 #include "almalloc.h"
44 #include "alnumeric.h"
45 #include "aloptional.h"
46 #include "alspan.h"
47 #include "alstring.h"
48 #include "alu.h"
49 #include "ambdec.h"
50 #include "ambidefs.h"
51 #include "bformatdec.h"
52 #include "bs2b.h"
53 #include "devformat.h"
54 #include "hrtf.h"
55 #include "logging.h"
56 #include "math_defs.h"
57 #include "opthelpers.h"
58 #include "uhjfilter.h"
61 constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromN3D;
62 constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromSN3D;
63 constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromFuMa;
64 constexpr std::array<uint8_t,MAX_AMBI_CHANNELS> AmbiIndex::FromFuMa;
65 constexpr std::array<uint8_t,MAX_AMBI_CHANNELS> AmbiIndex::FromACN;
66 constexpr std::array<uint8_t,MAX_AMBI2D_CHANNELS> AmbiIndex::From2D;
67 constexpr std::array<uint8_t,MAX_AMBI_CHANNELS> AmbiIndex::From3D;
70 namespace {
72 using namespace std::placeholders;
73 using std::chrono::seconds;
74 using std::chrono::nanoseconds;
76 inline const char *GetLabelFromChannel(Channel channel)
78 switch(channel)
80 case FrontLeft: return "front-left";
81 case FrontRight: return "front-right";
82 case FrontCenter: return "front-center";
83 case LFE: return "lfe";
84 case BackLeft: return "back-left";
85 case BackRight: return "back-right";
86 case BackCenter: return "back-center";
87 case SideLeft: return "side-left";
88 case SideRight: return "side-right";
90 case UpperFrontLeft: return "upper-front-left";
91 case UpperFrontRight: return "upper-front-right";
92 case UpperBackLeft: return "upper-back-left";
93 case UpperBackRight: return "upper-back-right";
94 case LowerFrontLeft: return "lower-front-left";
95 case LowerFrontRight: return "lower-front-right";
96 case LowerBackLeft: return "lower-back-left";
97 case LowerBackRight: return "lower-back-right";
99 case Aux0: return "aux-0";
100 case Aux1: return "aux-1";
101 case Aux2: return "aux-2";
102 case Aux3: return "aux-3";
103 case Aux4: return "aux-4";
104 case Aux5: return "aux-5";
105 case Aux6: return "aux-6";
106 case Aux7: return "aux-7";
107 case Aux8: return "aux-8";
108 case Aux9: return "aux-9";
109 case Aux10: return "aux-10";
110 case Aux11: return "aux-11";
111 case Aux12: return "aux-12";
112 case Aux13: return "aux-13";
113 case Aux14: return "aux-14";
114 case Aux15: return "aux-15";
116 case MaxChannels: break;
118 return "(unknown)";
122 void AllocChannels(ALCdevice *device, const ALuint main_chans, const ALuint real_chans)
124 TRACE("Channel config, Main: %u, Real: %u\n", main_chans, real_chans);
126 /* Allocate extra channels for any post-filter output. */
127 const ALuint num_chans{main_chans + real_chans};
129 TRACE("Allocating %u 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.data(), device->MixBuffer.size()};
134 device->Dry.Buffer = buffer.first(main_chans);
135 buffer = buffer.subspan(main_chans);
136 if(real_chans != 0)
138 device->RealOut.Buffer = buffer.first(real_chans);
139 buffer = buffer.subspan(real_chans);
141 else
142 device->RealOut.Buffer = device->Dry.Buffer;
146 struct ChannelMap {
147 Channel ChanName;
148 ALfloat 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:
160 * LF = Front left
161 * RF = Front right
162 * LS = Side left
163 * RS = Side right
164 * LB = Back left
165 * RB = Back right
166 * CE = Front center
167 * CB = Back center
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,
173 * and vice-versa.
175 Channel ch{};
176 if(speaker.Name == "LF")
177 ch = FrontLeft;
178 else if(speaker.Name == "RF")
179 ch = FrontRight;
180 else if(speaker.Name == "CE")
181 ch = FrontCenter;
182 else if(speaker.Name == "LS")
184 if(device->FmtChans == DevFmtX51Rear)
185 ch = BackLeft;
186 else
187 ch = SideLeft;
189 else if(speaker.Name == "RS")
191 if(device->FmtChans == DevFmtX51Rear)
192 ch = BackRight;
193 else
194 ch = SideRight;
196 else if(speaker.Name == "LB")
198 if(device->FmtChans == DevFmtX51)
199 ch = SideLeft;
200 else
201 ch = BackLeft;
203 else if(speaker.Name == "RB")
205 if(device->FmtChans == DevFmtX51)
206 ch = SideRight;
207 else
208 ch = BackRight;
210 else if(speaker.Name == "CB")
211 ch = BackCenter;
212 else
214 const char *name{speaker.Name.c_str()};
215 unsigned int n;
216 char c;
218 if(sscanf(name, "AUX%u%c", &n, &c) == 1 && n < 16)
219 ch = static_cast<Channel>(Aux0+n);
220 else
222 ERR("AmbDec speaker label \"%s\" not recognized\n", name);
223 return INVALID_CHANNEL_INDEX;
226 const ALuint chidx{GetChannelIdxByName(device->RealOut, ch)};
227 if(chidx == INVALID_CHANNEL_INDEX)
228 ERR("Failed to lookup AmbDec speaker label %s\n", speaker.Name.c_str());
229 return chidx;
231 std::transform(conf->Speakers.begin(), conf->Speakers.end(), std::begin(speakermap), map_spkr);
232 /* Return success if no invalid entries are found. */
233 auto spkrmap_end = std::begin(speakermap) + conf->Speakers.size();
234 return std::find(std::begin(speakermap), spkrmap_end, INVALID_CHANNEL_INDEX) == spkrmap_end;
238 constexpr ChannelMap MonoCfg[1] = {
239 { FrontCenter, { 1.0f } },
240 }, StereoCfg[2] = {
241 { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f } },
242 { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f } },
243 }, QuadCfg[4] = {
244 { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, -2.04124145e-1f } },
245 { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 2.04124145e-1f } },
246 { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 2.04124145e-1f } },
247 { BackRight, { 3.53553391e-1f, -2.04124145e-1f, -2.04124145e-1f } },
248 }, X51SideCfg[4] = {
249 { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, -2.00042375e-1f, -2.12307769e-2f, -1.14579885e-2f } },
250 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 1.66295695e-1f, 7.30571517e-2f, 2.10901184e-2f } },
251 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 1.66295695e-1f, -7.30571517e-2f, 2.10901184e-2f } },
252 { SideRight, { 3.33000782e-1f, -1.89084803e-1f, -2.00042375e-1f, 2.12307769e-2f, -1.14579885e-2f } },
253 }, X51RearCfg[4] = {
254 { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, -2.00042375e-1f, -2.12307769e-2f, -1.14579885e-2f } },
255 { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 1.66295695e-1f, 7.30571517e-2f, 2.10901184e-2f } },
256 { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 1.66295695e-1f, -7.30571517e-2f, 2.10901184e-2f } },
257 { BackRight, { 3.33000782e-1f, -1.89084803e-1f, -2.00042375e-1f, 2.12307769e-2f, -1.14579885e-2f } },
258 }, X61Cfg[6] = {
259 { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f } },
260 { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f } },
261 { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f } },
262 { SideRight, { 2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f } },
263 { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f } },
264 }, X71Cfg[6] = {
265 { BackLeft, { 2.04124145e-1f, 1.08880247e-1f, -1.88586120e-1f, -1.29099444e-1f, 7.45355993e-2f, 3.73460789e-2f, 0.00000000e+0f } },
266 { SideLeft, { 2.04124145e-1f, 2.17760495e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.49071198e-1f, -3.73460789e-2f, 0.00000000e+0f } },
267 { FrontLeft, { 2.04124145e-1f, 1.08880247e-1f, 1.88586120e-1f, 1.29099444e-1f, 7.45355993e-2f, 3.73460789e-2f, 0.00000000e+0f } },
268 { FrontRight, { 2.04124145e-1f, -1.08880247e-1f, 1.88586120e-1f, -1.29099444e-1f, 7.45355993e-2f, -3.73460789e-2f, 0.00000000e+0f } },
269 { SideRight, { 2.04124145e-1f, -2.17760495e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.49071198e-1f, 3.73460789e-2f, 0.00000000e+0f } },
270 { BackRight, { 2.04124145e-1f, -1.08880247e-1f, -1.88586120e-1f, 1.29099444e-1f, 7.45355993e-2f, -3.73460789e-2f, 0.00000000e+0f } },
273 void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALuint order,
274 const al::span<const ALuint,MAX_AMBI_ORDER+1> chans_per_order)
276 /* NFC is only used when AvgSpeakerDist is greater than 0. */
277 const char *devname{device->DeviceName.c_str()};
278 if(!GetConfigValueBool(devname, "decoder", "nfc", 0) || !(ctrl_dist > 0.0f))
279 return;
281 device->AvgSpeakerDist = clampf(ctrl_dist, 0.1f, 10.0f);
282 TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
284 auto iter = std::copy(chans_per_order.begin(), chans_per_order.begin()+order+1,
285 std::begin(device->NumChannelsPerOrder));
286 std::fill(iter, std::end(device->NumChannelsPerOrder), 0u);
289 void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf,
290 const ALuint (&speakermap)[MAX_OUTPUT_CHANNELS])
292 auto get_max = std::bind(maxf, _1,
293 std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance), _2));
294 const ALfloat maxdist{
295 std::accumulate(conf->Speakers.begin(), conf->Speakers.end(), float{0.0f}, get_max)};
297 const char *devname{device->DeviceName.c_str()};
298 if(!GetConfigValueBool(devname, "decoder", "distance-comp", 1) || !(maxdist > 0.0f))
299 return;
301 const auto distSampleScale = static_cast<ALfloat>(device->Frequency)/SPEEDOFSOUNDMETRESPERSEC;
302 const auto ChanDelay = device->ChannelDelay.as_span();
303 size_t total{0u};
304 for(size_t i{0u};i < conf->Speakers.size();i++)
306 const AmbDecConf::SpeakerConf &speaker = conf->Speakers[i];
307 const ALuint chan{speakermap[i]};
309 /* Distance compensation only delays in steps of the sample rate. This
310 * is a bit less accurate since the delay time falls to the nearest
311 * sample time, but it's far simpler as it doesn't have to deal with
312 * phase offsets. This means at 48khz, for instance, the distance delay
313 * will be in steps of about 7 millimeters.
315 ALfloat delay{std::floor((maxdist - speaker.Distance)*distSampleScale + 0.5f)};
316 if(delay > ALfloat{MAX_DELAY_LENGTH-1})
318 ERR("Delay for speaker \"%s\" exceeds buffer length (%f > %d)\n",
319 speaker.Name.c_str(), delay, MAX_DELAY_LENGTH-1);
320 delay = ALfloat{MAX_DELAY_LENGTH-1};
323 ChanDelay[chan].Length = static_cast<ALuint>(delay);
324 ChanDelay[chan].Gain = speaker.Distance / maxdist;
325 TRACE("Channel %u \"%s\" distance compensation: %u samples, %f gain\n", chan,
326 speaker.Name.c_str(), ChanDelay[chan].Length, ChanDelay[chan].Gain);
328 /* Round up to the next 4th sample, so each channel buffer starts
329 * 16-byte aligned.
331 total += RoundUp(ChanDelay[chan].Length, 4);
334 if(total > 0)
336 device->ChannelDelay.setSampleCount(total);
337 ChanDelay[0].Buffer = device->ChannelDelay.getSamples();
338 auto set_bufptr = [](const DistanceComp::DistData &last, const DistanceComp::DistData &cur) -> DistanceComp::DistData
340 DistanceComp::DistData ret{cur};
341 ret.Buffer = last.Buffer + RoundUp(last.Length, 4);
342 return ret;
344 std::partial_sum(ChanDelay.begin(), ChanDelay.end(), ChanDelay.begin(), set_bufptr);
349 auto GetAmbiScales(AmbiNorm scaletype) noexcept -> const std::array<float,MAX_AMBI_CHANNELS>&
351 if(scaletype == AmbiNorm::FuMa) return AmbiScale::FromFuMa;
352 if(scaletype == AmbiNorm::SN3D) return AmbiScale::FromSN3D;
353 return AmbiScale::FromN3D;
356 auto GetAmbiLayout(AmbiLayout layouttype) noexcept -> const std::array<uint8_t,MAX_AMBI_CHANNELS>&
358 if(layouttype == AmbiLayout::FuMa) return AmbiIndex::FromFuMa;
359 return AmbiIndex::FromACN;
363 void InitPanning(ALCdevice *device)
365 al::span<const ChannelMap> chanmap;
366 ALuint coeffcount{};
368 switch(device->FmtChans)
370 case DevFmtMono:
371 chanmap = MonoCfg;
372 coeffcount = 1;
373 break;
375 case DevFmtStereo:
376 chanmap = StereoCfg;
377 coeffcount = 3;
378 break;
380 case DevFmtQuad:
381 chanmap = QuadCfg;
382 coeffcount = 3;
383 break;
385 case DevFmtX51:
386 chanmap = X51SideCfg;
387 coeffcount = 5;
388 break;
390 case DevFmtX51Rear:
391 chanmap = X51RearCfg;
392 coeffcount = 5;
393 break;
395 case DevFmtX61:
396 chanmap = X61Cfg;
397 coeffcount = 5;
398 break;
400 case DevFmtX71:
401 chanmap = X71Cfg;
402 coeffcount = 7;
403 break;
405 case DevFmtAmbi3D:
406 break;
409 if(device->FmtChans == DevFmtAmbi3D)
411 const char *devname{device->DeviceName.c_str()};
412 const std::array<uint8_t,MAX_AMBI_CHANNELS> &acnmap = GetAmbiLayout(device->mAmbiLayout);
413 const std::array<float,MAX_AMBI_CHANNELS> &n3dscale = GetAmbiScales(device->mAmbiScale);
415 /* For DevFmtAmbi3D, the ambisonic order is already set. */
416 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
417 std::transform(acnmap.begin(), acnmap.begin()+count, std::begin(device->Dry.AmbiMap),
418 [&n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
419 { return BFChannelConfig{1.0f/n3dscale[acn], acn}; }
421 AllocChannels(device, static_cast<ALuint>(count), 0);
423 ALfloat nfc_delay{ConfigValueFloat(devname, "decoder", "nfc-ref-delay").value_or(0.0f)};
424 if(nfc_delay > 0.0f)
426 static const ALuint chans_per_order[MAX_AMBI_ORDER+1]{ 1, 3, 5, 7 };
427 InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC, device->mAmbiOrder,
428 chans_per_order);
431 else
433 ChannelDec chancoeffs[MAX_OUTPUT_CHANNELS]{};
434 ALuint idxmap[MAX_OUTPUT_CHANNELS]{};
435 for(size_t i{0u};i < chanmap.size();++i)
437 const ALuint idx{GetChannelIdxByName(device->RealOut, chanmap[i].ChanName)};
438 if(idx == INVALID_CHANNEL_INDEX)
440 ERR("Failed to find %s channel in device\n",
441 GetLabelFromChannel(chanmap[i].ChanName));
442 continue;
444 idxmap[i] = idx;
445 std::copy_n(chanmap[i].Config, coeffcount, chancoeffs[i]);
448 /* For non-DevFmtAmbi3D, set the ambisonic order given the mixing
449 * channel count. Built-in speaker decoders are always 2D, so just
450 * reverse that calculation.
452 device->mAmbiOrder = (coeffcount-1) / 2;
454 std::transform(AmbiIndex::From2D.begin(), AmbiIndex::From2D.begin()+coeffcount,
455 std::begin(device->Dry.AmbiMap),
456 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
458 AllocChannels(device, coeffcount, device->channelsFromFmt());
460 TRACE("Enabling %s-order%s ambisonic decoder\n",
461 (coeffcount > 5) ? "third" :
462 (coeffcount > 3) ? "second" : "first",
465 device->AmbiDecoder = al::make_unique<BFormatDec>(coeffcount,
466 static_cast<ALsizei>(chanmap.size()), chancoeffs, idxmap);
470 void InitCustomPanning(ALCdevice *device, bool hqdec, const AmbDecConf *conf,
471 const ALuint (&speakermap)[MAX_OUTPUT_CHANNELS])
473 static const ALuint chans_per_order2d[MAX_AMBI_ORDER+1] = { 1, 2, 2, 2 };
474 static const ALuint chans_per_order3d[MAX_AMBI_ORDER+1] = { 1, 3, 5, 7 };
476 if(!hqdec && conf->FreqBands != 1)
477 ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
478 conf->XOverFreq);
480 const ALuint order{(conf->ChanMask > AMBI_2ORDER_MASK) ? 3u :
481 (conf->ChanMask > AMBI_1ORDER_MASK) ? 2u : 1u};
482 device->mAmbiOrder = order;
484 ALuint count;
485 if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
487 count = static_cast<ALuint>(AmbiChannelsFromOrder(order));
488 std::transform(AmbiIndex::From3D.begin(), AmbiIndex::From3D.begin()+count,
489 std::begin(device->Dry.AmbiMap),
490 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
493 else
495 count = static_cast<ALuint>(Ambi2DChannelsFromOrder(order));
496 std::transform(AmbiIndex::From2D.begin(), AmbiIndex::From2D.begin()+count,
497 std::begin(device->Dry.AmbiMap),
498 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
501 AllocChannels(device, count, device->channelsFromFmt());
503 TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
504 (!hqdec || conf->FreqBands == 1) ? "single" : "dual",
505 (conf->ChanMask > AMBI_2ORDER_MASK) ? "third" :
506 (conf->ChanMask > AMBI_1ORDER_MASK) ? "second" : "first",
507 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
509 device->AmbiDecoder = al::make_unique<BFormatDec>(conf, hqdec, count, device->Frequency,
510 speakermap);
512 auto accum_spkr_dist = std::bind(std::plus<float>{}, _1,
513 std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance), _2));
514 const ALfloat avg_dist{
515 std::accumulate(conf->Speakers.begin(), conf->Speakers.end(), 0.0f, accum_spkr_dist) /
516 static_cast<ALfloat>(conf->Speakers.size())};
517 InitNearFieldCtrl(device, avg_dist, order,
518 (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? chans_per_order3d : chans_per_order2d);
520 InitDistanceComp(device, conf, speakermap);
523 void InitHrtfPanning(ALCdevice *device)
525 static const AngularPoint AmbiPoints[]{
526 { Deg2Rad( 0.000000f), Deg2Rad( 0.000000f) },
527 { Deg2Rad( 0.000000f), Deg2Rad( 180.000000f) },
528 { Deg2Rad( 0.000000f), Deg2Rad( -90.000000f) },
529 { Deg2Rad( 0.000000f), Deg2Rad( 90.000000f) },
530 { Deg2Rad( 90.000000f), Deg2Rad( 0.000000f) },
531 { Deg2Rad(-90.000000f), Deg2Rad( 0.000000f) },
532 { Deg2Rad( 45.000000f), Deg2Rad( -90.000000f) },
533 { Deg2Rad(-45.000000f), Deg2Rad( -90.000000f) },
534 { Deg2Rad( 45.000000f), Deg2Rad( 90.000000f) },
535 { Deg2Rad(-45.000000f), Deg2Rad( 90.000000f) },
536 { Deg2Rad( 45.000000f), Deg2Rad( 0.000000f) },
537 { Deg2Rad(-45.000000f), Deg2Rad( 0.000000f) },
538 { Deg2Rad( 45.000000f), Deg2Rad( 180.000000f) },
539 { Deg2Rad(-45.000000f), Deg2Rad( 180.000000f) },
540 { Deg2Rad( 0.000000f), Deg2Rad( -45.000000f) },
541 { Deg2Rad( 0.000000f), Deg2Rad( 45.000000f) },
542 { Deg2Rad( 0.000000f), Deg2Rad(-135.000000f) },
543 { Deg2Rad( 0.000000f), Deg2Rad( 135.000000f) },
544 { Deg2Rad( 35.264390f), Deg2Rad( -45.000000f) },
545 { Deg2Rad(-35.264390f), Deg2Rad( -45.000000f) },
546 { Deg2Rad( 35.264390f), Deg2Rad( 45.000000f) },
547 { Deg2Rad(-35.264390f), Deg2Rad( 45.000000f) },
548 { Deg2Rad( 35.264390f), Deg2Rad(-135.000000f) },
549 { Deg2Rad(-35.264390f), Deg2Rad(-135.000000f) },
550 { Deg2Rad( 35.264390f), Deg2Rad( 135.000000f) },
551 { Deg2Rad(-35.264390f), Deg2Rad( 135.000000f) },
553 static const float AmbiMatrix[][MAX_AMBI_CHANNELS]{
554 { 3.84615387e-02f, 0.00000000e+00f, 0.00000000e+00f, 6.66173389e-02f, 0.00000000e+00f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, 8.60662966e-02f },
555 { 3.84615387e-02f, 0.00000000e+00f, 0.00000000e+00f, -6.66173389e-02f, 0.00000000e+00f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, 8.60662966e-02f },
556 { 3.84615387e-02f, 6.66173389e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, -8.60662966e-02f },
557 { 3.84615387e-02f, -6.66173389e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, -8.60662966e-02f },
558 { 3.84615379e-02f, 0.00000000e+00f, 6.66173384e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 9.93807988e-02f, 0.00000000e+00f, 0.00000000e+00f },
559 { 3.84615379e-02f, 0.00000000e+00f, -6.66173384e-02f, 0.00000000e+00f, 0.00000000e+00f, 0.00000000e+00f, 9.93807988e-02f, 0.00000000e+00f, 0.00000000e+00f },
560 { 3.84615383e-02f, 4.71055721e-02f, 4.71055717e-02f, 0.00000000e+00f, 0.00000000e+00f, 6.83467647e-02f, 2.48451995e-02f, 0.00000000e+00f, -4.30331483e-02f },
561 { 3.84615383e-02f, 4.71055721e-02f, -4.71055717e-02f, 0.00000000e+00f, 0.00000000e+00f, -6.83467647e-02f, 2.48451995e-02f, 0.00000000e+00f, -4.30331483e-02f },
562 { 3.84615383e-02f, -4.71055721e-02f, 4.71055717e-02f, 0.00000000e+00f, 0.00000000e+00f, -6.83467647e-02f, 2.48451995e-02f, 0.00000000e+00f, -4.30331483e-02f },
563 { 3.84615383e-02f, -4.71055721e-02f, -4.71055717e-02f, 0.00000000e+00f, 0.00000000e+00f, 6.83467647e-02f, 2.48451995e-02f, 0.00000000e+00f, -4.30331483e-02f },
564 { 3.84615383e-02f, 0.00000000e+00f, 4.71055717e-02f, 4.71055721e-02f, 0.00000000e+00f, 0.00000000e+00f, 2.48451995e-02f, 6.83467647e-02f, 4.30331483e-02f },
565 { 3.84615383e-02f, 0.00000000e+00f, -4.71055717e-02f, 4.71055721e-02f, 0.00000000e+00f, 0.00000000e+00f, 2.48451995e-02f, -6.83467647e-02f, 4.30331483e-02f },
566 { 3.84615383e-02f, 0.00000000e+00f, 4.71055717e-02f, -4.71055721e-02f, 0.00000000e+00f, 0.00000000e+00f, 2.48451995e-02f, -6.83467647e-02f, 4.30331483e-02f },
567 { 3.84615383e-02f, 0.00000000e+00f, -4.71055717e-02f, -4.71055721e-02f, 0.00000000e+00f, 0.00000000e+00f, 2.48451995e-02f, 6.83467647e-02f, 4.30331483e-02f },
568 { 3.84615387e-02f, 4.71055721e-02f, 0.00000000e+00f, 4.71055721e-02f, 6.83467654e-02f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, 0.00000000e+00f },
569 { 3.84615387e-02f, -4.71055721e-02f, 0.00000000e+00f, 4.71055721e-02f, -6.83467654e-02f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, 0.00000000e+00f },
570 { 3.84615387e-02f, 4.71055721e-02f, 0.00000000e+00f, -4.71055721e-02f, -6.83467654e-02f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, 0.00000000e+00f },
571 { 3.84615387e-02f, -4.71055721e-02f, 0.00000000e+00f, -4.71055721e-02f, 6.83467654e-02f, 0.00000000e+00f, -4.96903997e-02f, 0.00000000e+00f, 0.00000000e+00f },
572 { 3.84615385e-02f, 3.84615384e-02f, 3.84615386e-02f, 3.84615384e-02f, 4.55645099e-02f, 4.55645100e-02f, 0.00000000e+00f, 4.55645100e-02f, 0.00000000e+00f },
573 { 3.84615385e-02f, 3.84615384e-02f, -3.84615386e-02f, 3.84615384e-02f, 4.55645099e-02f, -4.55645100e-02f, 0.00000000e+00f, -4.55645100e-02f, 0.00000000e+00f },
574 { 3.84615385e-02f, -3.84615384e-02f, 3.84615386e-02f, 3.84615384e-02f, -4.55645099e-02f, -4.55645100e-02f, 0.00000000e+00f, 4.55645100e-02f, 0.00000000e+00f },
575 { 3.84615385e-02f, -3.84615384e-02f, -3.84615386e-02f, 3.84615384e-02f, -4.55645099e-02f, 4.55645100e-02f, 0.00000000e+00f, -4.55645100e-02f, 0.00000000e+00f },
576 { 3.84615385e-02f, 3.84615384e-02f, 3.84615386e-02f, -3.84615384e-02f, -4.55645099e-02f, 4.55645100e-02f, 0.00000000e+00f, -4.55645100e-02f, 0.00000000e+00f },
577 { 3.84615385e-02f, 3.84615384e-02f, -3.84615386e-02f, -3.84615384e-02f, -4.55645099e-02f, -4.55645100e-02f, 0.00000000e+00f, 4.55645100e-02f, 0.00000000e+00f },
578 { 3.84615385e-02f, -3.84615384e-02f, 3.84615386e-02f, -3.84615384e-02f, 4.55645099e-02f, -4.55645100e-02f, 0.00000000e+00f, -4.55645100e-02f, 0.00000000e+00f },
579 { 3.84615385e-02f, -3.84615384e-02f, -3.84615386e-02f, -3.84615384e-02f, 4.55645099e-02f, 4.55645100e-02f, 0.00000000e+00f, 4.55645100e-02f, 0.00000000e+00f },
581 static const float AmbiOrderHFGain1O[MAX_AMBI_ORDER+1]{
582 3.60555128e+00f, 2.08166600e+00f
583 }, AmbiOrderHFGain2O[MAX_AMBI_ORDER+1]{
584 2.68741925e+00f, 2.08166600e+00f, 1.07496770e+00f
586 static const ALuint ChansPerOrder[MAX_AMBI_ORDER+1]{ 1, 3, 5, 7 };
587 const float *AmbiOrderHFGain{AmbiOrderHFGain1O};
589 static_assert(al::size(AmbiPoints) == al::size(AmbiMatrix), "Ambisonic HRTF mismatch");
591 /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
592 * and it eases the CPU/memory load.
594 device->mRenderMode = HrtfRender;
595 ALuint ambi_order{1};
596 if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf-mode"))
598 struct HrtfModeEntry {
599 char name[8];
600 RenderMode mode;
601 ALuint order;
603 static const HrtfModeEntry hrtf_modes[]{
604 { "full", HrtfRender, 1 },
605 { "ambi1", NormalRender, 1 },
606 { "ambi2", NormalRender, 2 },
609 const char *mode{modeopt->c_str()};
610 if(al::strcasecmp(mode, "basic") == 0 || al::strcasecmp(mode, "ambi3") == 0)
612 ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2");
613 mode = "ambi2";
616 auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
617 { return al::strcasecmp(mode, entry.name) == 0; };
618 auto iter = std::find_if(std::begin(hrtf_modes), std::end(hrtf_modes), match_entry);
619 if(iter == std::end(hrtf_modes))
620 ERR("Unexpected hrtf-mode: %s\n", mode);
621 else
623 device->mRenderMode = iter->mode;
624 ambi_order = iter->order;
627 TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
628 (((ambi_order%100)/10) == 1) ? "th" :
629 ((ambi_order%10) == 1) ? "st" :
630 ((ambi_order%10) == 2) ? "nd" :
631 ((ambi_order%10) == 3) ? "rd" : "th",
632 (device->mRenderMode == HrtfRender) ? "+ Full " : "",
633 device->HrtfName.c_str());
635 if(ambi_order >= 2)
636 AmbiOrderHFGain = AmbiOrderHFGain2O;
637 else if(ambi_order == 1)
638 AmbiOrderHFGain = AmbiOrderHFGain1O;
639 device->mAmbiOrder = ambi_order;
641 const size_t count{AmbiChannelsFromOrder(ambi_order)};
642 device->mHrtfState = DirectHrtfState::Create(count);
644 std::transform(AmbiIndex::From3D.begin(), AmbiIndex::From3D.begin()+count,
645 std::begin(device->Dry.AmbiMap),
646 [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
648 AllocChannels(device, static_cast<ALuint>(count), device->channelsFromFmt());
650 BuildBFormatHrtf(device->mHrtf, device->mHrtfState.get(), AmbiPoints, AmbiMatrix,
651 AmbiOrderHFGain);
653 HrtfEntry *Hrtf{device->mHrtf};
654 InitNearFieldCtrl(device, Hrtf->field[0].distance, ambi_order, ChansPerOrder);
657 void InitUhjPanning(ALCdevice *device)
659 /* UHJ is always 2D first-order. */
660 constexpr size_t count{Ambi2DChannelsFromOrder(1)};
662 device->mAmbiOrder = 1;
664 auto acnmap_end = AmbiIndex::FromFuMa.begin() + count;
665 std::transform(AmbiIndex::FromFuMa.begin(), acnmap_end, std::begin(device->Dry.AmbiMap),
666 [](const uint8_t &acn) noexcept -> BFChannelConfig
667 { return BFChannelConfig{1.0f/AmbiScale::FromFuMa[acn], acn}; }
669 AllocChannels(device, ALuint{count}, device->channelsFromFmt());
672 } // namespace
674 void aluInitRenderer(ALCdevice *device, ALint hrtf_id, HrtfRequestMode hrtf_appreq, HrtfRequestMode hrtf_userreq)
676 /* Hold the HRTF the device last used, in case it's used again. */
677 HrtfEntry *old_hrtf{device->mHrtf};
679 device->mHrtfState = nullptr;
680 device->mHrtf = nullptr;
681 device->HrtfName.clear();
682 device->mRenderMode = NormalRender;
684 if(device->FmtChans != DevFmtStereo)
686 if(old_hrtf)
687 old_hrtf->DecRef();
688 old_hrtf = nullptr;
689 if(hrtf_appreq == Hrtf_Enable)
690 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
692 const char *layout{nullptr};
693 switch(device->FmtChans)
695 case DevFmtQuad: layout = "quad"; break;
696 case DevFmtX51: /* fall-through */
697 case DevFmtX51Rear: layout = "surround51"; break;
698 case DevFmtX61: layout = "surround61"; break;
699 case DevFmtX71: layout = "surround71"; break;
700 /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
701 case DevFmtMono:
702 case DevFmtStereo:
703 case DevFmtAmbi3D:
704 break;
707 const char *devname{device->DeviceName.c_str()};
708 ALuint speakermap[MAX_OUTPUT_CHANNELS];
709 AmbDecConf *pconf{nullptr};
710 AmbDecConf conf{};
711 if(layout)
713 if(auto decopt = ConfigValueStr(devname, "decoder", layout))
715 if(!conf.load(decopt->c_str()))
716 ERR("Failed to load layout file %s\n", decopt->c_str());
717 else if(conf.Speakers.size() > MAX_OUTPUT_CHANNELS)
718 ERR("Unsupported speaker count %zu (max %d)\n", conf.Speakers.size(),
719 MAX_OUTPUT_CHANNELS);
720 else if(conf.ChanMask > AMBI_3ORDER_MASK)
721 ERR("Unsupported channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
722 AMBI_3ORDER_MASK);
723 else if(MakeSpeakerMap(device, &conf, speakermap))
724 pconf = &conf;
728 if(!pconf)
729 InitPanning(device);
730 else
732 int hqdec{GetConfigValueBool(devname, "decoder", "hq-mode", 1)};
733 InitCustomPanning(device, !!hqdec, pconf, speakermap);
735 if(device->AmbiDecoder)
736 device->PostProcess = &ALCdevice::ProcessAmbiDec;
737 return;
740 bool headphones{device->IsHeadphones != AL_FALSE};
741 if(device->Type != Loopback)
743 if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-mode"))
745 const char *mode{modeopt->c_str()};
746 if(al::strcasecmp(mode, "headphones") == 0)
747 headphones = true;
748 else if(al::strcasecmp(mode, "speakers") == 0)
749 headphones = false;
750 else if(al::strcasecmp(mode, "auto") != 0)
751 ERR("Unexpected stereo-mode: %s\n", mode);
755 if(hrtf_userreq == Hrtf_Default)
757 bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
758 (hrtf_appreq == Hrtf_Enable);
759 if(!usehrtf) goto no_hrtf;
761 device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
762 if(headphones && hrtf_appreq != Hrtf_Disable)
763 device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
765 else
767 if(hrtf_userreq != Hrtf_Enable)
769 if(hrtf_appreq == Hrtf_Enable)
770 device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
771 goto no_hrtf;
773 device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
776 if(device->HrtfList.empty())
777 device->HrtfList = EnumerateHrtf(device->DeviceName.c_str());
779 if(hrtf_id >= 0 && static_cast<ALuint>(hrtf_id) < device->HrtfList.size())
781 const EnumeratedHrtf &entry = device->HrtfList[static_cast<ALuint>(hrtf_id)];
782 HrtfEntry *hrtf{GetLoadedHrtf(entry.hrtf)};
783 if(hrtf && hrtf->sampleRate == device->Frequency)
785 device->mHrtf = hrtf;
786 device->HrtfName = entry.name;
788 else if(hrtf)
789 hrtf->DecRef();
792 if(!device->mHrtf)
794 auto find_hrtf = [device](const EnumeratedHrtf &entry) -> bool
796 HrtfEntry *hrtf{GetLoadedHrtf(entry.hrtf)};
797 if(!hrtf) return false;
798 if(hrtf->sampleRate != device->Frequency)
800 hrtf->DecRef();
801 return false;
803 device->mHrtf = hrtf;
804 device->HrtfName = entry.name;
805 return true;
807 std::find_if(device->HrtfList.cbegin(), device->HrtfList.cend(), find_hrtf);
810 if(device->mHrtf)
812 if(old_hrtf)
813 old_hrtf->DecRef();
814 old_hrtf = nullptr;
816 InitHrtfPanning(device);
817 device->PostProcess = &ALCdevice::ProcessHrtf;
818 return;
820 device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
822 no_hrtf:
823 if(old_hrtf)
824 old_hrtf->DecRef();
825 old_hrtf = nullptr;
827 device->mRenderMode = StereoPair;
829 if(device->Type != Loopback)
831 if(auto cflevopt = ConfigValueInt(device->DeviceName.c_str(), nullptr, "cf_level"))
833 if(*cflevopt > 0 && *cflevopt <= 6)
835 device->Bs2b = al::make_unique<bs2b>();
836 bs2b_set_params(device->Bs2b.get(), *cflevopt,
837 static_cast<int>(device->Frequency));
838 TRACE("BS2B enabled\n");
839 InitPanning(device);
840 device->PostProcess = &ALCdevice::ProcessBs2b;
841 return;
846 if(auto encopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-encoding"))
848 const char *mode{encopt->c_str()};
849 if(al::strcasecmp(mode, "uhj") == 0)
850 device->mRenderMode = NormalRender;
851 else if(al::strcasecmp(mode, "panpot") != 0)
852 ERR("Unexpected stereo-encoding: %s\n", mode);
854 if(device->mRenderMode == NormalRender)
856 device->Uhj_Encoder = al::make_unique<Uhj2Encoder>();
857 TRACE("UHJ enabled\n");
858 InitUhjPanning(device);
859 device->PostProcess = &ALCdevice::ProcessUhj;
860 return;
863 TRACE("Stereo rendering\n");
864 InitPanning(device);
865 device->PostProcess = &ALCdevice::ProcessAmbiDec;
869 void aluInitEffectPanning(ALeffectslot *slot, ALCdevice *device)
871 const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
872 slot->MixBuffer.resize(count);
873 slot->MixBuffer.shrink_to_fit();
875 auto acnmap_end = AmbiIndex::From3D.begin() + count;
876 auto iter = std::transform(AmbiIndex::From3D.begin(), acnmap_end, slot->Wet.AmbiMap.begin(),
877 [](const uint8_t &acn) noexcept -> BFChannelConfig
878 { return BFChannelConfig{1.0f, acn}; }
880 std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
881 slot->Wet.Buffer = {slot->MixBuffer.data(), slot->MixBuffer.size()};
885 void CalcAmbiCoeffs(const float y, const float z, const float x, const float spread,
886 const al::span<float,MAX_AMBI_CHANNELS> coeffs)
888 /* Zeroth-order */
889 coeffs[0] = 1.0f; /* ACN 0 = 1 */
890 /* First-order */
891 coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
892 coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
893 coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
894 /* Second-order */
895 coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
896 coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
897 coeffs[6] = 1.118033989f * (z*z*3.0f - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
898 coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
899 coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
900 /* Third-order */
901 coeffs[9] = 2.091650066f * y * (x*x*3.0f - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
902 coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
903 coeffs[11] = 1.620185175f * y * (z*z*5.0f - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
904 coeffs[12] = 1.322875656f * z * (z*z*5.0f - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
905 coeffs[13] = 1.620185175f * x * (z*z*5.0f - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
906 coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
907 coeffs[15] = 2.091650066f * x * (x*x - y*y*3.0f); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
908 /* Fourth-order */
909 /* ACN 16 = sqrt(35)*3/2 * X * Y * (X*X - Y*Y) */
910 /* ACN 17 = sqrt(35/2)*3/2 * (3*X*X - Y*Y) * Y * Z */
911 /* ACN 18 = sqrt(5)*3/2 * X * Y * (7*Z*Z - 1) */
912 /* ACN 19 = sqrt(5/2)*3/2 * Y * Z * (7*Z*Z - 3) */
913 /* ACN 20 = 3/8 * (35*Z*Z*Z*Z - 30*Z*Z + 3) */
914 /* ACN 21 = sqrt(5/2)*3/2 * X * Z * (7*Z*Z - 3) */
915 /* ACN 22 = sqrt(5)*3/4 * (X*X - Y*Y) * (7*Z*Z - 1) */
916 /* ACN 23 = sqrt(35/2)*3/2 * (X*X - 3*Y*Y) * X * Z */
917 /* ACN 24 = sqrt(35)*3/8 * (X*X*X*X - 6*X*X*Y*Y + Y*Y*Y*Y) */
919 if(spread > 0.0f)
921 /* Implement the spread by using a spherical source that subtends the
922 * angle spread. See:
923 * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
925 * When adjusted for N3D normalization instead of SN3D, these
926 * calculations are:
928 * ZH0 = -sqrt(pi) * (-1+ca);
929 * ZH1 = 0.5*sqrt(pi) * sa*sa;
930 * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
931 * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
932 * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
933 * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
935 * The gain of the source is compensated for size, so that the
936 * loudness doesn't depend on the spread. Thus:
938 * ZH0 = 1.0f;
939 * ZH1 = 0.5f * (ca+1.0f);
940 * ZH2 = 0.5f * (ca+1.0f)*ca;
941 * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
942 * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
943 * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
945 const float ca{std::cos(spread * 0.5f)};
946 /* Increase the source volume by up to +3dB for a full spread. */
947 const float scale{std::sqrt(1.0f + spread/al::MathDefs<float>::Tau())};
949 const float ZH0_norm{scale};
950 const float ZH1_norm{scale * 0.5f * (ca+1.f)};
951 const float ZH2_norm{scale * 0.5f * (ca+1.f)*ca};
952 const float ZH3_norm{scale * 0.125f * (ca+1.f)*(5.f*ca*ca-1.f)};
954 /* Zeroth-order */
955 coeffs[0] *= ZH0_norm;
956 /* First-order */
957 coeffs[1] *= ZH1_norm;
958 coeffs[2] *= ZH1_norm;
959 coeffs[3] *= ZH1_norm;
960 /* Second-order */
961 coeffs[4] *= ZH2_norm;
962 coeffs[5] *= ZH2_norm;
963 coeffs[6] *= ZH2_norm;
964 coeffs[7] *= ZH2_norm;
965 coeffs[8] *= ZH2_norm;
966 /* Third-order */
967 coeffs[9] *= ZH3_norm;
968 coeffs[10] *= ZH3_norm;
969 coeffs[11] *= ZH3_norm;
970 coeffs[12] *= ZH3_norm;
971 coeffs[13] *= ZH3_norm;
972 coeffs[14] *= ZH3_norm;
973 coeffs[15] *= ZH3_norm;
977 void ComputePanGains(const MixParams *mix, const float*RESTRICT coeffs, const float ingain,
978 const al::span<float,MAX_OUTPUT_CHANNELS> gains)
980 auto ambimap = mix->AmbiMap.cbegin();
982 auto iter = std::transform(ambimap, ambimap+mix->Buffer.size(), gains.begin(),
983 [coeffs,ingain](const BFChannelConfig &chanmap) noexcept -> float
984 { return chanmap.Scale * coeffs[chanmap.Index] * ingain; }
986 std::fill(iter, gains.end(), 0.0f);