18template <
typename SampleType =
float>
28 void reset() noexcept { phase_ = SampleType{0}; }
33 if (!std::isfinite(normalized_phase) || normalized_phase < SampleType{0} ||
34 normalized_phase >= SampleType{1}) {
37 phase_ = normalized_phase;
38 tri_state_ = SampleType{0};
44 SampleType dt = freq_ / sample_rate_;
49 out = std::sin(SampleType{2.0f} * pi * phase_);
53 out = SampleType{2.0f} * phase_ - SampleType{1.0f};
54 out -= poly_blep(phase_, dt);
58 out = phase_ < SampleType{0.5f} ? SampleType{1.0f} : SampleType{-1.0f};
59 out += poly_blep(phase_, dt);
60 out -= poly_blep(std::fmod(phase_ + SampleType{0.5f}, SampleType{1.0f}), dt);
66 out = phase_ < SampleType{0.5f} ? SampleType{1.0f} : SampleType{-1.0f};
67 out += poly_blep(phase_, dt);
68 out -= poly_blep(std::fmod(phase_ + SampleType{0.5f}, SampleType{1.0f}), dt);
70 tri_state_ = dt * out + (SampleType{1.0f} - dt) * tri_state_;
72 out = tri_state_ * SampleType{4.0f};
78 if (phase_ >= SampleType{1.0f}) phase_ -= SampleType{1.0f};
83 SampleType
phase()
const {
return phase_; }
87 static constexpr SampleType pi = SampleType{3.14159265358979323846f};
89 SampleType sample_rate_ = SampleType{44100.0f};
90 SampleType freq_ = SampleType{440.0f};
91 SampleType phase_ = 0;
92 SampleType tri_state_ = 0;
96 static SampleType poly_blep(SampleType t, SampleType dt) {
99 return t +
t -
t *
t - SampleType{1.0f};
101 if (t > SampleType{1.0f} - dt) {
102 t = (
t - SampleType{1.0f}) / dt;
103 return t *
t +
t +
t + SampleType{1.0f};
105 return SampleType{0.0f};
Definition oscillator.hpp:19
void set_frequency(SampleType hz)
Definition oscillator.hpp:24
void set_waveform(Waveform w)
Definition oscillator.hpp:25
void set_sample_rate(SampleType sr)
Definition oscillator.hpp:23
Waveform
Definition oscillator.hpp:21
@ square
Definition oscillator.hpp:21
@ sine
Definition oscillator.hpp:21
@ saw
Definition oscillator.hpp:21
@ triangle
Definition oscillator.hpp:21
SampleType frequency() const
Definition oscillator.hpp:84
SampleType phase() const
Definition oscillator.hpp:83
SampleType next()
Definition oscillator.hpp:43
void reset() noexcept
Definition oscillator.hpp:28
bool reset_phase(SampleType normalized_phase) noexcept
Definition oscillator.hpp:32
Definition additive_bank.hpp:218
OscillatorT< double > Oscillator64
Definition oscillator.hpp:110
OscillatorT< float > Oscillator
Definition oscillator.hpp:109
@ t
Definition input_events.hpp:41