91 static_assert(std::is_floating_point_v<T>,
"Oscillator requires float or double");
120 assert(sampleRate > 0.0);
121 if (!(sampleRate > 0.0) || !std::isfinite(sampleRate))
return;
122 sampleRate_ = sampleRate;
127 if (!syncOn_) primeMinBlep();
148 if (freq != freq)
return;
150 T nyquist =
static_cast<T
>(sampleRate_ * 0.5);
151 frequency_ = std::clamp(freq, T(0), nyquist);
161 if (w == waveform_)
return;
166 if (!syncOn_) primeMinBlep();
181 if (mode == antiAliasing_)
return;
182 antiAliasing_ = mode;
183 if (!syncOn_) primeMinBlep();
212 syncRatio_ = std::max(T(0), ratio);
238 if (width != width)
return;
240 if (w == pulseWidth_)
return;
241 const T old = pulseWidth_;
243 pulseDc_ = T(2) * w - T(1);
250 const Phase ph = syncOn_ ? slavePhase_ : phase_;
251 const bool wasHigh = ph < old;
252 const bool isHigh = ph < w;
254 scheduleMinBlep(isHigh ? T(2) : T(-2), T(0));
275 if (!std::isfinite(phase))
return;
278 phase_ =
static_cast<Phase
>(phase) - std::floor(
static_cast<Phase
>(phase));
279 if (phase_ >= 1.0) phase_ -= 1.0;
307 resetAge_ = { kNoReset, kNoReset };
308 if (!syncOn_) primeMinBlep();
321 return nextSyncSample();
323 return nextMinBlepSample();
332 out =
static_cast<T
>(
fastSin(phase_ * twoPi<Phase>));
336 out =
static_cast<T
>(2.0 * phase_ - 1.0 - polyBlep(phase_, phaseInc_));
341 Phase raw = (phase_ < pulseWidth_) ? 1.0 : -1.0;
342 raw += polyBlep(phase_, phaseInc_);
346 Phase halfPhase = phase_ + (1.0 -
static_cast<Phase
>(pulseWidth_));
347 if (halfPhase >= 1.0) halfPhase -= 1.0;
349 raw -= polyBlep(halfPhase, phaseInc_);
350 out =
static_cast<T
>(raw -
static_cast<Phase
>(pulseDc_));
356 Phase raw = (phase_ < 0.5) ? 1.0 : -1.0;
357 raw += polyBlep(phase_, phaseInc_);
359 Phase halfPhase = phase_ + 0.5;
360 if (halfPhase >= 1.0) halfPhase -= 1.0;
361 raw -= polyBlep(halfPhase, phaseInc_);
366 const double inc =
static_cast<double>(phaseInc_);
367 triState_ = inc *
static_cast<double>(raw) + (1.0 - inc) * triState_;
368 out =
static_cast<T
>(triState_) * triNorm_;
375 if (phase_ >= 1.0) phase_ -= 1.0;
392 for (
size_t i = 0; i < numSamples; ++i)
408 const int nCh = buffer.getNumChannels();
409 const int nS = buffer.getNumSamples();
412 for (
int i = 0; i < nS; ++i)
416 T* ch0 = buffer.getChannel(0);
417 for (
int i = 0; i < nS; ++i)
419 for (
int ch = 1; ch < nCh; ++ch)
420 std::copy_n(ch0,
static_cast<size_t>(nS), buffer.getChannel(ch));
423 [[nodiscard]] T
getPhase() const noexcept {
return static_cast<T
>(phase_); }
431 [[nodiscard]] T rawSlaveValue(Phase ph)
const noexcept
468 void seedTriangle() noexcept
471 Phase ph = syncOn_ ? slavePhase_ : phase_;
472 const Phase peak =
static_cast<Phase
>(triExpectedPeak_);
475 ph -= 0.5 * phaseInc_;
476 ph -= std::floor(ph);
478 const Phase decay = std::log1p(-phaseInc_) / phaseInc_;
479 triState_ = (ph < 0.5) ? -peak - (1.0 + peak) * std::expm1(ph * decay)
480 : peak + (1.0 + peak) * std::expm1((ph - 0.5) * decay);
485 ph -= phaseInc_ * blepDelay_;
486 ph -= std::floor(ph);
488 const Phase ideal = (ph < 0.5) ? (4.0 * ph - 1.0) : (3.0 - 4.0 * ph);
489 triState_ = ideal * peak;
504 void primeMinBlep() noexcept
509 || !(phaseInc_ > 0.0))
512 const Phase period = 1.0 / phaseInc_;
513 const auto primeEdge = [&](Phase edgePhase, T jump)
noexcept {
516 Phase age = (phase_ - edgePhase) * period;
517 if (age < 0.0) age += period;
518 for (; age < static_cast<Phase>(kCorrLen); age += period)
519 scheduleMinBlep(jump, age);
523 primeEdge(0.0, T(-2));
527 primeEdge(0.0, T(2));
528 primeEdge(fallingEdge(), T(-2));
534 [[nodiscard]] Phase fallingEdge() const noexcept
536 return waveform_ ==
Waveform::Square ?
static_cast<Phase
>(pulseWidth_) : 0.5;
548 [[nodiscard]] T nextMinBlepSample() noexcept
550 T raw = rawSlaveValue(phase_) + corr_[
static_cast<size_t>(corrHead_)];
551 corr_[
static_cast<size_t>(corrHead_)] = T(0);
552 corrHead_ = (corrHead_ + 1) & kCorrMask;
561 out -=
static_cast<T
>(2.0 * phaseInc_ * blepDelay_);
571 const double inc =
static_cast<double>(phaseInc_);
572 triState_ = inc *
static_cast<double>(raw) + (1.0 - inc) * triState_;
573 out =
static_cast<T
>(triState_) * triNorm_;
576 const Phase old = phase_;
585 const Phase alpha = (1.0 - old) / phaseInc_;
586 scheduleMinBlep(T(-2), 1.0 - alpha);
597 const Phase edge = fallingEdge();
598 if (old < edge && phase_ >= edge)
600 const Phase alpha = (edge - old) / phaseInc_;
601 scheduleMinBlep(T(-2), 1.0 - alpha);
605 const Phase alpha = (1.0 - old) / phaseInc_;
606 scheduleMinBlep(T(2), 1.0 - alpha);
607 if (phase_ >= 1.0 + edge)
609 const Phase alphaFall = (1.0 + edge - old) / phaseInc_;
610 scheduleMinBlep(T(-2), 1.0 - alphaFall);
615 if (phase_ >= 1.0) phase_ -= 1.0;
631 [[nodiscard]] T nextSyncSample() noexcept
633 const bool squareLike =
639 ?
static_cast<T
>(
fastSin(delayedSinePhase() * twoPi<Phase>))
640 : rawSlaveValue(slavePhase_);
641 T raw = naive + corr_[
static_cast<size_t>(corrHead_)];
642 corr_[
static_cast<size_t>(corrHead_)] = T(0);
643 corrHead_ = (corrHead_ + 1) & kCorrMask;
651 out -=
static_cast<T
>(2.0 * slaveInc_ * blepDelay_);
663 const double inc =
static_cast<double>(slaveInc_);
664 triState_ = inc * (
static_cast<double>(raw) -
static_cast<double>(syncSquareDc_))
665 + (1.0 - inc) * triState_;
666 out =
static_cast<T
>(triState_) * triNorm_;
670 const Phase masterOld = phase_;
671 const Phase slaveOld = slavePhase_;
673 slavePhase_ += slaveInc_;
675 const bool masterWrap = phase_ >= 1.0;
676 const Phase alphaSync = masterWrap ? (1.0 - masterOld) / phaseInc_ : 2.0;
683 const auto naturalEdge = [&](Phase at, T jump)
noexcept {
684 const Phase alphaEdge = (at - slaveOld) / slaveInc_;
685 if (alphaEdge <= alphaSync && alphaEdge <= 1.0)
686 scheduleMinBlep(jump, 1.0 - alphaEdge);
692 const Phase edge = fallingEdge();
693 if (slaveOld < edge && slavePhase_ >= edge)
694 naturalEdge(edge, T(-2));
695 if (slavePhase_ >= 1.0)
697 naturalEdge(1.0, T(2));
698 if (slavePhase_ >= 1.0 + edge)
699 naturalEdge(1.0 + edge, T(-2));
702 else if (slavePhase_ >= 1.0)
704 naturalEdge(1.0, T(-2));
707 if (slavePhase_ >= 1.0) slavePhase_ -= 1.0;
713 Phase atJump = slaveOld + alphaSync * slaveInc_;
714 atJump -= std::floor(atJump);
715 const T jump = rawSlaveValue(0.0) - rawSlaveValue(atJump);
721 const Phase age = 1.0 - alphaSync;
722 const auto slopeJump =
static_cast<T
>(
723 twoPi<Phase> * slaveInc_ * (1.0 - std::cos(twoPi<Phase> * atJump)));
724 table.addDelayedStep(corr_.data(), corrHead_, jump,
static_cast<T
>(age));
725 table.addDelayedRamp(corr_.data(), corrHead_, slopeJump,
static_cast<T
>(age));
726 resetAge_[1] = resetAge_[0];
727 prePhase_[1] = prePhase_[0];
729 prePhase_[0] = atJump;
731 else if (jump != T(0))
733 scheduleMinBlep(jump, 1.0 - alphaSync);
737 slavePhase_ = (phase_ / std::max(phaseInc_, 1e-12)) * slaveInc_;
738 slavePhase_ -= std::floor(slavePhase_);
750 void scheduleMinBlep(T jump, Phase frac)
noexcept
764 void reseedSlave() noexcept
766 const Phase r = slaveInc_ / std::max(phaseInc_, 1e-12);
767 slavePhase_ = phase_ * r;
768 slavePhase_ -= std::floor(slavePhase_);
771 resetAge_ = { kNoReset, kNoReset };
783 [[nodiscard]] Phase delayedSinePhase() const noexcept
785 const Phase d = blepDelay_;
787 if (resetAge_[0] >= d) ph = slavePhase_ - slaveInc_ * d;
788 else if (resetAge_[1] >= d) ph = prePhase_[0] - slaveInc_ * (d - resetAge_[0]);
789 else ph = prePhase_[1] - slaveInc_ * (d - resetAge_[1]);
790 return ph - std::floor(ph);
800 static inline Phase polyBlep(Phase phase, Phase inc)
noexcept
802 if (inc < 1e-10)
return 0.0;
806 Phase t = phase / inc;
807 return t + t - t * t - 1.0;
809 else if (phase > 1.0 - inc)
811 Phase t = (phase - 1.0) / inc;
812 return t * t + t + t + 1.0;
818 void updatePhaseInc() noexcept
824 phaseInc_ = std::min(
static_cast<Phase
>(frequency_) / sampleRate_, 0.5);
825 const bool wasOn = syncOn_;
826 syncOn_ = syncRatio_ > T(1.001) && phaseInc_ > 0.0;
828 slaveInc_ = std::min(phaseInc_ *
static_cast<Phase
>(syncRatio_), 0.5);
836 if (syncOn_ && phaseInc_ > 0.0)
838 const Phase r = slaveInc_ / phaseInc_;
839 const Phase f = r - std::floor(r);
840 syncSquareDc_ = (std::min(f, 0.5) - std::max(f - 0.5, 0.0)) / r;
845 if (syncOn_ && !wasOn)
859 void updateTriNorm() noexcept
862 const Phase inc = syncOn_ ? slaveInc_ : phaseInc_;
863 if (inc == triNormInc_)
866 if (inc > 0.0 && inc < 1.0)
879 if (std::abs(inc - triAnchorInc_) <= 1e-3 * triAnchorInc_)
881 expectedPeak = triAnchorPeak_ + triAnchorSlope_ * (inc - triAnchorInc_);
885 const Phase lnQ = 0.5 * std::log1p(-inc) / inc;
886 const Phase q = std::exp(lnQ);
887 expectedPeak = (1.0 - q) / (1.0 + q);
889 const Phase dLnQ = 0.5 * (-inc / (1.0 - inc) - std::log1p(-inc)) / (inc * inc);
891 triAnchorPeak_ = expectedPeak;
892 triAnchorSlope_ = -2.0 / ((1.0 + q) * (1.0 + q)) * q * dLnQ;
894 triExpectedPeak_ =
static_cast<T
>(expectedPeak);
895 triNorm_ = (expectedPeak > 0.001) ?
static_cast<T
>(1.0 / expectedPeak) : T(4);
899 triExpectedPeak_ = T(0.25);
904 double sampleRate_ = 48000.0;
905 T frequency_ = T(440);
907 Phase phaseInc_ = 0.0;
908 double triState_ = 0.0;
910 T triExpectedPeak_ = T(0.25);
911 Phase triNormInc_ = -1.0;
912 Phase triAnchorInc_ = -1.0;
913 Phase triAnchorPeak_ = 0.25;
914 Phase triAnchorSlope_ = 0.0;
917 Phase blepDelay_ = 0.0;
921 static constexpr int kCorrMask = kCorrLen - 1;
922 static_assert((kCorrLen & kCorrMask) == 0,
"minBLEP ring needs a power-of-two span");
924 bool syncOn_ =
false;
926 Phase slavePhase_ = 0.0;
927 Phase slaveInc_ = 0.0;
928 Phase syncSquareDc_ = 0.0;
929 static constexpr Phase kNoReset = 1e9;
930 std::array<Phase, 2> resetAge_{ kNoReset, kNoReset };
931 std::array<Phase, 2> prePhase_{};
932 T pulseWidth_ = T(0.5);
934 std::array<T, kCorrLen> corr_{};