SoX reads and writes audio files in most popular formats and can optionally apply effects to them. In this tutorial you will learn how to use it.

It has a lot of features but it can be as simple as:

sox input.wav output.mp3
  • This converts a WAV file to MP3.

Table of Contents

Info about an audio file

soxi outputs file info such as sample rate, bitrate, encoding or file size.

soxi input.mp3

Player

SoX also includes an audio player that works inside the command line: play.

play input.wav

Combining multiple audios into one

You can simply add audio files in order before the output filename.

sox i1.mp3 i2.mp3 i3.mp3 output.mp3

Effects

SoX allows to add several effects to the audio files. These effects must be specified after the output filename. The following is a small list of available effects (check SoX man page for more).

  • speed <factor>: adjust the audio speed (and its pitch). A factor of more than 1 increases the speed, and less than 1 slows down.
    sox input.mp3 output.mp3 speed 1.25
    
  • tempo <factor>: adjust the audio speed but not its pitch. <factor> meaning is the same as in speed.
  • rate <rate>: change audio sampling rate.
    sox input.mp3 output.mp3 rate 48k
    
  • gain <dB>: apply amplification or attenuation to the audio signal.
  • trim <position>: cuts portions out of the audio.
    # Copy the first 20 seconds
    sox input.mp3 output.mp3 trim 0 20
    
    # Copy from 0:20 to 0:40
    sox input.mp3 output.mp3 trim 00:20 =00:40
    
    # Copy 30 seconds from 00:10
    sox input.mp3 output.mp3 trim 00:10 00:30
    
    # Copy from 00:10 until the end
    sox input.mp3 output.mp3 00:10
    
  • channels <number>: change the number of channels in the audio signal, mixing them if decreasing the number or duplicating if increasing.
  • fade <fade in length> [<final stop position> [<fade out length>]]: adds a fade effect to the beginning and/or to the end.
    # Add a 3 seconds fade-in
    sox input.mp3 output.mp3 fade 3
    
    # Add a 3 seconds fade-out
    sox input.mp3 output.mp3 fade 0 -0 3
    
    # End audio at 00:30, add a 3 seconds fade-in and a 3 seconds fade-out
    sox input.mp3 output.mp3 fade 3 00:30 3
    
  • noiseprof and noisered: Noise reduction. It works in two steps: profiling and reduction. You can run these steps in one or two lines.
    # Select a portion of the audio that should contain silence but actually contains noise.
    # In this example, I've selected 2 seconds from the second 1 of the audio. Then, apply the noise reduction.
    sox input.mp3 -n trim 1 2 noiseprof | sox input.mp3 out.mp3 noisered
    
    # Same in two lines
    sox input.mp3 -n trim 1 2 noiseprof input.noise-profile
    sox input.mp3 out.mp3 noisered input.noise-profile
    
    • You can add the amount of noise reduction by appending to the noisered command a number between 0 and 1 (from less to more noise reduction). The default is 0.5. Remember to add - before the number if you use the one-line command.
      sox input.mp3 -n trim 1 2 noiseprof | sox input.mp3 out.mp3 noisered - 0.7
      
Test with this online terminal: