Last active
November 30, 2016 17:07
-
-
Save nomelif/e04f3c348e0745711c7a938a961fb320 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import numpy as np | |
import time | |
class audioServer(): | |
channel = None | |
def __init__(self): | |
pygame.mixer.init(41000, -16, 1, 1024) | |
self.channel = pygame.mixer.Channel(0) | |
def needsAudio(self): | |
if self.channel.get_queue() == None: | |
return True | |
else: | |
return False | |
def playChunk(self, inputData): | |
snd = pygame.sndarray.make_sound(np.int16(inputData*(2**15))) | |
self.channel.queue(snd) | |
def generateSine(t): | |
return np.sin((np.arange(1024)/41000 + t)*2*np.pi*400) | |
s = audioServer() | |
t = 0 | |
while True: | |
if s.needsAudio(): | |
s.playChunk(generateSine(t)) | |
t = t + 1024/41000 | |
time.sleep(0.01) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment