-
Notifications
You must be signed in to change notification settings - Fork 51
Open
Description
Hi there!
I'm trying to create a class just for sound analysis but i can't run the audioReceived() function outside testApp.
I'm tying to adapt visualizer example to do it so far i have the following code.
Maybe you can help me?
thanks in advance
João
testApp.h
#pragma once
#include "ofMain.h"
#include "audioGuiController.h"
#include "audioAnalysis.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
audioAnalysis audio;
};
testApp.cpp
#include "testApp.h"
void testApp::setup(){
audio.setup();
}
void testApp::update(){
audio.update();
}
void testApp::draw(){
audio.draw();
}
audioAnalysis.h
#pragma once
#include "ofMain.h"
#include "ofxFft.h"
#define MIC 0
#define NOISE 1
#define SINE 2
class audioAnalysis : public ofBaseSoundInput{
public:
//audioAnalysis();
void setup();
void update();
void draw();
//
void plot(float* array, int length, float scale, float offset);
void audioReceived(float* input, int bufferSize, int nChannels);
int plotHeight, bufferSize;
ofxFft* fft;
float* audioInput;
float* fftOutput;
float* eqFunction;
float* eqOutput;
float* ifftOutput;
float appWidth;
float appHeight;
int mode;
//
//void audioIn(float * input, int bufferSize, int nChannels);
vector <float> left;
vector <float> right;
vector <float> volHistory;
int bufferCounter;
int drawCounter;
float smoothedVol;
float scaledVol;
ofSoundStream soundStream;
float * fftSmoothed;
};
audioAnalysis.cpp
#include "audioAnalysis.h"
void audioAnalysis::setup(){
plotHeight = 128;
bufferSize = 512;
fft = ofxFft::create(bufferSize, OF_FFT_WINDOW_BARTLETT);
// To use with FFTW, try:
// fft = ofxFft::create(bufferSize, OF_FFT_WINDOW_BARTLETT, OF_FFT_FFTW);
audioInput = new float[bufferSize];
fftOutput = new float[fft->getBinSize()];
eqFunction = new float[fft->getBinSize()];
eqOutput = new float[fft->getBinSize()];
ifftOutput = new float[bufferSize];
// 0 output channels,
// 1 input channel
// 44100 samples per second
// [bins] samples per buffer
// 4 num buffers (latency)
// this describes a linear low pass filter
for(int i = 0; i < fft->getBinSize(); i++)
eqFunction[i] = (float) (fft->getBinSize() - i) / (float) fft->getBinSize();
mode = MIC;
appWidth = ofGetWidth();
appHeight = ofGetHeight();
ofSoundStreamSetup(0, 1, 44100, bufferSize, 4); // ofSoundStreamSetup(0, 1, this, 44100, bufferSize, 4);
soundStream.setInput(this); //
ofBackground(0, 0, 0);
}
void audioAnalysis::draw(){
ofSetHexColor(0xffffff);
ofPushMatrix();
glTranslatef(16, 16, 0);
ofDrawBitmapString("Audio Input", 0, 0);
plot(audioInput, bufferSize, plotHeight / 2, 0);
glTranslatef(0, plotHeight + 16, 0);
ofDrawBitmapString("FFT Output", 0, 0);
plot(fftOutput, fft->getBinSize(), -plotHeight, plotHeight / 2);
ofPushMatrix();
glTranslatef(fft->getBinSize(), 0, 0);
ofDrawBitmapString("EQd FFT Output", 0, 0);
plot(eqOutput, fft->getBinSize(), -plotHeight, plotHeight / 2);
ofPopMatrix();
glTranslatef(0, plotHeight + 16, 0);
ofDrawBitmapString("IFFT Output", 0, 0);
plot(ifftOutput, fft->getSignalSize(), plotHeight / 2, 0);
ofPopMatrix();
string msg = ofToString((int) ofGetFrameRate()) + " fps";
ofDrawBitmapString(msg, appWidth - 80, appHeight - 20);
}
void audioAnalysis::plot(float* array, int length, float scale, float offset) {
ofNoFill();
ofRect(0, 0, length, plotHeight);
glPushMatrix();
glTranslatef(0, plotHeight / 2 + offset, 0);
ofBeginShape();
for (int i = 0; i < length; i++)
ofVertex(i, array[i] * scale);
ofEndShape();
glPopMatrix();
}
void audioAnalysis::audioReceived(float* input, int bufferSize, int nChannels) {
//cout<<"qwerty"<<endl;
if (mode == MIC) {
// store input in audioInput buffer
memcpy(audioInput, input, sizeof(float) * bufferSize);
} else if (mode == NOISE) {
for (int i = 0; i < bufferSize; i++)
audioInput[i] = ofRandom(-1, 1);
} else if (mode == SINE) {
for (int i = 0; i < bufferSize; i++)
audioInput[i] = sinf(PI * i *200 / appWidth);
}
fft->setSignal(audioInput);
memcpy(fftOutput, fft->getAmplitude(), sizeof(float) * fft->getBinSize());
for(int i = 0; i < fft->getBinSize(); i++)
eqOutput[i] = fftOutput[i] * eqFunction[i];
fft->setPolar(eqOutput, fft->getPhase());
fft->clampSignal();
memcpy(ifftOutput, fft->getSignal(), sizeof(float) * fft->getSignalSize());
}Metadata
Metadata
Assignees
Labels
No labels