T-046 — Keys from the editor: the keyboard plays in Play and selects in Edit, Audition, a note picker, keyboard zoom

Status: done (the maintainer’s check in a DAW pending) · Area: rust · Depends on: T-044, T-043

Read for that: Spec 003 (Keys played from the editor, Window, egui and audio), T-043 (Implementation: addresses per key), T-020 (D1 key marks, D5), T-045 (Outcome: width classes, D6), crates/piano/src/{audio.rs,view.rs,gui.rs,gui/keyboard.rs,gui/tests.rs,lib.rs}, crates/piano/tests/{host.rs,no_alloc.rs,note_capacity.rs}; the real APIs: clack-plugin-0.2.0/src/host.rs (request_process, request_callback), clack-common-0.2.0/src/events/pckn.rs (Pckn, Match).

Why: the keyboard has been display-only since T-020 (D5), because notes from the GUI are a second event source with its own ownership rules. Without it, a note can only be heard through a MIDI track or controller, and T-047’s inspector needs a way to pick a note and hear it.

What

  1. Notes from the editor to the audio thread (D1): the editor writes which keys it holds into shared atomics; the audio thread reads them at the start of each block (sample offset 0) and turns them into strikes and releases. Nothing on the audio thread allocates or locks; the editor asks the host for request_process so a sleeping plugin wakes up.
  2. Ownership (D2): editor notes are a separate source in the adapter’s per-key address table (T-043). A key sounds while any source holds it and is released when the last one lets go. Host NoteOff/NoteChoke (including wildcard ones) and MIDI note-offs never end an editor note; the editor never ends a host note. All notes off (CC 120/123), stop_processing and reset end both, as now.
  3. Releasing what the editor holds (D3): pointer up anywhere; the pointer leaving the keyboard while a key is held; the window losing focus; switching tab or opening Details; hiding or closing the editor; the plugin deactivating.
  4. Play tab, keyboard plays (D4, D5): a press strikes the key under the pointer, black keys winning the overlap; dragging across keys releases the old key and strikes the new one (glissando). Velocity from where the key is hit (D5).
  5. Edit tab, keyboard selects (D6): a click selects a note without sound, drawn with its own outline (Spec 003: in Edit the selected note has its own outline); an Audition button plays the selected note while it is held down. The selection survives tab switches and closing the editor for the instance’s lifetime.
  6. Note picker (D6): a small field in the dock taking a note name (C4, C#4, Db4, A0 … C8, ASCII only like the rest of the parameter text) or a MIDI number (21–108); Enter selects it (in Edit) or selects and auditions it (in Play). Works without the painted keyboard, so it is also the keyboard-focus path.
  7. Keyboard zoom (D7): the keyboard fits the width by default (T-045 D6); zoom buttons show fewer octaves with larger keys and a horizontal scroll bar.

Out of scope: playing from the computer keyboard (QWERTY), pedals from the editor (the dock’s pedal slots stay display only), note output to the host (the plugin has no note output port), MPE or per-note expression from the GUI, the inspector itself (T-047).

Decisions (accepted by the maintainer, 2026-12-04)

QuestionOptionsProposal
D1Path from the editor to the audio thread(a) per-key atomics: for each of the 88 keys an AtomicU32 [strike count u16, velocity u8, held bit], plus a 128-bit dirty set ([AtomicU64; 2]) the audio thread swaps to zero each block and scans only the set bits; a changed strike count is a strike (followed by a release if the held bit is already clear: a quick tap within one block is not lost); a cleared held bit is a release. The editor calls request_process after a change; (b) a fixed-size SPSC ring of note events (e.g. 64 entries of packed atomics); a full ring drops the event; (c) a mutex-guarded Vec taken with try_lock(a): lossless for the held state (a release can never be dropped, so no stuck notes), bounded and allocation-free, at most two swaps and a few loads per block. Several taps on one key within a block collapse to one strike, which a mouse cannot produce audibly anyway. (b) can lose a release under load; (c) is a lock on the audio thread
D2Ownership of editor notes(a) the adapter’s NoteAddress gets a source (Host / Editor); editor notes take one extra slot per key (33 in all, so T-043’s 32 host addresses stay); host note-offs and chokes match host addresses only; the engine gets NoteOff when no slot of either source holds the key; (b) editor notes as host-like addresses on a reserved port number, so wildcard host note-offs would end them too(a): Spec 003 says external MIDI stays independent, and a host’s wildcard note-off (port and key All, e.g. on transport stop) should not cut a note the user is holding with the mouse; the panic paths still end everything. The extra slot keeps T-043’s capacity rule and its test unchanged
D3When the editor lets go(a) pointer up anywhere; the pointer leaving the keyboard during a press; window focus lost; tab change or Details opened; editor hidden or closed (drop); on activate the editor’s keys start released; (b) as (a) but a note keeps sounding while the pointer is dragged outside the keyboard until pointer up(a): a held note always has a visible cause (the pointer on the key); (b) is what Spec 003’s “released when a gesture ends outside the dock” literally allows, but invites notes that hang while the user drags toward a slider
D4Hit test(a) black keys win where they overlap, using the drawn rectangles (one geometry function shared by painting and hit testing); dock labels and the legend are not keys(a): the Spec’s rule; sharing the geometry means a zoomed or resized keyboard needs no second code path
D5Velocity of an editor strike(a) from the hit position along the key: near the back of the key soft (MIDI 40), at the front edge loud (MIDI 120), linear in between; then shaped by Velocity curve and Hardest touch like host notes; (b) a fixed velocity (100); (c) a Velocity field in the dock (default 100)(a): common for plugin keyboards, needs no extra control, and the shaping parameters apply as for MIDI. Audition and the note picker use a fixed MIDI 100, shaped too
D6Selection and Audition(a) one selected note per instance (AtomicU8 in PianoShared, default C4 = MIDI 60), not in the state or presets; Edit: click selects, Audition plays while pressed (pointer or Space/Enter on the focused button); Play: click plays, the picker’s Enter selects and plays a 400 ms strike; (b) selection per editor opening (lost on close); (c) selection kept in EDIT (per project)(a): survives tab switches and reopening the editor as the Spec asks, and T-047 can read it; storing it in the project is easy to add later (an EDIT v3 byte) if it turns out to matter
D7Keyboard zoom(a) three zoom steps in the dock: all 88 keys (fit, default), 4 octaves, 2 octaves; zoomed views scroll horizontally (scroll bar, wheel with Shift, and the view follows the selected note when it changes from the picker); zoom kept per editor opening, not in state; (b) a minimum white-key width (e.g. 14 points) below which the keyboard scrolls, no buttons; (c) no zoom(a): T-045 left zoom to this task; three steps are simple to test and cover the narrow window (9-point keys) and touch-sized targets; (b) changes behaviour with window width, which T-045 kept out of the keyboard

Done when

  • The gates of AGENTS.md pass, including bundle, clap-validator and the GUI smoke test.
  • Tests (written first):
    • The note table (moved out of PianoAudioProcessor into a pure type so it can be tested without a host): an editor note and a host note on one key — releasing either leaves the key held, releasing both sends one NoteOff; a wildcard host NoteOff and NoteChoke leave an editor note held; CC 123 ends both; 32 host addresses plus the editor’s note fit, a 33rd host address is ignored.
    • The editor path (D1): strike, release, a tap within one block (strike then release), repeated strikes, the dirty set cleared after a block; filtered keys (MTS-ESP, Scala x) ignore editor strikes like host ones.
    • Hit test (D4) at the black/white overlap, at the edges, at three zoom steps and three window widths; velocity from position (D5) at both ends and monotonic; the picker’s parser (names with #/b, numbers, out-of-range and garbage rejected).
    • egui frames with pointer events (like T-045’s grip test): press strikes, drag across keys moves the note, pointer leaving the keyboard / tab switch / focus loss release; in Edit a click selects without striking and Audition holds the note while pressed.
    • Host test (clack-host): an editor note sounds with no host events and stops after release; a sleeping processor is woken by the editor (request_process); a host wildcard note-off during an editor note leaves it sounding.
    • no_alloc: process with editor strikes and releases in flight.
  • Smoke test: a click on a key in Play produces sound in the rendered output (or, if the X11 test cannot render audio, the key shows held in the capture); screenshots in light and dark with a selected note in Edit and a zoomed keyboard, attached to the outcome.
  • The default sound is unchanged (golden test); host note handling is unchanged (existing host and note_capacity tests pass as they are).
  • The maintainer has played the keyboard in a DAW.

Outcome

  • Ownership (crates/piano/src/notes.rs, new): NoteTable is T-043’s per-key address table moved out of PianoAudioProcessor into a pure type, plus one editor slot per key (D2): host note-offs and chokes, wildcard ones included, never end an editor note; a key is released when its last holder lets go; clear (CC 120/123, stop_processing, reset) ends both. The host’s 32 addresses per key are unchanged (note_capacity passes as it was).
  • Editor → audio (D1): EditorNotes holds per key an AtomicU32 [strike count u16, velocity u8, held bit] and a 128-bit dirty set. press is a compare-exchange update (any non-audio thread may call it), release a fetch_and; the audio thread swaps the dirty words to zero at the start of each block and applies only the changed keys (drain), before the host’s events at offset 0. A new cursor on activate treats everything as released. Editor strikes go through Velocity curve, Hardest touch and the MTS-ESP/Scala note filters like host notes. Every change asks the host for request_process.
  • Release rules (D3): pointer up, the pointer leaving the keyboard, the window losing focus (the pointer then plays nothing until focus returns), a tab change or Details, hide (main thread, PianoMainThread::hide) and closing the editor (PianoGui::drop).
  • Dock (gui/keyboard.rs, rewritten): one Geometry paints the keys and hit-tests them (black keys first, D4); velocity(depth) maps the back of a key to MIDI 40 and the front edge to 120 (D5); a Player tracks which editor source (pointer, Audition, picker) holds which key; Dock keeps the zoom, the picker text and the selected note (an AtomicU8 in PianoShared, default C4, D6). The legend row carries the zoom pills (88, 4 oct, 2 oct), the picker field (Note, C4; names with #/b or MIDI 21–108, Enter; Not a key: on bad input) and, on Edit, Audition (held while pressed, or Space/Enter while focused). Edit outlines the selected key (ink frame with a white inner line, readable on white and black keys in both themes) and adds Selected to the legend. A zoomed keyboard scrolls horizontally and centres the selected note on a zoom change or a picker Enter. Octave labels: every C where an octave is at least 26 points wide, A0 only where it does not crowd C1, end labels kept inside the keyboard.
  • Fixed on the way: at 480 points the pedal row was wider than the window (visible in T-045’s narrow capture), which widened the dock and the keyboard; the pedal state words are now 12 pt like the legend and the keyboard takes the window’s width, not the panel’s. The dock panel has a minimum height (DOCK_HEIGHT, 146 points): a bottom panel sizes itself from its last frame, so the keyboard’s scroll area would otherwise start collapsed.
  • Follow-up (maintainer, 2026-12-04): each pedal’s state word sits in a box as wide as the widest word that pedal can show (Up/Down for Sustain, unsupported for Sostenuto and Soft until T-010), so Sustain switching between Up and Down no longer moves the Sostenuto and Soft slots (pedal_slots_stay_put_when_the_sustain_state_changes; the old layout moved them by 16 points).
  • Tests: notes::tests (ownership with host and editor on one key, wildcard note-off and choke, clear, 32 + editor capacity, strike/release/tap/re-strike through the dirty set, cursor on activate, velocity range, count wrap); tests/editor_notes.rs through clack-host and the testing::EditorKeys hook (an editor note equals a host note struck and released at offset 0, sample for sample; a wildcard host note-off leaves it held; the editor releasing a key the host holds changes nothing; a strike after the processor asked to sleep sounds; a key the master filters stays silent, an unfiltered one sounds); no_alloc with editor strikes, releases and a tap in flight; egui frame tests for the hit test at three zoom steps and three widths, velocity, the picker’s parser, Play (strike, glide, same-key move, leaving the keyboard, pointer up, tab switch, focus loss), Edit (select without sound, Audition, picker in Edit and Play with its 400 ms strike) and the keyboard’s fit at 480 × 360 up to 2560 × 1600 with the selection in view when zoomed.
  • Smoke test (tests/gui_x11.rs): after the chord, a real X11 pointer press on a white key of the drawn keyboard (found in the capture) makes the plugin sound with no host notes, and the release damps it (peak 0.37, tail 0.0013 after 1 s); with GUI_SMOKE_TAB=edit the same click stays silent. GUI_SMOKE_TAB and GUI_SMOKE_ZOOM set the starting view through testing::open_editor_on; the test waits until the keys are drawn and, when capturing, one more second (software OpenGL at scale 2 draws a frame in a few hundred ms).
  • Gates (2026-12-04): fmt, clippy, nextest (221 passed, 2 skipped), Julia Pkg.test(), cargo xtask bundle piano --release, clap-validator validate (38 passed, 0 failed, 6 skipped), the smoke test in light and dark, Play and Edit, zoom 88 / 4 oct / 2 oct, 480 × 360, 720 × 400, 1100 × 600, scale 1, 1.5 and 2. The default sound is unchanged (golden test; no DSP change).
  • Not checked: the host’s request_process wake-up (the in-process test host ignores it; the tests show that a strike after a Sleep status is picked up by the next process); Shift + wheel for horizontal scrolling (egui’s default handling, not tested); a real DAW, macOS, Windows.

Captures from the X11 smoke test (pedalled chord, then the editor click): Play at 720 × 400; Edit with the selected C4 at four and two octaves (light, dark); narrow Play; wide Edit in dark; Play at scale 2 with four octaves.