The Red Penguin

Use a MixerAudioSource to play more than one file at a time


Monday 3 August 2020

1. Go to MainComponent.h and in private add:
juce::MixerAudioSource mixerSource;

2. We need to hook this into the Audio Life Cycle now. In MainComponent.cpp in MainComponent::prepareToPlay we add:
mixerSource.prepareToPlay(samplesPerBlockExpected, sampleRate);

3. We also need to set up player 2 in prepareToPlay because we haven't done that yet. So add
player2.prepareToPlay(samplesPerBlockExpected, sampleRate);

4. Then we need to add this in the same function:
mixerSource.addInputSource(&player1, false);
mixerSource.addInputSource(&player2, false);

5. In MainComponent::getNextAudioBlock we replace player1.getNextAudioBlock(bufferToFill); with
mixerSource.getNextAudioBlock(bufferToFill);
We do this because we are delegating the responsibilty of dealing with player1 and player2 to mixerSource.

6. In MainComponent::releaseResources we put:
player1.releaseResources();
player2.releaseResources();
mixerSource.releaseResources();
although the worksheet said
    mixerSource.removeAllInputs();
mixerSource.releaseResources();
player1.releaseResources();
player2.releaseResources();

All seems to be working after another compile and build.