views:

104

answers:

1

I have 2 audio files for primary and background music that I want to merge (not concatenate). The final audio file should be as long as the primary file, and if the background music is shorter then it should repeat.

If there a Linux command or a Python library that can be used to do this? Sox supports merging, but does not appear to allow repeating the background audio.

+1  A: 

As a possible solution, why not detect if the length of the background file < length of the foreground file and then construct a background file which is a loop, if necessary? Then you can pass that into sox.

You should be able to get the length from sndhdr (look at the frames count).

As far as a python way of merging the streams, audioop.add may do what you need, although if you're merging two full volume sources, you might want to reduce the volume of one of them (try -12db as a start) if you're mixing music and not.

More audio libraries can be found here.

Rizwan Kassim