@johnhenry/mallory-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -2
- package/bin/mallory.mjs +51 -0
- package/package.json +51 -4
- package/src/a2a/client.js +511 -0
- package/src/a2a/demo-agent.js +396 -0
- package/src/a2a/hub.js +578 -0
- package/src/a2a/settings.js +88 -0
- package/src/a2a/task.js +358 -0
- package/src/acp/client.js +573 -0
- package/src/acp/demo-agent.js +332 -0
- package/src/acp/hub.js +632 -0
- package/src/acp/settings.js +78 -0
- package/src/actions/map.js +524 -0
- package/src/agent/cloud.js +77 -0
- package/src/agent/compact.js +84 -0
- package/src/agent/constants.js +39 -0
- package/src/agent/diff.js +101 -0
- package/src/agent/fork.js +159 -0
- package/src/agent/grounding.js +73 -0
- package/src/agent/guards.js +129 -0
- package/src/agent/local-transport.js +152 -0
- package/src/agent/loop.js +479 -0
- package/src/agent/park-store.js +77 -0
- package/src/agent/park.js +76 -0
- package/src/agent/ports.js +28 -0
- package/src/agent/providers.js +591 -0
- package/src/agent/restart.js +51 -0
- package/src/agent/router.js +380 -0
- package/src/agent/run.js +861 -0
- package/src/agent/tools.js +247 -0
- package/src/agent-library/engine.js +43 -0
- package/src/ai/bridge.js +155 -0
- package/src/ai/policy.js +131 -0
- package/src/aimatey/vendor.js +18 -0
- package/src/audio/container-duration.js +35 -0
- package/src/audio/instruments.js +776 -0
- package/src/audio/lanes.js +230 -0
- package/src/audio/patch.js +1410 -0
- package/src/audio/plan.js +276 -0
- package/src/audio/rack.js +1006 -0
- package/src/audio/sample-trim.js +293 -0
- package/src/audio/soundfont.js +243 -0
- package/src/audio/take-store.js +87 -0
- package/src/audio/takes.js +129 -0
- package/src/audio/wav.js +170 -0
- package/src/audio/zzfx.js +333 -0
- package/src/autonomy/bundle.js +207 -0
- package/src/autonomy/confine.js +61 -0
- package/src/autonomy/enforce.js +155 -0
- package/src/autonomy/file-kv.js +88 -0
- package/src/autonomy/ledger.js +203 -0
- package/src/autonomy/pins.js +50 -0
- package/src/autonomy/psl/index.js +32 -0
- package/src/autonomy/psl/suffixes.json +1 -0
- package/src/autonomy/record.js +28 -0
- package/src/autonomy/routines.js +107 -0
- package/src/autonomy/server.js +175 -0
- package/src/autonomy/signer-node.js +167 -0
- package/src/autonomy/store.js +43 -0
- package/src/autonomy/unattended.js +13 -0
- package/src/bytes/bytes.js +378 -0
- package/src/calc/syntax.js +125 -0
- package/src/calc/tex.js +28 -0
- package/src/calendar/vendor.js +2 -0
- package/src/clipboard.js +130 -0
- package/src/clsh/clsh.js +3417 -0
- package/src/codec/capnp.js +1804 -0
- package/src/codec/core.js +2043 -0
- package/src/codec/vendor/avro.js +136 -0
- package/src/codec/vendor/cbor.js +25 -0
- package/src/codec/vendor/flatbuffers.js +206 -0
- package/src/codec/vendor/msgpack.js +9 -0
- package/src/codec/vendor/protobuf.js +53 -0
- package/src/delegation/caps.js +60 -0
- package/src/delegation/config.js +32 -0
- package/src/device/model.js +246 -0
- package/src/device/recording.js +198 -0
- package/src/device/stream-model.js +151 -0
- package/src/documents/documents.js +1224 -0
- package/src/effects/catalog.js +63 -0
- package/src/effects/extras.js +133 -0
- package/src/effects/parse.js +339 -0
- package/src/embed/protocol.js +131 -0
- package/src/env/env.js +159 -0
- package/src/exact/vendor.js +45 -0
- package/src/example-meta.js +164 -0
- package/src/examples.js +94 -0
- package/src/export/canvas-image.js +217 -0
- package/src/export/expr-latex-lazy.js +8 -0
- package/src/export/formats.js +322 -0
- package/src/export/latex.js +183 -0
- package/src/export/pane.js +184 -0
- package/src/export/value.js +452 -0
- package/src/export/zip.js +214 -0
- package/src/ext/access.js +58 -0
- package/src/ext/btasks.js +225 -0
- package/src/ext/client.js +209 -0
- package/src/ext/tools.js +69 -0
- package/src/ext/view.js +105 -0
- package/src/ext/webmcp-tabs.js +299 -0
- package/src/files/archive.js +232 -0
- package/src/files/files.js +317 -0
- package/src/formats/json.js +24 -0
- package/src/formats/jupyter.js +188 -0
- package/src/formats/markdown.js +254 -0
- package/src/formats/n8n.js +276 -0
- package/src/formats/node-red.js +363 -0
- package/src/formats/registry.js +177 -0
- package/src/formats/rivet.js +259 -0
- package/src/formats/shared.js +98 -0
- package/src/formats/structure.js +182 -0
- package/src/formats/yaml.js +127 -0
- package/src/frame/frame.js +962 -0
- package/src/frame/vendor.js +1484 -0
- package/src/gate/audit.js +116 -0
- package/src/gate/gate.js +365 -0
- package/src/gate/policy.js +295 -0
- package/src/gate/scripted.js +110 -0
- package/src/gate/seams.js +32 -0
- package/src/gate/store.js +66 -0
- package/src/geometry/model.js +533 -0
- package/src/grant-key.js +149 -0
- package/src/graph/model.js +381 -0
- package/src/headless/a2a-push.js +123 -0
- package/src/headless/channels/config.js +142 -0
- package/src/headless/channels/connectors.js +283 -0
- package/src/headless/channels/index.js +257 -0
- package/src/headless/channels/mail.js +197 -0
- package/src/headless/channels/mailnet.js +223 -0
- package/src/headless/channels/platforms.js +194 -0
- package/src/headless/channels/replay.js +49 -0
- package/src/headless/cli.js +1397 -0
- package/src/headless/companion-client.js +182 -0
- package/src/headless/cost-cli.js +177 -0
- package/src/headless/deprecations-cli.js +102 -0
- package/src/headless/examples.js +48 -0
- package/src/headless/ffmpeg-exec.js +217 -0
- package/src/headless/gateways/config.js +213 -0
- package/src/headless/gateways/homeassistant.js +159 -0
- package/src/headless/gateways/index.js +121 -0
- package/src/headless/gateways/matter.js +150 -0
- package/src/headless/gateways/mqtt-client.js +177 -0
- package/src/headless/gateways/mqtt.js +70 -0
- package/src/headless/gateways/netway.js +226 -0
- package/src/headless/gateways/serverpod-worker.mjs +16 -0
- package/src/headless/gateways/serverpod.js +71 -0
- package/src/headless/gateways/util.js +27 -0
- package/src/headless/grant-cli.js +208 -0
- package/src/headless/guard.js +125 -0
- package/src/headless/guards.js +124 -0
- package/src/headless/machine-cli.js +853 -0
- package/src/headless/mcp-endpoint.js +287 -0
- package/src/headless/mcp-roots.js +38 -0
- package/src/headless/model-endpoint.js +88 -0
- package/src/headless/net.js +151 -0
- package/src/headless/node.js +263 -0
- package/src/headless/npm-tarball.js +29 -0
- package/src/headless/operator-page.js +112 -0
- package/src/headless/operator.js +242 -0
- package/src/headless/people.js +34 -0
- package/src/headless/pkg-cli.js +1103 -0
- package/src/headless/pkg-serve.js +138 -0
- package/src/headless/process-worker.js +88 -0
- package/src/headless/runner-remote.js +477 -0
- package/src/headless/sandbox-child.mjs +21 -0
- package/src/headless/scheduled.js +557 -0
- package/src/headless/serve-stream.js +97 -0
- package/src/headless/serve.js +2238 -0
- package/src/headless/short-links.js +158 -0
- package/src/headless/static-dirs.js +207 -0
- package/src/headless/trust-file.js +51 -0
- package/src/headless/watch.js +291 -0
- package/src/headless/ws-connect.js +65 -0
- package/src/headless/ws-connections.js +44 -0
- package/src/headless/ws-echo.js +150 -0
- package/src/headless/ws-proxy.js +354 -0
- package/src/headless/ws-ticket.js +48 -0
- package/src/hooks/config.js +50 -0
- package/src/hooks/engine.js +189 -0
- package/src/input/log.js +613 -0
- package/src/input/player.js +217 -0
- package/src/input/replay.js +209 -0
- package/src/jth/vendor.js +28 -0
- package/src/ledger/tx.js +94 -0
- package/src/machine/apply.js +193 -0
- package/src/machine/diff.js +168 -0
- package/src/machine/drive.js +296 -0
- package/src/machine/engine.js +478 -0
- package/src/machine/files-tool.js +76 -0
- package/src/machine/lint.js +139 -0
- package/src/machine/llmtm-trace.js +117 -0
- package/src/machine/llmtm.js +475 -0
- package/src/machine/otel.js +80 -0
- package/src/machine/pane-provider.js +51 -0
- package/src/machine/propose.js +159 -0
- package/src/machine/random.js +52 -0
- package/src/machine/risk.js +107 -0
- package/src/machine/session.js +1017 -0
- package/src/machine/shell-tool.js +45 -0
- package/src/machine/tape.js +349 -0
- package/src/machine/templates.js +104 -0
- package/src/machine/trace.js +664 -0
- package/src/machine/view.js +73 -0
- package/src/machine/vm-tool.js +47 -0
- package/src/machine/watchdog.js +46 -0
- package/src/mcp/app-args.js +51 -0
- package/src/mcp/app-run.js +56 -0
- package/src/mcp/apps.js +468 -0
- package/src/mcp/cache.js +232 -0
- package/src/mcp/client.js +507 -0
- package/src/mcp/demo-app.js +183 -0
- package/src/mcp/demo-server.js +602 -0
- package/src/mcp/elicitation.js +116 -0
- package/src/mcp/hub.js +1326 -0
- package/src/mcp/roots.js +139 -0
- package/src/mcp/sampling.js +105 -0
- package/src/mcp/settings.js +125 -0
- package/src/mcp/tape.js +292 -0
- package/src/mcp/uri-template.js +156 -0
- package/src/mcp/wsh-client.js +164 -0
- package/src/media/animated-codec.js +7 -0
- package/src/media/animated.js +623 -0
- package/src/media/compare-agent.js +70 -0
- package/src/media/compare-job.js +63 -0
- package/src/media/compare-page.js +296 -0
- package/src/media/compare-worker.js +14 -0
- package/src/media/compare.js +600 -0
- package/src/media/contract.js +62 -0
- package/src/media/decode-frame.js +57 -0
- package/src/media/decode-pool.js +188 -0
- package/src/media/ffmpeg-edit.js +344 -0
- package/src/media/ffmpeg-wasm-client.js +121 -0
- package/src/media/ffmpeg-wasm-session.js +79 -0
- package/src/media/ffmpeg-wasm.js +139 -0
- package/src/media/ffmpeg-worker.js +23 -0
- package/src/media/ffmpeg.js +903 -0
- package/src/media/helpers.js +419 -0
- package/src/media/keep.js +108 -0
- package/src/media/metadata-worker.js +16 -0
- package/src/media/metadata.js +2518 -0
- package/src/media/mime.js +102 -0
- package/src/media/page-decode.js +74 -0
- package/src/media/persist.js +114 -0
- package/src/media/png.js +226 -0
- package/src/media/sample-input.js +67 -0
- package/src/media/store.js +86 -0
- package/src/media/wav.js +109 -0
- package/src/midi/mappings.js +250 -0
- package/src/midi/message.js +125 -0
- package/src/model/action-hub.js +143 -0
- package/src/model/adapters.js +284 -0
- package/src/model/agent-runs.js +12 -0
- package/src/model/arrange.js +308 -0
- package/src/model/automation-clock.js +494 -0
- package/src/model/automation.js +653 -0
- package/src/model/batches.js +78 -0
- package/src/model/binary-view.js +195 -0
- package/src/model/breakpoints.js +392 -0
- package/src/model/calc-settings.js +65 -0
- package/src/model/calendar.js +254 -0
- package/src/model/canvas-id.js +5 -0
- package/src/model/canvas.js +5048 -0
- package/src/model/channel-names.js +29 -0
- package/src/model/channels.js +692 -0
- package/src/model/chart-clock.js +631 -0
- package/src/model/chart-code.js +69 -0
- package/src/model/codecs.js +60 -0
- package/src/model/companion-access.js +271 -0
- package/src/model/companion-pane.js +50 -0
- package/src/model/composite.js +324 -0
- package/src/model/context.js +380 -0
- package/src/model/controls.js +231 -0
- package/src/model/converter-presets.js +368 -0
- package/src/model/cost.js +580 -0
- package/src/model/curve-transform.js +122 -0
- package/src/model/cycles.js +176 -0
- package/src/model/debug-log.js +168 -0
- package/src/model/decision-clock.js +100 -0
- package/src/model/decisions.js +265 -0
- package/src/model/desk.js +87 -0
- package/src/model/device-hub.js +400 -0
- package/src/model/diff.js +445 -0
- package/src/model/emit-events.js +179 -0
- package/src/model/errors.js +238 -0
- package/src/model/event-presets.js +297 -0
- package/src/model/events.js +1303 -0
- package/src/model/explicit-setters.js +139 -0
- package/src/model/field-mapping.js +623 -0
- package/src/model/files-pane.js +263 -0
- package/src/model/flow-idle.js +89 -0
- package/src/model/foreach.js +549 -0
- package/src/model/frame-loop.js +71 -0
- package/src/model/frame-pressure.js +180 -0
- package/src/model/function-scaffold.js +1000 -0
- package/src/model/geometry-settings.js +54 -0
- package/src/model/goals.js +176 -0
- package/src/model/graph-settings.js +76 -0
- package/src/model/grid-widget.js +175 -0
- package/src/model/groups.js +332 -0
- package/src/model/hash.js +21 -0
- package/src/model/history.js +259 -0
- package/src/model/http-copy.js +88 -0
- package/src/model/http-send-companion.js +110 -0
- package/src/model/http-send.js +163 -0
- package/src/model/http-tap.js +521 -0
- package/src/model/http-tools.js +1138 -0
- package/src/model/import-lock.js +78 -0
- package/src/model/imports.js +258 -0
- package/src/model/infer-outputs.js +822 -0
- package/src/model/ink-connect.js +150 -0
- package/src/model/ink-diff.js +75 -0
- package/src/model/ink-gone.js +71 -0
- package/src/model/ink-route.js +120 -0
- package/src/model/ink-svg.js +206 -0
- package/src/model/ink.js +739 -0
- package/src/model/input-log-hub.js +456 -0
- package/src/model/input-split.js +69 -0
- package/src/model/json-schema-host.js +9 -0
- package/src/model/json-schema.js +368 -0
- package/src/model/jth.js +78 -0
- package/src/model/latex-expr.js +528 -0
- package/src/model/layout-history.js +120 -0
- package/src/model/lazy-map.js +88 -0
- package/src/model/ledger-pane.js +101 -0
- package/src/model/legacy-viaframe.js +90 -0
- package/src/model/lessons.js +448 -0
- package/src/model/links.js +981 -0
- package/src/model/live.js +411 -0
- package/src/model/math-widget.js +53 -0
- package/src/model/memory-settings.js +33 -0
- package/src/model/midi-controls.js +350 -0
- package/src/model/nl-time.js +408 -0
- package/src/model/notebook.js +137 -0
- package/src/model/pane-view-store.js +15 -0
- package/src/model/partial.js +115 -0
- package/src/model/pause-clock.js +53 -0
- package/src/model/peer-pane.js +32 -0
- package/src/model/persist-state.js +290 -0
- package/src/model/person-only.js +122 -0
- package/src/model/pin.js +209 -0
- package/src/model/plot-3d.js +213 -0
- package/src/model/plot-series.js +157 -0
- package/src/model/plot-settings.js +531 -0
- package/src/model/ports.js +312 -0
- package/src/model/presentation.js +222 -0
- package/src/model/preset-apply.js +316 -0
- package/src/model/preset-banks.js +335 -0
- package/src/model/preset-morph.js +263 -0
- package/src/model/preset-ops.js +205 -0
- package/src/model/preset-player.js +283 -0
- package/src/model/preset-states.js +172 -0
- package/src/model/profile-settings.js +109 -0
- package/src/model/promote.js +103 -0
- package/src/model/protected.js +10 -0
- package/src/model/proxy-log.js +230 -0
- package/src/model/proxy.js +367 -0
- package/src/model/quantity-widget.js +61 -0
- package/src/model/queue-book.js +359 -0
- package/src/model/quick-add.js +224 -0
- package/src/model/rate-limit.js +313 -0
- package/src/model/rate.js +95 -0
- package/src/model/recovery.js +216 -0
- package/src/model/rename-follow.js +113 -0
- package/src/model/repeater.js +1126 -0
- package/src/model/retention.js +664 -0
- package/src/model/route-edit.js +132 -0
- package/src/model/router.js +155 -0
- package/src/model/runaway.js +232 -0
- package/src/model/runlog.js +1033 -0
- package/src/model/runners/a2a.js +2 -0
- package/src/model/runners/acp.js +2 -0
- package/src/model/runners/actions.js +4 -0
- package/src/model/runners/agent.js +4 -0
- package/src/model/runners/audio.js +21 -0
- package/src/model/runners/automation.js +2 -0
- package/src/model/runners/calc.js +23 -0
- package/src/model/runners/catch.js +10 -0
- package/src/model/runners/codec.js +24 -0
- package/src/model/runners/companion-run.js +2 -0
- package/src/model/runners/device.js +7 -0
- package/src/model/runners/dom.js +13 -0
- package/src/model/runners/effects.js +2 -0
- package/src/model/runners/embed.js +11 -0
- package/src/model/runners/eval.js +2 -0
- package/src/model/runners/files.js +19 -0
- package/src/model/runners/geometry.js +23 -0
- package/src/model/runners/goals.js +18 -0
- package/src/model/runners/graph.js +23 -0
- package/src/model/runners/html.js +2 -0
- package/src/model/runners/http-proxy.js +185 -0
- package/src/model/runners/http-send.js +61 -0
- package/src/model/runners/http-static.js +83 -0
- package/src/model/runners/http-tap.js +26 -0
- package/src/model/runners/http.js +40 -0
- package/src/model/runners/import.js +2 -0
- package/src/model/runners/index.js +74 -0
- package/src/model/runners/input.js +14 -0
- package/src/model/runners/inputlog.js +7 -0
- package/src/model/runners/ledger.js +15 -0
- package/src/model/runners/machine.js +4 -0
- package/src/model/runners/mcp.js +2 -0
- package/src/model/runners/mcpapp.js +2 -0
- package/src/model/runners/media.js +18 -0
- package/src/model/runners/memory.js +28 -0
- package/src/model/runners/mesh-inspect.js +13 -0
- package/src/model/runners/peer.js +14 -0
- package/src/model/runners/plot.js +22 -0
- package/src/model/runners/render.js +14 -0
- package/src/model/runners/respond.js +2 -0
- package/src/model/runners/sample-soundfont.js +85 -0
- package/src/model/runners/sample.js +159 -0
- package/src/model/runners/scene.js +8 -0
- package/src/model/runners/score.js +24 -0
- package/src/model/runners/secret.js +10 -0
- package/src/model/runners/select.js +2 -0
- package/src/model/runners/shared.js +4 -0
- package/src/model/runners/shares.js +15 -0
- package/src/model/runners/shell.js +7 -0
- package/src/model/runners/skill.js +26 -0
- package/src/model/runners/state.js +2 -0
- package/src/model/runners/statechart.js +4 -0
- package/src/model/runners/subgraph.js +9 -0
- package/src/model/runners/tabs.js +25 -0
- package/src/model/runners/terminal.js +5 -0
- package/src/model/runners/text.js +4 -0
- package/src/model/runners/timer.js +7 -0
- package/src/model/runners/traffic.js +65 -0
- package/src/model/runners/trigger.js +2 -0
- package/src/model/runners/verdict.js +29 -0
- package/src/model/runners/vm.js +7 -0
- package/src/model/runners/wait.js +4 -0
- package/src/model/runners/ws-client.js +57 -0
- package/src/model/safe-regex.js +719 -0
- package/src/model/sample-settings.js +43 -0
- package/src/model/sample-source.js +47 -0
- package/src/model/saved-mapping.js +153 -0
- package/src/model/score-controllers.js +87 -0
- package/src/model/score-edit.js +399 -0
- package/src/model/score-instruments.js +300 -0
- package/src/model/score-library.js +407 -0
- package/src/model/score-markup.js +235 -0
- package/src/model/score-mix.js +248 -0
- package/src/model/score-patterns.js +276 -0
- package/src/model/score-selection.js +34 -0
- package/src/model/score-time.js +139 -0
- package/src/model/score-transforms.js +336 -0
- package/src/model/score-transport.js +192 -0
- package/src/model/selection.js +351 -0
- package/src/model/serial-controls.js +328 -0
- package/src/model/shares-pane.js +17 -0
- package/src/model/shell-pane.js +106 -0
- package/src/model/signature.js +1122 -0
- package/src/model/size-hints.js +106 -0
- package/src/model/size-ratio.js +12 -0
- package/src/model/skill-settings.js +23 -0
- package/src/model/snapping.js +466 -0
- package/src/model/split.js +375 -0
- package/src/model/spring-player.js +222 -0
- package/src/model/statecharts.js +1369 -0
- package/src/model/states-preset.js +208 -0
- package/src/model/static-route.js +100 -0
- package/src/model/step-words.js +45 -0
- package/src/model/subgraph.js +1042 -0
- package/src/model/switch.js +98 -0
- package/src/model/tabs-store.js +10 -0
- package/src/model/tensor-view.js +146 -0
- package/src/model/timer-clock.js +361 -0
- package/src/model/timers.js +422 -0
- package/src/model/timing.js +212 -0
- package/src/model/traffic-agent.js +111 -0
- package/src/model/traffic-store.js +20 -0
- package/src/model/traffic.js +665 -0
- package/src/model/transport.js +364 -0
- package/src/model/turns.js +86 -0
- package/src/model/types.js +425 -0
- package/src/model/value-path.js +138 -0
- package/src/model/versions.js +535 -0
- package/src/model/view-outputs.js +144 -0
- package/src/model/viewport-bookmarks.js +194 -0
- package/src/model/virtual-clock.js +85 -0
- package/src/model/vm-pane.js +258 -0
- package/src/model/wait-clock.js +715 -0
- package/src/model/wait-persist.js +248 -0
- package/src/model/waits.js +431 -0
- package/src/model/watch.js +80 -0
- package/src/model/webhook.js +496 -0
- package/src/model/when.js +48 -0
- package/src/model/widget-values.js +392 -0
- package/src/model/window-commands.js +278 -0
- package/src/model/windows.js +348 -0
- package/src/model/wire-inspect.js +343 -0
- package/src/model/wire-ops.js +324 -0
- package/src/model/wire-pipeline.js +378 -0
- package/src/model/wire-route.js +637 -0
- package/src/model/workspaces.js +122 -0
- package/src/model/ws-client.js +155 -0
- package/src/model/ws-demo.js +31 -0
- package/src/model/ws-frames.js +141 -0
- package/src/model/ws-hub.js +219 -0
- package/src/model/ws-served.js +13 -0
- package/src/model/xstate.js +374 -0
- package/src/narrative/narrative.js +1557 -0
- package/src/network/auth.js +224 -0
- package/src/network/csp-frame.js +197 -0
- package/src/network/declarative-shadow.js +27 -0
- package/src/network/frame-net-shim.js +962 -0
- package/src/network/hosts.js +157 -0
- package/src/network/policy.js +520 -0
- package/src/network/rtc-shim.js +418 -0
- package/src/network/secret-gate.js +78 -0
- package/src/network/secrets.js +261 -0
- package/src/network/sockets.js +287 -0
- package/src/network/store.js +186 -0
- package/src/network/streams.js +99 -0
- package/src/packages/asset-values.js +50 -0
- package/src/packages/assets.js +99 -0
- package/src/packages/deprecation.js +203 -0
- package/src/packages/fetch.js +152 -0
- package/src/packages/function-canvas.js +73 -0
- package/src/packages/module.js +120 -0
- package/src/packages/package.js +474 -0
- package/src/packages/published.js +27 -0
- package/src/packages/requires.js +83 -0
- package/src/packages/signing.js +145 -0
- package/src/packages/trust.js +107 -0
- package/src/panes/a2a.js +51 -0
- package/src/panes/acp.js +60 -0
- package/src/panes/actions.js +54 -0
- package/src/panes/agent.js +83 -0
- package/src/panes/audio.js +103 -0
- package/src/panes/automation.js +112 -0
- package/src/panes/calc.js +67 -0
- package/src/panes/catch.js +52 -0
- package/src/panes/codec.js +59 -0
- package/src/panes/colors.js +12 -0
- package/src/panes/companion.js +54 -0
- package/src/panes/decision.js +65 -0
- package/src/panes/device.js +56 -0
- package/src/panes/dom.js +37 -0
- package/src/panes/effects.js +118 -0
- package/src/panes/embed.js +30 -0
- package/src/panes/eval.js +186 -0
- package/src/panes/fields.js +299 -0
- package/src/panes/files.js +45 -0
- package/src/panes/gate.js +70 -0
- package/src/panes/geometry.js +64 -0
- package/src/panes/goals.js +75 -0
- package/src/panes/graph.js +66 -0
- package/src/panes/html.js +54 -0
- package/src/panes/http.js +82 -0
- package/src/panes/import.js +38 -0
- package/src/panes/input.js +124 -0
- package/src/panes/inputlog.js +66 -0
- package/src/panes/ledger.js +56 -0
- package/src/panes/machine.js +138 -0
- package/src/panes/mcp.js +85 -0
- package/src/panes/mcpapp.js +64 -0
- package/src/panes/media.js +38 -0
- package/src/panes/memory.js +46 -0
- package/src/panes/mesh-inspect.js +49 -0
- package/src/panes/note.js +38 -0
- package/src/panes/peer.js +44 -0
- package/src/panes/plot.js +196 -0
- package/src/panes/registry.js +475 -0
- package/src/panes/render.js +87 -0
- package/src/panes/respond.js +44 -0
- package/src/panes/router.js +84 -0
- package/src/panes/sample.js +75 -0
- package/src/panes/scene.js +55 -0
- package/src/panes/score.js +87 -0
- package/src/panes/secret.js +38 -0
- package/src/panes/select.js +67 -0
- package/src/panes/shader.js +166 -0
- package/src/panes/shared.js +33 -0
- package/src/panes/shares.js +43 -0
- package/src/panes/shell.js +47 -0
- package/src/panes/skill.js +44 -0
- package/src/panes/state.js +91 -0
- package/src/panes/statechart.js +63 -0
- package/src/panes/story.js +33 -0
- package/src/panes/subgraph.js +94 -0
- package/src/panes/tabs.js +52 -0
- package/src/panes/terminal.js +57 -0
- package/src/panes/text.js +36 -0
- package/src/panes/timer.js +55 -0
- package/src/panes/traffic.js +59 -0
- package/src/panes/verdict.js +62 -0
- package/src/panes/vm.js +46 -0
- package/src/panes/wait.js +61 -0
- package/src/panes/watch.js +57 -0
- package/src/panes/webhook.js +67 -0
- package/src/panes/ws-client.js +67 -0
- package/src/persist.js +281 -0
- package/src/plot/tex.js +19 -0
- package/src/plot/vendor.js +59 -0
- package/src/plot/view-vendor.js +5 -0
- package/src/privacy/levels.js +108 -0
- package/src/protocol/devtools.js +116 -0
- package/src/protocol/inspector.js +368 -0
- package/src/render/time.js +381 -0
- package/src/rooms/mesh-metrics.js +162 -0
- package/src/run/companion.js +355 -0
- package/src/run/describe.js +114 -0
- package/src/run/local-model.js +150 -0
- package/src/run/monitor.js +208 -0
- package/src/run/publish.js +360 -0
- package/src/run/respond-outcome.js +12 -0
- package/src/run/run.js +820 -0
- package/src/run/runner-pairing.js +243 -0
- package/src/run/values.js +122 -0
- package/src/runtime/compile.js +122 -0
- package/src/runtime/console-tag.js +64 -0
- package/src/runtime/frame-signals.js +126 -0
- package/src/runtime/iteration-push.js +19 -0
- package/src/runtime/keys.js +474 -0
- package/src/runtime/midi-input.js +170 -0
- package/src/runtime/person-waits.js +140 -0
- package/src/runtime/resource-timing.js +56 -0
- package/src/runtime/runtime.js +2150 -0
- package/src/runtime/sandbox-modules.js +163 -0
- package/src/runtime/spring.js +246 -0
- package/src/runtime/strict.js +215 -0
- package/src/runtime/transfer.js +75 -0
- package/src/runtime/tween.js +158 -0
- package/src/runtime/wasm-fuel.js +596 -0
- package/src/runtime/worker/00-prelude.js +88 -0
- package/src/runtime/worker/10-helpers.js +295 -0
- package/src/runtime/worker/20-io.js +645 -0
- package/src/runtime/worker/30-previews.js +210 -0
- package/src/runtime/worker/40-profile-lineage.js +278 -0
- package/src/runtime/worker/50-own-worker.js +233 -0
- package/src/runtime/worker/60-timers.js +172 -0
- package/src/runtime/worker/70-state-keyed.js +193 -0
- package/src/runtime/worker/80-each-compile-generators.js +619 -0
- package/src/runtime/worker/90-events-operators.js +655 -0
- package/src/runtime/worker/95-agents-batches.js +1820 -0
- package/src/runtime/worker/96-plot.js +47 -0
- package/src/runtime/worker/97-calc.js +28 -0
- package/src/runtime/worker/98-geometry.js +24 -0
- package/src/runtime/worker/99-graph.js +30 -0
- package/src/runtime/worker/99-shell.js +30 -0
- package/src/safety/config.js +57 -0
- package/src/safety/engine.js +324 -0
- package/src/scene/pixi-scene.js +806 -0
- package/src/scene/scene-assets.js +241 -0
- package/src/scene/scene-edit.js +319 -0
- package/src/secret/settings.js +41 -0
- package/src/security/multipart.js +257 -0
- package/src/security/redact.js +261 -0
- package/src/serial/broker.js +473 -0
- package/src/serial/config.js +185 -0
- package/src/serial/framers.js +389 -0
- package/src/serial/packets.js +64 -0
- package/src/shader/audio-texture.js +87 -0
- package/src/shader/channels.js +496 -0
- package/src/shader/common.js +90 -0
- package/src/shader/model.js +867 -0
- package/src/shader/passes.js +150 -0
- package/src/shader/shadertoy.js +198 -0
- package/src/shell/hub.js +287 -0
- package/src/sim/checkpoints.js +850 -0
- package/src/sim/compile.js +37 -0
- package/src/sim/fixed.js +377 -0
- package/src/sim/headless.js +205 -0
- package/src/sim/physics.js +399 -0
- package/src/smf-helper/vendor.js +2 -0
- package/src/sources/config.js +435 -0
- package/src/storage.js +153 -0
- package/src/table/table-geo.js +2344 -0
- package/src/table/table-lineage.js +808 -0
- package/src/table/table-quality.js +355 -0
- package/src/table/table-schema.js +392 -0
- package/src/table/table-sql.js +1650 -0
- package/src/table/table-stats.js +480 -0
- package/src/table/table.js +2102 -0
- package/src/tensor/tensor.js +1153 -0
- package/src/terminal/hub.js +715 -0
- package/src/terminal/screen.js +516 -0
- package/src/tests/capture.js +88 -0
- package/src/tests/compare.js +480 -0
- package/src/tests/dataset.js +170 -0
- package/src/tests/evaluate.js +286 -0
- package/src/tests/from-runs.js +99 -0
- package/src/tests/judge.js +134 -0
- package/src/tests/matchers.js +263 -0
- package/src/tests/matrix-spec.js +87 -0
- package/src/tests/matrix.js +397 -0
- package/src/tests/metrics.js +369 -0
- package/src/tests/no-value.js +42 -0
- package/src/tests/pane-check.js +45 -0
- package/src/tests/report.js +124 -0
- package/src/tests/reporters.js +140 -0
- package/src/tests/run.js +155 -0
- package/src/tilemap/tilemap.js +1122 -0
- package/src/units/units.js +3 -0
- package/src/util/bytes-text.js +21 -0
- package/src/util/num.js +10 -0
- package/src/util/regexp.js +2 -0
- package/src/util/text.js +38 -0
- package/src/verdict/engine.js +154 -0
- package/src/vm/guest-wsh.js +108 -0
- package/src/vm/hub.js +457 -0
- package/src/vm/image-files.js +4 -0
- package/src/vm/image.js +33 -0
- package/src/vm/image.lock.json +13 -0
- package/src/vm/link.js +266 -0
- package/src/vm/mount.js +212 -0
- package/src/vm/net.js +85 -0
- package/src/vm/pipe-ws.js +187 -0
- package/src/vm/v86-loader.js +11 -0
- package/src/webmcp/args.js +212 -0
- package/src/webmcp/hidden.js +218 -0
- package/src/webmcp/text.js +250 -0
- package/src/webmcp/tools/bookmark-tools.js +106 -0
- package/src/webmcp/tools/breakpoint-tools.js +48 -0
- package/src/webmcp/tools/canvases.js +208 -0
- package/src/webmcp/tools/context-tools.js +84 -0
- package/src/webmcp/tools/core.js +170 -0
- package/src/webmcp/tools/devices.js +41 -0
- package/src/webmcp/tools/edit.js +357 -0
- package/src/webmcp/tools/event-tools.js +145 -0
- package/src/webmcp/tools/image-tools.js +173 -0
- package/src/webmcp/tools/ink-tools.js +247 -0
- package/src/webmcp/tools/input-log-tools.js +163 -0
- package/src/webmcp/tools/live.js +166 -0
- package/src/webmcp/tools/machine-tools.js +74 -0
- package/src/webmcp/tools/media.js +161 -0
- package/src/webmcp/tools/midi-tools.js +32 -0
- package/src/webmcp/tools/preset-tools.js +184 -0
- package/src/webmcp/tools/quick-add-tools.js +95 -0
- package/src/webmcp/tools/reshape.js +152 -0
- package/src/webmcp/tools/run-tools.js +309 -0
- package/src/webmcp/tools/runs.js +101 -0
- package/src/webmcp/tools/scaffold-tools.js +161 -0
- package/src/webmcp/tools/serial-tools.js +36 -0
- package/src/webmcp/tools/shared.js +338 -0
- package/src/webmcp/tools/subgraphs.js +360 -0
- package/src/webmcp/tools/tests.js +170 -0
- package/src/webmcp/tools/traffic-tools.js +70 -0
- package/src/webmcp/tools/transport-tools.js +65 -0
- package/src/webmcp/tools/version-tools.js +126 -0
- package/src/webmcp/tools/versions.js +11 -0
- package/src/webmcp/tools/wire-tools.js +90 -0
- package/src/webmcp/tools/workspace-tools.js +93 -0
- package/src/webmcp/tools.js +1323 -0
- package/src/wsh/connect.js +347 -0
- package/src/wsh/demo-host.js +361 -0
- package/src/wsh/e2e.js +27 -0
- package/src/wsh/keys.js +154 -0
- package/src/wsh/known-hosts.js +39 -0
- package/src/wsh/private-key.js +95 -0
- package/src/wsh/reverse.js +34 -0
- package/src/wsh/settings.js +118 -0
- package/src/wsh/transfer.js +62 -0
- package/src/wsh/vendor.js +5 -0
- package/src/x402/challenge.js +171 -0
- package/src/x402/demo-server.js +127 -0
- package/src/x402/pay.js +125 -0
- package/src/x402/retry.js +95 -0
- package/src/x402/store.js +76 -0
package/src/media/wav.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Reading WAV (RIFF/WAVE) files of any common kind, for decodeAudio() and the
|
|
2
|
+
// waveform: PCM 8/16/24/32-bit, IEEE float 32/64, WAVE_FORMAT_EXTENSIBLE.
|
|
3
|
+
// Writing WAV is src/audio/wav.js encodeWav (one encoder, shared with audio
|
|
4
|
+
// panes). Pure: no DOM and no imports, so it runs in the sandbox Worker (as the
|
|
5
|
+
// module "mallory:media/wav"), on the page and under Node.
|
|
6
|
+
|
|
7
|
+
const ascii = (view, at, text) => {
|
|
8
|
+
for (let i = 0; i < text.length; i++) view.setUint8(at + i, text.charCodeAt(i));
|
|
9
|
+
};
|
|
10
|
+
const readAscii = (view, at, n) => {
|
|
11
|
+
let s = "";
|
|
12
|
+
for (let i = 0; i < n; i++) s += String.fromCharCode(view.getUint8(at + i));
|
|
13
|
+
return s;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** One Float32Array per channel, from a Float32Array (mono), an array of numbers, or an array of channels; shorter channels are padded with silence. */
|
|
17
|
+
export function toChannels(samples) {
|
|
18
|
+
if (ArrayBuffer.isView(samples)) return [Float32Array.from(samples)];
|
|
19
|
+
if (!Array.isArray(samples) || !samples.length) throw new TypeError("audio samples: expected a Float32Array, or an array of channels");
|
|
20
|
+
if (typeof samples[0] === "number") return [Float32Array.from(samples)];
|
|
21
|
+
const channels = samples.map((c, i) => {
|
|
22
|
+
if (!ArrayBuffer.isView(c) && !Array.isArray(c)) throw new TypeError(`audio samples: channel ${i} isn't an array of numbers`);
|
|
23
|
+
return c instanceof Float32Array ? c : Float32Array.from(c);
|
|
24
|
+
});
|
|
25
|
+
const frames = Math.max(...channels.map((c) => c.length));
|
|
26
|
+
return channels.map((c) => (c.length === frames ? c : padded(c, frames)));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function padded(channel, frames) {
|
|
30
|
+
const out = new Float32Array(frames);
|
|
31
|
+
out.set(channel);
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Is this the start of a WAV file? (bytes: Uint8Array or ArrayBuffer, at least 12 bytes) */
|
|
36
|
+
export function isWav(bytes) {
|
|
37
|
+
const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
38
|
+
if (b.length < 12) return false;
|
|
39
|
+
const view = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
40
|
+
return readAscii(view, 0, 4) === "RIFF" && readAscii(view, 8, 4) === "WAVE";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Read a WAV file's format without decoding it (the first few KB are enough):
|
|
45
|
+
* `{ format, channels, sampleRate, bitDepth, frames?, duration? }`, or null.
|
|
46
|
+
* `frames`/`duration` are there when the data chunk's header was in `bytes`.
|
|
47
|
+
*/
|
|
48
|
+
export function wavInfo(bytes) {
|
|
49
|
+
const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
50
|
+
if (!isWav(b)) return null;
|
|
51
|
+
const view = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
52
|
+
let at = 12;
|
|
53
|
+
let fmt = null;
|
|
54
|
+
for (let chunks = 0; at + 8 <= b.length && chunks <= 1024; chunks++) { // a hostile file of empty chunks costs 8 bytes of file for a pass: the walk is capped
|
|
55
|
+
const id = readAscii(view, at, 4);
|
|
56
|
+
const size = view.getUint32(at + 4, true);
|
|
57
|
+
if (id === "fmt " && at + 24 <= b.length) {
|
|
58
|
+
let format = view.getUint16(at + 8, true);
|
|
59
|
+
const channels = view.getUint16(at + 10, true);
|
|
60
|
+
const sampleRate = view.getUint32(at + 12, true);
|
|
61
|
+
const bitDepth = view.getUint16(at + 22, true);
|
|
62
|
+
// WAVE_FORMAT_EXTENSIBLE: the real format is the first 2 bytes of the sub-format GUID.
|
|
63
|
+
if (format === 0xfffe && size >= 40 && at + 34 <= b.length) format = view.getUint16(at + 32, true);
|
|
64
|
+
fmt = { format, channels, sampleRate, bitDepth };
|
|
65
|
+
} else if (id === "data" && fmt) {
|
|
66
|
+
const frames = Math.floor(size / Math.max(1, (fmt.channels * fmt.bitDepth) / 8));
|
|
67
|
+
return { ...fmt, frames, duration: frames / fmt.sampleRate, dataOffset: at + 8, dataBytes: size };
|
|
68
|
+
}
|
|
69
|
+
at += 8 + size + (size % 2);
|
|
70
|
+
}
|
|
71
|
+
return fmt;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Decode a WAV file: PCM 8/16/24/32-bit or IEEE float 32/64.
|
|
76
|
+
* @param {ArrayBuffer | Uint8Array} bytes
|
|
77
|
+
* @returns {{ channels: Float32Array[], sampleRate: number, bitDepth: number, duration: number }}
|
|
78
|
+
*/
|
|
79
|
+
export function decodeWav(bytes) {
|
|
80
|
+
const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
81
|
+
const info = wavInfo(b);
|
|
82
|
+
if (!info || info.dataOffset === undefined) throw new Error("decodeWav: not a WAV file (no RIFF/WAVE header with fmt and data chunks)");
|
|
83
|
+
const { format, channels: count, sampleRate, bitDepth, dataOffset } = info;
|
|
84
|
+
const float = format === 3;
|
|
85
|
+
if (format !== 1 && !float) throw new Error(`decodeWav: unsupported WAV encoding (format ${format}); PCM and float are supported`);
|
|
86
|
+
const bytesPer = bitDepth / 8;
|
|
87
|
+
const available = Math.min(info.dataBytes, b.length - dataOffset);
|
|
88
|
+
const frames = Math.floor(available / (count * bytesPer));
|
|
89
|
+
const view = new DataView(b.buffer, b.byteOffset + dataOffset, frames * count * bytesPer);
|
|
90
|
+
const channels = Array.from({ length: count }, () => new Float32Array(frames));
|
|
91
|
+
let at = 0;
|
|
92
|
+
for (let f = 0; f < frames; f++) {
|
|
93
|
+
for (let c = 0; c < count; c++) {
|
|
94
|
+
let s;
|
|
95
|
+
if (float) s = bitDepth === 64 ? view.getFloat64(at, true) : view.getFloat32(at, true);
|
|
96
|
+
else if (bitDepth === 8) s = (view.getUint8(at) - 128) / 128;
|
|
97
|
+
else if (bitDepth === 16) s = view.getInt16(at, true) / 0x8000;
|
|
98
|
+
else if (bitDepth === 24) {
|
|
99
|
+
let v = view.getUint8(at) | (view.getUint8(at + 1) << 8) | (view.getUint8(at + 2) << 16);
|
|
100
|
+
if (v & 0x800000) v -= 0x1000000;
|
|
101
|
+
s = v / 0x800000;
|
|
102
|
+
} else if (bitDepth === 32) s = view.getInt32(at, true) / 0x80000000;
|
|
103
|
+
else throw new Error(`decodeWav: unsupported bit depth ${bitDepth}`);
|
|
104
|
+
channels[c][f] = s;
|
|
105
|
+
at += bytesPer;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { channels, sampleRate, bitDepth, duration: frames / sampleRate };
|
|
109
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// MIDI learn mappings: which message moves which control, with a range,
|
|
2
|
+
// an inversion and a curve. Saved with the canvas as plain data (`midi:
|
|
3
|
+
// { mappings, channel? }`) and in every format and share link. Pure: no DOM, no
|
|
4
|
+
// canvas; the tests are test/midi-mappings.test.mjs.
|
|
5
|
+
//
|
|
6
|
+
// midi: {
|
|
7
|
+
// mappings: [{
|
|
8
|
+
// pane: "cutoff", an input pane, BY NAME (Follow renames repoints it)
|
|
9
|
+
// -- or --
|
|
10
|
+
// param: { pane, name }, a subgraph pane's parameter: the subgraph pane's name and the
|
|
11
|
+
// parameter's name (its input pane inside), both BY NAME
|
|
12
|
+
// -- or --
|
|
13
|
+
// morph: { bank, to, from? }, a preset bank's morph position
|
|
14
|
+
// axis?: "x" | "y", xy widgets: which axis this mapping moves (default x); curve
|
|
15
|
+
// widgets: which coordinate of the point (default y)
|
|
16
|
+
// point?: 0 to 255, curve widgets: which point (0 is the first); required there
|
|
17
|
+
// source: { type, channel?, number? },
|
|
18
|
+
// type "cc" (number needed), "note" (number optional: any note),
|
|
19
|
+
// "pitch"; channel 1 to 16, omitted: any
|
|
20
|
+
// port?: "Keystation 49", only messages from the input with this NAME
|
|
21
|
+
// min?, max?, the range the control covers (default: the widget's own, per
|
|
22
|
+
// axis; a morph's 0 to 1; a select spreads over its options)
|
|
23
|
+
// invert?: true, high messages give low values
|
|
24
|
+
// curve?: "linear" | "ease-in" | "ease-out" | "s-curve",
|
|
25
|
+
// smooth?: true, settle on the value with a spring (number and range inputs)
|
|
26
|
+
// }],
|
|
27
|
+
// channel?: "midi" forward note, controller and pitch events on this event channel
|
|
28
|
+
// }
|
|
29
|
+
//
|
|
30
|
+
// Nothing here identifies a device or a person: no ports' ids, no serial
|
|
31
|
+
// numbers, no manufacturer. A `port` is the name the browser shows ("Launchkey
|
|
32
|
+
// MIDI"); it is only a filter, and a mapping without one hears every input.
|
|
33
|
+
// Granted permissions never appear in a canvas: they live in this browser
|
|
34
|
+
// (src/midi/store.js).
|
|
35
|
+
import { constrainValue, optionsOf } from "../model/preset-morph.js";
|
|
36
|
+
import { axisOf, curveMove } from "../model/widget-values.js";
|
|
37
|
+
import { channelProblem } from "../model/channel-names.js";
|
|
38
|
+
|
|
39
|
+
export const MAX_MAPPINGS = 128;
|
|
40
|
+
export const CURVES = ["linear", "ease-in", "ease-out", "s-curve"];
|
|
41
|
+
export const SOURCE_TYPES = ["cc", "note", "pitch"];
|
|
42
|
+
/** The widget kinds a message can drive. */
|
|
43
|
+
export const MIDI_WIDGETS = ["number", "range", "checkbox", "select", "xy", "curve"];
|
|
44
|
+
/** The widgets that have more than one thing to move: an xy pad's two axes, a curve's points. */
|
|
45
|
+
export const MAX_POINT = 255;
|
|
46
|
+
const KEYS = ["pane", "param", "morph", "source", "port", "min", "max", "invert", "curve", "smooth", "axis", "point"];
|
|
47
|
+
|
|
48
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
49
|
+
const isNum = (v) => typeof v === "number" && Number.isFinite(v);
|
|
50
|
+
const isInt = (v, lo, hi) => Number.isInteger(v) && v >= lo && v <= hi;
|
|
51
|
+
const only = (o, keys) => Object.keys(o).find((k) => !keys.includes(k));
|
|
52
|
+
|
|
53
|
+
/** Why `m` isn't a mapping (a sentence), or "". */
|
|
54
|
+
export function mappingProblem(m) {
|
|
55
|
+
if (!isObject(m)) return "a mapping is { pane, source, min?, max?, invert?, curve? }";
|
|
56
|
+
const extra = only(m, KEYS);
|
|
57
|
+
if (extra) return `"${extra}" isn't a field of a mapping (${KEYS.join(", ")})`;
|
|
58
|
+
if ([m.pane, m.param, m.morph].filter((v) => v !== undefined).length !== 1) return "a mapping names an input pane (pane), a subgraph pane's parameter (param) or a preset bank's morph (morph), one of them";
|
|
59
|
+
if (m.pane !== undefined && (typeof m.pane !== "string" || !m.pane.trim() || m.pane.length > 200)) return "pane is the input pane's name (text)";
|
|
60
|
+
if (m.param !== undefined) {
|
|
61
|
+
if (!isObject(m.param) || only(m.param, ["pane", "name"])) return "param is { pane, name }: the subgraph pane's name and the parameter's";
|
|
62
|
+
for (const k of ["pane", "name"]) if (typeof m.param[k] !== "string" || !m.param[k].trim() || m.param[k].length > 200) return `param.${k} is a name (text)`;
|
|
63
|
+
}
|
|
64
|
+
if (m.axis !== undefined && m.axis !== "x" && m.axis !== "y") return "axis is x or y";
|
|
65
|
+
if (m.point !== undefined && !isInt(m.point, 0, MAX_POINT)) return `point is a whole number from 0 to ${MAX_POINT}`;
|
|
66
|
+
if (m.morph !== undefined && (m.axis !== undefined || m.point !== undefined)) return "a morph has no axis or point";
|
|
67
|
+
if (m.morph !== undefined) {
|
|
68
|
+
if (!isObject(m.morph) || only(m.morph, ["bank", "to", "from"])) return "morph is { bank, to, from? }";
|
|
69
|
+
for (const k of ["bank", "to", "from"]) if (m.morph[k] !== undefined && (typeof m.morph[k] !== "string" || !m.morph[k] || m.morph[k].length > 80)) return `morph.${k} is a name (text)`;
|
|
70
|
+
if (!m.morph.bank || !m.morph.to) return "morph needs a bank and a preset (to)";
|
|
71
|
+
}
|
|
72
|
+
const s = m.source;
|
|
73
|
+
if (!isObject(s) || only(s, ["type", "channel", "number"])) return "source is { type, channel?, number? }";
|
|
74
|
+
if (!SOURCE_TYPES.includes(s.type)) return `source.type is ${SOURCE_TYPES.join(", ")}`;
|
|
75
|
+
if (s.channel !== undefined && !isInt(s.channel, 1, 16)) return "source.channel is 1 to 16";
|
|
76
|
+
if (s.number !== undefined && !isInt(s.number, 0, 127)) return "source.number is 0 to 127";
|
|
77
|
+
if (s.type === "cc" && s.number === undefined) return "a cc source needs the controller number (source.number)";
|
|
78
|
+
if (s.type === "pitch" && s.number !== undefined) return "a pitch source has no number";
|
|
79
|
+
if (m.port !== undefined && (typeof m.port !== "string" || !m.port || m.port.length > 80)) return "port is the input's name (text, at most 80 characters)";
|
|
80
|
+
for (const k of ["min", "max"]) if (m[k] !== undefined && !isNum(m[k])) return `${k} is a number`;
|
|
81
|
+
if (m.curve !== undefined && !CURVES.includes(m.curve)) return `curve is ${CURVES.join(", ")}`;
|
|
82
|
+
for (const k of ["invert", "smooth"]) if (m[k] !== undefined && typeof m[k] !== "boolean") return `${k} is true or false`;
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** @throws a readable error for a malformed `midi` block of a doc (optional). */
|
|
87
|
+
export function validateMidi(value) {
|
|
88
|
+
if (value === undefined) return;
|
|
89
|
+
if (!isObject(value)) throw new Error("Malformed midi: an object { mappings, channel? }.");
|
|
90
|
+
const extra = only(value, ["mappings", "channel"]);
|
|
91
|
+
if (extra) throw new Error(`Malformed midi: "${extra}" isn't a field (mappings, channel).`);
|
|
92
|
+
if (value.mappings !== undefined) {
|
|
93
|
+
if (!Array.isArray(value.mappings)) throw new Error("Malformed midi: mappings is a list.");
|
|
94
|
+
if (value.mappings.length > MAX_MAPPINGS) throw new Error(`MIDI mappings: too many (max ${MAX_MAPPINGS}).`);
|
|
95
|
+
value.mappings.forEach((m, i) => {
|
|
96
|
+
const bad = mappingProblem(m);
|
|
97
|
+
if (bad) throw new Error(`MIDI mapping ${i + 1}: ${bad}.`);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (value.channel !== undefined) {
|
|
101
|
+
const bad = typeof value.channel === "string" ? channelProblem(value.channel) : "channel is the name of an event channel (text)";
|
|
102
|
+
if (bad) throw new Error(`Malformed midi: ${bad}.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** One mapping as saved: known fields only, defaults left out. */
|
|
107
|
+
export function cleanMapping(m) {
|
|
108
|
+
const out = {};
|
|
109
|
+
if (m.pane !== undefined) out.pane = m.pane.trim();
|
|
110
|
+
if (m.param) out.param = { pane: m.param.pane.trim(), name: m.param.name.trim() };
|
|
111
|
+
if (m.morph) out.morph = { bank: m.morph.bank, ...(m.morph.from ? { from: m.morph.from } : {}), to: m.morph.to };
|
|
112
|
+
out.source = { type: m.source.type, ...(m.source.channel !== undefined ? { channel: m.source.channel } : {}), ...(m.source.number !== undefined ? { number: m.source.number } : {}) };
|
|
113
|
+
if (m.port) out.port = m.port;
|
|
114
|
+
if (m.min !== undefined) out.min = m.min;
|
|
115
|
+
if (m.max !== undefined) out.max = m.max;
|
|
116
|
+
if (m.invert) out.invert = true;
|
|
117
|
+
if (m.curve && m.curve !== "linear") out.curve = m.curve;
|
|
118
|
+
if (m.smooth) out.smooth = true;
|
|
119
|
+
if (m.axis) out.axis = m.axis;
|
|
120
|
+
if (m.point !== undefined) out.point = m.point;
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The `midi` block as the canvas keeps it: well-formed mappings only; `{ mappings: [] }` when there is none. */
|
|
125
|
+
export function cleanMidi(value) {
|
|
126
|
+
const list = isObject(value) && Array.isArray(value.mappings) ? value.mappings.filter((m) => !mappingProblem(m)).slice(0, MAX_MAPPINGS).map(cleanMapping) : [];
|
|
127
|
+
const channel = isObject(value) && typeof value.channel === "string" && !channelProblem(value.channel) ? value.channel : undefined;
|
|
128
|
+
return { mappings: list, ...(channel ? { channel } : {}) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Is there anything to save? */
|
|
132
|
+
export const midiIsEmpty = (midi) => !midi?.mappings?.length && !midi?.channel;
|
|
133
|
+
|
|
134
|
+
/** The same mapping twice: same target and source. */
|
|
135
|
+
export const sameMapping = (a, b) => JSON.stringify([a.pane, a.param, a.morph, a.axis ?? null, a.point ?? null, a.source, a.port ?? null]) === JSON.stringify([b.pane, b.param, b.morph, b.axis ?? null, b.point ?? null, b.source, b.port ?? null]);
|
|
136
|
+
|
|
137
|
+
// ── applying a message ──────────────────────────────────────────────────────
|
|
138
|
+
|
|
139
|
+
const SHAPE = {
|
|
140
|
+
linear: (t) => t,
|
|
141
|
+
"ease-in": (t) => t * t,
|
|
142
|
+
"ease-out": (t) => Math.sqrt(t),
|
|
143
|
+
"s-curve": (t) => t * t * (3 - 2 * t),
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** Does `rec` come from the source `m` listens to? */
|
|
147
|
+
export function matches(m, rec) {
|
|
148
|
+
const s = m.source;
|
|
149
|
+
if (s.channel !== undefined && s.channel !== rec.channel) return false;
|
|
150
|
+
if (m.port && m.port !== rec.port) return false;
|
|
151
|
+
switch (s.type) {
|
|
152
|
+
case "cc":
|
|
153
|
+
return rec.type === "cc" && rec.number === s.number;
|
|
154
|
+
case "note":
|
|
155
|
+
return (rec.type === "noteon" || rec.type === "noteoff") && (s.number === undefined || s.number === rec.number);
|
|
156
|
+
case "pitch":
|
|
157
|
+
return rec.type === "pitch";
|
|
158
|
+
default:
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The message's position, 0 to 1, and whether it is "on" (a gate). Null: not something that moves a value (a note-off for a number). */
|
|
164
|
+
function position(m, rec) {
|
|
165
|
+
switch (m.source.type) {
|
|
166
|
+
case "cc":
|
|
167
|
+
return { t: rec.value / 127, on: rec.value >= 64 };
|
|
168
|
+
case "pitch":
|
|
169
|
+
return { t: rec.value / 16383, on: rec.value >= 8192 };
|
|
170
|
+
default:
|
|
171
|
+
return rec.type === "noteon" ? { t: rec.value / 127, on: true } : { t: 0, on: false, off: true };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The widget's own range for a number or range control. */
|
|
176
|
+
const rangeOf = (w) => ({ lo: isNum(w.min) ? w.min : 0, hi: isNum(w.max) ? w.max : 100 });
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* What message `rec` does to a control `widget` through mapping `m`:
|
|
180
|
+
* null it doesn't (another source, or a note-off for something that isn't a gate)
|
|
181
|
+
* { problem } the widget can't take MIDI, or the mapping doesn't say enough for it
|
|
182
|
+
* { value } the control's new value, clamped and snapped (constrainValue)
|
|
183
|
+
* `widget` is null for a morph position (0 to 1). `current` is the control's value now: an xy pad's
|
|
184
|
+
* other axis and a curve's other points keep it.
|
|
185
|
+
*
|
|
186
|
+
* number, range the position inside min..max (the widget's own range by default)
|
|
187
|
+
* checkbox a gate: a controller at 64 or more, a note while held
|
|
188
|
+
* select the position picks one of the options, evenly (min and max don't apply)
|
|
189
|
+
* xy the position moves one axis (m.axis, x by default) inside min..max (that axis's range)
|
|
190
|
+
* curve the position moves one coordinate (m.axis, y by default) of one point (m.point)
|
|
191
|
+
*/
|
|
192
|
+
export function convert(m, rec, widget, current = widget?.value) {
|
|
193
|
+
if (!matches(m, rec)) return null;
|
|
194
|
+
const pos = position(m, rec);
|
|
195
|
+
if (widget?.kind === "checkbox") return { value: m.invert ? !pos.on : pos.on };
|
|
196
|
+
if (widget && !MIDI_WIDGETS.includes(widget.kind)) return { problem: `a ${widget.kind} widget can't be driven by MIDI (${MIDI_WIDGETS.join(", ")} can)` };
|
|
197
|
+
if (pos.off) return null; // a released key leaves a value where it is
|
|
198
|
+
const t0 = m.invert ? 1 - pos.t : pos.t;
|
|
199
|
+
const t = (SHAPE[m.curve] ?? SHAPE.linear)(Math.min(1, Math.max(0, t0)));
|
|
200
|
+
if (!widget) return { value: Math.min(1, Math.max(0, (m.min ?? 0) + ((m.max ?? 1) - (m.min ?? 0)) * t)) };
|
|
201
|
+
if (widget.kind === "select") {
|
|
202
|
+
const options = optionsOf(widget);
|
|
203
|
+
if (!options.length) return { problem: "the select widget has no options" };
|
|
204
|
+
return { value: options[Math.min(options.length - 1, Math.floor(t * options.length))] };
|
|
205
|
+
}
|
|
206
|
+
const span = (axis) => {
|
|
207
|
+
const a = axisOf(widget, axis);
|
|
208
|
+
const from = m.min ?? a.min;
|
|
209
|
+
const to = m.max ?? a.max;
|
|
210
|
+
return from + (to - from) * t;
|
|
211
|
+
};
|
|
212
|
+
if (widget.kind === "xy") {
|
|
213
|
+
const axis = m.axis ?? "x";
|
|
214
|
+
const now = current && typeof current === "object" && isNum(current.x) && isNum(current.y) ? current : widget.value;
|
|
215
|
+
const v = constrainValue(widget, { ...now, [axis]: span(axis) });
|
|
216
|
+
return v.problem ? { problem: v.problem } : { value: v.value };
|
|
217
|
+
}
|
|
218
|
+
if (widget.kind === "curve") {
|
|
219
|
+
const axis = m.axis ?? "y";
|
|
220
|
+
if (m.point === undefined) return { problem: "a curve mapping needs to know which point to move (point, 0 is the first)" };
|
|
221
|
+
const now = current && typeof current === "object" && Array.isArray(current.points) ? current : widget.value;
|
|
222
|
+
const at = now?.points?.[m.point];
|
|
223
|
+
if (!at) return { problem: `the curve has ${now?.points?.length ?? 0} points: there is no point ${m.point}` };
|
|
224
|
+
const moved = axis === "x" ? curveMove(now, widget, m.point, span("x"), at.y) : curveMove(now, widget, m.point, at.x, span("y"));
|
|
225
|
+
const v = constrainValue(widget, moved);
|
|
226
|
+
return v.problem ? { problem: v.problem } : { value: v.value };
|
|
227
|
+
}
|
|
228
|
+
const { lo, hi } = rangeOf(widget);
|
|
229
|
+
const from = m.min ?? lo;
|
|
230
|
+
const to = m.max ?? hi;
|
|
231
|
+
const raw = from + (to - from) * t;
|
|
232
|
+
const v = constrainValue(widget, raw);
|
|
233
|
+
return v.problem ? { problem: v.problem } : { value: v.value };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** A mapping's source in words: "cc 74, channel 1". */
|
|
237
|
+
export function describeSource(m) {
|
|
238
|
+
const s = m.source;
|
|
239
|
+
const what = s.type === "pitch" ? "pitch bend" : s.type === "note" ? (s.number === undefined ? "any note" : `note ${s.number}`) : `cc ${s.number}`;
|
|
240
|
+
return `${what}${s.channel !== undefined ? `, channel ${s.channel}` : ""}${m.port ? `, ${m.port}` : ""}`;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** What a mapping drives, in words: "cutoff", "voice.pos y", "env point 2 y", "morph Bank → hi". */
|
|
244
|
+
export function describeTarget(m, kind) {
|
|
245
|
+
if (m.morph) return `morph ${m.morph.bank}${m.morph.from ? `: ${m.morph.from}` : ""} → ${m.morph.to}`;
|
|
246
|
+
const base = m.param ? `${m.param.pane}.${m.param.name}` : m.pane;
|
|
247
|
+
if (m.point !== undefined || kind === "curve") return m.point === undefined ? base : `${base} point ${m.point + 1} ${m.axis ?? "y"}`;
|
|
248
|
+
const axis = m.axis ?? (kind === "xy" ? "x" : undefined);
|
|
249
|
+
return axis ? `${base} ${axis}` : base;
|
|
250
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// MIDI messages as plain records. Pure: bytes in, a structured-clone-safe
|
|
2
|
+
// object out, so the same record comes from a device, a recording or a test.
|
|
3
|
+
//
|
|
4
|
+
// { type, channel, number?, value, velocity?, port, time }
|
|
5
|
+
//
|
|
6
|
+
// type "noteon" | "noteoff" | "cc" | "pitch" | "pressure" | "aftertouch" | "program"
|
|
7
|
+
// channel 1 to 16
|
|
8
|
+
// number the note or controller (0 to 127); not for pitch or pressure
|
|
9
|
+
// value 0 to 127 (a note's velocity, a controller's value); pitch: 0 to 16383 (8192 is centre)
|
|
10
|
+
// velocity notes only: 0 to 1
|
|
11
|
+
// port the input's name (never its id), "" when unknown
|
|
12
|
+
// time milliseconds on the page's clock (the message's timeStamp), 0 when unknown
|
|
13
|
+
//
|
|
14
|
+
// Only channel messages are read. System messages (clock, sysex, start/stop) are
|
|
15
|
+
// ignored: SysEx is never requested, and a stream of clock ticks isn't a control.
|
|
16
|
+
|
|
17
|
+
export const MESSAGE_TYPES = ["noteon", "noteoff", "cc", "pitch", "pressure", "aftertouch", "program"];
|
|
18
|
+
export const PITCH_MAX = 16383;
|
|
19
|
+
|
|
20
|
+
const isInt = (v, lo, hi) => Number.isInteger(v) && v >= lo && v <= hi;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Read the bytes of one MIDI message. Returns a record, or null for a message
|
|
24
|
+
* that isn't a channel message (or is cut short).
|
|
25
|
+
* @param {ArrayLike<number>} data
|
|
26
|
+
*/
|
|
27
|
+
export function parseMidi(data, { port = "", time = 0 } = {}) {
|
|
28
|
+
if (!data || data.length < 1) return null;
|
|
29
|
+
const status = data[0];
|
|
30
|
+
if (!isInt(status, 0x80, 0xef)) return null;
|
|
31
|
+
const channel = (status & 0x0f) + 1;
|
|
32
|
+
const kind = status >> 4;
|
|
33
|
+
const a = data[1];
|
|
34
|
+
const b = data[2];
|
|
35
|
+
const base = { channel, port: String(port ?? "").slice(0, 80), time: Number.isFinite(time) ? time : 0 };
|
|
36
|
+
const seven = (v) => isInt(v, 0, 127);
|
|
37
|
+
switch (kind) {
|
|
38
|
+
case 0x8:
|
|
39
|
+
return seven(a) && seven(b) ? { type: "noteoff", ...base, number: a, value: b, velocity: b / 127 } : null;
|
|
40
|
+
case 0x9:
|
|
41
|
+
if (!seven(a) || !seven(b)) return null;
|
|
42
|
+
return b === 0 ? { type: "noteoff", ...base, number: a, value: 0, velocity: 0 } : { type: "noteon", ...base, number: a, value: b, velocity: b / 127 };
|
|
43
|
+
case 0xa:
|
|
44
|
+
return seven(a) && seven(b) ? { type: "aftertouch", ...base, number: a, value: b } : null;
|
|
45
|
+
case 0xb:
|
|
46
|
+
return seven(a) && seven(b) ? { type: "cc", ...base, number: a, value: b } : null;
|
|
47
|
+
case 0xc:
|
|
48
|
+
return seven(a) ? { type: "program", ...base, number: a, value: a } : null;
|
|
49
|
+
case 0xd:
|
|
50
|
+
return seven(a) ? { type: "pressure", ...base, value: a } : null;
|
|
51
|
+
case 0xe:
|
|
52
|
+
return seven(a) && seven(b) ? { type: "pitch", ...base, value: (b << 7) | a } : null;
|
|
53
|
+
default:
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Why `rec` isn't a MIDI record (a sentence), or "". For replayed and pasted records. */
|
|
59
|
+
export function recordProblem(rec) {
|
|
60
|
+
if (!rec || typeof rec !== "object" || Array.isArray(rec)) return "a record is { type, channel, number?, value, port?, time? }";
|
|
61
|
+
if (!MESSAGE_TYPES.includes(rec.type)) return `type is one of ${MESSAGE_TYPES.join(", ")}`;
|
|
62
|
+
if (!isInt(rec.channel, 1, 16)) return "channel is 1 to 16";
|
|
63
|
+
const max = rec.type === "pitch" ? PITCH_MAX : 127;
|
|
64
|
+
if (!isInt(rec.value, 0, max)) return `value is a whole number from 0 to ${max}`;
|
|
65
|
+
if (rec.type !== "pitch" && rec.type !== "pressure" && !isInt(rec.number, 0, 127)) return "number is a whole number from 0 to 127";
|
|
66
|
+
if (rec.port !== undefined && (typeof rec.port !== "string" || rec.port.length > 80)) return "port is the input's name (text)";
|
|
67
|
+
if (rec.time !== undefined && !Number.isFinite(rec.time)) return "time is a number of milliseconds";
|
|
68
|
+
return "";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A record as replayed: only the fields above, `velocity` filled in for notes. */
|
|
72
|
+
export function cleanRecord(rec) {
|
|
73
|
+
const out = { type: rec.type, channel: rec.channel, ...(rec.number !== undefined ? { number: rec.number } : {}), value: rec.value, port: rec.port ?? "", time: rec.time ?? 0 };
|
|
74
|
+
if (rec.type === "noteon" || rec.type === "noteoff") out.velocity = rec.type === "noteoff" && rec.value === 0 ? 0 : rec.value / 127;
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The bytes of a record, as a controller would send them (the inverse of parseMidi): `Uint8Array` of 1 to 3 bytes, or null for a
|
|
80
|
+
* record that isn't a channel message. A note-off is 0x8n (a note-on of velocity 0 was already read as one).
|
|
81
|
+
*/
|
|
82
|
+
export function toBytes(rec) {
|
|
83
|
+
if (recordProblem(rec)) return null;
|
|
84
|
+
const status = (kind) => (kind << 4) | ((rec.channel - 1) & 0x0f);
|
|
85
|
+
switch (rec.type) {
|
|
86
|
+
case "noteoff":
|
|
87
|
+
return Uint8Array.of(status(0x8), rec.number, rec.value);
|
|
88
|
+
case "noteon":
|
|
89
|
+
return Uint8Array.of(status(0x9), rec.number, rec.value);
|
|
90
|
+
case "aftertouch":
|
|
91
|
+
return Uint8Array.of(status(0xa), rec.number, rec.value);
|
|
92
|
+
case "cc":
|
|
93
|
+
return Uint8Array.of(status(0xb), rec.number, rec.value);
|
|
94
|
+
case "program":
|
|
95
|
+
return Uint8Array.of(status(0xc), rec.number);
|
|
96
|
+
case "pressure":
|
|
97
|
+
return Uint8Array.of(status(0xd), rec.value);
|
|
98
|
+
case "pitch":
|
|
99
|
+
return Uint8Array.of(status(0xe), rec.value & 0x7f, rec.value >> 7);
|
|
100
|
+
default:
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A short line for a monitor: "cc 74 = 93 (ch 1)". */
|
|
106
|
+
export function describeRecord(rec) {
|
|
107
|
+
const n = rec.number === undefined ? "" : ` ${rec.number}`;
|
|
108
|
+
return `${rec.type}${n} = ${rec.value} (ch ${rec.channel}${rec.port ? `, ${rec.port}` : ""})`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The event a pane receives on the canvas's MIDI channel: the record, with a
|
|
113
|
+
* note's `note` and a 0 to 1 `velocity`, ready for a voice (`{ note, on, velocity }`).
|
|
114
|
+
*/
|
|
115
|
+
export function eventPayload(rec) {
|
|
116
|
+
// `timestamp`, not `time`: a voice reads `time` as seconds from when it starts sounding, so the page clock's milliseconds would delay the note by hours.
|
|
117
|
+
const p = { type: rec.type, channel: rec.channel, value: rec.value, port: rec.port, timestamp: rec.time };
|
|
118
|
+
if (rec.number !== undefined) p.number = rec.number;
|
|
119
|
+
if (rec.type === "noteon" || rec.type === "noteoff") {
|
|
120
|
+
p.note = rec.number;
|
|
121
|
+
p.on = rec.type === "noteon"; // what noteEvents reads (its types are on / off; a note-on of velocity 0 is already a noteoff here)
|
|
122
|
+
p.velocity = rec.velocity ?? rec.value / 127;
|
|
123
|
+
}
|
|
124
|
+
return p;
|
|
125
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// The action map panes of one canvas: each pane reads the values wired into it (a device pane's keyboard,
|
|
2
|
+
// pointer or gamepad, an XY pad, a slider, MIDI records from the MIDI panel's event channel or midi.open()), steps its map (src/actions/map.js, pure) and its value is the
|
|
3
|
+
// state of every action. What carries over from one run to the next (which buttons were down, so that
|
|
4
|
+
// pressed and released are one-step edges, and which MIDI notes are held and where each controller is) is kept here, in the page, never in the canvas doc or the
|
|
5
|
+
// sandbox; a sandbox restart only reruns the pane, which then starts from nothing held.
|
|
6
|
+
//
|
|
7
|
+
// Rebinding ("press a key…"): the page arms a slot of an action (`learn`); the next thing that becomes
|
|
8
|
+
// active, in the values wired in or in a key the pane itself hears (`learned`), becomes a binding of that
|
|
9
|
+
// action, saved with the canvas as an ordinary edit. Nothing here reads a device: a person's device pane
|
|
10
|
+
// does, after their click.
|
|
11
|
+
import { LazyMap } from "./lazy-map.js";
|
|
12
|
+
import { step, actionMapSettings, detectBinding, fitBinding, sameBinding, idleValue, MAX_BINDINGS } from "../actions/map.js";
|
|
13
|
+
|
|
14
|
+
export class ActionHub {
|
|
15
|
+
/** @param {object} canvas the Canvas (src/model/canvas.js) */
|
|
16
|
+
constructor(canvas) {
|
|
17
|
+
this.canvas = canvas;
|
|
18
|
+
this.states = new LazyMap(); // id -> { down, readings } after the last step
|
|
19
|
+
this.values = new LazyMap(); // id -> the last value
|
|
20
|
+
this.steps = new LazyMap(); // id -> how many steps it has taken
|
|
21
|
+
this.learning = new LazyMap(); // id -> { action, index?, slot? } the slot being learned
|
|
22
|
+
this.bodies = new LazyMap(); // id -> how many bodies (views) of the pane are open
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Action map panes on this canvas. */
|
|
26
|
+
ids() {
|
|
27
|
+
return [...this.canvas.attached].filter((id) => this.canvas.panes.get(id)?.type === "actions");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The wired values by input name, as JSON (an input that was skipped, or that has no value, is left out). */
|
|
31
|
+
async #inputs(pane, sources, skippedKeys) {
|
|
32
|
+
const c = this.canvas;
|
|
33
|
+
const out = {};
|
|
34
|
+
for (const [n, input] of pane.inputs.entries()) {
|
|
35
|
+
if (c.writeTarget(input) || !sources[n] || skippedKeys.includes(sources[n])) continue;
|
|
36
|
+
try {
|
|
37
|
+
const reply = await c.runtime.exportValue(sources[n], "json");
|
|
38
|
+
if (typeof reply?.text === "string") out[input.name] = JSON.parse(reply.text);
|
|
39
|
+
} catch {
|
|
40
|
+
// no value to read: the same as a skipped input
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Run pane `id`: one step of its map over the inputs now. Sets the pane's value in the sandbox, sends the
|
|
48
|
+
* pressed and released events, and gives a pending rebinding the first thing that became active.
|
|
49
|
+
*/
|
|
50
|
+
async run(id, runId, view, sources, skippedKeys) {
|
|
51
|
+
const c = this.canvas;
|
|
52
|
+
const pane = c.panes.get(id);
|
|
53
|
+
const inputs = await this.#inputs(pane, sources, skippedKeys);
|
|
54
|
+
if (c.disposed || !c.attached.has(id)) return { superseded: true };
|
|
55
|
+
const before = this.states.get(id);
|
|
56
|
+
const result = step(pane.actionMap, before, inputs);
|
|
57
|
+
const readings = result.readings; // with each input's MIDI state (#516)
|
|
58
|
+
this.states.set(id, { ...result.state, readings });
|
|
59
|
+
this.values.set(id, result.value);
|
|
60
|
+
this.steps.set(id, (this.steps.get(id) ?? 0) + 1);
|
|
61
|
+
const reply = await c.runtime.set({ id, runId, value: result.value, view });
|
|
62
|
+
for (const e of result.events) c.events?.builtin(id, e.name, e.payload);
|
|
63
|
+
const wants = this.learning.get(id);
|
|
64
|
+
if (wants && before) {
|
|
65
|
+
const found = detectBinding(before.readings, readings);
|
|
66
|
+
if (found) queueMicrotask(() => this.learned(id, found));
|
|
67
|
+
}
|
|
68
|
+
this.#emit(id);
|
|
69
|
+
return reply;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A body of pane `id` is open (the pane's view draws again by closing one body and opening the next in the same task).
|
|
74
|
+
* Returns how to close it: a rebinding does not outlive the last body, since nothing could hear a key for it.
|
|
75
|
+
*/
|
|
76
|
+
attach(id) {
|
|
77
|
+
this.bodies.set(id, (this.bodies.get(id) ?? 0) + 1);
|
|
78
|
+
return () => {
|
|
79
|
+
this.bodies.set(id, Math.max(0, (this.bodies.get(id) ?? 1) - 1));
|
|
80
|
+
queueMicrotask(() => {
|
|
81
|
+
if (!this.bodies.get(id)) this.cancelLearn(id);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Arm a slot for rebinding: `{ action, index?, slot? }` (`index`: the binding to replace; `slot`: `{ scale?, to? }`). */
|
|
87
|
+
learn(id, target) {
|
|
88
|
+
if (!this.canvas.attached.has(id)) return false;
|
|
89
|
+
this.learning.set(id, { ...target });
|
|
90
|
+
this.#emit(id);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
cancelLearn(id) {
|
|
95
|
+
if (this.learning.delete(id)) this.#emit(id);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A binding the page heard (or found in the inputs) for the armed slot: the pane's map gets it. Returns whether it was used. */
|
|
99
|
+
learned(id, binding) {
|
|
100
|
+
const c = this.canvas;
|
|
101
|
+
const target = this.learning.get(id);
|
|
102
|
+
if (!target || !binding || !c.attached.has(id) || c.disposed) return false;
|
|
103
|
+
this.learning.delete(id);
|
|
104
|
+
const map = actionMapSettings(c.panes.get(id).actionMap);
|
|
105
|
+
const a = map.actions.find((x) => x.name === target.action);
|
|
106
|
+
if (!a) {
|
|
107
|
+
this.#emit(id);
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
const fitted = fitBinding(binding, target.slot, a.type);
|
|
111
|
+
if (target.index !== undefined && a.bindings[target.index]) a.bindings[target.index] = fitted;
|
|
112
|
+
else if (a.bindings.length < MAX_BINDINGS && !a.bindings.some((b) => sameBinding(b, fitted))) a.bindings.push(fitted);
|
|
113
|
+
c.updateStep(id, { actionMap: actionMapSettings(map) }, `bind ${a.name}`);
|
|
114
|
+
this.#emit(id);
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** What an action map pane is doing, for its view and for agents. */
|
|
119
|
+
status(id) {
|
|
120
|
+
const pane = this.canvas.panes.get(id);
|
|
121
|
+
const map = actionMapSettings(pane?.actionMap);
|
|
122
|
+
return { actions: map.actions.length, steps: this.steps.get(id) ?? 0, learning: this.learning.get(id) ?? null, value: this.values.get(id) ?? idleValue(map) };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Forget the panes that went (deleted, retyped). */
|
|
126
|
+
sync() {
|
|
127
|
+
if (this.canvas.disposed) return;
|
|
128
|
+
const ids = new Set(this.ids());
|
|
129
|
+
for (const map of [this.states, this.values, this.steps, this.learning, this.bodies]) for (const id of [...map.keys()]) if (!ids.has(id)) map.delete(id);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#emit(id) {
|
|
133
|
+
this.canvas.dispatchEvent?.(new CustomEvent("actions", { detail: { id } }));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
dispose() {
|
|
137
|
+
this.states.clear();
|
|
138
|
+
this.values.clear();
|
|
139
|
+
this.steps.clear();
|
|
140
|
+
this.learning.clear();
|
|
141
|
+
this.bodies.clear();
|
|
142
|
+
}
|
|
143
|
+
}
|