Command Line USER

Converting Audio Formats Getting the swing The command line swings. In this issue of “Command Line”we investi- gate how you can grab tracks from audio CDs and convert sound files to other formats. No matter whether you choose wav, MP3 or , you can rely on the shell to convert your files. BY HEIKE JURZIK www.sxc .hu

ou do not need a GUI-based tool to Before you launch cdda2wav, check to first and last tracks you want to grab; for convert sound files to different for- see if you can access your CD-ROM drive example, Ymats – the command line gives with a non-privileged user account (see you everything you need. To get started, Box 1). To grab a track, you need to use cdda2wav -t 1+3 we will be looking at two programs that the -t option and the track number. The grab tracks from audio CDs and store program also expects the device name of gives you the first three tracks. To prevent them in wav format. Following that, we your CD-ROM drive, which you can sup- the output from ending up in a single will be looking at tips and tricks for ply by adding a -D option: wav file, you might like to add the -B converting various audio formats and option (see Figure 1). This will give you a learning how clever use of bash features $ cdda2wav -D /dev/hdc -t 1 collection of tracks named audio_01.wav, can speed up the process. ... audio_02.wav and so on. recording 276.6666 seconds sterU Grabbing with cdda2wav eo with 16 bits @ 44100.0 Hz ->U cdparanoia cdda2wav [1] (“ digital 'audio'... 0/ 0/ 1 0 11% cdparanoia [2] is a useful alternative to audio to wav”) is a program that most cdda2wav for grabbing tracks off audio distributors include. Debian users can The output includes information on the CDs. Again, the tool is included by most install the tool by running titles, the CDDB disk ID, and a progress distributions. To test the program on the indicator. The wav file is stored as command line, set the -Q flag. If you do apt-get install cdda2wav audio.wav in the current working direc- not see output like the output in Listing tory. Tip: to avoid repeatedly typing the 1, or if you see an error message such as: Command Line device name, you can temporarily set the CDDA_DEVICE environment variable by GLOSSARY Although GUIs such as KDE or GNOME are typing useful for various tasks,if you intend to get CDDB:The “Compact Disk Database”con- tains information on audio CDs and the titles the most out of your Linux machine,you will export CDDA_DEVICE=/dev/hdc they contain. Many CD players automatically need to revert to the good old command query a free CCDB server (assuming you have line from time to time. Apart from that,you an Internet connection),and add information in the active shell, or you can add the will probably be confronted with various on the artist, the album name,and the track command to your .bashrc to make this scenarios where some working knowledge list to the play list. CDDB is a commercial ser- assignment permanent. vice,and Linux players more typically access will be extremely useful in finding your way To grab multiple (or all the) tracks the FreeDB database at www.freedb.org through the command line jungle. from a CD, add -t and the numbers of the instead.

www.linux-magazine.com January 2005 83 LINUX USER Command Line

/dev/cdrom exists U Encoding audio_01.wav tU but isn't accessible. o audio_01.wav.mp3 ... check the permissions for the device file. As Box 1 explains, Of course, you can specify a it makes sense to assign the name for the output file: device file to the cdrom group, and to add users who lame audio_01.wav 01.mp3 need CD access to that group. To grab a single track with If you need better sound cdparanoia, simply stipulate quality, you can change the the track number, for example bit rate by setting the -b flag. For example, lame uses a bit cdparanoia 1 rate of 192Kbps when you type the following It is just as easy to define a Figure 1: Grabbing audio tracks in next to no time with cdda2wav. list of tracks. If you want to lame -b 192 1.wav 1.mp3 grab the first five tracks, for example, offers a source code download, but you can do so by specifying 1-5. If you search machines such as rpmseek.com As lame does not support wildcards by are grabbing multiple tracks, again you [4] will point you to pre-compiled pack- default (that is, a command such as lame need to set the -B flag to avoid storing ages for Debian and RPM-based *.wav will display an error message and everything in a single wav file, as in distributions. Unfortunately, the link to quit), you will need a for loop to harness the Debian package is broken, but you the full power of the command line and $ cdparanoia -B 1-2 can check out [5] for packages for all convert all the wav files in a directory, ... three Debian branches (stable, testing, using the basename command to remove outputting to track01.cdda.wav and unstable). the .wav extension, and assign the cor- If you run lame against a wav file rect extension to your MP3 files. cdparanoia names the wav files without specifying any options, the tool track01.cdda.wav, track02.cdda.wav, etc. produces a MP3 file with a bit rate of for i in *.wav; do lame $i U 128Kbps. And if you fail to supply a `basename $i .wav`.mp3; done Converting wav to MP3 name for the MP3 file, lame will just pick There are quite a few command line a name for you: Converting MP3 back to wav tools for converting wav files to MP3, lame also has an option for converting and lame [3] is one of the more popular $ lame audio_01.wav MP3 files back to wav – of course, lossy conversion tools. The project homepage ... MP3 encoding means that the sound quality of the wav file will not compare Box 1: Access Privileges for the CD-ROM Device well to the original audio CD. The The various distributions have different group for security reasons (since it would --decode option allows you to turn the ways of defining which users should have give them write access to hard disk parti- tables, and convert MP3 back to wav: access to the CD-ROM drive. ls -l outputs the tions), the administrator, root, needs to permissions for the device: change the group assignment for cdrom: $ lame --decode 01.mp3 input: 01.mp3 (44.1 kHz, 2 chU $ls-l/dev/cdrom chown root.cdrom /dev/hdc annels, MPEG-1 Layer III) lrwxrwxrwx 1 root root 3 After doing so,and still working as the U Jun 7 00:41 /dev/cdrom -> hdc admin user,edit /etc/group,adding the user output: 01.mp3.wav (16 bit, Mi crosoft WAVE) As is to be expected,/dev/cdrom is a sym- account that needs access to the drive to the U bolic link that points to the device file:hdc in cdrom group.You can add multiple accounts skipping initial 1105 samples our case.You need another ls command to by separating them with a comma (avoiding (encoder+decoder delay) display the permissions here: spaces),for example: Frame# 807/4348 128 kbps L R $ls -l /dev/hdc cdrom:x:24:huhn,petronella GLOSSARY brw-rw---- 1 root disk 22, 0 To apply these changes, the user needs to log Mar 14 2002 /dev/hdc off and back on to the system.You can run Bit rate:When compressing sound files, the the groups command to check if everything Debian Specifics bit rate (which is typically given in Kbits per has worked out; the command displays a second) defines the sound quality of the MP3 While most distributions simply change the user’s group memberships on the command file.The higher the bit rate, the better the owner of the drive when a user logs onto a line: MP3 file will sound. On the downside, the file system,Debian has a different approach.The size increases to match. Under normal cir- drive belongs to the disk group by default. As $groups cumstances,bit rates between 128 and it is not advisable to add users to the disk audio cdrom video 192Kbps make sense.

84 January 2005 www.linux-magazine.com Command Line LINUX USER

In this scenario lame again chooses the $ oggenc 01.wav filenames, unless you explicitly specify Opening with wav module: U them. And again, a for loop can help you WAV file reader convert multiple files with one fell Encoding "01.wav" to swoop. "01.ogg" The [6] command line player at quality 3.00 is another useful alternative. The player [ 17.8%] U has a -w option for converting MP3s to [ 1m24s remaining] wav files: Again the tool automatically chooses $ mpg123 -w file.wav 01.mp3 filenames, but in contrast to the pro- ... grams we have looked at so far, oggenc Playing MPEG stream from 01.mp3 supports wildcards and can convert all MPEG 1.0 layer III, 128 kbit/s,U the wav files in a directory with a single Figure 2: oggenc supports wildcards. 44100 Hz joint-stereo command (oggenc *.wav) (Figure 2). [1:53] Decoding of 01.mp3 finisU To choose a different name for the out- From Ogg to wav hed. put file, you need to set the -o flag: Converting Ogg Vorbis to wav is just as simple with oggdec. Again you need the Note the importance of the order of the oggenc 01.wav -o bla.ogg -o option if you want to specify a file- input and output files. You need to spec- name. And again, the program supports ify the output file first, using the -w One of oggenc‘s most practical features is wildcards. The following command option, and then the name of the MP3 its ability to read information from com- file you want to convert. mand line parameters for the artist, (-a), oggdec *.ogg album name (-l), title (-t), genre (-G), From wav to Ogg and date (-d) while converting a file: converts all the Ogg Vorbis files in a Ogg Vorbis is a free alternative to MP3 – directory into wav files. free in the sense that the codecs are oggenc 01.wav -a U patent-free and released under a free "The Alan Parsons Project" U Conclusion license. Additionally, Ogg Vorbis sup- -t "Sirius" -l "Eye in the Converting audio files on the command ports superior sound quality. If you Sky" -G "Pop" -d "1981" U line is easy: if the tool you are using does install the vorbis-tools from your distri- -o blubb.ogg not support wildcards, bash gives you a bution media, you should have the workaround in the form of a simple for ogg123 command line player, along with This allows current players, such as loop. If all of this sounds like too much an encoder and decoder. oggenc is the XMMS, to display this information when effort, or too much typing, check out the tool you need to convert wav files to Ogg playing the tracks later by reading the abcde [7] script, which uses the com- Vorbis format: ID3 tags. mands we have looked at in this issue, adding comments or ID3 tags, and even Listing 1: cdparanoia -Q Output querying the CDDB database to retrieve ■ 01 $ cdparanoia -Q names and track titles. 02 cdparanoia III release 9.8 (March 23, 2001) INFO 03 (C) 2001 Monty and Xiphophorus 04 Report bugs to [email protected] [1] cdda2wav homepage:http://www. 05 http://www.xiph.org/paranoia/ cdda2wav.de/ 06 Table of contents (audio tracks only): [2] cdparanoia website:http://www.xiph. 07 track length begin copy pre ch org/paranoia/ 08 ======[3] lame project:http://lame.sourceforge. 09 1. 8515 [01:53.40] 33 [00:00.33] no no 2 net/ 10 2. 20750 [04:36.50] 8548 [01:53.73] no no 2 [4] Search machine for RPMs and Debian 11 3. 21827 [04:51.02] 29298 [06:30.48] no no 2 packages: 12 4. 9848 [02:11.23] 51125 [11:21.50] no no 2 http://rpmseek.com/ 13 5. 33262 [07:23.37] 60973 [13:32.73] no no 2 [5] lame for Debian Linux:ftp://ftp.nerim. 14 6. 19750 [04:23.25] 94235 [20:56.35] no no 2 net/debian-marillat/ 15 7. 21835 [04:51.10] 113985 [25:19.60] no no 2 [6] Command line MP3 player 16 8. 16148 [03:35.23] 135820 [30:10.70] no no 2 mpg123:http://www.mpg123.de/ 17 9. 17562 [03:54.12] 151968 [33:46.18] no no 2 [7] Heike Jurzik:“Music was my first love: 18 10. 22103 [04:54.53] 169530 [37:40.30] no no 2 abcde – A Better CD Encoder”,Linux Mag- 19 TOTAL 191600 [42:34.50] (audio only) azine #30 / May 2003,p82.

www.linux-magazine.com January 2005 85