microbit-foundation / micropython-microbit-v2

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

A way to create an AudioFrame of a specific size #193

Closed microbit-carlos closed 2 months ago

microbit-carlos commented 5 months ago

When working on an example to send recorded data via radio like walkie-talkie, one idea is to record AudioFrames of the same size than the packet size for radio.

With the current API is not straightforward at all to work out how to create an AudioFrame of say 64 bytes. We need to use a sample rate that is easily divisible by the packet size, and a packet size that is a multiple of 32 bytes. Initial attempts using the default rate (which is not a multiple of 32 bytes either) ended up with floats as the duration, which doesn't work as duration has to be an integer. So you end up setting a non-default rate.

We originally discarded a size arguments (AudioFrame(size, duration, rate)), as size is directly related to the other two parameters, so providing all three options would have to raise an error or similar.

We could replace the duration parameter from AudioFrame, but still have it for microphone.record(duration, rate), as that is the simplest way to create an audio sample. In this case we would end up with code like this:

DURATION = 4000
RATE = 7812
af1 = audio.AudioFrame(size=DURATION*RATE, rate=RATE)
af2 = microphone.record(DURATION)

Not too sure what other simple options we might have, or if there is a better way to implement a size parameter. Any suggestions are very welcomed.


As a workaround, I've been creating AudioFrames with the size as the rate and 1s duration, but then we need to set the rate back to the value we'd like. For example, for 128 bytes:

af = AudioFrame(duration=1000, rate=128)
af.set_rate(7812)
microbit-carlos commented 5 months ago

Let's try AudioFrame(duration=None, *, rate=7812, size=None), where providing 3 of these arguments raises an exception.

microbit-carlos commented 2 months ago

A different class has been created that can be initialised from an external buffer, which can be set to a specific size in bytes, as described in: