Skip to content

Commit c7db1ca

Browse files
committed
Rewrite of audio data processing/beat detection.
Consolidated audio processing code into the PCM class, removing the BeatDetect class in the process. Beat detection now uses the same algorithm as Milkdrop, passing the proper relative bass/mid/treb values to presets. Makes many presets look less jumpy/flickering, as the values are now (smoothly) alternating around 1.0. Updating frame audio is now done in a function that must specifically be called. Any subsequent calls to GetFrameAudioData() will then return the exact same copy of the audio data. As of now with the exception that new waveform data may be passed in via a separate thread, which will then be returned and might not match the spectrum data. Will fix that in a later commit.
1 parent 6428d16 commit c7db1ca

File tree

19 files changed

+394
-648
lines changed

19 files changed

+394
-648
lines changed

src/libprojectM/Audio/BeatDetect.cpp

Lines changed: 0 additions & 151 deletions
This file was deleted.

src/libprojectM/Audio/BeatDetect.hpp

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/libprojectM/Audio/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

22
add_library(Audio OBJECT
33
AudioConstants.hpp
4-
BeatDetect.cpp
5-
BeatDetect.hpp
64
MilkdropFFT.cpp
75
MilkdropFFT.hpp
86
FrameAudioData.cpp
97
FrameAudioData.hpp
108
PCM.cpp
119
PCM.hpp
10+
Loudness.cpp
11+
Loudness.hpp
1212
)
1313

1414
target_include_directories(Audio

src/libprojectM/Audio/Loudness.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "Loudness.hpp"
2+
3+
#include <cmath>
4+
5+
namespace libprojectM {
6+
namespace Audio {
7+
8+
Loudness::Loudness(Loudness::Band band)
9+
: m_band(band)
10+
{
11+
}
12+
13+
void Loudness::Update(const std::array<float, SpectrumSamples>& spectrumSamples, double secondsSinceLastFrame, uint32_t frame)
14+
{
15+
SumBand(spectrumSamples);
16+
UpdateBandAverage(secondsSinceLastFrame, frame);
17+
}
18+
19+
auto Loudness::CurrentRelative() const -> float
20+
{
21+
return m_currentRelative;
22+
}
23+
24+
auto Loudness::AverageRelative() const -> float
25+
{
26+
return m_averageRelative;
27+
}
28+
29+
void Loudness::SumBand(const std::array<float, SpectrumSamples>& spectrumSamples)
30+
{
31+
int start = SpectrumSamples * static_cast<int>(m_band) / 6;
32+
int end = SpectrumSamples * static_cast<int>(m_band) / 6;
33+
34+
m_current = 0.0f;
35+
for (int sample = start; sample < end; sample++)
36+
{
37+
m_current += spectrumSamples[sample];
38+
}
39+
}
40+
41+
void Loudness::UpdateBandAverage(double secondsSinceLastFrame, uint32_t frame)
42+
{
43+
float rate = AdjustRateToFps(m_current > m_average ? 0.2f : 0.5f, secondsSinceLastFrame);
44+
m_average = m_average * rate + m_current * (1.0f - rate);
45+
46+
rate = AdjustRateToFps(frame < 50 ? 0.9f : 0.992f, secondsSinceLastFrame);
47+
m_longAverage = m_longAverage * rate + m_current * (1.0f - rate);
48+
49+
m_currentRelative = std::fabs(m_longAverage) < 0.001f ? 1.0f : m_current / m_longAverage;
50+
m_averageRelative = std::fabs(m_longAverage) < 0.001f ? 1.0f : m_average / m_longAverage;
51+
}
52+
53+
auto Loudness::AdjustRateToFps(float rate, double secondsSinceLastFrame) -> float
54+
{
55+
float const perSecondDecayRateAtFps1 = std::pow(rate, 30.0f);
56+
float const perFrameDecayRateAtFps2 = std::pow(perSecondDecayRateAtFps1, static_cast<float>(secondsSinceLastFrame));
57+
58+
return perFrameDecayRateAtFps2;
59+
}
60+
61+
} // namespace Audio
62+
} // namespace libprojectM

0 commit comments

Comments
 (0)