Skip to content

Keep buffered audio working after a soft reboot - #148

Merged
microbit-matt-hillsdon merged 1 commit into
mainfrom
audio-soft-reboot
Nov 12, 2026
Merged

microbit-matt-hillsdon merged 1 commit into
mainfrom
audio-soft-reboot

Conversation

@microbit-matt-hillsdon

Copy link
Copy Markdown
Contributor

After Ctrl-D, audio.play of a sound expression or AudioFrames, and speech, played one buffer and then hung without returning to the REPL. Music was unaffected as it uses the oscillator.

Cause

A soft reboot stops the board's components via microbit_hal_deinit, and stopping audio replaced each BufferedAudio's callback with a no-op. The callbacks are only connected in createModule(), which a soft reboot doesn't repeat, so nothing asked for the next buffer. For sound expressions, isSoundExpressionActive() then stayed true for ever.

The no-op came from #99, which made the AudioContext live for the whole page, so that buffers still scheduled couldn't call into a stopped module. Since 8b4381b stop() cancels the scheduled buffers and clears their onended handlers, which covers that case without disconnecting the callback.

Change

boardStopped() now stops the buffered audio and leaves the callbacks connected. dispose() is removed.

Tests

  • Each kind of buffered audio plays to the end after a soft reboot. These failed before the fix.
  • Stopping the program while each kind is playing, then running another program. Only music had this before. These pass with and without the fix, and are there to guard what Fix Safari AudioContext initialisation #99 was protecting.

Closes #112

After Ctrl-D, audio.play of a sound expression or AudioFrames, and
speech, played one buffer and then hung without returning to the REPL.

A soft reboot stops the board's components via microbit_hal_deinit, and
stopping audio replaced each BufferedAudio's callback with a no-op. The
callbacks are only connected when the module is created, which a soft
reboot doesn't repeat, so nothing asked for the next buffer. For sound
expressions the expression then stayed active for ever.

The no-op was added in a3e92f7 so that buffers still scheduled on the
now long-lived AudioContext couldn't call into a stopped module. Since
8b4381b stop() cancels the scheduled buffers and their onended handlers,
which covers that without disconnecting the callback.

Add tests for each kind of buffered audio after a soft reboot, and for
stopping the program while each is playing, which only music had.

Closes #112
@github-actions

Copy link
Copy Markdown

@microbit-robert microbit-robert left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@microbit-matt-hillsdon
microbit-matt-hillsdon merged commit c4b7451 into main Nov 12, 2026
2 checks passed
@microbit-matt-hillsdon
microbit-matt-hillsdon deleted the audio-soft-reboot branch November 12, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

audio.play examples don't work after Ctrl-C, Ctrl-D soft reboot

2 participants