00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef _Oscillator_H
00015 #define _Oscillator_H
00016
00017 #include "CSL_Core.h"
00018 #include <stdarg.h>
00019
00020 #define DEFAULT_WAVETABLE_SIZE 8192 // use large wave tables by default
00021
00022 namespace csl {
00023
00028
00029 class Oscillator : public UnitGenerator, public Phased, public Scalable {
00030 public:
00031 Oscillator(float frequency = 220.0, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00032 Oscillator(UnitGenerator & frequency, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00033 Oscillator(UnitGenerator & frequency, UnitGenerator & ampl, float offset = 0.0, float phase = 0.0);
00034 ~Oscillator();
00035
00036 void dump();
00037 };
00038
00042
00043 #ifdef CSL_ENUMS
00044 typedef enum {
00045 kTruncate,
00046 kLinear,
00047 kCubic,
00048 kAllPass
00049 } InterpolationPolicy;
00050 #else
00051 #define kTruncate 1
00052 #define kLinear 2
00053 #define kCubic 3
00054 #define kAllPass 4
00055 typedef int InterpolationPolicy;
00056 #endif
00057
00063
00064 class WavetableOscillator : public Oscillator {
00065 public:
00066
00067 WavetableOscillator(Buffer & wave);
00068
00069 #ifndef SWIG_ONLY // SWIG can't handle the initializers
00070
00071
00072 WavetableOscillator(float frequency = 1, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00073 #else
00074 WavetableOscillator(Buffer & wave, float frequency = 220.0f);
00075 WavetableOscillator(Buffer & wave, float frequency = 220.0f, float phase = 0.0f);
00076 WavetableOscillator(float frequency = 1, float ampl = 1.0f, float offset = 0.0f, float phase = 0.0f);
00077 #endif
00078 void setWaveform(Buffer & wave);
00079
00081 void setInterpolate(InterpolationPolicy whether) { mInterpolate = whether; };
00082
00083 virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00084
00085 InterpolationPolicy mInterpolate;
00086 Buffer mWavetable;
00087
00088 void fillSine();
00089 };
00090
00094
00095 class CompOrCacheOscillator : public WavetableOscillator, public Cacheable {
00096 public:
00097 CompOrCacheOscillator(bool whether = false, float frequency = 220, float phase = 0.0);
00098 void createCache();
00099
00100 protected:
00101 virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00102 virtual void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz) = 0;
00103 };
00104
00108
00109 class Sine : public Oscillator {
00110 public:
00111 Sine(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00112 void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00113 };
00114
00118
00119 class Sawtooth : public Oscillator {
00120 public:
00121 Sawtooth(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00122 void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00123 };
00124
00128
00129 class Square : public Oscillator {
00130 public:
00131 Square(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00132 void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00133 };
00134
00138
00139 class Impulse : public Oscillator {
00140 public:
00141 Impulse();
00142 Impulse(float delay);
00143 Impulse(float frequency, float ampl);
00144 Impulse(float frequency, float ampl, float offset);
00145 Impulse(float frequency, float ampl, float offset, float phase);
00146
00147 void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00148
00149 protected:
00150 int mCounter;
00151 };
00152
00156
00157 typedef struct {
00158 float number;
00159 float amplitude;
00160 float phase;
00161 } Partial;
00162
00166
00167 #ifdef CSL_ENUMS
00168 typedef enum {
00169 kFrequency,
00170 kFreqAmp,
00171 kFreqAmpPhase
00172 } PartialDescriptionMode;
00173 #else
00174 #define kFrequency 1
00175 #define kFreqAmp 2
00176 #define kFreqAmpPhase 3
00177 typedef int PartialDescriptionMode;
00178 #endif
00179
00197
00198 class SumOfSines : public CompOrCacheOscillator {
00199 public:
00200 SumOfSines();
00201 SumOfSines(float frequency);
00202 SumOfSines(PartialDescriptionMode format, unsigned partialCount, ...);
00203
00204 void addPartial(Partial * pt);
00205 void addPartials(unsigned num_p, Partial ** pt);
00206 void addPartials(int argc, void ** argv);
00207 void addPartial(float nu, float amp);
00208 void addPartial(float nu, float amp, float phase);
00209 void clearPartials();
00210
00211 void dump();
00212
00213 protected:
00214 std::vector<Partial *> mPartials;
00215 void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz);
00216
00217 private:
00218 Buffer outputBuffer;
00219 };
00220
00221 }
00222
00223 #endif