2003-10-01
01 Oct 2003 17:00
Getting ripped
Assuming that you care about accurate CD ripping, with no clicks or glitches, the package you want to use is cdparanoia. On Gentoo, emerge cdparanoia.
Once you’ve got the audio off the CD, to get the best quality MP3s you should use LAME—emerge media-sound/lame. It has a bewildering variety of options for changing everything from the bit rate to the precise filters and psychoacoustic modeling algorithms used. You can spend weeks testing different settings, or you can do what I do and use one of the presets that have been worked out by audiophiles on Internet forums.
The --alt-preset default option gives MP3 files that I find it pretty much impossible to tell from the original CD. However, the resulting files are pretty large, averaging around 180kbps in many cases.
A more compact option is --r3mix. This is close to CD quality, but on occasion it won’t quite manage to reproduce the original sound precisely. It averages around 160kbps.
While classical music snobs like to think that the majestic sweep of the orchestra is difficult for MP3 to reproduce, it’s actually electronic music that’s hardest to encode accurately. The reason is, most audio processing relies on Fourier transforms, which break down sound waves into a summation of sine waves of different frequencies and amplitudes. Physical instruments generally produce audio with sine-like waveforms. Unfortunately, techno relies heavily on square waves and triangular waves, and pure square and triangle waves don’t break down into sine waves cleanly—you actually need an infinite number of sine waves to reproduce a square wave.
So, I tend to use --r3mix for general rock and pop, and call out --alt-preset standard when dealing with electronica and particularly detailed all-digital recordings.
iTunes has a horrible MP3 encoder. If you have music encoded from CD using iTunes, you would be well advised to re-rip using LAME --r3mix, which beats iTunes at similar bit rates. In fact, LAME MP3 also clearly beats iTunes MP4 files at comparable bitrates.
Tagging
The id3v2 package (emerge id3v2) provides a robust command-line utility for dealing with ID3 tags, in both version 1 and version 2 formats. However, the ID3v2.4 format isn’t yet supported—only ID3v2.3. If you have MP3s encoded with iTunes… well, you probably ought to re-encode with LAME. But if you won’t or can’t, you should at least convert the ID3 tags to v2.3, so Linux applications can deal with them.
Glue
Getting ripped, tagging, and now glue… so far it’s an average day in the life of a suburban punk. But if you try the entire CD to MP3 process once using the raw command-line tools, you’ll realize it’s a colossal pain. Apple’s iTunes does a great job of making it simple, even if the results aren’t very high quality; so I set out to look for something iTunes-like on Linux that would glue together all the command-line tools.
I’m told that the grip program is great if you have an X desktop. However, I didn’t want to install X and Gnome on the Linux box, and an X server on the Mac, just to rip CDs—so I searched for a terminal-based alternative.
The best tool I’ve been able to find is called mp3c. Conveniently, there’s a Portage ebuild for it, so you can just emerge mp3c. You’ll need to configure it before setting off to rip your CD collection. By default it uses what can only be described as the wrong ripper and the wrong MP3 encoder, at least for anyone concerned with audio quality.
I configured mp3c to use LAME for encoding. I also made it add the ID3 tags directly using LAME, instead of using id3v2 to do it afterwards; that way you don’t need to shunt the entire file down by 4K to add the frame that the ID3 tag is stored in. (That’s what iTunes is doing when it gives you a slow-moving progress bar as it adds ID3 tags to a huge MP3.)
Anyway, you can download my .mp3crc file and make a few small edits where appropriate. Once mp3c is set up, you can insert a disc, push F if necessary to force it to refresh and fetch the disc track details, then hit 3 to rip and encode.
Playback
It’s useful to be able to play back MP3s from the command line. Most of the more elaborate jukebox software calls command-line MP3 player software for the actual audio playing.
The most commonly-used program is mpg123. A higher quality free software alternative is mpg321, which uses a standards-compliant integer MP3 decoder library called MAD. The difference in sound quality is quite amazing. You guessed it, emerge mpg321 on Gentoo.
(The Whamb music player on OS X also uses the MAD decoder, and beats iTunes in sound quality. Unfortunately, I haven’t been able to find out how to make Whamb read an MP3 library via Rendezvous; the documentation is severely lacking.)
Streaming and Rendezvous
Assuming you’re a Mac user like me, what you really want is for any Mac in the house to be able to access the MP3 library on the Linux box straight from iTunes, with no configuration required. To achieve this we require a piece of software called daapd. It can use its own Rendezvous code, or you can use Apple’s open source Rendezvous implementation.
How it works is quite simple: The daapd software scans your MP3 library when it starts up, reading in the ID3 tag information and building its own database. It then starts a lightweight HTTP server, almost the same as any old web server except that it’s on a different port, and uses persistent HTTP connections—that is, it keeps a single session open rather than opening a new connection for each file transferred.
iTunes finds the daapd on your jukebox server because the jukebox server broadcasts its name and network address using Rendezvous. Hence as well as starting daapd when the jukebox boots, you also start a Rendezvous broadcast service.
The daapd program itself is easy enough to build—download it, unpack it, run the configure script, then make. Unfortunately, it needs four libraries. If you installed mpg321 like I suggested, you should already have the MAD libid3tag. If you’ve somehow manage to install Linux and not end up with zlib, emerge sys-libs/zlib. You can download the other libraries, daaplib and libhttpd-persistent, from the daapd web site.
The Apple Rendezvous package you want for Linux is in the mDNSPosix directory of the source archive. The Makefile should work without any changes; make os=Linux. The binary you need is built as build/mDNSResponderPosix. I copied it to /usr/local/bin/mdnsresponder; you’ll need to do the same for my scripts below to work.
Once you’ve installed all three, rc-update add daapd default will make everything start up when you boot Gentoo. To check it manually, type /etc/init.d/daapd start.
(Note that other Linux distributions use different tools and slightly different locations for startup scripts. If you’re not using Gentoo, you’ll need to consult the appropriate documentation.)
One problem you might have is that daapd tends to crash and core dump if you feed it corrupt MP3s. Whereas most MP3 players can cope with a little corruption, the ID3 tag libraries in daapd seem to be less robust than they might ideally be. If you find that daapd is crashing, I suggest editing the source to put in a few debug-type print statements to report each filename as it is opened, so you can find and eliminate the bad MP3s. Of course, if you’re ripping your music from CD, you shouldn’t have any problems.
(If you have gigabytes of stolen music containing hundreds of partly corrupt files which cause daapd to crash, well, I have no sympathy.)
Now the fun part begins
At this point you should be ready to rock… or blues or punk or classical or whatever. Now you’ll find out how long it takes to rip your entire CD collection. I’d already done most of mine over the course of several months, and had CD-Rs with the files on. Even so, I had to copy all the files onto the hard drive, and make sure all the ID3 tags were correct.
One thing you might want is a script to tidy up a whole bunch of tagged MP3 files, so that they’re all filed as /var/music/Artist_Name/Album_Name/dnn-Track_Name.mp3 where d is the disc in a multi-disc set, and nn is the track number. It’s also useful if the directory names and filenames have no spaces or other special characters in, as a lot of command line software is awkward to use—or even chokes—when your filenames have ‘special’ characters in. Finally, it’s useful to make them 31 characters or less, so you can burn them on CDs without problems.
I searched for a while for a suitable script, and eventually gave up and wrote my own, which I call mp3mv. It’s in Perl. You edit it to configure it; there are only two parameters, the source and destination directories, which should be distinct. It then does the right thing, as described above. Any files it can’t read the ID3 tags from, it leaves alone. It also leaves any non-MP3 files or directory structure in the source directory alone. To delete empty subdirectories under /var/music without the risk of deleting any files, the following incantation should work:
find /var/music -type d -exec rmdir \{\} \;
The script needs three Perl libraries from CPAN. As a reminder, g-cpan.pl MP3::Tag is the Gentoo way to install a Perl library.
Music everywhere
So, hopefully now you have daapd and mdnsresponder running on a set of cleaned-up MP3 files. Fire up iTunes, and make sure you have “Look for shared music” checked under the Sharing section of the preferences panel. You should see your jukebox machine appear as a blue shared music library in the left pane of the iTunes window. If so, the mdnsresponder part is working… Click on the icon, and if daapd is working you’ll get a list of available MP3s.