Write a .wav file from buffer example

#1

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

Somehow I can’t find anything. Thanks.

0 Likes

#2

That’s the code:

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

1 Like

#3

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’?
0 Likes

#4

Should be FileOutputStream, and createWriteFor() :wink:

0 Likes

#5

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

0 Likes

#6

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.

0 Likes

#7

Here is some code, just a little more elaborate:

1 Like

#8

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.

0 Likes