100 lines
1.7 KiB
C++
Executable File
100 lines
1.7 KiB
C++
Executable File
#ifndef BASSDETECTION_H
|
|
#define BASSDETECTION_H
|
|
|
|
#include <KLib/misc/gnuplot/Gnuplot.h>
|
|
#include <KLib/misc/gnuplot/GnuplotPlot.h>
|
|
#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
|
|
#include <KLib/misc/gnuplot/GnuplotSplot.h>
|
|
|
|
#include "BiquadFilterGate.h"
|
|
#include "MovingAVG.h"
|
|
#include "EndlessAVG.h"
|
|
|
|
#include <iostream>
|
|
|
|
template <typename Scalar> class BassDetection {
|
|
|
|
K::Gnuplot gp;
|
|
K::GnuplotPlot plot;
|
|
K::GnuplotPlotElementLines lines0;
|
|
|
|
BiquadFilterGate<1> filter;
|
|
MovingAVG<float> avg;
|
|
MovingAVG<float> avgLong;
|
|
|
|
public:
|
|
|
|
/** setup */
|
|
BassDetection() : avg(2500), avgLong(100000) {
|
|
setSampleRate(44100);
|
|
plot.add(&lines0);
|
|
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;
|
|
|
|
lines0.add(K::GnuplotPoint2(x, delta));
|
|
|
|
// debug view?
|
|
//if (x % 6000 == 0) {show();}
|
|
show();
|
|
|
|
if (x % 1000 == 0) {
|
|
return delta;
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
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 // BASSDETECTION_H
|