OpenWareLaboratory
RampOscillator.h
Go to the documentation of this file.
1 #ifndef __RampOscillator_h
2 #define __RampOscillator_h
3 
4 #include "Oscillator.h"
5 
9 class RampOscillator : public OscillatorTemplate<RampOscillator> {
10 public:
11  static constexpr float begin_phase = -1;
12  static constexpr float end_phase = 1;
14  RampOscillator(float sr){
15  setSampleRate(sr);
16  }
17  float getSample(){
18  return phase;
19  }
20 };
21 
25 class InvertedRampOscillator : public OscillatorTemplate<InvertedRampOscillator> {
26 public:
27  static constexpr float begin_phase = -1;
28  static constexpr float end_phase = 1;
31  setSampleRate(sr);
32  }
33  float getSample(){
34  return -phase;
35  }
36 };
37 
38 class AntialiasedRampOscillator : public OscillatorTemplate<AntialiasedRampOscillator> {
39 protected:
40  float lastblep;
41 public:
42  static constexpr float begin_phase = 0;
43  static constexpr float end_phase = 1;
46  setSampleRate(sr);
47  }
48  void setPhase(float ph){
49  lastblep = 0;
51  }
52  void reset(){
53  lastblep = 0;
55  }
56  float getSample(){
57  float sample = 2*phase-1; // naive ramp
58  sample -= polyblep(phase, incr);
59  return sample;
60  }
65  void generate(FloatArray output){
66  size_t len = output.getSize();
67  float blep = lastblep;
68  for(size_t i=0; i<len; ++i){
69  float sample = 2*phase-1;
70  sample -= blep;
71  phase += incr;
72  if(phase >= 1){
73  // wrap phase
74  phase -= 1;
75  // correct current sample
76  float t = (phase - incr) / incr;
77  sample -= t*t + t+t + 1;
78  // correct next sample
79  t = phase / incr;
80  blep = t+t - t*t - 1;
81  }else{
82  blep = 0;
83  }
84  output[i] = sample;
85  }
86  lastblep = blep; // carry over polyblep correction
87  }
89 };
90 
91 #endif /* __RampOscillator_h */
void setPhase(float ph)
Set current oscillator phase in radians.
void generate(FloatArray output)
Note: mixing sample based and block based generate() calls is not supported by this class.
static constexpr float begin_phase
static constexpr float end_phase
void reset()
Reset oscillator (typically resets phase)
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
Inverted ramp oscillator generates falling output values from 1 to -1.
static constexpr float begin_phase
InvertedRampOscillator(float sr)
static constexpr float end_phase
static float polyblep(float t, float dt)
Calculate poly blep antialiasing compensation on normalised (to range [0, 1]) phase and phase increme...
Definition: Oscillator.h:128
void setPhase(float ph)
Set current oscillator phase in radians.
Definition: Oscillator.h:89
void reset()
Reset oscillator (typically resets phase)
Definition: Oscillator.h:96
Ramp oscillator generates rising output values from -1 to 1.
Definition: RampOscillator.h:9
RampOscillator(float sr)
static constexpr float begin_phase
static constexpr float end_phase
size_t getSize() const
Definition: SimpleArray.h:31