15 Awesome Examples to Manipulate Audio Files Using Sound eXchange (SoX)

by SelvaGaneshan S on May 7, 2009

Music and Audio File Manipulation From Command Line
Photo courtesy of sean dreilinger


This article is part of the on-going Software for Geeks series. SoX stands for Sound eXchange. SoX is a cross-platform command line audio utility tool that works on Linux, Windows and MacOS. It is very helpful in the following areas while dealing with audio and music files.

  • Audio File Converter
  • Editing audio files
  • Changing audio attributes
  • Adding audio effects
  • Plus lot of advanced sound manipulation features


In general, audio data is described by following four characteristics:

  1. Rate – The sample rate is in samples per second. For example, 44100/8000
  2. Data size - The precision the data is stored in.  For example, 8/16 bits
  3. Data encoding - What encoding the data type uses. For example, u-law,a-law
  4. Channels – How many channels are contained in the audio data.  For example, Stereo 2 channels

SoX supports over 20 audio file formats. To get the list of all supported formats, execute sox -h from the command line. One of the major benefits of a command-line audio/music tool is easy to use in scripts to perform more complex tasks in batch mode.

All the 15 examples mentioned below can be used to manipulate Audio files on Unix, Windows and MacOS. Make sure to download the corresponding SoX utility for your platform from the SoX – Sound eXchange download page.

1. Combine Multiple Audio Files to Single File

With the -m flag, sox adds two input files together to produce its output. The example below adds first_part.wav and second_part.wav leaving the result in whole_part.wav. You can also use soxmix command for this purpose.

$ sox -m first_part.wav second_part.wav whole_part.wav

(or)

$ soxmix first_part.wav second_part.wav whole_part.wav

2. Extract Part of the Audio File

Trim can trim off unwanted audio from the audio file.

Syntax : sox old.wav new.wav trim [SECOND TO START] [SECONDS DURATION].
  • SECOND TO START – Starting point in the voice file.
  • SECONDS DURATION – Duration of voice file to remove.

The command below will extract first 10 seconds from input.wav and stored it in output.wav

$ sox input.wav output.wav trim 0 10

3. Increase and Decrease Volume Using Option -v

Option -v is used to change (increase or decrease ) the volume.

Increase Volume

$ sox -v 2.0 foo.wav bar.wav

Decrease Volume

If we need to lower the volume on some files, we can lower them by using negative numbers. Lower Negative number will get more soft . In the following example, the 1st command (-0.5) will be louder than the 2nd command (-0.1)

$ sox -v -0.5 srcfile.wav test05.wav

$ sox -v -0.1 srcfile.wav test01.wav

4. Get Audio File Information

The stat option can provide lot of statistical information about a given audio file. The -e flag tells sox not to generate any output other than the statistical information.

$ sox foo.wav -e stat
Samples read: 3528000
Length (seconds): 40.000000
Scaled by: 2147483647.0
Maximum amplitude: 0.999969
Minimum amplitude: -1.000000
Midline amplitude: -0.000015
Mean norm: 0.217511
Mean amplitude: 0.003408
RMS amplitude: 0.283895
Maximum delta: 1.478455
Minimum delta: 0.000000
Mean delta: 0.115616
RMS delta: 0.161088
Rough frequency: 3982
Volume adjustment: 1.000

5. Play an Audio Song

Sox provides the option for playing and recording sound files. This example explains how to play an audio file on Unix, Linux. Playing a sound file is accomplished by copying the file to the device special file /dev/dsp. The following command plays the file music.wav: Option -t specifies the type of the file /dev/dsp.

$ sox music.wav -t ossdsp /dev/dsp

You can also use play command to play the audio file as shown below.

Syntax :play options Filename audio_effects

$ play -r 8000 -w music.wav

6. Play an Audio Song Backwards

Use the ‘reverse’ effect to reverse the sound in a sound file. This will reverse the file and store the result in output.wav

$ sox input.wav output.wav reverse

You can also use play command to hear the song in reverse without modifying the source file as shown below.

$ play test.wav reverse

7. Record a Voice File

‘play’ and ‘rec’ commands are companion commands for sox . /dev/dsp is the digital sampling and digital recording device. Reading the device activates the A/D converter for sound recording and analysis. /dev/dsp file works for both playing and recording sound samples.

$ sox -t ossdsp /dev/dsp test.wav

You can also use rec command for recording voice. If SoX is invoked as ‘rec’ the default sound device is used as an input source.

$ rec -r 8000 -c 1 record_voice.wav

8. Changing the Sampling Rate of a Sound File

To change the sampling rate of a sound file, use option -r followed by the sample rate to use, in Hertz. Use the following example, to change the sampling rate of file ‘old.wav’ to 16000 Hz, and write the output to ‘new.wav’

$ sox old.wav -r 16000 new.wav

9. Changing the Sampling Size of a Sound File

If we increase the sampling size , we will get better quality. Sample Size for audio is most often expressed as 8 bits or 16 bits. 8bit audio is more often used for voice recording.

  • -b Sample data size in bytes
  • -w Sample data size in words
  • -l Sample data size in long words
  • -d Sample data size in double long words

The following example will convert 8-bit audio file to 16-bit audio file.

$ sox -b input.wav -w output.wav

10. Changing the Number of Channels

The following example converts mono audio files to stereo.  Use Option -c to specify the number of channels .

$ sox mono.wav -c 2 stereo.wav

There are methods to convert stereo sound files to mono sound.  i.e to get a single channel from stereo file.

Selecting a Particular Channel

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.  Following example will extract the left channel

$ sox stereo.wav -c 1 mono.wav avg -l

Average the Channels

$ sox stereo.wav -c 1 mono.wav avg

11. Audio Converter – Music File Format Conversion

Sox is useful to convert one audio format to another. i.e from one encoding (ALAW, MP3) to another. Sox can recognize the input and desired output formats by parsing the file name extensions . It will take infile.ulaw and creates a GSM encoded file called outfile.gsm. You can also use sox to convert wav to mp3.

$ sox infile.ulaw outfile.gsm

If the file doesn’t have an extension in its name , using ‘-t’ option we can express our intention . Option -t  is used to specify the encoding type .

$ sox -t ulaw infile -t gsm outfile

12. Generate Different Types of Sounds

Using synth effect we can generate a number of standard wave forms and types of noise. Though this effect is used to generate audio, an input file must still be given, ‘-n’ option is used to specify the input file as null file .

$ sox -n synth len type freq
  • len – length of audio to synthesize. Format for specifying lengths in time is hh:mm:ss.frac
  • type is one of sine, square, triangle, sawtooth, trapezium, exp, [white]noise, pinknoise, brown-
    noise. Default is sine
  • freq – frequencies at the beginning/end of synthesis in Hz

The following example produces a 3 second 8000 kHz, audio file containing a sine-wave swept from 300 to 3300 Hz

$ sox -r 8000 -n output.au synth 3 sine 300-3300

13. Speed up the Sound in 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 default factor 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.

Syntax: sox input.wav output.wav speed factor

$ sox input.wav output.wav speed 2.0

14. Multiple Changes to Audio File in Single Command

By default, SoX attempts to write audio data using the same data type, sample rate and channel count as per the input data. If the user wants the output file to be of a different format then user has to specify format options. If an output file format doesn’t support the same data type, sample rate, or channel count as the given input file format, then SoX will automatically select the closest values which it supports.

Converting a wav to raw. Following example convert sampling rate , sampling size , channel in single command line .

$ sox -r 8000 -w -c 1 -t wav source -r 16000 -b -c 2 -t raw destination

15. Convert Raw Audio File to MP3 Music File

There is no way to directly convert raw to mp3 file because mp3 will require compression information from raw file . First we need to convert raw to wav. And then convert wav to mp3.  In the exampe below, option -h indicates high quality.

Convert Raw Format to Wav Format:

$ sox -w -c 2 -r 8000 audio1.raw audio1.wav

Conver Wav Format to MP3 Format:

$ lame -h audio1.wav audio1.mp3


This article was written by SelvaGaneshan. S He is working at bk Systems (p) Ltd, and interested in contributing to the open source. The Geek Stuff welcomes your tips and guest articles


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 33 comments… read them below or add one }

1 Shanker Bakshi May 8, 2009 at 1:37 am

Gulp Gulp Gulp – heck mixing audio with codes, Ma.tt you there – change it to Code is symphony”

2 sasikala May 10, 2009 at 11:11 pm

Thanks for the great article.

3 Rog May 28, 2009 at 10:11 pm

“If we increase the sampling size , we will get better quality” – this is somewhat misleading, nothing can magically add information to an 8-bit source file to make it sound better when converted to 16-bit.

Great summary otherwise. sox can do some amazing stuff.

4 Adnan June 18, 2009 at 4:37 am

Nice article. It helped me alot. Thanks

5 Anonymous July 24, 2009 at 2:56 am

i cannot working -e stat, is that correct?

6 Myke Carter August 15, 2009 at 2:12 am

I often digitally remaster old cassette tapes using sox and audacity. Frequently, one channel of a tape recording is 1-3 dB louder than the other. For years, I have used sox to split my stereo cassette transfers into two mono WAVs – one for each channel. This enables me to work with each channel independently – to balance their loudness levels, for instance. What I have never learned to do is rejoin these modified L/R mono WAVs back into a single stereo WAV. For that, I have always had to use Audacity. Is it possible to do this with sox? If so, how?

7 Myke Carter September 1, 2009 at 11:16 pm

With regard to part 10 (above) and changing the number of channels…

The “avg” effect for sox seems to have recently disappeared. About 3 months ago I upgraded my Ubuntu Linux distribution and the “upgraded” version of sox that is packaged with it fails to run my old scripts which make use of the “avg” effect. There is no mention of the “avg” effect in my sox man page either.

Anyone know how to achieve the same effect without “avg” which “avg” is supposed to produce?

8 Phil G January 7, 2010 at 5:53 pm

The code in “10. Changing the Number of Channels”

doesn’t work for me. I keep getting:

“sox FAIL formats: can’t open input file `mono.wav’: No such file or directory”

when I run the following:

sox stereo.wav -c 1 mono.wav avg

(I’ve confirmed that my stereo file is indeed stereo. Using SoX v14.3.0 )

9 selva January 8, 2010 at 2:13 am

Phil G

I have used SoX v14.0.0 version in Ubuntu . In that version ‘avg’ effect is deprecated .

so used the following command line argument
sox stereo.wav -c 1 mono.wav

It is working for me . I tried with wav file .

10 Dan Milward January 13, 2010 at 4:17 pm

Would it be possible to make a web based tool for trimming MP3 files. I want to make a preview file but I want to be able to select which bit of the file. It would have to be done using PHP and jQuery. (we’d probably want to make it work with our WP e-Commerce Plugin).

Currently we use SoX to automatically create a preview file from the first 30 seconds of any sound file. But that sucks if the first 30 seconds is musical crud.

Best,
Dan

11 Ian Vaughan March 4, 2010 at 7:11 am

Recoding : Do you know I could record “what I hear”?
As in, if I play a internet stream, I want to be able to record it!
Thanks, Ian

12 Chris March 10, 2010 at 10:00 am

> Posts tagged as: Wma to Wav Converter

Surely some mistake? SoX does not support WMA.

13 rakesh May 6, 2010 at 6:18 am

Hello ,

Is SOX support to convert any music file to ringtone extenstion like midi,mmf,aac ,poliphonic ringtone,mono pholic ring tone etc.
please help on that point

Thanks in advanced

14 Gib May 17, 2010 at 11:21 am

sox foo.wav -e stat -> Will not work on the latest versions of sox.

Must use -n instead of -e e.g.
sox foo.wav -n stat

15 roy7 August 18, 2010 at 10:46 am

Ian Vaughan, you can do that with Audacity. I think it’s in their FAQ.

16 Marios September 29, 2010 at 10:57 am

Phil G

avg has been deprecated but has been replaced by mixer.

To get the left channel from stereo try:
sox stereo.wav -c 1 mono.wav mixer -l

for more info look at the mixer notes here:
http://sox.sourceforge.net/sox.html

17 anon November 21, 2010 at 11:40 am

Thanks for the tutorial, also thanks Marios for the update about ‘avg’…!

18 Rob January 6, 2011 at 4:02 pm

Need some help. I am trying to execute the soxmix command from a php call, but it does nothing… can you show me how to do it?

$output = array();
$result = -1;
exec(‘/usr/bin/soxmix test/exports/audiotest1.wav audio/003-half.wav test/exports/mixedaudio.wav’, $output, $result);
var_dump($output, $result);

19 daniel February 9, 2011 at 8:04 am

wow, nice article…

although i didnt find a reference to the general “problem” of combining 2 audio file, one is voice and the other is background music, in a way that the volume of the music is reduced when the voice is “speaking”…

is it possiable to perform using SoX?

thanks alot

Daniel

20 Jim Hoadley April 26, 2011 at 4:52 pm

Your instructions for decreasing the volume are incorrect. Positive numbers less the 1 decrease the volume. You can make those numbers negative, but that will just invert the phase; it has no effect on volume. I would suggest you replace your examples with these:

$ sox -v 0.5 srcfile.wav test05.wav
$ sox -v 0.1 srcfile.wav test01.wav

The first command reduces the gain by 50%, the second by 90%.

Feel free to check the sox man page to verify my comments. Thanks!

21 ted January 19, 2012 at 3:03 pm

fyi- example #4 does not work.
i have SoX installed on Mac OSX.

22 tom January 20, 2012 at 3:02 pm

How can I get Sox to play from a point? For example, begin playing at two seconds in. I bet I can create a new file with the audio i want and play that, but it seems obtuse

23 ram March 31, 2012 at 10:01 pm

can sox return length of audio

24 Max Waterman May 31, 2012 at 7:35 am

NB to anyone looking at this “these days” : since this was written, there have been many changes to sox, so you need to look at how to do it with the latest version, using the man page or whatever. For example, I want to convert from 2 channels to 1 channel, mixing both together – it seems it is as simple as ‘$ sox inputfile.mp3 outputfile.mp3 remix -’ – note the final ‘-’ (easy to miss). I expect all the other examples have equivalents in the new sox, if they don’t work as they are.

25 rudy trubitt June 20, 2012 at 5:51 pm

regarding the use of the stat or stats command–I also could not make the example work, but eventually figured out this alternative:

sox YourInputFilename -n stats

The -n command means “null output”, in other words, just provide the stats and don’t make a new audio file.

26 Christopher Stevens May 20, 2013 at 9:59 pm

I’ve been looking for a good command line audio editor. Thanks for the great SoX intro. :D

27 Rajat October 30, 2013 at 8:19 pm

Helpful article. One thing I would like to know is how to record audio for a specific duration say 1 second?

28 Bob Taylor November 16, 2013 at 3:14 pm

I have been really trying to find out why I can’t run SOX from a remote directory.
I have SOX in directory A
I’m in directory B and the wav files are in B.
my command line is: C:\A\sox.exe -v 1.8 number1.wav number2.wav
But the command won’t work.. it just keeps failing and saying it can’t find the directory or the command is invalid.
What am I missing.. is there another thing to consider?

29 Asha November 19, 2013 at 7:18 am

How to convert nist sphere file to .wav file using sox

30 Tom November 19, 2013 at 6:30 pm

Bob Taylor- you probably have to pass in c:\B\number1.wav

31 Bob Taylor November 19, 2013 at 6:43 pm

nope.. tried that..

32 valeriancafe June 13, 2014 at 4:20 pm

Im trying to do a Audio-DVD ( DVD-video ) I found how to convert one file to Lpcm wav
” sox name.flac -b 24 name.wavpcm rate -v -s -I 96k ” then I renamed it .wav and moved the file over to Window 7 and used a program called lplex and it made the iso file I tested it in a DVD player and it worked, but I would like to do this in command Iine
linux, it seem faster for some reason. So does any body now how to do 25 flac files in a folder one after another ????

33 Sree Visu July 4, 2014 at 3:02 am

Thank you! You saved my time

Leave a Comment

Previous post:

Next post: