This module allows you play sounds from a speaker attached to the Microbit. In order to use the audio module you will need to provide a sound source.
A sound source is an iterable (sequence, like list or tuple, or a generator) of frames, each of 32 samples. The audio
modules plays samples at the rate of 7812.5 samples per second, which means that it can reproduce frequencies up to 3.9kHz.
You will need a sound source, as input to the play
function. You can generate your own, like in examples/waveforms.py
or you can use the sound sources provided by modules like synth
.
Note
You don't need to understand this section to use the audio
module. It is just here in case you wanted to know how it works.
The audio
module consumes samples at 7812.5 kHz, and uses linear interpolation to output a PWM signal at 32.5 kHz, which gives tolerable sound quality.
The function play
fully copies all data from each AudioFrame
before it calls next()
for the next frame, so a sound source can use the same AudioFrame
repeatedly.
The audio
module has an internal 64 sample buffer from which it reads samples. When reading reaches the start or the mid-point of the buffer, it triggers a callback to fetch the next AudioFrame
which is then copied into the buffer. This means that a sound source has under 4ms to compute the next AudioFrame
, and for reliable operation needs to take less 2ms (which is 32000 cycles, so should be plenty).