-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpytalk.py
144 lines (128 loc) · 5.05 KB
/
pytalk.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
140
141
142
143
144
#!/usr/bin/python
###################################################################################
# Copyright (C) 2014, 2015, 2016, 2017, 2018 N4IRR
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
###################################################################################
from time import time, sleep, clock, localtime, strftime
from random import randint
import socket
import struct
import thread
import shlex
import alsaaudio
from gpiozero import LED, Button
from numpy import linspace,sin,pi,int16
def note(freq, len, amp=1, rate=8000):
t = linspace(0,len,len*rate)
data = sin(2*pi*freq*t)*amp
return data.astype(int16)
ipAddress = "127.0.0.1"
led = LED(25)
button = Button(23)
silence = chr(0)* 2048
def rxAudioStream():
global ipAddress
print('Start audio thread')
p.setformat(alsaaudio.PCM_FORMAT_S16_LE)
p.setrate(8000)
p.setchannels(1)
def tones():
p.write(note(900, .2, amp=1000, rate=8000))
p.write(note(600, .2, amp=1000, rate=8000))
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
udp.bind(("", 32001))
lastKey = -1
start_time = time()
call = ''
tg = ''
loss = '0.00%'
rxslot = '0'
while True:
soundData, addr = udp.recvfrom(1024)
if addr[0] != ipAddress:
ipAddress = addr[0]
if (soundData[0:4] == 'USRP'):
eye = soundData[0:4]
seq, = struct.unpack(">i", soundData[4:8])
memory, = struct.unpack(">i", soundData[8:12])
keyup, = struct.unpack(">i", soundData[12:16])
talkgroup, = struct.unpack(">i", soundData[16:20])
type, = struct.unpack("i", soundData[20:24])
mpxid, = struct.unpack(">i", soundData[24:28])
reserved, = struct.unpack(">i", soundData[28:32])
audio = soundData[32:]
if (type == 0): # voice
audio = soundData[32:]
#print(eye, seq, memory, keyup, talkgroup, type, mpxid, reserved, audio, len(audio), len(soundData))
if (len(audio) == 320):
# stream.write(audio,160)
p.write(audio)
if (keyup != lastKey):
# print('key' if keyup else 'unkey')
if keyup:
start_time = time()
if keyup == False:
if (time() - start_time)>=1.2:
tones();
print '{} {} {} {} {} {} {:.2f}s'.format(
strftime(" %m/%d/%y", localtime(start_time)),
strftime("%H:%M:%S", localtime(start_time)),
call, rxslot, tg, loss, time() - start_time)
lastKey = keyup
if (type == 2): #metadata
audio = soundData[32:]
if ord(audio[0]) == 8:
tg = (ord(audio[9]) << 16) + (ord(audio[10]) << 8) + ord(audio[11])
rxslot = ord(audio[12]);
call = audio[14:]
else:
print(soundData, len(soundData))
udp.close()
def txAudioStream():
q.setformat(alsaaudio.PCM_FORMAT_S16_LE)
q.setrate(8000)
q.setchannels(1)
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
lastPtt = ptt
seq = 0
while True:
try:
audio = q.read()
if ptt != lastPtt:
usrp = 'USRP' + struct.pack('>iiiiiii',seq, 0, ptt, 0, 0, 0, 0)
udp.sendto(usrp, (ipAddress, 34001))
seq = seq + 1
print 'PTT: {}'.format(ptt)
lastPtt = ptt
if ptt:
usrp = 'USRP' + struct.pack('>iiiiiii',seq, 0, ptt, 0, 0, 0, 0) + audio
udp.sendto(usrp, (ipAddress, 34001))
print 'transmitting'
seq = seq + 1
except:
print("overflow")
ptt = False # toggle this to transmit (left up to you)
p = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK)
q = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
thread.start_new_thread( rxAudioStream, () )
# thread.start_new_thread( txAudioStream, () )
while True:
if button.is_pressed:
led.on()
ptt=True
else:
led.off()
ptt=False
sleep(0.02)