#!/bin/bash

# CD Ripping Script for Rivendell
#
#   (C) Copyright 2003-2005 Fred Gleason <fredg@paravelsystems.com>
#
#      $Id: rd_rip_cd,v 1.14 2007-02-14 21:59:12 fredg Exp $
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2 as
#   published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Usage:
#  rd_rip_cd [args]
#
# Argument Order:
#      <track> = CD track to rip.
#     <ripdev> = CD Device
#   <paranoia> = Paranoia level. 0 = Normal, 1 = Low, 2 = None
#      <level> = Normalization level, expressed as linear ratio, 
#                0 = No normalization
#     <format> = Output format, 0 = PCM16, 1 = MPEG-1 Layer 2
#      <chans> = Output channels, 1 or 2
#   <samprate> = Output sample rate, in samples/sec
#    <bitrate> = MPEG Bitrate, in kbps, or 0 for PCM16
#    <wavfile> = Destination file for ripped WAV data.
#    <datfile> = Temporary file for peak level data.
#    <tmpfile> = Temporary file for intermediate audio

#
# Some sane variable names
#
TRACK=${1}
RIPDEV=${2}
PARANOIA_LEVEL=${3}
NORMAL_LEVEL=${4}
FORMAT=${5}
CHANS=${6}
SAMPRATE=${7}
BITRATE=${8}
WAVE=${9}
PEAK=${10}
WORK=${11}

#
# Sanity Checks
#
set -e
if [ $FORMAT = "2" ]; then    # MPEG Layer 3 not supported!
   exit 1;
fi 

#
# Set Paranoia Level
#
if [ $PARANOIA_LEVEL = "0" ]; then
    PARANOIA_FLAG="--paranoia"
fi
if [ $PARANOIA_LEVEL = "1" ]; then
    PARANOIA_FLAG="-P 10"
fi

#
# Set Resample Algorithm
#
if [ $SAMPRATE -ne 44100 ]; then
    RESAMPLE_FLAG="resample"
#    RESAMPLE_FLAG="resample -q"
fi

# ############################################################################
# Encode to PCM16
if [ $FORMAT = 0 ]; then
    if [ $NORMAL_LEVEL = "0" ]; then
# ############################################################################
# # No Normalization, do single pass
#	echo "SINGLE PASS, PCM, NO NORMALIZATION!"
	cdda2wav --quiet -D $RIPDEV -t $TRACK $PARANOIA_FLAG - | sox -t wav - -t raw -w -s -r $SAMPRATE -c $CHANS - $RESAMPLE_FLAG | rdfilewrite --channels=$CHANS --sample-rate=$SAMPRATE $WAVE
# ############################################################################

# ############################################################################
# # Normalization needed, do two passes
    else
#
# Phase One - Rip to 44.1 Stereo WAV, get peak level data
#
#	echo "PASS ONE, PCM, NORMALIZATION!"
	cdda2wav --quiet -D $RIPDEV -t $TRACK $PARANOIA_FLAG - | sox -t wav - $WORK stat -v 2> $PEAK

#
# Calculate Needed Gain
#
	PEAK_LEVEL=`cat $PEAK`
	RATIO=`echo $NORMAL_LEVEL*$PEAK_LEVEL | bc -l`

#
# Phase Two - Encode audio to final destination
#
#	echo "PASS TWO, PCM, NORMALIZATION!"
	sox -v $RATIO $WORK -t raw -w -s -c $CHANS -r $SAMPRATE - $RESAMPLE_FLAG | rdfilewrite --channels=$CHANS --sample-rate=$SAMPRATE $WAVE
    
#
# Clean Up
#
	rm -f $PEAK
	rm -f $WORK
# ############################################################################
    fi
fi
# ############################################################################

# ############################################################################
# Encode to MPEG Layer 2
if [ $FORMAT = 1 ]; then
    if [ $CHANS = 1 ]; then
	MODE="m"
    else
	MODE="s"
    fi
    case "$SAMPRATE" in
	"32000")
	    LAMERATE="32"
        ;;
	"44100")
	    LAMERATE="44.1"
        ;;
	"48000")
	    LAMERATE="48"
	;;
    esac
    if [ $NORMAL_LEVEL = "0" ]; then
# ############################################################################
# # No Normalization, do single pass
#	echo "SINGLE PASS, MPEG, NO NORMALIZATION!"
	cdda2wav --quiet -D $RIPDEV -t $TRACK $PARANOIA_FLAG - | sox -t wav - -t raw -r $SAMPRATE -c $CHANS - $RESAMPLE_FLAG | toolame -t 0 -W -m $MODE -b $BITRATE -s $LAMERATE - $WAVE
# ############################################################################

# ############################################################################
# # Normalization needed, do two passes
    else
#
# Phase One - Rip to 44.1 Stereo WAV, get peak level data
#
#	echo "PASS ONE, MPEG, NORMALIZATION!"
	cdda2wav --quiet -D $RIPDEV -t $TRACK $PARANOIA_FLAG - | sox -t wav - $WORK stat -v 2> $PEAK

#
# Calculate Needed Gain
#
	PEAK_LEVEL=`cat $PEAK`
	RATIO=`echo $NORMAL_LEVEL*$PEAK_LEVEL | bc -l`

#
# Phase Two - Encode audio to final destination
#
#	echo "PASS TWO, MPEG, NORMALIZATION!"
	sox -v $RATIO $WORK -t raw -c $CHANS -r $SAMPRATE - $RESAMPLE_FLAG | toolame -t 0 -W -m $MODE -b $BITRATE -s $LAMERATE - $WAVE
    
#
# Clean Up
#
	rm -f $PEAK
	rm -f $WORK
# ############################################################################
    fi

fi




# End of rd_rip_cd
