Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.04 KB | None | 0 0
  1. from __future__ import division
  2. from math import sin,cos,tan,atan2,hypot,pi
  3. from copy import deepcopy
  4. x="x"
  5. y="y"
  6. azimuth="azimuth"
  7. hypot="hypot"
  8. pi=3.141592653
  9.  
  10. class flowglyph:
  11. def __init__(self,left=None,up=None,right=None,down=None):
  12. self.left=left;self.up=up;self.right=right;self.down=down;
  13. self.value=0
  14. self.inpath=0
  15.  
  16. class addglyph:
  17. def __init__(self,add,left=None,up=None,right=None,down=None):
  18. self.left=left;self.up=up;self.right=right;self.down=down;
  19. self.value=0
  20. self.add=add
  21. self.inpath=0
  22. class limiterglyph:
  23. def __init__(self,upper=1,lower=-1,left=None,up=None,right=None,down=None):
  24. self.left=left;self.up=up;self.right=right;self.down=down;
  25. self.value=0
  26. self.upper=upper
  27. self.lower=lower
  28. self.inpath=0
  29. class gtglyph:
  30. def __init__(self,gt,left=None,up=None,right=None,down=None):
  31. self.left=left;self.up=up;self.right=right;self.down=down;
  32. self.value=0
  33. self.gt=gt
  34. self.inpath=0
  35. class ltglyph:
  36. def __init__(self,lt,left=None,up=None,right=None,down=None):
  37. self.left=left;self.up=up;self.right=right;self.down=down;
  38. self.value=0
  39. self.lt=lt
  40. self.inpath=0
  41. class amplifierglyph:
  42. def __init__(self,magnitude,left=None,up=None,right=None,down=None):
  43. self.left=left;self.up=up;self.right=right;self.down=down;
  44. self.magnitude=magnitude
  45. self.value=0
  46. self.inpath=0
  47. class multiplierglyph:
  48. def __init__(self,left=None,up=None,right=None,down=None):
  49. self.left=left;self.up=up;self.right=right;self.down=down;
  50. self.value=0
  51. self.inpath=0
  52. class logglyph:
  53. def __init__(self,base,left=None,up=None,right=None,down=None):
  54. self.left=left;self.up=up;self.right=right;self.down=down;
  55. self.base=base
  56. self.value=0
  57. self.inpath=0
  58. class traceglyph:
  59. def __init__(self,varname,left=None,up=None,right=None,down=None):
  60. self.varname=varname
  61. self.value=0
  62. self.inpath=0
  63. class circleglyph:
  64. def __init__(self,startangle=0,left=None,up=None,right=None,down=None):
  65. self.left=left;self.up=up;self.right=right;self.down=down;
  66. self.value=startangle
  67. self.inpath=0
  68. def setvals(self):
  69. self.value=self.value%(2*pi)
  70. self.y=sin(self.value)
  71. self.x=cos(self.value)
  72. #self.hypot=hypot(self.x,self.y)
  73. class capacitorglyph:
  74. def __init__(self,capacity,threshold,dischargepercent=100,left=None,up=None,right=None,down=None):
  75. self.left=left;self.up=up;self.right=right;self.down=down;
  76. self.capacity=capacity
  77. self.threshold=threshold
  78. self.dischargepercent=dischargepercent
  79. self.value=0
  80. self.inpath=0
  81.  
  82.  
  83. def isnum(obj):
  84. return isinstance(obj,float) or isinstance(obj,int)
  85.  
  86.  
  87. class grid:
  88. def __init__(self,constants={},glyphs={}): #glyph {(x,y):glyph..}
  89. self.constants=constants
  90. self.glyphs=glyphs
  91. def pipein(self,pos,value):
  92. for i in self.glyphs:
  93. self.glyphs[i].inputs=0
  94. self.glyphs[pos].value+=value
  95. self.eminatefrom(pos)
  96. def makewave(self,inpos,inputwave,tracepoint):
  97. o=[]
  98. for i in inputwave:
  99. self.calculatepaths(inpos)
  100. self.pipein(inpos,i)
  101. o.append(self.traces()[tracepoint])
  102. self.next()
  103. return o
  104. def exportsound(self,wav):
  105. from struct import pack
  106. import wave
  107. out=wave.open("out.wav","wb")
  108. out.setparams((1,4,44100,1,"NONE","NONE"))
  109. for i in wav:
  110. out.writeframes(pack("l",(2**31-1)*i))
  111.  
  112. def drawpath(self):
  113. s=""
  114. for j in range(32):
  115. for i in range(32):
  116. s+= str(self.glyphs[(i,j)].inpath) if (i,j) in self.glyphs else " "
  117. s+="\n"
  118. return s
  119. def traces(self):
  120. o={}
  121. for i in self.glyphs:
  122. if isinstance(self.glyphs[i],traceglyph):
  123. o[self.glyphs[i].varname]=deepcopy(self.glyphs[i].value)
  124. return o
  125. def next(self):
  126. for i in self.glyphs:
  127. g=self.glyphs[i]
  128. 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):
  129. self.glyphs[i].value=0
  130. def calculatepaths(self,pos,first=True):
  131. if first:
  132. for i in self.glyphs:
  133. self.glyphs[i].inpath=0
  134. currentglyph=self.glyphs[pos]
  135. if isinstance(currentglyph,traceglyph):
  136. currentglyph.inpath+=1
  137. else:
  138. if currentglyph.inpath>0:
  139. currentglyph.inpath+=1
  140. else:
  141. currentglyph.inpath+=1
  142. direction=[i!=None for i in [currentglyph.left,currentglyph.up,currentglyph.right,currentglyph.down]]
  143. if direction[0]:#left
  144. self.calculatepaths((pos[0]-1,pos[1]),False)
  145. if direction[1]:#up
  146. self.calculatepaths((pos[0],pos[1]-1),False)
  147. if direction[2]:#right
  148. self.calculatepaths((pos[0]+1,pos[1]),False)
  149. if direction[3]:#down
  150. self.calculatepaths((pos[0],pos[1]+1),False)
  151. def eminatedirection(self,val,dval,np,currentglyph):
  152. if np in self.glyphs:
  153. nextglyph=self.glyphs[np]
  154. if isnum(dval):
  155. if isinstance(nextglyph,multiplierglyph):
  156. if nextglyph.inputs==0:
  157. nextglyph.value=dval*val
  158. else:
  159. nextglyph.value*=dval*val
  160. else:
  161. nextglyph.value+=dval*val
  162. nextglyph.inputs+=1
  163. if isinstance(nextglyph,circleglyph):
  164. nextglyph.setvals()
  165. if nextglyph.inputs==nextglyph.inpath:
  166. self.eminatefrom(np)
  167. else:
  168. if isinstance(currentglyph,abstractglyph) and dval !=None:
  169. nextglyph.value+=currentglyph.traces[dval]
  170. if dval==x:
  171. nextval=currentglyph.x
  172. if dval==y:
  173. nextval=currentglyph.y
  174. if dval==azimuth:
  175. nextval=val
  176. if dval!=None:
  177. if isinstance(nextglyph,multiplierglyph):
  178. if nextglyph.inputs==0:
  179. nextglyph.value=nextval
  180. else:
  181. nextglyph.value*=nextval
  182. else:
  183. nextglyph.value+=nextval
  184. nextglyph.inputs+=1
  185. if isinstance(nextglyph,circleglyph):
  186. nextglyph.setvals()
  187. if nextglyph.inputs==nextglyph.inpath:
  188. self.eminatefrom(np)
  189.  
  190. def eminatefrom(self,pos):
  191. currentglyph=self.glyphs[pos]
  192. if isinstance(currentglyph,abstractglyph):
  193. currentglyph.run()
  194. if isinstance(currentglyph,traceglyph):
  195. pass
  196. else:
  197. if isinstance(currentglyph,capacitorglyph):
  198. if currentglyph.value>currentglyph.capacity:
  199. currentglyph.value=currentglyph.capacity
  200. val=deepcopy(currentglyph.value)
  201. if isinstance(currentglyph,amplifierglyph):
  202. mag=currentglyph.magnitude
  203. if isinstance(mag,str):
  204. mag=self.constants[mag]
  205. val*=mag
  206. if isinstance(currentglyph,addglyph):
  207. add=currentglyph.add
  208. if isinstance(add,str):
  209. add=self.constants[add]
  210. val+=add
  211. if isinstance(currentglyph,logglyph):
  212. base=currentglyph.base
  213. if isinstance(base,str):
  214. base=self.constants[base]
  215. val=base**val
  216. if isinstance(currentglyph,capacitorglyph):
  217. threshold=currentglyph.threshold
  218. dcp=currentglyph.dischargepercent
  219. if isinstance(threshold,str):
  220. threshold=self.constants[threshold]
  221. if isinstance(dcp,str):
  222. dcp=self.constants[dcp]
  223. dcr=dcp/100
  224. if val>=threshold or val<=-threshold:
  225. fired=val*dcp/100
  226. val*=dcp/100
  227. else:
  228. val=0
  229. fired=False
  230. #left
  231. leftval=currentglyph.left
  232. np=(pos[0]-1,pos[1])
  233. self.eminatedirection(val,leftval,np,currentglyph)
  234. #up
  235. upval=currentglyph.up
  236. np=(pos[0],pos[1]-1)
  237. self.eminatedirection(val,upval,np,currentglyph)
  238. #right
  239. rightval=currentglyph.right
  240. np=(pos[0]+1,pos[1])
  241. self.eminatedirection(val,rightval,np,currentglyph)
  242. #down
  243. downval=currentglyph.down
  244. np=(pos[0],pos[1]+1)
  245. self.eminatedirection(val,downval,np,currentglyph)
  246. if isinstance(currentglyph,capacitorglyph) and fired!=False:
  247. currentglyph.value-=fired
  248.  
  249. class abstractglyph:
  250. def __init__(self,grid,pipein,variables=["A"],left=None,up=None,right=None,down=None):
  251. self.grid=grid
  252. self.pipein=pipein
  253. self.variables=variables
  254. self.left=left
  255. self.up=up
  256. self.right=right
  257. self.down=down
  258. self.value=0
  259. def new(self,*variables):
  260. d={}
  261. for i in range(len(self.variables)):
  262. d[self.variables[i]]=variables[i]
  263. self.program=deepcopy(self.grid)
  264. self.program.constants=d
  265. return deepcopy(self)
  266. def run(self):
  267. self.program.calculatepaths(self.pipein)
  268. self.program.pipein(self.pipein,self.value)
  269. self.value=0
  270. self.traces=self.program.traces()
  271. self.program.next()
  272.  
  273.  
  274. ##Subprocess
  275. """
  276. harmonyglyph=abstractglyph(grid({},
  277. {
  278. (0,0):flowglyph(None,None,1,None),
  279. (1,0):amplifierglyph(pi,None,None,1,None),
  280. (2,0):amplifierglyph("N",None,None,1,None),
  281. (3,0):circleglyph(0,None,None,y,None),
  282. (4,0):amplifierglyph("V",None,None,None,1),
  283. (4,1):traceglyph("wave"),
  284. }),
  285. (0,0),["N","V"],down="wave")
  286.  
  287. myprogram=grid({"F":110},
  288. {
  289. (0,0):amplifierglyph("F",None,None,None,1),
  290. (1,0):amplifierglyph(1/44100,1,None,None,None),
  291. (2,0):logglyph(2,1,None,None,None),
  292. (0,1):flowglyph(None,None,1,1),
  293. (1,1):flowglyph(None,None,1,1),
  294. (2,1):flowglyph(None,None,1,1),
  295. (3,1):flowglyph(None,None,1,1),
  296. (4,1):flowglyph(None,None,None,1),
  297. (0,2):harmonyglyph.new(1,1),
  298. (1,2):harmonyglyph.new(2,1/2),
  299. (2,2):harmonyglyph.new(3,1/3),
  300. (3,2):harmonyglyph.new(4,1/4),
  301. (4,2):harmonyglyph.new(5,1/5),
  302. (0,3):flowglyph(None,None,1,None),
  303. (1,3):flowglyph(None,None,1,None),
  304. (2,3):flowglyph(None,None,1,None),
  305. (3,3):flowglyph(None,None,1,None),
  306. (4,3):flowglyph(None,None,1,None),
  307. (5,3):traceglyph("wave")
  308.  
  309. })
  310.  
  311.  
  312. import matplotlib.pyplot as plt
  313. o=[]
  314. for i in range(4410):
  315. myprogram.calculatepaths((1,0))
  316. #print myprogram.drawpath()
  317. myprogram.pipein((1,0),1)
  318. o.append(myprogram.glyphs[(5,3)].value)
  319. #myprogram.traces()
  320. #[myprogram.glyphs[x].value for x in myprogram.glyphs]
  321. myprogram.next()
  322.  
  323. plt.plot(o)
  324. plt.show()"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement