-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOscillatorNode.cpp
64 lines (59 loc) · 1.66 KB
/
OscillatorNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/***** OscillatorNode.cpp *****/
#include <OscillatorNode.h>
#include <cmath>
void OscillatorNode::render(){
if (!state) return;
if (type == OSC_SINE){
for (int f = 0; f<audioFrames; f++){
for (int ch = 0; ch<outputChannels; ch++){
outputBuffer[0][ch][f] = sinf(phase);
}
incrementPhase(f);
}
} else if (type == OSC_SQUARE){
for (int f = 0; f<audioFrames; f++){
for (int ch = 0; ch<outputChannels; ch++){
if (phase >= 0){
outputBuffer[0][ch][f] = 1.0f;
} else {
outputBuffer[0][ch][f] = -1.0f;
}
}
incrementPhase(f);
}
} else if (type == OSC_SAWTOOTH){
for (int f = 0; f<audioFrames; f++){
for (int ch = 0; ch<outputChannels; ch++){
outputBuffer[0][ch][f] = phase / M_PI;
}
incrementPhase(f);
}
} else if (type == OSC_TRIANGLE){
for (int f = 0; f<audioFrames; f++){
for (int ch = 0; ch<outputChannels; ch++){
if (phase >= 0.0f && phase <= M_PI/2.0f){
outputBuffer[0][ch][f] = 2.0f/M_PI * phase;
} else if (phase <= -M_PI/2.0f){
outputBuffer[0][ch][f] = -2.0f/M_PI * (phase + M_PI);
} else if (phase > M_PI/2.0f){
outputBuffer[0][ch][f] = 1.0f - 2.0f/M_PI * (phase - M_PI/2.0f);
} else if (phase > -M_PI/2.0f && phase < 0.0f){
outputBuffer[0][ch][f] = 2.0f/M_PI * phase;
}
}
incrementPhase(f);
}
}
}
void OscillatorNode::incrementPhase(int frame){
phase += 2.0 * M_PI * params[0]->getValueAt(frame) * pow(2, params[1]->getValueAt(frame) / 1200.0f) * inverseSampleRate;
if(phase > M_PI)
phase -= 2.0 * M_PI;
}
void OscillatorNode::setType(int _type){
type = _type;
}
void OscillatorNode::setState(int _state){
state = _state;
if (!state) resetOutputs();
}