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/ofApp.cpp | 456 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 456 insertions(+) create mode 100644 gistanalysis/src/ofApp.cpp (limited to 'gistanalysis/src/ofApp.cpp') 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]); + +} -- cgit v1.2.3