0

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.

hoju
  • 28,392
  • 37
  • 134
  • 178
  • How did you manage to merge it? I have not been able to do it. Only merge so that one file plays after the other with -m. "sox -M new_input.wav myrecording.wav output_test.aiff" does not seem to work... – Ellen S Feb 26 '12 at 20:40

1 Answers1

1

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
  • 7,931
  • 3
  • 24
  • 34