SoX a cross-platform command line audio utility tool, the name stands for Sound eXchange. SoX is very handy if you need to do some simple tasks with music or audio files right from the terminal. To be more specific, SoX is usually used for converting and edit audio files; changing audio attributes; adding audio effects and other advanced sound manipulation features.
SoX is available in the repository of all popular Linux distros so installing it is very simple. If you are using Ubuntu or the like distros, the command to install SoX is:
sudo apt-get install sox
To get the complete understanding of how to use SoX, you should check its official documentation page. But in this article, I will enumerate several common examples of how SoX can be used:
- Combine multiple audio files into a single file:
For example, if you have 2 audio files, file1.wav and file2.wav, the command to combine these two files is:
sox -m file1.wav file2.wav output.wav
Or instead of using the -m option, you can also use "soxmix":
soxmix file1.wav file2.wav output.wav
- Copy-Paste a part of an audio file into another file:
The syntax to extract a part of an audio file to the output file is:
sox file.wav output.wav trim [start time in seconds] [duration in seconds]
For example, the command below will copy paste a part of an audio file that starts in second #90 and last for 2 minutes:
sox input.wav output.wav trim 90 120
- Play an audio file backward:
One interesting feature of SoX is that it can be used to play a song backward, the command will be:
sox input.wav output.wav reverse
Or you can use the command "play":
play input.wav reverse
Note: you can use SoX to play audio files normally if you dont use the "reverse" option.
- Change the sample rate of an audio file:
The sample rate is in samples per second. For example, 44100/8000. To change the sample rate with SoX, the command you can use is:
sox input.wav -r 8000 output.wav
Just change 8000 to get another sample rate.
- Convert mono audio to stereo and vice versa:
The "-c" option is used to convert mono audio to multi channels. For example, to convert a mono audio file into stereo with 3 channels, the command is
sox mono.wav -c 3 stereo.wav
Converting a stereo file into mono is more complicated since you will have to determine the channels. This is done by using the avg effect with an option indicating what channel to use. The options are "-l" for left, "-r" for right, "-f" for front, and "-b" for back. Here is how you extract the back channel into a mono audio file:
sox stereo.wav -c 1 mono.wav avg -b
- Change the speed of an audio file:
To speed up or slow down the sound of a file, use speed to modify the pitch and the duration of the file. This raises the speed and reduces the time.
The syntax to change the speed of an audio is:
sox input.wav output.wav speed factor-value
The default factor value is 1.0 which makes no change to the audio. 2.0 doubles speed, thus time length is cut by a half and pitch is one interval higher.
For example, to triple up an audio, the command will be:
sox input.wav output.wav speed 3.0