Last active
October 27, 2019 21:49
-
-
Save prisonerjohn/bf8bab6eea766d4c42ef1a39120e2c81 to your computer and use it in GitHub Desktop.
Sensing Machines Sound In
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
ofBackground(0); | |
// Print audio device list to the console. | |
soundStream.printDeviceList(); | |
// Setup sound stream. | |
ofSoundStreamSettings settings; | |
settings.setInListener(this); | |
//settings.sampleRate = 44100; | |
settings.bufferSize = 64; | |
settings.numOutputChannels = 0; | |
settings.numInputChannels = 2; | |
soundStream.setup(settings); | |
// Setup FFT. | |
fft = ofxFft::create(settings.bufferSize, OF_FFT_WINDOW_HAMMING); | |
amplitudeBands.resize(settings.bufferSize, 0.0f); | |
// Initialize sample history. | |
leftSamples.assign(settings.bufferSize, 0.0); | |
rightSamples.assign(settings.bufferSize, 0.0); | |
// Use ofParameter for volume values so that we can print them in the GUI. | |
currVol.set("Curr Vol", 0.0, 0.0, 1.0); | |
smoothVol.set("Smooth Vol", 0.0, 0.0, 1.0); | |
scaledVol.set("Scaled Vol", 0.0, 0.0, 1.0); | |
smoothPct.set("Smooth Pct", 0.05, 0.0, 1.0); | |
volPeak.set("Vol Peak", 0.001, 0.0, 1.0); | |
onsetThreshold.set("Onset Thresh", 0.5, 0.0, 1.0); | |
guiPanel.setup("Audio In", "settings.json"); | |
guiPanel.add(currVol); | |
guiPanel.add(smoothVol); | |
guiPanel.add(scaledVol); | |
guiPanel.add(smoothPct); | |
guiPanel.add(volPeak); | |
guiPanel.add(onsetThreshold); | |
} | |
void ofApp::update() | |
{ | |
// Scale the volume up to a 0-1 range. | |
volPeak = max(volPeak, smoothVol); | |
scaledVol = ofMap(smoothVol, 0.0, volPeak, 0.0, 1.0, true); | |
} | |
void ofApp::draw() | |
{ | |
ofSetLineWidth(2.0); | |
// Draw the left channel history. | |
ofSetColor(200, 0, 0); | |
ofBeginShape(); | |
for (int i = 0; i < leftSamples.size(); i++) | |
{ | |
float x = ofMap(i, 0, leftSamples.size(), 0, ofGetWidth()); | |
float y = ofMap(leftSamples[i], -1.0, 1.0, 0, 200); | |
ofVertex(x, y); | |
} | |
ofEndShape(false); | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, 0, ofGetWidth(), 200); | |
// Draw the right channel history. | |
ofSetColor(200, 0, 0); | |
ofBeginShape(); | |
for (int i = 0; i < rightSamples.size(); i++) | |
{ | |
float x = ofMap(i, 0, rightSamples.size(), 0, ofGetWidth()); | |
float y = ofMap(rightSamples[i], -1.0, 1.0, 200, 400); | |
ofVertex(x, y); | |
} | |
ofEndShape(false); | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, 200, ofGetWidth(), 200); | |
// Draw the frequency spectrum (FFT). | |
ofSetColor(0, 0, 200); | |
ofFill(); | |
float bandWidth = ofGetWidth() / fft->getBinSize(); | |
for (int i = 0; i < fft->getBinSize(); i++) | |
{ | |
// Negative height to draw from the bottom up. | |
ofDrawRectangle(i * bandWidth, 600, bandWidth, amplitudeBands[i] * -200); | |
} | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, 400, ofGetWidth(), 200); | |
// Draw the volume level. | |
float volWidth = ofMap(scaledVol, 0.0, 1.0, 0.0, ofGetWidth()); | |
ofFill(); | |
if (scaledVol < onsetThreshold) | |
{ | |
ofSetColor(200, 0, 0); | |
} | |
else | |
{ | |
ofSetColor(0, 200, 0); | |
} | |
ofDrawRectangle(0, ofGetHeight() - 120, volWidth, 120); | |
// Draw the threshold level. | |
ofSetColor(225); | |
float threshPos = ofMap(onsetThreshold, 0.0, 1.0, 0.0, ofGetWidth()); | |
ofDrawRectangle(threshPos, ofGetHeight() - 120, 2, 120); | |
// Draw a border around the box. | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, ofGetHeight() - 120, ofGetWidth(), 120); | |
guiPanel.draw(); | |
} | |
void ofApp::audioIn(ofSoundBuffer& input) | |
{ | |
// Calculate the volume using RMS. | |
currVol = 0.0; | |
int numCounted = 0; | |
for (int i = 0; i < input.getNumFrames(); i++) | |
{ | |
// Samples should be between -1 and 1, but sometimes go a bit haywire | |
// when there's no data, so let's clamp the values to avoid outliers. | |
leftSamples[i] = ofClamp(input[i * 2 + 0], -1, 1) * 0.5; | |
rightSamples[i] = ofClamp(input[i * 2 + 1], -1, 1) * 0.5; | |
currVol += leftSamples[i] * leftSamples[i]; | |
currVol += rightSamples[i] * rightSamples[i]; | |
numCounted += 2; | |
} | |
currVol /= (float)numCounted; | |
currVol = sqrt(currVol); | |
// Smooth volume over time. | |
smoothVol = ofLerp(smoothVol, currVol, smoothPct); | |
// Calculate amplitudes. | |
fft->setSignal(input.getBuffer()); | |
float* amplitude = fft->getAmplitude(); | |
// Scale FFT bands to 0-1 range. | |
float maxValue = 0; | |
for (int i = 0; i < fft->getBinSize(); i++) | |
{ | |
if (abs(amplitude[i]) > maxValue) | |
{ | |
maxValue = abs(amplitude[i]); | |
} | |
} | |
// Smooth FFT bands over time. | |
for (int i = 0; i < fft->getBinSize(); i++) | |
{ | |
if (maxValue > 0) | |
{ | |
amplitude[i] /= maxValue; | |
} | |
amplitudeBands[i] = ofLerp(amplitudeBands[i], amplitude[i], smoothPct); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxFft.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void audioIn(ofSoundBuffer& input); | |
ofSoundStream soundStream; | |
ofxFft* fft; | |
std::vector<float> amplitudeBands; | |
std::vector<float> leftSamples; | |
std::vector<float> rightSamples; | |
ofParameter<float> smoothPct; | |
ofParameter<float> volPeak; | |
ofParameter<float> currVol; | |
ofParameter<float> smoothVol; | |
ofParameter<float> scaledVol; | |
ofParameter<float> onsetThreshold; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
ofBackground(0); | |
// Print audio device list to the console. | |
soundStream.printDeviceList(); | |
// Setup sound stream. | |
ofSoundStreamSettings settings; | |
settings.setInListener(this); | |
//settings.sampleRate = 44100; | |
settings.bufferSize = 512; | |
settings.numOutputChannels = 0; | |
settings.numInputChannels = 2; | |
soundStream.setup(settings); | |
// Initialize sample history. | |
leftSamples.assign(settings.bufferSize, 0.0); | |
rightSamples.assign(settings.bufferSize, 0.0); | |
// Use ofParameter for volume values so that we can print them in the GUI. | |
currVol.set("Curr Vol", 0.0, 0.0, 1.0); | |
smoothVol.set("Smooth Vol", 0.0, 0.0, 1.0); | |
scaledVol.set("Scaled Vol", 0.0, 0.0, 1.0); | |
smoothPct.set("Smooth Pct", 0.05, 0.0, 1.0); | |
volPeak.set("Vol Peak", 0.001, 0.0, 1.0); | |
guiPanel.setup("Audio In", "settings.json"); | |
guiPanel.add(currVol); | |
guiPanel.add(smoothVol); | |
guiPanel.add(scaledVol); | |
guiPanel.add(smoothPct); | |
guiPanel.add(volPeak); | |
} | |
void ofApp::update() | |
{ | |
// Scale the volume up to a 0-1 range. | |
volPeak = max(volPeak, smoothVol); | |
scaledVol = ofMap(smoothVol, 0.0, volPeak, 0.0, 1.0, true); | |
} | |
void ofApp::draw() | |
{ | |
ofSetLineWidth(2.0); | |
// Draw the left channel history. | |
ofSetColor(200, 0, 0); | |
ofBeginShape(); | |
for (int i = 0; i < leftSamples.size(); i++) | |
{ | |
float x = ofMap(i, 0, leftSamples.size(), 0, ofGetWidth()); | |
float y = ofMap(leftSamples[i], -1.0, 1.0, 0, 200); | |
ofVertex(x, y); | |
} | |
ofEndShape(false); | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, 0, ofGetWidth(), 200); | |
// Draw the right channel history. | |
ofSetColor(200, 0, 0); | |
ofBeginShape(); | |
for (int i = 0; i < rightSamples.size(); i++) | |
{ | |
float x = ofMap(i, 0, rightSamples.size(), 0, ofGetWidth()); | |
float y = ofMap(rightSamples[i], -1.0, 1.0, 200, 400); | |
ofVertex(x, y); | |
} | |
ofEndShape(false); | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, 200, ofGetWidth(), 200); | |
// Draw the volume level. | |
float volWidth = ofMap(scaledVol, 0.0, 1.0, 0.0, ofGetWidth()); | |
ofSetColor(200, 0, 0); | |
ofFill(); | |
ofDrawRectangle(0, ofGetHeight() - 120, volWidth, 120); | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, ofGetHeight() - 120, ofGetWidth(), 120); | |
guiPanel.draw(); | |
} | |
void ofApp::audioIn(ofSoundBuffer& input) | |
{ | |
// Calculate the volume using RMS. | |
currVol = 0.0; | |
int numCounted = 0; | |
for (int i = 0; i < input.getNumFrames(); i++) | |
{ | |
// Samples should be between -1 and 1, but sometimes go a bit haywire | |
// when there's no data, so let's clamp the values to avoid outliers. | |
leftSamples[i] = ofClamp(input[i * 2 + 0], -1, 1) * 0.5; | |
rightSamples[i] = ofClamp(input[i * 2 + 1], -1, 1) * 0.5; | |
currVol += leftSamples[i] * leftSamples[i]; | |
currVol += rightSamples[i] * rightSamples[i]; | |
numCounted += 2; | |
} | |
currVol /= (float)numCounted; | |
currVol = sqrt(currVol); | |
smoothVol = ofLerp(smoothVol, currVol, smoothPct); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void audioIn(ofSoundBuffer& input); | |
ofSoundStream soundStream; | |
std::vector<float> leftSamples; | |
std::vector<float> rightSamples; | |
ofParameter<float> smoothPct; | |
ofParameter<float> volPeak; | |
ofParameter<float> currVol; | |
ofParameter<float> smoothVol; | |
ofParameter<float> scaledVol; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
ofBackground(0); | |
// Print audio device list to the console. | |
soundStream.printDeviceList(); | |
// Setup sound stream. | |
ofSoundStreamSettings settings; | |
settings.setInListener(this); | |
//settings.sampleRate = 44100; | |
settings.bufferSize = 512; | |
settings.numOutputChannels = 0; | |
settings.numInputChannels = 2; | |
soundStream.setup(settings); | |
// Initialize sample history. | |
leftSamples.assign(settings.bufferSize, 0.0); | |
rightSamples.assign(settings.bufferSize, 0.0); | |
// Use ofParameter for volume values so that we can print them in the GUI. | |
currVol.set("Curr Vol", 0.0, 0.0, 1.0); | |
smoothVol.set("Smooth Vol", 0.0, 0.0, 1.0); | |
scaledVol.set("Scaled Vol", 0.0, 0.0, 1.0); | |
smoothPct.set("Smooth Pct", 0.05, 0.0, 1.0); | |
volPeak.set("Vol Peak", 0.001, 0.0, 1.0); | |
onsetThreshold.set("Onset Thresh", 0.5, 0.0, 1.0); | |
guiPanel.setup("Audio In", "settings.json"); | |
guiPanel.add(currVol); | |
guiPanel.add(smoothVol); | |
guiPanel.add(scaledVol); | |
guiPanel.add(smoothPct); | |
guiPanel.add(volPeak); | |
guiPanel.add(onsetThreshold); | |
} | |
void ofApp::update() | |
{ | |
// Scale the volume up to a 0-1 range. | |
volPeak = max(volPeak, smoothVol); | |
scaledVol = ofMap(smoothVol, 0.0, volPeak, 0.0, 1.0, true); | |
} | |
void ofApp::draw() | |
{ | |
ofSetLineWidth(2.0); | |
// Draw the left channel history. | |
ofSetColor(200, 0, 0); | |
ofBeginShape(); | |
for (int i = 0; i < leftSamples.size(); i++) | |
{ | |
float x = ofMap(i, 0, leftSamples.size(), 0, ofGetWidth()); | |
float y = ofMap(leftSamples[i], -1.0, 1.0, 0, 200); | |
ofVertex(x, y); | |
} | |
ofEndShape(false); | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, 0, ofGetWidth(), 200); | |
// Draw the right channel history. | |
ofSetColor(200, 0, 0); | |
ofBeginShape(); | |
for (int i = 0; i < rightSamples.size(); i++) | |
{ | |
float x = ofMap(i, 0, rightSamples.size(), 0, ofGetWidth()); | |
float y = ofMap(rightSamples[i], -1.0, 1.0, 200, 400); | |
ofVertex(x, y); | |
} | |
ofEndShape(false); | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, 200, ofGetWidth(), 200); | |
// Draw the volume level. | |
float volWidth = ofMap(scaledVol, 0.0, 1.0, 0.0, ofGetWidth()); | |
ofFill(); | |
if (scaledVol < onsetThreshold) | |
{ | |
ofSetColor(200, 0, 0); | |
} | |
else | |
{ | |
ofSetColor(0, 200, 0); | |
} | |
ofDrawRectangle(0, ofGetHeight() - 120, volWidth, 120); | |
// Draw the threshold level. | |
ofSetColor(225); | |
float threshPos = ofMap(onsetThreshold, 0.0, 1.0, 0.0, ofGetWidth()); | |
ofDrawRectangle(threshPos, ofGetHeight() - 120, 2, 120); | |
// Draw a border around the box. | |
ofSetColor(225); | |
ofNoFill(); | |
ofDrawRectangle(0, ofGetHeight() - 120, ofGetWidth(), 120); | |
guiPanel.draw(); | |
} | |
void ofApp::audioIn(ofSoundBuffer& input) | |
{ | |
// Calculate the volume using RMS. | |
currVol = 0.0; | |
int numCounted = 0; | |
for (int i = 0; i < input.getNumFrames(); i++) | |
{ | |
// Samples should be between -1 and 1, but sometimes go a bit haywire | |
// when there's no data, so let's clamp the values to avoid outliers. | |
leftSamples[i] = ofClamp(input[i * 2 + 0], -1, 1) * 0.5; | |
rightSamples[i] = ofClamp(input[i * 2 + 1], -1, 1) * 0.5; | |
currVol += leftSamples[i] * leftSamples[i]; | |
currVol += rightSamples[i] * rightSamples[i]; | |
numCounted += 2; | |
} | |
currVol /= (float)numCounted; | |
currVol = sqrt(currVol); | |
smoothVol = ofLerp(smoothVol, currVol, smoothPct); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void audioIn(ofSoundBuffer& input); | |
ofSoundStream soundStream; | |
std::vector<float> leftSamples; | |
std::vector<float> rightSamples; | |
ofParameter<float> smoothPct; | |
ofParameter<float> volPeak; | |
ofParameter<float> currVol; | |
ofParameter<float> smoothVol; | |
ofParameter<float> scaledVol; | |
ofParameter<float> onsetThreshold; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
// Print audio device list to the console. | |
soundStream.printDeviceList(); | |
// Setup sound stream. | |
ofSoundStreamSettings settings; | |
settings.setInListener(this); | |
//settings.sampleRate = 44100; | |
settings.numOutputChannels = 0; | |
settings.numInputChannels = 2; | |
soundStream.setup(settings); | |
guiPanel.setup("Audio In", "settings.json"); | |
} | |
void ofApp::update() | |
{ | |
} | |
void ofApp::draw() | |
{ | |
guiPanel.draw(); | |
} | |
void ofApp::audioIn(ofSoundBuffer& input) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void keyPressed(int key); | |
void audioIn(ofSoundBuffer& input); | |
ofSoundStream soundStream; | |
ofxPanel guiPanel; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
void ofApp::setup() | |
{ | |
// Print audio device list to the console. | |
soundStream.printDeviceList(); | |
// Setup sound stream. | |
ofSoundStreamSettings settings; | |
settings.setInListener(this); | |
//settings.sampleRate = 44100; | |
settings.numOutputChannels = 0; | |
settings.numInputChannels = 2; | |
soundStream.setup(settings); | |
// Use ofParameter for volume values so that we can print them in the GUI. | |
currVol.set("Curr Vol", 0.0, 0.0, 1.0); | |
smoothVol.set("Smooth Vol", 0.0, 0.0, 1.0); | |
scaledVol.set("Scaled Vol", 0.0, 0.0, 1.0); | |
smoothPct.set("Smooth Pct", 0.05, 0.0, 1.0); | |
volPeak.set("Vol Peak", 0.001, 0.0, 1.0); | |
guiPanel.setup("Audio In", "settings.json"); | |
guiPanel.add(currVol); | |
guiPanel.add(smoothVol); | |
guiPanel.add(scaledVol); | |
guiPanel.add(smoothPct); | |
guiPanel.add(volPeak); | |
} | |
void ofApp::update() | |
{ | |
// Scale the volume up to a 0-1 range. | |
volPeak = max(volPeak, smoothVol); | |
scaledVol = ofMap(smoothVol, 0.0, volPeak, 0.0, 1.0, true); | |
} | |
void ofApp::draw() | |
{ | |
float volWidth = ofMap(scaledVol, 0.0, 1.0, 0.0, ofGetWidth()); | |
ofSetColor(200, 0, 0); | |
ofFill(); | |
ofDrawRectangle(0, ofGetHeight() - 120, volWidth, 120); | |
ofSetColor(0); | |
ofNoFill(); | |
ofDrawRectangle(0, ofGetHeight() - 120, ofGetWidth(), 120); | |
guiPanel.draw(); | |
} | |
void ofApp::audioIn(ofSoundBuffer& input) | |
{ | |
// Calculate the volume using RMS. | |
currVol = 0.0; | |
int numCounted = 0; | |
for (int i = 0; i < input.getNumFrames(); i++) | |
{ | |
// Samples should be between -1 and 1, but sometimes go a bit haywire | |
// when there's no data, so let's clamp the values to avoid outliers. | |
float leftSample = ofClamp(input[i * 2 + 0], -1, 1) * 0.5; | |
float rightSample = ofClamp(input[i * 2 + 1], -1, 1) * 0.5; | |
currVol += leftSample * leftSample; | |
currVol += rightSample * rightSample; | |
numCounted += 2; | |
} | |
currVol /= (float)numCounted; | |
currVol = sqrt(currVol); | |
smoothVol = ofLerp(smoothVol, currVol, smoothPct); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxGui.h" | |
class ofApp : public ofBaseApp | |
{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void keyPressed(int key); | |
void audioIn(ofSoundBuffer& input); | |
ofSoundStream soundStream; | |
ofParameter<float> smoothPct; | |
ofParameter<float> volPeak; | |
ofParameter<float> currVol; | |
ofParameter<float> smoothVol; | |
ofParameter<float> scaledVol; | |
ofxPanel guiPanel; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment