You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is that the generator is looping forever and never yielding. A simpler reproduction is:
defgen():
whileTrue:
passyieldaudio.play(gen())
This is really hard to fix, because the generator code is running in the background. In MicroPython, ctrl-C can only break out of code running in the foreground. It's assumed that you don't write nasty code that runs in the background!
If we allow ctrl-C to break out of the generator then foreground code may miss a ctrl-C. Eg if you have a well behaved generator then it may still catch ctrl-C:
defgen():
f=AudioFrame()
whileTrue:
yieldfaudio.play(gen())
# pressing ctrl-C might not stop this loop, it might instead stop the generatorwhileTrue:
pass
Adding a sleep(1) to the generator does work with Ctrl+C, is that because CODAL might be yielding to the MicroPython foreground?
Either way, I suspected it'd be something along these lines, so happy to close this as a "won't fix", just needed to check if there some something sensible that could be done to resolve it.
Adding a sleep(1) to the generator does work with Ctrl+C, is that because CODAL might be yielding to the MicroPython foreground?
The sleep() function explicitly checks for ctrl-C, so that's why it's being handled there. But using sleep() within an audio generator callback is definitely not recommended! That will significantly affect the scheduling of the entire system, not just audio.
so happy to close this as a "won't fix",
I think we should document audio generators as callbacks that happen in the background and that must return quickly, and make it clear they are a bit different to normal foreground Python code (ie can't use ctrl-C to stop them).
Activity
dpgeorge commentedon Apr 22, 2024
The problem is that the generator is looping forever and never yielding. A simpler reproduction is:
This is really hard to fix, because the generator code is running in the background. In MicroPython, ctrl-C can only break out of code running in the foreground. It's assumed that you don't write nasty code that runs in the background!
If we allow ctrl-C to break out of the generator then foreground code may miss a ctrl-C. Eg if you have a well behaved generator then it may still catch ctrl-C:
microbit-carlos commentedon Apr 24, 2024
Thanks Damien!
Adding a
sleep(1)to the generator does work with Ctrl+C, is that because CODAL might be yielding to the MicroPython foreground?Either way, I suspected it'd be something along these lines, so happy to close this as a "won't fix", just needed to check if there some something sensible that could be done to resolve it.
dpgeorge commentedon Apr 29, 2024
The
sleep()function explicitly checks for ctrl-C, so that's why it's being handled there. But usingsleep()within an audio generator callback is definitely not recommended! That will significantly affect the scheduling of the entire system, not just audio.I think we should document audio generators as callbacks that happen in the background and that must return quickly, and make it clear they are a bit different to normal foreground Python code (ie can't use ctrl-C to stop them).