forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaveformMath.cpp
More file actions
146 lines (120 loc) · 5.28 KB
/
WaveformMath.cpp
File metadata and controls
146 lines (120 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "Waveforms/WaveformMath.hpp"
#include "PerFrameContext.hpp"
#include "PresetState.hpp"
#include <algorithm>
#include <cmath>
namespace libprojectM {
namespace MilkdropPreset {
namespace Waveforms {
auto WaveformMath::GetVertices(const PresetState& presetState,
const PerFrameContext& presetPerFrameContext) -> std::array<VertexList, 2>
{
static_assert(WaveformMaxPoints >= libprojectM::Audio::SpectrumSamples, "WaveformMaxPoints is smaller than SpectrumSamples");
static_assert(WaveformMaxPoints >= libprojectM::Audio::WaveformSamples, "WaveformMaxPoints is smaller than WaveformSamples");
using libprojectM::Audio::WaveformSamples;
// Get the correct audio sample type for the current waveform mode.
if (IsSpectrumWave())
{
std::copy(begin(presetState.audioData.spectrumLeft),
begin(presetState.audioData.spectrumLeft) + Audio::SpectrumSamples,
begin(m_pcmDataL));
std::copy(begin(presetState.audioData.spectrumRight),
begin(presetState.audioData.spectrumRight) + Audio::SpectrumSamples,
begin(m_pcmDataR));
}
else
{
std::copy(begin(presetState.audioData.waveformLeft),
begin(presetState.audioData.waveformLeft) + Audio::WaveformSamples,
begin(m_pcmDataL));
std::copy(begin(presetState.audioData.waveformRight),
begin(presetState.audioData.waveformRight) + Audio::WaveformSamples,
begin(m_pcmDataR));
}
// Scale and smooth waveform data
float scale = presetState.waveScale / 128.0f;
// The first sample gets scaled directly because it is not mixed with other samples.
m_pcmDataL[0] *= scale;
m_pcmDataR[0] *= scale;
/* If s[i] is output sample i and p[i] is input sample i, then:
* s[0] = waveScale*p[i];
* s[i] = waveScale*(1-waveSmoothing)*p[i] + waveSmoothing*s[i-1]
* Note that this is an IIR filter with alpha = waveSmoothing.
*/
float mix2 = presetState.waveSmoothing; // amount of previous sample to add to this sample
float mix1 = scale * (1.0f - mix2); // amount to scale this sample
// Scale and mix samples after the first one.
for (size_t i = 1; i < m_pcmDataL.size(); ++i)
{
m_pcmDataL[i] = m_pcmDataL[i] * mix1 + m_pcmDataL[i - 1] * mix2;
m_pcmDataR[i] = m_pcmDataR[i] * mix1 + m_pcmDataR[i - 1] * mix2;
}
// Aspect multipliers
if (presetState.renderContext.viewportSizeX > presetState.renderContext.viewportSizeY)
{
m_aspectY = static_cast<float>(presetState.renderContext.viewportSizeY) / static_cast<float>(presetState.renderContext.viewportSizeX);
}
else
{
m_aspectX = static_cast<float>(presetState.renderContext.viewportSizeX) / static_cast<float>(presetState.renderContext.viewportSizeY);
}
m_mysteryWaveParam = static_cast<float>(*presetPerFrameContext.wave_mystery);
if (UsesNormalizedMysteryParam() && (m_mysteryWaveParam < -1.0f || m_mysteryWaveParam > 1.0f))
{
m_mysteryWaveParam = m_mysteryWaveParam * 0.5f + 0.5f;
m_mysteryWaveParam -= std::floor(m_mysteryWaveParam);
m_mysteryWaveParam = std::fabs(m_mysteryWaveParam);
m_mysteryWaveParam = m_mysteryWaveParam * 2 - 1;
}
m_waveX = 2.0f * static_cast<float>(*presetPerFrameContext.wave_x) - 1.0f;
m_waveY = 2.0f * static_cast<float>(*presetPerFrameContext.wave_y) - 1.0f;
GenerateVertices(presetState, presetPerFrameContext);
std::array<VertexList, 2> smoothedVertices;
SmoothWave(m_wave1Vertices, smoothedVertices.at(0));
SmoothWave(m_wave2Vertices, smoothedVertices.at(1));
return smoothedVertices;
}
auto WaveformMath::IsLoop() -> bool
{
return false;
}
auto WaveformMath::IsSpectrumWave() -> bool
{
return false;
}
auto WaveformMath::UsesNormalizedMysteryParam() -> bool
{
return false;
}
void WaveformMath::SmoothWave(const VertexList& inputVertices, VertexList& outputVertices)
{
constexpr float c1{-0.15f};
constexpr float c2{1.15f};
constexpr float c3{1.15f};
constexpr float c4{-0.15f};
constexpr float inverseSum{1.0f / (c1 + c2 + c3 + c4)};
size_t outputIndex = 0;
size_t indexBelow = 0;
size_t indexAbove2 = 1;
if (inputVertices.empty())
{
outputVertices.clear();
return;
}
outputVertices.resize(static_cast<size_t>(m_samples) * 2 - 1);
for (size_t inputIndex = 0; inputIndex < static_cast<size_t>(m_samples) - 1; inputIndex++)
{
size_t const indexAbove = indexAbove2;
indexAbove2 = std::min(static_cast<size_t>(m_samples) - 1, inputIndex + 2);
outputVertices[outputIndex] = inputVertices[inputIndex];
outputVertices[outputIndex + 1] = {
(c1 * inputVertices[indexBelow].X() + c2 * inputVertices[inputIndex].X() + c3 * inputVertices[indexAbove].X() + c4 * inputVertices[indexAbove2].X()) * inverseSum,
(c1 * inputVertices[indexBelow].Y() + c2 * inputVertices[inputIndex].Y() + c3 * inputVertices[indexAbove].Y() + c4 * inputVertices[indexAbove2].Y()) * inverseSum};
indexBelow = inputIndex;
outputIndex += 2;
}
outputVertices.at(outputIndex) = inputVertices.at(m_samples - 1);
}
} // namespace Waveforms
} // namespace MilkdropPreset
} // namespace libprojectM