OpenWareLaboratory
TriangleOscillator.h
Go to the documentation of this file.
1 #ifndef __TriangleOscillator_h
2 #define __TriangleOscillator_h
3 
4 #include "Oscillator.h"
5 
6 class TriangleOscillator : public OscillatorTemplate<TriangleOscillator> {
7 public:
8  static constexpr float begin_phase = -1;
9  static constexpr float end_phase = 1;
11  TriangleOscillator(float sr){
12  setSampleRate(sr);
13  }
14  float getSample(){
15  return -2 * (fabsf(phase) - 0.5);
16  }
17 };
18 
19 class AntialiasedTriangleOscillator : public OscillatorTemplate<AntialiasedTriangleOscillator> {
20 protected:
21  float previousSample = 0;
22 public:
23  static constexpr float begin_phase = 0;
24  static constexpr float end_phase = 1;
25  float getSample(){
26  float sample = phase < 0.5f ? 1 : -1; // naive square wave
27  sample += polyblep(phase, incr);
28  sample -= polyblep(fmod(phase + 0.5f, 1), incr); // polyblep square wave
29  // Leaky integrator: y[n] = A * x[n] + (1 - A) * y[n-1] = A * (x[n] - y[n-1]) + y[n-1]
30  float lambda = incr*2*M_PI;
31  sample = lambda * (sample - previousSample) + previousSample;
32  previousSample = sample;
33  return sample;
34  }
35 };
36 
37 #endif /* __TriangleOscillator_h */
#define M_PI
Definition: basicmaths.h:52
static constexpr float begin_phase
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
static constexpr float end_phase
static constexpr float begin_phase