#! /usr/bin/perl
#
# Read a list of Shadow sentence label files and construct a praat script  
# get the DTW time shift to align the shadow with the original labels
#
# use:
#> GetShadowDTW 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";

my @GlobFileList = @ARGV;
my $FilePattern;
my $Tier = 'TRANSLIT';

# Determine CrossCorrelation from -$SemiInterval to +$SemiInterval
my $LeftInterval = 0.2;
my $RightInterval = $LeftInterval;

# Praat commands to calculate the DTW
my $PraatScript = join("",
(
"select Sound destination\n",
"plus Sound source\n",
"To MFCC... 12 0.02 0.005\n",
"To DTW... yes yes no restriction\n"
));

# Start with Praat script
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 and get the name of the shadowing file
         	$Code =~ /^([FM][\d]+[A-Z]+)/;
        	my $SpeakerID = $1;
		my $SpeechFile = "$SpeechPath/$SpeakerID/$Code.aifc";
		
		#  Get the ID of the original item
		$Code =~ /\+/;
		$OrigID = $';
		$OrigID =~ s/\.shadow//g;
		$OrigID =~ /^([FM][\d]+[A-Z]+)/;
		$OrigSpeaker = $1;
		
		# Get the times
		# The new file
		my $Source = new SentenceLabel;
		$Source->ReadLabelFile($FileName);
		$Source->presetItem($Tier);
		
		my $DTWFile = "../$SpeakerID/DTW/$Code.dtw";
		my $DTWpathFile = "../$SpeakerID/DTW/$Code.dtwpath";
			
		# Start the Praat script
		print << "PRAATSCRIPTSTART";
# Get shadow speech file (destination)
Read two Sounds from stereo file... $SpeechFile

PRAATSCRIPTSTART
		my $Item = 0;
		while($Source->nextItem($Tier) > -1)
		{
			next if $Source->currentValue($Tier) =~ /^([\*\#]|ISI\*x)$/;
			# Get current Item in Tier
			my ($destinationStart, $destinationEnd) = 
				($Source->currentStart($Tier) - $LeftInterval, $Source->currentEnd($Tier) + $RightInterval);
				
			# Get the start of the aligned item in the Shadow tier
			$Item = $Source->StartAlignedinTier($Tier, 'PHONEMES', $Item);
			last unless $Item > -1;
			$Source->currentItem('SHADOW', $Item);
			my $sourceStart = $Source->currentStart('SHADOW') - $LeftInterval;
			
			# Get the end of the aligned item in the Shadow tier
			$Item = $Source->EndAlignedinTier($Tier, 'PHONEMES', $Item);
			last unless $Item > -1;
			$Source->currentItem('SHADOW', $Item);
			my $sourceEnd = $Source->currentEnd('SHADOW') + $RightInterval;
			my $ItemID = $Source->currentIDcode($Tier);
		
		print << "PRAATSCRIPT";
# Run script 
# Shadow recording
select Sound left
Extract part... $destinationStart $sourceEnd Rectangular 1.0 yes
Rename... source

# Original recording
select Sound right
Extract part... $destinationStart $sourceEnd Rectangular 1.0 yes
Rename... destination

# Execute command
$PraatScript

# Write to output
Write to text file... $DTWFile
system cat $DTWFile |egrep -v 'z \\[' > $DTWpathFile
system rm $DTWFile
system gzip $DTWpathFile

PRAATSCRIPT
		};
		print << "PRAATSCRIPTEND";
# get rid of files
select all
Remove

PRAATSCRIPTEND
    	};
};

print "Quit\n";
