Volume control in audio player

how can i control volume with slider in the audio player given in this tutorial JUCE: Tutorial: Build an audio player

welcome. easiest way would be to make a juce::Slider, set its range to [0, 1] and multiplicate it with the signal

1 Like

thanks, what will be the signal in this JUCE: Tutorial: Build an audio player or how can i get that signal and how to multiplicate that

I am new to all of this

This tutorial will help:

https://docs.juce.com/master/tutorial_synth_level_control.html

1 Like

yeah i gone through this tutorial but in this tutorial we are generating audio output in getNextAudioBlock( )
my problem is that i want to control the volume of audio file which we have loaded

You don’t want to change the volume of the actual audio file.
To change volume of an audio player with a slider, you just want to change the volume of each block of audio.

So i guess you’re still not figuring out “how can i get that signal and how to multiplicate that”.

In the audio player tutorial, the getNextAudioBlock function is:

    void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override
{
    if (readerSource.get() == nullptr)
    {
        bufferToFill.clearActiveBufferRegion();
        return;
    }

    transportSource.getNextAudioBlock (bufferToFill);
}

So, by the end of that function, bufferToFill is filled with the data from your audio file to play back for that block.

The getNextAudioBlock function in the synth level control tutorial shows how to multiply a buffer by a value:

        auto level = (float) levelSlider.getValue();

    for (auto channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
    {
        auto* buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample);

        for (auto sample = 0; sample < bufferToFill.numSamples; ++sample)
        {
            auto noise = random.nextFloat() * 2.0f - 1.0f;
            buffer[sample] = noise * level;
        }
    }

But of course you don’t want the noise, you want the audio file playback. So after adding the volume slider to your audio file playback project, you can then combine the two ideas together like this:

    void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override
{
    if (readerSource.get() == nullptr)
    {
        bufferToFill.clearActiveBufferRegion();
        return;
    }

    transportSource.getNextAudioBlock (bufferToFill);

    auto level = (float) levelSlider.getValue();

    for (auto channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
    {
        auto* buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample);

        for (auto sample = 0; sample < bufferToFill.numSamples; ++sample)
        {
            buffer[sample]  = buffer[sample] * level;
        }
    }
}

There are lots more things that you need to take care of for a professional application, such as scaling the volume slider to a natural decibel range, smoothing the slider value so there are no sudden jumps…etc, but hopefully that helps get you started?

2 Likes

yeah thats what i wanted thanks a lot