cscoremod

#!/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 $Pos = $ARGV[1];
my $Rep = $ARGV[2];
&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 params 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;
 my $comment = "";
 my $pos = $Pos-2;
 if ($line =~ /^(\s*;*\s*i\s*\d+\s+)/) {
 $comment = $line;
 if( $Rep =~ /[\+\-\*\/]{1}[0-9.]+/ ){
 if( $Pos == 1 ) {
 $comment =~ s/^(;*\s*i\s*)([0-9]+)(\s+.+)$/$2/;
 $comment = "; cscoremod ($date): ins was i$comment ;";
 $line =~ m/^(;*\s*i\s*)([0-9]+)(\s+.+)$/;
 my $rep = eval("$2$Rep");
 $line =~ s/(;*\s*i\s*)([0-9]+)(\s+.+)/$1$rep$3/;
 }
 elsif( $Pos == 2 ){
 $comment =~ s/^(;*\s*i\s*[0-9]+)(\s+[0-9.]+)(\s+.+)$/$2/;
 $comment = "; cscoremod ($date): dur was $comment ;";
 $line =~ m/^(;*\s*i\s*[0-9]+)(\s+[0-9.]+)(\s+.+)$/;
 my $rep = eval("$2$Rep");
 $line =~ s/^(;*\s*i\s*[0-9]+)(\s+[0-9.]+)(\s+.+)$/$1\t$rep$3/;
 }
 elsif( $Pos > 2 ){
 $comment =~ s/^(;*\s*i\s*\d+)((\s+[0-9.]+){$pos})(\s+[0-9.]+)(.*)$/$4/;
 $comment = "; cscoremod ($date): p$Pos was $comment ;";
 $line =~ m/(;*\s*i\s*\d+)((\s+[0-9.]+){$pos})(\s+[0-9.]+)(.*)/;
 my $rep = eval("$3$Rep");
 $line =~ s/^(;*\s*i\s*\d+)((\s+[0-9.]+){$pos})(\s+[0-9.]+)(.*)$/$1$2\t$rep$5/; # 4th main () is named $5 due to inner ().
 }
 else { die("invalid pos (a).") }
 }
 else{ 
 if( $Pos == 1 ) { 
 $comment =~ s/^(;*\s*i\s*)([0-9]+)(\s+.+)$/$2/;
 $comment = "; cscoremod ($date): ins was i$comment ;";
 $line =~ s/^(;*\s*i\s*)([0-9]+)(\s+.+)$/$1$Rep$3/;
 }
 elsif( $Pos == 2 ){
 $comment =~ s/^(;*\s*i\s*[0-9]+)(\s+[0-9.]+)(\s+.+)$/$2/;
 $comment = "; cscoremod ($date): dur was $comment ;" ;
 $line =~ s/^(;*\s*i\s*[0-9]+)(\s+[0-9.]+)(\s+.+)$/$1\t$Rep$3/;
 }
 elsif( $Pos > 2 ){
 $comment =~ s/^(;*\s*i\s*\d+)((\s+[0-9.]+){$pos})(\s+[0-9.]+)(.*)$/$4/;
 $comment = "; cscoremod ($date): p$Pos was $comment ;";
 $line =~ s/^(;*\s*i\s*\d+)((\s+[0-9.]+){$pos})(\s+[0-9.]+)(.*)$/$1$2\t$Rep$5/; 
 }
 else { die("invalid pos (b).") }
 }
 } # END line =~ /^i/;
 $modif .= $line.$comment."\n";
 } # 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".
 "This program can be used to replace a given param in any file containing Csound i-statements.\n\n".
 " Try: ./cscoredmod.pl infile pos replacement-string\n".
 " - result in 'outfile.txt';\n".
 " - pos starts at 1 (instrument's number);\n".
 " - replacement-string can be literal or operate arithmetically \n".
 " on previous value e.g.: '127.333', '+0.3', '/2',..\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