Reducing/removing clipping in SoX when converting the sample rate

cesar picture cesar · Jul 30, 2011 · Viewed 26.1k times · Source

I'm using SoX to trim a set of wav files into 16kHz, 16bit, mono channel wav files (which will be subsets of one of the initial wav files). Most of the source wav files are already set to this specification, however, I just found out that some of them have different sample rates. Since it's going to be automated in Java using a ProcessBuilder, I figured I could use the following command:

sox <source_wav> -b 16 <dest_wav> channels 1 rate 16000 trim <startTime> =<endTime>

and it'll only change the sample rate if it isn't 16000 Hz. It does what it's supposed to on files with the same specification, but on files with different sample rates, I get:

sox WARN rate: rate clipped 48 samples; decrease volume?
sox WARN dither: dither clipped 44 samples; decrease volume?

How should I deal with this without degrading the quality of the audio? Note that I don't know anything about signal processing.

Answer

user871634 picture user871634 · Jul 31, 2011

As suggested by the tool, try reducing the volume slightly, e.g. by preceding with -v 0.99 (or 0.98 etc.). Such small changes in volume are imperceptible.

Example:

sox -v 0.99 <source_wav> -b 16 <dest_wav> channels 1 rate 16000 trim <startTime> =<endTime>

If you still get clipping then the audio is likely severely clipped (i.e. disorted) to begin with (this is common with modern music; see Wikipedia: Loudness war) and so the warnings can be ignored—no additional distortion is being introduced.

As mentioned in the comments, the -G option can be given which will automatically make any adjustment to the volume needed to avoid clipping (at the expense of a little extra CPU time, i.e. it runs slightly slower with -G).