OpenWareLaboratory
WavetableOscillator.h
Go to the documentation of this file.
1 #ifndef __WavetableOscillator_h__
2 #define __WavetableOscillator_h__
3 
4 #include "Oscillator.h"
5 
6 class WavetableOscillator : public OscillatorTemplate<WavetableOscillator> {
7 private:
8  FloatArray wave;
9 public:
10  static constexpr float begin_phase = 0;
11  static constexpr float end_phase = 1;
14  setSampleRate(sr);
15  }
16  WavetableOscillator(float sr, const FloatArray wavetable): wave(wavetable) {
17  setSampleRate(sr);
18  }
20  return wave;
21  }
22  float getSample(){
23  size_t index = phase*wave.getSize();
24  // index = min(index, size-1);
25  return wave[index];
26  }
27 };
28 
29 
30 class AntialiasedWavetableOscillator : public OscillatorTemplate<AntialiasedWavetableOscillator> {
31 private:
32  FloatArray wave;
33 public:
34  static constexpr float begin_phase = 0;
35  static constexpr float end_phase = 1;
38  setSampleRate(sr);
39  }
40  AntialiasedWavetableOscillator(float sr, FloatArray wave):wave(wave){
41  setSampleRate(sr);
42  }
43  float getSample(){
44  size_t index = phase*wave.getSize();
45  float sample = wave[index];
46  sample -= polyblep(phase, incr);
47  return sample;
48  }
50 };
51 
52 template<InterpolationMethod im = LINEAR_INTERPOLATION>
53 class InterpolatingWavetableOscillator : public OscillatorTemplate<InterpolatingWavetableOscillator<im>> {
54 private:
56 public:
57  static constexpr float begin_phase = 0;
58  static constexpr float end_phase = 1;
62  }
64  : wave(data.getData(), data.getSize()) {
66  }
68  return FloatArray(wave.getData(), wave.getSize());
69  }
70  void setWavetable(FloatArray wavetable){
71  wave.setData(wavetable.getData(), wavetable.getSize());
72  }
73  float getSample(){
75  return wave.readAt(index);
76  }
77  static InterpolatingWavetableOscillator<im>* create(float sr, size_t length){
79  }
82  delete obj;
83  }
84 };
85 
86 #endif /* __WavetableOscillator_h__ */
static constexpr float begin_phase
AntialiasedWavetableOscillator(float sr, FloatArray wave)
static constexpr float end_phase
void setData(DataType *data, IndexType len)
IndexType getSize() const
DataType * getData()
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
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
float readAt(float index)
Interpolated read at sub-sample index.
InterpolatingWavetableOscillator(float sr, FloatArray data)
static InterpolatingWavetableOscillator< im > * create(float sr, size_t length)
static constexpr float begin_phase
static void destroy(InterpolatingWavetableOscillator< im > *obj)
void setWavetable(FloatArray wavetable)
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 begin_phase
WavetableOscillator(float sr, const FloatArray wavetable)
static constexpr float end_phase