#!/usr/bin/perl -w


$version    = "V1.1";


if ($#ARGV+1 != 3 || $ARGV[0] eq "-h" || $ARGV[0] eq "-H")
{   print 
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n".
    "                                                   $version, 21/11/05, vst\n".
    "                                                                          \n".
    "Usage:  label_fix_plosives  in.mlf  out.mlf  thresh                       \n".
    "                                                                          \n".
    "    in.mlf   input master label file with timing info, typically the final \n".
    "             aligned.3.mlf made by \$MULTISYN_BUILD/bin/do_alignment      \n".
    "    out.mlf  output master label file with with no timing info, i.e. same \n".
    "             format as the initial phone labels, with some /sp/ replaced  \n".
    "             /sil/                                                        \n".
    "    thresh  duration thresh for word-initial stops in seconds             \n".
    "                                                                          \n".
    "                                                                          \n".
    "   Each word is assumed to be preceeded by a /sp/.  When the word starts  \n".
    "   with a plosive longer than `thresh`, the /sp/ is replaced by /sil/.    \n".
    "   Use out.ml as new initial alignment file \"utts.mlf\" for the entire   \n".
    "   iterated aligning procedure \$MULTISYN_BUILD/bin/do_alignment.         \n".
    "   (Repeating just the final alignment is not good enough).               \n".
    "                                                                          \n".
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    exit;
}
# ===========================================================================
$in_mlf  = $ARGV[0];
$out_mlf = $ARGV[1];
$thresh  = $ARGV[2];


open(IN,     "$in_mlf") or die "cannot create file $in_mlf\n";
open(OUT, "> $out_mlf") or die "cannot create file $out_mlf\n";

if($thresh > 10 || $thresh < 0.001) {die "$thresh is not a reasonable thresh.\n";}


# ===========================================================================
$tot_n_plosives=0;
$tot_n_fixes=0;
$lc=0;

while(<IN>)
{   if(/^#/)
    {   print OUT $_;
    }
    elsif(/^"/)
    {   print OUT $_;
        chop;
        $uttname = $_;
    }
    elsif(/^\./)
    {
        for($i=0; $i<$lc; $i++)
        {   if($phone[$i] =~ /_cl/)
            {   if($phone[$i-1] eq "sp" && $dur[$i]+$dur[$i+1] > $thresh)
                {   print "fix \/sp ".$phone[$i+1]."\/ at ".$time[$i+1]. " in $uttname\n";
                    $phone[$i-1] = "sil";
                    $tot_n_fixes++;
                }
                $tot_n_plosives++;
            }
        }
        for($i=0; $i<$lc; $i++)
        {   print OUT $phone[$i]."\n";
        }
        print OUT ".\n";
    }
    else
    {   @tokens = split;
        $dur[$lc] = $tokens[1] - $tokens[0];
        $time[$lc] = $tokens[1];
        $phone[$lc] = $tokens[2];
        $lc++;
    }
}
close(IN);
close(OUT);

print "\n$tot_n_fixes of $tot_n_plosives to be fixed\n";
