83 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0))
return;
86 nominal_ = std::max(1.0, nominalSamples);
87 ips_ = std::max(0.1, ips);
88 const double dt = 1.0 / fs_;
93 const double dc =
static_cast<double>(kControl) * dt;
94 noiseA_ = std::exp(-2.0 * std::numbers::pi * 3.0 * dc);
95 scrapeA1_ = std::exp(-2.0 * std::numbers::pi * 90.0 * dc);
96 scrapeA2_ = std::exp(-2.0 * std::numbers::pi * 40.0 * dc);
99 const double a = noiseA_;
100 const double gain = (1.0 - a) * (1.0 + a * a) / ((1.0 + a) * (1.0 + a) * (1.0 + a));
101 noiseNorm_ = 1.0 / (kControlNoise * std::sqrt(std::max(1e-30, gain / 3.0)));
111 speed_ = speedTarget_ = 1.0;
113 capPhase_ = reelPhase_ = 0.0;
114 noise1_ = noise2_ = scrape1_ = scrape2_ = 0.0;
116 devFrom_ = devTo_ = 0.0;
123 if (!std::isfinite(ips) || !(ips > 0.0) || ips == ips_)
return;
124 speed_ *= ips_ / ips;
131 if (!std::isfinite(ips) || !(ips > 0.0))
return;
133 speed_ = speedTarget_ = 1.0;
142 if (!std::isfinite(samples))
return;
143 speedTarget_ = nominal_ / std::clamp(samples, std::max(lo_, 1e-3), hi_);
149 if (!std::isfinite(samples))
return;
151 speed_ = speedTarget_;
152 delay_ = std::clamp(samples, lo_, hi_);
158 lo_ = std::max(0.0, lo);
159 hi_ = std::max(lo_, hi);
160 delay_ = std::clamp(delay_, lo_, hi_);
164 void syncDelay(
double samples)
noexcept { delay_ = std::clamp(samples, lo_, hi_); }
166 [[nodiscard]]
double getDelay() const noexcept {
return delay_; }
167 [[nodiscard]]
double getNominal() const noexcept {
return nominal_; }
174 [[nodiscard]]
double next(
double depth)
noexcept
176 speed_ = speedTarget_ + ((speed_ < speedTarget_) ? up_ : down_) * (speed_ - speedTarget_);
177 if (std::abs(speed_ - speedTarget_) < 1e-12) speed_ = speedTarget_;
179 if (controlAt_ == 0) advanceControl(depth);
180 const double share =
static_cast<double>(++controlAt_) /
static_cast<double>(kControl);
181 if (controlAt_ == kControl) controlAt_ = 0;
182 const double deviation = devFrom_ + (devTo_ - devFrom_) * share;
183 const double target = nominal_ / speed_;
184 const double step = std::clamp(-deviation + restore_ * (target - delay_), -s_.
maxStep, s_.
maxStep);
185 delay_ = std::clamp(delay_ + step, lo_, hi_);
190 static constexpr int kControl = 16;
194 void advanceControl(
double depth)
noexcept
196 const double ipsNow = ips_ * speed_;
197 const double dc =
static_cast<double>(kControl) / fs_;
198 capPhase_ += kCapstanHzPerIps * ipsNow * dc;
199 capPhase_ -= std::floor(capPhase_);
200 reelPhase_ += kReelHzPerIps * ipsNow * dc;
201 reelPhase_ -= std::floor(reelPhase_);
202 rng_ = rng_ * 1664525u + 1013904223u;
205 const double n = (
static_cast<double>(rng_ >> 8) / 8388608.0 - 1.0) * kControlNoise;
206 noise1_ = noiseA_ * noise1_ + (1.0 - noiseA_) * n;
207 noise2_ = noiseA_ * noise2_ + (1.0 - noiseA_) * noise1_;
208 scrape1_ = scrapeA1_ * scrape1_ + (1.0 - scrapeA1_) * n;
209 scrape2_ = scrapeA2_ * scrape2_ + (1.0 - scrapeA2_) * n;
211 devTo_ = deviationNow(depth);
215 [[nodiscard]]
double deviationNow(
double depth)
const noexcept
217 if (!(depth > 0.0))
return 0.0;
218 const double twoPi = 2.0 * std::numbers::pi;
219 const double cap =
twoPi * capPhase_;
220 const double reel = s_.
reel != 0.0 ? s_.
reel * std::sin(
twoPi * reelPhase_) : 0.0;
221 return depth * std::sqrt(15.0 / ips_)
224 + s_.
noise * noiseNorm_ * noise2_
225 + s_.
scrape * (scrape1_ - scrape2_));
228 static constexpr double kControlNoise = 0.25;
230 static constexpr double kCapstanHzPerIps = 1.0 / 3.0;
231 static constexpr double kReelHzPerIps = 0.04;
234 double fs_ = 48000.0, nominal_ = 96.0, ips_ = 15.0;
235 double up_ = 0.0, down_ = 0.0, restore_ = 0.0, noiseA_ = 0.0, noiseNorm_ = 1.0;
236 double scrapeA1_ = 0.0, scrapeA2_ = 0.0;
237 double lo_ = 3.0, hi_ = 1e12;
238 double speed_ = 1.0, speedTarget_ = 1.0, delay_ = 96.0;
239 double capPhase_ = 0.0, reelPhase_ = 0.0, noise1_ = 0.0, noise2_ = 0.0, scrape1_ = 0.0, scrape2_ = 0.0;
240 uint32_t rng_ = 0x2C1B3C6Du;
241 double devFrom_ = 0.0, devTo_ = 0.0;
The delay of a tape moved by a transport with inertia, wow and flutter.
void setTargetDelay(double samples) noexcept
A delay to glide to, in samples: the speed nominal / samples, reached with the transport's inertia (t...
void prepare(double sampleRate, double nominalSamples, double ips, const Settings &settings=Settings {}) noexcept
Sets the rate, the head distance (as a delay at the nominal speed), the nominal speed and the constan...
void syncDelay(double samples) noexcept
Starts the delay from a value (switching in from another reader).
void setSpeed(double ips) noexcept
A new nominal speed: the tape glides to it from the speed it has.
double getDelay() const noexcept
void setSpeedNow(double ips) noexcept
A new nominal speed, taken at once (no glide).
void setTargetDelayNow(double samples) noexcept
A delay taken at once: the speed and the delay jump to it.
void setLimits(double lo, double hi) noexcept
The range the delay stays in, in samples (default from 3 up).
double getSpeedRatio() const noexcept
double getNominal() const noexcept
double next(double depth) noexcept
The delay for the next sample.
void reset() noexcept
Speed at nominal, delay at the head distance, phases and noise from the start.
Main namespace for the DSPark framework.
constexpr T twoPi
2 * Pi (6.28318...).
The transport's constants.
double restoreSeconds
Restoring time of the delay to the head distance.
double capstanSecond
smoothed noise (RMS) and scrape.
double reel
Speed deviations at full depth and 15 ips:
double slowDownSeconds
... and when slowing down.
double speedUpSeconds
Inertia when speeding up.
double capstan
reel, capstan, its second harmonic's share,
double maxStep
Largest delay change per sample (bounds the pitch).