Guest User

Untitled

a guest
Nov 4th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #import pygame
  2. from math import pi,sin
  3. from random import random
  4. tau=2*pi
  5. saw=lambda x:x%tau/tau
  6. tri=lambda x:abs(((x+pi)%tau)-pi)/pi
  7. samplerate=30.0
  8. wavesize=4.0
  9. nf=lambda n:map(lambda x:(0,tri(x/wavesize*pi*2)),range(int(wavesize)))*n
  10. sf=lambda s:map(lambda x:(0 if s =="=" else 1 if s=="<" else 2 if s==">" else None,(sin(x/wavesize/2.0*pi*2-pi/2)+1)/3),range(int(wavesize*2)))
  11. def makesignal(hz,exp):
  12. o=[]
  13. for i in exp:
  14. if isinstance(i,INT):
  15. o+=nf(i.n)
  16. if isinstance(i,symbol):
  17. o+=sf(i.sym)
  18. return o
  19. class INT:
  20. def __init__(self,n):
  21. self.n=n
  22. class symbol:
  23. def __init__(self,sym):
  24. self.sym=sym
  25. class comparison:
  26. def __init__(self,symbol,a,b):
  27. self.symbol=symbol
  28. self.a=a
  29. self.b=b
  30. def out(self):
  31. return [INT(self.a),symbol(self.symbol),INT(self.b)]
  32.  
  33. def generatecomparison(low,high):
  34. a=int(random()*(high-low))-low
  35. b=int(random()*(high-low))-low
  36. s="=" if a==b else ("<" if a<b else ">")
  37. return comparison(s,a,b)
  38.  
  39.  
  40. r=44100
  41. chirp1=lambda x:tri(x*tau)*sin(x*440*tau)
  42. chirp2=lambda x:tri(x*pi)*sin(x*880*tau)
  43. chirp3=lambda x:saw(x*tau)*sin(x*2**(x+1)*440*tau)
  44. chirp4=lambda x:saw(x*tau)*sin(x*2**(2-x)*440*tau)
  45. chirp5=lambda x:abs(sin(x*tau))*tri(x*220*tau)
  46. from PIL import Image,ImageDraw
  47. im=Image.new("RGB",(r,64),(0,0,0))
  48. dr=ImageDraw.Draw(im)
  49.  
  50. def graph(l,r):
  51. m=len(l)/r
  52. for i in range(r):
  53. dr.line((i,l[i*m]*64+32,i+1,l[i*m+1]*64+32))
  54.  
  55. def makeaudiosignal(hz,exp,ss,ps):
  56. o=[]
  57. for i in exp:
  58. if isinstance(i,INT):
  59. o+=[chirp1(x/(hz*ss)) for x in range(int(hz*ss))]*i.n
  60. if isinstance(i,symbol):
  61. if i.sym=="=":
  62. o+=[chirp2(x/(hz*ss)) for x in range(int(hz*ss))]
  63. if i.sym=="<":
  64. o+=[chirp3(x/(hz*ss)) for x in range(int(hz*ss))]
  65. if i.sym==">":
  66. o+=[chirp4(x/(hz*ss)) for x in range(int(hz*ss))]
  67. o+=[chirp5(x/(hz*ss)) for x in range(int(hz*ss))]
  68. return o
  69.  
  70. def audioloop(numbers):
  71. hz=44100
  72. signalsize=.4
  73. pause=.2
  74. o=[]
  75. for i in range(numbers):
  76. s=makeaudiosignal(hz,generatecomparison(1,12).out(),signalsize,pause)
  77. o+=s
  78. return o
  79. import wave
  80. import struct
  81. noise_output = wave.open('noise2.wav', 'w')
  82. noise_output.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))
  83.  
  84. values = []
  85. a=audioloop(64)
  86. for i in a:
  87. packed_value = struct.pack('h', int(i*32767))
  88. values.append(packed_value)
  89. values.append(packed_value)
  90. value_str = ''.join(values)
  91. noise_output.writeframes(value_str)
  92. noise_output.close()
  93.  
  94.  
  95. def visualloop():
  96. # Set the height and width of the screen
  97. size = [640, 480]
  98. screen = pygame.display.set_mode(size)
  99. #Loop until the user clicks the close button.
  100. clock = pygame.time.Clock()
  101. j=True
  102. while j:
  103. s=makesignal(samplerate,generatecomparison(1,12).out())
  104. for i in s:
  105. clock.tick(samplerate)
  106. for event in pygame.event.get(): # User did something
  107. if event.type == pygame.QUIT: # If user clicked close
  108. done=True # Flag that we are done so we exit this loop
  109. col=[0,0,0]
  110. col[i[0]]=int(i[1]*255)
  111. screen.fill(tuple(col))
  112. pygame.display.flip()
  113. #termsignal
  114. for i in range(int(wavesize)):
  115. clock.tick(samplerate)
  116. screen.fill(tuple([i*128/wavesize+64]*3))
  117. pygame.display.flip()
  118. # Be IDLE friendly
  119. pygame.quit()
  120. #visualloop()
Advertisement
Add Comment
Please, Sign In to add comment