OpenWareLaboratory
SquareWaveOscillator.h
Go to the documentation of this file.
1 #ifndef __SquareWaveOscillator_h
2 #define __SquareWaveOscillator_h
3 
4 #include "Oscillator.h"
5 
6 class SquareWaveOscillator : public OscillatorTemplate<SquareWaveOscillator> {
7 protected:
8  float pw = 0.5;
9 public:
10  static constexpr float begin_phase = 0;
11  static constexpr float end_phase = 1;
14  setSampleRate(sr);
15  }
19  void setPulseWidth(float value){
20  pw = value;
21  }
22  float getSample(){
23  return phase < pw ? 1 : -1;
24  }
25 };
26 
27 class InvertedSquareWaveOscillator : public OscillatorTemplate<InvertedSquareWaveOscillator> {
28 protected:
29  float pw = 0.5;
30 public:
31  static constexpr float begin_phase = 0;
32  static constexpr float end_phase = 1;
36  void setPulseWidth(float value){
37  pw = value;
38  }
39  float getSample(){
40  return phase < pw ? -1 : 1;
41  }
42 };
43 
44 class AntialiasedSquareWaveOscillator : public OscillatorTemplate<AntialiasedSquareWaveOscillator> {
45 protected:
46  float pw = 0.5f;
47 public:
48  static constexpr float begin_phase = 0;
49  static constexpr float end_phase = 1;
53  void setPulseWidth(float value){
54  pw = value;
55  }
56  float getSample(){
57  float sample = phase < pw ? 1 : -1;
58  sample += polyblep(phase, incr);
59  sample -= polyblep(fmod(phase + 1 - pw, 1), incr);
60  return sample;
61  }
62  void generate(FloatArray output){
63  float sample;
64  if(phase < pw){
65  sample = 1;
66  if(phase < incr){
67  float t = phase / incr;
68  sample += t+t - t*t - 1;
69  }
70  }else{
71  sample = -1;
72  if(phase-pw < incr){
73  float t = fmod(phase + 1 - pw, 1) / incr;
74  sample -= t+t - t*t - 1;
75  }
76  }
77  size_t len = output.getSize();
78  float* dest = output.getData();
79  while(len--){
80  phase += incr;
81  if(phase >= pw){
82  if(phase >= 1){
83  // wrap phase
84  phase -= 1;
85  // correct current sample
86  float t = (phase - incr) / incr;
87  sample += t*t + t+t + 1;
88  *dest++ = sample;
89  sample = 1;
90  // correct next sample
91  t = phase / incr;
92  sample += t+t - t*t - 1;
93  }else if(sample == 1){
94  // correct current sample
95  float t = (fmod(phase + 1 - pw, 1) - incr) / incr;
96  sample -= t*t + t+t + 1;
97  *dest++ = sample;
98  sample = -1;
99  // correct next sample
100  t = fmod(phase + 1 - pw, 1) / incr;
101  sample -= t+t - t*t - 1;
102  }else{
103  *dest++ = sample;
104  sample = -1;
105  }
106  }else{
107  *dest++ = sample;
108  sample = 1;
109  }
110  }
111  }
113 };
114 
115 #endif /* __SquareWaveOscillator_h */
static constexpr float begin_phase
void setPulseWidth(float value)
Set pulse width to a value between 0 and 1.
void generate(FloatArray output)
Produce a block of samples.
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
void setPulseWidth(float value)
Set pulse width to a value between 0 and 1.
static constexpr float end_phase
static constexpr float begin_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
size_t getSize() const
Definition: SimpleArray.h:31
T * getData()
Get the data stored in the Array.
Definition: SimpleArray.h:27
static constexpr float end_phase
static constexpr float begin_phase
void setPulseWidth(float value)
Set pulse width to a value between 0 and 1.