Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Sound Processing [closed]

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

like image 205
xian Avatar asked Oct 14 '22 12:10

xian


2 Answers

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).

like image 121
Jeremy Friesner Avatar answered Oct 18 '22 05:10

Jeremy Friesner


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).

like image 29
hlovdal Avatar answered Oct 18 '22 03:10

hlovdal