Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.py[cod]
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `midi-drums/` — device-access layer for MIDI drum controllers. Scans for input devices through the core `window.feedBack.midiInput` capability, opens several at once, merges their note-ons into one stream tagged with `logicalSourceKey`, and exposes `window.midiDrumsDevices` (`getState`, `subscribe`, `discover`, `setEnabled`, `setActive`, `onHit`, `dispose`). Saved device keys persist server-side through `/api/plugins/midi-drums/settings`. Degrades to a no-op on a Host without the domain and retries a denied permission request rather than latching it.
- `nav` field in `my-plugin/plugin.json` (`{ "label": "My Plugin", "screen": "plugin-my-plugin" }`) so the plugin registers a sidebar entry.
- `AGENTS.md` — reference guide for AI assistants/contributors covering the actual API shape, plugin conventions, known code notes, and a verification checklist.

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

A reference template for creating [feedBack plugins](https://github.com/got-feedBack/feedback-plugin-spec).

## Plugins in this repository

| Directory | Purpose |
|---|---|
| `my-plugin/` | The reference template described below. Copy and rename it — it is not used as-is. |
| `midi-drums/` | Device-access layer for MIDI drum controllers. Opens several input devices at once and merges their note-ons into one tagged stream, via the Host's `window.feedBack.midiInput` capability rather than the Web MIDI API. See [`midi-drums/README.md`](midi-drums/README.md). |

Everything below this section documents `my-plugin/` specifically.

## ⚠️ DISCLAIMER AND WARRANTY

**This template is provided "AS-IS" without any warranty of any kind, express or implied.** No warranty is given that this template is correct, complete, safe, or fit for any particular purpose.
Expand Down
151 changes: 151 additions & 0 deletions midi-drums/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# midi-drums

Device-access layer for MIDI drum controllers. Scans for input devices, opens
several at once, and merges their note-ons into a single tagged stream — the
foundation the kit-profile work builds on.

This plugin never touches the Web MIDI API. It goes through the Host's
capability layer, `window.feedBack.midiInput`, and is written to work when that
domain is absent or mid-version.

## Files

| File | Purpose |
|---|---|
| `plugin.json` | Manifest — capability declaration, `type: "input"`, nav entry, no settings panel |
| `screen.html` | Device list, rescan control, saved-device status, bounded hit log |
| `screen.js` | Device layer (`window.midiDrumsDevices`) + screen lifecycle |
| `routes.py` | Server-persisted selection (`source_keys`) at `/api/plugins/midi-drums/settings` |
| `assets/plugin.css` | Styling scoped to the plugin |

## How it talks to MIDI

`plugin.json` asks for a narrow slice of the domain:

```json
"capabilities": {
"midiInput": {
"roles": ["requester"],
"requests": ["discover", "list-sources", "select-source", "open-source", "close-source"],
"mode": "active",
"compatibility": "degrade-noop",
"ownership": "requester-only",
"safety": "sensitive",
"version": 1
}
}
```

- **`mode: "active"`** — the screen calls `discover()` only once it is the
active screen, so the permission prompt never appears while the user is on
some other part of the Host.
- **`compatibility: "degrade-noop"`** — an older Host without the domain still
loads the plugin. The screen explains the situation instead of failing.
- **`ownership: "requester-only"`** — the plugin never asserts itself as a MIDI
provider, so it cannot collide with core `input_setup`.
- **`safety: "sensitive"`** — device access is a deliberate user act, reached
through an explicit tick per device.

`availability` is gated on `mi && mi.version === 1`, not on
`navigator.requestMIDIAccess`. A version mismatch, a late-loading domain, and a
genuinely unavailable Host all read as "not available" rather than as a crash.

## The `window.midiDrumsDevices` API

```js
const devices = window.midiDrumsDevices;

const state = devices.getState();
// { available, ready, discovered, active, sources, savedKeys, openKeys,
// status: 'idle'|'scanning'|'ready'|'empty'|'denied'|'unavailable'|'failed',
// reason }

const stop = devices.subscribe((state) => { /* re-render */ });
stop(); // unsubscribe

await devices.discover({ force: true }); // retry after denial
devices.setEnabled('web-midi::pads', true);
devices.setActive(false); // close everything, no rediscovery
devices.dispose(); // release listeners + bus handles

const offHits = devices.onHit((hit) => {
// hit.logicalSourceKey, hit.note, hit.velocity, hit.channel (0-based)
});
offHits();
```

`subscribe` fires immediately with the current state. `onHit` is the merged
stream across every open source; each hit carries the `logicalSourceKey` it came
from, so a consumer never has to track connections itself.

## Device identity

Sources are keyed by `logicalSourceKey` (`web-midi::<stable-id>`), never by a
label or list index. Labels come from user-supplied hardware and can change or
be duplicated between connections; keys do not. The selected keys are persisted
in `routes.py`, so a selection survives reloads.

## Behaviour worth knowing

- **Replug, no fallback-switching.** A ticked device that is unplugged keeps its
tick, the screen says it will reopen automatically, and a different device is
*never* substituted in its place. A stale or renamed device must not silently
become someone else's input.
- **Denial is retryable.** `discover()` is the permission boundary, so a denied
result is recorded but never latched — the rescan button re-runs it.
- **Per-source serialisation.** The domain keeps one shared session per source,
refcounted by requester name. Two concurrent opens add a single reference,
while two closes would remove it twice and tear the session out from under
whichever open survived. Every open and close for a source therefore runs
through a promise chain (`chains` in `screen.js`), and a superseded open
releases the reference it just took. A redundant queued open short-circuits,
which is what keeps one listener per source.
- **Opens never outrun the screen.** `active` is re-checked where the open
actually happens, not just before the loop that started it. A user who
navigates away while a tick's POST is still in flight would otherwise leave a
session that `closeAll()` had already walked past.
- **Generation guard.** Each source carries a generation counter, bumped on
every close. An `open()` that resolves against a retired generation is
discarded rather than resurrecting a session the user has already cancelled.
- **Everything closes.** Sessions are released on untick, unplug, `screen:
changing`, `pagehide`, and view disposal. `screen:changed` and `pageshow`
reopen the saved selection. `screen:changing` is best-effort rather than a
spec guarantee; `pagehide` is the guaranteed backstop.
- **Late Host wiring.** `window.feedBack` and the `midiInput` domain can both
appear after this script runs. The bus subscription is retried on every
activation, including no-op transitions, so a late bus cannot silently kill
replug recovery for the session.
- **Only declared verbs.** The plugin calls the five verbs in `plugin.json` and
nothing else. `getSelected()` is treated as an optional optimisation: absent
or throwing is normal, since a Host may implement only what is declared.
- **Device labels use `textContent`.** A MIDI device name is user-controlled
hardware text and is never parsed as markup.
- **Bounded work.** At most 8 sources and 200 characters per key
(`MAX_SOURCES` / `MAX_SOURCE_KEY_LEN`, matching `routes.py`), so an over-long
key can't turn every save into a rejected POST; the hit log keeps its newest
12 rows and its own running total.
- **Settings failures degrade.** A failed load leaves discovery intact; a failed
save keeps the session and the in-memory selection working. Saves are
chained, so overlapping POSTs can't roll the file back to an older selection.
- **A hand-edited config can't wipe a selection.** POST validates strictly, but
a file containing one bad entry is salvaged key-by-key on read rather than
discarded wholesale, and writes go through a temp file + rename so a torn
write cannot produce the corrupt JSON that recovery would then treat as empty.

## Verification

The repository has no build, lint, or test tooling, so verification was done
with throwaway harnesses outside the tree (jsdom + a fake MIDI domain, and
FastAPI + `httpx` against `routes.py`). Behaviour covered: fail-soft on an
absent domain, late domain arrival, denial then retry, two simultaneous sources
with tagged/merged hits, note-on filtering (note-off, zero-velocity strikes and
other channel traffic ignored), replug without fallback-switching, close and
reopen across screen changes, no discovery while inactive, rescan picking up a
later device, idempotent re-hydration, the source cap and its visible rejection,
the bounded hit log, settings-load/save failure, navigating away mid-save, a
Host without `getSelected()`, and a bus that only appears after the script runs.

For a manual pass, follow the checklist in the repository `AGENTS.md` against a
real Host build with a device attached: the plugin loads, the nav entry appears,
ticking a device opens it, strikes show in the hit log, and navigating away
closes the session.
168 changes: 168 additions & 0 deletions midi-drums/assets/plugin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
* Styles for midi-drums.
*
* Every selector is scoped under `.midi-drums`, the plugin's own root class,
* so nothing leaks into the rest of the app (spec §6.3, §6.6). The Host applies
* these styles only while the plugin's screen is active.
*/

.midi-drums {
display: flex;
flex-direction: column;
gap: 1.25rem;
padding: 1.5rem;
max-width: 720px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, sans-serif;
}

.midi-drums__header {
display: flex;
flex-direction: column;
gap: 0.35rem;
}

.midi-drums__title {
margin: 0;
font-size: 1.4rem;
font-weight: 600;
line-height: 1.2;
}

.midi-drums__lede {
margin: 0;
font-size: 0.95rem;
opacity: 0.75;
}

.midi-drums__panel {
display: flex;
flex-direction: column;
gap: 0.75rem;
padding: 1rem;
border: 1px solid rgba(128, 128, 128, 0.3);
border-radius: 8px;
}

.midi-drums__panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}

.midi-drums__panel-title {
margin: 0;
font-size: 1rem;
font-weight: 600;
}

.midi-drums__counter {
font-size: 1rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
}

.midi-drums__hint {
margin: 0;
font-size: 0.85rem;
opacity: 0.7;
line-height: 1.45;
}

.midi-drums__empty {
margin: 0;
font-size: 0.9rem;
opacity: 0.6;
font-style: italic;
}

.midi-drums__saved {
margin: 0;
font-size: 0.8rem;
opacity: 0.65;
}

.midi-drums__devices,
.midi-drums__hits {
margin: 0;
padding: 0;
list-style: none;
display: flex;
flex-direction: column;
gap: 0.35rem;
}

.midi-drums__device {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding: 0.45rem 0.6rem;
border: 1px solid rgba(128, 128, 128, 0.25);
border-radius: 6px;
}

.midi-drums__device label {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.95rem;
cursor: pointer;
min-width: 0;
}

.midi-drums__device-box {
width: 1em;
height: 1em;
flex: none;
cursor: pointer;
}

.midi-drums__device-label {
overflow-wrap: anywhere;
}

.midi-drums__device-meta {
flex: none;
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.04em;
opacity: 0.6;
font-variant-numeric: tabular-nums;
}

.midi-drums__hit {
padding: 0.3rem 0.6rem;
border-left: 3px solid rgba(128, 128, 128, 0.5);
font-size: 0.85rem;
font-variant-numeric: tabular-nums;
}

.midi-drums__button {
padding: 0.35rem 0.9rem;
font: inherit;
font-size: 0.85rem;
color: inherit;
background: transparent;
border: 1px solid currentColor;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}

.midi-drums__button:hover {
background-color: rgba(128, 128, 128, 0.12);
}

@media (prefers-color-scheme: dark) {
.midi-drums__device,
.midi-drums__panel {
border-color: rgba(255, 255, 255, 0.12);
}

.midi-drums__hit {
border-left-color: rgba(255, 255, 255, 0.35);
}
}
32 changes: 32 additions & 0 deletions midi-drums/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"id": "midi-drums",
"name": "MIDI Drums",
"version": "0.1.0",
"description": "Reads drum triggers from MIDI devices through the core midi-input domain, so calibration and scoring work on any keyboard, pad controller or e-kit module.",
"type": "input",
"nav": { "label": "MIDI Drums", "screen": "plugin-midi-drums" },
"screen": "screen.html",
"script": "screen.js",
"scriptType": "classic",
"styles": "assets/plugin.css",
"routes": "routes.py",
"category": "practice",
"standards": [
"capability-pipelines.v1",
"plugin-runtime-idempotent.v1"
],
"capabilities": {
"midi-input": {
"roles": ["requester"],
"requests": ["discover", "list-sources", "select-source", "open-source", "close-source"],
"mode": "active",
"compatibility": "degrade-noop",
"ownership": "requester-only",
"safety": "sensitive",
"version": 1,
"description": "Picks the drum-trigger MIDI devices through the core midi-input domain (the Web-MIDI provider ships built-in with the domain). Absent domain means no MIDI devices; the plugin fails soft."
}
},
"icon": "🥁",
"minHost": "1.0.0"
}
Loading