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 //
00004 //  See the copyright notice and acknowledgment of authors in the file COPYRIGHT
00005 //
00006 // What's here:
00007 //  Oscillator -- Abstract oscillator class
00008 //  WavetableOscillator -- Oscillator with a stored wave table (default wave table is an 8192-sample  sine)
00009 //  CompOrCacheOscillator -- Abstract oscillator class for those who can compute of cache their wavetables
00010 //  Sine -- oscillator class (computes the sine fcn on the fly)
00011 //  FSine -- (uses a ringing filter for the sine calc)
00012 //  Sawtooth -- Sawtooth oscillator class (non-band-limited)
00013 //  Square -- Square oscillator class
00014 //  SumOfSines -- Sum-of-sines oscillator class
00015 
00016 #ifndef _Oscillator_H
00017 #define _Oscillator_H
00018 
00019 #include "CSL_Core.h"
00020 #include <stdarg.h>     // for varargs
00021 
00022 #define DEFAULT_WAVETABLE_SIZE CSL_mMaxBufferFrames     // use large wave tables by default
00023 
00024 namespace csl {
00025 
00030 
00031 class Oscillator : public UnitGenerator, public Phased, public Scalable {
00032 public:                             
00033     Oscillator(float frequency = 220.0, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00034     Oscillator(UnitGenerator & frequency, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00035     Oscillator(UnitGenerator & frequency, UnitGenerator & ampl, float offset = 0.0, float phase = 0.0);
00036     ~Oscillator();                      
00037     
00038     void dump();                        
00039 };
00040 
00044 
00045 #ifdef CSL_ENUMS
00046 typedef enum {
00047     kTruncate,
00048     kLinear,
00049     kCubic,
00050     kAllPass
00051 } InterpolationPolicy;
00052 #else
00053     #define kTruncate 1
00054     #define kLinear 2
00055     #define kCubic 3
00056     #define kAllPass 4
00057     typedef int  InterpolationPolicy;
00058 #endif
00059 
00065 
00066 class WavetableOscillator : public Oscillator {
00067 public:
00068     WavetableOscillator(Buffer & wave);
00069 #ifndef SWIG_ONLY                       // SWIG can't handle the initializers
00070     WavetableOscillator(float frequency = 1, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00071 #else
00072     WavetableOscillator(Buffer & wave, float frequency = 220.0f);
00073     WavetableOscillator(Buffer & wave, float frequency = 220.0f, float phase = 0.0f);
00074     WavetableOscillator(float frequency = 1, float ampl = 1.0f, float offset = 0.0f, float phase = 0.0f);
00075 #endif
00076     void setWaveform(Buffer & wave);        
00077 
00078     void setInterpolate(InterpolationPolicy whether) { mInterpolate = whether; };
00079                                         // get the next buffer of samples
00080     virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00081 
00082     InterpolationPolicy mInterpolate;   
00083     Buffer mWavetable;                  
00084 
00085 protected:
00086     void fillSine();                    
00087 };
00088 
00092 
00093 class CompOrCacheOscillator : public WavetableOscillator, public Cacheable {
00094 public:
00095     CompOrCacheOscillator(bool whether = false, float frequency = 220, float phase = 0.0);
00096     void createCache();
00097 
00098 protected:
00099     virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00100     virtual void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz) = 0;
00101 };
00102 
00106 
00107 class Sine : public Oscillator {    
00108 public:
00109     Sine(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00110     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00111 };
00112 
00114 
00115 class FSine : public Oscillator {   
00116 public:
00117     FSine(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00118     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00119 };
00120 
00124 
00125 class Sawtooth : public Oscillator {
00126 public:
00127     Sawtooth(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00128     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00129 };
00130 
00134 
00135 class Square : public Oscillator {
00136 public:
00137     Square(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
00138     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00139 };
00140 
00144 
00145 class Impulse : public Oscillator {
00146 public:
00147     Impulse();
00148     Impulse(float delay);
00149     Impulse(float frequency, float ampl);
00150     Impulse(float frequency, float ampl, float offset);
00151     Impulse(float frequency, float ampl, float offset, float phase);
00152     
00153     void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
00154 
00155 protected:
00156     int mCounter;
00157 };
00158 
00162 
00163 typedef struct {                // Harmonic partial data structure used here and by the SHARC classes
00164     float number;           // partial number (need not be an integer)
00165     float amplitude;            // partial amplitude (0.0 - 1.0)
00166     float phase;                // Partial phase in radians
00167 } Partial;
00168 
00172 
00173 #ifdef CSL_ENUMS
00174 typedef enum {
00175     kFrequency,
00176     kFreqAmp,
00177     kFreqAmpPhase
00178 } PartialDescriptionMode;
00179 #else
00180     #define kFrequency 1
00181     #define kFreqAmp 2
00182     #define kFreqAmpPhase 3
00183     typedef int PartialDescriptionMode;
00184 #endif
00185 
00203 
00204 class SHARCSpectrum;
00205 
00206 class SumOfSines : public CompOrCacheOscillator {
00207 public:
00208     SumOfSines();                           
00209     SumOfSines(float frequency);                    
00210     SumOfSines(unsigned numHarms, float noise);     
00211     SumOfSines(float frequency, unsigned numHarms, float noise);
00213     SumOfSines(PartialDescriptionMode format, unsigned partialCount, ...);
00214     SumOfSines(SHARCSpectrum & spect);              
00215     
00216     void addPartial(Partial * pt);
00217     void addPartials(unsigned num_p, Partial ** pt);
00218     void addPartials(int argc, void ** argv);
00219     void addPartial(float nu, float amp);
00220     void addPartial(float nu, float amp, float phase);
00221     void clearPartials();
00222 
00223     void dump();                        
00224     
00225 protected:
00226     std::vector<Partial *> mPartials;
00227     void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz);
00228 
00229 private:
00230     Buffer outputBuffer;            // kludj so we can use the inherited macros
00231 };
00232 
00233 }
00234 
00235 #endif

Generated on Sat Oct 17 14:12:31 2009 for CSL by  doxygen 1.4.5-20051010