Skip to content

Instantly share code, notes, and snippets.

@mirfan899
Forked from fopina/mic_client.py
Created April 29, 2019 16:59
Show Gist options
  • Save mirfan899/4dec738636c711d378eac1793ec95329 to your computer and use it in GitHub Desktop.
Save mirfan899/4dec738636c711d378eac1793ec95329 to your computer and use it in GitHub Desktop.
microphone streaming with pyAudio
#!/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()
#!/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