panning.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <algorithm>
  22. #include <array>
  23. #include <chrono>
  24. #include <cmath>
  25. #include <cstdio>
  26. #include <cstring>
  27. #include <functional>
  28. #include <iterator>
  29. #include <memory>
  30. #include <new>
  31. #include <numeric>
  32. #include <string>
  33. #include "AL/al.h"
  34. #include "AL/alc.h"
  35. #include "AL/alext.h"
  36. #include "al/auxeffectslot.h"
  37. #include "alcmain.h"
  38. #include "alconfig.h"
  39. #include "almalloc.h"
  40. #include "alnumeric.h"
  41. #include "aloptional.h"
  42. #include "alspan.h"
  43. #include "alstring.h"
  44. #include "alu.h"
  45. #include "ambdec.h"
  46. #include "ambidefs.h"
  47. #include "bformatdec.h"
  48. #include "bs2b.h"
  49. #include "devformat.h"
  50. #include "hrtf.h"
  51. #include "logging.h"
  52. #include "math_defs.h"
  53. #include "opthelpers.h"
  54. #include "uhjfilter.h"
  55. constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromN3D;
  56. constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromSN3D;
  57. constexpr std::array<float,MAX_AMBI_CHANNELS> AmbiScale::FromFuMa;
  58. constexpr std::array<uint8_t,MAX_AMBI_CHANNELS> AmbiIndex::FromFuMa;
  59. constexpr std::array<uint8_t,MAX_AMBI_CHANNELS> AmbiIndex::FromACN;
  60. constexpr std::array<uint8_t,MAX_AMBI2D_CHANNELS> AmbiIndex::From2D;
  61. constexpr std::array<uint8_t,MAX_AMBI_CHANNELS> AmbiIndex::From3D;
  62. namespace {
  63. using namespace std::placeholders;
  64. using std::chrono::seconds;
  65. using std::chrono::nanoseconds;
  66. inline const char *GetLabelFromChannel(Channel channel)
  67. {
  68. switch(channel)
  69. {
  70. case FrontLeft: return "front-left";
  71. case FrontRight: return "front-right";
  72. case FrontCenter: return "front-center";
  73. case LFE: return "lfe";
  74. case BackLeft: return "back-left";
  75. case BackRight: return "back-right";
  76. case BackCenter: return "back-center";
  77. case SideLeft: return "side-left";
  78. case SideRight: return "side-right";
  79. case UpperFrontLeft: return "upper-front-left";
  80. case UpperFrontRight: return "upper-front-right";
  81. case UpperBackLeft: return "upper-back-left";
  82. case UpperBackRight: return "upper-back-right";
  83. case LowerFrontLeft: return "lower-front-left";
  84. case LowerFrontRight: return "lower-front-right";
  85. case LowerBackLeft: return "lower-back-left";
  86. case LowerBackRight: return "lower-back-right";
  87. case Aux0: return "aux-0";
  88. case Aux1: return "aux-1";
  89. case Aux2: return "aux-2";
  90. case Aux3: return "aux-3";
  91. case Aux4: return "aux-4";
  92. case Aux5: return "aux-5";
  93. case Aux6: return "aux-6";
  94. case Aux7: return "aux-7";
  95. case Aux8: return "aux-8";
  96. case Aux9: return "aux-9";
  97. case Aux10: return "aux-10";
  98. case Aux11: return "aux-11";
  99. case Aux12: return "aux-12";
  100. case Aux13: return "aux-13";
  101. case Aux14: return "aux-14";
  102. case Aux15: return "aux-15";
  103. case MaxChannels: break;
  104. }
  105. return "(unknown)";
  106. }
  107. void AllocChannels(ALCdevice *device, const ALuint main_chans, const ALuint real_chans)
  108. {
  109. TRACE("Channel config, Main: %u, Real: %u\n", main_chans, real_chans);
  110. /* Allocate extra channels for any post-filter output. */
  111. const ALuint num_chans{main_chans + real_chans};
  112. TRACE("Allocating %u channels, %zu bytes\n", num_chans,
  113. num_chans*sizeof(device->MixBuffer[0]));
  114. device->MixBuffer.resize(num_chans);
  115. al::span<FloatBufferLine> buffer{device->MixBuffer.data(), device->MixBuffer.size()};
  116. device->Dry.Buffer = buffer.first(main_chans);
  117. buffer = buffer.subspan(main_chans);
  118. if(real_chans != 0)
  119. {
  120. device->RealOut.Buffer = buffer.first(real_chans);
  121. buffer = buffer.subspan(real_chans);
  122. }
  123. else
  124. device->RealOut.Buffer = device->Dry.Buffer;
  125. }
  126. struct ChannelMap {
  127. Channel ChanName;
  128. ALfloat Config[MAX_AMBI2D_CHANNELS];
  129. };
  130. bool MakeSpeakerMap(ALCdevice *device, const AmbDecConf *conf, ALuint (&speakermap)[MAX_OUTPUT_CHANNELS])
  131. {
  132. auto map_spkr = [device](const AmbDecConf::SpeakerConf &speaker) -> ALuint
  133. {
  134. /* NOTE: AmbDec does not define any standard speaker names, however
  135. * for this to work we have to by able to find the output channel
  136. * the speaker definition corresponds to. Therefore, OpenAL Soft
  137. * requires these channel labels to be recognized:
  138. *
  139. * LF = Front left
  140. * RF = Front right
  141. * LS = Side left
  142. * RS = Side right
  143. * LB = Back left
  144. * RB = Back right
  145. * CE = Front center
  146. * CB = Back center
  147. *
  148. * Additionally, surround51 will acknowledge back speakers for side
  149. * channels, and surround51rear will acknowledge side speakers for
  150. * back channels, to avoid issues with an ambdec expecting 5.1 to
  151. * use the side channels when the device is configured for back,
  152. * and vice-versa.
  153. */
  154. Channel ch{};
  155. if(speaker.Name == "LF")
  156. ch = FrontLeft;
  157. else if(speaker.Name == "RF")
  158. ch = FrontRight;
  159. else if(speaker.Name == "CE")
  160. ch = FrontCenter;
  161. else if(speaker.Name == "LS")
  162. {
  163. if(device->FmtChans == DevFmtX51Rear)
  164. ch = BackLeft;
  165. else
  166. ch = SideLeft;
  167. }
  168. else if(speaker.Name == "RS")
  169. {
  170. if(device->FmtChans == DevFmtX51Rear)
  171. ch = BackRight;
  172. else
  173. ch = SideRight;
  174. }
  175. else if(speaker.Name == "LB")
  176. {
  177. if(device->FmtChans == DevFmtX51)
  178. ch = SideLeft;
  179. else
  180. ch = BackLeft;
  181. }
  182. else if(speaker.Name == "RB")
  183. {
  184. if(device->FmtChans == DevFmtX51)
  185. ch = SideRight;
  186. else
  187. ch = BackRight;
  188. }
  189. else if(speaker.Name == "CB")
  190. ch = BackCenter;
  191. else
  192. {
  193. const char *name{speaker.Name.c_str()};
  194. unsigned int n;
  195. char c;
  196. if(sscanf(name, "AUX%u%c", &n, &c) == 1 && n < 16)
  197. ch = static_cast<Channel>(Aux0+n);
  198. else
  199. {
  200. ERR("AmbDec speaker label \"%s\" not recognized\n", name);
  201. return INVALID_CHANNEL_INDEX;
  202. }
  203. }
  204. const ALuint chidx{GetChannelIdxByName(device->RealOut, ch)};
  205. if(chidx == INVALID_CHANNEL_INDEX)
  206. ERR("Failed to lookup AmbDec speaker label %s\n", speaker.Name.c_str());
  207. return chidx;
  208. };
  209. std::transform(conf->Speakers.begin(), conf->Speakers.end(), std::begin(speakermap), map_spkr);
  210. /* Return success if no invalid entries are found. */
  211. auto spkrmap_end = std::begin(speakermap) + conf->Speakers.size();
  212. return std::find(std::begin(speakermap), spkrmap_end, INVALID_CHANNEL_INDEX) == spkrmap_end;
  213. }
  214. constexpr ChannelMap MonoCfg[1] = {
  215. { FrontCenter, { 1.0f } },
  216. }, StereoCfg[2] = {
  217. { FrontLeft, { 5.00000000e-1f, 2.88675135e-1f, 5.52305643e-2f } },
  218. { FrontRight, { 5.00000000e-1f, -2.88675135e-1f, 5.52305643e-2f } },
  219. }, QuadCfg[4] = {
  220. { BackLeft, { 3.53553391e-1f, 2.04124145e-1f, -2.04124145e-1f } },
  221. { FrontLeft, { 3.53553391e-1f, 2.04124145e-1f, 2.04124145e-1f } },
  222. { FrontRight, { 3.53553391e-1f, -2.04124145e-1f, 2.04124145e-1f } },
  223. { BackRight, { 3.53553391e-1f, -2.04124145e-1f, -2.04124145e-1f } },
  224. }, X51SideCfg[4] = {
  225. { SideLeft, { 3.33000782e-1f, 1.89084803e-1f, -2.00042375e-1f, -2.12307769e-2f, -1.14579885e-2f } },
  226. { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 1.66295695e-1f, 7.30571517e-2f, 2.10901184e-2f } },
  227. { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 1.66295695e-1f, -7.30571517e-2f, 2.10901184e-2f } },
  228. { SideRight, { 3.33000782e-1f, -1.89084803e-1f, -2.00042375e-1f, 2.12307769e-2f, -1.14579885e-2f } },
  229. }, X51RearCfg[4] = {
  230. { BackLeft, { 3.33000782e-1f, 1.89084803e-1f, -2.00042375e-1f, -2.12307769e-2f, -1.14579885e-2f } },
  231. { FrontLeft, { 1.88542860e-1f, 1.27709292e-1f, 1.66295695e-1f, 7.30571517e-2f, 2.10901184e-2f } },
  232. { FrontRight, { 1.88542860e-1f, -1.27709292e-1f, 1.66295695e-1f, -7.30571517e-2f, 2.10901184e-2f } },
  233. { BackRight, { 3.33000782e-1f, -1.89084803e-1f, -2.00042375e-1f, 2.12307769e-2f, -1.14579885e-2f } },
  234. }, X61Cfg[6] = {
  235. { SideLeft, { 2.04460341e-1f, 2.17177926e-1f, -4.39996780e-2f, -2.60790269e-2f, -6.87239792e-2f } },
  236. { FrontLeft, { 1.58923161e-1f, 9.21772680e-2f, 1.59658796e-1f, 6.66278083e-2f, 3.84686854e-2f } },
  237. { FrontRight, { 1.58923161e-1f, -9.21772680e-2f, 1.59658796e-1f, -6.66278083e-2f, 3.84686854e-2f } },
  238. { SideRight, { 2.04460341e-1f, -2.17177926e-1f, -4.39996780e-2f, 2.60790269e-2f, -6.87239792e-2f } },
  239. { BackCenter, { 2.50001688e-1f, 0.00000000e+0f, -2.50000094e-1f, 0.00000000e+0f, 6.05133395e-2f } },
  240. }, X71Cfg[6] = {
  241. { BackLeft, { 2.04124145e-1f, 1.08880247e-1f, -1.88586120e-1f, -1.29099444e-1f, 7.45355993e-2f, 3.73460789e-2f, 0.00000000e+0f } },
  242. { SideLeft, { 2.04124145e-1f, 2.17760495e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.49071198e-1f, -3.73460789e-2f, 0.00000000e+0f } },
  243. { FrontLeft, { 2.04124145e-1f, 1.08880247e-1f, 1.88586120e-1f, 1.29099444e-1f, 7.45355993e-2f, 3.73460789e-2f, 0.00000000e+0f } },
  244. { FrontRight, { 2.04124145e-1f, -1.08880247e-1f, 1.88586120e-1f, -1.29099444e-1f, 7.45355993e-2f, -3.73460789e-2f, 0.00000000e+0f } },
  245. { SideRight, { 2.04124145e-1f, -2.17760495e-1f, 0.00000000e+0f, 0.00000000e+0f, -1.49071198e-1f, 3.73460789e-2f, 0.00000000e+0f } },
  246. { BackRight, { 2.04124145e-1f, -1.08880247e-1f, -1.88586120e-1f, 1.29099444e-1f, 7.45355993e-2f, -3.73460789e-2f, 0.00000000e+0f } },
  247. };
  248. void InitNearFieldCtrl(ALCdevice *device, ALfloat ctrl_dist, ALuint order,
  249. const al::span<const ALuint,MAX_AMBI_ORDER+1> chans_per_order)
  250. {
  251. /* NFC is only used when AvgSpeakerDist is greater than 0. */
  252. const char *devname{device->DeviceName.c_str()};
  253. if(!GetConfigValueBool(devname, "decoder", "nfc", 0) || !(ctrl_dist > 0.0f))
  254. return;
  255. device->AvgSpeakerDist = clampf(ctrl_dist, 0.1f, 10.0f);
  256. TRACE("Using near-field reference distance: %.2f meters\n", device->AvgSpeakerDist);
  257. auto iter = std::copy(chans_per_order.begin(), chans_per_order.begin()+order+1,
  258. std::begin(device->NumChannelsPerOrder));
  259. std::fill(iter, std::end(device->NumChannelsPerOrder), 0u);
  260. }
  261. void InitDistanceComp(ALCdevice *device, const AmbDecConf *conf,
  262. const ALuint (&speakermap)[MAX_OUTPUT_CHANNELS])
  263. {
  264. auto get_max = std::bind(maxf, _1,
  265. std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance), _2));
  266. const ALfloat maxdist{
  267. std::accumulate(conf->Speakers.begin(), conf->Speakers.end(), float{0.0f}, get_max)};
  268. const char *devname{device->DeviceName.c_str()};
  269. if(!GetConfigValueBool(devname, "decoder", "distance-comp", 1) || !(maxdist > 0.0f))
  270. return;
  271. const auto distSampleScale = static_cast<ALfloat>(device->Frequency)/SPEEDOFSOUNDMETRESPERSEC;
  272. const auto ChanDelay = device->ChannelDelay.as_span();
  273. size_t total{0u};
  274. for(size_t i{0u};i < conf->Speakers.size();i++)
  275. {
  276. const AmbDecConf::SpeakerConf &speaker = conf->Speakers[i];
  277. const ALuint chan{speakermap[i]};
  278. /* Distance compensation only delays in steps of the sample rate. This
  279. * is a bit less accurate since the delay time falls to the nearest
  280. * sample time, but it's far simpler as it doesn't have to deal with
  281. * phase offsets. This means at 48khz, for instance, the distance delay
  282. * will be in steps of about 7 millimeters.
  283. */
  284. ALfloat delay{std::floor((maxdist - speaker.Distance)*distSampleScale + 0.5f)};
  285. if(delay > ALfloat{MAX_DELAY_LENGTH-1})
  286. {
  287. ERR("Delay for speaker \"%s\" exceeds buffer length (%f > %d)\n",
  288. speaker.Name.c_str(), delay, MAX_DELAY_LENGTH-1);
  289. delay = ALfloat{MAX_DELAY_LENGTH-1};
  290. }
  291. ChanDelay[chan].Length = static_cast<ALuint>(delay);
  292. ChanDelay[chan].Gain = speaker.Distance / maxdist;
  293. TRACE("Channel %u \"%s\" distance compensation: %u samples, %f gain\n", chan,
  294. speaker.Name.c_str(), ChanDelay[chan].Length, ChanDelay[chan].Gain);
  295. /* Round up to the next 4th sample, so each channel buffer starts
  296. * 16-byte aligned.
  297. */
  298. total += RoundUp(ChanDelay[chan].Length, 4);
  299. }
  300. if(total > 0)
  301. {
  302. device->ChannelDelay.setSampleCount(total);
  303. ChanDelay[0].Buffer = device->ChannelDelay.getSamples();
  304. auto set_bufptr = [](const DistanceComp::DistData &last, const DistanceComp::DistData &cur) -> DistanceComp::DistData
  305. {
  306. DistanceComp::DistData ret{cur};
  307. ret.Buffer = last.Buffer + RoundUp(last.Length, 4);
  308. return ret;
  309. };
  310. std::partial_sum(ChanDelay.begin(), ChanDelay.end(), ChanDelay.begin(), set_bufptr);
  311. }
  312. }
  313. auto GetAmbiScales(AmbiNorm scaletype) noexcept -> const std::array<float,MAX_AMBI_CHANNELS>&
  314. {
  315. if(scaletype == AmbiNorm::FuMa) return AmbiScale::FromFuMa;
  316. if(scaletype == AmbiNorm::SN3D) return AmbiScale::FromSN3D;
  317. return AmbiScale::FromN3D;
  318. }
  319. auto GetAmbiLayout(AmbiLayout layouttype) noexcept -> const std::array<uint8_t,MAX_AMBI_CHANNELS>&
  320. {
  321. if(layouttype == AmbiLayout::FuMa) return AmbiIndex::FromFuMa;
  322. return AmbiIndex::FromACN;
  323. }
  324. void InitPanning(ALCdevice *device)
  325. {
  326. al::span<const ChannelMap> chanmap;
  327. ALuint coeffcount{};
  328. switch(device->FmtChans)
  329. {
  330. case DevFmtMono:
  331. chanmap = MonoCfg;
  332. coeffcount = 1;
  333. break;
  334. case DevFmtStereo:
  335. chanmap = StereoCfg;
  336. coeffcount = 3;
  337. break;
  338. case DevFmtQuad:
  339. chanmap = QuadCfg;
  340. coeffcount = 3;
  341. break;
  342. case DevFmtX51:
  343. chanmap = X51SideCfg;
  344. coeffcount = 5;
  345. break;
  346. case DevFmtX51Rear:
  347. chanmap = X51RearCfg;
  348. coeffcount = 5;
  349. break;
  350. case DevFmtX61:
  351. chanmap = X61Cfg;
  352. coeffcount = 5;
  353. break;
  354. case DevFmtX71:
  355. chanmap = X71Cfg;
  356. coeffcount = 7;
  357. break;
  358. case DevFmtAmbi3D:
  359. break;
  360. }
  361. if(device->FmtChans == DevFmtAmbi3D)
  362. {
  363. const char *devname{device->DeviceName.c_str()};
  364. const std::array<uint8_t,MAX_AMBI_CHANNELS> &acnmap = GetAmbiLayout(device->mAmbiLayout);
  365. const std::array<float,MAX_AMBI_CHANNELS> &n3dscale = GetAmbiScales(device->mAmbiScale);
  366. /* For DevFmtAmbi3D, the ambisonic order is already set. */
  367. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  368. std::transform(acnmap.begin(), acnmap.begin()+count, std::begin(device->Dry.AmbiMap),
  369. [&n3dscale](const uint8_t &acn) noexcept -> BFChannelConfig
  370. { return BFChannelConfig{1.0f/n3dscale[acn], acn}; }
  371. );
  372. AllocChannels(device, static_cast<ALuint>(count), 0);
  373. ALfloat nfc_delay{ConfigValueFloat(devname, "decoder", "nfc-ref-delay").value_or(0.0f)};
  374. if(nfc_delay > 0.0f)
  375. {
  376. static const ALuint chans_per_order[MAX_AMBI_ORDER+1]{ 1, 3, 5, 7 };
  377. InitNearFieldCtrl(device, nfc_delay * SPEEDOFSOUNDMETRESPERSEC, device->mAmbiOrder,
  378. chans_per_order);
  379. }
  380. }
  381. else
  382. {
  383. ChannelDec chancoeffs[MAX_OUTPUT_CHANNELS]{};
  384. ALuint idxmap[MAX_OUTPUT_CHANNELS]{};
  385. for(size_t i{0u};i < chanmap.size();++i)
  386. {
  387. const ALuint idx{GetChannelIdxByName(device->RealOut, chanmap[i].ChanName)};
  388. if(idx == INVALID_CHANNEL_INDEX)
  389. {
  390. ERR("Failed to find %s channel in device\n",
  391. GetLabelFromChannel(chanmap[i].ChanName));
  392. continue;
  393. }
  394. idxmap[i] = idx;
  395. std::copy_n(chanmap[i].Config, coeffcount, chancoeffs[i]);
  396. }
  397. /* For non-DevFmtAmbi3D, set the ambisonic order given the mixing
  398. * channel count. Built-in speaker decoders are always 2D, so just
  399. * reverse that calculation.
  400. */
  401. device->mAmbiOrder = (coeffcount-1) / 2;
  402. std::transform(AmbiIndex::From2D.begin(), AmbiIndex::From2D.begin()+coeffcount,
  403. std::begin(device->Dry.AmbiMap),
  404. [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
  405. );
  406. AllocChannels(device, coeffcount, device->channelsFromFmt());
  407. TRACE("Enabling %s-order%s ambisonic decoder\n",
  408. (coeffcount > 5) ? "third" :
  409. (coeffcount > 3) ? "second" : "first",
  410. ""
  411. );
  412. device->AmbiDecoder = al::make_unique<BFormatDec>(coeffcount,
  413. static_cast<ALsizei>(chanmap.size()), chancoeffs, idxmap);
  414. }
  415. }
  416. void InitCustomPanning(ALCdevice *device, bool hqdec, const AmbDecConf *conf,
  417. const ALuint (&speakermap)[MAX_OUTPUT_CHANNELS])
  418. {
  419. static const ALuint chans_per_order2d[MAX_AMBI_ORDER+1] = { 1, 2, 2, 2 };
  420. static const ALuint chans_per_order3d[MAX_AMBI_ORDER+1] = { 1, 3, 5, 7 };
  421. if(!hqdec && conf->FreqBands != 1)
  422. ERR("Basic renderer uses the high-frequency matrix as single-band (xover_freq = %.0fhz)\n",
  423. conf->XOverFreq);
  424. const ALuint order{(conf->ChanMask > AMBI_2ORDER_MASK) ? 3u :
  425. (conf->ChanMask > AMBI_1ORDER_MASK) ? 2u : 1u};
  426. device->mAmbiOrder = order;
  427. ALuint count;
  428. if((conf->ChanMask&AMBI_PERIPHONIC_MASK))
  429. {
  430. count = static_cast<ALuint>(AmbiChannelsFromOrder(order));
  431. std::transform(AmbiIndex::From3D.begin(), AmbiIndex::From3D.begin()+count,
  432. std::begin(device->Dry.AmbiMap),
  433. [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
  434. );
  435. }
  436. else
  437. {
  438. count = static_cast<ALuint>(Ambi2DChannelsFromOrder(order));
  439. std::transform(AmbiIndex::From2D.begin(), AmbiIndex::From2D.begin()+count,
  440. std::begin(device->Dry.AmbiMap),
  441. [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
  442. );
  443. }
  444. AllocChannels(device, count, device->channelsFromFmt());
  445. TRACE("Enabling %s-band %s-order%s ambisonic decoder\n",
  446. (!hqdec || conf->FreqBands == 1) ? "single" : "dual",
  447. (conf->ChanMask > AMBI_2ORDER_MASK) ? "third" :
  448. (conf->ChanMask > AMBI_1ORDER_MASK) ? "second" : "first",
  449. (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? " periphonic" : ""
  450. );
  451. device->AmbiDecoder = al::make_unique<BFormatDec>(conf, hqdec, count, device->Frequency,
  452. speakermap);
  453. auto accum_spkr_dist = std::bind(std::plus<float>{}, _1,
  454. std::bind(std::mem_fn(&AmbDecConf::SpeakerConf::Distance), _2));
  455. const ALfloat avg_dist{
  456. std::accumulate(conf->Speakers.begin(), conf->Speakers.end(), 0.0f, accum_spkr_dist) /
  457. static_cast<ALfloat>(conf->Speakers.size())};
  458. InitNearFieldCtrl(device, avg_dist, order,
  459. (conf->ChanMask&AMBI_PERIPHONIC_MASK) ? chans_per_order3d : chans_per_order2d);
  460. InitDistanceComp(device, conf, speakermap);
  461. }
  462. void InitHrtfPanning(ALCdevice *device)
  463. {
  464. constexpr float PI{al::MathDefs<float>::Pi()};
  465. constexpr float PI_2{al::MathDefs<float>::Pi() / 2.0f};
  466. constexpr float PI_4{al::MathDefs<float>::Pi() / 4.0f};
  467. constexpr float PI3_4{al::MathDefs<float>::Pi() * 3.0f / 4.0f};
  468. const float CornerElev{static_cast<float>(std::atan2(1.0, std::sqrt(2.0)))};
  469. static const AngularPoint AmbiPoints1O[]{
  470. { ElevRadius{ CornerElev}, AzimRadius{ -PI_4} },
  471. { ElevRadius{ CornerElev}, AzimRadius{-PI3_4} },
  472. { ElevRadius{ CornerElev}, AzimRadius{ PI_4} },
  473. { ElevRadius{ CornerElev}, AzimRadius{ PI3_4} },
  474. { ElevRadius{-CornerElev}, AzimRadius{ -PI_4} },
  475. { ElevRadius{-CornerElev}, AzimRadius{-PI3_4} },
  476. { ElevRadius{-CornerElev}, AzimRadius{ PI_4} },
  477. { ElevRadius{-CornerElev}, AzimRadius{ PI3_4} },
  478. }, AmbiPoints2O[]{
  479. { ElevRadius{ 0.0f}, AzimRadius{ 0.0f} },
  480. { ElevRadius{ 0.0f}, AzimRadius{ PI} },
  481. { ElevRadius{ 0.0f}, AzimRadius{ -PI_2} },
  482. { ElevRadius{ 0.0f}, AzimRadius{ PI_2} },
  483. { ElevRadius{ PI_2}, AzimRadius{ 0.0f} },
  484. { ElevRadius{ -PI_2}, AzimRadius{ 0.0f} },
  485. { ElevRadius{ PI_4}, AzimRadius{ -PI_2} },
  486. { ElevRadius{ PI_4}, AzimRadius{ PI_2} },
  487. { ElevRadius{ -PI_4}, AzimRadius{ -PI_2} },
  488. { ElevRadius{ -PI_4}, AzimRadius{ PI_2} },
  489. { ElevRadius{ PI_4}, AzimRadius{ 0.0f} },
  490. { ElevRadius{ PI_4}, AzimRadius{ PI} },
  491. { ElevRadius{ -PI_4}, AzimRadius{ 0.0f} },
  492. { ElevRadius{ -PI_4}, AzimRadius{ PI} },
  493. { ElevRadius{ 0.0f}, AzimRadius{ -PI_4} },
  494. { ElevRadius{ 0.0f}, AzimRadius{-PI3_4} },
  495. { ElevRadius{ 0.0f}, AzimRadius{ PI_4} },
  496. { ElevRadius{ 0.0f}, AzimRadius{ PI3_4} },
  497. { ElevRadius{ CornerElev}, AzimRadius{ -PI_4} },
  498. { ElevRadius{ CornerElev}, AzimRadius{-PI3_4} },
  499. { ElevRadius{ CornerElev}, AzimRadius{ PI_4} },
  500. { ElevRadius{ CornerElev}, AzimRadius{ PI3_4} },
  501. { ElevRadius{-CornerElev}, AzimRadius{ -PI_4} },
  502. { ElevRadius{-CornerElev}, AzimRadius{-PI3_4} },
  503. { ElevRadius{-CornerElev}, AzimRadius{ PI_4} },
  504. { ElevRadius{-CornerElev}, AzimRadius{ PI3_4} },
  505. };
  506. static const float AmbiMatrix1O[][MAX_AMBI_CHANNELS]{
  507. { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
  508. { 1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
  509. { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, 1.250000000e-01f },
  510. { 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f },
  511. { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
  512. { 1.250000000e-01f, 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
  513. { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, 1.250000000e-01f },
  514. { 1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f, -1.250000000e-01f },
  515. }, AmbiMatrix2O[][MAX_AMBI_CHANNELS]{
  516. { 3.846153846e-02f, 0.000000000e+00f, 0.000000000e+00f, 6.661733875e-02f, 0.000000000e+00f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, 8.606629658e-02f },
  517. { 3.846153846e-02f, 0.000000000e+00f, 0.000000000e+00f, -6.661733875e-02f, 0.000000000e+00f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, 8.606629658e-02f },
  518. { 3.846153846e-02f, 6.661733875e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, -8.606629658e-02f },
  519. { 3.846153846e-02f, -6.661733875e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, -8.606629658e-02f },
  520. { 3.846153846e-02f, 0.000000000e+00f, 6.661733875e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 9.938079900e-02f, 0.000000000e+00f, 0.000000000e+00f },
  521. { 3.846153846e-02f, 0.000000000e+00f, -6.661733875e-02f, 0.000000000e+00f, 0.000000000e+00f, 0.000000000e+00f, 9.938079900e-02f, 0.000000000e+00f, 0.000000000e+00f },
  522. { 3.846153846e-02f, 4.710557198e-02f, 4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, 6.834676493e-02f, 2.484519975e-02f, 0.000000000e+00f, -4.303314829e-02f },
  523. { 3.846153846e-02f, -4.710557198e-02f, 4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, -6.834676493e-02f, 2.484519975e-02f, 0.000000000e+00f, -4.303314829e-02f },
  524. { 3.846153846e-02f, 4.710557198e-02f, -4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, -6.834676493e-02f, 2.484519975e-02f, 0.000000000e+00f, -4.303314829e-02f },
  525. { 3.846153846e-02f, -4.710557198e-02f, -4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, 6.834676493e-02f, 2.484519975e-02f, 0.000000000e+00f, -4.303314829e-02f },
  526. { 3.846153846e-02f, 0.000000000e+00f, 4.710557198e-02f, 4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, 2.484519975e-02f, 6.834676493e-02f, 4.303314829e-02f },
  527. { 3.846153846e-02f, 0.000000000e+00f, 4.710557198e-02f, -4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, 2.484519975e-02f, -6.834676493e-02f, 4.303314829e-02f },
  528. { 3.846153846e-02f, 0.000000000e+00f, -4.710557198e-02f, 4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, 2.484519975e-02f, -6.834676493e-02f, 4.303314829e-02f },
  529. { 3.846153846e-02f, 0.000000000e+00f, -4.710557198e-02f, -4.710557198e-02f, 0.000000000e+00f, 0.000000000e+00f, 2.484519975e-02f, 6.834676493e-02f, 4.303314829e-02f },
  530. { 3.846153846e-02f, 4.710557198e-02f, 0.000000000e+00f, 4.710557198e-02f, 6.834676493e-02f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, 0.000000000e+00f },
  531. { 3.846153846e-02f, 4.710557198e-02f, 0.000000000e+00f, -4.710557198e-02f, -6.834676493e-02f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, 0.000000000e+00f },
  532. { 3.846153846e-02f, -4.710557198e-02f, 0.000000000e+00f, 4.710557198e-02f, -6.834676493e-02f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, 0.000000000e+00f },
  533. { 3.846153846e-02f, -4.710557198e-02f, 0.000000000e+00f, -4.710557198e-02f, 6.834676493e-02f, 0.000000000e+00f, -4.969039950e-02f, 0.000000000e+00f, 0.000000000e+00f },
  534. { 3.846153846e-02f, 3.846153846e-02f, 3.846153846e-02f, 3.846153846e-02f, 4.556450996e-02f, 4.556450996e-02f, 0.000000000e+00f, 4.556450996e-02f, 0.000000000e+00f },
  535. { 3.846153846e-02f, 3.846153846e-02f, 3.846153846e-02f, -3.846153846e-02f, -4.556450996e-02f, 4.556450996e-02f, 0.000000000e+00f, -4.556450996e-02f, 0.000000000e+00f },
  536. { 3.846153846e-02f, -3.846153846e-02f, 3.846153846e-02f, 3.846153846e-02f, -4.556450996e-02f, -4.556450996e-02f, 0.000000000e+00f, 4.556450996e-02f, 0.000000000e+00f },
  537. { 3.846153846e-02f, -3.846153846e-02f, 3.846153846e-02f, -3.846153846e-02f, 4.556450996e-02f, -4.556450996e-02f, 0.000000000e+00f, -4.556450996e-02f, 0.000000000e+00f },
  538. { 3.846153846e-02f, 3.846153846e-02f, -3.846153846e-02f, 3.846153846e-02f, 4.556450996e-02f, -4.556450996e-02f, 0.000000000e+00f, -4.556450996e-02f, 0.000000000e+00f },
  539. { 3.846153846e-02f, 3.846153846e-02f, -3.846153846e-02f, -3.846153846e-02f, -4.556450996e-02f, -4.556450996e-02f, 0.000000000e+00f, 4.556450996e-02f, 0.000000000e+00f },
  540. { 3.846153846e-02f, -3.846153846e-02f, -3.846153846e-02f, 3.846153846e-02f, -4.556450996e-02f, 4.556450996e-02f, 0.000000000e+00f, -4.556450996e-02f, 0.000000000e+00f },
  541. { 3.846153846e-02f, -3.846153846e-02f, -3.846153846e-02f, -3.846153846e-02f, 4.556450996e-02f, 4.556450996e-02f, 0.000000000e+00f, 4.556450996e-02f, 0.000000000e+00f },
  542. };
  543. static const float AmbiOrderHFGain1O[MAX_AMBI_ORDER+1]{
  544. 2.000000000e+00f, 1.154700538e+00f
  545. }, AmbiOrderHFGain2O[MAX_AMBI_ORDER+1]{
  546. 2.687419249e+00f, 2.081665999e+00f, 1.074967700e+00f
  547. };
  548. static const ALuint ChansPerOrder[MAX_AMBI_ORDER+1]{ 1, 3, 5, 7 };
  549. static_assert(al::size(AmbiPoints1O) == al::size(AmbiMatrix1O), "First-Order Ambisonic HRTF mismatch");
  550. static_assert(al::size(AmbiPoints2O) == al::size(AmbiMatrix2O), "Second-Order Ambisonic HRTF mismatch");
  551. /* Don't bother with HOA when using full HRTF rendering. Nothing needs it,
  552. * and it eases the CPU/memory load.
  553. */
  554. device->mRenderMode = HrtfRender;
  555. ALuint ambi_order{1};
  556. if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf-mode"))
  557. {
  558. struct HrtfModeEntry {
  559. char name[8];
  560. RenderMode mode;
  561. ALuint order;
  562. };
  563. static const HrtfModeEntry hrtf_modes[]{
  564. { "full", HrtfRender, 1 },
  565. { "ambi1", NormalRender, 1 },
  566. { "ambi2", NormalRender, 2 },
  567. };
  568. const char *mode{modeopt->c_str()};
  569. if(al::strcasecmp(mode, "basic") == 0 || al::strcasecmp(mode, "ambi3") == 0)
  570. {
  571. ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2");
  572. mode = "ambi2";
  573. }
  574. auto match_entry = [mode](const HrtfModeEntry &entry) -> bool
  575. { return al::strcasecmp(mode, entry.name) == 0; };
  576. auto iter = std::find_if(std::begin(hrtf_modes), std::end(hrtf_modes), match_entry);
  577. if(iter == std::end(hrtf_modes))
  578. ERR("Unexpected hrtf-mode: %s\n", mode);
  579. else
  580. {
  581. device->mRenderMode = iter->mode;
  582. ambi_order = iter->order;
  583. }
  584. }
  585. TRACE("%u%s order %sHRTF rendering enabled, using \"%s\"\n", ambi_order,
  586. (((ambi_order%100)/10) == 1) ? "th" :
  587. ((ambi_order%10) == 1) ? "st" :
  588. ((ambi_order%10) == 2) ? "nd" :
  589. ((ambi_order%10) == 3) ? "rd" : "th",
  590. (device->mRenderMode == HrtfRender) ? "+ Full " : "",
  591. device->HrtfName.c_str());
  592. al::span<const AngularPoint> AmbiPoints{};
  593. const float (*AmbiMatrix)[MAX_AMBI_CHANNELS]{};
  594. const float *AmbiOrderHFGain{};
  595. if(ambi_order >= 2)
  596. {
  597. AmbiPoints = AmbiPoints2O;
  598. AmbiMatrix = AmbiMatrix2O;
  599. AmbiOrderHFGain = AmbiOrderHFGain2O;
  600. }
  601. else /*if(ambi_order == 1)*/
  602. {
  603. AmbiPoints = AmbiPoints1O;
  604. AmbiMatrix = AmbiMatrix1O;
  605. AmbiOrderHFGain = AmbiOrderHFGain1O;
  606. }
  607. device->mAmbiOrder = ambi_order;
  608. const size_t count{AmbiChannelsFromOrder(ambi_order)};
  609. device->mHrtfState = DirectHrtfState::Create(count);
  610. std::transform(AmbiIndex::From3D.begin(), AmbiIndex::From3D.begin()+count,
  611. std::begin(device->Dry.AmbiMap),
  612. [](const uint8_t &index) noexcept { return BFChannelConfig{1.0f, index}; }
  613. );
  614. AllocChannels(device, static_cast<ALuint>(count), device->channelsFromFmt());
  615. BuildBFormatHrtf(device->mHrtf, device->mHrtfState.get(), AmbiPoints, AmbiMatrix,
  616. AmbiOrderHFGain);
  617. HrtfEntry *Hrtf{device->mHrtf};
  618. InitNearFieldCtrl(device, Hrtf->field[0].distance, ambi_order, ChansPerOrder);
  619. }
  620. void InitUhjPanning(ALCdevice *device)
  621. {
  622. /* UHJ is always 2D first-order. */
  623. constexpr size_t count{Ambi2DChannelsFromOrder(1)};
  624. device->mAmbiOrder = 1;
  625. auto acnmap_end = AmbiIndex::FromFuMa.begin() + count;
  626. std::transform(AmbiIndex::FromFuMa.begin(), acnmap_end, std::begin(device->Dry.AmbiMap),
  627. [](const uint8_t &acn) noexcept -> BFChannelConfig
  628. { return BFChannelConfig{1.0f/AmbiScale::FromFuMa[acn], acn}; }
  629. );
  630. AllocChannels(device, ALuint{count}, device->channelsFromFmt());
  631. }
  632. } // namespace
  633. void aluInitRenderer(ALCdevice *device, ALint hrtf_id, HrtfRequestMode hrtf_appreq, HrtfRequestMode hrtf_userreq)
  634. {
  635. /* Hold the HRTF the device last used, in case it's used again. */
  636. HrtfEntry *old_hrtf{device->mHrtf};
  637. device->mHrtfState = nullptr;
  638. device->mHrtf = nullptr;
  639. device->HrtfName.clear();
  640. device->mRenderMode = NormalRender;
  641. if(device->FmtChans != DevFmtStereo)
  642. {
  643. if(old_hrtf)
  644. old_hrtf->DecRef();
  645. old_hrtf = nullptr;
  646. if(hrtf_appreq == Hrtf_Enable)
  647. device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  648. const char *layout{nullptr};
  649. switch(device->FmtChans)
  650. {
  651. case DevFmtQuad: layout = "quad"; break;
  652. case DevFmtX51: /* fall-through */
  653. case DevFmtX51Rear: layout = "surround51"; break;
  654. case DevFmtX61: layout = "surround61"; break;
  655. case DevFmtX71: layout = "surround71"; break;
  656. /* Mono, Stereo, and Ambisonics output don't use custom decoders. */
  657. case DevFmtMono:
  658. case DevFmtStereo:
  659. case DevFmtAmbi3D:
  660. break;
  661. }
  662. const char *devname{device->DeviceName.c_str()};
  663. ALuint speakermap[MAX_OUTPUT_CHANNELS];
  664. AmbDecConf *pconf{nullptr};
  665. AmbDecConf conf{};
  666. if(layout)
  667. {
  668. if(auto decopt = ConfigValueStr(devname, "decoder", layout))
  669. {
  670. if(!conf.load(decopt->c_str()))
  671. ERR("Failed to load layout file %s\n", decopt->c_str());
  672. else if(conf.Speakers.size() > MAX_OUTPUT_CHANNELS)
  673. ERR("Unsupported speaker count %zu (max %d)\n", conf.Speakers.size(),
  674. MAX_OUTPUT_CHANNELS);
  675. else if(conf.ChanMask > AMBI_3ORDER_MASK)
  676. ERR("Unsupported channel mask 0x%04x (max 0x%x)\n", conf.ChanMask,
  677. AMBI_3ORDER_MASK);
  678. else if(MakeSpeakerMap(device, &conf, speakermap))
  679. pconf = &conf;
  680. }
  681. }
  682. if(!pconf)
  683. InitPanning(device);
  684. else
  685. {
  686. int hqdec{GetConfigValueBool(devname, "decoder", "hq-mode", 1)};
  687. InitCustomPanning(device, !!hqdec, pconf, speakermap);
  688. }
  689. if(device->AmbiDecoder)
  690. device->PostProcess = &ALCdevice::ProcessAmbiDec;
  691. return;
  692. }
  693. bool headphones{device->IsHeadphones != AL_FALSE};
  694. if(device->Type != Loopback)
  695. {
  696. if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-mode"))
  697. {
  698. const char *mode{modeopt->c_str()};
  699. if(al::strcasecmp(mode, "headphones") == 0)
  700. headphones = true;
  701. else if(al::strcasecmp(mode, "speakers") == 0)
  702. headphones = false;
  703. else if(al::strcasecmp(mode, "auto") != 0)
  704. ERR("Unexpected stereo-mode: %s\n", mode);
  705. }
  706. }
  707. if(hrtf_userreq == Hrtf_Default)
  708. {
  709. bool usehrtf = (headphones && hrtf_appreq != Hrtf_Disable) ||
  710. (hrtf_appreq == Hrtf_Enable);
  711. if(!usehrtf) goto no_hrtf;
  712. device->HrtfStatus = ALC_HRTF_ENABLED_SOFT;
  713. if(headphones && hrtf_appreq != Hrtf_Disable)
  714. device->HrtfStatus = ALC_HRTF_HEADPHONES_DETECTED_SOFT;
  715. }
  716. else
  717. {
  718. if(hrtf_userreq != Hrtf_Enable)
  719. {
  720. if(hrtf_appreq == Hrtf_Enable)
  721. device->HrtfStatus = ALC_HRTF_DENIED_SOFT;
  722. goto no_hrtf;
  723. }
  724. device->HrtfStatus = ALC_HRTF_REQUIRED_SOFT;
  725. }
  726. if(device->HrtfList.empty())
  727. device->HrtfList = EnumerateHrtf(device->DeviceName.c_str());
  728. if(hrtf_id >= 0 && static_cast<ALuint>(hrtf_id) < device->HrtfList.size())
  729. {
  730. const EnumeratedHrtf &entry = device->HrtfList[static_cast<ALuint>(hrtf_id)];
  731. HrtfEntry *hrtf{GetLoadedHrtf(entry.hrtf)};
  732. if(hrtf && hrtf->sampleRate == device->Frequency)
  733. {
  734. device->mHrtf = hrtf;
  735. device->HrtfName = entry.name;
  736. }
  737. else if(hrtf)
  738. hrtf->DecRef();
  739. }
  740. if(!device->mHrtf)
  741. {
  742. auto find_hrtf = [device](const EnumeratedHrtf &entry) -> bool
  743. {
  744. HrtfEntry *hrtf{GetLoadedHrtf(entry.hrtf)};
  745. if(!hrtf) return false;
  746. if(hrtf->sampleRate != device->Frequency)
  747. {
  748. hrtf->DecRef();
  749. return false;
  750. }
  751. device->mHrtf = hrtf;
  752. device->HrtfName = entry.name;
  753. return true;
  754. };
  755. std::find_if(device->HrtfList.cbegin(), device->HrtfList.cend(), find_hrtf);
  756. }
  757. if(device->mHrtf)
  758. {
  759. if(old_hrtf)
  760. old_hrtf->DecRef();
  761. old_hrtf = nullptr;
  762. InitHrtfPanning(device);
  763. device->PostProcess = &ALCdevice::ProcessHrtf;
  764. return;
  765. }
  766. device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  767. no_hrtf:
  768. if(old_hrtf)
  769. old_hrtf->DecRef();
  770. old_hrtf = nullptr;
  771. device->mRenderMode = StereoPair;
  772. if(device->Type != Loopback)
  773. {
  774. if(auto cflevopt = ConfigValueInt(device->DeviceName.c_str(), nullptr, "cf_level"))
  775. {
  776. if(*cflevopt > 0 && *cflevopt <= 6)
  777. {
  778. device->Bs2b = al::make_unique<bs2b>();
  779. bs2b_set_params(device->Bs2b.get(), *cflevopt,
  780. static_cast<int>(device->Frequency));
  781. TRACE("BS2B enabled\n");
  782. InitPanning(device);
  783. device->PostProcess = &ALCdevice::ProcessBs2b;
  784. return;
  785. }
  786. }
  787. }
  788. if(auto encopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-encoding"))
  789. {
  790. const char *mode{encopt->c_str()};
  791. if(al::strcasecmp(mode, "uhj") == 0)
  792. device->mRenderMode = NormalRender;
  793. else if(al::strcasecmp(mode, "panpot") != 0)
  794. ERR("Unexpected stereo-encoding: %s\n", mode);
  795. }
  796. if(device->mRenderMode == NormalRender)
  797. {
  798. device->Uhj_Encoder = al::make_unique<Uhj2Encoder>();
  799. TRACE("UHJ enabled\n");
  800. InitUhjPanning(device);
  801. device->PostProcess = &ALCdevice::ProcessUhj;
  802. return;
  803. }
  804. TRACE("Stereo rendering\n");
  805. InitPanning(device);
  806. device->PostProcess = &ALCdevice::ProcessAmbiDec;
  807. }
  808. void aluInitEffectPanning(ALeffectslot *slot, ALCdevice *device)
  809. {
  810. const size_t count{AmbiChannelsFromOrder(device->mAmbiOrder)};
  811. slot->MixBuffer.resize(count);
  812. slot->MixBuffer.shrink_to_fit();
  813. auto acnmap_end = AmbiIndex::From3D.begin() + count;
  814. auto iter = std::transform(AmbiIndex::From3D.begin(), acnmap_end, slot->Wet.AmbiMap.begin(),
  815. [](const uint8_t &acn) noexcept -> BFChannelConfig
  816. { return BFChannelConfig{1.0f, acn}; }
  817. );
  818. std::fill(iter, slot->Wet.AmbiMap.end(), BFChannelConfig{});
  819. slot->Wet.Buffer = {slot->MixBuffer.data(), slot->MixBuffer.size()};
  820. }
  821. void CalcAmbiCoeffs(const float y, const float z, const float x, const float spread,
  822. const al::span<float,MAX_AMBI_CHANNELS> coeffs)
  823. {
  824. /* Zeroth-order */
  825. coeffs[0] = 1.0f; /* ACN 0 = 1 */
  826. /* First-order */
  827. coeffs[1] = 1.732050808f * y; /* ACN 1 = sqrt(3) * Y */
  828. coeffs[2] = 1.732050808f * z; /* ACN 2 = sqrt(3) * Z */
  829. coeffs[3] = 1.732050808f * x; /* ACN 3 = sqrt(3) * X */
  830. /* Second-order */
  831. coeffs[4] = 3.872983346f * x * y; /* ACN 4 = sqrt(15) * X * Y */
  832. coeffs[5] = 3.872983346f * y * z; /* ACN 5 = sqrt(15) * Y * Z */
  833. coeffs[6] = 1.118033989f * (z*z*3.0f - 1.0f); /* ACN 6 = sqrt(5)/2 * (3*Z*Z - 1) */
  834. coeffs[7] = 3.872983346f * x * z; /* ACN 7 = sqrt(15) * X * Z */
  835. coeffs[8] = 1.936491673f * (x*x - y*y); /* ACN 8 = sqrt(15)/2 * (X*X - Y*Y) */
  836. /* Third-order */
  837. coeffs[9] = 2.091650066f * y * (x*x*3.0f - y*y); /* ACN 9 = sqrt(35/8) * Y * (3*X*X - Y*Y) */
  838. coeffs[10] = 10.246950766f * z * x * y; /* ACN 10 = sqrt(105) * Z * X * Y */
  839. coeffs[11] = 1.620185175f * y * (z*z*5.0f - 1.0f); /* ACN 11 = sqrt(21/8) * Y * (5*Z*Z - 1) */
  840. coeffs[12] = 1.322875656f * z * (z*z*5.0f - 3.0f); /* ACN 12 = sqrt(7)/2 * Z * (5*Z*Z - 3) */
  841. coeffs[13] = 1.620185175f * x * (z*z*5.0f - 1.0f); /* ACN 13 = sqrt(21/8) * X * (5*Z*Z - 1) */
  842. coeffs[14] = 5.123475383f * z * (x*x - y*y); /* ACN 14 = sqrt(105)/2 * Z * (X*X - Y*Y) */
  843. coeffs[15] = 2.091650066f * x * (x*x - y*y*3.0f); /* ACN 15 = sqrt(35/8) * X * (X*X - 3*Y*Y) */
  844. /* Fourth-order */
  845. /* ACN 16 = sqrt(35)*3/2 * X * Y * (X*X - Y*Y) */
  846. /* ACN 17 = sqrt(35/2)*3/2 * (3*X*X - Y*Y) * Y * Z */
  847. /* ACN 18 = sqrt(5)*3/2 * X * Y * (7*Z*Z - 1) */
  848. /* ACN 19 = sqrt(5/2)*3/2 * Y * Z * (7*Z*Z - 3) */
  849. /* ACN 20 = 3/8 * (35*Z*Z*Z*Z - 30*Z*Z + 3) */
  850. /* ACN 21 = sqrt(5/2)*3/2 * X * Z * (7*Z*Z - 3) */
  851. /* ACN 22 = sqrt(5)*3/4 * (X*X - Y*Y) * (7*Z*Z - 1) */
  852. /* ACN 23 = sqrt(35/2)*3/2 * (X*X - 3*Y*Y) * X * Z */
  853. /* ACN 24 = sqrt(35)*3/8 * (X*X*X*X - 6*X*X*Y*Y + Y*Y*Y*Y) */
  854. if(spread > 0.0f)
  855. {
  856. /* Implement the spread by using a spherical source that subtends the
  857. * angle spread. See:
  858. * http://www.ppsloan.org/publications/StupidSH36.pdf - Appendix A3
  859. *
  860. * When adjusted for N3D normalization instead of SN3D, these
  861. * calculations are:
  862. *
  863. * ZH0 = -sqrt(pi) * (-1+ca);
  864. * ZH1 = 0.5*sqrt(pi) * sa*sa;
  865. * ZH2 = -0.5*sqrt(pi) * ca*(-1+ca)*(ca+1);
  866. * ZH3 = -0.125*sqrt(pi) * (-1+ca)*(ca+1)*(5*ca*ca - 1);
  867. * ZH4 = -0.125*sqrt(pi) * ca*(-1+ca)*(ca+1)*(7*ca*ca - 3);
  868. * ZH5 = -0.0625*sqrt(pi) * (-1+ca)*(ca+1)*(21*ca*ca*ca*ca - 14*ca*ca + 1);
  869. *
  870. * The gain of the source is compensated for size, so that the
  871. * loudness doesn't depend on the spread. Thus:
  872. *
  873. * ZH0 = 1.0f;
  874. * ZH1 = 0.5f * (ca+1.0f);
  875. * ZH2 = 0.5f * (ca+1.0f)*ca;
  876. * ZH3 = 0.125f * (ca+1.0f)*(5.0f*ca*ca - 1.0f);
  877. * ZH4 = 0.125f * (ca+1.0f)*(7.0f*ca*ca - 3.0f)*ca;
  878. * ZH5 = 0.0625f * (ca+1.0f)*(21.0f*ca*ca*ca*ca - 14.0f*ca*ca + 1.0f);
  879. */
  880. const float ca{std::cos(spread * 0.5f)};
  881. /* Increase the source volume by up to +3dB for a full spread. */
  882. const float scale{std::sqrt(1.0f + spread/al::MathDefs<float>::Tau())};
  883. const float ZH0_norm{scale};
  884. const float ZH1_norm{scale * 0.5f * (ca+1.f)};
  885. const float ZH2_norm{scale * 0.5f * (ca+1.f)*ca};
  886. const float ZH3_norm{scale * 0.125f * (ca+1.f)*(5.f*ca*ca-1.f)};
  887. /* Zeroth-order */
  888. coeffs[0] *= ZH0_norm;
  889. /* First-order */
  890. coeffs[1] *= ZH1_norm;
  891. coeffs[2] *= ZH1_norm;
  892. coeffs[3] *= ZH1_norm;
  893. /* Second-order */
  894. coeffs[4] *= ZH2_norm;
  895. coeffs[5] *= ZH2_norm;
  896. coeffs[6] *= ZH2_norm;
  897. coeffs[7] *= ZH2_norm;
  898. coeffs[8] *= ZH2_norm;
  899. /* Third-order */
  900. coeffs[9] *= ZH3_norm;
  901. coeffs[10] *= ZH3_norm;
  902. coeffs[11] *= ZH3_norm;
  903. coeffs[12] *= ZH3_norm;
  904. coeffs[13] *= ZH3_norm;
  905. coeffs[14] *= ZH3_norm;
  906. coeffs[15] *= ZH3_norm;
  907. }
  908. }
  909. void ComputePanGains(const MixParams *mix, const float*RESTRICT coeffs, const float ingain,
  910. const al::span<float,MAX_OUTPUT_CHANNELS> gains)
  911. {
  912. auto ambimap = mix->AmbiMap.cbegin();
  913. auto iter = std::transform(ambimap, ambimap+mix->Buffer.size(), gains.begin(),
  914. [coeffs,ingain](const BFChannelConfig &chanmap) noexcept -> float
  915. { return chanmap.Scale * coeffs[chanmap.Index] * ingain; }
  916. );
  917. std::fill(iter, gains.end(), 0.0f);
  918. }