Created
March 1, 2019 02:52
-
-
Save charlesalexanderlee/fd126af38edd0e83642e36b8fd2f7039 to your computer and use it in GitHub Desktop.
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
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 = 2 | |
RATE = 44100 | |
stream = p.open(format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
output=True, | |
frames_per_buffer=CHUNK) | |
with socket.socket() as client_socket: | |
client_socket.connect((HOST, PORT)) | |
print(client_socket.recv(2048).decode('utf-8')) | |
while True: | |
data = client_socket.recv(CHUNK) | |
stream.write(data) | |
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
import socket | |
import pyaudio | |
import threading | |
# 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 = 2 | |
RATE = 44100 | |
stream = p.open(format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=CHUNK) | |
def client_listener(host_socket): | |
while True: | |
try: | |
connection, address = host_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')) | |
pass | |
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, args=(server_socket,)) | |
client_listener_thread.start() | |
print('Listening for clients...') | |
# Records and sends microphone input to connected clients (the main thread) | |
while True: | |
microphone_input = stream.read(CHUNK) | |
for client in CONNECTED_CLIENTS: | |
client.send(microphone_input) | |
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