Plaquette
 
Loading...
Searching...
No Matches
AbstractOscillator.h
1/*
2 * AbstractOscillator.h
3 *
4 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2025 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef ABSTRACT_OSCILLATOR_H_
22#define ABSTRACT_OSCILLATOR_H_
23
24#include "PqCore.h"
25#include "Timeable.h"
26#include "pq_phase_utils.h"
27
28namespace pq {
29
34public:
40
41 virtual ~AbstractOscillator() {}
42
47 virtual void period(float period);
48
50 virtual float period() const { return _period; }
51
56 virtual void frequency(float frequency);
57
59 virtual float frequency() const {
60#if PQ_OPTIMIZE_FOR_CPU
61 return _frequency;
62#else
63 return periodToFrequency(_period);
64#endif
65 }
66
71 virtual void bpm(float bpm);
72
74 virtual float bpm() const { return frequency() * HZ_TO_BPM; }
75
80 virtual void phase(float phase);
81
83 virtual float phase() const { return pq::fixed32ToFloat(_phase32); }
84
91 virtual void phaseShift(float phaseShift);
92
97 virtual float phaseShift() const;
98
104 virtual float timeToPhase(float time) const;
105
110 virtual void setTime(float time);
111
116 virtual void addTime(float time);
117
119 virtual bool isRunning() const { return _isRunning; }
120
122 virtual bool isForward() const { return _isForward; }
123
128 virtual void setForward(bool isForward) { _isForward = isForward; }
129
131 virtual void forward() { _isForward = true; }
132
134 virtual void reverse() { _isForward = false; }
135
137 virtual void toggleReverse() { _isForward = !_isForward; }
138
140 virtual float randomness() const;
141
143 virtual void randomize(float randomness=1.0f);
144
146 virtual void noRandomize() { randomize(0.0f); }
147
148protected:
149 // Perform step with under
150 void _stepPhase(float deltaTimeSecondsTimesFixed32Max);
151
152 // Sets phase time.
153 virtual void _setPhase32(q0_32u_t phase32);
154
155 // Sets running state.
156 virtual void _setRunning(bool isRunning);
157
158 // Picks next random frequency multiplier (for randomized oscillation).
159 void _randomPickNext();
160
161 // Period (seconds).
162 float _period;
163
164#if PQ_OPTIMIZE_FOR_CPU
165 // Frequency (Hz).
166 float _frequency;
167#endif
168
169 // Non-random mode: Phase shift (in % of period).
170 // Random mode: random frequency ratio (in % of frequency).
171 float _phaseShiftOrRandomFrequencyRatio = 0;
172
173 // Internal use: holds current phase time.
174 q0_32u_t _phase32;
175
176 // Internal use : overflow indicator
177 bool _overflowed : 1;
178
179 // Is the wave currently running?
180 bool _isRunning : 1;
181
182 // The direction of oscillation.
183 bool _isForward : 1;
184
185 // Flag that makes sure the value is updated only on a need basis.
186 bool _valueNeedsUpdate : 1;
187
188 // Randomness level.
189 uint8_t _randomness : 4;
190};
191
192}
193
194#endif
Triangle/sawtooth oscillator.
Definition AbstractOscillator.h:33
virtual float randomness() const
Returns the randomness level in [0, 1].
Definition AbstractOscillator.cpp:63
virtual float period() const
Returns the period (in seconds).
Definition AbstractOscillator.h:50
virtual void toggleReverse()
Toggles the direction of oscillation.
Definition AbstractOscillator.h:137
virtual float phaseShift() const
Returns the phase shift (ie.
Definition AbstractOscillator.cpp:166
virtual void setForward(bool isForward)
Sets the direction of oscillation.
Definition AbstractOscillator.h:128
virtual float timeToPhase(float time) const
Utility function to convert time to phase.
Definition AbstractOscillator.cpp:170
virtual float phase() const
Returns the phase (in % of period).
Definition AbstractOscillator.h:83
virtual bool isForward() const
Returns true iff the wave is moving forward in time.
Definition AbstractOscillator.h:122
virtual void randomize(float randomness=1.0f)
Sets the randomness level in [0, 1] (0: no randomness, 1: full randomness).
Definition AbstractOscillator.cpp:67
virtual void forward()
Sets the direction of oscillation to move forward in time.
Definition AbstractOscillator.h:131
virtual float frequency() const
Returns the frequency (in Hz).
Definition AbstractOscillator.h:59
virtual float bpm() const
Returns the frequency (in BPM).
Definition AbstractOscillator.h:74
virtual void reverse()
Sets the direction of oscillation to move backward in time.
Definition AbstractOscillator.h:134
virtual void addTime(float time)
Adds time to current time (in seconds).
Definition AbstractOscillator.cpp:179
virtual void noRandomize()
Disables randomness.
Definition AbstractOscillator.h:146
virtual bool isRunning() const
Returns true iff the wave is currently running.
Definition AbstractOscillator.h:119
virtual void setTime(float time)
Forces current time (in seconds).
Definition AbstractOscillator.cpp:172
Abstract class for time-based objects.
Definition Timeable.h:27