diff --git a/src/processors/other/Compressor.cpp b/src/processors/other/Compressor.cpp index c0686afe..3975b44b 100644 --- a/src/processors/other/Compressor.cpp +++ b/src/processors/other/Compressor.cpp @@ -1,5 +1,6 @@ #include "Compressor.h" #include "../ParameterHelpers.h" +#include "../BufferHelpers.h" class Compressor::GainComputer { @@ -136,36 +137,47 @@ void Compressor::prepare (double sampleRate, int samplesPerBlock) void Compressor::processAudio (AudioBuffer& buffer) { - const auto numChannels = buffer.getNumChannels(); const auto numSamples = buffer.getNumSamples(); - // set up level buffer - if (numChannels == 1) + levelOutBuffer.setSize(1, numSamples, false, false, true); + if (inputsConnected.contains(LevelInput)) { - levelBuffer.makeCopyOf (buffer); - } - else + BufferHelpers::collapseToMonoBuffer(getInputBuffer(LevelInput), levelOutBuffer); + } else { - levelBuffer.setSize (1, numSamples, true, true, false); - levelBuffer.copyFrom (0, 0, buffer, 0, 0, numSamples); - - for (int ch = 1; ch < numChannels; ++ch) - levelBuffer.addFrom (0, 0, buffer, ch, 0, numSamples); - - levelBuffer.applyGain (1.0f / (float) numChannels); + if (inputsConnected.contains(AudioInput)) + { + levelDetector.setParameters(*attackMsParam, *releaseMsParam); + levelDetector.processBlock(getInputBuffer(AudioInput), levelOutBuffer); + } else + { + levelOutBuffer.clear(); + } } - auto&& levelBlock = dsp::AudioBlock { levelBuffer }; - levelDetector.setParameters (*attackMsParam, *releaseMsParam); - levelDetector.process (dsp::ProcessContextReplacing { levelBlock }); - - gainComputer->setParameters (*threshDBParam, *ratioParam, *kneeDBParam); - gainComputer->process (levelBlock); - - auto&& block = dsp::AudioBlock { buffer }; - for (int ch = 0; ch < numChannels; ++ch) - block.getSingleChannelBlock ((size_t) ch) *= gainComputer->gainBlock; + if (inputsConnected.contains (AudioInput)) + { + levelBuffer.makeCopyOf(levelOutBuffer); + gainComputer->setParameters (*threshDBParam, *ratioParam, *kneeDBParam); + gainComputer->process (levelBuffer); + + auto& audioInBuffer = getInputBuffer (AudioInput); + const auto numChannels = audioInBuffer.getNumChannels(); + audioOutBuffer.setSize (numChannels, numSamples, false, false, true); + for (int ch = 0; ch < numChannels; ++ch) + { + FloatVectorOperations::multiply(audioOutBuffer.getWritePointer(ch), audioInBuffer.getReadPointer(ch), gainComputer->gainBlock.getChannelPointer(0), numSamples); + } + + auto&& audioOutBlock = dsp::AudioBlock { audioOutBuffer }; + makeupGain.setGainDecibels (*makeupDBParam); + makeupGain.process (dsp::ProcessContextReplacing { audioOutBlock }); + }else + { + audioOutBuffer.setSize (1, numSamples, false, false, true); + audioOutBuffer.clear(); + } - makeupGain.setGainDecibels (*makeupDBParam); - makeupGain.process (dsp::ProcessContextReplacing { block }); + outputBuffers.getReference (AudioOutput) = &audioOutBuffer; + outputBuffers.getReference (LevelOutput) = &levelOutBuffer; } diff --git a/src/processors/other/Compressor.h b/src/processors/other/Compressor.h index 55137f29..09e2f8f2 100644 --- a/src/processors/other/Compressor.h +++ b/src/processors/other/Compressor.h @@ -23,6 +23,8 @@ class Compressor : public BaseProcessor chowdsp::FloatParameter* makeupDBParam = nullptr; AudioBuffer levelBuffer; + AudioBuffer levelOutBuffer; + AudioBuffer audioOutBuffer; chowdsp::LevelDetector levelDetector; class GainComputer;