Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import array
- import math
- import time
- from threading import Thread
- import pyaudio
- import applesms
- class Sine:
- def __init__(self, hz, amplitude=1, rate=44100):
- self.hz = hz
- self.rate = rate
- self.amp = amplitude
- def value(self, frame):
- return self.amp * math.sin(math.pi * 2 * self.hz * frame / self.rate)
- class Audio(Thread):
- def __init__(self):
- Thread.__init__(self)
- self.rate = 44100
- self.sounds = []
- self.stopped = False
- self.samples = 100
- self.frame = 0
- self.volume = 1.0
- self.tempo = 120
- self.p = pyaudio.PyAudio()
- self.stream = self.p.open(rate=self.rate,
- channels=1, format=pyaudio.paFloat32, output=True)
- def run(self):
- while not self.stopped:
- try:
- self.stream.write(array.array('f',
- (self._amp() for _ in range(self.samples))).tostring())
- except IOError:
- break
- def _amp(self): # amplitude
- self.frame += 1
- return sum([i.value(self.frame) for i in self.sounds]) * self.volume
- def add(self, instrument):
- self.sounds.append(instrument)
- def remove(self, index):
- del self.sounds[index]
- def stop(self):
- self.stopped = True
- self.stream.close()
- self.p.terminate()
- if __name__ == '__main__':
- a = Audio()
- sine = Sine(300)
- a.add(sine)
- a.start()
- try:
- while True:
- (x, y, z) = applesms.coords()
- sine.hz = 300 + z
- time.sleep(0.05)
- finally:
- a.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement