Csound Csound-dev Csound-tekno Search About

Csound fractal granular score generator

Date1999-01-08 01:20
FromHans Mikelson
SubjectCsound fractal granular score generator
#!/usr/bin/perl
#-------------------------------------------------------------------------
# Automatic Csound Score Generation by Hans Mikelson January 1999
#
# This program generates a fractal csound score for use in granular
synthesis.
#-------------------------------------------------------------------------
# SUBROUTINES
#-------------------------------------------------------------------------

# Mandeloop
sub mandeloop
{
  my $cr = $_[0];
  my $ci = $_[1];
  my $zr = 0;
  my $zi = 0;
  my $zzr = 0;

  $grain{$_[2]} = 0;

  while (($grain{$_[2]} < 200) && ($zr*$zr + $zi*$zi < 4))
   {
#   z = z^2 + c
    $zzr = $zr*$zr - $zi*$zi + $cr;
    $zi  = 2*$zr*$zi + $ci;
    $zr  = $zzr;
    $grain{$_[2]}++;
   }
}

# Barnsleyj1
sub barnsleyj1
{
  my $cr = $_[0];
  my $ci = $_[1];
  my $zzr = 0;

# z(0) = pixel;
  $grain{$_[4]} = $_[2];
  $grain{$_[5]} = $_[3];

# if real(z) >= 0
#   z(n+1) = (z-1)*c
# else
#   z(n+1) = (z+1)*c

  if ($grain{$_[4]} >= 0)
   { 
    $zzr = ($grain{$_[4]}-1)*$cr - $grain{$_[5]}*$ci;
    $grain{$_[5]} = $grain{$_[5]}*$cr + $ci*($grain{$_[4]} - 1);
    $grain{$_[4]} = $zzr;
   }
  else
   { 
    $zzr = ($grain{$_[4]} + 1)*$cr - $grain{$_[5]}*$ci;
    $grain{$_[5]} = $grain{$_[5]}*$cr + $ci*($grain{$_[4]} + 1);
    $grain{$_[4]} = $zzr;
   }
}

# Henon Attractor
sub henon
{
  my $a = $_[0];
  my $b = $_[1];
  my $xx = 1 + $grain{$_[3]} - $a*$grain{$_[2]}*$grain{$_[2]};
  my $yy = $b*$grain{$_[2]};
  $grain{$_[2]} = $xx;
  $grain{$_[3]} = $yy;
}

sub gs1
{
  $stao = $_[0];
  $stam = $_[1];
  $duro = $_[2];
  $durm = $_[3];
  $ampo = $_[4];
  $ampm = $_[5];
  $fqco = $_[6];
  $fqcm = $_[7];
  $iter = $_[8];

  $maxpan = 10;

  for ($i=0; $i<$iter; $i++)
   {
    $sy = int($i/sqrt($iter));
    $sx = $i % int(sqrt($iter));

    henon(1.4, .3, 'sta', 'dur');
    barnsleyj1(1.2, .5, -1.3+.1*$sx, .25+.1*$sy, 'amp', 'fqc');
    mandeloop(-1.5+.01*$sx, -1+.01*$sy, 'pan');

    if (abs($grain{'pan'})>$maxpan) {$maxpan = abs($grain{'pan'})}
    
    $sta = $stam*int($sf*abs($grain{'sta'}))/$sf + $stao;
    $dur = $durm*int($sf*abs($grain{'dur'}))/$sf + $duro;
    $amp = $ampm*int($sf*abs($grain{'amp'}))/$sf + $ampo;
    $fqc = $fqcm*int($sf*abs($grain{'fqc'}))/$sf + $fqco;
    $pan = int($sf*abs($grain{'pan'}))/$sf/$maxpan;

    print SCORE "i1 $sta $dur $amp $fqc $pan\n";
   }
}

sub gs2
{
  $stao = $_[0];
  $stam = $_[1];
  $duro = $_[2];
  $durm = $_[3];
  $ampo = $_[4];
  $ampm = $_[5];
  $fqco = $_[6];
  $fqcm = $_[7];
  $iter = $_[8];

  $maxpan = .001;

  for ($i=0; $i<$iter; $i++)
   {
    henon(1.4, .3, 'sta', 'dur');
    henon(1.4, .3, 'amp', 'fqc');
    henon(1.4, .3, 'pan', 'atk');
    if (abs($grain{'pan'})>$maxpan) {$maxpan = abs($grain{'pan'})}
    
    $sta = $stam*int($sf*abs($grain{'sta'}))/$sf + $stao;
    $dur = $durm*int($sf*abs($grain{'dur'}))/$sf + $duro;
    $amp = $ampm*int($sf*abs($grain{'amp'}))/$sf + $ampo;
    $fqc = $fqcm*int($sf*abs($grain{'fqc'}))/$sf + $fqco;
    $pan = int($sf*abs($grain{'pan'}))/$sf/$maxpan;

    print SCORE "i1 $sta $dur $amp $fqc $pan\n";
   }
}

# Main Program begins here

$scorefile = "fscore.sco";
open (SCORE, ">$scorefile");
print SCORE "f1 0 8192 10 1\n\n";

$sf = 1000;
#   Sta        Dur       Amp          Fqc         Grains
#   Min   Rng  Min  Rng  Min    Rng   Min   Rng   #
gs1(0,    2,   .1,  4,   10,    2000, 200,  3000, 100);
gs1(3,    1,   .1,  4,   20,    4000, 400,  400,  20);
gs1(5,    2,   .3,  2,   30,    1000, 480,  1000, 30);
gs1(6,    1,   .1,  7,   40,    1000, 200,  200,  200);
gs1(7,    2,   .5,  4,   50,    2000, 200,  1000, 120);
gs1(8,    3,   .1,  4,   100,   3000, 300,  400,  20);
gs1(9,    2,   .3,  .2,  200,   2000, 280,  300,  40);
gs1(10,   2,   .2,  .3,  300,   3000, 100,  200,  80);
gs1(12,   2,   .1,  .4,  400,   2000, 200,  100,  100);
gs1(14,   1,   .1,  .8,  500,   1000, 200,  90,   20);
gs1(15,   2,   .3,  1,   700,   400,  280,  80,   30);
gs1(16,   4,   .4,  1.5, 800,   500,  300,  70,   100);
gs1(17,   3,   .5,  2,   900,   600,  330,  60,   110);
gs1(18,   3,   .6,  4,   1000,  700,  440,  50,   120);
gs1(20,   2.5, .7,  6,   1100,  800,  580,  30,   150);
gs1(22,   2,   1,   8,   1200,  1000, 600,  20,   180);
;
gs1(26,   1,   .2,  9,   500,   400,  880,  20,   100);
gs1(30,   1,   .2,  9,   600,   400,  700,  40,   100);
gs1(34,   1,   .2,  9,   700,   400,  630,  80,   100);
gs1(38,   1,   .2,  9,   800,   400,  540, 160,   100);
gs1(42,   1,   .2,  9,   900,   400,  480, 320,   100);
gs1(46,   1,   .2,  9,   1000,  400,  300, 440,   100);
gs1(50,   1,   .2,  9,   1100,  400,  280, 540,   100);
gs1(54,   1,   .2,  9,   1200,  400,  240, 640,   100);
gs1(58,   1,   .2,  9,   1300,  400,  220, 840,   100);
gs1(62,   1,   .2,  9,   1400,  400,  200, 1040,  100);
gs1(66,   1,   .2,  9,   1500,  400,  180, 1240,  100);

close SCORE;