mirror of
https://github.com/projectM-visualizer/projectm.git
synced 2026-03-07 07:55:15 +00:00
100 lines
3.0 KiB
C++
Executable File
100 lines
3.0 KiB
C++
Executable File
/**
|
|
* projectM -- Milkdrop-esque visualisation SDK
|
|
* Copyright (C)2003-2004 projectM Team
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library 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
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
* See 'LICENSE.txt' included within this release
|
|
*
|
|
*/
|
|
/**
|
|
* Takes sound data from wherever and returns beat detection values
|
|
* Uses statistical Energy-Based methods. Very simple
|
|
*
|
|
* Some stuff was taken from Frederic Patin's beat-detection article,
|
|
* you'll find it online
|
|
*/
|
|
|
|
#include <numeric>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "wipemalloc.h"
|
|
|
|
#include "BeatDetect.hpp"
|
|
#include "PCM.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
|
|
BeatDetect::BeatDetect(Pcm& _pcm)
|
|
: pcm(_pcm)
|
|
{
|
|
}
|
|
|
|
|
|
void BeatDetect::Reset()
|
|
{
|
|
this->treb = 0;
|
|
this->mid = 0;
|
|
this->bass = 0;
|
|
this->trebAtt = 0;
|
|
this->midAtt = 0;
|
|
this->bassAtt = 0;
|
|
this->volAtt = 0;
|
|
this->volOld = 0;
|
|
}
|
|
|
|
|
|
void BeatDetect::CalculateBeatStatistics()
|
|
{
|
|
volOld = vol;
|
|
|
|
std::array<float, fftLength> vdataL{};
|
|
std::array<float, fftLength> vdataR{};
|
|
pcm.GetSpectrum(vdataL.data(), CHANNEL_0, fftLength);
|
|
pcm.GetSpectrum(vdataR.data(), CHANNEL_1, fftLength);
|
|
|
|
auto const intensityBetween =
|
|
[&vdataL, &vdataR](size_t const from, size_t const to) {
|
|
return std::accumulate(&vdataL[from], &vdataL[to], 0.f) +
|
|
std::accumulate(&vdataR[from], &vdataR[to], 0.f);
|
|
};
|
|
|
|
auto const& updateBand =
|
|
[](float& band, float& bandAtt, LowPassFilter& bandSmoothed, float const bandIntensity) {
|
|
bandSmoothed.Update(bandIntensity);
|
|
band = bandIntensity / std::max(0.0001f, bandSmoothed.Get());
|
|
bandAtt = .6f * bandAtt + .4f * band;
|
|
bandAtt = std::min(bandAtt, 100.f);
|
|
band = std::min(band, 100.f);
|
|
};
|
|
|
|
static_assert(fftLength >= 256, "fftLength too small");
|
|
std::array<size_t, 4> constexpr ranges{0, 3, 23, 255};
|
|
|
|
float const bassIntensity = intensityBetween(ranges[0], ranges[1]);
|
|
updateBand(bass, bassAtt, bassSmoothed, bassIntensity);
|
|
|
|
float const midIntensity = intensityBetween(ranges[1], ranges[2]);
|
|
updateBand(mid, midAtt, midSmoothed, midIntensity);
|
|
|
|
float const trebIntensity = intensityBetween(ranges[2], ranges[3]);
|
|
updateBand(treb, trebAtt, trebSmoothed, trebIntensity);
|
|
|
|
float const volIntensity = bassIntensity + midIntensity + trebIntensity;
|
|
updateBand(vol, volAtt, volSmoothed, volIntensity);
|
|
}
|