I am learning JUCE. Currently, I have a simple AudioSource and AudioTransportSource instance that just plays a local audio file.
How could I go about reading the volume to show a volume level drawing to the user?
Where you call getNextAudioBlock() you can add a getRMSLevel() or getMagnitude() on the buffer and store the result into an std::atomic<float>
.
In the GUI you can then have a juce::Slider (juce::Slider::LinearBarVertical, juce::Slider::NoTextBox)
and update it from a timer:
void getNextAudioBlock (juce::AudioSourceChannelInfo& bufferToFill) override
{
transport.getNextAudioBlock (bufferToFill);
auto mag = bufferToFill.buffer->getMagnitude (bufferToFill.startSample, bufferToFill.numSamples);
magnitude.store (mag);
}
void timerCallback() override
{
slider.setValue (magnitude.load());
}
// members:
std::atomic<float> magnitude { 0.0f };
juce::Slider bar { juce::SLider::LinearBarVertical, juce::Slider::NoTextBox };
juce::AudioTransportSource transport;
Improvments: use the juce::Decibels to set the value in dB rather than gain into the atomic.
Hope that helps