Friday, July 13, 2007

Writing Audio CDs in Linux

I used to reboot my dual boot machine to login to Windows just because I wanted to write some audio CDs to play in my car. But off late I'm putting extra effort not to leave Linux and go to Windows. So I set out to write my own Nero replacement for audio CDs. This is what I came up with. I have been using it for a while and it works flawlessly for me. You might need to make few modifications to suite your needs.



#! /bin/sh

#
# Make an audio CD out of the given mp3 files.
# Author :
# Fri Jul 13 13:15:19 CDT 2007
#

MODE="TAO"
DEVICE="ATA:0,0,0"
TMPDIR=/tmp/audio

if [ ! -d ${TMPDIR} ]; then
mkdir ${TMPDIR}
fi

# don't do rm * at /
cd ${TMPDIR}
if [ $? -eq 0 ]; then
rm -rf ${TMPDIR}/*
fi

while getopts m:d: opt
do
case "$opt" in
m) MODE="$OPTARG";;
d) DEVICE="$OPTARG";;
\?) echo "Usage: $0 [-m T/D] [-d ] <input files>\n"
echo -e "\t -m : CD write Mode : D - DAO/T - TAO ( Default is TAO )"
echo -e "\t -d : CD device to use"
exit 2;;
esac
done
shift $(($OPTIND-1))

if [ $# -eq "0" ]; then
echo "Usage: $0 [-m tao/dao] [-d ] <audio files>\n"
echo -e "\t -m : CD write Mode : D - DAO/T - TAO ( Default is TAO )"
echo -e "\t -d : CD device to use"
exit
fi

echo "Copying files to work area and Removing spaces from file names..."
for i in $*
do
if [ ! -f "$i" ]; then
echo "Couldn't find the file [${i}]. Perhaps the file has spaces and you didn't quote t
hem? "
exit
fi
FILE_N=`echo "$i" | tr ' ' '_'`
TARGET_FILE=`basename ${FILE_N}`
cp "$i" ${TARGET_FILE}
done

echo "Converting mp3 files to wav files..."
for i in *;
do
echo "$i"
mpg321 --rate 44100 --stereo --buffer 3072 --resync -w `basename $i`.wav $i;
done

echo "Normalizing Audio.i.e. Adjusting the volumes..."
#normalize-audio -m *.wav
normalize -m *.wav


if [ ${MODE} == "DAO" ]; then
# if DAO mode, create the TOC and then
# write to CD
echo "Generating TOC for DAO mode"
DAOTOC_FILE="/tmp/toc_cd.toc"
echo CD_DA > ${DAOTOC_FILE}
echo >> ${DAOTOC_FILE}
for fff in *.wav
do
echo -e "TRACK AUDIO\nAUDIOFILE \"${fff}\" 0\n" >> ${DAOTOC_FILE}
done
echo "--------------------------------"
cat ${DAOTOC_FILE}
echo "--------------------------------"
echo "Writing to CD...DAO mode"
cdrao write --device ${DEVICE} ${DAOTOC_FILE}
else
# if TAO we can now write to cd
echo "Writing to CD...TAO mode"
cdrecord -v dev=${DEVICE} -audio -pad *.wav
fi



I would be happy if you drop a note if you find it useful.