I’m new to Juce and I want to read in mp3/m4a with it. In the Juce Demo, there is a AudioPlayBackPage where mp3 and m4a files can be played back, and that is why I think Juce can handle this.
But I met a problem when I was trying to do so
Here is how I wrote my own class handling the audio read in:
[code]#include "JuceHeader.h"
class AudioReader : public AudioFormatReaderSource
{
public:
AudioSourceChannelInfo bufferToFill;
int64 totalLength;
unsigned int numChannels;
double sampleRate;
AudioFormatReader *sourceReader;
AudioReader(AudioFormatReader *sourceReader, bool deleteReaderWhenThisIsDeleted):AudioFormatReaderSource(sourceReader,deleteReaderWhenThisIsDeleted) {
totalLength = sourceReader->lengthInSamples;
numChannels = sourceReader->numChannels;
sampleRate = sourceReader->sampleRate;
this->sourceReader=sourceReader;
}
~AudioReader(){}
float** readAudio(){
this->bufferToFill.buffer->readFromAudioReader(sourceReader, 0, totalLength, 0, true, true);
return this->bufferToFill.buffer->getArrayOfChannels();
}
};[/code]
And this is how I call it in main function:
[code]int main (int argc, char* argv[])
{
float** audio;
File * audioFile = new File("/Users/Ajay/Documents/MATLAB/unBecame_v3.mp3");
AudioFormatManager formatManager;
formatManager.registerBasicFormats();
AudioFormatReader* reader = formatManager.createReaderFor (*audioFile);
if (reader != 0)
{
AudioReader * audioReader = new AudioReader (reader, true);
printf("%d, %lld, %f\n", audioReader->numChannels,audioReader->totalLength,audioReader->sampleRate);
audio=audioReader->readAudio();
delete(audioReader);
}
delete(audioFile);
return 0;
}[/code]
As the code runs, the printf inside the main function can be run and the numbers printed out are right. But then it pops up a “EXC_BAD_ACCESS”, and lead me into the implementation of void AudioSampleBuffer::readFromAudioReader(), and the program stops at jassert (startSample >= 0 && startSample + numSamples <= size);
What is wrong with my code?