Skip to content

Instantly share code, notes, and snippets.

@Sketchwhale
Created April 20, 2017 20:40
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 Sketchwhale/90e3f821fecce5cdfaaf3e84e5bc2970 to your computer and use it in GitHub Desktop.
Save Sketchwhale/90e3f821fecce5cdfaaf3e84e5bc2970 to your computer and use it in GitHub Desktop.
import socket, pyaudio, sys, wave, signal
#reset something about signals in unix systems... or something
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
# network setup
host = socket.gethostname()
port = 12345
chunk = 4096
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
# instantiate PyAudio
p = pyaudio.PyAudio()
# open stream
stream = p.open(format = p.get_format_from_width(2),
channels = 2,
rate = 41100,
output = True)
buffer = client_socket.recv(4096)
while len(buffer) > 0:
chunk = client_socket.recv(4096)
stream.write(chunk)
if not chunk:
# if we can't receive any data, then the socket has died
break
buffer += chunk
# stop stream
stream.stop_stream()
stream.close()
# close pyaudio
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment