I'm looking for a library that could be used to manipulate audio files. Essentially what I would like to do is:
Load an MP3/WAV file
Get a 15 second clip of the file
Overlay another MP3/WAV file ontop of it
Render as a new MP3/WAV file
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).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With