Skip to content

midi: a source going away lets go of its notes - #49

Open
mparrett wants to merge 2 commits into
nooga:masterfrom
mparrett:midi-release-on-source-loss
Open

mparrett wants to merge 2 commits into
nooga:masterfrom
mparrett:midi-release-on-source-loss

Conversation

@mparrett

@mparrett mparrett commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Slab releases held MIDI notes when a source disappears only if it was the last one (if (count == 0) allOff() in midi_in.rescan). With a second device connected, unplugging a keyboard mid-note, or quitting a program that sends through a virtual source, leaves its notes held: the source is gone, so their note-offs never come.

The live state doesn't record which source held which note, so a departed source now releases them all. Sources are compared by CoreMIDI's unique ID (slab_midi_source_id, sourcesNow), not by count, so a source replaced between two rescans counts as gone too: a program restarting its virtual source gets a new ID. A key still held on a device that stays connected is cut short as well; that is the cheaper failure than a note stuck until panic.

poll now drains the input ring before it rescans, so a note-on a source queued just before it went is released with the rest instead of arriving after the release.

Found by reading the code while testing per-channel MIDI input, which this is independent of.

Testing. Unit tests cover a source arriving (nothing released), one leaving while another remains, one replaced with the count unchanged, and a note-on queued ahead of the rescan (pollFrom with a fake queue). zig build test passes. Live, a program's virtual source gets a new unique ID each time it is recreated, which is what the ID comparison relies on.

🤖 Generated with Claude Code

Slab released held MIDI notes only when the last source disappeared. A
device unplugged, or a program quitting its virtual source, while others
stayed connected left its notes stuck: it can't send their note-offs.
The live state doesn't record which source held which note, so any
source leaving now releases them all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mparrett
mparrett marked this pull request as ready for review November 18, 2026 10:18
…nput)

Two holes in releasing a departed source's notes:

- poll rescanned before draining the ring, so a note-on the source queued
  just before it went was applied after the release and stuck. The ring
  now drains first (pollFrom, which tests drive with a queue of their own).
- The check compared source counts, so a source replaced between two
  rescans (a program restarting its virtual source) changed nothing.
  Rescans now compare CoreMIDI unique IDs (slab_midi_source_id); any ID
  gone releases held notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant