sf2test

#!/usr/bin/perl

##
# sf2test
# emilbarton 131015. 
##
use Term::ReadLine;
use Term::ReadLine::Gnu;

# VARIABLES

my $SF2_PATH = "/usr/lib/csound/IN/sf2/";
my $CSD_PATH = "/usr/bin/csound";
my $CSD_OPTS = "-+rtaudio=jack -odac -iadc";
$SF2_PATH = $ENV{SF2_PATH} unless not( defined $ENV{SF2_PATH} );
$CSD_PATH = $ENV{CSD_PATH} unless not( defined $ENV{CSD_PATH} );
$CSD_OPTS = $ENV{CSD_OPTS} unless not( defined $ENV{CSD_OPTS} );
my $Cmd;
my $Term = Term::ReadLine->new("Perl Shell");
$Term->ReadHistory(".history");
$Term->StifleHistory(100000);
my @Data;
while (<DATA>) { push @Data, $_; }

# READLINE

print "\n*** sf2test.pl: type 'help' to get some usage info. ***\n\n";

while (defined($Cmd = $Term->readline (localtime()." $ENV{PWD} :\n\n"))){ 
 $RPID = $$;
 if ($Cmd =~ /^~/) {
  $Cmd =~ s/^\~//;
  $Cmd = "ls -lsh" if ($Cmd =~ /^(ls)$/);
  my @output = `$Cmd` ;
  print @output;
  next;
 }
 else {
  no warnings;
  eval $Cmd ;
  print $@;
 }
}

# SUBS
sub comment {
 my $comment = shift;
 open FH, ">>overview.txt" or die "can't w-open overview.txt";
 print FH "$Instr:\t\t\t$comment (".localtime().")\n"; 
 close FH;
}

sub help {
 print "\n*** Help for sf2test.pl ***\n\n";
 print "* First set 3 environment variables:\n";
 print " \$CSD_PATH to the path of your Csound executable (default = /usr/bin/csound),\n";
 print " \$SF2_PATH to the path of your sf2 directory (default = /usr/lib/csound/IN/sf2/)\n";
 print " \$SF2_OPTS to your Csound realtime options (default = -+rtaudio=jack -odac -iadc).\n\n";
 print "* To test a soundfont, cd to your \$SF2_PATH (writable) and try for example :\n\n";
 print " play \"brass/19Trumpet.sf2\"\n\n";
 print " This will attempt to play the soundfont with prog=0 and bank=0 but display its settings too.\n";
 print " Then once these figures are known, do:\n\n";
 print " play \"brass/19Trumpet.sf2\", 56, 0\n\n";
 print "* To comment the last soundfont into the file overview.txt, try for example :\n\n";
 print " comment \"This sf2 is realistic; prog=56, bnk=0\"\n\n";
 print " This will append a new line in the file overview.txt\n";
 print " with the name of the last sf2 played followed by this comment.\n\n";
 print "* Otherwise, to launch a shell command like 'more overview.txt', do:\n\n";
 print " ~ more overview.txt\n\n";
 print "* Type 'quit' to exit and 'help' to see this message again.\n\n";
}

sub play {
 my $instr = @_[0];
 my $preset = @_[1];
 my $bank = @_[2];
 my $params;
 $preset = 0 unless $preset =~ /\d+/;
 $bank = 0 unless $bank =~ /\d+/;
 my $data;
 foreach my $line (@Data) { 
  if( $line =~ /\s*gisf1\s+sfload\s+\"[^\"]+\"\s*/ ){
 $line =~ s/^(\s*gisf1\s+sfload\s+)(\"[^\"]+\"\s*)(;?.*)$/$1\"$SF2_PATH\/$instr\"$3/ ;
  }
  if( $line =~ /^(\s*i\s*[0-9.]+\s+.+)(\s+[0-9.]+\s+[0-9.]+)(\s+[0-9.]+\s+[0-9.]+.*)$/ ){
   $line =~ s/^(\s*i\s*[0-9.]+\s+.+)(\s+[0-9.]+\s+[0-9.]+)(\s+[0-9.]+\s+[0-9.]+.*)$/$1\t$preset\t$bank$3/ ;
  }
  $data .= $line;
 }
 # WRITE
 open FH, ">tmp.csd" or die "can't w-open tmp.csd";
 print FH $data; 
 close FH;
 # PLAY
 my $pid=fork();
 die "Cannot fork: $!" if (! defined $pid);
 if (! $pid) {
  exec("/usr/bin/xterm", "-sb", "-sl", 10000, "-si", "-e","$CSD_PATH $CSD_OPTS -b1000 -B4096 -d tmp.csd & $ENV{SHELL}");
 }
 $Instr = $instr;
}
sub quit { 
 $Term->WriteHistory(".history");
 exit;
} 

1;
__DATA__
<CsoundSynthesizer>
<CsOptions>
-m3 --displays -odac
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 10
nchnls = 2
;; REVERB:
garvb init 0;
garvbsig init 0;
garvb1 init 0;
gamet1 init 0;
gacmb1 init 0;
ga1eff1 init 0;
ga2eff1 init 0;
gisf1 sfload "/my/sound.sf2";
sfilist gisf1;
sfplist gisf1;
instr 1
;;parameters names
; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
; i st dur amp num ft1 atk rel hd1 hd2 gl1 gl2 fq1 fq2 fq3 pa1 pa2 del rvn rvs vel frq idx pnb bnk rvs off
;;Csgrouper param recognition:
; CSGF=3:dur=1; CSGF=4:amp=1; CSGF=5:mid=60; CSGF=6:ft1=0; CSGF=7:atk=0.02; CSGF=8:rel=0.2; CSGF=9:hd1=0.02; CSGF=10:hd2=0.02; 
; CSGF=11:gl1=0-02; CSGF=12:gl2=0.02; CSGF=13:fq1=1; CSGF=14:fq2=1; CSGF=15:fq3=1; CSGF=16:pa1=0.5; CSGF=17:pa2=0.5;
; CSGF=18:del=0.1; CSGF=19:rvn=1; CSGF=20:rvs=0.05; CSGF=21:vel=0.5; CSGF=22:frq=1; CSGF=23:idx=0; CSGF=24:pnb=0;
; CSGF=25:bnk=0; CSGF=26:rvs=0; CSGF=27:off=0;
isf= gisf1 ; 
; 
idur = p3 ; ENVELOPE
iamp = p4 ; 
inum = p5 ; 
ift1 = p6 ; 
iatk = p7 ; 
irel = p8 ; 
ihd1 = p9 ; during the attack
ivel = p21 ; 
idx = p23 ; 
ista = idur-iatk-irel ; 
if (ift1 == 0) goto NOFTNV ; 
kenv envlpx iamp, iatk, idur, irel, ift1, 1, .01 ; 
goto GLISS ; 
; 
NOFTNV: ; 
kenv linseg 0, iatk, iamp, idur-irel-iatk, iamp, irel, 0 ; END ENVELOPE
; 
GLISS: ; PITCH
ifreq = p22 ; 
ihd2 = p10 ; during the release
igl1 = p11 ; from freq1 to freq2
igl2 = p12 ; from freq2 to freq3
itop = idur-ihd1-ihd2-igl1-igl2 ; 
ifq1 = p13 ; 1 / local factor
ifq2 = p13 ; 
ifq3 = p13 ; 
iflg = p26 ; 0=local pitch/GLISS, 1=note num.
if (iflg == 1) goto NOGLIS ; absolute freq.
if (ifreq == 1) goto NOGLIS ; notenum no GLISS.
ifq2 = p14 ; 
ifq3 = p15 ; 
kfrq linseg ifq1, ihd1, ifq1, igl1, ifq2, itop, ifq2, igl2, ifq3, ihd2, ifq3 ; 
goto ENDGLI ; 
; 
NOGLIS: ; 
kfrq = ifreq ; 
; 
ENDGLI: ; END PITCH 
; 
ipn1 = p16 ; PAN (*must* be set)
ipn2 = p17 ; 
idel = p18 ; pan ratio
ifd1 = idur-(idur*idel) ; 
ifd2 = idur-ifd1 ; 
ipn1 = abs(ipn1) ; Starting balance: <.5 = R, >.5 = L. 
ipn2 = abs(ipn2) ; Target balance.
kpan linseg ipn1, ifd1, ipn2, ifd2, ipn2 ; END PAN 
; 
iprg = p24 ; PRESET
ibnk = p25 ; 
ioff = p27 ; 
ipre sfpreset iprg, ibnk, isf, idx ; 
a1,a2 sfplay3 ivel, inum, iamp*kenv, kfrq, ipre, iflg, ioff ; END PRESET
; 
irvn = p19 ; EFFECT (nbr)
irvs = p20 ; (amp ratio)
; REVERB
if (irvn == 0) goto NOREV ; 
if (irvn == 1) goto REV1 ;
if (irvn == 2) goto REV2 ;
if (irvn == 3) goto REV3 ; 
if (irvn == 4) goto REV4 ; 
if (irvn == 5) goto ECH1 ; 
if (irvn == 6) goto EFF1 ; 
if (irvn == 7) goto EFF2 ; 
goto NOREV ; 
; 
REV1: ; 
garvbsig= garvbsig+(((a1+a2)/2)*irvs) ; 
goto NOREV ; 
REV2: ; 
garvb = garvb+(((a1+a2)/2)*irvs) ; 
goto NOREV ; 
REV3: ; 
garvb1 = garvb1+(((a1+a2)/2)*irvs) ; 
goto NOREV ; 
REV4: ; 
gamet1 = gamet1+(((a1+a2)/2)*irvs) ; 
goto NOREV ; 
ECH1: 
gacmb1 = gacmb1+(((a1+a2)/2)*irvs) ; 
goto NOREV ; 
EFF1: 
ga1eff1 = ga1eff1+(((a1+a2)/2)*irvs) ; 
goto NOREV ; 
EFF2: 
ga2eff1 = ga2eff1+(((a1+a2)/2)*irvs) ; 
goto NOREV ; 
; 
NOREV: ; END EFFECT 
; 
outs a1*(1-kpan),a2*kpan ; OUT
; 
endin
instr 576; 
 ; global reverb: receives multiple instruments
; CSGF=3:dur=60; CSGF=4:rvt=1;
irvbtime = p4
 asig reverb garvbsig,irvbtime
 outs asig,asig

 garvbsig = 0
endin

</CsInstruments>
<CsScore>
; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
; i st dur amp num ft1 atk rel hd1 hd2 gl1 gl2 fq1 fq2 fq3 pa1 pa2 del rvn rvs vel frq idx pnb bnk rvs off
i1 0 1 0.8 58 0 0.02 0.2 0.2 0.2 0.2 0.2 1 1 1 0.5 0.5 0.1 1 0.07 100 0 0 0 0 0 0 ;
i1 + . . 67 . . . . . . . . . . . . . . . . . . . . . . ;
i1 + . . 72 . . . . . . . . . . . . . . . . . . . . . . ;
i1 + . . 53 . . . . . . . . . . . . . . . . . . . . . . ;
i1 + . . 36 . . . . . . . . . . . . . . . . . . . . . . ;

e
</CsScore>
</CsoundSynthesizer>
Advertisements

One thought on “sf2test

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