#! /usr/bin/perl
#
# Convert a playlist to a set of Label files with the transliteration
# One labelfile per "style"
#
# use:
# PlayList2LabelFiles.pl <PlayList>
#
$HomeDir = '../../..';
require "$HomeDir/Links.pl";
require "$Scripts/SentenceLabel.pl";

my $root = '..';
my $ChunkLabels = "ChunkLabels";
my $RawSentences = "$HomeDir/Labels/sentences";

my $Tier = 'TRANSLIT';

# Convert all playlists
my $Silence = 2;
my $Beep = 0.5;
my $BeepInterval = 1;
my $StartSilence = 0;
my $EndSilence = 0;
my $Filecount = 0;
my $LastSpeaker = "";
my $LastStyle = "";
my $LastRecording = "";
my $FirstChunkNo = 0;
my $LastChunkNo = 0;
my $LastID = "";
my $Style = "";
my $PlayList;

foreach $PlayList (@ARGV)
{
	$PlayList =~ m@(^|/)([^/\.]+).playlist$@;
	my $Name = $2;
	$Name =~ /\+/;
	$RecName = $`;
	$Name =~ /^([FM][\d]+[A-Z]+)/;
	my $Subject = $1;
	
	my $SentenceLabel;
	my $LastIDcode = "";
	
	my $FileCount = 0;
	open(PLAYLIST, "<$PlayList") || die "$PlayList: $!\n";
	while(<PLAYLIST>)
	{
		# Get the start/end and ID of the speech
		my ($Sound, $Start, $EndTime, $ID) = split;
		$ID =~ /^([A-Z]+\d+[A-Z]+)(\d+)([A-Z]+)([\d]+)/;
		my $Speaker = $1;
		my $Recording = $2;
		my $Style = $3;
		my $ChunkNo = $4;
		
		# New label file
		if($Style ne $LastStyle)
		{
			# Write current sentence label to file
			if(ref($SentenceLabel) eq 'SentenceLabel')
			{
				# Push silence on the tierprint "insertItemAfter($Tier, '*', '', $SentenceLabel->currentEnd($Tier) + $Silence)\n";
				$SentenceLabel->pushItem($Tier, 'ISI*x', "$RecName+$LastIDcode"
		 		, $SentenceLabel->currentEnd($Tier), $SentenceLabel->currentEnd($Tier) + $Silence + $Beep + $BeepInterval);
				$SentenceLabel->lastItem($Tier);
				
				# Push last silence on the tier
				$SentenceLabel->pushItem($Tier, '*', ''
				, $SentenceLabel->currentEnd($Tier), $SentenceLabel->currentEnd($Tier) + $EndSilence) if $EndSilence > 0;

				# Intertpolate id's
				$SentenceLabel->InterpolateIDtier($Tier);
				my $LabelFile = "$root/$Subject/$ChunkLabels/$RecName+$LastSpeaker$LastRecording$LastStyle${FirstChunkNo}+$LastChunkNo.label";
				$LabelFile = "$root/$Subject/$ChunkLabels/$RecName+$LastID.label" if $FileCount <= 1;
				$SentenceLabel->print($LabelFile, $Tier);
				$FileCount = 0;
				
			};
			
			$SentenceLabel = new SentenceLabel;
			$SentenceLabel->FoldedIDcodes($Tier);
			$SentenceLabel->createTier($Tier);
			# Add first silence item
			$SentenceLabel->pushItem ($Tier, '*', '', 0, $StartSilence) if $StartSilence > 0;
			$SentenceLabel->lastItem($Tier);
			
			# Store ChunkNo
			$FirstChunkNo = $ChunkNo;
		};
		
		# Store current settings
		$LastStyle = $Style;
		$LastID = $ID;
		$LastRecording = $Recording;
		$LastSpeaker = $Speaker;	
		$LastChunkNo = $ChunkNo;
		
		# Get the tranliteration of the speech
		my $RawSentenceName = "$RawSentences/$Speaker/translit/$ID.translit";
		my $RawSentence = new SentenceLabel;
		$RawSentence->ReadLabelFile($RawSentenceName);
		print STDERR "$ID not found in $RawSentenceName\n" if $RawSentence->locateID($Tier, $ID) < 0;
		$TranslitText = $RawSentence->currentValue($Tier);
		
		# Push silence on the tierprint "insertItemAfter($Tier, '*', '', $SentenceLabel->currentEnd($Tier) + $Silence)\n";
		$SentenceLabel->pushItem($Tier, 'ISI*x', "$RecName+$LastIDcode"
		 , $SentenceLabel->currentEnd($Tier), $SentenceLabel->currentEnd($Tier) + $Silence + $Beep + $BeepInterval);
		$SentenceLabel->lastItem($Tier);
		
		# Push the current item on the tier
		$SentenceLabel->pushItem($Tier, $TranslitText, "$RecName+$ID"
		 , $SentenceLabel->currentEnd($Tier), $SentenceLabel->currentEnd($Tier) + ($EndTime - $Start));
		$SentenceLabel->lastItem($Tier);

		$LastIDcode = $ID;
		$LastIDcode .= $ID =~ /[A-Z\-]+$/ ? '-A' : '-1';
		++$FileCount;
	};
	close(PLAYLIST);
};
