Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import pygame
- from math import pi,sin
- from random import random
- tau=2*pi
- saw=lambda x:x%tau/tau
- tri=lambda x:abs(((x+pi)%tau)-pi)/pi
- samplerate=30.0
- wavesize=4.0
- nf=lambda n:map(lambda x:(0,tri(x/wavesize*pi*2)),range(int(wavesize)))*n
- 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)))
- def makesignal(hz,exp):
- o=[]
- for i in exp:
- if isinstance(i,INT):
- o+=nf(i.n)
- if isinstance(i,symbol):
- o+=sf(i.sym)
- return o
- class INT:
- def __init__(self,n):
- self.n=n
- class symbol:
- def __init__(self,sym):
- self.sym=sym
- class comparison:
- def __init__(self,symbol,a,b):
- self.symbol=symbol
- self.a=a
- self.b=b
- def out(self):
- return [INT(self.a),symbol(self.symbol),INT(self.b)]
- def generatecomparison(low,high):
- a=int(random()*(high-low))-low
- b=int(random()*(high-low))-low
- s="=" if a==b else ("<" if a<b else ">")
- return comparison(s,a,b)
- r=44100
- chirp1=lambda x:tri(x*tau)*sin(x*440*tau)
- chirp2=lambda x:tri(x*pi)*sin(x*880*tau)
- chirp3=lambda x:saw(x*tau)*sin(x*2**(x+1)*440*tau)
- chirp4=lambda x:saw(x*tau)*sin(x*2**(2-x)*440*tau)
- chirp5=lambda x:abs(sin(x*tau))*tri(x*220*tau)
- from PIL import Image,ImageDraw
- im=Image.new("RGB",(r,64),(0,0,0))
- dr=ImageDraw.Draw(im)
- def graph(l,r):
- m=len(l)/r
- for i in range(r):
- dr.line((i,l[i*m]*64+32,i+1,l[i*m+1]*64+32))
- def makeaudiosignal(hz,exp,ss,ps):
- o=[]
- for i in exp:
- if isinstance(i,INT):
- o+=[chirp1(x/(hz*ss)) for x in range(int(hz*ss))]*i.n
- if isinstance(i,symbol):
- if i.sym=="=":
- o+=[chirp2(x/(hz*ss)) for x in range(int(hz*ss))]
- if i.sym=="<":
- o+=[chirp3(x/(hz*ss)) for x in range(int(hz*ss))]
- if i.sym==">":
- o+=[chirp4(x/(hz*ss)) for x in range(int(hz*ss))]
- o+=[chirp5(x/(hz*ss)) for x in range(int(hz*ss))]
- return o
- def audioloop(numbers):
- hz=44100
- signalsize=.4
- pause=.2
- o=[]
- for i in range(numbers):
- s=makeaudiosignal(hz,generatecomparison(1,12).out(),signalsize,pause)
- o+=s
- return o
- import wave
- import struct
- noise_output = wave.open('noise2.wav', 'w')
- noise_output.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))
- values = []
- a=audioloop(64)
- for i in a:
- packed_value = struct.pack('h', int(i*32767))
- values.append(packed_value)
- values.append(packed_value)
- value_str = ''.join(values)
- noise_output.writeframes(value_str)
- noise_output.close()
- def visualloop():
- # Set the height and width of the screen
- size = [640, 480]
- screen = pygame.display.set_mode(size)
- #Loop until the user clicks the close button.
- clock = pygame.time.Clock()
- j=True
- while j:
- s=makesignal(samplerate,generatecomparison(1,12).out())
- for i in s:
- clock.tick(samplerate)
- for event in pygame.event.get(): # User did something
- if event.type == pygame.QUIT: # If user clicked close
- done=True # Flag that we are done so we exit this loop
- col=[0,0,0]
- col[i[0]]=int(i[1]*255)
- screen.fill(tuple(col))
- pygame.display.flip()
- #termsignal
- for i in range(int(wavesize)):
- clock.tick(samplerate)
- screen.fill(tuple([i*128/wavesize+64]*3))
- pygame.display.flip()
- # Be IDLE friendly
- pygame.quit()
- #visualloop()
Advertisement
Add Comment
Please, Sign In to add comment