From 459d1daf71dd8991c60dc8c84d1154802eec331b Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Tue, 22 May 2018 00:00:06 +0100 Subject: OE started --- gistanalysis/src/main.cpp | 13 ++ gistanalysis/src/ofApp.cpp | 456 +++++++++++++++++++++++++++++++++++++++++++++ gistanalysis/src/ofApp.h | 132 +++++++++++++ 3 files changed, 601 insertions(+) create mode 100644 gistanalysis/src/main.cpp create mode 100644 gistanalysis/src/ofApp.cpp create mode 100644 gistanalysis/src/ofApp.h (limited to 'gistanalysis/src') diff --git a/gistanalysis/src/main.cpp b/gistanalysis/src/main.cpp new file mode 100644 index 0000000..e57370b --- /dev/null +++ b/gistanalysis/src/main.cpp @@ -0,0 +1,13 @@ +#include "ofMain.h" +#include "ofApp.h" + +//======================================================================== +int main( ){ + ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context + + // this kicks off the running of my app + // can be OF_WINDOW or OF_FULLSCREEN + // pass in width and height too: + ofRunApp(new ofApp()); + +} diff --git a/gistanalysis/src/ofApp.cpp b/gistanalysis/src/ofApp.cpp new file mode 100644 index 0000000..b6a0a1c --- /dev/null +++ b/gistanalysis/src/ofApp.cpp @@ -0,0 +1,456 @@ +#include "ofApp.h" + +using namespace std; + +//-------------------------------------------------------------- +void ofApp::setup(){ + ofSetCircleResolution(80); + ofSetFrameRate(60); + ofBackground(255); + ofEnableSmoothing(); + ofEnableAlphaBlending(); + ofSetVerticalSync(true); + + + bufferSize = 512; + sampleRate = 44100; + + + + + useMic = 1; + isPaused = 0; + + player.setLoop(true); + + mfccMax = 0; + showMFCC = 0; + + vector features = ofxGist::getFeatureNames(); + + int num = features.size(); + + for(int v = 0;vsetShowSmoothedCurve(1); + */ + + //gist.setUseForOnsetDetection(GIST_PEAK_ENERGY); + + //gist.setUseForOnsetDetection(GIST_SPECTRAL_DIFFERENCE); + //gist.setThreshold(GIST_SPECTRAL_DIFFERENCE, .2); + + gist.setUseForOnsetDetection(GIST_PEAK_ENERGY); + gist.setThreshold(GIST_PEAK_ENERGY, .05);// + + ofAddListener(GistEvent::ON,this,&ofApp::onNoteOn); + ofAddListener(GistEvent::OFF,this,&ofApp::onNoteOff); + + + + noteOnRadius = 0; + + soundStream.setup(this,0, 1, sampleRate, bufferSize, 1); + + loadSong("passades/1 The Ninth Set-sector1:sector2pt.1.aiff"); + //loadSong("assets/sounds/Coltrane_acc_VUIMM.wav"); + + plotter=Audioplotter(8); +} + + +void ofApp::onNoteOn(GistEvent &e){ + + noteOnRadius = 100; +}; + + +void ofApp::onNoteOff(GistEvent &e){ + + //noteOnRadius = 0; +}; + + +//-------------------------------------------------------------- +void ofApp::update(){ + + + + if(isPaused){ + return; + } + if(!useMic){ + if(player.isLoaded()){ + vector output = player.getCurrentBuffer(bufferSize); + processAudio(&output[0], bufferSize, 2); + fftSmoothed = player.getFFT(); + + plotter.addpoints(centre,256); + } + } + + + + int num = ofxGist::getFeatureNames().size(); + + for(int v = 0;vsetRange(gist.getMin(v),gist.getMax(v)); + plots[v]->update(gist.getValue(v)); + } + + + + vectormfcc = gist.getMelFrequencyCepstralCoefficients(); + + //vectormfcc = gist.getMelFrequencySpectrum(); + + if(mfccSmoothed.size().999f){ + mfccSmoothed[f] = mfcc[i]; + } + // let the smoothed value sink to zero: + mfccSmoothed[i] *= damping; + f++; + + if(mfccMaxsetRange(gist.getMFCCMin(i),gist.getMFCCMax(i)); + + mfccPlots[i]->update(mfcc[i]); + } + + + + + /* + //crest avg + plots.back()->setRange(gist.getMin(GIST_SPECTRAL_CREST),gist.getMax(GIST_SPECTRAL_CREST)); + plots.back()->update(gist.getAvg(GIST_SPECTRAL_CREST)); +*/ + + + if(noteOnRadius>0){ + noteOnRadius--; + } +} + +//-------------------------------------------------------------- +void ofApp::draw(){ + ofBackground(0); + + + ofFill(); + + + // draw the left: + //ofSetHexColor(0x333333); + int waveHeight = ofGetHeight(); + + //ofRect(0,0,256,waveHeight); + float barW = ofGetWidth()/(float)fftSmoothed.size(); + float currX = 0; + + ofSetColor(255,255,255,100); + + for (int i = 0; i < fftSmoothed.size(); i++){ + ofRect(currX,waveHeight,barW,-fftSmoothed[i]*waveHeight); + currX+=barW; + } + + currX = 0; + waveHeight = ofGetHeight(); + barW = ofGetWidth()/(float)mfccSmoothed.size(); + ofSetColor(255,100,100,200); + for (int i = 0; i < mfccSmoothed.size() && showMFCC; i++){ + + ofRect(currX,ofGetHeight(),barW,- ofMap(mfccSmoothed[i], 0.0,mfccMax, 0.0,1.0,true )*waveHeight); + currX+=barW; + } + + ofNoFill(); + + ofSetColor(0,255,100,255); + for (int i = 0; i < left.size(); i++){ + //ofLine(i,300+left[i]*waveHeight,i,300+left[i]*waveHeight+1); + } + ofSetColor(000,100,255,255); + for (int i = 0; i < right.size(); i++){ + //ofLine(i,500+right[i]*waveHeight,i,500+right[i]*waveHeight+1); + } + +/* + int num; + int margin = 5; + if(showMFCC){ + num = mfccPlots.size(); + int plotHeight = (ofGetHeight()-margin*num)/(float)num; + + for(int v = 0;vdraw(margin,margin+plotHeight*v, ofGetWidth()-20, 100); + } + }else{ + + + num = plots.size(); + + int plotHeight = (ofGetHeight()-margin*num)/(float)num; + + for(int v = 0;vdraw(margin,margin+plotHeight*v, ofGetWidth()-20, 100); + } + + } + */ + + ofFill(); + + ofSetColor(255,0,0,200); + ofCircle(ofGetWidth()/2,ofGetHeight()/2,noteOnRadius); + + /* + if(!showMFCC){ + + + ofSetColor(0,0,0,250); + ofRect(5,5,600,180); + stringstream str; + if(plots.size() ){ + for(int v = 0;vgetVariableName()<<" | min: "<draw(); + } + +} + + + +ofxHistoryPlot* ofApp::addGraph(string varName,float max,ofColor color){ + + ofxHistoryPlot* graph = new ofxHistoryPlot(NULL, varName, max, false); //true for autoupdate + //graph2->setLowerRange(0); //set only the lowest part of the range upper is adaptative to curve + graph->setAutoRangeShrinksBack(true); //graph2 scale can shrink back after growing if graph2 curves requires it + graph->setRange(0,max); + graph->setColor(color); + graph->setShowNumericalInfo(true); + graph->setRespectBorders(true); + graph->setLineWidth(2); + + + graph->setDrawBackground(false); + + graph->setDrawGrid(true); + graph->setGridColor(ofColor(30)); //grid lines color + graph->setGridUnit(14); + graph->setShowSmoothedCurve(0); //graph2 a smoothed version of the values, but alos the original in lesser alpha + graph->setSmoothFilter(0.1); //smooth filter strength + + graph->setMaxHistory(2000); + + + + return graph; + +}; + + +void ofApp::clear(){ + + +} + + +void ofApp::loadSong(string str){ + + cout<<"loadSong "<max){ + max=centre[i]; + } + } + + //ofLog()<<"audio data max "<buffer; + buffer.assign(&input[0],&input[bufferSize]); + + gist.processAudio(buffer, bufferSize, nChannels,sampleRate); +} + + +void ofApp::audioIn(float * input, int bufferSize, int nChannels){ + if(!useMic){ + return; + } + + processAudio(input, bufferSize, nChannels); + +} + + + + +//-------------------------------------------------------------- +void ofApp::keyPressed(int key){ + if(key =='f'){ + ofToggleFullscreen(); + } + + if(key =='m'){ + useMic = !useMic; + + if(!useMic){ + player.play(); + }else{ + player.stop(); + } + + + gist.clearHistory(); + } + + if(key ==' '){ + isPaused = !isPaused; + player.setPaused(isPaused); + } + + if(key =='r'){ + gist.clearHistory(); + } + + + if(key =='c'){ + showMFCC = !showMFCC; + } +} + +//-------------------------------------------------------------- +void ofApp::keyReleased(int key){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseMoved(int x, int y ){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseDragged(int x, int y, int button){ + if(!useMic){ + player.setPosition(x/(float)ofGetWidth()); + } + + float t = x/(float)ofGetWidth(); + gist.setThreshold(GIST_SPECTRAL_DIFFERENCE,t); +} + +//-------------------------------------------------------------- +void ofApp::mousePressed(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void ofApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void ofApp::windowResized(int w, int h){ + +} + +//-------------------------------------------------------------- +void ofApp::gotMessage(ofMessage msg){ + +} + +//-------------------------------------------------------------- +void ofApp::dragEvent(ofDragInfo dragInfo){ + clear(); + vector paths = ofSplitString(dragInfo.files[0], "data/"); + loadSong(paths[1]); + +} diff --git a/gistanalysis/src/ofApp.h b/gistanalysis/src/ofApp.h new file mode 100644 index 0000000..e3ad339 --- /dev/null +++ b/gistanalysis/src/ofApp.h @@ -0,0 +1,132 @@ +#pragma once + +#include "ofMain.h" +#include "ofxHelios.h" +#include "lineTransformer.h" + +//This is included only as a way of getting buffer out of loaded sound. +//There are many other ways you can do that. +//This player includes a version of kissFFT. You can remove the one included in Gist. +//https://github.com/borg/ofxOpenALSoundPlayer +#include "ofxOpenALSoundPlayer.h" + + +//Slightly modified to add a dynamic getVariable method to be able to plot based on +//gist feature list +//https://github.com/local-projects/ofxHistoryPlot +#include "ofxHistoryPlot.h" +#include "ofxGist.h" + + +class Audioplotter{ + //store and draw a numbr of audio samples + //how best to handle transforms - maybe pass in a transform to be added to 2nd and subsequent + //how best to handle length of history data - fixed number that can be set, or line budget? +public: + Audioplotter(int _size=1,bool _joined=true){ + set_size(_size); + set_joined(_joined); + } + void set_size(int _size){ + history_size=_size; + } + void set_joined(bool _joined){ + joined=_joined; + } + const vector &output(const ofMatrix4x4 xform=ofMatrix4x4(1.0f,0.0f,0.0f,0.0f, + 0.0f,1.0f,0.0f,0.0f, + 0.0f,0.0f,1.0f,0.0f, + 0.0f,0.0f,0.0f,1.0f)){ + //destructive or non? + float fadefactor=1.0f-(1.0f/history_size); + + for (int i=0;i &audio,int number){ + colourPolyline newdata; + int num=min(number,(int)audio.size()); + int step=audio.size()/num; + int start=audio.size()/(num+1); + for (int i=0;ihistory_size) { + data.pop_back(); + } + + } +private: + vector data; + bool joined; + int history_size; +}; + + +class ofApp : public ofBaseApp{ + + public: + void setup(); + void update(); + void draw(); + + void keyPressed(int key); + void keyReleased(int key); + void mouseMoved(int x, int y ); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); + + ofSoundStream soundStream; + ofxOpenALSoundPlayer player; + void processAudio(float * input, int bufferSize, int nChannels); + + void audioIn(float * input, int bufferSize, int nChannels); + + vectorfftSmoothed; + vector left; + vector right; + vector centre; + vector volHistory; + vectormfccSmoothed; + float mfccMax; + + + int bufferSize; + int sampleRate; + bool useMic; + + bool isPaused; + + void clear(); + void loadSong(string str); + + + vectorplots; + mapplotMap; + + ofxHistoryPlot* addGraph(string varName,float max,ofColor color); + + + ofxGist gist; + void onNoteOn(GistEvent &e); + void onNoteOff(GistEvent &e); + + int noteOnRadius; + + + bool showMFCC; + vectormfccPlots; + + ofxHelios laser; + + Audioplotter plotter; + +}; -- cgit v1.2.3