-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofApp.cpp
96 lines (74 loc) · 2.64 KB
/
ofApp.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofBackground(100);
sampleRate = 44100;
bufferSize = 512;
gui.setup();
// add ofParameters to "gui" panel
gui.add(carrierFreq.set("carrier frequency", 440, 20, 1000));
gui.add(modFreq.set("mod frequency", 100, 0, 500));
gui.add(modIndex.set("mod index", 100, 0, 500));
gui.add(gain.set("gain", 0.5, 0, 1));
gui.add(cutoff.set("cutoff", 400, 1, 8000));
gui.add(resonance.set("resonance", 50, 0, 100));
gui.add(myOscFreq.set("my osc frequency", 100, 0, 500));
gui.add(myOscIndex.set("my osc index", 100, 0, 500));
// switch on the DAC - needs to happen at the end of setup
ofSoundStreamSetup(2, 2, this, sampleRate, bufferSize, 4);
}
//--------------------------------------------------------------
void ofApp::update() {
}
//--------------------------------------------------------------
void ofApp::draw() {
gui.draw();
drawWaveform();
}
//--------------------------------------------------------------
void ofApp::audioOut(float * output, int bufferSize, int nChannels) {
for (int i = 0; i < bufferSize; i++) {
// modulator
modOUT = mod.square(modFreq) * modIndex;
myOscOUT = mod.saw(myOscFreq) * myOscIndex;
// carrier
oscOUT = carrier.sinewave(carrierFreq + modOUT + myOscOUT);
filterOUT = lowpass.lores(oscOUT, cutoff, resonance);
mix = filterOUT * gain;
output[i*nChannels] = mix; // left channel
output[i*nChannels + 1] = mix; // right channel
soundBuffer[i] = mix;
}
}
//--------------------------------------------------------------
void ofApp::drawWaveform() {
ofNoFill();
ofSetColor(50);
ofSetLineWidth(5);
ofBeginShape();
for (int i = 0; i < bufferSize; i++) {
float x = ofMap(i, 0, bufferSize, 0, ofGetWidth());
if (pause == false) {
ofVertex(x, ofGetHeight()*.5 + soundBuffer[i] * 200);
pauseBuffer[i] = soundBuffer[i];
}
else
ofVertex(x, ofGetHeight()*.5 + pauseBuffer[i] * 200);
}
ofEndShape();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
if (key == 32) pause = !pause;
if (key == 114) random();
}
//--------------------------------------------------------------
void ofApp::random() {
carrierFreq.set(ofRandom(carrierFreq.getMin(), carrierFreq.getMax()));
modFreq.set(ofRandom(modFreq.getMin(), modFreq.getMax()));
modIndex.set(ofRandom(modIndex.getMin(), modIndex.getMax()));
cutoff.set(ofRandom(cutoff.getMin(), cutoff.getMax()));
resonance.set(ofRandom(resonance.getMin(), resonance.getMax()));
myOscFreq.set(ofRandom(myOscFreq.getMin(), myOscFreq.getMax()));
myOscIndex.set(ofRandom(myOscIndex.getMin(), myOscIndex.getMax()));
}