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

AudioFrame doesn't accept negative indexes or slices #188

Open microbit-carlos opened 2 months ago

microbit-carlos commented 2 months ago

Tested with v2.1.2 and the latest version (at the time of writing) of the recording & playback branch: https://github.com/microbit-foundation/micropython-microbit-v2/commit/0b06914c71c18533da90df85230ac198578669bf

We can use positive array indexes to access and modify the data inside an AudioFrame, but not a negative index to access.

>>> af = audio.AudioFrame()
>>> af[len(af) - 1] = 123
>>> af[-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: index out of bounds
>>> af[1:]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert slice to int
>>> af[1:2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert slice to int
>>> 

Technically we could use a memoryview for this, but it's not a well know feature, so even if we documented well and add it to the examples it might not be used as much. We also have issue https://github.com/microbit-foundation/micropython-microbit-v2/issues/187 released to memoryview.

If we wanted to enabled this for AudioFrame we should probably enable it for V1 as well. Is the performance/size penalty for this significant?

microbit-carlos commented 2 months ago

This will be covered as well with this new proposal: