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

`audio.stop()` and `microphone.stop_recording()` don't work #180

Closed microbit-carlos closed 2 months ago

microbit-carlos commented 3 months ago

Tested with v2.1.2 and the lates recording&playback branch.

audio.stop doesn't work in this example:

from microbit import *

while True:
    if button_a.is_pressed():
        audio.play(Sound.HAPPY, wait=False)
        # We can see here that the image is shown as soon as the sound starts playing
        display.show(Image.HAPPY)
        # But the sound plays to completion
        audio.stop()
        sleep(200)
    sleep(100)
    display.clear()

Simpler example in REPL (the next prompt >>> appears before the sound naturally ends playing):

>>> audio.play(Sound.HAPPY, wait=False); audio.stop()
>>> audio.play(Sound.HAPPY, wait=False); sleep(10); audio.stop()
>>> 

Replacing the play call with audio.play(audio.SoundEffect(duration=3000), wait=False) doesn't work either.

dpgeorge commented 2 months ago

This is not related to the audio-recording branch, it's a bug in the master branch.

Now fixed on master by c143b24acf26fdebc6eb965c625430521d4a0c21

microbit-carlos commented 2 months ago

Sorry, I forgot to mention that microphone.stop_recording() didn't work either:

>>> a = microphone.record(4000)
>>> # Recording plays for 4 seconds instead of 1
>>> audio.play(a, wait=False); sleep(1000); microphone.stop_recording()
>>> 
dpgeorge commented 2 months ago

I'm not sure what's wrong with the above code. It should play for 4 seconds. The microphone.record() is synchronous and records for 4 seconds, then the audio.play() will play the full sample, because microphone.stop_recording() has no effect on audio playback.

microbit-carlos commented 2 months ago

Oh yes, you are right, brain fart from my side!