microbit-foundation / micropython-microbit-v2

Temporary home for MicroPython for micro:bit v2 as we stablise it before pushing upstream
MIT License
45 stars 25 forks source link

With low rates, microphone.record_into() takes less time than expected #202

Open microbit-carlos opened 6 months ago

microbit-carlos commented 6 months ago

Similar to:

But in this case, I would not expect CODAL to be able to return early as it needs to record the expected length of audio before it returns.

With the default rate works as expected:

>>> af = audio.AudioFrame(100)
>>> t1 = running_time(); microphone.record_into(af); t2 = running_time(); t2-t1
101

With low enough rates with have issue https://github.com/microbit-foundation/micropython-microbit-v2/issues/201 and it returns immediately:

>>> af = audio.AudioFrame(1000, rate=128)
>>> t1 = running_time(); microphone.record_into(af); t2 = running_time(); t2-t1
2
>>> bytes(af)
b'\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80'
>>> 

But with a rate of 256, data does go into the AudioFrame, but takes less time than expected:

>>> af = audio.AudioFrame(200, rate=256)
>>> len(af)
64
>>> t1 = running_time(); microphone.record_into(af); t2 = running_time(); t2-t1
162
>>> bytes(af)
b'\x81\x81\x80\x83\x81\x81\x80\x82\x81z~\x7f\x81\x81\x80\x7f\x80\x82~\x81\x80\x81\x80{|\x80\x80z\x85\x80\x80\x7f~~\x80\x81\x80\x83\x81\x85\x81\x82\x83\x82{|\x80{|\x82}\x80\x80\x80~\x82\x81}}\x7f|}\x80\x81'
>>> af.get_rate()
256
>>> 

I guess this could also be an issue in CODAL, if that's the case let me know and I'll raise it in the codal-microbit-v2 repository.

microbit-carlos commented 6 months ago

I'll try to replicate this on CODAL first to check if this is an issue there.