OpenWareLaboratory
MorphingOscillator.h
Go to the documentation of this file.
1 #ifndef __MorphingOscillator_h
2 #define __MorphingOscillator_h
3 
4 #include "Oscillator.h"
5 
13 protected:
15  size_t osc_count;
17  Oscillator* lo = NULL;
18  Oscillator* hi = NULL;
19  float xf = 0;
20 public:
22  : osc(osc), osc_count(count), buffer(buffer){}
23  void setFrequency(float value){
24  lo->setFrequency(value);
25  hi->setFrequency(value);
26  }
27  float getFrequency(){
28  return lo->getFrequency();
29  }
30  void setPhase(float value){
31  lo->setPhase(value);
32  hi->setPhase(value);
33  }
34  float getPhase(){
35  return lo->getPhase();
36  }
37  void reset(){
38  lo->reset();
39  hi->reset();
40  }
41  float generate(){
42  float l = lo->generate();
43  float h = hi->generate();
44  return l + (h - l) * xf;
45  }
46  float generate(float fm){
47  float l = lo->generate(fm);
48  float h = hi->generate(fm);
49  return l + (h - l) * xf;
50  }
51  void generate(FloatArray output){
52  lo->generate(output);
53  output.multiply(1-xf);
54  hi->generate(buffer);
56  output.add(buffer);
57  }
58  void generate(FloatArray output, FloatArray fm){
59  lo->generate(output, fm);
60  output.multiply(1-xf);
61  hi->generate(buffer, fm);
63  output.add(buffer);
64  }
69  void morph(float value){
70  value *= osc_count - 1;
71  size_t idx = clamp((size_t)value, (size_t)0, osc_count - 2);
72  Oscillator* newlo = osc[idx];
73  Oscillator* newhi = osc[idx+1];
74  xf = value - idx;
75  if(lo != newlo){
76  newlo->setPhase(getPhase());
77  newlo->setFrequency(getFrequency());
78  lo = newlo;
79  }
80  if(hi != newhi){
81  newhi->setPhase(getPhase());
82  newhi->setFrequency(getFrequency());
83  hi = newhi;
84  }
85  }
87  return osc_count;
88  }
89  Oscillator* getOscillator(size_t index){
90  return osc[index];
91  }
92  void setOscillator(size_t index, Oscillator* oscillator){
93  osc[index] = oscillator;
94  lo = hi;
95  hi = oscillator;
96  }
97  static MorphingOscillator* create(size_t oscillator_count, size_t blocksize){
98  return new MorphingOscillator(new Oscillator*[oscillator_count], oscillator_count,
99  FloatArray::create(blocksize));
100  }
101  static void destroy(MorphingOscillator* obj){
103  delete[] obj->osc;
104  delete obj;
105  }
106 };
107 
108 #endif /* __MorphingOscillator_h */
#define clamp(x, lo, hi)
Definition: basicmaths.h:47
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
void multiply(FloatArray operand2, FloatArray destination)
Element-wise multiplication between arrays.
Definition: FloatArray.cpp:290
void add(FloatArray operand2, FloatArray destination)
Element-wise sum between arrays.
Definition: FloatArray.cpp:231
static void destroy(FloatArray array)
Destroys a FloatArray created with the create() method.
Definition: FloatArray.cpp:472
static FloatArray create(int size)
Creates a new FloatArray.
Definition: FloatArray.cpp:466
The MorphingOscillator wraps a fixed number of Oscillators and crossfades between their outputs.
void generate(FloatArray output, FloatArray fm)
Produce a block of samples with frequency modulation.
MorphingOscillator(Oscillator **osc, size_t count, FloatArray buffer)
Oscillator * getOscillator(size_t index)
void setPhase(float value)
Set current oscillator phase in radians.
void setFrequency(float value)
Set oscillator frequency in Hertz.
void reset()
Reset oscillator (typically resets phase)
static MorphingOscillator * create(size_t oscillator_count, size_t blocksize)
void setOscillator(size_t index, Oscillator *oscillator)
float generate()
Produce the next consecutive sample.
float getPhase()
Get current oscillator phase in radians.
void generate(FloatArray output)
Produce a block of samples.
float getFrequency()
Get oscillator frequency in Hertz.
float generate(float fm)
Produce a sample with frequency modulation.
void morph(float value)
Morph between all configured oscillators.
static void destroy(MorphingOscillator *obj)
An Oscillator is a SignalGenerator that operates at a given frequency and that can be frequency modul...
Definition: Oscillator.h:12
virtual void reset()=0
Reset oscillator (typically resets phase)
virtual float getFrequency()=0
Get oscillator frequency in Hertz.
virtual float generate(float fm)=0
Produce a sample with frequency modulation.
virtual void setPhase(float phase)=0
Set current oscillator phase in radians.
virtual void setFrequency(float value)=0
Set oscillator frequency in Hertz.
virtual float getPhase()=0
Get current oscillator phase in radians.