src/ofxAubioAttackClass.cpp: add real audioIn
authorPaul Brossier <piem@piem.org>
Tue, 19 Jan 2016 23:46:54 +0000 (00:46 +0100)
committerPaul Brossier <piem@piem.org>
Tue, 19 Jan 2016 23:46:54 +0000 (00:46 +0100)
example_aubioDemo/src/ofApp.cpp
src/ofxAubioAttackClass.cpp
src/ofxAubioAttackClass.h

index bd61a67..d7e4206 100644 (file)
@@ -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);
 
 }
 
index 9824c52..40a8201 100644 (file)
@@ -18,6 +18,9 @@
 
 */
 
+#include <math.h>
+#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;
index 976c28c..ac8cf33 100644 (file)
@@ -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();
 };