#!/bin/bash
#
# BpmDj: Free Dj Tools
# Copyright (C) 2001-2011 Werner Van Belle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

songname="$2"
songlocation=$songname
rawname=$1"`basename "$2"`.raw"
extension=${songname##*.}

#echo "BPMDJ-RAW arguments"
#echo LOCATION = $songlocation
#echo RAWNAME = $rawname
#echo EXTENSION = $extension
#echo DECODER = $DECODER

# if the raw filename already exists, don't write it
(test -f "$rawname") && exit 0;

# the song must exist
(! (test -f "$songlocation") ) && songlocation=music/"$songlocation" \
&& (! (test -f "$songlocation") ) && exit 2;

#____________________________________________________________
# DECODER 1: mpg123 decoder
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if test $DECODER = "1"; then
    echo "Information: decoder is mpg123"
    mpg123 -r 44100 --stereo -s "$songlocation" >"$rawname" ;

#____________________________________________________________
# DECODER 2: mpg321 decoder
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elif test $DECODER = "2"; then
    echo "Information: decoder is mpg321"
    mpg321 -s "$songlocation" >"$rawname";
    
#____________________________________________________________
# DECODER 3: ogg123 decoder
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elif test $DECODER = "3"; then
    echo "Information: decoder is ogg123"
    ogg123 --audio-buffer 0 -q -d raw -f "$rawname" "$songlocation";

#____________________________________________________________
# DECODER 4: mplayer 1.0pre6-3.3.5 decoder
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elif test $DECODER = "4"; then
    echo "Information: decoder is mplayer 1.0pre6"
    mplayer "$songlocation" -ao pcm -aofile "$rawname" -af resample=44100 -vo null -quiet;
    
#____________________________________________________________
# DECODER 5: mplayer 1.0pre7-3.3.5 decoder
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elif test $DECODER = "5"; then
    echo "Information: decoder is mplayer 1.0pre7"
    # the pre7 release is somewhat troubling because it seems unable to 
    # use a resmapling filter of the same freq as the normal data format
    mplayer "$songlocation" -ao pcm:file="$rawname" -af resample=44100 -vo null -quiet;
    (test -f "$rawname") && exit 0;
    mplayer "$songlocation" -ao pcm:file="$rawname" -vo null -quiet;
fi

(test -f "$rawname") && exit 0;
exit 1
