views:

444

answers:

4

I'm looking for a library that could be used to manipulate audio files. Essentially what I would like to do is:

  1. Load an MP3/WAV file

  2. Get a 15 second clip of the file

  3. Overlay another MP3/WAV file ontop of it

  4. Render as a new MP3/WAV file

+1  A: 

You can use any common MP3 codec API to decode the stream, work with it, and save it again. For instance you could use libLAME for this part.

As for mixing, you could either do it yourself (e.g., naively, add two samples and divide by two -- which might not sound too good), or find a proper library for it.

You might also be interested in a related Stack Overflow question at http://stackoverflow.com/questions/1042752/best-c-audio-library-linux

csl
+1  A: 

The library libsox from sox (and not http://libsox.sourceforge.net/ which is something completely different) seems to have a simple API which can be used. The documentation gives the following example for stereo to mono mixing:

Representing samples as integers can cause problems when processing the audio. For example, if an effect to mix down left and right channels into one monophonic channel were to use the line

    *obuf++ = (*ibuf++ + *ibuf++)/2;

distortion might occur since the intermediate addition can overflow 32 bits. The line

    *obuf++ = *ibuf++/2 + *ibuf++/2;

would get round the overflow problem (at the expense of the least significant bit).

hlovdal
A: 

You might try giving this a look over http://terminatorx.org/ I was looking at it to do something similar.

willz
+2  A: 

It doesn't support MP3 for patent reasons, but libsndfile is a very nice open-source (LGPL) library for loading and saving audio in a variety of other audio formats, including WAV.

As for the overlay part, that's easy once you've got the samples loaded into memory... you just sum each sample in file A with its corresponding sample in file B (and possibly scale the resulting sample value down a bit by multiplying by a constant, if you're worried about clipping).

Jeremy Friesner
+1 for libsndfile.
James Morris