#!/usr/bin/perl
# rnc2sphere.pl: Mark Hasegawa-Johnson, 5/17/2005
# Illinois Speech and Language Engineering (ISLE group)
#
# Usage (printed if rnc2sphere.pl is called with no arguments):
#
$USAGE=<<ENDUSAGE;

RNC2SPHERE: Convert Radio News transcriptions to SPHERE transcription
 
Usage:
  rnc2mlf.pl SAMPLING_RATE foo.rnc foo.sph
Example:
  rnc2mlf.pl 8000 telephone.rnc telephone.sph
  rnc2mlf.pl 16000 wideband.rnc wideband.sph

This script reads in a file in ESPS file format, and writes out a file
in SPHERE file format.  SAMPLING_RATE must be the sampling rate, in
samples per second.

ENDUSAGE

####################################################################
# GLOBAL VARIABLES

# Conversion rate for SPHERE file format
$ESPSPERIOD = 1;

# Math functions
sub abs { my($x)=shift; ($x>0) ? $x : -$x ; }


###################################################
# Subroutine to read RNC-format transcription file (TON, BRK, or WRD)
#
# Syntax: $data = read_RNC_file("filename");
#
# $data is an arrayref of arrayrefs.
# $$data[$i][0] is the start time of the $ith label
# $$data[$i][1] is the end time of the $ith label
# @$$data[$i][2..$#{$$tran[0]}] is the sequence of labels at time $i.
#
sub read_RNC_file {
  # Initialize @data to null
  my(@data) = ();

  # Open the INPUTFILE or die with an error message
  open(INPUTFILE,$_[0]) || die "Unable to open input file $_[0]: $!";

  # Read in all lines from the INPUTFILE
  foreach $_ (<INPUTFILE>) {
    # Ignore lines not in the format of number, number, labels
    if ( /^\s*([\d\-\.]+)\s+([\d\-\.]+)\s+(.*)/ ) {
      my(@labels) = split(/\s+/,$3);

      # Push a reference to @record onto the @data array
      push(@data, [ 0, $1 * $ESPSPERIOD, @labels ] );
    }
  }
  close(INPUTFILE);

  # Set start time of each segment to end time of preceding
  foreach $n (1..$#data) 
    { ${$data[$n]}[0] = ${$data[$n-1]}[1]; }

  return(\@data);
}

###################################################
# Subroutine to write a SPHERE-format transcription file
#
# Syntax: write_SPHERE_file("filename", $data);
#
# $data is an arrayref of arrayrefs.
# $$data[$i][0] is the start time of the $ith label
# $$data[$i][1] is the end time of the $ith label
# @$$data[$i][2..$#{$$tran[0]}] is the sequence of labels at time $i.
#
sub write_SPHERE_file {
    my($filename)=$_[0];
    my($data)=$_[1];

    # Write to the SPHERE file
    open(F,">$filename") || die "Unable to write to $filename: $!";

    # Write each line
    foreach $datum (@{$data}) {
	my(@fields)=@{$datum};
	# Multiply start and end by sampling frequency,
	# then add 0.5, and truncate (thus rounding to nearest int)
	$fields[0] = $fields[0] * $SPHEREFS + 0.5;
	$fields[0] =~ s/\..*//;
	$fields[1] = $fields[1] * $SPHEREFS;
	$fields[1] =~ s/\..*//;
	# Interpolate array into string
	print F "@fields\n";
    }
}
    

###########################################
# Main Program

if (@ARGV < 3) { print $USAGE; exit; }

# Read the sampling rate 
$SPHEREFS = $ARGV[0];

# Read in the input file
#
$data = read_RNC_file($ARGV[1]);

# Write out the output file
write_SPHERE_file($ARGV[2],$data);

