HOWTO Create an Audio CD

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

[edit] Introduction

I've put together this guide for other Gentoo users as a guide to creating audio CDs. Although there exists plenty of information on the Internet how to do this under Linux generally there is no specifc information for Gentoo. I have pulled together information from many online sources and added any Gentoo specific information as required.

[edit] Getting organized

[edit] Applications used

There are quite a few bits of software mentioned in this HOWTO. Fortunately it is all avalible in the portage tree for emerging. Here is a simple table of the software you will require for making audio CDs. Please note you don't need to install every single decoder, for most people Lame is sufficent on its own.

Package Description
media-sound/mpg123 The original MP3 decoder/player for *nix
media-sound/mpg321 Opensource equivelent to mpg123
media-sound/madplay The Madplay MP3 decoder/player
media-sound/lame The Lame MP3 decoder/player
media-sound/vorbis-tools The OGG Vorbis toolkit
media-video/mplayer Mplayer media player
media-video/ffmpeg FFmpeg audio/video encoder/decoder
media-sound/sox Sox, the sound processing 'Swiss Army Knife'
app-cdr/cdrtools A set of tools for CD recording, including cdrecord
app-cdr/cdrdao Burn CDs in disk-at-once mode
media-sound/normalize Audio file volume normalizer


Note: On systems (distributions) where cdrecord is unavailable, wodim provides the same functionality with identical syntax (at least as far as this howto is concerned).

[edit] Organizing the music

I recommend you create a new directory in your home for your music cd to work in. Gather all the music you want to add to your music cd and place it in this directory. If you have copied or move the files from elsewhere ensure that the files have sensible permissions. Don't worry too much if the filenames are mixed case and format we will clean that up in the next section.

[edit] Clean up the file names

If any filenames contain spaces, first convert them to underscores:

Code:
for i in *.mp3; do mv "$i" `echo $i | tr ' ' '_'`; done

This first step is important because, even if *nix itself allows spaces in filenames, most programs get confused by them.

Also, if your MP3 files came from DOS/Windows or perhaps a Samba server, they may have uppercase extensions. You can convert whole names to lowercase or just extensions. For everything lowercase do:

Code:
for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

To convert just extensions:

Code:
for i in *.MP3; do mv "$i" "`basename "$i" .MP3`.mp3"; done

These two methods often do not work as stated, however. Converting to the same filename that only has case changes, in effect is moving the file to itself and you are doing nothing. In these cases, firs change the name to a very different one and then convert it again to the one you desired.

[edit] Convert to WAV

Before we can create an audio CD we need to get the audio in a format the burning software (cdrecord) understands ie, WAV format. How you do this depends on what format your audio files are currently in and what tools you have available. The first part of the section below describes how to use ffmpeg to convert your files. The remainder describes how to use mpg123, ogg123, and mplayer. Jump to the correct subsection for your audio files and software.

[edit] FFmpeg (many common formats)

FFmpeg can convert and resample OGG, MP3, M4A, WMA, FLAC, and many other file formats. To convert a single file to a CD quality WAV file:

Code:
ffmpeg -i infile.xxx -acodec pcm_s16le -ar 44100 -ac 2 -f wav outfile.wav

An easy way to convert all of your files at once to place all of your songs in a single folder with no other files, and then:

Code:
for i in *; do ffmpeg -i "$i" -acodec pcm_s16le -ar 44100 -ac 2 -f wav "$i.wav"; done

Note that that command will produce files that end with things such as ".mp3.wav". This will not cause problems as you'll probably be deleting all these WAV files once your disc is burned anyway! More creative commands that produce prettier names or better organization (such as outputting files to some other directory) are left as an exercise to the reader. :)

If FFmpeg, as used above, is given an input file that is already in WAV format, it will resample/copy the data as necessary. You may use FFmpeg to ensure that existing WAV files are in the correct format.

[edit] MP3 format

There is a wide variety of decoders available for MP3 format audio. This HOWTO will only cover the three main ones in use, mpg123/mpg321, MAD and Lame. Of the three covered here, Lame produces the best audio and copes with strange and damaged MP3 files the best. Unless you have a reason not to, it's recommended you use Lame.

[edit] mpg123 and mpg321

Originally, there was only mpg123. However, it uses proprietary licensing, and now there's an open source replacement, mpg321. Both commands use the same syntax:

Code:
for i in *.mp3; do mpg321 -w "`basename "$i" .mp3`.wav" "$i"; done

When decoding 22khz MP3 files, the output of mpg123 may be distorted. If you're converting with mpg123, use:

Code:
for i in *.mp3; do mpg123 --rate 44100 --stereo --buffer 3072 --resync -w `basename $i .mp3`.wav $i; done

It has been noted that with some MP3 files, mpg123 output is distorted regardless. Also, mpg123 flatly refuses to decode some MP3 files.

[edit] MAD

Another MP3 player/decoder is called madplay. With madplay, the command line is:

Code:
for i in *.mp3; do madplay -o "`basename "$i" .mp3`.wav" "$i"; done

Unfortunately, madplay also has problems with some MP3 files.

[edit] Lame

The lame encoder, which has a decoding mode, seems to handle difficult cases very well.

Code:
for i in *.mp3; do lame --decode "$i" "`basename "$i" .mp3`.wav"; done

[edit] OGG format

Well, this HOWTO is focused on MP3 files, but OGG standard is an alternative which many people prefer over MP3 because of the licensing. To deal with OGG files, you need to use:

Code:
for i in *.ogg ; do ogg123 -d wav -f "`basename "$i" .ogg`.wav" "$i"; done

[edit] FLAC, WMA and other formats

You can use Mplayer to convert audio in many different formats to WAV with automatic resampling to 44100Hz. Mplayer is known to be able to read almost anything, and it can convert your .OGG, .MP3, .FLAC, .WMA etc. Here is an example to convert WMA files to WAV.

Code:
for i in *.wma; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader:file="${i%.wma}.wav" "$i" ; done

Basically if Mplayer can play the file, you can convert it and put it on an audio CD!

[edit] Check the output

After the conversion, run file *.wav and check the output for any files that differ from 16 bit, stereo 44100 Hz.

If there are files with different characteristics, convert them to the above format. For example, to convert file track01.wav to obtain sample rate 44.1 kHz using sox, you could use:

Code:
sox track01.wav -r 44100 track01-new.wav resample

or, if the above introduces static when converting mono files:

Code:
sox track01.wav -r 44100 -c 2 track01-new.wav

The sox command-line options are somewhat cryptic for the casual user. Look at http://www.spies.com/Sox/sox.tips.html for some tips on usage.

You may also accomplish the same thing with ffmpeg if you do not have sox. The command-line options are equally cryptic, but the resample command is identical to the conversion command described above:

Code:
ffmpeg track01.wav -acodec pcm_s16le -ar 44100 -ac 2 -f wav track01-new.wav

[edit] Normalize the audio

Normalisation is a process during which all the sound files are brought to the same relative loudness level. The following syntax (-m is for mix mode, where all files become as loud as the average overall volume) should be used:

Code:
normalize -m *.wav

[edit] Creating the audio CD

Writing audio CDs is very similar to the steps for data CDs. You can choose between two techniques: DAO or TAO. TAO (track at once) is less suitable for music, because you will hear clicks between the individual tracks. It is described anyway, because it is a little bit easier to deal with. There is also some information on configuring your kernel for CD burning. If you've already got cdrecord setup and working you can skip this.

[edit] Some notes on CD burning under Linux

There is a little bit of configuration required to make Linux understand CD burning. The easiest and most reliable to use is probably SCSI however most people do not have SCSI based systems. For everyone else using IDE based CD burners it depends which kernel series you use. For users of 2.4 its recommended you use ide-scsi whereas users of 2.6 should use the new ATA interface. The old ATAPI interface is also covered for completeness. The examples use the new 2.6 ATA interface so you will have to adjust your command line as appropriate.

[edit] SCSI

SCSI is fairly simple to use. With cdrecord simply do the following

Code:
cdrecord -scanbus

This should give you an overview of all the SCSI devices in your system. Identify which one is your burner and then query the device.

Code:
cdrecord dev=0,0,0 driveropts=help -checkdrive

This should tell you what driver options you can use. Most importantly if your drive supports buffer underrun protection and how to enable it.

[edit] IDE-SCSI on Linux 2.4

For help with IDE-SCSI please refer to the SCSI Burning HOW-TO

[edit] ATA on Linux 2.6

IMPORTANT: Make sure the user you're trying to burn CDs with is a member of the 'cdrw' group.

Using ATA to access your drive is similar to SCSI. For information on configuring kernel ATA support, see the ATAPI CD Burning HOWTO. To start off we need to scan the ATA bus.

Code:
cdrecord dev=ATA -scanbus

The procedure is the same as for SCSI. You should have an overview of all the ATA CD/DVD devices in your system. Identify which one is your burner and then query the device. For example, here is the output from my system.

Code:
gking@terror ~/musiccd $ cdrecord --scanbus dev=ATA
Cdrecord-Clone 2.01 (i686-pc-linux-gnu) Copyright (C) 1995-2004 Jörg Schilling
cdrecord: Warning: Running on Linux-2.6.8-gentoo-r3
cdrecord: There are unsettled issues with Linux-2.5 and newer.
cdrecord: If you have unexpected problems, please try Linux-2.4 or Solaris.
cdrecord: Warning: Linux-2.6.8 introduced incompatible interface changes.
cdrecord: Warning: SCSI transport does no longer work for suid root programs.
cdrecord: Warning: if cdrecord fails, try to run it from a root account.
scsidev: 'ATA'
devname: 'ATA'
scsibus: -2 target: -2 lun: -2
Warning: Using badly designed ATAPI via /dev/hd* interface.
Linux sg driver version: 3.5.27
Using libscg version 'schily-0.8'.
scsibus0:
        0,0,0     0) 'PLEXTOR ' 'CD-R   PX-W1210A' '1.07' Removable CD-ROM
        0,1,0     1) *
        0,2,0     2) *
        0,3,0     3) *
        0,4,0     4) *
        0,5,0     5) *
        0,6,0     6) *
        0,7,0     7) *
scsibus1:
        1,0,0   100) *
cdrecord: Warning: controller returns wrong size for CD capabilities page.
        1,1,0   101) '        ' '54X CD-ROM      ' '6.43' Removable CD-ROM
        1,2,0   102) *
        1,3,0   103) *
        1,4,0   104) *
        1,5,0   105) *
        1,6,0   106) *
        1,7,0   107) *

As you can clearly see the ATA bus ID is 0,0,0 for my Plextor CDRW drive. The next step is to determine the capabilites of the drive.

Code:
cdrecord dev=ATA:0,0,0 driveropts=help -checkdrive

This should tell you what driver options you can use. Most importantly if your drive supports buffer underrun protection and how to enable it. For example, here's my plextor cdrw again.

Code:
gking@terror ~/musiccd $ cdrecord dev=ATA:0,0,0 driveropts=help -checkdrive
Cdrecord-Clone 2.01 (i686-pc-linux-gnu) Copyright (C) 1995-2004 Jörg Schilling
cdrecord: Warning: Running on Linux-2.6.8-gentoo-r3
cdrecord: There are unsettled issues with Linux-2.5 and newer.
cdrecord: If you have unexpected problems, please try Linux-2.4 or Solaris.
cdrecord: Warning: Linux-2.6.8 introduced incompatible interface changes.
cdrecord: Warning: SCSI transport does no longer work for suid root programs.
cdrecord: Warning: if cdrecord fails, try to run it from a root account.
scsidev: 'ATA:0,0,0'
devname: 'ATA'
scsibus: 0 target: 0 lun: 0
Warning: Using badly designed ATAPI via /dev/hd* interface.
Linux sg driver version: 3.5.27
Using libscg version 'schily-0.8'.
Device type    : Removable CD-ROM
Version        : 0
Response Format: 1
Vendor_info    : 'PLEXTOR '
Identifikation : 'CD-R   PX-W1210A'
Revision       : '1.07'
Device seems to be: Generic mmc CD-RW.
Driver options:
burnfree        Prepare writer to use BURN-Free technology
noburnfree      Disable using BURN-Free technology

So we can quite clearly see the correct driver options for my drive is 'burnfree'. (e.g. cdrecord dev=ATA:0,0,0 driveropts=burnfree ...)

Note: In linux 2.6.xx you can bypass all the scsi numbers and just use /dev/hdx instead of ATA:0,0,0 (or whatever the device is on your system). Cdrecord will complain but it still works. There been lovely little arguments between Linus and Jorg over this...still they manage to make it all work for us. :)

[edit] TAO Mode with cdrecord

Cdrecord writes CD images as audio tracks if the option -audio is specified. The other options are identical to those used for writing data-CDs (unless you have very special requirements).

Code:
cdrecord -v dev=ATA:0,0,0 -audio -pad *.wav

By doing this, you will produce an audio CD which has a 2 seconds of pause between audio tracks. Also note the -pad argument is neccessary, because all audio tracks on the CD must be adjusted for the proper data length, which is not always the case with mp3 files.

Another way, would be to convert WAV files with sox into CDR format before burning:

Code:

sox file.wav file.cdr

And then burn as so

Code:
cdrecord -v dev=ATA:0,0,0 -audio -pad *.cdr

[edit] DAO Mode with cdrdao

DAO, Disc-At-Once, is as of now the only method for burning a CD without a 2-second pause between the tracks. It's useful for burning party mixes. The program for burning CDs in DAO mode is cdrdao.

The cdrdao program uses description files called TOC (Table Of Contents). The simplest way is to simply create such file yourself in a text editor of your choice. Here is a self-explanatory example:

File:

CD_DA

TRACK AUDIO
AUDIOFILE "mix-01.wav" 0

TRACK AUDIO
AUDIOFILE "mix-02.wav" 0

TRACK AUDIO
AUDIOFILE "mix-03.wav" 0

TRACK AUDIO
AUDIOFILE "mix-04.wav" 0

TRACK AUDIO
AUDIOFILE "mix-05.wav" 0

The 0 (zero) after the wave filename means start from the beginning of the file. There can be a second number providing the length (time) of file to record.

You can also use a program called tocgen to generate the toc file.

Alternatively, to add CD-Text support to the compilation, the toc file must be like follows:

File:

CD_DA
CD_TEXT{

   LANGUAGE_MAP {
      0 : EN
  }
  LANGUAGE 0 {
      TITLE "The disc title"
      Performer "the performer"
  }

}
TRACK AUDIO

 CD_TEXT {
     LANGUAGE 0 {
        TITLE "mix 1"
        PERFORMER "the performer"
     }
 }

AUDIOFILE "mix-01.wav" 0

TRACK AUDIO
CD_TEXT {

     LANGUAGE 0 {
        TITLE "mix 2"
        PERFORMER "the performer"
     }
 }

AUDIOFILE "mix-02.wav" 0

After we have created our toc file we can now burn the CD.

Code:

cdrdao write --device ATA:0,0,0 cd.toc

[edit] Mixed mode CD-ROMs

There is not much to say about this. Just indicate the type of the (subsequent) images with the options -data and -audio. Example:

Code:

cdrecord -v dev=ATA:0,0,0 -data cd_image -audio *.cdr

[edit] Quick and dirty bash script

If you're in a hurry you can always use the following bash script to do the dirty work for you - uses lame to convert to *.wav and then you need to manually run the burn script but it saves plenty of keystrokes: Save the file to ~/makeCD and chmod 744, change to the directory with your mp3's and run ~/makeCD.

Code:

#!/bin/bash
for i in *.mp3; do mv "$i" `echo $i | tr ' ' '_'`; done
for i in *.mp3; do lame --decode "$i" "`basename "$i" .mp3`.wav"; done
normalize *.wav
echo CD_DA > cd
for x in *.wav; do echo "
TRACK AUDIO
AUDIOFILE \"$x\" 0" >> cd; done;

Then 'su' and run:

Code:

cdrdao write --device ATA:0,0,0 cd

You'll need to change the ATA stuff to match but that's what works for me :)

[edit] Refrences

This HOWTO is an amalgamation of a couple of other HOWTO's and articles. Most notably:

The CD Writing HOWTO
The MP3 CD Burning mini-HOWTO
The CDROM HOWTO

Personal tools