# Write a .wav file from buffer example

**URL:** <https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789>\
**Category:** General JUCE discussion\
**Created:** [February 15, 2019, 8:34pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789 "2019-02-15T20:34:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pizzafilms](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pizzafilms/32/562_2.png) [@pizzafilms](https://forum.juce.com/u/pizzafilms)\
**Post date:** [February 15, 2019, 8:34pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/1 "2019-02-15T20:34:29Z")

</div>

Is there a current example of how to simply write an AudioBuffer to a wave file?

Somehow I can’t find anything. Thanks.

---

<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:** [February 15, 2019, 8:52pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/2 "2019-02-15T20:52:43Z")

</div>

That’s the code:

```auto
AudioBuffer<float> buffer;
WavAudioFormat format;
std::unique_ptr<AudioFormatWriter> writer;
writer.reset (format.createWriterFor (new FileOutputStream (file),
                                      48000.0,
                                      buffer.getNumChannels(),
                                      24,
                                      {},
                                      0));
if (writer != nullptr)
    writer->writeFromAudioSampleBuffer (buffer, 0, buffer.getNumSamples());

```

Hope that helps

---

<div class="post-metadata">

**Author:** ![pizzafilms](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pizzafilms/32/562_2.png) [@pizzafilms](https://forum.juce.com/u/pizzafilms)\
**Post date:** [February 15, 2019, 8:59pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/3 "2019-02-15T20:59:57Z")

</div>

Thanks, but I’m getting errors…

- Allocating an object of abstract class type ‘juce::OutputStream’
- No member named ‘createWriter’ in ‘juce::WavAudioFormat’
- Unknown type name ‘OutputFileStream’; did you mean ‘OutputStream’?

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [February 15, 2019, 9:01pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/4 "2019-02-15T21:01:33Z")

</div>

Should be `FileOutputStream`, and `createWriteFor()` 😉

---

<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:** [February 15, 2019, 9:03pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/5 "2019-02-15T21:03:26Z")

</div>

Sloppy me, shouldn’t write out of my head… 🙂 thanks, I’ll edit the code above

---

<div class="post-metadata">

**Author:** ![pizzafilms](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pizzafilms/32/562_2.png) [@pizzafilms](https://forum.juce.com/u/pizzafilms)\
**Post date:** [February 15, 2019, 9:05pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/6 "2019-02-15T21:05:02Z")

</div>

Thanks, gents.

I’m still a little stunned that I couldn’t find an example of writing a wav file anywhere on the forum or docs.

---

<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:** [February 15, 2019, 9:07pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/7 "2019-02-15T21:07:16Z")

</div>

Here is some code, just a little more elaborate:

> <https://github.com/WeAreROLI/JUCE/blob/master/examples/Audio/AudioRecordingDemo.h#L87>

---

<div class="post-metadata">

**Author:** ![pizzafilms](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pizzafilms/32/562_2.png) [@pizzafilms](https://forum.juce.com/u/pizzafilms)\
**Post date:** [February 15, 2019, 9:17pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/8 "2019-02-15T21:17:46Z")

</div>

Thanks, but your first example is exactly what I needed. I found quite a few streaming examples but I just needed to write an AudioBuffer. Thanks again.

---

<div class="post-metadata">

**Author:** ![Elan\_Hickler](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@Elan\_Hickler](https://forum.juce.com/u/Elan_Hickler)\
**Post date:** [October 21, 2019, 6:50am UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/9 "2019-10-21T06:50:53Z")

</div>

Trying to write wav file, what am I doing wrong?

```cpp
AudioBuffer<float> buffer(1, singleChannelAudio.size());

auto ptr = buffer.getWritePointer(0);

for (int f = 0; f < singleChannelAudio.size(); ++f)
    ptr[f] = float(singleChannelAudio[f]);

WavAudioFormat format;
std::unique_ptr<AudioFormatWriter> writer;
writer.reset(format.createWriterFor(new FileOutputStream(path), sampleRate, 1, bitDepth, {}, 0));

if (writer == nullptr)
    return false;

writer->writeFromAudioSampleBuffer(buffer, 0, buffer.getNumSamples());    

return true;

```

this gives error:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/b/b4e08f678808b8162076ed11c1061cf64beada6b.png)

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [October 21, 2019, 3:05pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/10 "2019-10-21T15:05:44Z")

</div>

That code works for me (with the obvious additions to allow it to compile). There’s probably some problem with the file path you are using. Maybe trying to write to a directory that doesn’t exist or the directory doesn’t have write permissions.

---

<div class="post-metadata">

**Author:** ![Elan\_Hickler](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@Elan\_Hickler](https://forum.juce.com/u/Elan_Hickler)\
**Post date:** [October 21, 2019, 10:03pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/11 "2019-10-21T22:03:32Z")

</div>

file wasn’t created due to bad file name

---

<div class="post-metadata">

**Author:** ![shlomo](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@shlomo](https://forum.juce.com/u/shlomo)\
**Post date:** [June 25, 2021, 4:13pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/12 "2021-06-25T16:13:43Z")

</div>

In the example, the active writer thread is using inputChannelData, which is a float\*\* and not a buffer.  
`activeWriter.load()->write (inputChannelData, numSamples);`  
([JUCE/AudioRecordingDemo.h at 90e8da0cfb54ac593cdbed74c3d0c9b09bad3a9f · juce-framework/JUCE · GitHub](https://github.com/juce-framework/JUCE/blob/90e8da0cfb54ac593cdbed74c3d0c9b09bad3a9f/examples/Audio/AudioRecordingDemo.h#L145))

How can I use a buffer to write directly using the thread like so, so that the modified buffer is written to the wav file via the stream:  
`activeWriter.load()->write (buffer, numSamples);`

Thanks

---

<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:** [June 25, 2021, 7:48pm UTC](https://forum.juce.com/t/write-a-wav-file-from-buffer-example/31789/13 "2021-06-25T19:48:43Z")

</div>

You can always get the raw float pointers from a buffer using [getArrayOfReadPointers()](https://docs.juce.com/master/classAudioBuffer.html#a9cf2897fe524acc82e95337a033718a3):

```auto
activeWriter.load()->write (buffer.getArrayOfReadPointers(), buffer.getNumSamples());

```

HTH
