Skip to content

Commit b5de1d5

Browse files
authored
Fix PeakController attack/decay, use linear interpolation between samples (LMMS#7566)
Historically, the PeakController has had issues like attack/decay knobs acting like on/off switches, and audio artifacts like buzzing or clicking. This patch aims to address those issues. The PeakController previously used lerp (linear interpolation) when looping through the sample buffer, which was in updateValueBuffer. This lerp utilized attack/decay values from control knobs. This is not the correct place to utilize attack/decay because the only temporal data available to the function is the frame and sample size. Therefore the coefficient should simply be the sample rate instead. Between each sample, processImpl would set m_lastSample to the RMS without any sort of lerp. This resulted in m_lastSample producing stair-like patterns over time, rather than a smooth line. For context, m_lastSample is used to set the value of whatever is connected to the PeakController. The basic lerp formula is: m_lastSample = m_lastSample + ((1 - attack) * (RMS - m_lastSample)) This is useful because an attack of 0 sets m_lastSample to RMS, whereas an attack of 1 would set m_lastSample to m_lastSample. This means our attack/decay knobs can be used on a range from "snap to the next value immediately" to "never stray from the last value". * Remove attack/decay from PeakController frame lerp. * Set frame lerp coefficient to 100.0 / sample_rate to fix buzzing. * Add lerp between samples for PeakController to fix stairstep bug. * The newly added lerp utilizes (1 - attack or decay) as the coefficient, which means the knobs actually do something now.
1 parent 1f37c9b commit b5de1d5

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

include/PeakController.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public slots:
7878
static int m_loadCount;
7979
static bool m_buggedFile;
8080

81-
float m_attackCoeff;
82-
float m_decayCoeff;
81+
float m_coeff;
8382
bool m_coeffNeedsUpdate;
8483
} ;
8584

plugins/PeakControllerEffect/PeakControllerEffect.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,18 @@ Effect::ProcessStatus PeakControllerEffect::processImpl(SampleFrame* buf, const
132132
float curRMS = sqrt_neg(sum / frames);
133133
const float tres = c.m_tresholdModel.value();
134134
const float amount = c.m_amountModel.value() * c.m_amountMultModel.value();
135+
const float attack = 1.0f - c.m_attackModel.value();
136+
const float decay = 1.0f - c.m_decayModel.value();
137+
135138
curRMS = qAbs( curRMS ) < tres ? 0.0f : curRMS;
136-
m_lastSample = qBound( 0.0f, c.m_baseModel.value() + amount * curRMS, 1.0f );
139+
float target = c.m_baseModel.value() + amount * curRMS;
140+
// Use decay when the volume is decreasing, attack otherwise.
141+
// Since direction can change as often as every sampleBuffer, it's difficult
142+
// to witness attack/decay working in isolation unless using large buffer sizes.
143+
const float t = target < m_lastSample ? decay : attack;
144+
// Set m_lastSample to the interpolation between itself and target.
145+
// When t is 1.0, m_lastSample snaps to target. When t is 0.0, m_lastSample shouldn't change.
146+
m_lastSample = std::clamp(m_lastSample + t * (target - m_lastSample), 0.0f, 1.0f);
137147

138148
return ProcessStatus::Continue;
139149
}

src/core/PeakController.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ void PeakController::updateValueBuffer()
8080
{
8181
if( m_coeffNeedsUpdate )
8282
{
83-
const float ratio = 44100.0f / Engine::audioEngine()->outputSampleRate();
84-
m_attackCoeff = 1.0f - powf( 2.0f, -0.3f * ( 1.0f - m_peakEffect->attackModel()->value() ) * ratio );
85-
m_decayCoeff = 1.0f - powf( 2.0f, -0.3f * ( 1.0f - m_peakEffect->decayModel()->value() ) * ratio );
83+
m_coeff = 100.0f / Engine::audioEngine()->outputSampleRate();
8684
m_coeffNeedsUpdate = false;
8785
}
8886

@@ -97,14 +95,7 @@ void PeakController::updateValueBuffer()
9795
for( f_cnt_t f = 0; f < frames; ++f )
9896
{
9997
const float diff = ( targetSample - m_currentSample );
100-
if( m_currentSample < targetSample ) // going up...
101-
{
102-
m_currentSample += diff * m_attackCoeff;
103-
}
104-
else if( m_currentSample > targetSample ) // going down
105-
{
106-
m_currentSample += diff * m_decayCoeff;
107-
}
98+
m_currentSample += diff * m_coeff;
10899
values[f] = m_currentSample;
109100
}
110101
}

0 commit comments

Comments
 (0)