Skip to content
Snippets Groups Projects
Commit 08f46c7c authored by William Fish's avatar William Fish
Browse files

Merge branch 'fftfreq' into 'master'

Resolve "fftFreq"

Closes #2

See merge request !5
parents 980de4ac cec2a547
Branches
Tags
1 merge request!5Resolve "fftFreq"
//
// Visualizer.cpp
// Visualizer 2
//
// Created by james carty on 08/03/2018.
// William Fish - serial communication
//
#include "Visualizer.h"
void Visualizer::setup() {
// Setup the audio component
song.loadSound("C:/users/Billy/Desktop/fftLED/bin/data/techno.mp3");
song.setVolume(1.0f);
fftSmoothed = new float[8192];
for (int i=0; i < 8192; i++) {
fftSmoothed[i] = 0;
}
nBandsToGet = 256;
ofEnableSmoothing();
ofSetFrameRate(60); //speed up frame rate
serial.setup("COM5",57600);
// Allocate drawing buffer
int w = ofGetWidth();
int h = ofGetHeight();
fbo.allocate(w, h, GL_RGB32F_ARB);
// Fill buffer with white color
fbo.begin();
ofBackground(255,255,255);
fbo.end();
// Create particles
Particle mid1;
Param mid1Param;
mid1Param.sensitivity = 1.4;
mid1Param.emitterRad = 60;
mid1Param.direction = 1;
mid1Param.sections = 16;
mid1Param.nBands = nBandsToGet;
mid1Param.color = ofColor(200, 200, 100); //reduced colour gradient
mid1Param.numParticles = 2048;
mid1Param.rotationSpeed = -40;
mid1.setup(mid1Param);
particles.push_back(mid1);
// Start the song
song.setLoop(true);
song.play();
}
//-------------------------------------------
void Visualizer::update() {
ofSoundUpdate();
// grab the fft
curAvgVolume = 0;
float *val = ofSoundGetSpectrum(nBandsToGet);
//std::cout << *val << std::endl;
for (int i=0; i<nBandsToGet; i++) {
fftSmoothed[i] *= 0.94f;
if (fftSmoothed[i] < val[i]) {
fftSmoothed[i] = val[i];
}
curAvgVolume += fftSmoothed[i];
}
//send a byte over a volume threshold to create a 'pulse' in the LEDS, second byte turns them back off
if (*fftSmoothed>=4.2) {
serial.writeByte(1);
//serial.writeByte(0); //sending 2 bytes is slow
}
curAvgVolume /= nBandsToGet;
// Update the particles
for (int i=0; i<particles.size(); i++) {
particles[i].update(fftSmoothed, i * (nBandsToGet / particles.size()));
}
}
//--------------------------------------------------------------
void Visualizer::draw() {
float volume = ofMap(curAvgVolume, 0, 1, 40, 210);
ofColor inColor = ofColor(volume, volume+10, volume);
ofColor outColor = ofColor(volume, volume-40, volume);
fbo.begin();
ofRect(0, 0, ofGetWidth(), ofGetHeight());
ofBackgroundGradient(inColor, outColor, OF_GRADIENT_CIRCULAR);
for (int i=0; i<particles.size(); i++) {
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotate(ofGetElapsedTimef() * particles[i].param.rotationSpeed);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2);
particles[i].draw();
ofPopMatrix();
}
fbo.end();
ofSetColor(200,255,155);
fbo.draw(0, 0);
}
//--------------------------------------------------------------
void Visualizer::keyPressed(int key) {
}
//--------------------------------------------------------------
void Visualizer::keyReleased(int key) {
}
//--------------------------------------------------------------
void Visualizer::mouseMoved(int x, int y ) {
}
//--------------------------------------------------------------
void Visualizer::mouseDragged(int x, int y, int button) {
}
//--------------------------------------------------------------
void Visualizer::mousePressed(int x, int y, int button) {
}
//--------------------------------------------------------------
void Visualizer::mouseReleased(int x, int y, int button) {
}
//--------------------------------------------------------------
void Visualizer::windowResized(int w, int h) {
}
//--------------------------------------------------------------
void Visualizer::gotMessage(ofMessage msg) {
}
//--------------------------------------------------------------
void Visualizer::dragEvent(ofDragInfo dragInfo) {
}
//
// Visualizer.cpp
// Visualizer 2
//
// Created by james carty on 08/03/2018.
// William Fish - serial communication
//
#include "Visualizer.h"
void Visualizer::setup() {
// Setup the audio component
song.loadSound("C:/users/Billy/Desktop/fftLED/bin/data/techno.mp3");
song.setVolume(1.0f);
fftSmoothed = new float[8192];
for (int i = 0; i < 8192; i++) {
fftSmoothed[i] = 0;
}
nBandsToGet = 256;
ofEnableSmoothing();
ofSetFrameRate(60); //speed up frame rate
serial.setup("COM5", 57600);
// Allocate drawing buffer
int w = ofGetWidth();
int h = ofGetHeight();
fbo.allocate(w, h, GL_RGB32F_ARB);
// Fill buffer with white color
fbo.begin();
ofBackground(255, 255, 255);
fbo.end();
// Create particles
Particle mid1;
Param mid1Param;
mid1Param.sensitivity = 1.4;
mid1Param.emitterRad = 60;
mid1Param.direction = 1;
mid1Param.sections = 16;
mid1Param.nBands = nBandsToGet;
mid1Param.color = ofColor(200, 200, 100); //reduced colour gradient
mid1Param.numParticles = 2048;
mid1Param.rotationSpeed = -40;
mid1.setup(mid1Param);
particles.push_back(mid1);
// Start the song
song.setLoop(true);
song.play();
}
//-------------------------------------------
void Visualizer::update() {
ofSoundUpdate();
// grab the fft
curAvgVolumeLow = 0;
curAvgVolumeMid = 0;
curAvgVolumeHigh = 0;
float *val = ofSoundGetSpectrum(nBandsToGet);
//std::cout << *val << std::endl;
//isolate low freq
for (int i = 0; i < 3; i++) {
fftSmoothed[i] *= 0.94f;
if (fftSmoothed[i] < val[i]) {
fftSmoothed[i] = val[i];
}
curAvgVolumeLow += fftSmoothed[i];
}
curAvgVolumeLow /= nBandsToGet;
//isolate mid freq
for (int i = 3; i < 9; i++) {
fftSmoothed[i] *= 0.94f;
if (fftSmoothed[i] < val[i]) {
fftSmoothed[i] = val[i];
}
curAvgVolumeMid += fftSmoothed[i];
}
curAvgVolumeMid /= nBandsToGet;
//isolate high freq
for (int i = 9; i < nBandsToGet; i++) {
fftSmoothed[i] *= 0.94f;
if (fftSmoothed[i] < val[i]) {
fftSmoothed[i] = val[i];
}
curAvgVolumeHigh += fftSmoothed[i];
}
curAvgVolumeHigh /= nBandsToGet;
//testing for isolating low mid and high , values need adjusting
if (curAvgVolumeLow > 0.02) {
//std::cout << curAvgVolumeLow << "Low" << std::endl;
}
if (curAvgVolumeMid > 0.01) {
std::cout << curAvgVolumeHigh << "high" << std::endl;
}
if (curAvgVolumeHigh > 0.02) {
//std::cout << curAvgVolumeHigh << "High" << std::endl;
}
//send a byte over a volume threshold to create a 'pulse' in the LEDS, second byte turns them back off
//if (*fftSmoothed>=4.2) {
// serial.writeByte(1);
// //serial.writeByte(0); //sending 2 bytes is //}
// Update the particles
for (int i = 0; i < particles.size(); i++) {
particles[i].update(fftSmoothed, i * (nBandsToGet / particles.size()));
}
}
//--------------------------------------------------------------
void Visualizer::draw() {
float volume = ofMap(curAvgVolumeLow, 0, 1, 40, 210);
ofColor inColor = ofColor(volume, volume + 10, volume);
ofColor outColor = ofColor(volume, volume - 40, volume);
fbo.begin();
ofRect(0, 0, ofGetWidth(), ofGetHeight());
ofBackgroundGradient(inColor, outColor, OF_GRADIENT_CIRCULAR);
for (int i = 0; i < particles.size(); i++) {
ofPushMatrix();
ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2);
ofRotate(ofGetElapsedTimef() * particles[i].param.rotationSpeed);
ofTranslate(-ofGetWidth() / 2, -ofGetHeight() / 2);
particles[i].draw();
ofPopMatrix();
}
fbo.end();
ofSetColor(200, 255, 155);
fbo.draw(0, 0);
}
//--------------------------------------------------------------
void Visualizer::keyPressed(int key) {
}
//--------------------------------------------------------------
void Visualizer::keyReleased(int key) {
}
//--------------------------------------------------------------
void Visualizer::mouseMoved(int x, int y) {
}
//--------------------------------------------------------------
void Visualizer::mouseDragged(int x, int y, int button) {
}
//--------------------------------------------------------------
void Visualizer::mousePressed(int x, int y, int button) {
}
//--------------------------------------------------------------
void Visualizer::mouseReleased(int x, int y, int button) {
}
//--------------------------------------------------------------
void Visualizer::windowResized(int w, int h) {
}
//--------------------------------------------------------------
void Visualizer::gotMessage(ofMessage msg) {
}
//--------------------------------------------------------------
void Visualizer::dragEvent(ofDragInfo dragInfo) {
}
......@@ -34,7 +34,9 @@ public:
ofSoundPlayer song;
float *fftSmoothed;
int nBandsToGet;
float curAvgVolume;
float curAvgVolumeLow;
float curAvgVolumeMid;
float curAvgVolumeHigh;
//LEDS
ofSerial serial; //create serial
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment