Fix Equalizer::ComputeCoefficients pointer arithmetic bug
The band index was incorrectly calculated using pointer arithmetic on a local parameter address, which is meaningless. Now uses the band index passed as a parameter instead.
This commit is contained in:
@@ -33,7 +33,7 @@ public:
|
||||
|
||||
private:
|
||||
void ProcessBand(float* buffer, uint32 sampleCount, uint32 channel, uint32 band);
|
||||
void ComputeCoefficients(float frequency, float q, float gainDb);
|
||||
void ComputeCoefficients(uint32 band, float frequency, float q, float gainDb);
|
||||
|
||||
private:
|
||||
uint32 m_bandCount = 4;
|
||||
|
||||
@@ -18,7 +18,7 @@ Equalizer::Equalizer()
|
||||
for (uint32 i = 0; i < m_bandCount; ++i) {
|
||||
m_gains[i] = 0.0f;
|
||||
m_qs[i] = 1.0f;
|
||||
ComputeCoefficients(m_frequencies[i], m_qs[i], m_gains[i]);
|
||||
ComputeCoefficients(i, m_frequencies[i], m_qs[i], m_gains[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ void Equalizer::SetBandFrequency(uint32 band, float frequency) {
|
||||
}
|
||||
|
||||
m_frequencies[band] = std::max(20.0f, std::min(frequency, 20000.0f));
|
||||
ComputeCoefficients(m_frequencies[band], m_qs[band], m_gains[band]);
|
||||
ComputeCoefficients(band, m_frequencies[band], m_qs[band], m_gains[band]);
|
||||
}
|
||||
|
||||
float Equalizer::GetBandFrequency(uint32 band) const {
|
||||
@@ -102,7 +102,7 @@ void Equalizer::SetBandGain(uint32 band, float gainDb) {
|
||||
}
|
||||
|
||||
m_gains[band] = std::max(-24.0f, std::min(gainDb, 24.0f));
|
||||
ComputeCoefficients(m_frequencies[band], m_qs[band], m_gains[band]);
|
||||
ComputeCoefficients(band, m_frequencies[band], m_qs[band], m_gains[band]);
|
||||
}
|
||||
|
||||
float Equalizer::GetBandGain(uint32 band) const {
|
||||
@@ -118,7 +118,7 @@ void Equalizer::SetBandQ(uint32 band, float q) {
|
||||
}
|
||||
|
||||
m_qs[band] = std::max(0.1f, std::min(q, 10.0f));
|
||||
ComputeCoefficients(m_frequencies[band], m_qs[band], m_gains[band]);
|
||||
ComputeCoefficients(band, m_frequencies[band], m_qs[band], m_gains[band]);
|
||||
}
|
||||
|
||||
float Equalizer::GetBandQ(uint32 band) const {
|
||||
@@ -136,7 +136,11 @@ void Equalizer::SetWetMix(float wetMix) {
|
||||
m_wetMix = std::max(0.0f, std::min(1.0f, wetMix));
|
||||
}
|
||||
|
||||
void Equalizer::ComputeCoefficients(float frequency, float q, float gainDb) {
|
||||
void Equalizer::ComputeCoefficients(uint32 band, float frequency, float q, float gainDb) {
|
||||
if (band >= m_bandCount) {
|
||||
return;
|
||||
}
|
||||
|
||||
float A = std::pow(10.0f, gainDb / 40.0f);
|
||||
float w0 = 2.0f * 3.14159265f * frequency / m_sampleRate;
|
||||
float cosw0 = std::cos(w0);
|
||||
@@ -150,15 +154,11 @@ void Equalizer::ComputeCoefficients(float frequency, float q, float gainDb) {
|
||||
float a1 = -2.0f * cosw0;
|
||||
float a2 = 1.0f - alpha / A;
|
||||
|
||||
size_t bandIndex = &frequency - &m_frequencies[0];
|
||||
|
||||
if (bandIndex < m_bandCount) {
|
||||
m_a0[bandIndex] = b0 / a0;
|
||||
m_a1[bandIndex] = b1 / a0;
|
||||
m_a2[bandIndex] = b2 / a0;
|
||||
m_b1[bandIndex] = a1 / a0;
|
||||
m_b2[bandIndex] = a2 / a0;
|
||||
}
|
||||
m_a0[band] = b0 / a0;
|
||||
m_a1[band] = b1 / a0;
|
||||
m_a2[band] = b2 / a0;
|
||||
m_b1[band] = a1 / a0;
|
||||
m_b2[band] = a2 / a0;
|
||||
}
|
||||
|
||||
} // namespace Audio
|
||||
|
||||
Reference in New Issue
Block a user