Permalink
Browse files

first commit, moving from google code

  • Loading branch information...
0 parents commit 8b466355b2ad9a1a29307fc3d850103a41519337 @kylemcdonald committed Oct 31, 2011
@@ -0,0 +1,12 @@
+*.depend
+*.layout
+*.mode*v3
+*.pbxuser
+*.app*
+*.DS_*
+
+.svn/
+obj/
+bin/
+build/
+!data/

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,9 @@
+//THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT.
+//THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED
+OF_PATH = ../../..
+
+//THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
+#include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
+
+OTHER_LDFLAGS = $(OF_CORE_LIBS)
+HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS)
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.yourcompany.openFrameworks</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+</dict>
+</plist>
@@ -0,0 +1,9 @@
+#include "ofMain.h"
+#include "testApp.h"
+#include "ofAppGlutWindow.h"
+
+int main() {
+ ofAppGlutWindow window;
+ ofSetupOpenGL(&window, 512 + 32, (128 + 16) * 4 + 16, OF_WINDOW);
+ ofRunApp(new testApp());
+}
@@ -0,0 +1,141 @@
+#include "testApp.h"
+
+void testApp::setup() {
+ ofSetVerticalSync(true);
+
+ plotHeight = 128;
+ bufferSize = 8192;
+
+ //fft = ofxFft::create(bufferSize, OF_FFT_WINDOW_HAMMING);
+ // To use FFTW, try:
+ fft = ofxFft::create(bufferSize, OF_FFT_WINDOW_HAMMING, OF_FFT_FFTW);
+
+ spectrogram.allocate(512, fft->getBinSize() / 4, OF_IMAGE_GRAYSCALE);
+ memset(spectrogram.getPixels(), 0, (int) (spectrogram.getWidth() * spectrogram.getHeight()) );
+ spectrogramOffset = 0;
+
+ drawBuffer.resize(bufferSize);
+ middleBuffer.resize(bufferSize);
+ audioBuffer.resize(bufferSize);
+
+ drawBins.resize(fft->getBinSize());
+ middleBins.resize(fft->getBinSize());
+ audioBins.resize(fft->getBinSize());
+
+ // 0 output channels,
+ // 1 input channel
+ // 44100 samples per second
+ // [bins] samples per buffer
+ // 4 num buffers (latency)
+
+ ofSoundStreamSetup(0, 1, this, 44100, bufferSize, 4);
+
+ mode = SINE;
+ appWidth = ofGetWidth();
+ appHeight = ofGetHeight();
+
+ ofBackground(0, 0, 0);
+}
+
+void testApp::draw() {
+ ofSetColor(255);
+ ofPushMatrix();
+ ofTranslate(16, 16);
+ ofDrawBitmapString("Time Domain", 0, 0);
+
+ soundMutex.lock();
+ drawBuffer = middleBuffer;
+ drawBins = middleBins;
+ soundMutex.unlock();
+
+ plot(drawBuffer, plotHeight / 2, 0);
+ ofTranslate(0, plotHeight + 16);
+ ofDrawBitmapString("Frequency Domain", 0, 0);
+ plot(drawBins, -plotHeight, plotHeight / 2);
+ ofTranslate(0, plotHeight + 16);
+ spectrogram.update();
+ spectrogram.draw(0, 0);
+ ofPopMatrix();
+ string msg = ofToString((int) ofGetFrameRate()) + " fps";
+ ofDrawBitmapString(msg, appWidth - 80, appHeight - 20);
+}
+
+void testApp::plot(vector<float>& buffer, float scale, float offset) {
+ ofNoFill();
+ int n = buffer.size();
+ ofRect(0, 0, n, plotHeight);
+ glPushMatrix();
+ glTranslatef(0, plotHeight / 2 + offset, 0);
+ ofBeginShape();
+ for (int i = 0; i < n; i++) {
+ ofVertex(i, buffer[i] * scale);
+ }
+ ofEndShape();
+ glPopMatrix();
+}
+
+void testApp::audioReceived(float* input, int bufferSize, int nChannels) {
+ if (mode == MIC) {
+ // store input in audioInput buffer
+ memcpy(&audioBuffer[0], input, sizeof(float) * bufferSize);
+
+ float maxValue = 0;
+ for(int i = 0; i < bufferSize; i++) {
+ if(abs(audioBuffer[i]) > maxValue) {
+ maxValue = abs(audioBuffer[i]);
+ }
+ }
+ for(int i = 0; i < bufferSize; i++) {
+ audioBuffer[i] /= maxValue;
+ }
+
+ } else if (mode == NOISE) {
+ for (int i = 0; i < bufferSize; i++)
+ audioBuffer[i] = ofRandom(-1, 1);
+ } else if (mode == SINE) {
+ for (int i = 0; i < bufferSize; i++)
+ audioBuffer[i] = sinf(PI * i * mouseX / appWidth);
+ }
+
+ fft->setSignal(&audioBuffer[0]);
+
+ float* curFft = fft->getAmplitude();
+ memcpy(&audioBins[0], curFft, sizeof(float) * fft->getBinSize());
+
+ float maxValue = 0;
+ for(int i = 0; i < fft->getBinSize(); i++) {
+ if(abs(audioBins[i]) > maxValue) {
+ maxValue = abs(audioBins[i]);
+ }
+ }
+ for(int i = 0; i < fft->getBinSize(); i++) {
+ audioBins[i] /= maxValue;
+ }
+
+ int spectrogramWidth = (int) spectrogram.getWidth();
+ int spectrogramHeight = (int) spectrogram.getHeight();
+ unsigned char* pixels = spectrogram.getPixels();
+ for(int i = 0; i < spectrogramHeight; i++) {
+ pixels[i * spectrogramWidth + spectrogramOffset] = (unsigned char) (255. * audioBins[i]);
+ }
+ spectrogramOffset = (spectrogramOffset + 1) % spectrogramWidth;
+
+ soundMutex.lock();
+ middleBuffer = audioBuffer;
+ middleBins = audioBins;
+ soundMutex.unlock();
+}
+
+void testApp::keyPressed(int key) {
+ switch (key) {
+ case 'm':
+ mode = MIC;
+ break;
+ case 'n':
+ mode = NOISE;
+ break;
+ case 's':
+ mode = SINE;
+ break;
+ }
+}
@@ -0,0 +1,33 @@
+#pragma once
+
+#include "ofMain.h"
+#include "ofxFft.h"
+
+#define MIC 0
+#define NOISE 1
+#define SINE 2
+
+class testApp : public ofBaseApp {
+public:
+ void setup();
+ void plot(vector<float>& buffer, float scale, float offset);
+ void audioReceived(float* input, int bufferSize, int nChannels);
+ void draw();
+ void keyPressed(int key);
+
+ int plotHeight, bufferSize;
+
+ ofxFft* fft;
+
+ float appWidth;
+ float appHeight;
+
+ ofImage spectrogram;
+ int spectrogramOffset;
+
+ int mode;
+
+ ofMutex soundMutex;
+ vector<float> drawBuffer, middleBuffer, audioBuffer;
+ vector<float> drawBins, middleBins, audioBins;
+};
@@ -0,0 +1,9 @@
+#include "ofMain.h"
+#include "testApp.h"
+#include "ofAppGlutWindow.h"
+
+int main() {
+ ofAppGlutWindow window;
+ ofSetupOpenGL(&window, 512 + 32, (128 + 32) * 3, OF_WINDOW);
+ ofRunApp(new testApp());
+}
@@ -0,0 +1,111 @@
+#include "testApp.h"
+
+void testApp::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 = SINE;
+ appWidth = ofGetWidth();
+ appHeight = ofGetHeight();
+
+ ofSoundStreamSetup(0, 1, this, 44100, bufferSize, 4);
+
+ ofBackground(0, 0, 0);
+}
+
+void testApp::draw() {
+ ofSetColor(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 testApp::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 testApp::audioReceived(float* input, int bufferSize, int nChannels) {
+ 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 * mouseX / 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());
+}
+
+void testApp::keyPressed(int key) {
+ switch (key) {
+ case 'm':
+ mode = MIC;
+ break;
+ case 'n':
+ mode = NOISE;
+ break;
+ case 's':
+ mode = SINE;
+ break;
+ }
+}
@@ -0,0 +1,32 @@
+#pragma once
+
+#include "ofMain.h"
+#include "ofxFft.h"
+
+#define MIC 0
+#define NOISE 1
+#define SINE 2
+
+class testApp : public ofBaseApp {
+public:
+ void setup();
+ void plot(float* array, int length, float scale, float offset);
+ void audioReceived(float* input, int bufferSize, int nChannels);
+ void draw();
+ void keyPressed(int key);
+
+ int plotHeight, bufferSize;
+
+ ofxFft* fft;
+
+ float* audioInput;
+ float* fftOutput;
+ float* eqFunction;
+ float* eqOutput;
+ float* ifftOutput;
+
+ float appWidth;
+ float appHeight;
+
+ int mode;
+};
Oops, something went wrong.

0 comments on commit 8b46635

Please sign in to comment.