voice.cpp 51 KB

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