#!/usr/bin/perl
#
my $PrintLine = 0;
my $NumberOfTiers = 0;
my $prefend = 0;
my %TierBase = ();
my @lastnumbers = ();
my $lastspeaker = "";


print << "ENDOFHEADER";
# Speaker: Text (<Utterance ID>: <distance to previous>, <duration>)
ENDOFHEADER

while(<>)
{
    s/\x00//g;
    s/\'\'/\'/g;
    
    ++$NumberOfTiers if m/\"IntervalTier\"/;
    if(m/\"(\w+)ackawd\"/i)
    {
        $TierBase{$NumberOfTiers} = $1;
    };
    next if m/\"IntervalTier\"/;
    
    next unless $NumberOfTiers;
    
    if(m/^(\d+)\s+([\d\.]+)\s+([\d\.]+)/)
    {
		my $CurrentID = $TierBase{$1};
		if($CurrentID)
		{
        	@lastnumbers = ($TierBase{$1}, $2, $3);
        	$lastnumbers[0] =~ s/^DV[AB]\d+([FM]\d+[A-Z]+)\d+$/$1/g;
		}
		else
		{
			@lastnumbers = ("", 0, 0);
		};
    }
    elsif(m/^\s*\"([^\"]+)__(\w+)\"/)
    {
		next unless $lastnumbers[0];
		
        $text = $1;
        $id = $2;
        if($text ne "sil")
        {
            if($lastnumbers[0] ne $lastspeaker)
            {
                print "$lastnumbers[0]: ";
            }
            else
            {
                print " " x length"$lastnumbers[0]: ";
            };
            $dist = sprintf("%.3f", $lastnumbers[1] - $prefend);
            $duration = sprintf("%.3f", $lastnumbers[2] - $lastnumbers[1]);
            print "$text \t($id: $dist, $duration)\n";
            $lastspeaker = $lastnumbers[0];
            $prefend = $lastnumbers[2];
        };
    };
};
