DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
TapeTransport.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
40#include "DspMath.h"
41
42#include <algorithm>
43#include <cmath>
44#include <cstdint>
45#include <numbers>
46
47namespace dspark {
48
55template <FloatType T>
57{
58public:
60 struct Settings
61 {
62 double speedUpSeconds = 0.45;
63 double slowDownSeconds = 0.7;
64 double restoreSeconds = 1.5;
65 double maxStep = 0.02;
66 double reel = 0.0012;
67 double capstan = 0.0006;
68 double capstanSecond = 0.3;
69 double noise = 0.0004;
70 double scrape = 0.003;
71 };
72
81 void prepare(double sampleRate, double nominalSamples, double ips, const Settings& settings = Settings {}) noexcept
82 {
83 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0)) return;
84 fs_ = sampleRate;
85 s_ = settings;
86 nominal_ = std::max(1.0, nominalSamples);
87 ips_ = std::max(0.1, ips);
88 const double dt = 1.0 / fs_;
89 up_ = std::exp(-dt / s_.speedUpSeconds);
90 down_ = std::exp(-dt / s_.slowDownSeconds);
91 restore_ = dt / s_.restoreSeconds;
92 // The noises and the scrape run at the control rate.
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);
97 {
98 // RMS of a uniform noise in [-1, 1) through two such one-poles.
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)));
102 }
103 lo_ = 3.0;
104 hi_ = 1e12;
105 reset();
106 }
107
109 void reset() noexcept
110 {
111 speed_ = speedTarget_ = 1.0;
112 delay_ = nominal_;
113 capPhase_ = reelPhase_ = 0.0;
114 noise1_ = noise2_ = scrape1_ = scrape2_ = 0.0;
115 rng_ = 0x2C1B3C6Du;
116 devFrom_ = devTo_ = 0.0;
117 controlAt_ = 0;
118 }
119
121 void setSpeed(double ips) noexcept
122 {
123 if (!std::isfinite(ips) || !(ips > 0.0) || ips == ips_) return;
124 speed_ *= ips_ / ips;
125 ips_ = ips;
126 }
127
129 void setSpeedNow(double ips) noexcept
130 {
131 if (!std::isfinite(ips) || !(ips > 0.0)) return;
132 ips_ = ips;
133 speed_ = speedTarget_ = 1.0;
134 }
135
140 void setTargetDelay(double samples) noexcept
141 {
142 if (!std::isfinite(samples)) return;
143 speedTarget_ = nominal_ / std::clamp(samples, std::max(lo_, 1e-3), hi_);
144 }
145
147 void setTargetDelayNow(double samples) noexcept
148 {
149 if (!std::isfinite(samples)) return;
150 setTargetDelay(samples);
151 speed_ = speedTarget_;
152 delay_ = std::clamp(samples, lo_, hi_);
153 }
154
156 void setLimits(double lo, double hi) noexcept
157 {
158 lo_ = std::max(0.0, lo);
159 hi_ = std::max(lo_, hi);
160 delay_ = std::clamp(delay_, lo_, hi_);
161 }
162
164 void syncDelay(double samples) noexcept { delay_ = std::clamp(samples, lo_, hi_); }
165
166 [[nodiscard]] double getDelay() const noexcept { return delay_; }
167 [[nodiscard]] double getNominal() const noexcept { return nominal_; }
168 [[nodiscard]] double getSpeedRatio() const noexcept { return speed_; }
169
174 [[nodiscard]] double next(double depth) noexcept
175 {
176 speed_ = speedTarget_ + ((speed_ < speedTarget_) ? up_ : down_) * (speed_ - speedTarget_);
177 if (std::abs(speed_ - speedTarget_) < 1e-12) speed_ = speedTarget_;
178 // The deviation of the control point ahead; a straight line to it.
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_);
186 return delay_;
187 }
188
189private:
190 static constexpr int kControl = 16;
191
194 void advanceControl(double depth) noexcept
195 {
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;
203 // One draw per control step: scaled by 1 / sqrt(kControl) it holds
204 // the spectrum the filters gave a draw per sample.
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;
210 devFrom_ = devTo_;
211 devTo_ = deviationNow(depth);
212 }
213
215 [[nodiscard]] double deviationNow(double depth) const noexcept
216 {
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_)
222 * (reel
223 + s_.capstan * (std::sin(cap) + s_.capstanSecond * std::sin(2.0 * cap))
224 + s_.noise * noiseNorm_ * noise2_
225 + s_.scrape * (scrape1_ - scrape2_));
226 }
227
228 static constexpr double kControlNoise = 0.25;
229
230 static constexpr double kCapstanHzPerIps = 1.0 / 3.0;
231 static constexpr double kReelHzPerIps = 0.04;
232
233 Settings s_ {};
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;
242 int controlAt_ = 0;
243};
244
245} // namespace dspark
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...).
Definition DspMath.h:51
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).