AudioIODeviceCallback Silent Audio


#1

I’ve been learning JUCE (with the purpose of audio development) for a few months now and am tying to strengthen my general understanding of how some of the audio classes work together. The documentation has been extremely helpful by the way. That, plus the JUCE demo examples have been so much fun to tear apart and learn from. I’m hooked.

On to my question:

When an AudioDeviceManager streams audio to an AudioIODeviceCallback does it still stream silent or dead audio if no legitimate audio information is being produced?

For example I have a Synthesizer that is sending its audio to an AudioSourcePlayer which is being used as an AudioIODeviceCallback to stream audio to my AudioDeviceManager to output audio. The intention of this callback is only to send audio produced by the synthesizer to the system output. The Synthesizer only outputs sound when MIDI keys are pressed.
I also have a Spectrogram Visualizer for the audio that is an AudioIODeviceCallback. This visualizer has also been added as a callback to the AudioDeviceManager, but with the intention of grabbing the output data from the synthesizer and displaying it.

Within my Spectrogram (a callback) does the overridden function audioDeviceIOCallback() continuously have audio samples that the Synthesizer outputs passed as its parameters, even if no audio is being outputted? Would the audioDeviceIOCallback() have only zeroes passed in its outputChannelData parameter for silent audio?

The reason for this question is because I have to use an FFT to display the audio data on my spectrogram. If I am only receiving silent audio, I feel like I would not want to run my FFT on a large array of just zeroes. That would be unneeded work.
If the audioDeviceIOCallback() does indeed receive even silent audio in its outputChannelData parameter, I think I would want to create some kind of flag that only processed the data through FFT if non-silent audio was incoming.

Or is there something more in depth that I’m missing? Some of the functions (such as “audioDeviceAboutToStart” & “audioDeviceStopped”) within the AudioIODeviceCallback class make me think that I need to make my own custom AudioIODevice that will stop streaming audio if no audio is being produced.

Sorry this is complicated. I’m trying to further my understanding of these classes.
Thanks!


#2

There really is no such thing as “no audio”: as long as audioDeviceAboutToStart has been called, your synthesizer is expected to output audio data until audioDeviceStopped is called. You just need to output zeros if no audio data is being processed.

It’s up to your Spectrogram to deal with this, i.e. it should detect if there are only zeros in the buffer, for example, by calling FloatVectorOperations::findMinAndMax which should be blazing fast.

If you are only using AudioSources and AudioProcessors then you can also use the AudioSampleBuffer's hasBeenCleared method. However, this method may return false even if the buffer is all zeros (but it will never return true if the buffer contains non-zeroes). It effectively, sets a cleared flag everytime you call AudioSampleBuffer's clear method. And unsets the cleared flag if any method could make the buffer dirty.

Some plug-ins (VST3/AU) have a bit more elaborate functions when no audio is coming in/out but, as I understand you, that’s not your use case.