From 26476c375f4288fa8076bba146fe0a0b87c2f238 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Thu, 21 Jan 2016 09:09:35 +0100 Subject: [PATCH] src/ofxAubioFilterDetect.*: add cut-off high and low pass detection --- src/ofxAubioFilterDetect.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++ src/ofxAubioFilterDetect.h | 54 +++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 src/ofxAubioFilterDetect.cpp create mode 100644 src/ofxAubioFilterDetect.h diff --git a/src/ofxAubioFilterDetect.cpp b/src/ofxAubioFilterDetect.cpp new file mode 100644 index 0000000..2d0dab8 --- /dev/null +++ b/src/ofxAubioFilterDetect.cpp @@ -0,0 +1,102 @@ +/* + 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 +#define LIN2DB(v) (20.0*log10f(v)) + +#include "ofxAubioFilterDetect.h" +#include "ofLog.h" + +ofxAubioFilterDetect::ofxAubioFilterDetect() + : bands(NULL) +{ + lag = 50; +} + +ofxAubioFilterDetect::~ofxAubioFilterDetect() +{ +} + +void ofxAubioFilterDetect::setup() +{ + setup("default", 512, 256, 44100); +} +void ofxAubioFilterDetect::setup(string method, int buf_s, int hop_s, int samplerate) +{ + hop_size = hop_s; + buf_size = buf_s; +} + +void ofxAubioFilterDetect::setBands(ofxAubioMelBands & _bands) { + bands = &_bands; +} + +void ofxAubioFilterDetect::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 ofxAubioFilterDetect::blockAudioIn() { + energies.push_back(bands->energies); + if (energies.size() > lag) { + energies.erase (energies.begin()); + } + filterDetect(); +} + +void ofxAubioFilterDetect::filterDetect() { + if (energies.size() >= lag) { + max_low_cutoff = 0; + for (int i = 0; i < bands->nBands; i ++) { + float band_sum = 0; + for (int j = 0; j < lag; j ++) { + band_sum += (energies[energies.size() - j - 1][i]); + } + if (band_sum < 0.01) { + max_low_cutoff = i; + } else { + break; + } + } + min_high_cutoff = 0; + for (int i = bands->nBands - 1; i >= 0; i --) { + float band_sum = 0; + for (int j = 0; j < lag; j ++) { + band_sum += (energies[energies.size() - j - 1][i]); + } + if (band_sum < 0.0001) { + min_high_cutoff = bands->nBands - 1 - i; + } else { + break; + } + } + //ofLog() << "got min_high_cutoff: " << min_high_cutoff + // << " max_low_cutoff: " << max_low_cutoff; + } +} diff --git a/src/ofxAubioFilterDetect.h b/src/ofxAubioFilterDetect.h new file mode 100644 index 0000000..7a0151a --- /dev/null +++ b/src/ofxAubioFilterDetect.h @@ -0,0 +1,54 @@ +/* + 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 ofxAubioFilterDetect: public ofxAubioBlock { + + public: + + ofxAubioFilterDetect(); + ~ofxAubioFilterDetect(); + + ofxAubioMelBands *bands; + void setBands(ofxAubioMelBands & bands); + std::vectorenergies; + + void setup(); + void setup(std::string method, int buf_s, int hop_s, int samplerate); + + void audioIn(float *input, int bufferSize, int nChannels); + + void filterDetect(); + + int lag; + int max_low_cutoff; + int min_high_cutoff; + private: + void blockAudioIn(); +}; -- 2.11.0