SoX

From Media Design: Networked & Lens-Based wiki
(Redirected from Sox)
Jump to navigation Jump to search
Website http://sox.sourceforge.net/
License GPL
OS GNU/Linux, OS X, Windows
Media Audio
Format AIFF
Interface Command-line interface,
Wikipedia http://en.wikipedia.org/wiki/SoX

Installation

Mac/Windows

Specific installers for Mac & Windows can be found here.

Debian/Linux

apt-get install sox

Testing if sox is installed

sox installs the following commands:

  • sox
  • play
  • rec

Typing one of them should be a quick way to test if sox is installed. To check the version:

sox --version

sox is a handy command-line tool for manipulating, filtering, and generating audio.

Help

Actually, there's quite a bit of help available from sox itself, it's just a little spread out, try one or all of the following:

man sox
man soxexam
sox
sox --help-effect all


In case they're not installed, you can also check out the man pages online: sox soxexam

Displaying information about an audio file

sox -V clap.wav -n


displays in my case...

sox: SoX v14.0.1

Input File     : 'clap.wav'
Sample Size    : 16-bit (2 bytes)
Sample Encoding: signed (2's complement)
Channels       : 1
Sample Rate    : 44100
Duration       : 00:00.50 = 22050 samples = 37.5 CDDA sectors
Endian Type    : little
Reverse Nibbles: no
Reverse Bits   : no


Or use the "stat" effect:

sox clap.wav -e stat


displays

Samples read:             22050
Length (seconds):      0.500000
Scaled by:         2147483647.0
Maximum amplitude:     0.999969
Minimum amplitude:    -0.999878
Midline amplitude:     0.000046
Mean    norm:          0.015108
Mean    amplitude:     0.000009
RMS     amplitude:     0.047986
Maximum delta:         0.552368
Minimum delta:         0.000000
Mean    delta:         0.005487
RMS     delta:         0.017184
Rough   frequency:         2513
Volume adjustment:        1.000


Generating tones or noise

Generating a tone & playing it immediately:

play -c1 -r8000 -n synth 0.25 sine 220 vol 0.7
play -c1 -r8000 -n synth 0.25 noise vol 0.7


Using -n removes the need for an input file.

sox -c1 -r8000 -n beep.wav synth 0.25 sine 220 vol 0.7
sox -c1 -r8000 -n sweep_up.wav synth 0.25 sine 220-440 vol 0.7
sox -c1 -r8000 -n -t wav sweep_down.wav synth 0.25 sine 440-220 vol 0.7

sox -c1 -r8000 -n ch.wav synth 0.25 noise vol 0.7
sox -c1 -r8000 -n sweep_up_long.wav synth 2.0 sine 220-1000 vol 0.7
sox -c1 -r8000 -n sweep_down_long.wav synth 2.0 sine 1000-220 vol 0.7

sox -c1 -r8000 -n bleep.wav synth 0.5 square 330 vol 0.7


To send output to stdout, in place of the filename, and specify your "--type" (since sox can't guess by the filename, here it's "wav"):

sox -c1 -r8000 -n -t wav - synth 0.25 sine 220 vol 0.7 > beep.wav


Generating "drum" hits in various colors, 0.2 secs with a quick decay, using stdout / file redirection.

sox -c2 -r44100 -n -t wav - synth 0.2 pinknoise vol 1.0 fade l 0 0.2 0.19 > pink.wav
sox -c2 -r44100 -n -t wav - synth 0.2 brownnoise vol 1.0 fade l 0 0.2 0.19 > brown.wav
sox -c2 -r44100 -n -t wav - synth 0.2 noise vol 1.0 fade l 0 0.2 0.19 > white.wav


Generating silence using a noise generator at zero volume (must be other ways to achieve the same):

sox -c2 -r44100 -n silence.wav synth 0.2 noise vol 0.0

melody with bend

play -n synth 60 sin 300 gain 1    bend 3,180,.25  6,20,.53  12,-520,.3

Pluck

play -n synth 1 pluck E2
sox -n e2.wav synth 1 pluck E2
for n in E2 A2 D3 G3 B3 E4
do
    # play -n synth 1 pluck $n
    sox -c2 -r44100 -n pluck_$n.ogg synth 1 pluck $n
done

Manipulations

changing the file speed

(does not preserve the duration)

sox In.ogg In_slow.ogg speed 0.45
sox In.ogg In_fast.ogg speed 1.5

Working with "raw audio"

Working with raw files allows audio to get appended together; here sox is used to take a bunch of wavs, make them raw, send them all to stdout, the repackage them as a wav file. In the examples listed below, the rate used is "CD-Quality" 44kHz, 16-bit stereo.

Converting a sound file to raw audio

sox source.wav --rate 44100 --bits 16 --channels 2 --encoding signed-integer source.raw


Converting raw back to audio

sox --rate 44100 --bits 16 --channels 2 --encoding signed-integer new.raw new.wav


Extracting audio

Use the trim filter. For example, to extract 1 second of audio starting at 0.250 secs from an OGG audio file, saving the result as out.wav:

sox defenders_dick_mka.ogg out.wav trim 00:00:00.250 00:00:01.000


In Linux, you can use the play command to "preview" your cut before you make it:

play in.wav out.wav trim 0.7

the to "perform it, simply change play to sox:

sox in.wav out.wav trim 0.7


Batch processing

(for file in *; do sox "$file" -t .raw -r 44100 -sw -c 2 -; done) | \
  sox -t .raw -r 44100 -sw -c 2 - -t .wav newfile.wav

source: [1]

Script to cat two wavs together using sox/raw format

#!/bin/sh
sox $1 -r 44100 -c 2 -s -2 /tmp/$$-1.raw
sox $2 -r 44100 -c 2 -s -2 /tmp/$$-2.raw
cat /tmp/$$-1.raw /tmp/$$-2.raw > /tmp/$$.raw
sox -r 44100 -c 2 -s -2 /tmp/$$.raw $3
rm /tmp/$$*.raw

[2]

(changed -w to -2 to work on mac, new version of sox?!)

Compand

 compand 0.3,1 6:-70,-60,-20 -5 -90 0.2

Source: https://sourceforge.net/p/sox/mailman/message/23427259/

Resources