#!/usr/bin/perl
#
# Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
# Creation Date: Fri Jan 13 06:07:57 PST 2006
# Last Modified: Fri Jan 13 06:07:57 PST 2006
# Filename:      /tmp/r/processfile
# Syntax:        perl 5
#
# Description:
#


use strict;

my $datafile = $ARGV[0];
my $headerfile = $ARGV[1];
my $offset = -14;

printHeader($headerfile);

my @measures;
my @beats;
my @timings1;
my @timings2;
my @timings3;
my @timings4;
my @timings5;
my @timings6;

my $line;
my @pieces;

open (DFILE, "$datafile") || die;
my $offsetq = 0;

while ($line = <DFILE>) {
   chomp $line;
   $line =~ s/^\s+//;
   $line =~ s/\s+$//;
   next if $line =~ /^\#/;                      # comment
   next if $line =~ /^;/;                       # comment
   next if $line =~ /^\s*$/;
   $line =~ s/^\s+//;
   $line =~ s/\s+$//;

   if ($offsetq == 0) {
      print "!!!offset_correction:\t$offset\n";
      $offsetq = 1;
   }

   @pieces = split(/\s+/, $line);
   storePieces(@pieces);
}



printPieces();


exit(0);


##########################################################################


##############################
##
## printPieces --
##

sub printPieces {

   my $i;

   print "**bar\t**abeat\t**kern\t**time\t**time\t**time\t**time\t**time\t**time\n";
   for ($i=0; $i<@measures; $i++) {
      print "$measures[$i]";
      print "\t";
      print "$beats[$i]";
      print "\t";
      if ($i < @measures-1) {
         print getDuration($beats[$i], $beats[$i+1]);
      } else {
         print ".";
      }
      print "\t";
      print "$timings1[$i]";
      print "\t";
      print "$timings2[$i]";
      print "\t";
      print "$timings3[$i]";
      print "\t";
      print "$timings4[$i]";
      print "\t";
      print "$timings5[$i]";
      print "\t";
      print "$timings6[$i]";
      print "\n";
   }
   print "*-\t*-\t*-\t*-\t*-\t*-\t*-\t*-\t*-\n";

}


sub getDuration {
   my ($num1, $num2) = @_;

   $num1 = int($num1 + 0.5) if $num1 !~ /\./;
   $num2 = int($num2 + 0.5) if $num2 !~ /\./;

   if ($num2 - $num1 == 1)  {
      return "4";
   } elsif ($num2 - $num1 == 2)  {
      return "2";
   } elsif ($num2 - $num1 == 3)  {
      return "2.";
   } elsif ($num2 - $num1 == 4)  {
      return "1";
   } else {
      return "error:$num1:$num2";
   }
}


sub storePieces {
   my @pieces = @_;
   my $i;
   my $num;


   #
   #for ($i=0; $i<@pieces; $i++) {
   #   $num = getNumber($pieces[$i]);
   #   print $num;
   #   if ($i < @pieces-1) {
   #      print "\t";
   #   }
   #}
   #print "\n";


   # only want to print the beat number and the corrected timr for now...



   $measures[@measures] = getNumber($pieces[0]);
   $beats[@beats] = getNumber($pieces[1]);

   $timings1[@timings1] = int(getNumber($pieces[2]) * 1000.0 + 0.5) + $offset;
   $timings2[@timings2] = int(getNumber($pieces[3]) * 1000.0 + 0.5) + $offset;
   $timings3[@timings3] = int(getNumber($pieces[4]) * 1000.0 + 0.5) + $offset;
   $timings4[@timings4] = int(getNumber($pieces[5]) * 1000.0 + 0.5) + $offset;
   $timings5[@timings5] = int(getNumber($pieces[6]) * 1000.0 + 0.5) + $offset;
   $timings6[@timings6] = int(getNumber($pieces[7]) * 1000.0 + 0.5) + $offset;

}


sub getNumber {
   my ($string) = @_;
   my $exponent;
   my $mantissa;

   if ($string =~ /^\s*$/) {
      return "error";
   }

   my $value;
   $string =~ /^([0-9\.]+)e([0-9\+\-]+)/i;
   $mantissa = $1;
   $exponent = $2;
   $value = $mantissa * (10 ** $exponent);
   return $value;
}



##############################
##
## printHeader --
##

sub printHeader {
   my ($headerfile) = @_;

   open (HFILE, $headerfile) || die;

   my $line;
   my $fnext = 0;
   my $pid = "";
   my $type = "";
   
   my $date = `date`;
   my $value;
   chomp $date;
   
   print "!!!date:\t$date\n";
   
   while ($line = <>) {
      chomp $line;
   
      if ($fnext == 1) {
         if ($line =~ /(pid\d+[a-z]?-\d+)/) {
            print "!!!pid:\t$1\n";
         }
         if ($line =~ /notes(\d\d)/) {
            $value = int($1);
            print "!!!tap_trials:\t$value\n";
         } else {
            print "!!!tap_trials:\t20\n";
         }
         print "!!!tap_input:\t$line\n";
         $fnext = 0;
         next;
      }
   
      if ($line =~ /k\s*=\s*(.*)/) {
         print "!!!wavelet_k:\t$1\n";
      } elsif ($line =~ /window\s*=\s*(.*)/) {
         print "!!!wavelet_window:\t$1\n";
      } elsif ($line =~ /frequency\s*correction\s*factor\s*=\s*(.*)/) {
         print "!!!wavelet_freq_corr_factor:\t$1\n";
      } elsif ($line =~ /LPF\s*order\s*=\s*(.*)/) {
         print "!!!wavelet_LPF_order:\t$1\n";
      } elsif ($line =~ /LPF\s*cutoff\s*=\s*(.*)/) {
         print "!!!wavelet_LPF_cutoff:\t$1\n";
      } elsif ($line =~ /Windowing method:\s*(.*)\s*$/) {
         $type = $1;
         print "!!!window_method:\t$type";

         print " [-0.33ioi:0.33ioi]" if $type eq "a";
         print " [-0.50ioi:0.50ioi]" if $type eq "b";
         print " [-0.67ioi:0.67ioi]" if $type eq "c";
         print " [-0.33ioi:0.67ioi]" if $type eq "d";
         print " [-1.00ioi:1.00ioi]" if $type eq "e";
         print " [-100ms:100ms]"     if $type eq "f";
         print " [-200ms:200ms]"     if $type eq "g";
         print " [-300ms:300ms]"     if $type eq "h";
         print " [-400ms:400ms]"     if $type eq "i";
         print " [-500ms:500ms]"     if $type eq "j";
         print " [-200ms:500ms]"     if $type eq "k";
         print " [-1sd:1sd]"     if $type eq "l";
         print " [-2sd:2sd]"     if $type eq "m";
         print " [-3sd:3sd]"     if $type eq "n";
         print " [-4sd:4sd]"     if $type eq "o";
         print " [-5sd:5sd]"     if $type eq "p";
         print " [-2sd:4sd]"     if $type eq "q";

         print "\n";
      } elsif ($line =~ /Input\s*data\s*filename/) {
         $fnext = 1;
      }
   
   }

}


