play=0
make=1
dc=1 # drum channel
SF='/usr/share/soundfonts/FluidR3_GM.sf2'

run () {
	cmd=$1
	echo "Executing '$cmd'"
	eval "$cmd"
	if [ $? -ne 0 ]; then
		echo "Aborting ..."
		exit 1
	fi
}

if [ $# -lt 1 ]; then
	echo "Usage: $0 (<dtt>|<dtm>) [<m> <n> <s>]"
	echo "will convert a <dtt> or <dtm> file to midi and play"
	echo "<m> is a start measure to play"
	echo "<n> is the number of measures to play"
	echo "<s> is the number of beats preceeding the first measure"
	exit 1
fi
m=0
n=1
s=0
if [[ $# > 1 ]]; then
	m=$2
fi
if [[ $# > 2 ]]; then
	n=$3
fi
if [[ $# > 3 ]]; then
	s=$4
fi
d=$(dirname "$0")
i=$1
b=$(basename "$1")
e="${b##*.}"  # extension
f="${b%.*}"
o=${f}.mid
echo "i=$i, b=$b, e=$e, f=$f, o=$o"
echo "Reading drums from column: $dc"
echo "Reading $i and writing to $o"
if [ $m -ne 0 ]; then
	q=${f}.dtc
	c="$d/compile -v -m $m -n $n -s $s -d $dc $i $o"
else
	c="$d/compile -v -d $dc $i $o"
fi
run "$c"
if [ $play -eq 1 ]; then
	if [ ! -f '/etc/issue' ]; then
		run "sh ./vlc.sh $o"
	else
		echo "SF=$SF"
		run "fluidsynth -i $SF $o"
	fi
fi
if [ $make -eq 1 ]; then
	w=${f}.wav
	run "fluidsynth -i $SF -F $w $o"
fi

