#! /usr/bin/perl -w

########################################################################
#### file          : srt2ctm.pl
#### date          : april 22 1998
#### author        : Cedric Auzanne for NIST
#### description   : parses a srt file to create a ctm file
####
########################################################################
#### usage         : srt2ctm.pl srtfile_full_path
####
#### see also      : ctm2srt.pl does reverse operation
####
########################################################################
#### comments      : words that are out of bouds (not included in a section
####                 are printed as comments like the following
#### <Comment> Out of boundary word : WATERS, S=33.76, E=34.12 </Comment>
#### where WATERS is the word, S is start time and E end time, in the srt file
####
#### lines starting with <Comment> Out of boundary word : will be put back
#### in the ctm format
####
########################################################################
#### Formats :
#### see man ctm for the specs of the ctm format
#### see ctm2srt.pl for the srt format
####
########################################################################


########################################################################
#### check parameters
die "\nERROR : no srt filename supplied\n" if ($#ARGV != 0);

$srtfilename      = $ARGV[0];
$ctmfilename      = "$1.ctm" if ($srtfilename =~ /(.*?).srt/);
$showname         = $1       if ($srtfilename =~ /.*?\/(.*?).srt/);
$channel          = 1;

die "\nERROR : srt file $srtfilename not found\n" if (! -f $srtfilename);

print "Processing file $srtfilename to $ctmfilename\n";


########################################################################
#### open output file, print comments

open OFD, ">$ctmfilename" or die "\nERROR : can't open output file $ctmfilename\n";
print OFD ";\n";
print OFD ";\tThis file was generated by srt2ctm.pl\n";
print OFD ";\tdate      : ", scalar localtime, "\n";
print OFD ";\tsrt file  : $srtfilename\n";
print OFD ";\n";



########################################################################
########################################################################
########################################################################
#### start processing

# read each line in ctm file and process
open SRTFD, "<$srtfilename" or die "ERROR : can't open srt file $srtfilename\n";
while (<SRTFD>)
{
    # line is out of boundary word
    if (/^<Comment>\s+Out of boundary word :\s+(\w+),\s+S=(\d+\.\d+),\s+E=(\d+\.\d+)/)
    {
	my $outputs  = sprintf "%s %d %9.3f %4.3f %s\n", $showname, $channel, $2, $3-$2, $1;
	print OFD $outputs;
	next;
    }
    # line is a word
    if (/^<Word\s+S_time=(\d+\.\d+)\s+E_time=(\d+\.\d+)\s*>\s*(.*?)\s*<\/Word>/)
    {
	my $word     = $3;
	$word        =~ tr/a-z/A-Z/;
	my $outputs  = sprintf "%s %d %9.3f %4.3f %s\n", $showname, $channel, $1, $2-$1, $word;
	print OFD $outputs;
	next;
    }
} # while
close SRTFD;


# finish processing
print OFD ";\n; end of file\n;\n";
close OFD;

print "Done.\n";








