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:
- Rate – The sample rate is in samples per second. For example, 44100/8000
- Data size – The precision the data is stored in. For example, 8/16 bits
- Data encoding – What encoding the data type uses. For example, u-law,a-law
- 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
Comments on this entry are closed.
Gulp Gulp Gulp – heck mixing audio with codes, Ma.tt you there – change it to Code is symphony”
Thanks for the great article.
“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.
Nice article. It helped me alot. Thanks
i cannot working -e stat, is that correct?
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?
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?
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 )
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 .
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
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
> Posts tagged as: Wma to Wav Converter
Surely some mistake? SoX does not support WMA.
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
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
Ian Vaughan, you can do that with Audacity. I think it’s in their FAQ.
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
Thanks for the tutorial, also thanks Marios for the update about ‘avg’…!
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);
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
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!
fyi- example #4 does not work.
i have SoX installed on Mac OSX.
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
can sox return length of audio
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.
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.
I’ve been looking for a good command line audio editor. Thanks for the great SoX intro. 😀
Helpful article. One thing I would like to know is how to record audio for a specific duration say 1 second?
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?
How to convert nist sphere file to .wav file using sox
Bob Taylor- you probably have to pass in c:\B\number1.wav
nope.. tried that..
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 ????
Thank you! You saved my time
Thanks, I’m using it with soundKonverter and works fine!!
Thanks! I use SoX to convert mp3 files to PCMA and PCMU (like gsm format).
And one thing i want to say. If you cannot convert mp3 files, install this package:
sudo apt-get install libsox-fmt-mp3
I used sox to cut mp3 files. They become ring tones for my Nokia N70.
Great!
rec file.wav
play file.wav
lame -h file.wav FILE.mp3
Thank you for this article. I have a question on how it might be possible to integrate this on another program and use it to convert raw to wav sound files. Example, how to use this with Visual Studio, Xcode, etc and use this code with code in another language? Thanks.
Is there a way to trim only parts over a certain level ?
sox -V -S -b32 inputfile.mp3 outputfile.wv norm -3 rate -vMa 28224k rate -vMa 192k
simple example for upsample and harmonize, allow aliasing for higher harmonics growing, distortion of aliasing phenomena is reduced trought very high intermodulate rate and this technique produce best sound coloring what i ever hear