csensemble

#!/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 $I1 = $ARGV[1];
my $I2 = $ARGV[2];
my $mode = $ARGV[3];
my %csH;
my (@a1, @a2, @a3);
$csH{$I1} = \@a1;
$csH{$I2} = \@a2;
$csH{'new2'} = \@a3;
&Replace;

######################################################################
# 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 start times in a file and write them into another.
sub Replace{
 my ($line,$modif);
 my $date=&Datem();
 open FH, "<$Infile" or die "can't open $Infile\n";
 while (<FH>) { 
 $line = $_;
 chomp($line);
 if ($line =~ /^$I1/) {
 push @{$csH{$I1}}, $line;
 }
 elsif ($line =~ /^$I2/) {
 push @{$csH{$I2}}, $line;
 }
 } # END while line; 
 close FH;
 for( my $n = 0; $n < (@{$csH{$I1}}); $n++ ){
 next unless ( defined( ${$csH{$I2}}[$n] ) );
 my $modelline = ${$csH{$I1}}[$n];
 my $modelstart = $modelline;
 my $modeldur = $modelline;
 $modelstart =~ s/^(i\d+\s+)([0-9.]+)(.*)$/$2/;
 $modeldur =~ s/^(i\d+\s+)([0-9.]+\s+)([0-9.]+)(.*)$/$3/;
 my $origline = ${$csH{$I2}}[$n];
 my $beg = $origline; 
 my $end = $origline;
 my $origstart = $origline;
 my $origdur = $origline;
 $beg =~ s/^(i\d+\s+)([0-9.]+)(.*)$/$1/;
 $origstart =~ s/^(i\d+\s+)([0-9.]+)(.*)$/$2/;
 $origdur =~ s/^(i\d+\s+)([0-9.]+\s+)([0-9.]+)(.*)$/$3/;
 my $comment;
 my $replacement;
 $end =~ s/^(i\d+\s+)([0-9.]+)(.*)$/$3/;
 $replacement = $modelstart."\t";
 $comment = "; csensemble ($date): start was $origstart ;";
 if( $mode =~ /^(sd)$/ ){
 $end = $origline;
 $end =~ s/^(i\d+\s+)([0-9.]+\s+)([0-9.]+)(.*)$/$4/;
 $replacement .= $modeldur."\t";
 $comment .= " dur was $origdur ;";
 }
 my $newline = $beg.$replacement.$end.$comment;
 push @{$csH{$I3}}, $newline; 
 }
 my $modif;
 my $i = 0;
 open FH, "<$Infile" or die "can't open $Infile\n";
 while (<FH>) { 
 $line = $_;
 if ($line =~ /^$I2/) {
 $line = ${$csH{$I3}}[$i++]."\n" ;
 }
 $modif .= $line;
 } # END while line; 
 close FH;
 open FH, ">outfile.csd" or die "can't w-open outfile.csd.\n";
 print FH $modif; 
 close FH;
 print "done\n";
} ## END Replace()

## Help:
sub Usage {
 my $subname = "usage";
 die "Usage:\n\n".
 "[ WARNING: csensemble doesn't respect sections' boundaries, on multi-sections \n".
 "scores, it is required to treat each of them in a separate file. ]\n".
 "This program can be used to replace the start time and duration of an instrument\n".
 " i2 by the same values of i1 in any file containing Csound i-statements.\n\n".
 " Try: ./csdmod2.pl infile i1 i2 mode\n".
 " [ the 'i' is mandatory in params i1 and i2 ]\n".
 " - result in 'outfile.txt';\n".
 " - i1 and i2 start at 1 (instrument's number);\n".
 " - mode can be 's' (treat start only) or 'sd' (default);\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