Learn Linux for Beginner

Friday, April 15, 2005

MP3 Play in Linux Systems

MP3 format

If you've touched a computer in the last couple of years you've at least heard about the MPEG layer 3 format, popularly known as MP3. You have probably listened to an MP3 and you may have even "ripped a track", that is, made an MP3 file from a song on a CD.

MP3 has become somewhat controversial. In light of the Napster case, sharing MP3 files has opened up a Pandora's Box of questions about copyright, fair use and intellectual property. This really isn't the place to debate those questions but there are some issues related to the making of MP3s that concern Linux, Open Source and Free Software in particular.

We'll talk about the programs available to make sound files in MP3 format, but I should point out that MP3 is a non-free format. That is to say, the Fraunhofer Institute and Thomson Multimedia developed MPEG Layer 3 technology and they hold the patent rights it. If you want to write programs that encode and/or decode MP3 format, you have to pay licensing fees. Thomson has set up a website to talk about these issues, if you're interested. What does this mean for Linux? Well, if you are an advocate of the Open Source and/or Free Software model that Linux grew out of, MP3 might be seen as a format to be avoided. Actually, if you think this way, you now have an alternative with Vorbis' *.ogg format. We'll talk about that shortly. But being that MP3 is so prevalent nowadays and encoders/decoders do exist for Linux, we'll talk about making and playing MP3 files.

Let's go back to our "reminder" system. If you go into the /reminders directory we created (cd reminders) and type:

ls -l *.wav

you'll see that the default quality of a 5 second recording gives us a file of about 1/4 megabyte. If we made the file a little better, as I do when I'm working with sample sentence for language learning, you doubled the file size to just under half a megabyte each. To check this, I made one of each format:

-rw-r--r--    1 mike     users      132344 Nov 27 12:11 0intro.wav
-rw-r--r-- 1 mike users 220544 Nov 28 08:37 2001-11-28_fair.wav
-rw-r--r-- 1 mike users 441044 Nov 28 08:34 2001-11-28_good.wav

If for some reason you wanted to keep these reminders, files of this size would quickly begin to take up space. Disk space is cheap, but there's no reason to occupy space if we can compress it. We could take out our tools gzip or bzip2 that we learned about in a previous lesson. 'bzip2' will actually get the file down to about half its original size:

-rw-r--r--    1 mike     users      206442 Nov 28 08:34 2001-11-28_good.wav.bz2

But there is a much better way of doing this, for now, by converting it to MP3 format. Look what we get for the same file as an MP3:

-rw-r--r--    1 mike     users       80234 Nov 28 08:48 2001-11-28_good.mp3

You've got it down to about one fifth of its size. And you can hear it right away with an MP3 decoder/player. Let's show how we did this.

First of all, you need a program that converts *.wav files to MP3 format. There are a few of these available for Linux. One of these, and probably the most popular, is Tord Jansson's BladeEnc. He's had some trouble with the MP3 patent holders so, again, we've stepped into controversy here. How do we avoid bringing patent holders' wrath down upon ourselves? Well, by going over to Fraunhofer's website and getting a demo of their MP3 encoder, appropriately called 'mp3enc' (the demo is actually called 'mp3encdemo' - very logical) we can convert these files in MP3 format. That will at least give us an idea about how all this works in Linux.

You'll get a *.tgz (Slackware package) compressed file. Just unzip and untar in your home directory:

tar -zxvpf mp3encdemo.tgz (or whatever the current file is called)

There is a pre-compiled binary (aka - program that works right away) and some documentation (manual, other README files). There is a section in the manual that says "For the impatient". Being impatient by nature, I went right to that and found out how to convert my *.wav files to acceptable quality MP3s. Now, we're lucky that our reminders aren't very long, because the generous Fraunhofer people have given us a demo that only encodes 30 second long files. I know what you were thinking. You were going to run next door and borrow the neighbor's "Greatest Punk Love Songs" and start rippin'. We'll you're out of luck unless you want to do a medley. At any rate, the Fraunhofer demo will do nicely for learning purposes. To use the program, just pick one of the reminders and type:

mp3encdemo -br 128000 -if 2001-[whatever].wav -of 2001-[whatever].mp3

where [whatever] is the date of your reminder. Let's look at these options. -br stands for bitrate This is 128 kilobits per second and you'll get an acceptable, almost CD quality file. -if stands for "if it works" - naah - that's only a joke. (I couldn't resist). -if means input file, that is, the file your inputing or feeding to the encoder. Logically then, -of means output file, the MP3 file you're going to get. There you have it. Feel free to encode any files you want. (as long as they're under thirty seconds, of course)

Actually any MP3 encoder for Linux that you can find out there works in basically the same way. You input a *.wav file with some options and you get an MP3 file. The program BladeEnc that we mentioned before, behaves similarly and there is no 30 second limit. (wink, wink, nudge nudge, and the author begins to whistle nervously)

Playing MP3 files

OK. Now we have our MP3 file. That begs the question: How can I hear it? Well, we need an MP3 decoder and/or player. I say "and/or" because these could be two different things under the x-window system. Popular graphic MP3 players are actually front-ends for MP3 decoders. That means they provide a graphic control panel for using a program that you don't really see. That's why I usually use the command line programs, as I mentioned earlier. So let's save some CPU power and learn how things work in the process.

'mpg123' is a very popular command line program for playing MP3 files. It is also highly versatile. You can create playlists and play songs in alphabetical order or in random order. You can even play little tricks and create weird disco versions of songs. (no kidding) You can even "reverse engineer" the MP3 file or even parts of it back to *.wav format. As far as I have tried, most graphic programs can manipulate playlists and add an echo effect but can't take full advantage of mpg123's features. You can only do it on the command line.

Anyway, 'mpg123' comes with most major distributions. If you visit mpg123's home page you can find out all about it. You can also get source code and RPMs if you don't have it installed already.

To simply play an MP3 file, enter the directory where the files are and type:

mpg123 your_mp3_file.mp3

Let's say you already have a lot of MP3s. You may have gotten some ... then again, I don't want to know where you got them. You could random play them

mpg123 -z *.mp3

One of my favorite things to do is to create a playlist. Here's how I do it. This will be good review practice for the command line as well.

First, I enter a directory where I have MP3s:

cd classical

Then I see what songs I have:

ls *.mp3

Then I pick out some songs that I particularly like and make a file that is going to be my playlist:

ls -1 Mozart_nightmusic.mp3 > favorites

The command 'ls' with the option '-1' lists the file name without any other information. The > symbol as you remember from our lesson on pipes, etc. creates a file called 'favorites' and includes the 'ls -1' output in it. To add more songs, we would repeat the same command, but this time we would (obviously) change the MP3 file name and most importantly we would change the one > symbol to two >> symbols. This way, we just add to the playlist file and we don't overwrite the file as would be the case if we didn't use two >> symbols.

ls -1 Beethoven_fur_elise.mp3 >> favorites

So I'd just keep adding songs until I had a nice playlist. To see the list, you'd just type:

less favorites

If everything is satisfactory, then we can play these songs. Just type:

mpg123 -@ favorites

The email (@) symbol tells mpg123 to look for the playlist. That would play the songs in the order they are on the list. If you want to play them in random order, you would just add a -z before the -@ option

More fun with mpg123

Let's say you wanted to play "Name That Tune". Don't laugh! I have done this at parties. You know, you only play so much of a song and the people have to guess which one it is. Well, with mpg123, you're all set. This command will only play the first 50 "frames" of a song:

mpg123 -k 0 -n 50 Mozart_nightmusic.mp3

And you'll have the liveliest parties on your block! All courtesy of Linux and mpg123! Seriously, the -k option tells the program which frame to start at and then -n option indicates where it should stop. Pretty easy!

You want more party ideas? Well, how about doing Techno-Mozart? That's easy too. Just type:

mpg123 -d 2 Mozart_nightmusic.mp3

This will make the program skip over every 2nd frame. That is, it will play a frame and skip one. This is *not* the Chipmunk effect. You will hear the same tone of voice because this doesn't effect the sampling rate of the file. It will just sound "techno" as I pointed out. Even though I mentioned parties and that, I have used this program and this effect in my language work for purposes of "serious" study. As the rate of the voice is not altered, you can change the number and see at what point people can't understand something. By this, you can test the aural skills of a person learning a language. By the way, you can also do the opposite by changing the -d X option to -h X. Instead of skipping frames, mpg123 will play the same frame X number of times. Remember, these options do not produce the "slow-mo" or "Chipmunk" effects. It just slows down the speed of playback.

As we mentioned before, you can also "reverse engineer" the MP3 file back to a *.wav format. Remember, before you try this at home, the *.wav format is at least 5 times bigger, so make sure you've got plenty of hard disk space.

mpg123 -w Mozart_nightmusic.wav Mozart_nightmusic.mp3

You can also use the different options like -d or -h combined with the -k and -n to produce weird *.wav sound effects files. Your imagination is the limit. If you do want to do techno-multimedia things with mpg123, you can use the -v option to get complete information about the track your playing.

Well, after all that is said and done about MP3, I must confess that I don't like working with MP3s as much as I like working with the new, free (as in beer and source code) Ogg Vorbis format.

0 Comments:

Post a Comment



<< Home