104 feedbackMode_.store(mode, std::memory_order_relaxed);
110 return feedbackMode_.load(std::memory_order_relaxed);
126 std::memory_order_relaxed);
132 return interpolation_.load(std::memory_order_relaxed);
146 const int v = std::clamp(
static_cast<int>(c), 0,
static_cast<int>(
Character::Bucket));
147 character_.store(
static_cast<Character>(v), std::memory_order_relaxed);
158 if (!std::isfinite(amount))
return;
159 wowFlutter_.store(std::clamp(amount, SampleType(0), SampleType(1)), std::memory_order_relaxed);
162 [[nodiscard]] SampleType
getWowFlutter() const noexcept {
return wowFlutter_.load(std::memory_order_relaxed); }
171 if (!std::isfinite(dbfs))
return;
172 characterNoiseDb_.store(std::clamp(dbfs, SampleType(-140), SampleType(-40)), std::memory_order_relaxed);
175 [[nodiscard]] SampleType
getCharacterNoise() const noexcept {
return characterNoiseDb_.load(std::memory_order_relaxed); }
198 if (!spec.
isValid() || !(maxDelaySeconds >= 0.0))
return;
199 sampleRate_ =
static_cast<SampleType
>(spec.
sampleRate);
209 const double capacityD =
210 std::min(std::ceil(maxDelaySeconds * spec.
sampleRate) + blockSize_,
212 maxDelaySamples_ = nextPow2(
static_cast<int>(capacityD));
213 bufferMask_ = maxDelaySamples_ - 1;
215 delayBuffer_.
resize(numChannels_, maxDelaySamples_);
216 wetBuffer_.
resize(numChannels_, blockSize_);
217 lastWetSamples_ = blockSize_;
218 sinc_ = &sincTable();
222 if (
const SampleType pending = pendingDelaySamples_.exchange(SampleType(-1), std::memory_order_relaxed);
223 pending >= SampleType(0))
224 delayMs_.store(pending * SampleType(1000) / sampleRate_, std::memory_order_relaxed);
225 globalDelay_.store(clampToCapacity(delayMs_.load(std::memory_order_relaxed) * sampleRate_ / SampleType(1000)),
226 std::memory_order_relaxed);
230 float timeMs = smoothingTimeMs_.load(std::memory_order_relaxed);
231 for (
int ch = 0; ch < std::min(numChannels_,
kMaxChannels); ++ch)
233 resetChannelSmoothers(states_[ch], timeMs, 0.0f);
234 writeIndices_[ch] = 0;
239 tape_.resize(
static_cast<size_t>(numChannels_));
240 bbd_.resize(
static_cast<size_t>(numChannels_));
251 for (
auto& t : tape_)
254 t.setLimits(3.0,
static_cast<double>(maxDelaySamples_ - 1));
258 headCoeff_ = 1.0 - std::exp(-2.0 * 3.141592653589793 * std::min(kHeadLossHz, 0.45 * spec.
sampleRate) / spec.
sampleRate);
259 switchFade_ = std::max(1,
static_cast<int>(kSwitchFadeSeconds * spec.
sampleRate));
262 insertMixMaxStep_ =
static_cast<SampleType
>(1.0 / std::max(1.0, spec.
sampleRate * 0.02));
265 prepared_.store(
true, std::memory_order_release);
275 prepare(spec, maxDelayMs / 1000.0);
283 delayBuffer_.
clear();
287 writeIndices_[ch] = 0;
288 resetChannelState(states_[ch]);
290 for (
auto& t : tape_) t.reset();
291 for (
auto& b : bbd_) b.reset();
292 tapeSeeded_.fill(
false);
293 bucketCornerFor_.fill(-1.0);
296 bucketHissCoeff_.fill(0.5);
297 hissDbFor_.fill(1.0);
301 hissRng_[
static_cast<size_t>(ch)] = 0x3C6EF372u + 0x9E3779B9u *
static_cast<uint32_t
>(ch);
302 lastCharacter_ = character_.load(std::memory_order_relaxed);
304 insertMix_ = mix_.load(std::memory_order_relaxed);
316 smootherType_.store(type, std::memory_order_relaxed);
317 smootherDirty_.store(
true, std::memory_order_release);
326 if (!std::isfinite(ms))
return;
327 smoothingTimeMs_.store(std::max(0.0f, ms), std::memory_order_relaxed);
328 smootherDirty_.store(
true, std::memory_order_release);
341 if (!std::isfinite(samples))
return;
342 samples = std::max(samples, SampleType(0));
343 if (prepared_.load(std::memory_order_acquire))
345 delayMs_.store(samples * SampleType(1000) / sampleRate_, std::memory_order_relaxed);
346 pendingDelaySamples_.store(SampleType(-1), std::memory_order_relaxed);
351 pendingDelaySamples_.store(samples, std::memory_order_relaxed);
353 globalDelay_.store(clampToCapacity(samples), std::memory_order_relaxed);
358 delayTargetDirty_.store(
true, std::memory_order_release);
365 if (!std::isfinite(ms))
return;
366 ms = std::max(ms, SampleType(0));
367 delayMs_.store(ms, std::memory_order_relaxed);
368 pendingDelaySamples_.store(SampleType(-1), std::memory_order_relaxed);
369 globalDelay_.store(clampToCapacity(ms * sampleRate_ / SampleType(1000)), std::memory_order_relaxed);
370 delayTargetDirty_.store(
true, std::memory_order_release);
379 const SampleType pending = pendingDelaySamples_.load(std::memory_order_relaxed);
380 return pending >= SampleType(0) ? pending * SampleType(1000) / sampleRate_
381 : delayMs_.load(std::memory_order_relaxed);
385 [[nodiscard]] SampleType
getCurrentDelaySamples() const noexcept {
return globalDelay_.load(std::memory_order_relaxed); }
395 if (!std::isfinite(gain))
return;
396 feedbackGain_.store(gain, std::memory_order_relaxed);
403 if (!std::isfinite(freq))
return;
404 fbLpCoef_.store((freq > 0) ? calcLpCoef(freq) : SampleType(0), std::memory_order_relaxed);
405 fbLpHzShadow_.store(freq, std::memory_order_relaxed);
412 if (!std::isfinite(freq))
return;
413 fbHpCoef_.store((freq > 0) ? calcHpCoef(freq) : SampleType(0), std::memory_order_relaxed);
414 fbHpHzShadow_.store(freq, std::memory_order_relaxed);
427 if (ch < 0 || ch >= numChannels_ || !prepared_.load(std::memory_order_acquire))
return input;
428 maybeUpdateSmoothers();
430 auto& s = states_[ch];
431 auto st = smootherType_.load(std::memory_order_relaxed);
433 ? globalDelay_.load(std::memory_order_relaxed) : advanceSmoother(s, st);
435 SampleType out = processSampleInternal(ch, input, delay, s,
436 feedbackGain_.load(std::memory_order_relaxed),
437 fbLpCoef_.load(std::memory_order_relaxed),
438 fbHpCoef_.load(std::memory_order_relaxed));
439 advanceWriteIndexUnchecked(ch);
456 if (!prepared_.load(std::memory_order_acquire))
return;
457 maybeUpdateSmoothers();
459 const int nS = buffer.getNumSamples();
460 const int nCh = std::min(buffer.getNumChannels(), numChannels_);
461 const auto smoothType = smootherType_.load(std::memory_order_relaxed);
462 const SampleType targetDelay = globalDelay_.load(std::memory_order_relaxed);
463 const SampleType fb = feedbackGain_.load(std::memory_order_relaxed);
464 const SampleType lpC = fbLpCoef_.load(std::memory_order_relaxed);
465 const SampleType hpC = fbHpCoef_.load(std::memory_order_relaxed);
466 const SampleType mixTarget = mix_.load(std::memory_order_relaxed);
467 const SampleType mixStart = insertMix_;
468 const bool pingPong = pingPong_.load(std::memory_order_relaxed) && nCh >= 2;
475 SampleType* L = buffer.getChannel(0);
476 SampleType* R = buffer.getChannel(1);
477 auto& sL = states_[0];
478 auto& sR = states_[1];
479 for (
int i = 0; i < nS; ++i)
482 ? targetDelay : advanceSmoother(sL, smoothType);
483 advanceSmoother(sR, smoothType);
484 const SampleType dryL = L[i], dryR = R[i];
485 const SampleType outL = processSampleInternal(0, dryL + sL.pingPongFb, currentDelay, sL, SampleType(0), lpC, hpC,
false);
486 const SampleType outR = processSampleInternal(1, dryR + sR.pingPongFb, currentDelay, sR, SampleType(0), lpC, hpC,
false);
487 sR.pingPongFb = saturateFeedback(processFbFilters(1, outL * fb, lpC, hpC), analogFb);
488 sL.pingPongFb = saturateFeedback(processFbFilters(0, outR * fb, lpC, hpC), analogFb);
489 advanceWriteIndexUnchecked(0);
490 advanceWriteIndexUnchecked(1);
491 const SampleType m =
moveTowards(mixStart, mixTarget,
492 insertMixMaxStep_ *
static_cast<SampleType
>(i + 1));
493 L[i] = dryL + (outL - dryL) * m;
494 R[i] = dryR + (outR - dryR) * m;
498 for (
int ch = pingPong ? 2 : 0; ch < nCh; ++ch)
500 SampleType* data = buffer.getChannel(ch);
501 auto& s = states_[ch];
502 for (
int i = 0; i < nS; ++i)
505 ? targetDelay : advanceSmoother(s, smoothType);
506 const SampleType dry = data[i];
507 const SampleType wet = processSampleInternal(ch, dry, currentDelay, s, fb, lpC, hpC);
508 advanceWriteIndexUnchecked(ch);
509 const SampleType m =
moveTowards(mixStart, mixTarget,
510 insertMixMaxStep_ *
static_cast<SampleType
>(i + 1));
511 data[i] = dry + (wet - dry) * m;
514 insertMix_ =
moveTowards(mixStart, mixTarget, insertMixMaxStep_ *
static_cast<SampleType
>(nS));
524 if (!std::isfinite(mix))
return;
525 mix_.store(std::clamp(mix, SampleType(0), SampleType(1)), std::memory_order_relaxed);
529 [[nodiscard]] SampleType
getMix() const noexcept {
return mix_.load(std::memory_order_relaxed); }
539 void setPingPong(
bool on)
noexcept { pingPong_.store(on, std::memory_order_relaxed); }
542 [[nodiscard]]
bool getPingPong() const noexcept {
return pingPong_.load(std::memory_order_relaxed); }
561 SampleType feedback = 0, SampleType lpHz = 0, SampleType hpHz = 0) noexcept
563 if (!prepared_.load(std::memory_order_acquire))
return;
569 maybeUpdateSmoothers();
575 const auto smoothType = smootherType_.load(std::memory_order_relaxed);
576 const SampleType targetDelay = globalDelay_.load(std::memory_order_relaxed);
577 const SampleType fb = feedbackGain_.load(std::memory_order_relaxed);
578 const SampleType lpC = fbLpCoef_.load(std::memory_order_relaxed);
579 const SampleType hpC = fbHpCoef_.load(std::memory_order_relaxed);
581 for (
int ch = 0; ch < nCh; ++ch)
584 auto& s = states_[ch];
586 for (
int i = 0; i < nS; ++i)
588 SampleType currentDelay = (smoothType ==
SmootherType::None) ? targetDelay : advanceSmoother(s, smoothType);
589 data[i] = processSampleInternal(ch, data[i], currentDelay, s, fb, lpC, hpC);
590 advanceWriteIndexUnchecked(ch);
599 SampleType feedback = 0, SampleType lpHz = 0, SampleType hpHz = 0) noexcept
601 if (ch < 0 || ch >= numChannels_ || !prepared_.load(std::memory_order_acquire))
return;
607 maybeUpdateSmoothers();
612 const auto smoothType = smootherType_.load(std::memory_order_relaxed);
613 const SampleType targetDelay = globalDelay_.load(std::memory_order_relaxed);
614 const SampleType fb = feedbackGain_.load(std::memory_order_relaxed);
615 const SampleType lpC = fbLpCoef_.load(std::memory_order_relaxed);
616 const SampleType hpC = fbHpCoef_.load(std::memory_order_relaxed);
618 auto& s = states_[ch];
620 for (
int i = 0; i < nS; ++i)
622 SampleType currentDelay = (smoothType ==
SmootherType::None) ? targetDelay : advanceSmoother(s, smoothType);
623 data[i] = processSampleInternal(ch, data[i], currentDelay, s, fb, lpC, hpC);
624 advanceWriteIndexUnchecked(ch);
633 pushDryToWetImpl([&](
int ch) {
return dry.getChannel(ch); }, dry.getNumChannels(), dry.getNumSamples());
639 pushDryToWetImpl([&](
int ch) {
return dry.getChannel(ch); }, dry.getNumChannels(), dry.getNumSamples());
647 void processWet(SampleType delayMs, SampleType feedback = 0, SampleType lpHz = 0, SampleType hpHz = 0) noexcept
649 processBlock(wetBuffer_.
toView().getSubView(0, lastWetSamples_), delayMs, feedback, lpHz, hpHz);
660 void processPingPong(SampleType delayMs, SampleType feedback = 0, SampleType lpHz = 0, SampleType hpHz = 0) noexcept
662 if (wetBuffer_.
getNumChannels() < 2 || !prepared_.load(std::memory_order_acquire))
return;
668 maybeUpdateSmoothers();
672 const int nS = std::min(lastWetSamples_, wetBuffer_.
getNumSamples());
674 const auto smoothType = smootherType_.load(std::memory_order_relaxed);
675 const SampleType targetDelay = globalDelay_.load(std::memory_order_relaxed);
676 const SampleType fb = feedbackGain_.load(std::memory_order_relaxed);
677 const SampleType lpC = fbLpCoef_.load(std::memory_order_relaxed);
678 const SampleType hpC = fbHpCoef_.load(std::memory_order_relaxed);
681 for (
int i = 0; i < nS; ++i)
683 auto& sL = states_[0];
684 auto& sR = states_[1];
686 SampleType currentDelay = (smoothType ==
SmootherType::None) ? targetDelay : advanceSmoother(sL, smoothType);
687 advanceSmoother(sR, smoothType);
694 SampleType inL = L[i] + sL.pingPongFb;
695 SampleType inR = R[i] + sR.pingPongFb;
697 SampleType outL = processSampleInternal(0, inL, currentDelay, sL, SampleType(0), lpC, hpC,
false);
698 SampleType outR = processSampleInternal(1, inR, currentDelay, sR, SampleType(0), lpC, hpC,
false);
700 sR.pingPongFb = saturateFeedback(processFbFilters(1, outL * fb, lpC, hpC), analogFb);
701 sL.pingPongFb = saturateFeedback(processFbFilters(0, outR * fb, lpC, hpC), analogFb);
705 advanceWriteIndexUnchecked(0);
706 advanceWriteIndexUnchecked(1);
719 mix = std::min(SampleType(1), std::max(SampleType(0), mix));
720 auto st = smootherType_.load(std::memory_order_relaxed);
724 const int nS = std::min(dry.getNumSamples(), wetBuffer_.
getNumSamples());
725 const int nCh = std::min(dry.getNumChannels(), wetBuffer_.
getNumChannels());
732 std::array<SampleType*, kMaxChannels> d {};
733 std::array<const SampleType*, kMaxChannels> w {};
734 for (
int ch = 0; ch < nCh; ++ch)
736 d[ch] = dry.getChannel(ch);
739 for (
int i = 0; i < nS; ++i)
742 ?
static_cast<SampleType
>(mixSmoother_.
getNextValue()) : mix;
743 for (
int ch = 0; ch < nCh; ++ch)
744 d[ch][i] = d[ch][i] * (SampleType(1) - m) + w[ch][i] * m;
756 [[nodiscard]] std::vector<uint8_t>
getState()
const
761 w.
write(
"delaySamples",
static_cast<float>(globalDelay_.load(std::memory_order_relaxed)));
763 w.
write(
"feedback",
static_cast<float>(feedbackGain_.load(std::memory_order_relaxed)));
764 w.
write(
"fbLpHz",
static_cast<float>(fbLpHzShadow_.load(std::memory_order_relaxed)));
765 w.
write(
"fbHpHz",
static_cast<float>(fbHpHzShadow_.load(std::memory_order_relaxed)));
766 w.
write(
"smoother",
static_cast<int32_t
>(smootherType_.load(std::memory_order_relaxed)));
767 w.
write(
"smoothingMs", smoothingTimeMs_.load(std::memory_order_relaxed));
768 w.
write(
"fbMode",
static_cast<int32_t
>(feedbackMode_.load(std::memory_order_relaxed)));
769 w.
write(
"mix",
static_cast<float>(mix_.load(std::memory_order_relaxed)));
770 w.
write(
"pingPong",
static_cast<int32_t
>(pingPong_.load(std::memory_order_relaxed) ? 1 : 0));
771 w.
write(
"interp",
static_cast<int32_t
>(interpolation_.load(std::memory_order_relaxed)));
772 w.
write(
"character",
static_cast<int32_t
>(character_.load(std::memory_order_relaxed)));
773 w.
write(
"wowFlutter",
static_cast<float>(wowFlutter_.load(std::memory_order_relaxed)));
774 w.
write(
"charNoise",
static_cast<float>(characterNoiseDb_.load(std::memory_order_relaxed)));
785 if (
const float ms = r.
read(
"delayMs", -1.0f); ms >= 0.0f)
799 setMix(
static_cast<SampleType
>(r.
read(
"mix", 0.3f)));
839 advanceWriteIndexUnchecked(ch);
844 void advanceWriteIndexUnchecked(
int ch)
noexcept { writeIndices_[ch] = (writeIndices_[ch] + 1) & bufferMask_; }
849 [[nodiscard]] SampleType saturateFeedback(SampleType x,
bool analog)
const noexcept
851 return analog ? std::tanh(x) : std::clamp(x, SampleType(-2), SampleType(2));
854 template <
typename ChannelFn>
855 void pushDryToWetImpl(ChannelFn&& getCh,
int dryCh,
int drySamples)
noexcept
858 const int nS = std::min(drySamples, wetBuffer_.
getNumSamples());
859 for (
int ch = 0; ch < nCh; ++ch)
860 std::memcpy(wetBuffer_.
getChannel(ch), getCh(ch),
static_cast<std::size_t
>(nS) *
sizeof(SampleType));
861 lastWetSamples_ = nS;
864 void resetChannelState(ChannelState& s)
noexcept
866 s.fbLpZ1 = s.fbHpZ1 = s.lastFeedback = s.pingPongFb = 0;
867 s.currentDelay = s.targetDelay = 0;
870 void resetChannelSmoothers(ChannelState& s,
float timeMs,
float init)
noexcept
872 double sr =
static_cast<double>(sampleRate_);
873 s.lin.reset(sr, timeMs, init);
874 s.exp.reset(sr, timeMs, std::max(init, 1e-6f));
875 s.onePole.reset(sr, timeMs, init);
876 s.multi2.reset(sr, timeMs, init);
877 s.asym.reset(sr, timeMs / 5.0f, timeMs, init);
878 float timeSec = timeMs / 1000.0f;
879 float maxRate =
static_cast<float>(maxDelaySamples_) / std::max(timeSec, 1e-6f);
880 s.slew.reset(sr, maxRate, init);
881 s.svf.reset(sr, timeMs, 0.707f, init);
882 s.butter.reset(sr, timeMs, init);
883 s.crit.reset(sr, timeMs, init);
886 void maybeUpdateSmoothers() noexcept
894 delayTargetDirty_.store(
false, std::memory_order_relaxed);
895 smootherDirty_.store(
false, std::memory_order_relaxed);
896 const SampleType target = globalDelay_.load(std::memory_order_acquire);
897 const float timeMs = smoothingTimeMs_.load(std::memory_order_relaxed);
898 for (
int ch = 0; ch < numChannels_; ++ch)
900 auto& s = states_[ch];
901 resetChannelSmoothers(s, timeMs,
static_cast<float>(target));
902 s.currentDelay = target;
903 setSmootherTarget(s, target);
911 if (delayTargetDirty_.load(std::memory_order_relaxed)
912 && delayTargetDirty_.exchange(
false, std::memory_order_acquire))
913 updateSmootherTargets(globalDelay_.load(std::memory_order_relaxed));
918 if (!smootherDirty_.load(std::memory_order_relaxed)
919 || !smootherDirty_.exchange(
false, std::memory_order_acquire))
return;
920 const float timeMs = smoothingTimeMs_.load(std::memory_order_relaxed);
921 const SampleType target = globalDelay_.load(std::memory_order_relaxed);
922 for (
int ch = 0; ch < numChannels_; ++ch)
926 auto& s = states_[ch];
927 resetChannelSmoothers(s, timeMs,
static_cast<float>(s.currentDelay));
928 setSmootherTarget(s, target);
933 void updateSmootherTargets(SampleType target)
noexcept
935 const bool none = smootherType_.load(std::memory_order_relaxed) ==
SmootherType::None;
936 for (
int ch = 0; ch < numChannels_; ++ch)
942 states_[ch].targetDelay = target;
943 states_[ch].currentDelay = target;
947 setSmootherTarget(states_[ch], target);
953 [[nodiscard]] SampleType clampToCapacity(SampleType samples)
const noexcept
955 samples = std::max(samples, SampleType(0));
956 return maxDelaySamples_ > 0 ? std::min(samples,
static_cast<SampleType
>(maxDelaySamples_ - 1))
961 static const SincInterpolator<SampleType>& sincTable()
963 static const SincInterpolator<SampleType> table;
967 void setSmootherTarget(ChannelState& s, SampleType target)
noexcept
969 s.targetDelay = target;
970 float t =
static_cast<float>(target);
971 switch (smootherType_.load(std::memory_order_relaxed))
986 inline SampleType advanceSmoother(ChannelState& s,
SmootherType st)
noexcept
1000 default: val =
static_cast<float>(s.targetDelay);
1002 s.currentDelay =
static_cast<SampleType
>(val);
1003 return s.currentDelay;
1006 inline SampleType processSampleInternal(
int ch, SampleType input, SampleType delaySamples,
1007 ChannelState& s, SampleType fbMult,
1008 SampleType lpC, SampleType hpC,
1009 bool straightFeedback =
true) noexcept
1017 if (!std::isfinite(input)) input = SampleType(0);
1031 constexpr SampleType kMinHermiteDelay = SampleType(3);
1032 const bool sinc = sinc_ && interpolation_.load(std::memory_order_relaxed) ==
Interpolation::Sinc;
1033 const SampleType minDelay = sinc ? SampleType(SincInterpolator<SampleType>::kAfter + 1)
1035 delaySamples = std::clamp(delaySamples, minDelay,
1036 static_cast<SampleType
>(maxDelaySamples_ - 1));
1038 const Character chr = character_.load(std::memory_order_relaxed);
1039 if (chr != lastCharacter_) [[unlikely]] switchCharacter(chr, delaySamples);
1041 SampleType* data = delayBuffer_.
getChannel(ch);
1042 const int writeIdx = writeIndices_[ch];
1043 const auto c =
static_cast<size_t>(ch);
1046 if (switchLeft_[c] > 0) [[unlikely]]
1050 const double outgoing = readCharacter(switchFrom_, ch, input, delaySamples, minDelay, sinc, s);
1051 const double incoming = readCharacter(lastCharacter_, ch, input, delaySamples, minDelay, sinc, s);
1052 const int left = switchLeft_[c]--;
1053 const double w = left > switchFade_ ? 0.0
1054 :
static_cast<double>(switchFade_ - left + 1) /
static_cast<double>(switchFade_);
1055 double y = outgoing + (incoming - outgoing) * w;
1058 if (hissOut + hissIn > 0.0)
1061 ? lastCharacter_ : switchFrom_;
1062 y += (hissOut + hissIn) * hiss(ch, hissCoeffOf(ch, heard));
1064 delayed =
static_cast<SampleType
>(y);
1070 double y = readCharacter(now, ch, input, delaySamples, minDelay, sinc, s);
1072 delayed =
static_cast<SampleType
>(y);
1080 SampleType fbInput = SampleType(0);
1081 if (straightFeedback)
1083 fbInput = processFbFilters(ch, delayed * fbMult, lpC, hpC);
1084 if (fbInput != SampleType(0))
1085 fbInput = saturateFeedback(fbInput,
1088 s.lastFeedback = fbInput;
1090 data[writeIdx] = input + s.lastFeedback;
1094 inline SampleType processFbFilters(
int ch, SampleType sample, SampleType lpC, SampleType hpC)
noexcept
1096 auto& s = states_[ch];
1097 SampleType out = sample;
1100 out += SampleType(1e-18);
1102 if (hpC > SampleType(0))
1104 s.fbHpZ1 = out * (SampleType(1) - hpC) + s.fbHpZ1 * hpC;
1107 if (lpC > SampleType(0))
1109 s.fbLpZ1 = out * (SampleType(1) - lpC) + s.fbLpZ1 * lpC;
1113 out -= SampleType(1e-18);
1123 void switchCharacter(
Character chr, SampleType delaySamples)
noexcept
1125 for (
int ch = 0; ch < std::min(numChannels_,
kMaxChannels); ++ch)
1126 if (switchLeft_[
static_cast<size_t>(ch)] > 0)
return;
1127 switchFrom_ = lastCharacter_;
1128 lastCharacter_ = chr;
1133 for (
auto& b : bbd_) b.
reset();
1134 bucketCornerFor_.fill(-1.0);
1135 fill =
static_cast<int>(delaySamples) +
static_cast<int>(kBucketSettleSeconds *
static_cast<double>(sampleRate_));
1137 switchLeft_.fill(fill + switchFade_);
1141 SampleType readRing(
int ch, SampleType delaySamples,
bool sinc)
noexcept
1143 SampleType* data = delayBuffer_.
getChannel(ch);
1144 const int writeIdx = writeIndices_[ch];
1151 const int dInt =
static_cast<int>(delaySamples);
1152 const SampleType dFrac = delaySamples -
static_cast<SampleType
>(dInt);
1155 if (dFrac > SampleType(0))
1157 idx0 = (writeIdx - dInt - 1) & bufferMask_;
1158 frac = SampleType(1) - dFrac;
1162 idx0 = (writeIdx - dInt) & bufferMask_;
1163 frac = SampleType(0);
1166 return sinc_->
readRing(data,
static_cast<int64_t
>(bufferMask_),
static_cast<int64_t
>(idx0),
static_cast<double>(frac));
1168 const int idxM1 = (idx0 - 1 + maxDelaySamples_) & bufferMask_;
1169 const int idx1 = (idx0 + 1) & bufferMask_;
1170 const int idx2 = (idx0 + 2) & bufferMask_;
1171 const SampleType ym1 = data[idxM1];
1172 const SampleType y0 = data[idx0];
1173 const SampleType y1 = data[idx1];
1174 const SampleType y2 = data[idx2];
1175 const SampleType c = (y1 - ym1) * SampleType(0.5);
1176 const SampleType v = y0 - y1;
1177 const SampleType w = c + v;
1178 const SampleType a = w + v + (y2 - y0) * SampleType(0.5);
1179 const SampleType b = w + a;
1180 return ((((a * frac) - b) * frac + c) * frac + y0);
1188 double readCharacter(
Character chr,
int ch, SampleType input, SampleType delaySamples, SampleType minDelay,
1189 bool sinc, ChannelState& s)
noexcept
1191 const auto c =
static_cast<size_t>(ch);
1193 return static_cast<double>(bucketRead(ch, input + s.lastFeedback, delaySamples));
1194 SampleType d = delaySamples;
1199 auto& tp = tape_[c];
1200 const double target =
static_cast<double>(delaySamples);
1201 if (!tapeSeeded_[c])
1203 tp.setTargetDelayNow(target);
1204 tapeSeeded_[c] =
true;
1208 tp.setTargetDelay(target);
1210 d = std::clamp(
static_cast<SampleType
>(tp.next(
static_cast<double>(wowFlutter_.load(std::memory_order_relaxed)))),
1211 minDelay,
static_cast<SampleType
>(maxDelaySamples_ - 1));
1213 const double y =
static_cast<double>(readRing(ch, d, sinc));
1215 headLp_[c] += headCoeff_ * (y - headLp_[c]);
1224 SampleType bucketRead(
int ch, SampleType in, SampleType delaySamples)
noexcept
1226 const auto c =
static_cast<size_t>(ch);
1228 const double fs =
static_cast<double>(sampleRate_);
1229 const double d =
static_cast<double>(delaySamples);
1230 if (bucketCornerFor_[c] < 0.0 || ++bucketTick_[c] >= kBucketCornerGrid)
1233 const double corner = std::min(0.3 * b.clockFor(d / fs), 0.45 * fs);
1234 if (bucketCornerFor_[c] < 0.0) b.setCornersNow(corner, corner);
1240 const double before = std::min(0.3 * b.clockFor(bucketCornerFor_[c] / fs), 0.45 * fs);
1241 const double ahead = std::clamp(2.0 * corner - before, 0.5 * corner, 2.0 * corner);
1244 bucketHissCoeff_[c] = 1.0 - std::exp(-2.0 * 3.141592653589793 * corner / fs);
1245 bucketCornerFor_[c] = d;
1249 return b.processSample(in, b.delayFor(d / fs));
1257 [[nodiscard]]
double hissCoeffOf(
int ch,
Character chr)
const noexcept
1259 return chr ==
Character::Tape ? headCoeff_ : bucketHissCoeff_[
static_cast<size_t>(ch)];
1263 double hiss(
int ch,
double hissCoeff)
noexcept
1265 const auto c =
static_cast<size_t>(ch);
1267 const double noiseDb =
static_cast<double>(characterNoiseDb_.load(std::memory_order_relaxed));
1268 if (noiseDb > -120.0)
1270 if (noiseDb != hissDbFor_[c] || hissCoeff != hissCoeffFor_[c]) [[unlikely]]
1272 hissAmp_[c] = std::pow(10.0, noiseDb / 20.0) * 1.7320508075688772
1273 / std::sqrt(std::max(1e-9, hissCoeff / (2.0 - hissCoeff)));
1274 hissDbFor_[c] = noiseDb;
1275 hissCoeffFor_[c] = hissCoeff;
1277 hissRng_[c] = hissRng_[c] * 1664525u + 1013904223u;
1278 const double n =
static_cast<double>(hissRng_[c] >> 8) / 8388608.0 - 1.0;
1279 hissLp_[c] += hissCoeff * (n - hissLp_[c]);
1280 y = hissAmp_[c] * hissLp_[c];
1286 SampleType calcOnePoleCoef(SampleType freq)
const noexcept {
return std::exp(-twoPi<SampleType> * freq / sampleRate_); }
1287 SampleType calcLpCoef(SampleType freq)
const noexcept {
return calcOnePoleCoef(freq); }
1288 SampleType calcHpCoef(SampleType freq)
const noexcept {
return calcOnePoleCoef(freq); }
1290 static int nextPow2(
int v)
noexcept
1293 while (r < v) r <<= 1;
1298 std::atomic<bool> prepared_{
false };
1299 AudioBuffer<SampleType> delayBuffer_, wetBuffer_;
1300 std::array<ChannelState, kMaxChannels> states_ {};
1301 std::array<int, kMaxChannels> writeIndices_ {};
1303 int maxDelaySamples_ = 0, bufferMask_ = 0;
1304 int numChannels_ = 0, blockSize_ = 0;
1305 int lastWetSamples_ = 0;
1306 SampleType sampleRate_ = SampleType(48000);
1308 std::atomic<SampleType> globalDelay_ { SampleType(0) };
1309 std::atomic<SampleType> delayMs_ { SampleType(0) };
1310 std::atomic<SampleType> pendingDelaySamples_ { SampleType(-1) };
1312 const SincInterpolator<SampleType>* sinc_ =
nullptr;
1313 bool snapNext_ =
true;
1314 std::atomic<SampleType> feedbackGain_ { SampleType(0) };
1315 std::atomic<SampleType> fbLpCoef_ { SampleType(0) };
1316 std::atomic<SampleType> fbLpHzShadow_ { SampleType(0) };
1317 std::atomic<SampleType> fbHpHzShadow_ { SampleType(0) };
1318 std::atomic<SampleType> fbHpCoef_ { SampleType(0) };
1321 std::atomic<float> smoothingTimeMs_ { 20.0f };
1322 std::atomic<bool> smootherDirty_ {
false };
1323 std::atomic<bool> delayTargetDirty_ {
false };
1326 Smoothers::LinearSmoother mixSmoother_;
1327 std::atomic<SampleType> mix_ { SampleType(0.3) };
1328 std::atomic<bool> pingPong_ {
false };
1329 SampleType insertMix_ = SampleType(0.3);
1330 SampleType insertMixMaxStep_ = SampleType(1.0 / 960.0);
1333 static constexpr int kBucketStages = 4096;
1334 static constexpr int kBucketCornerGrid = 32;
1335 static constexpr double kBucketSettleSeconds = 0.004;
1336 static constexpr double kSwitchFadeSeconds = 0.01;
1337 static constexpr double kHeadLossHz = 7000.0;
1339 std::atomic<SampleType> wowFlutter_ { SampleType(0.5) };
1340 std::atomic<SampleType> characterNoiseDb_ { SampleType(-84) };
1342 std::vector<TapeTransport<SampleType>> tape_;
1343 std::vector<BucketBrigade<SampleType>> bbd_;
1344 std::array<bool, kMaxChannels> tapeSeeded_ {};
1345 std::array<double, kMaxChannels> bucketCornerFor_ {};
1346 std::array<int, kMaxChannels> bucketTick_ {};
1347 std::array<int, kMaxChannels> switchLeft_ {};
1349 int switchFade_ = 480;
1350 std::array<double, kMaxChannels> headLp_ {}, hissLp_ {}, bucketHissCoeff_ {};
1351 std::array<double, kMaxChannels> hissAmp_ {}, hissDbFor_ {}, hissCoeffFor_ {};
1352 std::array<uint32_t, kMaxChannels> hissRng_ {};
1353 double headCoeff_ = 0.5;