Sox: Difference between revisions

From EMC23 - Satellite Of Love
Jump to navigation Jump to search
(Created page with "= SoX − Sound eXchange, the Swiss Army knife of audio manipulation = == SYNOPSIS == sox [global-options] [ format-options] infile1 [[format-options] infile2] . .. [format-op...")
 
mNo edit summary
Line 18: Line 18:
rec, t he default sound device is used as an input source. Additionally, t he soxi(1) command provides a con-
rec, t he default sound device is used as an input source. Additionally, t he soxi(1) command provides a con-
venient way to just query audio file header information.
venient way to just query audio file header information.
The  heart  of  SoX  is  a  library  called  libSoX. Those  interested  in  extending  SoX  or  using  it  in  other  pro-
The  heart  of  SoX  is  a  library  called  libSoX. Those  interested  in  extending  SoX  or  using  it  in  other  pro-
grams should refer to the libSoX manual page: libsox(3).
grams should refer to the libSoX manual page: libsox(3).
SoX is a command-line audio processing tool, particularly suited to making quick, simple edits and to batch
SoX is a command-line audio processing tool, particularly suited to making quick, simple edits and to batch
processing.  If you need an interactive, g raphical audio editor, u se audacity(1).
processing.  If you need an interactive, g raphical audio editor, u se audacity(1).
* * *
The overall SoX processing chain can be summarised as follows:
Input(s) → Combiner → Effects → Output(s)
Note  however,  that  on  the  SoX  command  line,  the  positions  of  the  Output(s)  and  the  Effects  are  swapped
w.r.t.  the  logical  flow j ust  shown. Note also  that  whilst  options  pertaining  to  files  are  placed  before  their
respective file name, the opposite is true for effects.  To s how h ow this works in practice, here is a selection
of examples of how S oX might be used. The simple
sox recital.au recital.wav
translates an audio file in Sun AU f ormat to a Microsoft WAV  fi le, whilst
sox recital.au −b 16 recital.wav channels 1 rate 16k fade 3 norm
performs the same format translation, but also applies four effects (down-mix to one channel, sample rate
change, fade-in, nomalize), and stores the result at a bit-depth of 16.
sox −r 16k −e signed −b 8 −c 1 voice-memo.raw voice-memo.wav
converts ‘raw’ (a.k.a. ‘headerless’) audio to a self-describing file format,
sox slow.aiff fixed.aiff speed 1.027
adjusts audio speed,
sox short.wav long.wav longer.wav
concatenates two a udio files, and
sox −m music.mp3 voice.wav mixed.flac
mixes together two a udio files.
play "The Moonbeams/Greatest/*.ogg" bass +3
plays a collection of audio files whilst applying a bass boosting effect,
play −n −c1 synth sin %−12 sin %−9 sin %−5 sin %−2 fade h 0.1 1 0.1
plays a synthesised ‘A m inor seventh’ chord with a pipe-organ s ound,
rec −c 2 radio.aiff trim 0 30:00
sox December 31, 2014 1
The soxi(1) command can be used to display information from audio file headers.
=== Determining & Setting The File Format ===
There are several mechanisms available for SoX to use to determine or set the format characteristics of an
audio file. Depending on the circumstances, individual characteristics may be determined or set using dif-
ferent mechanisms.
To  d etermine the format of an input file, SoX will use, in order of precedence and as given or a vailable:
1. Command-line format options.
2. The contents of the file header.
3. The filename extension.
To  s et the output file format, SoX will use, in order of precedence and as given or a vailable:
1. Command-line format options.
2. The filename extension.
3. The input file format characteristics, or the closest that is supported by the output file type.
For a ll files, SoX will exit with an error if the file type cannot be determined. Command-line format options
may need to be added or changed to resolve t he problem.
=== Playing & Recording Audio ===
The play and rec commands are provided so that basic playing and recording is as simple as
play existing-file.wav
and
rec new-file.wav
These two c ommands are functionally equivalent to
sox existing-file.wav −d
and
sox −d new-file.wav
Of course, further options and effects (as described below) can be added to the commands in either form.
* * *
Some  systems  provide  more  than  one  type  of  (SoX-compatible)  audio  driver,  e.g.  ALSA  &  OSS,  or
SUNAU  &  AO. S ystems can also have more than one audio device (a.k.a. ‘sound card’). If more than one
audio driver h as been built-in to SoX, and the default selected by SoX when recording or playing is not the
one  that  is  wanted,  then  the AUDIODRIVER environment  variable  can  be  used  to  override  the  default.
For e xample (on many s ystems):
set AUDIODRIVER=oss
play ...
The AUDIODEV environment variable can be used to override the default audio device, e.g.
set AUDIODEV=/dev/dsp2
play ...
sox ... −t oss
or
set AUDIODEV=hw:soundwave,1,2
play ...
sox ... −t alsa
Note that the way of setting environment variables varies from system to system—for some specific exam-
ples, see ‘SOX_OPTS’ below.
When playing a file with a sample rate that is not supported by the audio output device, SoX will automati-
cally  invoke  the rate effect  to  perform  the  necessary  sample  rate  conversion. For  compatibility  with  old
hardware, the default rate quality level i s s et to ‘low’. This can be changed by explicitly specifying the rate
effect with a different quality level, e.g.
play ... rate −m
or by using the −−play−rate−arg option (see below).
* * *
On some systems, SoX allows audio playback volume to be adjusted whilst using play. W here supported,
this is achieved by t apping the ‘v’ & ‘V’ keys d uring playback.
To  h elp  with  setting  a  suitable  recording  level,  SoX  includes  a  peak-level m eter  which  can  be  invoked
(before making the actual recording) as follows:
rec −n
The  recording  level s hould  be  adjusted  (using  the  system-provided  mixer  program,  not  SoX)  so  that  the
meter is at most occasionally full scale, and never ‘ in the red’ (an exclamation mark is shown).  See also −S
below.
=== Accuracy ===
Many fi le formats that compress audio discard some of the audio signal information whilst doing so. Con-
verting  to  such  a  format  and  then  converting  back  again  will  not  produce  an  exact  copy of t he  original
audio.  This is the case for many f ormats used in telephony ( e.g. A-law,  GSM) where low s ignal bandwidth
is more important than high audio fidelity, a nd for many f ormats used in portable music players (e.g. MP3,
Vo rbis)  where  adequate  fidelity  can  be  retained  even w ith  the  large  compression  ratios  that  are  needed  to
make p ortable players practical.
Formats that discard audio signal information are called ‘lossy’. Formats that do not are called ‘lossless’.
The  term  ‘quality’  is  used  as  a  measure  of  how c losely  the  original  audio  signal  can  be  reproduced  when
using a lossy format.
Audio file conversion with SoX is lossless when it can be, i.e. when not using lossy compression, when not
reducing the sampling rate or number of channels, and when the number of bits used in the destination for-
mat is not less than in the source format. E.g.  converting from an 8-bit PCM format to a 16-bit PCM for-
mat is lossless but converting from an 8-bit PCM format to (8-bit) A-law i sn’t.
N.B. SoX converts all audio files to an internal uncompressed format before performing any a udio process-
ing.  This  means  that  manipulating  a  file  that  is  stored  in  a  lossy  format  can  cause  further  losses  in  audio
fidelity. E .g. with
sox long.mp3 short.mp3 trim 10
SoX first decompresses the input MP3 file, then applies the trim effect, and finally creates the output MP3
file by re-compressing the audio—with a possible reduction in fidelity above that which occurred when the
input file was created. Hence, if what is ultimately desired is lossily compressed audio, it is highly recom-
mended to perform all audio processing using lossless file formats and then convert to the lossy format only
at the final stage.
N.B. Applying multiple effects with a single SoX invocation will, in general, produce more accurate results
than those produced using multiple SoX invocations.
=== Dithering ===
Dithering is a technique used to maximise the dynamic range of audio stored at a particular bit-depth. Any
distortion introduced by quantisation is decorrelated by adding a small amount of white noise to the signal.
In  most  cases,  SoX  can  determine  whether  the  selected  processing  requires  dither  and  will  add  it  during
output formatting if appropriate.
Specifically, by d efault, SoX automatically adds TPDF dither when the output bit-depth is less than 24 and
any of t he following are true:
• bit-depth reduction has been specified explicitly using a command-line option
• the output file format supports only bit-depths lower than that of the input file format
• an effect has increased effective bit-depth within the internal processing chain
For e xample,  adjusting  volume  with vol 0 .25 requires  two a dditional  bits  in  which  to  losslessly  store  its
results (since 0.25 decimal equals 0.01 binary). So if the input file bit-depth is 16, then SoX’s i nternal rep-
resentation will utilise 18 bits after processing this volume change. In order to store the output at the same
depth as the input, dithering is used to remove the additional bits.
Use  the −V option  to  see  what  processing  SoX  has  automatically  added.  The −D option  may  be  given t o
override  automatic  dithering. To  i nv oke  dithering  manually  (e.g.  to  select  a  noise-shaping  curve),  see  the
dither effect.
==== Clipping ====
Clipping is distortion that occurs when an audio signal level ( or ‘volume’) exceeds the range of the chosen
representation. In most  cases,  clipping  is  undesirable  and  so  should  be  corrected  by  adjusting  the  level
prior to the point (in the processing chain) at which it occurs.
In SoX, clipping could occur, as y ou might expect, when using the vol or gain effects to increase the audio
volume.  Clipping  could  also  occur  with  many o ther  effects,  when  converting  one  format  to  another, a nd
ev en w hen simply playing the audio.
Playing  an  audio  file  often  involves  resampling,  and  processing  by  analogue  components  can  introduce  a
small DC offset and/or amplification, all of which can produce distortion if the audio signal level w as ini-
tially too close to the clipping point.
For t hese reasons, it is usual to make s ure that an audio file’s s ignal level h as some ‘headroom’, i.e. it does
not  exceed  a  particular  level b elow t he  maximum  possible  level f or  the  given r epresentation. Some stan-
dards bodies recommend as much as 9dB headroom, but in most cases, 3dB (≈ 70% linear) is enough. Note
that this wisdom seems to have  been lost in modern music production; in fact, many C Ds, MP3s, etc. are
now m astered at levels above 0dBFS i.e. the audio is clipped as delivered.
SoX’s stat and stats effects can assist in determining the signal level in an a udio file. The gain or vol effect
can be used to prevent clipping, e.g.
sox dull.wav bright.wav gain −6 treble +6
guarantees that the treble boost will not clip.
If clipping occurs at any p oint during processing, SoX will display a warning message to that effect.
See also −G and the gain and norm effects.
Input File Combining
SoX’s i nput combiner can be configured (see OPTIONS below) to combine multiple files using any of t he
following  methods:  ‘concatenate’,  ‘sequence’,  ‘mix’,  ‘mix-power’,  ‘merge’,  or  ‘multiply’. The  default
method is ‘sequence’ for play, a nd ‘concatenate’ for rec and sox.
For a ll methods other than ‘sequence’, multiple input files must have the same sampling rate. If necessary,
separate SoX invocations can be used to make s ampling rate adjustments prior to combining.
If the ‘concatenate’ combining method is selected (usually, t his will be by default) then the input files must
also have the same number of channels. The audio from each input will be concatenated in the order given
to form the output file.
The ‘sequence’ combining method is selected automatically for play. I t i s s imilar to ‘concatenate’ in that
the audio from each input file is sent serially to the output file. However, here the output file may be closed
and  reopened  at  the  corresponding  transition  between  input  files.  This  may  be  just  what  is  needed  when
sending different types of audio to an output device, but is not generally useful when the output is a normal
file.
If either the ‘mix’ or ‘mix-power’ combining method is selected then two or m ore input files must be given
and will be mixed together to form the output file. The number of channels in each input file need not be
the same, but SoX will issue a warning if they a re not and some channels in the output file will not contain
audio from every input file. A m ixed audio file cannot be un-mixed without reference to the original input
files.
If the ‘merge’ combining method is selected then two or m ore input files must be given a nd will be merged
together to form the output file. The number of channels in each input file need not be the same. A m erged
audio  file  comprises  all  of  the  channels  from  all  of  the  input  files.  Un-merging  is  possible  using  multiple
invocations of SoX with the remix effect.  For example, two m ono files could be merged to form one stereo
file. The first and second mono files would become the left and right channels of the stereo file.
The ‘multiply’ combining method multiplies the sample values of corresponding channels (treated as num-
bers in the interval −1 to +1). If the number of channels in the input files is not the same, the missing chan-
nels are considered to contain all zero.
When  combining  input  files,  SoX  applies  any s pecified  effects  (including,  for  example,  the vol volume
adjustment effect) after the audio has been combined. However,  it is o ften useful to be able to set the vol-
ume of (i.e. ‘balance’) the inputs individually, b efore combining takes place.
For a ll  combining  methods,  input  file  volume  adjustments  can  be  made  manually  using  the −v option
(below) which can be given f or one or more input files. If it is given f or only some of the input files then the
others  receive  no v olume  adjustment. In  some  circumstances,  automatic  volume  adjustments  may  be
applied (see below).
The −V option  (below)  can  be  used  to  show t he  input  file  volume  adjustments  that  have  been  selected
(either manually or automatically).
There are some special considerations that need to made when mixing input files:
Unlike t he other methods, ‘mix’ combining has the potential to cause clipping in the combiner if no balanc-
ing is performed. In this case, if manual volume adjustments are not given, SoX will try to ensure that clip-
ping does not occur by automatically adjusting the volume (amplitude) of each input signal by a factor of
¹/n, w here  n  is  the  number  of  input  files. If  this  results  in  audio  that  is  too  quiet  or  otherwise  unbalanced
then  the  input  file  volumes  can  be  set  manually  as  described  above. U sing  the norm effect  on  the  mix  is
another alternative.
If mixed audio seems loud enough at some points but too quiet in others then dynamic range compression
should be applied to correct this—see the compand effect.
With the ‘mix-power’ combine method, the mixed volume is approximately equal to that of one of the input
signals.  This is achieved b y b alancing using a factor of ¹/√n instead of ¹/n. N ote that this balancing factor
does not guarantee that clipping will not occur, b ut the number of clips will usually be low a nd the resultant
distortion is generally imperceptible.
==== Output Files ====
SoX’s d efault behaviour is to take o ne or more input files and write them to a single output file.
This  behaviour  can  be  changed  by  specifying  the  pseudo-effect  ‘newfile’  within  the  effects  list. SoX  will
then enter multiple output mode.
In multiple output mode, a new fi le is created when the effects prior to the ‘newfile’ indicate they a re done.
The effects chain listed after ‘newfile’ is then started up and its output is saved to t he new fi le.
In multiple output mode, a unique number will automatically be appended to the end of all filenames. If the
filename has an extension then the number is inserted before the extension.  This behaviour can be custom-
ized by placing a %n anywhere in the filename where the number should be substituted. An optional num-
ber can be placed after the % to indicate a minimum fixed width for the number.
Multiple  output  mode  is  not  very  useful  unless  an  effect  that  will  stop  the  effects  chain  early  is  specified
before the ‘newfile’. If end of file is reached before the effects chain stops itself then no new fi le will be cre-
ated as it would be empty.
The  following  is  an  example  of  splitting  the  first  60  seconds  of  an  input  file  into  two 30 s econd  files  and
ignoring the rest.
sox song.wav ringtone%1n.wav trim 0 30 : newfile : trim 0 30
==== Stopping SoX ====
Usually  SoX  will  complete  its  processing  and  exit  automatically  once  it  has  read  all  available  audio  data
from the input files.
If desired, it can be terminated earlier by sending an interrupt signal to the process (usually by pressing the
keyboard interrupt key which is normally Ctrl-C). This is a natural requirement in some circumstances, e.g.
when  using  SoX  to  make a r ecording. Note that  when  using  SoX  to  play  multiple  files,  Ctrl-C  behaves
slightly differently: pressing it once causes SoX to skip to the next file; pressing it twice in quick succession
causes SoX to exit.
Another  option  to  stop  processing  early  is  to  use  an  effect  that  has  a  time  period  or  sample  count  to
determine  the  stopping  point.  The  trim  effect  is  an  example  of  this. Once  all  effects  chains  have  stopped
then SoX will also stop.
==== FILENAMES ====
Filenames can be simple file names, absolute or relative path names, or URLs (input files only). Note that
URL support requires that wget(1) is available.
Note:  Giving  SoX  an  input  or  output  filename  that  is  the  same  as  a  SoX  effect-name  will  not  work  since
SoX will treat it as an effect specification. The only work-around to this is to avoid such filenames. This is
generally not difficult since most audio filenames have a filename ‘extension’, whilst effect-names do not.
==== Special Filenames ====
The following special filenames may be used in certain circumstances in place of a normal filename on the
command line:
− SoX can be used in simple pipeline operations by using the special filename ‘−’ which, if used as
an input filename, will cause SoX will read audio data from ‘standard input’ (stdin), and which, if
used  as  the  output  filename,  will  cause  SoX  will  send  audio  data  to  ‘standard  output’  (stdout).
Note that when using this option for the output file, and sometimes when using it for an input file,
the file-type (see −t below) must also be given.
" | program [options] . .."
This can be used in place of an input filename to specify the the given p rogram’s s tandard output
(stdout) be used as an input file. Unlike − (above), this can be used for several inputs to one SoX
command.  For example, if ‘genw’ generates mono WAV  f ormatted signals to its standard output,
then the following command makes a stereo file from two g enerated signals:
sox −M "|genw −−imd −" "|genw −−thd −" out.wav
For h eaderless (raw) audio, −t (and perhaps other format options) will need to be given, preceding
the input command.
"wildcard-filename"
Specifies that filename ‘globbing’ (wild-card matching) should be performed by SoX instead of by
the shell. This allows a single set of file options to be applied to a group of files. For e xample, if
the current directory contains three ‘vox’ files, file1.vox, file2.vox, and file3.vox, then
play −−rate 6k *.vox
will be expanded by the ‘shell’ (in most environments) to
play −−rate 6k file1.vox file2.vox file3.vox
which will treat only the first vox file as having a
sample rate of 6k. With
play −−rate 6k "*.vox"
the given s ample rate option will be applied to all three vox files.
−p, −−sox−pipe
This can be used in place of an output filename to specify that the SoX command should be used
as in input pipe to another SoX command. For e xample, the command:
play "|sox −n −p synth 2" "|sox −n −p synth 2 tremolo 10" stat
plays two ‘ files’ in succession, each with different effects.
−p is in fact an alias for ‘−t sox −’.
−d, −−default−device
This can be used in place of an input or output filename to specify that the default audio device (if
one  has  been  built  into  SoX)  is  to  be  used. This  is  akin  to  invoking rec or play (as  described
above).
−−combine concatenate | merge | mix | mix−power | multiply | sequence
Select the input file combining method; for some of these, short options are available: −m selects
‘mix’, −M selects ‘merge’, and −T selects ‘multiply’.
See Input File Combining above for a description of the different combining methods.
−D, −−no−dither
Disable automatic dither—see ‘Dithering’ above. A n e xample of why t his might occasionally be
useful is if a file has been converted from 16 to 24 bit with the intention of doing some processing
on  it,  but  in  fact  no  processing  is  needed  after  all  and  the  original  16  bit  file  has  been  lost,  then,
strictly speaking, no dither is needed if converting the file back to 16 bit. See also the stats effect
for how to d etermine the actual bit depth of the audio within a file.
−−effects−file FILENAME
Use FILENAME to obtain all effects and their arguments.  The file is parsed as if the values were
specified on the command line. A n ew line can be used in place of the special : marker to separate
effect  chains. For c onvenience,  such  markers  at  the  end  of  the  file  are  normally  ignored;  if  you
want  to  specify  an  empty  last  effects  chain,  use  an  explicit : by  itself  on  the  last  line  of  the  file.
This option causes any e ffects specified on the command line to be discarded.
−G, −−guard
Automatically invoke the gain effect to guard against clipping. E.g.
sox −G infile −b 16 outfile rate 44100 dither −s
is shorthand for
sox infile −b 16 outfile gain −h rate 44100 gain −rh dither −s
See also −V, − −norm, and the gain effect.
−h, −−help
Show v ersion number and usage information.
−−help−effect NAME
Show u sage  information  on  the  specified  effect. The name all can  be  used  to  show u sage  on  all
effects.
−−help−format NAME
Show i nformation about the specified file format. The name all can be used to show i nformation
on all formats.
−−i, −−info
Only if given as t he first parameter to sox, behave as soxi(1).
−m | −M
Equivalent to −−combine mix and −−combine merge, respectively.
−−magic
If SoX has been built with the optional ‘libmagic’ library then this option can be given to e nable
its use in helping to detect audio file types.
−−multi−threaded | −−single−threaded
By default, SoX is ‘single threaded’. If the −−multi−threaded option is given however then SoX
will  process  audio  channels  for  most  multi-channel  effects  in  parallel  on  hyper-threading/multi-
core architectures. This may reduce processing time, though sometimes it may be necessary to use
this  option  in  conjunction  with  a  larger  buffer  size  than  is  the  default  to  gain  any benefit  from
multi-threaded processing (e.g. 131072; see −−buffer above).
−−no−clobber
Prompt before overwriting an existing file with the same name as that given f or the output file.
N.B. Unintentionally  overwriting  a  file  is  easier  than  you  might  think,  for  example,  if  you  acci-
dentally enter


=== File Format Types ===
sox file1 file2 effect1 effect2 ...
SoX  can  work  with  ‘self-describing’  and  ‘raw’  audio  files. ‘self-describing’  formats  (e.g. WAV , FLAC,
when what you really meant was
MP3) have a header that completely describes the signal and encoding attributes of the audio data that fol-
lows.  ‘raw’  or  ‘headerless’  formats  do  not  contain  this  information,  so  the  audio  characteristics  of  these
must be described on the SoX command line or inferred from those of the input file.
The  following  four  characteristics  are  used  to  describe  the  format  of  audio  data  such  that  it  can  be  pro-
cessed with SoX:
==== sample rate ====
The  sample  rate  in  samples  per  second  (‘Hertz’  or  ‘Hz’). Digital  telephony t raditionally  uses  a
sample rate of 8000 Hz (8 kHz), though these days, 16 and even 3 2 k Hz are becoming more com-
mon.  Audio  Compact  Discs  use  44100 Hz  (44.1 k Hz).  Digital  Audio  Tape  and  many c omputer
systems use 48 kHz. Professional audio systems often use 96 kHz.
==== sample size ====
The number of bits used to store each sample. To day, 1 6-bit is commonly used. 8-bit was popular
in the early days of computer audio. 24-bit is used in the professional audio arena. Other sizes are
also used.
==== data encoding ====
The way in which each audio sample is represented (or ‘encoded’). Some encodings have variants
with  different  byte-orderings  or  bit-orderings. Some  compress  the  audio  data  so  that  the  stored
audio  data  takes  up  less  space  (i.e.  disk  space  or  transmission  bandwidth)  than  the  other  format
parameters  and  the  number  of  samples  would  imply. C ommonly-used  encoding  types  include
floating-point, μ-law, ADPCM, signed-integer PCM, MP3, and FLAC.
==== channels ====
The  number  of  audio  channels  contained  in  the  file. One  (‘mono’)  and  two ( ‘stereo’)  are  widely
used.  ‘Surround sound’ audio typically contains six or more channels.
The term ‘bit-rate’ is a measure of the amount of storage occupied by an encoded audio signal over a u nit
of  time. It  can  depend  on  all  of  the  above  and  is  typically  denoted  as  a  number  of  kilo-bits  per  second
(kbps).  An A-law t elephony s ignal has a bit-rate of 64 kbps. MP3-encoded stereo music typically has a bit-
rate of 128−196 kbps. FLAC-encoded stereo music typically has a bit-rate of 550−760 kbps

Revision as of 10:06, 2 September 2021

SoX − Sound eXchange, the Swiss Army knife of audio manipulation[edit]

SYNOPSIS[edit]

sox [global-options] [ format-options] infile1 [[format-options] infile2] . .. [format-options] outfile [effect [effect-options]] ... play [global-options] [ format-options] infile1 [[format-options] infile2] . .. [format-options] [effect [effect-options]] ... rec [global-options] [ format-options] outfile [effect [effect-options]] ...

DESCRIPTION[edit]

Introduction[edit]

SoX reads and writes audio files in most popular formats and can optionally apply effects to them. It can combine multiple input sources, synthesise audio, and, on many s ystems, act as a general purpose audio player or a multi-track audio recorder. It a lso has limited ability to split the input into multiple output files. All SoX functionality is available using just the sox command. To s implify playing and recording audio, if SoX is invoked a s play, t he output file is automatically set to be the default sound device, and if invoked a s rec, t he default sound device is used as an input source. Additionally, t he soxi(1) command provides a con- venient way to just query audio file header information.

The heart of SoX is a library called libSoX. Those interested in extending SoX or using it in other pro- grams should refer to the libSoX manual page: libsox(3).

SoX is a command-line audio processing tool, particularly suited to making quick, simple edits and to batch processing. If you need an interactive, g raphical audio editor, u se audacity(1).

  • * *

The overall SoX processing chain can be summarised as follows:

Input(s) → Combiner → Effects → Output(s)

Note however, that on the SoX command line, the positions of the Output(s) and the Effects are swapped w.r.t. the logical flow j ust shown. Note also that whilst options pertaining to files are placed before their respective file name, the opposite is true for effects. To s how h ow this works in practice, here is a selection of examples of how S oX might be used. The simple

sox recital.au recital.wav

translates an audio file in Sun AU f ormat to a Microsoft WAV fi le, whilst sox recital.au −b 16 recital.wav channels 1 rate 16k fade 3 norm performs the same format translation, but also applies four effects (down-mix to one channel, sample rate change, fade-in, nomalize), and stores the result at a bit-depth of 16.

sox −r 16k −e signed −b 8 −c 1 voice-memo.raw voice-memo.wav

converts ‘raw’ (a.k.a. ‘headerless’) audio to a self-describing file format,

sox slow.aiff fixed.aiff speed 1.027

adjusts audio speed,

sox short.wav long.wav longer.wav

concatenates two a udio files, and

sox −m music.mp3 voice.wav mixed.flac

mixes together two a udio files.

play "The Moonbeams/Greatest/*.ogg" bass +3

plays a collection of audio files whilst applying a bass boosting effect,

play −n −c1 synth sin %−12 sin %−9 sin %−5 sin %−2 fade h 0.1 1 0.1

plays a synthesised ‘A m inor seventh’ chord with a pipe-organ s ound,

rec −c 2 radio.aiff trim 0 30:00

sox December 31, 2014 1

The soxi(1) command can be used to display information from audio file headers.

Determining & Setting The File Format[edit]

There are several mechanisms available for SoX to use to determine or set the format characteristics of an audio file. Depending on the circumstances, individual characteristics may be determined or set using dif- ferent mechanisms.

To d etermine the format of an input file, SoX will use, in order of precedence and as given or a vailable: 1. Command-line format options. 2. The contents of the file header. 3. The filename extension. To s et the output file format, SoX will use, in order of precedence and as given or a vailable: 1. Command-line format options. 2. The filename extension. 3. The input file format characteristics, or the closest that is supported by the output file type.

For a ll files, SoX will exit with an error if the file type cannot be determined. Command-line format options may need to be added or changed to resolve t he problem.

Playing & Recording Audio[edit]

The play and rec commands are provided so that basic playing and recording is as simple as

play existing-file.wav

and

rec new-file.wav

These two c ommands are functionally equivalent to

sox existing-file.wav −d

and

sox −d new-file.wav

Of course, further options and effects (as described below) can be added to the commands in either form.

  • * *

Some systems provide more than one type of (SoX-compatible) audio driver, e.g. ALSA & OSS, or SUNAU & AO. S ystems can also have more than one audio device (a.k.a. ‘sound card’). If more than one audio driver h as been built-in to SoX, and the default selected by SoX when recording or playing is not the one that is wanted, then the AUDIODRIVER environment variable can be used to override the default. For e xample (on many s ystems):

set AUDIODRIVER=oss
play ...

The AUDIODEV environment variable can be used to override the default audio device, e.g.

set AUDIODEV=/dev/dsp2
play ...
sox ... −t oss

or

set AUDIODEV=hw:soundwave,1,2
play ...
sox ... −t alsa

Note that the way of setting environment variables varies from system to system—for some specific exam- ples, see ‘SOX_OPTS’ below.

When playing a file with a sample rate that is not supported by the audio output device, SoX will automati- cally invoke the rate effect to perform the necessary sample rate conversion. For compatibility with old hardware, the default rate quality level i s s et to ‘low’. This can be changed by explicitly specifying the rate effect with a different quality level, e.g.

play ... rate −m

or by using the −−play−rate−arg option (see below).

  • * *

On some systems, SoX allows audio playback volume to be adjusted whilst using play. W here supported, this is achieved by t apping the ‘v’ & ‘V’ keys d uring playback.

To h elp with setting a suitable recording level, SoX includes a peak-level m eter which can be invoked (before making the actual recording) as follows:

rec −n

The recording level s hould be adjusted (using the system-provided mixer program, not SoX) so that the meter is at most occasionally full scale, and never ‘ in the red’ (an exclamation mark is shown). See also −S below.

Accuracy[edit]

Many fi le formats that compress audio discard some of the audio signal information whilst doing so. Con- verting to such a format and then converting back again will not produce an exact copy of t he original audio. This is the case for many f ormats used in telephony ( e.g. A-law, GSM) where low s ignal bandwidth is more important than high audio fidelity, a nd for many f ormats used in portable music players (e.g. MP3, Vo rbis) where adequate fidelity can be retained even w ith the large compression ratios that are needed to make p ortable players practical.

Formats that discard audio signal information are called ‘lossy’. Formats that do not are called ‘lossless’. The term ‘quality’ is used as a measure of how c losely the original audio signal can be reproduced when using a lossy format.

Audio file conversion with SoX is lossless when it can be, i.e. when not using lossy compression, when not reducing the sampling rate or number of channels, and when the number of bits used in the destination for- mat is not less than in the source format. E.g. converting from an 8-bit PCM format to a 16-bit PCM for- mat is lossless but converting from an 8-bit PCM format to (8-bit) A-law i sn’t. N.B. SoX converts all audio files to an internal uncompressed format before performing any a udio process- ing. This means that manipulating a file that is stored in a lossy format can cause further losses in audio fidelity. E .g. with

sox long.mp3 short.mp3 trim 10

SoX first decompresses the input MP3 file, then applies the trim effect, and finally creates the output MP3 file by re-compressing the audio—with a possible reduction in fidelity above that which occurred when the input file was created. Hence, if what is ultimately desired is lossily compressed audio, it is highly recom- mended to perform all audio processing using lossless file formats and then convert to the lossy format only at the final stage.

N.B. Applying multiple effects with a single SoX invocation will, in general, produce more accurate results than those produced using multiple SoX invocations.

Dithering[edit]

Dithering is a technique used to maximise the dynamic range of audio stored at a particular bit-depth. Any distortion introduced by quantisation is decorrelated by adding a small amount of white noise to the signal. In most cases, SoX can determine whether the selected processing requires dither and will add it during output formatting if appropriate.

Specifically, by d efault, SoX automatically adds TPDF dither when the output bit-depth is less than 24 and any of t he following are true:

• bit-depth reduction has been specified explicitly using a command-line option • the output file format supports only bit-depths lower than that of the input file format • an effect has increased effective bit-depth within the internal processing chain

For e xample, adjusting volume with vol 0 .25 requires two a dditional bits in which to losslessly store its results (since 0.25 decimal equals 0.01 binary). So if the input file bit-depth is 16, then SoX’s i nternal rep- resentation will utilise 18 bits after processing this volume change. In order to store the output at the same depth as the input, dithering is used to remove the additional bits.

Use  the −V option  to  see  what  processing  SoX  has  automatically  added.  The −D option  may  be  given t o

override automatic dithering. To i nv oke dithering manually (e.g. to select a noise-shaping curve), see the dither effect.

Clipping[edit]

Clipping is distortion that occurs when an audio signal level ( or ‘volume’) exceeds the range of the chosen representation. In most cases, clipping is undesirable and so should be corrected by adjusting the level prior to the point (in the processing chain) at which it occurs.

In SoX, clipping could occur, as y ou might expect, when using the vol or gain effects to increase the audio volume. Clipping could also occur with many o ther effects, when converting one format to another, a nd ev en w hen simply playing the audio.

Playing an audio file often involves resampling, and processing by analogue components can introduce a small DC offset and/or amplification, all of which can produce distortion if the audio signal level w as ini- tially too close to the clipping point.

For t hese reasons, it is usual to make s ure that an audio file’s s ignal level h as some ‘headroom’, i.e. it does not exceed a particular level b elow t he maximum possible level f or the given r epresentation. Some stan- dards bodies recommend as much as 9dB headroom, but in most cases, 3dB (≈ 70% linear) is enough. Note that this wisdom seems to have been lost in modern music production; in fact, many C Ds, MP3s, etc. are now m astered at levels above 0dBFS i.e. the audio is clipped as delivered. SoX’s stat and stats effects can assist in determining the signal level in an a udio file. The gain or vol effect can be used to prevent clipping, e.g.

sox dull.wav bright.wav gain −6 treble +6

guarantees that the treble boost will not clip.

If clipping occurs at any p oint during processing, SoX will display a warning message to that effect. See also −G and the gain and norm effects.

Input File Combining

SoX’s i nput combiner can be configured (see OPTIONS below) to combine multiple files using any of t he following methods: ‘concatenate’, ‘sequence’, ‘mix’, ‘mix-power’, ‘merge’, or ‘multiply’. The default method is ‘sequence’ for play, a nd ‘concatenate’ for rec and sox. For a ll methods other than ‘sequence’, multiple input files must have the same sampling rate. If necessary, separate SoX invocations can be used to make s ampling rate adjustments prior to combining. If the ‘concatenate’ combining method is selected (usually, t his will be by default) then the input files must also have the same number of channels. The audio from each input will be concatenated in the order given to form the output file.

The ‘sequence’ combining method is selected automatically for play. I t i s s imilar to ‘concatenate’ in that the audio from each input file is sent serially to the output file. However, here the output file may be closed and reopened at the corresponding transition between input files. This may be just what is needed when sending different types of audio to an output device, but is not generally useful when the output is a normal file.

If either the ‘mix’ or ‘mix-power’ combining method is selected then two or m ore input files must be given and will be mixed together to form the output file. The number of channels in each input file need not be the same, but SoX will issue a warning if they a re not and some channels in the output file will not contain audio from every input file. A m ixed audio file cannot be un-mixed without reference to the original input files.

If the ‘merge’ combining method is selected then two or m ore input files must be given a nd will be merged together to form the output file. The number of channels in each input file need not be the same. A m erged audio file comprises all of the channels from all of the input files. Un-merging is possible using multiple invocations of SoX with the remix effect. For example, two m ono files could be merged to form one stereo file. The first and second mono files would become the left and right channels of the stereo file.

The ‘multiply’ combining method multiplies the sample values of corresponding channels (treated as num- bers in the interval −1 to +1). If the number of channels in the input files is not the same, the missing chan- nels are considered to contain all zero.

When combining input files, SoX applies any s pecified effects (including, for example, the vol volume adjustment effect) after the audio has been combined. However, it is o ften useful to be able to set the vol- ume of (i.e. ‘balance’) the inputs individually, b efore combining takes place. For a ll combining methods, input file volume adjustments can be made manually using the −v option (below) which can be given f or one or more input files. If it is given f or only some of the input files then the others receive no v olume adjustment. In some circumstances, automatic volume adjustments may be applied (see below).

The −V option (below) can be used to show t he input file volume adjustments that have been selected (either manually or automatically).

There are some special considerations that need to made when mixing input files:

Unlike t he other methods, ‘mix’ combining has the potential to cause clipping in the combiner if no balanc- ing is performed. In this case, if manual volume adjustments are not given, SoX will try to ensure that clip- ping does not occur by automatically adjusting the volume (amplitude) of each input signal by a factor of ¹/n, w here n is the number of input files. If this results in audio that is too quiet or otherwise unbalanced then the input file volumes can be set manually as described above. U sing the norm effect on the mix is another alternative.

If mixed audio seems loud enough at some points but too quiet in others then dynamic range compression should be applied to correct this—see the compand effect.

With the ‘mix-power’ combine method, the mixed volume is approximately equal to that of one of the input signals. This is achieved b y b alancing using a factor of ¹/√n instead of ¹/n. N ote that this balancing factor does not guarantee that clipping will not occur, b ut the number of clips will usually be low a nd the resultant distortion is generally imperceptible.

Output Files[edit]

SoX’s d efault behaviour is to take o ne or more input files and write them to a single output file. This behaviour can be changed by specifying the pseudo-effect ‘newfile’ within the effects list. SoX will then enter multiple output mode.

In multiple output mode, a new fi le is created when the effects prior to the ‘newfile’ indicate they a re done. The effects chain listed after ‘newfile’ is then started up and its output is saved to t he new fi le. In multiple output mode, a unique number will automatically be appended to the end of all filenames. If the filename has an extension then the number is inserted before the extension. This behaviour can be custom- ized by placing a %n anywhere in the filename where the number should be substituted. An optional num- ber can be placed after the % to indicate a minimum fixed width for the number.

Multiple output mode is not very useful unless an effect that will stop the effects chain early is specified before the ‘newfile’. If end of file is reached before the effects chain stops itself then no new fi le will be cre- ated as it would be empty.

The following is an example of splitting the first 60 seconds of an input file into two 30 s econd files and ignoring the rest.

sox song.wav ringtone%1n.wav trim 0 30 : newfile : trim 0 30

Stopping SoX[edit]

Usually SoX will complete its processing and exit automatically once it has read all available audio data from the input files.

If desired, it can be terminated earlier by sending an interrupt signal to the process (usually by pressing the

keyboard interrupt key which is normally Ctrl-C). This is a natural requirement in some circumstances, e.g. when using SoX to make a r ecording. Note that when using SoX to play multiple files, Ctrl-C behaves slightly differently: pressing it once causes SoX to skip to the next file; pressing it twice in quick succession causes SoX to exit. Another option to stop processing early is to use an effect that has a time period or sample count to determine the stopping point. The trim effect is an example of this. Once all effects chains have stopped then SoX will also stop.

FILENAMES[edit]

Filenames can be simple file names, absolute or relative path names, or URLs (input files only). Note that URL support requires that wget(1) is available. Note: Giving SoX an input or output filename that is the same as a SoX effect-name will not work since SoX will treat it as an effect specification. The only work-around to this is to avoid such filenames. This is generally not difficult since most audio filenames have a filename ‘extension’, whilst effect-names do not.

Special Filenames[edit]

The following special filenames may be used in certain circumstances in place of a normal filename on the command line:

− SoX can be used in simple pipeline operations by using the special filename ‘−’ which, if used as an input filename, will cause SoX will read audio data from ‘standard input’ (stdin), and which, if used as the output filename, will cause SoX will send audio data to ‘standard output’ (stdout). Note that when using this option for the output file, and sometimes when using it for an input file, the file-type (see −t below) must also be given. " | program [options] . .." This can be used in place of an input filename to specify the the given p rogram’s s tandard output (stdout) be used as an input file. Unlike − (above), this can be used for several inputs to one SoX command. For example, if ‘genw’ generates mono WAV f ormatted signals to its standard output, then the following command makes a stereo file from two g enerated signals: sox −M "|genw −−imd −" "|genw −−thd −" out.wav For h eaderless (raw) audio, −t (and perhaps other format options) will need to be given, preceding the input command.

"wildcard-filename"

Specifies that filename ‘globbing’ (wild-card matching) should be performed by SoX instead of by the shell. This allows a single set of file options to be applied to a group of files. For e xample, if the current directory contains three ‘vox’ files, file1.vox, file2.vox, and file3.vox, then

play −−rate 6k *.vox

will be expanded by the ‘shell’ (in most environments) to

play −−rate 6k file1.vox file2.vox file3.vox

which will treat only the first vox file as having a sample rate of 6k. With

play −−rate 6k "*.vox"

the given s ample rate option will be applied to all three vox files.

−p, −−sox−pipe

This can be used in place of an output filename to specify that the SoX command should be used as in input pipe to another SoX command. For e xample, the command:

play "|sox −n −p synth 2" "|sox −n −p synth 2 tremolo 10" stat

plays two ‘ files’ in succession, each with different effects.

−p is in fact an alias for ‘−t sox −’.

−d, −−default−device

This can be used in place of an input or output filename to specify that the default audio device (if one has been built into SoX) is to be used. This is akin to invoking rec or play (as described above).


−−combine concatenate | merge | mix | mix−power | multiply | sequence

Select the input file combining method; for some of these, short options are available: −m selects ‘mix’, −M selects ‘merge’, and −T selects ‘multiply’.

See Input File Combining above for a description of the different combining methods. −D, −−no−dither

Disable automatic dither—see ‘Dithering’ above. A n e xample of why t his might occasionally be useful is if a file has been converted from 16 to 24 bit with the intention of doing some processing on it, but in fact no processing is needed after all and the original 16 bit file has been lost, then, strictly speaking, no dither is needed if converting the file back to 16 bit. See also the stats effect for how to d etermine the actual bit depth of the audio within a file. −−effects−file FILENAME

Use FILENAME to obtain all effects and their arguments. The file is parsed as if the values were specified on the command line. A n ew line can be used in place of the special : marker to separate effect chains. For c onvenience, such markers at the end of the file are normally ignored; if you want to specify an empty last effects chain, use an explicit : by itself on the last line of the file. This option causes any e ffects specified on the command line to be discarded. −G, −−guard

Automatically invoke the gain effect to guard against clipping. E.g. sox −G infile −b 16 outfile rate 44100 dither −s is shorthand for

sox infile −b 16 outfile gain −h rate 44100 gain −rh dither −s

See also −V, − −norm, and the gain effect.

−h, −−help

Show v ersion number and usage information.

−−help−effect NAME

Show u sage information on the specified effect. The name all can be used to show u sage on all effects.

−−help−format NAME

Show i nformation about the specified file format. The name all can be used to show i nformation on all formats.

−−i, −−info

Only if given as t he first parameter to sox, behave as soxi(1).

−m | −M

Equivalent to −−combine mix and −−combine merge, respectively.

−−magic

If SoX has been built with the optional ‘libmagic’ library then this option can be given to e nable its use in helping to detect audio file types.

−−multi−threaded | −−single−threaded

By default, SoX is ‘single threaded’. If the −−multi−threaded option is given however then SoX will process audio channels for most multi-channel effects in parallel on hyper-threading/multi- core architectures. This may reduce processing time, though sometimes it may be necessary to use this option in conjunction with a larger buffer size than is the default to gain any benefit from multi-threaded processing (e.g. 131072; see −−buffer above).

−−no−clobber

Prompt before overwriting an existing file with the same name as that given f or the output file. N.B. Unintentionally overwriting a file is easier than you might think, for example, if you acci- dentally enter

sox file1 file2 effect1 effect2 ... when what you really meant was