Created
July 11, 2017 20:43
-
-
Save dariocazzani/6142dee188a5eabeec2a508834191080 to your computer and use it in GitHub Desktop.
Stream Microphone via UDP
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
''' | |
Simple udp socket server to stream audio from mic | |
''' | |
import pyaudio | |
import socket | |
import sys | |
class UDPstream(object): | |
def __init__(self, CLIENT_IP, PORT): | |
self.client_addr = (CLIENT_IP, PORT) | |
try: | |
p = pyaudio.PyAudio() | |
self.stream = p.open(format=pyaudio.paFloat32, | |
channels=1, | |
rate=16000, | |
input=True, | |
frames_per_buffer=1024) | |
except Exception as e: | |
print 'Could not create stream, error was %s' %e | |
sys.exit(1) | |
try : | |
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
print 'Socket created' | |
except socket.error, msg : | |
print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] | |
sys.exit() | |
HOST = '' # Symbolic name meaning all available interfaces | |
# Bind socket to local host and port | |
try: | |
self.s.bind((HOST, PORT)) | |
except socket.error , msg: | |
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] | |
sys.exit(1) | |
print 'Socket bind complete' | |
def send_stream(self): | |
while 1: | |
try: | |
self.s.sendto(self.stream.read(1024), self.client_addr) | |
print 'Sending data...' | |
except KeyboardInterrupt: | |
print '^C received, shutting down server...' | |
self.s.close() | |
sys.exit(2) | |
if __name__ == '__main__': | |
server = UDPstream('0.0.0.0', 9999) | |
server.send_stream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment