From 4e58547a6eb6fd1eabc2b2d1f3725fd4c539bfa3 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 19 Jan 2016 21:50:29 +0100 Subject: [PATCH] src/ofxAubioAttackClass.cpp: move from onsetClass, add beat --- src/ofxAubioAttackClass.cpp | 136 ++++++++++++++++++++++++++++++++++++++++++++ src/ofxAubioAttackClass.h | 69 ++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 src/ofxAubioAttackClass.cpp create mode 100644 src/ofxAubioAttackClass.h diff --git a/src/ofxAubioAttackClass.cpp b/src/ofxAubioAttackClass.cpp new file mode 100644 index 0000000..91e2e7e --- /dev/null +++ b/src/ofxAubioAttackClass.cpp @@ -0,0 +1,136 @@ +/* + Copyright (C) 2015 Paul Brossier + + 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 . + +*/ + +#include "ofxAubioAttackClass.h" +#include "ofLog.h" + +ofEvent ofxAubioAttackClass::gotGlobalOnsetClass = ofEvent(); +ofEvent ofxAubioAttackClass::gotGlobalBeatClass = ofEvent(); + +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 index 0000000..0f558f1 --- /dev/null +++ b/src/ofxAubioAttackClass.h @@ -0,0 +1,69 @@ +/* + Copyright (C) 2015 Paul Brossier + + 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 . + +*/ + +#pragma once + +#include +#include +#include "ofEvents.h" +#include "ofxAubioBlock.h" +#include "ofxAubioOnset.h" +#include "ofxAubioBeat.h" +#include "ofxAubioMelBands.h" + +class ofxAubioAttackClass : public ofxAubioBlock { + + public: + + ofxAubioAttackClass(); + ~ofxAubioAttackClass(); + + ofEvent gotOnsetClass; + static ofEvent gotGlobalOnsetClass; + void onsetEvent(float & time); + + ofEvent gotBeatClass; + static ofEvent 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::vectorenergies; + + 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; +}; -- 2.11.0