Only showing results from stackoverflow.com

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.

Answer from scruss on Stack Overflow
Top answer
1 of 3
12

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.

2 of 3
6

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
🌐
Stack Overflow
stackoverflow.com › questions › 31901554 › sox-what-is-the-correct-method-to-trim-pipe-and-concatenate-audio-files-togeth
SoX: What is the correct method to trim, pipe and concatenate audio files together? - Stack Overflow
April 27, 2017 - The following code was used: sox "|sox audio.wav -p trim 0s =661500s" "|sox audio.wav -p trim 661500s" concatenated.wav · Wave file sample rate is 44100hz. Sample size is 16 bit.
🌐
Stack Overflow
stackoverflow.com › questions › 36645542 › sox-couple-actions-at-once
audio processing - Sox - couple actions at once - Stack Overflow
sox --combine mix first.mp3 second.mp3 -p | sox - --combine concatenate third.mp3 -p | sox - --combine concatenate fourth.mp3 output.mp3 ... Find the answer to your question by asking.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 40619604 › concatenate-single-file-with-multiple-files-using-sox
windows - Concatenate single file with multiple files using sox - Stack Overflow
I've also seen the use of : newfile, restart and %n to direct Sox to create a new file for each time an effect runs. I'm just not clear on how I can use these to accomplish my goal of automating the task of concatenation of a pair of files as I indicated. 2016-11-15T23:43:40.7Z+00:00 ... @ECHO OFF &SETLOCAL for /F %%x in ('dir /B audiotts*.wav') do call set "Myvar=%%Myvar%% %%x" sox -S %Myvar% result-merged.wav ECHO Done merge %Myvar% pause
🌐
Stack Overflow
stackoverflow.com › questions › 20897795 › merge-audio-files
php - Merge audio files - Stack Overflow
January 3, 2014 - Copyshell_exec('/usr/bin/sox 1.wav 2.wav combined.wav '); It work fine and generate combined audio file.
🌐
Stack Overflow
stackoverflow.com › questions › 17810292 › appending-audio-files-with-different-channel-counts-using-sox
ffmpeg - Appending audio files with different channel counts using sox - Stack Overflow
July 23, 2013 - For those looking for the solution: I converted the single channel audio to two channel audio using ffmpeg like "ffmpeg -i singleChannel.mp3 -ac 2 2Channel.mp3" and then appended using sox like "sox 2Channel.mp3 input.mp3 output.mp3" ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... The 2026 Developer Survey is now open (for human developers... Code isn’t the only thing causing your production... ... Attribute table does not reflect changes with script opened from file or added to the toolbox in QGIS Processing script
🌐
Stack Overflow
stackoverflow.com › questions › 55136464 › merge-multiple-raw-file-into-single-wav-file-using-sox
audio - Merge multiple .raw file into single wav file using Sox - Stack Overflow
You need to specify all input file parameters per file (as RAW obviously does not have a header). Your initial guess was same as mine: that -r 16000 -b 16 -c 1 -e signed-integer applies to both file1.raw and file2.raw. It doesn't. ... sox -r 16000 -b 16 -c 1 -e signed-integer file1.raw -r 16000 -b 16 -c 1 -e signed-integer file2.raw out.wav