#! /usr/bin/perl
#
# Read a Praat speech file and create a corresponding Center Of Gravity analsysis.
#
# CreateCoG.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 $CoGFiles        = "$DataPath/$CenterOfGravity/";

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

@CoGcalculations = (
"Rename... Label\n",
"select Sound Label\n",
"To Spectrogram... 0.025 16000 0.001 10 Gaussian\n",
"select Spectrogram Label\n",
"To Matrix\n",
"Rename... FE\n",
"select Spectrogram Label\n",
"Remove\n",
"select Matrix FE\n",
"Copy... E\n",
"\n",
"select Matrix FE\n",
"Formula... if(row>1) then self*y+self[row-1,col] else self*y fi\n",
"To Sound (slice)... -1\n",
"select Matrix FE\n",
"Remove\n",
"\n",
"select Matrix E\n",
"Formula... if(row>1) then self+self[row-1,col] else self fi\n",
"To Sound (slice)... -1\n",
"select Matrix E\n",
"Remove\n",
"\n",
"select Sound FE\n",
"Rename... CoG\n",
"Formula... 12*log2(self/Sound_E[row,col])\n",
"select Sound E\n",
"Remove\n",
"\n",
"select Sound CoG\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 $FileName = $1;
			$FileName =~ /\.([\w]+)$/i;
			my $SoundExtension = $1;
			$FileName =~ s/\.[\w]+$//ig;
			die "$FilePath does not contain a filename\n" unless $FileName =~ /\w/;
			# Skip existing files
			next if -e "$CoGFiles$Channel/$ID/$FileName.cog";
			# The actual script
			push(@PraatScript, "Read from file... $SoundFiles$Channel/$ID/$FileName.$SoundExtension\n");
			push(@PraatScript, @CoGcalculations);
			push(@PraatScript, "Write to short text file... $CoGFiles$Channel/$ID/$FileName.cog\n");
			push(@PraatScript, "select all\n");
			push(@PraatScript, "Remove\n");
		};
	};

	return @PraatScript;
};
	

my $ID = shift(@ARGV) || exit;
my @DirectoryList = glob("$SoundFiles/fm/$ID");
my $CurrentPath;
foreach $CurrentPath (@DirectoryList)
{
	$CurrentPath =~ m@/([^/]+)$@;
        my $CurrentID = $1;
	create_CoGdirectory($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;


