MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin_Oscillator.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef MARVIN_OSCILLATOR_H
12#define MARVIN_OSCILLATOR_H
18
22 enum class Bandlimiting {
25 };
26
30 template <FloatType SampleType>
32 public:
33 virtual ~OscillatorBase() noexcept = default;
38 virtual void initialise(double sampleRate);
39
44 [[nodiscard]] virtual SampleType operator()() noexcept = 0;
51 [[nodiscard]] virtual SampleType operator()(SampleType phase) noexcept = 0;
55 virtual void reset() noexcept;
56
62 void setFrequency(SampleType newFrequency) noexcept;
63
69 void setPhaseOffset(SampleType newPhaseOffset) noexcept;
70
71 protected:
75 void incrementPhase() noexcept;
76
77 double m_sampleRate{};
78 SampleType m_phaseIncrement{ static_cast<SampleType>(0.0) };
79 SampleType m_phase{ static_cast<SampleType>(0.0) };
80 SampleType m_phaseOffset{ static_cast<SampleType>(0.0) };
81 };
82
86 template <FloatType SampleType>
87 class SineOscillator final : public OscillatorBase<SampleType> {
88 public:
89 ~SineOscillator() noexcept override = default;
90 [[nodiscard]] SampleType operator()() noexcept override;
91 [[nodiscard]] SampleType operator()(SampleType phase) noexcept override;
92 };
93
97 template <FloatType SampleType, Bandlimiting Blamp = Bandlimiting::Off>
98 class TriOscillator final : public OscillatorBase<SampleType> {
99 public:
100 ~TriOscillator() noexcept override = default;
101 [[nodiscard]] SampleType operator()() noexcept override;
102 [[nodiscard]] SampleType operator()(SampleType phase) noexcept override;
103
104 private:
105 math::LeakyIntegrator<SampleType> m_integrator;
106 };
107
111 template <FloatType SampleType, Bandlimiting Blep = Bandlimiting::Off>
112 class SawOscillator final : public OscillatorBase<SampleType> {
113 public:
114 ~SawOscillator() noexcept override = default;
115 [[nodiscard]] SampleType operator()() noexcept override;
116 [[nodiscard]] SampleType operator()(SampleType phase) noexcept override;
117 };
118
122 template <FloatType SampleType, Bandlimiting Blep = Bandlimiting::Off>
123 class SquareOscillator final : public OscillatorBase<SampleType> {
124 public:
125 ~SquareOscillator() noexcept override = default;
126 [[nodiscard]] SampleType operator()() noexcept override;
127 [[nodiscard]] SampleType operator()(SampleType phase) noexcept override;
128 };
129
133 template <FloatType SampleType, Bandlimiting Blep = Bandlimiting::Off>
134 class PulseOscillator final : public OscillatorBase<SampleType> {
135 public:
136 ~PulseOscillator() noexcept override = default;
137 [[nodiscard]] SampleType operator()() noexcept override;
138 [[nodiscard]] SampleType operator()(SampleType phase) noexcept override;
143 void setPulsewidth(SampleType newPulsewidth) noexcept;
144
145 private:
146 SampleType m_pulsewidth{ 0.5 };
147 };
148
152 template <FloatType SampleType>
153 class NoiseOscillator final : public OscillatorBase<SampleType> {
154 public:
159 explicit NoiseOscillator(std::random_device& rd);
160 ~NoiseOscillator() noexcept override = default;
161 [[nodiscard]] SampleType operator()() noexcept override;
166 [[nodiscard]] SampleType operator()(SampleType phase) noexcept override;
167
168 private:
169 utils::Random m_rng;
170 };
171
175 template <FloatType SampleType, Bandlimiting Bandlimit = Bandlimiting::Off>
176 class MultiOscillator final {
177 public:
189
194 explicit MultiOscillator(std::random_device& rd);
200 explicit MultiOscillator(std::random_device& rd, Shape shape);
201
206 void initialise(double sampleRate);
211 [[nodiscard]] SampleType operator()() noexcept;
215 void reset() noexcept;
216
221 void setShape(Shape shape);
226 void setFrequency(SampleType newFrequency) noexcept;
227
232 void setPhaseOffset(SampleType newPhaseOffset) noexcept;
236 void setPulsewidth(SampleType newPulsewidth) noexcept;
237
238 private:
239 void incrementPhase() noexcept;
240 double m_sampleRate{};
241 Shape m_shape{ Shape::Sine };
242 SampleType m_phase{ static_cast<SampleType>(0.0) };
243 SampleType m_phaseIncrement{ static_cast<SampleType>(0.0) };
244 SampleType m_phaseOffset{ static_cast<SampleType>(0.0) };
245 SineOscillator<SampleType> m_sine;
246 TriOscillator<SampleType, Bandlimit> m_tri;
247 SawOscillator<SampleType, Bandlimit> m_saw;
248 SquareOscillator<SampleType, Bandlimit> m_square;
249 PulseOscillator<SampleType, Bandlimit> m_pulse;
250 NoiseOscillator<SampleType> m_noise;
251 };
252} // namespace marvin::dsp::oscillators
253#endif
void setPhaseOffset(SampleType newPhaseOffset) noexcept
MultiOscillator(std::random_device &rd)
void setFrequency(SampleType newFrequency) noexcept
Shape
Definition marvin_Oscillator.h:181
@ Triangle
Definition marvin_Oscillator.h:183
@ Sine
Definition marvin_Oscillator.h:182
@ Noise
Definition marvin_Oscillator.h:187
@ Saw
Definition marvin_Oscillator.h:184
@ Square
Definition marvin_Oscillator.h:185
@ Pulse
Definition marvin_Oscillator.h:186
void setPulsewidth(SampleType newPulsewidth) noexcept
MultiOscillator(std::random_device &rd, Shape shape)
NoiseOscillator(std::random_device &rd)
~NoiseOscillator() noexcept override=default
Base class for all single-shape oscillator types.
Definition marvin_Oscillator.h:31
virtual void initialise(double sampleRate)
SampleType m_phaseOffset
Definition marvin_Oscillator.h:80
virtual ~OscillatorBase() noexcept=default
SampleType m_phase
Definition marvin_Oscillator.h:79
SampleType m_phaseIncrement
Definition marvin_Oscillator.h:78
void setPhaseOffset(SampleType newPhaseOffset) noexcept
double m_sampleRate
Definition marvin_Oscillator.h:77
void setFrequency(SampleType newFrequency) noexcept
A pulse oscillator, with optional BLEP, and pulsewidth control.
Definition marvin_Oscillator.h:134
void setPulsewidth(SampleType newPulsewidth) noexcept
~PulseOscillator() noexcept override=default
A sawtooth oscillator, with optional BLEP.
Definition marvin_Oscillator.h:112
~SawOscillator() noexcept override=default
A sine oscillator.
Definition marvin_Oscillator.h:87
~SineOscillator() noexcept override=default
A square oscillator, with optional BLEP.
Definition marvin_Oscillator.h:123
~SquareOscillator() noexcept override=default
A triangle oscillator, with optional BLAMP.
Definition marvin_Oscillator.h:98
~TriOscillator() noexcept override=default
Contrains T to be either a float or a double.
Definition marvin_Concepts.h:27
Oscillator functions and classes..
Definition marvin_Oscillator.h:17
Bandlimiting
Enum to configure PolyBLEP or BLAMP where applicable, to classes deriving from marvin::oscillators::O...
Definition marvin_Oscillator.h:22
@ On
Definition marvin_Oscillator.h:24
@ Off
Definition marvin_Oscillator.h:23
Math helper functions and classes.
Definition marvin_Math.h:22
Utility helper functions and classes.
Definition marvin_Utils.h:21