-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio-comms-socket-3-server.py
74 lines (57 loc) · 2.11 KB
/
io-comms-socket-3-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Original Code from https://pyshine.com//How-to-send-audio-from-PyAudio-over-socket/
"""
######################
# RUN THIS IN YOUR IDE
######################
# Import libraries
import socket
import threading
import wave
import pyaudio
import pickle
import struct
# Declare all variables and constants
host_name = socket.gethostname()
host_ip = '192.168.1.102' # socket.gethostbyname(host_name)
print(host_ip)
port = 9611 # socket server port number MUST BE SAME AS SERVER
# Function that listens out for client and accepts audio stream
def audio_stream():
# Instantiate a Socket object
server_socket = socket.socket()
# Bind the host address and port together
# look closely. The bind() function takes tuple as argument
server_socket.bind((host_ip, (port - 1)))
# configure how many client the server can listen simultaneously
server_socket.listen(5)
# Stuff from pyaudio: set chunk size and open the file to stream
CHUNK = 1024
wf = wave.open("temp.wav", 'rb') # Change file path to access YOUR audio file. rb = read
# Instantiate a pyaudio object
p = pyaudio.PyAudio()
print('server listening at', (host_ip, (port - 1)))
# create a pyaudio stream (do this stuff first so not to break the connection)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
input=True,
frames_per_buffer=CHUNK)
# Accept the connection (handshake)
client_socket, addr = server_socket.accept()
data = None
# Endless loop
while True:
if client_socket:
# Stream the audio file a chunk at a time
while True:
data = wf.readframes(CHUNK)
# Pack into a pickle file
a = pickle.dumps(data)
# send chunk (as pickle) with length
message = struct.pack("Q", len(a)) + a
# Send it off
client_socket.sendall(message)
# Make a thread.
t1 = threading.Thread(target=audio_stream, args=())
t1.start()