Made a game and are trying to add multiple sound effects, all from binary resource wav files. These multiple sound effects may or may not be playing while another is playing, and sometimes while one specific is playing, it may be instructed to play again (restarted), and optimally if I restart again, any earlier same sound that has not finished should continue playing until end also.
I figure that I eventually should instead be using a polyphonic synthesizer, but for now until I learned a lot more, I am trying to keep it as simple as possible.
Here is what I got. In my MainCompent.h file;
class MainComponent : public AudioAppComponent,
public Timer,
public ChangeListener,
public MixerAudioSource
{
public:
//==============================================================================
MainComponent();
~MainComponent();
//==============================================================================
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override;
void getNextAudioBlock(const AudioSourceChannelInfo& bufferToFill) override;
void releaseResources() override;
void changeListenerCallback(ChangeBroadcaster* source) override;
AudioFormatManager formatManager;
std::unique_ptr<AudioFormatReaderSource> readerSource;
AudioTransportSource transportSource;
MixerAudioSource mixer;
And in my MainComponent.cpp;
formatManager.registerBasicFormats();
transportSource.addChangeListener(this);
auto readerBrickHit = formatManager.createReaderFor(new MemoryInputStream(BinaryData::BrickHit_wav, BinaryData::BrickHit_wavSize, false));
if (readerBrickHit != nullptr)
{
std::unique_ptr<AudioFormatReaderSource> newSource(new AudioFormatReaderSource(readerBrickHit, true));
transportSource.setSource(newSource.get(), 0, nullptr, readerBrickHit->sampleRate);
readerSource.reset(newSource.release());
}
and further down in same file I got;
MainComponent::~MainComponent()
{
// This shuts down the audio device and clears the audio source.
shutdownAudio();
}
void MainComponent::changeListenerCallback(ChangeBroadcaster* source)
{
if (source == &transportSource)
{
transportSource.setPosition(0.0);
if (transportSource.isPlaying()) transportSource.start();
}
}
//==============================================================================
void MainComponent::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
transportSource.prepareToPlay(samplesPerBlockExpected, sampleRate);
}
void MainComponent::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
{
if (readerSource.get() == nullptr)
{
bufferToFill.clearActiveBufferRegion();
return;
}
transportSource.getNextAudioBlock(bufferToFill);
}
void MainComponent::releaseResources()
{
transportSource.releaseResources();
}
And finally anywhere in same I just do transportSource.start(); to start the one sound effect.
Now I need to add more sound effects, wav files, and be able to start each whenever needed. I figure I need to something like this;
mixer.addInputSource(new AudioFormatReaderSource(readerBrickHit, true), true); and perhaps only need one transport but if that is the case I certainly do not know how to specify which sample (source) to start playing whenever needed.
If somebody could help me get going with a quick example I would appreciate it a lot.
