#!/bin/bash

mydir=`dirname "$0"`
infile="$1"
outfile="$2"

if [ t"$infile" = "t" ] || [ t"$outfile" = "t" ]; then
    echo "Usage: $0 infile.wav outfile.txt"
    exit 2
fi

set -eu

touch "$outfile"

echo "Processing input WAV file $infile, writing results to $outfile..." 1>&2

# Convert
#
# ;time;"label"
#
# to
#
# starttime<TAB>endtime<TAB>label
#
# NB this omits the final chord! In practice the final one will almost
# certainly be an N rather than a chord but this will surely be a
# problem sometimes. What to do about it?

VAMP_PATH="$mydir" sonic-annotator \
    -t "$mydir"/chordino.ttl \
    -w csv --csv-stdout --csv-separator ";" \
    "$infile" \
    | perl -e 'while (<>) { chomp; @a = split /;/; if (defined $p) { print "$p\t$a[1]\t$pl\n"; }; $p = $a[1]; $pl = $a[2]; $pl =~ s/"//g; }; $pp = $p + 1; print "$p\t$pp\t$pl\n";' \
    > "$outfile"

