Loading samples from WAV into a vector


Hey all,

I'm new to C/C++ and JUCE, and am struggling with simply getting data out of a WAV file and into a vector. Related topics in the forum appear to offer solutions that have since been deprecated.

I've tested whether or not all the pieces appear to be in place: the JUCE::File, AudioFormatManager, AudioFormatReader, and FileInputStream all check out in terms of the metadata of my dummy file (number of samples, sample rate, channels, etc.), and they appear to be talking to each other just fine. I'm at the point where I've created an AudioSampleBuffer for reading from the FileInputStream, but I can't figure out what's going wrong with how I'm implementing FileInputStream->read. I've posted a snippet of code below; happy to post more if it'll help.


//Creates sample buffer for one channel and 1 second of samples
    AudioSampleBuffer buffer(1, 48000);

//Loads 1 second of samples into buffer
    if (mInputStream->getTotalLength()> 0)
        mInputStream->read(&buffer, 48000);

I've also tried:

mAudioReader->read(&buffer, 1, 0, 48000, true, true);


In both cases, when I iterate through samples in the buffer, every value is 0.0. Can anybody help by telling me what I'm doing wrong, and how to do it right?



I think you need to read using the AudioFormatReader. The documentation here:


shows the meaning for the arguments to this read function:

read (AudioSampleBuffer *buffer, int startSampleInDestBuffer, int numSamples, int64 readerStartSample, bool useReaderLeftChan, bool useReaderRightChan)

So, when you call 

   mAudioReader->read(&buffer, 1, 0, 48000, true, true);

you are asking for 0 samples. Maybe try this:

   mAudioReader->read(&buffer, 0, 48000, 0, true, true);

This asks for 48000 samples, starting at the 0th sample in the file, and starts writing at the 0th sample in your buffer.



Argh, yes, that was it!

I am curious, though, what I was doing wrong with

mInputStream->read(&buffer, 48000);

Since I'd have expected that to work fine.


<removed due to poorly thought out reply> :)


Your mInputStream is an arbitrary file reader where calling read just reads the number of bytes you specify and stores them into memory starting at the address you specify. It is designed for reading many types of files, and it doesn't know anything about the particulars of interpreting audio files. That function takes a void*, and you normally would pass something like a int* or a float*.

Passing the address of an AudioSampleBuffer and a request to store 48000 bytes means that you are wiping out all the useful data that the AudioSampleBuffer is keeping track of, and then once you've stomped all over your AudioSampleBuffer, you continue writing the rest of the 48000 bytes into memory that is supposed to be storing other useful program data. So that call is stomping all over and trashing your program memory. If you're lucky, the program will crash soon afterward. Often this kind of big will corrupt your program in ways that can be difficult to track down.