Sox: concatenate multiple audio files without a gap in between

The best — though least helpful — way to do this is not to use MP3 files as your source files. WAV, FLAC or M4A files don't have this problem.

MP3s aren't made up of fixed-rate samples, so cropping out a section of an arbitrary length will not work as you expect. Unless the encoder was smart (like lame), there will often be a gap at the start or end of the MP3 file's audio. I did a test with a sample 0.98s long (which is precisely 73½ CDDA frames, and many MP3 encoders use frames for minimum sample lengths). I then encoded the sample with three different MP3 encoders (lame, sox, and the ancient shine), then decoded those files with three decoders (lame, sox, and madplay). Here's how the sample lengths compare to the original:

 Enc.→Dec.          Length     Samples  CDDA Frames
 -----------------  ---------  -------  -----------
 shine→lame         0.95"      42095    71.5901
 shine→madplay      0.97"      42624    72.4898
 shine→sox          0.97"      42624    72.4898
 lame→lame          0.98"      43218    73.5000
*Original           0.98"      43218    73.5000
 sox→sox            0.99"      43776    74.4490
 sox→lame           1.01"      44399    75.5085
 lame→madplay       1.02"      44928    76.4082
 lame→sox           1.02"      44928    76.4082
 sox→madplay        1.02"      44928    76.4082

Only the file encoded and decoded by lame ended up the same length (mostly because lame inserts a length tag to correct for these too-short samples, and knows how to decode it). Everything encoded by sox ended up with a tiny gap, no matter what decoder I used. So joining the files will result in tiny clicks.

Your browser is likely mixing and overlapping the source files very slightly so you don't hear the clicks. Gapless playback is hard to do correctly.


This is my guess for your issue:

  • sox does not add time gap during concatenation,
  • however it add time-gap in other operations, for instance if you do a conversion before the concatenation.

To find out what happens I suggest you to check all durations of your files at each time (you can use soxi for instance) to see what's going on.

If it doesn't work (the time-gap is added during concatenation), let me please do another guess:

  • Sox add time gap because your samples at the beginning or at the end of the file are not close to zero.

To solve this, you could use very short fade-in an fade-out on you files.

Moreover, to force sox to output files with a well-defined length, you could use the trim parameter like this:

sox filein.mp3 trim 0 duration fileout.mp3

First you need really check if the start and the end of your files has no silences, i dont know if sox can do it but you need check the energy(rms, dB) of the start and end audio signals and cut start and end silence, to join audio files without gaps you need apply one window function in your signal to works like a fadein/fadeout and then crossfade the beginning of one with the end of the other.

sox provide a splice function to crossfade:

splice [−h|−t|−q] { position[,excess[,leeway]] }
Splice together audio sections. This effect provides two things over simple audio concatenation: a (usually short) cross-fade is applied at the join, and a wave similarity comparison is made to help determine the best place at which to make the join.

Check Documentation here