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 class Oscillator : public UnitGenerator, public Phased, public Scalable {
00029 public:                             
00030     Oscillator(float frequency = 220.0, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00031     ~Oscillator();                      
00032     
00033     void dump();                        
00034 };
00035 
00039 #ifdef CSL_ENUMS
00040 typedef enum {
00041     kTruncate,
00042     kLinear,
00043     kCubic,
00044     kAllPass
00045 } InterpolationPolicy;
00046 #else
00047     #define kTruncate 1
00048     #define kLinear 2
00049     #define kCubic 3
00050     #define kAllPass 4
00051     typedef int  InterpolationPolicy;
00052 #endif
00053 
00059 
00060 class WavetableOscillator : public Oscillator {
00061 public:
00062     WavetableOscillator();
00063     WavetableOscillator(Buffer & wave);
00064     WavetableOscillator(sample * samps, unsigned size);
00065     WavetableOscillator(Buffer & wave, float frequency);
00066     WavetableOscillator(Buffer & wave, float frequency, float phase);
00067 //  WavetableOscillator(float frequency = 220.0, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00068     WavetableOscillator(float frequency, float ampl, float offset, float phase);
00069 
00070     void setWaveform(Buffer & wave);        
00071     void setWaveform(sample * samps, unsigned size);
00073     void setInterpolate(InterpolationPolicy whether) { mInterpolate = whether; };
00074                                         // get the next buffer of samples
00075     virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00076 
00077 protected:
00078     InterpolationPolicy mInterpolate;       
00079     Buffer mWavetable;      
00080     
00081     void fillSine();            
00082 };
00083 
00087 class CompOrCacheOscillator : public WavetableOscillator, public Cacheable {
00088 public:
00089     CompOrCacheOscillator(bool whether = false, float frequency = 220, float phase = 0.0);
00090     
00091     void createCache();
00092 
00093 protected:
00094     virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00095     virtual void nextWaveInto(sample * dest, unsigned count, bool oneHz) = 0;
00096 };
00097 
00101 class Sine : public Oscillator {    
00102 public:
00103     Sine(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00104     
00105     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00106 };
00107 
00111 class Sawtooth : public Oscillator {
00112 public:
00113     Sawtooth(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00114     
00115     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00116 };
00117 
00121 class Square : public Oscillator {
00122 public:
00123     Square(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00124     
00125     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00126 };
00127 
00131 class Impulse : public Oscillator {
00132 public:
00133     Impulse();
00134     Impulse(float delay);
00135     Impulse(float frequency, float ampl);
00136     Impulse(float frequency, float ampl, float offset);
00137     Impulse(float frequency, float ampl, float offset, float phase);
00138     
00139     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00140 
00141 protected:
00142     int mCounter;
00143 };
00144 
00148 typedef struct {                // Harmonic partial data structure used here and by the SHARC classes
00149     float number;           // partial number (need not be an integer)
00150     float amplitude;            // partial amplitude (0.0 - 1.0)
00151     float phase;                // Partial phase in radians
00152 } Partial;
00153 
00157 #ifdef CSL_ENUMS
00158 typedef enum {
00159     kFrequency,
00160     kFreqAmp,
00161     kFreqAmpPhase
00162 } PartialDescriptionMode;
00163 #else
00164     #define kFrequency 1
00165     #define kFreqAmp 2
00166     #define kFreqAmpPhase 3
00167     typedef int PartialDescriptionMode;
00168 #endif
00169 
00187     
00188 class SumOfSines : public CompOrCacheOscillator {
00189 public:
00190     SumOfSines();
00191     SumOfSines(float frequency);
00192     SumOfSines(PartialDescriptionMode format, unsigned partialCount, ...);
00193     
00194     void addPartial(Partial * pt);
00195     void addPartials(unsigned num_p, Partial ** pt);
00196     void addPartial(float nu, float amp);
00197     void addPartial(float nu, float amp, float phase);
00198 
00199     void dump();                        
00200 
00201 protected:
00202     std::vector<Partial *> mPartials;
00203     void nextWaveInto(sample * dest, unsigned count, bool oneHz);
00204 
00205 private:
00206     Buffer outputBuffer;            // kludj so we can use the inherited macros
00207 };
00208 
00209 }
00210 
00211 #endif

Generated on Fri Apr 6 20:18:13 2007 for CSL by  doxygen 1.4.5-20051010