11

I'm using the sox tool and I would like to merge two audio files, let's say long.ogg and short.ogg to output a file output.ogg. This is very easy using $ sox -m long.ogg short.ogg output.ogg.

Thing is, I would like the short.ogg to be played after n seconds (while long.ogg should start right from the beginning). To do so, I've found the pad effect. But I don't understand the syntax to delay only the short.ogg input file, not the long.ogg one.

I found a (dirty) way of doing so (with n=6):

$ sox short.ogg delayed.ogg pad 6
$ sox -m long.ogg delayed.ogg output.ogg

I would like not to have to create an intermediate file. Thanks in advance for your help.

zopieux
  • 2,854
  • 2
  • 25
  • 32

5 Answers5

13

You should be able to do something like:

sox short.ogg -p pad 0 6|sox - long.ogg output.ogg

-p option to sox is used for piping - basically, it tells sox to use stdout as the output. Using - as the input to the second sox is actually saying input is stdin (which happens to be the stdout of the previous sox, as we are piping with |). pad 0 6 tells pad 0 seconds at the beginning and 6 seconds at the end.

Hope this helps.

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • Thanks! I actually had to add the mix option `-m` because without it, the whole sound was delayed. Please see my edit. – zopieux Apr 29 '11 at 07:30
  • I did not read the question - what I wrote will play short.ogg, wait 6 seconds and then play long.ogg, writing the whole result to output.ogg. Anyway, glad you found the solution! I suggest adding it as an answer and officially choosing as the solution - for the others with the same problem. – icyrock.com Apr 30 '11 at 00:52
12

Thanks to icyrock, I managed to find a solution. I'm using:

$ sox short.ogg -p pad 6 0 | sox - -m long.ogg output.ogg

For multi tracks (credits to Orlando):

$ sox starts-last.mp3 -p pad 2 0 | sox - -m starts-second.mp3 -p pad 2 0 | sox - -m starts-first.mp3 combined.mp3
zopieux
  • 2,854
  • 2
  • 25
  • 32
2

Here is another solution benefiting from the use of double-quotes. It makes the command much more readable and very easy to extend:

sox −−combine sequence  "|sox input/odin.wav -p pad 0 1" "|sox input/dva.wav -p pad 0 1" "|sox input/tri.wav -p pad 0 1" output/test.wav

This would concatenate all three files, with a 1-second pause after each (odin, silence, dva, silence, tri, silence.)

Applied to the original post, we'd get:

sox −−combine sequence  "|sox long.ogg -p pad 0 6" "|sox short.ogg -p pad 0 1" newfile.ogg

(Tested OK on SoX 14.4.1 on Windows.)

I have hardly seen any SoX example using double-quotes, so I hope this helps someone!

Fabien Snauwaert
  • 4,995
  • 5
  • 52
  • 70
1

Because I'm not allowed to comment yet, I would just like to say that I like Fabien's answer because it can also be used with the "NULL" soundfile: e.g.,

sox --combine concatenate "|sox -n -p synth 1 sine 300" "|sox -n -p synth 1 sine 400" useful.wav

Will write 1 second of a sine tone at 300 Hz followed by 1 second of a sine tone at 400 Hz and write it to the file "useful.wav".

sox -n -p synth 1 sine 300 | sox --combine concatenate - farts.wav synth 1 sine 400

Will only write 1 second of a sine wave at 400 Hz to the file "farts.wav". The file is missing the first sine wave at 300 Hz.

mondaugen
  • 425
  • 5
  • 12
0

Ran across delay command as well.

You could do the following: sox.exe -m short.ogg long.ogg delay 6

This will merge the two files and short.ogg will start 6 seconds into long.ogg

VicF
  • 21
  • 3