I would highly recommend to rely on AudioBuffer<float> here for memory management,
a) it follows RAII principles (vs. your new float[x] call can easily leak
b) the methods are much simpler, because they were designed to work together
You still can get the same result, if you simply call afterwards:
Just scrolled down, why are you calling a reinterpret_cast? You better forget, that this exists, for constructions you are using it is definitely the wrong thing. There are very rare cases, where you need a reinterpret_cast. You probably added it in, because it didn’t compile otherwise, so that should have told you, that you are not doing it right.
Understood, that’s why I posted the use of AudioBuffer::getArrayOfWritePointers(), which gives you access to exactly that float** pointer, that your other code uses.
For clarification: AudioSampleBuffer == AudioBuffer<float>.
The read method you used, will return the data keeping the datatype that is used in the file. It is your responsibility to convert the data. But a reinterpret_cast doesn’t convert, it “reinterprets” ignoring the actual type before.
The method using an AudioBuffer converts to float for you.
So the void AudioFormatReader::read (AudioBuffer<float>* buffer...) methods internally uses the bool AudioFormatReader::read (int* const* destSamples...) but without checking the possible false result? Why that? How are we supposed to check for possible errors when reading the file using the void AudioFormatReader::read (AudioBuffer<float>* buffer...) API ?
…and will try to intelligently cope with mismatches between the number of channels in the reader and the buffer.
that this version corrects the errors that would return false in the prior method.
One last word to reinterpret_cast: sure it works, in fact you can use everywhere reinterpret_cast. It is the override for any type checking in C++. But the type checking is usually in your favour. I don’t know, if here a reinterpret_cast is necessary, a static_cast might work as well. Usually if an API is intended for “figure out the type yourself”, it would return a void pointer.
Here are the different cast types explained in casts on cppreference.
“try to intelligently cope” : I don’t need too much intelligent JUCE code in this case. I just need to get the audio data, and want to know if it fails. So I’ll keep the lover-level read version and conversion to float afterward.