Open microbit-carlos opened 3 months ago
The exact time taken by audio.play()
is somewhat set by CODAL and the way it queues data for playback, via timing of the pullRequest()
/pull()
methods.
I suspect the following:
audio.play()
call is always less than the above speaker output time, because the method returns once the last few frames are queued.If you play increasingly large audio frames, then the time for audio.play()
also increases in proportion to the length of the audio frame.
Based on this:
The CODAL isPlaying()
method should indicate real sound playback, but when calling audio.is_playing()
right after it does indicate no more sound should be playing:
>>> t = running_time(); audio.play(audioframe_50_ms); audio.is_playing(); running_time() - t
False
44
Do you think this is an issue in the CODAL side?
I'll try replicate this on the CODAL side first as well.
Note that MicroPython doesn't use uBit.audio.isPlaying()
. Instead we track whether there's any more data to go out to CODAL. If not, then the audio is "finished".
Ah, thanks Damien. The reason we looked into implementing uBit.audio.isPlaying()
is that we found situations were the audio playback ends up triggering the microphone, for example in programmes like this one:
from microbit import *
while True:
audio.play(Sound.HELLO)
# Wait until we hear something loud to do something else
while microphone.sound_level() < 60:
pass
# At this point audio.play() has triggered the microphone sound level
sleep(100)
Using the latest version (at the time of writing) of the recording & playback branch: https://github.com/microbit-foundation/micropython-microbit-v2/commit/0b06914c71c18533da90df85230ac198578669bf Hex: https://github.com/microbit-foundation/micropython-microbit-v2/actions/runs/8416237764?pr=163
Playing a 50 ms AudioFrame takes less time than expected:
Using time.ticks_ms() instead: