CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
51 } InterpolationPolicy;
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
66 class WavetableOscillator : public Oscillator {
67 public:
68 WavetableOscillator(Buffer & wave);
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
94 class CompOrCacheOscillator : public WavetableOscillator, public Cacheable {
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,
178 kFreqAmpPhase
179 } PartialDescriptionMode;
180 #else
181 #define kFrequency 1
182 #define kFreqAmp 2
183 #define kFreqAmpPhase 3
184 typedef int PartialDescriptionMode;
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
207 class SumOfSines : public CompOrCacheOscillator {
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