cscorebytime

#!/usr/bin/perl
# emilbarton 130813. 
######################################################################
if ((@ARGV)== 0 || @ARGV[0] =~ /help/){&Usage()};
die "No such file @Args[0]." unless (-f @ARGV[0]);
# Global:
my $Infile = $ARGV[0];
my %csH;
my $Score;
&Gather;

######################################################################
# Subroutines:
sub Datem {
 my $mode = shift;
 my $subname = "Datem"; 
 my $error; 
 my @date = localtime ; 
 my $year = @date[5] + 1900 ;
 my $month = @date[4] + 1 ; $month = "0".$month if (length ($month) == 1);
 my $day = @date[3]; $day = "0".$day if (length ($day) == 1);
 my $hour = @date[2]; $hour = "0".$hour if (length ($hour) == 1);
 my $min = @date[1]; $min = "0".$min if (length ($min) == 1);
 my $sec = @date[0]; $sec = "0".$sec if (length ($sec) == 1);
 if ($mode =~ /print/){ return ("$year $month $day $hour $min $sec") }
 elsif ($mode =~ /digit/) { return ("$year$month$day$hour$min$sec") }
 else { return ("$year\-$month\-$day\.$hour\:$min\:$sec") }
}
## Change cs instr order in a file and write them into another.
sub Gather{ 
 my ($line,$modif);
 my $date=&Datem();
 open FH, "<$Infile" or die "can't open $Infile\n";
 while (<FH>) { 
 $line = $_;
 if ($line =~ /^(\s*;*\s*i\s*\d+\s+)([0-9.]+)(\s+.*)$/) {
 my $start = $line; $start =~ s/^(\s*;*\s*i\s*\d+\s+)([0-9.]+)(\s+.*)$/$2/;
 $csH{$start} .= $line;
 # print $csH{$start};

 }
 elsif ($line =~ /^(\s*s|e.*)$/) {
 my $section = &OrderScore(\%csH);
 $Score .= $section;
 $Score .= $line;
 }
 else{ $Score .= $line; }

 } # END while line; 
 close FH;
 open FH, ">outfile.csd" or die "can't w-open outfile.csd.\n";
 print FH $Score; 
 close FH;
 print "done\n";
}## END Gather()

# Order:
sub OrderScore {
 my $href = shift;
 my $section;
 for my $start ( sort { $a <=> $b } keys %$href ) {
 $section .= $href->{$start};
 delete $href->{$start};
 }
 return $section;
} ## END rowtest()
## Help:
sub Usage {
 my $subname = "usage";
 die "Usage:\n\n".
 "This program can be used to reorder instruments by start time\n".
 " and by section in any file containing Csound i-statements.\n\n".
 " Try: ./cscorebytime.pl infile\n".
 " - result in 'outfile.csd';\n".
 "\n";
} ## END Usage()

0;
__DATA__
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s