Skip to content

Instantly share code, notes, and snippets.

@fopina
Created July 28, 2016 12:24
Show Gist options
  • Save fopina/3cefaed1b2d2d79984ad7894aef39a68 to your computer and use it in GitHub Desktop.
Save fopina/3cefaed1b2d2d79984ad7894aef39a68 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()
@KDTAAMAH
Copy link

KDTAAMAH commented Dec 4, 2019

can you explain how to code work
what does

def callback(in_data, frame_count, time_info, status):
    for s in read_list[1:]:
        s.send(in_data)
    return (None, pyaudio.paContinue)

work ?
and what is the benefits form line 32 to 45

@Dragon445
Copy link

Cool

@rakesh-krishna
Copy link

rakesh-krishna commented Oct 13, 2020

can you explain how to code work
what does

def callback(in_data, frame_count, time_info, status):
    for s in read_list[1:]:
        s.send(in_data)
    return (None, pyaudio.paContinue)

work ?
and what is the benefits form line 32 to 45

I think read_list is list of clients connected to the server
so basically they send the data to every client connected

@SpirosArk
Copy link

I run on Python3 and i get the error on client
python3 client.py Traceback (most recent call last): File "client.py", line 13, in <module> s.connect((sys.argv[0], int(sys.argv[1]))) IndexError: list index out of range
Does anyone know how to solve this?

@rakesh-krishna
Copy link

rakesh-krishna commented Apr 2, 2021

I run on Python3 and i get the error on client
python3 client.py Traceback (most recent call last): File "client.py", line 13, in <module> s.connect((sys.argv[0], int(sys.argv[1]))) IndexError: list index out of range
Does anyone know how to solve this?

I think you have not given any arguments when running the code

I think the arguments should be IP address and port number

Ex : python client.py 127.0.0.1 4444
Or try giving arguments other way around

@SpirosArk
Copy link

I run on Python3 and i get the error on client
python3 client.py Traceback (most recent call last): File "client.py", line 13, in <module> s.connect((sys.argv[0], int(sys.argv[1]))) IndexError: list index out of range
Does anyone know how to solve this?

I think you have not given any arguments when running the code

I think the arguments should be IP address and port number

Ex : python client.py 127.0.0.1 4444
Or try giving arguments other way around

Now i got the following error:

python3 client.py 127.0.0.1 4444 Traceback (most recent call last): File "client.py", line 13, in <module> s.connect((sys.argv[0], int(sys.argv[1]))) ValueError: invalid literal for int() with base 10: '127.0.0.1'

@rakesh-krishna
Copy link

I run on Python3 and i get the error on client
python3 client.py Traceback (most recent call last): File "client.py", line 13, in <module> s.connect((sys.argv[0], int(sys.argv[1]))) IndexError: list index out of range
Does anyone know how to solve this?

I think you have not given any arguments when running the code
I think the arguments should be IP address and port number
Ex : python client.py 127.0.0.1 4444
Or try giving arguments other way around

Now i got the following error:

python3 client.py 127.0.0.1 4444 Traceback (most recent call last): File "client.py", line 13, in <module> s.connect((sys.argv[0], int(sys.argv[1]))) ValueError: invalid literal for int() with base 10: '127.0.0.1'

if you have made any changes to the code on line 13 , try comparing your code and this gist
I can say for sure the code in gist works .

@SpirosArk
Copy link

Oh yeah you are right, it is my fault. I accidentally had changed an int value from 0 to 1. Thank you very much for your time!

@Jayachandra-M
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment