Created
July 28, 2016 12:24
-
-
Save fopina/3cefaed1b2d2d79984ad7894aef39a68 to your computer and use it in GitHub Desktop.
microphone streaming with pyAudio
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
#!/usr/bin/env python | |
import pyaudio | |
import socket | |
import sys | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 1 | |
RATE = 44100 | |
CHUNK = 4096 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((sys.argv[1], int(sys.argv[2]))) | |
audio = pyaudio.PyAudio() | |
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=CHUNK) | |
try: | |
while True: | |
data = s.recv(CHUNK) | |
stream.write(data) | |
except KeyboardInterrupt: | |
pass | |
print('Shutting down') | |
s.close() | |
stream.close() | |
audio.terminate() |
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
#!/usr/bin/env python | |
import pyaudio | |
import socket | |
import select | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 1 | |
RATE = 44100 | |
CHUNK = 4096 | |
audio = pyaudio.PyAudio() | |
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
serversocket.bind(('', 4444)) | |
serversocket.listen(5) | |
def callback(in_data, frame_count, time_info, status): | |
for s in read_list[1:]: | |
s.send(in_data) | |
return (None, pyaudio.paContinue) | |
# start Recording | |
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK, stream_callback=callback) | |
# stream.start_stream() | |
read_list = [serversocket] | |
print "recording..." | |
try: | |
while True: | |
readable, writable, errored = select.select(read_list, [], []) | |
for s in readable: | |
if s is serversocket: | |
(clientsocket, address) = serversocket.accept() | |
read_list.append(clientsocket) | |
print "Connection from", address | |
else: | |
data = s.recv(1024) | |
if not data: | |
read_list.remove(s) | |
except KeyboardInterrupt: | |
pass | |
print "finished recording" | |
serversocket.close() | |
# stop Recording | |
stream.stop_stream() | |
stream.close() | |
audio.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can some one say how to stop listening from serverside with a keyboard interrupt like
if keyboard.is_pressed("q"):
stream.close()
serversocket.close()
audio.terminate()
I know these steps but I don't know where to insert in your code,so kindly can you give me a solution