I’m working on a simple plugin to add random noise to a signal but I’ve ran into a problem while adding filters to the signal. Adding this plugin to a project instantly makes it use 70% cpu even if the plugin isn’t getting any sound in input.
I’m generating a noise signal and after that I’m doing some processing to before storing it into a buffer and applying 2 filters to it like this:
void PluginTest1AudioProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages)
{
ScopedNoDenormals noDenormals;
auto totalNumInputChannels = getTotalNumInputChannels();
auto totalNumOutputChannels = getTotalNumOutputChannels();
//Temporary buffer for DSP processing.
juce::AudioBuffer<float> tmpBuffer = *new juce::AudioBuffer<float>(buffer.getNumChannels() ,buffer.getNumSamples());
//resault of randomization
double randRes = rand.nextDouble();
// counter to check if recalculation is nessacary.
double factorCount = 0;
// 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());
// This is the place where you'd normally do the guts of your plugin's
// audio processing...
// Make sure to reset the state if your inner loop is processing
// the samples and the outer loop is handling the channels.
// Alternatively, you can process the samples with the channels
// interleaved by keeping the same state.
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
//get channel data
auto* channelData = buffer.getWritePointer (channel);
// loop through each sample
for (int sample = 0; sample < buffer.getNumSamples(); sample++)
{
// validate if recalculation is needed
if (factorCount == (int)*treeState.getRawParameterValue(RRF_ID))
{
// get a value for the noise;
randRes = rand.nextDouble();
// set counter back to 0;
factorCount = 0;
}
// apply 0 gating
if (buffer.getSample(channel, sample) == 0.0f && zeroGate)
//the change in amplitude is 0.
staticChange = 0;
else
// add the noise as static
staticChange = ((randRes * 2.0f) - 1.0f) * *treeState.getRawParameterValue(AMT_ID);
//check if dynamicFollow mode is activated
if (dynamicFollow)
// multiply static change by the sample value * DFF
dynamicChange = staticChange + *treeState.getRawParameterValue(DFF_ID) * (staticChange * buffer.getSample(channel, sample) - staticChange);
else
// dynamicchange is equal to the static change value.
dynamicChange = staticChange;
// Add noise sample to the audio buffer
tmpBuffer.addSample(channel, sample, dynamicChange);
//TMP Disabled
//// apply the change value to the sample
//channelData[sample] = buffer.getSample(channel, sample) + dynamicChange;
// increment factor counter
factorCount++;
}
}
//Do DSP thing
//instantiate an audio block
dsp::AudioBlock<float> block(tmpBuffer);
//process lowpass filter
updateLowPass();
lowPass.process(dsp::ProcessContextReplacing<float>(block));
//process highpass filter
updateHighPass();
highPass.process(dsp::ProcessContextReplacing<float>(block));
//Add input to the tmpBuffer.
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
for (int sample = 0; sample < buffer.getNumSamples(); sample++)
{
buffer.setSample(channel,sample , buffer.getSample(channel, sample) + tmpBuffer.getSample(channel, sample));
}
}
//Set output buffer equal to tmpBuffer.
}
void PluginTest1AudioProcessor::updateLowPass()
{
*lowPass.state = *dsp::IIR::Coefficients<float>::makeLowPass(44100,*treeState.getRawParameterValue(LPC_ID), *treeState.getRawParameterValue(LPR_ID));
}
void PluginTest1AudioProcessor::updateHighPass()
{
*highPass.state = *dsp::IIR::Coefficients<float>::makeHighPass(44100, *treeState.getRawParameterValue(HPC_ID), *treeState.getRawParameterValue(HPR_ID));
}
I’m hoping you guys could help me with resolving this problem.
Here are the full files I use (document with links as I can only post 2 links as of now… ):
https://pastebin.com/YT3HSSyJ
What am I doing wrong and how can I improve my code to be more efficient?
thanks