#ifndef BASSDETECTION_H #define BASSDETECTION_H //#define PLOT_ME #ifdef PLOT_ME #include #include #include #include #endif #include "BiquadFilterGate.h" #include "MovingAVG.h" #include "EndlessAVG.h" #include template class BassDetection { #ifdef PLOT_ME K::Gnuplot gp; K::GnuplotPlot plot; K::GnuplotPlotElementLines lines0; #endif BiquadFilterGate<1> filter; MovingAVG avg; MovingAVG avgLong; public: /** setup */ BassDetection() : avg(2500), avgLong(100000) { setSampleRate(44100); #ifdef PLOT_ME plot.add(&lines0); #endif avgLong.add(1); } void setSampleRate(int srate) { std::cout << "setting sample-rate to " << srate << std::endl; filter.setLowPass(80, 1, srate); } /** add single value */ float add(Scalar s) { const float val = s / 32768.0f; const float res = filter.filter(0, val); avg.add(res*res); avgLong.add(res*res); const float res2 = avg.get() / avgLong.get(); avgLong.add(res2); static float prev = 0; static int x = 0; ++x; if (x % 1000 == 0) { const float delta = res2 - prev; prev = res2; #ifdef PLOT_ME lines0.add(K::GnuplotPoint2(x, delta)); #endif // debug view? //if (x % 6000 == 0) {show();} #ifdef PLOT_ME show(); #endif if (x % 1000 == 0) { return delta; } } return 0; } #ifdef PLOT_ME void show() { int limit = 500; int d0 = lines0.size() - limit; if (d0 > 0) { lines0.remove(0, d0); } int x0 = lines0[0].x; int x1 = lines0[lines0.size()-1].x; plot.getAxisX().setRange(x0, x1); plot.getAxisY().setRange(-1, +5); //plot.getAxisY().setRange(-32768, +32768); gp.draw(plot); gp.flush(); } #endif }; #endif // BASSDETECTION_H