tags:

views:

356

answers:

4

I've always been curious about audio conversion software, but I have never seen a proper explanation from a beginners point of view as to how to write a simple program that converts for example, a mp3 file to a wav. I'm not asking about any of the complex algorithms involved, just a small example using a simple library. Searching on SO, I came up with several names including:

  • Lame
  • The Synthesis Toolkit
  • OpenAL
  • DirectSound

But I'm unable to find a straightforward example of any of these libraries. Usually I don't mind wading through tons of code, but here I have absolutely no knowledge about the subject and so I always feel like I'm shooting in the dark.

Anyone here have a simple example / tutorial on converting a sound file using any of these libraries? My question is specifically directed towards C/C++ because those are the two languages I'm currently learning and so I'd like to continue to focus on them.

Edit: One thing I forgot to mention: I'm on a *NIX system.

A: 

Check libmad http://mad.sourceforge.net " "M"peg "A"udio "D"ecoder library", should provide a good example. Also for an easy cross-platform audio handling, check SDL http://www.libsdl.org/. Hope that helps.

OneOfOne
I got this one working but the example creates .PCM files, which I can't figure out how to play. Related question at superuser.com here: http://superuser.com/questions/76665/playing-a-pcm-file-on-a-unix-system
Goose Bumper
Update: I can import the PCM files in Audacity by going to Project > Import Raw Data.
Goose Bumper
+1  A: 

If your curious about DSP and computers, take a look at the Synthesis Toolkit. It's sweet. It's designed for learning. The examples and tutorials they have on their website are straightforward and thorough. Keep in mind, the guys who wrote it, wrote it so they could create acoustic models of real instruments. As a result, they've included some instruments that are just plain wacky, but fun. It will give you a core understanding of processing PCM sound. And you'll probably be able to hack together some fun little noisemakers while your at it.

https://ccrma.stanford.edu/software/stk/

just_wes
+2  A: 

For converting between various formats (except MP3) check libsndfile http://mega-nerd.com/libsndfile/

Libsndfile is a library designed to allow the reading and writing of many different sampled sound file formats (such as MS Windows WAV and the Apple/SGI AIFF format) through one standard library interface.

During read and write operations, formats are seamlessly converted between the format the application program has requested or supplied and the file's data format. The application programmer can remain blissfully unaware of issues such as file endian-ness and data format

It is also simple to use, with the API following the style of the Standard C library function names:

http://mega-nerd.com/libsndfile/api.html

And examples are included in the source distribution.

For actual audio output, another library will be needed, SDL as already mentioned might be a good place to start. While SDL can also read/write audio files, libsndfile is far superior.

James Morris
+1  A: 

Thanks everyone for the responses! I sort of cobbled them together to successfully make a small utility that converts a AIFF/WAV/etc file to an mp3 file. There seems to be some interest in this question, so here it what I did, step by step:

Step 1: Download and install the libsndfile library as suggested by James Morris. This library is very easy to use – its only shortcoming is it won't work with mp3 files.

Step 2: Look inside the 'examples' folder that comes with libsndfile and find generate.c. This gives a nice working example of converting any non-mp3 file to various file formats. It also gives a glimpse of the power behind libsndfile.

Step 3: Borrowing code from generate.c, I created a c file that just converts an audio file to a .wav file. Here is my code: http://pastie.org/719546

Step 4: Download and install the LAME encoder. This will install both the libmp3lame library and the lame command-line utility.

Step 5: Now you can peruse LAME's API or just fork & exec a process to lame to convert your wav file to an mp3 file.

Step 6: Bring out the champagne and caviar!

If there is a better way (I'm sure there is) to do this, please let me know. I personally have never seen a step-by-step roadmap like this so I thought I'd put it out there.

Goose Bumper