1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-20 23:03:03 +02:00

Deduplicate Compressor2 effect code.

Signed-off-by: Max Maisel <max.maisel@posteo.de>
This commit is contained in:
Max Maisel 2020-09-14 13:04:16 +02:00
parent dfffeb76dc
commit 9606aa7312
2 changed files with 21 additions and 18 deletions

@ -1068,9 +1068,7 @@ double EffectCompressor2::CompressorGain(double env)
std::unique_ptr<SamplePreprocessor> EffectCompressor2::InitPreprocessor( std::unique_ptr<SamplePreprocessor> EffectCompressor2::InitPreprocessor(
double rate, bool preview) double rate, bool preview)
{ {
size_t window_size = size_t window_size = CalcWindowLength(rate);
std::max(1, int(round((mLookaheadTime + mLookbehindTime) * rate)));
if(mCompressBy == kAmplitude) if(mCompressBy == kAmplitude)
return std::unique_ptr<SamplePreprocessor>(safenew return std::unique_ptr<SamplePreprocessor>(safenew
SlidingMaxPreprocessor(window_size)); SlidingMaxPreprocessor(window_size));
@ -1091,12 +1089,10 @@ std::unique_ptr<EnvelopeDetector> EffectCompressor2::InitEnvelope(
!preview && mCompressBy != kAmplitude)); !preview && mCompressBy != kAmplitude));
} }
size_t EffectCompressor2::CalcBufferSize(size_t sampleRate) size_t EffectCompressor2::CalcBufferSize(double sampleRate)
{ {
size_t capacity; size_t capacity;
mLookaheadLength = CalcLookaheadLength(sampleRate);
mLookaheadLength =
std::max(0, int(round(mLookaheadTime * sampleRate)));
capacity = mLookaheadLength + capacity = mLookaheadLength +
size_t(float(TAU_FACTOR) * (1.0 + mAttackTime) * sampleRate); size_t(float(TAU_FACTOR) * (1.0 + mAttackTime) * sampleRate);
if(capacity < MIN_BUFFER_CAPACITY) if(capacity < MIN_BUFFER_CAPACITY)
@ -1104,6 +1100,16 @@ size_t EffectCompressor2::CalcBufferSize(size_t sampleRate)
return capacity; return capacity;
} }
size_t EffectCompressor2::CalcLookaheadLength(double rate)
{
return std::max(0, int(round(mLookaheadTime * rate)));
}
size_t EffectCompressor2::CalcWindowLength(double rate)
{
return std::max(1, int(round((mLookaheadTime + mLookbehindTime) * rate)));
}
/// Get required buffer size for the largest whole track and allocate buffers. /// Get required buffer size for the largest whole track and allocate buffers.
/// This reduces the amount of allocations required. /// This reduces the amount of allocations required.
void EffectCompressor2::AllocPipeline() void EffectCompressor2::AllocPipeline()
@ -1133,9 +1139,7 @@ void EffectCompressor2::AllocPipeline()
void EffectCompressor2::AllocRealtimePipeline() void EffectCompressor2::AllocRealtimePipeline()
{ {
mLookaheadLength = mLookaheadLength = CalcLookaheadLength(mSampleRate);
std::max(0, int(round(mLookaheadTime * mSampleRate)));
size_t blockSize = std::max(mLookaheadLength, size_t(512)); size_t blockSize = std::max(mLookaheadLength, size_t(512));
if(mAlgorithm == kExpFit) if(mAlgorithm == kExpFit)
{ {
@ -1606,8 +1610,7 @@ void EffectCompressor2::UpdateResponsePlot()
std::unique_ptr<EnvelopeDetector> envelope; std::unique_ptr<EnvelopeDetector> envelope;
float plot_rate = RESPONSE_PLOT_SAMPLES / RESPONSE_PLOT_TIME; float plot_rate = RESPONSE_PLOT_SAMPLES / RESPONSE_PLOT_TIME;
size_t lookahead_size = size_t lookahead_size = CalcLookaheadLength(plot_rate);
std::max(0, int(round(mLookaheadTime * plot_rate)));
ssize_t block_size = float(TAU_FACTOR) * (mAttackTime + 1.0) * plot_rate; ssize_t block_size = float(TAU_FACTOR) * (mAttackTime + 1.0) * plot_rate;
preproc = InitPreprocessor(plot_rate, true); preproc = InitPreprocessor(plot_rate, true);
@ -1633,11 +1636,8 @@ void EffectCompressor2::UpdateResponsePlot()
void EffectCompressor2::UpdateRealtimeParams() void EffectCompressor2::UpdateRealtimeParams()
{ {
std::lock_guard<std::mutex> guard(mRealtimeMutex); std::lock_guard<std::mutex> guard(mRealtimeMutex);
// TODO: extract it size_t window_size = CalcWindowLength(mSampleRate);
size_t window_size = mLookaheadLength = CalcLookaheadLength(mSampleRate);
std::max(1, int(round((mLookaheadTime + mLookbehindTime) * mSampleRate)));
mLookaheadLength = // TODO: depup this everywhere
std::max(0, int(round(mLookaheadTime * mSampleRate)));
mPreproc->SetWindowSize(window_size); mPreproc->SetWindowSize(window_size);
mEnvelope->SetParams(mSampleRate, mAttackTime, mReleaseTime); mEnvelope->SetParams(mSampleRate, mAttackTime, mReleaseTime);
} }

@ -212,7 +212,10 @@ private:
double rate, bool preview = false); double rate, bool preview = false);
std::unique_ptr<EnvelopeDetector> InitEnvelope( std::unique_ptr<EnvelopeDetector> InitEnvelope(
double rate, size_t blockSize = 0, bool preview = false); double rate, size_t blockSize = 0, bool preview = false);
size_t CalcBufferSize(size_t sampleRate); size_t CalcBufferSize(double sampleRate);
inline size_t CalcLookaheadLength(double rate);
inline size_t CalcWindowLength(double rate);
void AllocPipeline(); void AllocPipeline();
void AllocRealtimePipeline(); void AllocRealtimePipeline();