How to write wav files in C++ (using libsndfile)

December 16, 2007

As the other how-tos of this serie, this is also a side-effect of the project I’ve been working recently.

Libsndfile is a very popular library for reading and writing lossless audio files written by Erik de Castro Lopo. We use it in Clam and I’ve use it in other small applications.

This time I wanted to use the C++ wrapper (sndfile.hh header) added recently and, since I couldn’t find an example of use, well, time to post mine here.

I like a lot better the C++ api than the C one. See also the C sndfile API documentation.

int main()
const int format=SF_FORMAT_WAV | SF_FORMAT_PCM_16;
// const int format=SF_FORMAT_WAV | SF_FORMAT_FLOAT;
const int channels=1;
const int sampleRate=48000;
const char* outfilename=”foo.wav”;

SndfileHandle outfile(outfilename, SFM_WRITE, format, channels, sampleRate);
if (not outfile) return -1;

// prepare a 3 seconds buffer and write it
const int size = sampleRate*3;
float sample[size];
float current=0.;
for (int i=0; iYou’ll find a complete reference on the available formats on the sndfile API doc. But this are typical subformats of the Wav format. As in the example above, put them after the SF_FORMAT_WAV | portion:

Signed 16, 24, 32 bit data:


Float 32, 64 bit data:


11 Responses to “How to write wav files in C++ (using libsndfile)”

  1. Carl-Erik Says:

    Thanks for posting this! There is really not much info for the beginning sound programmer out there, so every code bit counts. What I really do not understand, is how to convert one format to another using the C API.

    Could you post an (incomplete), bare-bones example showing how to read one file (for instance FLAC) into a buffer, and then from this buffer write another file in another format (WAV or something else)?

    I would just need the absolute minimum (pseudo-?)code, no need for the includes, etc.

    Please? :)

  2. Angelos Says:

    hey, check this code that i wrote, using this code you can easily write a wave file, header, sub-header and data part everythign is explained along with the code, enjoy!!

  3. Niv Says:

    I was wondering if you can give me the libraries you used here.

    Thnks Again,
    Niv Noiman.

  4. Your webpage is a very good summary of your activities. great!

    Wiko looks like very interesting.

  5. johnm Says:

    good info, thanks

  6. Aario Says:

    Please help. Whatever I do, I cannot compile it.
    I saved the code in musicrobot.cpp and ran below command:
    gcc ./musicrobot.cpp -lm -o ./musicrobot
    And got:
    /tmp/cch73aFh.o: In function `__static_initialization_and_destruction_0(int, int)’:
    musicrobot.cpp:(.text+0x185): undefined reference to `std::ios_base::Init::Init()’
    musicrobot.cpp:(.text+0x18a): undefined reference to `std::ios_base::Init::~Init()’
    /tmp/cch73aFh.o: In function `SndfileHandle::SNDFILE_ref::~SNDFILE_ref()’:
    musicrobot.cpp:(.text._ZN13SndfileHandle11SNDFILE_refD2Ev[_ZN13SndfileHandle11SNDFILE_refD5Ev]+0x18): undefined reference to `sf_close’
    /tmp/cch73aFh.o: In function `SndfileHandle::SndfileHandle(char const*, int, int, int, int)’:
    musicrobot.cpp:(.text._ZN13SndfileHandleC2EPKciiii[_ZN13SndfileHandleC5EPKciiii]+0x14): undefined reference to `std::nothrow’
    musicrobot.cpp:(.text._ZN13SndfileHandleC2EPKciiii[_ZN13SndfileHandleC5EPKciiii]+0x20): undefined reference to `operator new(unsigned int, std::nothrow_t const&)’
    musicrobot.cpp:(.text._ZN13SndfileHandleC2EPKciiii[_ZN13SndfileHandleC5EPKciiii]+0xbd): undefined reference to `sf_open’
    /tmp/cch73aFh.o: In function `SndfileHandle::~SndfileHandle()’:
    musicrobot.cpp:(.text._ZN13SndfileHandleD2Ev[_ZN13SndfileHandleD5Ev]+0x4a): undefined reference to `operator delete(void*)’
    /tmp/cch73aFh.o: In function `SndfileHandle::write(float const*, long long)’:
    musicrobot.cpp:(.text._ZN13SndfileHandle5writeEPKfx[SndfileHandle::write(float const*, long long)]+0x32): undefined reference to `sf_write_float’
    /tmp/cch73aFh.o:(.eh_frame+0xf3): undefined reference to `__gxx_personality_v0′
    collect2: ld returned 1 exit status

  7. Rootedy Says:

    Thanks… It exactly what I’m needing… I used this with portaudio library to capture the audio of a microphone and write it in a mp3 file ( to write in a mp3 file just change the name of the file to ‘*fileName*.mp3’). Thanks a lot.

    • snm Says:

      Hi, I can not record a wav file using portaudio example paex_record.c .How can you record as an mp3 ?

  8. Thank you, I’ve just been searching for info about this subject for ages and yours is the greatest I’ve discovered till now. However, what about the bottom line? Are you sure concerning the source?

  9. 86Christy Says:

    I have noticed you don’t monetize your page, don’t waste your traffic, you can earn extra
    cash every month because you’ve got high
    quality content. If you want to know how to make extra money, search for: best adsense alternative Wrastain’s

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: