-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbluetooth_audio.py
executable file
·361 lines (332 loc) · 9.87 KB
/
bluetooth_audio.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
#!/usr/bin/env python3
import bluetooth
import logging
import time
import threading
import struct
import sys
import math
# from specs
SOL_BLUETOOTH = 274
SOL_SCO = 17
BT_VOICE = 11
BT_VOICE_TRANSPARENT = 0x0003
BT_VOICE_CVSD_16BIT = 0x0060
SCO_OPTIONS = 1
L2CAP_UUID = "0100"
SCO_HEADERS_SIZE = 16
class BluetoothAudio:
""" This object connect to Bluetooth handset/nandsfree device
stream audio from microphone and to speaker.
"""
HFP_TIMEOUT = 1.0
HFP_CONNECT_AUDIO_TIMEOUT = 10.0
AUDIO_8KHZ_SIGNED_16BIT_LE_MONO = 0
AUDIO_16KHZ_SIGNED_16BIT_LE_MONO = 1
CAPTURE_BUFFER_MAX_SIZE = 16777216 # 16 Mb
def __init__(self, addr, format = AUDIO_8KHZ_SIGNED_16BIT_LE_MONO):
""" Create object which connects to bluetooth device in the background.
Class automatically reconnects to the device in case of any errors.
:param addr: MAC address of Bluetooth device, string.
"""
self.audio = None
self.hfp = None
self.addr = addr
self.resample = (format == self.AUDIO_16KHZ_SIGNED_16BIT_LE_MONO)
self.wlt = threading.Thread(target=self._worker_loop)
self.wlt.start()
self.buf = bytes()
self.rlt = None
self.rltl = threading.Lock()
def _read_loop(self):
logging.info('Read loop start')
self.buf = bytes()
while self.rlt:
try:
data_raw = self.audio.recv(self.sco_payload)
except bluetooth.btcommon.BluetoothError:
data_raw = None
if not data_raw or len(data_raw) == 0:
self.audio.close()
self.audio = None
logging.warning('Capture audio failed')
break
if self.resample:
# convert data
data = bytes()
prev = 0
for i in range(0, len(data_raw), 2):
# convert from 8 kHz signed 16 bit le to 16 kHz signed 16 bit le
v = struct.unpack_from('<h', data_raw, i)[0]
data += struct.pack('<hh', int((prev + v) / 2), v)
prev = v
else:
data = data_raw
self.rltl.acquire(True)
if len(self.buf) > self.CAPTURE_BUFFER_MAX_SIZE:
logging.warning('Capture buffer overflow')
self.buf = bytes()
self.buf += data
self.rltl.release()
logging.info('Read loop stop')
def _worker_loop(self):
logging.info('HFPDevice class is initialised, using ' + self.addr)
while self.wlt:
self._find_channel()
if not self.channel:
time.sleep(self.HFP_TIMEOUT)
continue
logging.info('HSP/HFP found on RFCOMM channel ' + str(self.channel))
self._connect_service_level()
if not self.hfp:
time.sleep(self.HFP_TIMEOUT)
continue
try:
self._parse_channel()
except bluetooth.btcommon.BluetoothError as e:
logging.warning('Service level connection disconnected: ' + str(e))
time.sleep(self.HFP_TIMEOUT)
self._cleanup()
def _parse_channel(self):
audio_time = time.time() + self.HFP_CONNECT_AUDIO_TIMEOUT
sevice_notice = True
while self.wlt:
data = self._read_at()
if data:
if b'AT+BRSF=' in data:
self._send_at(b'+BRSF: 0')
self._send_ok()
elif b'AT+CIND=?\r' == data:
self._send_at(b'+CIND: ("service",(0,1)),("call",(0,1))')
self._send_ok()
elif b'AT+CIND?\r' == data:
self._send_at(b'+CIND: 1,0')
self._send_ok()
elif b'AT+CMER=' in data:
self._send_ok()
# after this command we can establish audio connection
sevice_notice = False
self._connect_audio()
elif b'AT+CHLD=?\r' == data:
self._send_at(b'+CHLD: 0')
self._send_ok()
else:
self._send_error()
# if we don't get service level connection, try audio anyway
if not self.audio:
if audio_time < time.time():
if sevice_notice:
logging.warning('Service connection timed out, try audio anyway...')
sevice_notice = False
self._connect_audio()
def _connect_service_level(self):
hfp = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
try:
hfp.connect((self.addr, self.channel))
except bluetooth.btcommon.BluetoothError as e:
hfp.close()
logging.warning('Failed to establish service level connection: ' + str(e))
return
hfp.settimeout(self.HFP_TIMEOUT)
logging.info('HSP/HFP service level connection is established')
self.hfp = hfp
def _connect_audio(self):
audio = bluetooth.BluetoothSocket(bluetooth.SCO)
# socket config
opt = struct.pack ("H", BT_VOICE_CVSD_16BIT)
audio.setsockopt(SOL_BLUETOOTH, BT_VOICE, opt)
try:
audio.connect((self.addr,))
except bluetooth.btcommon.BluetoothError as e:
audio.close()
logging.info('Failed to establish audio connection: ' + str(e))
return
opt = audio.getsockopt(SOL_SCO, SCO_OPTIONS, 2)
mtu = struct.unpack('H', opt)[0]
self.audio = audio
self.sco_payload = mtu - SCO_HEADERS_SIZE
self.rlt = threading.Thread(target=self._read_loop)
self.rlt.start()
logging.info('Audio connection is established, mtu = ' + str(mtu))
def _find_channel(self):
# discovery RFCOMM channell, prefer HFP.
hsp_channel = None
generic_channel = None
services = bluetooth.find_service(address=self.addr, uuid=L2CAP_UUID)
for svc in services:
for c in svc["service-classes"]:
service_class = c.lower()
if bluetooth.HANDSFREE_CLASS.lower() == service_class:
self.channel = int(svc["port"])
return
elif bluetooth.HEADSET_CLASS.lower() == service_class:
hsp_channel = int(svc["port"])
elif bluetooth.GENERIC_AUDIO_CLASS.lower() == service_class:
generic_channel = int(svc["port"])
if hsp_channel:
self.channel = hsp_channel
else:
self.channel = generic_channel
def _read_at(self):
try:
d = self.hfp.recv(1024)
logging.debug('> ' + d.decode('utf8'))
return d
except bluetooth.btcommon.BluetoothError as e:
if str(e) != 'timed out':
raise
return None
def _send(self, data):
logging.debug('< ' + data.decode('utf8').replace('\r\n', ''))
self.hfp.send(data)
def _send_at(self, data):
self._send(b'\r\n' + data + b'\r\n')
def _send_ok(self):
self._send_at(b'OK')
def _send_error(self):
self._send_at(b'ERROR')
def _cleanup(self):
if self.rlt:
rlt = self.rlt
self.rlt = None
rlt.join()
if self.audio:
self.audio.close()
if self.hfp:
self.hfp.close()
self.hfp = None
self.audio = None
def close(self):
wlt = self.wlt
self.wlt = None
wlt.join()
self._cleanup()
def is_connected(self):
""" Check if headset/handfree device is connected.
:return: True if connected, False otherwise.
"""
return (self.audio != None)
def flush(self):
""" Clean up capture buffer
"""
self.rltl.acquire(True)
self.buf = bytes()
self.rltl.release()
def read(self, length = None):
""" Receive audio from bluetooth device. Block until read something.
:param length: number of bytes(not samples) to read, not more then CAPTURE_BUFFER_MAX_SIZE.
If None or 0, all aviliable will be read
:return: Array with audio data(16 kHz signed 16 bit little endian mono data) or None on error.
"""
if not self.rlt:
return None
if length == None:
length = 0
while len(self.buf) == 0 or len(self.buf) < length:
if not self.rlt:
return None
if not self.rlt.isAlive():
return None
s = (length - len(self.buf)) / 8000
if s <= 0:
self.rlt.join(0.001)
else:
if self.resample:
s = s / 2
self.rlt.join(s)
self.rltl.acquire(True)
if length == 0:
data = self.buf
self.buf = bytes()
else:
data = self.buf[0:length]
self.buf =self.buf[length:]
self.rltl.release()
return data
def write(self, data):
""" Send audio data to bluetooth device. Blocking.
:param data: array with audio data.
:param format: audio fromat, for example AUDIO_8KHZ_SIGNED_8BIT_MONO or AUDIO_16KHZ_SIGNED_16BIT_LE_MONO.
:return: True on success, False on error.
"""
if not self.audio:
return False
try:
if self.resample:
# convert data
data_raw = bytes()
for i in range(0, len(data), 4):
val1, val2 = struct.unpack_from('<hh', data, i) # two samples of signed 16 bit le
val = round((val1 + val2) / 2) # downsample to 8 kHz
data_raw += struct.pack('<h', val)
else:
data_raw = data
sent = 0
while sent < len(data_raw):
ts = data_raw[sent:(sent+int(self.sco_payload))]
if len(ts) < self.sco_payload:
ts += bytes([0] * (self.sco_payload - len(ts)))
sent += self.audio.send(ts)
return True
except bluetooth.btcommon.BluetoothError:
return False
except AttributeError: # 'NoneType' object has no attribute 'send'
return False
def beep(self, length_ms = 300, frequency = 1000.0, amplitude = 0.5):
""" Make a beep sound with specified parameters
:return: True on success, False on error.
"""
fps = 8000
if self.resample:
fps = 16000
logging.info('Beep {} Hz, {} ms'.format(frequency, length_ms))
period = int(fps / frequency)
length = int(fps * length_ms / 1000)
snd = bytes()
for i in range(0, length):
val = 32767.0 * amplitude * math.sin(2.0 * math.pi * float(i % period) / period)
snd += struct.pack('<h', int(val))
return self.write(snd)
def demo_ring(hf):
time.sleep(1)
hf._send_at(b'RING')
def main():
""" Sample of usage BluetoothAudio.
This sample loopback audio from microphone to speaker.
"""
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
if len(sys.argv) == 1:
print("Please specify device MAC address or 'scan' to scan it.")
sys.exit(1)
if sys.argv[1] == 'scan':
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
print(nearby_devices)
return
if not bluetooth.is_valid_address(sys.argv[1]):
print("Wrong device address.")
return
hf = BluetoothAudio(sys.argv[1], BluetoothAudio.AUDIO_16KHZ_SIGNED_16BIT_LE_MONO)
# Make a test RING from headset
#threading.Thread(target=demo_ring, args=[hf]).start()
try:
while not hf.is_connected():
time.sleep(0.1)
time.sleep(1.5)
hf.beep()
time.sleep(0.5)
hf.flush()
while True:
d = hf.read()
if d:
hf.write(d)
else:
time.sleep(0.5)
# generate noise
#hf.write(bytes(i for i in range(hf.sco_payload)))
except KeyboardInterrupt:
pass
hf.close()
logging.info('\nExiting...')
if __name__ == '__main__':
main()