Skip to content

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

Closed
@microbit-carlos

Description

@microbit-carlos

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)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions