Proper clean-up from playing a wave file


#1

I need to play a wave file.  I have, more or less, harvested code snippets from the JUCE Demo solution to accomplish this first bit of work (see below), but I'm having problems with an exception being thrown when I exit.

Looking at the debugger it appears as though there is still an active WSAPI thread running, so I assume I haven't performed proper shutdown/clean-up on one of objects (maybe the reader object??).  Any ideas?


AudioFormatManager formatManager;
formatManager.registerBasicFormats();

ScopedPointer<AudioDeviceManager> deviceManager = new AudioDeviceManager();
deviceManager->initialise (2, 2, nullptr, true, String::empty, nullptr);

AudioSourcePlayer audioSourcePlayer;
deviceManager->addAudioCallback(&audioSourcePlayer);

AudioTransportSource transportSource;
audioSourcePlayer.setSource (&transportSource);

ScopedPointer<AudioFormatReader> reader = formatManager.createReaderFor(srcFile);
ScopedPointer<AudioFormatReaderSource> currentAudioFileSource = 
    new AudioFormatReaderSource (reader, true);

transportSource.setSource (currentAudioFileSource, 0, nullptr, reader->sampleRate);
transportSource.start();

// in the future other will be done here
// this works until that process is hooked up
getchar();

// stop and clean-up...
transportSource.stop();
transportSource.releaseResources();
currentAudioFileSource->releaseResources();
transportSource.setSource (nullptr);
audioSourcePlayer.setSource (nullptr);
deviceManager->removeAudioCallback (&audioSourcePlayer);

return; // exception thrown here....


 

 


#2

It seems I have to remove the ScopedPointer off of the AudioFormatReader in order to stop getting the exception.

From this...

ScopedPointer<AudioFormatReader> reader = formatManager.createReaderFor(srcFile);

To this...

AudioFormatReader *reader = formatManager.createReaderFor(srcFile);

But who will free the reader?  According to the tooltip for createReaderFor (File&), it is the caller's responsiblity to free the reader.


#3

 According to the tooltip for createReaderFor (File&), it is the caller's responsiblity to free the reader.

No - the docs for createReaderFor say the exact opposite!


#4

From juce_AudioFormatManager.h...

    /** Searches through the known formats to try to create a suitable reader for
        this file.
        If none of the registered formats can open the file, it'll return 0. If it
        returns a reader, it's the caller's responsibility to delete the reader.
    */
    AudioFormatReader* createReaderFor (const File& audioFile);

And it says the same thing in the online doc ere...

http://www.juce.com/api/classAudioFormatManager.html#ab78bd5aa0ff2b9b667b5156b3bac5e83

No big deal, I fixed my code.


#5

Oh, sorry, I mis-read that, and thought you were asking about deleting the stream, not the reader.