Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- from math import sin,cos,tan,atan2,hypot,pi
- from copy import deepcopy
- x="x"
- y="y"
- azimuth="azimuth"
- hypot="hypot"
- pi=3.141592653
- class flowglyph:
- def __init__(self,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=0
- self.inpath=0
- class addglyph:
- def __init__(self,add,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=0
- self.add=add
- self.inpath=0
- class limiterglyph:
- def __init__(self,upper=1,lower=-1,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=0
- self.upper=upper
- self.lower=lower
- self.inpath=0
- class gtglyph:
- def __init__(self,gt,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=0
- self.gt=gt
- self.inpath=0
- class ltglyph:
- def __init__(self,lt,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=0
- self.lt=lt
- self.inpath=0
- class amplifierglyph:
- def __init__(self,magnitude,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.magnitude=magnitude
- self.value=0
- self.inpath=0
- class multiplierglyph:
- def __init__(self,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=0
- self.inpath=0
- class logglyph:
- def __init__(self,base,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.base=base
- self.value=0
- self.inpath=0
- class traceglyph:
- def __init__(self,varname,left=None,up=None,right=None,down=None):
- self.varname=varname
- self.value=0
- self.inpath=0
- class circleglyph:
- def __init__(self,startangle=0,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=startangle
- self.inpath=0
- def setvals(self):
- self.value=self.value%(2*pi)
- self.y=sin(self.value)
- self.x=cos(self.value)
- #self.hypot=hypot(self.x,self.y)
- class capacitorglyph:
- def __init__(self,capacity,threshold,dischargepercent=100,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.capacity=capacity
- self.threshold=threshold
- self.dischargepercent=dischargepercent
- self.value=0
- self.inpath=0
- def isnum(obj):
- return isinstance(obj,float) or isinstance(obj,int)
- class grid:
- def __init__(self,constants={},glyphs={}): #glyph {(x,y):glyph..}
- self.constants=constants
- self.glyphs=glyphs
- def pipein(self,pos,value):
- for i in self.glyphs:
- self.glyphs[i].inputs=0
- self.glyphs[pos].value+=value
- self.eminatefrom(pos)
- def makewave(self,inpos,inputwave,tracepoint):
- o=[]
- for i in inputwave:
- self.calculatepaths(inpos)
- self.pipein(inpos,i)
- o.append(self.traces()[tracepoint])
- self.next()
- return o
- def exportsound(self,wav):
- from struct import pack
- import wave
- out=wave.open("out.wav","wb")
- out.setparams((1,4,44100,1,"NONE","NONE"))
- for i in wav:
- out.writeframes(pack("l",(2**31-1)*i))
- def drawpath(self):
- s=""
- for j in range(32):
- for i in range(32):
- s+= str(self.glyphs[(i,j)].inpath) if (i,j) in self.glyphs else " "
- s+="\n"
- return s
- def traces(self):
- o={}
- for i in self.glyphs:
- if isinstance(self.glyphs[i],traceglyph):
- o[self.glyphs[i].varname]=deepcopy(self.glyphs[i].value)
- return o
- def next(self):
- for i in self.glyphs:
- g=self.glyphs[i]
- if isinstance(g,flowglyph) or isinstance(g,addglyph) or isinstance(g,multiplierglyph) or isinstance(g,amplifierglyph) or isinstance(g,logglyph) or isinstance(g,traceglyph) or isinstance(g,abstractglyph):
- self.glyphs[i].value=0
- def calculatepaths(self,pos,first=True):
- if first:
- for i in self.glyphs:
- self.glyphs[i].inpath=0
- currentglyph=self.glyphs[pos]
- if isinstance(currentglyph,traceglyph):
- currentglyph.inpath+=1
- else:
- if currentglyph.inpath>0:
- currentglyph.inpath+=1
- else:
- currentglyph.inpath+=1
- direction=[i!=None for i in [currentglyph.left,currentglyph.up,currentglyph.right,currentglyph.down]]
- if direction[0]:#left
- self.calculatepaths((pos[0]-1,pos[1]),False)
- if direction[1]:#up
- self.calculatepaths((pos[0],pos[1]-1),False)
- if direction[2]:#right
- self.calculatepaths((pos[0]+1,pos[1]),False)
- if direction[3]:#down
- self.calculatepaths((pos[0],pos[1]+1),False)
- def eminatedirection(self,val,dval,np,currentglyph):
- if np in self.glyphs:
- nextglyph=self.glyphs[np]
- if isnum(dval):
- if isinstance(nextglyph,multiplierglyph):
- if nextglyph.inputs==0:
- nextglyph.value=dval*val
- else:
- nextglyph.value*=dval*val
- else:
- nextglyph.value+=dval*val
- nextglyph.inputs+=1
- if isinstance(nextglyph,circleglyph):
- nextglyph.setvals()
- if nextglyph.inputs==nextglyph.inpath:
- self.eminatefrom(np)
- else:
- if isinstance(currentglyph,abstractglyph) and dval !=None:
- nextglyph.value+=currentglyph.traces[dval]
- if dval==x:
- nextval=currentglyph.x
- if dval==y:
- nextval=currentglyph.y
- if dval==azimuth:
- nextval=val
- if dval!=None:
- if isinstance(nextglyph,multiplierglyph):
- if nextglyph.inputs==0:
- nextglyph.value=nextval
- else:
- nextglyph.value*=nextval
- else:
- nextglyph.value+=nextval
- nextglyph.inputs+=1
- if isinstance(nextglyph,circleglyph):
- nextglyph.setvals()
- if nextglyph.inputs==nextglyph.inpath:
- self.eminatefrom(np)
- def eminatefrom(self,pos):
- currentglyph=self.glyphs[pos]
- if isinstance(currentglyph,abstractglyph):
- currentglyph.run()
- if isinstance(currentglyph,traceglyph):
- pass
- else:
- if isinstance(currentglyph,capacitorglyph):
- if currentglyph.value>currentglyph.capacity:
- currentglyph.value=currentglyph.capacity
- val=deepcopy(currentglyph.value)
- if isinstance(currentglyph,amplifierglyph):
- mag=currentglyph.magnitude
- if isinstance(mag,str):
- mag=self.constants[mag]
- val*=mag
- if isinstance(currentglyph,addglyph):
- add=currentglyph.add
- if isinstance(add,str):
- add=self.constants[add]
- val+=add
- if isinstance(currentglyph,logglyph):
- base=currentglyph.base
- if isinstance(base,str):
- base=self.constants[base]
- val=base**val
- if isinstance(currentglyph,capacitorglyph):
- threshold=currentglyph.threshold
- dcp=currentglyph.dischargepercent
- if isinstance(threshold,str):
- threshold=self.constants[threshold]
- if isinstance(dcp,str):
- dcp=self.constants[dcp]
- dcr=dcp/100
- if val>=threshold or val<=-threshold:
- fired=val*dcp/100
- val*=dcp/100
- else:
- val=0
- fired=False
- #left
- leftval=currentglyph.left
- np=(pos[0]-1,pos[1])
- self.eminatedirection(val,leftval,np,currentglyph)
- #up
- upval=currentglyph.up
- np=(pos[0],pos[1]-1)
- self.eminatedirection(val,upval,np,currentglyph)
- #right
- rightval=currentglyph.right
- np=(pos[0]+1,pos[1])
- self.eminatedirection(val,rightval,np,currentglyph)
- #down
- downval=currentglyph.down
- np=(pos[0],pos[1]+1)
- self.eminatedirection(val,downval,np,currentglyph)
- if isinstance(currentglyph,capacitorglyph) and fired!=False:
- currentglyph.value-=fired
- class abstractglyph:
- def __init__(self,grid,pipein,variables=["A"],left=None,up=None,right=None,down=None):
- self.grid=grid
- self.pipein=pipein
- self.variables=variables
- self.left=left
- self.up=up
- self.right=right
- self.down=down
- self.value=0
- def new(self,*variables):
- d={}
- for i in range(len(self.variables)):
- d[self.variables[i]]=variables[i]
- self.program=deepcopy(self.grid)
- self.program.constants=d
- return deepcopy(self)
- def run(self):
- self.program.calculatepaths(self.pipein)
- self.program.pipein(self.pipein,self.value)
- self.value=0
- self.traces=self.program.traces()
- self.program.next()
- ##Subprocess
- """
- harmonyglyph=abstractglyph(grid({},
- {
- (0,0):flowglyph(None,None,1,None),
- (1,0):amplifierglyph(pi,None,None,1,None),
- (2,0):amplifierglyph("N",None,None,1,None),
- (3,0):circleglyph(0,None,None,y,None),
- (4,0):amplifierglyph("V",None,None,None,1),
- (4,1):traceglyph("wave"),
- }),
- (0,0),["N","V"],down="wave")
- myprogram=grid({"F":110},
- {
- (0,0):amplifierglyph("F",None,None,None,1),
- (1,0):amplifierglyph(1/44100,1,None,None,None),
- (2,0):logglyph(2,1,None,None,None),
- (0,1):flowglyph(None,None,1,1),
- (1,1):flowglyph(None,None,1,1),
- (2,1):flowglyph(None,None,1,1),
- (3,1):flowglyph(None,None,1,1),
- (4,1):flowglyph(None,None,None,1),
- (0,2):harmonyglyph.new(1,1),
- (1,2):harmonyglyph.new(2,1/2),
- (2,2):harmonyglyph.new(3,1/3),
- (3,2):harmonyglyph.new(4,1/4),
- (4,2):harmonyglyph.new(5,1/5),
- (0,3):flowglyph(None,None,1,None),
- (1,3):flowglyph(None,None,1,None),
- (2,3):flowglyph(None,None,1,None),
- (3,3):flowglyph(None,None,1,None),
- (4,3):flowglyph(None,None,1,None),
- (5,3):traceglyph("wave")
- })
- import matplotlib.pyplot as plt
- o=[]
- for i in range(4410):
- myprogram.calculatepaths((1,0))
- #print myprogram.drawpath()
- myprogram.pipein((1,0),1)
- o.append(myprogram.glyphs[(5,3)].value)
- #myprogram.traces()
- #[myprogram.glyphs[x].value for x in myprogram.glyphs]
- myprogram.next()
- plt.plot(o)
- plt.show()"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement