Skip to content

Commit

Permalink
GainControla
Browse files Browse the repository at this point in the history
  • Loading branch information
kuncgregor committed Jan 30, 2022
1 parent 7c70d9d commit 45d17e4
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 42 deletions.
156 changes: 124 additions & 32 deletions Source/PluginProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,20 @@ Multiband_compAudioProcessor::Multiband_compAudioProcessor()
boolHelper(midBandComp.bypassed, Names::Bypassed_Mid_Band);
boolHelper(highBandComp.bypassed, Names::Bypassed_High_Band);

boolHelper(lowBandComp.mute, Names::Mute_Low_Band);
boolHelper(midBandComp.mute, Names::Mute_Mid_Band);
boolHelper(highBandComp.mute, Names::Mute_High_Band);

boolHelper(lowBandComp.solo, Names::Solo_Low_Band);
boolHelper(midBandComp.solo, Names::Solo_Mid_Band);
boolHelper(highBandComp.solo, Names::Solo_High_Band);

floatHelper(lowMidCrossover, Names::Low_Mid_Crossover_Freq);
floatHelper(midHighCrossover, Names::Mid_High_Crossover_Freq);

floatHelper(inputGainParam, Names::Gain_In);
floatHelper(outputGainParam, Names::Gain_Out);

LP1.setType(juce::dsp::LinkwitzRileyFilterType::lowpass);
HP1.setType(juce::dsp::LinkwitzRileyFilterType::highpass);

Expand Down Expand Up @@ -176,6 +187,11 @@ void Multiband_compAudioProcessor::prepareToPlay (double sampleRate, int samples

//invAPBuffer.setSize(spec.numChannels, samplesPerBlock);

inputGain.prepare(spec);
outputGain.prepare(spec);

inputGain.setRampDurationSeconds(0.05); //50ms
outputGain.setRampDurationSeconds(0.05);

for (auto& buffer : filterBuffers) {
buffer.setSize(spec.numChannels, samplesPerBlock);
Expand Down Expand Up @@ -214,41 +230,30 @@ bool Multiband_compAudioProcessor::isBusesLayoutSupported (const BusesLayout& la
}
#endif

void Multiband_compAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
juce::ScopedNoDenormals noDenormals;
auto totalNumInputChannels = getTotalNumInputChannels();
auto totalNumOutputChannels = getTotalNumOutputChannels();

// In case we have more outputs than inputs, this code clears any output
// channels that didn't contain input data, (because these aren't
// guaranteed to be empty - they may contain garbage).
// This is here to avoid people getting screaming feedback
// when they first compile a plugin, but obviously you don't need to keep
// this code if your algorithm always overwrites all the output channels.
for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear(i, 0, buffer.getNumSamples());

for( auto& cmp : compressor)
void Multiband_compAudioProcessor::updateState() {
for (auto& cmp : compressor)
cmp.updateCompressorSettings();
//compressor.process(buffer);

for (auto& fb : filterBuffers) {
fb = buffer;
}

//invAPBuffer = buffer;


auto lowMidCutoffFreq = lowMidCrossover->get();
LP1.setCutoffFrequency(lowMidCutoffFreq);
HP1.setCutoffFrequency(lowMidCutoffFreq);
//invAP1.setCutoffFrequency(lowMidCutoffFreq);


auto midHighCutoffFreq = midHighCrossover->get();
AP2.setCutoffFrequency(midHighCutoffFreq);
LP2.setCutoffFrequency(midHighCutoffFreq);
HP2.setCutoffFrequency(midHighCutoffFreq);
//invAP2.setCutoffFrequency(midHighCutoffFreq);


inputGain.setGainDecibels(inputGainParam->get());
outputGain.setGainDecibels(outputGainParam->get());
}

void Multiband_compAudioProcessor::splitBands(const juce::AudioBuffer<float>& inputBuffer) {

for (auto& fb : filterBuffers) {
fb = inputBuffer;
}

auto fb0Block = juce::dsp::AudioBlock<float>(filterBuffers[0]);
auto fb1Block = juce::dsp::AudioBlock<float>(filterBuffers[1]);
Expand All @@ -267,6 +272,32 @@ void Multiband_compAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer

HP2.process(fb2Ctx);

}

void Multiband_compAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
juce::ScopedNoDenormals noDenormals;
auto totalNumInputChannels = getTotalNumInputChannels();
auto totalNumOutputChannels = getTotalNumOutputChannels();

// In case we have more outputs than inputs, this code clears any output
// channels that didn't contain input data, (because these aren't
// guaranteed to be empty - they may contain garbage).
// This is here to avoid people getting screaming feedback
// when they first compile a plugin, but obviously you don't need to keep
// this code if your algorithm always overwrites all the output channels.
for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear(i, 0, buffer.getNumSamples());


updateState();

applyGain(buffer, inputGain);


splitBands(buffer);


for (size_t i = 0; i < filterBuffers.size(); ++i) {
compressor[i].process(filterBuffers[i]);
}
Expand All @@ -284,10 +315,32 @@ void Multiband_compAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer
}
};

addFilterBand(buffer, filterBuffers[0]);
addFilterBand(buffer, filterBuffers[1]);
addFilterBand(buffer, filterBuffers[2]);
auto bandsAreSoloed = false;
for (auto& comp : compressor) {
if (comp.solo->get()) {
bandsAreSoloed = true;
break;
}
}


if (bandsAreSoloed) {
for (rsize_t i = 0; i < compressor.size(); ++i) {
auto& comp = compressor[i];
if (comp.solo->get()) {
addFilterBand(buffer, filterBuffers[i]);
}
}
}
else {
for (rsize_t i = 0; i < compressor.size(); ++i) {
auto& comp = compressor[i];
if(!comp.mute->get())
addFilterBand(buffer, filterBuffers[i]);
}
}

applyGain(buffer, outputGain);

}

Expand Down Expand Up @@ -331,20 +384,33 @@ juce::AudioProcessorValueTreeState::ParameterLayout Multiband_compAudioProcessor
using namespace Params;
const auto& params = GetParams();

auto gainRange = NormalisableRange<float>(-24.f, 24.f, 0.5f, 1.f);
layout.add(std::make_unique<AudioParameterFloat>(
params.at(Names::Gain_In),
params.at(Names::Gain_In),
gainRange,
0));
layout.add(std::make_unique<AudioParameterFloat>(
params.at(Names::Gain_Out),
params.at(Names::Gain_Out),
gainRange,
0));

auto thresholdRange = NormalisableRange<float>(-60, 12, 1, 1);
layout.add(std::make_unique<AudioParameterFloat>(
params.at(Names::Threshold_Low_Band),
params.at(Names::Threshold_Low_Band),
NormalisableRange<float>(-60, 12, 1, 1),
thresholdRange,
0));
layout.add(std::make_unique<AudioParameterFloat>(
params.at(Names::Threshold_Mid_Band),
params.at(Names::Threshold_Mid_Band),
NormalisableRange<float>(-60, 12, 1, 1),
thresholdRange,
0));
layout.add(std::make_unique<AudioParameterFloat>(
params.at(Names::Threshold_High_Band),
params.at(Names::Threshold_High_Band),
NormalisableRange<float>(-60, 12, 1, 1),
thresholdRange,
0));

auto attackReleseRange = NormalisableRange<float>(5, 500, 1, 1);
Expand Down Expand Up @@ -419,6 +485,32 @@ juce::AudioProcessorValueTreeState::ParameterLayout Multiband_compAudioProcessor
params.at(Names::Bypassed_High_Band),
false));

layout.add(std::make_unique<AudioParameterBool>(
params.at(Names::Mute_Low_Band),
params.at(Names::Mute_Low_Band),
false));
layout.add(std::make_unique<AudioParameterBool>(
params.at(Names::Mute_Mid_Band),
params.at(Names::Mute_Mid_Band),
false));
layout.add(std::make_unique<AudioParameterBool>(
params.at(Names::Mute_High_Band),
params.at(Names::Mute_High_Band),
false));

layout.add(std::make_unique<AudioParameterBool>(
params.at(Names::Solo_Low_Band),
params.at(Names::Solo_Low_Band),
false));
layout.add(std::make_unique<AudioParameterBool>(
params.at(Names::Solo_Mid_Band),
params.at(Names::Solo_Mid_Band),
false));
layout.add(std::make_unique<AudioParameterBool>(
params.at(Names::Solo_High_Band),
params.at(Names::Solo_High_Band),
false));



layout.add(std::make_unique <AudioParameterFloat>(
Expand Down
38 changes: 38 additions & 0 deletions Source/PluginProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ namespace Params {
Bypassed_Low_Band,
Bypassed_Mid_Band,
Bypassed_High_Band,

Mute_Low_Band,
Mute_Mid_Band,
Mute_High_Band,

Solo_Low_Band,
Solo_Mid_Band,
Solo_High_Band,

Gain_In,
Gain_Out,
};
inline const std::map<Names, juce::String>& GetParams() {

Expand Down Expand Up @@ -66,6 +77,17 @@ namespace Params {
{Bypassed_Mid_Band, "Bypassed Mid Band"},
{Bypassed_High_Band, "Bypassed High Band"},

{Mute_Low_Band, "Mute Low Band"},
{Mute_Mid_Band, "Mute Mid Band"},
{Mute_High_Band, "Mute High Band"},

{Solo_Low_Band, "Solo Low Band"},
{Solo_Mid_Band, "Solo Mid Band"},
{Solo_High_Band, "Solo High Band"},

{Gain_In, "Gain In"},
{Gain_Out, "Gain Out"},




Expand All @@ -82,6 +104,9 @@ struct CompressorBand {
juce::AudioParameterFloat* threshold{ nullptr };
juce::AudioParameterChoice* ratio{ nullptr };
juce::AudioParameterBool* bypassed{ nullptr };
juce::AudioParameterBool* mute{ nullptr };
juce::AudioParameterBool* solo{ nullptr };


void prepare(const juce::dsp::ProcessSpec& spec) {
compressor.prepare(spec);
Expand Down Expand Up @@ -181,6 +206,19 @@ class Multiband_compAudioProcessor : public juce::AudioProcessor

std::array<juce::AudioBuffer<float>, 3> filterBuffers;

juce::dsp::Gain<float> inputGain, outputGain;
juce::AudioParameterFloat* inputGainParam { nullptr };
juce::AudioParameterFloat* outputGainParam { nullptr };

template<typename T, typename U>
void applyGain(T& buffer, U& gain) {
auto block = juce::dsp::AudioBlock<float>(buffer);
auto ctx = juce::dsp::ProcessContextReplacing<float>(block);
gain.process(ctx);
}

void updateState();
void splitBands(const juce::AudioBuffer<float>& inputBuffer);
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Multiband_compAudioProcessor)
};
Loading

0 comments on commit 45d17e4

Please sign in to comment.