src/ofxAubioAttackClass.cpp: move from onsetClass, add beat
authorPaul Brossier <piem@piem.org>
Tue, 19 Jan 2016 20:50:29 +0000 (21:50 +0100)
committerPaul Brossier <piem@piem.org>
Tue, 19 Jan 2016 20:50:29 +0000 (21:50 +0100)
src/ofxAubioAttackClass.cpp [new file with mode: 0644]
src/ofxAubioAttackClass.h [new file with mode: 0644]

diff --git a/src/ofxAubioAttackClass.cpp b/src/ofxAubioAttackClass.cpp
new file mode 100644 (file)
index 0000000..91e2e7e
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+  Copyright (C) 2015 Paul Brossier <piem@aubio.org>
+
+  This file is part of ofxAubio.
+
+  ofxAubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "ofxAubioAttackClass.h"
+#include "ofLog.h"
+
+ofEvent<int> ofxAubioAttackClass::gotGlobalOnsetClass = ofEvent<int>();
+ofEvent<int> ofxAubioAttackClass::gotGlobalBeatClass = ofEvent<int>();
+
+ofxAubioAttackClass::ofxAubioAttackClass()
+    : bands(NULL)
+{
+    lag_onset = 3;
+    lag_beat = 3;
+}
+
+ofxAubioAttackClass::~ofxAubioAttackClass()
+{
+}
+
+void ofxAubioAttackClass::setup()
+{
+    setup("default", 512, 256, 44100);
+}
+void ofxAubioAttackClass::setup(string method, int buf_s, int hop_s, int samplerate)
+{
+    //setup("default", 512, 256, 44100);
+}
+
+void ofxAubioAttackClass::setBands(ofxAubioMelBands & _bands) {
+    bands = &_bands;
+}
+
+void ofxAubioAttackClass::setOnset(ofxAubioOnset & _onset) {
+    onset = &_onset;
+    ofAddListener(onset->gotOnset, this, &ofxAubioAttackClass::onsetEvent);
+}
+
+void ofxAubioAttackClass::setBeat(ofxAubioBeat & _beat) {
+    beat = &_beat;
+    ofAddListener(beat->gotBeat, this, &ofxAubioAttackClass::beatEvent);
+}
+
+void ofxAubioAttackClass::audioIn()
+{
+    energies.push_back(bands->energies);
+    if (energies.size() > max(lag_onset, lag_beat) - 1) {
+        energies.erase (energies.begin());
+    }
+    // hack to add a counter to delay lag * blockSize frames
+    if (startOnsetSelection > 0) {
+        startOnsetSelection--;
+        if (startOnsetSelection == 1) {
+            onsetClassify();
+        }
+    }
+    if (startBeatSelection > 0) {
+        startBeatSelection--;
+        if (startBeatSelection == 1) {
+            beatClassify();
+        }
+    }
+}
+
+void ofxAubioAttackClass::onsetEvent(float & time)
+{
+    //ofLog() << "ofxAubioAttackClass got onset at " << time;
+    // hack to add a counter to delay lag * blockSize frames
+    int delay = 0;
+    startOnsetSelection = lag_onset + delay;
+}
+
+void ofxAubioAttackClass::onsetClassify() {
+    if (energies.size() == lag_onset - 1) {
+        int max_band = 0;
+        float max_energy = 0;
+        for (int i = 0; i < bands->nBands; i ++) {
+            float band_sum = 0;
+            for (int j = 0; j < energies.size(); j ++) {
+                band_sum += energies[j][i];
+            }
+            if (max_energy < band_sum) {
+                max_energy = band_sum;
+                max_band = i;
+            }
+        }
+        currentClass = max_band;
+        ofNotifyEvent(gotOnsetClass, currentClass, this);
+        ofNotifyEvent(gotGlobalOnsetClass, currentClass);
+    }
+}
+
+void ofxAubioAttackClass::beatEvent(float & time)
+{
+    //ofLog() << "ofxAubioAttackClass got onset at " << time;
+    // hack to add a counter to delay lag * blockSize frames
+    int delay = 0;
+    startBeatSelection = lag_beat + delay;
+}
+
+void ofxAubioAttackClass::beatClassify() {
+    if (energies.size() == lag_beat - 1) {
+        int max_band = 0;
+        float max_energy = 0;
+        for (int i = 0; i < bands->nBands; i ++) {
+            float band_sum = 0;
+            for (int j = 0; j < energies.size(); j ++) {
+                band_sum += energies[j][i];
+            }
+            if (max_energy < band_sum) {
+                max_energy = band_sum;
+                max_band = i;
+            }
+        }
+        currentClass = max_band;
+        ofNotifyEvent(gotBeatClass, currentClass, this);
+        ofNotifyEvent(gotGlobalBeatClass, currentClass);
+    }
+}
diff --git a/src/ofxAubioAttackClass.h b/src/ofxAubioAttackClass.h
new file mode 100644 (file)
index 0000000..0f558f1
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+  Copyright (C) 2015 Paul Brossier <piem@aubio.org>
+
+  This file is part of ofxAubio.
+
+  ofxAubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#pragma once
+
+#include <iostream>
+#include <aubio/aubio.h>
+#include "ofEvents.h"
+#include "ofxAubioBlock.h"
+#include "ofxAubioOnset.h"
+#include "ofxAubioBeat.h"
+#include "ofxAubioMelBands.h"
+
+class ofxAubioAttackClass : public ofxAubioBlock {
+
+    public:
+
+       ofxAubioAttackClass();
+       ~ofxAubioAttackClass();
+
+       ofEvent<int> gotOnsetClass;
+       static ofEvent<int> gotGlobalOnsetClass;
+       void onsetEvent(float & time);
+
+       ofEvent<int> gotBeatClass;
+       static ofEvent<int> gotGlobalBeatClass;
+       void beatEvent(float & time);
+
+       ofxAubioOnset *onset;
+       void setOnset(ofxAubioOnset & onset);
+       ofxAubioBeat *beat;
+       void setBeat(ofxAubioBeat & beat);
+       ofxAubioMelBands *bands;
+       void setBands(ofxAubioMelBands & bands);
+
+       int currentClass;
+       std::vector<float *>energies;
+
+       void setup();
+       void setup(std::string method, int buf_s, int hop_s, int samplerate);
+
+       void audioIn();
+
+       void onsetClassify();
+       void beatClassify();
+
+    private:
+       int lag_onset;
+       int lag_beat;
+       int startOnsetSelection;
+       int startBeatSelection;
+};