# Save an AudioBuffer to Xml

**URL:** <https://forum.juce.com/t/save-an-audiobuffer-to-xml/36809>\
**Category:** General JUCE discussion\
**Created:** [December 24, 2019, 2:51pm UTC](https://forum.juce.com/t/save-an-audiobuffer-to-xml/36809 "2019-12-24T14:51:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cila](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@cila](https://forum.juce.com/u/cila)\
**Post date:** [December 24, 2019, 2:51pm UTC](https://forum.juce.com/t/save-an-audiobuffer-to-xml/36809/1 "2019-12-24T14:51:33Z")

</div>

Hello,

I need to save an AudioBuffer to a XmlElement, so I was wondering if there is some way to that similar to this one related to saving an Image:

```
MemoryOutputStream out;
PNGImageFormat format;

format.writeImageToStream(map, out);
juce::MemoryBlock pngData(out.getData(), out.getDataSize());

mapXml->setAttribute("MapImage", pngData.toBase64Encoding());

```

Is there any way to do this? Thanks in advance for any kind of help!  
Best Regards,  
Cila

---

<div class="post-metadata">

**Author:** ![cila](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@cila](https://forum.juce.com/u/cila)\
**Post date:** [December 25, 2019, 2:00pm UTC](https://forum.juce.com/t/save-an-audiobuffer-to-xml/36809/2 "2019-12-25T14:00:02Z")

</div>

Okay so I have tried this solution:

For Saving:

```
        MemoryOutputStream out;
	WavAudioFormat format;
	AudioFormatWriter* writer = format.createWriterFor(&out, 48000, 1, 16, StringPairArray(), 0);
	writer->writeFromAudioSampleBuffer(impulseResponse, 0, impulseResponse.getNumSamples());

	juce::MemoryBlock wavData(out.getData(), out.getDataSize());
	parentXml->getChildByName("ImpulseResponse")->setAttribute("AudioFile", wavData.toBase64Encoding());

```

And loading back:

```
               if (wavData->fromBase64Encoding(parentXml->getChildByName("ImpulseResponse")->getStringAttribute("AudioFile")))
	{

		impulseResponse.clear();
		MemoryInputStream in(wavData->getData(), wavData->getSize(), false);
		WavAudioFormat format;
		AudioFormatReader* reader = format.createReaderFor(&in, false);

		impulseResponse.setSize(1, reader->lengthInSamples);
		reader->read(&impulseResponse, 0, reader->lengthInSamples, 0, true, true);
	}

```

But apparently reader-\>lengthInSamples is always 0 so I dont get my buffer as it should. Is this the right way to do it or I am skipping something?  
Cila

---

<div class="post-metadata">

**Author:** ![cila](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@cila](https://forum.juce.com/u/cila)\
**Post date:** [December 27, 2019, 2:39pm UTC](https://forum.juce.com/t/save-an-audiobuffer-to-xml/36809/3 "2019-12-27T14:39:27Z")

</div>

Nobody had some experience with this? ☹

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [December 27, 2019, 3:58pm UTC](https://forum.juce.com/t/save-an-audiobuffer-to-xml/36809/4 "2019-12-27T15:58:33Z")

</div>

Your problem is, that the writer does not finish, because it is leaking.  
The length of a wav file will be written into the header, when the writer object is destroyed.

Best practice is to put the writer into a unique\_ptr:

```auto
MemoryBlock wavData;
{
    // MemoryOutputStream is owned, if the writer was created successfully
    std::unique_ptr<AudioFormatWriter> writer (format.createWriterFor (new MemoryOutputStream (wavData, false), 48000, 1, 16, StringPairArray(), 0));
    writer->writeFromAudioSampleBuffer(impulseResponse, 0, impulseResponse.getNumSamples());
    // end of scope flushes the writer
}
auto response = parentXml->getChildByName ("ImpulseResponse");
if (response)
    response->setAttribute ("AudioFile", wavData.toBase64Encoding());

```

Hope that helps

---

<div class="post-metadata">

**Author:** ![cila](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@cila](https://forum.juce.com/u/cila)\
**Post date:** [January 6, 2020, 9:18am UTC](https://forum.juce.com/t/save-an-audiobuffer-to-xml/36809/5 "2020-01-06T09:18:56Z")

</div>

Sorry for the late answer! That works like a charm, thank you very much! Cheers 🙂
