This would be a really useful function to have in JUCE somewhere…unless it’s already there and I’ve not noticed. Serious applications might need more flexibility - but a lot of the time this is all that’s needed for testing and making toys…
AudioSampleBuffer * loadAudioSampleBuffer(const File & source, double * sampleRate = nullptr)
{
jassert(source.existsAsFile());
AudioFormatManager afm;
afm.registerBasicFormats();
ScopedPointer<AudioFormatReader> reader = afm.createReaderFor(source);
if (!reader)
return nullptr;
auto buffer = new AudioSampleBuffer(reader->numChannels, int(reader->lengthInSamples));
reader->read(buffer, 0, int(reader->lengthInSamples), 0, true, true);
if (sampleRate != nullptr)
*sampleRate = reader->sampleRate;
return buffer;
}