@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
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// Choosing the useful part of a recording and finding its pitch (the sample pane, src/panes/sample.js). Pure: no DOM, no Web Audio, no imports,
|
|
2
|
+
// so it runs under node --test and on the page. Audio comes in as `{ channels: Float32Array[], sampleRate }`.
|
|
3
|
+
//
|
|
4
|
+
// Times are seconds from the start of the recording; the loop points are seconds from the start of the trimmed sound.
|
|
5
|
+
|
|
6
|
+
export const TRIM_LIMITS = Object.freeze({
|
|
7
|
+
/** Longest recording the pane takes (seconds): a sampler holds the whole sound in memory. */
|
|
8
|
+
seconds: 60,
|
|
9
|
+
/** Most frames across all channels the pane decodes (a decoded sample is 4 bytes a frame): 64 MB. */
|
|
10
|
+
frames: 16_000_000,
|
|
11
|
+
/** The shortest sound it will make (seconds). */
|
|
12
|
+
minLength: 0.01,
|
|
13
|
+
name: 40,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/** Below this a signal counts as silence (-50 dBFS: room hiss, not a soft attack). */
|
|
17
|
+
export const SILENCE_DB = -50;
|
|
18
|
+
/** Audio kept before the first loud frame, so the attack isn't sheared (seconds). */
|
|
19
|
+
export const ATTACK_LEAD = 0.005;
|
|
20
|
+
/** …and after the last, so a tail doesn't stop short. */
|
|
21
|
+
export const RELEASE_TAIL = 0.02;
|
|
22
|
+
|
|
23
|
+
export const DEFAULT_TRIM = Object.freeze({ start: 0, end: 0, fadeIn: 0, fadeOut: 0.005, trimSilence: false, loop: false, loopStart: 0, loopEnd: 0, root: 60 });
|
|
24
|
+
|
|
25
|
+
const num = (v) => typeof v === "number" && Number.isFinite(v);
|
|
26
|
+
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
|
27
|
+
const round = (v, digits = 4) => Math.round(v * 10 ** digits) / 10 ** digits;
|
|
28
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
29
|
+
|
|
30
|
+
/** Why trim settings can't be used, or "". `end` 0 means to the end of the recording; loop points 0 and 0 mean the whole sound. */
|
|
31
|
+
export function trimProblem(raw) {
|
|
32
|
+
if (raw === undefined) return "";
|
|
33
|
+
if (!isObject(raw)) return "sample: an object of the trim settings";
|
|
34
|
+
for (const k of Object.keys(raw)) if (!(k in DEFAULT_TRIM)) return `sample has no setting "${k}" (trim settings: ${Object.keys(DEFAULT_TRIM).join(", ")})`;
|
|
35
|
+
for (const k of ["start", "end", "fadeIn", "fadeOut", "loopStart", "loopEnd"]) {
|
|
36
|
+
const v = raw[k];
|
|
37
|
+
if (v !== undefined && (!num(v) || v < 0 || v > TRIM_LIMITS.seconds)) return `sample.${k}: seconds from 0 to ${TRIM_LIMITS.seconds}`;
|
|
38
|
+
}
|
|
39
|
+
for (const k of ["trimSilence", "loop"]) if (raw[k] !== undefined && typeof raw[k] !== "boolean") return `sample.${k}: true or false`;
|
|
40
|
+
if (raw.root !== undefined && (!Number.isInteger(raw.root) || raw.root < 0 || raw.root > 127)) return "sample.root: a MIDI note from 0 to 127";
|
|
41
|
+
if (num(raw.end) && raw.end > 0 && num(raw.start) && raw.end <= raw.start) return "sample: end is not after start (end 0 means the end of the recording)";
|
|
42
|
+
if (num(raw.loopEnd) && raw.loopEnd > 0 && num(raw.loopStart) && raw.loopEnd <= raw.loopStart) return "sample: loopEnd is not after loopStart (0 and 0 loop the whole sound)";
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The settings with every number clamped and every missing one at its default. */
|
|
47
|
+
export function cleanTrim(raw) {
|
|
48
|
+
const r = isObject(raw) ? raw : {};
|
|
49
|
+
const sec = (k) => round(clamp(num(r[k]) ? r[k] : DEFAULT_TRIM[k], 0, TRIM_LIMITS.seconds));
|
|
50
|
+
const out = {
|
|
51
|
+
start: sec("start"),
|
|
52
|
+
end: sec("end"),
|
|
53
|
+
fadeIn: sec("fadeIn"),
|
|
54
|
+
fadeOut: sec("fadeOut"),
|
|
55
|
+
trimSilence: r.trimSilence === true,
|
|
56
|
+
loop: r.loop === true,
|
|
57
|
+
loopStart: sec("loopStart"),
|
|
58
|
+
loopEnd: sec("loopEnd"),
|
|
59
|
+
root: Number.isInteger(r.root) ? clamp(r.root, 0, 127) : DEFAULT_TRIM.root,
|
|
60
|
+
};
|
|
61
|
+
if (out.end > 0 && out.end <= out.start) out.end = 0;
|
|
62
|
+
if (out.loopEnd > 0 && out.loopEnd <= out.loopStart) out.loopEnd = 0;
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ── notes ────────────────────────────────────────────────────────────────────
|
|
67
|
+
|
|
68
|
+
const NAMES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
|
69
|
+
/** A MIDI note's name, middle C (60) being C4. */
|
|
70
|
+
export const noteName = (n) => `${NAMES[((Math.round(n) % 12) + 12) % 12]}${Math.floor(Math.round(n) / 12) - 1}`;
|
|
71
|
+
export const noteToHz = (n) => 440 * 2 ** ((n - 69) / 12);
|
|
72
|
+
export const hzToNote = (hz) => 69 + 12 * Math.log2(hz / 440);
|
|
73
|
+
|
|
74
|
+
// ── what is in the recording ─────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
const frames = (audio) => audio?.channels?.[0]?.length ?? 0;
|
|
77
|
+
|
|
78
|
+
const quietOf = new WeakMap(); // a recording (never changed once decoded) -> Map(db -> its silenceRange)
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The first and last loud frames as seconds (with a short lead and tail), or the whole recording when none is loud (a silent take is more likely a mistake than empty).
|
|
82
|
+
* Looked for once per recording and threshold: the pane asks again on every pointer move while a handle is dragged.
|
|
83
|
+
*/
|
|
84
|
+
export function silenceRange(audio, db = SILENCE_DB) {
|
|
85
|
+
if (audio === null || typeof audio !== "object") return scanSilence(audio, db);
|
|
86
|
+
let byDb = quietOf.get(audio);
|
|
87
|
+
if (!byDb) quietOf.set(audio, (byDb = new Map()));
|
|
88
|
+
if (!byDb.has(db)) byDb.set(db, scanSilence(audio, db));
|
|
89
|
+
return { ...byDb.get(db) };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function scanSilence(audio, db) {
|
|
93
|
+
const n = frames(audio);
|
|
94
|
+
const rate = audio?.sampleRate;
|
|
95
|
+
if (!n || !(rate > 0)) return { start: 0, end: 0 };
|
|
96
|
+
const duration = n / rate;
|
|
97
|
+
const threshold = 10 ** (db / 20);
|
|
98
|
+
let first = -1;
|
|
99
|
+
let last = -1;
|
|
100
|
+
for (let i = 0; i < n; i++) {
|
|
101
|
+
let peak = 0;
|
|
102
|
+
for (const ch of audio.channels) {
|
|
103
|
+
const v = Math.abs(ch[i]);
|
|
104
|
+
if (v > peak) peak = v;
|
|
105
|
+
}
|
|
106
|
+
if (peak >= threshold) {
|
|
107
|
+
if (first < 0) first = i;
|
|
108
|
+
last = i;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (first < 0) return { start: 0, end: duration };
|
|
112
|
+
return { start: round(Math.max(0, first / rate - ATTACK_LEAD)), end: round(Math.min(duration, last / rate + RELEASE_TAIL)) };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Peak envelope for drawing: `buckets` pairs of min and max over all channels. */
|
|
116
|
+
export function peaks(audio, buckets) {
|
|
117
|
+
const n = frames(audio);
|
|
118
|
+
const count = Math.max(1, Math.floor(buckets));
|
|
119
|
+
const out = [];
|
|
120
|
+
if (!n) return Array.from({ length: count }, () => ({ min: 0, max: 0 }));
|
|
121
|
+
const per = n / count;
|
|
122
|
+
for (let b = 0; b < count; b++) {
|
|
123
|
+
const from = Math.floor(b * per);
|
|
124
|
+
const to = Math.min(n, Math.max(from + 1, Math.floor((b + 1) * per)));
|
|
125
|
+
let min = 0;
|
|
126
|
+
let max = 0;
|
|
127
|
+
for (let i = from; i < to; i++) {
|
|
128
|
+
for (const ch of audio.channels) {
|
|
129
|
+
const v = ch[i];
|
|
130
|
+
if (v < min) min = v;
|
|
131
|
+
if (v > max) max = v;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
out.push({ min, max });
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The pitch of a recording, from the loudest steady stretch of its first channel (the YIN method: a difference function, normalised by its running mean).
|
|
141
|
+
* `{ hz, note (a float MIDI number), midi (rounded), cents (off that note), confidence 0..1 }`, or null for noise, silence or a sound with no clear pitch.
|
|
142
|
+
*/
|
|
143
|
+
export function detectPitch(audio, { minHz = 40, maxHz = 2000, threshold = 0.15 } = {}) {
|
|
144
|
+
const x = audio?.channels?.[0];
|
|
145
|
+
const rate = audio?.sampleRate;
|
|
146
|
+
if (!x || !x.length || !(rate > 0)) return null;
|
|
147
|
+
const maxLag = Math.min(Math.floor(rate / minHz), Math.floor(x.length / 2) - 1);
|
|
148
|
+
const minLag = Math.max(2, Math.floor(rate / maxHz));
|
|
149
|
+
if (maxLag <= minLag + 2) return null;
|
|
150
|
+
const win = Math.min(4096, x.length - maxLag - 1);
|
|
151
|
+
if (win < maxLag) return null;
|
|
152
|
+
// The window with the most energy: the pitch is clearest where the sound is loudest, away from the attack's noise and the tail's decay.
|
|
153
|
+
const hop = Math.max(256, win >> 2);
|
|
154
|
+
let at = 0;
|
|
155
|
+
let best = -1;
|
|
156
|
+
for (let s = 0; s + win + maxLag <= x.length; s += hop) {
|
|
157
|
+
let e = 0;
|
|
158
|
+
for (let i = s; i < s + win; i += 4) e += x[i] * x[i];
|
|
159
|
+
if (e > best) (best = e, (at = s));
|
|
160
|
+
}
|
|
161
|
+
if (best / (win / 4) < 1e-8) return null;
|
|
162
|
+
const d = new Float64Array(maxLag + 1);
|
|
163
|
+
for (let tau = 1; tau <= maxLag; tau++) {
|
|
164
|
+
let sum = 0;
|
|
165
|
+
for (let i = 0; i < win; i++) {
|
|
166
|
+
const diff = x[at + i] - x[at + i + tau];
|
|
167
|
+
sum += diff * diff;
|
|
168
|
+
}
|
|
169
|
+
d[tau] = sum;
|
|
170
|
+
}
|
|
171
|
+
const cm = new Float64Array(maxLag + 1);
|
|
172
|
+
cm[0] = 1;
|
|
173
|
+
let running = 0;
|
|
174
|
+
for (let tau = 1; tau <= maxLag; tau++) {
|
|
175
|
+
running += d[tau];
|
|
176
|
+
cm[tau] = running === 0 ? 1 : (d[tau] * tau) / running;
|
|
177
|
+
}
|
|
178
|
+
let lag = -1;
|
|
179
|
+
for (let tau = minLag; tau < maxLag; tau++) {
|
|
180
|
+
if (cm[tau] < threshold) {
|
|
181
|
+
while (tau + 1 < maxLag && cm[tau + 1] < cm[tau]) tau++;
|
|
182
|
+
lag = tau;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (lag < 0) return null;
|
|
187
|
+
// Parabolic interpolation between the lags either side of the dip.
|
|
188
|
+
const a = cm[lag - 1] ?? cm[lag];
|
|
189
|
+
const c = cm[lag + 1] ?? cm[lag];
|
|
190
|
+
const denom = a - 2 * cm[lag] + c;
|
|
191
|
+
const refined = denom !== 0 ? lag + (a - c) / (2 * denom) : lag;
|
|
192
|
+
const hz = rate / refined;
|
|
193
|
+
if (!(hz >= minHz && hz <= maxHz)) return null;
|
|
194
|
+
const note = hzToNote(hz);
|
|
195
|
+
const midi = Math.round(note);
|
|
196
|
+
return { hz: round(hz, 2), note: round(note, 3), midi, name: noteName(midi), cents: Math.round((note - midi) * 100), confidence: round(clamp(1 - cm[lag], 0, 1), 3) };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ── making the sound ─────────────────────────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The part of the recording the settings keep: `{ from, to }` in frames. With `trimSilence` the quiet either side of the sound is cut first, then
|
|
203
|
+
* `start` and `end` (seconds from the start of the recording) narrow it further. Never empty: a window shorter than the minimum grows.
|
|
204
|
+
*/
|
|
205
|
+
export function windowOf(audio, settings) {
|
|
206
|
+
const s = cleanTrim(settings);
|
|
207
|
+
const n = frames(audio);
|
|
208
|
+
const rate = audio.sampleRate;
|
|
209
|
+
const total = n / rate;
|
|
210
|
+
let lo = 0;
|
|
211
|
+
let hi = total;
|
|
212
|
+
if (s.trimSilence) {
|
|
213
|
+
const quiet = silenceRange(audio);
|
|
214
|
+
lo = quiet.start;
|
|
215
|
+
hi = quiet.end || total;
|
|
216
|
+
}
|
|
217
|
+
if (s.start > 0) lo = Math.max(lo, s.start);
|
|
218
|
+
if (s.end > 0) hi = Math.min(hi, s.end);
|
|
219
|
+
lo = clamp(lo, 0, total);
|
|
220
|
+
hi = clamp(hi, 0, total);
|
|
221
|
+
if (hi - lo < TRIM_LIMITS.minLength) {
|
|
222
|
+
if (lo + TRIM_LIMITS.minLength <= total) hi = lo + TRIM_LIMITS.minLength;
|
|
223
|
+
else (lo = Math.max(0, total - TRIM_LIMITS.minLength), (hi = total));
|
|
224
|
+
}
|
|
225
|
+
const from = Math.min(n - 1, Math.round(lo * rate));
|
|
226
|
+
const to = Math.max(from + 1, Math.min(n, Math.round(hi * rate)));
|
|
227
|
+
return { from, to };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Why a recording can't be made into a sound, or "": empty, silent in every channel count, too long or too big. */
|
|
231
|
+
export function audioProblem(audio) {
|
|
232
|
+
if (!audio || !Array.isArray(audio.channels) || !audio.channels.length) return "no audio";
|
|
233
|
+
const n = frames(audio);
|
|
234
|
+
if (!n) return "the recording is empty";
|
|
235
|
+
if (!(audio.sampleRate >= 3000 && audio.sampleRate <= 768000)) return "the recording has no usable sample rate";
|
|
236
|
+
if (audio.channels.some((c) => c.length !== n)) return "the recording's channels differ in length";
|
|
237
|
+
if (n / audio.sampleRate > TRIM_LIMITS.seconds) return `the recording is ${Math.round(n / audio.sampleRate)} seconds: at most ${TRIM_LIMITS.seconds} (trim it before it comes here)`;
|
|
238
|
+
if (n * audio.channels.length > TRIM_LIMITS.frames) return `the recording is too big to hold as a sound (over ${TRIM_LIMITS.frames} frames across its channels)`;
|
|
239
|
+
return "";
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Cut the recording to the settings and fade it. Returns `{ channels, sampleRate, frames, seconds, loop, window }`: new arrays (the recording is not touched);
|
|
244
|
+
* `loop` is `{ start, end }` in seconds from the start of the cut sound, or null; `window` is `{ from, to }` in frames of the recording.
|
|
245
|
+
* A fade in and a fade out are linear and never longer than the sound together.
|
|
246
|
+
*/
|
|
247
|
+
export function applyTrim(audio, settings) {
|
|
248
|
+
const bad = audioProblem(audio);
|
|
249
|
+
if (bad) throw new Error(bad);
|
|
250
|
+
const s = cleanTrim(settings);
|
|
251
|
+
const window = windowOf(audio, s);
|
|
252
|
+
const rate = audio.sampleRate;
|
|
253
|
+
const length = window.to - window.from;
|
|
254
|
+
const seconds = length / rate;
|
|
255
|
+
const channels = audio.channels.map((c) => Float32Array.from(c.subarray(window.from, window.to)));
|
|
256
|
+
let inFrames = Math.min(length, Math.round(s.fadeIn * rate));
|
|
257
|
+
let outFrames = Math.min(length, Math.round(s.fadeOut * rate));
|
|
258
|
+
if (inFrames + outFrames > length) {
|
|
259
|
+
const k = length / (inFrames + outFrames);
|
|
260
|
+
inFrames = Math.floor(inFrames * k);
|
|
261
|
+
outFrames = Math.floor(outFrames * k);
|
|
262
|
+
}
|
|
263
|
+
for (const ch of channels) {
|
|
264
|
+
for (let i = 0; i < inFrames; i++) ch[i] *= i / inFrames;
|
|
265
|
+
for (let i = 0; i < outFrames; i++) ch[length - 1 - i] *= i / outFrames;
|
|
266
|
+
}
|
|
267
|
+
let loop = null;
|
|
268
|
+
if (s.loop) {
|
|
269
|
+
const a = Math.min(seconds, s.loopStart);
|
|
270
|
+
const b = s.loopEnd > 0 ? Math.min(seconds, s.loopEnd) : seconds;
|
|
271
|
+
if (b - a >= TRIM_LIMITS.minLength) loop = { start: round(a), end: round(b) };
|
|
272
|
+
}
|
|
273
|
+
return { channels, sampleRate: rate, frames: length, seconds: round(seconds, 5), loop, window };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Where the loop's ends are when the sound is cut at a zero crossing near each: a loop that starts and ends near zero doesn't click. Seconds in, seconds out. */
|
|
277
|
+
export function snapToZero(audio, seconds, reach = 0.01) {
|
|
278
|
+
const x = audio?.channels?.[0];
|
|
279
|
+
const rate = audio?.sampleRate;
|
|
280
|
+
if (!x || !(rate > 0)) return seconds;
|
|
281
|
+
const at = Math.round(seconds * rate);
|
|
282
|
+
const span = Math.round(reach * rate);
|
|
283
|
+
let best = at;
|
|
284
|
+
let bestAbs = Math.abs(x[Math.min(x.length - 1, Math.max(0, at))] ?? 1);
|
|
285
|
+
for (let d = 1; d <= span; d++) {
|
|
286
|
+
for (const i of [at - d, at + d]) {
|
|
287
|
+
if (i < 0 || i >= x.length) continue;
|
|
288
|
+
const a = Math.abs(x[i]);
|
|
289
|
+
if (a < bestAbs) (bestAbs = a, (best = i));
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return round(best / rate, 5);
|
|
293
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// Instruments as sample banks (INS-04, INS-03): a few pitches of each of 19 FluidR3_GM General MIDI instruments, fetched one small mp3 each from the
|
|
2
|
+
// MIDI.js project's static host, and the Salamander grand piano (nine samples, from Tone.js's static host), pitch-shifted between by a sampler's zones. Pure and
|
|
3
|
+
// network-free: the fetch is handed in (the page's network policy, via the sample pane's runner), so there is no second way out.
|
|
4
|
+
//
|
|
5
|
+
// Fail closed, in this order: a font must be one of the names here (so the URL's host and path never come from the canvas: each font belongs to one of the TWO
|
|
6
|
+
// fixed sources below, each a fixed host and path); the request goes through the network policy, which allows the host or asks the person; an answer that came from
|
|
7
|
+
// another host than its font's own, is not audio, is over the per-file or total download cap is refused. Any failure stops the load and nothing partial is used.
|
|
8
|
+
// What the files may decode to is bounded by `decodedProblem` (src/audio/instruments.js): a file must start like an MP3 (a Content-Type proves nothing: Ogg, FLAC
|
|
9
|
+
// and WAV decode just the same) and its frames, counted from its own header, are held to the same limits as what the browser decodes (the audio frame checks again
|
|
10
|
+
// before it decodes). The two allowances, both for the Salamander grand only (fixed tables keyed by font id): its samples may be 24 seconds (its lowest, A1, is 23.38), where
|
|
11
|
+
// every other font's stop at 20, and its nine together may decode to 48 MB (they are 44.7), where every other font's stop at 32; the file and total download
|
|
12
|
+
// caps are the same for every font.
|
|
13
|
+
//
|
|
14
|
+
// The samples are not in this repository. The General MIDI soundfont is FluidR3_GM (MIT licence, Frank Wen), served by github.com/gleitz/midi-js-soundfonts.
|
|
15
|
+
// The Salamander Grand Piano V3 is by Alexander Holm, licensed CC BY 3.0 (credit required), served as part of the Tone.js project's audio files
|
|
16
|
+
// (github.com/Tonejs/audio, tonejs.github.io/audio/salamander/). Real sizes, measured from the live host on 2026-11-09: A1 115,061 bytes / 23.38 s, A2 81,678 / 16.01 s,
|
|
17
|
+
// A3 74,526 / 15.67 s, C4 78,718 / 16.25 s, D#4 73,519 / 15.88 s, F#4 68,219 / 14.76 s, A4 61,148 / 13.40 s, A5 45,747 / 10.61 s, A6 30,013 / 6.87 s (stereo,
|
|
18
|
+
// 44.1 kHz): 628,629 bytes, 44.69 MB decoded as float32.
|
|
19
|
+
|
|
20
|
+
import { decodedProblem, mp3Info, sampleOfFont } from "./instruments.js";
|
|
21
|
+
|
|
22
|
+
export const SOUNDFONT_HOST = "gleitz.github.io";
|
|
23
|
+
export const SOUNDFONT_ORIGIN = `https://${SOUNDFONT_HOST}`;
|
|
24
|
+
export const SOUNDFONT_BASE = `${SOUNDFONT_ORIGIN}/midi-js-soundfonts/FluidR3_GM`;
|
|
25
|
+
|
|
26
|
+
/** The Salamander grand's host and path: Tone.js's. */
|
|
27
|
+
export const SALAMANDER_HOST = "tonejs.github.io";
|
|
28
|
+
export const SALAMANDER_BASE = `https://${SALAMANDER_HOST}/audio/salamander`;
|
|
29
|
+
|
|
30
|
+
/** Every host a soundfont is loaded from: a fixed list of two (the network policy asks the person about each, like any host). */
|
|
31
|
+
export const SOUNDFONT_HOSTS = Object.freeze([SOUNDFONT_HOST, SALAMANDER_HOST]);
|
|
32
|
+
|
|
33
|
+
export const SOUNDFONT_LIMITS = Object.freeze({
|
|
34
|
+
/** One file (bytes): the largest sample of any set is a fraction of this (the Salamander grand's biggest is 115,061). */
|
|
35
|
+
fileBytes: 512 * 1024,
|
|
36
|
+
/** All files of one instrument (bytes). */
|
|
37
|
+
totalBytes: 2 * 1024 * 1024,
|
|
38
|
+
/** One file's request (ms): a host that trickles its answer cannot hold a load. */
|
|
39
|
+
fileMs: 30000,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const DEFAULT_NOTES = [36, 48, 60, 72, 84]; // C2..C6, one per octave
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The instruments: 19 General MIDI ones (the folder name is the id; they are the MIDI.js host's) and the Salamander grand (`source: "salamander"`, Tone.js's host).
|
|
46
|
+
* A font's `source` is a name in SOURCES below, never a host or a path.
|
|
47
|
+
*/
|
|
48
|
+
export const FONTS = Object.freeze({
|
|
49
|
+
acoustic_grand_piano: { name: "Grand Piano", program: 0 },
|
|
50
|
+
electric_piano_1: { name: "Rhodes", program: 4 },
|
|
51
|
+
acoustic_guitar_nylon: { name: "Nylon Guitar", program: 24 },
|
|
52
|
+
acoustic_guitar_steel: { name: "Steel Guitar", program: 25 },
|
|
53
|
+
acoustic_bass: { name: "Acoustic Bass", program: 32, notes: [24, 36, 48, 60] },
|
|
54
|
+
violin: { name: "Violin", program: 40, notes: [55, 60, 72, 84] },
|
|
55
|
+
viola: { name: "Viola", program: 41, notes: [48, 60, 72] },
|
|
56
|
+
cello: { name: "Cello", program: 42, notes: [36, 48, 60, 72] },
|
|
57
|
+
string_ensemble_1: { name: "String Ensemble", program: 48 },
|
|
58
|
+
choir_aahs: { name: "Choir Aahs", program: 52 },
|
|
59
|
+
flute: { name: "Flute", program: 73, notes: [60, 72, 84] },
|
|
60
|
+
clarinet: { name: "Clarinet", program: 71, notes: [48, 60, 72] },
|
|
61
|
+
oboe: { name: "Oboe", program: 68, notes: [60, 72, 84] },
|
|
62
|
+
trumpet: { name: "Trumpet", program: 56, notes: [48, 60, 72] },
|
|
63
|
+
trombone: { name: "Trombone", program: 57, notes: [36, 48, 60] },
|
|
64
|
+
alto_sax: { name: "Alto Sax", program: 65, notes: [48, 60, 72] },
|
|
65
|
+
church_organ: { name: "Church Organ", program: 19 },
|
|
66
|
+
vibraphone: { name: "Vibraphone", program: 11 },
|
|
67
|
+
orchestral_harp: { name: "Harp", program: 46 },
|
|
68
|
+
// The Salamander Grand Piano V3 (INS-03): the nine samples music-studio loads, in ascending pitch: the A's an octave apart (A1 to A6) and C4, D#4, F#4 between
|
|
69
|
+
// A3 and A4. They decode to 44.7 MB, over the 32 MB default, so this font has its own 48 MB (SAMPLE_BYTES in src/audio/instruments.js).
|
|
70
|
+
// (its release, 1 s as music-studio's Tone.Sampler for this set, is in FONT_RELEASE, src/audio/instruments.js: the frame side needs it too)
|
|
71
|
+
salamander_grand: { name: "Salamander Grand Piano", program: 0, source: "salamander", notes: [33, 45, 57, 60, 63, 66, 69, 81, 93] },
|
|
72
|
+
});
|
|
73
|
+
export const FONT_IDS = Object.freeze(Object.keys(FONTS));
|
|
74
|
+
|
|
75
|
+
const SHARP_NAMES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
|
76
|
+
/** The file name of a pitch in the set: sharps, middle C being C4 ("C#4.mp3"). */
|
|
77
|
+
export const noteFile = (midi) => `${SHARP_NAMES[midi % 12]}${Math.floor(midi / 12) - 1}.mp3`;
|
|
78
|
+
|
|
79
|
+
/** The file name of a pitch in the Salamander set: flat names are not used, a sharp is "s" (A1.mp3, Ds4.mp3, Fs4.mp3). */
|
|
80
|
+
const salamanderFile = (midi) => noteFile(midi).replace("#", "s");
|
|
81
|
+
|
|
82
|
+
/** The two places a font can come from: each a fixed host, a fixed path, and how a pitch is named there. */
|
|
83
|
+
const SOURCES = Object.freeze({
|
|
84
|
+
midijs: Object.freeze({ host: SOUNDFONT_HOST, origin: SOUNDFONT_ORIGIN, url: (font, midi) => `${SOUNDFONT_BASE}/${font}-mp3/${noteFile(midi)}` }),
|
|
85
|
+
salamander: Object.freeze({ host: SALAMANDER_HOST, origin: `https://${SALAMANDER_HOST}`, url: (font, midi) => `${SALAMANDER_BASE}/${salamanderFile(midi)}` }),
|
|
86
|
+
});
|
|
87
|
+
const sourceOf = (font) => SOURCES[FONTS[font].source ?? "midijs"];
|
|
88
|
+
|
|
89
|
+
/** The credit each source asks for or gives (the sample pane shows it where the instrument is chosen). */
|
|
90
|
+
const CREDITS = Object.freeze({
|
|
91
|
+
midijs: Object.freeze({ text: "General MIDI sounds: FluidR3_GM by Frank Wen (MIT licence), via the MIDI.js soundfonts.", url: "https://github.com/gleitz/midi-js-soundfonts", licenseUrl: "https://opensource.org/license/mit", short: "MIT" }),
|
|
92
|
+
salamander: Object.freeze({ text: "Salamander Grand Piano V3 by Alexander Holm, licensed CC BY 3.0 (credit required), hosted by the Tone.js project.", url: "https://archive.org/details/SalamanderGrandPianoV3", licenseUrl: "https://creativecommons.org/licenses/by/3.0/", short: "CC BY 3.0" }),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const known = (font) => typeof font === "string" && Object.hasOwn(FONTS, font);
|
|
96
|
+
|
|
97
|
+
/** Why `font` can't be loaded, or "". */
|
|
98
|
+
export const fontProblem = (font) => (known(font) ? "" : `font: one of ${FONT_IDS.join(", ")}`);
|
|
99
|
+
|
|
100
|
+
/** The pitches (MIDI notes) a font is sampled at. */
|
|
101
|
+
export function bankNotes(font) {
|
|
102
|
+
if (!known(font)) throw new Error(fontProblem(font));
|
|
103
|
+
return [...(FONTS[font].notes ?? DEFAULT_NOTES)];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The host a font is loaded from (the network policy decides on it, and the person is asked about it, like any host). */
|
|
107
|
+
export function fontHost(font) {
|
|
108
|
+
if (!known(font)) throw new Error(fontProblem(font));
|
|
109
|
+
return sourceOf(font).host;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** `{ text, url, licenseUrl, short }`: who made the font and under what licence. Shown where the instrument is chosen and in the example's note. */
|
|
113
|
+
export function fontCredit(font) {
|
|
114
|
+
if (!known(font)) throw new Error(`"${String(font).slice(0, 40)}" is not one of the soundfont instruments (${FONT_IDS.join(", ")})`);
|
|
115
|
+
return CREDITS[FONTS[font].source ?? "midijs"];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The URL of one pitch of a font; throws for a font or pitch that is not in the set (so no URL is ever built from free text). */
|
|
119
|
+
export function fontUrl(font, midi) {
|
|
120
|
+
if (!known(font)) throw new Error(`"${String(font).slice(0, 40)}" is not one of the soundfont instruments (${FONT_IDS.join(", ")})`);
|
|
121
|
+
if (!bankNotes(font).includes(midi)) throw new Error(`${font} has no sample at that pitch`);
|
|
122
|
+
return sourceOf(font).url(font, midi);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The sampler zones for a font's bank: one per pitch, together covering the keyboard (each pitch plays the notes nearest it), reading the
|
|
127
|
+
* voices pane input `input`, which carries the bank (`{ bank: { <pitch>: audio } }`).
|
|
128
|
+
*/
|
|
129
|
+
export function fontZones(input, font) {
|
|
130
|
+
const roots = bankNotes(font);
|
|
131
|
+
return roots.map((root, i) => ({
|
|
132
|
+
input,
|
|
133
|
+
bank: true,
|
|
134
|
+
lo: i === 0 ? 0 : Math.floor((roots[i - 1] + root) / 2) + 1,
|
|
135
|
+
hi: i === roots.length - 1 ? 127 : Math.floor((root + roots[i + 1]) / 2),
|
|
136
|
+
root,
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const fail = (kind, message) => Object.assign(new Error(message), { loadKind: kind });
|
|
141
|
+
const headerOf = (headers, name) => {
|
|
142
|
+
for (const h of headers ?? []) if (Array.isArray(h) && String(h[0]).toLowerCase() === name) return String(h[1]);
|
|
143
|
+
return headers && !Array.isArray(headers) ? headers[name] : undefined;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Fetch the files of one instrument through `fetch(url, init, context)` (the network policy's function). Resolves
|
|
148
|
+
* `{ font, bank: { <pitch>: { bytes: ArrayBuffer, type } }, bytes }`; rejects with an Error whose `loadKind` says which check failed:
|
|
149
|
+
* "refused" (the policy), "network", "http", "size", "type", "host".
|
|
150
|
+
*/
|
|
151
|
+
export async function loadSoundfont({ font, fetch, signal, pane }) {
|
|
152
|
+
const label = FONTS[font]?.name ?? font;
|
|
153
|
+
const bank = {};
|
|
154
|
+
const estimates = []; // what each file so far decodes to, from its own header
|
|
155
|
+
let pinned = true; // every file so far is the font's pinned recording (#1023): only then does its longer allowance apply
|
|
156
|
+
let total = 0;
|
|
157
|
+
for (const midi of bankNotes(font)) {
|
|
158
|
+
if (signal?.aborted) throw Object.assign(new Error("the load was cancelled"), { name: "AbortError" });
|
|
159
|
+
const url = fontUrl(font, midi);
|
|
160
|
+
let res;
|
|
161
|
+
try {
|
|
162
|
+
res = await fetch(url, { method: "GET", headers: [] }, { pane, signal, kind: "fetch", maxBytes: SOUNDFONT_LIMITS.fileBytes, timeoutMs: SOUNDFONT_LIMITS.fileMs });
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (signal?.aborted || error?.name === "AbortError") throw Object.assign(new Error("the load was cancelled"), { name: "AbortError" });
|
|
165
|
+
if (/response too large/.test(String(error?.message))) throw Object.assign(fail("size", `${label} didn't load: ${noteFile(midi)} is too big (at most ${SOUNDFONT_LIMITS.fileBytes} bytes)`), { cause: error }); // the transport stopped reading at the cap
|
|
166
|
+
const kind = error?.fetchFailure === "refused" ? "refused" : error?.fetchFailure === "secret" ? "refused" : "network";
|
|
167
|
+
throw Object.assign(fail(kind, `${label} didn't load (${noteFile(midi)}): ${error?.message ?? error}`), { cause: error });
|
|
168
|
+
}
|
|
169
|
+
if (signal?.aborted) throw Object.assign(new Error("the load was cancelled"), { name: "AbortError" });
|
|
170
|
+
// The answer must come from the font's own host's origin (scheme, host and port), and must say where it came from: a redirect to another host (the other font's
|
|
171
|
+
// included), or an answer that names nothing, is refused.
|
|
172
|
+
const expected = sourceOf(font).origin;
|
|
173
|
+
let origin = "";
|
|
174
|
+
try {
|
|
175
|
+
origin = new URL(String(res.url ?? "")).origin;
|
|
176
|
+
} catch {}
|
|
177
|
+
if (origin !== expected) throw fail("host", `${label} didn't load: the answer came from ${origin && origin !== "null" ? origin : "no URL we could check"}, not ${expected}`);
|
|
178
|
+
const status = Number(res.status);
|
|
179
|
+
if (!(status >= 200 && status < 300)) throw fail("http", `${label} didn't load (${noteFile(midi)}): ${status} ${res.statusText ?? ""}`.trim());
|
|
180
|
+
const declared = Number(headerOf(res.headers, "content-length"));
|
|
181
|
+
if (Number.isFinite(declared) && declared > SOUNDFONT_LIMITS.fileBytes) throw fail("size", `${label} didn't load: ${noteFile(midi)} says it is ${declared} bytes, too big (at most ${SOUNDFONT_LIMITS.fileBytes})`);
|
|
182
|
+
const raw = res.body;
|
|
183
|
+
const body = raw instanceof ArrayBuffer ? raw : ArrayBuffer.isView(raw) ? raw.buffer.slice(raw.byteOffset, raw.byteOffset + raw.byteLength) : null;
|
|
184
|
+
if (!body) throw fail("type", `${label} didn't load: ${noteFile(midi)} came back as something other than audio bytes`);
|
|
185
|
+
if (body.byteLength > SOUNDFONT_LIMITS.fileBytes) throw fail("size", `${label} didn't load: ${noteFile(midi)} is ${body.byteLength} bytes, too big (at most ${SOUNDFONT_LIMITS.fileBytes})`);
|
|
186
|
+
total += body.byteLength;
|
|
187
|
+
if (total > SOUNDFONT_LIMITS.totalBytes) throw fail("size", `${label} didn't load: the files add up to over ${SOUNDFONT_LIMITS.totalBytes} bytes, too big`);
|
|
188
|
+
const type = (headerOf(res.headers, "content-type") ?? "").split(";")[0].trim().toLowerCase();
|
|
189
|
+
if (!body.byteLength || !/^audio\//.test(type)) throw fail("type", `${label} didn't load: ${noteFile(midi)} is ${body.byteLength ? type || "of no type" : "empty"}, not audio`);
|
|
190
|
+
const info = mp3Info(new Uint8Array(body));
|
|
191
|
+
if (info.problem) throw fail("type", `${label} didn't load: ${noteFile(midi)} ${info.problem}`);
|
|
192
|
+
estimates.push({ frames: Math.round(info.seconds * info.sampleRate), channels: info.channels, sampleRate: info.sampleRate });
|
|
193
|
+
pinned = pinned && sampleOfFont(font, midi, new Uint8Array(body));
|
|
194
|
+
const heavy = decodedProblem(estimates, pinned ? font : undefined);
|
|
195
|
+
if (heavy) throw fail("size", `${label} didn't load: ${noteFile(midi)} (${heavy}${pinned ? "" : "; its file isn't the pinned recording, so the font's longer allowance doesn't apply"})`);
|
|
196
|
+
bank[midi] = { bytes: body, type };
|
|
197
|
+
}
|
|
198
|
+
return { font, bank, bytes: total };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Why a bank (`{ <pitch>: { bytes, type } }`, as loadSoundfont answers) is not one of `font`'s, or "". The same checks a load makes of what it fetched, for a bank
|
|
203
|
+
* kept in the person's browser (src/soundfont-cache/): its pitches are exactly the font's, each file is audio bytes within the caps and starts like an MP3, and the
|
|
204
|
+
* set decodes to no more than the font's allowance. A bank that fails is not used.
|
|
205
|
+
*/
|
|
206
|
+
export function bankProblem(font, bank) {
|
|
207
|
+
if (!known(font)) return fontProblem(font);
|
|
208
|
+
if (!bank || typeof bank !== "object") return "no bank";
|
|
209
|
+
const notes = bankNotes(font);
|
|
210
|
+
const have = Object.keys(bank);
|
|
211
|
+
if (have.length !== notes.length || !notes.every((n) => Object.hasOwn(bank, n))) return "the pitches are not the font's";
|
|
212
|
+
let total = 0;
|
|
213
|
+
let pinned = true;
|
|
214
|
+
const estimates = [];
|
|
215
|
+
for (const midi of notes) {
|
|
216
|
+
const { bytes, type } = bank[midi] ?? {};
|
|
217
|
+
if (!(bytes instanceof ArrayBuffer) || !bytes.byteLength) return `${noteFile(midi)} is not audio bytes`;
|
|
218
|
+
if (bytes.byteLength > SOUNDFONT_LIMITS.fileBytes) return `${noteFile(midi)} is over the file cap`;
|
|
219
|
+
total += bytes.byteLength;
|
|
220
|
+
if (total > SOUNDFONT_LIMITS.totalBytes) return "the files are over the total cap";
|
|
221
|
+
if (typeof type !== "string" || !/^audio\//.test(type)) return `${noteFile(midi)} is not audio`;
|
|
222
|
+
const info = mp3Info(new Uint8Array(bytes));
|
|
223
|
+
if (info.problem) return `${noteFile(midi)} ${info.problem}`;
|
|
224
|
+
estimates.push({ frames: Math.round(info.seconds * info.sampleRate), channels: info.channels, sampleRate: info.sampleRate });
|
|
225
|
+
pinned = pinned && sampleOfFont(font, midi, new Uint8Array(bytes));
|
|
226
|
+
const heavy = decodedProblem(estimates, pinned ? font : undefined);
|
|
227
|
+
if (heavy) return pinned ? heavy : `${noteFile(midi)} (${heavy}; its file isn't the pinned recording, so the font's longer allowance doesn't apply)`;
|
|
228
|
+
}
|
|
229
|
+
return "";
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* What a font's source is, as a hex SHA-256: the font's id, and the URL of each of its files (host, path, pitches). A kept bank is looked up by this and the
|
|
234
|
+
* font id, so it is used only while the font still comes from the same files; a change to the table makes a new digest and the old bank is simply not found.
|
|
235
|
+
*/
|
|
236
|
+
export async function fontDigest(font) {
|
|
237
|
+
if (!known(font)) throw new Error(fontProblem(font));
|
|
238
|
+
const text = [font, ...bankNotes(font).map((midi) => fontUrl(font, midi))].join("\n");
|
|
239
|
+
const hash = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
240
|
+
return [...new Uint8Array(hash)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export { decodedProblem };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Recorded takes kept in this browser (docs/guide/media-and-audio.md, "Recording live audio"): the opt-in `record.keepTakes` of an audio pane.
|
|
2
|
+
// A take is the person's recording and stays in the pane's frame until they download or remove it; with this on it
|
|
3
|
+
// also goes to the same IndexedDB the media pane uses (src/media/store.js), under the canvas's id and names of its own
|
|
4
|
+
// ("take~<pane>~<number>" for the WAV, "...~meta" for its numbers), so a reload or a collapsed pane doesn't lose it.
|
|
5
|
+
// It is never in the canvas document, an export or a share link. Browser only.
|
|
6
|
+
import { mediaStore } from "../media/store.js";
|
|
7
|
+
import { cleanTakeFields } from "./takes.js";
|
|
8
|
+
import { trait } from "../panes/registry.js";
|
|
9
|
+
|
|
10
|
+
const pad = (index) => String(Math.round(index)).padStart(4, "0");
|
|
11
|
+
export const takeKey = (pane, index) => `take~${pane}~${pad(index)}`;
|
|
12
|
+
export const metaKey = (pane, index) => `${takeKey(pane, index)}~meta`;
|
|
13
|
+
|
|
14
|
+
/** The ids of the audio panes (subgraphs included) of a saved doc that keep their takes: what a store clean-up must leave. */
|
|
15
|
+
export function keepers(panes = [], out = new Set()) {
|
|
16
|
+
for (const p of panes) {
|
|
17
|
+
if (p?.type === "audio" && p.audio?.record?.keepTakes === true) out.add(p.id);
|
|
18
|
+
if (p?.type && trait(p.type, "keepsRecording")) out.add(p.id); // a sample pane's kept recording stays while the pane does
|
|
19
|
+
if (p?.type === "subgraph") keepers(p.graph?.panes ?? [], out);
|
|
20
|
+
}
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Keep a take: its WAV, then its numbers (a take without its numbers isn't found again, so the numbers go last).
|
|
26
|
+
* Resolves true when both were stored in the browser's database, false when they are only held in memory for this
|
|
27
|
+
* visit (no IndexedDB here).
|
|
28
|
+
*/
|
|
29
|
+
export async function keepTake(canvasId, pane, take) {
|
|
30
|
+
const wav = await mediaStore.put(canvasId, takeKey(pane, take.index), take.media.blob);
|
|
31
|
+
const numbers = JSON.stringify(cleanTakeFields(take));
|
|
32
|
+
const meta = await mediaStore.put(canvasId, metaKey(pane, take.index), new Blob([numbers], { type: "application/json" }));
|
|
33
|
+
return wav && meta;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Forget the kept copy of one take. */
|
|
37
|
+
export async function forgetTake(canvasId, pane, index) {
|
|
38
|
+
await mediaStore.delete(canvasId, metaKey(pane, index));
|
|
39
|
+
await mediaStore.delete(canvasId, takeKey(pane, index));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Forget every kept take of a pane (the person turned keeping off); resolves with how many there were. */
|
|
43
|
+
export async function forgetTakes(canvasId, pane) {
|
|
44
|
+
const names = await mediaStore.names(canvasId, `take~${pane}~`);
|
|
45
|
+
for (const name of names) await mediaStore.delete(canvasId, name);
|
|
46
|
+
return names.filter((n) => !n.endsWith("~meta")).length;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The takes kept for a pane, oldest first: `[{ ...numbers, blob }]`. A take whose numbers or file are missing is skipped. */
|
|
50
|
+
export async function loadTakes(canvasId, pane) {
|
|
51
|
+
const found = [];
|
|
52
|
+
for (const name of await mediaStore.names(canvasId, `take~${pane}~`)) {
|
|
53
|
+
if (name.endsWith("~meta")) continue;
|
|
54
|
+
const blob = await mediaStore.get(canvasId, name);
|
|
55
|
+
const meta = await mediaStore.get(canvasId, `${name}~meta`);
|
|
56
|
+
if (!blob || !meta) continue;
|
|
57
|
+
let numbers;
|
|
58
|
+
try {
|
|
59
|
+
numbers = JSON.parse(await meta.text());
|
|
60
|
+
} catch {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
found.push({ ...cleanTakeFields(numbers, Number(name.split("~").at(-1)) || 1), blob });
|
|
64
|
+
}
|
|
65
|
+
return found.sort((a, b) => a.index - b.index);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ── a sample pane's kept recording (src/panes/sample.js) ─────────────────────
|
|
69
|
+
// The person's click on "keep" stores the pane's source recording (a WAV) under the canvas's own id, so the sound a pane made from a take is still there
|
|
70
|
+
// after a reload without the take wired in. One per pane; the trim numbers are the pane's settings, the bytes are never in the canvas document, an
|
|
71
|
+
// export or a share link. Browser only.
|
|
72
|
+
|
|
73
|
+
export const sampleKey = (pane) => `sample~${pane}`;
|
|
74
|
+
|
|
75
|
+
/** Keep a pane's recording (a Blob of a WAV); true when it was stored in the browser's database, false when it is only held for this visit. */
|
|
76
|
+
export const keepSample = (canvasId, pane, blob) => mediaStore.put(canvasId, sampleKey(pane), blob);
|
|
77
|
+
|
|
78
|
+
/** The kept recording of a pane, or undefined. */
|
|
79
|
+
export const loadSample = (canvasId, pane) => mediaStore.get(canvasId, sampleKey(pane));
|
|
80
|
+
|
|
81
|
+
/** Forget a pane's kept recording. */
|
|
82
|
+
export const forgetSample = (canvasId, pane) => mediaStore.delete(canvasId, sampleKey(pane));
|
|
83
|
+
|
|
84
|
+
/** Is a recording kept for the pane? */
|
|
85
|
+
export async function hasSample(canvasId, pane) {
|
|
86
|
+
return (await mediaStore.names(canvasId, sampleKey(pane))).includes(sampleKey(pane));
|
|
87
|
+
}
|