|
9 | 9 |
|
10 | 10 | def receive_data(data): |
11 | 11 | global audio_buffer |
12 | | - global expected_length |
13 | 12 | audio_buffer += data |
14 | 13 | print( |
15 | | - f" Downloading microphone data {str(len(audio_buffer))} / {str(int(expected_length))} bytes ", |
| 14 | + f" Downloading microphone data {str(len(audio_buffer))} bytes ", |
16 | 15 | end="\r", |
17 | 16 | ) |
18 | 17 |
|
19 | 18 |
|
20 | 19 | async def test_microphone(b: Bluetooth): |
21 | 20 | global audio_buffer |
22 | | - global expected_length |
23 | | - expected_length = 3 * 8000 * (8 / 8) |
24 | | - |
25 | | - await b.send_lua("frame.microphone.record{seconds=3}") |
26 | | - await asyncio.sleep(3) |
27 | | - |
28 | 21 | audio_buffer = b"" |
29 | 22 |
|
| 23 | + await b.send_lua("frame.microphone.start { bit_depth=16 }") |
| 24 | + |
30 | 25 | await b.send_lua( |
31 | | - "while true do local i = frame.microphone.read(frame.bluetooth.max_length()) if (i == nil) then break end while true do if pcall(frame.bluetooth.send, i) then break end end end" |
| 26 | + "while true do s=frame.microphone.read(frame.bluetooth.max_length()); if s==nil then break end if s~='' then while true do if (pcall(frame.bluetooth.send,s)) then break end end end end" |
32 | 27 | ) |
33 | 28 |
|
34 | | - while len(audio_buffer) < expected_length: |
35 | | - await asyncio.sleep(0.001) |
| 29 | + await asyncio.sleep(5) |
36 | 30 |
|
37 | | - audio_data = np.frombuffer(audio_buffer, dtype=np.int8) |
| 31 | + await b.send_break_signal() |
| 32 | + await b.send_lua(f"frame.microphone.stop()") |
| 33 | + |
| 34 | + audio_data = np.frombuffer(audio_buffer, dtype=np.int16) |
38 | 35 | audio_data = audio_data.astype(np.float32) |
39 | | - audio_data /= np.iinfo(np.int8).max |
| 36 | + audio_data /= np.iinfo(np.int16).max |
40 | 37 |
|
41 | 38 | sd.play(audio_data, 8000) |
42 | 39 | sd.wait() |
43 | 40 |
|
44 | 41 |
|
45 | | -if __name__ == "__main__": |
| 42 | +async def main(): |
46 | 43 | b = Bluetooth() |
| 44 | + await b.connect(data_response_handler=receive_data) |
| 45 | + await test_microphone(b) |
| 46 | + await b.disconnect() |
| 47 | + |
47 | 48 |
|
48 | | - loop = asyncio.get_event_loop() |
49 | | - loop.run_until_complete(b.connect(data_response_handler=receive_data)) |
50 | | - loop.run_until_complete(test_microphone(b)) |
51 | | - loop.run_until_complete(b.disconnect()) |
| 49 | +asyncio.run(main()) |
0 commit comments