voice.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. #include "config.h"
  2. #include "config_simd.h"
  3. #include "voice.h"
  4. #include <algorithm>
  5. #include <array>
  6. #include <atomic>
  7. #include <cassert>
  8. #include <climits>
  9. #include <cstdint>
  10. #include <cstdlib>
  11. #include <iterator>
  12. #include <memory>
  13. #include <new>
  14. #include <optional>
  15. #include <utility>
  16. #include <vector>
  17. #include "alnumeric.h"
  18. #include "alspan.h"
  19. #include "alstring.h"
  20. #include "ambidefs.h"
  21. #include "async_event.h"
  22. #include "buffer_storage.h"
  23. #include "context.h"
  24. #include "cpu_caps.h"
  25. #include "devformat.h"
  26. #include "device.h"
  27. #include "filters/biquad.h"
  28. #include "filters/nfc.h"
  29. #include "filters/splitter.h"
  30. #include "fmt_traits.h"
  31. #include "logging.h"
  32. #include "mixer.h"
  33. #include "mixer/defs.h"
  34. #include "mixer/hrtfdefs.h"
  35. #include "opthelpers.h"
  36. #include "resampler_limits.h"
  37. #include "ringbuffer.h"
  38. #include "vector.h"
  39. #include "voice_change.h"
  40. struct CTag;
  41. #if HAVE_SSE
  42. struct SSETag;
  43. #endif
  44. #if HAVE_NEON
  45. struct NEONTag;
  46. #endif
  47. static_assert(!(DeviceBase::MixerLineSize&3), "MixerLineSize must be a multiple of 4");
  48. static_assert(!(MaxResamplerEdge&3), "MaxResamplerEdge is not a multiple of 4");
  49. static_assert((BufferLineSize-1)/MaxPitch > 0, "MaxPitch is too large for BufferLineSize!");
  50. static_assert((INT_MAX>>MixerFracBits)/MaxPitch > BufferLineSize,
  51. "MaxPitch and/or BufferLineSize are too large for MixerFracBits!");
  52. namespace {
  53. using uint = unsigned int;
  54. using namespace std::chrono;
  55. using namespace std::string_view_literals;
  56. using HrtfMixerFunc = void(*)(const al::span<const float> InSamples,
  57. const al::span<float2> AccumSamples, const uint IrSize, const MixHrtfFilter *hrtfparams,
  58. const size_t SamplesToDo);
  59. using HrtfMixerBlendFunc = void(*)(const al::span<const float> InSamples,
  60. const al::span<float2> AccumSamples, const uint IrSize, const HrtfFilter *oldparams,
  61. const MixHrtfFilter *newparams, const size_t SamplesToDo);
  62. HrtfMixerFunc MixHrtfSamples{MixHrtf_<CTag>};
  63. HrtfMixerBlendFunc MixHrtfBlendSamples{MixHrtfBlend_<CTag>};
  64. inline MixerOutFunc SelectMixer()
  65. {
  66. #if HAVE_NEON
  67. if((CPUCapFlags&CPU_CAP_NEON))
  68. return Mix_<NEONTag>;
  69. #endif
  70. #if HAVE_SSE
  71. if((CPUCapFlags&CPU_CAP_SSE))
  72. return Mix_<SSETag>;
  73. #endif
  74. return Mix_<CTag>;
  75. }
  76. inline MixerOneFunc SelectMixerOne()
  77. {
  78. #if HAVE_NEON
  79. if((CPUCapFlags&CPU_CAP_NEON))
  80. return Mix_<NEONTag>;
  81. #endif
  82. #if HAVE_SSE
  83. if((CPUCapFlags&CPU_CAP_SSE))
  84. return Mix_<SSETag>;
  85. #endif
  86. return Mix_<CTag>;
  87. }
  88. inline HrtfMixerFunc SelectHrtfMixer()
  89. {
  90. #if HAVE_NEON
  91. if((CPUCapFlags&CPU_CAP_NEON))
  92. return MixHrtf_<NEONTag>;
  93. #endif
  94. #if HAVE_SSE
  95. if((CPUCapFlags&CPU_CAP_SSE))
  96. return MixHrtf_<SSETag>;
  97. #endif
  98. return MixHrtf_<CTag>;
  99. }
  100. inline HrtfMixerBlendFunc SelectHrtfBlendMixer()
  101. {
  102. #if HAVE_NEON
  103. if((CPUCapFlags&CPU_CAP_NEON))
  104. return MixHrtfBlend_<NEONTag>;
  105. #endif
  106. #if HAVE_SSE
  107. if((CPUCapFlags&CPU_CAP_SSE))
  108. return MixHrtfBlend_<SSETag>;
  109. #endif
  110. return MixHrtfBlend_<CTag>;
  111. }
  112. } // namespace
  113. void Voice::InitMixer(std::optional<std::string> resopt)
  114. {
  115. if(resopt)
  116. {
  117. struct ResamplerEntry {
  118. const std::string_view name;
  119. const Resampler resampler;
  120. };
  121. constexpr std::array ResamplerList{
  122. ResamplerEntry{"none"sv, Resampler::Point},
  123. ResamplerEntry{"point"sv, Resampler::Point},
  124. ResamplerEntry{"linear"sv, Resampler::Linear},
  125. ResamplerEntry{"spline"sv, Resampler::Spline},
  126. ResamplerEntry{"gaussian"sv, Resampler::Gaussian},
  127. ResamplerEntry{"bsinc12"sv, Resampler::BSinc12},
  128. ResamplerEntry{"fast_bsinc12"sv, Resampler::FastBSinc12},
  129. ResamplerEntry{"bsinc24"sv, Resampler::BSinc24},
  130. ResamplerEntry{"fast_bsinc24"sv, Resampler::FastBSinc24},
  131. ResamplerEntry{"bsinc48"sv, Resampler::BSinc48},
  132. ResamplerEntry{"fast_bsinc48"sv, Resampler::FastBSinc48},
  133. };
  134. std::string_view resampler{*resopt};
  135. if (al::case_compare(resampler, "cubic"sv) == 0)
  136. {
  137. WARN("Resampler option \"{}\" is deprecated, using spline", *resopt);
  138. resampler = "spline"sv;
  139. }
  140. else if(al::case_compare(resampler, "sinc4"sv) == 0
  141. || al::case_compare(resampler, "sinc8"sv) == 0)
  142. {
  143. WARN("Resampler option \"{}\" is deprecated, using gaussian", *resopt);
  144. resampler = "gaussian"sv;
  145. }
  146. else if(al::case_compare(resampler, "bsinc"sv) == 0)
  147. {
  148. WARN("Resampler option \"{}\" is deprecated, using bsinc12", *resopt);
  149. resampler = "bsinc12"sv;
  150. }
  151. auto iter = std::find_if(ResamplerList.begin(), ResamplerList.end(),
  152. [resampler](const ResamplerEntry &entry) -> bool
  153. { return al::case_compare(resampler, entry.name) == 0; });
  154. if(iter == ResamplerList.end())
  155. ERR("Invalid resampler: {}", *resopt);
  156. else
  157. ResamplerDefault = iter->resampler;
  158. }
  159. MixSamplesOut = SelectMixer();
  160. MixSamplesOne = SelectMixerOne();
  161. MixHrtfBlendSamples = SelectHrtfBlendMixer();
  162. MixHrtfSamples = SelectHrtfMixer();
  163. }
  164. namespace {
  165. /* IMA ADPCM Stepsize table */
  166. constexpr std::array<int,89> IMAStep_size{{
  167. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
  168. 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
  169. 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
  170. 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
  171. 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
  172. 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
  173. 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
  174. 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
  175. 32767
  176. }};
  177. /* IMA4 ADPCM Codeword decode table */
  178. constexpr std::array<int,16> IMA4Codeword{{
  179. 1, 3, 5, 7, 9, 11, 13, 15,
  180. -1,-3,-5,-7,-9,-11,-13,-15,
  181. }};
  182. /* IMA4 ADPCM Step index adjust decode table */
  183. constexpr std::array<int,16> IMA4Index_adjust{{
  184. -1,-1,-1,-1, 2, 4, 6, 8,
  185. -1,-1,-1,-1, 2, 4, 6, 8
  186. }};
  187. /* MSADPCM Adaption table */
  188. constexpr std::array<int,16> MSADPCMAdaption{{
  189. 230, 230, 230, 230, 307, 409, 512, 614,
  190. 768, 614, 512, 409, 307, 230, 230, 230
  191. }};
  192. /* MSADPCM Adaption Coefficient tables */
  193. constexpr std::array MSADPCMAdaptionCoeff{
  194. std::array{256, 0},
  195. std::array{512, -256},
  196. std::array{ 0, 0},
  197. std::array{192, 64},
  198. std::array{240, 0},
  199. std::array{460, -208},
  200. std::array{392, -232}
  201. };
  202. void SendSourceStoppedEvent(ContextBase *context, uint id)
  203. {
  204. RingBuffer *ring{context->mAsyncEvents.get()};
  205. auto evt_vec = ring->getWriteVector();
  206. if(evt_vec[0].len < 1) return;
  207. auto &evt = InitAsyncEvent<AsyncSourceStateEvent>(evt_vec[0].buf);
  208. evt.mId = id;
  209. evt.mState = AsyncSrcState::Stop;
  210. ring->writeAdvance(1);
  211. }
  212. al::span<const float> DoFilters(BiquadFilter &lpfilter, BiquadFilter &hpfilter,
  213. const al::span<float,BufferLineSize> dst, const al::span<const float> src, int type)
  214. {
  215. switch(type)
  216. {
  217. case AF_None:
  218. lpfilter.clear();
  219. hpfilter.clear();
  220. break;
  221. case AF_LowPass:
  222. lpfilter.process(src, dst);
  223. hpfilter.clear();
  224. return dst.first(src.size());
  225. case AF_HighPass:
  226. lpfilter.clear();
  227. hpfilter.process(src, dst);
  228. return dst.first(src.size());
  229. case AF_BandPass:
  230. DualBiquad{lpfilter, hpfilter}.process(src, dst);
  231. return dst.first(src.size());
  232. }
  233. return src;
  234. }
  235. template<FmtType Type>
  236. inline void LoadSamples(const al::span<float> dstSamples, const al::span<const std::byte> srcData,
  237. const size_t srcChan, const size_t srcOffset, const size_t srcStep,
  238. const size_t samplesPerBlock [[maybe_unused]]) noexcept
  239. {
  240. using TypeTraits = al::FmtTypeTraits<Type>;
  241. using SampleType = typename TypeTraits::Type;
  242. assert(srcChan < srcStep);
  243. auto converter = TypeTraits{};
  244. al::span<const SampleType> src{reinterpret_cast<const SampleType*>(srcData.data()),
  245. srcData.size()/sizeof(SampleType)};
  246. auto ssrc = src.cbegin() + ptrdiff_t(srcOffset*srcStep + srcChan);
  247. dstSamples.front() = converter(*ssrc);
  248. std::generate(dstSamples.begin()+1, dstSamples.end(), [&ssrc,srcStep,converter]
  249. {
  250. ssrc += ptrdiff_t(srcStep);
  251. return converter(*ssrc);
  252. });
  253. }
  254. template<>
  255. inline void LoadSamples<FmtIMA4>(al::span<float> dstSamples, al::span<const std::byte> src,
  256. const size_t srcChan, const size_t srcOffset, const size_t srcStep,
  257. const size_t samplesPerBlock) noexcept
  258. {
  259. static constexpr int MaxStepIndex{static_cast<int>(IMAStep_size.size()) - 1};
  260. assert(srcStep > 0 || srcStep <= 2);
  261. assert(srcChan < srcStep);
  262. assert(samplesPerBlock > 1);
  263. const size_t blockBytes{((samplesPerBlock-1)/2 + 4)*srcStep};
  264. /* Skip to the ADPCM block containing the srcOffset sample. */
  265. src = src.subspan(srcOffset/samplesPerBlock*blockBytes);
  266. /* Calculate how many samples need to be skipped in the block. */
  267. size_t skip{srcOffset % samplesPerBlock};
  268. /* NOTE: This could probably be optimized better. */
  269. auto dst = dstSamples.begin();
  270. while(dst != dstSamples.end())
  271. {
  272. /* Each IMA4 block starts with a signed 16-bit sample, and a signed(?)
  273. * 16-bit table index. The table index needs to be clamped.
  274. */
  275. auto prevSample = int(src[srcChan*4 + 0]) | (int(src[srcChan*4 + 1]) << 8);
  276. auto prevIndex = int(src[srcChan*4 + 2]) | (int(src[srcChan*4 + 3]) << 8);
  277. const auto nibbleData = src.subspan((srcStep+srcChan)*4);
  278. src = src.subspan(blockBytes);
  279. /* Sign-extend the 16-bit sample and index values. */
  280. prevSample = (prevSample^0x8000) - 32768;
  281. prevIndex = std::clamp((prevIndex^0x8000) - 32768, 0, MaxStepIndex);
  282. if(skip == 0)
  283. {
  284. *dst = static_cast<float>(prevSample) / 32768.0f;
  285. if(++dst == dstSamples.end()) return;
  286. }
  287. else
  288. --skip;
  289. /* The rest of the block is arranged as a series of nibbles, contained
  290. * in 4 *bytes* per channel interleaved. So every 8 nibbles we need to
  291. * skip 4 bytes per channel to get the next nibbles for this channel.
  292. */
  293. auto decode_nibble = [&prevSample,&prevIndex,srcStep,nibbleData](const size_t nibbleOffset)
  294. noexcept -> int
  295. {
  296. static constexpr auto NibbleMask = std::byte{0xf};
  297. const auto byteShift = (nibbleOffset&1) * 4;
  298. const auto wordOffset = (nibbleOffset>>1) & ~3_uz;
  299. const auto byteOffset = wordOffset*srcStep + ((nibbleOffset>>1)&3);
  300. const auto nibble = al::to_underlying((nibbleData[byteOffset]>>byteShift)&NibbleMask);
  301. prevSample += IMA4Codeword[nibble] * IMAStep_size[static_cast<uint>(prevIndex)] / 8;
  302. prevSample = std::clamp(prevSample, -32768, 32767);
  303. prevIndex += IMA4Index_adjust[nibble];
  304. prevIndex = std::clamp(prevIndex, 0, MaxStepIndex);
  305. return prevSample;
  306. };
  307. /* First, decode the samples that we need to skip in the block (will
  308. * always be less than the block size). They need to be decoded despite
  309. * being ignored for proper state on the remaining samples.
  310. */
  311. size_t nibbleOffset{0};
  312. const size_t startOffset{skip + 1};
  313. for(;skip;--skip)
  314. {
  315. std::ignore = decode_nibble(nibbleOffset);
  316. ++nibbleOffset;
  317. }
  318. /* Second, decode the rest of the block and write to the output, until
  319. * the end of the block or the end of output.
  320. */
  321. const auto todo = std::min(samplesPerBlock - startOffset,
  322. size_t(std::distance(dst, dstSamples.end())));
  323. dst = std::generate_n(dst, todo, [&]
  324. {
  325. const auto sample = decode_nibble(nibbleOffset);
  326. ++nibbleOffset;
  327. return static_cast<float>(sample) / 32768.0f;
  328. });
  329. }
  330. }
  331. template<>
  332. inline void LoadSamples<FmtMSADPCM>(al::span<float> dstSamples, al::span<const std::byte> src,
  333. const size_t srcChan, const size_t srcOffset, const size_t srcStep,
  334. const size_t samplesPerBlock) noexcept
  335. {
  336. assert(srcStep > 0 || srcStep <= 2);
  337. assert(srcChan < srcStep);
  338. assert(samplesPerBlock > 2);
  339. const size_t blockBytes{((samplesPerBlock-2)/2 + 7)*srcStep};
  340. src = src.subspan(srcOffset/samplesPerBlock*blockBytes);
  341. size_t skip{srcOffset % samplesPerBlock};
  342. auto dst = dstSamples.begin();
  343. while(dst != dstSamples.end())
  344. {
  345. /* Each MS ADPCM block starts with an 8-bit block predictor, used to
  346. * dictate how the two sample history values are mixed with the decoded
  347. * sample, and an initial signed 16-bit scaling value which scales the
  348. * nibble sample value. This is followed by the two initial 16-bit
  349. * sample history values.
  350. */
  351. const auto blockpred = std::min(uint8_t(src[srcChan]),
  352. uint8_t{MSADPCMAdaptionCoeff.size()-1});
  353. auto scale = int(src[srcStep + 2*srcChan + 0]) | (int(src[srcStep + 2*srcChan + 1]) << 8);
  354. auto sampleHistory = std::array{
  355. int(src[3*srcStep + 2*srcChan + 0]) | (int(src[3*srcStep + 2*srcChan + 1])<<8),
  356. int(src[5*srcStep + 2*srcChan + 0]) | (int(src[5*srcStep + 2*srcChan + 1])<<8)};
  357. const auto nibbleData = src.subspan(7*srcStep);
  358. src = src.subspan(blockBytes);
  359. const auto coeffs = al::span{MSADPCMAdaptionCoeff[blockpred]};
  360. scale = (scale^0x8000) - 32768;
  361. sampleHistory[0] = (sampleHistory[0]^0x8000) - 32768;
  362. sampleHistory[1] = (sampleHistory[1]^0x8000) - 32768;
  363. /* The second history sample is "older", so it's the first to be
  364. * written out.
  365. */
  366. if(skip == 0)
  367. {
  368. *dst = static_cast<float>(sampleHistory[1]) / 32768.0f;
  369. if(++dst == dstSamples.end()) return;
  370. *dst = static_cast<float>(sampleHistory[0]) / 32768.0f;
  371. if(++dst == dstSamples.end()) return;
  372. }
  373. else if(skip == 1)
  374. {
  375. --skip;
  376. *dst = static_cast<float>(sampleHistory[0]) / 32768.0f;
  377. if(++dst == dstSamples.end()) return;
  378. }
  379. else
  380. skip -= 2;
  381. /* The rest of the block is a series of nibbles, interleaved per-
  382. * channel.
  383. */
  384. auto decode_nibble = [&sampleHistory,&scale,coeffs,nibbleData](const size_t nibbleOffset)
  385. noexcept -> int
  386. {
  387. static constexpr auto NibbleMask = std::byte{0xf};
  388. const auto byteOffset = nibbleOffset>>1;
  389. const auto byteShift = ((nibbleOffset&1)^1) * 4;
  390. const auto nibble = al::to_underlying((nibbleData[byteOffset]>>byteShift)&NibbleMask);
  391. const auto pred = ((nibble^0x08) - 0x08) * scale;
  392. const auto diff = (sampleHistory[0]*coeffs[0] + sampleHistory[1]*coeffs[1]) / 256;
  393. const auto sample = std::clamp(pred + diff, -32768, 32767);
  394. sampleHistory[1] = sampleHistory[0];
  395. sampleHistory[0] = sample;
  396. scale = MSADPCMAdaption[nibble] * scale / 256;
  397. scale = std::max(16, scale);
  398. return sample;
  399. };
  400. /* First, skip samples. */
  401. const size_t startOffset{skip + 2};
  402. size_t nibbleOffset{srcChan};
  403. for(;skip;--skip)
  404. {
  405. std::ignore = decode_nibble(nibbleOffset);
  406. nibbleOffset += srcStep;
  407. }
  408. /* Now decode the rest of the block, until the end of the block or the
  409. * dst buffer is filled.
  410. */
  411. const auto todo = std::min(samplesPerBlock - startOffset,
  412. size_t(std::distance(dst, dstSamples.end())));
  413. dst = std::generate_n(dst, todo, [&]
  414. {
  415. const auto sample = decode_nibble(nibbleOffset);
  416. nibbleOffset += srcStep;
  417. return static_cast<float>(sample) / 32768.0f;
  418. });
  419. }
  420. }
  421. void LoadSamples(const al::span<float> dstSamples, const al::span<const std::byte> src,
  422. const size_t srcChan, const size_t srcOffset, const FmtType srcType, const size_t srcStep,
  423. const size_t samplesPerBlock) noexcept
  424. {
  425. #define HANDLE_FMT(T) case T: \
  426. LoadSamples<T>(dstSamples, src, srcChan, srcOffset, srcStep, \
  427. samplesPerBlock); \
  428. break
  429. switch(srcType)
  430. {
  431. HANDLE_FMT(FmtUByte);
  432. HANDLE_FMT(FmtShort);
  433. HANDLE_FMT(FmtInt);
  434. HANDLE_FMT(FmtFloat);
  435. HANDLE_FMT(FmtDouble);
  436. HANDLE_FMT(FmtMulaw);
  437. HANDLE_FMT(FmtAlaw);
  438. HANDLE_FMT(FmtIMA4);
  439. HANDLE_FMT(FmtMSADPCM);
  440. }
  441. #undef HANDLE_FMT
  442. }
  443. void LoadBufferStatic(VoiceBufferItem *buffer, VoiceBufferItem *bufferLoopItem,
  444. const size_t dataPosInt, const FmtType sampleType, const size_t srcChannel,
  445. const size_t srcStep, al::span<float> voiceSamples)
  446. {
  447. if(!bufferLoopItem)
  448. {
  449. float lastSample{0.0f};
  450. /* Load what's left to play from the buffer */
  451. if(buffer->mSampleLen > dataPosInt) LIKELY
  452. {
  453. const size_t buffer_remaining{buffer->mSampleLen - dataPosInt};
  454. const size_t remaining{std::min(voiceSamples.size(), buffer_remaining)};
  455. LoadSamples(voiceSamples.first(remaining), buffer->mSamples, srcChannel, dataPosInt,
  456. sampleType, srcStep, buffer->mBlockAlign);
  457. lastSample = voiceSamples[remaining-1];
  458. voiceSamples = voiceSamples.subspan(remaining);
  459. }
  460. if(const size_t toFill{voiceSamples.size()})
  461. std::fill_n(voiceSamples.begin(), toFill, lastSample);
  462. }
  463. else
  464. {
  465. const size_t loopStart{buffer->mLoopStart};
  466. const size_t loopEnd{buffer->mLoopEnd};
  467. ASSUME(loopEnd > loopStart);
  468. const size_t intPos{(dataPosInt < loopEnd) ? dataPosInt
  469. : (((dataPosInt-loopStart)%(loopEnd-loopStart)) + loopStart)};
  470. /* Load what's left of this loop iteration */
  471. const size_t remaining{std::min(voiceSamples.size(), loopEnd-dataPosInt)};
  472. LoadSamples(voiceSamples.first(remaining), buffer->mSamples, srcChannel, intPos,
  473. sampleType, srcStep, buffer->mBlockAlign);
  474. voiceSamples = voiceSamples.subspan(remaining);
  475. /* Load repeats of the loop to fill the buffer. */
  476. const size_t loopSize{loopEnd - loopStart};
  477. while(const size_t toFill{std::min(voiceSamples.size(), loopSize)})
  478. {
  479. LoadSamples(voiceSamples.first(toFill), buffer->mSamples, srcChannel, loopStart,
  480. sampleType, srcStep, buffer->mBlockAlign);
  481. voiceSamples = voiceSamples.subspan(toFill);
  482. }
  483. }
  484. }
  485. void LoadBufferCallback(VoiceBufferItem *buffer, const size_t dataPosInt,
  486. const size_t numCallbackSamples, const FmtType sampleType, const size_t srcChannel,
  487. const size_t srcStep, al::span<float> voiceSamples)
  488. {
  489. float lastSample{0.0f};
  490. if(numCallbackSamples > dataPosInt) LIKELY
  491. {
  492. const size_t remaining{std::min(voiceSamples.size(), numCallbackSamples-dataPosInt)};
  493. LoadSamples(voiceSamples.first(remaining), buffer->mSamples, srcChannel, dataPosInt,
  494. sampleType, srcStep, buffer->mBlockAlign);
  495. lastSample = voiceSamples[remaining-1];
  496. voiceSamples = voiceSamples.subspan(remaining);
  497. }
  498. if(const size_t toFill{voiceSamples.size()})
  499. std::fill_n(voiceSamples.begin(), toFill, lastSample);
  500. }
  501. void LoadBufferQueue(VoiceBufferItem *buffer, VoiceBufferItem *bufferLoopItem,
  502. size_t dataPosInt, const FmtType sampleType, const size_t srcChannel,
  503. const size_t srcStep, al::span<float> voiceSamples)
  504. {
  505. float lastSample{0.0f};
  506. /* Crawl the buffer queue to fill in the temp buffer */
  507. while(buffer && !voiceSamples.empty())
  508. {
  509. if(dataPosInt >= buffer->mSampleLen)
  510. {
  511. dataPosInt -= buffer->mSampleLen;
  512. buffer = buffer->mNext.load(std::memory_order_acquire);
  513. if(!buffer) buffer = bufferLoopItem;
  514. continue;
  515. }
  516. const size_t remaining{std::min(voiceSamples.size(), buffer->mSampleLen-dataPosInt)};
  517. LoadSamples(voiceSamples.first(remaining), buffer->mSamples, srcChannel, dataPosInt,
  518. sampleType, srcStep, buffer->mBlockAlign);
  519. lastSample = voiceSamples[remaining-1];
  520. voiceSamples = voiceSamples.subspan(remaining);
  521. if(voiceSamples.empty())
  522. break;
  523. dataPosInt = 0;
  524. buffer = buffer->mNext.load(std::memory_order_acquire);
  525. if(!buffer) buffer = bufferLoopItem;
  526. }
  527. if(const size_t toFill{voiceSamples.size()})
  528. std::fill_n(voiceSamples.begin(), toFill, lastSample);
  529. }
  530. void DoHrtfMix(const al::span<const float> samples, DirectParams &parms, const float TargetGain,
  531. const size_t Counter, size_t OutPos, const bool IsPlaying, DeviceBase *Device)
  532. {
  533. const uint IrSize{Device->mIrSize};
  534. const auto HrtfSamples = al::span{Device->ExtraSampleData};
  535. const auto AccumSamples = al::span{Device->HrtfAccumData};
  536. /* Copy the HRTF history and new input samples into a temp buffer. */
  537. auto src_iter = std::copy(parms.Hrtf.History.begin(), parms.Hrtf.History.end(),
  538. HrtfSamples.begin());
  539. std::copy_n(samples.begin(), samples.size(), src_iter);
  540. /* Copy the last used samples back into the history buffer for later. */
  541. if(IsPlaying) LIKELY
  542. {
  543. const auto endsamples = HrtfSamples.subspan(samples.size(), parms.Hrtf.History.size());
  544. std::copy_n(endsamples.cbegin(), endsamples.size(), parms.Hrtf.History.begin());
  545. }
  546. /* If fading and this is the first mixing pass, fade between the IRs. */
  547. size_t fademix{0};
  548. if(Counter && OutPos == 0)
  549. {
  550. fademix = std::min(samples.size(), Counter);
  551. float gain{TargetGain};
  552. /* The new coefficients need to fade in completely since they're
  553. * replacing the old ones. To keep the gain fading consistent,
  554. * interpolate between the old and new target gains given how much of
  555. * the fade time this mix handles.
  556. */
  557. if(Counter > fademix)
  558. {
  559. const float a{static_cast<float>(fademix) / static_cast<float>(Counter)};
  560. gain = lerpf(parms.Hrtf.Old.Gain, TargetGain, a);
  561. }
  562. MixHrtfFilter hrtfparams{
  563. parms.Hrtf.Target.Coeffs,
  564. parms.Hrtf.Target.Delay,
  565. 0.0f, gain / static_cast<float>(fademix)};
  566. MixHrtfBlendSamples(HrtfSamples, AccumSamples.subspan(OutPos), IrSize, &parms.Hrtf.Old,
  567. &hrtfparams, fademix);
  568. /* Update the old parameters with the result. */
  569. parms.Hrtf.Old = parms.Hrtf.Target;
  570. parms.Hrtf.Old.Gain = gain;
  571. OutPos += fademix;
  572. }
  573. if(fademix < samples.size())
  574. {
  575. const size_t todo{samples.size() - fademix};
  576. float gain{TargetGain};
  577. /* Interpolate the target gain if the gain fading lasts longer than
  578. * this mix.
  579. */
  580. if(Counter > samples.size())
  581. {
  582. const float a{static_cast<float>(todo) / static_cast<float>(Counter-fademix)};
  583. gain = lerpf(parms.Hrtf.Old.Gain, TargetGain, a);
  584. }
  585. MixHrtfFilter hrtfparams{
  586. parms.Hrtf.Target.Coeffs,
  587. parms.Hrtf.Target.Delay,
  588. parms.Hrtf.Old.Gain,
  589. (gain - parms.Hrtf.Old.Gain) / static_cast<float>(todo)};
  590. MixHrtfSamples(HrtfSamples.subspan(fademix), AccumSamples.subspan(OutPos), IrSize,
  591. &hrtfparams, todo);
  592. /* Store the now-current gain for next time. */
  593. parms.Hrtf.Old.Gain = gain;
  594. }
  595. }
  596. void DoNfcMix(const al::span<const float> samples, al::span<FloatBufferLine> OutBuffer,
  597. DirectParams &parms, const al::span<const float,MaxOutputChannels> OutGains,
  598. const uint Counter, const uint OutPos, DeviceBase *Device)
  599. {
  600. using FilterProc = void (NfcFilter::*)(const al::span<const float>, const al::span<float>);
  601. static constexpr std::array<FilterProc,MaxAmbiOrder+1> NfcProcess{{
  602. nullptr, &NfcFilter::process1, &NfcFilter::process2, &NfcFilter::process3}};
  603. MixSamples(samples, al::span{OutBuffer[0]}.subspan(OutPos), parms.Gains.Current[0],
  604. OutGains[0], Counter);
  605. OutBuffer = OutBuffer.subspan(1);
  606. auto CurrentGains = al::span{parms.Gains.Current}.subspan(1);
  607. auto TargetGains = OutGains.subspan(1);
  608. const auto nfcsamples = al::span{Device->ExtraSampleData}.first(samples.size());
  609. size_t order{1};
  610. while(const size_t chancount{Device->NumChannelsPerOrder[order]})
  611. {
  612. (parms.NFCtrlFilter.*NfcProcess[order])(samples, nfcsamples);
  613. MixSamples(nfcsamples, OutBuffer.first(chancount), CurrentGains, TargetGains, Counter,
  614. OutPos);
  615. if(++order == MaxAmbiOrder+1)
  616. break;
  617. OutBuffer = OutBuffer.subspan(chancount);
  618. CurrentGains = CurrentGains.subspan(chancount);
  619. TargetGains = TargetGains.subspan(chancount);
  620. }
  621. }
  622. } // namespace
  623. void Voice::mix(const State vstate, ContextBase *Context, const nanoseconds deviceTime,
  624. const uint SamplesToDo)
  625. {
  626. static constexpr std::array<float,MaxOutputChannels> SilentTarget{};
  627. ASSUME(SamplesToDo > 0);
  628. DeviceBase *Device{Context->mDevice};
  629. const uint NumSends{Device->NumAuxSends};
  630. /* Get voice info */
  631. int DataPosInt{mPosition.load(std::memory_order_relaxed)};
  632. uint DataPosFrac{mPositionFrac.load(std::memory_order_relaxed)};
  633. VoiceBufferItem *BufferListItem{mCurrentBuffer.load(std::memory_order_relaxed)};
  634. VoiceBufferItem *BufferLoopItem{mLoopBuffer.load(std::memory_order_relaxed)};
  635. const uint increment{mStep};
  636. if(increment < 1) UNLIKELY
  637. {
  638. /* If the voice is supposed to be stopping but can't be mixed, just
  639. * stop it before bailing.
  640. */
  641. if(vstate == Stopping)
  642. mPlayState.store(Stopped, std::memory_order_release);
  643. return;
  644. }
  645. /* If the static voice's current position is beyond the buffer loop end
  646. * position, disable looping.
  647. */
  648. if(mFlags.test(VoiceIsStatic) && BufferLoopItem)
  649. {
  650. if(DataPosInt >= 0 && static_cast<uint>(DataPosInt) >= BufferListItem->mLoopEnd)
  651. BufferLoopItem = nullptr;
  652. }
  653. uint OutPos{0u};
  654. /* Check if we're doing a delayed start, and we start in this update. */
  655. if(mStartTime > deviceTime) UNLIKELY
  656. {
  657. /* If the voice is supposed to be stopping but hasn't actually started
  658. * yet, make sure its stopped.
  659. */
  660. if(vstate == Stopping)
  661. {
  662. mPlayState.store(Stopped, std::memory_order_release);
  663. return;
  664. }
  665. /* If the start time is too far ahead, don't bother. */
  666. auto diff = mStartTime - deviceTime;
  667. if(diff >= seconds{1})
  668. return;
  669. /* Get the number of samples ahead of the current time that output
  670. * should start at. Skip this update if it's beyond the output sample
  671. * count.
  672. */
  673. OutPos = static_cast<uint>(round<seconds>(diff * Device->mSampleRate).count());
  674. if(OutPos >= SamplesToDo) return;
  675. }
  676. /* Calculate the number of samples to mix, and the number of (resampled)
  677. * samples that need to be loaded (mixing samples and decoder padding).
  678. */
  679. const uint samplesToMix{SamplesToDo - OutPos};
  680. const uint samplesToLoad{samplesToMix + mDecoderPadding};
  681. /* Get a span of pointers to hold the floating point, deinterlaced,
  682. * resampled buffer data to be mixed.
  683. */
  684. auto SamplePointers = std::array<float*,DeviceBase::MixerChannelsMax>{};
  685. const auto MixingSamples = al::span{SamplePointers}.first(mChans.size());
  686. {
  687. const uint channelStep{(samplesToLoad+3u)&~3u};
  688. auto base = Device->mSampleData.end() - MixingSamples.size()*channelStep;
  689. std::generate(MixingSamples.begin(), MixingSamples.end(), [&base,channelStep]
  690. {
  691. const auto ret = base;
  692. base += channelStep;
  693. return al::to_address(ret);
  694. });
  695. }
  696. /* UHJ2 and SuperStereo only have 2 buffer channels, but 3 mixing channels
  697. * (3rd channel is generated from decoding). MonoDup only has 1 buffer
  698. * channel, but 2 mixing channels (2nd channel is just duplicated).
  699. */
  700. const size_t realChannels{(mFmtChannels == FmtMonoDup) ? 1u
  701. : (mFmtChannels == FmtUHJ2 || mFmtChannels == FmtSuperStereo) ? 2u
  702. : MixingSamples.size()};
  703. for(size_t chan{0};chan < realChannels;++chan)
  704. {
  705. static constexpr uint ResBufSize{std::tuple_size_v<decltype(DeviceBase::mResampleData)>};
  706. static constexpr uint srcSizeMax{ResBufSize - MaxResamplerEdge};
  707. const al::span prevSamples{mPrevSamples[chan]};
  708. std::copy(prevSamples.cbegin(), prevSamples.cend(), Device->mResampleData.begin());
  709. const auto resampleBuffer = al::span{Device->mResampleData}.subspan<MaxResamplerEdge>();
  710. int intPos{DataPosInt};
  711. uint fracPos{DataPosFrac};
  712. /* Load samples for this channel from the available buffer(s), with
  713. * resampling.
  714. */
  715. for(uint samplesLoaded{0};samplesLoaded < samplesToLoad;)
  716. {
  717. /* Calculate the number of dst samples that can be loaded this
  718. * iteration, given the available resampler buffer size, and the
  719. * number of src samples that are needed to load it.
  720. */
  721. auto calc_buffer_sizes = [fracPos,increment](uint dstBufferSize)
  722. {
  723. /* If ext=true, calculate the last written dst pos from the dst
  724. * count, convert to the last read src pos, then add one to get
  725. * the src count.
  726. *
  727. * If ext=false, convert the dst count to src count directly.
  728. *
  729. * Without this, the src count could be short by one when
  730. * increment < 1.0, or not have a full src at the end when
  731. * increment > 1.0.
  732. */
  733. const bool ext{increment <= MixerFracOne};
  734. uint64_t dataSize64{dstBufferSize - ext};
  735. dataSize64 = (dataSize64*increment + fracPos) >> MixerFracBits;
  736. /* Also include resampler padding. */
  737. dataSize64 += ext + MaxResamplerEdge;
  738. if(dataSize64 <= srcSizeMax)
  739. return std::array{dstBufferSize, static_cast<uint>(dataSize64)};
  740. /* If the source size got saturated, we can't fill the desired
  741. * dst size. Figure out how many dst samples we can fill.
  742. */
  743. dataSize64 = srcSizeMax - MaxResamplerEdge;
  744. dataSize64 = ((dataSize64<<MixerFracBits) - fracPos) / increment;
  745. if(dataSize64 < dstBufferSize)
  746. {
  747. /* Some resamplers require the destination being 16-byte
  748. * aligned, so limit to a multiple of 4 samples to maintain
  749. * alignment if we need to do another iteration after this.
  750. */
  751. dstBufferSize = static_cast<uint>(dataSize64) & ~3u;
  752. }
  753. return std::array{dstBufferSize, srcSizeMax};
  754. };
  755. const auto [dstBufferSize, srcBufferSize] = calc_buffer_sizes(
  756. samplesToLoad - samplesLoaded);
  757. size_t srcSampleDelay{0};
  758. if(intPos < 0) UNLIKELY
  759. {
  760. /* If the current position is negative, there's that many
  761. * silent samples to load before using the buffer.
  762. */
  763. srcSampleDelay = static_cast<uint>(-intPos);
  764. if(srcSampleDelay >= srcBufferSize)
  765. {
  766. /* If the number of silent source samples exceeds the
  767. * number to load, the output will be silent.
  768. */
  769. std::fill_n(MixingSamples[chan]+samplesLoaded, dstBufferSize, 0.0f);
  770. std::fill_n(resampleBuffer.begin(), srcBufferSize, 0.0f);
  771. goto skip_resample;
  772. }
  773. std::fill_n(resampleBuffer.begin(), srcSampleDelay, 0.0f);
  774. }
  775. /* Load the necessary samples from the given buffer(s). */
  776. if(!BufferListItem) UNLIKELY
  777. {
  778. const uint avail{std::min(srcBufferSize, MaxResamplerEdge)};
  779. const uint tofill{std::max(srcBufferSize, MaxResamplerEdge)};
  780. const auto srcbuf = resampleBuffer.first(tofill);
  781. /* When loading from a voice that ended prematurely, only take
  782. * the samples that get closest to 0 amplitude. This helps
  783. * certain sounds fade out better.
  784. */
  785. auto srciter = std::min_element(srcbuf.begin(), srcbuf.begin()+ptrdiff_t(avail),
  786. [](const float l, const float r) { return std::abs(l) < std::abs(r); });
  787. std::fill(srciter+1, srcbuf.end(), *srciter);
  788. }
  789. else if(mFlags.test(VoiceIsStatic))
  790. {
  791. const auto uintPos = static_cast<uint>(std::max(intPos, 0));
  792. const auto bufferSamples = resampleBuffer.subspan(srcSampleDelay,
  793. srcBufferSize-srcSampleDelay);
  794. LoadBufferStatic(BufferListItem, BufferLoopItem, uintPos, mFmtType, chan,
  795. mFrameStep, bufferSamples);
  796. }
  797. else if(mFlags.test(VoiceIsCallback))
  798. {
  799. const auto uintPos = static_cast<uint>(std::max(intPos, 0));
  800. const uint callbackBase{mCallbackBlockBase * mSamplesPerBlock};
  801. const size_t bufferOffset{uintPos - callbackBase};
  802. const size_t needSamples{bufferOffset + srcBufferSize - srcSampleDelay};
  803. const size_t needBlocks{(needSamples + mSamplesPerBlock-1) / mSamplesPerBlock};
  804. if(!mFlags.test(VoiceCallbackStopped) && needBlocks > mNumCallbackBlocks)
  805. {
  806. const size_t byteOffset{mNumCallbackBlocks*size_t{mBytesPerBlock}};
  807. const size_t needBytes{(needBlocks-mNumCallbackBlocks)*size_t{mBytesPerBlock}};
  808. const int gotBytes{BufferListItem->mCallback(BufferListItem->mUserData,
  809. &BufferListItem->mSamples[byteOffset], static_cast<int>(needBytes))};
  810. if(gotBytes < 0)
  811. mFlags.set(VoiceCallbackStopped);
  812. else if(static_cast<uint>(gotBytes) < needBytes)
  813. {
  814. mFlags.set(VoiceCallbackStopped);
  815. mNumCallbackBlocks += static_cast<uint>(gotBytes) / mBytesPerBlock;
  816. }
  817. else
  818. mNumCallbackBlocks = static_cast<uint>(needBlocks);
  819. }
  820. const size_t numSamples{size_t{mNumCallbackBlocks} * mSamplesPerBlock};
  821. const auto bufferSamples = resampleBuffer.subspan(srcSampleDelay,
  822. srcBufferSize-srcSampleDelay);
  823. LoadBufferCallback(BufferListItem, bufferOffset, numSamples, mFmtType, chan,
  824. mFrameStep, bufferSamples);
  825. }
  826. else
  827. {
  828. const auto uintPos = static_cast<uint>(std::max(intPos, 0));
  829. const auto bufferSamples = resampleBuffer.subspan(srcSampleDelay,
  830. srcBufferSize-srcSampleDelay);
  831. LoadBufferQueue(BufferListItem, BufferLoopItem, uintPos, mFmtType, chan,
  832. mFrameStep, bufferSamples);
  833. }
  834. /* If there's a matching sample step and no phase offset, use a
  835. * simple copy for resampling.
  836. */
  837. if(increment == MixerFracOne && fracPos == 0)
  838. std::copy_n(resampleBuffer.cbegin(), dstBufferSize,
  839. MixingSamples[chan]+samplesLoaded);
  840. else
  841. mResampler(&mResampleState, Device->mResampleData, fracPos, increment,
  842. {MixingSamples[chan]+samplesLoaded, dstBufferSize});
  843. /* Store the last source samples used for next time. */
  844. if(vstate == Playing) LIKELY
  845. {
  846. /* Only store samples for the end of the mix, excluding what
  847. * gets loaded for decoder padding.
  848. */
  849. const uint loadEnd{samplesLoaded + dstBufferSize};
  850. if(samplesToMix > samplesLoaded && samplesToMix <= loadEnd) LIKELY
  851. {
  852. const size_t dstOffset{samplesToMix - samplesLoaded};
  853. const size_t srcOffset{(dstOffset*increment + fracPos) >> MixerFracBits};
  854. std::copy_n(Device->mResampleData.cbegin()+srcOffset, prevSamples.size(),
  855. prevSamples.begin());
  856. }
  857. }
  858. skip_resample:
  859. samplesLoaded += dstBufferSize;
  860. if(samplesLoaded < samplesToLoad)
  861. {
  862. fracPos += dstBufferSize*increment;
  863. const uint srcOffset{fracPos >> MixerFracBits};
  864. fracPos &= MixerFracMask;
  865. intPos += static_cast<int>(srcOffset);
  866. /* If more samples need to be loaded, copy the back of the
  867. * resampleBuffer to the front to reuse it. prevSamples isn't
  868. * reliable since it's only updated for the end of the mix.
  869. */
  870. std::copy_n(Device->mResampleData.cbegin()+srcOffset, MaxResamplerPadding,
  871. Device->mResampleData.begin());
  872. }
  873. }
  874. }
  875. if(mFmtChannels == FmtMonoDup)
  876. {
  877. /* NOTE: a mono source shouldn't have a decoder or the VoiceIsAmbisonic
  878. * flag, so aliasing instead of copying to the second channel shouldn't
  879. * be a problem.
  880. */
  881. MixingSamples[1] = MixingSamples[0];
  882. }
  883. else for(auto &samples : MixingSamples.subspan(realChannels))
  884. std::fill_n(samples, samplesToLoad, 0.0f);
  885. if(mDecoder)
  886. mDecoder->decode(MixingSamples, samplesToMix, (vstate==Playing));
  887. if(mFlags.test(VoiceIsAmbisonic))
  888. {
  889. auto voiceSamples = MixingSamples.begin();
  890. for(auto &chandata : mChans)
  891. {
  892. chandata.mAmbiSplitter.processScale({*voiceSamples, samplesToMix},
  893. chandata.mAmbiHFScale, chandata.mAmbiLFScale);
  894. ++voiceSamples;
  895. }
  896. }
  897. const uint Counter{mFlags.test(VoiceIsFading) ? std::min(samplesToMix, 64u) : 0u};
  898. if(!Counter)
  899. {
  900. /* No fading, just overwrite the old/current params. */
  901. for(auto &chandata : mChans)
  902. {
  903. {
  904. DirectParams &parms = chandata.mDryParams;
  905. if(!mFlags.test(VoiceHasHrtf))
  906. parms.Gains.Current = parms.Gains.Target;
  907. else
  908. parms.Hrtf.Old = parms.Hrtf.Target;
  909. }
  910. for(uint send{0};send < NumSends;++send)
  911. {
  912. if(mSend[send].Buffer.empty())
  913. continue;
  914. SendParams &parms = chandata.mWetParams[send];
  915. parms.Gains.Current = parms.Gains.Target;
  916. }
  917. }
  918. }
  919. auto voiceSamples = MixingSamples.begin();
  920. for(auto &chandata : mChans)
  921. {
  922. /* Now filter and mix to the appropriate outputs. */
  923. const al::span<float,BufferLineSize> FilterBuf{Device->FilteredData};
  924. {
  925. DirectParams &parms = chandata.mDryParams;
  926. const auto samples = DoFilters(parms.LowPass, parms.HighPass, FilterBuf,
  927. {*voiceSamples, samplesToMix}, mDirect.FilterType);
  928. if(mFlags.test(VoiceHasHrtf))
  929. {
  930. const float TargetGain{parms.Hrtf.Target.Gain * float(vstate == Playing)};
  931. DoHrtfMix(samples, parms, TargetGain, Counter, OutPos, (vstate == Playing),
  932. Device);
  933. }
  934. else
  935. {
  936. const auto TargetGains = (vstate == Playing) ? al::span{parms.Gains.Target}
  937. : al::span{SilentTarget};
  938. if(mFlags.test(VoiceHasNfc))
  939. DoNfcMix(samples, mDirect.Buffer, parms, TargetGains, Counter, OutPos, Device);
  940. else
  941. MixSamples(samples, mDirect.Buffer, parms.Gains.Current, TargetGains, Counter,
  942. OutPos);
  943. }
  944. }
  945. for(uint send{0};send < NumSends;++send)
  946. {
  947. if(mSend[send].Buffer.empty())
  948. continue;
  949. SendParams &parms = chandata.mWetParams[send];
  950. const auto samples = DoFilters(parms.LowPass, parms.HighPass, FilterBuf,
  951. {*voiceSamples, samplesToMix}, mSend[send].FilterType);
  952. const auto TargetGains = (vstate == Playing) ? al::span{parms.Gains.Target}
  953. : al::span{SilentTarget};
  954. MixSamples(samples, mSend[send].Buffer, parms.Gains.Current, TargetGains, Counter,
  955. OutPos);
  956. }
  957. ++voiceSamples;
  958. }
  959. mFlags.set(VoiceIsFading);
  960. /* Don't update positions and buffers if we were stopping. */
  961. if(vstate == Stopping) UNLIKELY
  962. {
  963. mPlayState.store(Stopped, std::memory_order_release);
  964. return;
  965. }
  966. /* Update voice positions and buffers as needed. */
  967. DataPosFrac += increment*samplesToMix;
  968. DataPosInt += static_cast<int>(DataPosFrac>>MixerFracBits);
  969. DataPosFrac &= MixerFracMask;
  970. uint buffers_done{0u};
  971. if(BufferListItem && DataPosInt > 0) LIKELY
  972. {
  973. if(mFlags.test(VoiceIsStatic))
  974. {
  975. if(BufferLoopItem)
  976. {
  977. /* Handle looping static source */
  978. const uint LoopStart{BufferListItem->mLoopStart};
  979. const uint LoopEnd{BufferListItem->mLoopEnd};
  980. uint DataPosUInt{static_cast<uint>(DataPosInt)};
  981. if(DataPosUInt >= LoopEnd)
  982. {
  983. assert(LoopEnd > LoopStart);
  984. DataPosUInt = ((DataPosUInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
  985. DataPosInt = static_cast<int>(DataPosUInt);
  986. }
  987. }
  988. else
  989. {
  990. /* Handle non-looping static source */
  991. if(static_cast<uint>(DataPosInt) >= BufferListItem->mSampleLen)
  992. BufferListItem = nullptr;
  993. }
  994. }
  995. else if(mFlags.test(VoiceIsCallback))
  996. {
  997. /* Handle callback buffer source */
  998. const uint currentBlock{static_cast<uint>(DataPosInt) / mSamplesPerBlock};
  999. const uint blocksDone{currentBlock - mCallbackBlockBase};
  1000. if(blocksDone < mNumCallbackBlocks)
  1001. {
  1002. const size_t byteOffset{blocksDone*size_t{mBytesPerBlock}};
  1003. const size_t byteEnd{mNumCallbackBlocks*size_t{mBytesPerBlock}};
  1004. const al::span data{BufferListItem->mSamples};
  1005. std::copy(data.cbegin()+ptrdiff_t(byteOffset), data.cbegin()+ptrdiff_t(byteEnd),
  1006. data.begin());
  1007. mNumCallbackBlocks -= blocksDone;
  1008. mCallbackBlockBase += blocksDone;
  1009. }
  1010. else
  1011. {
  1012. BufferListItem = nullptr;
  1013. mNumCallbackBlocks = 0;
  1014. mCallbackBlockBase += blocksDone;
  1015. }
  1016. }
  1017. else
  1018. {
  1019. /* Handle streaming source */
  1020. do {
  1021. if(BufferListItem->mSampleLen > static_cast<uint>(DataPosInt))
  1022. break;
  1023. DataPosInt -= static_cast<int>(BufferListItem->mSampleLen);
  1024. ++buffers_done;
  1025. BufferListItem = BufferListItem->mNext.load(std::memory_order_relaxed);
  1026. if(!BufferListItem) BufferListItem = BufferLoopItem;
  1027. } while(BufferListItem);
  1028. }
  1029. }
  1030. /* Capture the source ID in case it gets reset for stopping. */
  1031. const uint SourceID{mSourceID.load(std::memory_order_relaxed)};
  1032. /* Update voice info */
  1033. mPosition.store(DataPosInt, std::memory_order_relaxed);
  1034. mPositionFrac.store(DataPosFrac, std::memory_order_relaxed);
  1035. mCurrentBuffer.store(BufferListItem, std::memory_order_relaxed);
  1036. if(!BufferListItem)
  1037. {
  1038. mLoopBuffer.store(nullptr, std::memory_order_relaxed);
  1039. mSourceID.store(0u, std::memory_order_relaxed);
  1040. }
  1041. std::atomic_thread_fence(std::memory_order_release);
  1042. /* Send any events now, after the position/buffer info was updated. */
  1043. const auto enabledevt = Context->mEnabledEvts.load(std::memory_order_acquire);
  1044. if(buffers_done > 0 && enabledevt.test(al::to_underlying(AsyncEnableBits::BufferCompleted)))
  1045. {
  1046. RingBuffer *ring{Context->mAsyncEvents.get()};
  1047. auto evt_vec = ring->getWriteVector();
  1048. if(evt_vec[0].len > 0)
  1049. {
  1050. auto &evt = InitAsyncEvent<AsyncBufferCompleteEvent>(evt_vec[0].buf);
  1051. evt.mId = SourceID;
  1052. evt.mCount = buffers_done;
  1053. ring->writeAdvance(1);
  1054. }
  1055. }
  1056. if(!BufferListItem)
  1057. {
  1058. /* If the voice just ended, set it to Stopping so the next render
  1059. * ensures any residual noise fades to 0 amplitude.
  1060. */
  1061. mPlayState.store(Stopping, std::memory_order_release);
  1062. if(enabledevt.test(al::to_underlying(AsyncEnableBits::SourceState)))
  1063. SendSourceStoppedEvent(Context, SourceID);
  1064. }
  1065. }
  1066. void Voice::prepare(DeviceBase *device)
  1067. {
  1068. /* Even if storing really high order ambisonics, we only mix channels for
  1069. * orders up to the device order. The rest are simply dropped.
  1070. */
  1071. uint num_channels{(mFmtChannels == FmtMonoDup) ? 2
  1072. : (mFmtChannels == FmtUHJ2 || mFmtChannels == FmtSuperStereo) ? 3
  1073. : ChannelsFromFmt(mFmtChannels, std::min(mAmbiOrder, device->mAmbiOrder))};
  1074. if(num_channels > device->MixerChannelsMax) UNLIKELY
  1075. {
  1076. ERR("Unexpected channel count: {} (limit: {}, {} : {})", num_channels,
  1077. device->MixerChannelsMax, NameFromFormat(mFmtChannels), mAmbiOrder);
  1078. num_channels = device->MixerChannelsMax;
  1079. }
  1080. if(mChans.capacity() > 2 && num_channels < mChans.capacity())
  1081. {
  1082. decltype(mChans){}.swap(mChans);
  1083. decltype(mPrevSamples){}.swap(mPrevSamples);
  1084. }
  1085. mChans.reserve(std::max(2u, num_channels));
  1086. mChans.resize(num_channels);
  1087. mPrevSamples.reserve(std::max(2u, num_channels));
  1088. mPrevSamples.resize(num_channels);
  1089. mDecoder = nullptr;
  1090. mDecoderPadding = 0;
  1091. if(mFmtChannels == FmtSuperStereo)
  1092. {
  1093. switch(UhjDecodeQuality)
  1094. {
  1095. case UhjQualityType::IIR:
  1096. mDecoder = std::make_unique<UhjStereoDecoderIIR>();
  1097. mDecoderPadding = UhjStereoDecoderIIR::sInputPadding;
  1098. break;
  1099. case UhjQualityType::FIR256:
  1100. mDecoder = std::make_unique<UhjStereoDecoder<UhjLength256>>();
  1101. mDecoderPadding = UhjStereoDecoder<UhjLength256>::sInputPadding;
  1102. break;
  1103. case UhjQualityType::FIR512:
  1104. mDecoder = std::make_unique<UhjStereoDecoder<UhjLength512>>();
  1105. mDecoderPadding = UhjStereoDecoder<UhjLength512>::sInputPadding;
  1106. break;
  1107. }
  1108. }
  1109. else if(IsUHJ(mFmtChannels))
  1110. {
  1111. switch(UhjDecodeQuality)
  1112. {
  1113. case UhjQualityType::IIR:
  1114. mDecoder = std::make_unique<UhjDecoderIIR>();
  1115. mDecoderPadding = UhjDecoderIIR::sInputPadding;
  1116. break;
  1117. case UhjQualityType::FIR256:
  1118. mDecoder = std::make_unique<UhjDecoder<UhjLength256>>();
  1119. mDecoderPadding = UhjDecoder<UhjLength256>::sInputPadding;
  1120. break;
  1121. case UhjQualityType::FIR512:
  1122. mDecoder = std::make_unique<UhjDecoder<UhjLength512>>();
  1123. mDecoderPadding = UhjDecoder<UhjLength512>::sInputPadding;
  1124. break;
  1125. }
  1126. }
  1127. /* Clear the stepping value explicitly so the mixer knows not to mix this
  1128. * until the update gets applied.
  1129. */
  1130. mStep = 0;
  1131. /* Make sure the sample history is cleared. */
  1132. std::fill(mPrevSamples.begin(), mPrevSamples.end(), HistoryLine{});
  1133. if(mFmtChannels == FmtUHJ2 && !device->mUhjEncoder)
  1134. {
  1135. /* 2-channel UHJ needs different shelf filters. However, we can't just
  1136. * use different shelf filters after mixing it, given any old speaker
  1137. * setup the user has. To make this work, we apply the expected shelf
  1138. * filters for decoding UHJ2 to quad (only needs LF scaling), and act
  1139. * as if those 4 quad channels are encoded right back into B-Format.
  1140. *
  1141. * This isn't perfect, but without an entirely separate and limited
  1142. * UHJ2 path, it's better than nothing.
  1143. *
  1144. * Note this isn't needed with UHJ output (UHJ2->B-Format->UHJ2 is
  1145. * identity, so don't mess with it).
  1146. */
  1147. const BandSplitter splitter{device->mXOverFreq / static_cast<float>(device->mSampleRate)};
  1148. for(auto &chandata : mChans)
  1149. {
  1150. chandata.mAmbiHFScale = 1.0f;
  1151. chandata.mAmbiLFScale = 1.0f;
  1152. chandata.mAmbiSplitter = splitter;
  1153. chandata.mDryParams = DirectParams{};
  1154. chandata.mDryParams.NFCtrlFilter = device->mNFCtrlFilter;
  1155. std::fill_n(chandata.mWetParams.begin(), device->NumAuxSends, SendParams{});
  1156. }
  1157. mChans[0].mAmbiLFScale = DecoderBase::sWLFScale;
  1158. mChans[1].mAmbiLFScale = DecoderBase::sXYLFScale;
  1159. mChans[2].mAmbiLFScale = DecoderBase::sXYLFScale;
  1160. mFlags.set(VoiceIsAmbisonic);
  1161. }
  1162. /* Don't need to set the VoiceIsAmbisonic flag if the device is not higher
  1163. * order than the voice. No HF scaling is necessary to mix it.
  1164. */
  1165. else if(mAmbiOrder && device->mAmbiOrder > mAmbiOrder)
  1166. {
  1167. auto OrdersSpan = Is2DAmbisonic(mFmtChannels)
  1168. ? al::span<const uint8_t>{AmbiIndex::OrderFrom2DChannel}
  1169. : al::span<const uint8_t>{AmbiIndex::OrderFromChannel};
  1170. auto OrderFromChan = OrdersSpan.cbegin();
  1171. const auto scales = AmbiScale::GetHFOrderScales(mAmbiOrder, device->mAmbiOrder,
  1172. device->m2DMixing);
  1173. const BandSplitter splitter{device->mXOverFreq / static_cast<float>(device->mSampleRate)};
  1174. for(auto &chandata : mChans)
  1175. {
  1176. chandata.mAmbiHFScale = scales[*(OrderFromChan++)];
  1177. chandata.mAmbiLFScale = 1.0f;
  1178. chandata.mAmbiSplitter = splitter;
  1179. chandata.mDryParams = DirectParams{};
  1180. chandata.mDryParams.NFCtrlFilter = device->mNFCtrlFilter;
  1181. std::fill_n(chandata.mWetParams.begin(), device->NumAuxSends, SendParams{});
  1182. }
  1183. mFlags.set(VoiceIsAmbisonic);
  1184. }
  1185. else
  1186. {
  1187. for(auto &chandata : mChans)
  1188. {
  1189. chandata.mDryParams = DirectParams{};
  1190. chandata.mDryParams.NFCtrlFilter = device->mNFCtrlFilter;
  1191. std::fill_n(chandata.mWetParams.begin(), device->NumAuxSends, SendParams{});
  1192. }
  1193. mFlags.reset(VoiceIsAmbisonic);
  1194. }
  1195. }