I am trying to learn a little about Juce, but I am no expert in programming. I do not understand exactly the use of the ScopedPointer below:
(that is from AudioSynthesiserDemo)
void setUsingSampledSound()
{
WavAudioFormat wavFormat;
ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (new MemoryInputStream (BinaryData::cello_wav, BinaryData::cello_wavSize,
false),
true));
BigInteger allNotes;
allNotes.setRange (0, 128, true);
synth.clearSounds();
synth.addSound (new SamplerSound ("demo sound",
*audioReader,
allNotes,
74, // root midi note
0.1, // attack time
0.1, // release time
10.0 // maximum sample length
));
}
Is it absolutely necessary ? Does it have to do with the dynamic allocation of MemoryInputStream ? Does it have to do with its use in SamplerSound constructor ?
Have a read about the C++ operators new and delete: http://www.cplusplus.com/doc/tutorial/dynamic/ .
Also, feel free to read up on scope: http://en.wikipedia.org/wiki/Scope_%28computer_science%29
In short, a ScopedPointer, when it goes out of scope, simply handles deleting the object it has ownership over. This makes use of the RAII idiom to avoid accidentally leaking memory (i.e.: Forgetting to "delete" a heap object).
I suggest reading the description of the class for more information: https://www.juce.com/api/classScopedPointer.html#details
It's not necessary to use this, but it is suggested to use this approach since it's modern practice in C++. JUCE provides ScopedPointer because not all platforms implement the new C++11 stuff. Check out: http://en.cppreference.com/w/cpp/memory/auto_ptr and http://en.cppreference.com/w/cpp/memory/unique_ptr .
This type of thing has no direct relation to any class, it's a memory handling technique.
Thank you for your answer. I know it has to do mainly with C++ functionning rather than Juce. But I was wondering if the context here made it necessary.
In the following code, in AudioPlaybackDemo, there is a simple pointer: