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

Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. 10 Awesome Examples for Viewing Huge Log Files in Unix
  2. The Ultimate Wget Download Guide With 15 Awesome Examples
  3. Daddy, I found it!, 15 Awesome Linux Find Command Examples (Part2)
  4. How To Convert Files To Different Formats Without Using Software
  5. Compress, Encrypt, Split and Transport Big Files Safely
  

Vim 101 Hacks Book

{ 12 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.

Leave a Comment

Previous post:

Next post: