#!/usr/bin/perl
#
# Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
# Creation Date: Mon Oct  3 09:51:30 PDT 2005
# Last Modified: Sat Oct  8 07:32:12 PDT 2005
# Last Modified: Mon Oct 24 19:02:02 PDT 2005 (allowed for missing beats)
# Filename:      trials2mma
# Syntax:        perl 5
#
# Description:   Converts multiple reverse conducting data files containing
#                a single trial into a Mathematica data file
#                with one trial on a line in an array.
#
# Usage:         ./trials2mma trial*.dat > combinedtrials.mma
#

use strict;

my $filecount = 0;

# set the variable name for the array to be related 
# to the name of the first file.
my $variable = "data";

my $file;
my $offsetq = 0;
my $offset = 0;
foreach $file (@ARGV) {
   if ($file =~ /^-o/) {
      $offsetq = 1;
   }
}


foreach $file (@ARGV) {
   next if $file =~ /^-/;
   processFile($file);
}

print "};\n";

exit(0);

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



##############################
##
## processFile --
##

sub processFile {
   my ($file) = @_;
   open (DFILE, $file) || die "Cannot read file $file";
   my @data;
   $filecount++;

   my $asum = 0;

   my $line;
   my $comment;
   while ($line = <DFILE>) {
      chomp $line;

      if ($line =~ /^\!\!\!offset:\s*([\d\+\.-]+)/) {
         $offset = $1 if $offsetq > 0;
      }

      if (($filecount == 1) && ($line =~ /^\!\!\!(.*)/)) {

         $comment = "$1\t";
         if ($comment =~ /trial:\s+\d+\/(\d+)/) {
            $comment = "trials:\t\t$1\t\t";
         }
         if ($comment =~ /reverse-conductor/) {
            $comment =~ s/\t$//;
         }
         if ($comment =~ /performance-date/) {
            $comment .= "\t";
         }
         if ($comment =~ /label/) {
            $comment =~ s/\t$//;
         }
         if ($comment =~ /trial-hardware/) {
            $comment =~ s/\t$//;
         }
         if ($comment =~ /composition/) {
            $comment =~ s/\t$//;
         }

         $variable = $file;
         $variable =~ s/\..*$//;
         $variable =~ s/-/x/g;
         $variable =~ s/x\d\d$//;
         $variable .= "abs" if $offsetq > 0;

         next if $comment =~ /^offset/;
         print "(* $comment\t*)\n";
      }


      next if $line =~ /^\!/;
      next if $line =~ /^\*/;
      next if $line =~ /^\s*$/;
      next if $line =~ /^=/;
      if ($line =~ /^(\d+)\t(\d+)\t(\d+)\t(\d+)/) {
         $data[@data] = $4;
      }
   }
   if ($filecount == 1) {

      if ($offsetq == 0) {
      print <<"EOT";

(* 
    The following data consists of delta times between conducted beats.
    Each conducting trial is on a separate line.  The first beat starts
    at time 0, so the first number on a line represents the time in 
    milliseconds to wait until the next conducted beat occurs.
*)

EOT
      } else {

      print <<"EOT";

(*
   The following data consists of absolute times of each tap into
   the recording of a particular performance.  The first beat starts
   off at time 0 in the data acquisition, but all values consists of
   a constant time offset to match the tapping times up to the beat
   times in the recording.
*)

EOT

      }


      print "\n$variable = {\n";
   }

   my $i;
   if ($filecount < 10) {
      print "(* trial  $filecount *)\t{";
   } else {
      print "(* trial $filecount *)\t{";
   }

   # skip the first beat which is always 0 (or at least should be)
   my $totalcount = @data;
   my $start = 1;
   $start = 0 if $offsetq > 0;
   
   for ($i=$start; $i<@data; $i++) {
       if ($data[$i] !~ /\d/) {
          print " (* beat $i is empty *) ";
          $totalcount = $totalcount - 1;
          next;
       }
       print $data[$i] + $offset + $asum;
       $asum += $data[$i] if $offsetq > 0;
       if ($i < $totalcount - 1) {
          print ", ";
       }
   }
   print "}";
   if ($filecount < @ARGV) {
      print ",";
   }

   print "\n";

}