Repository navigation
Improve audio performance with better buffering, audio worklet - #149
Draft
microbit-matt-hillsdon wants to merge 4 commits into
Draft
microbit-matt-hillsdon wants to merge 4 commits into
microbit-matt-hillsdon wants to merge 4 commits into
Conversation
The firmware prepares one audio frame per pass of the scheduler and each frame is 4 ms. The wait loop sleeps 5 ms per pass in microbit_hal_idle, so frames were produced more slowly than they played and playback ran dry between them, heard as buzzing. Before sleeping, ask the firmware for frames until 100 ms is scheduled, the margin sound expressions already use. Under 8x CPU throttling 50 ms was not always enough to cover the main thread stalling as a program starts; mid-program 25 ms was. With that much scheduled: - a sound played after another follows on from it rather than starting straight away, which would overlap the end of the first; - audio.stop(), or replacing a sound, is heard up to 100 ms late as the firmware has no hook for it, and audio.play returns that much before the sound ends. Also start the first frame 20 ms ahead of the audio clock. The clock moves in steps that can be longer than a frame, so the first frame could be past its end before the second was written, leaving a gap. A program that plays without waiting and never sleeps doesn't reach microbit_hal_idle, so is unchanged.
The speech renderer hands over one buffer at a time, as an earlier one finishes playing. Its only margin was a quirk: a fresh start called back into the firmware while it was still writing, which wrote the first buffer twice. That is one buffer of slack, 6.7 ms in the default mode. Speech at the start of a program always dropped out, and each dropout restarted the same way, so also repeated a buffer. While the renderer waits for its buffer to be taken, take it straight away until 100 ms is scheduled, as for audio frames and sound expressions. Do the same there for audio frames, which covers programs that play without waiting and never sleep. Don't yield to the browser after writing a buffer, or the margin builds too slowly to be in place for the main thread stalling as a program starts. With that the fresh start no longer needs to call back, so the first buffer is played once. Speech gets the start delay audio frames have, as its buffers can also be shorter than a step of the audio clock. speech.say now returns up to 100 ms before the speech ends, rather than a buffer or two. The speech test sets mode=3. Programs rarely do, but its 3.4 ms buffers dropped out every time before, where the default mode only did at the start of a program, which the tests hold off until the recorder is ready.
Audio frames, speech and sound expressions were each scheduled as an AudioBufferSourceNode per buffer, timed against the audio clock from the main thread and refilled as each node ended. That needed care around the clock moving in steps, and stopping meant stopping every node. Queue the buffers to a worklet on the audio thread instead. It plays them back to back, resampling across the joins, and reports each as it ends. The main thread counts what it has written that hasn't ended, so it no longer reads the clock, and a stop is one message. The margin kept queued, the top-ups from the firmware and the 20 ms start delay for audio frames and speech are as before. The delay is still needed: without it, playback under 8x CPU throttling ran out between the first buffer and the second. The worklet is a second script, build/audio-worklet.js. Browsers only load worklets in a secure context; without one buffered audio is silent, though buffers still finish so that programs don't hang. Safari before 14.1 has no AudioWorklet and was already below the build target.
Audio frames and speech waited a fixed 20 ms after their first buffer before playing, to give the firmware time to produce the buffers that follow. That is longer than needed when they arrive promptly. Start as soon as 10 ms is queued, waiting at most the 20 ms. Unthrottled, sounds now start within 6 ms. Under 8x CPU throttling there were no dropouts starting at 5, 10 or 20 ms queued, where starting straight away dropped out in up to 4 runs in 10.
|
Preview build will be at |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft for now, more analysis and slow device testing to follow.
Some further improvements are possible with some simple HAL additions which I'll propose separately if this seems OK as-is or might want to roll-in if testing shows practical issues.
Closes #146