|
1 |
| -import queue |
2 |
| -import socket |
3 |
| -import struct |
4 |
| -import sys |
5 |
| -import time |
6 |
| -import threading |
7 |
| - |
8 |
| -import simpleaudio as sa |
9 |
| -import numpy as np |
10 |
| - |
11 |
| - |
12 |
| -SAMPLE_RATE = 48000 |
13 |
| -BYTES_PER_FRAME = 8192 |
14 |
| -FRAMES_PER_SECOND = 48 |
15 |
| -BYTES_PER_SECOND = BYTES_PER_FRAME * FRAMES_PER_SECOND |
16 |
| - |
17 |
| -# Need to open the incoming port in your firewall first. |
18 |
| -HOST = '169.254.103.120' |
19 |
| -PORT = 11009 |
20 |
| - |
21 |
| -def server_thread(q): |
22 |
| - # create TCP socket |
23 |
| - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
24 |
| - s.bind((HOST, PORT)) |
25 |
| - s.settimeout(120) |
26 |
| - |
27 |
| - print("Listening to socket...") |
28 |
| - s.listen() |
29 |
| - try: |
30 |
| - print("Waiting for connection") |
31 |
| - conn, addr = s.accept() |
32 |
| - except: |
33 |
| - print("Timed out waiting for connection") |
34 |
| - return |
35 |
| - |
36 |
| - print("Connected!!") |
37 |
| - |
38 |
| - conn.settimeout(60) |
39 |
| - |
40 |
| - frames_recvd = 0 |
41 |
| - prev_time = -1 |
42 |
| - sound_data = bytearray() |
43 |
| - |
44 |
| - while True: |
45 |
| - # wait for a message |
46 |
| - try: |
47 |
| - data = conn.recv(8) |
48 |
| - except: |
49 |
| - break |
50 |
| - |
51 |
| - if data[0:4] != b'\x1a\xcf\xfc\x1d': |
52 |
| - print("Invalid sync pattern", data[0:4]) |
53 |
| - print(data[0:4].decode()) |
54 |
| - break |
55 |
| - |
56 |
| - frames_recvd += 1 |
57 |
| - if prev_time == -1: |
58 |
| - prev_time = time.time() |
59 |
| - elif (time.time() - prev_time > 1): |
60 |
| - print("Samples rcvd", frames_recvd) |
61 |
| - frames_recvd = 0 |
62 |
| - prev_time = time.time() |
63 |
| - |
64 |
| - total_message_length = list(bytes(data[4:8])) |
65 |
| - total_message_length = ((total_message_length[0] << 24) | |
66 |
| - (total_message_length[1] << 16) | |
67 |
| - (total_message_length[2] << 8) | |
68 |
| - (total_message_length[3] << 0)) |
69 |
| - |
70 |
| - # read the rest of the message from the socket using the given length |
71 |
| - audio_data = bytearray() |
72 |
| - bytes_read = 0 |
73 |
| - default_read_size = 8192 |
74 |
| - while (bytes_read != total_message_length): |
75 |
| - bytes_remaining = total_message_length - bytes_read |
76 |
| - |
77 |
| - if default_read_size > bytes_remaining: |
78 |
| - read_size = bytes_remaining |
79 |
| - elif default_read_size > total_message_length: |
80 |
| - read_size = total_message_length |
81 |
| - else: |
82 |
| - read_size = default_read_size |
83 |
| - |
84 |
| - message = conn.recv(read_size) |
85 |
| - bytes_read += len(message) |
86 |
| - audio_data.extend(message) |
87 |
| - |
88 |
| - q.put(audio_data) |
89 |
| - |
90 |
| - |
91 |
| -def play_audio_thread(q): |
92 |
| - audio_stream = bytearray() |
93 |
| - while True: |
94 |
| - try: |
95 |
| - audio = q.get(timeout=60) |
96 |
| - audio_stream.extend(audio) |
97 |
| - except: |
98 |
| - break |
99 |
| - |
100 |
| - if len(audio_stream) >= (BYTES_PER_SECOND): |
101 |
| - #print ("got audio", len(audio_stream)) |
102 |
| - play_obj = sa.play_buffer(audio_stream, 2, 4, SAMPLE_RATE) |
103 |
| - audio_stream = bytearray() |
104 |
| - |
105 |
| - |
106 |
| -def main(): |
107 |
| - q = queue.Queue() |
108 |
| - server_t1 = threading.Thread(target=server_thread, args=(q, )) |
109 |
| - server_t1.daemon = True |
110 |
| - server_t1.start() |
111 |
| - |
112 |
| - server_t2 = threading.Thread(target=play_audio_thread, args=(q, )) |
113 |
| - server_t2.daemon = True |
114 |
| - server_t2.start() |
115 |
| - |
116 |
| - server_t1.join() |
117 |
| - server_t2.join() |
118 |
| - |
119 |
| - |
120 |
| -if __name__ == "__main__": |
121 |
| - main() |
| 1 | +import queue |
| 2 | +import socket |
| 3 | +import struct |
| 4 | +import sys |
| 5 | +import time |
| 6 | +import threading |
| 7 | + |
| 8 | +import simpleaudio as sa |
| 9 | +import numpy as np |
| 10 | + |
| 11 | + |
| 12 | +SAMPLE_RATE = 48000 |
| 13 | +BYTES_PER_FRAME = 8192 |
| 14 | +FRAMES_PER_SECOND = 48 |
| 15 | +BYTES_PER_SECOND = BYTES_PER_FRAME * FRAMES_PER_SECOND |
| 16 | + |
| 17 | +# Need to open the incoming port in your firewall first. |
| 18 | +HOST = '169.254.103.120' |
| 19 | +PORT = 11009 |
| 20 | + |
| 21 | +def server_thread(q): |
| 22 | + # create TCP socket |
| 23 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 24 | + s.bind((HOST, PORT)) |
| 25 | + s.settimeout(120) |
| 26 | + |
| 27 | + print("Listening to socket...") |
| 28 | + s.listen() |
| 29 | + try: |
| 30 | + print("Waiting for connection") |
| 31 | + conn, addr = s.accept() |
| 32 | + except: |
| 33 | + print("Timed out waiting for connection") |
| 34 | + return |
| 35 | + |
| 36 | + print("Connected!!") |
| 37 | + |
| 38 | + conn.settimeout(60) |
| 39 | + |
| 40 | + frames_recvd = 0 |
| 41 | + prev_time = -1 |
| 42 | + sound_data = bytearray() |
| 43 | + |
| 44 | + while True: |
| 45 | + # wait for a message |
| 46 | + try: |
| 47 | + data = conn.recv(8) |
| 48 | + except: |
| 49 | + break |
| 50 | + |
| 51 | + if data[0:4] != b'\x1a\xcf\xfc\x1d': |
| 52 | + print("Invalid sync pattern", data[0:4]) |
| 53 | + print(data[0:4].decode()) |
| 54 | + break |
| 55 | + |
| 56 | + frames_recvd += 1 |
| 57 | + if prev_time == -1: |
| 58 | + prev_time = time.time() |
| 59 | + elif (time.time() - prev_time > 1): |
| 60 | + print("Samples rcvd", frames_recvd) |
| 61 | + frames_recvd = 0 |
| 62 | + prev_time = time.time() |
| 63 | + |
| 64 | + total_message_length = list(bytes(data[4:8])) |
| 65 | + total_message_length = ((total_message_length[0] << 24) | |
| 66 | + (total_message_length[1] << 16) | |
| 67 | + (total_message_length[2] << 8) | |
| 68 | + (total_message_length[3] << 0)) |
| 69 | + |
| 70 | + # read the rest of the message from the socket using the given length |
| 71 | + audio_data = bytearray() |
| 72 | + bytes_read = 0 |
| 73 | + default_read_size = 8192 |
| 74 | + while (bytes_read != total_message_length): |
| 75 | + bytes_remaining = total_message_length - bytes_read |
| 76 | + |
| 77 | + if default_read_size > bytes_remaining: |
| 78 | + read_size = bytes_remaining |
| 79 | + elif default_read_size > total_message_length: |
| 80 | + read_size = total_message_length |
| 81 | + else: |
| 82 | + read_size = default_read_size |
| 83 | + |
| 84 | + message = conn.recv(read_size) |
| 85 | + bytes_read += len(message) |
| 86 | + audio_data.extend(message) |
| 87 | + |
| 88 | + q.put(audio_data) |
| 89 | + |
| 90 | + |
| 91 | +def play_audio_thread(q): |
| 92 | + audio_stream = bytearray() |
| 93 | + while True: |
| 94 | + try: |
| 95 | + audio = q.get(timeout=60) |
| 96 | + audio_stream.extend(audio) |
| 97 | + except: |
| 98 | + break |
| 99 | + |
| 100 | + if len(audio_stream) >= (BYTES_PER_SECOND): |
| 101 | + #print ("got audio", len(audio_stream)) |
| 102 | + play_obj = sa.play_buffer(audio_stream, 2, 4, SAMPLE_RATE) |
| 103 | + audio_stream = bytearray() |
| 104 | + |
| 105 | + |
| 106 | +def main(): |
| 107 | + q = queue.Queue() |
| 108 | + server_t1 = threading.Thread(target=server_thread, args=(q, )) |
| 109 | + server_t1.daemon = True |
| 110 | + server_t1.start() |
| 111 | + |
| 112 | + server_t2 = threading.Thread(target=play_audio_thread, args=(q, )) |
| 113 | + server_t2.daemon = True |
| 114 | + server_t2.start() |
| 115 | + |
| 116 | + server_t1.join() |
| 117 | + server_t2.join() |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments