-
Notifications
You must be signed in to change notification settings - Fork 10
/
chirp.py
executable file
·376 lines (312 loc) · 11.8 KB
/
chirp.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python
"""
Chirp.io Encoder/Decoder
"""
import os
import sys
import wave
import time
import magic
import string
import pyaudio
import reedsolo
import requests
import argparse
import threading
import webbrowser
import numpy as np
MIN_AMPLITUDE = 2500
SAMPLE_RATE = 44100.0 # Hz
SAMPLE_LENGTH = 3 # sec
class Audio():
""" Audio Processing """
CHUNK = 4096
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = SAMPLE_RATE
HUMAN_RANGE = 20000
def __init__(self):
self.audio = pyaudio.PyAudio()
def __del__(self):
try:
self.audio.terminate()
except:
pass
def record(self, seconds, filename=None):
""" Record audio from system microphone """
frames = []
stream = self.audio.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=int(self.RATE),
input=True,
frames_per_buffer=self.CHUNK)
for i in range(0, int(self.RATE / self.CHUNK * seconds)):
data = stream.read(self.CHUNK)
frames.append(data)
if filename:
self.write(frames, filename)
stream.stop_stream()
stream.close()
return b''.join(frames)
def play(self, frames):
""" Write data to system audio buffer"""
stream = self.audio.open(format=pyaudio.paInt16,
channels=1,
rate=int(self.RATE),
output=True)
stream.write(frames, len(frames))
stream.stop_stream()
stream.close()
def read(self, filename):
""" Read wave file """
wf = wave.open(filename, 'rb')
buf = bytearray()
chunk = wf.readframes(self.CHUNK)
buf.extend(chunk)
while chunk != '':
chunk = wf.readframes(self.CHUNK)
buf.extend(chunk)
return buf
def write(self, frames, filename):
""" Write wave file """
wf = wave.open(filename, 'wb')
wf.setnchannels(self.CHANNELS)
wf.setsampwidth(self.audio.get_sample_size(self.FORMAT))
wf.setframerate(self.RATE)
wf.writeframes(b''.join(frames))
wf.close()
class Signal():
""" Digital Signal Processing """
def __init__(self, fs):
self.fs = float(fs) # sampling frequency
def fft(self, y):
""" Perform FFT on y with sampling rate"""
n = len(y) # length of the signal
k = np.arange(n)
T = n / self.fs
freq = k / T # two sides frequency range
freq = freq[range(int(n / 2))] # one side frequency range
Y = np.fft.fft(y) / n # fft computing and normalisation
Y = Y[range(int(n / 2))]
return (freq, abs(Y))
def max_freq(self, data):
""" Perform FFT on data and return maximum frequency """
x, y = self.fft(data)
index = y.argmax()
return x[index]
def sine_wave(self, freq, duration):
""" Generate a sine wave array at given frequency for duration in seconds """
return np.sin(2 * np.pi * np.arange(self.fs * duration) * freq / self.fs)
class Chirp():
""" Chirp Encoding/Decoding
http://www.chirp.io/technology """
RATE = SAMPLE_RATE
CHAR_LENGTH = 0.0872 # duration of one chirp character - 87.2ms
CHAR_SAMPLES = CHAR_LENGTH * RATE # number of samples in one chirp character
CHIRP_SAMPLES = CHAR_SAMPLES * 20 # number of samples in an entire chirp
CHIRP_VOLUME = 2 ** 16 / 48 # quarter of max amplitude
GET_URL = 'http://labs.chirp.io/get'
POST_URL = 'http://labs.chirp.io/chirp'
FILE_URL = 'http://labs.chirp.io/file'
def __init__(self):
self.map = self.get_map()
self.chars = sorted(self.map.keys())
self.dsp = Signal(self.RATE)
self.rs = reedsolo.RSCodec(nsym=8, nsize=20, prim=0x25, generator=2, c_exp=5)
def get_map(self):
""" Construct map of chirp characters to frequencies
0 = 1760Hz 1 = 1864Hz v = 10.5kHz """
a6 = 1760
a = 2 ** (1 / 12.0)
# characters range from 0-9 and a-v
chars = string.digits + string.ascii_letters[0:22]
d = {}
for n in range(0, 32):
d[chars[n]] = a6 * (a ** n)
return d
def get_code(self, url):
""" Request a long code from chirp API """
try:
r = requests.post(self.POST_URL, {'url': url})
rsp = r.json()
if 'longcode' in rsp:
return 'hj' + rsp['longcode']
elif 'error' in rsp:
print(rsp['error']['msg'])
sys.exit(-1)
except:
print('Server failed to respond')
sys.exit(-1)
def get_file_code(self, filename, filetype):
""" Request a long code for a file from chirp API """
try:
headers = {'Content-Type': filetype, 'Accept': 'application/json'}
params = {'title': os.path.basename(filename)}
r = requests.post(self.FILE_URL, files={filename: open(filename, 'rb')},
headers=headers, params=params)
rsp = r.json()
if 'longcode' in rsp:
return 'hj' + rsp['longcode']
elif 'error' in rsp:
print(rsp['error']['msg'])
sys.exit(-1)
except:
print('Server failed to respond')
sys.exit(-1)
def get_char(self, data):
""" Find maximum frequency in fft data then find the closest
frequency in chirp map and return character """
freq = self.dsp.max_freq(data)
ch, f = min(self.map.items(), key=lambda kv: abs(kv[1] - freq))
return ch
def decode(self, data):
""" Try and find a chirp in the data, and decode into a string """
s = 0
chirp = ''
# check for frontdoor pair
chirp += self.get_char(data[s:s+int(self.CHAR_SAMPLES)])
s += self.CHAR_SAMPLES
if chirp != 'h':
return 1
chirp += self.get_char(data[int(s):int(s+self.CHAR_SAMPLES)])
s += self.CHAR_SAMPLES
if chirp != 'hj':
return 2
for i in range(2, 20):
chirp += self.get_char(data[int(s):int(s+self.CHAR_SAMPLES)])
s += self.CHAR_SAMPLES
return chirp
def encode(self, chirp, internal=False):
""" Generate audio data from a chirp string """
samples = np.array([], dtype=np.int16)
if internal:
chirp = self.ecc(chirp, encode=internal)
for s in chirp:
freq = self.map[s]
char = self.dsp.sine_wave(freq, self.CHAR_LENGTH)
samples = np.concatenate([samples, char])
samples = (samples * self.CHIRP_VOLUME).astype(np.int16)
return samples
def search(self, data):
""" Search data for audio, and try and decode """
s = 0
chirp_code = None
datalen = len(data)
if data.argmax() < MIN_AMPLITUDE:
return
while s < datalen - self.CHIRP_SAMPLES:
# search for start of audio
if data[int(s)] > MIN_AMPLITUDE:
# check for any chirps, if unsuccessful
# carry on searching..
chirp_code = self.decode(data[int(s):])
# advance pointer by searched data
if isinstance(chirp_code, int):
s += chirp_code * self.CHAR_SAMPLES
else:
# try and perform error correction
corrected = self.ecc(chirp_code)
if corrected:
chirp_code = corrected
r = requests.get(self.GET_URL + '/' + chirp_code[2:12])
if r.status_code == 200:
print('\nFound Chirp!')
rsp = r.json()
# print (chirp_code)
print('URL: %s' % rsp['url'])
if 'data' in rsp and 'description' in rsp['data']:
print(rsp['data']['description'])
webbrowser.open(rsp['url'])
return
s += 1
def string_to_list(self, s):
""" Convert string to list for reed solomon """
arr = []
for i in s:
arr.append(self.chars.index(i))
return arr
def list_to_string(self, a):
""" Convert list to string for reed solomon """
s = ''
for i in a:
s += self.chars[i]
return s
def ecc(self, data, encode=False):
""" Reed Solomon Error Correction """
try:
if encode:
arr = self.string_to_list(data[0:12])
out = self.rs.encode(arr)
return self.list_to_string(out)
else: # decode
arr = self.string_to_list(data)
out = self.rs.decode(arr)
return self.list_to_string(out)
except reedsolo.ReedSolomonError:
return None
class DecodeThread(threading.Thread):
""" Thread to run digital signal processing functions """
def __init__(self, fn, quit=False):
self.fn = fn
self.data = None
self.quit = quit
self.window = np.array([], dtype=np.int16)
threading.Thread.__init__(self)
def run(self):
while not self.quit:
if self.data is not None:
# add data to window so we don't miss any chirps
d1, d2 = np.array_split(self.data, 2)
w1, w2 = np.array_split(self.window, 2)
self.window = np.concatenate([w2, d1])
self.fn(self.window)
w1, w2 = np.array_split(self.window, 2)
self.window = np.concatenate([w2, d2])
self.fn(self.window)
self.data = None
time.sleep(SAMPLE_LENGTH)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Chirp.io Encoder/Decoder')
parser.add_argument('-l', '--listen', action='store_true', default=False, help='listen out for a chirp')
parser.add_argument('-i', '--internal', action='store_true', default=False, help='use internal error correction')
parser.add_argument('-u', '--url', help='chirp a url')
parser.add_argument('-c', '--code', help='chirp a code')
parser.add_argument('-f', '--file', help='chirp a file, path to either a jpg, png or pdf')
args = parser.parse_args()
chirp = Chirp()
audio = Audio()
dsp = Signal(SAMPLE_RATE)
if args.listen:
try:
thread = DecodeThread(chirp.search)
thread.start()
print('Recording...')
while (True):
buf = audio.record(SAMPLE_LENGTH)
thread.data = np.frombuffer(buf, dtype=np.int16)
except KeyboardInterrupt:
print('Exiting..')
thread.quit = True
sys.exit(0)
elif args.code:
samples = chirp.encode(args.code, internal=args.internal)
print('Chirping code: %s' % args.code)
audio.play(samples)
elif args.url:
code = chirp.get_code(args.url)
samples = chirp.encode(code, internal=args.internal)
print('Chirping url: %s' % args.url)
audio.play(samples)
elif args.file:
filetype = magic.from_file(args.file, mime=True)
if filetype not in ('image/jpeg', 'image/png', 'application/pdf'):
print('Filetype not supported')
sys.exit(-1)
code = chirp.get_file_code(args.file, filetype)
samples = chirp.encode(code, internal=args.internal)
print('Chirping file: %s' % args.file)
audio.play(samples)
else:
print('No arguments specified!')
print('Exiting..')
sys.exit(0)