Skip to content

Instantly share code, notes, and snippets.

@charlesalexanderlee
Created September 26, 2019 17:52
Show Gist options
  • Save charlesalexanderlee/c007dad31f7991c97f13f2ecc5541e9e to your computer and use it in GitHub Desktop.
Save charlesalexanderlee/c007dad31f7991c97f13f2ecc5541e9e to your computer and use it in GitHub Desktop.
import socket
import pyaudio
# Socket
HOST = socket.gethostbyname(socket.gethostname())
# Use this instead if connecting from another computer
# HOST = 'enter server IPV4 here'
PORT = 5000
# Audio
p = pyaudio.PyAudio()
CHUNK = 1024 * 2
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
output=True,
frames_per_buffer=CHUNK)
with socket.socket() as client_socket:
try:
client_socket.connect((HOST, PORT))
print(client_socket.recv(2048).decode('utf-8'))
while True:
data = client_socket.recv(CHUNK)
stream.write(data)
except KeyboardInterrupt:
print(': Closing socket...')
import socket
import pyaudio
import threading
# To do:
# [done] Handle client disconnect
# - Implement specific client voice communication
# - Identify clients by room number
# - Record microphone input only if broadcasting voice
# Socket
HOST = socket.gethostbyname(socket.gethostname())
PORT = 5000
SERVER_ADDRESS = HOST + ':' + str(PORT)
CONNECTED_CLIENTS = []
# Audio
p = pyaudio.PyAudio()
CHUNK = 1024 * 2
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
def client_listener():
while True:
try:
connection, address = server_socket.accept()
handle_client_thread = threading.Thread(target=handle_client, args=(connection, address))
handle_client_thread.start()
CONNECTED_CLIENTS.append(connection)
print(CONNECTED_CLIENTS)
print('Connection from ' + address[0] + ':' + str(address[1]))
except KeyboardInterrupt:
pass
def handle_client(client, client_address):
client.send(f'Connected to {SERVER_ADDRESS}'.encode('utf-8'))
with socket.socket() as server_socket:
try:
server_socket.bind((HOST, PORT))
server_socket.listen()
print('[Server hosted at ' + SERVER_ADDRESS + ']')
# Creates a separate thread for listening for clients
client_listener_thread = threading.Thread(target=client_listener)
client_listener_thread.start()
print('Listening for clients...')
# Records and sends microphone input to connected clients (the main thread)
while True:
try:
microphone_input = stream.read(CHUNK)
for client in CONNECTED_CLIENTS:
client.send(microphone_input)
# Handles client disconnect
except ConnectionResetError:
print(client.getpeername()[0] + ' Disconnected')
CONNECTED_CLIENTS.remove(client)
print(CONNECTED_CLIENTS)
except KeyboardInterrupt:
for client in CONNECTED_CLIENTS:
client.close()
break
except socket.error as error:
print(str(error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment