Thursday, December 3, 2009

Tut 4: Writing Audio To File

Sup, lets get our audio onto the disk:
That involves writing files. Read this if that's new to you.

The library were going to use to do this is LibSndFile. That's probably the most used sound file reading/writing library out there, and has tons of features.

For the minute, were only concerned with the basics: Creating a "sound-file" inside C++, writing a Sin() wave into a buffer, and writing this buffer to disk.

I'm going to quote Pau Arumi's blog, because he has some fantastic code written for us. Grab his main.cpp from here, (zipped up & hosted on this blogs data page so the link wont break.)

You'll need to link to the libsndfile library,or sometimes its just called sndfile.

You should take note, that were #includeing  sndfile.hh.
This is the C++ interface to libsndfile, which gives SndFile a nicer API to work with. If compiling gives errors like:
*"outfile" was not declared
*"SF_FORMAT_WAV" was not declared
etc etc, your missing the header file, or the linking isnt going according to plan.

The code should compile & when run, write a file called "foo.wav" to the the directory where your code is RUN. This is not nesisarily the same place as were your sources are. Check for suspicious folders, "build", "debug","release" etc. Or search for "foo.wav" ;-)

Till next time, -Harry

No comments:

Post a Comment