#! /usr/bin/perl
#
# Read a Praat speech file and create a corresponding Pitch file
# ("raw" and smoothened).
#
# CreatePitch.pl --datapath <PathToSLspeech>] SpeakerID [FilePattern]
#
# --datapath <PathToSLspeech> gives an alternative speech repository.
#   default is the one in Links.pl
#
# FilePattern is a RegExp pattern $FileName =~ m@($FilePattern)@
#
#    Copyright (C) 2000, 2001  R.J.J.H. van Son
# 
#    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
#

$HomeDir = '..';
require "$HomeDir/Links.pl";

# Use alternate datapaths
if(grep(/\-\-datapath/i, @ARGV))
{
	my $i;
	for($i=0; $i < $#ARGV; ++$i)
	{
		if($ARGV[$i] =~ /\-\-datapath/i)
		{
			$DataPath = $ARGV[$i+1];
			splice(@ARGV, $i, 2);
			
		};
	};
	
};

my $SoundFiles        = "$DataPath/$Sentences";
my $PitchFiles        = "$DataPath/$Pitch";
my $SmoothPitchFiles  = "$DataPath/$SmoothPitch";

sub create_Pitchdirectory  # ($ID) -> 1/0 create directory if necessary
{
	my $ID = shift || return 0;
	#
	(mkdir("$PitchFiles", 0744) || die "$PitchFiles: $!\n") unless -d "$PitchFiles";
	(mkdir("$PitchFiles/fm", 0744) || die "$PitchFiles/fm: $!\n") unless -d "$PitchFiles/fm";
	my $DirExists = -d "$PitchFiles/fm/$ID";
	(mkdir("$PitchFiles/fm/$ID", 0744) || die "$PitchFiles/fm/$ID: $!\n") unless $DirExists;
	#
	(mkdir("$PitchFiles/hm", 0744) || die "$PitchFiles/hm: $!\n") unless -d "$PitchFiles/hm";
	$DirExists = -d "$PitchFiles/hm/$ID";
	(return mkdir("$PitchFiles/hm/$ID", 0744) || die "$PitchFiles/hm/$ID: $!\n") unless $DirExists;
	return 0;   # Nothing done
}

sub create_SmoothPitchdirectory  # ($ID) -> 1/0 create directory if necessary
{
	my $ID = shift || return 0;
	#
	(mkdir("$SmoothPitchFiles", 0744) || die "$SmoothPitchFiles: $!\n") unless -d "$SmoothPitchFiles";
	(mkdir("$SmoothPitchFiles/fm", 0744) || die "$SmoothPitchFiles/fm: $!\n") unless -d "$SmoothPitchFiles/fm";
	my $DirExists = -d "$SmoothPitchFiles/fm/$ID";
	(mkdir("$SmoothPitchFiles/fm/$ID", 0744) || die "$SmoothPitchFiles/fm/$ID: $!\n") unless $DirExists;
	#
	(mkdir("$SmoothPitchFiles/hm", 0744) || die "$SmoothPitchFiles/hm: $!\n") unless -d "$SmoothPitchFiles/hm";
	$DirExists = -d "$SmoothPitchFiles/hm/$ID";
	(return mkdir("$SmoothPitchFiles/hm/$ID", 0744) || die "$SmoothPitchFiles/hm/$ID: $!\n") unless $DirExists;
	return 0;   # Nothing done
}

@Pitchcalculations = (
"Rename... Label\n",
"select Sound Label\n",
"To Pitch... 0.001 75 600\n",
"Rename... Pitch\n",
"select Pitch Pitch\n",
"Down to PitchTier\n",
"Rename... PitchTier\n",
"select Pitch Pitch\n",
"Kill octave jumps\n",
"Interpolate\n",
"Smooth... 10\n",
"Down to PitchTier\n",
"Rename... SmoothPitchTier\n"
);

sub create_praat_script	# ($ID) -> @PraatScript;
{
	my $ID = shift || return ();
	my $Pattern = shift || ".";

	my @PraatScript = ();

	foreach $Channel ('fm', 'hm')
	{
		my @FileList = glob("$SoundFiles/$Channel/$ID/*");
		foreach $FilePath (@FileList)
		{
			# Check whether this file is wanted (needed when several copies of this script are running)
			next unless $FilePath =~ m@($Pattern)@;
			
			$FilePath =~ m@/([^/]+)/([^/]+)$@;
			
			my $RealID = $1;
			my $FileName = $2;
			$FileName =~ /\.([\w]+)$/i;
			my $SoundExtention = $1;
			$FileName =~ s/\.([\w]+)$//ig;
			die "$FilePath does not contain a filename\n" unless $FileName =~ /\w/;
			# Skip existing files
			next if -e "$PitchFiles/$Channel/$RealID/$FileName.Pitch" 
				&& -e "$SmoothPitchFiles/$Channel/$RealID/$FileName.SmoothPitch";
			
			# The actual script
			push(@PraatScript, "Read from file... $SoundFiles/$Channel/$RealID/$FileName.$SoundExtention\n");
			push(@PraatScript, @Pitchcalculations);
			push(@PraatScript, "select PitchTier PitchTier\n");
			push(@PraatScript, "Write to binary file... $PitchFiles/$Channel/$RealID/$FileName.Pitch\n") unless -e "$PitchFiles/$Channel/$RealID/$FileName.Pitch";
			push(@PraatScript, "select PitchTier SmoothPitchTier\n");
			push(@PraatScript, "Write to binary file... $SmoothPitchFiles/$Channel/$RealID/$FileName.SmoothPitch\n")
				unless  -e "$SmoothPitchFiles/$Channel/$RealID/$FileName.SmoothPitch";
			push(@PraatScript, "select all\n");
			push(@PraatScript, "Remove\n");
		};
	};

	return @PraatScript;
};
	

my $ID = shift(@ARGV) || '*';
my @DirectoryList = glob("$SoundFiles/fm/$ID");
my $CurrentPath;
foreach $CurrentPath (@DirectoryList)
{
	$CurrentPath =~ m@/([^/]+)$@;
        my $CurrentID = $1;
	create_Pitchdirectory($CurrentID);
	create_SmoothPitchdirectory($CurrentID);
	my @MasterPraatScript = create_praat_script($CurrentID, $Pattern);

	# Execute Praat script
	if(@MasterPraatScript)
	{
		open(PRAAT, "|praat -") || die "praat: $!\n";
		foreach $Line (@MasterPraatScript)
		{
			print PRAAT $Line;
		};
		print PRAAT "Quit\n";
		close(PRAAT);
	};
};
exit;


