18 void setSampleRate (
double sampleRate)
23 void setSensitivity (
double newSensitivity)
25 const double C_MIN = 0.8f;
26 const double C_MAX = 4.0f;
28 sensitivity = C_MIN + newSensitivity * (C_MAX - C_MIN);
31 template <
typename Buffer>
32 void audioProcess (
const Buffer& buffer)
34 double blockEnergy = 0;
35 auto size = buffer.getSize();
37 for (
decltype (size.numChannels) chan = 0; chan < size.numChannels; ++chan)
39 auto d = buffer.getIterator (chan);
41 for (
decltype (size.numFrames) i = 0; i < size.numFrames; ++i)
43 auto sample =
static_cast<double> (*d);
44 blockEnergy += sample * sample;
49 pushEnergy (blockEnergy);
52 SampleCount getBlockSize()
const {
return blockSize; }
56 static constexpr int historyLength = 43;
57 double energy[historyLength] = {};
58 int curBlock = 0, lastBlock = -2;
59 SampleCount blockSize = 0;
61 double sensitivity = 0;
63 void pushEnergy (
double e)
65 if (curBlock < historyLength)
67 energy[curBlock++] = e;
69 if (curBlock == historyLength)
71 auto ave = getAverageEnergy();
73 for (
int i = 0; i < historyLength; ++i)
74 if (energy[i] > sensitivity * ave)
80 energy[curBlock % historyLength] = e;
82 if (e > sensitivity * getAverageEnergy())
91 if (i - 1 != lastBlock)
92 beatSamples.
add (i * blockSize);
97 double getAverageEnergy()
const noexcept
101 for (
auto e : energy)
104 return total / historyLength;