#! /usr/local/bin/perl

#./data/script/test.pl /cavs/hse/data2/isip/data/exp/cavs/exp_073/enrollment_newdemo/gao /cavs/hse/data2/isip/data/exp/cavs/exp_073/enrollment_newdemo/gao/data/script


# need to fix the ISIP_VERIIFCATION stuff
# need to fic the $ISIP_DEVEL
# Need more documentation and cleaning up
# need to close all files open for reading or writing
# need to remove temp files
# need to give better name for count_1 in correct_transitions

$input = $ARGV[0];
$arg2 = $ARGV[1];
&create_exp_setup;
print "$input";
#$temp_list = `ls -l $input/data/raw >$input/data/temp_list`;
`ls -l $input/data/raw >$input/data/lists/temp.list`;
print "$temp_list";
&create_list;
print "before create list";
&create_transcription;
print "after transcription";

@args = ("extract_feature -delta -acc -cms -zero_mean -energy -energy_norm -mfcc 12 -delta_win 2 -lifter_coeff 22 -window_dur 25 -frame_dur 10 -window_type hamming -sf 16000.0 -mel_width 85.842581 -output_mode binary -pre_emph_coeff 0.97 -input  $input/data/lists/raw_train.list -output  $input/data/lists/mfc_train.list");
system(@args);

&train_models;



sub create_list {

print "$temp_list";

$output_basename = "data/raw/";
$mfcc_basename = "data/feature/";

print "$temp_list";
$temp_list = "$input/data/lists/temp.list"; 
open(fp_file, $temp_list);
#open(fp_file, $temp_list);
open(fp_out, ">$input/data/lists/raw_train.list") || die "ERROR: can't open $input/data/raw_train.list.\n";
open(mfcc_out, ">$input/data/lists/mfc_train.list") || die "ERROR: can't open $input/data/mfc_train.list .\n";

print "$temp_list";
$count++;

#$workdir=`pwd`;

$workdir = $input;
print "$temp_list";
#print "$fp_file";
#while (<fp_file>) {

while (<fp_file>) {

#while ($temp_list) {

   #print "$temp_list";
    if ($count == 1) {
	$output = $_;
    }
    else {

	@fields = split(/\s+/, $_);
	print "$workdir\n";
	chomp($workdir);
	print fp_out "$workdir/$output_basename$fields[8]\n";

	$mfcc = $fields[8];
	$mfcc =~ s/raw/mfc/g;
	print mfcc_out "$workdir/$mfcc_basename$mfcc\n";

	print "processing file $fields[8]\n";
    }

    $count++;
}

$count -= 2;
print "total $count files\n";

close(fp_file);
close(fp_out);
}

sub create_transcription {

   # as we open the transcription file in append mode we remove the
   # transcriptin file everytime to make sure that we just dont append
   # transcriptions to already existing transcriptions.

    @args = ("rm $input/data/transcription.text");
    system(@args);
    $temp_list = "$input/data/lists/raw_train.list"; 
    open(fp_file, $temp_list);
    while (<fp_file>) {
	
	# removes the newline character from the line read from the file
	chop;
	$rawfile = $_;                            
	print "processing file $rawfile for transcription";
	
	#
	#
	@args = ("$arg2/vad $arg2/sigd_params.text $rawfile /$input/data/raw/temp.raw > $input/sigd.text");
	(system(@args) == 0) or return 0 ;	
	&extract_segments ;
	@args = ("rm /$input/data/raw/temp.raw");
	system(@args);
	
    }
    
}


sub extract_segments 
{

$sigd_list = "$input/sigd.text"; 
    open(sigd_file, $sigd_list);    

$seg_count = 0;
while (<sigd_file>) {
     $distance = $_;
     $distance =~ /beg_time = (.*)/g;
     $value = $1;
     if ($value =~ /([0-9\.]*) (.*)/ ) {
	 print "$value\n";
	$seg_count++;
     }
 }

# this transcription file needs to go to the corpora directory 


$transcription_file = "$input/data/transcription.text";

print "transcription file found at $input/data/transcription.text";
# here I am appending the file everytime. This will work as for everytime
# this is needed if featureextraction is done onece
#open (data,">>$transcription_file") || die("Cannot Open File");

# but as we do it everytime and also as we need to create a new one

open (data,">>$transcription_file") || die("Cannot Open File");

print data "sil";

while ($seg_count > 0) {
    
    print  data " speech sil";
    $seg_count--;
}
  print data "\n";
close (data);

# commenting needs to be removed

#@args = (" rm $input/sigd.text");
#system(@args);


}


sub train_models {

# building initial word models
#
@args = ("create_models -states $input/models/r00/hmm0/model_lengths.text -output $input/models/r00/hmm0/models.text");
system(@args);

@args = ("create_triphone_map -mono $input/models/r00/hmm0/monophones.text -clist $input/models/r00/hmm0/monophones.text -context ci -models $input/models/r00/hmm0/models.text -output $input/models/r00/hmm0/phones.text");
system(@args);

@args = ("init_hmm -input $input/data/lists/mfc_train.list -models $input/models/r00/hmm0/model_lengths.text -trans $input/models/r00/hmm0/transitions.text -state $input/models/r00/hmm0/states.text -mode binary -vfloor_file $input/models/r00/hmm0/vfloor.text -var_floor 0.002 -num_features 39");
system(@args);

# fix the bug in transitions.text
#
#@args = ("$input/models/r00/hmm0/correct_tr.sh");
#system(@args);

#@args = ("mv $input/models/r00/hmm0/transitions.text $input/models/r00/hmm0/transitions_temp.text");
#system(@args);

 ($argument_1 ,$argument_2 ) = ("$input/models/r00/hmm0/transitions_temp.text", "$input/models/r00/hmm0/transitions.text");

#$argument_1 = "$input/models/r00/hmm0/transitions_temp.text";
#$argument_2 = "$input/models/r00/hmm0/transitions.text";

#&correct_transitions($argument_1 , $argument_2);
&correct_transitions;

@args = ("convert_mmf -input_mode ascii -output_mode binary $input/models/r00/hmm0/states.text $input/models/r00/hmm0/states.bin");
system(@args);

# training 1-mixture models (4 passes of baum-welch)
#
@args = ("bw_train -p $input/models/r00/hmm0/params.text;bw_train -p $input/models/r00/hmm1/params.text;bw_train -p $input/models/r00/hmm2/params.text;bw_train -p $input/models/r00/hmm3/params.text");
system(@args);

# splitting and training 2-mixture models (4 passes of baum-welch)
#
@args = ("create_mixtures -input $input/models/r00/hmm4/states.bin -output $input/models/r00/hmm20/states.text -num_mixtures 2");
system(@args);

@args = ("convert_mmf -input_mode ascii -output_mode binary $input/models/r00/hmm20/states.text $input/models/r00/hmm20/states.bin");
system(@args);

@args = ("cp $input/models/r00/hmm4/transitions.text $input/models/r00/hmm20/transitions.text");
system(@args);

@args = ("bw_train -p $input/models/r00/hmm20/params.text;bw_train -p $input/models/r00/hmm21/params.text;bw_train -p $input/models/r00/hmm22/params.text;bw_train -p $input/models/r00/hmm23/params.text");
system(@args);

# splitting and training 4-mixture models (4 passes of baum-welch)
#

@args = ("create_mixtures -input $input/models/r00/hmm24/states.bin -output $input/models/r00/hmm40/states.text -num_mixtures 4");
system(@args);

@args = ("convert_mmf -input_mode ascii -output_mode binary $input/models/r00/hmm40/states.text $input/models/r00/hmm40/states.bin");
system(@args);

@args = ("cp $input/models/r00/hmm24/transitions.text $input/models/r00/hmm40/transitions.text");
system(@args);

@args = ("bw_train -p $input/models/r00/hmm40/params.text;bw_train -p $input/models/r00/hmm41/params.text;bw_train -p $input/models/r00/hmm42/params.text;bw_train -p $input/models/r00/hmm43/params.text");
system(@args);

# splitting and training 8-mixture models (4 passes of baum-welch)
#
@args = ("create_mixtures -input $input/models/r00/hmm44/states.bin -output $input/models/r00/hmm80/states.text -num_mixtures 8");
system(@args);

@args = ("convert_mmf -input_mode ascii -output_mode binary $input/models/r00/hmm80/states.text $input/models/r00/hmm80/states.bin");
system(@args);

@args = ("cp $input/models/r00/hmm44/transitions.text $input/models/r00/hmm80/transitions.text");
system(@args);

@args = ("bw_train -p $input/models/r00/hmm80/params.text;bw_train -p $input/models/r00/hmm81/params.text;bw_train -p $input/models/r00/hmm82/params.text;bw_train -p $input/models/r00/hmm83/params.text");
system(@args);

# splitting and training 16-mixture models (4 passes of baum-welch)
#
@args = ("create_mixtures -input $input/models/r00/hmm84/states.bin -output $input/models/r00/hmm160/states.text -num_mixtures 16");
system(@args);

@args = ("convert_mmf -input_mode ascii -output_mode binary $input/models/r00/hmm160/states.text $input/models/r00/hmm160/states.bin");
system(@args);

@args = ("cp $input/models/r00/hmm84/transitions.text $input/models/r00/hmm160/transitions.text");
system(@args);

@args = ("bw_train -p $input/models/r00/hmm160/params.text;bw_train -p $input/models/r00/hmm161/params.text;bw_train -p $input/models/r00/hmm162/params.text;bw_train -p $input/models/r00/hmm163/params.text");
system(@args);

@args = ("touch finish_train.txt");
system(@args);

}

sub create_exp_setup {


$tool_path =  $ENV{ISIP_DEVEL};

@args = ("mkdir $input/data $input/data/feature $input/data/lists $input/data/raw $input/models $input/models/r00 $input/models/r00/hmm0 $input/models/r00/hmm1 $input/models/r00/hmm2 $input/models/r00/hmm3 $input/models/r00/hmm4 $input/models/r00/hmm20 $input/models/r00/hmm21 $input/models/r00/hmm22 $input/models/r00/hmm23 $input/models/r00/hmm24 $input/models/r00/hmm40 $input/models/r00/hmm41 $input/models/r00/hmm42 $input/models/r00/hmm43 $input/models/r00/hmm44 $input/models/r00/hmm80 $input/models/r00/hmm81 $input/models/r00/hmm82 $input/models/r00/hmm83 $input/models/r00/hmm84 $input/models/r00/hmm160 $input/models/r00/hmm161 $input/models/r00/hmm162 $input/models/r00/hmm163 $input/models/r00/hmm164");
system(@args);

@args = ("cp $tool_path/util/demo/isip_demo_data/speaker_verification/models/speaker/decode/speaker_params.text $input/decode_params.text");
system(@args);

open(FILE,"<$input/decode_params.text");
  while(<FILE>){
    $_=~s/ISIP_VERIFICATION/$input/;
    push @new_file,$_;
    }
  close(FILE);

  # write substitution to file(s)

  open(FILE,">$input/decode_params.text");
  for $i(0..$#new_file){
    print FILE $new_file[$i];
    }
  close(FILE);

@args = ("cp $input/*.raw $input/data/raw");
system(@args);


@args = ("cp $tool_path/util/demo/isip_demo_data/speaker_verification/models/speaker/models/*.* $input/models/r00/hmm0");
system(@args);

@args = ("cp $tool_path/util/demo/isip_demo_data/speaker_verification/params/params.text $input/params.text");
system(@args);

open(FILE,"<$input/params.text");
  while(<FILE>){
    $_=~s/ISIP_VERIFICATION/$input/;
    push @new_file,$_;
    }
  close(FILE);

  # write substitution to file(s)

  open(FILE,">$input/params.text");
  for $i(0..$#new_file){
    print FILE $new_file[$i];
    }
  close(FILE);


@args = ("cp $input/params.text $input/models/r00/hmm0; perl -pi -e s/INPUT_DIR/hmm0/g $input/models/r00/hmm0/params.text ; perl -pi -e s/UPDATE_DIR/hmm1/g $input/models/r00/hmm0/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm1; perl -pi -e s/INPUT_DIR/hmm1/g $input/models/r00/hmm1/params.text ; perl -pi -e s/UPDATE_DIR/hmm2/g $input/models/r00/hmm1/params.text ");
system(@args);


@args = ("cp $input/params.text $input/models/r00/hmm2; perl -pi -e s/INPUT_DIR/hmm2/g $input/models/r00/hmm2/params.text ; perl -pi -e s/UPDATE_DIR/hmm3/g $input/models/r00/hmm2/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm3; perl -pi -e s/INPUT_DIR/hmm3/g $input/models/r00/hmm3/params.text ; perl -pi -e s/UPDATE_DIR/hmm4/g $input/models/r00/hmm3/params.text ");
system(@args);



@args = ("cp $input/params.text $input/models/r00/hmm20; perl -pi -e s/INPUT_DIR/hmm20/g $input/models/r00/hmm20/params.text ; perl -pi -e s/UPDATE_DIR/hmm21/g $input/models/r00/hmm20/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm21; perl -pi -e s/INPUT_DIR/hmm21/g $input/models/r00/hmm21/params.text ; perl -pi -e s/UPDATE_DIR/hmm22/g $input/models/r00/hmm21/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm22; perl -pi -e s/INPUT_DIR/hmm22/g $input/models/r00/hmm22/params.text ; perl -pi -e s/UPDATE_DIR/hmm23/g $input/models/r00/hmm22/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm23; perl -pi -e s/INPUT_DIR/hmm23/g $input/models/r00/hmm23/params.text ; perl -pi -e s/UPDATE_DIR/hmm24/g $input/models/r00/hmm23/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm40; perl -pi -e s/INPUT_DIR/hmm40/g $input/models/r00/hmm40/params.text ; perl -pi -e s/UPDATE_DIR/hmm41/g $input/models/r00/hmm40/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm41; perl -pi -e s/INPUT_DIR/hmm41/g $input/models/r00/hmm41/params.text ; perl -pi -e s/UPDATE_DIR/hmm42/g $input/models/r00/hmm41/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm42; perl -pi -e s/INPUT_DIR/hmm42/g $input/models/r00/hmm42/params.text ; perl -pi -e s/UPDATE_DIR/hmm43/g $input/models/r00/hmm42/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm43; perl -pi -e s/INPUT_DIR/hmm43/g $input/models/r00/hmm43/params.text ; perl -pi -e s/UPDATE_DIR/hmm44/g $input/models/r00/hmm43/params.text ");
system(@args);



@args = ("cp $input/params.text $input/models/r00/hmm80; perl -pi -e s/INPUT_DIR/hmm80/g $input/models/r00/hmm80/params.text ; perl -pi -e s/UPDATE_DIR/hmm81/g $input/models/r00/hmm80/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm81; perl -pi -e s/INPUT_DIR/hmm81/g $input/models/r00/hmm81/params.text ; perl -pi -e s/UPDATE_DIR/hmm82/g $input/models/r00/hmm81/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm82; perl -pi -e s/INPUT_DIR/hmm82/g $input/models/r00/hmm82/params.text ; perl -pi -e s/UPDATE_DIR/hmm83/g $input/models/r00/hmm82/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm83; perl -pi -e s/INPUT_DIR/hmm83/g $input/models/r00/hmm83/params.text ; perl -pi -e s/UPDATE_DIR/hmm84/g $input/models/r00/hmm83/params.text ");
system(@args);




@args = ("cp $input/params.text $input/models/r00/hmm160; perl -pi -e s/INPUT_DIR/hmm160/g $input/models/r00/hmm160/params.text ; perl -pi -e s/UPDATE_DIR/hmm161/g $input/models/r00/hmm160/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm161; perl -pi -e s/INPUT_DIR/hmm161/g $input/models/r00/hmm161/params.text ; perl -pi -e s/UPDATE_DIR/hmm162/g $input/models/r00/hmm161/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm162; perl -pi -e s/INPUT_DIR/hmm162/g $input/models/r00/hmm162/params.text ; perl -pi -e s/UPDATE_DIR/hmm163/g $input/models/r00/hmm162/params.text ");
system(@args);

@args = ("cp $input/params.text $input/models/r00/hmm163; perl -pi -e s/INPUT_DIR/hmm163/g $input/models/r00/hmm163/params.text ; perl -pi -e s/UPDATE_DIR/hmm164/g $input/models/r00/hmm163/params.text; rm $input/params.text");
system(@args);



}

sub correct_transitions {
# $in_file = $ARGV[3];
# $out_file = $ARGV[4];

@args = ("mv $input/models/r00/hmm0/transitions.text $input/models/r00/hmm0/transitions_temp.text");
system(@args);
#    my $in_file = shift @_;
#   my $out_file = shift @_;


$in_file = "$input/models/r00/hmm0/transitions_temp.text"; 
$out_file = "$input/models/r00/hmm0/transitions.text";

open(fp1_file, $in_file) || die "ERROR: can't open $in_file.\n";
open(fp1_out, ">$out_file") || die "ERROR: can't open $out_file.\n";

$count1++;

my ($line8, $line9, $line10, $line11);

while (<fp1_file>) {

    $myline = $_;
    if($count1 == 2) {
	$myline =~ s/2/3/g;
	print fp1_out "$myline";
    }
    else {
	print fp1_out "$myline";
    }

    if ($count1 == 8) {
	$line8=$_;
	$line8 =~ s/1/2/g;
    }

    if ($count1 == 9) {
	$line9 = $_;
    }
    if ($count1 == 10) {
	$line10 = $_;
    }
    if ($count1 == 11) {
	$line11 = $_;
    }

    print "processing file $myline\n";

    $count1++;
}

print fp1_out "$line8";
print fp1_out "$line9";
print fp1_out "$line10";
print fp1_out "$line11";


print "total $count files\n";

close(fp1_file);
close(fp1_out);

#@args = ("rm $input/models/r00/hmm0/transitions_temp.text");
#system(@args);

}
