AudioBuffer to MemoryBlock

Hey, apologies if this is a totally dumb/noob question, but is there a direct way to store audiobuffer data to a memory block? I was wanting to use toBase64Encoding on the block to save the audio data into the xml element as an attribute and then somehow read it back into my audiobuffer when the plugin state loads.

I’ll also add that I tried to just add the buffer to the block with something like

MemoryBlock loadedaudio;
	    loadedaudio.append(fileBuffer, sizeof(fileBuffer.getSample(0,0)) * fileBuffer.getNumSamples()* fileBuffer.getNumChannels());

But clearly this does not work because the input of the first argument has to be in the form of const void *, which the audioBuffer call fileBuffer is not. Is there a way to make that conversion? Perhaps I’m going about this all wrong to begin with?

Sure, just use a MemoryOutputStream in the AudioFormat::createWriterFor (OutputStream*, …)

I don’t know, if that’s a great idea. I would rather append the audio file to the xml file and use a SubregionStream to read it back. In the xml you can save the size of the xml as header. Allow some padding, because the numbers in the xml could change the header size by a few bytes.


Hey daniel, thanks for the insight! I’ll give this a try. I’ll also mention for anybody else that finds any of this useful, I mainly wanted a way for the plugin to remember what audio file I had opened with file chooser. Since I’m using AudioProcessorValueTreeState for my plugin parameters, In the interim I decided to just save out the full file path in the ValueTree state of the processor whenever I open an audio file:


save out the parameters in getStateInformation

void AudioProcessor::getStateInformation (MemoryBlock& destData)
	ScopedPointer<XmlElement> xml (parameters.state.createXml());
	copyXmlToBinary (*xml, destData);

and then reload the file with that path in the setStateInformation function. It seems to be working well. Obviously there’s the pitfall of it not finding the audio data if I move audio files around on my computer, etc. But I suppose it’s another option for anybody wanting to reload the original audio data, and not AudioBuffer data that’s already been through some processing by the plugin?