#! /usr/bin/perl
#
# Generate a praat script to inspect selected pairs of sentences
#
# Use:
# ./PairsToPraatScript.pl PairList
#
# PairList should contain pairs of lines with
# ID and SpeakerID 
#
# Uses: praat (see: http://www.praat.org/)
# 
# This page 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 (http://www.gnu.org/licenses/gpl.html) 
# for more details. 
#
# THIS SCRIPT CONTAINS POTENTIALLY HAZARDOUS COMMANDS. MAKE SURE
# YOU ARE AUTHORIZED TO EXECUTE THESE COMMANDS AND THAT YOU 
# UNDERSTAND THE ACTIONS THIS SCRIPT WILL PERFORM BEFORE YOU EXECUTE 
# IT. REMEMBER, THE USE OF THIS SCRIPT IS COMPLETELY AT YOUR
# OWN RISK!
#
# Please read the license information that is downloaded with this 
# script as License.txt, gpl.txt, and History.txt before you use 
# the script or any of the files downloaded with it.
print << "ENDOFCOPYRIGHTMESSAGE";
#! praat
# 
###############################################################################
#
# 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.
#
#
#######################################################
#
#
progress\$ = "0"
form Path to SLcorpus and SLspeech (default is script path)?
    text corpusPath
	integer startposition 0 (= all)
endform
if (length(corpusPath\$) > 0) and (right\$(corpusPath\$, 1) <> "/")
    corpusPath\$ = corpusPath\$ + "/"
endif
if startposition > 0
	progress\$ = "'startposition'"
endif
#
ENDOFCOPYRIGHTMESSAGE
#
$HomeDir = ".";
until(-d "$HomeDir/SLcorpus" || ++$i > 3)
{
    $HomeDir .= "/..";
};

my $ProgressPointer = 0;
while(<>)
{
    ($ID, $Speaker) = split;
    if($PrevID)
    {
        ++$ProgressPointer;
        ($PrevLabelFile) = glob("$HomeDir/SLcorpus/Labels/sentences/$Speaker/phoneme/${PrevID}_*.phoneme");
        $PrevLabelFile = "SLcorpus/Labels/sentences/$Speaker/phoneme/$1" if $PrevLabelFile =~ m@([^/]+)$@;
        $PrevSoundFile = "SLspeech/sentences/fm/$Speaker/${PrevID}_fm.aifc";
        ($LabelFile) = glob("$HomeDir/SLcorpus/Labels/sentences/$Speaker/phoneme/${ID}_*.phoneme");
        $LabelFile = "SLcorpus/Labels/sentences/$Speaker/phoneme/$1" if $LabelFile =~ m@([^/]+)$@;
        $SoundFile = "SLspeech/sentences/fm/$Speaker/${ID}_fm.aifc";
        print << "ENDOFLINE";
# Read $PrevID and $ID
if 'progress\$' <= $ProgressPointer
    Open long sound file... 'corpusPath\$'$PrevSoundFile
    Rename... First$ProgressPointer
    Read from file... 'corpusPath\$'$PrevLabelFile
    Rename... First$ProgressPointer
    Open long sound file... 'corpusPath\$'$SoundFile
    Rename... Second$ProgressPointer
    Read from file... 'corpusPath\$'$LabelFile
    Rename... Second$ProgressPointer
    # Edit
    select TextGrid First$ProgressPointer
    plus LongSound First$ProgressPointer
    Edit
    select TextGrid Second$ProgressPointer
    plus LongSound Second$ProgressPointer
    Edit
    pause $ProgressPointer: Next pair of sentences
    select all
    Remove
    progress\$ = "$ProgressPointer"
endif

ENDOFLINE
        $PrevID = "";
    }
    else
    {
        $PrevID = $ID;
    };
};
