# Handling mp3/m4a read into an array

**URL:** <https://forum.juce.com/t/handling-mp3-m4a-read-into-an-array/8252>\
**Category:** General JUCE discussion\
**Created:** [March 1, 2012, 6:31pm UTC](https://forum.juce.com/t/handling-mp3-m4a-read-into-an-array/8252 "2012-03-01T18:31:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wshen](https://avatars.discourse-cdn.com/v4/letter/w/f08c70/32.png) [@wshen](https://forum.juce.com/u/wshen)\
**Post date:** [March 1, 2012, 6:31pm UTC](https://forum.juce.com/t/handling-mp3-m4a-read-into-an-array/8252/1 "2012-03-01T18:31:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jaydee](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jaydee](https://forum.juce.com/u/jaydee)\
**Post date:** [March 2, 2012, 1:24pm UTC](https://forum.juce.com/t/handling-mp3-m4a-read-into-an-array/8252/2 "2012-03-02T13:24:33Z")

</div>

Your bufferToFill doesn’t have a buffer allocated.

```auto
class AudioReader
{
public:
AudioSampleBuffer buffer;
....

AudioReader::AudioReader() : buffer(2,8192) // for 2 channels, 8192 samples
{
...
bufferToFill.buffer=&buffer;

}
```

Also you can put things that don’t need to persist like File on the stack(no need to use new)

And for other things, instead of using delete, use a scopedpointer so

```auto
ScopedPointer<AudioReader> audioReader(new AudioReader(reader,true));
```

---

<div class="post-metadata">

**Author:** ![jaydee](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jaydee](https://forum.juce.com/u/jaydee)\
**Post date:** [March 2, 2012, 1:26pm UTC](https://forum.juce.com/t/handling-mp3-m4a-read-into-an-array/8252/3 "2012-03-02T13:26:21Z")

</div>

Oh, and you probably DON’T want to load the totalLength of the audio in on your readaudio. At most the number of samples assigned to your sample buffer.

---

<div class="post-metadata">

**Author:** ![wshen](https://avatars.discourse-cdn.com/v4/letter/w/f08c70/32.png) [@wshen](https://forum.juce.com/u/wshen)\
**Post date:** [March 2, 2012, 8:40pm UTC](https://forum.juce.com/t/handling-mp3-m4a-read-into-an-array/8252/4 "2012-03-02T20:40:56Z")

</div>

Thanks man! You solved my problem! Awesome!
