microbit-foundation / micropython-microbit-v2

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

Recording with a low rate doesn't work #201

Open microbit-carlos opened 2 months ago

microbit-carlos commented 2 months ago

Tested with the latest version of the recording & playback branch: https://github.com/microbit-foundation/micropython-microbit-v2/pull/163/commits/c91ae4e83c5a1a82f647740277f7d4a462192160

When the recording rate is set below 143 the recording function returns immediately without filling the AudioFrame.

>>> af = audio.AudioFrame()
>>> bytes(af)    # Default value is 0x80
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'
>>> # Timing how long it takes to record at 142 samples per second
>>> t = running_time(); microphone.record_into(af, rate=142); running_time() - t
1
>>> bytes(af)    # AudioFrame data untouched
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'
>>> t = running_time(); microphone.record_into(af, rate=143); running_time() - t
181
>>> bytes(af)
b'\x80\x81\x80\x7f\x80\x80\x80\x80\x80\x80\x81}~\x82}\x7f{\x81\x7f\x80\x81|\x81\x80\x7f|\x80\x84n\x8ey\x8a'
>>> 
>>> t = running_time(); af = microphone.record(100, 142); running_time() - t
1
>>> 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'
>>> t = running_time(); af = microphone.record(100, 143); running_time() - t
178
>>> bytes(af)
b'\x80\x81\x86}\x7f\x80|\x7f~\x81|||~\x82\x7f\x7f\x80\x7fz\x80\x82\x82|\x80\x80{\x80}}\x81\x82'
>>> 

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

dpgeorge commented 2 months ago

I guess this could be an issue in the CODAL layer

I can't see any reason why a rate of 142 is different to a rate of 143 on the MicroPython side, so, yes, this looks like an issue in CODAL.

microbit-carlos commented 2 months ago

I'll try to create an example to replicate in CODAL and raise this issue there.

microbit-carlos commented 2 months ago

Raised in CODAL: