144 if (!(sampleRate > 0.0) || !std::isfinite(sampleRate))
return;
145 sampleRate_ = sampleRate;
157 retrigFrom_ =
static_cast<double>(
value());
158 retrigLeft_ = retrigSamples_;
159 cycles_ = startPhase_;
171 if (hz != hz)
return;
183 if (!(bpm >= 1.0) || !(bpm <= 999.0))
return;
191 if (!std::isfinite(phase))
return;
192 startPhase_ = phase - std::floor(phase);
198 if (!(ms >= 0.0) || !std::isfinite(ms))
return;
206 if (!(ms >= 0.0) || !std::isfinite(ms))
return;
212 void setSeed(uint64_t seed)
noexcept { seed_ = seed; stepCacheCycle_ = kNoCycle; }
220 if (!(ms >= 0.0) || !std::isfinite(ms))
return;
230 numSteps_ = std::clamp(count, 1,
kMaxSteps);
231 stepCacheCycle_ = kNoCycle;
233 [[nodiscard]]
int getNumSteps() const noexcept {
return numSteps_; }
245 void setStep(
int index,
double value,
double glide = 0.0,
double curve = 0.0) noexcept
247 if (index < 0 || index >=
kMaxSteps || !std::isfinite(
value) || !std::isfinite(glide) || !std::isfinite(curve))
return;
248 const auto i =
static_cast<size_t>(index);
249 stepValue_[i] = std::clamp(
value, -1.0, 1.0);
250 stepGlide_[i] = std::clamp(glide, 0.0, 1.0);
251 stepCurve_[i] = std::clamp(curve, -1.0, 1.0);
252 stepCacheCycle_ = kNoCycle;
258 if (index < 0 || index >=
kMaxSteps)
return;
259 stepRandom_[
static_cast<size_t>(index)] =
random;
260 stepCacheCycle_ = kNoCycle;
263 [[nodiscard]]
double getStepValue(
int index)
const noexcept {
return stepValue_[stepIndex(index)]; }
264 [[nodiscard]]
double getStepGlide(
int index)
const noexcept {
return stepGlide_[stepIndex(index)]; }
265 [[nodiscard]]
double getStepCurve(
int index)
const noexcept {
return stepCurve_[stepIndex(index)]; }
266 [[nodiscard]]
bool isStepRandom(
int index)
const noexcept {
return stepRandom_[stepIndex(index)]; }
275 if (std::isfinite(swing)) swing_ = std::clamp(swing, -1.0, 1.0);
287 stepQuantize_ = std::clamp(divisions, 0, 1200);
288 stepCacheCycle_ = kNoCycle;
296 [[nodiscard]]
double stepsAt(
double cycles)
const noexcept
298 if (!std::isfinite(cycles)) cycles = 0.0;
299 const double index = std::floor(cycles);
300 const double ph = cycles - index;
301 const auto cycle =
static_cast<int64_t
>(index);
302 if (cycle != stepCacheCycle_) fillStepCache(cycle);
304 const double pos = ph *
static_cast<double>(numSteps_);
305 int step =
static_cast<int>(pos);
306 double within = pos -
static_cast<double>(step);
307 if (swing_ != 0.0 && numSteps_ >= 2)
309 const int pair = step / 2;
310 if (2 * pair + 1 < numSteps_)
312 const double inPair = pos - 2.0 *
static_cast<double>(pair);
313 const double edge = 1.0 + 0.5 * swing_;
314 if (inPair < edge) { step = 2 * pair; within = inPair / edge; }
315 else { step = 2 * pair + 1; within = (inPair - edge) / (2.0 - edge); }
318 step = std::clamp(step, 0, numSteps_ - 1);
321 const size_t at = stepIndex(step);
322 const double to = stepCache_[at];
323 const double from = step > 0 ? stepCache_[stepIndex(step - 1)] : stepCacheBefore_;
324 const double glide = stepGlide_[at];
325 if (!(within < glide))
return to;
326 const double x = within / glide;
327 const double c = stepCurve_[at];
328 const double bent = c > 0.0 ? 1.0 - std::pow(1.0 - x, 1.0 + 4.0 * c)
329 : c < 0.0 ? std::pow(x, 1.0 - 4.0 * c) : x;
330 return from + (to - from) * bent;
334 [[nodiscard]]
double getRateHz() const noexcept {
return increment_ * sampleRate_; }
337 [[nodiscard]]
double getCycles() const noexcept {
return cycles_; }
342 [[nodiscard]] T
value() const noexcept
345 const double v = shaped *
depthAt(age_);
346 if (retrigLeft_ <= 0)
return static_cast<T
>(v);
347 const double share =
static_cast<double>(retrigLeft_) /
static_cast<double>(retrigSamples_);
348 return static_cast<T
>(v + (retrigFrom_ - v) * share);
358 const auto n =
static_cast<int64_t
>(std::max(samples, 0));
359 cycles_ += increment_ *
static_cast<double>(n);
361 retrigLeft_ = std::max<int64_t>(0, retrigLeft_ - n);
372 const auto n =
static_cast<int64_t
>(std::max(samples, 0));
373 const double scale = (rateScale >= 0.0) ? std::min(rateScale, 64.0) : 0.0;
374 cycles_ += increment_ * scale *
static_cast<double>(n);
376 retrigLeft_ = std::max<int64_t>(0, retrigLeft_ - n);
384 [[nodiscard]]
double depthAt(int64_t samples)
const noexcept
386 if (samples < delaySamples_)
return 0.0;
387 if (fadeSamples_ <= 0)
return 1.0;
388 const double x =
static_cast<double>(samples - delaySamples_) /
static_cast<double>(fadeSamples_);
389 return std::min(x, 1.0);
399 [[nodiscard]]
static double valueAt(
Shape shape,
double cycles, uint64_t seed)
noexcept
401 if (!std::isfinite(cycles)) cycles = 0.0;
402 const double index = std::floor(cycles);
403 const double ph = cycles - index;
406 case Shape::Sine:
return std::sin(twoPi<double> * ph);
408 : (ph < 0.75) ? 2.0 - 4.0 * ph
414 return random(seed,
static_cast<int64_t
>(index));
417 const double a =
random(seed,
static_cast<int64_t
>(index));
418 const double b =
random(seed,
static_cast<int64_t
>(index) + 1);
419 return a + (b - a) * 0.5 * (1.0 - std::cos(pi<double> * ph));
427 [[nodiscard]]
static double random(uint64_t seed, int64_t index)
noexcept
429 uint64_t x = seed + 0x9E3779B97F4A7C15ull * (
static_cast<uint64_t
>(index) + 1ull);
430 x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9ull;
431 x = (x ^ (x >> 27)) * 0x94D049BB133111EBull;
433 return static_cast<double>(x >> 11) * (2.0 / 9007199254740992.0) - 1.0;
437 void updateIncrement() noexcept
439 const double hz = sync_ ? (bpm_ / 60.0) /
beatsPerCycle(division_) : rateHz_;
440 increment_ = hz / sampleRate_;
443 void updateEnvelope() noexcept
445 delaySamples_ =
static_cast<int64_t
>(std::llround(delayMs_ * 0.001 * sampleRate_));
446 fadeSamples_ =
static_cast<int64_t
>(std::llround(fadeMs_ * 0.001 * sampleRate_));
447 retrigSamples_ =
static_cast<int64_t
>(std::llround(retrigMs_ * 0.001 * sampleRate_));
448 retrigLeft_ = std::min(retrigLeft_, retrigSamples_);
451 [[nodiscard]]
static size_t stepIndex(
int index)
noexcept {
return static_cast<size_t>(std::clamp(index, 0,
kMaxSteps - 1)); }
456 [[nodiscard]]
double stepValueIn(int64_t cycle,
int step,
double before)
const noexcept
458 double v = stepValue_[stepIndex(step)];
459 if (stepRandom_[stepIndex(step)])
461 const double u = 0.5 * (
random(seed_ ^ 0x5DEECE66Dull, cycle *
kMaxSteps + step) + 1.0);
462 const bool up = before < 0.0;
463 const double room = up ? 1.0 - before : before + 1.0;
464 const double away = room * (0.25 + 0.75 * u);
465 v = std::clamp(up ? before + away : before - away, -1.0, 1.0);
471 [[nodiscard]]
double quantized(
double v)
const noexcept
473 if (stepQuantize_ <= 0)
return v;
474 const double q =
static_cast<double>(stepQuantize_);
475 return std::clamp(std::round(v * q) / q, -1.0, 1.0);
483 void fillStepCache(int64_t cycle)
const noexcept
485 auto walk = [&](int64_t c, std::array<double, kMaxSteps>& out) {
486 const int last = numSteps_ - 1;
487 double before = quantized(stepValue_[stepIndex(last)]);
488 for (
int s = 0; s < numSteps_; ++s)
490 out[stepIndex(s)] = stepValueIn(c, s, before);
491 before = out[stepIndex(s)];
494 std::array<double, kMaxSteps> previous {};
495 walk(cycle - 1, previous);
496 stepCacheBefore_ = previous[stepIndex(numSteps_ - 1)];
497 walk(cycle, stepCache_);
498 stepCacheCycle_ = cycle;
501 double sampleRate_ = 48000.0;
503 double rateHz_ = 1.0;
507 double startPhase_ = 0.0;
508 double delayMs_ = 0.0;
509 double fadeMs_ = 0.0;
513 static constexpr int64_t kNoCycle = INT64_MIN;
515 std::array<double, kMaxSteps> stepValue_ {};
516 std::array<double, kMaxSteps> stepGlide_ {};
517 std::array<double, kMaxSteps> stepCurve_ {};
518 std::array<bool, kMaxSteps> stepRandom_ {};
520 int stepQuantize_ = 0;
521 mutable std::array<double, kMaxSteps> stepCache_ {};
522 mutable double stepCacheBefore_ = 0.0;
523 mutable int64_t stepCacheCycle_ = kNoCycle;
525 double retrigMs_ = 0.0;
526 int64_t retrigSamples_ = 0;
527 int64_t retrigLeft_ = 0;
528 double retrigFrom_ = 0.0;
530 double increment_ = 1.0 / 48000.0;
531 double cycles_ = 0.0;
533 int64_t delaySamples_ = 0;
534 int64_t fadeSamples_ = 0;
Modulation oscillator with tempo sync, delay, fade-in and seeded random shapes.
int getStepQuantize() const noexcept
void setTempo(double bpm) noexcept
Tempo in BPM for tempo sync, 1..999 (invalid values ignored).
void prepare(double sampleRate) noexcept
Prepares for a sample rate (invalid rates are ignored).
double getRateHz() const noexcept
Effective rate in Hz (the synced one when tempo sync is on).
static constexpr int kNumDivisions
double getStepSwing() const noexcept
static double valueAt(Shape shape, double cycles, uint64_t seed) noexcept
The shape's value at an absolute cycle position.
void setDivision(Division d) noexcept
void setStepQuantize(int divisions) noexcept
Quantizes the step values to a grid of 1/divisions (0, the default, leaves them free; 12 puts them on...
static constexpr double beatsPerCycle(Division d) noexcept
Beats (quarter notes) per cycle of a division.
static double random(uint64_t seed, int64_t index) noexcept
Uniform value in [-1, 1) for a cycle index (splitmix64 hash).
double getStepCurve(int index) const noexcept
static constexpr int kNumShapes
void setShape(Shape s) noexcept
double getCycles() const noexcept
Cycles run since reset(), start phase included.
T value() const noexcept
The output now (shape times delay/fade depth), without advancing.
void reset() noexcept
Restarts the cycle at the start phase, the cycle count at zero and the delay and fade-in from the beg...
void setRateHz(double hz) noexcept
Free rate in Hz, 0..kMaxRateHz (NaN ignored). Used while sync is off.
void setNumSteps(int count) noexcept
Steps in a cycle, 1..kMaxSteps (16, the default).
T advance(int samples=1) noexcept
Returns the output now, then advances by samples at the current rate (a control-rate owner advances i...
void setStepRandom(int index, bool random) noexcept
Makes a step draw its value from the seed every cycle (see the file notes).
static constexpr int kMaxSteps
Most steps of the Steps shape.
void setSeed(uint64_t seed) noexcept
Seed of the random shapes (the same seed repeats the same sequence).
void setStartPhase(double phase) noexcept
Cycle position reset() starts at, 0..1 (wrapped).
void setFadeInMs(double ms) noexcept
Linear rise to full depth after the delay, in ms (>= 0).
double stepsAt(double cycles) const noexcept
The Steps shape's value at an absolute cycle position (the integer part is the cycle whose random ste...
void setStep(int index, double value, double glide=0.0, double curve=0.0) noexcept
One step: its value, how much of its length it glides to it from the step before, and the curve of th...
int getNumSteps() const noexcept
bool isStepRandom(int index) const noexcept
T advanceScaled(int samples, double rateScale) noexcept
Advances by samples at rateScale times the current rate (rate modulation; the scale is clamped to 0....
double getStepValue(int index) const noexcept
static constexpr double kMaxRateHz
Highest free rate (Hz).
void setStepSwing(double swing) noexcept
Moves the boundary inside each pair of steps, -1..1: at 0.5 the first step of a pair lasts 1....
void setRetriggerGlideMs(double ms) noexcept
How long reset() glides from the output it was at to the new start, in ms (0, the default,...
void setDelayMs(double ms) noexcept
Silence before the depth starts rising, in ms (>= 0).
double depthAt(int64_t samples) const noexcept
Depth of the delay and fade-in samples after reset(): 0 during the delay, then a linear rise to 1.
Division
Note values for tempo sync (four beats to the whole note).
double getStepGlide(int index) const noexcept
Shape getShape() const noexcept
void setTempoSync(bool on) noexcept
Tempo sync on or off; when on, the rate is the division of the tempo.
Main namespace for the DSPark framework.