Converting mp3 to wav

Lynn David Newton plug-discuss@lists.plug.phoenix.az.us
Wed, 30 Jan 2002 08:02:24 -0700


>>         o=$(echo "$i" | sed -e "s/mp3/wav/")
>> 
>> This script lists the mp3 files in the current directory, converts the
>> mp3 extention to wav using sed, and then does the conversion.
> 
> Be careful when using sed to change extensions, what if you have an mp3
> called something like "foo thump3.mp3", sed will turn it into "foo
> thuwav.mp3".  I know it's not as cool as using sed to do it, but basename
> generally leads to more consistent results in this case.
> 
> o="$(basename $i .mp3).wav"

True enough. A more precise rendering using sed would be

o=$(echo "$i" | sed 's/\.mp3$/.wav/')

However, the string manipulation tricks that Hans alluded to obviates the
need for such machinations.

The subtleties of parameter evaluation are among the shell's best-kept
secrets. Every shell programmer seeking competence should make it a goal to
become familiar with these forms, and should use them, because they are
built into the shell and are lightning fast, just a few machine level
instructions, as compared with all the overhead of running a whole
sub-process. Perhaps in the grand scheme of things saving a microsecond
means very little. As is the philosophy among Perl programmers, if it works,
and you get it done before your boss begins to consider you for the next
round of layoffs, it's good code. However, most true hackers I've known are
more idealistic about their code than that, even if it's "only" shell
code.[1]

Ten or twelve years ago I taught a class in Korn shell (for which I also
developed the courseware) wherein I demonstrated the difference in run time
running an expression such as the one above in a loop a thousand times or
so, comparing the Bourne shell old-fashioned method versus the Korn shell
sophisticated method. I no longer have the courseware or example to refer
to, but the difference was something like 10 seconds or longer run time in
Bourne shell versus barely being able to get your hand off the return key
before it's done in Korn shell, something like 0.02 seconds. Which one would
*you* rather rather use in a script for permanent use? And the ksh version
was much more compact, too.

[1] I just broke a rule of my own by saying "only" shell code. May the shell
never be regarded denigratingly! The Unix shell is capable of vast power,
and is for many users their primary contact with the system. In about 1990,
while working for Motorola, I wrote an entire domain-specific scripting
language for the purpose of automating the control of test suites, which I
eventually wrote a paper on that was presented at a conference. The feature
rich test suite driver, which was written in Bourne shell, had 42 scripting
keywords and 26 command line switches. I did put the keyword parsing code in
an external C program in order to make it faster and to avoid putting all
that stuff in the script, especially since Bourne shell does not have
function calls.

The point is that the shell, if well used, is a tool of inestimable power. I
say that even though my own scripting language of choice these days is Perl.

-- 
Lynn David Newton
Phoenix, Arizona
http://wolf.eecs.umich.edu/~lnewton