-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.py
119 lines (112 loc) · 3.13 KB
/
t.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
#!/usr/bin/env python
import pyaudio
from numpy import zeros,linspace,short,fromstring,hstack,transpose
from scipy import fft
from time import sleep, time
from os import system
from FSKChars import FSKToEnglish
import signal
import sys
import validate
#Sensitivity, 0.05: more Sensitive
# 0.1: Probably Ideal
# 1: less sensitive,
SENSITIVITY= 0.05
#Bandwidth for detection
BANDWIDTH = 60
#Set up audio sampler
SAMPLES = 1024
RATE = 88200
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=SAMPLES)
def sigHandler(s, fr):
global p, stream
stream.stop_stream()
stream.close()
p.terminate()
exit(0)
signal.signal(signal.SIGINT, sigHandler)
lo = 2300
hi = 2600
DETECT = [lo,hi]
o = ""
msg = ""
print "listening for code.."
def processBits(bits):
curChr = ""
sp = 0
bt = 0
buf = []
dotMin = 1
dashMin = 6
space = 6
m=""
for bit in bits:
if int(bit)==1:
bt+=1
sp=0
else:
sp+=1
if bt >= dotMin and bt < dashMin:
curChr+="."
bt = 0
elif bt>=dashMin:
curChr+="-"
bt = 0
if sp >= space:
if curChr in FSKToEnglish.keys():
m+= FSKToEnglish[curChr] + " "
else:
m+= curChr
curChr = ""
t,c,y,z="",1,m.replace(" ","").split("0X"),""
for x in y:
if c == len(y):
z=x
break
t+=x
c+=1
return [t,"0X{}".format(z),validate.crc(t).upper()]
lockFlag = False
loScore = 0
while True:
while stream.get_read_available()< SAMPLES: sleep(0.005)
audioData = fromstring(stream.read(
stream.get_read_available()), dtype=short)[-SAMPLES:]
normalizedData = audioData / 32768.0
intensity = abs(fft(normalizedData))[:SAMPLES/2]
frequencies = linspace(0.0, float(RATE)/2, num=SAMPLES/2)
for tone in DETECT:
try:
if max(intensity[(frequencies < tone+BANDWIDTH) &
(frequencies > tone-BANDWIDTH )]) >\
max(intensity[(frequencies < tone-100) &
(frequencies > tone-200)]) + SENSITIVITY:
b = int(tone == hi)
msg+=str(b)
if not b:
loScore += 1
if not lockFlag:
print "Tone detected! Attempting to demodulate.."
lockFlag = True
else:
loScore = 0
if loScore >= 25:
loScore = 0
while "101" in msg: #clean up stray bits
msg=msg.replace("101","111")
while "010" in msg:
msg=msg.replace("010","000")
msg=processBits(msg)
print "PKT:{}, C1:{}, C2:{}, VERIFIED:{}"\
.format(msg[0], msg[1], msg[2], (msg[1] == msg[2]))
msg=""
except Exception as e:
print e
print 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno)
scoreIndex=0
pass