Browser patch editor for Pure Data (closes #1)

Open a .pd file, drag boxes and pull cords, save it back as a file Pd
will load. Dragging is Dragology: `d.vary` moves a box, and a cord drag
is `d.closest(...).whenFar(vary)` over every inlet in the patch, so the
plug snaps to an inlet and cancels on empty space.

The parser is a byte-exact round trip. Two Pd format details that matter:
`#X connect` indices count comments, and `;;` (an empty record) is not the
same as a blank line. Anything unmodelled — arrays, `#X declare`,
subpatches — is preserved verbatim.

Tests run real Pd: patches round trip, an edited patch loads in
`pd -nogui` without new complaints, and the bridge carries a message into
a running Pd and back.

The bridge writes to server/patches/ and runs `pd -nogui` itself, so
saving and hearing the patch is one button.
This commit is contained in:
2026-11-04 19:19:53 -08:00
commit fe6f2bfe76
24 changed files with 3920 additions and 0 deletions
+110
View File
@@ -0,0 +1,110 @@
# dragology-pd
A browser patch editor for [Pure Data](https://puredata.info/), built on
[Dragology](https://declarative-dragging.github.io/).
Drag boxes around, pull cords out of outlets, and save the result as a `.pd`
file that Pd itself will open. Optionally run it: a small WebSocket bridge
starts `pd -nogui` and streams its stdout back into the page, so you hear what
you just wired.
```
browser ──WebSocket──▶ bridge ──UDP/FUDI──▶ pd
▲ │
└──────UDP/FUDI──────┘
```
## Running it
```sh
pnpm install
pnpm dev # http://localhost:5174
pnpm bridge # optional, in a second terminal
```
Then click **Connect** in the sidebar, hit **Save & run in pd**, and the patch
is written to `server/patches/` and opened with `pd -nogui`. Pd's output —
including "couldn't create" errors — appears in the sidebar.
`pnpm bridge --silent` passes `-nosound` to Pd, which is handy when you want to
check that a patch loads without waking the neighbours.
## What works
- **Open / save `.pd` files.** Parsing and serialisation are byte-exact round
trips, so opening a patch and saving it unchanged produces the same file.
Anything the editor does not model — arrays, `#X declare`, subpatches —
survives untouched.
- **Drag boxes.** Positions update live and snap to the grid you pick.
- **Pull cords.** Grab an outlet nub and drag: the plug follows the pointer,
and snaps onto an inlet when you get close. Drop in empty space to cancel.
- **Add and delete objects.** Type the object text, click *Add object*, then
click the canvas. Select and press Delete to remove an object and its cords.
- **Warn about broken files.** Records Pd will reject are kept verbatim and
flagged in the toolbar instead of being quietly dropped.
Not there yet: editing an object's text in place, undo, subpatch navigation,
and re-patching an existing cord by its destination end.
## How the dragging works
Three Dragology behaviours do the work, in `src/editor/PatchCanvas.tsx`:
| Interaction | Spec |
|---|---|
| Move a box | `d.vary(state, [param("nodes", id, "x"), param("nodes", id, "y")])` |
| Pull a cord | `d.closest(inletBranches).whenFar(d.vary(state, [param("drag", "x"), param("drag", "y")]), { gap: 34 })` |
| Drop | `.onDrop(commitDrag)` — turns the drag's snap target into a real cord |
The cord drag is the interesting one. Every inlet in the patch becomes a
candidate state in which the plug sits on that inlet and records it; `closest`
picks the branch by how near the pointer is, and `whenFar` hands control back
to a free-floating `vary` when no inlet is close. The drop reads `drag.snap`
and builds the cord, so no half-made connections ever reach the document.
## Layout
```
src/pd/ .pd parsing, serialisation, box geometry
src/editor/ Dragology draggable, pure state operations
src/bridge/ WebSocket client
server/bridge.mjs FUDI <-> WebSocket bridge, pd process runner
test/ parser round trips, pd integration, bridge loop
```
## Tests
```sh
pnpm test
```
15 tests. The interesting ones run real Pd:
- every patch in `~/Arduino/sketches/uno-q-audio-synth/pd` round trips byte
for byte;
- a patch edited by the app (moved box, new cord, new object) loads in
`pd -nogui` without producing a complaint the original did not;
- the bridge carries a message from a WebSocket client into a running Pd and
receives one back on UDP.
## Notes on the Pd format
Two things cost time and are worth writing down:
- **Comments occupy an index.** `#X connect` numbers refer to the object list
*including* comments, so a cord that skips them is rejected with
`(text->osc~) connection failed`. Verified against Pd 0.57.
- **`;;` is not a blank line.** Pd writes an empty record as `;;` on one line
and a section break as a blank line. Both are preserved.
## A bug in the sample patches
`synth.pd` (and its generator) writes
```
# DO NOT EDIT — generated by scripts/gen_synth_pd.py;
```
Pd treats a bare `#` record as an object and prints
`error: #: no such object`. A comment should be `#X text x y ...`. The editor
keeps the line and shows a warning rather than rewriting it for you.