I make Pianory. AI helped draft this article; I checked the code paths and focused tests described here against the repository.
A MIDI keyboard can disappear while a note or sustain pedal is held. Treating that as only a device-list update leaves the input controller’s note state behind. The controller needs to release tracked state before it forgets the old port.
In Pianory’s Web MIDI input controller, active notes are tracked by channel and pitch, and sustain state by channel. When the selected input disappears during a state change, the controller detaches it with cleanup. The cleanup emits sustain-off events before note-offs, removes the message listener, and closes the port. This ordering gives downstream consumers explicit release events instead of relying on a future message from hardware that is no longer connected.
A focused hot-plug test checks that removing the selected input reports an empty device list, emits sustain-off then note-off, and ignores messages from that removed input afterward. Separate coverage checks MIDI All Sound Off, Reset Controllers, and All Notes Off.
The practical contract is: any path that stops listening to an input must settle the state that input created. Test removal with a held note and pedal, not only an idle port. The hot-plug test checks release events and ignores later messages from the removed input. Listener removal and port closure are visible in the source, but that test does not directly assert those calls. None of this proves that every physical audio device stops sounding.
Source: src/lib/input/midi.ts and src/lib/input/midi.spec.ts in the Pianory repository.
Top comments (0)