Repository navigation
audiodev.worklet_audio: AudioWorklet output for Python in a Pyodide Web Worker - #209
Merged
Merged
Conversation
bdbarnett
force-pushed
the
ostinato/audio
branch
from
October 15, 2026 07:24
dc8b091 to
8b6b37d
Compare
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.
If your Python runs in a Pyodide Web Worker, it can't touch an
AudioContext, andweb_audiocan't help.audiodev.worklet_audiois the worker's half of an AudioWorklet output: the page owns the context and anAudioWorkletProcessor, hands your worker one end of aMessageChannel, and this module answers the worklet's requests for audio on it.Two ways to feed it:
The protocol. Audio moves in blocks of 256 frames of int16, interleaved for stereo. The worklet sends
{need, asked}; the worker replies once with{pcm: Int16Array, asked}, the array's buffer in the transfer list. The worklet may also send{offset}(rendered frame k is heard at context frame k + offset, exposed asout.offsetso you can place events at a sample) and{underrun}(summed inout.underruns). Each reply is one bulk copy into JS and onepostMessage, with no per-sample Python.Push mode can't block. The worklet's requests arrive on the worker's own thread, so waiting inside
write()would stop them.write()drops the oldest audio when the queue is full and counts it indropped;try_write()takes only what fits;awrite()andadrain()wait by yielding to the event loop. Requests drain the queue and pad with silence when it runs short, counted instarved.latencyandqueue_mssize the queue (500 ms by default, 100 ms for"low", never under eight blocks).audiodev.autocan't tell a worker from a page, so it never selects this; import it yourself. It ships in pydevices-desktop with the other browser backends, and the protocol is documented inlib/audiodev/README.md.Tests.
tests/test_worklet_audio.pyruns it against a fakeMessagePortand fake JS types: pull mode, short and long sources, a failing source still getting a reply, stereo interleave, offset and underrun tracking, the queue in order, starvation padding, latency sizing,try_write, drop-oldest,awrite/adrainwaiting, and that each reply transfers its own buffer. It was also run in real Pyodide under node over a realMessageChannel: replies arrived asInt16Arrays of the right length, the sender's buffer was detached (transferred, not copied), and offset, underruns and starvation were tracked.