Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute beginners guide to working with audio in C/C++?

Tags:

c++

c

audio

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.

like image 468
Vlad the Impala Avatar asked Nov 29 '09 03:11

Vlad the Impala


2 Answers

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.

like image 174
Vlad the Impala Avatar answered Nov 08 '22 00:11

Vlad the Impala


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.

like image 23
James Morris Avatar answered Nov 08 '22 01:11

James Morris