AudioFormatReader AudioBuffer and multi channel WAV files

I’m trying to fill an AudioSampleBuffer (AudioBuffer) with a selected channel of audio from a multi-channel WAV file is there any built-in support for this using an AudioFormatReader or do I need to write this one myself?

My ultimate goal is to create a new audio file which only contains the channels I require from the multi-channel WAV file. I have my own classes to do this where I don’t use JUCE, but I’d prefer to use the JUCE API.



Okay… a bit of a runaround… but this works…

ScopedPointer<AudioFormatReader> pReader;

pReader = pFormatManager->createReaderFor (File (szSrcPath));
header.numChannels      = pReader->numChannels;
header.sampleRate       = pReader->sampleRate;
header.lengthInSamples  = pReader->lengthInSamples;
header.bitsPerSample    = pReader->bitsPerSample;

WavAudioFormat wav;

AudioSampleBuffer  mainBuffer (header.numChannels, header.lengthInSamples);
AudioSampleBuffer  destBuffer (2, header.lengthInSamples);

pReader->read (&mainBuffer, 0, header.lengthInSamples, 0, true, true);  // Fill mainBuffer with all data

destBuffer.copyFrom (0, 0, mainBuffer, 0, 0, header.lengthInSamples);   // Copy chan 1
destBuffer.copyFrom (1, 0, mainBuffer, 1, 0, header.lengthInSamples);   // Copy chan 2

ScopedPointer<AudioFormatWriter> pWriter;

pWriter = wav.createWriterFor (pFos, pReader->sampleRate, 2, pReader->bitsPerSample, nullptr, 0);

pWriter->writeFromAudioSampleBuffer (destBuffer, 0, header.lengthInSamples);    // Write the new file

AudioFormatReader::read docs could be improved.


I’m working on surround and multi-channel support for most of the readers and writers. It’s super easy for the CoreAudio reader/writer but a bit more work for the other readers and writers. It’s not top-priority atm so it may take a while until it’s finished.