Skip to content

Commit 6656236

Browse files
authored
Merge pull request #214 from brilliantlabsAR/mic-fix
Fixed audio cut off bug in mic driver
2 parents ce3017d + 539caaf commit 6656236

File tree

7 files changed

+145
-301
lines changed

7 files changed

+145
-301
lines changed

production/test_microphone_script.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,41 @@
99

1010
def receive_data(data):
1111
global audio_buffer
12-
global expected_length
1312
audio_buffer += data
1413
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 ",
1615
end="\r",
1716
)
1817

1918

2019
async def test_microphone(b: Bluetooth):
2120
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-
2821
audio_buffer = b""
2922

23+
await b.send_lua("frame.microphone.start { bit_depth=16 }")
24+
3025
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"
3227
)
3328

34-
while len(audio_buffer) < expected_length:
35-
await asyncio.sleep(0.001)
29+
await asyncio.sleep(5)
3630

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)
3835
audio_data = audio_data.astype(np.float32)
39-
audio_data /= np.iinfo(np.int8).max
36+
audio_data /= np.iinfo(np.int16).max
4037

4138
sd.play(audio_data, 8000)
4239
sd.wait()
4340

4441

45-
if __name__ == "__main__":
42+
async def main():
4643
b = Bluetooth()
44+
await b.connect(data_response_handler=receive_data)
45+
await test_microphone(b)
46+
await b.disconnect()
47+
4748

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

Comments
 (0)