When prepareToPlay() is called, and then also AudioAppComponent::getNextAudioBlock(), what would be passed to it as bufferToFill? What’s different from AudioTransportSource::getNextAudioBlock?

How do Juce know when getNextAudioBlock is needed?

The operating system calls the Juce code when the audio is playing, that code in turn calls AudioAppComponent::getNextAudioBlock. AudioAppComponent is a special AudioSource that works that way. For other AudioSources you need to make the getNextAudioBlock calls happen in some other way, such as call them from the AudioAppComponent::getNextAudioBlock or by using an AudioSourcePlayer with the AudioDeviceManager.

“The prepareToPlay() method is guaranteed to be called at least once on an ‘unprepared’ source to put it into a ‘prepared’ state before any calls will be made to getNextAudioBlock(). ”
In this tutorial, there’s no any codes in this virtual function, so that means some codes elsewhere make an unprepared source prepared?

There is no AudioSources or anything else to prepare with prepareToPlay in that tutorial code. The “preparation” that is done, is done in the openButtonClicked method by filling the fileBuffer with the needed audio. (AudioBuffer is not an AudioSource subclass.)

If the code was, for example, playing directly from a file using AudioTransportSource and AudioFormatReaderSource, then the AudioAppComponent::prepareToPlay method implementation would need to prepare those.

Where in this tutorial plays the audio?

The AudioAppComponent::getNextAudioBlock implementation generates the output audio by copying from the fileBuffer.

To make it more clear, the call sequence is as follows :

setAudioChannels(ins,outs) // starts the audio playback (IMHO, a badly named method in AudioAppComponent)
prepareToPlay() * // just once when playback is started, the tutorial in question here doesn't need to do anything there, though
getNextAudioBlock() *
getNextAudioBlock() *
getNextAudioBlock() *
... // repeat periodically until
shutDownAudio() // stops the playback
releaseResources()  * // implementation can clean up resources here

// the methods marked with * are called automatically by Juce/the operating system when needed.
1 Like

Thanks, all seem clear now.