OpenWareLaboratory
AgnesiOscillator.h
Go to the documentation of this file.
1 #ifndef __AgnesiOscillator_h
2 #define __AgnesiOscillator_h
3 
4 #include "Oscillator.h"
5 
11 class AgnesiOscillator : public Oscillator {
12 private:
13  const float sr;
14  float a;
15  float N;
16  float incr = 0;
17  float x = 0;
18  float offset = 0;
19  float gain = 1;
20 public:
21  AgnesiOscillator(float sr, float a, float N)
22  : sr(sr), a(a), N(N) {}
23  static float agnesi(float x, float a){
24  return (8*a*a*a) / (x*x + 4*a*a);
25  }
26  void setFrequency(float freq){
27  incr = 2*N*freq/sr;
28  }
29  float getFrequency(){
30  return incr*sr/(2*N);
31  }
32  void setPhase(float phase){
33  x = N*(phase-M_PI)/M_PI;
34  }
35  float getPhase(){
36  return M_PI*x/N+M_PI;
37  }
38  void reset(){
39  x = -N;
40  }
44  void normalise(){
45  offset = agnesi(N, a);
46  gain = 2/(agnesi(0, a) - offset);
47  offset += 0.5;
48  }
49  float generate(){
50  float y = agnesi(x, a);
51  x += incr;
52  if(x > N)
53  x -= 2*N;
54  return clamp(gain*(y-offset), -1.0f, 1.0f);
55  }
56  float generate(float fm){
57  // modulate coefficient 'a' instead of rate
58  float y = agnesi(x, a*(1+fm));
59  x += incr;
60  if(x > N)
61  x -= 2*N;
62  return clamp(gain*(y-offset), -1.0f, 1.0f);
63  }
65  static AgnesiOscillator* create(float sr, float a=0.5, float N=5){
66  AgnesiOscillator* osc = new AgnesiOscillator(sr, a, N);
67  osc->normalise();
68  return osc;
69  }
70  static void destroy(AgnesiOscillator* obj){
71  delete obj;
72  }
73 };
74 
75 #endif /* __AgnesiOscillator_h */
#define clamp(x, lo, hi)
Definition: basicmaths.h:47
#define M_PI
Definition: basicmaths.h:52
Oscillator that produces an Agnesi curve (Witch of Agnesi) With a=0.5, the output is between near 0 (...
void setPhase(float phase)
Set current oscillator phase in radians.
float getPhase()
Get current oscillator phase in radians.
AgnesiOscillator(float sr, float a, float N)
static AgnesiOscillator * create(float sr, float a=0.5, float N=5)
float generate()
Produce the next consecutive sample.
static float agnesi(float x, float a)
float getFrequency()
Get oscillator frequency in Hertz.
float generate(float fm)
Produce a sample with frequency modulation.
void normalise()
Normalise offset and gain so that signal is between -1 and 1.
static void destroy(AgnesiOscillator *obj)
void setFrequency(float freq)
Set oscillator frequency in Hertz.
void reset()
Reset oscillator (typically resets phase)
An Oscillator is a SignalGenerator that operates at a given frequency and that can be frequency modul...
Definition: Oscillator.h:12
virtual float generate()
Produce the next consecutive sample.