#! /usr/bin/perl
#
# Read a list of label sentence ID's and construct a long praat script to edit them all
#
# use:
#> CorrectLabelFiles.pl filenames (or -) > <praat script>
#
###############################################################################
#
# 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";

print "\#! praat\n\n";

while(<>)
{
	chomp;
	m@[^/]+$@;
	my $FileName = $&;
	# Remove word-number
	$FileName =~ s/\d+$//g;
	# Get Speaker ID
	$FileName =~ /([FM]\d+[A-Z]+)/i;
	my $Speaker = $1;
	
	# Get all matchin label files
	my @LabelFileList = glob("$SentenceLabels/$Speaker/phoneme/${FileName}_*.phoneme");
	
	my $LabelFile;
	foreach $LabelFile (@LabelFileList)
	{
		$LabelFile =~ m@(${Speaker}[^/\_\.]+)_@;
		my $SpeechFile = "${1}_fm.aifc";
		$SpeechFile = "$DataPath/$Sentences/fm/$Speaker/$SpeechFile";
		print STDOUT << "PRAATSCRIPT";
filename\$ = "${LabelFile}.corrected"
if not fileReadable(filename\$)
    Read from file... $LabelFile
    Rename... Label
    Open long sound file... $SpeechFile
    Rename... Speech
    select all
    Edit
    pause Save and continue
    select TextGrid Label
    Write to short text file... ${LabelFile}.corrected
    select all
    Remove
endif

PRAATSCRIPT
		
	};
};
