Oscillator.h

Go to the documentation of this file.
00001 //
00002 //  Oscillator.h -- specification of the base oscillator class and a few simple waveform generators
00003 //  See the copyright notice and acknowledgment of authors in the file COPYRIGHT
00004 //
00005 // What's here:
00006 //  Oscillator -- Abstract oscillator class
00007 //  WavetableOscillator -- Oscillator with a stored wave table (default wave table is an 8192-sample  sine)
00008 //  CompOrCacheOscillator -- Abstract oscillator class for those who can compute of cache their wavetables
00009 //  Sine -- oscillator class (this computes the sine fcn on the fly)
00010 //  Sawtooth -- Sawtooth oscillator class
00011 //  Square -- Square oscillator class
00012 //  SumOfSines -- Sum-of-sines oscillator class
00013 
00014 #ifndef _Oscillator_H
00015 #define _Oscillator_H
00016 
00017 #include "CSL_Core.h"
00018 #include <stdarg.h>     // for varargs
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 //  WavetableOscillator();
00067     WavetableOscillator(Buffer & wave);
00068 //  WavetableOscillator(SampleBuffer samps, unsigned size);
00069 #ifndef SWIG_ONLY                       // SWIG can't handle the initializers
00070 //  WavetableOscillator(Buffer & wave, float frequency);
00071 //  WavetableOscillator(Buffer & wave, float frequency, float phase);
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 //  void setWaveform(SampleBuffer samps, unsigned size);
00081     void setInterpolate(InterpolationPolicy whether) { mInterpolate = whether; };
00082                                         // get the next buffer of samples
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 {                // Harmonic partial data structure used here and by the SHARC classes
00158     float number;           // partial number (need not be an integer)
00159     float amplitude;            // partial amplitude (0.0 - 1.0)
00160     float phase;                // Partial phase in radians
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;            // kludj so we can use the inherited macros
00219 };
00220 
00221 }
00222 
00223 #endif

Generated on Thu Sep 17 23:14:16 2009 for CSL by  doxygen 1.5.8