Skip to content

Instantly share code, notes, and snippets.

@esehara
Created October 22, 2011 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esehara/1305959 to your computer and use it in GitHub Desktop.
Save esehara/1305959 to your computer and use it in GitHub Desktop.
Pynoise
# -*- coding:utf-8 -*-
import threading
import time
import pyaudio
import math
import array
import random
class System(threading.Thread):
def __init__(self,channels,pine = 3.14):
threading.Thread.__init__(self)
self.setDaemon(True)
self.tempo = 0.5
self.sounds = []
self.que = []
self.channels = channels
self.pine = pine
p = pyaudio.PyAudio()
self.stream = p.open(rate=44100,channels=self.channels,format=pyaudio.paFloat32,output=True)
def run(self):
while 1:
if len(self.sounds) == 0:
continue
if len(self.sounds) == 1:
self.play(self.sounds[0])
continue
if len(self.sounds) > 1:
for sound in self.sounds:
self.play(sound)
def soundset(self,x,y = 1):
self.sounds.append([y,x])
def play(self,sound):
self.stream.write(self.tone(sound[0],sound[1],sec=self.tempo))
def tone(self,val,freq,sec=1,velocity=.2,rate=44100):
w = rate / freq
def gen():
if val == 0:
time.sleep(freq * self.tempo)
for i in xrange(int(rate * sec)):
if val == 1 : yield math.sin(float(i % w) / w * 10. * self.pine) * velocity
if val == 2 : yield math.tan(float(i % w) / w * 10. * self.pine) * velocity
if val == 3 : yield math.log(float(i % w) / w * 10. * self.pine + 0.1) * velocity
if val == 4 : yield random.uniform(0,float(i % w) /w * 10. * self.pine) * velocity
return array.array('f',gen()).tostring()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment