#!/bin/bash
#
#
# audio convert 0.1
#
# a program to convert wav, ogg, mp3, mpc, flac, ape or wma files into wav,
# ogg, mp3, mpc, flac or ape files. with an easy to use interface it's
# actually possible to fill in the tags for a few formats, and choose the
# quality of compression.
#
# copyright (C) 2005 linfasoft
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
# USA
#
# dependencies
#	bash
#       zenity
#	awk
#       mplayer -- if you want to decode wma files
#       lame
#       vorbis tools
#       id3tag
#       musepack-tools
#	flac
#	mac
#
# based on
#	wom_audioconverter, by yekcim <yeknan@yahoo.fr>, http://yeknan.free.fr.
#
# history
#	july the second, twthousandandfive, version 0.1 released!

version="0.1"
#################################################
#       TRADUCTIONS
        ###### Default = English #####
        title="audio convert "$version""
        pleasesel="please select at least one file."
        noselec=""$title" convert audio files. "$pleasesel""
        choix="extension of output file:"
        warning="warning"
        proceed="already exists. overwrite?"
        recur=""$title" can't convert directory. "$pleasesel""
        conversion="converting file:"
        ask_artist="enter the artist name:"
        ask_album="enter the album name:"
        ask_song="enter the song name:"
        ask_track="enter the track number:"
        ask_quality="select the desired quality:"
	confirmation="do you want to convert"
case $LANG in
        ######## Français ########
        fr* )
        	title="WOM audioconverter "$version""
        	pleasesel="Merci de selectionner au moins un fichier."
        	noselec=""$title" permet de convertir des fichiers audio. "$pleasesel""
        	choix="Format du fichier de sortie :"
        	warning="Attention"
        	proceed="existe deja. Ecraser ?"
        	recur=""$title" ne permet pas la conversion de dossiers. "$pleasesel""
        	conversion="Conversion du fichier :";;
	it* )
		title="audio convert "$version""
		pleasesel="per favore, scegli almeno un file."
		noselec=""$title" converte i file audio. "$pleasesel""
		choix="formato di conversione:"
		warning="attenzione"
		proceed="esiste! sovrascrivo?"
		recur=""$title" non pu convertire directory. "$pleasesel""
		conversion="sto convertendo il file:"
		ask_artist="immetti il nome dell'artista:"
		ask_album="immetti il nome dell'album:"
		ask_song="immetti il nome della canzone:"
		ask_track="immetti il numero della traccia:"
		ask_quality="scegli la qualit del file:"
		confirmation="vuoi convertire"
esac

#################################################
#       FONCTIONS
get_field_names ()
{
        artist_name=`zenity --entry --title="$title" --text="$ask_artist"`
        album_name=`zenity --entry --title="$title" --text="$ask_album"`
        song_name=`zenity --entry --title="$title" --text="$ask_song"`
        track_number=`zenity --entry --title="$title" --text="$ask_track"`
}

get_ogg_quality ()
{
        zenity --title="$title" --list --radiolist --column="" --column="$ask_quality" -- "-2" FALSE "-1" FALSE "0" FALSE "1" FALSE "2" FALSE "3" FALSE "4" FALSE "5" FALSE "6" TRUE "7" FALSE "8" FALSE "9" FALSE "10"
}

get_mp3_quality ()
{
        zenity --title="$title" --list --radiolist --column="" --column="$ask_quality" FALSE "medium" FALSE "standard" TRUE "extreme" FALSE "insane"
}

get_mpc_quality ()
{
        zenity --title="$title" --list --radiolist --column="" --column="$ask_quality" FALSE "thumb" FALSE "radio" TRUE "standard" FALSE "xtreme"
}

get_flac_quality ()
{
	zenity --title="$title" --list --radiolist --column="" --column="$ask_quality" FALSE "0" FALSE "1" FALSE "2" FALSE "3" FALSE "4" FALSE "5" FALSE "6" FALSE "7" TRUE "8"
}

get_mac_quality ()
{
	zenity --title="$title" --list --radiolist --column="" --column="$ask_quality" FALSE "1000" FALSE "2000" TRUE "3000" FALSE "4000" FALSE "5000"
}

mp3_encode ()
{
	lame -m auto --preset $quality "$2" "$3" 2>&1 | awk -vRS='\r' '(NR>3){gsub(/[()%|]/," ");print $2; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
}

ogg_encode ()
{
	oggenc "$2" -a "$artist_name" -l "$album_name" -t "$song_name" -N "$track_number" -q $quality -o "$3" 2>&1 | awk -vRS='\r' '(NR>1){gsub(/%/," ");print $2; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
}

mpc_encode ()
{
	mppenc --$quality "$2" "$3" 2>&1 | awk -vRS='\r' '!/^$/{if (NR>5) print $1; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
}

flac_encode ()
{
	flac --compression-level-$quality "$2" -o "$3" 2>&1 | awk -vRS='\r' '!/wrote/{gsub(/%/," ");if(NR>1)print $4; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
}

mac_encode ()
{
	mac "$2" "$3" -c$quality 2>&1 | awk -vRS='\r' '(NR>1){gsub(/%/," ");print $2; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
}

caf () # fonction "convert audio file"
{
        ### Format in_file = mp3 ###
        if [ "`file -b "$1" | grep 'MP3'`" != "" ] || [ "`echo $1 | grep -i '\.mp3$'`" != "" ]
        then
                if [ "$3" = "ogg" ]
                then # mp3-2-ogg
                        get_field_names "$1"
                        quality="$(get_ogg_quality)"
                        lame --quiet --decode "$1" - | ogg_encode "$1" - "$2"
                        break
                fi
                if [ "$3" = "mpc" ]
                then # mp3-2-mpc
                        quality="$(get_mpc_quality)"
                        mpc_encode "$1" "$1" "$2"
                        break
                fi
		if [ "$3" = "flac" ]
		then # mp3-2-flac
			quality="$(get_flac_quality)"
			lame --quiet --decode "$1" - | flac_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "ape" ]
		then # mp3-2-ape
			quality="$(get_mac_quality)"
			lame --quiet --decode "$1" - | mac_encode "$1" - "$2"
			break
		fi
                if [ "$3" = "wav" ]
                then # mp3-2-wav
                        lame --decode "$1" "$2" 2>&1 | awk -vRS='\r' -F'[ /]+' '(NR>2){print 100*$2/$3; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
                fi
                break
        fi
        ### Format in_file = ogg ###
        if [ "`file -b "$1" | grep 'Vorbis'`" != "" ] || [ "`echo $1 | grep -i '\.ogg$'`" != "" ]
        then
                if [ "$3" = "mp3" ]
                then # ogg-2-mp3
                        get_field_names "$1"
                        quality="$(get_mp3_quality)"
                        ogg123 -q --device=wav "$1" -f - | mp3_encode "$1" - "$2"
                        id3tag -a"$artist_name" -A"$album_name" -s"$song_name" -t"$track_number" "$2"
                        break
                fi
                if [ "$3" = "mpc" ]
                then # ogg-2-mpc
                        quality="$(get_mpc_quality)"
                        mpc_encode "$1" "$1" "$2"
                        break
                fi
		if [ "$3" = "flac" ]
		then # ogg-2-flac
			quality="$(get_flac_quality)"
			ogg123 -q --device=wav "$1" -f - | flac_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "ape" ]
		then # ogg-2-ape
			quality="$(get_mac_quality)"
			ogg123 -q --device=wav "$1" -f - | mac_encode "$1" - "$2"
			break
		fi
                if [ "$3" = "wav" ]
                then # ogg-2-wav
                        oggdec "$1" -o "$2" 2>&1 | awk -vRS='\r' '(NR>1){gsub(/%/," ");print $2; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
                fi
                break
        fi
        ### Format in_file = mpc ###
        if [ "`file -b "$1" | grep 'data'`" != "" ] && [ "`echo $1 | grep -i '\.mpc$'`" != "" ]
        then
                if [ "$3" = "mp3" ]
                then # mpc-2-mp3
                        get_field_names "$1"
                        quality="$(get_mp3_quality)"
                        mppdec "$1" - | mp3_encode "$1" - "$2"
                        id3tag -a"$artist_name" -A"$album_name" -s"$song_name" -t"$track_number" "$2"
                        break
                fi
                if [ "$3" = "ogg" ]
                then # mpc-2-ogg
                        get_field_names "$1"
                        quality="$(get_ogg_quality)"
                        mppdec "$1" - | ogg_encode "$1" - "$2"
                        break
                fi
		if [ "$3" = "flac" ]
		then # mpc-2-flac
			quality="$(get_flac_quality)"
			mppdec "$1" - | flac_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "ape" ]
		then # mpc-2-ape
			quality="$(get_mac_quality)"
			mppdec "$1" - | mac_encode "$1" - "$2"
			break
		fi
                if [ "$3" = "wav" ]
                then # mpc-2-wav
                        mppdec "$1" "$2" 2>&1 | awk -vRS='\r' -F'[ (]+' '!/s/{gsub(/(%)/," ");if(NR>5)print $5; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
                        break
                fi
                break
        fi
	### Format in_file = flac ###
	if [ "`file -b "$1" | grep 'FLAC'`" != "" ] || [ "`echo $1 | grep -i '\.flac$'`" != "" ]
	then
		if [ "$3" = "mp3" ]
		then # flac-2-mp3
			get_field_names "$1"
			quality="$(get_mp3_quality)"
			flac -d "$1" -o - | mp3_encode "$1" - "$2"
			id3tag -a"$artist_name" -A"$album_name" -s"$song_name" -t"$track_number" "$2"
			break
		fi
		if [ "$3" = "ogg" ]
		then # flac-2-ogg
			get_field_names "$1"
			quality="$(get_ogg_quality)"
			flac -d "$1" -o - | ogg_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "mpc" ]
		then # flac-2-mpc
			quality="$(get_mpc_quality)"
			flac -d "$1" -o - | mpc_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "ape" ]
		then # flac-2-ape
			quality="$(get_mac_quality)"
			flac -d "$1" -o - | mac_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "wav" ]
		then # flac-2-wav
			flac -d "$1" -o "$2" 2>&1 | awk -vRS='\r' -F':' '!/done/{gsub(/ /,"");gsub(/% complete/,"");if(NR>1)print $2; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
			break
		fi
		break
	fi
	### Format in_file = ape ###
	if [ "`file -b "$1" | grep 'data'`" != "" ] && [ "`echo $1 | grep -i '\.ape$'`" != "" ]
	then
		if [ "$3" = "mp3" ]
		then # ape-2-mp3
			get_field_names "$1"
			quality="$(get_mp3_quality)"
			mac "$1" - -d | mp3_encode "$1" - "$2"
			id3tag -a"$artist_name" -A"$album_name" -s"$song_name" -t"$track_number" "$2"
			break
		fi
		if [ "$3" = "ogg" ]
		then # ape-2-ogg
			get_field_names "$1"
			quality="$(get_ogg_quality)"
			mac "$1" - -d | ogg_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "mpc" ]
		then # ape-2-mpc
			quality="$(get_mpc_quality)"
			mac "$1" - -d |  mpc_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "flac" ]
		then #ape-2-flac
			quality="$(get_flac_quality)"
			mac "$1" - -d | flac_encode "$1" - "$2"
			break
		fi
		if [ "$3" = "wav" ]
		then #ape-2-wav
			mac "$1" "$2" -d 2>&1 | awk -vRS='\r' '(NR>1){gsub(/%/," ");print $2; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
			break
		fi
		break
	fi
        ### Format in_file = wav ###
        if [ "`file -b "$1" | grep 'WAVE'`" != "" ] || [ "`echo $1 | grep -i '\.wav$'`" != "" ]
        then
                if [ "$3" = "mp3" ]
                then # wav-2-mp3
                        get_field_names "$1"
                        quality="$(get_mp3_quality)"
                        mp3_encode "$1" "$1" "$2"
                        id3tag -a"$artist_name" -A"$album_name" -s"$song_name" -t"$track_number" "$2"
                        break
                fi
                if [ "$3" = "ogg" ]
                then # wav-2-ogg
                        get_field_names "$1"
                        quality="$(get_ogg_quality)"
                        ogg_encode "$1" "$1" "$2"
                        break
                fi
                if [ "$3" = "mpc" ]
                then # wav-2-mpc
                        quality="$(get_mpc_quality)"
                        mpc_encode "$1" "$1" "$2"
                        break
                fi
		if [ "$3" = "flac" ]
		then # wav-2-flac
			quality="$(get_flac_quality)"
			flac_encode "$1" "$1" "$2"
			break
		fi
		if [ "$3" = "ape" ]
		then # wav-2-ape
			quality="$(get_mac_quality)"
			mac_encode "$1" "$1" "$2"
			break
		fi
                break
        fi
        ### Format in_file = wma ###
        if [ "`file -b "$1" | grep 'Microsoft'`" != "" ] || [ "`echo $1 | grep -i '\.wma$'`" != "" ]
        then
                if [ "$3" = "mp3" ]
                then # wma-2-mp3
                        get_field_names "$1"
                        quality="$(get_mp3_quality)"
                        mplayer -ao pcm:file="$2.wav" "$1"
                        mp3_encode "$1" "$2.wav" "$2"
                        rm -f "$2.wav"
                        id3tag -a"$artist_name" -A"$album_name" -s"$song_name" -t"$track_number" "$2"
                        break
                fi

                if [ "$3" = "wav" ]
                then # wma-2-wav
                        mplayer -ao pcm:file="$2" "$1" 2>&1 | awk -vRS='\r' '(NR>1){gsub(/%/," ");print 100-$5; fflush();}' | zenity --progress --title="$title" --text="$conversion $1" --auto-close
                        break
                fi

                if [ "$3" = "ogg" ]
                then # wma-2-ogg
                        get_field_names "$1"
                        quality="$(get_ogg_quality)"
                        mplayer -ao pcm:file="$2.wav" "$1"
                        ogg_encode "$1" "$2.wav" "$2"
                        rm -f "$2.wav"
                        break
                fi
                if [ "$3" = "mpc" ]
                then # wma-2-mpc
                        quality="$(get_mpc_quality)"
                        mplayer -ao pcm:file="$2.wav" "$1"
                        mpc_encode "$1" "$2.wav" "$2"
                        rm -f "$2.wav"
                        break
                fi
		if [ "$3" = "flac" ]
		then # wma-2-flac
			quality="$(get_flac_quality)"
			mplayer -ao pcm:file="$2.wav" "$1"
			flac_encode "$1" "$2.wav" "$2"
			rm -f "$2.wav"
			break
		fi
		if [ "$3" = "ape" ]
		then # wma-2-ape
			quality="$(get_mac_quality)"
			mplayer -ao pcm:file="$2.wav" "$1"
			mac_encode "$1" "$2.wav" "$2"
			rm -f "$2.wav"
			break
		fi
                break
        fi
}

#################################################
#       PROGRAMME
#### Pas de fichiers sélectionné ###
if [ $# -eq 0 ]; then
        zenity --error --title="$warning" --text="$noselec"
        exit 1
fi
######## Check dépendance pour oggenc ou lame #######
depformat=""
if which lame 2>/dev/null
then
        if [ `echo $1 | cut -d . -f 2` != mp3 ]
        then
                depformat="mp3"
        fi
fi
if which oggenc 2>/dev/null
then
        if [ `echo $1 | cut -d . -f 2` != ogg ]
        then
                depformat="$depformat ogg"
        fi
fi
if which mppenc 2>/dev/null
then
        if [ `echo $1 | cut -d . -f 2` != mpc ]
        then
                depformat="$depformat mpc"
        fi
fi
if which flac 2>/dev/null
then
	if [ `echo $1 | cut -d . -f 2` != flac ]
	then
		depformat="$depformat flac"
	fi
fi
if which mac 2>/dev/null
then
	if [ `echo $1 | cut -d . -f 2` != ape ]
	then
		depformat="$depformat ape"
	fi
fi
if [ `echo $1 | cut -d . -f 2` != wav ]
then
        depformat="$depformat wav"
fi
######## Fenêtre principale ########
while [ ! "$formatout" ] # Réafficher la fenêtre tant que l'utilisateur n'a pas fait de choix
do


        formatout=`zenity --title "$title" --list --column="Format" $depformat --text "$choix"`
        ###### Choix -> Sortie boucle ######
        if  [ $? != 0 ]; then
                exit 1
        fi
        [ $? -ne 0 ] && exit 2 # Annulation
done
########## Conversion ############
#let "nbfiles = $#"
#compteur=0;
file_number=$#
(while [ $# -gt 0 ]; do
        for i in $formatout; do
                in_file=$1
                out_file=`echo "$in_file" | sed 's/\.\w*$/'.$formatout'/'`
                #echo "# $conversion $in_file"
                i=`echo $i | sed 's/"//g'`
                while `true`; do
                        ########## Le fichier de sortie existe déj| , l'écraser ? ##########
                        if [ "`ls "$out_file" | grep -v "^ls"`" != "" ]
                        then
                                if !(`gdialog --title "$warning" --yesno "$out_file $proceed" 200 100`)
                                then
                                        break
                                fi
                        fi
			if [ "$file_number" -gt 1 ] && [ "$formatout" != "wav" ]
			then
				answer=`zenity --question --text="$confirmation $in_file in $out_file?"`
			fi
                        caf "$in_file" "$out_file" "$formatout" # Lancer la conversion
                break
                shift
                done
                ######### Progression ########
#                let "compteur += 1"
#                let "progress = compteur*100/nbfiles"
#                echo $progress
        done
        shift
done
)
