127 constexpr double kMinAllowedHz = 0.1;
128 constexpr double kMaxAllowedSemitones = 4.0;
129 const double maxDeviation =
130 (kMaxAllowedSemitones * std::numbers::ln2 * sampleRate_)
131 / (2.0 * std::numbers::pi * kMinAllowedHz * 12.0);
132 const double required =
133 2.0 * maxDeviation +
static_cast<double>(kCentreOffset) + 128.0;
134 const int maxDelaySamples =
135 static_cast<int>(std::min(required,
static_cast<double>(1 << 28)));
137 delays_.resize(numChannels_);
138 phasors_.resize(numChannels_);
139 modPhasors_.resize(numChannels_);
140 readPos_.assign(
static_cast<size_t>(numChannels_), -1.0);
141 sinc_ = &sincTable();
143 bbd_.resize(
static_cast<size_t>(numChannels_));
144 noiseLp_.assign(
static_cast<size_t>(numChannels_), 0.0);
145 noiseRng_.resize(
static_cast<size_t>(numChannels_));
146 for (
int ch = 0; ch < numChannels_; ++ch)
148 delays_[ch].prepare(maxDelaySamples);
149 phasors_[ch].prepare(sampleRate_);
150 modPhasors_[ch].prepare(sampleRate_);
151 bbd_[
static_cast<size_t>(ch)].
prepare(sampleRate_, kAnalogStages, 0.45 * sampleRate_);
152 noiseRng_[
static_cast<size_t>(ch)] = 0x5A17C3u + 0x9E3779B9u *
static_cast<uint32_t
>(ch);
154 analogCornerFor_ = -1.0;
157 currentRate_ = rate_.load(std::memory_order_relaxed);
158 currentDepth_ = depthSemitones_.load(std::memory_order_relaxed);
159 currentModDepth_ = modDepth_.load(std::memory_order_relaxed);
168 const int numCh = std::min(buffer.getNumChannels(), numChannels_);
169 const int numSamples = buffer.getNumSamples();
170 if (numSamples == 0 || numCh == 0)
return;
173 const T targetRate = rate_.load(std::memory_order_relaxed);
174 const T targetDepth = depthSemitones_.load(std::memory_order_relaxed);
175 const T modRate = modRate_.load(std::memory_order_relaxed);
176 const T targetModDepth = modDepth_.load(std::memory_order_relaxed);
184 const T rateInc = (targetRate - currentRate_) /
static_cast<T
>(numSamples);
185 const T depthInc = (targetDepth - currentDepth_) /
static_cast<T
>(numSamples);
186 const T modDepthInc = (targetModDepth - currentModDepth_) /
static_cast<T
>(numSamples);
191 const bool fmActive = (targetModDepth > T(0)) || (currentModDepth_ > T(0));
193 constexpr double kTwoPi = 2.0 * std::numbers::pi;
194 const double deviationScaler = (std::numbers::ln2 * sampleRate_) / (kTwoPi * 12.0);
195 const bool analog = model_.load(std::memory_order_relaxed) ==
Model::Analog;
196 const bool sinc = !analog && sinc_ && interpolation_.load(std::memory_order_relaxed) ==
Interpolation::Sinc;
200 const double centreOffset = analog ? kAnalogTrough
202 :
static_cast<double>(kCentreOffset);
203 const double maxRead =
static_cast<double>(delays_[0].getCapacity()
206 double noiseAmp = 0.0;
211 const auto deviationOf = [&](T depth, T rate) {
212 return static_cast<double>(depth) * deviationScaler / std::max(
static_cast<double>(rate), 0.1);
214 const double peak = 2.0 * std::max(deviationOf(currentDepth_, currentRate_), deviationOf(targetDepth, targetRate))
216 const double slowest =
static_cast<double>(kAnalogStages) * sampleRate_ / (2.0 * peak);
217 if (std::abs(slowest - analogCornerFor_) > 1e-3 * slowest)
219 const double corner = std::min(0.2 * slowest, 0.45 * sampleRate_);
220 for (
auto& b : bbd_) b.setCorner(corner);
221 noiseCoeff_ = 1.0 - std::exp(-2.0 * std::numbers::pi * corner / sampleRate_);
222 analogCornerFor_ = slowest;
224 const T noiseDb = analogNoiseDb_.load(std::memory_order_relaxed);
228 if (noiseDb > T(-120))
229 noiseAmp = std::pow(10.0,
static_cast<double>(noiseDb) / 20.0) * 1.7320508075688772
230 / std::sqrt(std::max(1e-9, noiseCoeff_ / (2.0 - noiseCoeff_)));
233 for (
int ch = 0; ch < numCh; ++ch)
235 T* data = buffer.getChannel(ch);
236 auto& delay = delays_[ch];
237 auto& phasor = phasors_[ch];
238 auto& modPhasor = modPhasors_[ch];
240 modPhasor.setFrequency(modRate);
243 T smoothRate = currentRate_;
244 T smoothDepth = currentDepth_;
245 T smoothModDepth = currentModDepth_;
247 for (
int i = 0; i < numSamples; ++i)
249 smoothRate += rateInc;
250 smoothDepth += depthInc;
251 smoothModDepth += modDepthInc;
264 const double baseRate = std::max(
static_cast<double>(smoothRate), 0.1);
265 const double deviation = (
static_cast<double>(smoothDepth) * deviationScaler) / baseRate;
266 const double centre = deviation + centreOffset;
274 speed +=
fastSin(modPhasor.advance() * kTwoPi) *
static_cast<double>(smoothModDepth);
275 phasor.setFrequency(baseRate * std::max(speed, 0.0));
276 const double lfo =
fastSin(phasor.advance() * kTwoPi);
279 const double delaySamples = std::clamp(centre + lfo * deviation,
280 (sinc || analog) ? centreOffset : 1.0, maxRead);
290 double& pos = readPos_[
static_cast<size_t>(ch)];
291 pos = (pos < 0.0) ? delaySamples
292 : pos + std::clamp(delaySamples - pos, -kMaxReadSlope, kMaxReadSlope);
296 const auto c =
static_cast<size_t>(ch);
297 double y =
static_cast<double>(bbd_[c].processSample(data[i], pos / sampleRate_));
300 noiseRng_[c] = noiseRng_[c] * 1664525u + 1013904223u;
301 const double n =
static_cast<double>(noiseRng_[c] >> 8) / 8388608.0 - 1.0;
302 noiseLp_[c] += noiseCoeff_ * (n - noiseLp_[c]);
303 y += noiseAmp * (pos / centre) * noiseLp_[c];
305 data[i] =
static_cast<T
>(y);
309 data[i] = sinc ? readSinc(delay, pos) : readHermite(delay, pos);
315 currentRate_ = targetRate;
316 currentDepth_ = targetDepth;
317 currentModDepth_ = targetModDepth;