How to simply lower the volume on an AudioProcessor?

I’m using juce::dsp::Gain<float>

and on processBlock:

void HelloSamplerAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
    juce::dsp::AudioBlock<float> block (buffer);
    juce::dsp::ProcessContextReplacing<float> context (block);
    //play the samples here
    sampler.renderNextBlock (buffer, midiMessages, 0, buffer.getNumSamples());
    std::unique_lock<std::recursive_mutex> lk2{gainMutex};
    gain.process(context);
}

but then the sound is mute.

I tried setting values from -100 to 100 like this:

gain.setGainDecibels(juce::Decibels::decibelsToGain(value));

or

gain.setGainDecibels(value);

or

gain.setGainLinear(value)

but the sound is mute for every value and every combination of this.

How do I simply lower the volume? I just want a controller from 0 to 100 on volume

I think you forgot to initialise the processor in prepareToPlay using prepare(ProcessSpec&).

Also just curious, why the lock? I don’t think that should be there at all.

gain.setGainDecibels (value);  // value = -100..0

Should be the correct value.

I just added it to prepareToPlay:

void HelloSamplerAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
    sampler.setCurrentPlaybackSampleRate(sampleRate);
    juce::dsp::ProcessSpec spec { sampleRate, static_cast<juce::uint32> (samplesPerBlock), 2 };
    reverb.prepare(spec);
    gain.prepare(spec);
}

and the problem persists.

I also tried

gain.process(context);

and

buffer.applyGain(gain.getGainLinear());

but it always gets silent

But you get sound if you leave the gain processing out at all?
Just to rule out the obvious…

1 Like

yes. That’s the strange thing.

Try not to use the value from the gain processor, but use a factor instead just to test. Something is weird here…

even with std::atomic<float> gainValue like this:

buffer.applyGain(gainValue.load());

I still get nothing. I have a slider that goes from 0 to 100 and nothing happens