Repository navigation
midiports: Web MIDI ports for Pyodide - #208
Merged
Merged
Conversation
This was referenced Oct 15, 2026
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.
Adds
midiports.webmidi, so Python running in Pyodide can giveadafruit_midi.MIDIits ports through Web MIDI.Web MIDI only exists on a page's main thread, so there are two ways in:
midi_in, midi_out = await webmidi.open(name="", sysex=False). The name matches part of a port's name, ignoring case; the default merges every input and sends to the first output. Devices plugged in later, or unplugged and plugged back in, are picked up while the ports are open.webmidi.ports(access, name="")does the same for code that already holds aMIDIAccess.midi_in, midi_out = webmidi.relay_ports(port)over aMessagePortthat the page feeds. Page to worker is{data: Uint8Array, timeStamp: number}, one whole message per post; worker to page is{data: Uint8Array}.Inputs are a byte ring, so an empty poll allocates nothing and
any()is cheap. They also keep each message's arrival time: callmidi_in.take_timestamp()once for each messagereceive()returns and you get that message's time in milliseconds on theperformance.now()clock. Outputs send whole messages only, holding a partial write until it's finished, since Web MIDI'ssend()refuses part of a message.The README gains a row in the transport table and an "In a browser" section.
Testing: both halves are in
tests/midiports_contract.pyover a fakeMIDIAccessand a fakeMessagePort(200 of 200 notes each way,any()first, timestamps paired withreceive()), passing on CPython and unix MicroPython.tests/test_midiports.pyadds unit tests for the ring, overflow, timestamp pairing, hot-plug reattach, closing,open()and relay posting. The module was also run inside Pyodide 0.27.7 under Node against a JavaScript fakeMIDIAccessand a realMessageChannel. It hasn't been tried against a real device in a browser yet.