Created
January 18, 2016 15:49
-
-
Save mmmeri/68994a58b022f2217571 to your computer and use it in GitHub Desktop.
ZMQ audio stream sender
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 numpy as np | |
import zmq | |
context = zmq.Context() | |
sock = context.socket(zmq.SUB) | |
print "connect" | |
sock.setsockopt(zmq.SUBSCRIBE, '7578128') | |
sock.connect("tcp://127.0.0.1:5557") | |
while True: | |
envelope = sock.recv() | |
data = sock.recv() | |
y = np.fromstring(data, dtype=np.float32) | |
print y |
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 numpy as np | |
import pyaudio | |
import zmq | |
from PyQt4 import QtCore, QtGui | |
FS = 11025 # Hz | |
CHUNKSZ = 256 # samples | |
class MicrophoneRecorder(): | |
def __init__(self, sender): | |
self.sender = sender | |
self.p = pyaudio.PyAudio() | |
self.stream = self.p.open(format=pyaudio.paInt16, | |
channels=1, | |
rate=FS, | |
input=True, | |
frames_per_buffer=CHUNKSZ) | |
def read(self): | |
data = self.stream.read(CHUNKSZ) | |
self.sender.send(data) | |
def close(self): | |
self.stream.stop_stream() | |
self.stream.close() | |
self.p.terminate() | |
class Sender(): | |
def __init__(self): | |
self.context = zmq.Context() | |
self.socket = self.context.socket(zmq.PUB) | |
self.socket.bind("tcp://127.0.0.1:5557") | |
def send(self, buf): | |
topic = 0 | |
ret = self.socket.send("%d %s" % (topic, buf), zmq.NOBLOCK) | |
if __name__ == '__main__': | |
app = QtGui.QApplication([]) | |
sender = Sender() | |
mic = MicrophoneRecorder(sender) | |
while True: | |
mic.read() | |
app.exec_() | |
mic.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment