-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.py
executable file
·139 lines (106 loc) · 2.87 KB
/
receiver.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
"""
Lightweight Audio Communication
"""
import socket
import pyaudio
from os import system
from collections import deque
from sys import argv
# Chunk size = 512
# Can't send more than 1472 bytes through a socket
CHUNK = 512
# Audio track format = 16bit signed int
AUDIO_FORM = pyaudio.paInt16
# Audio sample width
AUDIO_SAMP_WIDTH = 2
# Number of channels = 1
AUDIO_CHAN = 1
# Audio sampling rate = 8000 Hz
AUDIO_RATE = 8000
# Communication partner has to be set somewhere
partner_ip = "111.1.111.1"
# Partner's port
partner_port = 17001
# Initialize PyAudio
p = pyaudio.PyAudio()
received = deque()
# Sending socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Receiving socket
sr = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def rec_callback(in_data, frame_count, time_info, status):
global sr
sr.sendto(in_data, (partner_ip, partner_port))
return (None, pyaudio.paContinue)
#return (None, pyaudio.paComplete)
def play_callback(in_data, frame_count, time_info, status):
global received
if len(received) != 0:
return (received.popleft(), pyaudio.paContinue)
else:
return (bytes(1024*2), pyaudio.paContinue)
#return (None, pyaudio.paComplete)
# Initialize PyAudio Stream
AUDIO_PLAY_STREAM = p.open(
format=AUDIO_FORM,
channels=AUDIO_CHAN,
rate=AUDIO_RATE,
input=False, output=True,
frames_per_buffer=CHUNK,
stream_callback=play_callback)
AUDIO_REC_STREAM = p.open(
format=AUDIO_FORM,
channels=AUDIO_CHAN,
rate=AUDIO_RATE,
input=True, output=False,
frames_per_buffer=CHUNK,
stream_callback=rec_callback)
def forward_port(int_port, ext_port, time):
"""
Forward a port using miniupnp
"""
command = "upnpc -a %r %r %r UDP %r" % (
socket.gethostbyname(socket.gethostname()),
int_port, ext_port, time
)
system(command)
def receive(port, r_only):
"""
Receive speach bytes
"""
global s
AUDIO_REC_STREAM.start_stream()
AUDIO_PLAY_STREAM.start_stream()
if r_only:
AUDIO_REC_STREAM.close()
communicate = True
while communicate:
data, addr = s.recvfrom(CHUNK*AUDIO_SAMP_WIDTH)
received.append(data)
print(214)
if not r_only:
AUDIO_REC_STREAM.close()
AUDIO_PLAY_STREAM.close()
def print_info():
print("Usage:")
print("./receiver.py in_port ext_port partner_export partner_ip receive_only")
print("Example:")
print("./receiver.py 17001 17002 17003 1.2.3.4 0")
if len(argv) < 5:
print_info()
exit(1)
else:
partner_port = int(argv[3])
partner_ip = argv[4]
host = socket.gethostbyname(socket.gethostname())
port = int(argv[1])
s.bind((host, port))
forward_port(argv[1], argv[2], 300)
r_only = False
if len(argv) > 5:
r_only = argv[5] == "1"
receive(int(argv[1]), r_only)
s.close()
sr.close()
p.terminate()