|
# Life
#
# Generate music from life
function weblife(gen,size,cellstring,scale,key,style,stepsize) {
p = 'a,b,c,d,e,f,g,a,b,c';
cells = []
for ( r=0; r<size; r++ ) {
cells[r] = []
for ( c=0; c<size; c++ ) {
pos = 1 + r*size + c
s = substr(cellstring,pos,1)
cells[r][c] = integer(s)
}
}
r = cells2scale(size,cells,scale,key,stepsize,style)
# printcells(size,cells);
for ( n=1; n<gen; n++ ) {
cells = lifegen(size,cells);
# printcells(size,cells);
r += cells2scale(size,cells,scale,key,stepsize,style)
}
writemf(r,"www.mid")
writelines(r,"www.lines")
}
function printcells(size,cells) {
printf(" ");
for ( r=0; r<size; r++ ) {
for ( c=0; c<size; c++ ) {
v = cells[r][c];
printf(string(v));
}
printf("<br>");
}
}
function lifegen(size,cells) {
newcells = []
for ( r=0; r<size; r++ ) {
newcells[r] = []
for ( c=0; c<size; c++ ) {
v = cells[r][c];
rowup = r-1
if ( rowup < 0 )
rowup = size-1
rowdown = r+1
if ( rowdown >= size )
rowdown = 0
colleft = c-1
if ( colleft < 0 )
colleft = size-1
colright = c+1
if ( colright >= size )
colright = 0
tot = cells[rowup][colleft]
+cells[rowup][c]
+cells[rowup][colright]
+cells[r][colleft]
+cells[r][colright]
+cells[rowdown][colleft]
+cells[rowdown][c]
+cells[rowdown][colright]
;
# print("r=",r," c=",c," v=",v," tot=",tot)
if ( v == 0 && tot == 3 ) {
# print("BIRTH r=",r," c=",c)
newcells[r][c] = 1 # birth
} else if ( v == 1 && tot != 2 && tot != 3 ) {
# print("DEATH r=",r," c=",c)
newcells[r][c] = 0 # death
} else {
newcells[r][c] = v
}
}
}
return(newcells)
}
function cells2scale(size,cells,scale,key,stepsize,style) {
sc = scalenamed(scale);
# print("sc=",sc);
scalesize = sizeof(sc);
scalenotenum = 1
oct = 0
scalenote = []
basent = phrase("'"+key+"'");
for ( n=0; n<size; n++ ) {
scalenote[size-n-1] = basent
scalenote[size-n-1].pitch += sc%scalenotenum.pitch + oct*12
# print ("scale n=",size-n-1," scalenote=",scalenote[size-n-1]);
scalenotenum++
if ( scalenotenum > scalesize ) {
scalenotenum = 1
oct++;
}
}
p = ''
for ( r=0; r<size; r++ ) {
for ( c=0; c<size; c++ ) {
if ( cells[r][c] ) {
nt = scalenote[r]
nt.time = c*stepsize
nt.dur = stepsize
p |= nt
}
}
}
if ( style == "raw" ) {
p.length = stepsize*size
} else if ( style == "arpeggiate" ) {
p = arpeggio(p)
p.length = latest(p)
} else if ( style == "highest" ) {
p = mono(p)
p.length = stepsize*size
} else if ( style == "lowest" ) {
p = mono(p,1)
p.length = stepsize*size
}
return(p)
}
|