CSL  6.0
Oscillator.h
Go to the documentation of this file.
1 //
2 // Oscillator.h -- specification of the base oscillator class and a few simple waveform generators
3 //
4 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
5 //
6 // What's here:
7 // Oscillator -- Abstract oscillator class
8 // WavetableOscillator -- Oscillator with a stored wave table (default wave table is an 8192-sample sine)
9 // CompOrCacheOscillator -- Abstract oscillator class for those who can compute of cache their wavetables
10 // Sine -- oscillator class (computes the sine fcn on the fly)
11 // FSine -- (uses a ringing filter for the sine calc)
12 // Sawtooth -- Sawtooth oscillator class (non-band-limited)
13 // Square -- Square oscillator class
14 // SumOfSines -- Sum-of-sines oscillator class
15 
16 #ifndef _Oscillator_H
17 #define _Oscillator_H
18 
19 #include "CSL_Core.h"
20 #include <stdarg.h> // for varargs
21 
22 #define DEFAULT_WAVETABLE_SIZE CSL_mMaxBufferFrames // use large wave tables by default
23 
24 namespace csl {
25 
26 ///
27 /// Oscillator -- Abstract oscillator class; inherits from UnitGenerator, Phased, and Scalable
28 /// and provides convenience constructors (freq, ampl, offset, phase)
29 ///
30 
31 class Oscillator : public UnitGenerator, public Phased, public Scalable {
32 public: /// Constructor: parameters are optional.
33  Oscillator(float frequency = 220.0, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
34  Oscillator(UnitGenerator & frequency, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
35  Oscillator(UnitGenerator & frequency, UnitGenerator & ampl, float offset = 0.0, float phase = 0.0);
36  virtual ~Oscillator(); ///< Destructor
37 
38  void dump(); ///< print the receiver for debugging
39 };
40 
41 ///
42 /// Enumeration for interpolation policies
43 ///
44 
45 #ifdef CSL_ENUMS
46 typedef enum {
47  kTruncate,
48  kLinear,
49  kCubic,
50  kAllPass
52 #else
53  #define kTruncate 1
54  #define kLinear 2
55  #define kCubic 3
56  #define kAllPass 4
57  typedef int InterpolationPolicy;
58 #endif
59 
60 ///
61 /// WavetableOscillator -- Oscillator with a stored wave table that does table look-up.
62 /// The default wave table is an 8192-sample sine.
63 /// (perhaps accept a vector of freqs and a multichannel buffer?)
64 ///
65 
67 public:
69 #ifndef SWIG_ONLY // SWIG can't handle the initializers
70  WavetableOscillator(float frequency = 1, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
71 #else
72  WavetableOscillator(Buffer & wave, float frequency = 220.0f);
73  WavetableOscillator(Buffer & wave, float frequency = 220.0f, float phase = 0.0f);
74  WavetableOscillator(float frequency = 1, float ampl = 1.0f, float offset = 0.0f, float phase = 0.0f);
75 #endif
76  ~WavetableOscillator(); ///< Destructor
77  void setWaveform(Buffer & wave, bool freeBufs = true); ///< plug in waveforms
78  /// set the interpolation flag
79  void setInterpolate(InterpolationPolicy whether) { mInterpolate = whether; };
80  // get the next buffer of samples
81  virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
82 
83  InterpolationPolicy mInterpolate; ///< whether/how I should interpolate between samples
84  Buffer mWavetable; ///< the stored wave form
85 
86 protected:
87  void fillSine(); ///< fill the shared wavetable with 1 cycle of a sine wave
88 };
89 
90 ///
91 /// CompOrCacheOscillator -- Abstract oscillator class for those who can compute of cache their wavetables
92 ///
93 
95 public:
96  CompOrCacheOscillator(bool whether = false, float frequency = 220, float phase = 0.0);
97  void createCache();
98 
99 protected:
100  virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
101  virtual void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz) = 0;
102 };
103 
104 ///
105 /// Sine -- oscillator class (this computes the sine fcn on the fly)
106 ///
107 
108 class Sine : public Oscillator {
109 public:
110  Sine(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
111  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
112 };
113 
114 /// FSine -- (uses a ringing filter for the sine calc)
115 
116 class FSine : public Oscillator {
117 public:
118  FSine(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
119  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
120 };
121 
122 ///
123 /// Sawtooth oscillator class (non-band-limited)
124 ///
125 
126 class Sawtooth : public Oscillator {
127 public:
128  Sawtooth(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
129  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
130 };
131 
132 ///
133 /// Square oscillator class (non-band-limited)
134 ///
135 
136 class Square : public Oscillator {
137 public:
138  Square(float frequency = 220, float ampl = 1.0, float offset = 0.0, float phase = 0.0);
139  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
140 };
141 
142 ///
143 /// Impulse -- oscillator class (this create a single impulse delayed by 'delay' samples)
144 ///
145 
146 class Impulse : public Oscillator {
147 public:
148  Impulse();
149  Impulse(float delay);
150  Impulse(float frequency, float ampl);
151  Impulse(float frequency, float ampl, float offset);
152  Impulse(float frequency, float ampl, float offset, float phase);
153 
154  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
155 
156 protected:
157  int mCounter;
158 };
159 
160 ///
161 /// Struct for partial overtones
162 ///
163 
164 typedef struct { // Harmonic partial data structure used here and by the SHARC classes
165  float number; // partial number (need not be an integer)
166  float amplitude; // partial amplitude (0.0 - 1.0)
167  float phase; // Partial phase in radians
168 } Partial;
169 
170 ///
171 /// Enum for SumOfSines description formats
172 ///
173 
174 #ifdef CSL_ENUMS
175 typedef enum {
176  kFrequency,
177  kFreqAmp,
180 #else
181  #define kFrequency 1
182  #define kFreqAmp 2
183  #define kFreqAmpPhase 3
185 #endif
186 
187 ///
188 /// SumOfSines -- Sum-of-sines oscillator class
189 ///
190 /// The constructor takes an int format (1, 2, or 3 elements per overtone), the number of partials, and a list of elements
191 ///
192 /// This can cache a wavetable (if you use a strictly harmonic overtone series) or compute a buffer on the fly by summation (slow)
193 ///
194 /// Examples:
195 /// Format kFrequency = list of values for overtone amplitudes with num = 1, 2, 3..., amps = values, phases = 0
196 /// SumOfSines vox(kFrequency, 5, 0.2, 0.2, 0.1, 0.1, 0.05) -- first 5 overtones with weights 0.2, 0.2...
197 ///
198 /// Format kFreqAmp = list of values for overtone freq/amplitudes with phases = 0
199 /// SumOfSines vox(kFreqAmp, 3, 1, 0.2, 2, 0.1, 3.01, 0.05) -- overtones 1, 2, and 3.01 (i.e., non-harmonic)
200 ///
201 /// Format kFreqAmpPhase = list of values for overtone num/amps/phases
202 /// SumOfSines vox(kFreqAmpPhase, 3 1.02, 0.2, 0, 2.1, 0.2, 0.1, 3.0, 0.1, 0.05) -- verbose constructor
203 ///
204 
205 class SHARCSpectrum;
206 
208 public:
209  SumOfSines(); /// Constructors
210  SumOfSines(float frequency); ///< empty constructor
211  SumOfSines(unsigned numHarms, float noise); ///< 1/f spectrum + noise
212  SumOfSines(float frequency, unsigned numHarms, float noise);
213  /// given a var-args partials list
214  SumOfSines(PartialDescriptionMode format, unsigned partialCount, ...);
215  SumOfSines(SHARCSpectrum & spect); /// given a SHARC spectrum
216 
217  void addPartial(Partial * pt);
218  void addPartials(unsigned num_p, Partial ** pt);
219  void addPartials(int argc, void ** argv);
220  void addPartial(float nu, float amp);
221  void addPartial(float nu, float amp, float phase);
222  void clearPartials();
223 
224  void dump(); ///< print the receiver for debugging
225 
226 protected:
227  std::vector<Partial *> mPartials;
228  void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz);
229 
230 private:
231  Buffer outputBuffer; // kludj so we can use the inherited macros
232 };
233 
234 }
235 
236 #endif
sample * SampleBuffer
1-channel buffer data type, vector of (sample)
Definition: CSL_Types.h:194
void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz)
Definition: Oscillator.cpp:514
#define kFrequency
Enum for SumOfSines description formats.
Definition: Oscillator.h:181
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Definition: Oscillator.cpp:228
#define kFreqAmpPhase
Definition: Oscillator.h:183
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Definition: Oscillator.cpp:262
FSine(float frequency=220, float ampl=1.0, float offset=0.0, float phase=0.0)
Definition: Oscillator.cpp:257
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
void setInterpolate(InterpolationPolicy whether)
Definition: Oscillator.h:79
virtual void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Definition: Oscillator.cpp:210
WavetableOscillator – Oscillator with a stored wave table that does table look-up. The default wave table is an 8192-sample sine. (perhaps accept a vector of freqs and a multichannel buffer?)
Definition: Oscillator.h:66
WavetableOscillator(Buffer &wave)
Destructor.
Definition: Oscillator.cpp:62
Sawtooth oscillator class (non-band-limited)
Definition: Oscillator.h:126
Phased – a mix-in for objects with phase accumulators (local float) and frequency controls (an input...
Definition: CSL_Core.h:497
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Definition: Oscillator.cpp:366
virtual void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Definition: Oscillator.cpp:132
std::vector< Partial * > mPartials
Definition: Oscillator.h:227
#define kCubic
Definition: Oscillator.h:55
void dump()
print the receiver for debugging
Definition: Oscillator.cpp:33
Impulse – oscillator class (this create a single impulse delayed by 'delay' samples) ...
Definition: Oscillator.h:146
void addPartials(unsigned num_p, Partial **pt)
Definition: Oscillator.cpp:490
Square(float frequency=220, float ampl=1.0, float offset=0.0, float phase=0.0)
Definition: Oscillator.cpp:322
Buffer outputBuffer
Definition: Oscillator.h:231
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Definition: Oscillator.cpp:294
Square oscillator class (non-band-limited)
Definition: Oscillator.h:136
Scalable – mix-in class with scale and offset control inputs (may be constants or generators)...
Definition: CSL_Core.h:403
Cacheable – a mix-in for caching streams.
Definition: CSL_Core.h:595
void clearPartials()
Definition: Oscillator.cpp:508
void fillSine()
fill the shared wavetable with 1 cycle of a sine wave
Definition: Oscillator.cpp:112
~WavetableOscillator()
Destructor.
Definition: Oscillator.cpp:70
Struct for partial overtones.
Definition: Oscillator.h:164
int PartialDescriptionMode
Definition: Oscillator.h:184
virtual ~Oscillator()
Destructor.
Definition: Oscillator.cpp:31
void addPartial(Partial *pt)
given a SHARC spectrum
Definition: Oscillator.cpp:486
void setWaveform(Buffer &wave, bool freeBufs=true)
plug in waveforms set the interpolation flag
Definition: Oscillator.cpp:76
Buffer mWavetable
the stored wave form
Definition: Oscillator.h:84
#define kFreqAmp
Definition: Oscillator.h:182
FSine – (uses a ringing filter for the sine calc)
Definition: Oscillator.h:116
CompOrCacheOscillator(bool whether=false, float frequency=220, float phase=0.0)
Definition: Oscillator.cpp:196
float amplitude
Definition: Oscillator.h:166
virtual void nextWaveInto(SampleBuffer dest, unsigned count, bool oneHz)=0
Sine – oscillator class (this computes the sine fcn on the fly)
Definition: Oscillator.h:108
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
int InterpolationPolicy
Definition: Oscillator.h:57
forward declaration
Definition: CSL_Core.h:241
Oscillator – Abstract oscillator class; inherits from UnitGenerator, Phased, and Scalable and provid...
Definition: Oscillator.h:31
Oscillator(float frequency=220.0, float ampl=1.0, float offset=0.0, float phase=0.0)
Constructor: parameters are optional.
Definition: Oscillator.cpp:16
CompOrCacheOscillator – Abstract oscillator class for those who can compute of cache their wavetable...
Definition: Oscillator.h:94
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Definition: Oscillator.cpp:325
InterpolationPolicy mInterpolate
whether/how I should interpolate between samples
Definition: Oscillator.h:83
Sine(float frequency=220, float ampl=1.0, float offset=0.0, float phase=0.0)
Definition: Oscillator.cpp:223
float number
Definition: Oscillator.h:165
SHARC spectrum class.
Definition: SHARC.h:76
void dump()
print the receiver for debugging
Definition: Oscillator.cpp:563
Base class of CSL exceptions (written upper-case). Has a string message.
Sawtooth(float frequency=220, float ampl=1.0, float offset=0.0, float phase=0.0)
Definition: Oscillator.cpp:291