Audio file conversion
Using the
unix program sox , it is extremely easy to convert
audio files
from one format to another. Suppose you have the aif file, "song.aif,"
on your desktop and you wish to convert it to wav format. Open the
terminal and type:
cd Desktop
This
changes
your working directory to your Desktop. Now all you have to do to
change
the format of your files is type:
sox song.aif song.wav
This will
create a file entitled "song.wav" on your desktop. Note: it will do
nothing
to your original "song.aif" file. Sox never alters
the
original file;
only the file it creates on the basis of the original.
Sox supports
a number of
different audio file formats, including not only aif and wav, but au,
dat, gsm, and ogg, among others. (See the sox man
pages for
more information.) One format sox doesn't support
is mp3.
However,
you can use another unix program, lame, to
translate mp3 files
into wav files. The following command will convert
the file
"Etude.mp3"
to "Song.wav":
lame --decode Etude.mp3
Song.wav
What
about converting files
to the mp3 format? Well, of course, you can easily do that in
iTunes.
But suppose you wish to do it in the terminal? Here
we once
again
have recourse to lame:
lame -h file.aif file.mp3
If
you desire
to encode by bitrate (at, say, a bitrate of 192) try:
lame -b 192 file.aif file.mp3
For
more
options, consult the lame man
pages (enter man lame
in the terminal).
Editing
sound files
with Audacity
Editing
sound files is very easy, regardless of whether you use Audacity
or some other shareware or commercial program.
Chunks of sound can be high-lighted, deleted, copied, and pasted as you
see fit. Audacity
is mostly
useful for removing or adding, through cut and paste, blocks of sound.
Here's
a screenshot of what Audacity
looks like:
Audacity is
an open source project and is still a little rough around the edges.
But it is constantly being worked on and improved and is good enough
for those with modest sound editing needs. For more ambitious projects,
a shareware program such as Amadeus
would
probably be preferable.
Equalizing volume
levels
Suppose
you have downloaded a number of mp3 files off the Internet, all from
different sources, and want to make a single CD out of them.
It
is likely that these files will have been recorded at
different
levels, so that
some will be too loud and some too soft. You could open them
up
in
an audio editor like Audacity and try to
level
them manually, but that would be a lot of work. A better
method
is
to "normalize" the files with the command-line program normalize.
This program will change the volume settings of each audio
file
so that they will all be around the same without affecting the dynamics
of any of the files in question. Using the program is very
simple. Open up the terminal and type normalize .
Make sure to include a space after the word normalize.
Then all you have to do is drag all the audio files you wish
to
normalize into the terminal and press enter. Just make sure
the
files are in .aiff or .wav format. MP3 files will not work
with normalize.
You can also
"batch" normalize — i.e., you can preserve the relative sound
values of
a group of sound files so that the dynamic range is preserved between
them. This is useful if you are combining files from a single
CD
or album with other files. To quote the normalize
"man"
page:
In
batch mode,
all the specified files are considered to be part of a single
album
and their relative volumes are preserved.
This is
done
by averaging the volumes of all the files, computing a single
adjustment
fromthat, and applying the same
adjustment to
all
the files. Some analysis is also done so that files with
volumes
that
appear to be statistical
aberrations are
not
considered in the average. This is useful if you have albums
(like
many of the author's) in which there is
one "quiet
song"
that throws off the average.
Using the
batch mode is very easy. Simply use the "-b" option with
every
file you wish to be part of the batch. Then cease using it
for
files not part of the batch. For example, suppose you have
five
files you wish to
normalize, file1.wav, file2.wav,
file3.wav, file4.wav, file5.wav.
Suppose further
that the first three files are from the same CD. You commence
your
normalizing festivities with the following command:
normalize
-b file1.wav
file2.wav file3.wav
Then
conclude with this command:
normalize
file4.wav file5.wav
And you're finished!
Normalize is the easiest of
command-line programs.
Flattening dynamics
Sometimes
it is convenient to flatten the dynamics of a soundfile.
There
may be a number of reasons to do this. Perhaps you desire to
make
a compilation disc for listening in your car. If there exists
a
wide dynamic range in the music on the disc, you will have trouble
hearing the quiet parts over the noise of the engine. Or
suppose
you are making a home movie and wish add a soundtrack. Again,
if
you chose orchestral music from a soundtrack or a work of classical
music, you may need to flatten out some of peaks and valleys in the
music's dynamic landscape. Sox allows you
to do just that
with the compand filter, although the syntax is
rather obscure. The soxexam man pages
recommend the
following settings to compand the classical music work Also
Sprach
Zarathustra by Richard Strauss:
sox
asz.aif aszc.aif compand
0.3,1 -90,-90,-70,-70,-60,-20,0,0 -5,0,0.2
Here's
what this terminal command does. It first takes the asz.aif
file
and outputs it as aszc.aif, subjecting it to the
compand
filter. The soxexam man pages (enter man soxexam in
the
terminal) explain the settings as follows:
The
transfer function ("-90,...")
says
that very soft sounds between -90 and -70 decibels
(-90 is about the limit
of 16-bit
encoding) will remain unchanged. That keeps the
compander
from boosting the volume on
"silent"passages
such as between movements. However,
sounds in the
range -60 decibels to 0 decibels (maximum volume)
will be boosted so that the 60-dB dynamic range of the original music
will
be compressed 3-to-1 into a 20-dB range, which is wide enough
to
enjoy the music but narrow enough to get around the road
noise.
The -5 dB output gain is needed to avoid clipping (the number
is
inexact, and was derived byexperimentation). The 0 for the
initial volume will work fine for a clip that starts with a
bit
of silence, and the delay of 0.2 has the effect of causing
the compander to react a bit more quickly to sudden
volume
changes.
If
this account
of the process is accurate, it would seem to suggest that the part of
the function that is critical to determining the extent of dynamics
compression is the -60,-20.
In order to test this, I
ran the following command :
sox
fg.aif fg2.aif compand
0.3,1
-90,-90,-60,-60,-50,-15,0,0 -5,0,0.2
Notice
that I
not only change -60,-20
to -50,-15,
but I also changed the -70,-70
part of the
function to -60,-60. The reason for this is that in quieter
passages, the -70,-70 was
creating a lot of gain noise.
I
also changed the output gain level from -5
to -1
in order to preserve the impact of the first climax. Visually, this is
what the results were like. First, the original file, "fg.aif":
Notice the
extremes in the dynamic range. The first compand filter, the
one
from the soxexam man
page,
produced the following output:
Now the results of the final compand filter:
This
has the effect of
pretty much
flattening out nearly all the dynamics — too much for music,
but
perhaps just right for voice. Changing -20
to -15 causes the 3-to-1
compression to be squeezed into
a 15 dB range rather than a -20
dB range. To
flatten the dynamics less, simply raise this number, say, to -30.
Be sure,however, to keep the output gain at -5
to avoid clipping and static.
This should provide at least a vague idea of how this filter works.
It's probably a good idea to play around with a little bit
until
you understand some of its nuances and can get from it precisely what
you want.
Batching
Multiple Sound Files
Running a command-line tool
like sox
can be tedious if you
have to do it one file at a time. Isn't the whole point of using the
terminal is the power and efficiency that lies within it's unassuming
surface? Certainly it is. But how can you tap into the batch power of sox when it contains
no batch
potential of its own, as, say, normalize
does?
It's not too terribly difficult, if you only know how. The secret is to
use the foreach
command in the
terminal. First, you must be certain you are using the tcsh shell.
Terminals run different
shells, each of which has it's own unique features. If you're on Jaguar
or a previous OS X system, tcsh
is the default shell; so unless you've changed it yourself (or someone
changed it for you), you shouldn't have a problem. If you're on
Panther, your default shell is bash.
You'll have to switch over to tcsh
to take advantage of this tutorial.
If you're confused about this, simply enter :
foreach
If you're output looks like this—
bash: foreach: command not
found
—then
you have a bash
shell. Enter tcsh and
you'll be on your way. To batch a directory of aif
files, here's what you do. Enter the first line:
foreach file ( *.aif )
The
word file
is a variable. You can use any word you like, even a single letter if
you're too lazy to type. When you enter this phrase, you will get a "foreach?"
prompt. Here we tell the shell what we wish to do with all the aif
files in the terminal's working directory. Let us compand them as
follows:
sox $file $file.aif compand 0.4,1
-90,-90,-70,-70,-60,-30,0,0 -5,0,0.2
Once
this line is entered, all you have to do is enter end to
set off the process in which every aif file in the terminal's working
directory will be sent the sox
command as configured above. One thing to keep in mind, however. When
batching like this with foreach,
sox
won't work properly
if your audio files have spaces in their names. Because it can be
extremely tedious to get the spaces out of file names, I wrote this
quick-and-dirty command-line program, spacer,
which replaces all the spaces in names with a "_". The binary can be
found here.
Just install it in
your /usr/local/bin directory.
Note on Installation
It has been brought to my
attention that some of the readers of the site are not clear on how to
install the programs discussed herein. I briefly discussed
the
installation on the homepage, and will info on it soon. All the Unix
programs discussed on these webpages, unless otherwise noted, can be
installed using the program fink, which can be found here.
This includes sox, normalize,
and lame. Audacity can
be found here.
—Last
updated February 19, 2004
|