From 4578136a0ffec7730a831bdcc70ca7201fa0e83f Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Wed, 20 Jan 2016 00:46:54 +0100 Subject: [PATCH] src/ofxAubioAttackClass.cpp: add real audioIn --- example_aubioDemo/src/ofApp.cpp | 8 +++++--- src/ofxAubioAttackClass.cpp | 28 +++++++++++++++++++++++----- src/ofxAubioAttackClass.h | 3 ++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/example_aubioDemo/src/ofApp.cpp b/example_aubioDemo/src/ofApp.cpp index bd61a67..d7e4206 100644 --- a/example_aubioDemo/src/ofApp.cpp +++ b/example_aubioDemo/src/ofApp.cpp @@ -87,7 +87,7 @@ void ofApp::audioIn(float * input, int bufferSize, int nChannels){ bands.audioIn(input, bufferSize, nChannels); // compute onset class - attackClass.audioIn(); //, onset, bands); + attackClass.audioIn(input, bufferSize, nChannels); } void audioOut(){ @@ -142,10 +142,12 @@ void ofApp::draw(){ bandPlot.draw(); // onset class - ofRect(250 + 190 + currentOnsetClass * 7, 150, 20, 20); + ofSetColor(200, 100, 100); + ofRect(250 + 190 + currentOnsetClass * 7, 180, 10, 30); // beat class - ofRect(190 + currentBeatClass * 7, 150, 20, 20); + ofSetColor(100, 200, 100); + ofRect(190 + currentBeatClass * 7, 170, 10, 30); } diff --git a/src/ofxAubioAttackClass.cpp b/src/ofxAubioAttackClass.cpp index 9824c52..40a8201 100644 --- a/src/ofxAubioAttackClass.cpp +++ b/src/ofxAubioAttackClass.cpp @@ -18,6 +18,9 @@ */ +#include +#define LIN2DB(v) (20.0*log10f(v)) + #include "ofxAubioAttackClass.h" #include "ofLog.h" @@ -44,6 +47,8 @@ void ofxAubioAttackClass::setup() void ofxAubioAttackClass::setup(string method, int buf_s, int hop_s, int samplerate) { //setup("default", 512, 256, 44100); + hop_size = hop_s; + buf_size = buf_s; } void ofxAubioAttackClass::setBands(ofxAubioMelBands & _bands) { @@ -60,8 +65,21 @@ void ofxAubioAttackClass::setBeat(ofxAubioBeat & _beat) { ofAddListener(beat->gotBeat, this, &ofxAubioAttackClass::beatEvent); } -void ofxAubioAttackClass::audioIn() +void ofxAubioAttackClass::audioIn(float * input, int bufferSize, int nChannels) { + uint_t i; + for (i = 0; i < bufferSize; i++) { + // run aubio block when appropriate + curpos += 1; + if (curpos == hop_size) + { + blockAudioIn(); + curpos = 0; + } + } +} + +void ofxAubioAttackClass::blockAudioIn() { energies.push_back(bands->energies); if (energies.size() > max(lag_onset, lag_beat)) { energies.erase (energies.begin()); @@ -92,11 +110,11 @@ void ofxAubioAttackClass::onsetEvent(float & time) void ofxAubioAttackClass::onsetClassify() { if (energies.size() >= lag_onset) { int max_band = 0; - float max_energy = 0; + float max_energy = -1000; for (int i = min_band_onset; i < bands->nBands; i ++) { float band_sum = 0; for (int j = 0; j < lag_onset; j ++) { - band_sum += energies[energies.size() - j - 1][i]; + band_sum += LIN2DB(energies[energies.size() - j - 1][i]); } if (max_energy < band_sum) { max_energy = band_sum; @@ -120,11 +138,11 @@ void ofxAubioAttackClass::beatEvent(float & time) void ofxAubioAttackClass::beatClassify() { if (energies.size() >= lag_beat) { int max_band = 0; - float max_energy = 0; + float max_energy = -1000; for (int i = min_band_beat; i < bands->nBands; i ++) { float band_sum = 0; for (int j = 0; j < lag_beat; j ++) { - band_sum += energies[energies.size() - j - 1][i]; + band_sum += LIN2DB(energies[energies.size() - j - 1][i]); } if (max_energy < band_sum) { max_energy = band_sum; diff --git a/src/ofxAubioAttackClass.h b/src/ofxAubioAttackClass.h index 976c28c..ac8cf33 100644 --- a/src/ofxAubioAttackClass.h +++ b/src/ofxAubioAttackClass.h @@ -56,7 +56,7 @@ class ofxAubioAttackClass : public ofxAubioBlock { void setup(); void setup(std::string method, int buf_s, int hop_s, int samplerate); - void audioIn(); + void audioIn(float *input, int bufferSize, int nChannels); void onsetClassify(); void beatClassify(); @@ -68,4 +68,5 @@ class ofxAubioAttackClass : public ofxAubioBlock { private: int startOnsetSelection; int startBeatSelection; + void blockAudioIn(); }; -- 2.11.0