Using and Creating MP3s with OpenBSD

Overview

MP3s are a handy way to have music in a portable format. An MP3 file takes up a small fraction of the space required for the raw music file -- they use a "lossy" compression system, giving up a tiny amount of quality for a huge gain in compression. But, you probably knew that.

I'm a relative latecomer to the world of MP3s -- until I recently purchased a laptop with a large hard drive, lots of processor and audio, I had no real reason to worry about them -- my Heathkit/Harmon Kardon stereo system works quite well for playing music, thank you very much. However, the laptop changes things a bit -- sometimes it is nice to have a bit of music playing while hacking. No laptop has an audio system comparable to my Heathkit/Harmon Kardon, so a small loss of sound quality is quite acceptable in my mind.

This Techguide will help you create and playback MP3 files on an OpenBSD system. Most of these tools are generic or available on other Unix-Like Operating Systems, so you should have no problem using this information on other systems. This is by no means an exhaustive list of MP3 related applications for OpenBSD, but rather a starter -- once you start with this information, you can explore other tools.

MP3 technology is available on most popular platforms, however, the Unix-Like OS world has a huge advantage: open source tools. Many of the Windows and Macintosh tools are either commercial, or paid for by less than legitimate means, such as "ad-ware" or "spy-ware" -- or just begging you to buy the commercial version, or begging you to upgrade to the latest version which is seemingly rarely better (or as good!) as the old one..

The OpenBSD ports tree has quite a few tools one should look into for MP3 playback, I would suggest using the following command to search:

     $ cd /usr/ports
     $ make search "key=mp3" | more

Playback

Two of the most popular mp3 players for OpenBSD are mpg123 mpg321. I have had better luck with mpg321, but I understand this is highly dependent upon your hardware.

The man page installed with both programs is quite good, please read it.

Creating MP3s

As is often the case in the Unix-Like OS world, there are many ways to make MP3 files, and not one tool to go from a CD to a collection of MP3 files. Instead, this can be done with a small collection of tools: To use this process, all the above ports (all located in /usr/ports/audio should be installed, and you should (as always) read the man pages associated with them before use.

Here is a sample script (from Margarida Sequeira, minor mods from Henning Brauer and me) which will take a CDROM and reduce it to a small collection of MP3 files:

$ more cd2mp3.sh
#!/bin/sh

FILE=cddbinfo.txt

cdparanoia -B -v

rm track00.cdda.wav

for i in `echo *.wav`
do
        lame -cbr -b 128 -q 2 -k -t -p $i $i.mp3;
#       rm $i;
done

mp3cddb *.mp3

if [ -f $FILE ]; then
        mp3cddbtag $FILE
fi

Explanation:

The cdparanoia line extracts all the audio tracks from your CD, and drops them in your current directory. The speed of this process will depend upon your hardware, mostly the speed of your CDROM drive, and more, the speed at which it can extract audio data (which is often much slower the speed it can read digital data).

The rm track00.cdda.wav line deletes a bogus track00 (a very few seconds of silence) that is sometimes created. It *seems* to be related to the CD drive used to read the audio CD. Some CDs on some drives create this file, others do not. This file's existence messes up the last step, so it is deleted here. Yes, you will get an error message if that file does not exist.

At this point, the contents of the CD are a set of .wav files in the current directory. The next step (the for - do - done loop) encodes the .wav files into MP3 files, giving them the same name as the incoming .wav file, but with the .mp3 tail. You will note that the line after the lame line deletes the .wav file that was just encoded, but I have it commented out here. I rather prefer to delete the .wav files AFTER a successful conversion of the entire CD, but I'm strange. You may well want to remove the '#' that leads off that line (my machine I use for this is rather slow -- if I can skip redoing the cdparanoia step, it saves a good chunk of time).

Now, we have a collection of annoyingly named MP3 files: track03.cdda.wav.mp3 and similar. The next few lines (mp3cddb, mp3cddbtag) look at the tracks and try to identify the CD from the freedb.org database. The mp3cddbtag command then renames and tags the .mp3 files with the appropriate track names and recording information.

Usage Notes:

I use a P166 with an old, slow CDR drive to do mp3 encoding, and it is quite slow. The MP3 encoding, with the above lame options, encodes at about half the playback rate (i.e., a five minute track takes almost 10 minutes to encode). My 400MHz Pentium II laptop seemed to go just a hair faster than real-time playback.

If you wish to have better sound quality, you can tweak some of the lame parameters -- notably the '-r 128' and '-q 2' above script. However, be realistic. If you are playing back on your computer, keep in mind computer sound hardware and speakers generally suck. On the other hand, if you pick a good sound card and play it back though your serious stereo system, bumping up the bit-rate to 192k ("-r 192") or fiddling with the the "-q 2" may be worthwhile.

Note that the above script leaves a lot to be desired in terms of error checking, however I think it is of value as it is, as it is very easy to see what is going on and how it works. Feel free to add whatever you wish to it.

In my case, I store MP3 files on a Pentium 133 NFS server, which makes them available to any Unix-like system in the house. When I get around to it, I'll add Samba to this machine, giving me some added flexibility of clients.

Legal comments

This article is intended as a tool to make your personal music collection more useful to you. In no way do I support the spreading of copyrighted music in mp3 form. Not only is it illegal, it is also outright theft of another person's property (and I'm not even going to get started on the security and privacy implications of Kazaa and its kin). "Inability to pay" is no excuse, either, as I can't afford that nice Dodge Viper I'd like to have, that doesn't give me the right to one. As my dad told me from about age 5 on, "Get a job!".

Back to Technical Guides
Holland Consulting home page
Contact Holland Consulting


since February 5, 2003

Copyright  2003, Nick Holland, Holland Consulting

Published: 2/5/2003
Revised: 2/5/2003