#! /usr/bin/perl
#
# Read a list of Shadow sentence label files and construct a praat script  
# get the time shift to align them with the original sentences
#
# use:
#> GetLabelTimeShift filepattern
#
###############################################################################
#
# Copyright R.J.J.H. van Son © 2000, 2001
#
# Author Rob van Son
# Institute of Phonetic Sciences & ACLC
# University of Amsterdam
# Herengracht 338
# NL-1016CG Amsterdam, The Netherlands
# Email: Rob.van.Son@hum.uva.nl
#        rob.van.son@workmail.com
# WWW  : http://www.fon.hum.uva.nl/rob/
# mail:  Institute of Phonetic Sciences
#        University of Amsterdam
#        Herengracht 338
#        NL-1016CG Amsterdam
#        The Netherlands
#        tel +31 205252183
#        fax +31 205252197
#
# License for use and disclaimers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
#######################################################
#
# Define and initialize
$HomeDir = '../../..';
require "$HomeDir/Links.pl";
require "$Scripts/SentenceLabel.pl";

$SpeechPath = "$DataPath/home/Shadowing/sentences";
$LabelDir = "sentences";
$SpeechSource = "$DataPath/sentences/fm";

my @GlobFileList = @ARGV;
my $FilePattern;

print "#! praat\n\n";
foreach $FilePattern (@GlobFileList)
{
        # Get the real filenames (expand * and ?)
        my @FileList = glob("$FilePattern");
        my $FileName;

        # Handle All files
        foreach $FileName (@FileList)
        {        	
        	# Take apart the file path to get the relevant directories and ID numbers
        	$FileName =~ m@([^/\.]+)\.([\w]+)$@i;
        	my $Code = $1;
         	
         	# Tear apart the CodeName
         	$Code =~ /^([FM][\d]+[A-Z]+)/;
        	my $SpeakerID = $1;
		my $SpeechFile = "$SpeechPath/$SpeakerID/$Code.aifc";
		
		#  Get the ID of the original sentence
		$Code =~ /\+/;
		$OrigID = $';
		$OrigID =~ s/\.shadow//g;
		$OrigID =~ /^([FM][\d]+[A-Z]+)/;
		$OrigSpeaker = $1;
		$OrigSpeech = "$SpeechSource/$OrigSpeaker/${OrigID}_fm.aifc";
		@Labels = glob("$SentenceLabels/$OrigSpeaker/phoneme/${OrigID}_*.phoneme");
		$OrigLabel = shift(@Labels);
		
		# Get the times
		# The new file
		my $Source = new SentenceLabel;
		$Source->ReadLabelFile($FileName);
		$Source->presetItem('TRANSLIT');
		$Source->grepnextID('TRANSLIT', "^$OrigID\$");
		my ($sourceStart, $sourceEnd) = 
			($Source->currentStart('TRANSLIT')-0.25, $Source->currentStart('TRANSLIT')+1.25);
		# The original file
		my $Destination = new SentenceLabel;
		$Destination->ReadLabelFile($OrigLabel);
		$Destination->presetItem('TRANSLIT');
		$Destination->grepnextID('TRANSLIT', "^$OrigID\$");
		my ($destinationStart, $destinationEnd) = 
			($Destination->currentStart('TRANSLIT')-0.25, $Destination->currentStart('TRANSLIT')+1.25);
		
		my $ShiftTimeFile = "../$SpeakerID/ShiftTime/$Code.ShiftTime";
		print << "PRAATSCRIPT";
# Get shadow speech file (destination)
Read two Sounds from stereo file... $SpeechFile
select Sound left
Remove
select Sound right
Rename... source

# Get original speech file (source)
Read from file... $OrigSpeech
Rename... destination

# Run script 
fileappend $ShiftTimeFile $FileName\'tab\$\'$OrigID
execute WriteShadowShift.praat $sourceStart $sourceEnd $destinationStart $destinationEnd 0.25 $ShiftTimeFile

# get rid of files
select all
Remove

PRAATSCRIPT
    	};
};

print "Quit\n";
