-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmitter.py
39 lines (32 loc) · 1 KB
/
transmitter.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
import socket
import pyaudio
import sys
import constants
import re
audio = pyaudio.PyAudio()
# Listen devices
for i in range(0, audio.get_device_count()):
print(i, audio.get_device_info_by_index(i)['name'])
device_index = int(input('Device index: '))
# Configure socket
local_ip = input("IP: ")
if not constants.IP_REGEX.match(local_ip):
sys.exit("Invalid IP")
try:
local_port = int(input("Port: "))
assert(1025 <= local_port <= 65535)
except:
sys.exit("Invalid port number")
address = (local_ip, local_port)
udp_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Send to server using created UDP socket
stream = audio.open(format=pyaudio.paInt16, channels=constants.NUM_CHANNELS,
rate=constants.SAMPLE_RATE, input=True, input_device_index=device_index,
frames_per_buffer=constants.CHUNK_SIZE)
print("Sending...")
while True:
try:
data = stream.read(constants.CHUNK_SIZE)
udp_socket.sendto(data, address)
except:
pass