CSL 5.2

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

- File List
- File Members

OscillatorBL.cpp
Go to the documentation of this file.
1 //
2 // OscillatorBL.cpp -- Band-limited oscillators -- these can use on-demand sine summation, or store their wavetables
3 // (the latter choice might be dangerous with dynamic frequency control, of course)
4 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
5 //
6
7 #include "OscillatorBL.h"
8 #include <math.h>
9
10 using namespace csl;
11
12 SquareBL::SquareBL() : SumOfSines() {
13 }
14
15 SquareBL::SquareBL(float frequency) : SumOfSines(true, frequency) {
16 this->setFrequency(frequency);
17 }
18
19 SquareBL::SquareBL(float frequency, float phase) : SumOfSines(true, frequency, phase) {
20 this->setFrequency(frequency);
21 }
22
23 // Recompute the partials list whenever you change the frequency
24
25
26 void SquareBL::nextWaveInto(sample * dest, unsigned count, bool oneHz) {
27 // void SquareBL::nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException) {
28 unsigned max_harm = ((float) mFrameRate / (mInputs[CSL_FREQUENCY]->nextValue() * 2.0f ));
29 if (mPartials.size() > max_harm)
30 mPartials.erase(mPartials.begin() + (max_harm - 1), mPartials.end());
31 else if (mPartials.size() < max_harm)
32 for (unsigned i = mPartials.size(); i < max_harm; i += 2)
33 this->addPartial((float)i, 0.5f / i);
34 SumOfSines::nextWaveInto(dest, count, oneHz);
35 }
36
37 // If you start cacheing, then compute a new band-limited wavetable
38 /*
39 status SquareBL::nextWaveInto(sample * dest, unsigned count, bool oneHz) {
40 // float incr = CSL_TWOPI / (count * mFrameRate);
41 float incr = CSL_TWOPI / (float) count;
42 sample frequency = mInputs[CSL_FREQUENCY]->nextValue();
43 sample * out_ptr = dest;
44 sample value;
45 float max_harm;
46
47 max_harm = floor(((float) mFrameRate) / (frequency * 2.0f ));
48 for (unsigned i = 0; i < count; i++) { // sample loop
49 value = 0;
50 for (float harm = 1.0; harm <= max_harm ; harm += 2)
51 value += sin(harm * phase) / harm ;
52 // *out_ptr++ = (value * scale) + offset;
53 *out_ptr++ = value;
54 phase += incr;
55 }
56 while (phase > CSL_TWOPI)
57 phase -= CSL_TWOPI;
58 }
59
60 ImpulseBL::ImpulseBL() : CompOrCacheOscillator() { }
61
62 unsigned ImpulseBL::next_buffer(sample * buffer, unsigned num_frames) {
63 float* fp = (float*) buffer;
64 float twopi = (CSL_PI * 2.0);
65
66 _frequency->next_buffer(_frequency_buffer, num_frames);
67 _phase->next_buffer(_phase_buffer, num_frames);
68 // _duty->next_buffer(_duty_buffer, num_frames);
69
70 float rateRecip = 1.0/_rate;
71 float scale = 0.025/CSL_PI; //4.0 / (CSL_PI );
72
73 // generate buffer_size 32-bit floats
74 for (unsigned i = 0; i < num_frames; i++) {
75 float nextFloat = 1.0;
76 unsigned num_of_max_harmonic = (unsigned) floor(_rate / (_frequency_buffer[i] * 2) );
77
78 for (unsigned which_harmonic = 1; which_harmonic <= num_of_max_harmonic; which_harmonic++) {
79 // for (int which_harmonic = 0; which_harmonic <10; which_harmonic++) {
80
81 //float gibbs = cos((which_harmonic-1)*CSL_PI/2/num_of_max_harmonic);
82 nextFloat += sin(which_harmonic * _phase_accumulator);// * gibbs * gibbs ;
83 }
84 (*fp++) = scale * nextFloat; // set all the channels equal to this float
85
86 static float max_amp = 0;
87 max_amp = (max_amp > scale * nextFloat) ? max_amp : scale * nextFloat;
88 // twopi * the normalized frequency + phase
89 _phase_accumulator += twopi * (_frequency_buffer[i] * rateRecip) + _phase_buffer[i];
90
91 // wraparound after 2pi radians
92 while (_phase_accumulator > twopi)
93 _phase_accumulator -= twopi;
94 }
95 return num_frames;
96 }
97
98 TriangleBL::TriangleBL() : CompOrCacheOscillator() { }
99
100 unsigned TriangleBL::next_buffer(sample * buffer, unsigned num_frames) {
101 float* fp = (float*) buffer;
102 float twopi = (CSL_PI * 2.0);
103
104 _frequency->next_buffer(_frequency_buffer, num_frames);
105 _phase->next_buffer(_phase_buffer, num_frames);
106 // _duty->next_buffer(_duty_buffer, num_frames);
107
108 float rateRecip = 1.0/_rate;
109 float scale = 8.0 / (CSL_PI * CSL_PI);
110 // generate buffer_size 32-bit floats
111 for (unsigned i = 0; i < num_frames; i++) {
112 float nextFloat = 0.0;
113 unsigned num_of_max_harmonic = (unsigned) (_rate / (_frequency_buffer[i] * 2) );
114 for (unsigned which_harmonic = 1; which_harmonic <= num_of_max_harmonic; which_harmonic += 2) {
115 // for (int which_harmonic = 0; which_harmonic <10; which_harmonic++) {
116 nextFloat += sin(which_harmonic * _phase_accumulator ) / (which_harmonic * which_harmonic );
117 }
118 (*fp++) = scale * nextFloat; // set all the channels equal to this float
119 static float max_amp = 0;
120 max_amp = (max_amp > scale * nextFloat) ? max_amp : scale * nextFloat;
121 // twopi * the normalized frequency + phase
122 _phase_accumulator += twopi * (_frequency_buffer[i] * rateRecip) + _phase_buffer[i];
123 // wraparound after 2pi radians
124 while (_phase_accumulator > twopi)
125 _phase_accumulator -= twopi;
126 }
127 return num_frames;
128 }
129
130 */