A local video studio for your coding agent. Describe a video in one sentence: your agent directs, your machine renders. Claude Code, Codex, Cursor, Devin and more. No API keys, no uploads.
Python agent-skillsai-agentscaptionsclaude-codeclaude-code-pluginclaude-skillscodexcursorexplainer-videoffmpegheadless-chromekokoro-ttslocal-firstmanimmcp-servermotion-graphicstext-to-speechvideovideo-editingvideo-generation
showtime 0.4.4
Favio Vázquez56fd27be0328
diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.jsonindex a991257..a80f705 100644--- a/.claude-plugin/marketplace.json+++ b/.claude-plugin/marketplace.json@@ -5,14 +5,14 @@},"metadata": {"description": "showtime: a local video studio for your coding agent",- "version": "0.4.3"+ "version": "0.4.4"},"plugins": [{"name": "showtime","source": "./","description": "A local video studio for your coding agent. Describe a video. Your agent directs. Your machine renders. Motion graphics, voice-over, music, footage editing, captions and platform-ready exports, with no cloud AI services or API keys.",- "version": "0.4.3",+ "version": "0.4.4","author": {"name": "Favio Vazquez"},diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.jsonindex 3a45c14..7c3adae 100644--- a/.claude-plugin/plugin.json+++ b/.claude-plugin/plugin.json@@ -1,6 +1,6 @@{"name": "showtime",- "version": "0.4.3",+ "version": "0.4.4","description": "A local video studio for your coding agent. Describe a video. Your agent directs. Your machine renders. Motion graphics rendered frame-exactly, voice-over, music and sound design, footage editing from transcripts, captions and platform-ready exports. Everything runs on your machine.","author": {"name": "Favio Vazquez"diff --git a/.github/ISSUE_TEMPLATE/xray-gallery.yml b/.github/ISSUE_TEMPLATE/xray-gallery.ymlnew file mode 100644index 0000000..1c24c06--- /dev/null+++ b/.github/ISSUE_TEMPLATE/xray-gallery.yml@@ -0,0 +1,105 @@+name: X-ray for the gallery+description: Post an X-ray you made to the gallery on the showtime site, with its receipt and its clip's credit.+title: "[x-ray] "+labels: ["type: example", "status: triage"]+body:+ - type: markdown+ attributes:+ value: |+ Thanks for sharing one. The gallery: https://faviovazquez.github.io/showtime/xray/+ Before it appears we run `showtime xray verify` on the film, check the clip's license and credit, and check that+ nothing is drawn over a person. A new mechanism (an object the library does not have yet) also goes in as a pull+ request: see "Add an X-ray mechanism" in CONTRIBUTING.md.+ - type: input+ id: film+ attributes:+ label: The film+ description: A public link to the finished MP4 (a release asset, a post, a file host). The file itself, not a page about it, if you can.+ placeholder: https://...+ validations:+ required: true+ - type: input+ id: object+ attributes:+ label: The object+ description: What it is, as exactly as you know it.+ placeholder: "An Underwood portable typewriter; a Polyphon disc music box"+ validations:+ required: true+ - type: dropdown+ id: mechanism-kind+ attributes:+ label: The mechanism+ options:+ - One from the library, as it ships+ - One from the library, changed+ - A new one (I will open, or have opened, a pull request)+ validations:+ required: true+ - type: input+ id: mechanism+ attributes:+ label: Its name, or the pull request+ placeholder: "typewriter; or #123"+ validations:+ required: true+ - type: dropdown+ id: layout+ attributes:+ label: Layout and look+ multiple: true+ options:+ - See-through (on the object)+ - Beside the clip+ - Living diagram+ - Score, tab or drum sheet+ - Dark (radiograph)+ - Light (blueprint)+ - type: input+ id: receipt+ attributes:+ label: The receipt line+ description: Paste the line `showtime xray verify` printed for this film, exactly as it printed.+ placeholder: "19 of 19 events on their frame · audio 0.0 ms from the original"+ validations:+ required: true+ - type: textarea+ id: commands+ attributes:+ label: The commands you ran (optional)+ description: They go under your film in the gallery, so others can make their own.+ render: text+ - type: dropdown+ id: source+ attributes:+ label: Where the clip comes from+ options:+ - I filmed it myself+ - It is under a Creative Commons license (or CC0, or public domain)+ validations:+ required: true+ - type: input+ id: credit+ attributes:+ label: The clip's license and credit line+ description: For your own footage, the name to credit. For any other clip, its license and credit line and a link to its source.+ placeholder: "Clip: Erik Pålsson / Sounds of Changes, CC BY 3.0, https://..."+ validations:+ required: true+ - type: input+ id: handle+ attributes:+ label: Credit you as+ description: Your GitHub handle, shown on the entry.+ placeholder: "@your-handle"+ - type: checkboxes+ id: rules+ attributes:+ label: Before you post+ options:+ - label: There are no people under the drawing. The inside is drawn over an object only.+ required: true+ - label: The receipt line is the one `showtime xray verify` printed for this exact film.+ required: true+ - label: I have the right to share this clip under the license above.+ required: truediff --git a/.gitignore b/.gitignoreindex d20db6a..955c4e6 100644--- a/.gitignore+++ b/.gitignore@@ -19,6 +19,7 @@ __pycache__/# FavioVazquez/showtime-examples.*.mp4!benchmarks/plugin-eval/**/*.mp4+!benchmarks/plugin-eval/**/*.webm# OS / editor noise.DS_Storediff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6f44e83..0338618 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,66 @@ All notable changes to showtime. Each entry says what changed and why, so this f`skills/showtime/lib/st/__init__.py` and `python3 scripts/check_release.py` keeps the plugin manifests, the registry files and`setup/package.json` in sync with it.+## 0.4.4 (2026-10-13)++- **New: X-ray, a film of a real thing with its inside drawn on the frames its own sound says.** Film something doing+ its thing (a typewriter typing, a music box playing, a guitar, a drum kit) and showtime draws the mechanism moving+ on the object as the camera moves (`--layout through`), beside the clip (`split`; on a phone the clip on top) or as+ an isometric living diagram next to it (`diagram`), as a dark radiograph or a light blueprint (`--theme`), 16:9 or+ 9:16. `showtime xray analyze <clip> --job <job>` finds every event (a keystroke, a pluck, a hit) in the clip's sound+ with its time and frame, writes `xray.json` and a `check.jpg` (the waveform with a tick per event), and with+ `--track --region ... --ref-time` follows the object (`--mask` leaves out hands). `--engine envelope|flux|auto`+ picks the detector; dense or sustained sound uses the spectral-flux one. `showtime new xray <dir> --mechanism+ <object>` starts from the mechanism library: six drawings of an inside, each made on a real clip and verified, a+ manual typewriter, a disc music box, a snap-action switch, a piano, a guitar and a drum kit (an object the library+ lacks starts as a copy of the closest entry, and anyone can add one). `showtime xray notes <clip> --instrument+ piano|guitar|musicbox|drums` reads the music with local models and writes `notes.json`, `notes.mid` and+ `notes.musicxml`: pitches, key, chords and a smooth tempo map for a scrolling score, a string and fret per note for+ a guitar tab, and every drum hit sorted by sound into kick, snare, hi-hat, toms and cymbals for a drum sheet+ (General MIDI channel 10 and percussion MusicXML). `showtime xray verify <job>` reads the finished film back: a+ small marker in a corner is white only on event frames and the film's sound is matched against the clip's, so the+ receipt says "19 of 19 events on their frame · audio 0.0 ms from the original"; missing or extra events, or audio+ more than 20 ms off, fail `showtime qa`, and `--band` also checks a scrolling score for stalls and jumps. `qa` also+ asks for the clip's credit (`xray.credit` in showtime.json, or `xray.own`), and the receipt and the end card carry+ it. A film made at another frame rate than its clip is converted by `floor(t * film fps) + offset`. The inside is+ a generic mechanism of that kind of object, which the film says on screen, and which key or note a hit was needs+ evidence (the paper, a display, the notes), otherwise the film says "a keystroke". Guide: [X-ray](docs/guides/xray.md)+ and [xray.md](skills/showtime/references/workflows/xray.md). Tests: `test_xray.py`, `test_xray_notes.py`,+ `test_xray_drums.py`, `test_xray_template.py`.+- **New: the X-ray live page, a film you play.** `showtime export html <xray project> --interactive` writes+ `<name>-live.html`, one offline file: the clip with its own sound and the inside drawn on every frame, slow motion,+ light or dark, see-through or beside, labels on hover, the receipt and the credit. It then went further. Pads under+ the picture play the next event of their kind from the real clip with its sound (a typewriter's keys laid out as a+ keyboard, an instrument's notes in pitch order, a kit's drums named as people say them); the keys of your keyboard+ press them, so you type on the typewriter; dragging the picture scrubs and a click plays the next event.+ Links are plain anchors: `#hit4`, `#t2.5`, `_light`, `_split` (`#hit4_light`), and the Link button copies one. The page+ shows the request that made the film in quotes with Copy, then the commands: pass `--prompt "<request>"` to the+ export (or `xray.made.prompt` in showtime.json); the commands use placeholders and never your paths, `showtime new+ xray` records only `xray.made` (mechanism, layout, theme), and the export report says what it embedded. The page embeds+ with `<showtime-xray src="you-live.html" ratio="16/9">` (a poster and a Play button; the one script is `embed.js`) or+ an iframe with `#embed`, and its policy allows framing. The letter shortcuts moved to Shift (Shift+S quarter speed,+ Shift+T theme, Shift+L layout). The live export takes the mechanism for its commands from `xray.made`, else from the+ project's own `mechanism.js`, and warns when it can only guess. Tests: `test_xray_live.py`, `test_xray_made.py`.+- **New: living diagrams you touch.** A figure's scene can answer the viewer, not only the knobs: `interactive.type`+ receives every character typed on the figure (from anywhere on the page, or from a Type field on a phone's soft+ keyboard; Enter and Backspace arrive too), `parts[].press` runs when a part is clicked or tapped (or Enter on its+ button) and `parts[].drag` turns a knob while the viewer drags the part (`axis`, and `span` in pixels for the whole+ range). With `type` set, plain letters belong to the scene and the figure's own shortcuts take Shift (Shift+R run+ again, Shift+L copy link, Shift+1-9 steps). Two new examples start from it: `showtime new diagram <dir> --example+ typewriter` (type on it: the key goes down, the typebar swings to the print point, the letter lands on the paper,+ the carriage steps, the bell rings near the margin, Enter returns the carriage) and `--example computer` (what+ happens when you press a key: the keyboard, a USB poll, an interrupt, RAM, the GPU and the screen's refresh, in slow+ motion with typical, labelled numbers; click a part to pulse it). The heat pump template's outdoor unit now drags to+ set the weather. Each still films from its tour, and a figure framed with `#embed` reports its height to the host+ like an X-ray page. `showtime check` walks a typed line and every pressable part. Tests: `test_figure_examples.py`,+ `test_figure_computer.py`.+- **The site's X-ray page.** A gallery of ten films (the typewriter four ways, the music box in a phone and a wide cut,+ the switch, the piano, the guitar and the drums), each with its receipt, the clip's credit with a link to the+ clip's file page (a share-alike clip says the film has the same license), and the commands that made it, with a+ copy button. "Play it live" on a card plays the live page in place (one at a time, on a stage beside the card's+ words), "Try it live" on the hero, an Embed snippet per card, and a shelf of three figures you touch (type on the+ typewriter, type on the computer, drag the heat pump's weather). Filters by kind and theme.+## 0.4.3 (2026-10-13)- **Recap and repro pick their music per video, and generated videos are quieter on the cuts.** Both used to getdiff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 519481e..cb213a1 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -59,6 +59,35 @@ or on the folder it already runs. `setup --link` points it at the linked checkou- Mention it in SKILL.md or a reference only as it really exists: `scripts/check_release.py` fails when thedocs name a command or sub-command the CLI does not have.+## Add an X-ray mechanism++The X-ray mechanism library (`skills/showtime/library/xray/`) holds one folder per kind of object, and+`showtime new xray <dir> --mechanism <object>` copies one into a project. It grows with every verified X-ray. To+show a film you made without adding a mechanism, use the "X-ray for the gallery" issue form instead; the+[X-ray page](https://faviovazquez.github.io/showtime/xray/) shows both lanes.++A pull request adds one folder, `skills/showtime/library/xray/<object>/`, started as a copy of the closest entry:++- `mechanism.js`: `XRAY.mechanism({...})` with the parts, the design box, four named anchors a clip places on its+ object, the outline, and `draw(k)` (the kit is described in the library's `README.md`).+- `diagram.js` (optional): the same object as an isometric living diagram, for `--layout diagram`.+- `README.md`: the parts; what one event moves; what is measured and what is drawn; the anchors; and a+ `Verified on:` line with the clip, its credit and license, and the receipt line `showtime xray verify` printed.++Checklist for the pull request:++- [ ] Made on a real clip and verified: the `Verified on:` line quotes the receipt as it printed ("N of N events on+ their frame · audio X ms from the original"), and qa had no FAIL.+- [ ] The clip is yours, or CC0, CC BY or CC BY-SA, with its credit line in the README. No license found, no entry.+- [ ] Objects only: the see-through layout is never drawn over a person.+- [ ] A strike peaks on its event's frame; anything that advances (a wheel, a carriage) steps on that frame.+- [ ] Which key, note or part is named only from evidence (the paper, a display, `notes.json`); otherwise the+ drawing says "a keystroke", "a pluck".+- [ ] Six to ten labelled parts of the real kind of mechanism, simplified, not a symbol; labels readable at+ 1080x1920 (28 px and up).+- [ ] `showtime new xray /tmp/x --mechanism <object>` then `showtime check /tmp/x` pass with the sample clip.+- [ ] No third-party drawings or code: the mechanism is your own work (see "Original work, clean licenses").+## Tests```bashdiff --git a/README.md b/README.mdindex 50d2d23..6949f63 100644--- a/README.md+++ b/README.md@@ -34,6 +34,7 @@ OpenCode and any agent that supports <a href="https://agentskills.io">Agent Skil<p align="center"><a href="#quick-start"><b>Quick start</b></a> ·+ <a href="#x-ray"><b>X-ray</b></a> ·<a href="#works-with"><b>Works with</b></a> ·<a href="#now-showing"><b>Gallery</b></a> ·<a href="#how-it-works"><b>How it works</b></a> ·@@ -106,6 +107,15 @@ languages or what leaves your machine: the [FAQ](docs/faq.md). Something went wr> quality (the default) has a critic review every finished video; lean skips the critic unless the video is going> to be published. Say **"no crew"** to keep the whole job in one session.+**What's new in 0.4.4** (the list is [below](#new-in-044), and [What's new](docs/whats-new.md) has a short+paragraph for each):++- **X-ray.** Film a real thing doing its thing and showtime draws its inside on the frames its own sound says; a score,+ a guitar tab or a drum sheet from the sound; a receipt that reads the finished film back.+- **A live page you play.** Pads that play the real clip with its sound, your keyboard typing on the typewriter, drag to+ scrub, `#hit4` links, the request that made it, and an embed.+- **Living diagrams you touch.** Type on a typewriter, follow a key press to the screen, drag the heat pump's weather.+**What's new in 0.4.3** (the list is [below](#new-in-043), and [What's new](docs/whats-new.md) has a shortparagraph for each):@@ -131,6 +141,38 @@ keeps to the plan and the delivery card. Anyone can rerun it.<img alt="" src="assets/readme/divider-light.svg" width="100%"></picture>+## X-ray++Film a real thing doing its thing, a typewriter typing or a music box playing, and showtime draws its inside moving+on the exact frames the clip's own sound says: on the object as the camera moves, or beside it, as a dark radiograph+or a light blueprint. Instruments get their music written out from the sound too (a score, a guitar tab, a drum+sheet), and every film ends with a receipt that proves it.++<p align="center"><a href="https://faviovazquez.github.io/showtime/xray/"><img alt="A typewriter filmed on a desk, its typebars, ribbon and escapement drawn in glowing lines on the machine itself as it types, with the receipt: 19 of 19 events on their frame, audio 0.0 ms from the original." src="assets/readme/xray/xray-hero.jpg" width="100%"></a></p>+<p align="center"><sub>An Underwood 315 typing, seen through. Clip: Erik Pålsson / Sounds of Changes, <a href="https://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>.</sub></p>++Say "X-ray this clip of my typewriter" to your agent, or run the three steps yourself:++```text+showtime xray analyze clip.mp4 --job xray-typewriter # every event in the sound, with its frame+showtime new xray xray-typewriter/project --mechanism typewriter --theme dark+showtime xray verify xray-typewriter # after the render: reads the film back+```++```text+19 of 19 events on their frame · audio 0.0 ms from the original+```++The mechanisms come from a library that grows with every verified X-ray, and anyone can add one. The+**[X-ray gallery](https://faviovazquez.github.io/showtime/xray/)** plays every verified film with its receipt and the+commands that made it, and shows how to post yours; the [X-ray guide](docs/guides/xray.md) walks through filming,+mechanisms, music and drums, and what the receipt checks.++<picture>+ <source media="(prefers-color-scheme: dark)" srcset="assets/readme/divider-dark.svg">+ <img alt="" src="assets/readme/divider-light.svg" width="100%">+</picture>+## Works withshowtime is a skill, a command line and an MCP server, so it runs in any coding agent that reads@@ -335,8 +377,8 @@ setup:is the same server from npm ([@faviovazquez/showtime-mcp](https://www.npmjs.com/package/@faviovazquez/showtime-mcp),also listed in the [MCP Registry](https://registry.modelcontextprotocol.io/v0.1/servers?search=io.github.FavioVazquez/showtime)as `io.github.FavioVazquez/showtime` and on [Smithery](https://smithery.ai/servers/favio-vazquezp/showtime)), and-[`showtime-0.4.3.mcpb`](https://github.com/FavioVazquez/showtime/releases/download/v0.4.3/showtime-0.4.3.mcpb) from-the v0.4.3 release opens in Claude Desktop with a double click. Setup still runs once on your machine: the+[`showtime-0.4.4.mcpb`](https://github.com/FavioVazquez/showtime/releases/download/v0.4.4/showtime-0.4.4.mcpb) from+the v0.4.4 release opens in Claude Desktop with a double click. Setup still runs once on your machine: themodels and tools never travel inside a package. Long tools answer with a task id after about 20 seconds when aclient stops calls early; the `status` tool reports progress and the result. Besides the tools that mirror the commands,`guide` reads a reference by the piece and `receipt` writes a job's receipt (tokens and cost are "not reported by this@@ -916,6 +958,37 @@ detectors on it, so a near-black scene or a barely visible change is reported be<img alt="" src="assets/readme/divider-light.svg" width="100%"></picture>+## New in 0.4.4++[What's new](docs/whats-new.md) has a short paragraph for each.++**X-ray**++- **`showtime xray`.** `analyze` finds every event (a keystroke, a pluck, a hit) in a clip's sound with its frame;+ `showtime new xray --mechanism <object>` draws the inside moving on those frames, on the object (`through`), beside+ it (`split`) or as an isometric diagram (`diagram`), as a dark radiograph or a light blueprint, 16:9 or 9:16; the+ library has a typewriter, a music box, a switch, a piano, a guitar and a drum kit. `notes` writes a score, a guitar+ tab or a drum sheet from the sound; `verify` reads the finished film back and `qa` fails a missing or extra event or+ audio more than 20 ms off. The receipt and the clip's credit go on the end card+ ([guide](docs/guides/xray.md), [xray](skills/showtime/references/workflows/xray.md)).+- **The live page.** `showtime export html <xray project> --interactive` writes one offline page: pads play the next+ event of their kind with the clip's sound, your keyboard types on the typewriter, drag scrubs, `#hit4` and `#t2.5`+ link to a moment, `--prompt` puts the request that made it on the page next to the commands, and+ `<showtime-xray src="you-live.html">` or an iframe embeds it.+- **The gallery.** The [X-ray page](https://faviovazquez.github.io/showtime/xray/) plays ten films with their+ receipts and plays each one live in place.++**Living diagrams**++- **You touch them.** A scene can take typing (`interactive.type`), a click on a part (`parts[].press`) and a drag that+ turns a knob (`parts[].drag`). `--example typewriter` and `--example computer` are new, and the heat pump's outdoor+ unit drags to set the weather ([guide](docs/guides/living-diagram.md)).++<picture>+ <source media="(prefers-color-scheme: dark)" srcset="assets/readme/divider-dark.svg">+ <img alt="" src="assets/readme/divider-light.svg" width="100%">+</picture>+## New in 0.4.3[What's new](docs/whats-new.md) has a short paragraph for each.@@ -1539,15 +1612,15 @@ language, a CPU limit) and the render progress monitor.<p align="center"><picture><source media="(prefers-color-scheme: dark)" srcset="assets/readme/diagrams/docs-map-dark.svg">- <img alt="The documentation map: seventeen workflows (launch video, explainer, tutorial, social short, data story, footage edit, trailer, slideshow, changelog video, music video, voice-over only, localize, repo explainer, paper explainer, repro, diagram, recap), then five acts and a finale of guides, from onboarding to qa and review." src="assets/readme/diagrams/docs-map-light.svg" width="100%">+ <img alt="The documentation map: eighteen workflows (launch video, explainer, tutorial, social short, data story, footage edit, trailer, slideshow, changelog video, music video, voice-over only, localize, repo explainer, paper explainer, repro, diagram, recap, x-ray), then five acts and a finale of guides, from onboarding to qa and review." src="assets/readme/diagrams/docs-map-light.svg" width="100%"></picture></p>-For people: [fourteen task guides](docs/guides/README.md) (what to say to your agent, what it runs, what you get, how+For people: [fifteen task guides](docs/guides/README.md) (what to say to your agent, what it runs, what you get, howlong it takes), [What's new](docs/whats-new.md) (each release in one sentence a feature) and the [FAQ](docs/faq.md)(GPUs, cost, agents, privacy, licences, disk, time, platforms, languages).The [documentation map](docs/README.md) is the way in: every guide, grouped by what you are making, from-workflows (launch, explainer, tutorial, social, data, footage, trailer, diagram, recap, repro) to story and craft, sound and+workflows (launch, explainer, tutorial, social, data, footage, trailer, diagram, recap, repro, X-ray) to story and craft, sound andvoice, rendering, and QA. The glossary is in [CONTEXT.md](CONTEXT.md), and what showtime deliberatelydoes not do, and why, is in [.out-of-scope/](.out-of-scope/README.md).diff --git a/assets/readme/xray/xray-hero.jpg b/assets/readme/xray/xray-hero.jpgnew file mode 100644index 0000000..c905fdeBinary files /dev/null and b/assets/readme/xray/xray-hero.jpg differdiff --git a/benchmarks/plugin-eval/README.md b/benchmarks/plugin-eval/README.mdindex df5ea06..686fe24 100644--- a/benchmarks/plugin-eval/README.md+++ b/benchmarks/plugin-eval/README.md@@ -22,6 +22,8 @@ quality. Each case comes from a failure mode seen in real runs:| `impossible-spec` | trigger, honesty | a 10-minute 4K 60 fps render "in one minute" gets a straight answer || `delivery-card` | trigger, contract, honesty | the hand-off is the delivery card, in order, with a true `Look:` line || `full-crew-trigger` | trigger, contract | "full crew" on a short music film stays quick (no pitch, no storyboard gate) and dispatches the sound designer and a critic |+| `oneliner-interactive-diagram` | trigger, contract | end to end, needs `--scaffold --allow-tools Bash Write Edit`: `/showtime explain how a heat pump works, interactive.` must make a diagram project (`new diagram`), export the live figure (`export html --interactive`, a `*-figure.html`) and name it; a video-only answer fails |+| `oneliner-xray` | trigger, contract | end to end, same flags: `/showtime x-ray this clip of my typewriter, drawn on the machine.` with the sample clip as `typewriter.webm`: `xray analyze`, `new xray`, `render`, `xray verify` and a reply that quotes the receipt (N of N events on their frame) || `non-trigger-photo-caption` | non-trigger | the skill does not fire on an unrelated request (scored in both arms) || `non-trigger-sql` | non-trigger | the skill does not fire on a SQL question, and the answer is still a correct query |@@ -74,6 +76,15 @@ out=~/.vbench/plugin-eval/smoke-$(date +%Y%m%d-%H%M); claude plugin eval . --evaA free load check of every case (the cost ceiling stops it before the first run): add `--max-cost-usd 0`.+## End-to-end cases (`oneliner-*`)++These two let the agent run `showtime` for real. Each scaffold clones your `~/.showtime` (and the uv Python its venv+points at) into the run's sealed home; run them from a copy of the checkout without `.git` (the sandbox cannot read+a worktree's `.git`, and `job init` used to crash on that). The eval sandbox cannot bind a local port, so `check`,+`look`, `render` and `xray verify` stop with `listen EPERM 127.0.0.1` there: `oneliner-xray` can only score its+`verify-passes` grader in a harness that allows `sandbox.network.allowLocalBinding`; `export html --interactive`+needs no port. The slash command loads the skill without a `Skill` call, so `skill-fired` reads 0 here.+## Scaffolded cases`footage-missing-take` has a `case.yaml` (`context.scaffold_script: scaffold.sh`) next to its `prompt.md`; the evaldiff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/case.yaml b/benchmarks/plugin-eval/oneliner-interactive-diagram/case.yamlnew file mode 100644index 0000000..1e8a7b2--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/case.yaml@@ -0,0 +1,4 @@+schema_version: "1.1"+name: oneliner-interactive-diagram+context:+ scaffold_script: scaffold.shdiff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/at-most-two-questions.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/at-most-two-questions.mdnew file mode 100644index 0000000..8da458a--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/at-most-two-questions.md@@ -0,0 +1,7 @@+---+type: tool_used+tool: AskUserQuestion+min: 0+max: 2+arm: both+---diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/exports-the-live-figure.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/exports-the-live-figure.mdnew file mode 100644index 0000000..0f6236f--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/exports-the-live-figure.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Bash+input_match: 'export\s+html[^\n]*--interactive'+---diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/makes-a-diagram-project.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/makes-a-diagram-project.mdnew file mode 100644index 0000000..8800ffc--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/makes-a-diagram-project.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Bash+input_match: 'showtime(?:\.cmd)?["\x27]?\s+new\s+diagram|new\s+diagram'+---diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/names-the-figure-file.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/names-the-figure-file.mdnew file mode 100644index 0000000..4d64054--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/names-the-figure-file.md@@ -0,0 +1,6 @@+---+type: regex+target: last_message+flags: i+pattern: '[\w./-]*-figure\.html'+---diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/no-question-pile.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/no-question-pile.mdnew file mode 100644index 0000000..8a53565--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/no-question-pile.md@@ -0,0 +1,8 @@+---+type: tool_used+tool: AskUserQuestion+input_match: '(?:"question"\s*:[\s\S]*?){3}'+min: 0+max: 0+arm: both+---diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/no-video-only.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/no-video-only.mdnew file mode 100644index 0000000..a416a51--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/no-video-only.md@@ -0,0 +1,12 @@+---+type: llm+focus: trace+---++The user typed "/showtime explain how a heat pump works, interactive." The word "interactive" means they want a+living figure they can touch in a browser, not a finished video file.+PASS if the run built a diagram project (`showtime new diagram`), exported the live page with+`showtime export html <project> --interactive` (a `*-figure.html` file) and the final reply says where that file is+and what the viewer can touch (a knob, steps, or the scene's parts).+FAIL if the run only renders an MP4 (or only plans one) and never produces the live figure, or if it stops after+asking the user questions without making anything.diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/skill-fired.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/skill-fired.mdnew file mode 100644index 0000000..4de22a3--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/graders/skill-fired.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Skill+input_match: '"skill"\s*:\s*"(?:[\w-]+:)?showtime"'+---diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/prompt.md b/benchmarks/plugin-eval/oneliner-interactive-diagram/prompt.mdnew file mode 100644index 0000000..f3a196e--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/prompt.md@@ -0,0 +1,9 @@+---+max_turns: 40+timeout_seconds: 1500+runs: 3+tags: [trigger, contract]+allowed_tools: [Read, Glob, Grep, Skill, Bash, Write, Edit, AskUserQuestion]+---++/showtime explain how a heat pump works, interactive.diff --git a/benchmarks/plugin-eval/oneliner-interactive-diagram/scaffold.sh b/benchmarks/plugin-eval/oneliner-interactive-diagram/scaffold.shnew file mode 100755index 0000000..fb5c36d--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-interactive-diagram/scaffold.sh@@ -0,0 +1,21 @@+#!/usr/bin/env bash+# Run by `claude plugin eval --scaffold` with the run's empty workspace ($HOME/cwd) as the current directory.+set -eu+# The eval gives each run a sealed HOME (the workspace is $HOME/cwd), so the agent would find no showtime setup+# and stop to ask for a 520 MB install. Clone the real one in (copy-on-write on APFS, a plain copy elsewhere).+real="${SHOWTIME_REAL_HOME:-$(eval echo "~$(id -un)")}"+if [ -d "$real/.showtime" ] && [ ! -e ../.showtime ]; then+ cp -cR "$real/.showtime" ../.showtime 2>/dev/null || cp -R "$real/.showtime" ../.showtime+fi+# The venv's interpreter is a symlink into the uv-managed Python under the real home, which the sandbox cannot+# read: clone that Python too and point the venv at the clone.+cfg=../.showtime/venv/pyvenv.cfg+if [ -f "$cfg" ]; then+ pyhome="$(sed -n 's/^home = //p' "$cfg")"+ case "$pyhome" in "$real"/*)+ pyroot="$(dirname "$pyhome")"+ cp -cR "$pyroot" ../.showtime/_python 2>/dev/null || cp -R "$pyroot" ../.showtime/_python+ sed -i.bak "s#^home = .*#home = $PWD/../.showtime/_python/bin#" "$cfg" && rm -f "$cfg.bak"+ for l in ../.showtime/venv/bin/python; do ln -sf "$(cd ../.showtime/_python/bin && pwd)/$(basename "$(readlink "$l")")" "$l"; done+ ;; esac+fidiff --git a/benchmarks/plugin-eval/oneliner-xray/case.yaml b/benchmarks/plugin-eval/oneliner-xray/case.yamlnew file mode 100644index 0000000..59a6f09--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/case.yaml@@ -0,0 +1,4 @@+schema_version: "1.1"+name: oneliner-xray+context:+ scaffold_script: scaffold.shdiff --git a/benchmarks/plugin-eval/oneliner-xray/graders/at-most-two-questions.md b/benchmarks/plugin-eval/oneliner-xray/graders/at-most-two-questions.mdnew file mode 100644index 0000000..8da458a--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/at-most-two-questions.md@@ -0,0 +1,7 @@+---+type: tool_used+tool: AskUserQuestion+min: 0+max: 2+arm: both+---diff --git a/benchmarks/plugin-eval/oneliner-xray/graders/makes-an-xray-project.md b/benchmarks/plugin-eval/oneliner-xray/graders/makes-an-xray-project.mdnew file mode 100644index 0000000..f2a6298--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/makes-an-xray-project.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Bash+input_match: 'new\s+xray'+---diff --git a/benchmarks/plugin-eval/oneliner-xray/graders/no-question-pile.md b/benchmarks/plugin-eval/oneliner-xray/graders/no-question-pile.mdnew file mode 100644index 0000000..8a53565--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/no-question-pile.md@@ -0,0 +1,8 @@+---+type: tool_used+tool: AskUserQuestion+input_match: '(?:"question"\s*:[\s\S]*?){3}'+min: 0+max: 0+arm: both+---diff --git a/benchmarks/plugin-eval/oneliner-xray/graders/renders.md b/benchmarks/plugin-eval/oneliner-xray/graders/renders.mdnew file mode 100644index 0000000..9021b4d--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/renders.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Bash+input_match: 'showtime(?:\.cmd)?["\x27]?\s+render'+---diff --git a/benchmarks/plugin-eval/oneliner-xray/graders/runs-analyze.md b/benchmarks/plugin-eval/oneliner-xray/graders/runs-analyze.mdnew file mode 100644index 0000000..6e37684--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/runs-analyze.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Bash+input_match: 'xray\s+analyze[^\n]*typewriter\.webm'+---diff --git a/benchmarks/plugin-eval/oneliner-xray/graders/runs-verify.md b/benchmarks/plugin-eval/oneliner-xray/graders/runs-verify.mdnew file mode 100644index 0000000..725be88--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/runs-verify.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Bash+input_match: 'xray\s+verify'+---diff --git a/benchmarks/plugin-eval/oneliner-xray/graders/skill-fired.md b/benchmarks/plugin-eval/oneliner-xray/graders/skill-fired.mdnew file mode 100644index 0000000..4de22a3--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/skill-fired.md@@ -0,0 +1,5 @@+---+type: tool_used+tool: Skill+input_match: '"skill"\s*:\s*"(?:[\w-]+:)?showtime"'+---diff --git a/benchmarks/plugin-eval/oneliner-xray/graders/verify-passes.md b/benchmarks/plugin-eval/oneliner-xray/graders/verify-passes.mdnew file mode 100644index 0000000..1bb740f--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/graders/verify-passes.md@@ -0,0 +1,11 @@+---+type: llm+focus: trace+---++The user typed "/showtime x-ray this clip of my typewriter, drawn on the machine." and gave typewriter.webm.+PASS if the run executed `showtime xray analyze` on the clip, made an xray project, rendered it, and ran+`showtime xray verify <job>` whose output (the receipt) shows every event on its frame (N of N), and the final reply+reports that result without overstating it.+FAIL if verify was never run, verify reported fewer than N of N events on their frame and the reply calls it a+success, or the numbers in the reply are not the ones verify printed.diff --git a/benchmarks/plugin-eval/oneliner-xray/media/clip.webm b/benchmarks/plugin-eval/oneliner-xray/media/clip.webmnew file mode 100644index 0000000..2bfe549Binary files /dev/null and b/benchmarks/plugin-eval/oneliner-xray/media/clip.webm differdiff --git a/benchmarks/plugin-eval/oneliner-xray/prompt.md b/benchmarks/plugin-eval/oneliner-xray/prompt.mdnew file mode 100644index 0000000..eb42f5e--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/prompt.md@@ -0,0 +1,9 @@+---+max_turns: 60+timeout_seconds: 1800+runs: 3+tags: [trigger, contract]+allowed_tools: [Read, Glob, Grep, Skill, Bash, Write, Edit, AskUserQuestion]+---++/showtime x-ray this clip of my typewriter, drawn on the machine.diff --git a/benchmarks/plugin-eval/oneliner-xray/scaffold.sh b/benchmarks/plugin-eval/oneliner-xray/scaffold.shnew file mode 100755index 0000000..eeda142--- /dev/null+++ b/benchmarks/plugin-eval/oneliner-xray/scaffold.sh@@ -0,0 +1,25 @@+#!/usr/bin/env bash+# Run by `claude plugin eval --scaffold` with the run's empty workspace as the current directory.+# Puts the typewriter sample clip (the one skills/showtime/templates/xray/media/clip.webm ships) there as+# typewriter.webm, the user's own clip for the case.+set -eu+here="$(cd "$(dirname "$0")" && pwd)"+cp "$here/media/clip.webm" typewriter.webm+# The eval gives each run a sealed HOME (the workspace is $HOME/cwd), so the agent would find no showtime setup+# and stop to ask for a 520 MB install. Clone the real one in (copy-on-write on APFS, a plain copy elsewhere).+real="${SHOWTIME_REAL_HOME:-$(eval echo "~$(id -un)")}"+if [ -d "$real/.showtime" ] && [ ! -e ../.showtime ]; then+ cp -cR "$real/.showtime" ../.showtime 2>/dev/null || cp -R "$real/.showtime" ../.showtime+fi+# The venv's interpreter is a symlink into the uv-managed Python under the real home, which the sandbox cannot+# read: clone that Python too and point the venv at the clone.+cfg=../.showtime/venv/pyvenv.cfg+if [ -f "$cfg" ]; then+ pyhome="$(sed -n 's/^home = //p' "$cfg")"+ case "$pyhome" in "$real"/*)+ pyroot="$(dirname "$pyhome")"+ cp -cR "$pyroot" ../.showtime/_python 2>/dev/null || cp -R "$pyroot" ../.showtime/_python+ sed -i.bak "s#^home = .*#home = $PWD/../.showtime/_python/bin#" "$cfg" && rm -f "$cfg.bak"+ for l in ../.showtime/venv/bin/python; do ln -sf "$(cd ../.showtime/_python/bin && pwd)/$(basename "$(readlink "$l")")" "$l"; done+ ;; esac+fidiff --git a/docs/README.md b/docs/README.mdindex e59d8b6..a0f48c5 100644--- a/docs/README.md+++ b/docs/README.md@@ -54,7 +54,7 @@ Written for people rather than for your agent:<p align="center"><picture><source media="(prefers-color-scheme: dark)" srcset="../assets/readme/diagrams/docs-map-dark.svg">- <img alt="The documentation map: seventeen workflows, then five acts and a finale of guides, from onboarding to qa and review." src="../assets/readme/diagrams/docs-map-light.svg" width="100%">+ <img alt="The documentation map: eighteen workflows, then five acts and a finale of guides, from onboarding to qa and review." src="../assets/readme/diagrams/docs-map-light.svg" width="100%"></picture></p>## Choose your path@@ -92,6 +92,9 @@ What are you making? Each card opens the workflow guide your agent follows; the<td width="33%" valign="top"><a href="https://github.com/FavioVazquez/showtime-examples/tree/main/examples/_recap"><img src="../assets/readme/gallery/_recap.webp" width="100%" alt="A preview of the recap demo"></a><br><sub>What your coding agent did in a session</sub><br><b><a href="../skills/showtime/references/workflows/recap.md">recap</a></b><br><sub>see it done: <a href="https://github.com/FavioVazquez/showtime-examples/tree/main/examples/_recap">the demo</a></sub></td><td width="33%" valign="top"><a href="https://github.com/FavioVazquez/showtime-examples/tree/main/examples/_repro"><img src="../assets/readme/gallery/_repro.webp" width="100%" alt="A preview of the repro demo"></a><br><sub>A failing browser test, for a bug report</sub><br><b><a href="../skills/showtime/references/workflows/repro.md">repro</a></b><br><sub>see it done: <a href="https://github.com/FavioVazquez/showtime-examples/tree/main/examples/_repro">the demo</a></sub></td></tr>+<tr>+<td width="33%" valign="top"><sub>The inside of a real thing you filmed, in sync with its sound</sub><br><b><a href="../skills/showtime/references/workflows/xray.md">xray</a></b><br><sub>see it done: <a href="https://faviovazquez.github.io/showtime/xray/">the X-ray gallery</a> · for people: <a href="guides/xray.md">the guide</a></sub></td>+</tr></table>## Act I · Before the curtaindiff --git a/docs/agents.md b/docs/agents.mdindex e3e0693..8c9b0b7 100644--- a/docs/agents.md+++ b/docs/agents.md@@ -317,7 +317,7 @@ Any client that starts local (stdio) servers can use showtime's MCP server on it([@faviovazquez/showtime-mcp](https://www.npmjs.com/package/@faviovazquez/showtime-mcp), also in the MCP Registry as`io.github.FavioVazquez/showtime`),`{"command": "npx", "args": ["-y", "@faviovazquez/showtime-mcp"]}` (see-[`packages/npm/README.md`](../packages/npm/README.md)), and [`showtime-0.4.3.mcpb`](https://github.com/FavioVazquez/showtime/releases/download/v0.4.3/showtime-0.4.3.mcpb) from the v0.4.3 release, which+[`packages/npm/README.md`](../packages/npm/README.md)), and [`showtime-0.4.4.mcpb`](https://github.com/FavioVazquez/showtime/releases/download/v0.4.4/showtime-0.4.4.mcpb) from the v0.4.4 release, whichClaude Desktop opens with a double click. Setup still runs once on your machine, because the models and toolsnever travel inside a package. Long tools answer with a task id after about20 s when a client stops calls early; the `status` tool reports progress and the result.diff --git a/docs/examples/showtime-release-video.yml b/docs/examples/showtime-release-video.ymlindex ef5c782..a0cb4a7 100644--- a/docs/examples/showtime-release-video.yml+++ b/docs/examples/showtime-release-video.yml@@ -34,7 +34,7 @@ jobs:- uses: actions/checkout@v7- id: video- uses: FavioVazquez/showtime/.github/actions/[email protected]+ uses: FavioVazquez/showtime/.github/actions/[email protected]with:mode: release# a published release: its tag, name and description come from the event; by hand: these@@ -69,7 +69,7 @@ jobs:# runs-on: ubuntu-24.04# timeout-minutes: 30# steps:-# - uses: FavioVazquez/showtime/.github/actions/[email protected]+# - uses: FavioVazquez/showtime/.github/actions/[email protected]# with:# upload: artifact#@@ -78,7 +78,7 @@ jobs:# showtime.json), rendered on every push to main that changes it:## - uses: actions/checkout@v7-# - uses: FavioVazquez/showtime/.github/actions/[email protected]+# - uses: FavioVazquez/showtime/.github/actions/[email protected]# with:# mode: project# project: videos/launch@@ -91,7 +91,7 @@ jobs:## - uses: actions/checkout@v7# - run: npm install -g @your-agent/cli-# - uses: FavioVazquez/showtime/.github/actions/[email protected]+# - uses: FavioVazquez/showtime/.github/actions/[email protected]# env:# YOUR_AGENT_API_KEY: ${{ secrets.YOUR_AGENT_API_KEY }}# with:diff --git a/docs/github-action.md b/docs/github-action.mdindex 7de158c..cee9285 100644--- a/docs/github-action.md+++ b/docs/github-action.md@@ -5,7 +5,7 @@ and no agent. It installs showtime on the runner, turns the notes into a project`showtime qa` on the finished file, exports a single-file HTML video, and uploads the result.```yaml-- uses: FavioVazquez/showtime/.github/actions/[email protected]+- uses: FavioVazquez/showtime/.github/actions/[email protected]```The complete workflow to copy is [`examples/showtime-release-video.yml`](examples/showtime-release-video.yml).diff --git a/docs/guides/README.md b/docs/guides/README.mdindex b5b0505..edf1a86 100644--- a/docs/guides/README.md+++ b/docs/guides/README.md@@ -15,6 +15,7 @@ look at. The references in `skills/showtime/references/` are written for your ag| [Make a video of a pull request](pr-video.md) | a short video for a PR's description in one command, and the GitHub Action || [Make a short film of what your agent did](session-recap.md) | a 30-60 s recap of a Claude Code session for a PR, a standup or a post, from allow-listed facts only || [Turn a failing test into a bug-report video](bug-report-video.md) | a failing Playwright test as a narrated video and a summary to paste into the issue |+| [Make an X-ray of a real thing](xray.md) | a clip of a machine or an instrument with its inside drawn moving on the exact frames its sound says, its music as a score, tab or drum sheet, and a receipt that proves it || [Make a living diagram, and a live figure](living-diagram.md) | how something works, as a figure that builds itself, and a live figure people hover, step through and tune || [Make an explainer that stops and asks](explainer-that-asks.md) | questions the web version stops on, links to a moment or a stretch, link previews || [Go all out: a showreel](go-all-out.md) | a hype reel with flash words, a shutter blur on snaps, and WebGL looks |diff --git a/docs/guides/living-diagram.md b/docs/guides/living-diagram.mdindex bd7ac32..8516ef3 100644--- a/docs/guides/living-diagram.md+++ b/docs/guides/living-diagram.md@@ -24,6 +24,9 @@ through and tune.5. For the live figure, the scene declares what is live (steps, knobs, readouts, the parts to hover) and`showtime export html <project> --interactive` writes `<slug>-figure.html`. `showtime check` walks every step,each knob value and each hovered part of it first.+6. To let people touch it, the scene can also take typing (`interactive.type`), a click on a part (`parts[].press`) or a+ drag on a part that turns a knob (`parts[].drag`). `--example typewriter` (type on it) and `--example computer`+ (what happens when you press a key) start from finished ones; the heat pump drags its outdoor unit to set the weather.## What you getdiff --git a/docs/guides/xray.md b/docs/guides/xray.mdnew file mode 100644index 0000000..2648e6e--- /dev/null+++ b/docs/guides/xray.md@@ -0,0 +1,90 @@+# Make an X-ray of a real thing++Film something doing its thing: a typewriter typing, a music box playing, a guitar, a drum kit. An X-ray is a film of+that clip where the inside of the object is drawn moving on the exact frames the clip's own sound says it did: on the+object itself as the camera moves (see-through), or beside it. Instruments can also get their music written out from+the sound, as a score, a guitar tab or a drum sheet. Every X-ray ends with a receipt that `showtime xray verify`+prints after reading the finished film back: "19 of 19 events on their frame · audio 0.0 ms from the original".++## Say this++> "X-ray this clip of my typewriter: draw its inside on the machine as it types."++> "Here's my music box playing. Show the disc and the comb, for a phone." · "Turn this drum clip into a drum sheet."++## What happens++1. **Film it.** Keep the sound: the events are found in it. A phone and a little shake are fine; close and well lit+ beats wide and dark. A clip you did not film needs a license that allows it (CC0, CC BY, CC BY-SA) and its credit.+2. **Measure it.** `showtime xray analyze <clip> --job <job>` writes every event (a keystroke, a pluck, a hit) with its+ time and frame into `xray.json`, and `check.jpg`: the waveform with a tick per event. Your agent plays the clip at+ a few ticks and counts events against what it sees. For see-through, `--track --region "x,y ..." --ref-time <t>`+ follows the object; `--mask` leaves out hands or parts that move by themselves.+3. **Pick a mechanism.** `showtime new xray <job>/project --mechanism <object>` copies one from the+ [mechanism library](../../skills/showtime/library/xray/README.md) (a manual typewriter, a disc music box and more),+ which grows with every verified X-ray; an object it lacks starts as a copy of the closest entry. Your agent first+ names the object from three frames and writes down what moves on each event.+4. **Choose a layout and a look.** `--layout through` (on the object), `split` (beside the clip; on a phone, the clip on+ top) or `diagram` (an isometric living diagram, in the manner of the [living diagram demo](https://github.com/FavioVazquez/showtime-examples/tree/main/examples/_diagram));+ `--theme dark` (a radiograph) or `light` (a blueprint on paper); 16:9 or 9:16.+5. **Music and drums.** `showtime xray notes <clip> --job <job> --instrument piano|guitar|musicbox|drums` reads the+ music with local models: pitches, key, chords and a smooth tempo map for a score; a string and fret per note for a+ guitar tab; every hit sorted into kick, snare, hi-hat, toms and cymbals for a drum sheet. Look at+ `check-notes.jpg` first. A score scrolls on the tempo map, never jumping to the latest note.+6. **Render and verify.** After `showtime render`, `showtime xray verify <job>` reads the final file: a small sync+ marker in a corner is white only on event frames, and the film's sound is matched against the clip's. Missing or+ extra events, or audio more than 20 ms off, fail `showtime qa`; `--band x,y,w,h` also checks a scrolling score for+ stalls and jumps. The line goes in the receipt and on the end card.+7. **Share it.** Post the receipt line and the clip's credit with the film. Add it to the+ [X-ray gallery](https://faviovazquez.github.io/showtime/xray/) with the gallery issue form, or add your mechanism+ with a pull request ([Add an X-ray mechanism](../../CONTRIBUTING.md#add-an-x-ray-mechanism)); anyone can then+ start from it with `showtime new xray <dir> --mechanism <object>`.++## What you get++- `final.mp4` at 1920x1080 or 1080x1920 with the clip's own sound, and an end card with the receipt and the credit.+- `xray.json` (every event with its frame) and `check.jpg`; for music, `notes.json`, `notes.mid` and `notes.musicxml`.+- The receipt line in the job's `receipt.md`, plus the usual poster, captions and exports.++## The live page++`showtime export html <project> --interactive --prompt "X-ray this clip of my typewriter."` writes `<name>-live.html`,+one offline file: the clip with its own sound and the inside drawn on every frame. The pads under the picture play+the next hit of their kind (on a typewriter, type the letter); drag the picture to scrub, click it for the next hit.+Space plays, the arrows step event to event, Shift+S is quarter speed, Shift+T light or dark, Shift+L the layout.+`#hit4` opens the 4th event, `#t2.5` a time; `_light`, `_split` and the rest join with `_`. The page shows your prompt+and the commands (placeholders, never your paths). To put it on a page:+`<showtime-xray src="you-live.html" ratio="16/9">` with the X-ray page's `embed.js` (a poster that loads on click), or `<iframe src="you-live.html#embed">`.++## How long it takes++Most of it is the render of a film about three seconds longer than the clip.++## Phrases that change it++- "Drawn on it", "see-through": `--layout through`; "beside it": `--layout split`; "as a diagram": `--layout diagram`.+- "Blueprint", "light": `--theme light`; "x-ray", "dark": `--theme dark`; "for Reels", "vertical": 9:16.+- "Write the music out", "as a score", "as a tab", "as a drum sheet": adds `showtime xray notes`.++## Limits++- **The inside is a generic mechanism.** Event times and counts are measured from the sound, and anything the clip+ shows (the typed text) is read from the picture; the inside is a simplified mechanism of that kind of object, not+ this exact unit, and the film says so on screen.+- **Which key, note or drum needs evidence**: the paper, a display, the notes read from the sound. Without it the film+ says "a keystroke", never a guessed key.+- **Tracking needs a steady object.** When the overlay slides (fast moves, a camera still settling, the object leaving+ the frame), trim the clip or use the split layout.+- **The drum sheet comes from a sound-type classifier.** Each hit is sorted by what it sounds like; it does not see+ which drum was struck, and two drums that sound alike can swap.+- **Dense music** that never goes quiet uses `analyze --engine flux` (picked by `auto`); very fast passages can merge.+- **Nothing leaves your machine**, but the job folder remembers: `xray/source.path` holds your clip's absolute path+ and `xray.json` its file name and sha256. Check them before you share a job folder.+- **Objects only.** An inside is never drawn over a person: a clip where someone fills the frame gets the split layout.++## The example++The [X-ray gallery](https://faviovazquez.github.io/showtime/xray/) plays every verified film with its receipt and the+commands that made it. The `xray` template starts as a 9.4 s sample: an Underwood 315 typing "Sounds of changes."+(Erik Pålsson / Sounds of Changes, CC BY 3.0). Your agent's guide:+[xray.md](../../skills/showtime/references/workflows/xray.md).diff --git a/docs/whats-new.md b/docs/whats-new.mdindex 1a5cd30..df1a46e 100644--- a/docs/whats-new.md+++ b/docs/whats-new.md@@ -3,6 +3,33 @@What each release changes for you, newest first, one sentence a feature. The[changelog](../CHANGELOG.md) has every change and the reason for it.+## 0.4.4++### X-ray: film a real thing and see its inside++++Film something doing its thing (a typewriter, a music box, a guitar, a drum kit) and showtime finds every event in the+clip's own sound and draws the inside moving on exactly those frames: on the object as the camera moves, beside it or as+an isometric diagram, in a dark radiograph or a light blueprint. `showtime xray analyze` measures, `showtime new xray+--mechanism <object>` starts from a library of six mechanisms (typewriter, music box, switch, piano, guitar, drums),+`showtime xray notes` writes a score, a guitar tab or a drum sheet from the sound, and `showtime xray verify` reads+the finished film back for a receipt: "19 of 19 events on their frame · audio 0.0 ms from the original"+([guide](guides/xray.md), [xray](../skills/showtime/references/workflows/xray.md), [the gallery](https://faviovazquez.github.io/showtime/xray/)).++### A live page you play++`showtime export html <xray project> --interactive` writes one offline page of the same film: pads under the picture+play the next hit from the real clip with its sound, your keyboard types on the typewriter, dragging scrubs, and+`#hit4` links to a moment. The page shows the request that made it (`--prompt`) and the commands, and embeds with+`<showtime-xray>` or an iframe. On the site, every film in the gallery plays live in place.++### Living diagrams you touch++A figure's scene can now take typing, a click on a part and a drag on a part. `showtime new diagram --example+typewriter` is a typewriter you type on, `--example computer` follows a key press from the keyboard to the screen, and+the heat pump's outdoor unit drags to change the weather ([guide](guides/living-diagram.md)).+## 0.4.3### Living diagrams: a system that explains itselfdiff --git a/gemini-extension.json b/gemini-extension.jsonindex 7b8ac08..cf4246a 100644--- a/gemini-extension.json+++ b/gemini-extension.json@@ -1,6 +1,6 @@{"name": "showtime",- "version": "0.4.3",+ "version": "0.4.4","description": "A local video studio for your coding agent. Describe a video. Your agent directs. Your machine renders. Motion graphics, voice-over, music, footage editing, captions and platform-ready exports, with no cloud AI services or API keys.","mcpServers": {"showtime": {diff --git a/packages/npm/package.json b/packages/npm/package.jsonindex 3d20edb..bb4b0a3 100644--- a/packages/npm/package.json+++ b/packages/npm/package.json@@ -1,6 +1,6 @@{"name": "@faviovazquez/showtime-mcp",- "version": "0.4.3",+ "version": "0.4.4","mcpName": "io.github.FavioVazquez/showtime","description": "A local video studio for your coding agent: showtime's MCP server and command line. Describe a video. Your agent directs. Your machine renders.","license": "MIT",diff --git a/plugin.json b/plugin.jsonindex aa5d000..5f1b0e7 100644--- a/plugin.json+++ b/plugin.json@@ -1,7 +1,7 @@{"$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json","name": "showtime",- "version": "0.4.3",+ "version": "0.4.4","description": "A local video studio for your coding agent. Describe a video. Your agent directs. Your machine renders. Motion graphics, voice-over, music, footage editing, captions and platform-ready exports, with no cloud AI services or API keys.","author": {"name": "Favio Vazquez",diff --git a/server.json b/server.jsonindex f3f5071..12fb564 100644--- a/server.json+++ b/server.json@@ -3,7 +3,7 @@"name": "io.github.FavioVazquez/showtime","title": "showtime","description": "Local video studio for your coding agent: scenes, voice, music and captions, on your machine.",- "version": "0.4.3",+ "version": "0.4.4","websiteUrl": "https://faviovazquez.github.io/showtime/","repository": {"url": "https://github.com/FavioVazquez/showtime",@@ -26,7 +26,7 @@"registryType": "npm","registryBaseUrl": "https://registry.npmjs.org","identifier": "@faviovazquez/showtime-mcp",- "version": "0.4.3",+ "version": "0.4.4","runtimeHint": "npx","transport": {"type": "stdio"diff --git a/site/README.md b/site/README.mdindex de43076..57d55d8 100644--- a/site/README.md+++ b/site/README.md@@ -11,6 +11,7 @@ the site itself, under their OFL licenses in `static/fonts/`).| `build.py` | the build: Markdown to HTML (markdown-it-py), pages, search index, media copy || `config.json` | the one value to set: `repo` (`owner/name`); plus `site_url` (the published address), the version and the hero film (its folder, film, still, length and credit) || `content/crew.json` | the one-line job of each crew member, as the crew page shows it |+| `content/xray.json` | the X-ray page (`xray/`): its hero, the kinds, and the gallery's entries (title, object, mechanism, layout, theme, aspect, receipt, clip credit and license, commands, author). Each video and poster is looked up as `examples/_xray/<file>` (the examples checkout, or `<dir>/_xray/<file>` in a `--media-from` folder, or its release asset) and copied to `media/xray/`; a missing film shows its poster, a missing poster a plain card. An entry with no receipt shows as "In the works", with no player and no commands || `content/previews.json` | the 4-second window of each example the gallery previews on hover || `content/harness.html` | the body of "How showtime is built" (`harness/`): the crew's operating manual, in its own design (`static/harness.css`, scoped to `.harness`, and `static/harness.js`; its fonts are in `static/fonts/` under the OFL). Its first line, `<!-- ARTICLE_URL=... -->`, is where section 14's "Favio on X" link goes. `media/NAME` in it is `examples/_harness/NAME` (loops, posters, the 0.4.3 film, the link-preview card) || `static/` | `style.css`, `app.js` (theme, gallery filters and players, copy buttons, search), fonts |diff --git a/site/build.py b/site/build.pyindex 686da96..ac4df50 100644--- a/site/build.py+++ b/site/build.py@@ -536,7 +536,8 @@ def shell(site: Site, page: str, title: str, body: str, active: str = "", desc:fav = site.copy_asset(REPO / "assets/brand/icon/favicon.svg")og = site.copy_asset(og_image or REPO / "assets/brand/social/github-social-preview.png")touch = site.copy_asset(REPO / "assets/brand/icon/apple-touch-icon.png")- nav = [("gallery.html", "Examples", "gallery"), ("crew.html", "Crew", "crew"), ("docs/index.html", "Docs", "docs")]+ nav = [("gallery.html", "Examples", "gallery"), (XRAY_PAGE, "X-ray", "xray"), ("crew.html", "Crew", "crew"),+ ("docs/index.html", "Docs", "docs")]links = "".join('<a href="%s"%s>%s</a>' % (r(h), ' aria-current="page"' if k == active else "", t) for h, t, k in nav)if site.repo:links += '<a href="https://github.com/%s">GitHub</a>' % esc(site.repo)@@ -582,7 +583,7 @@ def shell(site: Site, page: str, title: str, body: str, active: str = "", desc:</main><footer class="site"><div class="wrap"><span><span class="word">showtime</span> {version}, MIT licensed. A local video studio for your coding agent.</span>-<nav aria-label="Footer"><a href="{gallery}">Examples</a><a href="{crew}">Crew</a><a href="{docs}">Docs</a><a href="{harness}">How it\u2019s built</a>{exrepo}</nav>+<nav aria-label="Footer"><a href="{gallery}">Examples</a><a href="{xray}">X-ray</a><a href="{crew}">Crew</a><a href="{docs}">Docs</a><a href="{harness}">How it\u2019s built</a>{exrepo}</nav><span>No trackers, no cookies. Fonts are served from this site. Film music by Scott Buckley, CC BY 4.0: \u201cSupernova\u201d (the showreel), \u201cArtemis\u201d (the crew film), \u201cWith These Hands\u201d ({hands}).</span></div></footer><script src="{js}"></script>@@ -592,7 +593,7 @@ def shell(site: Site, page: str, title: str, body: str, active: str = "", desc:og_url=('\n<meta property="og:url" content="%s">\n<link rel="canonical" href="%s">' % (esc(page_abs), esc(page_abs)))if page_abs else "", fav=r(fav), touch=r(touch), css=r("static/style.css"), boot=THEME_BOOT,root="../" * page.count("/"), home=r("index.html"), menu=ICON_MENU, links=links, sun=ICON_SUN, body=body,- version=esc(VERSION), docs=r("docs/index.html"), gallery=r("gallery.html"), crew=r("crew.html"),+ version=esc(VERSION), docs=r("docs/index.html"), gallery=r("gallery.html"), crew=r("crew.html"), xray=r(XRAY_PAGE),harness=r(HARNESS_PAGE), og_w=og_size[0], og_h=og_size[1], head=("\n" + head) if head else "",js=r("static/app.js"), font=r("static/fonts/inter.woff2"),hands="the launch film and the recap demo" if CONFIG.get("gallery_film") else "the recap demo",@@ -1383,6 +1384,325 @@ Make it quick, no crew.</code></pre></div></div></section>write(site, page, shell(site, page, "The crew", body, "crew", "Ten optional specialist sub-agents, one director."))+# ----------------------------------------------------------------------------------------------- x-ray++XRAY_PAGE = "xray/index.html"+XRAY = json.loads((SITE / "content" / "xray.json").read_text(encoding="utf-8"))+XRAY_SRC = "examples/_xray" # where an entry's video and poster are looked up (the examples tree, or a --media-from folder)+XRAY_LIBRARY = REPO / "skills" / "showtime" / "library" / "xray"+ICON_CHECK = ('<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" '+ 'stroke-linejoin="round" aria-hidden="true"><path d="m5 12.5 4.5 4.5L19 7.5"/></svg>')+++def xray_media(site: Site, name: str) -> Optional[str]:+ """An X-ray entry's video or poster, copied to media/xray/NAME; None when this build cannot find it."""+ if not name:+ return None+ dst = "media/xray/" + name+ if dst in site.copied:+ return dst+ src = site.find_media("%s/%s" % (XRAY_SRC, name))+ if not src:+ return None+ p = site.out / dst+ p.parent.mkdir(parents=True, exist_ok=True)+ shutil.copy2(src, p)+ site.copied[dst] = "%s/%s" % (XRAY_SRC, name)+ return dst+++def xray_entries() -> List[dict]:+ """The gallery's entries, checked: unique ids, a known kind, a hero that exists. A finished entry (one with a+ receipt) needs its credit, license and commands: nothing appears as finished without them."""+ entries = XRAY.get("entries", [])+ kinds = {k["id"] for k in XRAY.get("kinds", [])}+ problems, seen = [], set()+ for e in entries:+ if e["id"] in seen:+ problems.append("%s is listed twice" % e["id"])+ seen.add(e["id"])+ if e.get("kind") not in kinds:+ problems.append("%s: kind %r is not one of %s" % (e["id"], e.get("kind"), ", ".join(sorted(kinds))))+ if e.get("theme") not in ("dark", "light"):+ problems.append("%s: theme must be dark or light" % e["id"])+ if e.get("receipt"):+ missing = [k for k in ("credit", "license", "commands") if not e.get(k)]+ if missing:+ problems.append("%s has a receipt but no %s" % (e["id"], " or ".join(missing)))+ if XRAY.get("hero") not in seen:+ problems.append("hero %r is not an entry" % XRAY.get("hero"))+ if problems:+ sys.exit("site/build.py: site/content/xray.json:\n " + "\n ".join(problems))+ return entries+++def xray_receipt(line: str) -> str:+ """A receipt line, its parts kept whole on a narrow screen ("19 of 19 events on their frame" \u00b7 "audio 0.0 ms ...")."""+ return '<span>%s</span>' % " \u00b7 ".join('<span class="w">%s</span>' % esc(x) for x in line.split(" \u00b7 "))+++def xray_cmds(lines: List[str]) -> str:+ """Commands for a copy block, one per line; a long one wraps at its spaces with a hanging indent (CSS .xl)."""+ return '<div class="copy install xcmds"><pre><code>%s</code></pre></div>' % "".join(+ '<span class="xl">%s</span>' % " ".join('<span class="w">%s</span>' % esc(w) for w in c.split(" ")) for c in lines)+++def xray_blank(e: dict) -> str:+ """The neutral card where an entry has no poster in this build: its object, drawn as a plate."""+ return ('<div class="xblank" aria-hidden="true"><span class="xblank-mark">X-ray</span><span class="xblank-obj">%s</span></div>'+ % esc(e.get("object", "")))+++def http_url(u: object) -> str:+ """A link target for an href: http(s) only, else empty (no javascript: or data: from a community entry)."""+ u = str(u or "").strip()+ return u if re.match(r"^https?://", u, re.I) else ""+++def xray_credit(e: dict, posted: bool = True) -> str:+ """The clip's credit and license, then who posted the entry (ours: showtime; a community one: its GitHub handle)."""+ lic = esc(e.get("license", ""))+ if http_url(e.get("license_url")):+ lic = '<a href="%s">%s</a>' % (esc(http_url(e["license_url"])), lic)+ who = e.get("author", "showtime")+ if who.startswith("@"):+ who = '<a href="https://github.com/%s">%s</a>' % (esc(who[1:]), esc(who))+ else:+ who = esc(who)+ src = ' \u00b7 <a href="%s">Source</a>' % esc(http_url(e["credit_url"])) if http_url(e.get("credit_url")) else ""+ film = (" \u00b7 " + esc(e["film_license"])) if e.get("film_license") else ""+ return '%s, %s%s%s%s' % (esc(e.get("credit", "")), lic, src, film, (" \u00b7 posted by " + who) if posted else "")+++def xray_card(site: Site, page: str, e: dict) -> str:+ layouts = XRAY.get("layouts", {})+ kinds = {k["id"]: k["name"] for k in XRAY.get("kinds", [])}+ done = bool(e.get("receipt"))+ video = xray_media(site, e.get("video", "")) if done else None+ poster = xray_media(site, e.get("poster", ""))+ tall = e.get("aspect") == "9:16"+ label = "%s, %s" % (e["title"], layouts.get(e.get("layout", ""), e.get("layout", "")))+ pic = ('<img class="still" src="%s" alt="%s" loading="lazy" decoding="async">' % (rel(page, poster), esc(label))+ if poster else xray_blank(e))+ action, clip, links = "", "", []+ if video:+ clip = ' data-clip="%s"' % rel(page, video)+ action = ('<button class="open" type="button" data-full="%s" aria-label="Play %s with sound"></button>'+ '<span class="hint" aria-hidden="true">%s Play with sound</span>' % (rel(page, video), esc(e["title"]), ICON_PLAY))+ elif done:+ asset = ASSET_BY_PATH.get("%s/%s" % (XRAY_SRC, e.get("video", "")))+ fb = site.release_url(asset) if asset else None+ if fb:+ links.append('<a href="%s">Watch the MP4</a>' % esc(fb))+ badge = "" if done else '<span class="badge xpending">In the works</span>'+ spec = " · ".join(x for x in (kinds.get(e["kind"], ""), layouts.get(e.get("layout", ""), ""), e.get("theme", ""), e.get("aspect", "")) if x)+ mech = e.get("mechanism", "")+ if mech and (XRAY_LIBRARY / mech).is_dir():+ url = site.gh("skills/showtime/library/xray/" + mech, is_dir=True)+ links.append(('<a href="%s">The mechanism: <code>%s</code></a>' % (esc(url), esc(mech))) if url+ else '<span>The mechanism: <code>%s</code> in the library</span>' % esc(mech))+ elif mech:+ links.append('<span class="muted">The <code>%s</code> mechanism joins the library with its film</span>' % esc(mech))+ live = xray_media(site, e.get("live", "")) if done else None+ ratio = "9/16" if tall else "16/9"+ if done:+ rows = ['<p class="xreceipt">%s%s</p>' % (ICON_CHECK, xray_receipt(e["receipt"])),+ '<p class="xcredit">%s</p>' % xray_credit(e)]+ if live:+ rows.insert(0, '<div class="xacts"><button class="btn primary xlive" type="button" data-live="%s" data-ratio="%s" '+ 'data-title="%s" aria-pressed="false">Play it live</button></div>' % (rel(page, live), ratio, esc(e["title"])))+ if e.get("prompt"):+ rows.append('<div class="xline"><h4>Make your own with one line</h4><p class="ask">\u201c%s\u201d</p></div>' % esc(e["prompt"]))+ rows.append('<details class="xmake"><summary>Make your own</summary>%s</details>' % xray_cmds(e["commands"]))+ base = site_url(site.repo)+ if live and base:+ src = base + live+ ptag = (' poster="%s"' % esc(base + poster)) if poster else ""+ tag = ('<script src="%sxray/embed.js"></script>\n<showtime-xray src="%s" ratio="%s" title="%s"%s></showtime-xray>'+ % (base, src, ratio, esc(e["title"]), ptag))+ # the film's own height at that width (420 x 16/9 = 747 and 960 x 9/16 = 540) plus about 320 px of controls+ size = 'width="420" height="1067"' if tall else 'width="960" height="860"'+ frame = '<iframe src="%s#embed" title="%s" %s allow="autoplay; clipboard-write" loading="lazy" style="border:0;max-width:100%%"></iframe>' % (src, esc(e["title"]), size)+ blocks = lambda t: '<div class="copy"><pre><code>%s</code></pre></div>' % esc(t) # noqa: E731+ rows.append('<details class="xmake xembed"><summary>Embed</summary><p class="xembed-note">On a page of yours, with the script:</p>%s'+ '<p class="xembed-note">Or as a plain frame:</p>%s</details>' % (blocks(tag), blocks(frame)))+ else:+ rows = ['<p class="xcredit">The film is made; it appears here with its receipt and its clip credit.</p>']+ return ('<article class="film xcard%s" data-kind="%s" data-xtheme="%s" id="x-%s"><div class="frame xframe%s"%s>%s%s%s</div>'+ '<div class="meta"><h3>%s</h3><span class="spec">%s</span></div><p class="shows">%s</p>%s<div class="links">%s</div></article>'+ % ("" if done else " pending", esc(e["kind"]), esc(e["theme"]), esc(e["id"]), " tall" if tall else "", clip, pic, badge, action,+ esc(e["title"]), esc(spec), esc(e.get("what", "")), "".join(rows), "".join(links)))+++def touch_card(site: Site, page: str, e: dict) -> str:+ """A living diagram people touch (site/content/xray.json "touch"): no clip, one live figure (`showtime export html+ <project> --interactive`), played in place like an X-ray's live page, with the line that made it and an embed."""+ live = xray_media(site, e.get("figure", ""))+ poster = xray_media(site, e.get("poster", ""))+ pic = ('<img class="still" src="%s" alt="%s" loading="lazy" decoding="async">' % (rel(page, poster), esc(e["title"]))+ if poster else '<div class="xblank" aria-hidden="true"><span class="xblank-mark">Live figure</span><span class="xblank-obj">%s</span></div>' % esc(e["title"]))+ rows = []+ if live:+ rows.append('<div class="xacts"><button class="btn primary xlive" type="button" data-live="%s" data-ratio="16/9" data-title="%s" '+ 'aria-pressed="false">%s</button></div>' % (rel(page, live), esc(e["title"]), esc(e.get("verb", "Touch it live"))))+ if e.get("prompt"):+ rows.append('<div class="xline"><h4>Make your own with one line</h4><p class="ask">\u201c%s\u201d</p></div>' % esc(e["prompt"]))+ if e.get("commands"):+ rows.append('<details class="xmake"><summary>Make your own</summary>%s</details>' % xray_cmds(e["commands"]))+ base = site_url(site.repo)+ if live and base:+ tag = ('<script src="%sxray/embed.js"></script>\n<showtime-xray src="%s" ratio="16/9" title="%s"%s></showtime-xray>'+ % (base, base + live, esc(e["title"]), (' poster="%s"' % esc(base + poster)) if poster else ""))+ rows.append('<details class="xmake xembed"><summary>Embed</summary><p class="xembed-note">On a page of yours, with the script:</p>'+ '<div class="copy"><pre><code>%s</code></pre></div></details>' % esc(tag))+ return ('<article class="film xcard xtouch" id="t-%s"><div class="frame xframe">%s</div><div class="meta"><h3>%s</h3>'+ '<span class="spec">%s</span></div><p class="shows">%s</p>%s</article>'+ % (esc(e["id"]), pic, esc(e["title"]), esc(e.get("spec", "living diagram · live figure")), esc(e.get("what", "")), "".join(rows)))+++def build_xray(site: Site) -> None:+ """xray/: the X-ray page. A hero (the flagship film looping, muted, its receipt under it), how it works in three+ steps with the real commands, the gallery from site/content/xray.json with a filter by kind and by theme, and+ Lane 2: how anyone adds an X-ray or a mechanism, what is checked before it appears."""+ page = XRAY_PAGE+ entries = xray_entries()+ hero = next(e for e in entries if e["id"] == XRAY["hero"])+ # the hero loops a silent cut when the entry names one ("loop"), else the film itself, muted+ hvideo = xray_media(site, hero.get("loop", "")) or xray_media(site, hero.get("video", ""))+ hposter = xray_media(site, hero.get("poster", ""))+ if hvideo:+ screen = ('<video class="teaser" muted loop playsinline preload="auto"%s aria-label="%s, muted"><source src="%s" type="video/mp4"></video>'+ % (' poster="%s"' % rel(page, hposter) if hposter else "", esc(hero["title"]), rel(page, hvideo)))+ elif hposter:+ screen = '<img src="%s" alt="%s">' % (rel(page, hposter), esc(hero["title"]))+ else:+ screen = xray_blank(hero)+ hlive = xray_media(site, hero.get("live", "")) if hero.get("receipt") else None+ hero_live = ('<button class="btn primary xlive xlive-hero" type="button" data-live="%s" data-ratio="%s" data-title="%s" aria-pressed="false">Try it live</button>'+ % (rel(page, hlive), "9/16" if hero.get("aspect") == "9:16" else "16/9", esc(hero["title"]))) if hlive else ""+ embed_src = REPO / "skills" / "showtime" / "runtime" / "xray-live" / "embed.js"+ embed_tag = ""+ if embed_src.is_file():+ ed = site.out / "xray" / "embed.js"+ ed.parent.mkdir(parents=True, exist_ok=True)+ shutil.copy2(embed_src, ed)+ embed_tag = '<script src="%s" defer></script>' % rel(page, "xray/embed.js")+ guide = "docs/guides/xray.html"+ guide_href = rel(page, guide) if guide in site.pages.values() else rel(page, "docs/workflows/xray.html")+ repo = site.repo+ form = ("https://github.com/%s/issues/new?template=xray-gallery.yml" % esc(repo)) if repo else ""+ contrib = rel(page, "docs/contributing.html") + "#add-an-x-ray-mechanism"+ library = site.gh("skills/showtime/library/xray", is_dir=True)+ kinds = XRAY.get("kinds", [])+ n_done = sum(1 for e in entries if e.get("receipt"))++ def tabs(group: str, label: str, items: List[Tuple[str, str]]) -> str:+ return ('<div class="filters xfilter" role="group" aria-label="%s">%s</div>' % (esc(label), "".join(+ '<button class="tab xtab" type="button" data-xgroup="%s" data-x="%s" aria-pressed="%s">%s<span class="c">%d</span></button>'+ % (group, k, "true" if k == "all" else "false", esc(n),+ len(entries) if k == "all" else sum(1 for e in entries if e.get(group) == k))+ for k, n in items)))+ filters = (tabs("kind", "Filter by kind", [("all", "All")] + [(k["id"], k["name"]) for k in kinds])+ + tabs("theme", "Filter by theme", [("all", "Any theme"), ("dark", "Dark"), ("light", "Light")]))+ cards = "".join(xray_card(site, page, e) for e in entries)+ touches = XRAY.get("touch", [])+ touch = ("""<section class="section" id="touch"><div class="wrap">+<div class="section-head"><h2 class="title" id="touch-a-living-diagram">No clip? Touch a living diagram</h2>+<p class="lede">The same drawings, with no video: a living diagram you type on, press and drag. Each one is a single+offline file made by <code>showtime export html --interactive</code>, live right here.</p></div>+<div class="films three xgrid">%s</div></div></section>+""" % "".join(touch_card(site, page, e) for e in touches)) if touches else ""+ body = """<section class="house xhero"><div class="wrap">+<div class="hero-head"><div><p class="xkicker">X-ray</p><h1 class="title">See inside a real thing, in time with its own sound.</h1>+<p class="lede">Film something doing its thing. showtime finds every event in the clip's sound, draws the inside moving on those+exact frames, on the object as the camera moves or beside it, and proves it with a receipt.</p></div>+<div class="actions">{hero_live}<a class="btn {make_cls}" href="#how">Make your own</a><a class="link-arrow" href="#gallery">The gallery <span>→</span></a></div></div>+<div class="screen lights xscreen">{screen}</div>+<p class="xreceipt xreceipt-hero">{check}{receipt}</p>+<p class="credit">{title}. {credit}. The receipt is <code>showtime xray verify</code> reading the finished film: the sync marker+on every frame against the events, and its audio against the original clip.</p>+</div></section>++<section class="section" id="how"><div class="wrap">+<div class="section-head"><h2 class="title" id="how-it-works">How it works</h2>+<p class="lede">Three commands, or one sentence to your agent: “X-ray this clip of my typewriter, drawn on the machine.”+It runs these for you, looks at the frames and tells you what it measured and what it drew.</p></div>+<ol class="xsteps">+<li><span class="xn">1</span><h3>Film it, then measure it</h3><p>A real thing doing its thing, with its sound: a typewriter typing, a+music box playing. A phone is fine. Every event in the sound gets its time and its frame; <code>--track</code> follows the object as+the camera moves.</p>{cmd1}</li>+<li><span class="xn">2</span><h3>Draw the inside</h3><p>Pick a mechanism from the library and a look: see-through on the object,+beside the clip, or a living diagram; dark like a radiograph or light like a blueprint. Each strike peaks on its event's frame.</p>+{cmd2}</li>+<li><span class="xn">3</span><h3>Verify it</h3><p>The finished film is read back frame by frame and its sound is matched to the+clip's. The line it prints goes on the end card and under the film wherever you post it.</p>+{cmd3}</li>+</ol>+<div class="facts xfacts"><div><h3>See-through</h3><p>The inside drawn on the object itself, inside its outline, as the camera moves. Objects only, never people.</p></div>+<div><h3>Beside it, or as a diagram</h3><p>When the object will not hold still, the inside sits beside the clip, or as an isometric living diagram.</p></div>+<div><h3>Music from the sound</h3><p>Pitches, key and tempo from an instrument's sound: a score for a piano, a tab for a guitar, a drum sheet for a kit.</p></div>+<div><h3>Honest about what is drawn</h3><p>On screen: what was measured (event times, counts, what the clip shows) and what was drawn (a generic mechanism of that kind).</p></div></div>+</div></section>++<section class="section" id="gallery"><div class="wrap">+<div class="section-head"><h2 class="title" id="the-gallery">The gallery</h2>+<p class="lede">Ours and yours. Every finished film carries its receipt, and every one can be copied: the commands are under it and+its mechanism is in the library.</p></div>+<div class="xfilters">{filters}</div>+<p class="count xcount" aria-live="polite">{n} films</p>+<div class="films three xgrid">{cards}</div>+<p class="note xnote">{n_done} finished, {n_pending} in the works. A film that is not here yet shows its poster or a plain card until its+receipt and its credit are in.</p>+</div></section>++{touch}<section class="section" id="lane-2"><div class="wrap">+<div class="section-head"><h2 class="title" id="add-yours">Lane 2: add yours</h2>+<p class="lede">The library grows with every verified X-ray. Post a film to the gallery, or add a mechanism so everyone can draw+that object; we post ours the same way, and every entry is there to copy.</p></div>+<div class="split xlane" style="margin-top:0">+<div><h3>Post your X-ray</h3>+<p>Make it with the three commands above, on a clip you filmed yourself or one under a Creative Commons license. Then open the+gallery form with the film's link, the object, the mechanism you used (or the new one you wrote), the verify line pasted as+it printed, and the clip's credit and license.</p>+<p>{form_link}</p></div>+<div><h3>Add a mechanism</h3>+<p>A new object starts as a copy of the closest entry in the {library_link}. Open a pull request that adds one folder:</p>+<pre class="xtree"><code>skills/showtime/library/xray/<object>/+ mechanism.js # its parts, four anchors, draw(k)+ diagram.js # optional: the living-diagram scene+ README.md # parts, what an event moves, measured vs drawn,+ # "Verified on" + the receipt + the clip credit</code></pre>+<p><a class="link-arrow" href="{contrib}">The pull request checklist <span>→</span></a></p></div></div>+<div class="facts xchecks"><div><h3>The receipt</h3><p>We run <code>showtime xray verify</code> on the film: every event on its frame and the audio+within 20 ms of the clip. A film without its line does not appear.</p></div>+<div><h3>The credit</h3><p>Your own footage, or a clip with a license that allows it (CC0, CC BY, CC BY-SA) and its credit line. No license+found, no entry.</p></div>+<div><h3>Objects only</h3><p>An inside is drawn over machines and instruments, never over people. A person in the frame gets the+beside-the-clip layout.</p></div>+<div><h3>Copyable</h3><p>Each entry lists the commands that made it and links its mechanism, so anyone can make their own from+it with <code>showtime new xray <dir> --mechanism <object></code>.</p></div></div>+<p class="more"><a class="link-arrow" href="{guide}">The X-ray guide: film it, pick a mechanism, music and drums, verify, share <span>→</span></a></p>+</div></section>+{embed_tag}+""".format(screen=screen, check=ICON_CHECK, hero_live=hero_live, make_cls="ghost" if hero_live else "primary", embed_tag=embed_tag, receipt=xray_receipt(hero["receipt"]), title=esc(hero["title"]), credit=xray_credit(hero, posted=False),+ cmd1=xray_cmds(["showtime xray analyze clip.mp4 --job xray-typewriter"]),+ cmd2=xray_cmds(["showtime new xray xray-typewriter/project --mechanism typewriter --theme dark",+ "showtime render xray-typewriter/project --job xray-typewriter"]),+ cmd3=xray_cmds(["showtime xray verify xray-typewriter"]),+ filters=filters, cards=cards, n=len(entries), n_done=count_words(n_done), n_pending=count_words(len(entries) - n_done).lower(),+ form_link=('<a class="btn primary" href="%s">Post an X-ray to the gallery</a>' % form) if form+ else "The gallery form: <code>.github/ISSUE_TEMPLATE/xray-gallery.yml</code> in the repository.",+ library_link=('<a href="%s">mechanism library</a>' % esc(library)) if library else "mechanism library (<code>skills/showtime/library/xray/</code>)",+ contrib=contrib, guide=guide_href, touch=touch)+ missing = [e["id"] for e in entries if e.get("receipt") and not xray_media(site, e.get("video", ""))]+ write(site, page, shell(site, page, "X-ray", body, "xray",+ "Film a real thing doing its thing: showtime draws its inside moving on the exact frames its own sound "+ "says, and proves it with a receipt. A gallery of verified X-rays, and how to add yours."))+ site.search.append(built_page_entry(site, "X-ray", page))+ print("x-ray: %d entries (%d finished); films not found in this build, shown as posters or plain cards: %s"+ % (len(entries), n_done, ", ".join(missing) or "none"))++# ----------------------------------------------------------------------------------------------- landingdef released(text: Optional[str] = None, version: str = "") -> List[Tuple[str, str, str]]:@@ -1509,6 +1829,11 @@ def build_landing(site: Site, exs) -> None:teaser = [e for e in exs if e["num"] in ("02", "12", "17", "19")]_new_ver, new_text = latest_release()new = new_tile(site, page, exs, new_text)+ # X-ray: the flagship's poster (copied by build_xray), its receipt and a way to the page+ xhero = next(e for e in xray_entries() if e["id"] == XRAY["hero"])+ xposter = xray_media(site, xhero.get("poster", ""))+ xpic = ('<img src="%s" alt="%s" loading="lazy" decoding="async">' % (rel(page, xposter), esc(xhero["title"]))+ if xposter else xray_blank(xhero))body = """<section class="house"><div class="wrap"><div class="hero-head"><div><h1 class="title">Describe a video. Your agent directs. Your machine renders.</h1><p class="lede">showtime is a local video studio for your coding agent: motion graphics, voice-over, music, captions and footage editing@@ -1547,6 +1872,15 @@ Make a 20-second launch video for this repo, with a voice-over and upbeat music.<p class="more"><a class="link-arrow" href="gallery.html">All {nexs} examples <span>→</span></a></p></div></section>+<section class="section" id="xray"><div class="wrap">+<div class="xland"><a class="xland-film" href="{xpage}" aria-label="The X-ray page">{xpic}</a>+<div><p class="xkicker">X-ray</p><h2 class="title">See inside a real thing, in time with its own sound</h2>+<p class="lede">Film a typewriter typing or a music box playing. showtime draws its inside moving on the exact frames the clip's sound+says, on the object as the camera moves or beside it, and proves it with a receipt.</p>+<p class="xreceipt">{xcheck}{xreceipt}</p>+<p class="more" style="margin-top:24px"><a class="link-arrow" href="{xpage}">The X-ray gallery, and how to add yours <span>→</span></a></p></div></div>+</div></section>+<section class="section" id="crew"><div class="wrap"><div class="section-head"><h2 class="title">A crew, when you want one</h2><p class="lede">Your agent can cast ten specialist sub-agents. Every finished video gets a critic by default; say "lean" to skip it (a video you will publish still gets one).@@ -1582,7 +1916,8 @@ bug-report video and a summary for the issue. <a href="examples/_repro.html">Seepipeline=art(site, page, "diagrams/pipeline", "How showtime works: one sentence, your agent directs, a first look, a local render, showtime qa, an MP4 and an HTML video."),teaser="".join(card(site, page, e, show_what=False) for e in teaser), nexs=len(exs),runs=art(site, page, "diagrams/runs-where", "What runs where: the director in your coding agent, the studio on your machine; the web only when you ask."),- cast=art(site, page, "crew/cast", "The crew: ten illustrated cards, all optional."))+ cast=art(site, page, "crew/cast", "The crew: ten illustrated cards, all optional."),+ xpage=rel(page, XRAY_PAGE), xpic=xpic, xcheck=ICON_CHECK, xreceipt=xray_receipt(xhero["receipt"]))write(site, page, shell(site, page, "showtime · a local video studio for your coding agent", body, "home"))@@ -1718,7 +2053,8 @@ def llms_txt(site: Site) -> str:link("docs/faq.html", "Questions people ask"),link("docs/index-claude.html", "The agent's own map (the reference index)"),link("docs/skill.html", "SKILL.md"),- link("gallery.html", "Examples")]+ link("gallery.html", "Examples"),+ link(XRAY_PAGE, "X-ray: a real clip with its inside drawn in sync, and the gallery")]skip = {"The crew's briefs", "Backstage"}listed = {"docs/index.html", "docs/index-claude.html", "docs/skill.html", "docs/agents.html", "docs/whats-new.html","docs/faq.html", "docs/guides/first-video.html", "docs/troubleshooting.html", "docs/glossary.html"}@@ -1782,6 +2118,7 @@ def main() -> None:build_example_pages(site, exs)build_crew(site)build_harness(site)+ build_xray(site)build_landing(site, exs)videos = tag_html_videos(site, exs)nurls = write_sitemap(site, videos)diff --git a/site/content/xray.json b/site/content/xray.jsonnew file mode 100644index 0000000..0e0b81e--- /dev/null+++ b/site/content/xray.json@@ -0,0 +1,344 @@+{+ "_about": "The X-ray page (site/build.py build_xray, the page xray/index.html): its hero, the three steps and the gallery. Each entry is one verified X-ray film. Its video and poster are looked up as examples/_xray/<file> (in this checkout, the examples checkout, or a --media-from folder, by path or by release asset name, like every example video) and copied to media/xray/<file>; a missing video shows the poster, a missing poster a neutral card. The hero entry may name a \"loop\": a short silent cut it loops instead of the whole film. An entry with an empty receipt is shown as \"in the works\": no player and no commands, because nothing appears as finished without its `showtime xray verify` line. author is \"showtime\" for ours or a GitHub handle (\"@name\") for a community entry; mechanism names a folder of skills/showtime/library/xray/, linked when it exists. An entry may name \"live\", its live page (the file `showtime export html <project> --interactive` writes), looked up and copied like the video: a finished card then has a \"Play it live\" button that swaps its frame for the page, and an Embed snippet. \"prompt\" is one sentence, the request an agent would be given to make that film; it is shown, with a Copy button, above the commands.",+ "hero": "typewriter-through-dark-16x9",+ "_kinds": "The kind filter: an entry's kind is one of these ids.",+ "kinds": [+ {+ "id": "machines",+ "name": "Machines"+ },+ {+ "id": "instruments",+ "name": "Instruments"+ }+ ],+ "_layouts": "How an entry's layout is named on its card.",+ "layouts": {+ "through": "see-through",+ "split": "beside the clip",+ "diagram": "living diagram",+ "score": "score",+ "tab": "guitar tab",+ "drums": "drum sheet"+ },+ "entries": [+ {+ "id": "typewriter-through-dark-16x9",+ "title": "A typewriter, seen through",+ "object": "Underwood 315 portable typewriter",+ "kind": "machines",+ "mechanism": "typewriter",+ "layout": "through",+ "theme": "dark",+ "aspect": "16:9",+ "video": "typewriter-through-dark-16x9.mp4",+ "loop": "typewriter-through-dark-16x9-loop.mp4",+ "poster": "typewriter-through-dark-16x9.jpg",+ "what": "Every keystroke swings its typebar up to the platen on the frame the clip's own sound says, drawn on the machine as the camera moves.",+ "receipt": "19 of 19 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: Erik Pålsson / Sounds of Changes",+ "license": "CC BY 3.0",+ "license_url": "https://creativecommons.org/licenses/by/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Underwood_315_portable_typewriter.webm",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-typewriter --track --region \"x,y x,y x,y x,y\" --ref-time 1.0",+ "showtime new xray xray-typewriter/project --mechanism typewriter --layout through --theme dark",+ "showtime render xray-typewriter/project --job xray-typewriter",+ "showtime xray verify xray-typewriter"+ ],+ "live": "typewriter-through-dark-16x9-live.html",+ "prompt": "/showtime x-ray this clip of my typewriter, drawn on the machine."+ },+ {+ "id": "typewriter-through-light-16x9",+ "title": "The same typewriter, as a blueprint",+ "object": "Underwood 315 portable typewriter",+ "kind": "machines",+ "mechanism": "typewriter",+ "layout": "through",+ "theme": "light",+ "aspect": "16:9",+ "video": "typewriter-through-light-16x9.mp4",+ "poster": "typewriter-through-light-16x9.jpg",+ "what": "The same clip and the same events in the light theme: the inside drawn as a blueprint on paper.",+ "receipt": "19 of 19 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: Erik Pålsson / Sounds of Changes",+ "license": "CC BY 3.0",+ "license_url": "https://creativecommons.org/licenses/by/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Underwood_315_portable_typewriter.webm",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-typewriter --track --region \"x,y x,y x,y x,y\" --ref-time 1.0",+ "showtime new xray xray-typewriter/project --mechanism typewriter --layout through --theme light",+ "showtime render xray-typewriter/project --job xray-typewriter",+ "showtime xray verify xray-typewriter"+ ],+ "live": "typewriter-through-light-16x9-live.html",+ "prompt": "/showtime x-ray this clip of my typewriter as a blueprint, drawn on the machine."+ },+ {+ "id": "typewriter-through-dark-9x16",+ "title": "A typewriter, for a phone",+ "object": "Underwood 315 portable typewriter",+ "kind": "machines",+ "mechanism": "typewriter",+ "layout": "through",+ "theme": "dark",+ "aspect": "9:16",+ "video": "typewriter-through-dark-9x16.mp4",+ "poster": "typewriter-through-dark-9x16.jpg",+ "what": "The vertical cut: the clip on top with its inside drawn in place, the mechanism below, every word inside the reels safe area.",+ "receipt": "19 of 19 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: Erik Pålsson / Sounds of Changes",+ "license": "CC BY 3.0",+ "license_url": "https://creativecommons.org/licenses/by/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Underwood_315_portable_typewriter.webm",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-typewriter --track --region \"x,y x,y x,y x,y\" --ref-time 1.0",+ "showtime new xray xray-typewriter/project --mechanism typewriter --layout through --theme dark",+ "showtime render xray-typewriter/project --job xray-typewriter",+ "showtime xray verify xray-typewriter"+ ],+ "live": "typewriter-through-dark-9x16-live.html",+ "prompt": "/showtime x-ray this clip of my typewriter, drawn on the machine, for a phone."+ },+ {+ "id": "typewriter-diagram-16x9",+ "title": "A typewriter, as a living diagram",+ "object": "Underwood 315 portable typewriter",+ "kind": "machines",+ "mechanism": "typewriter",+ "layout": "diagram",+ "theme": "dark",+ "aspect": "16:9",+ "video": "typewriter-diagram-16x9.mp4",+ "poster": "typewriter-diagram-16x9.jpg",+ "what": "The clip beside an isometric drawing of the mechanism, every part moving on the clip's own keystrokes.",+ "receipt": "18 of 18 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: Erik Pålsson / Sounds of Changes",+ "license": "CC BY 3.0",+ "license_url": "https://creativecommons.org/licenses/by/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Underwood_315_portable_typewriter.webm",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-typewriter",+ "showtime new xray xray-typewriter/project --mechanism typewriter --layout diagram --theme dark",+ "showtime render xray-typewriter/project --job xray-typewriter",+ "showtime xray verify xray-typewriter"+ ],+ "live": "typewriter-diagram-16x9-live.html",+ "prompt": "/showtime x-ray this clip of my typewriter as a living diagram beside it."+ },+ {+ "id": "musicbox-through-9x16",+ "title": "A music box disc, rebuilt from its sound",+ "object": "Disc music box",+ "kind": "instruments",+ "mechanism": "musicbox",+ "layout": "through",+ "theme": "dark",+ "aspect": "9:16",+ "video": "musicbox-through-9x16.mp4",+ "poster": "musicbox-through-9x16.jpg",+ "what": "Each pluck turns its star wheel and rings its comb tooth on the real disc, and the disc fills with what was played.",+ "receipt": "113 of 113 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: disc music box, Canada Science and Technology Museum",+ "license": "CC BY 3.0",+ "license_url": "https://creativecommons.org/licenses/by/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Music_Box_Plays_%22Angels_ever_bright_and_fair%22.webm",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-musicbox --track --region \"x,y x,y x,y x,y\" --ref-time 1.0",+ "showtime new xray xray-musicbox/project --mechanism musicbox --layout through --theme dark",+ "showtime render xray-musicbox/project --job xray-musicbox",+ "showtime xray verify xray-musicbox"+ ],+ "live": "musicbox-through-9x16-live.html",+ "prompt": "/showtime x-ray this music box clip, for a phone."+ },+ {+ "id": "musicbox-through-16x9",+ "title": "A music box disc, rebuilt from its sound (wide)",+ "object": "Disc music box",+ "kind": "instruments",+ "mechanism": "musicbox",+ "layout": "through",+ "theme": "dark",+ "aspect": "16:9",+ "video": "musicbox-through-16x9.mp4",+ "poster": "musicbox-through-16x9.jpg",+ "what": "Each pluck turns its star wheel and rings its comb tooth on the real disc, and the disc fills with what was played.",+ "receipt": "113 of 113 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: disc music box, Canada Science and Technology Museum",+ "license": "CC BY 3.0",+ "license_url": "https://creativecommons.org/licenses/by/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Music_Box_Plays_%22Angels_ever_bright_and_fair%22.webm",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-musicbox --track --region \"x,y x,y x,y x,y\" --ref-time 1.0",+ "showtime new xray xray-musicbox/project --mechanism musicbox --layout through --theme dark",+ "showtime render xray-musicbox/project --job xray-musicbox",+ "showtime xray verify xray-musicbox"+ ],+ "live": "musicbox-through-16x9-live.html",+ "prompt": "/showtime x-ray this music box clip."+ },+ {+ "id": "switch-through-16x9",+ "title": "A keyboard switch, seen through",+ "object": "1959 Cherry mechanical keyboard switch",+ "kind": "machines",+ "mechanism": "switch",+ "layout": "through",+ "theme": "dark",+ "aspect": "16:9",+ "video": "switch-through-16x9.mp4",+ "poster": "switch-through-16x9.jpg",+ "what": "Each click snaps the lever over its spring and closes the contact, drawn on the real switch on the frame the clip's own sound says.",+ "receipt": "6 of 6 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: “Clicking a 1959 Cherry mechanical keyboard switch”, LGR, Wikimedia Commons",+ "license": "CC BY 3.0",+ "license_url": "https://creativecommons.org/licenses/by/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Clicking_a_1959_Cherry_mechanical_keyboard_switch.webm",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-switch --track --region \"x,y x,y x,y x,y\" --ref-time 1.0",+ "showtime new xray xray-switch/project --mechanism switch --layout through --theme dark",+ "showtime render xray-switch/project --job xray-switch",+ "showtime xray verify xray-switch"+ ],+ "live": "switch-through-16x9-live.html",+ "prompt": "/showtime x-ray the click of this 1959 keyboard switch."+ },+ {+ "id": "piano-score-9x16",+ "title": "A piano, as a score",+ "object": "Piano",+ "kind": "instruments",+ "mechanism": "piano",+ "layout": "score",+ "theme": "dark",+ "aspect": "9:16",+ "video": "piano-score-9x16.mp4",+ "poster": "piano-score-9x16.jpg",+ "what": "The notes heard in the clip, written as a score that scrolls on a smoothed tempo map.",+ "receipt": "91 of 91 events on their frame · audio 0.0 ms from the original",+ "credit": "Minuet in G, BWV Anh. 114 (Notebook for Anna Magdalena Bach, attributed to Christian Petzold); recording KasraR, Wikimedia Commons",+ "license": "Public domain",+ "license_url": "",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Minuet-G-Major-BWV-Anh-114.ogv",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-piano",+ "showtime new xray xray-piano/project --mechanism piano --layout split --theme dark",+ "showtime render xray-piano/project --job xray-piano",+ "showtime xray verify xray-piano"+ ],+ "live": "piano-score-9x16-live.html",+ "prompt": "/showtime x-ray this piano recording as a score, for a phone."+ },+ {+ "id": "guitar-tab-9x16",+ "title": "A guitar, as a tab",+ "object": "Acoustic guitar",+ "kind": "instruments",+ "mechanism": "guitar",+ "layout": "tab",+ "theme": "dark",+ "aspect": "9:16",+ "video": "guitar-tab-9x16.mp4",+ "poster": "guitar-tab-9x16.jpg",+ "what": "Each note heard, placed on a string and a fret, as a tab that scrolls with the playing.",+ "receipt": "33 of 33 events on their frame · audio 0.1 ms from the original",+ "credit": "Clip: Elenaf, Wikimedia Commons",+ "license": "CC BY-SA 3.0",+ "license_url": "https://creativecommons.org/licenses/by-sa/3.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Fingerpicking_Pattern_4_p-ima_chitarra.webm",+ "film_license": "This film is under the same license, CC BY-SA 3.0 (share-alike).",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-guitar",+ "showtime new xray xray-guitar/project --mechanism guitar --layout split --theme dark",+ "showtime render xray-guitar/project --job xray-guitar",+ "showtime xray verify xray-guitar"+ ],+ "live": "guitar-tab-9x16-live.html",+ "prompt": "/showtime x-ray this guitar clip as a tab, for a phone."+ },+ {+ "id": "drums-sheet-9x16",+ "title": "A drum kit, as a drum sheet",+ "object": "Drum kit",+ "kind": "instruments",+ "mechanism": "drums",+ "layout": "drums",+ "theme": "dark",+ "aspect": "9:16",+ "video": "drums-sheet-9x16.mp4",+ "poster": "drums-sheet-9x16.jpg",+ "what": "Every hit sorted by its sound into kick, snare, hi-hat, toms and cymbals, written on a percussion staff.",+ "receipt": "30 of 30 events on their frame · audio 0.0 ms from the original",+ "credit": "Clip: Lukáš Perný, KC Dunaj Bratislava 2012, Wikimedia Commons",+ "license": "CC BY-SA 4.0",+ "license_url": "https://creativecommons.org/licenses/by-sa/4.0/",+ "credit_url": "https://commons.wikimedia.org/wiki/File:Luk%C3%A1%C5%A1_Pern%C3%BD%2C_drum_solo_%C5%A1ou_sample%2C_KC_Dunaj%2C_Bratislava%2C_2012.webm",+ "film_license": "This film is under the same license, CC BY-SA 4.0 (share-alike).",+ "author": "showtime",+ "commands": [+ "showtime xray analyze clip.mp4 --job xray-drums",+ "showtime new xray xray-drums/project --mechanism drums --layout split --theme dark",+ "showtime render xray-drums/project --job xray-drums",+ "showtime xray verify xray-drums"+ ],+ "live": "drums-sheet-9x16-live.html",+ "prompt": "/showtime x-ray this drum solo as a drum sheet, for a phone."+ }+ ],+ "_touch": "Living diagrams people touch (no clip): each a live figure from `showtime export html <project> --interactive`, found as examples/_xray/<figure> like the films; poster optional; verb is the button's words; prompt and commands as for the films.",+ "touch": [+ {+ "id": "typewriter",+ "title": "Type on a typewriter",+ "spec": "living diagram · type",+ "verb": "Type on it live",+ "what": "Type on your keyboard and the machine types it: the key goes down, its typebar swings up and strikes, the letter lands on the paper, the carriage steps, the bell rings near the margin, and Enter returns the carriage.",+ "figure": "typewriter-figure.html",+ "poster": "typewriter-figure.jpg",+ "prompt": "/showtime explain how a typewriter works inside, interactive: let me type on it.",+ "commands": [+ "showtime new diagram typewriter --example typewriter --theme dark",+ "showtime export html typewriter --interactive"+ ]+ },+ {+ "id": "computer",+ "title": "What happens when you press a key",+ "spec": "living diagram · type",+ "verb": "Type on it live",+ "what": "Type on your keyboard: the key goes down, its USB code waits for the next poll, the CPU takes the interrupt, and the screen draws it at its next refresh. Slowed down about 30 times; typical numbers, labelled.",+ "figure": "computer-figure.html",+ "poster": "computer-figure.jpg",+ "prompt": "/showtime explain what happens when you press a key, interactive.",+ "commands": [+ "showtime new diagram press-a-key --example computer",+ "showtime export html press-a-key --interactive"+ ]+ },+ {+ "id": "heat-pump",+ "title": "A heat pump: drag the weather",+ "spec": "living diagram · drag",+ "verb": "Touch it live",+ "what": "Drag the outdoor unit to make it colder or milder outside: the refrigerant and the heat moved per unit of power answer at once (illustrative numbers, from one formula).",+ "figure": "heat-pump-figure.html",+ "poster": "heat-pump-figure.jpg",+ "prompt": "/showtime explain how a heat pump works, interactive: let me drag the weather.",+ "commands": [+ "showtime new diagram heat-pump",+ "showtime export html heat-pump --interactive"+ ]+ }+ ]+}diff --git a/site/static/app.js b/site/static/app.jsindex 6fcc3f0..ec1c5fb 100644--- a/site/static/app.js+++ b/site/static/app.js@@ -141,6 +141,72 @@var h = (location.hash || '').slice(1); applyFilter(document.querySelector('.tab[data-filter="' + h + '"]') ? h : 'all');}+ // ---------- X-ray gallery: two filters over the same cards, by kind (machines, instruments) and by theme+ var xtabs = document.querySelectorAll('.xtab');+ if (xtabs.length) {+ var xsel = { kind: 'all', theme: 'all' };+ var xapply = function () {+ xtabs.forEach(function (b) { b.setAttribute('aria-pressed', b.getAttribute('data-x') === xsel[b.getAttribute('data-xgroup')]); });+ var n = 0;+ document.querySelectorAll('.xcard').forEach(function (c) {+ var on = (xsel.kind === 'all' || c.getAttribute('data-kind') === xsel.kind) && (xsel.theme === 'all' || c.getAttribute('data-xtheme') === xsel.theme);+ c.hidden = !on; if (on) n++;+ });+ var s = document.querySelector('.xcount');+ if (s) s.textContent = n ? n + (n === 1 ? ' film' : ' films') : 'No film is both yet: pick another filter.';+ };+ xtabs.forEach(function (b) { b.addEventListener('click', function () { xsel[b.getAttribute('data-xgroup')] = b.getAttribute('data-x'); xapply(); }); });+ xapply();+ }++ // ---------- X-ray live pages in place: "Play it live" / "Try it live". A card opens its live page on a stage as wide+ // as the gallery (the card spans every column, its poster frame hidden); the hero swaps its screen. One at a time:+ // the same button, or opening another, puts the poster back.+ var xopen = null;+ function xclose() {+ if (!xopen) return;+ var o = xopen; xopen = null;+ if (o.stage) { o.stage.remove(); o.frame.hidden = false; o.card.classList.remove('xcard-live'); }+ else { while (o.box.firstChild) o.box.removeChild(o.box.firstChild); o.box.appendChild(o.kept); o.box.classList.remove('xlive-on'); }+ o.btn.setAttribute('aria-pressed', 'false'); o.btn.textContent = o.label;+ var t = o.box && o.box.querySelector('video.teaser'); if (t && !reduce) t.play().catch(function () {});+ }+ function xlive(src, ratio, title) {+ var el;+ if (window.customElements && customElements.get('showtime-xray')) {+ el = document.createElement('showtime-xray'); el.setAttribute('src', src); el.setAttribute('ratio', ratio);+ el.setAttribute('title', title); el.setAttribute('autoload', '');+ } else { // embed.js did not load: the page itself, in a frame+ el = document.createElement('iframe'); el.className = 'xlive-frame'; el.src = src + '#embed'; el.title = title;+ el.style.setProperty('--xr', ratio); el.allow = 'autoplay; clipboard-write';+ el.style.minHeight = ratio === '9/16' ? '1067px' : '640px'; // the film plus the controls under it+ }+ return el;+ }+ document.addEventListener('click', function (e) {+ var b = e.target.closest('button.xlive'); if (!b) return;+ var was = xopen && xopen.btn === b; xclose(); if (was) return;+ var src = safeSrc(b.getAttribute('data-live')); if (!src) return;+ var ratio = b.getAttribute('data-ratio') || '16/9', title = b.getAttribute('data-title') || 'X-ray';+ stopOthers(null);+ document.querySelectorAll('.xgrid video, .xscreen video').forEach(function (v) { v.pause(); });+ if (b.classList.contains('xlive-hero')) {+ var box = b.closest('.house').querySelector('.xscreen'); if (!box) return;+ var kept = document.createDocumentFragment(); while (box.firstChild) kept.appendChild(box.firstChild);+ box.classList.add('xlive-on'); box.appendChild(xlive(src, ratio, title));+ xopen = { box: box, kept: kept, btn: b, label: b.textContent };+ } else {+ var card = b.closest('.xcard'), frame = card && card.querySelector('.xframe'); if (!frame) return;+ var stage = document.createElement('div');+ stage.className = 'xstage' + (ratio === '9/16' ? ' tall' : '');+ stage.appendChild(xlive(src, ratio, title));+ card.classList.add('xcard-live'); frame.hidden = true; frame.parentNode.insertBefore(stage, frame);+ xopen = { card: card, frame: frame, stage: stage, btn: b, label: b.textContent };+ requestAnimationFrame(function () { card.scrollIntoView({ behavior: reduce ? 'auto' : 'smooth', block: 'start' }); });+ }+ b.setAttribute('aria-pressed', 'true'); b.textContent = 'Back to the poster';+ });+// ---------- docs: contents that follow the reading positionvar toc = document.querySelectorAll('.toc a');if (toc.length && 'IntersectionObserver' in window) {diff --git a/site/static/style.css b/site/static/style.cssindex 54a3e6e..45b768d 100644--- a/site/static/style.css+++ b/site/static/style.css@@ -75,6 +75,8 @@ p{margin:0}.btn:active{transform:translateY(1px)}.btn.primary{background:#A3111D;color:#FFF8EF}.btn.primary:hover{background:#B8182A}+.btn.ghost{box-shadow:inset 0 0 0 1.5px rgba(243,234,221,.35);color:#F3EADD}+.btn.ghost:hover{background:rgba(243,234,221,.08)}.link-arrow{font-weight:600;text-decoration:none;display:inline-flex;align-items:center;gap:6px}.link-arrow span{transition:transform .2s var(--ease)}.link-arrow:hover span{transform:translateX(3px)}@@ -321,3 +323,90 @@ footer.site .word{font-family:var(--serif);font-weight:900;color:var(--ink);font.screen .watch em{font-style:normal;font-weight:500;color:#B9AB9C;font-size:.88rem}.screen.playing .watch{display:none}@media (max-width:560px){.screen .watch{left:12px;bottom:12px;padding-right:14px;font-size:.9rem}.screen .watch em{display:none}.screen .watch svg{width:34px;height:34px;padding:10px 9px 10px 11px}}++/* X-ray (xray/ and the landing's X-ray band): a film with its receipt, three steps, a gallery with two filters, Lane 2 */+.xkicker{font:600 .78rem var(--mono);letter-spacing:.14em;text-transform:uppercase;color:var(--mark);margin-bottom:14px}+.house .xkicker{color:#E9B949}+.xreceipt{display:flex;gap:8px;align-items:flex-start;margin-top:14px;font:500 .84rem/1.45 var(--mono);color:var(--ink);font-variant-numeric:tabular-nums}+.xreceipt svg{flex:none;width:16px;height:16px;margin-top:1px;color:var(--mark)}+.house .xreceipt-hero{margin-top:18px;font-size:.95rem;color:#F3EADD}+.house .xreceipt-hero svg{color:#E9B949;width:18px;height:18px}+.house .credit code{background:rgba(243,234,221,.08);color:#D6CABB}+.xscreen .xblank{position:absolute;inset:0}+.xblank{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:10px;width:100%;height:100%;padding:16px;text-align:center;+ background:linear-gradient(rgba(233,185,73,.07) 1px,transparent 1px) 0 0/28px 28px,linear-gradient(90deg,rgba(233,185,73,.07) 1px,transparent 1px) 0 0/28px 28px,+ radial-gradient(70% 70% at 50% 45%,#16202B,#080605)}+.xblank-mark{font:600 .74rem var(--mono);letter-spacing:.16em;text-transform:uppercase;color:#E9B949}+.xblank-obj{font:600 1.15rem/1.25 var(--serif);font-variation-settings:"opsz" 144;color:#D6CABB;max-width:22ch}+.frame .xblank{position:absolute;inset:0}+.xsteps{list-style:none;margin:0;padding:0;display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:32px}+.xsteps li{border-top:1px solid var(--line-2);padding-top:20px;min-width:0}+.xsteps .xn{display:block;font:600 2.2rem/1 var(--serif);font-variation-settings:"opsz" 144;color:var(--mark);margin-bottom:12px}+.xsteps h3{font-size:1.125rem;margin-bottom:8px}+.xsteps p{color:var(--ink-2);font-size:.96rem}+.xsteps .copy{margin-top:16px}+.xsteps pre{font-size:.78rem}+@media (max-width:960px){.xsteps{grid-template-columns:1fr;gap:28px}}+.xfacts{margin-top:56px}+.xfilters{display:flex;gap:8px 40px;flex-wrap:wrap}+.xfilters .filters{margin-top:0;flex:0 1 auto}+@media (max-width:720px){.xfilters .filters{flex:1 1 100%}}+.xgrid{margin-top:4px}+.xframe.tall .still,.xframe.tall .loop{object-fit:contain}+.xcard.pending .still{filter:saturate(.6) brightness(.72)}+.film .badge.xpending{background:rgba(14,11,9,.74);color:#F3EADD;box-shadow:inset 0 0 0 1px rgba(243,234,221,.22)}+.xcard .xcredit{margin-top:8px;color:var(--muted);font-size:.86rem;line-height:1.5}+.xcard .xcredit a{color:inherit}+.xmake{margin-top:12px;border-top:1px solid var(--line);padding-top:10px}+.xmake summary{cursor:pointer;font-weight:600;font-size:.9rem;color:var(--ink);list-style-position:inside}+.xmake summary::marker{color:var(--mark)}+.xmake .copy{margin-top:10px}+.xmake pre{font-size:.74rem;padding:.8rem .9rem}+.xcard .links code{font-size:.82em}+.xnote{margin-top:40px}+.xlane>div{min-width:0}+.xlane .btn{margin-top:4px}+.xchecks{margin-top:56px}+.xland{display:grid;grid-template-columns:minmax(0,1.15fr) minmax(0,1fr);gap:56px;align-items:center}+.xland .title{font-size:clamp(1.9rem,3.4vw,2.75rem)}+.xland .lede{margin-top:16px}+.xland-film{position:relative;display:block;aspect-ratio:16/9;border-radius:12px;overflow:hidden;background:var(--screen);box-shadow:0 24px 60px -32px rgba(0,0,0,.6)}+.xland-film img{display:block;width:100%;height:100%;object-fit:cover}+.xland-film .xblank{position:absolute;inset:0}+@media (max-width:860px){.xland{grid-template-columns:1fr;gap:28px}}+.xcard .meta{flex-direction:column;align-items:flex-start;gap:4px}+.xcard .spec{white-space:normal}+.xcmds pre{white-space:pre-wrap;overflow-wrap:normal;word-break:normal}+.xcmds .xl{display:block;padding-left:1.4em;text-indent:-1.4em}+.xcmds .xl+.xl{margin-top:4px}+.xcmds .w,.xreceipt .w{white-space:nowrap}+.xtree{margin:14px 0;font-size:.78rem;white-space:pre;overflow-x:auto}++/* X-ray live pages in place: the card's frame (or the hero screen) holds a <showtime-xray> element (runtime/xray-live/embed.js) */+.xacts{margin-top:14px}+.xacts .btn{height:38px;padding:0 16px;font-size:.88rem;cursor:pointer;border:0}+.xline{margin-top:12px}+.xline h4{font:600 .72rem var(--mono);letter-spacing:.12em;text-transform:uppercase;color:var(--mark)}+.xline .ask{margin-top:4px;font-size:.9rem}+.xembed .copy+.xembed-note{margin-top:12px}+.xembed-note{margin-top:10px;color:var(--muted);font-size:.84rem}+.xembed .copy{margin-top:6px}+.xembed pre{font-size:.72rem;padding:.8rem .9rem;white-space:pre-wrap;word-break:break-all}+.frame.xlive-on,.screen.xlive-on{aspect-ratio:auto;overflow:visible;background:transparent;box-shadow:none}+/* a card playing live spans the whole gallery; its stage is the live page, its words sit beside it on wide screens */+.xgrid .xcard-live{grid-column:1/-1;scroll-margin-top:84px;display:grid;grid-template-columns:minmax(0,2.2fr) minmax(0,1fr);gap:4px 28px;align-items:start}+.xgrid .xcard-live>.xstage{grid-row:1/span 20}+.xgrid .xcard-live>.xstage.tall{max-width:560px;justify-self:center;width:100%}+.xgrid .xcard-live>.xframe[hidden]{display:none}+.xstage showtime-xray,.xstage iframe.xlive-frame{display:block;width:100%}+.xstage iframe.xlive-frame{aspect-ratio:var(--xr,16/9);border:0;border-radius:12px;background:var(--screen)}+@media (max-width:900px){.xgrid .xcard-live{display:block}.xgrid .xcard-live>.xstage{margin-bottom:14px}}+.xlive-on>showtime-xray{display:block;position:static;width:100%}+.xlive-on>iframe.xlive-frame{position:static;display:block;width:100%;height:auto;aspect-ratio:var(--xr,16/9);border:0;border-radius:12px;background:var(--screen)}+showtime-xray{display:block;position:relative;width:100%;border-radius:12px;overflow:hidden;background:var(--screen)}+showtime-xray .stx{position:relative;width:100%}+showtime-xray .stx-poster{display:block;width:100%;height:auto;border-radius:12px;object-fit:cover}+showtime-xray .stx-play{position:absolute;left:14px;bottom:14px;appearance:none;border:0;border-radius:999px;background:rgba(14,11,9,.74);color:#F3EADD;font:600 .82rem var(--sans);padding:8px 14px;cursor:pointer}+showtime-xray .stx-play:hover{background:rgba(14,11,9,.92)}+showtime-xray .stx-poster,showtime-xray .stx-frame{position:static;inset:auto}+showtime-xray .stx-frame{display:block;width:100%;border:0;border-radius:12px;background:var(--screen)}diff --git a/skills/showtime/SKILL.md b/skills/showtime/SKILL.mdindex 74c9990..df4b2a1 100644--- a/skills/showtime/SKILL.md+++ b/skills/showtime/SKILL.md@@ -1,6 +1,6 @@---name: showtime-description: "Use when the user wants a video made, edited or finished: a launch or promo, product demo, explainer, trailer or teaser, tutorial or walkthrough, a screen recording turned into a demo, social short, reel, TikTok or YouTube Short, slideshow, animated chart or data story, motion graphics or animation, animated logo intro, outro or sting, lower thirds, audiogram, music video, Claude Design animation to MP4; captions or subtitles for a video; voice-over or narration; music or sound effects; thumbnail or poster frame; footage edits such as cutting the ums, removing silences, tightening a talking head, podcast or interview clips; reframing to vertical; re-voicing or subtitling a video in another language; turning a repo, website, pull request or changelog into a video; or when they type /showtime. Load it before replying, even to push back (a claim the user's own data does not support, an impossible spec or deadline). Not for editing still images, writing a caption for a photo post, or code unrelated to a video."+description: "Use when the user wants a video made, edited or finished: a launch or promo, product demo, explainer, trailer or teaser, tutorial or walkthrough, a screen recording turned into a demo, social short, reel, TikTok or YouTube Short, slideshow, animated chart or data story, motion graphics or animation, logo sting, lower thirds, audiogram, music video, Claude Design animation to MP4; an X-ray of their clip of a machine or instrument (its inside, in sync); captions or subtitles for a video; voice-over or narration; music or sound effects; thumbnail or poster frame; footage edits: cutting ums and silences, tightening a talking head, podcast or interview clips; reframing to vertical; a video in another language; turning a repo, website, pull request or changelog into a video; or when they type /showtime. Load it before replying, even to push back (a claim the user's own data does not support, an impossible spec or deadline). Not for still images, photo captions or code unrelated to a video."compatibility: Any Agent Skills client with a shell, on macOS, Linux or Windows. Needs Python 3.8+ (or uv) and Node.js 20+; a one-time `showtime setup` downloads the local tools and models into ~/.showtime (or $SHOWTIME_HOME) and needs the network for that step.---@@ -24,7 +24,7 @@ use its latest file.voice-over; full review, say 'lean' for a draft pass.") and start without waiting. Ask(1-2 questions, each with a recommended answer) only when the request is ambiguous: nothing to workfrom, or two readings that make different videos. Wait for a yes only before a destructive step- (dropping recorded content, touching their files) or an expensive one (a large install, a render+ (dropping recorded content, touching their files) or an expensive one (large install, renderover ~10 min).- **Asking: studio (opt-in)** when the user says studio, brainstorm, show me options or concepts first,storyboard it first, or wants control. Offer it once for high-stakes work@@ -61,7 +61,7 @@ Log each stage boundary in the ledger (`SHOWTIME.md`, `job.json`):*Done when:* check reports 0 errors and every WARN is read (footage: `edit check` passes, everyview PNG read).4. **Build.** Project: compose the hook complete at t=0 (`"poster": 0`), then- `showtime render <project> --job <job>` (`final.mp4`, `poster.jpg`; a later poster is baked in only when it matches the opening).+ `showtime render <project> --job <job>` (`final.mp4`, `poster.jpg`; a later poster bakes in only if it matches the opening).Footage: `showtime edit render <job> -o <job>/final.mp4`; poster via`showtime deliver poster <job> --at <t> --bake` (it becomes the latest final). Say the time ifover 30 s.@@ -99,6 +99,7 @@ Log each stage boundary in the ledger (`SHOWTIME.md`, `job.json`):| a PR (`showtime pr-video <N>`), release or changelog | `changelog-video` || an agent session as a film (`showtime recap <transcript>`) | `recap` || a failing browser test as a bug-report video (`showtime repro`) | `repro` |+| an X-ray of their clip (`showtime xray`) | `xray` || a trailer, teaser | `trailer` || a showreel, hype reel, "go all out" | `tones` || photos, screenshots with music | `slideshow` |@@ -129,7 +130,7 @@ Log each stage boundary in the ledger (`SHOWTIME.md`, `job.json`):| Tempting thought | Do this instead ||---|---|| "The render exited 0, so it's fine" | Run `showtime qa <job>`; the log proves frames were written, not that the file is right |-| "I'll wait for a yes before the final" (quick mode) | Show the first look and keep going; confirm only destructive or costly steps |+| "I'll wait for a yes before the final" (quick mode) | Show the first look, keep going; confirm only destructive or costly steps || "The template is close enough" | `--duration <len>` (or `showtime retime`); no placeholder left (74 %, northwind.example) || "check passed with warnings" | Read every WARN; `short_text`, still holds and `slow_scene` are real defects: fix them || "I'll call ffmpeg directly" | `footage trim`, `snap <video>`, `deliver exports --max-mb` (or `--targets gif,webp`) |@@ -142,7 +143,7 @@ Log each stage boundary in the ledger (`SHOWTIME.md`, `job.json`):| "Re-render everything for one fix" | `--from/--to --job`: splices the range; voice and transcripts are cached || "I'll ask which colours and features to show" | Look them up (repo, site, brand kit); say what you found || "The site blocked the capture; I'll work around it" | Exit code 3 is a bot wall: ask for screenshots, another URL or a local build |-| "Skip `job init`/`job note`, it's bookkeeping" | qa and resuming read the ledger |+| "Skip `job init`/`job note`" | qa and resuming read the ledger || "qa passed; skip the critic" | Quality mode runs it; lean only on request || "They asked for that headline" | Check it against their data (52 s vs 40 s is 1.3x, not 5x); flag it, offer the true figure (`story` §6) || "I'll take the spec and the deadline" | Say first what cannot be done, then what can, timed from `render` |diff --git a/skills/showtime/lib/st/__init__.py b/skills/showtime/lib/st/__init__.pyindex 89090b8..cc5b53d 100644--- a/skills/showtime/lib/st/__init__.py+++ b/skills/showtime/lib/st/__init__.py@@ -7,7 +7,7 @@ inside the showtime virtualenv (~/.showtime/venv, Python 3.12)."""import os-__version__ = "0.4.3"+__version__ = "0.4.4"# onnxruntime (Kokoro, alignment, stem separation, rembg) ships Microsoft's telemetry client on Linux# and starts it at import; this turns it off for every process that imports st, and for its children.diff --git a/skills/showtime/lib/st/assets/xray/LICENSE b/skills/showtime/lib/st/assets/xray/LICENSEnew file mode 100644index 0000000..dc22a73--- /dev/null+++ b/skills/showtime/lib/st/assets/xray/LICENSE@@ -0,0 +1,203 @@+Copyright 2022 Spotify AB++ Apache License+ Version 2.0, January 2004+ http://www.apache.org/licenses/++ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++ 1. Definitions.++ "License" shall mean the terms and conditions for use, reproduction,+ and distribution as defined by Sections 1 through 9 of this document.++ "Licensor" shall mean the copyright owner or entity authorized by+ the copyright owner that is granting the License.++ "Legal Entity" shall mean the union of the acting entity and all+ other entities that control, are controlled by, or are under common+ control with that entity. For the purposes of this definition,+ "control" means (i) the power, direct or indirect, to cause the+ direction or management of such entity, whether by contract or+ otherwise, or (ii) ownership of fifty percent (50%) or more of the+ outstanding shares, or (iii) beneficial ownership of such entity.++ "You" (or "Your") shall mean an individual or Legal Entity+ exercising permissions granted by this License.++ "Source" form shall mean the preferred form for making modifications,+ including but not limited to software source code, documentation+ source, and configuration files.++ "Object" form shall mean any form resulting from mechanical+ transformation or translation of a Source form, including but+ not limited to compiled object code, generated documentation,+ and conversions to other media types.++ "Work" shall mean the work of authorship, whether in Source or+ Object form, made available under the License, as indicated by a+ copyright notice that is included in or attached to the work+ (an example is provided in the Appendix below).++ "Derivative Works" shall mean any work, whether in Source or Object+ form, that is based on (or derived from) the Work and for which the+ editorial revisions, annotations, elaborations, or other modifications+ represent, as a whole, an original work of authorship. For the purposes+ of this License, Derivative Works shall not include works that remain+ separable from, or merely link (or bind by name) to the interfaces of,+ the Work and Derivative Works thereof.++ "Contribution" shall mean any work of authorship, including+ the original version of the Work and any modifications or additions+ to that Work or Derivative Works thereof, that is intentionally+ submitted to Licensor for inclusion in the Work by the copyright owner+ or by an individual or Legal Entity authorized to submit on behalf of+ the copyright owner. For the purposes of this definition, "submitted"+ means any form of electronic, verbal, or written communication sent+ to the Licensor or its representatives, including but not limited to+ communication on electronic mailing lists, source code control systems,+ and issue tracking systems that are managed by, or on behalf of, the+ Licensor for the purpose of discussing and improving the Work, but+ excluding communication that is conspicuously marked or otherwise+ designated in writing by the copyright owner as "Not a Contribution."++ "Contributor" shall mean Licensor and any individual or Legal Entity+ on behalf of whom a Contribution has been received by Licensor and+ subsequently incorporated within the Work.++ 2. Grant of Copyright License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ copyright license to reproduce, prepare Derivative Works of,+ publicly display, publicly perform, sublicense, and distribute the+ Work and such Derivative Works in Source or Object form.++ 3. Grant of Patent License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ (except as stated in this section) patent license to make, have made,+ use, offer to sell, sell, import, and otherwise transfer the Work,+ where such license applies only to those patent claims licensable+ by such Contributor that are necessarily infringed by their+ Contribution(s) alone or by combination of their Contribution(s)+ with the Work to which such Contribution(s) was submitted. If You+ institute patent litigation against any entity (including a+ cross-claim or counterclaim in a lawsuit) alleging that the Work+ or a Contribution incorporated within the Work constitutes direct+ or contributory patent infringement, then any patent licenses+ granted to You under this License for that Work shall terminate+ as of the date such litigation is filed.++ 4. Redistribution. You may reproduce and distribute copies of the+ Work or Derivative Works thereof in any medium, with or without+ modifications, and in Source or Object form, provided that You+ meet the following conditions:++ (a) You must give any other recipients of the Work or+ Derivative Works a copy of this License; and++ (b) You must cause any modified files to carry prominent notices+ stating that You changed the files; and++ (c) You must retain, in the Source form of any Derivative Works+ that You distribute, all copyright, patent, trademark, and+ attribution notices from the Source form of the Work,+ excluding those notices that do not pertain to any part of+ the Derivative Works; and++ (d) If the Work includes a "NOTICE" text file as part of its+ distribution, then any Derivative Works that You distribute must+ include a readable copy of the attribution notices contained+ within such NOTICE file, excluding those notices that do not+ pertain to any part of the Derivative Works, in at least one+ of the following places: within a NOTICE text file distributed+ as part of the Derivative Works; within the Source form or+ documentation, if provided along with the Derivative Works; or,+ within a display generated by the Derivative Works, if and+ wherever such third-party notices normally appear. The contents+ of the NOTICE file are for informational purposes only and+ do not modify the License. You may add Your own attribution+ notices within Derivative Works that You distribute, alongside+ or as an addendum to the NOTICE text from the Work, provided+ that such additional attribution notices cannot be construed+ as modifying the License.++ You may add Your own copyright statement to Your modifications and+ may provide additional or different license terms and conditions+ for use, reproduction, or distribution of Your modifications, or+ for any such Derivative Works as a whole, provided Your use,+ reproduction, and distribution of the Work otherwise complies with+ the conditions stated in this License.++ 5. Submission of Contributions. Unless You explicitly state otherwise,+ any Contribution intentionally submitted for inclusion in the Work+ by You to the Licensor shall be under the terms and conditions of+ this License, without any additional terms or conditions.+ Notwithstanding the above, nothing herein shall supersede or modify+ the terms of any separate license agreement you may have executed+ with Licensor regarding such Contributions.++ 6. Trademarks. This License does not grant permission to use the trade+ names, trademarks, service marks, or product names of the Licensor,+ except as required for reasonable and customary use in describing the+ origin of the Work and reproducing the content of the NOTICE file.++ 7. Disclaimer of Warranty. Unless required by applicable law or+ agreed to in writing, Licensor provides the Work (and each+ Contributor provides its Contributions) on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+ implied, including, without limitation, any warranties or conditions+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+ PARTICULAR PURPOSE. You are solely responsible for determining the+ appropriateness of using or redistributing the Work and assume any+ risks associated with Your exercise of permissions under this License.++ 8. Limitation of Liability. In no event and under no legal theory,+ whether in tort (including negligence), contract, or otherwise,+ unless required by applicable law (such as deliberate and grossly+ negligent acts) or agreed to in writing, shall any Contributor be+ liable to You for damages, including any direct, indirect, special,+ incidental, or consequential damages of any character arising as a+ result of this License or out of the use or inability to use the+ Work (including but not limited to damages for loss of goodwill,+ work stoppage, computer failure or malfunction, or any and all+ other commercial damages or losses), even if such Contributor+ has been advised of the possibility of such damages.++ 9. Accepting Warranty or Additional Liability. While redistributing+ the Work or Derivative Works thereof, You may choose to offer,+ and charge a fee for, acceptance of support, warranty, indemnity,+ or other liability obligations and/or rights consistent with this+ License. However, in accepting such obligations, You may act only+ on Your own behalf and on Your sole responsibility, not on behalf+ of any other Contributor, and only if You agree to indemnify,+ defend, and hold each Contributor harmless for any liability+ incurred by, or claims asserted against, such Contributor by reason+ of your accepting any such warranty or additional liability.++ END OF TERMS AND CONDITIONS++ APPENDIX: How to apply the Apache License to your work.++ To apply the Apache License to your work, attach the following+ boilerplate notice, with the fields enclosed by brackets "[]"+ replaced with your own identifying information. (Don't include+ the brackets!) The text should be enclosed in the appropriate+ comment syntax for the file format. We also recommend that a+ file or class name and description of purpose be included on the+ same "printed page" as the copyright notice for easier+ identification within third-party archives.++ Copyright [yyyy] [name of copyright owner]++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.diff --git a/skills/showtime/lib/st/assets/xray/NOTICE b/skills/showtime/lib/st/assets/xray/NOTICEnew file mode 100644index 0000000..7cb0b93--- /dev/null+++ b/skills/showtime/lib/st/assets/xray/NOTICE@@ -0,0 +1,49 @@+nmp.onnx and lib/st/xray_music.py use Basic Pitch by Spotify AB.++ Basic Pitch, https://github.com/spotify/basic-pitch (version 0.4.0)+ Copyright 2022 Spotify AB+ Licensed under the Apache License, Version 2.0 (the full text is in LICENSE next to this file).++nmp.onnx is the unmodified ONNX export of Basic Pitch's pretrained model+(basic_pitch/saved_models/icassp_2022/nmp.onnx).++What showtime changed: nothing in the model. showtime runs it with onnxruntime (no TensorFlow) and ported two+parts of the Python package to numpy/scipy in lib/st/xray_music.py: the windowing of the audio+(basic_pitch.inference.run_inference) and the conversion of the model output to note events+(basic_pitch.note_creation: output_to_notes_polyphonic and model_frames_to_time, with the package's defaults).+Pitch bends are not ported. The note events feed showtime's own tempo, key, chord, MIDI and MusicXML code.++The NOTICE of Basic Pitch 0.4.0 follows, as the license asks.++----------------------------------------------------------------------+Basic Pitch+Copyright 2022 Spotify AB++This product includes software developed at+Spotify AB (http://www.spotify.com/).++This product includes software from Librosa (ISC).+* Copyright (C) 2013--2017, librosa development team.++This product includes software from mir_eval (MIT)+* Copyright (C) 2014 Colin Raffel++This product includes software from numpy (BSD)+* Copyright (C) 2005-2022, NumPy Developers.++This product includes software from pretty-midi (MIT)+* Copyright (C) 2014 Colin Raffel++This product includes software from resampy (ISC)+* Copyright (C) 2016, Brian McFee++This product includes software from scipy (BSD)+* Copyright (C) 2001-2002 Enthought, Inc. 2003-2022, SciPy Developers++This product includes software from tensorflow (Apache 2.0)+* Copyright (C) 2019 Google, LLC <[email protected]>++The tests for `basic-pitch` include audio files from the+Vocadito dataset liscened under Creative Commons+Attribution 4.0 International. The dataset can be found at:+https://zenodo.org/record/5578807#.YnRm5vPMKDUdiff --git a/skills/showtime/lib/st/assets/xray/nmp.onnx b/skills/showtime/lib/st/assets/xray/nmp.onnxnew file mode 100644index 0000000..c30e5f9Binary files /dev/null and b/skills/showtime/lib/st/assets/xray/nmp.onnx differdiff --git a/skills/showtime/lib/st/cli_core.py b/skills/showtime/lib/st/cli_core.pyindex 3ebbec6..cbbc55e 100644--- a/skills/showtime/lib/st/cli_core.py+++ b/skills/showtime/lib/st/cli_core.py@@ -157,6 +157,50 @@ def _diagram_examples(src: Path) -> List[str]:return sorted(p.stem for p in d.glob("*.js")) if d.is_dir() else []+XRAY_LAYOUTS = ("through", "split", "diagram")+++def xray_mechanisms() -> List[str]:+ """The mechanism library: skills/showtime/library/xray/<object>/mechanism.js (one drawing per kind of object)."""+ d = paths()["skill"] / "library" / "xray"+ return sorted(p.parent.name for p in d.glob("*/mechanism.js")) if d.is_dir() else []+++def _apply_xray(dst: Path, cfg: Dict[str, Any], mechanism: Optional[str], layout: Optional[str]) -> List[str]:+ """The xray template's options: copy a library mechanism (and its living-diagram scene) into the project and pick+ the layout (through: drawn on the tracked object; split: beside it; diagram: an isometric living diagram beside+ it). Returns notes to print."""+ notes: List[str] = []+ xr = cfg.setdefault("xray", {})+ if mechanism:+ lib = paths()["skill"] / "library" / "xray" / mechanism+ for name in ("mechanism.js", "diagram.js"):+ if (lib / name).is_file():+ shutil.copyfile(str(lib / name), str(dst / name))+ if mechanism != "typewriter":+ for k in ("text", "place", "skip", "kinds"): # the sample clip's own evidence, not this object's+ xr.pop(k, None)+ notes.append("mechanism %s: media/clip.webm and xray.json are still the typewriter sample: put your clip in "+ "media/ and `showtime xray analyze` its xray.json next to index.html" % mechanism)+ if layout:+ xr["layout"] = layout+ if xr.get("layout") == "diagram":+ scene = paths()["skill"] / "library" / "xray" / (mechanism or "typewriter") / "diagram.js"+ if scene.is_file():+ shutil.copyfile(str(scene), str(dst / "diagram.js"))+ if (dst / "diagram.html").is_file():+ shutil.copyfile(str(dst / "diagram.html"), str(dst / "index.html"))+ else:+ xr["layout"] = "split"+ notes.append("mechanism %s has no living-diagram scene (diagram.js) yet: the layout is split instead "+ "(write a diagram.js to use --layout diagram)" % (mechanism or "typewriter"))+ if (dst / "diagram.html").is_file():+ (dst / "diagram.html").unlink()+ if xr.get("layout") != "diagram" and (dst / "diagram.js").is_file():+ (dst / "diagram.js").unlink()+ return notes++def _apply_diagram_example(src: Path, dst: Path, cfg: Dict[str, Any], name: str) -> None:"""Make one of the diagram template's examples the project's scene, at the example's own length (CUE.duration),with the poster near its end: a later --duration or retime then scales from the right base."""@@ -248,13 +292,19 @@ def register_new(sub: argparse._SubParsersAction) -> None:"away from this machine's recent looks), template (the template's own look) or an id from ""`showtime signature`; a brand kit or a job's style reference wins over auto")p.add_argument("--theme", choices=sorted(DIAGRAM_THEMES),- help="diagram template: light (paper, the default), dark (night schematic) or white (white, colour only "+ help="xray template: dark (the radiograph, default) or light (a blueprint on paper); diagram template: light (paper, the default), dark (night schematic) or white (white, colour only ""where it means something; the live figure's look); sets showtime.json ""\"theme\" and the page's data-theme, and \"background\" unless --background is given")p.add_argument("--example", metavar="NAME",- help="diagram template: start from one of its examples (enter, market, econ-lab) instead of the heat pump: "+ help="diagram template: start from one of its examples (enter, market, econ-lab, computer, typewriter) instead of the heat pump: ""it becomes scene.js and the video takes the example's length (CUE.duration), so --duration ""and a later retime scale from it")+ p.add_argument("--mechanism", metavar="OBJECT",+ help="xray template: the inside to draw, from the mechanism library (skills/showtime/library/xray: "+ "typewriter, musicbox, ...); default typewriter, the sample clip's")+ p.add_argument("--layout", choices=["through", "split", "diagram"],+ help="xray template: through (the inside drawn on the tracked object, the default when xray.json has a "+ "track), split (beside the clip) or diagram (an isometric living diagram beside the clip)")p.add_argument("--look-seed", metavar="SEED",help="seed for the automatic pick (default: the folder name); the same seed and history give the same look")p.add_argument("--json", action="store_true", help="print the created project as JSON")@@ -364,7 +414,10 @@ def cmd_new(args: argparse.Namespace) -> int:raise ShowtimeError("missing destination folder", hint="showtime new %s my-video" % args.template)tpl_cfg = read_json(Path(names[args.template]["path"]) / "showtime.json", {}) or {}is_diagram = "theme" in tpl_cfg and bool(_DATA_THEME_RE.search(_read_text(Path(names[args.template]["path"]) / "index.html")))- if getattr(args, "theme", None) and not is_diagram:+ if getattr(args, "theme", None) and args.template == "xray":+ if args.theme not in ("light", "dark"):+ raise ShowtimeError("the xray template has a dark (radiograph) and a light (blueprint) theme", hint="--theme dark or --theme light")+ elif getattr(args, "theme", None) and not is_diagram:raise ShowtimeError("--theme is for the diagram template", why="'%s' keeps its own look" % args.template,hint="showtime new diagram %s --theme %s (page templates: --look)" % (args.dir, args.theme))if getattr(args, "example", None):@@ -374,6 +427,11 @@ def cmd_new(args: argparse.Namespace) -> int:hint="showtime new diagram %s --example enter" % args.dir)if args.example not in avail:raise ShowtimeError("unknown example %r" % args.example, hint="examples of %s: %s" % (args.template, ", ".join(avail)))+ if (getattr(args, "mechanism", None) or getattr(args, "layout", None)) and args.template != "xray":+ raise ShowtimeError("--mechanism and --layout are for the xray template", hint="showtime new xray %s --mechanism typewriter" % (args.dir or "my-xray"))+ if getattr(args, "mechanism", None) and args.mechanism not in xray_mechanisms():+ raise ShowtimeError("unknown mechanism %r" % args.mechanism, why="the library has: %s" % (", ".join(xray_mechanisms()) or "(none)"),+ hint="draw your own: copy library/xray/typewriter/mechanism.js and change draw(); see `showtime guide xray`")if args.duration is not None and not (0 < args.duration <= 3600):raise ShowtimeError("--duration must be between 0 and 3600 seconds (got %g)" % args.duration)job_dir: Optional[Path] = None@@ -424,8 +482,20 @@ def cmd_new(args: argparse.Namespace) -> int:raise ShowtimeError("%s must be even for H.264 (got %s)" % (dim, cfg[dim]))if getattr(args, "example", None) and board is None:_apply_diagram_example(src, dst, cfg, args.example)- if getattr(args, "theme", None):+ xray_notes: List[str] = []+ if args.template == "xray" and board is None:+ xray_notes = _apply_xray(dst, cfg, getattr(args, "mechanism", None), getattr(args, "layout", None))+ if getattr(args, "theme", None) and args.template == "xray":+ cfg.setdefault("xray", {})["theme"] = args.theme+ if getattr(args, "background", None) is None:+ cfg["background"] = "#f4f0e7" if args.theme == "light" else "#070b16"+ elif getattr(args, "theme", None):_set_diagram_theme(dst, cfg, args.theme, background=getattr(args, "background", None) is None)+ if args.template == "xray" and board is None:+ # what the live page's "line that made it" is built from: three plain words, no paths, job names or regions+ xr = cfg.setdefault("xray", {})+ xr["made"] = {"mechanism": getattr(args, "mechanism", None) or "typewriter",+ "layout": xr.get("layout") or "through", "theme": xr.get("theme") or "dark"}tone_note = _tone_for_new(args, cfg, job_dir, dst)write_json(cfg_path, cfg)sb_files: List[str] = []@@ -444,6 +514,7 @@ def cmd_new(args: argparse.Namespace) -> int:cfg["duration"] = args.durationwrite_json(cfg_path, cfg)notes = _aspect_notes(dst, cfg)+ notes += xray_notesfor n in notes:warn(n)job_rec = _attach_to_job(dst, job_dir)diff --git a/skills/showtime/lib/st/cli_xray.py b/skills/showtime/lib/st/cli_xray.pynew file mode 100644index 0000000..15af613--- /dev/null+++ b/skills/showtime/lib/st/cli_xray.py@@ -0,0 +1,236 @@+"""`showtime xray`: measure a filmed clip (sound onsets, camera tracking) and check a finished film against it."""+from __future__ import annotations++import argparse+import os+import re+import sys+from pathlib import Path+from typing import Optional++from .common import ShowtimeError, log, print_json++COMMANDS = {+ "xray": "Measure what happens in a filmed clip (each hit's exact frame, the object's motion) and verify the finished film",+}++_F = argparse.RawDescriptionHelpFormatter+++def register(sub: argparse._SubParsersAction) -> None:+ x = sub.add_parser("xray", help=COMMANDS["xray"], formatter_class=_F, description=(+ "X-ray a real thing you filmed (a typewriter, a music box, a guitar): measure every hit from the clip's\n"+ "own sound and picture so the drawing inside it lands on the exact frame, then check the finished film.\n\n"+ " analyze clip -> <job>/xray/xray.json (events with their frame) + check.jpg (look at it before building)\n"+ " notes clip -> <job>/xray/notes.json (the notes played, tempo, key, chords, voices) + notes.mid, notes.musicxml,\n"+ " check-notes.jpg (a piano roll over the waveform)\n"+ " verify job -> reads the film back: the sync marker on every frame, the audio against the clip,\n"+ " optionally the smoothness of a scrolling band; writes the xray section of the job receipt"),+ epilog=("Examples:\n"+ " showtime xray analyze typewriter.mp4 --job typewriter\n"+ " showtime xray analyze clip.mp4 --from 2 --to 11 --min-gap 0.15 --sensitivity 0.6\n"+ " showtime xray analyze clip.mp4 --track --region '420,300 1500,310 1480,880 440,870' --ref-time 1.0\n"+ " showtime xray notes guitar.mp4 --job guitar --instrument guitar\n"+ " showtime xray verify typewriter --video final.mp4\n"))+ s = x.add_subparsers(dest="xray_cmd", metavar="<subcommand>")++ a = s.add_parser("analyze", help="clip -> xray.json (events: time, frame, strength, band) + check.jpg", formatter_class=_F,+ description=(+ "Detect the hits in a clip's sound: a high-passed RMS envelope on a 1 ms grid, bursts above an\n"+ "adaptive floor, each onset = the first millisecond within 6 dB of the burst's peak. Hits closer\n"+ "than --min-gap are merged (the first stays; the rest are listed under `dropped` with the reason).\n"+ "event frame = floor(t * fps) of the clip. Look at check.jpg before you build on it.\n\n"+ "--track also follows the object through the clip (KLT points inside --region, minus the --mask\n"+ "polygons of moving parts, frame-to-frame homographies composed outwards from the frame at\n"+ "--ref-time) and writes one 3x3 per frame under xray.json \"track\" (the whole clip, not only\n"+ "--from/--to). Points are x,y in the clip's pixels, read off the frame at --ref-time."),+ epilog="Writes <job>/xray/xray.json and check.jpg (a new job when --job is not given).")+ a.add_argument("clip", help="the video you filmed (with its sound)")+ a.add_argument("--job", help="job folder or name (default: a new showtime-out/xray-<timestamp>/)")+ a.add_argument("--from", dest="t_from", type=float, default=0.0, metavar="S", help="start of the part to analyze, seconds (default 0)")+ a.add_argument("--to", dest="t_to", type=float, default=None, metavar="S", help="end of the part to analyze, seconds")+ a.add_argument("--min-gap", type=float, default=None, metavar="S",+ help="closest two hits may be (default 0.12 s; 0.06 s with the flux engine)")+ a.add_argument("--engine", choices=["auto", "envelope", "flux"], default="auto",+ help="envelope: separate hits (clicks, strikes); flux: spectral flux for dense or sustained sound (music, "+ "plucks over a bed); auto (default): envelope, flux when it finds under 0.3 events per second of "+ "sustained sound. xray.json detector.engine and .note say which ran and why")+ a.add_argument("--sensitivity", type=float, default=0.5, metavar="0-1",+ help="0..1, higher finds quieter hits (default 0.5)")+ a.add_argument("--track", action="store_true", help="also track the object (needs --region and --ref-time)")+ a.add_argument("--region", metavar='"x,y x,y ..."', help="the object's outline on the reference frame (3+ points)")+ a.add_argument("--ref-time", type=float, default=None, metavar="T", help="the time of the reference frame the region is drawn on")+ a.add_argument("--mask", action="append", default=[], metavar='"x,y ..."',+ help="a polygon of a moving part to leave out of the tracking (repeatable)")+ a.add_argument("-o", "--out", metavar="DIR", help="write the files here (default <job>/xray)")+ a.add_argument("--json", action="store_true", help="print xray.json")+ a.set_defaults(func=cmd_analyze)++ n = s.add_parser("notes", help="clip -> notes.json (pitch, onset, tempo map, key, chords) + MIDI, MusicXML, check-notes.jpg",+ formatter_class=_F, description=(+ "Read the notes a filmed instrument plays (guitar, piano, music box) from the clip's sound with\n"+ "Basic Pitch (ONNX, no TensorFlow). Every note keeps frame = floor(t * fps) of the clip. Adds a smooth\n"+ "tempo map (scroll with it, never snap to the last note), eighth-note quantization, the key, a chord per\n"+ "beat, the melody (loudest note from E4 up) and bass voices, and with --instrument guitar the string,\n"+ "fret and finger of every note (standard tuning, open position first).\n"+ "Look at check-notes.jpg before you build on it."),+ epilog="Writes <job>/xray/notes.json, notes.mid, notes.musicxml, check-notes.jpg.")+ n.add_argument("clip", help="the video you filmed (with its sound)")+ n.add_argument("--job", help="job folder or name (default: a new showtime-out/xray-<timestamp>/)")+ n.add_argument("--from", dest="t_from", type=float, default=0.0, metavar="S", help="start of the part to read, seconds (default 0)")+ n.add_argument("--to", dest="t_to", type=float, default=None, metavar="S", help="end of the part to read, seconds")+ n.add_argument("--instrument", choices=["auto", "guitar", "piano", "musicbox", "drums"], default="auto",+ help="auto tells a music box (high register) from a piano; guitar adds strings and frets and must be asked for; drums sorts the hits by sound (kick, snare, hats, tom, crash)")+ n.add_argument("-o", "--out", metavar="DIR", help="write the files here (default <job>/xray)")+ n.add_argument("--json", action="store_true", help="print notes.json")+ n.set_defaults(func=cmd_notes)++ v = s.add_parser("verify", help="read a finished film back against xray.json (marker, audio, smoothness)", formatter_class=_F,+ description=(+ "1. Marker: the 6x6 px square at (width-10, height-10) of the film is read on every frame; it is\n"+ " white on the film frames of the events (event frame + offset) and black otherwise.\n"+ "2. Audio: the film's sound against the clip's (first 12 s), cross-correlated: the lag in ms.\n"+ "3. With --band x,y,w,h: the frame-to-frame horizontal shift of that band; stalls (0 px while the\n"+ " median speed is over 1 px) and jumps (the shift changes by over 3 px).\n\n"+ "Writes the xray section of the job receipt (receipt.json) and <job>/xray/verify.json, prints the\n"+ "end-card line. Exit 1 when a FAIL condition holds: a marker is present and an event is missing or\n"+ "extra, or the audio is more than 20 ms from the clip. No marker at all is a WARN (unverified)."),+ epilog="Example:\n showtime xray verify my-job --video my-job/final.mp4 --band 0,820,1920,200")+ v.add_argument("job", nargs="?", help="job folder or name (default: the current or newest job)")+ v.add_argument("--video", metavar="MP4", help="the film (default: the job's latest final or preview)")+ v.add_argument("--source", metavar="CLIP", help="the clip it was measured from (default: remembered by analyze)")+ v.add_argument("--offset-frames", type=int, default=None, metavar="N",+ help="the film frame of the clip's frame 0 (default: the project's showtime.json \"xray\".offset_frames, else 0)")+ v.add_argument("--skip", metavar="i,i", help="event indexes (i in xray.json) that are not the object's and carry no marker "+ "(default: the project's showtime.json \"xray\".skip)")+ v.add_argument("--band", metavar="x,y,w,h", help="a scrolling band of the film to check for stalls and jumps "+ "(default: the project's showtime.json \"xray\".band rows, full width)")+ v.add_argument("--json", action="store_true", help="print the result as JSON")+ v.set_defaults(func=cmd_verify)+ x.set_defaults(func=lambda args: (x.print_help() or 2))+++def _job_for(arg: Optional[str]) -> Path:+ from .job import ledger+ if arg:+ p = Path(arg).expanduser()+ if os.name != "nt" and re.match(r"^[A-Za-z]:[\\/]", arg):+ raise ShowtimeError("--job %s looks like a Windows path, which does not exist on this system" % arg,+ hint="pass a job folder or id on this machine")+ bare = not any(sep in arg for sep in ("/", "\\")) and not arg.startswith((".", "~"))+ if p.exists() or bare:+ try:+ return ledger.resolve(arg)+ except ShowtimeError:+ if p.exists():+ raise+ p.mkdir(parents=True, exist_ok=True)+ data = ledger.new_data("xray", "quick", "x-ray a clip", p.resolve(), None)+ data["history"][-1]["event"] = "job created by showtime xray"+ ledger.save(p, data)+ return p.resolve()+ here = ledger.enclosing_job(Path.cwd())+ d, _ = ledger.init("xray", goal="x-ray a clip", base=here.parent if here is not None else None)+ return d+++def cmd_analyze(args: argparse.Namespace) -> int:+ from . import xray+ track = None+ if args.track:+ if not args.region or args.ref_time is None:+ raise ShowtimeError("--track needs --region and --ref-time",+ hint='--region "x,y x,y x,y x,y" (the outline on the frame at --ref-time), e.g. --ref-time 1.0')+ track = {"region": xray.parse_points(args.region), "ref_time": args.ref_time,+ "masks": [xray.parse_points(m) for m in args.mask]}+ elif args.region or args.mask or args.ref_time is not None:+ raise ShowtimeError("--region, --mask and --ref-time only work with --track", hint="add --track")+ if not 0.0 <= args.sensitivity <= 1.0:+ raise ShowtimeError("--sensitivity is 0..1 (got %g)" % args.sensitivity)+ if args.min_gap is not None and args.min_gap <= 0:+ raise ShowtimeError("--min-gap must be above 0 seconds")+ xray.validate_clip(args.clip)+ job = _job_for(args.job)+ out = Path(args.out) if args.out else job / "xray"+ doc = xray.analyze(args.clip, out, t_from=args.t_from, t_to=args.t_to, min_gap=args.min_gap, engine=args.engine,+ sensitivity=args.sensitivity, track=track)+ try:+ from .job import ledger+ ledger.note(job, stage="xray", outputs=[], event="xray analyze: %d events from %s" % (len(doc["events"]), doc["source"]["file"]))+ except Exception: # noqa: BLE001 - the ledger is a convenience+ pass+ if args.json:+ print_json(doc)+ return 0+ ev = doc["events"]+ log("xray: %d events%s from %s (%d dropped)" % (len(ev), " (%.3f s to %.3f s)" % (ev[0]["t"], ev[-1]["t"]) if ev else "",+ doc["source"]["file"], len(doc["dropped"])))+ if doc["track"]:+ log("xray: tracked %d frames from reference frame %d" % (len(doc["track"]["H"]), doc["track"]["ref_frame"]))+ print(out / "xray.json")+ print(out / "check.jpg")+ log("look at check.jpg before building: every tick should sit on a hit you can hear")+ return 0+++def cmd_notes(args: argparse.Namespace) -> int:+ from . import xray, xray_music+ xray.validate_clip(args.clip)+ job = _job_for(args.job)+ out = Path(args.out) if args.out else job / "xray"+ doc = xray_music.analyze(args.clip, out, t_from=args.t_from, t_to=args.t_to, instrument=args.instrument)+ try:+ from .job import ledger+ ledger.note(job, stage="xray", outputs=[], event="xray notes: %d notes from %s" % (len(doc["notes"]), doc["source"]["file"]))+ except Exception: # noqa: BLE001 - the ledger is a convenience+ pass+ if args.json:+ print_json(doc)+ return 0+ if doc["instrument"] == "drums":+ from collections import Counter+ c = Counter(d["piece"] for d in doc["drums"])+ log("xray: %d drum hits (%s), %.0f bpm; a classifier of sound types, not a transcription" % (+ len(doc["drums"]), ", ".join("%d %s" % (v, k) for k, v in sorted(c.items())), doc["tempo"]["bpm"]))+ else:+ log("xray: %d notes (%s), %d melody, %d bass, %.0f bpm, key %s, %d chords" % (+ len(doc["notes"]), doc["instrument"], len(doc["voices"]["melody"]), len(doc["voices"]["bass"]),+ doc["tempo"]["bpm"], doc["key"] or "-", len(doc["chords"])))+ for name in ("notes.json", "notes.mid", "notes.musicxml", "check-notes.jpg"):+ print(out / name)+ log("look at check-notes.jpg before building: every bar should sit on a note you can hear")+ return 0+++def cmd_verify(args: argparse.Namespace) -> int:+ from . import xray+ from .job import ledger+ job = ledger.resolve(args.job)+ band = None+ if args.band:+ try:+ band = [int(float(v)) for v in args.band.split(",")]+ except ValueError:+ band = []+ if len(band) != 4:+ raise ShowtimeError("--band is x,y,w,h in film pixels (got %r)" % args.band)+ skip = None+ if args.skip is not None:+ try:+ skip = [int(v) for v in args.skip.replace(" ", "").split(",") if v != ""]+ except ValueError:+ raise ShowtimeError("--skip is event indexes like 0,3 (got %r)" % args.skip)+ res = xray.verify(job, args.video, args.source, args.offset_frames, band, skip)+ xray.add_to_receipt(job)+ if args.json:+ print_json(res)+ else:+ print(res["line"])+ for n in res["notes"]:+ log("xray: " + n)+ for f in res["findings"]:+ log("%s %s: %s" % (f["severity"], f["rule"], f["message"]))+ sm = res.get("smoothness")+ if sm:+ log("smoothness: %d frames, median %.1f px/frame, %d stalls, %d jumps" % (sm["frames"], sm["median_px"], sm["stalls"], sm["jumps"]))+ return 1 if res["status"] == "FAIL" else 0diff --git a/skills/showtime/lib/st/ff.py b/skills/showtime/lib/st/ff.pyindex 6843eba..c653930 100644--- a/skills/showtime/lib/st/ff.py+++ b/skills/showtime/lib/st/ff.py@@ -405,6 +405,8 @@ def probe(path: PathLike) -> Dict[str, Any]:"display_width": h if rot in (90, 270) else w,"display_height": w if rot in (90, 270) else h,"fps": round(fps, 6) if fps else None,+ "r_fps": _frac(v.get("r_frame_rate")),+ "avg_fps": _frac(v.get("avg_frame_rate")),"nb_frames": _int(v.get("nb_frames")),"video_duration": _float(v.get("duration")),"vcodec": v.get("codec_name"),diff --git a/skills/showtime/lib/st/job/ledger.py b/skills/showtime/lib/st/job/ledger.pyindex 58f7f5e..ffe664b 100644--- a/skills/showtime/lib/st/job/ledger.py+++ b/skills/showtime/lib/st/job/ledger.py@@ -51,6 +51,13 @@ def _parse_iso(s: Optional[str]) -> Optional[float]:def _git_sha(start: Path) -> Optional[str]:"""HEAD commit of the checkout containing `start`, read from .git without running git."""+ try:+ return _git_sha_unchecked(start)+ except (OSError, subprocess.SubprocessError, ValueError): # unreadable .git (a sandbox), odd ref files+ return None+++def _git_sha_unchecked(start: Path) -> Optional[str]:for d in [start] + list(start.parents):g = d / ".git"if g.is_file(): # worktree: "gitdir: <path>"diff --git a/skills/showtime/lib/st/job/receipt.py b/skills/showtime/lib/st/job/receipt.pyindex 4547111..5ccb965 100644--- a/skills/showtime/lib/st/job/receipt.py+++ b/skills/showtime/lib/st/job/receipt.py@@ -359,6 +359,13 @@ def build(job: Path, transcript: Optional[str] = None, host: Optional[str] = Non"note": "includes any time spent waiting for the user"},"usage": us,}+ try:+ from .. import xray+ xs = xray.receipt_section(job)+ except Exception: # noqa: BLE001 - the receipt never fails over it+ xs = None+ if xs:+ rec["xray"] = xsreturn rec@@ -604,6 +611,11 @@ def card_facts(rec: Dict[str, Any]) -> List[str]:out.append("QA: %s" % ("%s (%s fail, %s warn) on %s%s" % (dv["qa"], dv.get("qa_fail"), dv.get("qa_warn"), dv["final"],heard)if dv.get("qa") else "not run on %s" % (dv.get("final") or "the final")))+ xr = rec.get("xray")+ if xr and xr.get("line"):+ out.append("X-ray: %s%s" % (xr["line"], "" if xr.get("status") == "PASS" else " (%s)" % xr.get("status", "").lower()))+ if xr.get("credit"):+ out.append("Clip credit: %s" % xr["credit"])mode = (rec.get("review_mode") or {}).get("mode")note = rv.get("note") or ""if not rv.get("packed"):diff --git a/skills/showtime/lib/st/launcher.py b/skills/showtime/lib/st/launcher.pyindex c0e0a10..7b722b6 100644--- a/skills/showtime/lib/st/launcher.py+++ b/skills/showtime/lib/st/launcher.py@@ -61,7 +61,7 @@ STDLIB_CLI = ("paths", "new", "retime", "data", "install", "guide", "config")GROUPS = [("make", "Make a video from an HTML/canvas project",["new", "retime", "data", "adopt", "preview", "render", "export", "check", "look", "snap", "score", "motion", "code",- "server", "manim", "release-video", "pr-video", "recap", "repro"],+ "server", "manim", "release-video", "pr-video", "recap", "repro", "xray"],["showtime new dom my-launch --aspect 16:9","showtime retime my-launch -d 20 # or --from-voice voice/timeline.json","showtime preview my-launch",diff --git a/skills/showtime/lib/st/qa/floor.py b/skills/showtime/lib/st/qa/floor.pyindex c9e7dfb..a3ab8e8 100644--- a/skills/showtime/lib/st/qa/floor.py+++ b/skills/showtime/lib/st/qa/floor.py@@ -46,6 +46,9 @@ PLAYER_BAR_MAX = 0.012 # the bar is at most this share of the height thick (aPLAYER_END_W = 0.08 # glyph windows reach this share of the width either side of each bar endPLAYER_NEAR = 0.06 # and this share of the height above or below the barPLAYER_GLYPHS = (0.01, 0.30) # edge density that reads as small glyphs (not empty, not a texture)+XRAY_STRIP_PORTRAIT = 0.30 # an x-ray film's sound strip: the bottom share of a portrait frame ...+XRAY_STRIP_LANDSCAPE = 0.14 # ... and of a landscape one+XRAY_STRIP_NOTE = "player_chrome skipped: the x-ray sound strip"PLAYER_FRAMES = 2 # sampled frames with the strip in the same place (row within 3 px)PLAYER_DARK = 110 # median grey (0-255) just above the bar: a player's controls sit on a dark overlay@@ -328,9 +331,12 @@ def _span(times: Sequence[float]) -> str:def check(F: Any, video: Any, dur: float, W: int, H: int, *, crop: Optional[Sequence[int]] = None,- footage: bool = True, frames: Optional[Sequence[Tuple[float, Any]]] = None) -> Dict[str, Any]:+ footage: bool = True, frames: Optional[Sequence[Tuple[float, Any]]] = None,+ xray_strip: bool = False) -> Dict[str, Any]:"""Run the four detectors and add WARNs to F (st.qa.video.Findings). `frames` [(t, grey array)] skips- decoding (tests). Returns what was measured, for qa.json."""+ decoding (tests). `xray_strip`: the film is an x-ray film, whose sound strip (a full-width row of event ticks) lies in+ the bottom 30 % of a portrait frame (12 % of a landscape one): a bar found there is not player chrome.+ Returns what was measured, for qa.json."""if frames is None:from concurrent.futures import ThreadPoolExecutortimes = sample_times(dur)@@ -348,6 +354,11 @@ def check(F: Any, video: Any, dur: float, W: int, H: int, *, crop: Optional[Sequs = player_strip(g)if not s:continue+ if xray_strip and s[0] - g.shape[0] * PLAYER_NEAR >= g.shape[0] * (1.0 - (XRAY_STRIP_PORTRAIT if g.shape[0] > g.shape[1] else XRAY_STRIP_LANDSCAPE)):+ out.setdefault("notes", [])+ if XRAY_STRIP_NOTE not in out["notes"]:+ out["notes"].append(XRAY_STRIP_NOTE)+ continuetol = 0.02 * g.shape[1]for key, ts in groups:if abs(key[0] - s[0]) <= 3 and abs(key[1] - s[1]) <= tol and abs(key[2] - s[2]) <= tol:diff --git a/skills/showtime/lib/st/qa/video.py b/skills/showtime/lib/st/qa/video.pyindex e079a6e..9b3fdc6 100644--- a/skills/showtime/lib/st/qa/video.py+++ b/skills/showtime/lib/st/qa/video.py@@ -132,6 +132,11 @@ RULES = {"reference_copy": "the render copies its style reference (near-copy guard: sampled frames + cut rhythm)","reference_close": "some frames look like frames of the style reference","reference_credit": "the \"Style reference:\" credit is missing from credits.txt or share.txt",+ "xray_sync": "an x-ray film's sync marker is missing or extra on an event frame, or its audio is over 20 ms from the clip (showtime xray verify)",+ "xray_sync_off": "an x-ray film's marker lands 1-2 frames off an event's frame",+ "xray_unverified": "the job has xray/xray.json but the film carries no sync marker, so its events are unverified",+ "xray_credit": "an x-ray film's clip has no credit line (showtime.json xray.credit) and is not marked xray.own",+ "xray_choppy": "a scrolling band of an x-ray film stalls or jumps (showtime xray verify --band)",}RULES.update(floor.RULES) # the quality floor (st.qa.floor): looks-cheap patterns, WARN onlyRULES.update(hearing.RULES) # the hearing pass (st.qa.hearing): what an ear catches, WARN only@@ -403,7 +408,7 @@ def run(video: PathLike, *, project: Optional[PathLike] = None, expect_file: Opt_check_upscale(F, vpath)_check_cards(F, vpath)- rep["floor"] = _check_floor(F, vpath, dur, W, H, pic, _may_hold_footage(proj, cfg))+ rep["floor"] = _check_floor(F, vpath, dur, W, H, pic, _may_hold_footage(proj, cfg), _xray_strip(proj, vpath, cfg))_check_rhythm(F, vpath, dur, fps, proj, cfg, expect, rep, say)if loud.get("_x") is not None:_check_hearing(F, vpath, loud, dur, proj, rep, say)@@ -419,6 +424,7 @@ def run(video: PathLike, *, project: Optional[PathLike] = None, expect_file: Opt_check_must_show(F, proj, expect)rep["phone"] = _check_phone(F, vpath, proj, W, H, caps, cap_items)_check_reference(F, vpath, say)+ _check_xray(F, vpath, rep, say)seams = _check_seams(F, vpath, fps)if seams:rep["seams"] = seams@@ -1162,11 +1168,20 @@ def _check_cards(F: Findings, vpath: Path) -> None:LIST_MAX_CARDS = 12+def _xray_strip(proj: Optional[Path], vpath: Path, cfg: Dict[str, Any]) -> bool:+ """The film comes from the xray template (showtime.json "template": "xray"), or the job has xray/xray.json and the+ project an "xray" block: its sound strip is not a player's control bar."""+ if str(cfg.get("template") or "").lower() == "xray":+ return True+ job = job_of(vpath)+ return bool(job is not None and (job / "xray" / "xray.json").is_file() and isinstance(cfg.get("xray"), dict))++def _check_floor(F: Findings, vpath: Path, dur: float, W: int, H: int, crop: Optional[Sequence[int]],- footage: bool) -> Dict[str, Any]:+ footage: bool, xray_strip: bool = False) -> Dict[str, Any]:"""The quality floor (st.qa.floor): player chrome, soft footage, stepped caption boxes, empty borders."""try:- return floor.check(F, vpath, dur, W, H, crop=crop, footage=footage)+ return floor.check(F, vpath, dur, W, H, crop=crop, footage=footage, xray_strip=xray_strip)except Exception as e: # noqa: BLE001 - a warning-only look never breaks qafrom ..common import debugdebug("quality floor skipped: %s" % e)@@ -1335,6 +1350,35 @@ def _check_reference(F: Findings, vpath: Path, say: Any) -> None:debug("reference guard failed: %s" % e)+def _check_xray(F: Findings, vpath: Path, rep: Dict[str, Any], say: Any) -> None:+ """A job with xray/xray.json: the film's sync marker and audio against it (st.xray.verify)."""+ job = job_of(vpath)+ if job is None or not (job / "xray" / "xray.json").is_file():+ return+ from .. import xray as _x+ cfg = _x.project_config(job)+ credit = str(cfg.get("credit") or "").strip()+ if credit:+ F.ok("clip credit: %s" % _snip(credit, 60))+ elif cfg.get("own") is True:+ F.ok("clip credit: the clip is the user's own, none needed")+ else:+ F.add("xray_credit", "WARN", "x-ray clip has no credit line (showtime.json xray.credit, or xray.own: true for your own clip)",+ fix='set "xray": {"credit": "<title> by <author>, <licence>, <link>"} in the project\'s showtime.json (or "own": true)')+ say("qa: x-ray sync")+ try:+ from .. import xray+ res = xray.verify(job, vpath)+ except Exception as e: # noqa: BLE001 - an unreadable x-ray file is a finding, not a crash+ F.add("xray_unverified", "WARN", "the x-ray sync could not be checked: %s" % e)+ return+ rep["xray"] = {k: res.get(k) for k in ("events", "on_frame", "missing", "extra", "audio_lag_ms", "marker", "line")}+ for f in res["findings"]:+ F.add(f["rule"], f["severity"], f["message"], fix="showtime xray verify %s" % job.name)+ if not res["findings"]:+ F.ok(res["line"])++def _video_keyframes(vpath: Path) -> Optional[List[bool]]:"""Per video frame (decode order = display order: renders use no B-frames): is it a keyframe?"""from .. import ff@@ -1498,7 +1542,9 @@ def _check_credits(F: Findings, vpath: Path, proj: Optional[Path], expect: Dict[if have:F.ok("credits file present (%s)" % have.name)else:- F.ok("no attribution required (no CC-BY items in the mix report or asset sidecars)")+ job = job_of(vpath)+ if job is None or not (job / "xray" / "xray.json").is_file(): # an x-ray film's clip is judged in _check_xray+ F.ok("no attribution required (no CC-BY items in the mix report or asset sidecars)")returnif not have:F.add("missing_credits", "FAIL", "%d item(s) need attribution but no credits.txt ships with the video: %s" % (diff --git a/skills/showtime/lib/st/sandbox.py b/skills/showtime/lib/st/sandbox.pyindex 7aa3fc2..4dc4865 100644--- a/skills/showtime/lib/st/sandbox.py+++ b/skills/showtime/lib/st/sandbox.py@@ -19,6 +19,7 @@ import osimport reimport socketimport ssl+import threadingimport urllib.errorimport urllib.requestfrom pathlib import Path@@ -151,6 +152,18 @@ def probe_network(url: Optional[str] = None, timeout: float = PROBE_TIMEOUT) ->reachable: only DNS failures, refused/blocked connections and timeouts mean "no network".Honours HTTPS_PROXY like the downloads do. SHOWTIME_NET_PROBE sets the URL (tests)."""url = url or os.environ.get("SHOWTIME_NET_PROBE") or PROBE_URL+ box: List[Tuple[bool, str]] = []+ # The socket timeout does not cover the DNS lookup, which can block for minutes offline or behind a+ # sandbox: the probe runs in a daemon thread and is given up on after `timeout` seconds (plus a second).+ th = threading.Thread(target=lambda: box.append(_probe_network(url, timeout)), daemon=True)+ th.start()+ th.join(timeout + 1.0)+ if box:+ return box[0]+ return False, "no answer from %s within %.0f s" % (_host(url), timeout)+++def _probe_network(url: str, timeout: float) -> Tuple[bool, str]:req = urllib.request.Request(url, method="HEAD", headers={"User-Agent": "showtime-doctor"})ctx = ssl.create_default_context()try:diff --git a/skills/showtime/lib/st/xray.py b/skills/showtime/lib/st/xray.pynew file mode 100644index 0000000..1beb74c--- /dev/null+++ b/skills/showtime/lib/st/xray.py@@ -0,0 +1,949 @@+"""X-ray: measure what happened in a filmed clip (sound onsets, camera motion), then check a finished film against it.++`analyze` writes <job>/xray/xray.json (schema showtime.xray/1, the contract in references/workflows/xray.md) and+<job>/xray/check.jpg; `verify` reads a rendered film back: the sync marker in its corner, the audio against the+source, and (optionally) the smoothness of a scrolling band.++Onset detector: a high-passed copy of the sound, its RMS on a 1 ms grid, bursts above an adaptive floor, the onset+of a burst is its first millisecond within 6 dB of the burst's peak. numpy and scipy only (not st.audio.beats: that+one re-picks differently on loudness-normalised sound). Tracking: cv2 KLT + RANSAC homographies, seeded.+"""+from __future__ import annotations++import glob+import hashlib+import json+import math+import os+from pathlib import Path+from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple++import numpy as np++from . import ff+from .common import ShowtimeError, log, read_json, warn, write_json++SCHEMA = "showtime.xray/1"+SR = 48000+MARKER_SIZE = 6+MARKER_INSET = 10 # the marker's top-left is (width - 10, height - 10)+TRACK_MAX_BYTES = 1 << 30 # the gray frames --track keeps in memory; long clips are tracked at a smaller size, then refused+TRACK_MIN_WIDTH = 320+MARKER_SLACK = 2 # a marker frame this close to an expected one still counts as matched (reported with its offset)+LAG_LIMIT_MS = 20.0 # qa: audio more than this far from the source fails+AUDIO_SECONDS = 12.0+DETECT_HP_HZ = 500.0+FLOOR_PCT = 30 # the adaptive floor: this percentile of the envelope over FLOOR_WIN_S+FLOOR_WIN_S = 1.0+PEAK_DROP_DB = 6.0 # onset = first ms within this many dB of the burst's peak+RING_MS = 15 # fragments of one burst closer than this are joined+PRECURSOR_DB = 10.0 # a burst this much louder within min_gap/2 after a weaker one makes the weaker its precursor+ABS_GATE_DB = -75.0 # nothing below this is a sound (digital silence)+++# ------------------------------------------------------------------ small helpers++def frame_of(t: float, fps: float) -> int:+ """The source frame an event at t seconds lands on: floor(t * fps) (a hair of slack for float error)."""+ return int(math.floor(t * fps + 1e-9))+++def _sha256(path: Path) -> str:+ h = hashlib.sha256()+ with open(str(path), "rb") as f:+ for chunk in iter(lambda: f.read(1 << 20), b""):+ h.update(chunk)+ return h.hexdigest()+++def parse_points(text: str) -> List[List[float]]:+ """"x,y x,y x,y" -> [[x, y], ...] (at least 3 points)."""+ pts: List[List[float]] = []+ for tok in str(text).replace(";", " ").split():+ parts = tok.split(",")+ if len(parts) != 2:+ raise ShowtimeError("bad point %r (expected x,y)" % tok, hint='a region is "x,y x,y x,y ..." in source pixels')+ try:+ pts.append([float(parts[0]), float(parts[1])])+ except ValueError:+ raise ShowtimeError("bad point %r (expected numbers x,y)" % tok)+ if len(pts) < 3:+ raise ShowtimeError("a polygon needs at least 3 points (got %d)" % len(pts))+ return pts+++# ------------------------------------------------------------------ onset detector++def envelope_db(x: np.ndarray, sr: int = SR, hp_hz: float = DETECT_HP_HZ) -> np.ndarray:+ """RMS of the high-passed sound on a 1 ms grid, in dB re full scale, smoothed over 3 ms."""+ from scipy import ndimage, signal+ x = np.asarray(x, dtype=np.float64)+ sos = signal.butter(2, hp_hz, "hp", fs=sr, output="sos")+ y = signal.sosfilt(sos, x)+ per = max(1, int(round(sr / 1000.0)))+ n = len(y) // per+ if n < 3:+ return np.full(max(n, 1), -120.0)+ e = np.sqrt((y[: n * per].reshape(n, per) ** 2).mean(axis=1) + 1e-12)+ return ndimage.uniform_filter1d(20.0 * np.log10(e), 3)+++def _floor_db(env: np.ndarray) -> np.ndarray:+ """The adaptive floor: a low percentile of the envelope over about a second, per millisecond."""+ from scipy import ndimage+ step = 5+ coarse = env[::step]+ size = max(3, int(FLOOR_WIN_S * 1000 / step))+ fl = ndimage.percentile_filter(coarse, FLOOR_PCT, size=size, mode="nearest")+ return np.interp(np.arange(len(env)), np.arange(0, len(env), step)[: len(fl)], fl)+++def _runs(mask: np.ndarray) -> List[Tuple[int, int]]:+ """[(first, last)] of every run of True."""+ if not mask.any():+ return []+ d = np.diff(np.concatenate(([0], mask.astype(np.int8), [0])))+ return list(zip(np.where(d == 1)[0].tolist(), (np.where(d == -1)[0] - 1).tolist()))+++def _band_of(x: np.ndarray, i_ms: int, sr: int) -> str:+ """low (<300 Hz) / mid / high (>2 kHz): where the energy of the 24 ms after the onset is."""+ a = int(i_ms * sr / 1000.0)+ seg = np.asarray(x[a: a + int(0.024 * sr)], dtype=np.float64)+ if len(seg) < 32:+ return "mid"+ spec = np.abs(np.fft.rfft(seg * np.hanning(len(seg)))) ** 2+ f = np.fft.rfftfreq(len(seg), 1.0 / sr)+ e = (spec[f < 300].sum(), spec[(f >= 300) & (f <= 2000)].sum(), spec[f > 2000].sum())+ return ("low", "mid", "high")[int(np.argmax(e))]+++def detect(x: np.ndarray, sr: int = SR, *, min_gap: float = 0.12, sensitivity: float = 0.5,+ t_from: float = 0.0, t_to: Optional[float] = None) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:+ """(events, dropped) of a mono sound. Times are seconds from the start of `x` plus t_from (so pass the slice+ that starts at t_from). `sensitivity` 0..1: higher finds quieter and closer hits."""+ s = min(1.0, max(0.0, float(sensitivity)))+ env = envelope_db(x, sr)+ n = len(env)+ floor = _floor_db(env)+ k_db = 18.0 - 12.0 * s # how far over the floor a burst must rise+ rel_db = 15.0 + 20.0 * s # and how far under the loud events it may sit+ above = (env > floor + k_db) & (env > ABS_GATE_DB)+ runs = _runs(above)+ merged: List[List[int]] = []+ for a, b in runs: # ringing of one click: fragments within 15 ms are one burst+ if merged and a - merged[-1][1] <= RING_MS:+ merged[-1][1] = b+ else:+ merged.append([a, b])+ bursts = []+ for a, b in merged:+ seg = env[a: b + 1]+ pk = float(seg.max())+ on = a + int(np.argmax(seg >= pk - PEAK_DROP_DB))+ bursts.append({"i": on, "pk": pk})+ dropped: List[Dict[str, Any]] = []+ kept: List[Dict[str, Any]] = []+ for j, b in enumerate(bursts):+ nx = bursts[j + 1] if j + 1 < len(bursts) else None+ if nx is not None and (nx["i"] - b["i"]) / 1000.0 < min_gap / 2 and nx["pk"] - b["pk"] >= PRECURSOR_DB:+ dropped.append({"t": round(t_from + b["i"] / 1000.0, 3),+ "why": "precursor of the louder click at %.3f" % (t_from + nx["i"] / 1000.0)})+ else:+ kept.append(b)+ bursts = kept+ if bursts:+ ref = float(np.percentile([b["pk"] for b in bursts], 85))+ keep = []+ for b in bursts:+ if b["pk"] < ref - rel_db:+ dropped.append({"t": round(t_from + b["i"] / 1000.0, 3),+ "why": "%.0f dB under the loud events (below the sensitivity gate)" % (ref - b["pk"])})+ else:+ keep.append(b)+ bursts = keep+ events: List[Dict[str, Any]] = []+ for b in bursts:+ t = t_from + b["i"] / 1000.0+ if t_to is not None and t > t_to:+ continue+ if events and t - events[-1]["t"] < min_gap - 1e-9:+ dropped.append({"t": round(t, 3), "why": "within min_gap of event %d" % events[-1]["i"]})+ continue+ events.append({"i": len(events), "t": round(t, 3), "pk": b["pk"], "ms": b["i"]})+ top = max([10 ** (e["pk"] / 20.0) for e in events] or [1.0])+ out = []+ for e in events:+ out.append({"i": e["i"], "t": e["t"], "strength": round(min(1.0, 10 ** (e["pk"] / 20.0) / top), 3),+ "band": _band_of(x, e["ms"], sr)})+ dropped.sort(key=lambda d: d["t"])+ return out, dropped+++# ------------------------------------------------------------------ flux engine (dense or sustained sound)++FLUX_WIN = 2048 # STFT window (samples) and hop: the spectral-flux curve+FLUX_HOP = 256+FLUX_MEDIAN_S = 0.2 # the adaptive threshold: a moving median of the flux over this long ...+REFINE_MS = 20 # ... and each peak is refined on the 1 ms grid within +-20 ms+MIN_RISE_DB = 3.0+AUTO_RATE = 0.3 # auto: the envelope engine under this many events per second of a sustained sound -> flux+SUSTAINED = 0.5 # "sustained": at least this share of the sound is within 25 dB of its loud parts+DEFAULT_MIN_GAP = {"envelope": 0.12, "flux": 0.06}+++def flux_curve(x: np.ndarray, sr: int = SR) -> Tuple[np.ndarray, np.ndarray]:+ """(times of the frame centres, half-wave rectified spectral flux summed over bins of a log-magnitude STFT)."""+ from scipy import signal+ f, t, Z = signal.stft(np.asarray(x, dtype=np.float32), sr, nperseg=FLUX_WIN, noverlap=FLUX_WIN - FLUX_HOP,+ window="hann", boundary=None, padded=False)+ if Z.shape[1] < 3:+ return np.zeros(0), np.zeros(0)+ mag = np.log1p(1000.0 * np.abs(Z))+ d = np.concatenate([[0.0], np.maximum(mag[:, 1:] - mag[:, :-1], 0.0).sum(axis=0)])+ return t, d+++def _refine(env: np.ndarray, ms: int) -> int:+ """The 1 ms bin of the onset near `ms`: the first ms of the final rise to the local peak within 6 dB of it+ (inside +-20 ms); `ms` itself when the envelope does not rise there."""+ a, b = max(0, ms - REFINE_MS), min(len(env), ms + REFINE_MS + 1)+ if b - a < 4:+ return ms+ seg = env[a:b]+ ip = int(np.argmax(seg))+ i0 = ip+ while i0 > 0 and seg[i0 - 1] < seg[i0]:+ i0 -= 1+ if seg[ip] - seg[i0] < MIN_RISE_DB:+ return ms+ return a + i0 + int(np.argmax(seg[i0: ip + 1] >= seg[ip] - PEAK_DROP_DB))+++def detect_flux(x: np.ndarray, sr: int = SR, *, min_gap: float = 0.06, sensitivity: float = 0.5,+ t_from: float = 0.0, t_to: Optional[float] = None) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:+ """(events, dropped) by spectral flux: peaks over a moving median + k robust deviations (k falls as+ `sensitivity` rises), at least `min_gap` apart (the stronger of two close peaks stays), each placed on the 1 ms grid."""+ from scipy import ndimage, signal+ s = min(1.0, max(0.0, float(sensitivity)))+ t, d = flux_curve(x, sr)+ if len(d) == 0:+ return [], []+ size = max(3, int(FLUX_MEDIAN_S * sr / FLUX_HOP) | 1)+ med = ndimage.median_filter(d, size=size, mode="nearest")+ scale = 1.4826 * float(np.median(np.abs(d - med))) + 1e-9+ k = 3.8 - 3.0 * s+ pk, _ = signal.find_peaks(d, height=med + k * scale)+ order = sorted(pk.tolist(), key=lambda i: -d[i]) # the strongest first: a weaker neighbour within min_gap goes+ kept: List[int] = []+ dropped: List[Dict[str, Any]] = []+ env = envelope_db(x, sr)+ for i in order:+ near = [j for j in kept if abs(t[i] - t[j]) < min_gap - 1e-9]+ if near:+ dropped.append({"t": round(t_from + t[i], 3), "why": "within min_gap of the stronger onset at %.3f" % (t_from + t[near[0]])})+ else:+ kept.append(i)+ kept.sort()+ top = float(max(d[i] for i in kept)) if kept else 1.0+ events: List[Dict[str, Any]] = []+ for i in kept:+ ms = _refine(env, int(round(t[i] * 1000.0)))+ tt = t_from + ms / 1000.0+ if t_to is not None and tt > t_to:+ continue+ events.append({"i": 0, "t": round(tt, 3), "strength": round(float(d[i]) / top, 3), "band": _band_of(x, ms, sr)})+ events.sort(key=lambda e: e["t"])+ spaced: List[Dict[str, Any]] = []+ for e in events: # refining moved some: keep min_gap on the final times too+ if spaced and e["t"] - spaced[-1]["t"] < min_gap - 1e-9:+ if e["strength"] > spaced[-1]["strength"]:+ dropped.append({"t": spaced[-1]["t"], "why": "within min_gap of the stronger onset at %.3f" % e["t"]})+ spaced[-1] = e+ else:+ dropped.append({"t": e["t"], "why": "within min_gap of the stronger onset at %.3f" % spaced[-1]["t"]})+ else:+ spaced.append(e)+ events = spaced+ for n, e in enumerate(events):+ e["i"] = n+ dropped.sort(key=lambda d_: d_["t"])+ return events, dropped+++def is_sustained(x: np.ndarray, sr: int = SR) -> bool:+ """Most of the sound is within 25 dB of its loud parts (music, a tone, a hum), unlike separate clicks in quiet."""+ env = envelope_db(x, sr)+ if len(env) < 100:+ return False+ top = float(np.percentile(env, 99))+ return bool(float(np.mean((env > top - 25.0) & (env > ABS_GATE_DB))) >= SUSTAINED)+++def pick_engine(x: np.ndarray, engine: str, sr: int = SR, *, min_gap: Optional[float] = None, sensitivity: float = 0.5,+ t_from: float = 0.0, t_to: Optional[float] = None) -> Tuple[str, float, str, List[Dict[str, Any]], List[Dict[str, Any]]]:+ """(engine that ran, its min_gap, why, events, dropped) for engine envelope | flux | auto."""+ if engine not in ("envelope", "flux", "auto"):+ raise ShowtimeError("--engine is envelope, flux or auto (got %r)" % engine)+ if engine == "flux":+ mg = min_gap if min_gap is not None else DEFAULT_MIN_GAP["flux"]+ ev, dr = detect_flux(x, sr, min_gap=mg, sensitivity=sensitivity, t_from=t_from, t_to=t_to)+ return "flux", mg, "asked for flux", ev, dr+ mg = min_gap if min_gap is not None else DEFAULT_MIN_GAP["envelope"]+ ev, dr = detect(x, sr, min_gap=mg, sensitivity=sensitivity, t_from=t_from, t_to=t_to)+ if engine == "envelope":+ return "envelope", mg, "asked for envelope", ev, dr+ secs = len(x) / float(sr)+ rate = len(ev) / secs if secs > 0 else 0.0+ if rate < AUTO_RATE and is_sustained(x, sr):+ mg2 = min_gap if min_gap is not None else DEFAULT_MIN_GAP["flux"]+ ev2, dr2 = detect_flux(x, sr, min_gap=mg2, sensitivity=sensitivity, t_from=t_from, t_to=t_to)+ return "flux", mg2, ("auto: the envelope engine found %d events in %.1f s (under %.1f per second) of sustained sound, "+ "so spectral flux ran" % (len(ev), secs, AUTO_RATE)), ev2, dr2+ return "envelope", mg, "auto: separate hits (%d events in %.1f s), the envelope engine" % (len(ev), secs), ev, dr+++# ------------------------------------------------------------------ analyze++def _source_info(clip: Path) -> Dict[str, Any]:+ pr = ff.probe(clip)+ if not pr.get("has_video"):+ raise ShowtimeError("%s has no video stream" % clip.name, hint="x-ray analyzes a filmed clip (video with its sound)")+ fps = pr.get("fps") or 30.0+ vfr = _is_vfr(pr)+ info = {"file": clip.name, "sha256": _sha256(clip), "fps": round(float(fps), 6) if fps != int(fps) else int(fps),+ "duration": round(float(pr.get("duration") or pr.get("video_duration") or 0.0), 3),+ "width": int(pr.get("width") or 0), "height": int(pr.get("height") or 0), "has_audio": bool(pr.get("has_audio"))}+ if vfr:+ info["vfr"] = True+ return info+++def validate_clip(clip: Any) -> Path:+ """Refuse a clip that cannot be analyzed (missing, no picture, no sound) before anything is created for it."""+ clip = Path(clip)+ if not clip.is_file():+ raise ShowtimeError("clip not found: %s" % clip, hint="pass the video file you filmed")+ pr = ff.probe(clip)+ if not pr.get("has_video"):+ raise ShowtimeError("%s has no video stream" % clip.name, hint="x-ray analyzes a filmed clip (video with its sound)")+ if not pr.get("has_audio"):+ raise ShowtimeError("%s has no sound to analyze" % clip.name, hint="x-ray reads the events from the clip's audio")+ return clip+++def _is_vfr(pr: Dict[str, Any]) -> bool:+ """A variable-frame-rate clip (a phone video): r_frame_rate and avg_frame_rate differ by more than 1 %."""+ r, a = pr.get("r_fps"), pr.get("avg_fps")+ return bool(r and a and abs(r - a) > 0.01 * max(r, a))+++VFR_WARNING = ("the clip is variable frame rate (a phone video): event frames are counted at one rate and may drift. "+ "Run `showtime footage trim <clip> --webm` (constant 30 fps) and x-ray that file")+++def analyze(clip: Any, out_dir: Any, *, t_from: float = 0.0, t_to: Optional[float] = None, min_gap: Optional[float] = None,+ sensitivity: float = 0.5, track: Optional[Dict[str, Any]] = None, engine: str = "auto") -> Dict[str, Any]:+ """Write <out_dir>/xray.json and check.jpg for a clip. `track`: {"region": [[x, y]...], "ref_time": T,+ "masks": [[[x, y]...]...]} to also track the object. Returns the xray.json dict."""+ from .audio import wav+ clip = Path(clip)+ if not clip.is_file():+ raise ShowtimeError("clip not found: %s" % clip, hint="pass the video file you filmed")+ out = Path(out_dir)+ src = _source_info(clip)+ if src.get("vfr"):+ warn(VFR_WARNING)+ if not src.pop("has_audio"):+ raise ShowtimeError("%s has no sound to analyze" % clip.name, hint="x-ray reads the hits from the clip's audio")+ if t_to is not None and t_to <= t_from:+ raise ShowtimeError("--to (%g) must be after --from (%g)" % (t_to, t_from))+ out.mkdir(parents=True, exist_ok=True)+ dur = (t_to - t_from) if t_to is not None else None+ x = wav.load(clip, sr=SR, mono=True, offset=max(0.0, t_from), duration=dur)+ ran, min_gap, why, events, dropped = pick_engine(x, engine, SR, min_gap=min_gap, sensitivity=sensitivity,+ t_from=max(0.0, t_from), t_to=t_to)+ fps = float(src["fps"])+ for e in events:+ e["frame"] = frame_of(e["t"], fps)+ events = [{"i": e["i"], "t": e["t"], "frame": e["frame"], "strength": e["strength"], "band": e["band"]} for e in events]+ doc: Dict[str, Any] = {+ "schema": SCHEMA, "source": src,+ "detector": {"engine": ran, "min_gap": min_gap, "sensitivity": sensitivity, "from": t_from, "to": t_to, "note": why},+ "events": events, "dropped": dropped, "track": None}+ if track:+ doc["track"] = track_clip(clip, src, track["region"], track["ref_time"], track.get("masks") or [])+ write_json(out / "xray.json", doc)+ try:+ (out / "source.path").write_text(str(clip.resolve()), encoding="utf-8") # local only: lets verify find the clip+ except OSError:+ pass+ check_sheet(clip, doc, x, max(0.0, t_from), out / "check.jpg")+ return doc+++def check_sheet(clip: Path, doc: Dict[str, Any], x: np.ndarray, t0: float, dest: Path) -> Path:+ """check.jpg: the waveform with a numbered tick per event (dropped ones grey) and, with a track, the region on 6 frames."""+ from PIL import Image, ImageDraw+ W, H = 1800, 360+ img = Image.new("RGB", (W, H), (18, 20, 26))+ d = ImageDraw.Draw(img)+ dur = float(doc["source"]["duration"]) or (t0 + len(x) / SR)+ t1 = t0 + len(x) / SR+ left, right, top, bot = 30, W - 20, 40, H - 30+ mid = (top + bot) // 2++ def px(t: float) -> int:+ return int(left + (t / max(dur, 1e-6)) * (right - left))++ a, b = px(t0), px(t1)+ cols = max(1, b - a)+ n = len(x)+ if n:+ edges = np.linspace(0, n, cols + 1).astype(int)+ peak = max(float(np.abs(x).max()), 1e-9)+ for c in range(cols):+ seg = x[edges[c]: max(edges[c + 1], edges[c] + 1)]+ if len(seg):+ lo, hi = float(seg.min()) / peak, float(seg.max()) / peak+ d.line([(a + c, mid - hi * (mid - top)), (a + c, mid - lo * (mid - top))], fill=(120, 150, 200))+ for s in range(int(dur) + 1):+ d.line([(px(s), bot + 2), (px(s), bot + 8)], fill=(120, 120, 130))+ d.text((px(s) + 2, bot + 10), "%ds" % s, fill=(150, 150, 160))+ for dr in doc.get("dropped") or []:+ d.line([(px(dr["t"]), top), (px(dr["t"]), bot)], fill=(90, 90, 96))+ for e in doc["events"]:+ xx = px(e["t"])+ d.line([(xx, top - 8), (xx, bot)], fill=(255, 90, 60), width=1)+ d.text((xx + 2, 8 + (e["i"] % 2) * 12), str(e["i"]), fill=(255, 180, 160))+ d.text((left, H - 14), "%s %d events (red), %d dropped (grey) min_gap %.3g sensitivity %.3g" % (+ doc["source"]["file"], len(doc["events"]), len(doc.get("dropped") or []),+ doc["detector"]["min_gap"], doc["detector"]["sensitivity"]), fill=(200, 200, 210))+ tr = doc.get("track")+ if tr:+ img = _with_track_row(img, clip, doc, tr)+ dest.parent.mkdir(parents=True, exist_ok=True)+ img.save(str(dest), quality=88)+ return dest+++def _with_track_row(img: Any, clip: Path, doc: Dict[str, Any], tr: Dict[str, Any]) -> Any:+ """Append the region outline drawn on 6 frames, projected by the tracked homographies."""+ import tempfile+ from PIL import Image, ImageDraw+ from .qa import media+ Hs = tr["H"]+ n = len(Hs)+ fps = float(doc["source"]["fps"])+ frames = sorted({int(round(i * (n - 1) / 5.0)) for i in range(6)})+ tile_w = img.width // 6+ sw, sh = doc["source"]["width"], doc["source"]["height"]+ tile_h = int(tile_w * sh / max(sw, 1))+ row = Image.new("RGB", (img.width, tile_h + 4), (18, 20, 26))+ with tempfile.TemporaryDirectory(prefix="st-xray-") as td:+ paths = media.extract_frames(clip, [(f + 0.5) / fps for f in frames], td, width=tile_w,+ duration=doc["source"]["duration"], fps=fps, names=["f%d.jpg" % f for f in frames])+ by_frame = {} # extraction skips a failed frame: pair by the name it was written under+ for p in paths:+ try:+ by_frame[int(Path(p).stem[1:])] = p+ except ValueError:+ continue+ for k, f in enumerate(frames):+ if f not in by_frame:+ continue+ with Image.open(str(by_frame[f])) as raw:+ im = raw.convert("RGB")+ sc = im.width / float(sw)+ dr = ImageDraw.Draw(im)+ H = np.array(Hs[f], dtype=np.float64).reshape(3, 3)++ def proj(poly: Sequence[Sequence[float]]) -> List[Tuple[float, float]]:+ pts = np.hstack([np.array(poly, dtype=np.float64), np.ones((len(poly), 1))]) @ H.T+ return [(float(p[0] / p[2]) * sc, float(p[1] / p[2]) * sc) for p in pts]++ q = proj(tr["region"])+ dr.line(q + [q[0]], fill=(60, 255, 120), width=2)+ for m in tr.get("mask") or []:+ q = proj(m)+ dr.line(q + [q[0]], fill=(255, 200, 60), width=1)+ dr.text((4, 4), "frame %d" % f, fill=(255, 255, 255))+ row.paste(im, (k * tile_w, 0))+ out = Image.new("RGB", (img.width, img.height + row.height), (18, 20, 26))+ out.paste(img, (0, 0))+ out.paste(row, (0, img.height))+ return out+++# ------------------------------------------------------------------ tracking++def read_gray_frames(clip: Path, width: int, height: int, scale_w: int) -> Iterator[np.ndarray]:+ """Every frame of the clip as a gray array scaled to scale_w pixels wide (the resolved ffmpeg, a pipe)."""+ import subprocess+ sh = max(2, int(round(scale_w * height / float(width) / 2.0)) * 2)+ args = [ff.ffmpeg_path(), "-hide_banner", "-nostdin", "-loglevel", "error", "-i", str(clip), "-an",+ "-vf", "scale=%d:%d,format=gray" % (scale_w, sh), "-f", "rawvideo", "-pix_fmt", "gray", "-"]+ proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)+ size = scale_w * sh+ try:+ while True:+ buf = proc.stdout.read(size)+ if len(buf) < size:+ break+ yield np.frombuffer(buf, dtype=np.uint8).reshape(sh, scale_w)+ finally:+ try:+ proc.stdout.close()+ except OSError:+ pass+ proc.kill()+ proc.wait()+++def track_clip(clip: Path, src: Dict[str, Any], region: Sequence[Sequence[float]], ref_time: float,+ masks: Sequence[Sequence[Sequence[float]]]) -> Dict[str, Any]:+ """The `track` object of xray.json: homographies from the reference frame to every frame (source pixels)."""+ scale_w = min(int(src["width"]), 960)+ est = int(float(src.get("duration") or 0.0) * float(src.get("fps") or 30.0)) + 2+ while scale_w > TRACK_MIN_WIDTH and est * scale_w * scale_w * int(src["height"]) / float(src["width"]) > TRACK_MAX_BYTES:+ scale_w = max(TRACK_MIN_WIDTH, int(scale_w * 0.8) // 2 * 2) # smaller pictures keep a long clip within memory+ s = scale_w / float(src["width"])+ cap = max(1, int(TRACK_MAX_BYTES // max(1.0, scale_w * scale_w * int(src["height"]) / float(src["width"]))))+ frames = []+ for fr in read_gray_frames(clip, int(src["width"]), int(src["height"]), scale_w):+ frames.append(fr)+ if len(frames) > cap:+ raise ShowtimeError("%s is too long to track in memory (over %d frames at %d px wide)" % (clip.name, cap, scale_w),+ hint="trim the clip first: showtime footage trim <clip> --from A --to B --webm")+ if not frames:+ raise ShowtimeError("could not decode frames of %s for tracking" % clip.name)+ ref = frame_of(ref_time, float(src["fps"]))+ ref = min(max(ref, 0), len(frames) - 1)+ S = np.diag([s, s, 1.0])+ Hs = track_frames(frames, ref, np.array(region, dtype=np.float64) * s, [np.array(m, dtype=np.float64) * s for m in masks])+ full = [np.linalg.inv(S) @ H @ S for H in Hs]+ return {"ref_frame": ref, "region": [[float(a), float(b)] for a, b in region],+ "mask": [[[float(a), float(b)] for a, b in m] for m in masks],+ "H": [[round(float(v), 6) for v in (H / H[2, 2]).reshape(9)] for H in full]}+++def _poly_mask(shape: Tuple[int, int], region: np.ndarray, masks: Sequence[np.ndarray], H: np.ndarray) -> np.ndarray:+ import cv2++ def warp(poly: np.ndarray) -> np.ndarray:+ p = np.hstack([poly, np.ones((len(poly), 1))]) @ H.T+ return np.round(p[:, :2] / p[:, 2:3]).astype(np.int32)++ m = np.zeros(shape, dtype=np.uint8)+ cv2.fillPoly(m, [warp(region)], 255)+ for hole in masks:+ cv2.fillPoly(m, [warp(hole)], 0)+ return m+++def track_frames(frames: Sequence[np.ndarray], ref: int, region: np.ndarray, masks: Sequence[np.ndarray],+ max_corners: int = 400) -> List[np.ndarray]:+ """3x3 homographies (reference-frame pixels -> frame pixels), one per frame, from KLT point tracks inside+ the region (minus the masks), composed frame to frame from the reference outwards. Deterministic."""+ import cv2+ cv2.setRNGSeed(0)+ cv2.setNumThreads(1)+ n = len(frames)+ Hs: List[Optional[np.ndarray]] = [None] * n+ Hs[ref] = np.eye(3)+ lk = dict(winSize=(21, 21), maxLevel=3, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.01))++ def feats(img: np.ndarray, H: np.ndarray) -> Optional[np.ndarray]:+ mk = _poly_mask(img.shape, region, masks, H)+ return cv2.goodFeaturesToTrack(img, max_corners, 0.01, 7, mask=mk)++ for step in (1, -1):+ H_cum = np.eye(3)+ prev = frames[ref]+ pts = feats(prev, H_cum)+ n0 = 0 if pts is None else len(pts)+ i = ref + step+ while 0 <= i < n:+ cur = frames[i]+ Hstep = np.eye(3)+ if pts is not None and len(pts) >= 4:+ nxt, st, _ = cv2.calcOpticalFlowPyrLK(prev, cur, pts, None, **lk)+ good = st.reshape(-1) == 1+ a, b = pts[good], nxt[good]+ if len(a) >= 8:+ M, inl = cv2.findHomography(a, b, cv2.RANSAC, 2.0)+ if M is not None:+ Hstep = M+ keep = inl.reshape(-1) == 1+ b = b[keep]+ pts = b.reshape(-1, 1, 2).astype(np.float32) if len(b) else None+ H_cum = Hstep @ H_cum+ Hs[i] = H_cum+ prev = cur+ if pts is None or len(pts) < max(12, n0 // 2):+ fresh = feats(cur, H_cum)+ if fresh is not None and len(fresh) >= 8:+ pts, n0 = fresh, len(fresh)+ i += step+ return [H if H is not None else np.eye(3) for H in Hs]+++# ------------------------------------------------------------------ verify++def _read_marker_levels(video: Path, width: int, height: int) -> np.ndarray:+ """Mean gray of the marker patch on every frame of the film (one value per frame)."""+ import subprocess+ x, y = width - MARKER_INSET, height - MARKER_INSET+ args = [ff.ffmpeg_path(), "-hide_banner", "-nostdin", "-loglevel", "error", "-i", str(video), "-an",+ "-vf", "crop=%d:%d:%d:%d,format=gray" % (MARKER_SIZE, MARKER_SIZE, x, y),+ "-f", "rawvideo", "-pix_fmt", "gray", "-"]+ cp = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)+ if cp.returncode != 0:+ raise ShowtimeError("ffmpeg could not read the film %s: %s" % (video.name, cp.stderr.decode("utf-8", "replace")[-300:]))+ px = np.frombuffer(cp.stdout, dtype=np.uint8)+ per = MARKER_SIZE * MARKER_SIZE+ px = px[: (len(px) // per) * per].reshape(-1, per)+ return px.mean(axis=1) if len(px) else np.zeros(0)+++def match_markers(expected: Sequence[int], on: Sequence[int], slack: int = MARKER_SLACK) -> Dict[str, Any]:+ """Pair expected marker frames with ON frames (exact first, then the nearest within `slack`)."""+ exp = sorted(set(int(e) for e in expected))+ onset = sorted(set(int(o) for o in on))+ free_on = set(onset)+ exact, near = [], []+ rest = []+ for e in exp:+ if e in free_on:+ free_on.discard(e)+ exact.append(e)+ else:+ rest.append(e)+ missing = []+ for e in rest:+ cand = sorted((abs(o - e), o) for o in free_on if abs(o - e) <= slack)+ if cand:+ d, o = cand[0]+ free_on.discard(o)+ near.append({"expected": e, "on": o, "offset": o - e})+ else:+ missing.append(e)+ return {"exact": exact, "near": near, "missing": missing, "extra": sorted(free_on),+ "max_offset": max([abs(n["offset"]) for n in near] or [0])}+++def audio_lag(source: Path, film: Path, expected_ms: float = 0.0, seconds: float = AUDIO_SECONDS,+ sr: int = 16000) -> Dict[str, Any]:+ """How late the film's sound is against the source's (ms, positive = later), by cross-correlating the first+ `seconds` of both (high-passed, so the clicks lead). `expected_ms` is a deliberate delay (the film's offset)."""+ from scipy import signal+ from .audio import wav+ a = wav.load(source, sr=48000, mono=True, duration=seconds)+ b = wav.load(film, sr=48000, mono=True, duration=seconds + abs(expected_ms) / 1000.0 + 1.0)+ if len(a) < 4800 or len(b) < 4800 or float(np.abs(a).max()) < 1e-6 or float(np.abs(b).max()) < 1e-6:+ return {"lag_ms": None, "corr": 0.0, "note": "no usable sound in the source or the film"}+ q = 48000 // sr+ sos = signal.butter(2, 300.0, "hp", fs=48000, output="sos")+ a = signal.resample_poly(signal.sosfilt(sos, a), 1, q)+ b = signal.resample_poly(signal.sosfilt(sos, b), 1, q)+ a = a - a.mean()+ b = b - b.mean()+ cc = signal.correlate(b, a, mode="full", method="fft")+ lags = np.arange(-(len(a) - 1), len(b))+ limit = int((abs(expected_ms) / 1000.0 + 1.0) * sr)+ sel = np.abs(lags) <= limit+ ccs, lg = cc[sel], lags[sel]+ k = int(np.argmax(ccs))+ off = 0.0+ if 0 < k < len(ccs) - 1:+ y0, y1, y2 = ccs[k - 1], ccs[k], ccs[k + 1]+ den = y0 - 2 * y1 + y2+ off = 0.5 * (y0 - y2) / den if den != 0 else 0.0+ na, nb = float(np.sqrt((a ** 2).sum())), float(np.sqrt((b ** 2).sum()))+ # normalise by the overlapping part of b: the film is longer than the source window+ lagi = int(lg[k])+ lo, hi = max(0, lagi), min(len(b), len(a) + lagi)+ nb_o = float(np.sqrt((b[lo:hi] ** 2).sum())) if hi > lo else nb+ corr = float(ccs[k] / (na * nb_o + 1e-12))+ lag_ms = (lagi + off) / sr * 1000.0 - expected_ms+ return {"lag_ms": round(lag_ms, 1), "corr": round(corr, 3), "note": ""}+++def smoothness(video: Path, band: Sequence[int], max_shift: int = 40) -> Dict[str, Any]:+ """Frame-to-frame horizontal shift of a band (x, y, w, h) of the film by cross-correlating its column profiles.+ Stalls: 0 px while the median speed is over 1 px; jumps: the shift changes by more than 3 px."""+ import subprocess+ x, y, w, h = [int(v) for v in band]+ pr = ff.probe(video)+ if x < 0 or y < 0 or w < 8 or h < 1 or x + w > int(pr["width"]) or y + h > int(pr["height"]):+ raise ShowtimeError("--band %s lies outside the %dx%d film" % (",".join(str(v) for v in band), pr["width"], pr["height"]),+ hint="--band x,y,w,h in film pixels (w at least 8)")+ args = [ff.ffmpeg_path(), "-hide_banner", "-nostdin", "-loglevel", "error", "-i", str(video), "-an",+ "-vf", "crop=%d:%d:%d:%d,format=gray" % (w, h, x, y), "-f", "rawvideo", "-pix_fmt", "gray", "-"]+ proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)+ rows, size = [], w * h+ try: # one frame at a time: only its column profile is kept+ while True:+ buf = proc.stdout.read(size)+ if len(buf) < size:+ break+ rows.append(np.frombuffer(buf, dtype=np.uint8).reshape(h, w).astype(np.float64).mean(axis=0))+ err = proc.stderr.read()+ finally:+ proc.stdout.close()+ proc.stderr.close()+ if proc.poll() is None:+ proc.kill()+ proc.wait()+ if not rows and proc.returncode:+ raise ShowtimeError("ffmpeg could not read the film: %s" % err.decode("utf-8", "replace")[-300:])+ prof = np.array(rows) if rows else np.zeros((0, w))+ return smoothness_of_profiles(prof, max_shift)+++def _shift(a: np.ndarray, b: np.ndarray, max_shift: int) -> Optional[float]:+ """Horizontal shift (px, sub-pixel) that moves profile a onto b; None when a profile is flat."""+ a = a - a.mean()+ b = b - b.mean()+ na, nb = float(np.linalg.norm(a)), float(np.linalg.norm(b))+ if na < 1e-6 or nb < 1e-6:+ return None+ w = len(a)+ m = min(max_shift, w // 2)+ best, scores = None, {}+ for s in range(-m, m + 1):+ if s >= 0:+ u, v = a[: w - s], b[s:]+ else:+ u, v = a[-s:], b[: w + s]+ if len(u) < 4:+ continue+ scores[s] = float(np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v) + 1e-12))+ if not scores:+ return None+ best = max(scores, key=lambda k: scores[k])+ if best - 1 in scores and best + 1 in scores:+ y0, y1, y2 = scores[best - 1], scores[best], scores[best + 1]+ den = y0 - 2 * y1 + y2+ return best + (0.5 * (y0 - y2) / den if den != 0 else 0.0)+ return float(best)+++def smoothness_of_profiles(prof: np.ndarray, max_shift: int = 40) -> Dict[str, Any]:+ """The stall/jump report for per-frame column profiles (frames x width)."""+ shifts: List[Optional[float]] = [_shift(prof[i - 1], prof[i], max_shift) for i in range(1, len(prof))]+ vals = [abs(s) for s in shifts if s is not None]+ med = float(np.median(vals)) if vals else 0.0+ stalls, jumps = [], []+ if med > 1.0:+ for i, s in enumerate(shifts, start=1):+ if s is not None and abs(s) < 0.25:+ stalls.append(i)+ prev = None+ for i, s in enumerate(shifts, start=1): # a change into or out of a stalled frame counts as the stall+ if s is not None and prev is not None and abs(s - prev) > 3.0 and abs(s) >= 0.25 and abs(prev) >= 0.25:+ jumps.append(i)+ if s is not None:+ prev = s+ return {"frames": len(prof), "median_px": round(med, 2), "stalls": len(stalls), "jumps": len(jumps),+ "stall_frames": stalls[:50], "jump_frames": jumps[:50]}+++def project_config(job: Path) -> Dict[str, Any]:+ """The "xray" block of the job's project showtime.json ({"marker", "offset_frames", "band": [y0, y1]}):+ <job>/project/showtime.json first, then any */showtime.json under the job. {} when there is none."""+ job = Path(job)+ cands = [job / "project" / "showtime.json"]+ try:+ cands += sorted(p for p in job.glob("*/showtime.json") if p not in cands)+ cands += sorted(p for p in job.glob("*/*/showtime.json") if p not in cands and "node_modules" not in p.parts)[:20]+ except OSError:+ pass+ for p in cands:+ if p.is_file():+ d = read_json(p, {})+ x = d.get("xray") if isinstance(d, dict) else None+ if isinstance(x, dict):+ return x+ return {}+++def find_film(job: Path) -> Optional[Path]:+ from .job import ledger+ try:+ data = ledger.load(job)+ lo = ledger.last_output(job, data)+ if lo:+ return Path(lo[0])+ except Exception: # noqa: BLE001+ pass+ for pat in ("final*.mp4", "*.mp4"):+ hits = sorted(job.glob(pat), key=lambda p: p.stat().st_mtime, reverse=True)+ if hits:+ return hits[0]+ return None+++def find_source(job: Path, doc: Dict[str, Any], given: Optional[Any] = None) -> Optional[Path]:+ if given:+ p = Path(given).expanduser()+ return p if p.is_file() else None+ side = job / "xray" / "source.path"+ if side.is_file():+ p = Path(side.read_text(encoding="utf-8").strip())+ if p.is_file():+ return p+ name = (doc.get("source") or {}).get("file")+ name = Path(str(name).replace("\\", "/")).name if name else "" # a bare file name: never a path out of the job+ if name and name not in (".", ".."):+ root = job.resolve()+ for p in list(job.rglob(glob.escape(name)))[:5]:+ try:+ inside = p.is_file() and p.resolve().is_relative_to(root) if hasattr(Path, "is_relative_to") else p.is_file()+ except (OSError, ValueError):+ inside = False+ if inside:+ return p+ return None+++def verify(job: Any, video: Optional[Any] = None, source: Optional[Any] = None, offset_frames: Optional[int] = None,+ band: Optional[Sequence[int]] = None, skip: Optional[Sequence[int]] = None) -> Dict[str, Any]:+ """Read a film back against <job>/xray/xray.json. Returns the `xray` receipt section (also saved as+ <job>/xray/verify.json), with `findings` ([(severity, rule, message)]) for qa."""+ job = Path(job)+ doc = read_json(job / "xray" / "xray.json", None)+ if not isinstance(doc, dict) or doc.get("schema") != SCHEMA:+ raise ShowtimeError("%s has no xray/xray.json" % job.name, hint="run `showtime xray analyze <clip> --job %s` first" % job.name)+ cfg = project_config(job)+ if offset_frames is None:+ try:+ offset_frames = int(cfg.get("offset_frames") or 0)+ except (TypeError, ValueError):+ offset_frames = 0+ film = Path(video) if video else find_film(job)+ if film is None or not Path(film).is_file():+ raise ShowtimeError("no film to verify in %s" % job.name, hint="pass the rendered file: --video final.mp4")+ film = Path(film)+ pr = ff.probe(film)+ W, H = int(pr["width"]), int(pr["height"])+ ffps = float(pr.get("fps") or 0)+ levels = _read_marker_levels(film, W, H)+ nfr = len(levels)+ on = [int(i) for i in np.where(levels > 128)[0]]+ if skip is None: # --skip, else the project's "xray".skip: events that are not the object's+ sk = cfg.get("skip")+ skip = list(sk) if isinstance(sk, (list, tuple)) else []+ skipset = set()+ bad_skip = []+ known = set(int(e["i"]) for e in doc["events"])+ for v in skip:+ if isinstance(v, bool) or not isinstance(v, int):+ bad_skip.append(repr(v))+ elif v not in known:+ bad_skip.append(str(v))+ else:+ skipset.add(v)+ src_fps = float((doc.get("source") or {}).get("fps") or 0)+ retime = bool(ffps and src_fps and abs(ffps - src_fps) > 0.01) # a 29.97 clip in a 30 fps film: frames from times+ expected = [(frame_of(float(e["t"]), ffps) if retime else int(e["frame"])) + int(offset_frames)+ for e in doc["events"] if int(e["i"]) not in skipset]+ inside = [e for e in expected if 0 <= e < nfr]+ marker = bool(on) and len(on) < nfr # a corner that is bright on every frame is a picture, not a marker+ m = match_markers(inside, on)+ res: Dict[str, Any] = {+ "events": len(inside), "on_frame": len(m["exact"]), "missing": m["missing"], "extra": m["extra"],+ "audio_lag_ms": None, "marker": marker, "off_by": m["near"], "max_offset": m["max_offset"],+ "outside_film": len(expected) - len(inside), "skipped": len(skipset), "film": film.name, "film_frames": nfr}+ findings: List[Tuple[str, str, str]] = []+ notes: List[str] = []+ if bad_skip:+ notes.append("WARN: skip entries ignored (not an event index of xray.json): %s" % ", ".join(bad_skip))+ if len(set(inside)) < len(inside):+ notes.append("%d event(s) share a film frame with another (frames %s): they count once on the film" % (+ len(inside) - len(set(inside)), sorted(set(f for f in inside if inside.count(f) > 1))[:6]))+ audio_unmatched = False+ if retime:+ notes.append("the film runs at %.4g fps, the clip at %.4g: expected film frames are floor(t * film fps) + offset" % (ffps, src_fps))+ src = find_source(job, doc, source)+ if src is None:+ notes.append("the source clip was not found (pass --source): audio not checked")+ elif not pr.get("has_audio"):+ notes.append("the film has no audio: audio not checked")+ else:+ al = audio_lag(src, film, expected_ms=1000.0 * offset_frames / (ffps or src_fps or 30.0))+ res["audio_lag_ms"] = al["lag_ms"]+ res["audio_corr"] = al["corr"]+ if al["lag_ms"] is None or al["corr"] < 0.05:+ res["audio_lag_ms"] = None+ notes.append("the film's sound does not match the clip's well enough to measure a lag (corr %.2f)" % al["corr"])+ audio_unmatched = True+ if not band and isinstance(cfg.get("band"), (list, tuple)) and len(cfg["band"]) == 2:+ try:+ y0, y1 = int(cfg["band"][0]), int(cfg["band"][1])+ band = [0, y0, W, y1 - y0] # the scrolling band's rows, full width+ except (TypeError, ValueError):+ band = None+ res["offset_frames"] = int(offset_frames)+ if band:+ res["smoothness"] = smoothness(film, band)+ if not marker:+ findings.append(("WARN", "xray_unverified", "the film has no sync marker in its bottom-right corner, so the events are unverified"))+ else:+ if m["missing"] or m["extra"]:+ findings.append(("FAIL", "xray_sync", "%d missing marker frame(s) %s, %d extra %s" % (+ len(m["missing"]), m["missing"][:6], len(m["extra"]), m["extra"][:6])))+ if res["audio_lag_ms"] is not None and abs(res["audio_lag_ms"]) > LAG_LIMIT_MS:+ findings.append(("FAIL", "xray_sync", "the film's audio is %.1f ms from the original (limit %g ms)" % (res["audio_lag_ms"], LAG_LIMIT_MS)))+ if audio_unmatched:+ findings.append(("WARN", "xray_audio", "the film's sound could not be matched to the clip's"))+ if m["near"]:+ findings.append(("WARN", "xray_sync_off", "%d event(s) are off their frame by up to %d frame(s)" % (len(m["near"]), m["max_offset"])))+ sm = res.get("smoothness")+ if sm and (sm["stalls"] or sm["jumps"]):+ findings.append(("WARN", "xray_choppy", "the scrolling band has %d stall(s) and %d jump(s) (median %.1f px/frame)" % (+ sm["stalls"], sm["jumps"], sm["median_px"])))+ res["status"] = "FAIL" if any(f[0] == "FAIL" for f in findings) else "WARN" if findings else "PASS"+ res["line"] = end_card_line(res)+ res["notes"] = notes+ res["findings"] = [{"severity": s, "rule": r, "message": t} for s, r, t in findings]+ write_json(job / "xray" / "verify.json", res)+ return res+++def end_card_line(res: Dict[str, Any]) -> str:+ """The one line for an end card."""+ if not res.get("marker"):+ return "%d events measured from the clip (sync not verified: no marker in the film)" % res.get("events", 0)+ lag = res.get("audio_lag_ms")+ tail = "audio %s ms from the original" % ("%.0f" % abs(lag) if abs(lag) >= 10 else "%.1f" % abs(lag)) if lag is not None \+ else "audio not compared"+ return "%d of %d events on their frame · %s" % (res["on_frame"], res["events"], tail)+++RECEIPT_KEYS = ("events", "on_frame", "missing", "extra", "audio_lag_ms", "marker", "off_by", "max_offset", "outside_film", "skipped",+ "film", "smoothness", "status", "line", "notes")+++def receipt_section(job: Path) -> Optional[Dict[str, Any]]:+ """The `xray` section of the job receipt, from <job>/xray/verify.json (None when verify has not run)."""+ d = read_json(Path(job) / "xray" / "verify.json", None)+ if not isinstance(d, dict) or "events" not in d:+ return None+ sec = {k: d[k] for k in RECEIPT_KEYS if k in d}+ cfg = project_config(job)+ if str(cfg.get("credit") or "").strip():+ sec["credit"] = str(cfg["credit"]).strip()+ elif cfg.get("own") is True:+ sec["own"] = True+ return sec+++def add_to_receipt(job: Path) -> bool:+ """Rewrite the job's receipt (receipt.json carries the `xray` section). Quiet: a receipt never fails verify."""+ try:+ from .job import receipt+ receipt.write(Path(job))+ return True+ except Exception: # noqa: BLE001+ return Falsediff --git a/skills/showtime/lib/st/xray_drums.py b/skills/showtime/lib/st/xray_drums.pynew file mode 100644index 0000000..dc0b924--- /dev/null+++ b/skills/showtime/lib/st/xray_drums.py@@ -0,0 +1,345 @@+"""X-ray drums: `showtime xray notes --instrument drums`.++A classifier of sound types, not a transcription: the hits come from xray.detect_flux (spectral flux, so a kick+that has no high end is still a hit), and each hit is sorted by its spectrum in the first 5-35 ms (70 ms for the low tone) against the+sound just before it: kick (a low tone under 100 Hz), snare (1-5 kHz noise), tom (a tonal 100-300 Hz body without+noise), hi-hat (noise from 6 kHz up, closed when it dies within 120 ms, open up to 450 ms) and crash (longer).+It does not know the kit: a kick and a hat within the same onset are two pieces; a hat under a snare is not seen.+Writes the "drums" list of notes.json, MIDI on channel 10 (General MIDI drum map), a MusicXML percussion staff+(sixteenth grid) and check-notes.jpg.+"""+from __future__ import annotations++import math+from pathlib import Path+from typing import Any, Dict, List, Optional, Sequence, Tuple+from xml.sax.saxutils import escape++import numpy as np++from .common import ShowtimeError, write_json+from . import xray_music as xm++SCHEMA = xm.SCHEMA+ENGINE = "flux-onsets+band-classifier"+SR = 48000+GM = {"kick": 36, "snare": 38, "hihat_closed": 42, "hihat_open": 46, "crash": 49}+TOM_GM = ((110.0, 45), (160.0, 48), (1e9, 50)) # by the tone's frequency: low, mid, high tom+PIECES = ("kick", "snare", "tom", "hihat_closed", "hihat_open", "crash")+RISE_DB = 8.0+KICK_RISE_DB = 12.0+KICK_MAX_HZ = 100.0+TONAL = 4.0 # peak over mean of the 100-300 Hz spectrum: a tone, not noise+OPEN_S = 0.12+CRASH_S = 0.45+HAT_DROP_DB = 15.0+SEG_A, SEG_B = 0.005, 0.035 # seconds after the onset: skips the click of a kick+BASE_A, BASE_B = -0.030, -0.003+NFFT = 8192+# MusicXML percussion staff (display position, notehead)+XML_POS = {"kick": ("F", 4, None), "snare": ("C", 5, None), "hihat_closed": ("G", 5, "x"), "hihat_open": ("G", 5, "circle-x"),+ "crash": ("A", 5, "x")}+XML_TOM = {45: ("A", 4), 48: ("D", 5), 50: ("E", 5)}+DRUM_NAMES = {"kick": "Bass Drum", "snare": "Snare", "hihat_closed": "Closed Hi-Hat", "hihat_open": "Open Hi-Hat", "crash": "Crash Cymbal"}+++def _psd(seg: np.ndarray, sr: int) -> Tuple[np.ndarray, np.ndarray]:+ """(freqs, power per Hz up to a constant) of a Hann-windowed segment, zero padded."""+ if len(seg) < 8:+ return np.fft.rfftfreq(NFFT, 1.0 / sr), np.zeros(NFFT // 2 + 1)+ w = np.hanning(len(seg))+ X = np.fft.rfft(seg * w, NFFT)+ return np.fft.rfftfreq(NFFT, 1.0 / sr), (np.abs(X) ** 2) / float(np.sum(w * w))+++def _band(f: np.ndarray, P: np.ndarray, lo: float, hi: float) -> float:+ m = (f >= lo) & (f < hi)+ return float(P[m].mean()) if m.any() else 0.0+++def _db(a: float, b: float) -> float:+ return 10.0 * math.log10((a + 1e-12) / (b + 1e-12))+++def _slice(x: np.ndarray, a: int, t0: float, t1: float, sr: int) -> np.ndarray:+ i0, i1 = max(0, a + int(round(t0 * sr))), max(0, a + int(round(t1 * sr)))+ return x[i0:i1]+++def hat_length(x: np.ndarray, a: int, next_a: Optional[int], sr: int = SR) -> float:+ """Seconds the 6 kHz+ noise rings after the onset at sample `a`: until it falls 15 dB under its peak, or until the+ next hit (at most 0.7 s) while it is still ringing."""+ from scipy import signal+ n = int(0.7 * sr)+ if next_a is not None:+ n = min(n, max(1, next_a - a - int(0.004 * sr)))+ seg = x[max(0, a - int(0.005 * sr)):a + n]+ if len(seg) < int(0.02 * sr):+ return 0.0+ y = signal.sosfilt(signal.butter(4, 6000.0, "hp", fs=sr, output="sos"), seg)+ hop = int(0.002 * sr)+ k = len(y) // hop+ if k < 4:+ return 0.0+ env = np.sqrt((y[:k * hop].reshape(k, hop) ** 2).mean(axis=1)) + 1e-12+ ip = int(np.argmax(env[:max(2, int(0.03 / 0.002))]))+ lim = env[ip] * 10 ** (-HAT_DROP_DB / 20.0)+ below = np.where(env[ip:] < lim)[0]+ return float((ip + (below[0] if len(below) else len(env) - ip)) * 0.002 - 0.0)+++def measure(x: np.ndarray, a: int, sr: int = SR) -> Dict[str, float]:+ """What rose at the onset at sample `a` over the sound just before it (dB per band), and the tone and noise shape."""+ on = _slice(x, a, SEG_A, SEG_B, sr)+ pre = _slice(x, a, BASE_A, BASE_B, sr)+ f, Pon = _psd(on, sr)+ _, Ppre = _psd(pre, sr)+ fk, Pk = _psd(_slice(x, a, 0.0, 0.07, sr), sr)+ _, Pkpre = _psd(pre, sr)+ m = (fk >= 40) & (fk <= 300)+ body = (f >= 100) & (f <= 300)+ return {+ "mid_rise": _db(_band(f, Pon, 1000, 5000), _band(f, Ppre, 1000, 5000)),+ "high_rise": _db(_band(f, Pon, 6000, 16000), _band(f, Ppre, 6000, 16000)),+ "body_rise": _db(_band(f, Pon, 100, 300), _band(f, Ppre, 100, 300)),+ "low_rise": _db(_band(fk, Pk, 30, 110), _band(fk, Pkpre, 30, 110)),+ "mid_psd": _band(f, Pon, 1000, 5000), "high_psd": _band(f, Pon, 6000, 16000),+ "peak_hz": float(fk[m][int(np.argmax(Pk[m]))]) if m.any() else 0.0,+ "tonal": float(Pon[body].max() / (Pon[body].mean() + 1e-12)) if body.any() else 0.0}+++def is_hit(m: Dict[str, float]) -> bool:+ """Something rose by 8 dB (12 for the low tone) over the sound before it: a new hit, not the flux wobbling in a ring-out."""+ return max(m["mid_rise"], m["high_rise"], m["body_rise"] - 2.0) >= RISE_DB or m["low_rise"] >= KICK_RISE_DB+++def classify(x: np.ndarray, a: int, next_a: Optional[int], sr: int = SR, m: Optional[Dict[str, float]] = None) -> List[Dict[str, Any]]:+ """The pieces heard at the onset at sample `a`: [{"piece", "gm", ("hz")}]; empty when no band rose over the sound before it."""+ m = m or measure(x, a, sr)+ mid_psd, high_psd, peak_hz = m["mid_psd"], m["high_psd"], m["peak_hz"]+ out: List[Dict[str, Any]] = []+ kick = m["low_rise"] >= KICK_RISE_DB and peak_hz < KICK_MAX_HZ+ snare = m["mid_rise"] >= RISE_DB and mid_psd >= 0.25 * high_psd+ # a hat over a snare: the snare's noise is about as loud at 6-16 kHz as at 1-5 kHz, a hat adds a lot more up there+ hat = m["high_rise"] >= RISE_DB and high_psd > 1e-9 and (not snare or high_psd >= 3.0 * mid_psd)+ tom = m["body_rise"] >= 10.0 and m["tonal"] >= TONAL and peak_hz >= KICK_MAX_HZ and not kick and not snare+ if kick:+ out.append({"piece": "kick", "gm": GM["kick"]})+ if snare:+ out.append({"piece": "snare", "gm": GM["snare"]})+ if hat:+ dur = hat_length(x, a, next_a, sr)+ piece = "hihat_closed" if dur < OPEN_S else "hihat_open" if dur < CRASH_S else "crash"+ out.append({"piece": piece, "gm": GM[piece]})+ if tom:+ out.append({"piece": "tom", "gm": next(g for hz, g in TOM_GM if peak_hz < hz), "hz": round(peak_hz, 1)})+ return out+++def detect_hits(x: np.ndarray, t_from: float = 0.0, t_to: Optional[float] = None, sr: int = SR) -> List[Dict[str, Any]]:+ """[{t, piece, gm, velocity}] sorted by time (t absolute, with t_from)."""+ from . import xray+ ev, _ = xray.detect_flux(x, sr, min_gap=0.04, sensitivity=0.6, t_from=t_from, t_to=t_to)+ if not ev:+ return []+ idx = [int(round((e["t"] - t_from) * sr)) for e in ev]+ env = xray.envelope_db(x, sr)+ peaks = []+ for a in idx:+ ms = int(a / (sr / 1000.0))+ peaks.append(float(env[ms:ms + 60].max()) if ms < len(env) else -120.0)+ top = max(peaks)+ meas = [measure(x, a, sr) for a in idx]+ real = [k for k in range(len(ev)) if is_hit(meas[k])]+ hits = []+ for r, k in enumerate(real):+ e = ev[k]+ nxt = idx[real[r + 1]] if r + 1 < len(real) else None+ vel = float(min(1.0, max(0.1, 1.0 + (peaks[k] - top) / 30.0)))+ for p in classify(x, idx[k], nxt, sr, meas[k]):+ h = {"t": e["t"], "piece": p["piece"], "gm": p["gm"], "velocity": round(vel, 3)}+ if "hz" in p:+ h["hz"] = p["hz"]+ hits.append(h)+ return hits+++# ------------------------------------------------------------------ score and files++DIV = 4 # divisions per quarter: a sixteenth is 1+MEASURE = 4 * DIV+_TYPES = {1: ("16th", False), 2: ("eighth", False), 3: ("eighth", True), 4: ("quarter", False), 6: ("quarter", True),+ 8: ("half", False), 12: ("half", True), 16: ("whole", False)}+++def _pieces(d: int) -> List[int]:+ out = []+ for p in (16, 12, 8, 6, 4, 3, 2, 1):+ while d >= p:+ out.append(p)+ d -= p+ return out+++def _rest(dur: int) -> str:+ typ, dot = _TYPES[dur]+ return "<note><rest/><duration>%d</duration><voice>1</voice><type>%s</type>%s</note>" % (dur, typ, "<dot/>" if dot else "")+++def _hit_xml(group: Sequence[Dict[str, Any]], dur: int, tie_stop: bool, tie_start: bool) -> str:+ typ, dot = _TYPES[dur]+ xs = []+ for k, h in enumerate(group):+ if h["piece"] == "tom":+ step, octv = XML_TOM[h["gm"]]+ head = None+ else:+ step, octv, head = XML_POS[h["piece"]]+ s = "<note>" + ("<chord/>" if k else "")+ s += "<unpitched><display-step>%s</display-step><display-octave>%d</display-octave></unpitched><duration>%d</duration>" % (step, octv, dur)+ s += ('<tie type="stop"/>' if tie_stop else "") + ('<tie type="start"/>' if tie_start else "")+ s += '<instrument id="P1-I%d"/><voice>1</voice><type>%s</type>%s' % (h["gm"], typ, "<dot/>" if dot else "")+ s += "<notehead>%s</notehead>" % head if head else ""+ if tie_stop or tie_start:+ s += "<notations>%s%s</notations>" % ('<tied type="stop"/>' if tie_stop else "", '<tied type="start"/>' if tie_start else "")+ xs.append(s + "</note>")+ return "".join(xs)+++def musicxml(hits: Sequence[Dict[str, Any]], quant: Sequence[Dict[str, Any]], bpm: float, title: str = "x-ray drums") -> str:+ """A percussion-staff MusicXML 3.1 file: one voice, simultaneous pieces as chords, each hit an eighth or a sixteenth+ (as far as the next hit allows) followed by rests. Standard drum positions (kick F4, snare C5, hats G5 x, crash A5 x,+ toms E5/D5/A4)."""+ by: Dict[int, List[Dict[str, Any]]] = {}+ for h, q in zip(hits, quant):+ by.setdefault(int(round(q["beat"] * DIV)), []).append(h)+ starts = sorted(by)+ n_meas = max(1, int(math.ceil((starts[-1] + 1) / float(MEASURE)))) if starts else 1+ items: List[Tuple[int, int, Optional[List[Dict[str, Any]]], bool, bool]] = []+ pos = 0++ def add(st: int, d: int, group: Optional[List[Dict[str, Any]]]) -> None:+ cuts = []+ while d > 0:+ take = min(d, MEASURE - (st % MEASURE))+ for p in _pieces(take):+ cuts.append((st, p))+ st += p+ d -= take+ for k, (s, p) in enumerate(cuts):+ items.append((s, p, group, group is not None and k > 0, group is not None and k < len(cuts) - 1))++ for k, st in enumerate(starts):+ if st > pos:+ add(pos, st - pos, None)+ d = min(2, starts[k + 1] - st) if k + 1 < len(starts) else 1+ add(st, d, by[st])+ pos = st + d+ if pos < n_meas * MEASURE:+ add(pos, n_meas * MEASURE - pos, None)+ meas = ["" for _ in range(n_meas)]+ for st, d, g, ts, tt in items:+ meas[st // MEASURE] += _hit_xml(g, d, ts, tt) if g else _rest(d)+ used = sorted({(h["gm"], h["piece"]) for h in hits})+ si, mi = [], []+ for gm, piece in used:+ nm = DRUM_NAMES.get(piece, "Tom %d" % gm)+ si.append('<score-instrument id="P1-I%d"><instrument-name>%s</instrument-name></score-instrument>' % (gm, escape(nm)))+ mi.append('<midi-instrument id="P1-I%d"><midi-channel>10</midi-channel><midi-unpitched>%d</midi-unpitched></midi-instrument>' % (gm, gm + 1))+ x = ['<?xml version="1.0" encoding="UTF-8"?>',+ '<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">',+ '<score-partwise version="3.1"><work><work-title>%s</work-title></work>' % escape(title),+ '<identification><encoding><software>showtime xray notes</software></encoding></identification>',+ '<part-list><score-part id="P1"><part-name>Drums</part-name>%s%s</score-part></part-list><part id="P1">' % ("".join(si), "".join(mi))]+ for m in range(n_meas):+ x.append('<measure number="%d">' % (m + 1))+ if m == 0:+ x.append('<attributes><divisions>%d</divisions><key><fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time>'+ '<clef><sign>percussion</sign><line>2</line></clef></attributes>'+ '<direction placement="above"><direction-type><metronome><beat-unit>quarter</beat-unit><per-minute>%d</per-minute>'+ '</metronome></direction-type><sound tempo="%d"/></direction>' % (DIV, round(bpm), round(bpm)))+ x.append(meas[m])+ x.append("</measure>")+ x.append("</part></score-partwise>")+ return "".join(x)+++LANES = ["crash", "hihat_open", "hihat_closed", "tom", "snare", "kick"]+COLORS = {"kick": (80, 160, 255), "snare": (255, 150, 40), "hihat_closed": (120, 220, 140), "hihat_open": (60, 190, 110),+ "tom": (220, 120, 220), "crash": (250, 220, 90)}+++def check_sheet(doc: Dict[str, Any], audio: np.ndarray, t0: float, dest: Path, width: int = 1600) -> Path:+ """The waveform with a tick per hit and one lane per piece."""+ from PIL import Image, ImageDraw+ dur = len(audio) / float(SR)+ t1 = t0 + max(dur, 0.1)+ wave_h, lane_h, pad = 140, 34, 28+ H = pad + wave_h + 8 + lane_h * len(LANES) + 24+ img = Image.new("RGB", (width, H), (18, 20, 26))+ d = ImageDraw.Draw(img)++ def px(t: float) -> int:+ return int(round((t - t0) / (t1 - t0) * (width - 1)))++ mid = pad + wave_h // 2+ seg = max(1, len(audio) // width)+ top = max(1e-6, float(np.max(np.abs(audio))) if len(audio) else 1.0)+ for x in range(width):+ s = audio[x * seg:(x + 1) * seg]+ if len(s):+ a = int(float(np.max(np.abs(s))) * (wave_h / 2 - 2) / top)+ d.line([(x, mid - a), (x, mid + a)], fill=(84, 112, 150))+ y0 = pad + wave_h + 8+ for k, lane in enumerate(LANES):+ d.text((4, y0 + k * lane_h + 4), lane, fill=(120, 128, 150))+ for h in doc["drums"]:+ k = LANES.index(h["piece"])+ x = px(h["t"])+ d.line([(x, pad), (x, pad + wave_h)], fill=(255, 90, 90))+ d.rectangle([x, y0 + k * lane_h + 2, x + 4, y0 + (k + 1) * lane_h - 2], fill=COLORS[h["piece"]])+ d.text((8, 6), "%s: %d hits (classified by sound), %.0f bpm" % (doc["source"]["file"], len(doc["drums"]), doc["tempo"]["bpm"]), fill=(230, 232, 240))+ for s in range(int(math.ceil(t0)), int(t1) + 1):+ d.text((px(s) + 2, H - 16), "%ds" % s, fill=(130, 136, 154))+ dest.parent.mkdir(parents=True, exist_ok=True)+ img.save(str(dest), quality=88)+ return dest+++def analyze(clip: Any, out_dir: Any, *, t_from: float = 0.0, t_to: Optional[float] = None) -> Dict[str, Any]:+ """Write notes.json (with "drums"), notes.mid (channel 10), notes.musicxml and check-notes.jpg; returns the notes.json dict."""+ from . import xray+ from .audio import wav+ clip = Path(clip)+ if not clip.is_file():+ raise ShowtimeError("clip not found: %s" % clip, hint="pass the video file you filmed")+ if t_to is not None and t_to <= t_from:+ raise ShowtimeError("--to (%g) must be after --from (%g)" % (t_to, t_from))+ out = Path(out_dir)+ src = xray._source_info(clip)+ if not src.pop("has_audio"):+ raise ShowtimeError("%s has no sound to read hits from" % clip.name, hint="x-ray reads the hits from the clip's audio")+ out.mkdir(parents=True, exist_ok=True)+ t_from = max(0.0, t_from)+ x = np.asarray(wav.load(clip, sr=SR, mono=True, offset=t_from, duration=(t_to - t_from) if t_to is not None else None), dtype=np.float32)+ hits = detect_hits(x, t_from, t_to)+ fps = float(src["fps"])+ drums = []+ for i, h in enumerate(hits):+ d = {"i": i, "t": round(h["t"], 4), "frame": xray.frame_of(h["t"], fps), "piece": h["piece"], "gm": h["gm"], "velocity": h["velocity"]}+ if "hz" in h:+ d["hz"] = h["hz"]+ drums.append(d)+ onset_t = sorted({d["t"] for d in drums})+ tm = xm.tempo_map(onset_t, grid=0.25)+ quant = [{"i": d["i"], "beat": max(0.0, round(xm.beat_of(d["t"], tm["map"]) * 4.0) / 4.0), "dur_beats": 0.25} for d in drums]+ doc: Dict[str, Any] = {+ "schema": SCHEMA, "source": src, "engine": ENGINE, "instrument": "drums",+ "note": "a classifier of sound types (kick, snare, hats, tom, crash by spectrum), not a transcription of a known kit",+ "range": {"from": t_from, "to": t_to}, "notes": [], "drums": drums, "tempo": tm, "quantized": quant}+ write_json(out / "notes.json", doc)+ midi = [{"t": d["t"], "end": d["t"] + 0.1, "pitch": d["gm"], "velocity": d["velocity"]} for d in drums]+ (out / "notes.mid").write_bytes(xm.midi_bytes(midi, tm["bpm"], channel=9))+ (out / "notes.musicxml").write_text(musicxml(drums, quant, tm["bpm"], clip.stem), encoding="utf-8")+ check_sheet(doc, x, t_from, out / "check-notes.jpg")+ return docdiff --git a/skills/showtime/lib/st/xray_music.py b/skills/showtime/lib/st/xray_music.pynew file mode 100644index 0000000..53803fd--- /dev/null+++ b/skills/showtime/lib/st/xray_music.py@@ -0,0 +1,759 @@+"""X-ray music: the notes a filmed instrument plays (`showtime xray notes`).++Pitch model: Spotify's basic-pitch (Apache-2.0) as a 230 KB ONNX graph (assets/xray/nmp.onnx; its constant-Q+transform is inside the graph), run with onnxruntime: no TensorFlow. The windowing of the audio and the note+creation (model output -> note events) are numpy ports of basic_pitch.inference.run_inference and+basic_pitch.note_creation (see assets/xray/NOTICE); pitch bends are not ported.++On top of the notes: a smooth tempo map from the inter-onset intervals, eighth-note quantization, key+(Krumhansl-Schmuckler), chords, melody and bass voices, a guitar string/fret mapping, MIDI and MusicXML writers+(plain Python) and a check picture (piano roll over the waveform). Writes <job>/xray/notes.json (schema+showtime.xray.notes/1), notes.mid, notes.musicxml and check-notes.jpg.+"""+from __future__ import annotations++import math+import struct+from pathlib import Path+from typing import Any, Dict, List, Optional, Sequence, Tuple+from xml.sax.saxutils import escape++import numpy as np++from .common import ShowtimeError, write_json++SCHEMA = "showtime.xray.notes/1"+ENGINE = "basic-pitch-onnx"+MODEL = Path(__file__).resolve().parent / "assets" / "xray" / "nmp.onnx"++# basic_pitch.constants+SR = 22050+FFT_HOP = 256+ANNOT_FPS = SR // FFT_HOP # 86+ANNOT_N_FRAMES = ANNOT_FPS * 2 # 172+AUDIO_N_SAMPLES = SR * 2 - FFT_HOP # 43844+N_OVERLAP_FRAMES = 30+MIDI_OFFSET = 21+N_PITCHES = 88+MAX_FREQ_IDX = 87+FRAME_S = FFT_HOP / float(SR) # 11.6 ms++ONSET_THRESH = 0.5+FRAME_THRESH = 0.3+MIN_NOTE_LEN = 11 # frames (127.70 ms)+ENERGY_TOL = 11++NAMES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]+KEY_NAMES_MAJOR = ["C", "Db", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"]+KEY_NAMES_MINOR = ["C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B"]+CHORD_NAMES = ["C", "Db", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"]+KS_MAJOR = [6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88]+KS_MINOR = [6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17]+CHORD_TEMPLATES = [("", (0, 4, 7)), ("m", (0, 3, 7)), ("7", (0, 4, 7, 10)), ("dim", (0, 3, 6))]++MELODY_MIN_PITCH = 64 # E4+MELODY_MIN_CONF = 0.4+BASS_MIN_CONF = 0.3+CLUSTER_S = 0.035 # notes starting this close are one onset+GUITAR_OPEN = [64, 59, 55, 50, 45, 40] # strings 1..6: E4 B3 G3 D3 A2 E2+GUITAR_FRETS = 19+INSTRUMENTS = ("auto", "guitar", "piano", "musicbox", "drums")+++def note_name(p: int) -> str:+ return "%s%d" % (NAMES[p % 12], p // 12 - 1)+++# ------------------------------------------------------------------ audio and model++def load_audio(clip: Any, t_from: float = 0.0, t_to: Optional[float] = None) -> np.ndarray:+ """Mono float32 at 22050 Hz of the clip (or of --from..--to), decoded with showtime's own ffmpeg."""+ from .audio import wav+ dur = (t_to - t_from) if t_to is not None else None+ return np.asarray(wav.load(clip, sr=SR, mono=True, offset=max(0.0, t_from), duration=dur), dtype=np.float32)+++def _session() -> Any:+ try:+ import onnxruntime as ort+ except ImportError as e:+ raise ShowtimeError("onnxruntime is not installed, so the notes cannot be read (%s)" % e,+ hint="install showtime's environment (`showtime doctor --fix`) or `pip install onnxruntime` into it")+ if not MODEL.is_file():+ raise ShowtimeError("the note model is missing: %s" % MODEL, hint="reinstall showtime (assets/xray/nmp.onnx ships with it)")+ so = ort.SessionOptions()+ so.log_severity_level = 3+ return ort.InferenceSession(str(MODEL), sess_options=so, providers=["CPUExecutionProvider"])+++def infer(audio: np.ndarray) -> Dict[str, np.ndarray]:+ """basic_pitch.inference.run_inference on a 22050 Hz mono signal: 2 s windows with 30 frames of overlap, the+ overlap trimmed, the pieces joined. Returns {"note": (frames, 88), "onset": (frames, 88)}."""+ sess = _session()+ in_name = sess.get_inputs()[0].name+ names = [o.name for o in sess.get_outputs()]+ # the graph's outputs are named after the TF signature: note=:1, onset=:2, contour=:0+ want = {"note": "StatefulPartitionedCall:1", "onset": "StatefulPartitionedCall:2"}+ for k, v in want.items():+ if v not in names:+ raise ShowtimeError("unexpected note model (outputs %s)" % names, hint="use the nmp.onnx that ships with showtime")+ overlap_len = N_OVERLAP_FRAMES * FFT_HOP+ hop = AUDIO_N_SAMPLES - overlap_len+ x = np.asarray(audio, dtype=np.float32).reshape(-1)+ n_orig = int(x.shape[0])+ if n_orig == 0:+ raise ShowtimeError("no audio to read the notes from")+ x = np.concatenate([np.zeros(overlap_len // 2, dtype=np.float32), x])+ outs: Dict[str, List[np.ndarray]] = {"note": [], "onset": []}+ for i in range(0, x.shape[0], hop):+ w = x[i:i + AUDIO_N_SAMPLES]+ if len(w) < AUDIO_N_SAMPLES:+ w = np.pad(w, (0, AUDIO_N_SAMPLES - len(w)))+ res = sess.run([want["note"], want["onset"]], {in_name: w.reshape(1, -1, 1)})+ outs["note"].append(res[0])+ outs["onset"].append(res[1])+ n_olap = N_OVERLAP_FRAMES // 2+ n_keep = int(math.floor(n_orig * (ANNOT_FPS / float(SR))))+ if n_keep <= 0:+ raise ShowtimeError("no usable audio in the clip (under about 12 ms of sound)", hint="x-ray reads the notes from the clip's sound: pass a clip with audio")+ out = {}+ for k, parts in outs.items():+ o = np.concatenate(parts)[:, n_olap:-n_olap, :]+ out[k] = o.reshape(o.shape[0] * o.shape[1], o.shape[2])[:n_keep, :]+ return out+++# ------------------------------------------------------------------ note creation (port of basic_pitch.note_creation)++def _inferred_onsets(onsets: np.ndarray, frames: np.ndarray, n_diff: int = 2) -> np.ndarray:+ diffs = []+ for n in range(1, n_diff + 1):+ app = np.concatenate([np.zeros((n, frames.shape[1])), frames])+ diffs.append(app[n:, :] - app[:-n, :])+ d = np.min(diffs, axis=0)+ d[d < 0] = 0+ d[:n_diff, :] = 0+ mx = np.max(d)+ d = np.max(onsets) * d / mx if mx > 0 else d+ return np.max([onsets, d], axis=0)+++def frame_times(n_frames: int) -> np.ndarray:+ """basic_pitch.note_creation.model_frames_to_time: frame index -> seconds (with its per-window drift correction)."""+ t = np.arange(n_frames) * FFT_HOP / float(SR)+ win = np.floor(np.arange(n_frames) / ANNOT_N_FRAMES)+ offset = FRAME_S * (ANNOT_N_FRAMES - AUDIO_N_SAMPLES / float(FFT_HOP)) + 0.0018+ return t - offset * win+++def notes_from_output(frames: np.ndarray, onsets: np.ndarray, *, onset_thresh: float = ONSET_THRESH,+ frame_thresh: float = FRAME_THRESH, min_note_len: int = MIN_NOTE_LEN, infer_onsets: bool = True,+ melodia_trick: bool = True, energy_tol: int = ENERGY_TOL) -> List[Tuple[int, int, int, float]]:+ """basic_pitch output_to_notes_polyphonic: [(start_frame, end_frame, midi, amplitude)] (no frequency limits)."""+ from scipy import signal+ frames = np.array(frames, dtype=np.float64)+ onsets = np.array(onsets, dtype=np.float64)+ n_frames = frames.shape[0]+ if infer_onsets:+ onsets = _inferred_onsets(onsets, frames)+ peak = np.zeros(onsets.shape)+ pk = signal.argrelmax(onsets, axis=0)+ peak[pk] = onsets[pk]+ oi = np.where(peak >= onset_thresh)+ o_t = oi[0][::-1]+ o_f = oi[1][::-1]+ rem = frames.copy()+ events: List[Tuple[int, int, int, float]] = []+ for start, fi in zip(o_t, o_f):+ if start >= n_frames - 1:+ continue+ i = start + 1+ k = 0+ while i < n_frames - 1 and k < energy_tol:+ k = k + 1 if rem[i, fi] < frame_thresh else 0+ i += 1+ i -= k+ if i - start <= min_note_len:+ continue+ rem[start:i, fi] = 0+ if fi < MAX_FREQ_IDX:+ rem[start:i, fi + 1] = 0+ if fi > 0:+ rem[start:i, fi - 1] = 0+ events.append((int(start), int(i), int(fi) + MIDI_OFFSET, float(np.mean(frames[start:i, fi]))))+ if melodia_trick:+ shape = rem.shape+ while np.max(rem) > frame_thresh:+ i_mid, fi = np.unravel_index(np.argmax(rem), shape)+ rem[i_mid, fi] = 0+ i = i_mid + 1+ k = 0+ while i < n_frames - 1 and k < energy_tol:+ k = k + 1 if rem[i, fi] < frame_thresh else 0+ rem[i, fi] = 0+ if fi < MAX_FREQ_IDX:+ rem[i, fi + 1] = 0+ if fi > 0:+ rem[i, fi - 1] = 0+ i += 1+ i_end = i - 1 - k+ i = i_mid - 1+ k = 0+ while i > 0 and k < energy_tol:+ k = k + 1 if rem[i, fi] < frame_thresh else 0+ rem[i, fi] = 0+ if fi < MAX_FREQ_IDX:+ rem[i, fi + 1] = 0+ if fi > 0:+ rem[i, fi - 1] = 0+ i -= 1+ i_start = i + 1 + k+ if i_end - i_start <= min_note_len:+ continue+ events.append((int(i_start), int(i_end), int(fi) + MIDI_OFFSET, float(np.mean(frames[i_start:i_end, fi]))))+ return events+++def detect_notes(audio: np.ndarray, t_offset: float = 0.0) -> List[Dict[str, Any]]:+ """Audio -> notes [{t, end, pitch, velocity, confidence}] sorted by time (times + t_offset)."""+ out = infer(audio)+ ev = notes_from_output(out["note"], out["onset"])+ times = frame_times(out["note"].shape[0])+ onset = out["onset"]+ notes = []+ for s, e, p, amp in ev:+ lo, hi = max(0, s - 2), min(onset.shape[0], s + 3)+ conf = float(np.max(onset[lo:hi, p - MIDI_OFFSET])) if hi > lo else 0.0+ notes.append({"t": float(times[s]) + t_offset, "end": float(times[min(e, len(times) - 1)]) + t_offset, "pitch": p,+ "velocity": float(min(1.0, max(0.0, amp))), "confidence": float(min(1.0, max(0.0, conf)))})+ notes.sort(key=lambda n: (n["t"], n["pitch"]))+ return notes+++# ------------------------------------------------------------------ voices++def clusters(notes: Sequence[Dict[str, Any]], gap: float = CLUSTER_S) -> List[List[int]]:+ """Note indexes grouped by onset: a note joins the group when it starts within `gap` of the group's first note."""+ order = sorted(range(len(notes)), key=lambda i: notes[i]["t"])+ out: List[List[int]] = []+ for i in order:+ if out and notes[i]["t"] - notes[out[-1][0]]["t"] <= gap:+ out[-1].append(i)+ else:+ out.append([i])+ return out+++def voices(notes: Sequence[Dict[str, Any]]) -> Dict[str, List[int]]:+ """melody = at each onset the loudest note from E4 up with confidence >= 0.4; bass = the lowest note under the melody+ (under E4 where the onset has no melody note)."""+ mel: List[int] = []+ bass: List[int] = []+ for grp in clusters(notes):+ cand = [i for i in grp if notes[i]["pitch"] >= MELODY_MIN_PITCH and notes[i]["confidence"] >= MELODY_MIN_CONF]+ top = None+ if cand:+ top = max(cand, key=lambda i: (notes[i]["velocity"], notes[i]["pitch"]))+ mel.append(top)+ ref = notes[top]["pitch"] if top is not None else MELODY_MIN_PITCH+ low = [i for i in grp if notes[i]["pitch"] < ref and notes[i]["confidence"] >= BASS_MIN_CONF]+ if low:+ bass.append(min(low, key=lambda i: (notes[i]["pitch"], -notes[i]["velocity"])))+ return {"melody": sorted(mel), "bass": sorted(bass)}+++# ------------------------------------------------------------------ tempo, quantize++def _onset_times(notes: Sequence[Dict[str, Any]]) -> List[float]:+ return [float(np.mean([notes[i]["t"] for i in g])) for g in clusters(notes)]+++def tempo_map(onsets: Sequence[float], sigma: float = 2.5, grid: float = 0.5) -> Dict[str, Any]:+ """A smooth tempo map: beats from the median inter-onset interval (folded to 60..180 bpm), each onset put on the+ nearest `grid` (half beat; a quarter for drums) after the one before it, then a local weighted linear fit of beat against time (gaussian+ weights, `sigma` seconds) evaluated at the onsets. Never snaps a scroll to the last note: use `beat_of`.+ Returns {"bpm", "map": [[t, beat], ...]} (beat 0 at the first onset; knots at the onsets and 1 s past each end)."""+ t = np.asarray(sorted(onsets), dtype=float)+ if len(t) < 3:+ bpm = 100.0+ t0 = float(t[0]) if len(t) else 0.0+ return {"bpm": bpm, "map": [[round(t0, 4), 0.0], [round(t0 + 60.0 / bpm * 4, 4), 4.0]]}+ ioi = np.diff(t)+ ioi = ioi[(ioi >= 0.06) & (ioi <= 3.0)]+ period = float(np.median(ioi)) if len(ioi) else 0.5+ while period < 60.0 / 180.0:+ period *= 2+ while period > 1.0:+ period /= 2+ slope = np.full(len(t), period) # seconds per beat, per onset+ beats = np.zeros(len(t))+ for _ in range(3):+ beats = np.zeros(len(t))+ for i in range(1, len(t)):+ step = max(grid, round((t[i] - t[i - 1]) / slope[i - 1] / grid) * grid)+ beats[i] = beats[i - 1] + step+ sm = np.zeros(len(t))+ new_slope = np.zeros(len(t))+ for i in range(len(t)):+ w = np.exp(-0.5 * ((t - t[i]) / sigma) ** 2)+ sw = w.sum()+ mt = (w * t).sum() / sw+ mb = (w * beats).sum() / sw+ vt = (w * (t - mt) ** 2).sum()+ b = (w * (t - mt) * (beats - mb)).sum() / vt if vt > 1e-9 else 1.0 / period+ b = b if b > 1e-6 else 1.0 / period+ sm[i] = mb + b * (t[i] - mt)+ new_slope[i] = 1.0 / b+ sm = np.maximum.accumulate(sm)+ slope = np.clip(new_slope, period * 0.5, period * 2.0)+ beats = sm+ beats = beats - beats[0]+ knots_t = list(t)+ knots_b = list(beats)+ knots_t.insert(0, t[0] - 1.0)+ knots_b.insert(0, beats[0] - 1.0 / slope[0])+ knots_t.append(t[-1] + 1.0)+ knots_b.append(beats[-1] + 1.0 / slope[-1])+ # strictly increasing beats (np.interp needs it for the inverse)+ kb = np.maximum.accumulate(np.asarray(knots_b))+ for i in range(1, len(kb)):+ if kb[i] <= kb[i - 1]:+ kb[i] = kb[i - 1] + 1e-4+ bpm = 60.0 / float(np.median(slope))+ return {"bpm": round(bpm, 2), "map": [[round(float(a), 4), round(float(b), 4)] for a, b in zip(knots_t, kb)]}+++def beat_of(t: Any, tmap: Sequence[Sequence[float]]) -> Any:+ """Time (s) -> beat on the smooth map (linear between knots, extrapolated at the ends with the end slopes)."""+ mt = np.array([k[0] for k in tmap], dtype=float)+ mb = np.array([k[1] for k in tmap], dtype=float)+ x = np.asarray(t, dtype=float)+ y = np.interp(x, mt, mb)+ if len(mt) >= 2:+ s0 = (mb[1] - mb[0]) / (mt[1] - mt[0])+ s1 = (mb[-1] - mb[-2]) / (mt[-1] - mt[-2])+ y = np.where(x < mt[0], mb[0] + (x - mt[0]) * s0, y)+ y = np.where(x > mt[-1], mb[-1] + (x - mt[-1]) * s1, y)+ return float(y) if np.ndim(t) == 0 else y+++def time_of(beat: Any, tmap: Sequence[Sequence[float]]) -> Any:+ """Beat -> time (s), the inverse of beat_of."""+ mt = np.array([k[0] for k in tmap], dtype=float)+ mb = np.array([k[1] for k in tmap], dtype=float)+ x = np.asarray(beat, dtype=float)+ y = np.interp(x, mb, mt)+ if len(mt) >= 2:+ s0 = (mt[1] - mt[0]) / (mb[1] - mb[0])+ s1 = (mt[-1] - mt[-2]) / (mb[-1] - mb[-2])+ y = np.where(x < mb[0], mt[0] + (x - mb[0]) * s0, y)+ y = np.where(x > mb[-1], mt[-1] + (x - mb[-1]) * s1, y)+ return float(y) if np.ndim(beat) == 0 else y+++def quantize(notes: Sequence[Dict[str, Any]], tmap: Sequence[Sequence[float]]) -> List[Dict[str, Any]]:+ """Each note's start and length in beats, rounded to eighths (half beats), never shorter than an eighth."""+ out = []+ for i, n in enumerate(notes):+ b0 = beat_of(n["t"], tmap)+ b1 = beat_of(n["end"], tmap)+ beat = max(0.0, round(b0 * 2.0) / 2.0)+ dur = max(0.5, round((b1 - b0) * 2.0) / 2.0)+ out.append({"i": i, "beat": beat, "dur_beats": dur})+ return out+++# ------------------------------------------------------------------ key and chords++def _pc_weights(notes: Sequence[Dict[str, Any]]) -> np.ndarray:+ pcw = np.zeros(12)+ for n in notes:+ pcw[n["pitch"] % 12] += max(0.05, n["end"] - n["t"])+ return pcw+++def detect_key(notes: Sequence[Dict[str, Any]]) -> Optional[str]:+ """Krumhansl-Schmuckler on duration-weighted pitch classes: "A minor", "C major" (None without notes)."""+ pcw = _pc_weights(notes)+ if pcw.sum() <= 0:+ return None+ best = (-2.0, "")+ for mode, prof, names in (("major", KS_MAJOR, KEY_NAMES_MAJOR), ("minor", KS_MINOR, KEY_NAMES_MINOR)):+ p = np.asarray(prof)+ for tonic in range(12):+ r = _corr(pcw, np.roll(p, tonic))+ if r > best[0]:+ best = (r, "%s %s" % (names[tonic], mode))+ return best[1]+++def _corr(a: np.ndarray, b: np.ndarray) -> float:+ a = a - a.mean()+ b = b - b.mean()+ d = math.sqrt(float((a * a).sum() * (b * b).sum()))+ return float((a * b).sum() / d) if d > 0 else 0.0+++def chords(notes: Sequence[Dict[str, Any]], tmap: Sequence[Sequence[float]], per_beats: float = 1.0) -> List[Dict[str, Any]]:+ """Per beat: the template (major, minor, 7, dim) best matching the pitch-class energy of the notes sounding in it;+ equal neighbours merge. A beat with fewer than two pitch classes of substance has no chord."""+ if not notes:+ return []+ b_end = max(beat_of(n["end"], tmap) for n in notes)+ b_start = min(beat_of(n["t"], tmap) for n in notes)+ nb = beat_of([n["t"] for n in notes], tmap)+ ne = beat_of([n["end"] for n in notes], tmap)+ out: List[Dict[str, Any]] = []+ b = math.floor(max(0.0, b_start) / per_beats) * per_beats+ while b < b_end:+ e = np.zeros(12)+ for k, n in enumerate(notes):+ ov = min(ne[k], b + per_beats) - max(nb[k], b)+ if ov > 0:+ e[n["pitch"] % 12] += ov * (0.3 + n["velocity"])+ name = None+ if e.sum() > 0:+ strong = (e >= 0.15 * e.max()).sum()+ if strong >= 2:+ best = (-1.0, None)+ for root in range(12):+ for suffix, tpl in CHORD_TEMPLATES:+ v = np.zeros(12)+ for s in tpl:+ v[(root + s) % 12] = 1.0+ sc = float(v @ e) / (math.sqrt(float(v @ v)) * math.sqrt(float(e @ e))) - (0.04 if suffix in ("7", "dim") else 0.0)+ if sc > best[0]:+ best = (sc, CHORD_NAMES[root] + suffix)+ name = best[1]+ if name:+ t0, t1 = float(time_of(b, tmap)), float(time_of(b + per_beats, tmap))+ if out and out[-1]["name"] == name and abs(out[-1]["end"] - t0) < 1e-6:+ out[-1]["end"] = round(t1, 4)+ else:+ out.append({"t": round(t0, 4), "end": round(t1, 4), "name": name})+ b += per_beats+ return out+++# ------------------------------------------------------------------ guitar++def guitar_map(notes: Sequence[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """Standard tuning E2 A2 D3 G3 B3 E4: each note on the lowest fret that fits, open position (frets 0-4) first, and+ not on a string that is still ringing another note. Right hand p for strings 6-4, i/m/a for 3/2/1; `lh` is the+ left-hand finger (0 open, 1-4 from the hand's lowest fret in the neighbourhood). Notes outside the neck are left out."""+ busy = [-1.0] * 7 # string -> time its last note ends+ rows: List[Dict[str, Any]] = []+ for i, n in enumerate(notes):+ cands = [(s + 1, n["pitch"] - GUITAR_OPEN[s]) for s in range(6) if 0 <= n["pitch"] - GUITAR_OPEN[s] <= GUITAR_FRETS]+ if not cands:+ continue+ free = [c for c in cands if busy[c[0]] <= n["t"] + 0.02]+ pool = free or cands+ open_pos = [c for c in pool if c[1] <= 4]+ pool = open_pos or pool+ string, fret = min(pool, key=lambda c: (c[1], c[0]))+ busy[string] = n["end"]+ rows.append({"i": i, "string": string, "fret": fret, "finger": "p" if string >= 4 else {3: "i", 2: "m", 1: "a"}[string]})+ for k, r in enumerate(rows):+ near = [q["fret"] for q in rows[max(0, k - 3):k + 4] if q["fret"] > 0]+ base = min(near) if near else 1+ r["lh"] = 0 if r["fret"] == 0 else int(min(4, max(1, r["fret"] - base + 1)))+ return rows+++def guess_instrument(notes: Sequence[Dict[str, Any]]) -> str:+ """auto: a music box lives in the high register (median pitch from C5 up); everything else reads as piano.+ A guitar is only chosen when asked for."""+ if not notes:+ return "piano"+ med = float(np.median([n["pitch"] for n in notes]))+ return "musicbox" if med >= 72 else "piano"+++# ------------------------------------------------------------------ MIDI++def _vlq(n: int) -> bytes:+ out = [n & 0x7F]+ n >>= 7+ while n:+ out.append((n & 0x7F) | 0x80)+ n >>= 7+ return bytes(reversed(out))+++def midi_bytes(notes: Sequence[Dict[str, Any]], bpm: float, ppq: int = 480, channel: int = 0) -> bytes:+ """A format-0 standard MIDI file: one tempo, notes at their real times (ticks = seconds * bpm / 60 * ppq)."""+ bpm = max(20.0, float(bpm))+ k = bpm / 60.0 * ppq+ ev: List[Tuple[int, int, bytes]] = []+ for n in notes:+ a = int(round(n["t"] * k))+ b = max(a + 1, int(round(n["end"] * k)))+ vel = int(min(127, max(1, round(n["velocity"] * 127))))+ ev.append((a, 1, bytes([0x90 | channel, n["pitch"] & 0x7F, vel])))+ ev.append((b, 0, bytes([0x80 | channel, n["pitch"] & 0x7F, 0])))+ ev.sort(key=lambda e: (e[0], e[1]))+ trk = bytearray()+ trk += b"\x00\xff\x51\x03" + struct.pack(">I", int(round(60000000.0 / bpm)))[1:]+ trk += b"\x00\xff\x58\x04\x04\x02\x18\x08"+ last = 0+ for tick, _, data in ev:+ trk += _vlq(tick - last) + data+ last = tick+ trk += b"\x00\xff\x2f\x00"+ return b"MThd" + struct.pack(">IHHH", 6, 0, 1, ppq) + b"MTrk" + struct.pack(">I", len(trk)) + bytes(trk)+++def write_midi(path: Any, notes: Sequence[Dict[str, Any]], bpm: float) -> Path:+ p = Path(path)+ p.write_bytes(midi_bytes(notes, bpm))+ return p+++# ------------------------------------------------------------------ MusicXML++DIV = 2 # divisions per quarter: an eighth is 1+MEASURE = 4 * DIV+_TYPES = {1: ("eighth", False), 2: ("quarter", False), 3: ("quarter", True), 4: ("half", False), 6: ("half", True),+ 8: ("whole", False)}+_STEP = {0: ("C", 0), 1: ("C", 1), 2: ("D", 0), 3: ("D", 1), 4: ("E", 0), 5: ("F", 0), 6: ("F", 1), 7: ("G", 0), 8: ("G", 1),+ 9: ("A", 0), 10: ("A", 1), 11: ("B", 0)}+_FIFTHS_MAJOR = {"C": 0, "G": 1, "D": 2, "A": 3, "E": 4, "B": 5, "F#": 6, "Db": -5, "Ab": -4, "Eb": -3, "Bb": -2, "F": -1}+++def key_fifths(key: Optional[str]) -> int:+ if not key:+ return 0+ name, mode = key.split()+ if mode == "major":+ return _FIFTHS_MAJOR.get(name, 0)+ names = KEY_NAMES_MAJOR+ try:+ rel = names[(KEY_NAMES_MINOR.index(name) + 3) % 12]+ except ValueError:+ return 0+ return _FIFTHS_MAJOR.get(rel, 0)+++def _pieces(d: int) -> List[int]:+ out = []+ for p in (8, 6, 4, 3, 2, 1):+ while d >= p:+ out.append(p)+ d -= p+ return out+++def _note_xml(pitches: Sequence[int], dur: int, staff: int, tie_stop: bool, tie_start: bool, two_staves: bool) -> str:+ typ, dot = _TYPES[dur]+ xs = []+ for k, p in enumerate(pitches):+ step, alter = _STEP[p % 12]+ s = "<note>"+ if k:+ s += "<chord/>"+ s += "<pitch><step>%s</step>%s<octave>%d</octave></pitch>" % (step, "<alter>%d</alter>" % alter if alter else "", p // 12 - 1)+ s += "<duration>%d</duration>" % dur+ if tie_stop:+ s += '<tie type="stop"/>'+ if tie_start:+ s += '<tie type="start"/>'+ s += "<voice>%d</voice><type>%s</type>%s" % (staff, typ, "<dot/>" if dot else "")+ if two_staves:+ s += "<staff>%d</staff>" % staff+ if tie_stop or tie_start:+ s += "<notations>%s%s</notations>" % ('<tied type="stop"/>' if tie_stop else "", '<tied type="start"/>' if tie_start else "")+ xs.append(s + "</note>")+ return "".join(xs)+++def _rest_xml(dur: int, staff: int, two_staves: bool) -> str:+ typ, dot = _TYPES[dur]+ return "<note><rest/><duration>%d</duration><voice>%d</voice><type>%s</type>%s%s</note>" % (+ dur, staff, typ, "<dot/>" if dot else "", "<staff>%d</staff>" % staff if two_staves else "")+++def _staff_events(notes: Sequence[Dict[str, Any]], quant: Sequence[Dict[str, Any]], idxs: Sequence[int]) -> List[Tuple[int, int, List[int]]]:+ """[(start, duration, [pitches])] in divisions, one entry per onset, each cut at the next onset."""+ by: Dict[int, Dict[int, int]] = {}+ for i in idxs:+ q = quant[i]+ st = int(round(q["beat"] * DIV))+ d = max(1, int(round(q["dur_beats"] * DIV)))+ slot = by.setdefault(st, {})+ slot[notes[i]["pitch"]] = max(slot.get(notes[i]["pitch"], 0), d)+ starts = sorted(by)+ ev = []+ for k, st in enumerate(starts):+ d = min(by[st].values())+ if k + 1 < len(starts):+ d = min(d, starts[k + 1] - st)+ ev.append((st, max(1, d), sorted(by[st])))+ return ev+++def _staff_measures(ev: Sequence[Tuple[int, int, List[int]]], n_meas: int, staff: int, two: bool) -> List[str]:+ """XML of each measure for one staff: notes (tied across bar lines and into unwritable lengths) and rests."""+ items: List[Tuple[int, int, Optional[List[int]], bool, bool]] = [] # start, dur, pitches, tie_stop, tie_start+ pos = 0++ def add(st: int, d: int, pitches: Optional[List[int]]) -> None:+ cuts = []+ while d > 0:+ room = MEASURE - (st % MEASURE)+ take = min(d, room)+ for p in _pieces(take):+ cuts.append((st, p))+ st += p+ d -= take+ for k, (s, p) in enumerate(cuts):+ items.append((s, p, pitches, pitches is not None and k > 0, pitches is not None and k < len(cuts) - 1))++ for st, d, ps in ev:+ if st > pos:+ add(pos, st - pos, None)+ add(st, d, ps)+ pos = st + d+ if pos < n_meas * MEASURE:+ add(pos, n_meas * MEASURE - pos, None)+ meas = ["" for _ in range(n_meas)]+ for st, d, ps, ts, tt in items:+ m = st // MEASURE+ if m < n_meas:+ meas[m] += _note_xml(ps, d, staff, ts, tt, two) if ps else _rest_xml(d, staff, two)+ return meas+++def musicxml(notes: Sequence[Dict[str, Any]], quant: Sequence[Dict[str, Any]], bpm: float, key: Optional[str],+ bass: Sequence[int] = (), two_staves: bool = False, title: str = "x-ray notes") -> str:+ """A valid score-partwise MusicXML 3.1 file, one part. With `two_staves` (piano) the `bass` notes go to the lower staff."""+ bset = set(bass) if two_staves else set()+ upper = [i for i in range(len(notes)) if i not in bset]+ lower = sorted(bset)+ ends = [int(round((q["beat"] + q["dur_beats"]) * DIV)) for q in quant] or [MEASURE]+ n_meas = max(1, int(math.ceil(max(ends) / float(MEASURE))))+ up = _staff_measures(_staff_events(notes, quant, upper), n_meas, 1, two_staves)+ lo = _staff_measures(_staff_events(notes, quant, lower), n_meas, 2, True) if two_staves else None+ x = ['<?xml version="1.0" encoding="UTF-8"?>',+ '<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">',+ '<score-partwise version="3.1"><work><work-title>%s</work-title></work>' % escape(title),+ '<identification><encoding><software>showtime xray notes</software></encoding></identification>',+ '<part-list><score-part id="P1"><part-name>%s</part-name></score-part></part-list><part id="P1">' % ("Piano" if two_staves else "Notes")]+ for m in range(n_meas):+ x.append('<measure number="%d">' % (m + 1))+ if m == 0:+ x.append('<attributes><divisions>%d</divisions><key><fifths>%d</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time>'+ % (DIV, key_fifths(key)))+ if two_staves:+ x.append('<staves>2</staves><clef number="1"><sign>G</sign><line>2</line></clef><clef number="2"><sign>F</sign><line>4</line></clef>')+ else:+ x.append('<clef><sign>G</sign><line>2</line></clef>')+ x.append('</attributes><direction placement="above"><direction-type><metronome><beat-unit>quarter</beat-unit>'+ '<per-minute>%d</per-minute></metronome></direction-type><sound tempo="%d"/></direction>' % (round(bpm), round(bpm)))+ x.append(up[m])+ if lo is not None:+ x.append("<backup><duration>%d</duration></backup>" % MEASURE)+ x.append(lo[m])+ x.append("</measure>")+ x.append("</part></score-partwise>")+ return "".join(x)+++# ------------------------------------------------------------------ check picture++def check_sheet(doc: Dict[str, Any], audio: np.ndarray, t0: float, dest: Path, width: int = 1600) -> Path:+ """A piano roll over the waveform: one bar per note (melody in orange, bass in blue, the rest grey), a tick at each onset."""+ from PIL import Image, ImageDraw+ notes = doc["notes"]+ dur = len(audio) / float(SR)+ t1 = t0 + max(dur, 0.1)+ pitches = [n["pitch"] for n in notes] or [60]+ lo, hi = max(21, min(pitches) - 2), min(108, max(pitches) + 2)+ wave_h, roll_h, pad = 140, max(240, (hi - lo + 1) * 9), 28+ H = pad + wave_h + 8 + roll_h + 24+ img = Image.new("RGB", (width, H), (18, 20, 26))+ d = ImageDraw.Draw(img)++ def px(t: float) -> int:+ return int(round((t - t0) / (t1 - t0) * (width - 1)))++ mid = pad + wave_h // 2+ n_px = width+ seg = max(1, len(audio) // n_px)+ for x in range(n_px):+ s = audio[x * seg:(x + 1) * seg]+ if len(s):+ a = int(float(np.max(np.abs(s))) * (wave_h / 2 - 2) / max(1e-6, float(np.max(np.abs(audio))) if len(audio) else 1.0))+ d.line([(x, mid - a), (x, mid + a)], fill=(84, 112, 150))+ top = pad + wave_h + 8+ row = roll_h / float(hi - lo + 1)+ for p in range(lo, hi + 1):+ y = top + (hi - p) * row+ if p % 12 == 0:+ d.line([(0, y + row), (width, y + row)], fill=(46, 50, 62))+ d.text((3, y + row - 11), note_name(p), fill=(120, 128, 150))+ mel = set(doc["voices"]["melody"])+ bas = set(doc["voices"]["bass"])+ for n in notes:+ col = (255, 150, 40) if n["i"] in mel else (80, 160, 255) if n["i"] in bas else (150, 156, 170)+ x0, x1 = px(n["t"]), max(px(n["t"]) + 2, px(n["end"]))+ y = top + (hi - n["pitch"]) * row+ d.rectangle([x0, y, x1, y + max(2, row - 1)], fill=col)+ d.line([(x0, pad), (x0, pad + wave_h)], fill=(255, 90, 90))+ d.text((8, 6), "%s: %d notes, %s, %.0f bpm, key %s" % (doc["source"]["file"], len(notes), doc.get("instrument", ""),+ doc["tempo"]["bpm"], doc.get("key") or "-"), fill=(230, 232, 240))+ for s in range(int(math.ceil(t0)), int(t1) + 1):+ d.text((px(s) + 2, H - 16), "%ds" % s, fill=(130, 136, 154))+ dest.parent.mkdir(parents=True, exist_ok=True)+ img.save(str(dest), quality=88)+ return dest+++# ------------------------------------------------------------------ the command++def analyze(clip: Any, out_dir: Any, *, t_from: float = 0.0, t_to: Optional[float] = None, instrument: str = "auto") -> Dict[str, Any]:+ """Write notes.json, notes.mid, notes.musicxml and check-notes.jpg to `out_dir`; returns the notes.json dict."""+ from . import xray+ clip = Path(clip)+ if instrument not in INSTRUMENTS:+ raise ShowtimeError("--instrument is one of %s (got %r)" % ("|".join(INSTRUMENTS), instrument))+ if not clip.is_file():+ raise ShowtimeError("clip not found: %s" % clip, hint="pass the video file you filmed")+ if t_to is not None and t_to <= t_from:+ raise ShowtimeError("--to (%g) must be after --from (%g)" % (t_to, t_from))+ if instrument == "drums":+ from . import xray_drums+ return xray_drums.analyze(clip, out_dir, t_from=t_from, t_to=t_to)+ out = Path(out_dir)+ src = xray._source_info(clip)+ if not src.pop("has_audio"):+ raise ShowtimeError("%s has no sound to read notes from" % clip.name, hint="x-ray reads the notes from the clip's audio")+ out.mkdir(parents=True, exist_ok=True)+ t_from = max(0.0, t_from)+ audio = load_audio(clip, t_from, t_to)+ notes = detect_notes(audio, t_from)+ if instrument == "auto":+ instrument = guess_instrument(notes)+ fps = float(src["fps"])+ for i, n in enumerate(notes):+ n.update({"i": i, "frame": xray.frame_of(n["t"], fps), "name": note_name(n["pitch"])})+ onsets = _onset_times(notes)+ tm = tempo_map(onsets)+ quant = quantize(notes, tm["map"])+ vo = voices(notes)+ key = detect_key(notes)+ rows = [{"i": n["i"], "t": round(n["t"], 4), "end": round(n["end"], 4), "frame": n["frame"], "pitch": n["pitch"],+ "name": n["name"], "velocity": round(n["velocity"], 3), "confidence": round(n["confidence"], 3)} for n in notes]+ doc: Dict[str, Any] = {+ "schema": SCHEMA, "source": src, "engine": ENGINE, "instrument": instrument,+ "range": {"from": t_from, "to": t_to}, "notes": rows,+ "tempo": tm, "key": key, "chords": chords(notes, tm["map"]), "voices": vo,+ "quantized": [{"i": q["i"], "beat": q["beat"], "dur_beats": q["dur_beats"]} for q in quant]}+ if instrument == "guitar":+ doc["guitar"] = guitar_map(notes)+ write_json(out / "notes.json", doc)+ write_midi(out / "notes.mid", notes, tm["bpm"])+ xml = musicxml(notes, quant, tm["bpm"], key, vo["bass"], two_staves=(instrument == "piano"), title=clip.stem)+ (out / "notes.musicxml").write_text(xml, encoding="utf-8")+ check_sheet(doc, audio, t_from, out / "check-notes.jpg")+ return docdiff --git a/skills/showtime/library/xray/README.md b/skills/showtime/library/xray/README.mdnew file mode 100644index 0000000..8655bfa--- /dev/null+++ b/skills/showtime/library/xray/README.md@@ -0,0 +1,16 @@+# The X-ray mechanism library++One folder per kind of object, each a drawing of its inside that the `xray` template moves on the events measured+from a clip (`showtime new xray <dir> --mechanism <object>`):++- `mechanism.js`: `XRAY.mechanism({name, noun, one, box, anchors, outline, headline, measured, drawn, draw})`.+ `box` and every point are in the mechanism's own design space; `anchors` are four named points a clip places+ on its object (`showtime.json` `"xray".place`); `draw(k)` draws one frame with the kit `xray.js` hands it:+ `k.seg/ell/poly/path/dot/label`, `k.env(i)` (0..1, peaking on event i's frame), `k.cur`, `k.s`, `k.hot`,+ `k.ch` (the character the clip shows for it), `k.n` (events so far), `k.since(i)` (frames), `k.view`+ (`panel` or `inplace`).+- `diagram.js` (optional): the same object as an isometric living diagram for `--layout diagram`.+- `README.md`: the parts, what each event moves, what is measured and what is drawn, the clips it was made on.++Every entry was made on a real clip and verified (`showtime xray verify`: every event on its frame). A new one+starts as a copy of the closest entry; add it with the clip it was verified on and its receipt line.diff --git a/skills/showtime/library/xray/drums/README.md b/skills/showtime/library/xray/drums/README.mdnew file mode 100644index 0000000..7fed6d7--- /dev/null+++ b/skills/showtime/library/xray/drums/README.md@@ -0,0 +1,26 @@+# Drum kit (drum part and kit from above)++Parts: a percussion staff (percussion clef, the usual drum key), a fixed playhead, and a kit seen from above,+the drummer's seat at the bottom: kick (shell, batter head, pedal and beater), snare (with its wires), hi-hat,+two mounted toms, floor tom, crash and ride.++- An event = a hit: its pieces are written at the playhead, red on the event's frame: kick in the bottom space+ (stem down), floor tom, snare in the third space, mid and high tom above it (stems up), hi-hat x above the+ staff (with an o when open), ride x on the top line, crash x on the ledger line above; a beat's hits share a+ beam (a second beam for sixteenths). On the kit, each piece hit flashes and its skin ripples out from the+ centre, bigger for a louder hit; a cymbal sways and rings down; the kick's beater swings into the head.+ Every hit also flashes the playhead.+- The staff scrolls on the smooth tempo map (`k.beat`), hits on their quantized sixteenth.+- Which piece: `notes.json` `drums[]` (`showtime xray notes <clip> --job <job> --instrument drums`): a classifier+ of sound types (kick, snare, tom, hi-hat closed or open, crash), not a transcription; it does not see the kit.+ Toms are split floor, mid and high by their pitch against the clip's other toms. The ride is drawn but the+ sorter never names it.+- Analyze with `--engine flux` and skip (`"skip"`) the events with no sorted hit within 3 frames, so every red+ piece is a hit the sorter placed. `"px_per_beat"` widens the staff for fast playing.+- Split layout only: a drummer fills the frame, never see-through on a person.+- Measured: hit times and count (the clip's sound), the piece of each hit (sorted by its sound), loudness.+ Drawn: a generic kit and the drum part, not this drummer's kit.++Verified on: Lukáš Perný drum solo, KC Dunaj Bratislava 2012 (Wikimedia Commons, CC BY-SA 4.0: https://commons.wikimedia.org/wiki/File:Luk%C3%A1%C5%A1_Pern%C3%BD%2C_drum_solo_%C5%A1ou_sample%2C_KC_Dunaj%2C_Bratislava%2C_2012.webm; a film made from it is CC BY-SA too), 20-34 s, 9:16+dark: 30 of 30 events on their frame · audio 0.0 ms from the original; scroll 8.9 px/frame, 0 stalls and 0+jumps between the scan and the end card.diff --git a/skills/showtime/library/xray/drums/mechanism.js b/skills/showtime/library/xray/drums/mechanism.jsnew file mode 100644index 0000000..de07c08--- /dev/null+++ b/skills/showtime/library/xray/drums/mechanism.js@@ -0,0 +1,219 @@+/* A drum kit, written as it is heard (showtime X-ray mechanism library: drums).+ Design space: 1000 x 440 units (the 9:16 panel, about 0.95 px per unit): a percussion staff on top, a drum kit+ seen from above below it (the drummer's seat at the bottom): kick, snare, hi-hat, two mounted toms, floor tom,+ crash and ride.+ The staff scrolls left past a fixed playhead on the smooth tempo map (k.beat of the frame's time, never a jump).+ Each event is a hit: its pieces (notes.json drums[], joined to the event by the runtime) are written at the+ playhead in the usual drum key (kick in the bottom space, floor tom, snare in the third space, toms above it,+ hi-hat x above the staff with an o when open, crash x on a ledger line), red on the event's frame; on the kit+ each piece hit flashes and its skin ripples out from the centre (a cymbal sways and rings).+ Measured: hit times (xray.json), the piece of each hit sorted by its sound and its loudness (`showtime xray notes+ --instrument drums`: a classifier of sound types, not a transcription; it does not see the kit). Toms are split+ high, mid and floor by their pitch relative to the other toms in the clip. Set in showtime.json "xray":+ "px_per_beat". Without notes.json each hit is a slash on the staff and flashes the whole kit outline. */+(function () {+ var W = 1000, SP = 16, TT = 110, TB = TT + 4 * SP, PLAY = 862, RISE = 3, HX = 70;+ // the drum key: staff step (E4 bottom line = 30), notehead, stem+ var KEY = { kick: [31, 'n', 'd'], tom_floor: [33, 'n', 'u'], snare: [35, 'n', 'u'], tom_mid: [36, 'n', 'u'],+ tom_high: [37, 'n', 'u'], ride: [38, 'x', 'u'], hihat_closed: [39, 'x', 'u'], hihat_open: [39, 'x', 'u'],+ crash: [40, 'x', 'u'] };+ // the kit from above: centre, radius, kind+ var KIT = { crash: [292, 276, 52, 'cym'], ride: [738, 278, 60, 'cym'], hihat: [182, 366, 42, 'cym'],+ tom_high: [440, 278, 33, 'drum'], tom_mid: [568, 278, 36, 'drum'], snare: [326, 392, 40, 'drum'],+ tom_floor: [690, 390, 48, 'drum'], kick: [500, 356, 0, 'kick'] };+ var NAME = { crash: 'crash', ride: 'ride', hihat: 'hi-hat', tom_high: 'tom', tom_mid: 'tom', snare: 'snare', tom_floor: 'floor tom', kick: 'kick' };+ var M = null;++ function yOf(step) { return TB - (step - 30) * SP / 2; }+ function kitOf(p) { return p === 'hihat_closed' || p === 'hihat_open' ? 'hihat' : p; }+ function build(k) {+ var mus = k.music || {}, cfg = k.cfg || {}, q = {}, evOf = {}, raw = mus.drums || [];+ (mus.quantized || []).forEach(function (d) { q[d.i] = d; });+ k.ev.forEach(function (e, ei) { if (e.kind !== 'skip') (e.notes || []).forEach(function (n) { evOf[n.i] = ei; }); });+ var hz = raw.filter(function (d) { return d.piece === 'tom' && d.hz; }).map(function (d) { return d.hz; })+ .filter(function (v, i, a) { return a.indexOf(v) === i; }).sort(function (a, b) { return a - b; });+ var hits = raw.map(function (d, j) {+ var i = d.i != null ? d.i : j, p = d.piece, ev = evOf[i];+ if (p === 'tom') { // high, mid, floor: by pitch against the clip's other toms+ var r = hz.length > 1 && d.hz ? hz.indexOf(d.hz) / (hz.length - 1) : 0.5;+ p = r < 0.34 ? 'tom_floor' : r > 0.66 ? 'tom_high' : 'tom_mid';+ }+ if (!KEY[p]) p = 'snare';+ return { i: i, t: d.t, piece: p, vel: d.velocity == null ? 0.8 : d.velocity, beat: q[i] ? q[i].beat : k.beat(d.t), ev: ev,+ show: ev != null ? k.ev[ev].frame : Math.floor(d.t * k.fps + 1e-9) };+ });+ var cols = {};+ hits.forEach(function (h) { (cols[h.beat] = cols[h.beat] || { beat: h.beat, hits: [] }).hits.push(h); });+ var bpm = (mus.tempo && mus.tempo.bpm) || 120;+ M = { hits: hits, has: raw.length > 0, cols: Object.keys(cols).map(function (b) { return cols[b]; }), bpm: bpm,+ pxb: cfg.px_per_beat || Math.max(96, Math.min(160, 280 / (bpm / 60))) };+ }+ function line(g, x0, y0, x1, y1, col, w) { g.beginPath(); g.moveTo(x0, y0); g.lineTo(x1, y1); g.strokeStyle = col; g.lineWidth = w; g.stroke(); }+ function disc(g, x, y, r, fill) { g.beginPath(); g.arc(x, y, r, 0, Math.PI * 2); g.fillStyle = fill; g.fill(); }+ function ring(g, x, y, rx, ry, col, w) { g.beginPath(); g.ellipse(x, y, Math.max(0.1, rx), Math.max(0.1, ry), 0, 0, Math.PI * 2); g.strokeStyle = col; g.lineWidth = w; g.stroke(); }+ function text(k, s, x, y, px, col, align) {+ var g = k.g; g.save(); g.shadowBlur = 0; g.font = '600 ' + (px / k.scale).toFixed(2) + 'px "IBM Plex Mono"';+ g.fillStyle = col; g.textAlign = align || 'left'; g.textBaseline = 'alphabetic'; g.globalAlpha *= (k.labelAlpha == null ? 1 : k.labelAlpha);+ g.fillText(s, x, y); g.restore();+ }+ function head(g, x, y, kind, col) {+ if (kind === 'x') { line(g, x - 6.5, y - 6.5, x + 6.5, y + 6.5, col, 2.4); line(g, x - 6.5, y + 6.5, x + 6.5, y - 6.5, col, 2.4); return; }+ g.save(); g.translate(x, y); g.rotate(-0.35); g.beginPath(); g.ellipse(0, 0, 9.2, 6.4, 0, 0, Math.PI * 2); g.fillStyle = col; g.fill(); g.restore();+ }++ XRAY.mechanism({+ name: 'drums',+ noun: 'hits', one: 'a hit',+ box: [0, 0, 1000, 440],+ anchors: { snare: [326, 388], kick: [500, 352], crash: [292, 268], ride: [738, 270] },+ outline: [[120, 200], [820, 200], [820, 440], [120, 440]],+ headline: function (n) { return '<em>' + n + '</em> hits,<br>written from<br>the sound.'; },+ measured: function () {+ return 'Each hit lit up on the frame its sound was heard; its piece sorted by that sound, not seen.';+ },+ drawn: 'Drawn: a generic kit and the drum part; toms by relative pitch.',+ // the live page: pads that play the next event of each kind from the real clip+ touch: { verb: 'Hit it', by: 'note' },+ draw: function (k) {+ if (k.view === 'inplace') return;+ if (!M) build(k);+ var g = k.g, C = k.C, f = k.f, now = f / k.fps, bNow = k.beat(now), pxb = M.pxb;+ var o = k.pr(0, 0);+ g.save(); g.translate(o[0], o[1]); g.scale(k.scale, k.scale); g.shadowBlur = 0;+ var X = function (b) { return PLAY + (b - bNow) * pxb; };+ // the beat grid, everywhere+ g.save(); g.beginPath(); g.rect(HX, 0, W - HX, 218); g.clip();+ for (var b = Math.ceil(bNow - (PLAY - HX) / pxb) - 1; b <= bNow + (W - PLAY) / pxb + 1; b++) {+ var bx = X(b) - 0.14 * pxb;+ line(g, bx, TT - 8, bx, TT - 1, C.l(.3), 1.2); line(g, bx, TB + 1, bx, TB + 8, C.l(.3), 1.2);+ }+ g.restore();+ for (var i = 0; i < 5; i++) line(g, 14, TT + i * SP, W, TT + i * SP, C.l(.5), 1.3);+ // the playhead, under the notes; every hit flashes it+ var ph = k.hot ? k.s : 0;+ g.save(); if (ph > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 16 * ph; }+ line(g, PLAY, TT - 3 * SP, PLAY, TB + 3.5 * SP, C.h(.55 + .45 * ph), 2 + 2.5 * ph); g.restore();+ // the hits heard so far+ g.save(); g.beginPath(); g.rect(HX, 0, W - HX, 218); g.clip();+ g.shadowColor = C.l(.8); g.shadowBlur = C.glow;+ var shown = [];+ if (M.has) M.cols.forEach(function (c) {+ var x = X(c.beat); if (x < HX - 40 || x > W + 40) return;+ var vis = c.hits.filter(function (h) { return f >= h.show - RISE; });+ if (vis.length) shown.push({ c: c, x: x, vis: vis });+ });+ // one stem per column for the hands (up) and one for the feet (down); a beat's columns share beams+ var groups = {};+ shown.forEach(function (s) {+ ['u', 'd'].forEach(function (dir) {+ var hs = s.vis.filter(function (h) { return KEY[h.piece][2] === dir; });+ if (!hs.length) return;+ var ys = hs.map(function (h) { return yOf(KEY[h.piece][0]); }), hot = 0;+ hs.forEach(function (h) { hot = Math.max(hot, h.ev != null ? k.env(h.ev) : 0); });+ var col = { x: s.x, dir: dir, top: Math.min.apply(null, ys), bot: Math.max.apply(null, ys), hot: hot, beat: s.c.beat };+ col.sx = dir === 'u' ? s.x + 8.4 : s.x - 8.4;+ col.tip = dir === 'u' ? Math.min(col.top - 3.2 * SP, TT - 2.6 * SP) : col.bot + 2.6 * SP;+ var gk = dir + '|' + Math.floor(s.c.beat + 1e-6);+ (groups[gk] = groups[gk] || []).push(col);+ });+ s.vis.forEach(function (h) {+ var sv = h.ev != null ? k.env(h.ev) : 0, a = f >= h.show ? 1 : Math.max(0.15, sv), y = yOf(KEY[h.piece][0]);+ var cc = sv > 0.05 ? C.hot : C.ink;+ g.save(); g.globalAlpha *= a; if (sv > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 18 * sv; }+ if (KEY[h.piece][0] >= 40) line(g, s.x - 14, yOf(40), s.x + 14, yOf(40), C.i(.8), 1.6);+ head(g, s.x, y, KEY[h.piece][1], cc);+ if (h.piece === 'hihat_open') ring(g, s.x + 8.4, TT - 4.3 * SP, 5, 5, cc, 2);+ g.restore();+ });+ });+ Object.keys(groups).forEach(function (gk) {+ var cols = groups[gk].sort(function (a, b) { return a.x - b.x; }), up = cols[0].dir === 'u';+ var hot = Math.max.apply(null, cols.map(function (c) { return c.hot; }));+ if (cols.length > 1) { // a beam level across the beat: 8ths, and 16ths where they are+ var tip = up ? Math.min.apply(null, cols.map(function (c) { return c.tip; })) : Math.max.apply(null, cols.map(function (c) { return c.tip; }));+ cols.forEach(function (c) { c.tip = tip; });+ }+ g.save(); if (hot > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 14 * hot; }+ var colr = hot > 0.05 ? C.hot : C.ink;+ cols.forEach(function (c) { line(g, c.sx, up ? c.bot : c.top, c.sx, c.tip, c.hot > 0.05 ? C.hot : C.ink, 1.7); });+ if (cols.length > 1) {+ var th = up ? 0.5 * SP : -0.5 * SP, x0 = cols[0].sx - 0.8, x1 = cols[cols.length - 1].sx + 0.8, t0 = cols[0].tip;+ g.fillStyle = colr; g.fillRect(x0, up ? t0 : t0 + th, x1 - x0, Math.abs(th));+ for (var j = 0; j < cols.length - 1; j++) {+ var gap = cols[j + 1].beat - cols[j].beat;+ if (gap <= 0.26) g.fillRect(cols[j].sx - 0.8, up ? t0 + 1.6 * Math.abs(th) : t0 - 2.6 * Math.abs(th), cols[j + 1].sx - cols[j].sx + 1.6, Math.abs(th));+ }+ }+ g.restore();+ });+ if (!M.has) k.ev.forEach(function (e, ei) {+ if (e.kind === 'skip' || f < e.frame - RISE) return;+ var x = X(k.beat(e.t)), sv = k.env(ei), y = TT + 2 * SP;+ line(g, x - 8, y + 8, x + 8, y - 8, sv > 0.05 ? C.hot : C.ink, 5);+ });+ g.restore();+ // fade under the clef; the percussion clef (two bars)+ var gr = g.createLinearGradient(HX, 0, HX + 60, 0); gr.addColorStop(0, C.bg(1)); gr.addColorStop(1, C.bg(0));+ g.fillStyle = gr; g.fillRect(HX - 2, 0, 62, 218);+ for (i = 0; i < 5; i++) line(g, HX - 2, TT + i * SP, HX + 60, TT + i * SP, C.l(.5), 1.3);+ line(g, 14, TT, 14, TB, C.l(.7), 2);+ g.fillStyle = C.i(.92); g.fillRect(30, TT + SP, 6, 2 * SP); g.fillRect(42, TT + SP, 6, 2 * SP);+ // the kit from above: each piece flashes on its hit, its skin ripples, a cymbal sways+ var last = {};+ M.hits.forEach(function (h) {+ if (f < h.show - RISE) return;+ var kp = kitOf(h.piece), l = last[kp];+ if (!l || h.show >= l.show) last[kp] = h;+ });+ g.shadowColor = C.l(.8); g.shadowBlur = C.glow;+ // stands and the drummer's seat, faint+ ring(g, 500, 436, 34, 6, C.l(.25), 1.5);+ Object.keys(KIT).forEach(function (kp) {+ var P = KIT[kp], h = last[kp], age = h ? f - h.show : 999, sv = h && h.ev != null ? k.env(h.ev) : (h && age >= 0 && age < 6 ? 1 - age / 6 : 0);+ var hot = sv > 0.05, vel = h ? h.vel : 0, col = hot ? C.hot : C.line;+ if (!M.has && k.hot) { hot = true; sv = k.s; col = C.hot; }+ g.save(); if (hot) { g.shadowColor = C.hot; g.shadowBlur = 20 * sv; }+ if (P[3] === 'kick') { // the bass drum on its side: shell, batter head, pedal and beater+ var swing = hot ? sv : 0;+ g.beginPath(); g.rect(P[0] - 74, P[1] - 34, 148, 58); g.strokeStyle = col; g.lineWidth = 2.6; g.stroke();+ if (hot) { g.fillStyle = C.h(0.25 * sv); g.fill(); }+ line(g, P[0] - 74, P[1] + 24, P[0] + 74, P[1] + 24, col, 4.5);+ line(g, P[0], P[1] + 62, P[0], P[1] + 30 + 8 * (1 - swing), col, 2.4);+ disc(g, P[0], P[1] + 30 + 8 * (1 - swing), 6, col);+ if (h && age >= 0 && age < 18) { var u = age / 18; line(g, P[0] - 74 * u, P[1] + 24 - 30 * u, P[0] + 74 * u, P[1] + 24 - 30 * u, C.h(0.7 * (1 - u)), 2); }+ } else if (P[3] === 'cym') { // a cymbal: sways after a hit, rings out+ var sway = h && age >= 0 && age < 40 ? Math.sin(age * 0.55) * Math.exp(-age / 14) * (0.12 + 0.12 * vel) : 0;+ ring(g, P[0], P[1], P[2], P[2] * (0.92 + sway), col, 2.4);+ ring(g, P[0], P[1], P[2] * 0.22, P[2] * 0.22 * (0.92 + sway), col, 2);+ if (kp === 'hihat') ring(g, P[0] + 3, P[1] - 3, P[2] * 0.93, P[2] * 0.85 * (0.92 + sway), C.l(.45), 1.4);+ if (hot) { g.beginPath(); g.ellipse(P[0], P[1], P[2], P[2] * (0.92 + sway), 0, 0, Math.PI * 2); g.fillStyle = C.h(0.22 * sv); g.fill(); }+ for (var r = 0; r < 3; r++) ring(g, P[0], P[1], P[2] * (0.4 + r * 0.18), P[2] * (0.4 + r * 0.18) * (0.92 + sway), C.l(.15), 1);+ } else { // a drum: shell, head, lugs; the skin ripples from the centre+ ring(g, P[0], P[1], P[2], P[2], col, 2.6); ring(g, P[0], P[1], P[2] - 5, P[2] - 5, C.l(.3), 1.2);+ for (var lg = 0; lg < 8; lg++) { var an = lg * Math.PI / 4; disc(g, P[0] + Math.cos(an) * (P[2] + 3), P[1] + Math.sin(an) * (P[2] + 3), 2.4, col); }+ if (hot) { g.beginPath(); g.arc(P[0], P[1], P[2] - 5, 0, Math.PI * 2); g.fillStyle = C.h(0.25 * sv); g.fill(); }+ if (kp === 'snare') for (var sw = -2; sw <= 2; sw++) line(g, P[0] + sw * 7, P[1] - P[2] * 0.7, P[0] + sw * 7, P[1] + P[2] * 0.7, C.l(.18), 1);+ }+ // ripples: rings running out over half a second, bigger for a louder hit+ if (h && age >= 0 && age < 16 && P[3] !== 'kick') {+ for (var rr = 0; rr < 2; rr++) {+ var aa = age - rr * 5; if (aa < 0) continue;+ var uu = aa / 16, rad = (P[2] - 4) * (0.15 + 0.85 * uu) * (0.7 + 0.3 * vel);+ ring(g, P[0], P[1], rad, rad * (P[3] === 'cym' ? 0.92 : 1), C.h(0.8 * (1 - uu)), 2.2 - uu);+ }+ }+ g.restore();+ });+ text(k, M.has ? 'PIECES SORTED BY THEIR SOUND' : 'HITS, NO PIECES (NO NOTES.JSON)', 0, 26, 28, C.muted, 'left');+ g.restore();+ // labels: each piece, red while it rings+ var LBL = { crash: [-60, -36], ride: [70, -36], hihat: [-60, 0], tom_high: [-30, -80], tom_mid: [30, -80], snare: [-50, 36],+ tom_floor: [70, 30], kick: [-20, 76] };+ Object.keys(KIT).forEach(function (kp) {+ var h = last[kp], age = h ? f - h.show : 999;+ k.label(NAME[kp], [KIT[kp][0], KIT[kp][1]], LBL[kp][0], LBL[kp][1], age >= -RISE && age < 10);+ });++ }+ });+})();diff --git a/skills/showtime/library/xray/guitar/README.md b/skills/showtime/library/xray/guitar/README.mdnew file mode 100644index 0000000..4b12a26--- /dev/null+++ b/skills/showtime/library/xray/guitar/README.md@@ -0,0 +1,24 @@+# Guitar (tab and fretboard)++Parts: chord names, a six-line tab (string 1, high e, on top; string names on the left), the right-hand+fingers under it (p thumb, i index, m middle, a ring), a fixed playhead, and a fretboard diagram in the same+orientation (nut on the left, frets 0-12 at their real spacing, inlays at 3 5 7 9 12).++- An event = a pluck: its notes are written as fret numbers on their strings at the playhead, red on the+ event's frame, with the plucking fingers under them; on the fretboard the fretted dots (or an o at the nut for+ an open string) light while they sound and the plucked string vibrates from the fret to the bridge, ringing+ down over about a second. Every pluck also flashes the playhead.+- The tab scrolls on the smooth tempo map (`k.beat`), notes on their quantized beat.+- Which string, fret and finger: `notes.json` `guitar[]` (`showtime xray notes <clip> --job <job> --instrument+ guitar`): assigned by rule (standard tuning, open position first; p for strings 6-4, i m a for 3 2 1), not+ read from the hands. Say so on the end card (`"drawn"`).+- `showtime.json` `"xray"`: `min_confidence` (drop ghost notes: harmonics and buzz come out at 0.1-0.2),+ `chords` (`false` hides them; a name shows only when it holds for `chord_beats` beats, default 2, which drops+ passing one-beat guesses), `px_per_beat`. Skip the events with no pitch heard (finger and string noise).+- Trim the clip to the playing (`footage trim --from`): talk or tuning before it turns into ghost notes.+- Measured: pluck times and count, pitches, chords (guessed from the notes), tempo map. Drawn: the tab and+ fretboard, fingering by rule.++Verified on: fingerpicking C, Am, D, G, Em (Elenaf, Wikimedia Commons, CC BY-SA 3.0: https://commons.wikimedia.org/wiki/File:Fingerpicking_Pattern_4_p-ima_chitarra.webm; a film made from it is CC BY-SA too), trimmed from 6.0 s,+18.7 s, 9:16 dark, `min_confidence` 0.3: 33 of 33 events on their frame · audio 0.1 ms from the original;+scroll 6.0 px/frame, 0 stalls and 0 jumps between the scan and the end card.diff --git a/skills/showtime/library/xray/guitar/mechanism.js b/skills/showtime/library/xray/guitar/mechanism.jsnew file mode 100644index 0000000..0a6a00a--- /dev/null+++ b/skills/showtime/library/xray/guitar/mechanism.js@@ -0,0 +1,188 @@+/* A guitar, written as it is heard (showtime X-ray mechanism library: guitar).+ Design space: 1000 x 350 units (the 9:16 panel under a 4:3 clip, about 0.95 px per unit): chord names on top,+ a six-line tab (string 1, high e, on top) with the right-hand fingers (p i m a) under it, and a fretboard+ diagram below in the same orientation, nut on the left, frets 0-12 at their real spacing.+ The tab scrolls left past a fixed playhead on the smooth tempo map (k.beat of the frame's time, never a jump).+ Each event is a pluck: its notes (notes.json guitar[]: string, fret, finger) are written at the playhead, red on+ the event's frame; on the fretboard the fretted dots light while they sound and the plucked string vibrates+ from the fret to the bridge, ringing down over about a second.+ Measured: pluck times (xray.json), pitches, tempo map, chords (`showtime xray notes --instrument guitar`).+ Assigned by rule, not read from the picture: string and fret (standard tuning, open position first) and the+ finger (p for strings 6-4, i m a for 3 2 1). Set in showtime.json "xray": "min_confidence" (drop notes the+ model is less sure of), "chords" (false hides the names; a name shows when it holds for "chord_beats" beats,+ default 2), "px_per_beat". Without notes.json each pluck is a tick on the tab, never a guessed fret. */+(function () {+ var W = 1000, TY = 90, TS = 25, FY = 266, FS = 16.4, NUT = 46, PLAY = 862, RISE = 3, HX = 52;+ var NAMES = ['e', 'B', 'G', 'D', 'A', 'E'];+ var M = null;++ function fretX(n) { return NUT + (W - NUT) * (1 - Math.pow(2, -n / 12)) / 0.5; }+ function build(k) {+ var mus = k.music || {}, cfg = k.cfg || {}, q = {}, gt = {}, evOf = {}, minc = cfg.min_confidence || 0;+ (mus.quantized || []).forEach(function (d) { q[d.i] = d; });+ (mus.guitar || []).forEach(function (d) { gt[d.i] = d; });+ k.ev.forEach(function (e, ei) { if (e.kind !== 'skip') (e.notes || []).forEach(function (n) { evOf[n.i] = ei; }); });+ var notes = [];+ (mus.notes || []).forEach(function (n, j) {+ var i = n.i != null ? n.i : j, gd = gt[i];+ if (!gd || (n.confidence != null && n.confidence < minc)) return;+ var ev = evOf[i];+ notes.push({ i: i, t: n.t, end: n.end || n.t + 0.5, conf: n.confidence || 0, beat: q[i] ? q[i].beat : k.beat(n.t),+ string: gd.string, fret: gd.fret, finger: gd.finger || '', ev: ev,+ show: ev != null ? k.ev[ev].frame : Math.floor(n.t * k.fps + 1e-9) });+ });+ var cols = {}; // one column per beat; one note per string (the surer one)+ notes.forEach(function (n) {+ var c = cols[n.beat] = cols[n.beat] || { beat: n.beat, by: {} }, o = c.by[n.string];+ if (!o || n.conf > o.conf) c.by[n.string] = n;+ });+ var chords = [], need = cfg.chord_beats || 2;+ if (cfg.chords !== false) (mus.chords || []).forEach(function (c) {+ var last = chords[chords.length - 1];+ if (last && last.name === c.name && Math.abs(last.end - c.t) < 0.05) { last.end = c.end; last.n++; }+ else chords.push({ name: c.name, t: c.t, end: c.end, n: 1 });+ });+ var bpm = (mus.tempo && mus.tempo.bpm) || 120;+ M = { notes: notes, has: !!(mus.guitar && mus.guitar.length),+ cols: Object.keys(cols).map(function (b) { var c = cols[b]; c.notes = Object.keys(c.by).map(function (s) { return c.by[s]; }); return c; }),+ chords: chords.filter(function (c) { return c.n >= need; }).map(function (c) { c.beat = k.beat(c.t); return c; }),+ key: mus.key || null, bpm: bpm, pxb: cfg.px_per_beat || Math.max(56, Math.min(140, 190 / (bpm / 60))) };+ }+ function line(g, x0, y0, x1, y1, col, w) { g.beginPath(); g.moveTo(x0, y0); g.lineTo(x1, y1); g.strokeStyle = col; g.lineWidth = w; g.stroke(); }+ function disc(g, x, y, r, fill) { g.beginPath(); g.arc(x, y, r, 0, Math.PI * 2); g.fillStyle = fill; g.fill(); }+ function text(k, s, x, y, px, col, align, weight, family) {+ var g = k.g; g.save(); g.shadowBlur = 0; g.font = (weight || 600) + ' ' + (px / k.scale).toFixed(2) + 'px ' + (family || '"IBM Plex Mono"');+ g.fillStyle = col; g.textAlign = align || 'left'; g.textBaseline = 'alphabetic'; g.globalAlpha *= (k.labelAlpha == null ? 1 : k.labelAlpha);+ g.fillText(s, x, y); g.restore();+ }+ function strY(s) { return TY + (s - 1) * TS; }+ function boardY(s) { return FY + (s - 1) * FS; }++ XRAY.mechanism({+ name: 'guitar',+ noun: 'plucks', one: 'a pluck',+ box: [0, 0, 1000, 350],+ anchors: { nut_e: [NUT, boardY(1)], nut_E: [NUT, boardY(6)], fret12_e: [W, boardY(1)], fret12_E: [W, boardY(6)] },+ outline: [[NUT, boardY(1)], [W, boardY(1)], [W, boardY(6)], [NUT, boardY(6)]],+ headline: function (n) { return '<em>' + n + '</em> plucks,<br>the tab written<br>from the sound.'; },+ measured: function () {+ return 'Each pluck lit up on the frame its sound was heard; pitches, chords and tempo read from that sound.';+ },+ drawn: 'Drawn: tab and fretboard; strings, frets and fingers by rule, not read from the hands.',+ // the live page: pads that play the next event of each kind from the real clip+ touch: { verb: 'Play it', by: 'note' },+ draw: function (k) {+ if (k.view === 'inplace') return;+ if (!M) build(k);+ var g = k.g, C = k.C, f = k.f, now = f / k.fps, bNow = k.beat(now), pxb = M.pxb;+ var o = k.pr(0, 0);+ g.save(); g.translate(o[0], o[1]); g.scale(k.scale, k.scale); g.shadowBlur = 0;+ var X = function (b) { return PLAY + (b - bNow) * pxb; };+ // the beat grid, everywhere (ticks above and below the tab)+ g.save(); g.beginPath(); g.rect(HX, 0, W - HX, FY - 14); g.clip();+ for (var b = Math.ceil(bNow - (PLAY - HX) / pxb) - 1; b <= bNow + (W - PLAY) / pxb + 1; b++) {+ var bx = X(b) - 0.27 * pxb;+ line(g, bx, TY - 10, bx, TY - 3, C.l(.3), 1.2); line(g, bx, strY(6) + 3, bx, strY(6) + 10, C.l(.3), 1.2);+ }+ g.restore();+ // the six strings of the tab, their names, and the bracket+ for (var s = 1; s <= 6; s++) line(g, HX - 8, strY(s), W, strY(s), C.l(.5), 1.3);+ line(g, HX - 8, strY(1), HX - 8, strY(6), C.l(.7), 2);+ for (s = 1; s <= 6; s++) text(k, NAMES[s - 1], 14, strY(s) + 9, 26, C.muted, 'left', 600);+ // the playhead (every pluck flashes it), under the numbers+ var ph = k.hot ? k.s : 0;+ g.save(); if (ph > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 16 * ph; }+ line(g, PLAY, TY - 14, PLAY, strY(6) + 14, C.h(.55 + .45 * ph), 2 + 2.5 * ph); g.restore();+ // the notes heard so far: fret numbers on their strings, the plucking finger under them+ g.save(); g.beginPath(); g.rect(HX, 0, W - HX, FY - 10); g.clip();+ var fpx = 28 / k.scale;+ if (M.has) {+ M.cols.forEach(function (c) {+ var x = X(c.beat); if (x < HX - 40 || x > W + 40) return;+ var fingers = [], hotC = 0;+ c.notes.forEach(function (n) {+ if (f < n.show - RISE) return;+ var sv = n.ev != null ? k.env(n.ev) : 0, a = f >= n.show ? 1 : Math.max(0.15, sv), y = strY(n.string), lab = String(n.fret);+ hotC = Math.max(hotC, sv);+ g.save(); g.globalAlpha *= a;+ g.font = '600 ' + fpx.toFixed(2) + 'px "IBM Plex Mono"';+ var w = g.measureText(lab).width + 8;+ g.fillStyle = C.bg(1); g.fillRect(x - w / 2, y - 11.5, w, 23);+ if (sv > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 18 * sv; }+ g.fillStyle = sv > 0.05 ? C.hot : C.ink; g.textAlign = 'center'; g.textBaseline = 'middle'; g.fillText(lab, x, y + 1.5);+ g.restore();+ if (n.finger && fingers.indexOf(n.finger) < 0) fingers.push(n.finger);+ fingers.sort(function (a, b) { return 'pima'.indexOf(a) - 'pima'.indexOf(b); });+ });+ if (fingers.length) {+ g.save(); g.font = 'italic 600 ' + fpx.toFixed(2) + 'px "IBM Plex Mono"'; g.textAlign = 'center'; g.textBaseline = 'alphabetic';+ if (hotC > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 12 * hotC; }+ g.fillStyle = hotC > 0.05 ? C.hot : C.muted; g.fillText(fingers.join(''), x, strY(6) + 36); g.restore();+ }+ });+ } else {+ k.ev.forEach(function (e, ei) {+ if (e.kind === 'skip' || f < e.frame - RISE) return;+ var x = X(k.beat(e.t)), sv = k.env(ei);+ line(g, x, strY(2), x, strY(5), sv > 0.05 ? C.hot : C.ink, 3);+ });+ }+ // chord names, written once they have held long enough to be heard as chords+ M.chords.forEach(function (c) {+ if (now < c.t) return;+ var x = X(c.beat); if (x < HX - 60 || x > W + 60) return;+ var fresh = Math.max(0, 1 - (now - c.t) * 2);+ g.save(); if (fresh > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 14 * fresh; }+ g.font = '800 ' + (34 / k.scale).toFixed(2) + 'px "Bricolage Grotesque"'; g.textAlign = 'left'; g.textBaseline = 'alphabetic';+ g.fillStyle = fresh > 0.05 ? C.hot : C.ink; g.fillText(c.name, x - 8, TY - 20); g.restore();+ });+ g.restore();+ // fade the scroll out at the left edge, then re-draw the string lines there+ var gr = g.createLinearGradient(HX, 0, HX + 60, 0); gr.addColorStop(0, C.bg(1)); gr.addColorStop(1, C.bg(0));+ g.fillStyle = gr; g.fillRect(HX - 2, 0, 62, FY - 12);+ for (s = 1; s <= 6; s++) line(g, HX - 2, strY(s), HX + 60, strY(s), C.l(.5), 1.3);+ // the fretboard: nut, frets at their real spacing, inlays, strings; the sounding notes fretted and ringing+ var y1 = boardY(1), y6 = boardY(6);+ g.fillStyle = C.l(.06); g.fillRect(NUT, y1 - 7, W - NUT, y6 - y1 + 14);+ for (var fr = 1; fr <= 12; fr++) line(g, fretX(fr), y1 - 7, fretX(fr), y6 + 7, C.l(.45), 2);+ line(g, NUT, y1 - 8, NUT, y6 + 8, C.i(.85), 5);+ [3, 5, 7, 9].forEach(function (n) { disc(g, (fretX(n - 1) + fretX(n)) / 2, (y1 + y6) / 2, 4, C.l(.3)); });+ disc(g, (fretX(11) + fretX(12)) / 2, (y1 + y6) / 2 - FS * 1.5, 4, C.l(.3)); disc(g, (fretX(11) + fretX(12)) / 2, (y1 + y6) / 2 + FS * 1.5, 4, C.l(.3));+ var ring = {}; // per string: the last pluck heard on it+ M.notes.forEach(function (n) {+ if (f < n.show - RISE) return;+ var r = ring[n.string];+ if (!r || n.show > r.show || (n.show === r.show && n.conf > r.conf)) ring[n.string] = n;+ });+ for (s = 1; s <= 6; s++) {+ var n = ring[s], y = boardY(s), w0 = 1 + (s - 1) * 0.35;+ var sounding = n && f >= n.show - RISE && now <= n.end + 0.1, age = n ? f - n.show : 999;+ var sv = n && n.ev != null ? k.env(n.ev) : 0, hot = sv > 0.05;+ var x0 = n && sounding && n.fret > 0 && n.fret <= 12 ? (fretX(n.fret - 1) + fretX(n.fret)) / 2 : NUT;+ var amp = sounding ? (age < 0 ? 6 * sv : 6 * Math.exp(-age / 14)) : 0;+ if (n && sounding && n.fret > 12) amp = 0;+ line(g, NUT, y, sounding ? x0 : W, y, C.l(.55), w0);+ if (sounding) {+ g.save(); if (hot) { g.shadowColor = C.hot; g.shadowBlur = 14 * sv; }+ g.beginPath();+ for (var u = 0; u <= 60; u++) {+ var xx = x0 + (W - x0) * u / 60, yy = y + amp * Math.sin(Math.max(0, age) * 2.3 + 0.6) * Math.sin(Math.PI * u / 60);+ if (u) g.lineTo(xx, yy); else g.moveTo(xx, yy);+ }+ g.strokeStyle = hot ? C.hot : C.i(.9); g.lineWidth = w0 + 0.8; g.stroke(); g.restore();+ if (n.fret > 0 && n.fret <= 12) {+ g.save(); if (hot) { g.shadowColor = C.hot; g.shadowBlur = 16 * sv; }+ disc(g, x0, y, 7.5, hot ? C.hot : C.i(.92)); g.restore();+ } else if (n.fret === 0) {+ g.beginPath(); g.arc(NUT - 16, y, 5.5, 0, Math.PI * 2); g.strokeStyle = hot ? C.hot : C.i(.92); g.lineWidth = 2; g.stroke();+ }+ }+ }+ // words: the key and tempo heard+ var info = [M.key ? String(M.key).toUpperCase() : null, M.has ? Math.round(M.bpm) + ' BPM' : null].filter(Boolean).join(' · ');+ if (info) text(k, info, 0, 22, 28, C.muted, 'left');+ text(k, 'p i m a: right hand', PLAY + 8, 22, 28, C.muted, 'right');+ g.restore();+ }+ });+})();diff --git a/skills/showtime/library/xray/musicbox/README.md b/skills/showtime/library/xray/musicbox/README.mdnew file mode 100644index 0000000..028ef17--- /dev/null+++ b/skills/showtime/library/xray/musicbox/README.md@@ -0,0 +1,22 @@+# Disc music box (Polyphon, Symphonion, Regina style)++Parts: the perforated disc (drive holes on the rim), the play line from the spindle, one star wheel per track, the+comb with one tooth per pitch (bass long and at the top, treble short).++- An event = a pluck: its slot reaches the play line, its star wheel turns one tooth and the comb tooth rings,+ peaking on the event's frame; the slot then turns away with the disc, so the disc fills with what was played.+- Which tooth: from `notes.json` (`showtime xray notes <clip> --job <job> --instrument musicbox`, copied next to+ `index.html`): one track and one tooth per pitch heard. Without it, three registers by the sound's band (low,+ mid, high), said on screen.+- The disc's speed: `"xray": {"spin": <degrees per second>}`, measured from the clip (follow a mark on the disc+ over a few seconds); default 6.+- Anchors to place on the reference frame (see-through on a disc filmed face on): `rim_e` (where the play line+ meets the rim), then `rim_s`, `rim_w`, `rim_n` going clockwise from it (four rim points, no three in a line).+ Measure the spin by tracking the disc itself (`analyze --track --region` around the disc): the homographies turn+ with it; then track the cabinet with the disc masked to place the drawing.+- Analyze dense music with the flux engine (`--engine flux`, `auto` picks it when the sound never goes quiet).+- Measured: pluck times and count (the clip's sound), pitches (notes.json). Drawn: a generic disc music box; the+ slots are rebuilt from the sound, not read off the disc in the clip.++Verified on: "Music Box Plays \"Angels ever bright and fair\"", a disc music box at the Canada Science and Technology Museum+(Wikimedia Commons, CC BY 3.0, author: the museum): https://commons.wikimedia.org/wiki/File:Music_Box_Plays_%22Angels_ever_bright_and_fair%22.webmdiff --git a/skills/showtime/library/xray/musicbox/mechanism.js b/skills/showtime/library/xray/musicbox/mechanism.jsnew file mode 100644index 0000000..6ef1430--- /dev/null+++ b/skills/showtime/library/xray/musicbox/mechanism.js@@ -0,0 +1,107 @@+/* A disc music box, inside (showtime X-ray mechanism library: musicbox).+ Design space: the disc centred on (0, 0), radius 342, seen from above; the play line runs from the spindle to the+ right, where the star wheels sit; the comb is on the right, one tooth per pitch, bass at the top.+ Each event is a pluck: a slot of the disc reaches the play line, its star wheel turns one tooth and the comb+ tooth rings, PEAKING on the event's frame. The slot then turns away with the disc, so the disc fills with the+ slots the clip played: the disc rebuilt from the sound.+ Which tooth: from notes.json (`showtime xray notes`: the pitches heard); without it a pluck lights one of three+ registers by the sound's band (low, mid, high), never a guessed pitch. The disc's speed: "xray": {"spin": deg/s}+ (measure it from the clip; positive = clockwise as filmed, negative = anticlockwise; default 6). In place, only the+ disc is drawn (on the real disc); the comb and the links are drawn in the panel. */+(function () {+ var deg = function (a) { return a * Math.PI / 180; };+ var RIM = 342, R0 = 120, R1 = 318, COMB_X = 790, COMB_Y0 = -300, COMB_Y1 = 300;+ var BANDS = ['low', 'mid', 'high'];+ var tracks = null; // pitch (or band) -> track index, built on the first frame++ function build(k) {+ var keys = [];+ if (k.notes.length) {+ k.notes.forEach(function (n) { if (keys.indexOf(n.pitch) < 0) keys.push(n.pitch); });+ keys.sort(function (a, b) { return a - b; });+ } else keys = BANDS.slice();+ var names = {};+ k.notes.forEach(function (n) { names[n.pitch] = n.name; });+ tracks = { keys: keys, names: names, byNotes: !!k.notes.length };+ }+ function trackR(i) { var n = tracks.keys.length; return n < 2 ? (R0 + R1) / 2 : R0 + (R1 - R0) * i / (n - 1); }+ function toothY(i) { var n = tracks.keys.length; return n < 2 ? 0 : COMB_Y0 + (COMB_Y1 - COMB_Y0) * i / (n - 1); }+ function toothLen(i) { var n = tracks.keys.length; return 190 - 110 * (n < 2 ? 0.5 : i / (n - 1)); }+ function plucks(k, e) { // the track indexes event e plays+ if (e.kind !== 'event') return [];+ if (tracks.byNotes) return (e.notes || []).map(function (n) { return tracks.keys.indexOf(n.pitch); }).filter(function (i) { return i >= 0; });+ return [Math.max(0, BANDS.indexOf(e.band || 'mid'))];+ }++ XRAY.mechanism({+ name: 'musicbox',+ noun: 'plucks', noun1: 'pluck', one: 'a pluck',+ box: [-370, -370, 820, 370],+ // four points on the rim (no three in a line): the disc's top, right, bottom and left edges as filmed, rim_e being+ // where the play line meets the rim+ anchors: { rim_n: [0, -RIM], rim_e: [RIM, 0], rim_s: [0, RIM], rim_w: [-RIM, 0] },+ outline: [[-RIM, -RIM], [RIM, -RIM], [RIM, RIM], [-RIM, RIM]],+ headline: function (n) { return '<em>' + n + '</em> plucks,<br>the disc rebuilt<br>from its sound.'; },+ measured: function (n, text, hasNotes) {+ return hasNotes+ ? 'Each slot is a note heard in the clip: its track from its pitch, its place on the disc from its time.'+ : 'Each pluck lit up on the frame its sound was heard; its register from the sound’s band.';+ },+ drawn: 'Inside view: a generic disc music box, drawn; the slots are rebuilt from the sound, not read off this disc.',+ // the live page: pads that play the next event of each kind from the real clip+ touch: { verb: 'Play it', by: 'note' },+ draw: function (k) {+ if (!tracks) build(k);+ var C = k.C, g = k.g, spin = (k.cfg.spin || 6) / k.fps; // degrees per frame, + = clockwise+ var panel = k.view === 'panel', nT = tracks.keys.length;+ // the disc: rim, drive holes turning with it, faint tracks, the hub+ k.ell(0, 0, RIM, RIM, C.line, 4, 0, false);+ for (var h = 0; h < 72; h++) { var a = deg(h * 5 + spin * k.f); k.dot([Math.cos(a) * (RIM - 14), Math.sin(a) * (RIM - 14)], Math.max(1.5, 3 * k.scale), C.l(.4)); }+ for (var i = 0; i < nT; i++) k.ell(0, 0, trackR(i), trackR(i), i % 2 ? C.l(.07) : C.l(.13), 1.2, 0, false);+ k.ell(0, 0, R0 - 44, R0 - 44, C.line, 3, 0, false);+ k.dot([0, 0], Math.max(3, 8 * k.scale), C.line);+ // the play line, to the right+ g.setLineDash([6, 8]); k.seg([R0 - 40, 0], [RIM + 24, 0], C.i(.55), 2); g.setLineDash([]);+ // slots: a pluck at frame fe reached the play line on fe and has turned with the disc since+ var hotTracks = {};+ k.ev.forEach(function (e) {+ if (e.frame > k.f + 3) return;+ var age = k.f - e.frame, s = k.env(e.i);+ plucks(k, e).forEach(function (ti) {+ if (s > 0.05) hotTracks[ti] = Math.max(hotTracks[ti] || 0, s);+ var a = deg(spin * Math.max(0, age)), r = trackR(ti), p = [Math.cos(a) * r, Math.sin(a) * r];+ var big = panel ? 1 : 1.5, q = k.pr(p[0], p[1]), w = Math.max(3, 8 * big * k.scale), hgt = Math.max(6, 18 * big * k.scale);+ g.save(); g.translate(q[0], q[1]); g.rotate(a + Math.PI / 2);+ if (s > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 18 * s; }+ g.fillStyle = s > 0.05 ? C.hot : (age < 0 ? C.i(0) : C.i(.8));+ g.fillRect(-w / 2, -hgt / 2, w, hgt);+ if (!panel && age >= 0) { g.shadowBlur = 0; g.strokeStyle = C.bg(.9); g.lineWidth = 1.5; g.strokeRect(-w / 2, -hgt / 2, w, hgt); }+ g.restore();+ });+ });+ // star wheels on the play line, one per track; each turns a tooth when its slot passes+ for (var t = 0; t < nT; t++) {+ var on = hotTracks[t] || 0, turned = 0;+ k.ev.forEach(function (e) { if (e.frame <= k.f && plucks(k, e).indexOf(t) >= 0) turned++; });+ k.ell(trackR(t), 0, 13, 13, on ? C.hot : C.soft, 2, deg(turned * 30 + on * 30), true);+ if (!panel) continue;+ // the comb tooth it plucks, and the link between them+ var y = toothY(t), len = toothLen(t), ring = 0, last = -1;+ k.ev.forEach(function (e) { if (e.frame <= k.f && plucks(k, e).indexOf(t) >= 0) last = e.frame; });+ if (last >= 0) { var d = k.f - last; ring = d < 18 ? 1 - d / 18 : 0; }+ if (on > ring) ring = on;+ var wob = Math.sin((k.f - last) * 2.1) * 9 * ring;+ k.seg([trackR(t) + 13, 0], [COMB_X - len, y], ring > 0.05 ? C.h(.6) : C.l(.1), 1.2);+ g.strokeStyle = ring > 0.05 ? C.hot : C.line; g.lineWidth = k.lw(panel ? 5 : 4);+ if (ring > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 14 * ring; }+ k.path([[COMB_X, y], [COMB_X - len * 0.5, y + wob * 0.4], [COMB_X - len, y + wob]]); g.stroke(); g.shadowBlur = 0;+ }+ if (panel) k.seg([COMB_X, COMB_Y0 - 30], [COMB_X, COMB_Y1 + 30], C.line, 8);+ // labels+ k.label('disc', [-RIM * 0.7, -RIM * 0.7], -60, -40, false);+ k.label('play line', [RIM + 10, 0], 40, -150, Object.keys(hotTracks).length > 0);+ k.label('star wheels', [trackR(0), 0], -40, 150, Object.keys(hotTracks).length > 0);+ if (panel) k.label(tracks.byNotes ? 'comb · one tooth per pitch' : 'comb · low, mid, high', [COMB_X, COMB_Y1], -80, 70, false);+ }+ });+})();diff --git a/skills/showtime/library/xray/piano/README.md b/skills/showtime/library/xray/piano/README.mdnew file mode 100644index 0000000..389fde4--- /dev/null+++ b/skills/showtime/library/xray/piano/README.md@@ -0,0 +1,25 @@+# Piano (grand staff and keyboard)++Parts: a grand staff (brace, treble and bass clefs and key signature drawn as paths, no music font needed), a+fixed playhead, and an 88-key keyboard (A0 to C8, middle C marked).++- An event = a strike (one or more keys at once): its notes are written at the playhead, red on the event's+ frame (stems, flags, a beam once the second eighth of a pair is heard, dots, ledger lines, accidentals outside+ the key), and their keys go down and light while they sound. Every strike also flashes the playhead, so a+ strike with no pitch heard still shows.+- The staff scrolls on the smooth tempo map (`k.beat` of the frame's time), never by jumping to the last note;+ notes sit on their quantized beat (eighths of that map).+- Which notes: from `notes.json` (`showtime xray notes <clip> --job <job> --instrument piano`). Hands: the bass+ voice on the bass staff, the melody on the treble staff, other notes with the nearer of the two. Spelling+ follows the key heard (G major: F sharp in the signature).+- `showtime.json` `"xray"`: `meter` (beats per bar; bar lines, the time signature and the bar counter appear+ only when it is set, it is not measured), `downbeat` (beat of bar 1, default 0), `key` (override or `false`),+ `min_confidence`, `px_per_beat`. Without `notes.json`, a strike is a rhythm slash, never a guessed pitch.+- Skip events before the first note (room noise) and clicks after the last one (`"skip"`).+- Measured: strike times and count (the clip's sound), pitches, key, tempo map, eighth-note rhythm (the+ notes model). Drawn: the notation itself (no rests, one stem per hand per beat, ties shown as the longer+ value), not the printed score.++Verified on: Minuet in G, BWV Anh. 114 (Notebook for Anna Magdalena Bach, attributed to Christian Petzold), recording KasraR (Wikimedia Commons, public domain), 22.2 s, 9:16 dark:+91 of 91 events on their frame · audio 0.0 ms from the original; scroll 6.0 px/frame, 0 stalls and 0 jumps+between the scan and the end card.diff --git a/skills/showtime/library/xray/piano/mechanism.js b/skills/showtime/library/xray/piano/mechanism.jsnew file mode 100644index 0000000..e194576--- /dev/null+++ b/skills/showtime/library/xray/piano/mechanism.js@@ -0,0 +1,361 @@+/* A piano, written as it is heard (showtime X-ray mechanism library: piano).+ Design space: 1000 x 440 units (the 9:16 panel, about 0.95 px per unit), a grand staff on top (treble and bass,+ brace, clefs and key signature drawn as paths, no music font needed) and an 88-key keyboard below.+ The staff scrolls left past a fixed playhead on the smooth tempo map (k.beat of the frame's time: never a jump to+ the latest note). Each event is a strike: its notes (notes.json, joined to the event by the runtime) are written+ at the playhead, red on the event's frame, with their stems, flags, beams, dots, ledger lines and accidentals;+ their keys go down and light up on the keyboard while they sound.+ Measured: strike times (xray.json), pitches, key, tempo map and eighth-note rhythm (`showtime xray notes`).+ Set in showtime.json "xray": "meter" (beats per bar, e.g. 3; bar lines and the time signature appear only when+ it is set), "downbeat" (the beat of bar 1, default 0), "key" (a key name to override the one heard, or false to+ hide it), "min_confidence" (drop notes the model is less sure of), "px_per_beat".+ Without notes.json each strike is written as a rhythm slash on the treble staff, never a guessed pitch. */+(function () {+ var W = 1000, SP = 16, TT = 64, TB = TT + 4 * SP, BT = TB + 7 * SP, BB = BT + 4 * SP;+ var KY = 362, KH = 76, PLAY = 862, RISE = 3, HR = 9.4;+ var NAT = [0, 2, 4, 5, 7, 9, 11], LET = 'CDEFGAB', SHARPS = 'FCGDAEB', FLATS = 'BEADGCF';+ var MAJ = { C: 0, G: 1, D: 2, A: 3, E: 4, B: 5, 'F#': 6, 'C#': 7, F: -1, Bb: -2, Eb: -3, Ab: -4, Db: -5, Gb: -6, Cb: -7 };+ var PCN = { C: 0, 'C#': 1, Db: 1, D: 2, 'D#': 3, Eb: 3, E: 4, F: 5, 'F#': 6, Gb: 6, G: 7, 'G#': 8, Ab: 8, A: 9, 'A#': 10, Bb: 10, B: 11, Cb: 11 };+ // clefs as calligraphic ribbons: [x, y, width] in staff spaces, y up from the clef's line (G line, F line)+ var TREBLE = [[0.95, -1.95, 0.05], [1.05, -2.4, 0.12], [1.45, -2.62, 0.15], [1.85, -2.35, 0.17], [1.95, -1.6, 0.19],+ [1.85, -0.2, 0.21], [1.6, 1.6, 0.21], [1.5, 2.9, 0.19], [1.75, 4.0, 0.15], [2.05, 4.38, 0.1], [2.25, 3.9, 0.17],+ [2.05, 3.0, 0.31], [1.3, 2.0, 0.42], [0.45, 1.05, 0.44], [0.08, 0.0, 0.4], [0.45, -0.85, 0.3], [1.3, -1.12, 0.17],+ [2.15, -0.8, 0.23], [2.55, 0.0, 0.42], [2.25, 0.85, 0.38], [1.5, 1.08, 0.25], [0.85, 0.65, 0.17], [0.82, 0.0, 0.13],+ [1.2, -0.38, 0.09], [1.6, -0.25, 0.04]];+ var BASS = [[0.3, 0.0, 0.1], [0.45, 0.62, 0.22], [1.15, 0.98, 0.19], [1.95, 0.6, 0.32], [2.1, -0.3, 0.44],+ [1.7, -1.35, 0.36], [0.9, -2.15, 0.21], [0.15, -2.6, 0.06]];+ var M = null; // the score, built on the first frame++ function fifthsOf(key) {+ if (typeof key !== 'string') return null;+ var m = key.replace(/♯/g, '#').replace(/♭/g, 'b').match(/^\s*([A-G][#b]?)\s*(major|minor|maj|min|m)?\s*$/i);+ if (!m) return null;+ if (m[2] && /^(minor|min|m)$/i.test(m[2])) {+ var rel = (PCN[m[1]] + 3) % 12, best = null;+ Object.keys(MAJ).forEach(function (n) { if (PCN[n] === rel && (best === null || Math.abs(MAJ[n]) < Math.abs(best))) best = MAJ[n]; });+ return best;+ }+ return MAJ[m[1]] != null ? MAJ[m[1]] : null;+ }+ function spell(midi, fifths, keyAlt) { // -> {li, alt, step}: the letter in this key, the way a copyist would+ var pc = ((midi % 12) + 12) % 12, cands = [];+ for (var li = 0; li < 7; li++) for (var a = -1; a <= 1; a++) if ((NAT[li] + a + 12) % 12 === pc) cands.push({ li: li, alt: a });+ var pick = cands.filter(function (c) { return (keyAlt[LET[c.li]] || 0) === c.alt; })[0] ||+ cands.filter(function (c) { return c.alt === 0; })[0] ||+ cands.filter(function (c) { return c.alt === (fifths < 0 ? -1 : 1); })[0] || cands[0];+ pick.step = (Math.floor((midi - pick.alt) / 12) - 1) * 7 + pick.li;+ return pick;+ }+ function yOf(staff, step) { return staff === 't' ? TB - (step - 30) * SP / 2 : BB - (step - 18) * SP / 2; }++ function build(k) {+ var mus = k.music || {}, cfg = k.cfg || {}, raw = mus.notes || [], q = {}, vm = {}, vb = {}, evOf = {};+ (mus.quantized || []).forEach(function (d) { q[d.i] = d; });+ ((mus.voices || {}).melody || []).forEach(function (i) { vm[i] = 1; });+ ((mus.voices || {}).bass || []).forEach(function (i) { vb[i] = 1; });+ k.ev.forEach(function (e, ei) { if (e.kind !== 'skip') (e.notes || []).forEach(function (n) { evOf[n.i] = ei; }); });+ var fifths = cfg.key === false ? 0 : fifthsOf(typeof cfg.key === 'string' ? cfg.key : mus.key);+ var keyName = cfg.key === false ? null : (typeof cfg.key === 'string' ? cfg.key : mus.key) || null;+ if (fifths == null) { fifths = 0; keyName = null; }+ var keyAlt = {};+ for (var s = 0; s < Math.abs(fifths); s++) keyAlt[fifths > 0 ? SHARPS[s] : FLATS[s]] = fifths > 0 ? 1 : -1;+ var minc = cfg.min_confidence || 0, fps = k.fps;+ var notes = raw.filter(function (n) { return n.confidence == null || n.confidence >= minc; }).map(function (n, j) {+ var i = n.i != null ? n.i : j, qd = q[i], sp = spell(n.pitch, fifths, keyAlt);+ var fr = Math.floor(n.t * fps + 1e-9), ev = evOf[i];+ return { i: i, pitch: n.pitch, t: n.t, end: n.end || n.t + 0.3, beat: qd ? qd.beat : k.beat(n.t), dur: qd ? qd.dur_beats : 1,+ li: sp.li, alt: sp.alt, step: sp.step, ev: ev, show: ev != null ? k.ev[ev].frame : fr };+ });+ var byBeat = {};+ notes.forEach(function (n) { (byBeat[n.beat] = byBeat[n.beat] || []).push(n); });+ notes.forEach(function (n) { // the hands: the bass voice below, the melody above, the rest by nearness+ if (vb[n.i] && n.pitch < 67) n.staff = 'b';+ else if (vm[n.i]) n.staff = n.pitch >= 55 ? 't' : 'b';+ else {+ var same = byBeat[n.beat], mel = same.filter(function (o) { return vm[o.i]; })[0], bas = same.filter(function (o) { return vb[o.i]; })[0];+ n.staff = mel && bas ? (Math.abs(n.pitch - mel.pitch) <= Math.abs(n.pitch - bas.pitch) ? 't' : 'b') : (n.pitch >= 60 ? 't' : 'b');+ }+ n.y = yOf(n.staff, n.step);+ });+ var meter = cfg.meter > 0 ? cfg.meter : 0, down = cfg.downbeat || 0, mem = {};+ notes.slice().sort(function (a, b) { return a.beat - b.beat || a.pitch - b.pitch; }).forEach(function (n) {+ var bar = meter ? Math.floor((n.beat - down) / meter + 1e-6) : n.beat, key = n.staff + '|' + bar + '|' + n.step;+ var cur = mem[key] != null ? mem[key] : (keyAlt[LET[n.li]] || 0);+ n.acc = n.alt !== cur ? n.alt : null; // -1 flat, 0 natural, 1 sharp+ if (meter) mem[key] = n.alt;+ });+ var cols = {};+ notes.forEach(function (n) { var key = n.staff + '|' + n.beat; (cols[key] = cols[key] || { staff: n.staff, beat: n.beat, notes: [] }).notes.push(n); });+ var bpm = (mus.tempo && mus.tempo.bpm) || 120;+ M = { notes: notes, cols: Object.keys(cols).map(function (c) { return cols[c]; }), fifths: fifths, keyAlt: keyAlt, keyName: keyName,+ meter: meter, down: down, bpm: bpm, has: !!raw.length,+ pxb: cfg.px_per_beat || Math.max(56, Math.min(140, 190 / (bpm / 60))),+ first: notes.length ? Math.min.apply(null, notes.map(function (n) { return n.beat; })) : 0,+ last: notes.length ? Math.max.apply(null, notes.map(function (n) { return n.beat + n.dur; })) : 0 };+ var ks = 86 + Math.abs(fifths) * 12;+ M.tsX = ks + 6; M.hx = ks + (meter ? 40 : 6) + 10;+ }++ // ------------------------------------------------------------------ drawing helpers (design units, y down)+ function spline(pts, n) {+ var out = [];+ for (var i = 0; i < pts.length - 1; i++) {+ var p0 = pts[Math.max(0, i - 1)], p1 = pts[i], p2 = pts[i + 1], p3 = pts[Math.min(pts.length - 1, i + 2)];+ for (var s = 0; s < n; s++) {+ var t = s / n, t2 = t * t, t3 = t2 * t, o = [];+ for (var d = 0; d < 3; d++) o.push(0.5 * (2 * p1[d] + (-p0[d] + p2[d]) * t + (2 * p0[d] - 5 * p1[d] + 4 * p2[d] - p3[d]) * t2 + (-p0[d] + 3 * p1[d] - 3 * p2[d] + p3[d]) * t3));+ out.push(o);+ }+ }+ out.push(pts[pts.length - 1].slice());+ return out;+ }+ function ribbon(g, pts, ox, oy, sp, fill) { // a stroke of varying width, filled+ var c = spline(pts, 10), L = [], R = [];+ for (var i = 0; i < c.length; i++) {+ var a = c[Math.max(0, i - 1)], b = c[Math.min(c.length - 1, i + 1)], dx = b[0] - a[0], dy = b[1] - a[1];+ var l = Math.hypot(dx, dy) || 1, nx = -dy / l, ny = dx / l, w = c[i][2] / 2;+ L.push([ox + (c[i][0] + nx * w) * sp, oy - (c[i][1] + ny * w) * sp]);+ R.push([ox + (c[i][0] - nx * w) * sp, oy - (c[i][1] - ny * w) * sp]);+ }+ g.beginPath(); L.forEach(function (p, i) { if (i) g.lineTo(p[0], p[1]); else g.moveTo(p[0], p[1]); });+ for (var j = R.length - 1; j >= 0; j--) g.lineTo(R[j][0], R[j][1]);+ g.closePath(); g.fillStyle = fill; g.fill();+ }+ function disc(g, x, y, r, fill) { g.beginPath(); g.arc(x, y, r, 0, Math.PI * 2); g.fillStyle = fill; g.fill(); }+ function line(g, x0, y0, x1, y1, col, w) { g.beginPath(); g.moveTo(x0, y0); g.lineTo(x1, y1); g.strokeStyle = col; g.lineWidth = w; g.stroke(); }+ function treble(g, x, col) { ribbon(g, TREBLE, x, TB - SP, SP, col); disc(g, x + 0.82 * SP, TB - SP + 1.95 * SP, 0.36 * SP, col); }+ function bass(g, x, col) {+ var fy = BT + SP;+ ribbon(g, BASS, x, fy, SP, col); disc(g, x + 0.42 * SP, fy, 0.36 * SP, col);+ disc(g, x + 2.6 * SP, fy - 0.5 * SP, 0.15 * SP, col); disc(g, x + 2.6 * SP, fy + 0.5 * SP, 0.15 * SP, col);+ }+ function accidental(g, kind, cx, cy, col) {+ g.strokeStyle = col; g.fillStyle = col; g.lineCap = 'butt';+ if (kind === 1) { // sharp+ line(g, cx - 2.6, cy - 1.25 * SP, cx - 2.6, cy + 1.35 * SP, col, 1.4); line(g, cx + 2.6, cy - 1.35 * SP, cx + 2.6, cy + 1.25 * SP, col, 1.4);+ [-0.42, 0.42].forEach(function (o) { g.beginPath(); g.moveTo(cx - 6, cy + o * SP + 2.2); g.lineTo(cx + 6, cy + o * SP - 2.2);+ g.lineTo(cx + 6, cy + o * SP - 6.4); g.lineTo(cx - 6, cy + o * SP - 2); g.closePath(); g.fill(); });+ } else if (kind === -1) { // flat+ line(g, cx - 4, cy - 1.9 * SP, cx - 4, cy + 0.5 * SP, col, 1.5);+ g.beginPath(); g.moveTo(cx - 4, cy + 0.5 * SP); g.bezierCurveTo(cx + 9, cy - 0.15 * SP, cx + 5, cy - 0.95 * SP, cx - 4, cy - 0.2 * SP);+ g.lineWidth = 2.4; g.stroke();+ } else { // natural+ line(g, cx - 3, cy - 1.4 * SP, cx - 3, cy + 0.55 * SP, col, 1.4); line(g, cx + 3, cy - 0.55 * SP, cx + 3, cy + 1.4 * SP, col, 1.4);+ [-0.42, 0.42].forEach(function (o) { g.beginPath(); g.moveTo(cx - 3, cy + o * SP + 1.6); g.lineTo(cx + 3, cy + o * SP - 0.4);+ g.lineTo(cx + 3, cy + o * SP - 4.4); g.lineTo(cx - 3, cy + o * SP - 2.4); g.closePath(); g.fill(); });+ }+ g.lineCap = 'round';+ }+ function head(g, x, y, hollow, col) {+ g.save(); g.translate(x, y); g.rotate(-0.35); g.beginPath(); g.ellipse(0, 0, HR, 6.6, 0, 0, Math.PI * 2);+ if (hollow) { g.ellipse(0, 0, 6.4, 3.0, -0.35, 0, Math.PI * 2); }+ g.fillStyle = col; g.fill(hollow ? 'evenodd' : 'nonzero'); g.restore();+ }+ function flag(g, sx, tip, up, col) {+ var d = up ? 1 : -1;+ g.beginPath(); g.moveTo(sx - 0.8, tip);+ g.bezierCurveTo(sx + 1, tip + d * 10, sx + 15, tip + d * 15, sx + 14.5, tip + d * 29);+ g.bezierCurveTo(sx + 14, tip + d * 36, sx + 11, tip + d * 42, sx + 8, tip + d * 46);+ g.bezierCurveTo(sx + 11, tip + d * 38, sx + 11.5, tip + d * 30, sx + 5, tip + d * 24);+ g.bezierCurveTo(sx + 3, tip + d * 22, sx + 1, tip + d * 20, sx - 0.8, tip + d * 18);+ g.closePath(); g.fillStyle = col; g.fill();+ }+ function text(k, s, x, y, px, col, align) {+ var g = k.g; g.save(); g.shadowBlur = 0; g.font = '600 ' + (px / k.scale).toFixed(2) + 'px "IBM Plex Mono"';+ g.fillStyle = col; g.textAlign = align || 'left'; g.textBaseline = 'alphabetic'; g.globalAlpha = k.alpha * (k.labelAlpha == null ? 1 : k.labelAlpha);+ g.fillText(s, x, y); g.restore();+ }++ // ------------------------------------------------------------------ one column of the score (a staff, a beat)+ function geom(col, vis) {+ var mid = col.staff === 't' ? TT + 2 * SP : BT + 2 * SP, below = -1e9, above = -1e9;+ vis.forEach(function (v) { below = Math.max(below, v.n.y - mid); above = Math.max(above, mid - v.n.y); });+ return { up: below >= above, dur: Math.min.apply(null, vis.map(function (v) { return v.n.dur; })),+ top: Math.min.apply(null, vis.map(function (v) { return v.n.y; })), bot: Math.max.apply(null, vis.map(function (v) { return v.n.y; })) };+ }+ function drawColumn(k, col, x, vis, G, beamTip) {+ var g = k.g, C = k.C, hotS = 0, staffTop = col.staff === 't' ? TT : BT, staffBot = col.staff === 't' ? TB : BB;+ vis.forEach(function (v) { hotS = Math.max(hotS, v.s); });+ var colCol = hotS > 0.05 ? C.hot : C.ink;+ // ledger lines+ for (var ly = staffTop - SP; ly >= G.top - 1; ly -= SP) line(g, x - 15, ly, x + 15, ly, C.i(.8), 1.6);+ for (ly = staffBot + SP; ly <= G.bot + 1; ly += SP) line(g, x - 15, ly, x + 15, ly, C.i(.8), 1.6);+ // heads (a second steps to the other side of the stem), dots, accidentals+ var sorted = vis.slice().sort(function (a, b) { return G.up ? a.n.step - b.n.step : b.n.step - a.n.step; }), prev = null, accN = 0;+ sorted.forEach(function (v) {+ var n = v.n, side = 0;+ if (prev && Math.abs(n.step - prev.n.step) === 1 && !prev.side) side = G.up ? 1 : -1;+ v.side = side; prev = v;+ var hx = x + side * (2 * HR - 1.5), c = v.s > 0.05 ? C.hot : C.ink;+ g.save(); g.globalAlpha *= v.a;+ if (v.s > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 18 * v.s; }+ head(g, hx, n.y, n.dur >= 2, c);+ var onLine = ((n.step - (col.staff === 't' ? 30 : 18)) % 2 + 2) % 2 === 0;+ if (n.dur === 1.5 || n.dur === 3 || n.dur === 0.75) disc(g, x + HR + 7 + (side > 0 ? 2 * HR : 0), n.y - (onLine ? SP / 2 : 0), 2.6, c);+ if (n.acc != null) { accidental(g, n.acc, x - HR - 11 - (accN % 2) * 12, n.y, c); accN++; }+ g.restore();+ });+ if (G.dur >= 4) return;+ // the stem, with a flag when the eighth has no partner yet+ var sx = G.up ? x + HR - 0.9 : x - HR + 0.9;+ var tip = beamTip != null ? beamTip : (G.up ? G.top - 3.5 * SP : G.bot + 3.5 * SP);+ g.save(); if (hotS > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 14 * hotS; }+ line(g, sx, G.up ? G.bot : G.top, sx, tip, colCol, 1.7);+ if (G.dur <= 0.5 && beamTip == null) flag(g, sx, tip, G.up, colCol);+ g.restore();+ return sx;+ }++ XRAY.mechanism({+ name: 'piano',+ noun: 'strikes', one: 'a strike',+ box: [0, 0, 1000, 440],+ anchors: { a0: [0, KY + KH], c8: [W, KY + KH], c4: [W * 23 / 52, KY], top: [W / 2, TT] },+ outline: [[0, KY], [W, KY], [W, KY + KH], [0, KY + KH]],+ headline: function (n) { return '<em>' + n + '</em> strikes,<br>the score written<br>from the sound.'; },+ measured: function () {+ return 'Each strike lit up on the frame its sound was heard; the pitches, key and tempo were read from the clip’s sound.';+ },+ drawn: 'Drawn: the notation (rhythm fitted to eighths of the measured tempo, the hands split by register), not the printed score.',+ // the live page: pads that play the next event of each kind from the real clip+ touch: { verb: 'Play it', by: 'note' },+ draw: function (k) {+ if (k.view === 'inplace') return;+ if (!M) build(k);+ var g = k.g, C = k.C, f = k.f, now = f / k.fps, bNow = k.beat(now), pxb = M.pxb, hx = M.hx;+ var o = k.pr(0, 0);+ g.save(); g.translate(o[0], o[1]); g.scale(k.scale, k.scale);+ var X = function (b) { return PLAY + (b - bNow) * pxb; };+ // the grid (beats, and bars when the meter is set): it scrolls everywhere, ahead of the playhead too+ g.save(); g.beginPath(); g.rect(hx, 0, W - hx, KY - 10); g.clip(); g.shadowBlur = 0;+ var b0 = Math.ceil(bNow - (PLAY - hx) / pxb) - 1, b1 = Math.floor(bNow + (W - PLAY) / pxb) + 1;+ for (var b = b0; b <= b1; b++) {+ if (b <= Math.floor(M.first) || (M.has && b > Math.ceil(M.last) + (M.meter || 1))) continue;+ var bx = X(b), bar = M.meter && ((b - M.down) % M.meter + M.meter) % M.meter === 0;+ bx -= 0.27 * pxb; // a bar line sits just before its downbeat+ if (bar) line(g, bx, TT, bx, BB, C.l(.55), 1.6);+ else { line(g, bx, TT - 7, bx, TT - 1, C.l(.3), 1.2); line(g, bx, BB + 1, bx, BB + 7, C.l(.3), 1.2); }+ }+ g.restore();+ // staff lines+ g.save(); g.shadowBlur = 0;+ for (var i = 0; i < 5; i++) { line(g, 14, TT + i * SP, W, TT + i * SP, C.l(.5), 1.3); line(g, 14, BT + i * SP, W, BT + i * SP, C.l(.5), 1.3); }+ g.restore();+ // the notes heard so far, written where the tempo map puts their beat+ g.save(); g.beginPath(); g.rect(hx, 0, W - hx, KY - 8); g.clip();+ if (M.has) {+ var shown = [];+ M.cols.forEach(function (col) {+ var x = X(col.beat); if (x < hx - 40 || x > W + 40) return;+ var vis = [];+ col.notes.forEach(function (n) {+ if (f < n.show - RISE) return;+ var s = n.ev != null ? k.env(n.ev) : 0, a = f >= n.show ? 1 : Math.max(0.15, s);+ vis.push({ n: n, s: s, a: a });+ });+ if (vis.length) shown.push({ col: col, x: x, vis: vis, G: geom(col, vis) });+ });+ // eighths in pairs on one beat share a beam (written once the second one is heard)+ var byKey = {}; shown.forEach(function (c) { byKey[c.col.staff + '|' + c.col.beat] = c; });+ shown.forEach(function (c) {+ if (c.G.dur !== 0.5 || c.col.beat % 1 !== 0) return;+ var d = byKey[c.col.staff + '|' + (c.col.beat + 0.5)];+ if (!d || d.G.dur !== 0.5) return;+ var mid = c.col.staff === 't' ? TT + 2 * SP : BT + 2 * SP;+ var up = Math.max(c.G.bot, d.G.bot) - mid >= mid - Math.min(c.G.top, d.G.top);+ c.G.up = d.G.up = up;+ var ya = up ? c.G.top - 3.5 * SP : c.G.bot + 3.5 * SP, yb = up ? d.G.top - 3.5 * SP : d.G.bot + 3.5 * SP;+ var dy = Math.max(-0.75 * SP, Math.min(0.75 * SP, yb - ya)), y0 = up ? Math.min(ya, yb - dy) : Math.max(ya, yb - dy);+ c.beam = { to: d, y0: y0, dy: dy }; c.tip = y0; d.tip = y0 + dy; d.beamed = true;+ });+ g.shadowColor = C.l(.8); g.shadowBlur = C.glow;+ shown.forEach(function (c) { c.sx = drawColumn(k, c.col, c.x, c.vis, c.G, c.tip); });+ shown.forEach(function (c) {+ if (!c.beam || c.sx == null || c.beam.to.sx == null) return;+ var d = c.beam.to, hot = Math.max.apply(null, c.vis.concat(d.vis).map(function (v) { return v.s; })), th = (c.G.up ? 1 : -1) * 0.5 * SP;+ g.save(); if (hot > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 14 * hot; }+ g.beginPath(); g.moveTo(c.sx - 0.8, c.beam.y0); g.lineTo(d.sx + 0.8, c.beam.y0 + c.beam.dy);+ g.lineTo(d.sx + 0.8, c.beam.y0 + c.beam.dy + th); g.lineTo(c.sx - 0.8, c.beam.y0 + th); g.closePath();+ g.fillStyle = hot > 0.05 ? C.hot : C.ink; g.fill(); g.restore();+ });+ } else {+ // no notes.json: the rhythm only, a slash on the treble staff per strike+ k.ev.forEach(function (e, ei) {+ if (e.kind === 'skip' || f < e.frame - RISE) return;+ var x = X(k.beat(e.t)), s = k.env(ei), y = TT + 2 * SP;+ if (x < hx - 20 || x > W + 20) return;+ g.save(); if (s > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 18 * s; }+ line(g, x - 8, y + 8, x + 8, y - 8, s > 0.05 ? C.hot : C.ink, 5); line(g, x + 7, y - 6, x + 7, y - 3.5 * SP, s > 0.05 ? C.hot : C.ink, 1.7);+ g.restore();+ });+ }+ g.restore();+ // the notes fade out under the clefs; the staff lines run on+ g.save(); g.shadowBlur = 0;+ var gr = g.createLinearGradient(hx, 0, hx + 70, 0); gr.addColorStop(0, C.bg(1)); gr.addColorStop(1, C.bg(0));+ g.fillStyle = gr; g.fillRect(hx - 2, 0, 72, KY - 8);+ for (i = 0; i < 5; i++) { line(g, hx - 2, TT + i * SP, hx + 70, TT + i * SP, C.l(.5), 1.3); line(g, hx - 2, BT + i * SP, hx + 70, BT + i * SP, C.l(.5), 1.3); }+ // the system: brace, line, clefs, key signature, time signature+ ribbon(g, [[10, -TT, 0.6], [4, -(TT + 30), 2.4], [5, -((TT + BB) / 2 - 22), 3.4], [-1, -(TT + BB) / 2, 0.8],+ [5, -((TT + BB) / 2 + 22), 3.4], [4, -(BB - 30), 2.4], [10, -BB, 0.6]], 0, 0, 1, C.i(.85));+ line(g, 14, TT, 14, BB, C.l(.7), 2);+ treble(g, 24, C.i(.92)); bass(g, 26, C.i(.92));+ for (var a = 0; a < Math.abs(M.fifths); a++) {+ var tStep = M.fifths > 0 ? [38, 35, 39, 36, 33, 37, 34][a] : [34, 37, 33, 36, 32, 35, 31][a];+ accidental(g, M.fifths > 0 ? 1 : -1, 90 + a * 12, yOf('t', tStep), C.i(.92));+ accidental(g, M.fifths > 0 ? 1 : -1, 90 + a * 12, yOf('b', tStep - 14), C.i(.92));+ }+ if (M.meter) {+ g.font = '800 46px "Bricolage Grotesque"'; g.fillStyle = C.i(.92); g.textAlign = 'center'; g.textBaseline = 'alphabetic';+ [[TT, TB], [BT, BB]].forEach(function (s) { g.fillText(String(M.meter), M.tsX + 14, s[0] + 2 * SP + 1); g.fillText('4', M.tsX + 14, s[1] + 1); });+ }+ g.restore();+ // the playhead: where a note is written the frame it is heard+ g.save(); g.shadowBlur = 0;+ var ph = k.hot ? k.s : 0; // every strike flashes the playhead, pitch known or not+ if (ph > 0.05) { g.shadowColor = C.hot; g.shadowBlur = 16 * ph; }+ line(g, PLAY, TT - 2.2 * SP, PLAY, BB + 2.2 * SP, C.h(.55 + .45 * ph), 2 + 2.5 * ph);+ g.shadowBlur = 0;+ g.beginPath(); g.moveTo(PLAY - 7, TT - 2.2 * SP - 10); g.lineTo(PLAY + 7, TT - 2.2 * SP - 10); g.lineTo(PLAY, TT - 2.2 * SP); g.closePath(); g.fillStyle = C.h(.8); g.fill();+ g.restore();+ // the keyboard: 88 keys, the sounding ones down and lit, red on their strike+ var lit = {};+ M.notes.forEach(function (n) {+ if (f < n.show - RISE || now > n.end + 0.05) return;+ var s = n.ev != null ? k.env(n.ev) : 0, v = Math.max(s, f >= n.show && now <= n.end ? 0.32 : 0);+ if (v > (lit[n.pitch] || 0)) lit[n.pitch] = v;+ });+ var whites = [], ww;+ for (var m = 21; m <= 108; m++) if ([1, 3, 6, 8, 10].indexOf(m % 12) < 0) whites.push(m);+ ww = W / whites.length;+ g.save(); g.shadowBlur = 0;+ line(g, 0, KY - 4, W, KY - 4, C.l(.35), 2);+ whites.forEach(function (m, wi) {+ var v = lit[m] || 0, x = wi * ww, dy = v > 0.05 ? 2.5 : 0;+ g.beginPath(); g.rect(x + 1, KY + dy, ww - 2, KH - dy);+ if (v > 0.05) { g.fillStyle = C.h(Math.min(1, 0.25 + 0.75 * v)); g.fill(); }+ g.strokeStyle = C.l(.5); g.lineWidth = 1.2; g.stroke();+ if (m === 60) disc(g, x + ww / 2, KY + KH - 9, 2.6, C.l(.8));+ });+ whites.forEach(function (m, wi) {+ if (m + 1 > 108 || [1, 3, 6, 8, 10].indexOf((m + 1) % 12) < 0) return;+ var v = lit[m + 1] || 0, x = (wi + 1) * ww - ww * 0.31, dy = v > 0.05 ? 2 : 0;+ g.beginPath(); g.rect(x, KY + dy, ww * 0.62, KH * 0.6);+ g.fillStyle = C.bg(1); g.fill();+ g.fillStyle = v > 0.05 ? C.h(Math.min(1, 0.35 + 0.65 * v)) : C.l(.42); g.fill();+ g.strokeStyle = C.l(.7); g.lineWidth = 1.2; g.stroke();+ });+ g.restore();+ // words: the key and tempo heard, and where the playhead is+ var info = [M.keyName ? M.keyName.toUpperCase() : null, M.has ? Math.round(M.bpm) + ' BPM' : null].filter(Boolean).join(' · ');+ if (info) text(k, info, 0, 26, 30, C.muted, 'left');+ if (M.meter && bNow >= M.down) {+ var rel = bNow - M.down, barN = Math.floor(rel / M.meter) + 1, beatN = Math.floor(rel % M.meter) + 1;+ if (M.has && bNow > M.last + M.meter) barN = null;+ if (barN) text(k, 'BAR ' + barN + ' · ' + beatN, PLAY + 8, 26, 30, C.ink, 'right');+ }+ g.restore();+ }+ });+})();diff --git a/skills/showtime/library/xray/switch/README.md b/skills/showtime/library/xray/switch/README.mdnew file mode 100644index 0000000..937105b--- /dev/null+++ b/skills/showtime/library/xray/switch/README.md@@ -0,0 +1,16 @@+# Snap-action switch (an old keyboard key, a microswitch, a light switch)++Parts: the lever (the key presses its tip), its pivot on the post, the over-centre spring from the lever's tail to+the contact blade, the moving contact on the blade, the fixed contact. In the panel, a circuit with a lamp.++- An event = a click: the spring's line of action crosses the pivot (dead centre) and the blade snaps, peaking on+ the event's frame. Presses and releases alternate, starting with a press; `"kinds": {"<i>": "release"}` fixes one.+- When the switch is visible in the clip (a cut-away, a bare switch), the in-place view marks the real parts (the+ pivot, the spring's line of action, the contact flashing on each snap) instead of redrawing them; the panel draws+ the schematic and the circuit, lit while the contacts are closed.+- Anchors to place on the reference frame: `pivot`, `contact`, `base_left`, `base_right`.+- Measured: click times and count (the clip's sound). Drawn: a generic snap-action switch; press or release is+ inferred from the order of the clicks.++Verified on: a 1959 keyboard switch, close up ("Clicking a 1959 Cherry mechanical keyboard switch", LGR, Wikimedia+Commons, CC BY 3.0): https://commons.wikimedia.org/wiki/File:Clicking_a_1959_Cherry_mechanical_keyboard_switch.webmdiff --git a/skills/showtime/library/xray/switch/mechanism.js b/skills/showtime/library/xray/switch/mechanism.jsnew file mode 100644index 0000000..bd41fe8--- /dev/null+++ b/skills/showtime/library/xray/switch/mechanism.js@@ -0,0 +1,78 @@+/* A snap-action switch, inside (showtime X-ray mechanism library: switch): the kind under an old keyboard key, a+ microswitch, a light switch. Design space: the switch seen from the side, about 1000 x 600 units: the base along+ the bottom, the post with the lever's pivot in the middle, the over-centre spring from the lever's tail to the+ contact blade, the fixed contact on the right.+ Each event is a CLICK: the spring's line of action crosses the pivot (dead centre) and the blade snaps, closing+ the contacts on a press and opening them on a release, PEAKING on the event's frame. Presses and releases+ alternate from a press; "xray": {"kinds": {"3": "release"}} overrides one. When the switch itself is visible in+ the clip (as in a cut-away), the in-place view does not redraw its moving parts over the real ones: it marks the+ pivot, the spring's line of action and the contact, which flashes on each snap; the panel draws the schematic and+ a circuit whose lamp lights while the contacts are closed. */+(function () {+ var PIV = [840, 460], TAIL_UP = [450, 375], TAIL_DN = [480, 570], TIP_UP = [1190, 345], TIP_DN = [1040, 240];+ var BLADE0 = [880, 560], CONTACT = [1150, 575], FIXED = [[1050, 532], [1280, 532], [1280, 562], [1050, 562]];+ var BASE = [[330, 700], [1460, 700], [1460, 760], [330, 760]], POST = [[820, 360], [860, 360], [860, 710], [820, 710]];+ function lerp(a, b, s) { return [a[0] + (b[0] - a[0]) * s, a[1] + (b[1] - a[1]) * s]; }+ function presses(k) { // pressed (1) or not (0) after each event, and the snap around it+ var st = 0, out = [];+ k.ev.forEach(function (e) {+ if (e.kind === 'skip') { out.push(null); return; }+ var kind = e.kind === 'event' ? (st ? 'release' : 'press') : e.kind;+ st = kind === 'press' ? 1 : 0; out.push(st);+ });+ return out;+ }+ function state(k) { // 0 released .. 1 pressed at this frame: steps over the 3 frames before each click+ var p = presses(k), s = 0;+ k.ev.forEach(function (e, i) {+ if (p[i] == null) return;+ var d = k.since(i);+ if (d >= 0) s = p[i];+ else if (d > -3) { var prev = s, a = (d + 3) / 3; s = prev + (p[i] - prev) * a * a; }+ });+ return s;+ }++ XRAY.mechanism({+ name: 'switch',+ noun: 'clicks', noun1: 'click', one: 'a click',+ box: [300, 160, 1700, 780],+ anchors: { pivot: PIV, contact: CONTACT, base_left: [330, 700], base_right: [1460, 700] },+ outline: BASE.concat([[1460, 200], [330, 200]]),+ headline: function (n) { return '<em>' + n + '</em> clicks,<br>each one a spring<br>passing dead centre.'; },+ measured: function () { return 'Each click lit up on the frame its sound was heard; presses and releases alternate.'; },+ drawn: 'Inside view: a generic snap-action switch, drawn; the parts are marked on the real switch, not measured.',+ // the live page: pads that play the next event of each kind from the real clip+ touch: { verb: 'Click it', by: 'one' },+ draw: function (k) {+ var C = k.C, g = k.g, s = state(k), panel = k.view === 'panel';+ var tail = lerp(TAIL_DN, TAIL_UP, s), tip = lerp(TIP_DN, TIP_UP, s);+ var blade = [CONTACT[0], CONTACT[1] + (1 - s) * 34]; // the blade's contact end: up against the fixed contact when pressed+ var closed = s > 0.5, snap = k.hot ? k.s : 0;+ if (panel) {+ k.poly(BASE, C.soft, 2, C.l(.06)); k.poly(POST, C.line, 2.5);+ k.poly(FIXED, closed ? C.hot : C.line, 3);+ k.seg(tail, PIV, C.line, 6); k.seg(PIV, tip, C.line, 6); // the lever+ k.seg(BLADE0, blade, closed ? C.hot : C.line, 5); // the contact blade+ k.dot(blade, Math.max(4, 9 * k.scale), closed ? C.hot : C.line);+ // the circuit: terminals -> lamp -> back; lit while closed+ var L = [1560, 300];+ k.seg([1280, 520], [1560, 520], C.soft, 2); k.seg([1560, 520], [1560, 330], C.soft, 2);+ k.seg([1460, 730], [1600, 730], C.soft, 2); k.seg([1600, 730], [1600, 300], C.soft, 2);+ k.ell(L[0] + 20, L[1], 34, 34, closed ? C.hot : C.soft, 3, 0, false);+ if (closed) { g.save(); g.shadowColor = C.hot; g.shadowBlur = 30; k.dot([L[0] + 20, L[1]], Math.max(6, 22 * k.scale), C.h(.85)); g.restore(); }+ k.label(closed ? 'circuit closed' : 'circuit open', [L[0] + 20, L[1] - 34], -40, -60, closed);+ }+ // the spring and its line of action through dead centre (both views)+ var coils = [], n = 14;+ for (var i = 0; i <= n * 2; i++) { var q = lerp(tail, BLADE0, i / (n * 2)), dx = BLADE0[0] - tail[0], dy = BLADE0[1] - tail[1], L2 = Math.hypot(dx, dy), w = (i % 2 ? 1 : -1) * 16; coils.push([q[0] - dy / L2 * w, q[1] + dx / L2 * w]); }+ g.strokeStyle = snap > 0.05 ? C.hot : (panel ? C.line : C.l(.75)); g.lineWidth = k.lw(panel ? 3 : 2.5); k.path(coils); g.stroke();+ g.setLineDash([10, 10]); k.seg([tail[0] - (BLADE0[0] - tail[0]) * 0.15, tail[1] - (BLADE0[1] - tail[1]) * 0.15], [BLADE0[0] + (BLADE0[0] - tail[0]) * 0.25, BLADE0[1] + (BLADE0[1] - tail[1]) * 0.25], snap > 0.05 ? C.hot : C.l(.6), 2); g.setLineDash([]);+ k.ell(PIV[0], PIV[1], 16, 16, snap > 0.05 ? C.hot : C.line, 3, 0, false);+ if (snap > 0.05) { g.save(); g.shadowColor = C.hot; g.shadowBlur = 26 * snap; k.dot(CONTACT, Math.max(6, 18 * snap * Math.min(1.6, k.scale + 0.4)), C.h(.9)); g.restore(); }+ k.label('pivot', PIV, panel ? -40 : -200, panel ? -190 : -150, false);+ k.label('over-centre spring', [(tail[0] + BLADE0[0]) / 2, (tail[1] + BLADE0[1]) / 2], -160, panel ? 150 : 170, snap > 0.05);+ k.label('contacts', CONTACT, 120, panel ? -150 : 150, closed);+ }+ });+})();diff --git a/skills/showtime/library/xray/typewriter/README.md b/skills/showtime/library/xray/typewriter/README.mdnew file mode 100644index 0000000..6519610--- /dev/null+++ b/skills/showtime/library/xray/typewriter/README.md@@ -0,0 +1,16 @@+# Typewriter (manual, front-strike)++Parts: key levers, the typebar segment (a fan of typebars), the print point on the platen, the ribbon and its+spools, the escapement star wheel and carriage rack, the mainspring, the carriage return lever.++- An event = a keystroke: the key goes down and its typebar swings up to the print point, peaking on the+ event's frame; the ribbon lifts; the escapement lets the carriage step one letter on that frame.+- `"kinds": {"<i>": "return"}`: a carriage return winds the mainspring back.+- Which key: only from evidence, `"text"` (what the paper shows, one character per keystroke). Without it a+ keystroke lights the typebar fan, never a guessed key.+- Anchors to place on the reference frame: `key_q`, `key_p`, `key_m` (key centres) and `print_point`.+- Measured: keystroke times and count (the clip's sound), the text (the paper). Drawn: a generic manual+ typewriter, not this exact machine.++Verified on: Underwood 315 portable (Erik Pålsson / Sounds of Changes, CC BY 3.0: https://commons.wikimedia.org/wiki/File:Underwood_315_portable_typewriter.webm), 18 of 18 keystrokes on+their frame.diff --git a/skills/showtime/library/xray/typewriter/diagram.js b/skills/showtime/library/xray/typewriter/diagram.jsnew file mode 100644index 0000000..0f41eed--- /dev/null+++ b/skills/showtime/library/xray/typewriter/diagram.js@@ -0,0 +1,343 @@+/* A manual typewriter, inside, as a living diagram (showtime X-ray library: typewriter, the "diagram" layout).++ Registration: this file adds one builder to a global table,+ XRAY_DIAGRAM.typewriter = function (ctx) { return SCENE; }+ and the X-ray diagram page (templates/xray/diagram.html) calls it once xray.json and showtime.json are loaded, then+ mounts the result with Diagram.mount(SCENE). A builder for another object registers its own key the same way+ (XRAY_DIAGRAM.musicbox = ...) and the page picks it from showtime.json "xray": {"mechanism": "musicbox"}.++ ctx (from the page):+ events [{i, frame, kind, ch}]: every event of xray.json; frame is the FILM frame (source frame + offset_frames),+ kind 'event' (a keystroke), 'skip' (showtime.json xray.skip: not drawn) or another kind ('return'),+ ch the character read off the paper for that keystroke (showtime.json xray.text), or null+ fps the film's frame rate; every time below is frame / fps, so a strike PEAKS on its event's frame+ cue CUE from cue.js (labels: when the part names arrive, endcard: when they leave)+ region [x0, y0, x1, y1] design px (the 1920x1080 diagram canvas) the machine is fitted into+ safe [x0, y0, x1, y1] design px the part labels stay inside; clip [x, y, w, h]: the drawing is cut to it+ portrait true on a 9:16 page: part names only (no second line), fewer of them+ theme the Diagram theme in use (Diagram.themes.dark or .light): colours for the pieces drawn by onFrame++ The drawing follows engineering-drawing habits: hidden lines (the key levers under the keyboard) dashed, centre+ lines dash-dot (the platen's axis, the segment's centre), the side frames cut and hatched, the cover removed and+ shown as a dashed ghost, one line weight per role, the solids shaded by the theme's three face tones.++ What moves, per keystroke (film frame f): its key goes down and lights (explode + pulse, peak on f), a packet runs+ key -> typebar -> print point arriving on f, its typebar swings up out of the basket along its drawn arc and hits+ the print point on f (rising 3 frames before, settling over 7 after, a short ghost trail behind it), the ribbon+ lifts, the print point flashes, the escapement wheel lets go one tooth and the carriage steps one letter left from+ f; the ribbon spools and the mainspring turn with the letter count. Which key: only from ctx's ch (what the paper+ in the clip shows); without it the keystroke lights the whole typebar fan, never a guessed key. A space lights the+ space bar and only steps the carriage. An event of kind 'return' winds the mainspring back and runs the carriage+ home. The parts the library has no shape for (typebars, levers, star wheel, spiral, ribbon) are drawn by the+ scene's onFrame hook as a pure function of t, under and over the library's parts. */+(function () {+ 'use strict';+ var REG = window.XRAY_DIAGRAM = window.XRAY_DIAGRAM || {};+ var C30 = Math.cos(Math.PI / 6), deg = function (a) { return a * Math.PI / 180; };+ var clamp = function (x, a, b) { return Math.min(b, Math.max(a, x)); };+ var lerp = function (a, b, p) { return a + (b - a) * p; };+ var inOutSine = function (p) { return -(Math.cos(Math.PI * p) - 1) / 2; };+ var inOutCubic = function (p) { return p < 0.5 ? 4 * p * p * p : 1 - Math.pow(-2 * p + 2, 3) / 2; };+ var RISE = 3, FALL = 7, STEPF = 4; // frames: a strike rises 3 before its frame, settles over 7; a carriage step takes 4++ // ---------------------------------------------------------------- the machine, in world units (x: the machine's+ // width, left to right; y: back to front, the typist at +y; z: up)+ var ROWS = [['qwertyuiop', 7.0, 24.5, 4.6], ['asdfghjkl', 8.2, 27.5, 3.8], ['zxcvbnm,.-', 9.4, 30.5, 3.0]];+ var PITCH = 3.2, KR = 1.15;+ var KEYS = []; // [ch, x, y, z]+ ROWS.forEach(function (r) { r[0].split('').forEach(function (ch, i) { KEYS.push([ch, r[1] + i * PITCH, r[2], r[3]]); }); });+ var BYX = KEYS.slice().sort(function (a, b) { return a[1] - b[1] || a[2] - b[2]; }), NB = BYX.length, BAR = {};+ BYX.forEach(function (k, i) { BAR[k[0]] = i; });+ var PLATEN = { x0: 7, x1: 37, y: 7, z: 8.6, r: 1.7 };+ var PP = [22, PLATEN.y + PLATEN.r, PLATEN.z]; // the print point: the front of the platen+ var SEG = { r: 4.6, z: 4.8 }; // the typebar segment: an arc of pivots around the print point+ var LB = Math.hypot(SEG.r, PP[2] - SEG.z); // a typebar's length (pivot to type)+ var TH_REST = deg(-8), TH_HIT = Math.atan2(PP[2] - SEG.z, -SEG.r);+ var SPACE = { at: [13, 33.0, 1.8], size: [18, 1.7, 0.7] };+ var SHIFT = [5.6, 30.5, 3.0];+ var ESC = [38.6, 0.9, 7.7]; // the escapement star wheel (its top face), 16 teeth+ var SPRING = [4.6, 12.4, 4.4]; // the mainspring drum (its top face)+ var SPOOLS = [[14.2, 10.8, 6.7], [29.8, 10.8, 6.7]];+ var STEP = 0.52; // the carriage's step per letter, world units++ function barAngle(i) { return deg(168 - 156 * i / (NB - 1)); }+ function radial(i) { var a = barAngle(i); return [Math.cos(a), Math.sin(a)]; }+ function pivot(i) { var d = radial(i); return [PP[0] + SEG.r * d[0], PP[1] + SEG.r * d[1], SEG.z]; }+ function tip(i, s) { // the typebar's type end: s = 0 at rest, 1 on the platen+ var p = pivot(i), d = radial(i), th = lerp(TH_REST, TH_HIT, s);+ return [p[0] + LB * Math.cos(th) * d[0], p[1] + LB * Math.cos(th) * d[1], p[2] + LB * Math.sin(th)];+ }+ function keyOf(ch) { for (var i = 0; i < KEYS.length; i++) if (KEYS[i][0] === ch) return KEYS[i]; return null; }+ function stemFoot(k) { return [k[1], k[2], k[3] - 2.2]; }+ function lever(k) { var pv = pivot(BAR[k[0]]); return [stemFoot(k), [pv[0], pv[1] + 1.4, 1.5], [pv[0], pv[1], pv[2] - 0.25]]; }+ function keyId(ch) { return 'k-' + ({ ',': 'comma', '.': 'period', '-': 'dash' }[ch] || ch); }++ // a horizontal cylinder along x, as faces (the library's cylinder stands on z): the body's outline and the x+ cap+ function hull2(pts) {+ var p = pts.map(function (q, i) { return [q[0], q[1], i]; }).sort(function (a, b) { return a[0] - b[0] || a[1] - b[1]; });+ var cross = function (o, a, b) { return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]); }, lo = [], up = [], i;+ for (i = 0; i < p.length; i++) { while (lo.length >= 2 && cross(lo[lo.length - 2], lo[lo.length - 1], p[i]) <= 0) lo.pop(); lo.push(p[i]); }+ for (i = p.length - 1; i >= 0; i--) { while (up.length >= 2 && cross(up[up.length - 2], up[up.length - 1], p[i]) <= 0) up.pop(); up.push(p[i]); }+ up.pop(); lo.pop(); return lo.concat(up).map(function (q) { return q[2]; });+ }+ function hcyl(x0, x1, y, z, r, body, cap) {+ var circ = function (x, rr) { var a = []; for (var k = 0; k < 36; k++) { var th = k * Math.PI / 18; a.push([x, y + rr * Math.cos(th), z + rr * Math.sin(th)]); } return a; };+ var both = circ(x0, r).concat(circ(x1, r)), scr = both.map(function (p) { return [(p[0] - p[1]) * C30, (p[0] + p[1]) * 0.5 - p[2]]; });+ var out = hull2(scr).map(function (i) { return both[i]; });+ return [{ kind: 'face', pts: out, tone: body || 'left' }, { kind: 'face', pts: circ(x1, r), tone: cap || 'top' },+ { kind: 'face', pts: circ(x1, r * 0.42), tone: 'recess', lw: 0.6, stroke: 'detail' },+ { kind: 'line', pts: [[x0 + 0.3, y + r * 0.5, z + r * 0.87], [x1 - 0.1, y + r * 0.5, z + r * 0.87]], lw: 0.5 }];+ }++ REG.typewriter = function (ctx) {+ var fps = ctx.fps || 30, ev = ctx.events || [], cue = ctx.cue || {}, portrait = !!ctx.portrait, TH = ctx.theme || {};+ var dark = TH.name !== 'light' && TH.name !== 'white';+ var hotC = (TH.tones && TH.tones.hot && TH.tones.hot.flow) || '#FF6A3D', inkC = TH.ink || '#9FB4CF', detC = TH.detail || '#4E6585';+ var kept = ev.filter(function (e) { return e.kind !== 'skip'; });+ var letters = kept.filter(function (e) { return e.kind === 'event'; });+ var T = function (f) { return f / fps; };+ var t0 = cue.labels != null ? cue.labels : 1.5, tEnd = cue.endcard != null ? cue.endcard : 1e9;++ // ---- fit the machine into the region (design px): its screen box at scale 1, origin 0+ var corners = [];+ [[1, 0, 0], [43, 36, 0], [1, 36, 0], [43, 0, 0], [1, 0, 10.6], [43, 0, 10.6], [13, 4.8, 15.2], [31, 4.8, 15.2], [43, 36, 5.4]].forEach(function (p) {+ corners.push([(p[0] - p[1]) * C30, (p[0] + p[1]) * 0.5 - p[2]]);+ });+ var bx0 = Math.min.apply(null, corners.map(function (c) { return c[0]; })), bx1 = Math.max.apply(null, corners.map(function (c) { return c[0]; }));+ var by0 = Math.min.apply(null, corners.map(function (c) { return c[1]; })), by1 = Math.max.apply(null, corners.map(function (c) { return c[1]; }));+ var R = ctx.region || [1000, 230, 1790, 930];+ var S = Math.min((R[2] - R[0]) / (bx1 - bx0), (R[3] - R[1]) / (by1 - by0));+ var O = [(R[0] + R[2]) / 2 - (bx0 + bx1) / 2 * S, (R[1] + R[3]) / 2 - (by0 + by1) / 2 * S];+ var scr = function (p) { return [O[0] + (p[0] - p[1]) * C30 * S, O[1] + (p[0] + p[1]) * 0.5 * S - p[2] * S]; };++ // ---- the parts (library parts where they fit, plain shapes otherwise)+ var items = [+ // the frame: a base plate, the cover removed (a dashed ghost), the side frames cut (hatched sections), the rail+ { id: 'base', layer: 'floor', shapes: [{ kind: 'box', at: [1, 0, 0], size: [42, 36, 0.8], tone: 'padFaces' }], shadow: false },+ { id: 'cover', layer: 'floor', shapes: [{ kind: 'ghost', at: [1, 0, 0.8], size: [42, 36, 9.6] }] },+ // the side frames, cut away behind the keyboard: only their front halves stand, their cut faces hatched+ { id: 'side-l', shapes: [{ kind: 'box', at: [1, 17, 0.8], size: [1.1, 17, 4.2], tone: 'wallFaces' }, { kind: 'hatch', at: [1, 17, 0.8], size: [1.1, 17, 4.2], endFace: true }],+ bbox: [[1, 17, 0.8], [2.1, 34, 5]] },+ { id: 'side-r', shapes: [{ kind: 'box', at: [41.9, 17, 0.8], size: [1.1, 17, 4.2], tone: 'wallFaces' }, { kind: 'hatch', at: [41.9, 17, 0.8], size: [1.1, 17, 4.2], endFace: true }],+ bbox: [[41.9, 17, 0.8], [43, 34, 5]] },+ { id: 'post-l', part: 'box', at: [3.0, 2.6, 0.8], size: [1.2, 1.4, 5.4], bevel: 0 },+ { id: 'post-r', part: 'box', at: [39.8, 2.6, 0.8], size: [1.2, 1.4, 5.4], bevel: 0 },+ { id: 'rail', part: 'box', at: [2.4, 2.4, 6.2], size: [39.2, 1.8, 0.9], bevel: 0, faces: 'padFaces' },+ ];+ // the carriage: platen, knobs, the paper with its typed lines, the rack; it steps left one letter per keystroke+ var paper = [{ kind: 'face', pts: [[13, 6.1, 10.1], [31, 6.1, 10.1], [31, 4.8, 15.2], [13, 4.8, 15.2]], tone: 'blade' }];+ for (var rl = 0; rl < 5; rl++) paper.push({ kind: 'line', pts: [[15, 5.95 - rl * 0.24, 10.7 + rl * 0.95], [15 + (rl === 4 ? 6 : 13), 5.95 - rl * 0.24, 10.7 + rl * 0.95]], lw: 0.45 });+ var car = [];+ car.push({ kind: 'box', at: [7, 4.3, 7.15], size: [30, 0.5, 0.45], bevel: 0, tone: 'padFaces' });+ for (var rk = 0; rk < 30; rk++) car.push({ kind: 'line', pts: [[7.5 + rk, 4.3, 7.4], [7.9 + rk, 4.3, 7.15]], lw: 0.5 });+ car = car.concat(hcyl(4.4, 7, PLATEN.y, PLATEN.z, 1.25, 'right', 'top'), hcyl(PLATEN.x0, PLATEN.x1, PLATEN.y, PLATEN.z, PLATEN.r, 'left', 'top'),+ hcyl(PLATEN.x1, 39.6, PLATEN.y, PLATEN.z, 1.25, 'right', 'top'));+ items.push({ id: 'paper', shapes: paper, bbox: [[13, 4.8, 10.1], [31, 6.1, 15.2]], behind: ['carriage'] });+ items.push({ id: 'carriage', shapes: car, bbox: [[4.4, 4.3, 6.9], [39.6, 8.7, 10.3]], front: ['rail'] });+ // ribbon spools, the escapement's arbor and disc, the mainspring drum (their moving faces: onFrame)+ SPOOLS.forEach(function (p, i) {+ items.push({ id: 'spool-' + 'lr'[i], shapes: [{ kind: 'cyl', at: [p[0], p[1], 0.8], r: 0.35, h: p[2] - 1.5 }, { kind: 'cyl', at: [p[0], p[1], p[2] - 0.7], r: 1.9, h: 0.7, flat: true }],+ bbox: [[p[0] - 1.9, p[1] - 1.9, 0.8], [p[0] + 1.9, p[1] + 1.9, p[2]]], footprint: [p[0] - 0.6, p[1] - 0.6, 1.2, 1.2] });+ });+ items.push({ id: 'esc-arbor', shapes: [{ kind: 'box', at: [ESC[0] - 0.5, ESC[1] - 0.5, 0.8], size: [1.0, 1.0, ESC[2] - 1.15], bevel: 0 }], bbox: [[ESC[0] - 0.5, ESC[1] - 0.5, 0.8], [ESC[0] + 0.5, ESC[1] + 0.5, ESC[2] - 0.35]] });+ items.push({ id: 'escapement', shapes: [{ kind: 'cyl', at: [ESC[0], ESC[1], ESC[2] - 0.35], r: 1.8, h: 0.35, flat: true }], bbox: [[ESC[0] - 1.8, ESC[1] - 1.8, ESC[2] - 0.35], [ESC[0] + 1.8, ESC[1] + 1.8, ESC[2]]], front: ['esc-arbor'] });+ items.push({ id: 'mainspring', shapes: [{ kind: 'cyl', at: [SPRING[0], SPRING[1], 0.8], r: 2.1, h: SPRING[2] - 0.8, flat: true, bands: [1.1] }],+ footprint: [SPRING[0] - 2.1, SPRING[1] - 2.1, 4.2, 4.2] });+ // the typebar basket and segment: drawn by onFrame; this empty item only tells the label layout where they are+ items.push({ id: 'basket', shapes: [], bbox: [[PP[0] - 10.5, PP[1] - 0.5, 3.6], [PP[0] + 10.5, PP[1] + 10.5, 5.4]], shadow: false, front: ['rail', 'carriage'] });+ // the keyboard: one part per key (a cap on a stem), the shift key and the space bar+ KEYS.forEach(function (k) {+ items.push({ id: keyId(k[0]), shapes: [{ kind: 'line', pts: [[k[1], k[2], k[3]], stemFoot(k)], lw: 0.9, stroke: 'ink' },+ { kind: 'cyl', at: [k[1], k[2], k[3]], r: KR, h: 0.5, cap: 0.68 }], bbox: [[k[1] - KR, k[2] - KR, k[3] - 2.2], [k[1] + KR, k[2] + KR, k[3] + 0.5]] });+ });+ items.push({ id: 'k-shift', shapes: [{ kind: 'line', pts: [SHIFT, [SHIFT[0], SHIFT[1], SHIFT[2] - 2.2]], lw: 0.9, stroke: 'ink' },+ { kind: 'cyl', at: SHIFT, r: KR * 1.15, h: 0.5, cap: 0.68 }], bbox: [[SHIFT[0] - 1.4, SHIFT[1] - 1.4, SHIFT[2] - 2.2], [SHIFT[0] + 1.4, SHIFT[1] + 1.4, SHIFT[2] + 0.5]] });+ items.push({ id: 'k-space', part: 'box', at: SPACE.at, size: SPACE.size, bevel: 0.25, shadow: false });++ // ---- per keystroke: pulses (peak on the frame), the key's travel, the packet, the carriage step+ var states = [], explode = [], tokens = [], carKeys = []; // carKeys: [frame, dx in letters, frames] for onFrame+ var n = 0;+ function pulse(id, f, amount, tone) { states.push({ target: id, at: T(f - RISE), dur: T(2 * RISE), tone: tone || 'hot', pulse: true, amount: amount == null ? 1 : amount }); }+ kept.forEach(function (e, j) {+ var f = e.frame;+ if (e.kind === 'return') { // a carriage return: the carriage runs home, the spring winds back+ explode.push({ at: T(f), dur: T(12), offsets: { carriage: [n * STEP, 0, 0], paper: [n * STEP, 0, 0] } });+ carKeys.push([f, n, 12]); pulse('mainspring', f, 0.8, 'warm');+ n = 0; return;+ }+ if (e.kind !== 'event') return;+ var ch = e.ch, lc = ch ? ch.toLowerCase() : null, key = lc && lc !== ' ' ? keyOf(lc) : null;+ if (ch === ' ') {+ pulse('k-space', f, 1);+ explode.push({ at: T(f - RISE), dur: T(RISE), back: T(f), offsets: { 'k-space': [0, 0, -0.5] } });+ tokens.push({ id: 'pk' + j, tone: 'hot', r: 0.7, at: T(f - 9), from: [22, 33.9, 2.6],+ moves: [{ at: T(f - 6), dur: T(6), to: [ESC[0], ESC[1], ESC[2] + 0.2], hop: 4 }], until: T(f + 1) });+ } else if (key) {+ var id = keyId(lc), top = [key[1], key[2], key[3] + 0.5], pv = pivot(BAR[lc]), o = {};+ pulse(id, f, 1);+ o[id] = [0, 0, -0.7];+ explode.push({ at: T(f - RISE), dur: T(RISE), back: T(f), offsets: o });+ if (ch !== lc) pulse('k-shift', f, 0.9);+ tokens.push({ id: 'pk' + j, tone: 'hot', r: 0.7, at: T(f - 9), from: top,+ moves: [{ at: T(f - 6), dur: T(3), to: [pv[0], pv[1], pv[2] + 0.3] }, { at: T(f - 3), dur: T(3), to: PP }], until: T(f + 1) });+ }+ pulse('escapement', f, 0.45);+ n++;+ explode.push({ at: T(f), dur: T(STEPF), offsets: { carriage: [-STEP, 0, 0], paper: [-STEP, 0, 0] } });+ carKeys.push([f, -1, STEPF]);+ });++ // ---- the moving pieces the part library has no shape for, drawn every frame from t (pure function of t)+ var barEv = {}; // bar index -> its keystrokes' frames ('*' = the whole fan)+ letters.forEach(function (e) {+ var lc = e.ch ? e.ch.toLowerCase() : null;+ if (lc === ' ') return;+ var b = lc != null && BAR[lc] != null ? BAR[lc] : (e.ch == null ? '*' : null);+ if (b == null) return;+ (barEv[b] = barEv[b] || []).push(e.frame);+ });+ var stepFrames = letters.map(function (e) { return e.frame; });+ var hitFrames = letters.filter(function (e) { return e.ch !== ' '; }).map(function (e) { return e.frame; });+ function env(ft, f) { // 0..1, exactly 1 on the event's frame+ var d = ft - f;+ if (d < -RISE || d > FALL) return 0;+ if (d <= 0) { var a = (d + RISE) / RISE; return a * a; }+ return 1 - inOutSine(d / FALL);+ }+ function barS(i, ft) { var s = 0; (barEv[i] || []).forEach(function (f) { s = Math.max(s, env(ft, f)); }); return s; }+ function stepsAt(ft) { var s = 0; stepFrames.forEach(function (f) { s += inOutCubic(clamp((ft - f) / STEPF, 0, 1)); }); return s; }+ function carriageAt(ft) { // the carriage's x offset, exactly as the explode keys move it+ var x = 0; carKeys.forEach(function (c) { x += c[1] * STEP * inOutCubic(clamp((ft - c[0]) / c[2], 0, 1)); }); return x;+ }+ var NS = 'http://www.w3.org/2000/svg', gU = null, gO = null;+ function groups() {+ var w = document.getElementById('ld-world');+ if (!w) return false;+ if (!gU || !gU.isConnected) { gU = document.createElementNS(NS, 'g'); gU.setAttribute('id', 'xd-under'); w.insertBefore(gU, w.firstChild); }+ if (!gO || !gO.isConnected) { gO = document.createElementNS(NS, 'g'); gO.setAttribute('id', 'xd-over'); w.appendChild(gO); }+ return true;+ }+ var f1 = function (v) { return (Math.round(v * 10) / 10).toString(); };+ var LW = TH.lw || 1.5; // the theme's hairline: every weight below is a multiple of it+ var HID = ' stroke-dasharray="7 5"', CEN = ' stroke-dasharray="22 5 3 5"';+ function onFrame(t, k) {+ if (!groups()) return;+ var P = k.P, ft = t * fps, u = '', o = '';+ var path = function (pts) { return 'M' + pts.map(function (p) { var q = P(p[0], p[1], p[2]); return f1(q[0]) + ' ' + f1(q[1]); }).join('L'); };+ var stroke = function (pts, col, w, extra) { return '<path d="' + path(pts) + '" fill="none" stroke="' + col + '" stroke-width="' + f1(w) + '" stroke-linecap="round" stroke-linejoin="round"' + (extra || '') + '/>'; };+ var dot = function (p, r, col, extra) { var q = P(p[0], p[1], p[2]); return '<circle cx="' + f1(q[0]) + '" cy="' + f1(q[1]) + '" r="' + f1(r) + '" fill="' + col + '"' + (extra || '') + '/>'; };+ var steps = stepsAt(ft), cx = carriageAt(ft);+ var all = 0; (barEv['*'] || []).forEach(function (f) { all = Math.max(all, env(ft, f)); });+ // hidden lines: every key lever, dashed, under the keyboard; the struck one lit+ var lit = [];+ KEYS.forEach(function (key) {+ var s = barS(BAR[key[0]], ft);+ if (s > 0.004) lit.push([key, s]); else u += stroke(lever(key), detC, LW * 0.8, HID);+ });+ // the type rest pad and the segment (a heavy arc of pivots), its centre line through the print point+ var arc = [], pad = [];+ for (var a = 0; a <= 48; a++) {+ var th = deg(172 - 164 * a / 48);+ arc.push([PP[0] + SEG.r * Math.cos(th), PP[1] + SEG.r * Math.sin(th), SEG.z]);+ pad.push([PP[0] + (SEG.r + LB * 0.99) * Math.cos(th), PP[1] + (SEG.r + LB * 0.99) * Math.sin(th), SEG.z - LB * 0.16]);+ }+ u += stroke(pad, detC, LW * 3, ' stroke-opacity="0.9"');+ u += stroke([[PP[0], PP[1] + SEG.r + LB + 1.5, SEG.z], [PP[0], PP[1] - 0.6, SEG.z]], detC, LW * 0.8, CEN);+ // typebars at rest in the basket (thin), each with its type slug+ var moving = [];+ for (var i = 0; i < NB; i++) {+ var s = barS(i, ft);+ if (s > 0.004) { moving.push([i, s]); continue; }+ var pv = pivot(i), tp = tip(i, 0), hot = all > 0.004;+ u += stroke([pv, tp], hot ? hotC : inkC, LW * 1.1, hot ? ' stroke-opacity="' + (0.4 + 0.6 * all).toFixed(3) + '"' : ' stroke-opacity="0.85"');+ u += dot(tp, LW * 1.6, hot ? hotC : inkC);+ }+ u += stroke(arc, inkC, LW * 3.2);+ lit.forEach(function (x) { u += stroke(lever(x[0]), hotC, LW * 1.6, HID + ' stroke-opacity="' + (0.4 + 0.6 * x[1]).toFixed(3) + '"'); });+ // a struck typebar: its swing arc (thin, dashed), a short ghost trail, the bar itself, the type at its tip+ moving.forEach(function (m) {+ var i = m[0], s = m[1], pv = pivot(i), sw = [];+ for (var q = 0; q <= 24; q++) sw.push(tip(i, q / 24));+ o += stroke(sw, hotC, LW * 0.8, ' stroke-dasharray="3 5" stroke-opacity="' + (0.7 * Math.min(1, s * 2.5)).toFixed(3) + '"');+ [[2, 0.14], [1, 0.3]].forEach(function (g) { var sg = barS(i, ft - g[0]); if (Math.abs(sg - s) > 0.02) o += stroke([pv, tip(i, sg)], hotC, LW * 1.8, ' stroke-opacity="' + g[1] + '"'); });+ o += '<g filter="url(#ld-glowsoft)">' + stroke([pv, tip(i, s)], hotC, LW * 2.6) + '</g>';+ o += dot(tip(i, s), LW * 2.6, hotC) + dot(pv, LW * 1.6, hotC);+ });+ // ribbon: spool to spool through the vibrator, lifted on a strike; the spools turn with the letter count+ var lift = 0; hitFrames.forEach(function (f) { lift = Math.max(lift, env(ft, f)); });+ var vz = PP[2] - 1.1 + 0.9 * lift;+ SPOOLS.forEach(function (p, si) {+ var rot = steps * 0.32 * (si ? -1 : 1), ring = [];+ for (var q2 = 0; q2 <= 36; q2++) { var a2 = q2 * Math.PI / 18; ring.push([p[0] + 1.25 * Math.cos(a2), p[1] + 1.25 * Math.sin(a2), p[2]]); }+ o += stroke(ring, detC, LW * 0.8);+ for (var sp = 0; sp < 3; sp++) { var th2 = rot + sp * Math.PI * 2 / 3; o += stroke([p, [p[0] + 1.6 * Math.cos(th2), p[1] + 1.6 * Math.sin(th2), p[2]]], inkC, LW * 1.1); }+ o += dot(p, LW * 1.4, inkC);+ });+ o += stroke([[SPOOLS[0][0] + 1.5, SPOOLS[0][1] - 1.1, SPOOLS[0][2]], [PP[0] - 2.4, PP[1] + 0.45, vz], [PP[0] + 2.4, PP[1] + 0.45, vz], [SPOOLS[1][0] - 1.5, SPOOLS[1][1] - 1.1, SPOOLS[1][2]]],+ lift > 0.02 ? hotC : inkC, LW * 1.5, ' stroke-opacity="' + (lift > 0.02 ? (0.55 + 0.45 * lift) : 0.8).toFixed(3) + '"');+ // the platen's centre line (dash-dot, it rides with the carriage) and the print point's mark+ o += stroke([[2.6 + cx, PLATEN.y, PLATEN.z], [41.4 + cx, PLATEN.y, PLATEN.z]], inkC, LW * 0.7, CEN + ' stroke-opacity="0.75"');+ var pq = P(PP[0], PP[1], PP[2]), mk = 9;+ o += '<path d="M' + f1(pq[0] - mk) + ' ' + f1(pq[1]) + 'H' + f1(pq[0] + mk) + 'M' + f1(pq[0]) + ' ' + f1(pq[1] - mk) + 'V' + f1(pq[1] + mk) + '" stroke="' + inkC + '" stroke-width="' + f1(LW) + '"/>';+ hitFrames.forEach(function (f) {+ var d = ft - f; if (d < 0 || d >= 6) return;+ var al = 1 - d / 6;+ o += '<circle cx="' + f1(pq[0]) + '" cy="' + f1(pq[1]) + '" r="' + f1(7 + d * 3.5) + '" fill="none" stroke="' + hotC + '" stroke-width="' + f1(LW * 1.6) + '" opacity="' + al.toFixed(3) + '"/>';+ o += '<circle cx="' + f1(pq[0]) + '" cy="' + f1(pq[1]) + '" r="' + f1(6 * al + 1.5) + '" fill="' + hotC + '" opacity="' + al.toFixed(3) + '"' + (dark ? ' filter="url(#ld-glow)"' : '') + '/>';+ });+ // escapement star wheel: one tooth (1/16 turn) per keystroke, from its frame; its arbor's centre mark+ var esc = [], rot3 = steps * Math.PI * 2 / 16;+ for (var q3 = 0; q3 <= 64; q3++) { var th3 = rot3 + q3 * Math.PI / 32, rr = q3 % 4 === 0 ? 1.62 : (q3 % 4 === 2 ? 1.1 : 1.36); esc.push([ESC[0] + rr * Math.cos(th3), ESC[1] + rr * Math.sin(th3), ESC[2]]); }+ var escHot = 0; stepFrames.forEach(function (f) { escHot = Math.max(escHot, env(ft, f)); });+ o += stroke(esc, escHot > 0.02 ? hotC : inkC, LW * 1.3);+ o += stroke([ESC, [ESC[0] + 0.9 * Math.cos(rot3), ESC[1] + 0.9 * Math.sin(rot3), ESC[2]]], inkC, LW * 1.1) + dot(ESC, LW * 1.4, inkC);+ // the pinion's link to the carriage rack (hidden behind the rail: dashed)+ o += stroke([[ESC[0], ESC[1] + 1.4, ESC[2]], [ESC[0], 4.3, 7.4]], detC, LW * 0.8, HID);+ // mainspring: a spiral that unwinds a little per letter; its band runs to the carriage's left end+ var sp2 = [], turn = -steps * 0.09;+ for (var q4 = 0; q4 <= 110; q4++) { var th4 = turn + q4 * deg(14), r4 = 0.2 + q4 * 0.0165; sp2.push([SPRING[0] + r4 * Math.cos(th4), SPRING[1] + r4 * Math.sin(th4), SPRING[2]]); }+ o += stroke(sp2, inkC, LW * 1.0);+ o += stroke([[SPRING[0] + 0.2, SPRING[1] - 2.0, SPRING[2] - 0.5], [SPRING[0] + 0.2, 5.4, 6.4], [4.4 + cx, PLATEN.y - 0.9, PLATEN.z - 0.7]], detC, LW * 1.5);+ gU.innerHTML = u; gO.innerHTML = o;+ }++ // ---- the part names: a callout system with fixed places (no leader crosses another or a part), arriving one by+ // one and leaving before the end card. Each callout's shoulder sits at a fraction of the machine's box on screen.+ var bb = (function () { var q = corners.map(function (c) { return [O[0] + c[0] * S, O[1] + c[1] * S]; });+ return [Math.min.apply(null, q.map(function (c) { return c[0]; })), Math.min.apply(null, q.map(function (c) { return c[1]; })),+ Math.max.apply(null, q.map(function (c) { return c[0]; })), Math.max.apply(null, q.map(function (c) { return c[1]; }))]; })();+ var PLACES = portrait ? {+ Typebars: [0.343, 0.15, 'left'], Carriage: [0.645, -0.005, 'left'], Escapement: [1.05, 0.08, 'left'], Keys: [0.131, 0.919, 'left'] } : {+ Mainspring: [0.27, 0.24, 'left'], Typebars: [0.29, 0.07, 'left'], Keys: [0.30, 0.93, 'left'],+ Carriage: [0.60, -0.01, 'right'], Escapement: [1.031, 0.165, 'left'] };+ var L = [];+ var lab = function (o2, title, sub, i) {+ var l = Object.assign(o2, { at: t0 + i * 0.35, until: tEnd - 0.25, title: title, sub: portrait ? '' : sub });+ var pl = (ctx.places || PLACES)[title];+ if (pl) { var a2 = scr(l.anchor); l.elbow = [Math.round(bb[0] + pl[0] * (bb[2] - bb[0]) - a2[0]), Math.round(bb[1] + pl[1] * (bb[3] - bb[1]) - a2[1])]; l.side = pl[2]; }+ L.push(l);+ };+ lab({ for: 'k-space', anchor: [SPACE.at[0], SPACE.at[1] + SPACE.size[1], SPACE.at[2] + SPACE.size[2]] }, 'Keys', 'pressed by the typist', 0);+ lab({ for: 'basket', anchor: tip(2, 0) }, 'Typebars', 'one per key', 1);+ lab({ anchor: [20, 5.3, 13.2] }, 'Carriage', 'paper and platen step left', 2); // on the paper: it stays on it as it moves+ lab({ for: 'escapement', anchor: [ESC[0] + 1.62, ESC[1], ESC[2]] }, 'Escapement', 'one tooth per key', 3);+ if (!portrait) lab({ for: 'mainspring', anchor: [SPRING[0] - 1.45, SPRING[1] + 1.45, SPRING[2] - 1.2] }, 'Mainspring', 'pulls the carriage', 4);++ // the camera: one calm move, easing a little toward the acting parts (the basket and the print point)+ var cz = [(R[0] + R[2]) / 2, (R[1] + R[3]) / 2], act = scr([PP[0], PP[1] + 4, 6]);+ var foc = [lerp(cz[0], act[0], 0.5), lerp(cz[1], act[1], 0.5)];+ var camTo = [foc[0] + Math.round(lerp(cz[0], foc[0], 0.4) - foc[0]), foc[1] + Math.round(lerp(cz[1], foc[1], 0.4) - foc[1])];+ return {+ world: { scale: S, origin: O },+ clip: ctx.clip, safe: ctx.safe, avoid: ctx.avoid,+ leaderCost: 3,+ items: items, states: states, explode: explode, tokens: tokens, labels: L,+ // (on a phone the figure has no room to grow: it holds still). The runtime rounds a camera at rest to whole+ // pixels: the move starts from a whole-pixel pose (to - focus is whole) and ends under the end card, so no+ // frame snaps by half a pixel while the figure is in view.+ camera: portrait ? [] : [{ at: t0 + 0.4, dur: Math.max(1.5, tEnd - t0 + 0.2), zoom: 1.04, focus: foc, to: camTo }],+ onFrame: onFrame,+ xray: { bars: NB, letters: letters.length, scr: scr },+ };+ };+})();diff --git a/skills/showtime/library/xray/typewriter/mechanism.js b/skills/showtime/library/xray/typewriter/mechanism.jsnew file mode 100644index 0000000..135e703--- /dev/null+++ b/skills/showtime/library/xray/typewriter/mechanism.js@@ -0,0 +1,86 @@+/* A manual typewriter, inside (showtime X-ray mechanism library: typewriter).+ Design space: about 1430 x 930 units, the machine seen from the typist's side, keys at the bottom, the platen+ at the top. Each event is a keystroke: its key goes down, its typebar swings up to the print point and the+ ribbon lifts, PEAKING on the event's frame; the escapement lets the carriage step one letter on that frame.+ An event of kind "return" (showtime.json "xray": {"kinds": {"18": "return"}}) winds the mainspring back.+ Which key: only from evidence ("xray": {"text": "..."} = what the paper in the clip shows, one character per+ keystroke); without it the keystroke lights the typebar fan, never a guessed key. */+(function () {+ var deg = function (a) { return a * Math.PI / 180; };+ var ROWS = [['qwertyuiop', 493, 773, 81], ['asdfghjkl', 500, 867, 83], ['zxcvbnm,.-', 533, 953, 85.5]];+ var KEYS = [];+ ROWS.forEach(function (r) { r[0].split('').forEach(function (ch, i) { KEYS.push([ch, r[1] + i * r[3], r[2]]); }); });+ var BYX = KEYS.slice().sort(function (a, b) { return a[1] - b[1]; }), NB = BYX.length, BAR = {};+ BYX.forEach(function (k, i) { BAR[k[0]] = i; });+ var PP = [920, 286], CB = [920, 330], RX = 330, RY = 104; // print point, typebar segment centre and radii+ var SPACE = [[693, 1012], [1293, 1012], [1293, 1056], [693, 1056]];+ var BODY = [[440, 244], [1460, 267], [1547, 600], [1547, 1080], [260, 1080], [300, 613]];+ function pivot(i) { var th = deg(166 - 152 * i / (NB - 1)); return [CB[0] + RX * Math.cos(th), CB[1] + RY * Math.sin(th)]; }+ function rest(i) { var p = pivot(i); return [p[0] + 0.62 * (CB[0] - p[0]), p[1] + 0.62 * (CB[1] - p[1])]; }++ XRAY.mechanism({+ name: 'typewriter',+ noun: 'keystrokes', noun1: 'keystroke', one: 'a keystroke', textLabel: 'on the paper',+ box: [250, 140, 1700, 1080],+ // four points to place on the clip's reference frame (showtime.json "xray": {"place": {...}})+ anchors: { key_q: [493, 773], key_p: [1222, 773], key_m: [1046, 953], print_point: PP },+ outline: BODY,+ headline: function (n) { return '<em>' + n + '</em> keystrokes,<br>seen from inside<br>the machine.'; },+ measured: function (n, text) {+ return 'Each keystroke lit up on the frame its sound was heard' + (text ? '; which key, read from the paper in the clip.' : '.');+ },+ drawn: 'Inside view: a generic manual typewriter, drawn, not this exact machine.',+ // the live page: pads that play the next event of each kind from the real clip+ touch: { verb: 'Type on it', by: 'ch' },+ draw: function (k) {+ var C = k.C, g = k.g, s = k.s, hot = k.hot && k.kind === 'event', ch = hot ? k.ch : null;+ var n = k.n, bar = ch && ch !== ' ' && BAR[ch.toLowerCase()] != null ? BAR[ch.toLowerCase()] : -1;+ var rets = k.ev.filter(function (e) { return e.kind === 'return'; });+ var ret = 0; // 0..1 while a carriage return winds the spring back+ rets.forEach(function (e) { var d = k.since(e.i); if (d >= -2 && d < 25) ret = Math.max(ret, 1 - Math.abs(d - 8) / 17); });+ var panel = k.view === 'panel';+ if (panel) { // the machine's outline and its keyboard, for the panel only+ k.poly(BODY, C.faint, 2);+ KEYS.forEach(function (key) { k.ell(key[1], key[2], 26, 22, (hot && key[0] === (ch || '').toLowerCase()) ? C.hot : C.faint, 2, 0, false); });+ k.poly(SPACE, ch === ' ' && hot ? C.hot : C.faint, 2);+ }+ // key levers under the cover: every key to its typebar+ BYX.forEach(function (key, i) { if (i === bar) return; k.seg([key[1], key[2] - 30], pivot(i), panel ? C.l(.16) : C.l(.12), 1.5); });+ // the typebar fan, at rest+ for (var i = 0; i < NB; i++) { if (i === bar) continue; k.seg(pivot(i), rest(i), hot && bar < 0 && ch !== ' ' ? C.hot : C.soft, 3.5); }+ var arc = []; for (var a = 0; a < 40; a++) { var th = deg(166 - 152 * a / 39); arc.push([CB[0] + RX * Math.cos(th), CB[1] + RY * Math.sin(th)]); }+ g.strokeStyle = C.line; g.lineWidth = k.lw(3); k.path(arc); g.stroke();+ // ribbon spools (they turn a little each keystroke) and the ribbon, lifted on a strike+ k.ell(640, 352, 46, 20, C.line, 3, deg(n * 9), true); k.ell(1200, 356, 46, 20, C.line, 3, deg(n * 9), true);+ g.strokeStyle = hot ? C.hot : C.soft; g.lineWidth = k.lw(3); k.path([[640, 340], [920, 300 - (hot ? s * 12 : 0)], [1200, 344]]); g.stroke();+ // escapement star wheel (one tooth per keystroke) and the carriage rack+ k.ell(1336, 430, 40, 18, hot ? C.hot : C.line, 3.5, deg(n * 360 / 16), true);+ k.seg([700, 240], [1690, 252], C.soft, 3);+ for (var r = 0; r < 40; r++) { var x = 700 + r * 24.75, y = 240 + r * 0.3; k.seg([x, y], [x + 8, y + 9], C.faint, 2); }+ k.seg([1336, 430], [1336, 252], hot ? C.hot : C.soft, 3);+ // mainspring drum: unwinds a little per letter, winds back on a return+ var turns = n * 0.06 * (1 - ret), sp = [];+ for (var q = 0; q <= 120; q++) { var aa = deg(q * 12) + turns, rr = 6 + q * 0.32; sp.push([560 + Math.cos(aa) * rr, 430 + Math.sin(aa) * rr * 0.45]); }+ g.strokeStyle = ret > 0.05 ? C.hot : C.line; g.lineWidth = k.lw(2.5); k.path(sp); g.stroke();+ k.seg([598, 430], [700, 244], ret > 0.05 ? C.hot : C.soft, 3);+ k.seg([560, 226], [470, 150], ret > 0.05 ? C.hot : C.soft, 6);+ // the struck key, its lever and its typebar on the way up; the type hits the print point on the event frame+ if (hot) {+ g.shadowColor = C.hot; g.shadowBlur = 22 * s;+ if (ch === ' ') k.poly(SPACE, C.hot, 5, k.since(k.cur) >= 0 && k.since(k.cur) < 3 ? C.hot : null);+ else if (bar >= 0) {+ var key = BYX[bar], pv = pivot(bar), rs = rest(bar), tip = [rs[0] + (PP[0] - rs[0]) * s, rs[1] + (PP[1] - rs[1]) * s];+ k.ell(key[1], key[2], 30, 26, C.hot, 5, 0, false); k.seg([key[1], key[2] - 30], pv, C.hot, 4); k.seg(pv, tip, C.hot, 7);+ }+ if (k.since(k.cur) >= 0 && k.since(k.cur) < 3 && ch !== ' ') k.dot(PP, Math.max(6, 16 * Math.min(1.5, k.scale)), C.hot);+ g.shadowBlur = 0;+ }+ // labels (drawn where there is room for them: the panel, or in place when there is no panel)+ k.label('typebars', [1180, 330], 120, -150, hot && ch !== ' ');+ k.label('key levers', [700, 640], -180, 40, hot);+ k.label('escapement', [1376, 430], 200, 40, hot);+ k.label('mainspring', [520, 430], -160, -60, ret > 0.05);+ k.label('print point', PP, -260, -140, hot && s > 0.8);+ }+ });+})();diff --git a/skills/showtime/references/html-export.md b/skills/showtime/references/html-export.mdindex c968888..6d8553b 100644--- a/skills/showtime/references/html-export.md+++ b/skills/showtime/references/html-export.md@@ -50,19 +50,22 @@ showtime export html <diagram> --interactive # a living diagram's live figchange about every 2 s, holds as long as reading needs, callouts that name their year), then export it- Use the MP4 for footage-led videos and slow-to-draw pages: playing is drawing. Sound always needs a click;a `--folder` export opened from `file://` cannot `fetch()` footage, serve it over http (§ Limitations)+- An X-ray exports as a live page people scrub and step through: `--interactive` writes `<slug>-live.html`+ (workflows/xray.md, Live page); `--prompt "<request>"` shows the request that made it on the page, and the page+ embeds with `<showtime-xray>` or an iframe with `#embed`<!-- section lines: kept current by scripts/check_release.py -->| Section | Lines ||---|---|-| What you get | 67-133 |-| MP4 or HTML? | 135-145 |-| Audio modes (--audio): How the live score streams (and why seeking is exact) | 147-178 |-| Size budget | 180-203 |-| Sharing and hosting | 205-254 |-| Questions | 256-299 |-| Options | 301-332 |-| Live figure | 334-346 |-| Limitations | 348-382 |+| What you get | 70-136 |+| MP4 or HTML? | 138-148 |+| Audio modes (--audio): How the live score streams (and why seeking is exact) | 150-181 |+| Size budget | 183-206 |+| Sharing and hosting | 208-257 |+| Questions | 259-302 |+| Options | 304-335 |+| Live figure | 337-349 |+| Limitations | 351-385 |## What you getdiff --git a/skills/showtime/references/index.md b/skills/showtime/references/index.mdindex f47e0e5..4b6a39f 100644--- a/skills/showtime/references/index.md+++ b/skills/showtime/references/index.md@@ -26,6 +26,7 @@ for that step) with a table of its sections; read only what the current step nee| `references/workflows/trailer.md` | a teaser or trailer || `references/workflows/slideshow.md` | photos or screenshots set to music || `references/workflows/voiceover-only.md` | narration added to an existing video |+| `references/workflows/xray.md` | an X-ray of the user's clip of a machine or instrument: the inside drawn in sync with its sound, measured and verified; a live page to play (`showtime xray`) || `references/workflows/localize.md` | a video re-voiced or subtitled in another language || `references/adopt.md` | their own video code (a page with seek/render/draw(t), CSS animations, a Python frame script) checked, scored and rendered as it is || `references/adopt.md` § From Claude Design | a Claude Design animation (its HTML zip export) made into an MP4, then voice, music, captions and exports |diff --git a/skills/showtime/references/workflows/diagram.md b/skills/showtime/references/workflows/diagram.mdindex 7273509..9767fae 100644--- a/skills/showtime/references/workflows/diagram.md+++ b/skills/showtime/references/workflows/diagram.md@@ -29,20 +29,24 @@ or dark (night schematic) theme. For numbers that change use `data-story.md`; fo- A figure people poke: `showtime export html <project> --interactive [--target artifact]` writes`<slug>-figure.html`, one offline file: the board is live from the first frame (hover a part, pick a step, turn aknob). The scene says what is interactive in `interactive` (steps, knobs, readouts, a tour); `--example- econ-lab` and `--example enter` show both kinds (§ Interactive)+ econ-lab` and `--example enter` show both kinds; `--example typewriter` (type on it) and `--example computer`+ (what happens when you press a key) start from finished figures people type on and press (§ Interactive)+- "Interactive", "live" or "let me touch it" in the request: the live figure is the deliverable, and it needs no+ local server. If `check` or `render` cannot start (no local port, a sandbox), still run `export html --interactive`,+ hand over the `*-figure.html`, and say what could not run (§ Interactive)<!-- section lines: kept current by scripts/check_release.py -->| Section | Lines ||---|---|-| Inputs | 47-51 |-| Defaults | 53-59 |-| Steps | 61-88 |-| Scene spec | 90-132 |-| Labels and layout | 134-151 |-| Themes | 153-158 |-| Interactive | 160-204 |-| Pitfalls | 206-223 |-| Read next | 225-228 |+| Inputs | 51-55 |+| Defaults | 57-63 |+| Steps | 65-92 |+| Scene spec | 94-139 |+| Labels and layout | 141-158 |+| Themes | 160-165 |+| Interactive | 167-211 |+| Pitfalls | 213-230 |+| Read next | 232-235 |## Inputs@@ -127,7 +131,10 @@ z up; times are seconds; `world: {scale, origin}` maps them to the 1920x1080 pagtext). Typed: `type: [t0, t1]` reveals it left to right with a caret, `typeFrom: n` characters already there,`caretUntil`; `press: t` (with `pressTone`) presses it like a key.- **focus**: `[{part, with: [...], at, until}]` veils the rest; **camera**: keys `{at, dur, zoom, focus: part |- [x, y, z] | [sx, sy], to}` run in order; **explode**: `{at, dur, back, offsets: {id: [dx, dy, dz]}}`.+ [x, y, z] | [sx, sy], to}` run in order; **explode**: `{at, dur, back, offsets: {id: [dx, dy, dz]}}`;+ **offset**: `fn(id, t) -> [dx, dy, dz] | null`, a part's own offset as a pure function of t, added to explode's+ (the parts, their labels, tags and hit areas move with it: the typewriter example's carriage, from its typed log);+ **onFrame**: `fn(t, {cam, P, seg, E, focus})` after each frame, for pieces the library has no shape for.- **steps** (`.ld-steps` beat lines), **reveal** (`{sel, at}` page elements), **page** (kicker, title with`*accent*`, dek, footers).diff --git a/skills/showtime/references/workflows/xray.md b/skills/showtime/references/workflows/xray.mdnew file mode 100644index 0000000..caea0d7--- /dev/null+++ b/skills/showtime/references/workflows/xray.md@@ -0,0 +1,210 @@+# Workflow: an X-ray of a real clip (xray)++Read this when the user has a clip of something real doing its thing (a typewriter typing, a clock ticking, a+music box playing, a bike shifting, a lock turning, a sewing machine, a stapler) and wants to see what happens+inside it: "x-ray this clip", "show the mechanism while I type", "what's going on inside when it clicks". One+clip in; out comes a film where the inside moves on the exact frames the clip's own sound says it did, with a+receipt that proves it.++## Essentials++- `showtime xray analyze <clip> --job <job>` measures the clip: every event in its sound (time, frame,+ strength, band) into `<job>/xray/xray.json`, and `<job>/xray/check.jpg` to look at (§ Steps)+- `showtime new xray <job>/project --mechanism <object> [--layout through|split|diagram]`: the inside from the+ mechanism library (`library/xray/`: typewriter, musicbox, switch, piano, guitar, drums), drawn on the tracked object (through), beside the+ clip (split) or as an isometric living diagram (diagram); counters, sound strip, sync marker, end card; copy+ the clip and `xray.json` in (§ Steps, § Mechanism library)+- Before drawing: snap 3 frames, name the object and which parts move on each event; every event moves+ something on its own frame, a strike peaks ON its frame (§ Drawing the inside)+- After the render, `showtime xray verify <job>` checks the film itself: events on their frame (from the+ sync marker) and the audio against the original clip, in ms; qa runs it too; the end card quotes it (§ Verify)+- `showtime export html <project> --interactive` writes the live page, `<slug>-live.html`: the clip with its sound,+ every event a tick to jump to, the inside drawn by the same page at that frame; one offline file (§ Live page)+- On screen, say what is measured (event times and counts) and what is drawn (a generic mechanism of+ that kind of object) (§ Honesty)+- See-through in place (`--track`, the inside drawn on the object as the camera moves): objects only,+ never people; when the overlay drifts, use the split layout (§ See-through)+- A clip the user did not film: license and credit line before you start (§ Credits)++<!-- section lines: kept current by scripts/check_release.py -->+| Section | Lines |+|---|---|+| Inputs | 44-55 |+| Steps | 57-84 |+| Drawing the inside | 86-103 |+| Verify | 105-114 |+| Live page | 116-150 |+| Honesty | 152-161 |+| See-through | 163-177 |+| Mechanism library | 179-188 |+| Credits | 190-194 |+| Pitfalls | 196-204 |+| Read next | 206-210 |++## Inputs++- The clip, with its sound (events are found in the sound). A phone video is fine; shaky is fine. Close and+ well lit beats wide and dark: the inside is drawn to match what the camera shows.+- Music (a guitar, a piano, a music box): `showtime xray notes <clip> --job <job> [--instrument ...]` adds the+ pitches (notes.json, MIDI, MusicXML; key, chords, tempo map, guitar strings and frets), so the page can say which+ note, string or tooth; `--instrument drums` sorts each hit into kick, snare, hi-hat, toms, cymbals or ride+ (notes.json `drums`, a sound-type classifier, not a transcription). Dense music: `analyze --engine flux` (auto picks it when the sound never goes quiet).+- What the object is ("an Underwood portable typewriter", "a Polyphon disc music box"). If the user does not+ say, look at 3 frames and name it, then say what you assumed.+- Helpful: the part of the clip that matters (`--from/--to`), and anything the clip itself shows that fixes+ the events (the text typed on the paper, the gear being shifted to): it turns "a key" into "the key S".++## Steps++1. `showtime job init xray-<object> --request "<verbatim>"`.+2. `showtime xray analyze <clip> --job <job>`. Open `xray/check.jpg`: the waveform with every event tick,+ dropped events in grey. Play the clip at 3 or 4 of the ticks and confirm a real event sits there (a key, a+ click, a tick); count them against what you can see. Too many (releases counted as strikes): raise+ `--min-gap`; too few (soft events missed): raise `--sensitivity`. Read `dropped` in `xray.json`: it says why+ each one went.+3. Snap 3 frames of the clip (`showtime snap <clip> --at <t>`): the moment before the first event, one in the+ middle, the last. Write down: the object, the parts that move on each event, and anything that happens once+ (a carriage return, a bell, a gear change). Each kind of event gets its own motion.+4. `showtime new xray <job>/project --mechanism <object> --duration <clip length + about 3 s>`, then set+ `cue.js` `endcard` to the clip's length. Put the clip, with its sound, in `media/clip.webm`+ (`showtime footage trim <clip> --webm -o <job>/project/media/clip.webm`; the audio mix plays its sound, the+ page is muted) and `<job>/xray/xray.json` (and `notes.json`) next to `index.html`. Analyze the file you put in+ `media/`, so its frames are the film's; a 29.97 fps clip in a 30 fps film is converted by time (floor(t * 30)).+ In `showtime.json` `"xray"`: `skip` (event indexes that are not the object's: a hand bump, a cough), `kinds`+ (`{"18": "return"}`), `text` (what the clip itself shows the events produced, one character each), `place`+ (the mechanism's four anchors on the reference frame, for see-through; no three in a line), `window` (the+ see-through outline, reference-frame pixels; default the tracked region), `panel: true` (16:9 see-through+ with the schematic beside the clip), `theme` (`dark` radiograph or `light` blueprint: `new xray --theme`),+ `credit` (the clip's credit line; `own: true` for the user's own clip), `receipt`.+5. A library mechanism: place it and check (`showtime check`, `showtime look`, `showtime snap --at` an event's+ time: the struck part must sit on the real one). An object the library lacks: copy the closest+ `mechanism.js` and redraw `draw(k)` (§ Drawing the inside).+6. `showtime render <project> --job <job>`, then `showtime xray verify <job>` and put its line in `showtime.json`+ `"xray".receipt`; re-render only the end (`--from <end card start> --to <length>`).+7. `showtime qa <job>` (it runs verify again), the critic round, deliver with the credit line.++## Drawing the inside++- Draw the real kind of mechanism, simplified, not a symbol. A typewriter has a key lever, a typebar that swings+ up to the platen, a ribbon that lifts, an escapement that lets the carriage step one letter, a mainspring; a+ disc music box has a perforated disc, star wheels and a comb with one tooth per pitch; a derailleur has a+ cable, a parallelogram and a cage. Six to ten parts, each one labelled.+- Use the template's helpers: `seg`, `circ`, `ell`, `path` (lines and shapes in the panel's coordinates),+ `label` (a leader line and a tag, kept on screen), `env(i)` (0..1, the motion of event i, 1 on its frame),+ `typed(frame)` (events so far). Glow, cyan lines on the dark ground, red for what is acting right now.+- Motion rules: a strike PEAKS on its event's frame and starts 2-3 frames before (the sound is the impact);+ it settles over 5-8 frames. A part that advances (a wheel, a carriage, a counter) steps on the event's frame.+ Something that happens once (a return, a bell) gets its own, longer motion across its frames.+- Compare frames, never seconds: `xray.json` gives each event a `frame`; the template adds the film's offset.+- Counters and readouts come from the same events (keystrokes so far, rate, returns). Never a number the clip+ did not give.+- Phone sizes: labels 28 px and up, counters 80 px and up, on 1080x1920.+- Scrolling bands (a score, a tab, a timeline) follow a smoothed time-to-position curve, never a jump to the+ latest event: verify reports stalls and jumps.++## Verify++- The template draws the sync marker: a 6x6 square in the bottom-right corner, black, white only on event+ frames. Do not cover it or move it.+- `showtime xray verify <job>` reads that corner on every frame of the final film and compares the white+ frames with the events, then cross-correlates the film's audio with the clip's. It prints a line like+ "18 of 18 events on their frame · audio 0.0 ms from the original" and writes it into the job receipt.+- Missing or extra events, or audio more than 20 ms off: qa FAILs. Fix the page (an event drawn on the wrong+ frame, a trimmed clip without its offset), not the receipt.+- No marker (a page you wrote yourself): qa WARNs "unverified". Say so on delivery.++## Live page++- `showtime export html <project> --interactive` (`--target artifact` for an HTML artifact, `-o` to name it) writes+ `<slug>-live.html`: one file, no network request (a Content-Security-Policy blocks it), the clip packed once.+- The viewer plays the clip with its own sound, scrubs a timeline where every event is a tick (red kept, grey+ skipped), clicks a tick or presses ← / → to land on the next event's exact frame (shift + arrow: one+ frame; `,` `.` too, unless the pads claim them, as a typewriter's do), and Shift+S (0.25×) plays the event slowly and stops on its frame. Light or dark (Shift+T); see-through or+ split (Shift+L) when the clip is tracked. Space plays and pauses. Hovering or tapping a part label (or picking it+ under the timeline) lights it up.+- Touch: pads under the picture, one per kind of event (typewriter: a letter; instruments: a note; drums: kick,+ snare, hat; a switch: one). A pad plays the next event of its kind from the current frame and rests on it, so the+ viewer plays the clip's own sound back. Typing does the same: the typewriter's letter is its key, the other+ mechanisms take `a s d f g h j k l ;` in pad order. Dragging the picture sideways scrubs the clip; a click on it+ plays the next hit when paused, and pauses when playing.+- The picture is the film's own page at that frame: xray.js and mechanism.js (or the diagram scene) draw it, driven by+ the scrub time instead of the renderer. Nothing is drawn twice, so a page that verifies also reads right live.+- Under the picture: the receipt line (`"xray".receipt`, from `showtime xray verify`) and the credit line. No receipt+ yet: the page says so. The timeline stops at the clip's end; the sync marker is not shown (it is for verify).+- Links open a moment, as plain anchors (a host that drops `key=value` keeps them): `<file>#hit4` (the 4th event, as+ the page counts it), `#t2.5` (seconds), and the tokens `_light` / `_dark`, `_through` / `_split`, joined with `_`:+ `#hit4_light`, `#t2.5_dark_split`. The page's Link button copies one.+- Make your own with one line: under the picture, a one-line request (`/showtime x-ray ...`) with Copy, and the+ commands with Copy. The commands are written with placeholders (`clip.mp4`, `xray-<mechanism>`), never the project's own paths.+ The prompt is only what you give: `--prompt "<the request>"` on the export, else `"xray": {"made": {"prompt": ...}}`+ in showtime.json, else none (the job's request is never read). `showtime new xray` records `"xray": {"made":+ {"mechanism", "layout", "theme"}}` and nothing else. The export report (and `--json` `made`) states what was embedded.+- Embed: `<script src=".../xray/embed.js"></script>` and+ `<showtime-xray src="...-live.html" ratio="16/9" title="..." poster="..."></showtime-xray>` show a poster+ with a Play-live button and load the page on click (`autoload`: at once; `hit="4"`: opens on that event). Or a plain frame: `<iframe src="...-live.html#embed">`: `#embed` makes the+ page fit its width and report its height to the host. The page can be framed by any site (its policy has no+ frame-ancestors).+- Size: the clip is the file (the typewriter's 6.4 MB clip makes an 8.9 MB page). Over `--max-mb` (16, the artifact+ limit) the clip is re-encoded for this export only; trim it to the part that matters first.+- Big pictures on a phone: a 16:9 film at 390 px is a picture to look at; the parts list under the timeline is the+ way to the labels there.++## Honesty++- Nothing leaves the machine. The job folder holds `xray/source.path` (the absolute path of the clip, so `verify`+ finds it) and `xray.json` (the clip's file name and sha256): check them before sharing a job folder.+- Say on screen, in plain words: what was measured (the event times from the clip's sound, counts, anything+ read from the picture) and what was drawn (the inside: a generic mechanism of that kind of object, not this+ exact unit).+- Never invent which key, which note, which gear, unless the clip shows it (the paper, a display) or the user+ told you. "A keystroke" is fine; "the key S" needs evidence.+- Never put a command that does not exist on the end card.++## See-through++- `showtime xray analyze <clip> --job <job> --track --region "x,y x,y ..." --ref-time <t>` follows the object+ through the clip (points on the object, a perspective transform per frame) and writes it into `xray.json`.+ Mask parts that move by themselves (a carriage, hands over keys) with `--mask`.+- Look at the 6 frames in `check.jpg`: the outline must sit on the object in all of them. If it slides, use+ the split layout.+- Objects only, never people. A clip where a person fills the frame gets the split layout; never draw an+ "inside" over a body.+- Track the object, not its moving parts: on a turning disc, track the cabinet with the disc masked (tracking the+ disc itself measures its spin instead: the homographies turn with it). A camera that settles in the first second+ tracks badly there: trim it off.+- A mechanism that is visible in the clip (a bare switch, an open movement): mark the real parts and their+ forces instead of redrawing them over the footage; draw the schematic in the panel.+- Draw inside the object's outline (clip-path from the tracked polygon), the rest of the frame stays as filmed.++## Mechanism library++- `library/xray/<object>/`: `mechanism.js` (the drawing: design box, four anchors, `draw(k)`), `diagram.js`+ (the living-diagram scene, for `--layout diagram`), `README.md` (parts, what an event moves, what is measured and+ what is drawn, the clip it was verified on). `showtime new xray --mechanism <object>` copies one in.+- The kit `draw(k)` gets: `k.seg/ell/poly/path/dot/label` in design units, `k.env(i)` (0..1, 1 on event i's+ frame), `k.cur`/`k.s`/`k.hot` (the event acting now), `k.ch` (its character from `text`), `k.ev[i].notes`+ (its pitches from notes.json), `k.n` (events so far), `k.since(i)` (frames), `k.view` (panel or inplace).+- A new object: start from the closest entry, verify it on a real clip, then add it with its README and the+ receipt line: the library grows with every verified X-ray.++## Credits++- The user's own clip: nothing to add. Any other clip: its license and credit line before you start, in the+ job's credits so qa sees them, and on the end card. CC BY needs the credit; CC BY-SA makes the film BY-SA too;+ no license found means do not use it.++## Pitfalls++- Counting releases as strikes: a key makes two sounds (down and back); `--min-gap` merges them. Check against+ what you can see.+- A strike that starts on the event's frame looks late: the sound is the impact, so the motion peaks there.+- Seconds instead of frames: an event at 2.183 s is frame 65 at 30 fps; comparing `t >= 2.183` with the+ frame's start time (2.1667) fires one frame late.+- A filter is not an X-ray: tinting the clip blue shows nothing inside. Draw the mechanism.+- A trimmed clip without its offset puts every event early or late: set `"xray".offset_frames` in showtime.json (the page starts the clip at that film frame) and the clip's `start` in audio/mix.json to offset_frames / fps seconds.++## Read next++- `references/workflows/diagram.md` for drawn figures and their craft+- `references/editing.md` for trimming and preparing footage+- `references/qa.md` for the checks qa runsdiff --git a/skills/showtime/runtime/diagram.js b/skills/showtime/runtime/diagram.jsindex bed62f3..3e8f332 100644--- a/skills/showtime/runtime/diagram.js+++ b/skills/showtime/runtime/diagram.js@@ -633,6 +633,9 @@var p = seg(t, x.at, x.dur, E.inOutCubic) * (x.back != null ? 1 - seg(t, x.back, x.dur, E.inOutCubic) : 1);for (var k = 0; k < 3; k++) o[k] += (v[k] || 0) * p;});+ // spec.offset(id, t): the scene's own offset, a pure function of t (a live log of keystrokes, say)+ var so = spec.offset && id ? spec.offset(id, t) : null;+ if (so) for (var q = 0; q < 3; q++) o[q] += so[q] || 0;return o;}@@ -1487,6 +1490,14 @@if (text == null) { log.push({ at: at, i: null, tone: null }); return; }text = String(text);var i = -1; for (var j = 0; j < g.tx.length; j++) if (g.tx[j].textContent === text) { i = j; break; }+ var own = g.own != null ? g.own : (g.own = g.tx.length);+ if (i < 0 && !LIVE.seek && g.tx.length - own >= 48) { // a live page typed on for long: reuse a value no entry still shows+ var used = {}; log.forEach(function (x) { if (x.i != null) used[x.i] = 1; });+ for (var r = own; r < g.tx.length; r++) if (!used[r]) {+ g.tx[r].textContent = text; g.keys[r][1] = text; g.ws[r] = lay.done ? g.tx[r].getComputedTextLength() : text.length * g.fs * 0.6;+ i = r; break;+ }+ }if (i < 0) { // a value the scene did not list: one more persistent text, measured oncevar tn0 = toneOf(L, tone || g.tg.tone || g.tg.temp);var e = mk('text', { 'font-family': L.font.mono, 'font-size': g.fs, 'font-weight': 600, fill: tn0.tagInk, 'text-anchor': 'middle', opacity: 0, visibility: 'hidden' }, g.g);@@ -1494,7 +1505,8 @@i = g.tx.length - 1;}log.push({ at: at, i: i, tone: tone || null });- if (log.length > 400) log.splice(0, log.length - 400);+ var cap = LIVE.seek ? 400 : 32; // a film seeks back through its whole log; a live page only goes forward+ if (log.length > cap) log.splice(0, log.length - cap);};liveApi = {set: function (target, value, o) {diff --git a/skills/showtime/runtime/figure.css b/skills/showtime/runtime/figure.cssindex 5e49fa0..8a7bc23 100644--- a/skills/showtime/runtime/figure.css+++ b/skills/showtime/runtime/figure.css@@ -76,7 +76,7 @@ html, body { margin: 0; background: #FFFFFF; }.fg .seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; background: var(--fg-soft); border: 1px solid var(--fg-line); border-radius: 10px; padding: 3px; }.fg .seg button { border: 0; background: transparent; border-radius: 8px; padding: 8px 4px; cursor: pointer; min-height: 52px; }.fg .seg button b { display: block; font-family: 'JetBrains Mono', monospace; font-size: 19px; font-weight: 600; }-.fg .seg button span { display: block; font-size: 11.5px; color: var(--fg-ink3); margin-top: 1px; }+.fg .seg button span { display: block; font-size: 12px; color: var(--fg-ink3); margin-top: 1px; }.fg .seg button[aria-checked="true"] { background: var(--fg-bg); box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12), 0 0 0 1px var(--fg-line); }.fg .seg button[aria-checked="true"] b { color: var(--fg-blue-ink); }.fg input[type=range] { width: 100%; accent-color: var(--fg-blue); height: 32px; }@@ -86,7 +86,7 @@ html, body { margin: 0; background: #FFFFFF; }.fg .ro { display: block; background: var(--fg-bg); border: 1px solid var(--fg-line); border-radius: 12px; padding: 10px 12px; }.fg .ro .k { display: block; font-size: 12.5px; color: var(--fg-ink2); }.fg .ro .v { display: block; font-family: 'JetBrains Mono', monospace; font-weight: 600; font-size: 24px; font-variant-numeric: tabular-nums; margin-top: 3px; color: var(--fg-ink); }-.fg .ro .u { display: block; font-size: 11.5px; color: var(--fg-ink3); margin-top: 2px; }+.fg .ro .u { display: block; font-size: 12px; color: var(--fg-ink3); margin-top: 2px; }.fg .ro.txt .v { font-family: 'Inter', sans-serif; font-size: 16px; line-height: 1.3; }.fg .ro.fg-tone-hot .v, .fg .ro.fg-tone-warm .v { color: var(--fg-warm); }.fg .ro.fg-tone-cold .v { color: var(--fg-blue-ink); }@@ -118,3 +118,14 @@ html, body { margin: 0; background: #FFFFFF; }.fg-phone .fg .fg-stepb { flex: none; border: 1px solid var(--fg-line); min-height: 44px; }.fg-phone .fg .fg-stepb .k { white-space: nowrap; }.fg-phone .fg .fg-keys { display: none; }++/* touch: the Type field (a phone's own keyboard; on a desktop, typing anywhere works), parts that act, a drag */+.fg .fg-type { display: flex; align-items: center; gap: 10px; margin: 12px 2px 0; padding: 8px 12px; border: 1px solid var(--fg-line); border-radius: 10px; background: var(--fg-soft); cursor: text; }+.fg .fg-type-k { font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--fg-ink2); white-space: nowrap; }+.fg .fg-type input { flex: 1 1 auto; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--fg-ink); font: 500 15px/1.4 'JetBrains Mono', ui-monospace, monospace; }+.fg .fg-type:focus-within { border-color: var(--fg-ink2); }+.fg .fg-part.fg-pressable .n::after { content: ' · press'; font-weight: 500; color: var(--fg-ink3); font-size: 11px; }+.fg .fg-dragging { cursor: grabbing !important; }+.fg .fg-fig { -webkit-user-select: none; user-select: none; }+/* framed with #embed: the page is as tall as its content (a height tied to the window would grow with every resize) */+html[data-fg-embed] .fg .fg-app { min-height: 0; }diff --git a/skills/showtime/runtime/figure.js b/skills/showtime/runtime/figure.jsindex 563b824..3f239c2 100644--- a/skills/showtime/runtime/figure.js+++ b/skills/showtime/runtime/figure.js@@ -21,6 +21,12 @@* agree(state, stepId, {board, step, readouts}) -> [contradictions] (the walk reports them),* crop: {desk: [x0, y0, x1, y1], phone: [...]}, start: t (the live clock starts here: a film-first scene starts* after its build), film: 'tour' (index.html draws the tour as the film: Figure.film) }+ * Touch (the viewer acts on the board itself):+ * type: fn(fx, ch, state, stepId) | {hint, run}: every character the viewer types (Enter '\n', Backspace '\b'),+ * also from the "Type" field (a phone's own keyboard); with it, letters and digits are the scene's (the page's R,+ * L and 1-9 shortcuts are buttons only; arrows and Esc stay)+ * parts[].press: fn(fx, state, stepId): a click or tap on that part (or its button in the parts list) runs it+ * parts[].drag: {knob, axis: 'x' | 'y', span: px for the knob's whole range}: dragging that part turns the knob* A step's run(fx, again) writes keys through fx: level(id, v), count(id, n), say(tag, text, tone), hide(tag),* pulse(part, tone), tone(part, tone | null), show(part, on), send(token, moves, {stay}), park(token), focus(part),* every(ms, fn, name); each takes {delay, dur} where it@@ -48,6 +54,8 @@var mq = function (q) { try { return root.matchMedia && root.matchMedia(q).matches; } catch (e) { return false; } };var RM = opts.reduced != null ? !!opts.reduced : mq('(prefers-reduced-motion: reduce)');var parts = partsOf(spec, I), PART = {}; parts.forEach(function (p) { PART[p.id] = p; });+ var TYPE = typeof I.type === 'function' ? { run: I.type } : (I.type && typeof I.type.run === 'function' ? I.type : null);+ var typeHint = TYPE ? (TYPE.hint || 'Type on it') : '';var state = Object.assign(knobDefaults(I), opts.state || {}), S = { step: 0, hover: '', pin: '' };/* ---- DOM ---- */@@ -66,6 +74,13 @@(I.source ? '<p class="fg-src">' + esc(I.source) + '</p>' : '') + '</div>' +'<div class="fg-tour" hidden><span class="dot"></span><span>Showing you around: touch anything to take over</span><button type="button">Take over</button></div>';main.appendChild(strip);+ if (TYPE) {+ var typeEl = el('label', 'fg-type', '<span class="fg-type-k"></span><input type="text" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" enterkeyhint="enter">');+ typeEl.querySelector('.fg-type-k').textContent = typeHint;+ typeEl.querySelector('input').setAttribute('aria-label', typeHint + ': what you type here is typed on the figure');+ typeEl.querySelector('input').placeholder = 'type here or anywhere';+ main.insertBefore(typeEl, strip);+ }var rail = el('aside', 'fg-rail');rail.setAttribute('aria-label', 'Steps and parts');rail.innerHTML = '<div class="fg-brand">' + esc(I.kicker || 'Live figure') + '</div><h1>' + esc(I.title || (spec.page && spec.page.title || '').replace(/\*/g, '') || 'Living diagram') + '</h1>' +@@ -74,7 +89,8 @@(parts.length ? '<section class="fg-sec"><h2 id="fg-h-parts">Parts</h2><div class="fg-parts" role="group" aria-labelledby="fg-h-parts"></div></section>' : '') +'<section class="fg-sec fg-narr-sec" hidden><details><summary>Narration for this step</summary><p class="fg-narr"></p></details></section>' +'<section class="fg-sec fg-row">' + (I.steps.length ? '<button type="button" class="fg-btn fg-again">Run this step again</button>' : '') + '<button type="button" class="fg-btn fg-copy">Copy a link to this state</button></section>' +- '<div class="fg-keys">' + (I.steps.length ? '<kbd>←</kbd> <kbd>→</kbd> steps · <kbd>1</kbd>-<kbd>9</kbd> jump · <kbd>R</kbd> run again · ' : '') + '<kbd>Tab</kbd> parts and controls · <kbd>Esc</kbd> clear · <kbd>L</kbd> copy link</div>' ++ (TYPE ? '<div class="fg-keys">' + esc(typeHint) + ': type on your keyboard · ' + (I.steps.length ? '<kbd>←</kbd> <kbd>→</kbd> steps · ' : '') + '<kbd>Tab</kbd> parts and controls · <kbd>Esc</kbd> clear</div>'+ : '<div class="fg-keys">' + (I.steps.length ? '<kbd>←</kbd> <kbd>→</kbd> steps · <kbd>1</kbd>-<kbd>9</kbd> jump · <kbd>R</kbd> run again · ' : '') + '<kbd>Tab</kbd> parts and controls · <kbd>Esc</kbd> clear · <kbd>L</kbd> copy link</div>') +'<div class="fg-live" aria-live="polite"></div>';var app = el('div', 'fg-app'); app.appendChild(main); app.appendChild(rail); host.appendChild(app);var $ = function (s) { return host.querySelector(s); };@@ -191,7 +207,8 @@b.addEventListener('blur', function () { setHover(''); });b.addEventListener('mouseenter', function () { setHover(p.id); });b.addEventListener('mouseleave', function () { setHover(''); });- b.addEventListener('click', function () { touched(); pin(p.id); });+ b.addEventListener('click', function () { touched(); if (p.press) pressPart(p.id); else pin(p.id); });+ if (p.press) b.classList.add('fg-pressable');partsEl.appendChild(b);});var shownKnob = null;@@ -273,19 +290,89 @@}/* ---- pointer and keys ---- */+ // a part with drag turns its knob while dragged; a part with press acts on a click; any other part pins its card+ var dragging = null, dragged = false, dragRaf = 0;+ if (parts.some(function (p) { return p.drag; })) fig.style.touchAction = 'pan-y';+ fig.addEventListener('pointerdown', function (e) {+ var id = hit(e.clientX, e.clientY), p = id && PART[id], d = p && p.drag, K = d && I.knobs[d.knob];+ dragged = false;+ if (!K || K.options) return;+ dragging = { id: id, d: d, K: K, x: e.clientX, y: e.clientY, v0: Number(state[d.knob]), pid: e.pointerId, moved: false };+ });fig.addEventListener('pointermove', function (e) {- var id = hit(e.clientX, e.clientY); fig.style.cursor = id ? 'pointer' : 'default';+ if (dragging && dragging.pid === e.pointerId) {+ var g = dragging, dist = g.d.axis === 'y' ? g.y - e.clientY : e.clientX - g.x;+ if (!g.moved && Math.abs(dist) > 4) { g.moved = true; touched(); try { fig.setPointerCapture(e.pointerId); } catch (err) { /* gone */ } fig.classList.add('fg-dragging'); setHover(g.id); }+ if (g.moved) {+ var step = g.K.step || 1, v = g.v0 + dist / (g.d.span || 320) * (g.K.max - g.K.min);+ v = Math.max(g.K.min, Math.min(g.K.max, Math.round((v - g.K.min) / step) * step + g.K.min));+ v = +v.toFixed(6);+ if (v !== state[g.d.knob] && !dragRaf) { dragRaf = (root.requestAnimationFrame || setTimeout)(function () { dragRaf = 0; if (dragging) setKnob(dragging.d.knob, dragging.want); }); }+ g.want = v;+ return;+ }+ }+ var id = hit(e.clientX, e.clientY), pp = id && PART[id];+ fig.style.cursor = !id ? 'default' : pp && pp.drag ? 'grab' : 'pointer';if (e.pointerType === 'mouse') { touched(); setHover(id); }});- fig.addEventListener('pointerleave', function () { setHover(''); });- fig.addEventListener('click', function (e) { touched(); var id = hit(e.clientX, e.clientY); if (id) pin(id); else if (S.pin) pin(S.pin); });+ function endDrag(e) {+ if (!dragging || dragging.pid !== e.pointerId) return;+ var g = dragging; dragging = null; fig.classList.remove('fg-dragging');+ if (g.moved) { dragged = true; if (g.want != null && g.want !== state[g.d.knob]) setKnob(g.d.knob, g.want); }+ }+ fig.addEventListener('pointerup', endDrag);+ fig.addEventListener('pointercancel', endDrag);+ fig.addEventListener('pointerleave', function () { if (!dragging) setHover(''); });+ fig.addEventListener('click', function (e) {+ touched();+ if (dragged) { dragged = false; return; }+ var id = hit(e.clientX, e.clientY);+ if (id && PART[id] && PART[id].press) pressPart(id);+ else if (id) pin(id); else if (S.pin) pin(S.pin);+ });+ function pressPart(id) {+ var p = PART[id], sid = (I.steps[S.step] || {}).id;+ if (!p || !p.press) return;+ act(function () { p.press(fx, state, sid); });+ if (S.pin !== id) { S.pin = id; api.focus(id, { at: clock.now() }); }+ ui(); sync();+ }+ // typing: a character on the board (the scene's type.run), from anywhere on the page or from the Type field+ function typeCh(ch) {+ if (!TYPE) return;+ var sid = (I.steps[S.step] || {}).id;+ touched();+ act(function () { TYPE.run(fx, ch, state, sid); });+ ui();+ }+ if (TYPE) {+ var tin = $('.fg-type input');+ tin.addEventListener('input', function (e) {+ var v = tin.value; tin.value = '';+ for (var i = 0; i < v.length; i++) typeCh(v.charAt(i));+ });+ tin.addEventListener('keydown', function (e) {+ e.stopPropagation();+ if (e.key === 'Enter') { e.preventDefault(); typeCh('\n'); }+ else if (e.key === 'Backspace' && !tin.value) { e.preventDefault(); typeCh('\b'); }+ else if (e.key === 'Escape') tin.blur();+ });+ }root.addEventListener('keydown', function (e) {var tag = (e.target && e.target.tagName) || '';touched();if (e.key === 'Escape') { S.pin = ''; setHover(''); api.focus(null, { at: clock.now() }); dirty = true; ui(); sync(); return; }- if (tag === 'INPUT' || tag === 'TEXTAREA' || e.metaKey || e.ctrlKey || e.altKey) return;+ if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || e.metaKey || e.ctrlKey || e.altKey) return;if (e.key === 'ArrowRight' || e.key === 'PageDown') { if (I.steps.length) { e.preventDefault(); enter(S.step + 1); } }else if (e.key === 'ArrowLeft' || e.key === 'PageUp') { if (I.steps.length) { e.preventDefault(); enter(S.step - 1); } }+ else if (I.keys && I.keys[e.key.toLowerCase()]) pressKey(e.key.toLowerCase());+ else if (TYPE) {+ if (e.repeat && e.key.length !== 1) return;+ if (e.key === 'Enter') { if (tag === 'BUTTON' || tag === 'A') return; e.preventDefault(); typeCh('\n'); }+ else if (e.key === 'Backspace') { e.preventDefault(); typeCh('\b'); }+ else if (e.key.length === 1) { if (e.key === ' ' && (tag === 'BUTTON' || tag === 'A')) return; e.preventDefault(); typeCh(e.key); }+ }else if (/^[1-9]$/.test(e.key) && +e.key <= I.steps.length) enter(+e.key - 1);else if (e.key === 'r' || e.key === 'R') enter(S.step);else if (e.key === 'l' || e.key === 'L') copyLink();@@ -458,13 +545,17 @@});if (!I.steps.length) parts.forEach(function (p) { safely(p.id, function () { setHover(p.id); check(p.id + ' hovered'); }); });setHover('');+ // touch: a typed line (letters, a digit, a space, Return, Backspace) and every pressable part, from the first step+ if (TYPE) safely('typing', function () { if (I.steps.length) enter(0); 'Hi 2\n\bok.'.split('').forEach(function (ch) { act(function () { TYPE.run(fx, ch, state, (I.steps[S.step] || {}).id); }); }); ui(); check('typed "Hi 2", Return, Backspace, "ok."'); });+ parts.forEach(function (p) { if (p.press) safely(p.id + ' pressed', function () { if (I.steps.length) enter(0); pressPart(p.id); check(p.id + ' pressed'); }); });+ S.pin = '';var seen = {}; probs = probs.filter(function (p) { var key = p.code + '|' + p.text; if (seen[key]) return false; seen[key] = 1; return true; });- return { states: states, problems: probs, keys: keyNames.length, steps: I.steps.length, parts: parts.length, knobs: Object.keys(I.knobs || {}).length, theme: api.report.theme, warnings: api.report.warnings.slice() };+ return { states: states, problems: probs, keys: keyNames.length, typing: !!TYPE, presses: parts.filter(function (p) { return p.press; }).length, steps: I.steps.length, parts: parts.length, knobs: Object.keys(I.knobs || {}).length, theme: api.report.theme, warnings: api.report.warnings.slice() };}/* ---- start: the board is already there and moving ---- */var fig0 = { api: api, enter: enter, setKnob: setKnob, setHover: setHover, pin: pin, walk: walk, state: state, S: S, cost: cost, readouts: readouts,- tour: function () { return tour.on; }, hash: hashOf, phone: function () { return phone; } };+ tour: function () { return tour.on; }, hash: hashOf, phone: function () { return phone; }, type: typeCh, press: pressPart };api.ready.then(function () {fit();// the tour's first wait: opts.tourAfter, or window.SHOWTIME_TEST_TOUR_AFTER (tests), else 2.5 s@@ -474,6 +565,20 @@ui(); wake();});fit(); ui(); wake();+ // framed with #embed (an iframe, or <showtime-xray> from embed.js): tell the host how tall the page is+ var EMBED = false;+ try { EMBED = root.self !== root.top && /(^#|[&_])embed([&_]|$)/.test(root.location.hash || ''); } catch (e) { EMBED = false; }+ if (EMBED && root.ResizeObserver) {+ D.documentElement.setAttribute('data-fg-embed', '');+ var lastH = 0, lastGrow = -1e9;+ new ResizeObserver(function () {+ var h = Math.ceil(app.getBoundingClientRect().height + 24), now = root.performance ? performance.now() : 0;+ if (Math.abs(h - lastH) < 2 || (h < lastH && lastH - h < 24 && now - lastGrow < 1200)) return; // a host's scrollbar flip+ if (h > lastH) lastGrow = now;+ lastH = h;+ try { root.parent.postMessage({ type: 'showtime-xray:size', height: h }, '*'); } catch (e) { /* no host */ }+ }).observe(app);+ }Figure.current = fig0;return fig0;}diff --git a/skills/showtime/runtime/xray-live/embed.js b/skills/showtime/runtime/xray-live/embed.jsnew file mode 100644index 0000000..1607651--- /dev/null+++ b/skills/showtime/runtime/xray-live/embed.js@@ -0,0 +1,102 @@+/* <showtime-xray>: an X-ray's live page on any web page, with one tag (runtime/xray-live/embed.js).+ *+ * <script src="https://faviovazquez.github.io/showtime/xray/embed.js"></script>+ * <showtime-xray src="typewriter-live.html" ratio="16/9" title="A typewriter, seen through"+ * poster="typewriter.jpg" hit="4"></showtime-xray>+ *+ * The live page (`showtime export html <xray project> --interactive`) is one offline file; this element shows its+ * poster (or a plain plate) with a "Play it live" button and loads the page in a frame only when the viewer asks,+ * so a page with many X-rays stays light. With `autoload` it loads at once. The frame opens on the page's embed+ * layout (#embed: the film fits the width) at hit N when `hit` is set, and takes the height the page reports+ * (postMessage {type: 'showtime-xray:size', height}). No dependencies; nothing is fetched until the frame loads.+ * Style hooks (light DOM): .stx, .stx-poster, .stx-play, .stx-frame. el.load() loads it from script.+ */+(function () {+ 'use strict';+ var W = window, D = document;+ if (!W.customElements || W.customElements.get('showtime-xray')) return;++ var CSS = 'showtime-xray{display:block;max-width:100%}' ++ '.stx{position:relative;width:100%;border-radius:8px;overflow:hidden;background:#070b16;color:#e9f3ff;font:600 15px/1.3 system-ui,-apple-system,"Segoe UI",sans-serif}' ++ '.stx-poster{position:relative;width:100%;background:#070b16 center/cover no-repeat;display:flex;align-items:center;justify-content:center}' ++ '.stx-plate{position:absolute;left:16px;top:14px;right:16px;display:flex;flex-direction:column;gap:4px;text-shadow:0 1px 8px rgba(0,0,0,.6)}' ++ '.stx-mark{font:700 12px/1 ui-monospace,SFMono-Regular,Menlo,monospace;letter-spacing:.14em;text-transform:uppercase;color:#ff4a3d}' ++ '.stx-play{appearance:none;border:0;border-radius:999px;padding:12px 20px 12px 16px;background:#ff4a3d;color:#fff;font:700 15px/1 system-ui,-apple-system,"Segoe UI",sans-serif;' ++ 'display:inline-flex;align-items:center;gap:8px;cursor:pointer;box-shadow:0 6px 24px rgba(0,0,0,.35)}' ++ '.stx-play:hover{background:#ff6a5e}.stx-play:focus-visible{outline:3px solid #8fd3ff;outline-offset:3px}' ++ '.stx-play svg{width:18px;height:18px}' ++ '.stx-frame{display:block;width:100%;border:0;background:#070b16}';++ function style() {+ if (D.getElementById('stx-css')) return;+ var s = D.createElement('style'); s.id = 'stx-css'; s.textContent = CSS;+ (D.head || D.documentElement).appendChild(s);+ }+ function ratioOf(v) { // "16/9", "16:9", "1.777" -> width / height+ var m = /^\s*(\d+(?:\.\d+)?)\s*[/:x]\s*(\d+(?:\.\d+)?)\s*$/.exec(String(v || ''));+ if (m && +m[2] > 0) return +m[1] / +m[2];+ var n = parseFloat(v);+ return n > 0 ? n : 16 / 9;+ }++ // one listener for every frame on the page: the page in each frame reports its height+ var frames = [];+ W.addEventListener('message', function (ev) {+ var d = ev.data;+ if (!d || d.type !== 'showtime-xray:size' || !(d.height > 0)) return;+ for (var i = 0; i < frames.length; i++) {+ if (frames[i].contentWindow === ev.source) { frames[i].style.height = Math.min(20000, Math.ceil(d.height)) + 'px'; return; }+ }+ });++ class ShowtimeXray extends HTMLElement {+ connectedCallback() {+ if (this._built) return;+ this._built = true;+ style();+ var self = this, r = ratioOf(this.getAttribute('ratio'));+ var box = D.createElement('div'); box.className = 'stx';+ var poster = D.createElement('div'); poster.className = 'stx-poster';+ poster.style.aspectRatio = String(r);+ var pic = this.getAttribute('poster');+ if (pic) poster.style.backgroundImage = 'url("' + pic.replace(/["\\\n]/g, '') + '")';+ var plate = D.createElement('div'); plate.className = 'stx-plate';+ var mark = D.createElement('span'); mark.className = 'stx-mark'; mark.textContent = 'X-ray · live';+ var ttl = D.createElement('span'); ttl.textContent = this.getAttribute('title') || '';+ plate.appendChild(mark); if (ttl.textContent) plate.appendChild(ttl);+ var b = D.createElement('button'); b.type = 'button'; b.className = 'stx-play';+ b.innerHTML = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M7.5 4.8v14.4c0 .8.9 1.3 1.6.9l11.2-7.2c.6-.4.6-1.3 0-1.7L9.1 3.9c-.7-.4-1.6.1-1.6.9z"/></svg>';+ b.appendChild(D.createTextNode('Play it live'));+ b.setAttribute('aria-label', 'Play it live' + (ttl.textContent ? ': ' + ttl.textContent : ''));+ b.addEventListener('click', function () { self.load(true); });+ poster.appendChild(plate); poster.appendChild(b);+ box.appendChild(poster);+ this.textContent = '';+ this.appendChild(box);+ this._box = box; this._ratio = r;+ if (this.hasAttribute('autoload')) this.load(false);+ }+ load(focus) {+ if (!this._box || this._frame) return this._frame || null;+ var src = this.getAttribute('src');+ if (!src) return null;+ var hit = parseInt(this.getAttribute('hit'), 10);+ var f = D.createElement('iframe');+ f.className = 'stx-frame';+ f.title = this.getAttribute('title') || 'X-ray, live';+ f.setAttribute('allow', 'autoplay; fullscreen; clipboard-write');+ f.src = src.split('#')[0] + '#embed' + (hit > 0 ? '_hit' + hit : '');+ // until the page reports its height: the film plus room for its controls+ var w = this._box.clientWidth || 640;+ f.style.height = Math.round(w / this._ratio + (w < 700 ? 360 : 300)) + 'px';+ frames.push(f);+ this._box.textContent = '';+ this._box.appendChild(f);+ this._frame = f;+ if (focus) f.addEventListener('load', function () { try { f.focus(); } catch (e) { /* cross-origin */ } }, { once: true });+ this.dispatchEvent(new CustomEvent('showtime-xray:load', { bubbles: true }));+ return f;+ }+ }+ W.customElements.define('showtime-xray', ShowtimeXray);+})();diff --git a/skills/showtime/runtime/xray-live/live.css b/skills/showtime/runtime/xray-live/live.cssnew file mode 100644index 0000000..a128394--- /dev/null+++ b/skills/showtime/runtime/xray-live/live.css@@ -0,0 +1,141 @@+/* showtime X-ray live page: the controls around the film (runtime/xray-live/live.js). The film itself keeps its+ own styles; this only undoes the render-mode page size, scales the film to the window and draws the controls. */+:root[data-xl] {+ --xl-bg: #070b16; --xl-ink: #e9f3ff; --xl-muted: #a9bdd6; --xl-line: rgba(143, 211, 255, .32);+ --xl-hot: #ff4a3d; --xl-skip: rgba(233, 243, 255, .38); --xl-btn: rgba(143, 211, 255, .1); --xl-btn-on: rgba(255, 74, 61, .2);+ --xl-focus: #8fd3ff; color-scheme: dark;+}+:root[data-xl][data-xl-theme="light"] {+ --xl-bg: #f4f0e7; --xl-ink: #121e34; --xl-muted: #4d5a72; --xl-line: rgba(24, 62, 120, .3);+ --xl-hot: #c8202f; --xl-skip: rgba(18, 30, 52, .38); --xl-btn: rgba(24, 62, 120, .08); --xl-btn-on: rgba(200, 32, 47, .14);+ --xl-focus: #183e78; color-scheme: light;+}+html[data-xl] { overflow: auto !important; background: var(--xl-bg) !important; }+html[data-xl] body { width: auto !important; height: auto !important; min-height: 100vh; overflow: visible !important; margin: 0 !important;+ background: var(--xl-bg) !important; color: var(--xl-ink); }+.xl-app { box-sizing: border-box; width: 100%; max-width: 1632px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; align-items: center;+ font: 500 14px/1.45 "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--xl-ink); }+.xl-app * { box-sizing: border-box; }+.xl-head, .xl-bar, .xl-meta { width: var(--xl-w, 100%); max-width: 100%; min-width: min(100%, 640px); }+.xl-head { display: flex; flex-wrap: nowrap; align-items: baseline; justify-content: space-between; gap: 4px 16px; margin: 0 0 10px; }+.xl-title { flex: none; }+.xl-title { margin: 0; font: 700 18px/1.25 "IBM Plex Mono", ui-monospace, monospace; letter-spacing: .02em; }+/* one line that never wraps: the page must not move while it plays or as keys are struck */+.xl-now { margin: 0; color: var(--xl-muted); font-variant-numeric: tabular-nums; min-height: 1.45em; flex: 1 1 auto; min-width: 0; text-align: right; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }+/* the film, scaled from its own size; the marker square is for verify, not for viewers */+.xl-view { position: relative; overflow: hidden; border-radius: 6px; outline: 1px solid var(--xl-line); background: #000; flex: none; touch-action: pan-y; cursor: grab;+ -webkit-user-select: none; user-select: none; }+.xl-view.is-scrub { cursor: grabbing; }+.xl-view.on-label { cursor: pointer; }+/* a hint over the picture, gone at the first touch */+.xl-toast { position: absolute; left: 50%; bottom: 14px; transform: translate(-50%, 8px); margin: 0; max-width: calc(100% - 24px); padding: 7px 12px; border-radius: 6px;+ background: rgba(7, 11, 22, .82); color: #e9f3ff; font-size: 13px; line-height: 1.35; text-align: center; opacity: 0; pointer-events: none;+ transition: opacity .25s, transform .25s; }+.xl-toast.is-on { opacity: 1; transform: translate(-50%, 0); }+.xl-scale { position: absolute; left: 0; top: 0; transform-origin: 0 0; }+.xl-scale > #stage { position: relative !important; left: 0 !important; top: 0 !important; overflow: hidden !important; }+#xmark { display: none !important; }+canvas[data-xl-mirror] { position: absolute; left: 0; top: 0; }+.xl-hit { position: absolute; inset: 0; pointer-events: none; }+.xl-veil { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--xl-bg); color: var(--xl-muted); padding: 16px; text-align: center; }+.xl-veil[hidden] { display: none; }+/* the diagram layout's part labels: the hovered one acts, the rest step back */+[data-ld-label].xl-on text:first-of-type { fill: var(--xd-hot, #ff6a3d) !important; }+[data-ld-label].xl-on path, [data-ld-label].xl-on circle { stroke: var(--xd-hot, #ff6a3d) !important; stroke-width: 3px !important; }+[data-ld-label].xl-off { filter: opacity(.4); }+/* controls */+.xl-bar { margin-top: 12px; }+.xl-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }+.xl-grow { flex: 1 1 0; }+.xl-btn { appearance: none; border: 1px solid var(--xl-line); background: var(--xl-btn); color: var(--xl-ink); border-radius: 6px; min-height: 40px; min-width: 40px;+ padding: 0 12px; font: 600 14px/1 "IBM Plex Mono", ui-monospace, monospace; letter-spacing: .02em; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }+.xl-btn svg { width: 20px; height: 20px; }+.xl-btn:hover { border-color: var(--xl-ink); }+.xl-btn[aria-pressed="true"] { background: var(--xl-btn-on); border-color: var(--xl-hot); color: var(--xl-hot); }+.xl-btn:focus-visible, .xl-track:focus-visible, .xl-part:focus-visible, .xl-tick:focus-visible { outline: 2px solid var(--xl-focus); outline-offset: 2px; }+.xl-play { width: 48px; padding: 0; }+.xl-time { color: var(--xl-muted); font-variant-numeric: tabular-nums; white-space: nowrap; min-width: 17ch; }+.xl-btn[hidden] { display: none; }+.xl-track { position: relative; height: 56px; margin-top: 12px; border-bottom: 1px solid var(--xl-line); cursor: pointer; touch-action: none; }+.xl-ticks { position: absolute; inset: 0 3px; }+.xl-tick { position: absolute; bottom: 0; width: 14px; height: var(--h, 60%); margin-left: -7px; padding: 0; border: 0; background: transparent; cursor: pointer; }+.xl-tick::after { content: ""; position: absolute; left: 5px; right: 5px; bottom: 0; top: 0; border-radius: 2px 2px 0 0; background: var(--xl-hot); opacity: .45; }+.xl-tick.is-past::after { opacity: 1; }+.xl-tick.is-skip::after { background: var(--xl-skip); opacity: .6; }+.xl-tick:hover::after { left: 3px; right: 3px; opacity: 1; }+.xl-playhead { position: absolute; top: -4px; bottom: -4px; width: 2px; margin-left: 2px; background: var(--xl-ink); pointer-events: none; }+.xl-playhead::before { content: ""; position: absolute; top: -2px; left: -6px; border: 7px solid transparent; border-top-color: var(--xl-ink); border-bottom: 0; }+.xl-legend { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 8px 0 0; color: var(--xl-muted); font-size: 13px; }+.xl-key-hot::before, .xl-key-skip::before { content: ""; display: inline-block; width: 4px; height: 12px; margin-right: 7px; vertical-align: -1px; background: var(--xl-hot); border-radius: 1px; }+.xl-key-skip::before { background: var(--xl-skip); }+.xl-key-help { margin-left: auto; }+.xl-parts { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 12px; }+.xl-parts[hidden] { display: none; }+.xl-k { color: var(--xl-muted); font-size: 13px; text-transform: uppercase; letter-spacing: .08em; margin-right: 4px; }+.xl-part { appearance: none; border: 1px solid var(--xl-line); background: transparent; color: var(--xl-ink); border-radius: 999px; min-height: 32px; padding: 0 12px;+ font: 500 13px/1 "IBM Plex Mono", ui-monospace, monospace; text-transform: uppercase; letter-spacing: .05em; cursor: pointer; }+.xl-part[aria-pressed="true"], .xl-part:hover { border-color: var(--xl-hot); color: var(--xl-hot); }+.xl-meta { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--xl-line); }+.xl-meta p { margin: 0 0 6px; }+.xl-receipt { color: var(--xl-ink); border: 1.5px solid var(--xl-hot); border-radius: 6px; padding: 8px 12px; display: inline-block; }+.xl-drawn, .xl-credit { color: var(--xl-muted); font-size: 13px; }+/* pads: play it from the real clip (a keyboard for a typewriter, a strip of notes or drums) */+.xl-pads { margin-top: 14px; display: flex; flex-direction: column; gap: 6px; }+.xl-pads[hidden] { display: none; }+.xl-pads-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin: 0 0 2px; }+.xl-pads-head .xl-k { color: var(--xl-hot); }+.xl-pads-help { color: var(--xl-muted); font-size: 13px; }+.xl-keyrow { display: flex; justify-content: center; gap: 6px; }+.xl-keyrow-2 { padding-left: 18px; }+.xl-keyrow-3 { padding-left: 46px; }+.xl-padrow { display: flex; flex-wrap: wrap; gap: 6px; }+.xl-pad { appearance: none; position: relative; flex: 0 0 auto; min-width: 44px; height: 46px; padding: 0 8px; border-radius: 6px; cursor: pointer;+ border: 1px solid var(--xl-line); border-bottom-width: 3px; background: var(--xl-btn); color: var(--xl-ink);+ font: 600 15px/1 "IBM Plex Mono", ui-monospace, monospace; display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;+ transition: transform .06s, background-color .15s, border-color .15s; }+.xl-keyrow .xl-pad { flex: 0 1 46px; min-width: 0; padding: 0 2px; }+.xl-keyrow .xl-pad.is-wide { flex-basis: 180px; }+.xl-keyrow .xl-pad.is-word { flex-basis: 76px; font-size: 13px; }+.xl-pad:hover { border-color: var(--xl-ink); }+.xl-pad.is-off { opacity: .28; cursor: default; border-bottom-width: 1px; }+.xl-pad-n { font-size: 10px; font-weight: 500; color: var(--xl-muted); }+.xl-pad kbd { position: absolute; top: 3px; right: 4px; font: 500 9px/1 "IBM Plex Mono", ui-monospace, monospace; color: var(--xl-muted); }+.xl-pad.is-on, .xl-pad.is-hit { background: var(--xl-btn-on); border-color: var(--xl-hot); color: var(--xl-hot); transform: translateY(2px); border-bottom-width: 1px; }+.xl-pad.is-on .xl-pad-n, .xl-pad.is-hit .xl-pad-n { color: var(--xl-hot); }+.xl-pad:focus-visible { outline: 2px solid var(--xl-focus); outline-offset: 2px; }+/* link, embed, the line that made it */+.xl-share { margin-top: 12px; display: flex; flex-direction: column; gap: 10px; }+.xl-share[hidden] { display: none; }+.xl-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }+.xl-field-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }+.xl-field textarea { width: 100%; resize: vertical; border: 1px solid var(--xl-line); border-radius: 6px; padding: 8px 10px; background: var(--xl-btn); color: var(--xl-ink);+ font: 500 13px/1.45 "IBM Plex Mono", ui-monospace, monospace; white-space: pre-wrap; word-break: break-all; }+.xl-copy { min-height: 30px; padding: 0 10px; font-size: 12px; }+.xl-note { color: var(--xl-muted); font-size: 13px; margin: 0; }+.xl-made { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }+.xl-made > .xl-k { margin: 0; color: var(--xl-hot); }+.xl-cmds summary { cursor: pointer; color: var(--xl-muted); font-size: 13px; }+.xl-cmds[open] summary { margin-bottom: 6px; }+/* framed by <showtime-xray> or an iframe with #embed: no page chrome around the film */+html[data-xl-embed] body { min-height: 0; }+html[data-xl-embed] .xl-app { padding: 12px 16px 14px; }+@media (max-width: 700px) {+ .xl-app { padding: 12px 16px 24px; }+ .xl-head, .xl-bar, .xl-meta { min-width: 0; width: 100%; }+ .xl-title { font-size: 16px; }+ .xl-head { flex-direction: column; align-items: stretch; }+ .xl-now { font-size: 13px; text-align: left; }+ .xl-time { order: 10; width: 100%; font-size: 13px; }+ .xl-grow { display: none; }+ .xl-btn { min-height: 44px; padding: 0 10px; }+ .xl-key-help { display: none; }+ .xl-track { height: 64px; }+ .xl-keyrow { gap: 4px; }+ .xl-keyrow .xl-pad { height: 42px; font-size: 13px; }+ .xl-keyrow-2 { padding-left: 10px; }+ .xl-keyrow-3 { padding-left: 26px; }+ .xl-pads-help { display: none; }+}+@media (prefers-reduced-motion: reduce) {+ .xl-app *, .xl-app *::before, .xl-app *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }+}diff --git a/skills/showtime/runtime/xray-live/live.js b/skills/showtime/runtime/xray-live/live.jsnew file mode 100644index 0000000..ea65bdf--- /dev/null+++ b/skills/showtime/runtime/xray-live/live.js@@ -0,0 +1,879 @@+/* showtime X-ray live page, part 2 of 2 (runtime/xray-live/live.js): the controls around the film's own page.+ *+ * The page underneath is the project's index.html, unchanged: xray.js and mechanism.js (or the diagram layout's+ * page and its scene) draw every frame through ST.onSeek, exactly as in the film. This script only picks the+ * frame: from the clip while it plays (its own sound is the clock), or the one the viewer jumps to.+ * - the clip: packed once, one blob: URL; a second copy of it (the radiograph) is a canvas mirror of the first+ * - play / pause with the clip's sound; a timeline with every event as a tick (red kept, grey skipped); a click on+ * a tick, or the arrow keys, jumps to that event's exact frame (where its strike peaks)+ * - 0.25x around the current event; light / dark; see-through / split when the clip is tracked+ * - part labels: hover or tap one (or pick it in the list) and it lights up+ * - touch: pads under the picture, one per kind of event (a typewriter's keys, an instrument's notes, a kit's+ * drums); a pad, or its key on the keyboard, plays the next such event from the real clip with its sound.+ * The picture: drag sideways to scrub; a click plays the next event (or pauses while playing)+ * - links to a hit (#hit4) copied from the page; an embed snippet (<showtime-xray> or an iframe with #embed)+ * - the line that made it (the prompt and the commands, showtime.json "xray".made / export --prompt)+ * - the receipt line (showtime.json "xray".receipt) and the credit line; no sync marker (it is for verify only)+ * The timeline stops at the clip's end: the film's end card is the receipt and credit under the picture here.+ * window.xrayLive: state(), seek(t), jump(i), play(), pause(), slow(), strike(pad), theme(name), layout(name),+ * hover(name), link().+ */+(function () {+ 'use strict';+ var W = window, D = document, R = W.__XL_REAL__, ST = W.ST;+ var cfgEl = D.getElementById('xl-cfg');+ if (!R || !ST || !cfgEl) return;+ var C = JSON.parse(cfgEl.textContent), L = C.live, CFG = R.cfg || {}, XCFG = CFG.xray || {};+ var FPS = L.fps || 30, FW = L.W, FH = L.H, DIAGRAM = L.layout === 'diagram';+ var $ = function (s, r) { return (r || D).querySelector(s); };+ var $$ = function (s, r) { return Array.prototype.slice.call((r || D).querySelectorAll(s)); };+ function mk(tag, attrs, parent, text) {+ var e = D.createElement(tag);+ Object.keys(attrs || {}).forEach(function (k) { if (attrs[k] != null) e.setAttribute(k, attrs[k]); });+ if (text != null) e.textContent = text;+ if (parent) parent.appendChild(e);+ return e;+ }++ // ------------------------------------------------------------------ media: one blob: URL per packed file+ var urls = {};+ $$('script[data-xl-media]').forEach(function (s) {+ var b = atob(s.textContent.trim()), n = b.length, u = new Uint8Array(n);+ for (var i = 0; i < n; i++) u[i] = b.charCodeAt(i);+ urls[s.getAttribute('data-xl-media')] = URL.createObjectURL(new Blob([u], { type: s.getAttribute('data-type') || 'video/webm' }));+ s.textContent = '';+ });+ var vids = $$('video[data-xl-src]');+ vids.forEach(function (v) { v.src = urls[v.getAttribute('data-xl-src')]; v.preload = 'auto'; v.playsInline = true; v.muted = true; });+ var master = D.getElementById('clip') || vids[0];+ var followers = vids.filter(function (v) { return v !== master; });+ var mirrors = $$('canvas[data-xl-mirror]');++ // ------------------------------------------------------------------ the page's own drawing, observed+ // The mechanism's draw(k) is wrapped (never replaced): it records the events the page uses (k.ev), the kit's+ // state on each frame (which event acts, its 0..1 motion) and where each part label landed (the hit list), and+ // draws the hovered label as acting. The diagram layout: its scene builder hands over the same events.+ var X = W.XRAY, hot = null, pending = [], hits = [], kitState = null, events = null, partNames = [];+ function evCopy(list) {+ return list.map(function (e) {+ var nn = (e.notes || []).filter(function (q) { return q && q.name; });+ var notes = nn.map(function (q) { return String(q.name); });+ var pitches = nn.map(function (q) { return typeof q.pitch === 'number' ? q.pitch : null; });+ return { i: e.i, frame: e.frame, kind: e.kind, ch: e.ch == null ? null : e.ch, strength: e.strength, notes: notes, pitches: pitches };+ });+ }+ if (X && X.mech && typeof X.mech.draw === 'function') {+ var draw0 = X.mech.draw;+ X.mech.draw = function (k) {+ if (!events && k.ev) events = evCopy(k.ev);+ var label0 = k.label, mine = [], g = k.g;+ k.label = function (text, at, dx, dy, on) {+ var nm = String(text), isHot = !!hot && nm.toLowerCase() === hot, la = k.labelAlpha, rect = null;+ if (partNames.indexOf(nm) < 0) partNames.push(nm);+ if (hot && !isHot) k.labelAlpha = (la == null ? 1 : la) * 0.35;+ var own = Object.prototype.hasOwnProperty.call(g, 'fillRect'), fr = g.fillRect;+ g.fillRect = function (x, y, w, h) { if (!rect) rect = { x: x, y: y, w: w, h: h }; return fr.apply(g, arguments); };+ try { return label0.call(this, text, at, dx, dy, on || isHot); } finally {+ if (own) g.fillRect = fr; else delete g.fillRect;+ var shown = (la == null ? 1 : la) * (k.alpha == null ? 1 : k.alpha);+ k.labelAlpha = la;+ if (rect && shown > 0.5) { var p = k.pr(at[0], at[1]); mine.push({ name: nm, x: rect.x, y: rect.y, w: rect.w, h: rect.h, ax: p[0], ay: p[1] }); }+ }+ };+ try { draw0.call(this, k); } finally { k.label = label0; }+ mine.forEach(function (h) {+ pending.push(h);+ if (hot && h.name.toLowerCase() === hot) { // a ring on the part the hovered label names+ g.save(); g.globalAlpha = k.alpha == null ? 1 : k.alpha; g.shadowBlur = 0; g.strokeStyle = k.C.hot; g.lineWidth = 3;+ g.beginPath(); g.arc(h.ax, h.ay, 18, 0, Math.PI * 2); g.stroke(); g.restore();+ }+ });+ kitState = { f: k.f, cur: k.cur, s: k.s, hot: !!k.hot, n: k.n, view: k.view, kind: k.kind, ch: k.ch };+ };+ }+ var XD = W.XRAY_DIAGRAM;+ if (XD) Object.keys(XD).forEach(function (n) {+ var b = XD[n];+ if (typeof b !== 'function') return;+ XD[n] = function (ctx) { if (ctx && ctx.events) events = evCopy(ctx.events); return b.apply(this, arguments); };+ });+ // after the page's own handlers (registered first): this frame's labels are complete+ ST.onSeek(function () { if (!DIAGRAM) { hits = pending; pending = []; } });++ // ------------------------------------------------------------------ the layout: the film scaled, controls under it+ var root = D.documentElement;+ // framed by <showtime-xray> or an iframe with #embed: fit the width and tell the host how tall the page is+ var EMBED = !!R.look.embed && W.self !== W.top;+ root.setAttribute('data-xl', '');+ if (EMBED) root.setAttribute('data-xl-embed', '');+ root.setAttribute('data-xl-theme', XCFG.theme === 'light' ? 'light' : 'dark');+ var stage = D.getElementById('stage') || D.body.firstElementChild;+ var app = mk('div', { 'class': 'xl-app', 'data-st-free': '' });+ D.body.insertBefore(app, D.body.firstChild);+ var head = mk('header', { 'class': 'xl-head' }, app);+ mk('h1', { 'class': 'xl-title' }, head, L.title);+ var nowEl = mk('p', { 'class': 'xl-now', 'aria-live': 'polite' }, head, '');+ var view = mk('div', { 'class': 'xl-view' }, app);+ var scaler = mk('div', { 'class': 'xl-scale' }, view);+ scaler.appendChild(stage);+ stage.style.setProperty('width', FW + 'px', 'important');+ stage.style.setProperty('height', FH + 'px', 'important');+ var hitLayer = mk('div', { 'class': 'xl-hit', 'aria-hidden': 'true' }, view);+ var veil = mk('div', { 'class': 'xl-veil' }, view, 'Loading the clip…');++ var bar = mk('div', { 'class': 'xl-bar' }, app);+ var row = mk('div', { 'class': 'xl-row' }, bar);+ function btn(cls, label, text, parent) { var b = mk('button', { type: 'button', 'class': 'xl-btn ' + cls, 'aria-label': label, title: label }, parent || row); b.innerHTML = text; return b; }+ var ICON_PLAY = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M7.5 4.8v14.4c0 .8.9 1.3 1.6.9l11.2-7.2c.6-.4.6-1.3 0-1.7L9.1 3.9c-.7-.4-1.6.1-1.6.9z"/></svg>';+ var ICON_PAUSE = '<svg viewBox="0 0 24 24" aria-hidden="true"><rect fill="currentColor" x="6" y="4.5" width="4.2" height="15" rx="1.2"/><rect fill="currentColor" x="13.8" y="4.5" width="4.2" height="15" rx="1.2"/></svg>';+ var playB = btn('xl-play', 'Play (space)', ICON_PLAY);+ var prevB = btn('xl-prev', 'Previous event (←)', '<span aria-hidden="true">←</span> Event');+ var nextB = btn('xl-next', 'Next event (→)', 'Event <span aria-hidden="true">→</span>');+ var slowB = btn('xl-slow', 'This event at 0.25x (Shift+S)', '0.25×');+ var timeEl = mk('span', { 'class': 'xl-time' }, row, '');+ var spacer = mk('span', { 'class': 'xl-grow' }, row);+ var themeB = btn('xl-theme', 'Light or dark (Shift+T)', 'Light');+ var layoutB = btn('xl-layout', 'See-through or split (Shift+L)', 'Split');+ layoutB.hidden = !L.tracked;+ var linkB = btn('xl-link', 'Copy a link to this moment', 'Link');+ var embedB = btn('xl-embed', 'Embed this page on yours', 'Embed');+ embedB.setAttribute('aria-expanded', 'false');+ var track = mk('div', { 'class': 'xl-track', role: 'slider', tabindex: '0', 'aria-label': 'Clip time', 'aria-valuemin': '0' }, bar);+ var ticks = mk('div', { 'class': 'xl-ticks' }, track);+ var headEl = mk('div', { 'class': 'xl-playhead' }, track);+ var legend = mk('p', { 'class': 'xl-legend' }, bar);+ var padsEl = mk('div', { 'class': 'xl-pads', role: 'group' }, bar);+ var parts = mk('div', { 'class': 'xl-parts', role: 'group', 'aria-label': 'Parts' }, bar);+ var shareEl = mk('div', { 'class': 'xl-share', hidden: '' }, bar);+ var meta = mk('div', { 'class': 'xl-meta' }, app);+ var toast = mk('p', { 'class': 'xl-toast', role: 'status', 'aria-live': 'polite' }, view, '');+ void spacer;++ // ------------------------------------------------------------------ state+ // pass: a stretch of the clip played and stopped on purpose: 'slow' (0.25x around an event, ends on its frame) or+ // 'hit' (a pad: 1x from just before the event to its settle, with its sound)+ var S = { f: -1, playing: false, rate: 1, pass: null, ready: false, busy: false, want: null, going: null, goWant: null };+ var OFF = 0, SFPS = FPS, CLIPDUR = L.clipDur || 0, LAST = 0, kept = [];+ function noun(n) {+ var m = X && X.mech;+ if (m && m.noun) return n === 1 ? (m.noun1 || m.noun.replace(/s$/, '')) : m.noun;+ // the diagram page's own words: a typewriter's keystrokes, else showtime.json "xray" one / noun+ var one = DIAGRAM && (XCFG.mechanism || 'typewriter') === 'typewriter' ? 'keystroke' : (XCFG.one || 'event');+ return n === 1 ? one : (XCFG.noun && one === XCFG.one ? XCFG.noun : one + 's');+ }+ function fmt(t) { return (Math.max(0, t)).toFixed(2) + ' s'; }++ // the clip's time for a film time, as the stage itself seeks a <video> (data-offset, the middle of the frame)+ function clipTime(t) {+ var local = OFF + Math.max(0, t), dur = master.duration;+ if (isFinite(dur) && dur > 0) local = Math.min(local, Math.max(0, dur - 0.5 / SFPS));+ return local + 0.5 / SFPS;+ }+ function frameOfClip(ct) { return Math.max(0, Math.min(LAST, Math.floor((ct - OFF) * FPS + 0.01))); }++ function drawMirrors() {+ if (!master.videoWidth) return;+ mirrors.forEach(function (cv) {+ if (cv.width !== master.videoWidth) { cv.width = master.videoWidth; cv.height = master.videoHeight; }+ try { cv.getContext('2d').drawImage(master, 0, 0, cv.width, cv.height); } catch (e) { /* not decodable yet */ }+ });+ }++ // draw film frame f (the stage seeks one at a time; a newer frame asked meanwhile is drawn next)+ function stageTo(f) {+ if (S.busy) { S.want = f; return S.busyP; }+ S.busy = true; S.f = f;+ S.busyP = ST.seek(f / FPS).catch(function (e) { console.error('[xray live] ' + (e && e.message || e)); }).then(function () {+ S.busy = false;+ if (DIAGRAM) collectDiagramHits();+ paint();+ if (S.want != null && S.want !== S.f) { var w = S.want; S.want = null; return stageTo(w); }+ S.want = null;+ });+ return S.busyP;+ }+ function seeked(v, ct) {+ return new Promise(function (res) {+ if (Math.abs(v.currentTime - ct) < 1e-4 && v.readyState >= 2) return res();+ var done = false, fin = function () { if (done) return; done = true; v.removeEventListener('seeked', ok); res(); };+ var ok = function () {+ if (v.requestVideoFrameCallback) { v.requestVideoFrameCallback(fin); W.setTimeout(fin, 250); } else fin();+ };+ v.addEventListener('seeked', ok);+ W.setTimeout(fin, 3000);+ v.currentTime = ct;+ });+ }+ // paused: the exact frame, the clip seeked to it first, then the inside drawn on it+ function go(f) {+ f = Math.max(0, Math.min(LAST, Math.round(f)));+ S.goWant = f;+ if (S.going) return S.going;+ S.going = (async function () {+ while (S.goWant != null) {+ var g = S.goWant; S.goWant = null;+ var ct = clipTime(g / FPS);+ await Promise.all([master].concat(followers).map(function (v) { if (!v.paused) v.pause(); return seeked(v, ct); }));+ drawMirrors();+ await stageTo(g);+ }+ S.going = null;+ })();+ return S.going;+ }++ // playing: the clip's presented frame is the clock (its sound plays with it)+ function onVideoFrame(now, md) {+ if (!S.playing) return;+ tickPlay(md && typeof md.mediaTime === 'number' ? md.mediaTime : master.currentTime);+ master.requestVideoFrameCallback(onVideoFrame);+ }+ function rafLoop() { if (!S.playing) return; tickPlay(master.currentTime); R.raf(rafLoop); }+ function tickPlay(ct) {+ var f = frameOfClip(ct);+ drawMirrors();+ followers.forEach(function (v) { if (Math.abs(v.currentTime - master.currentTime) > 0.08) v.currentTime = master.currentTime; });+ if (S.pass && f >= S.pass.to) {+ // a slow pass ends on the event's own frame; a pad's pass rests where it stopped (no jump back)+ var ps = S.pass, rest = ps.kind === 'slow' ? ps.ev.frame : ps.to;+ endPass(); S.ending = ps.kind;+ go(rest).then(function () { S.ending = false; paint(); });+ return;+ }+ if (f >= LAST && master.currentTime >= clipTime(LAST / FPS) - 0.5 / SFPS) { pause(); return; }+ stageTo(f);+ }+ function play() {+ if (!S.ready) return;+ if (S.f >= LAST) { S.f = 0; master.currentTime = clipTime(0); }+ S.playing = true;+ master.muted = false;+ master.playbackRate = S.rate;+ followers.forEach(function (v) { v.muted = true; v.playbackRate = S.rate; var q = v.play(); if (q && q.catch) q.catch(function () {}); });+ var p = master.play();+ if (p && p.catch) p.catch(function (e) {+ // no sound allowed yet (a host without a user gesture): play muted rather than not at all+ master.muted = true; master.play().catch(function () { pause(); });+ console.warn('[xray live] ' + (e && e.message || e));+ });+ if (master.requestVideoFrameCallback) master.requestVideoFrameCallback(onVideoFrame); else R.raf(rafLoop);+ paint();+ }+ function pause() {+ var was = S.playing;+ S.playing = false;+ master.pause(); followers.forEach(function (v) { v.pause(); });+ if (S.pass) endPass();+ paint();+ if (was) return go(frameOfClip(master.currentTime));+ return Promise.resolve();+ }+ function endPass() { S.pass = null; S.rate = 1; master.playbackRate = 1; followers.forEach(function (v) { v.playbackRate = 1; }); if (S.playing) { S.playing = false; master.pause(); followers.forEach(function (v) { v.pause(); }); } }+ function toggle() { if (S.playing) pause(); else play(); }+ function isSlow() { return !!S.pass && S.pass.kind === 'slow'; }+ // play [from, to] of the clip at rate, then stop (tickPlay ends it); a newer pass replaces one under way+ var passGen = 0;+ function runPass(kind, e, from, to, rate) {+ // typed in the clip's own order: the next hit is just ahead of the one playing, so the clip plays on to it (no+ // seek, no cut back): typing the clip's words plays the clip straight through+ if (kind === 'hit' && S.playing && S.pass && S.pass.kind === 'hit' && S.rate === rate) {+ var cur = frameOfClip(master.currentTime);+ if (cur < e.frame && from - cur <= 12) { ++passGen; S.pass.ev = e; S.pass.to = Math.max(S.pass.to, to); return Promise.resolve(); }+ }+ var gen = ++passGen;+ return pause().then(function () { return go(from); }).then(function () {+ if (gen !== passGen) return; // a newer press already took over+ S.pass = { kind: kind, ev: e, to: to }; S.rate = rate;+ master.preservesPitch = true;+ play();+ });+ }++ // ------------------------------------------------------------------ events+ function eventAt(f) { for (var i = kept.length - 1; i >= 0; i--) if (kept[i].frame <= f) return kept[i]; return null; }+ function nearEvent(f) { // the event acting now (its motion spans 3 frames before, 7 after)+ var best = null, bd = 1e9;+ kept.forEach(function (e) { var d = f - e.frame; if (d >= -3 && d <= 7 && Math.abs(d) < bd) { bd = Math.abs(d); best = e; } });+ return best;+ }+ function stepEvent(dir) {+ if (!kept.length) return;+ var f = S.f, e = null;+ if (dir > 0) { for (var i = 0; i < kept.length; i++) if (kept[i].frame > f) { e = kept[i]; break; } }+ else { for (var j = kept.length - 1; j >= 0; j--) if (kept[j].frame < f) { e = kept[j]; break; } }+ if (!e) e = dir > 0 ? kept[kept.length - 1] : kept[0];+ jumpTo(e);+ }+ function jumpTo(e) { var p = S.playing ? pause() : Promise.resolve(); return p.then(function () { return go(e.frame); }); }+ function slow() {+ if (!S.ready || !kept.length) return Promise.resolve();+ var f = S.f, e = nearEvent(f);+ if (!e) { for (var i = 0; i < kept.length; i++) if (kept[i].frame >= f) { e = kept[i]; break; } }+ if (!e) e = kept[kept.length - 1];+ var from = Math.max(0, e.frame - 8), to = Math.min(LAST, e.frame + 15); // its rise (3 frames) and settle (7), with room+ return runPass('slow', e, from, to, 0.25);+ }+ // a pad or a click on the picture: the event heard and seen, at 1x, from just before its rise to its settle+ function strikeEvent(e) {+ if (!S.ready || !e) return Promise.resolve();+ touched();+ var to = Math.min(LAST, e.frame + 10);+ S.lastHit = { ev: e, rest: to };+ return runPass('hit', e, Math.max(0, e.frame - 6), to, 1);+ }+ // where "next" counts from: the struck event while its pass plays or rests where it stopped (events can be closer+ // than a pass is long), else the frame on screen+ function refFrame() {+ var lh = S.lastHit;+ if (lh && (S.pass && S.pass.ev === lh.ev || S.f === lh.rest || S.going)) return lh.ev.frame;+ return lh ? S.f : S.f - 1; // before the first strike, the event on the frame shown counts as next+ }+ function nextAfter(list, f) { // the first event after frame f (wraps to the first)+ if (!list.length) return null;+ for (var i = 0; i < list.length; i++) if (list[i].frame > f) return list[i];+ return list[0];+ }++ // ------------------------------------------------------------------ look: theme and layout+ // In place when the page can set itself up again (XRAY.restyle), else the page reloads on the same frame.+ function relook(over) {+ var t = S.f >= 0 ? S.f / FPS : 0;+ var theme = over.theme || root.getAttribute('data-xl-theme');+ var layout = over.layout || curLayout;+ if (!DIAGRAM && X && typeof X.restyle === 'function') {+ X.restyle({ theme: theme, layout: layout });+ root.setAttribute('data-xl-theme', theme); curLayout = layout;+ writeHash(); S.f = -1; stageTo(Math.round(t * FPS)); paint();+ return true;+ }+ try {+ W.location.hash = hashFor({ t: t, theme: theme, layout: layout, embed: R.look.embed }); W.location.reload();+ return true;+ } catch (e) { console.warn('[xray live] this host cannot reload the page: ' + (e && e.message || e)); return false; }+ }+ var curLayout = (R.look.layout && L.tracked) ? R.look.layout : (L.layout === 'split' ? 'split' : 'through');+ // a link to a moment: plain tokens joined by "_" (#hit4, #t2.533_light_split; pre.js reads them back). A frame+ // that is an event's own frame is named by the event (hit N, as the page counts them), any other by its time.+ function hashFor(o) {+ var tk = [];+ if (o.hit) tk.push('hit' + o.hit); else if (o.t != null) tk.push('t' + (+o.t).toFixed(3).replace(/\.?0+$/, ''));+ if (o.theme && o.theme !== L.theme) tk.push(o.theme);+ if (L.tracked && o.layout && o.layout !== (L.layout === 'split' ? 'split' : 'through')) tk.push(o.layout);+ if (o.embed) tk.push('embed');+ return '#' + tk.join('_');+ }+ function hereHash(embed) {+ var f = Math.max(0, S.f), k = -1;+ for (var i = 0; i < kept.length; i++) if (kept[i].frame === f) { k = i; break; }+ return hashFor({ hit: k >= 0 ? k + 1 : 0, t: f / FPS, theme: root.getAttribute('data-xl-theme'), layout: curLayout, embed: embed });+ }+ function writeHash() {+ try { if (W.history && history.replaceState) history.replaceState(null, '', hereHash(R.look.embed)); } catch (e) { /* a sandboxed host */ }+ }++ // ------------------------------------------------------------------ part labels: the hit list+ function collectDiagramHits() {+ var vr = scaler.getBoundingClientRect(), k = scale || 1;+ hits = $$('[data-ld-label]', stage).map(function (g) {+ var t = g.querySelector('text'), plate = g.querySelector('rect');+ var op = parseFloat((plate && plate.getAttribute('opacity')) || '1');+ var vis = g.getAttribute('visibility') !== 'hidden' && getComputedStyle(g).visibility !== 'hidden' && op > 0.5;+ var r = (plate || g).getBoundingClientRect();+ if (!vis || !t || !(r.width > 0)) return null;+ var nm = t.textContent.trim();+ return { name: nm, x: (r.left - vr.left) / k, y: (r.top - vr.top) / k, w: r.width / k, h: r.height / k, g: g };+ }).filter(Boolean);+ $$('[data-ld-label]', stage).forEach(function (g) {+ var t = g.querySelector('text'), nm = t && t.textContent.trim();+ if (nm && partNames.indexOf(nm) < 0) partNames.push(nm); // every part the diagram names, shown or not yet+ var on = !!hot && t && t.textContent.trim().toLowerCase() === hot;+ g.classList.toggle('xl-on', on); g.classList.toggle('xl-off', !!hot && !on);+ });+ }+ function setHot(name) {+ var n = name ? String(name).toLowerCase() : null;+ if (n === hot) return;+ hot = n;+ $$('.xl-part', parts).forEach(function (b) { b.setAttribute('aria-pressed', String(!!hot && b.textContent.toLowerCase() === hot)); });+ if (DIAGRAM) collectDiagramHits();+ else if (!S.playing && S.f >= 0 && !S.going) { var f = S.f; S.f = -1; stageTo(f); }+ hitLayer.classList.toggle('is-on', !!hot);+ }+ function hitAt(cx, cy) {+ var r = scaler.getBoundingClientRect(), k = scale || 1, x = (cx - r.left) / k, y = (cy - r.top) / k, pad = 8;+ for (var i = hits.length - 1; i >= 0; i--) {+ var h = hits[i];+ if (x >= h.x - pad && x <= h.x + h.w + pad && y >= h.y - pad && y <= h.y + h.h + pad) return h.name;+ }+ return null;+ }+ // The picture itself. Drag sideways: scrub (the picture's width is the whole clip). A click or tap that does not+ // drag: on a part label, light that part; elsewhere, pause while playing, else play the next event with its sound.+ // A mouse that only moves lights the label under it. Vertical drags stay the page's (touch-action: pan-y).+ var grab = null;+ view.addEventListener('pointerdown', function (e) {+ if (!S.ready || (e.pointerType === 'mouse' && e.button !== 0)) return;+ grab = { id: e.pointerId, x: e.clientX, y: e.clientY, f0: Math.max(0, S.f), scrub: false };+ });+ view.addEventListener('pointermove', function (e) {+ // a button let go outside the picture: the grab is over (no scrub without a button held)+ if (grab && e.pointerType === 'mouse' && !(e.buttons & 1)) { grab = null; view.classList.remove('is-scrub'); }+ if (grab && grab.id === e.pointerId) {+ var dx = e.clientX - grab.x, dy = e.clientY - grab.y;+ if (!grab.scrub && Math.abs(dx) > 6 && Math.abs(dx) > Math.abs(dy)) {+ grab.scrub = true; touched();+ try { view.setPointerCapture(e.pointerId); } catch (err) { /* already released */ }+ view.classList.add('is-scrub');+ if (S.playing) pause();+ }+ if (grab.scrub) { go(grab.f0 + dx / Math.max(1, view.clientWidth) * LAST); return; }+ }+ if (e.pointerType === 'mouse' && !grab) { var n = hitAt(e.clientX, e.clientY); view.classList.toggle('on-label', !!n); setHot(n); }+ });+ function ungrab(e, act) {+ if (!grab || grab.id !== e.pointerId) return;+ var g = grab; grab = null;+ view.classList.remove('is-scrub');+ if (g.scrub) { writeHash(); return; }+ if (!act) return;+ var n = hitAt(e.clientX, e.clientY);+ if (n) { setHot(e.pointerType === 'mouse' ? n : (n.toLowerCase() !== hot ? n : null)); return; }+ if (e.pointerType !== 'mouse') setHot(null);+ if (S.playing && !S.pass) { pause(); return; }+ strikeEvent(nextAfter(kept, refFrame()));+ }+ view.addEventListener('pointerup', function (e) { ungrab(e, true); });+ view.addEventListener('pointercancel', function (e) { ungrab(e, false); });+ view.addEventListener('pointerleave', function (e) { if (e.pointerType === 'mouse' && !grab) { setHot(null); view.classList.remove('on-label'); } });+ // ------------------------------------------------------------------ pads: play it from the real clip+ // One pad per kind of event, read from the events themselves: a typewriter's keys (laid out as a keyboard), an+ // instrument's notes (in pitch order), a kit's drums, else one pad. A mechanism may say how: XRAY.mechanism({...,+ // touch: {verb: 'Type on it', by: 'ch' | 'note' | 'kind' | 'one'}}). A pad, or its key, plays the next event of+ // its kind after the one on screen, with the clip's own sound; while the clip plays, the acting event's pad lights.+ var pads = [], padBy = 'one', padVerb = '';+ var KEYROWS = ['1234567890', 'qwertyuiop', 'asdfghjkl', 'zxcvbnm'];+ var PADKEYS = 'asdfghjkl;qwertyuiopzxcvbnm';+ function chKey(c) {+ if (c == null) return null;+ if (c === ' ') return 'space';+ if (c === '\n' || c === '\r') return 'return';+ return String(c).toLowerCase();+ }+ function noteOf(e) { // the event's top note (the melody line): name and pitch+ if (!e.notes || !e.notes.length) return null;+ var best = 0;+ for (var i = 1; i < e.notes.length; i++) if ((e.pitches[i] || -1) > (e.pitches[best] || -1)) best = i;+ return { name: e.notes[best], pitch: e.pitches[best] };+ }+ function pitchOfName(n) { // "C♯4" / "Db3" -> MIDI; null for a drum's name+ var m = /^([A-Ga-g])([#♯b♭]?)(-?\d+)$/.exec(String(n));+ if (!m) return null;+ var base = { c: 0, d: 2, e: 4, f: 5, g: 7, a: 9, b: 11 }[m[1].toLowerCase()];+ return 12 * (+m[3] + 1) + base + (m[2] === '#' || m[2] === '♯' ? 1 : m[2] === 'b' || m[2] === '♭' ? -1 : 0);+ }+ function prettyName(n) { // a drum's name as people say it: hihat_open -> open hi-hat+ var t = String(n).replace(/hi-?hat/i, 'hi-hat').replace(/_/g, ' ');+ var m = /^(hi-hat|ride|crash|tom|cymbal) (open|closed|bell|high|low|floor)$/i.exec(t);+ if (m) t = m[2].toLowerCase() === 'closed' ? m[1] : m[2] + ' ' + m[1];+ return t;+ }+ function buildPads() {+ padsEl.textContent = ''; pads = [];+ var T = (X && X.mech && X.mech.touch) || {};+ var withCh = kept.filter(function (e) { return (e.ch != null && e.ch !== '') || e.kind === 'return'; }).length;+ var withNote = kept.filter(function (e) { return e.notes && e.notes.length; }).length;+ var kinds = {}; kept.forEach(function (e) { kinds[e.kind || 'event'] = 1; });+ padBy = T.by || (withCh >= kept.length / 2 && withCh ? 'ch' : withNote >= kept.length / 3 && withNote ? 'note' : Object.keys(kinds).length > 1 ? 'kind' : 'one');+ padVerb = T.verb || { ch: 'Type on it', note: 'Play it', kind: 'Hit it', one: 'Try it' }[padBy] || 'Try it';+ var groups = {}, order = [];+ kept.forEach(function (e) {+ var k, label, pitch = null;+ if (padBy === 'ch') { k = e.kind === 'return' ? 'return' : chKey(e.ch); label = k; }+ else if (padBy === 'note') { var n = noteOf(e); if (!n) return; k = n.name; pitch = n.pitch != null ? n.pitch : pitchOfName(n.name); label = pitch == null ? prettyName(n.name) : n.name; }+ else if (padBy === 'kind') { k = e.kind || 'event'; label = k === 'event' ? noun(1) : k; }+ else { k = 'one'; label = padVerb; }+ if (k == null) return;+ if (!groups[k]) { groups[k] = { key: k, label: label, pitch: pitch, list: [] }; order.push(k); }+ groups[k].list.push(e);+ });+ var list = order.map(function (k) { return groups[k]; });+ // notes without a pitch are a kit's pieces: hit them+ if (padBy === 'note' && !T.verb && list.length && list.every(function (g) { return g.pitch == null; })) padVerb = 'Hit it';+ if (padBy === 'note') {+ if (list.length > 24) list = list.slice().sort(function (a, b) { return b.list.length - a.list.length; }).slice(0, 24);+ if (list.every(function (g) { return g.pitch != null; })) list.sort(function (a, b) { return a.pitch - b.pitch; });+ }+ if (!list.length) { padsEl.hidden = true; return; }+ padsEl.hidden = false;+ padsEl.setAttribute('aria-label', padVerb);+ padsEl.setAttribute('data-by', padBy);+ var head = mk('p', { 'class': 'xl-pads-head' }, padsEl);+ mk('span', { 'class': 'xl-k' }, head, padVerb);+ mk('span', { 'class': 'xl-pads-help' }, head, padBy === 'one' ? 'or click the picture' : 'click a pad or press its key: the real clip plays it');+ function pad(g, parent, keyName) {+ var b = mk('button', { type: 'button', 'class': 'xl-pad' + (g ? '' : ' is-off'), 'data-key': keyName || '' }, parent);+ if (!g) { b.disabled = true; b.textContent = keyName; b.setAttribute('aria-hidden', 'true'); return b; }+ mk('span', { 'class': 'xl-pad-l' }, b, g.label);+ if (g.list.length > 1) mk('span', { 'class': 'xl-pad-n' }, b, '×' + g.list.length);+ if (keyName && padBy !== 'ch' && padBy !== 'one') mk('kbd', {}, b, keyName === ';' ? ';' : keyName.toUpperCase());+ b.setAttribute('aria-label', padVerb + ': the next ' + (padBy === 'one' ? noun(1) : '“' + g.label + '”') + (keyName ? ' (key ' + keyName + ')' : '') ++ ', ' + g.list.length + ' in the clip');+ b.addEventListener('click', function () { strike(g); });+ g.el = b; g.keyName = keyName || null;+ pads.push(g);+ return b;+ }+ if (padBy === 'ch') { // a keyboard: the keys the clip typed, the rest dimmed+ var used = {}; list.forEach(function (g) { used[g.key] = g; });+ KEYROWS.forEach(function (rowKeys, ri) {+ var any = rowKeys.split('').some(function (c) { return used[c]; });+ if (ri === 0 && !any) return; // the digit row only when a digit was typed+ var r = mk('div', { 'class': 'xl-keyrow xl-keyrow-' + ri }, padsEl);+ rowKeys.split('').forEach(function (c) { pad(used[c] || null, r, c); delete used[c]; });+ });+ var rest = Object.keys(used);+ if (rest.length) {+ var r2 = mk('div', { 'class': 'xl-keyrow xl-keyrow-x' }, padsEl);+ rest.forEach(function (k) { var b = pad(used[k], r2, k === 'space' || k === 'return' ? k : k); if (k === 'space') b.classList.add('is-wide'); else if (k.length > 1) b.classList.add('is-word'); });+ }+ } else {+ var strip = mk('div', { 'class': 'xl-padrow' }, padsEl);+ list.forEach(function (g, i) { pad(g, strip, padBy === 'one' ? null : PADKEYS.charAt(i) || null); });+ }+ }+ function strike(g) {+ if (!g || !S.ready) return Promise.resolve();+ var e = nextAfter(g.list, refFrame());+ pads.forEach(function (p) { if (p.el && p !== g) p.el.classList.remove('is-hit'); });+ if (g.el) {+ g.el.classList.remove('is-hit'); void g.el.offsetWidth; g.el.classList.add('is-hit');+ W.clearTimeout(g.hitT); g.hitT = W.setTimeout(function () { g.el.classList.remove('is-hit'); }, 450);+ }+ return strikeEvent(e);+ }+ function padForKey(e) {+ if (!pads.length || e.repeat) return null;+ var k = e.key;+ if (padBy === 'ch') {+ var want = k === 'Enter' ? 'return' : k.length === 1 ? k.toLowerCase() : null;+ if (!want || want === ' ') return null; // space stays play / pause+ for (var i = 0; i < pads.length; i++) if (pads[i].key === want) return pads[i];+ return null;+ }+ if (k.length !== 1) return null;+ for (var j = 0; j < pads.length; j++) if (pads[j].keyName && pads[j].keyName === k.toLowerCase()) return pads[j];+ return null;+ }++ function buildParts() {+ parts.textContent = '';+ if (!partNames.length) { parts.hidden = true; return; }+ parts.hidden = false;+ mk('span', { 'class': 'xl-k' }, parts, 'Parts');+ partNames.forEach(function (nm) {+ var b = mk('button', { type: 'button', 'class': 'xl-part', 'aria-pressed': 'false' }, parts, nm);+ b.addEventListener('mouseenter', function () { setHot(nm); });+ b.addEventListener('mouseleave', function () { setHot(null); });+ b.addEventListener('focus', function () { setHot(nm); });+ b.addEventListener('blur', function () { setHot(null); });+ b.addEventListener('click', function () { setHot(hot === nm.toLowerCase() ? null : nm); });+ });+ }++ // ------------------------------------------------------------------ timeline+ function buildTimeline() {+ ticks.textContent = '';+ var n = (events || []).length, span = Math.max(1, LAST);+ (events || []).forEach(function (e) {+ if (e.frame > LAST) return;+ var skip = e.kind === 'skip', h = Math.round(30 + 70 * Math.max(0, Math.min(1, e.strength == null ? 0.5 : e.strength)));+ var what = skip ? 'skipped sound' : (e.kind && e.kind !== 'event' ? e.kind : noun(1));+ var b = mk('button', { type: 'button', 'class': 'xl-tick' + (skip ? ' is-skip' : ''), tabindex: '-1',+ 'aria-label': what + ' ' + (e.i + 1) + ' at ' + fmt(e.frame / FPS) + (e.ch && e.ch !== ' ' ? ' (' + e.ch + ')' : ''),+ title: what + ' · frame ' + e.frame + ' · ' + fmt(e.frame / FPS) });+ b.style.left = (100 * e.frame / span).toFixed(3) + '%';+ b.style.setProperty('--h', h + '%');+ b.addEventListener('click', function (ev) { ev.stopPropagation(); jumpTo(e); });+ ticks.appendChild(b);+ });+ track.setAttribute('aria-valuemax', (LAST / FPS).toFixed(2));+ var nk = kept.length, ns = n - nk, kinds = {}, order = [];+ kept.forEach(function (e) { var k = e.kind || 'event'; if (!kinds[k]) { kinds[k] = 0; order.push(k); } kinds[k]++; });+ legend.innerHTML = '';+ mk('span', { 'class': 'xl-key-hot' }, legend, order.map(function (k) {+ return kinds[k] + ' ' + (k === 'event' ? noun(kinds[k]) : k + (kinds[k] === 1 ? '' : 's'));+ }).join(', ') || '0 ' + noun(0));+ if (ns) mk('span', { 'class': 'xl-key-skip' }, legend, ns + ' skipped (not the object’s)');+ mk('span', { 'class': 'xl-key-help' }, legend, '← → event to event · space plays · drag the picture to scrub · Shift+S 0.25×');+ }+ var dragging = false;+ function trackFrame(cx) { var r = track.getBoundingClientRect(); return Math.round(Math.max(0, Math.min(1, (cx - r.left) / Math.max(1, r.width))) * LAST); }+ track.addEventListener('pointerdown', function (e) {+ if (!S.ready || e.target.classList.contains('xl-tick')) return;+ dragging = true; track.setPointerCapture(e.pointerId);+ (S.playing ? pause() : Promise.resolve()).then(function () { go(trackFrame(e.clientX)); });+ });+ track.addEventListener('pointermove', function (e) { if (dragging) go(trackFrame(e.clientX)); });+ track.addEventListener('pointerup', function () { dragging = false; writeHash(); });+ track.addEventListener('pointercancel', function () { dragging = false; });++ // ------------------------------------------------------------------ paint the controls+ function paint() {+ var f = Math.max(0, S.f);+ playB.innerHTML = S.playing ? ICON_PAUSE : ICON_PLAY;+ playB.setAttribute('aria-label', S.playing ? 'Pause (space)' : 'Play (space)');+ playB.title = playB.getAttribute('aria-label');+ slowB.setAttribute('aria-pressed', String(isSlow()));+ themeB.textContent = root.getAttribute('data-xl-theme') === 'dark' ? 'Light' : 'Dark';+ themeB.setAttribute('aria-label', 'Switch to the ' + themeB.textContent.toLowerCase() + ' theme (Shift+T)');+ layoutB.textContent = curLayout === 'split' ? 'See-through' : 'Split';+ layoutB.setAttribute('aria-label', curLayout === 'split' ? 'Draw the inside on the object (Shift+L)' : 'Draw the inside beside the clip (Shift+L)');+ headEl.style.left = (100 * f / Math.max(1, LAST)).toFixed(3) + '%';+ track.setAttribute('aria-valuenow', (f / FPS).toFixed(2));+ track.setAttribute('aria-valuetext', fmt(f / FPS) + ', frame ' + f);+ timeEl.textContent = fmt(f / FPS) + ' · frame ' + f;+ var e = nearEvent(f), done = kept.filter(function (x) { return x.frame <= f; }).length;+ var words;+ if (e) {+ var k = kept.indexOf(e) + 1;+ words = (e.kind && e.kind !== 'event' ? e.kind.charAt(0).toUpperCase() + e.kind.slice(1) : noun(1).charAt(0).toUpperCase() + noun(1).slice(1)) ++ ' ' + k + ' of ' + kept.length + (e.ch && e.ch !== ' ' ? ' · “' + e.ch + '”' : e.ch === ' ' ? ' · space' : '') ++ (e.notes && e.notes.length ? ' · ' + e.notes.slice(0, 4).join(' ') : '') ++ (f === e.frame ? ' · on its frame (' + e.frame + ')' : ' · ' + (f < e.frame ? (e.frame - f) + ' frames before' : (f - e.frame) + ' frames after') + ' its frame');+ } else words = done + ' of ' + kept.length + ' ' + noun(kept.length) + ' so far';+ if (isSlow()) words += ' · 0.25×';+ nowEl.textContent = words;+ $$('.xl-tick', ticks).forEach(function (b, i) { var ev = events[i]; if (ev) b.classList.toggle('is-past', ev.frame <= f); });+ // the acting event's pad lights: the keyboard types itself while the clip plays+ var act = e && f >= e.frame - 1 && f <= e.frame + 5 ? e : null;+ pads.forEach(function (g) { g.el.classList.toggle('is-on', !!act && g.list.indexOf(act) >= 0); });+ if (!S.playing && S.ready && f !== hashFrame) { hashFrame = f; writeHash(); } // the address names the moment on screen+ }+ var hashFrame = -1;++ // ------------------------------------------------------------------ fit the film to the window+ var scale = 1;+ function fit() {+ var cw = Math.min(D.documentElement.clientWidth, 1600) - 32;+ if (EMBED) { // framed: the frame's width sets the size, the page sets the height+ cw = D.documentElement.clientWidth - (cw + 32 < 700 ? 24 : 32);+ scale = Math.max(0.05, cw / FW);+ scaler.style.transform = 'scale(' + scale + ')';+ view.style.width = Math.round(FW * scale) + 'px';+ view.style.height = Math.round(FH * scale) + 'px';+ app.style.setProperty('--xl-w', Math.round(FW * scale) + 'px');+ if (DIAGRAM && S.ready) collectDiagramHits();+ postHeight();+ return;+ }+ var ch = D.documentElement.clientHeight;+ var phone = cw < 700;+ // the controls under the picture count, the pads, parts and share fields below them scroll (never shrink the film)+ var under = bar.offsetHeight - padsEl.offsetHeight - parts.offsetHeight - shareEl.offsetHeight;+ var room = phone ? ch * 0.62 : ch - under - head.offsetHeight - 64;+ scale = Math.max(0.05, Math.min(cw / FW, Math.max(160, room) / FH));+ if (phone) scale = Math.min(cw / FW, (ch * 0.62) / FH);+ scaler.style.transform = 'scale(' + scale + ')';+ view.style.width = Math.round(FW * scale) + 'px';+ view.style.height = Math.round(FH * scale) + 'px';+ app.style.setProperty('--xl-w', Math.round(FW * scale) + 'px');+ if (DIAGRAM && S.ready) collectDiagramHits();+ }+ W.addEventListener('resize', fit);+ var lastH = 0, lastGrow = -1e9;+ function postHeight() {+ if (!EMBED) return;+ var h = Math.ceil(app.getBoundingClientRect().height), now = R.now();+ if (Math.abs(h - lastH) < 2) return;+ // a host's scrollbar coming and going changes the width and so the height: a small shrink right after a grow is+ // that flip, not a change of the page; ignore it+ if (h < lastH && lastH - h < 24 && now - lastGrow < 1200) return;+ if (h > lastH) lastGrow = now;+ lastH = h;+ try { W.parent.postMessage({ type: 'showtime-xray:size', height: h }, '*'); } catch (e) { /* no host to tell */ }+ }+ if (EMBED && W.ResizeObserver) new ResizeObserver(function () { postHeight(); }).observe(app);++ // ------------------------------------------------------------------ hints, links, embed+ var hinted = false;+ function say(text, ms) {+ toast.textContent = text; toast.classList.add('is-on');+ W.clearTimeout(say.t); say.t = W.setTimeout(function () { toast.classList.remove('is-on'); }, ms || 2600);+ }+ function touched() { if (!hinted) { hinted = true; toast.classList.remove('is-on'); } }+ function pageUrl() { // this page without its #moment+ var u = String(W.location.href || '').split('#')[0];+ return /^https?:/.test(u) ? u : null;+ }+ function fileName() { var m = /([^/?#]+\.html?)(?:[?#]|$)/i.exec(String(W.location.href || '')); return m ? m[1] : 'xray-live.html'; }+ // copy on the click itself (clipboard needs the gesture); a host that refuses: the text is selected for the viewer+ function copyText(text, field) {+ var done = function () { say('Copied'); }, fail = function () { if (field) { field.focus(); field.select(); } say('Press Ctrl+C or ⌘C to copy'); };+ try {+ if (W.navigator.clipboard && W.navigator.clipboard.writeText) W.navigator.clipboard.writeText(text).then(done, fail);+ else fail();+ } catch (e) { fail(); }+ }+ function field(parent, label, text, rows) {+ var w = mk('div', { 'class': 'xl-field' }, parent);+ var top = mk('div', { 'class': 'xl-field-top' }, w);+ mk('span', { 'class': 'xl-k' }, top, label);+ var b = mk('button', { type: 'button', 'class': 'xl-btn xl-copy' }, top, 'Copy');+ var ta = mk('textarea', { readonly: '', rows: String(rows || 1), spellcheck: 'false', 'aria-label': label }, w);+ ta.value = text;+ b.addEventListener('click', function () { copyText(ta.value, ta); });+ return ta;+ }+ function openShare(what) {+ var open = !shareEl.hidden && shareEl.getAttribute('data-what') === what;+ shareEl.textContent = ''; shareEl.hidden = open; shareEl.setAttribute('data-what', what);+ embedB.setAttribute('aria-expanded', String(!open && what === 'embed'));+ if (open) { postHeight(); return; }+ var base = pageUrl(), src = base || ('https://your.site/' + fileName());+ if (what === 'link') {+ field(shareEl, base ? 'A link to this moment' : 'A link to this moment (once the page is on the web)', src + hereHash(false), 1);+ } else {+ var t = (L.title || 'X-ray').replace(/"/g, '"');+ // the snippets are text for another page (written so this offline page never names a file to fetch)+ var at = function (name, v) { return ' ' + name + '="' + v + '"'; };+ field(shareEl, 'Embed: one tag (loads when the viewer clicks)', '<script' + at('src', L.embedJs || 'https://faviovazquez.github.io/showtime/xray/embed.js') + '></' + 'script>\n' ++ '<showtime-xray' + at('src', src) + at('ratio', FW + '/' + FH) + at('title', t) + '></showtime-xray>', 2);+ var ew = FW >= FH ? 960 : 420, eh = Math.round(ew * FH / FW + 320); // the film at that width, and its controls+ field(shareEl, 'Or an iframe', '<iframe' + at('src', src + '#embed') + at('title', t) + at('style', 'width:100%;max-width:' + ew + 'px;height:' + eh + 'px;border:0') ++ ' loading="lazy" allow="autoplay; fullscreen; clipboard-write"></iframe>', 2);+ mk('p', { 'class': 'xl-note' }, shareEl, (base ? '' : 'Put this file on your site first; the snippets point at where it is served. ') ++ 'embed.js is one small file with no dependencies: link the showtime site’s copy, or host your own (showtime’s runtime/xray-live/embed.js).');+ }+ postHeight();+ }+ linkB.addEventListener('click', function () {+ var base = pageUrl();+ if (base) copyText(base + hereHash(false), null);+ openShare('link');+ });+ embedB.addEventListener('click', function () { openShare('embed'); });++ // ------------------------------------------------------------------ keys and buttons+ playB.addEventListener('click', toggle);+ prevB.addEventListener('click', function () { stepEvent(-1); });+ nextB.addEventListener('click', function () { stepEvent(1); });+ slowB.addEventListener('click', function () { if (isSlow()) pause(); else slow(); });+ themeB.addEventListener('click', function () { relook({ theme: root.getAttribute('data-xl-theme') === 'dark' ? 'light' : 'dark' }); });+ layoutB.addEventListener('click', function () { relook({ layout: curLayout === 'split' ? 'through' : 'split' }); });+ D.addEventListener('keydown', function (e) {+ if (e.metaKey || e.ctrlKey || e.altKey || !S.ready) return;+ var tg = e.target && e.target.tagName;+ if (tg === 'INPUT' || tg === 'TEXTAREA' || tg === 'SELECT') return;+ var k = e.key, used = true;+ // letters are the pads' (a typewriter's keys, an instrument's notes); the page's own shortcuts take Shift+ // a focused control keeps its own Space and Enter (a button, a link, a summary, the time slider)+ var ownKeys = tg === 'BUTTON' || tg === 'A' || tg === 'SUMMARY' || (e.target && e.target.getAttribute && e.target.getAttribute('role') === 'slider');+ if ((k === 'Enter' || k === ' ') && ownKeys && !(k === ' ' && e.target === track)) return;+ var pd = !e.shiftKey && padForKey(e);+ if (pd) { e.preventDefault(); strike(pd); return; }+ if (k === ' ') toggle();+ else if (k === 'ArrowRight') { if (e.shiftKey) (S.playing ? pause() : Promise.resolve()).then(function () { go(S.f + 1); }); else stepEvent(1); }+ else if (k === 'ArrowLeft') { if (e.shiftKey) (S.playing ? pause() : Promise.resolve()).then(function () { go(S.f - 1); }); else stepEvent(-1); }+ else if (k === '.' || k === ',') { var d = k === '.' ? 1 : -1; (S.playing ? pause() : Promise.resolve()).then(function () { go(S.f + d); }); }+ else if (k === 'Home') { pause().then(function () { go(0); }); }+ else if (k === 'End') { pause().then(function () { go(LAST); }); }+ else if (e.shiftKey && (k === 'S' || k === 's')) { if (isSlow()) pause(); else slow(); }+ else if (e.shiftKey && (k === 'T' || k === 't')) themeB.click();+ else if (e.shiftKey && (k === 'L' || k === 'l') && L.tracked) layoutB.click();+ else if (k === 'Escape') setHot(null);+ else used = false;+ if (used) e.preventDefault();+ });+ master.addEventListener('ended', function () { if (S.playing) pause(); });++ // ------------------------------------------------------------------ meta: receipt and credit+ function fillMeta() {+ meta.textContent = '';+ var n = kept.length;+ var rc = mk('p', { 'class': 'xl-receipt' }, meta);+ rc.textContent = L.receipt || (n + ' ' + noun(n) + ' measured from the clip’s sound (this page carries no verify receipt yet).');+ var drawn = L.drawn || (X && X.mech && X.mech.drawn) || 'The inside is drawn: a generic mechanism of this kind of object, not this exact one.';+ mk('p', { 'class': 'xl-drawn' }, meta, 'Measured: each ' + noun(1) + '’s frame, from the clip’s own sound. ' + drawn);+ if (L.credit) mk('p', { 'class': 'xl-credit' }, meta, L.credit);+ // the line that made it: what to ask an agent, and the commands it runs (no local paths: placeholders)+ var made = L.made || {};+ if (made.prompt || (made.commands && made.commands.length)) {+ var box = mk('div', { 'class': 'xl-made' }, meta);+ mk('p', { 'class': 'xl-k' }, box, 'Make your own');+ if (made.prompt) field(box, 'Make your own with one line', made.prompt, 1);+ if (made.commands && made.commands.length) {+ var dt = mk('details', { 'class': 'xl-cmds' }, box);+ mk('summary', {}, dt, 'The commands it runs');+ field(dt, 'Commands', made.commands.join('\n'), made.commands.length);+ dt.addEventListener('toggle', postHeight);+ }+ }+ }++ // ------------------------------------------------------------------ start+ function metadata(v) {+ return new Promise(function (res) {+ if (v.readyState >= 1) return res();+ v.addEventListener('loadedmetadata', function () { res(); }, { once: true });+ v.addEventListener('error', function () { res(); }, { once: true });+ W.setTimeout(res, 15000);+ });+ }+ var readyResolve;+ var readyP = new Promise(function (r) { readyResolve = r; });+ fit();+ Promise.all([ST.ready(), metadata(master)]).then(function () {+ OFF = parseFloat(master.getAttribute('data-offset')) || 0;+ SFPS = parseFloat(master.getAttribute('data-fps')) || FPS;+ if (isFinite(master.duration) && master.duration > 0) CLIPDUR = master.duration;+ var filmLast = Math.max(0, Math.round((ST.info().duration || L.duration) * FPS) - 1);+ LAST = CLIPDUR > 0 ? Math.min(filmLast, Math.max(0, Math.floor((CLIPDUR - OFF) * FPS) - 1)) : filmLast;+ events = events || [];+ kept = events.filter(function (e) { return e.kind !== 'skip' && e.frame <= LAST; });+ buildTimeline(); fillMeta();+ var start;+ if (R.look.hit != null && kept[R.look.hit - 1]) start = kept[R.look.hit - 1].frame;+ else if (R.look.event != null && events[R.look.event]) start = events[R.look.event].frame;+ else if (R.look.t != null) start = Math.round(R.look.t * FPS);+ else {+ // the first event after the part names arrive (the inside is drawn and labelled), else the first one+ var labelsAt = ((W.CUE && W.CUE.labels) || 1.5) + 0.6;+ var e0 = kept.filter(function (e) { return e.frame / FPS >= labelsAt; })[0] || kept[0];+ start = e0 ? e0.frame : Math.round((L.poster || 0) * FPS);+ }+ fit();+ return go(start).then(function () {+ S.ready = true; veil.hidden = true; // controls answer once the first frame is drawn+ buildParts(); buildPads(); fit(); paint(); readyResolve(state());+ if (kept.length && (!EMBED || !R.look.hit)) say(padBy === 'one' ? 'Click the picture to hear the next ' + noun(1) + ' · drag it to scrub'+ : padVerb + ': ' + (padBy === 'ch' ? 'press a key' : 'press a pad') + ' · drag the picture to scrub', 4200);+ });+ }).catch(function (e) {+ veil.hidden = false;+ veil.textContent = 'This page could not start: ' + (e && e.message || e);+ console.error('[xray live] ' + (e && e.message || e));+ });++ function state() {+ return { ready: S.ready, frame: S.f, t: S.f / FPS, playing: S.playing, rate: S.rate, slow: isSlow() || S.ending === 'slow', strike: (!!S.pass && S.pass.kind === 'hit') || S.ending === 'hit', last: LAST, fps: FPS,+ theme: root.getAttribute('data-xl-theme'), layout: DIAGRAM ? 'diagram' : curLayout, tracked: !!L.tracked, hot: hot,+ events: (events || []).map(function (e) { return { i: e.i, frame: e.frame, kind: e.kind, ch: e.ch }; }), kept: kept.map(function (e) { return e.i; }),+ kit: kitState, labels: hits.map(function (h) { return { name: h.name, x: h.x, y: h.y, w: h.w, h: h.h }; }), parts: partNames.slice(),+ clipTime: master.currentTime, restyle: !!(X && typeof X.restyle === 'function'), embed: EMBED,+ pads: { by: padBy, verb: padVerb, keys: pads.map(function (g) { return { key: g.key, label: g.label, n: g.list.length, keyName: g.keyName }; }) } };+ }+ W.xrayLive = {+ get ready() { return readyP; },+ state: state,+ seek: function (t) { return (S.playing ? pause() : Promise.resolve()).then(function () { return go(Math.round(t * FPS)); }).then(state); },+ jump: function (i) { var e = (events || [])[i]; return e ? jumpTo(e).then(state) : Promise.resolve(state()); },+ play: function () { play(); return state(); },+ pause: function () { return pause().then(state); },+ slow: function () { return slow().then(state); },+ strike: function (key) { var g = pads.filter(function (p) { return p.key === key; })[0] || pads[0]; return strike(g).then(state); },+ link: function () { return (pageUrl() || '') + hereHash(false); },+ theme: function (n) { return relook({ theme: n }); },+ layout: function (n) { return relook({ layout: n }); },+ hover: function (n) { setHot(n || null); return (S.going || S.busyP || Promise.resolve()).then(function () { return S.busyP; }).then(state); },+ };+})();diff --git a/skills/showtime/runtime/xray-live/pre.js b/skills/showtime/runtime/xray-live/pre.jsnew file mode 100644index 0000000..effe094--- /dev/null+++ b/skills/showtime/runtime/xray-live/pre.js@@ -0,0 +1,77 @@+/* showtime X-ray live page, part 1 of 2 (runtime/xray-live/pre.js): runs before the stage runtime.+ *+ * `showtime export html <xray project> --interactive` packs the project's own page and puts this script right+ * before /_st/stage.js. It:+ * 1. keeps the real clocks (requestAnimationFrame, performance.now, Date) for the controls: the stage runs in+ * render mode and replaces them with its virtual clock, so the drawing is the film's, frame for frame;+ * 2. hands the stage showtime.json (the look picked in the address, #hit4_light_split, applied first);+ * 3. answers the page's fetch() of its data files (xray.json, showtime.json, notes.json) from the packed copies:+ * the file makes no network request;+ * 4. reports the film's size as the window's (the living-diagram runtime scales its stage by innerWidth / 1920;+ * the controls (live.js) scale the whole film to the real window themselves).+ */+(function () {+ 'use strict';+ var W = window, D = document;+ var el = D.getElementById('xl-cfg');+ if (!el) return;+ var C = JSON.parse(el.textContent), L = C.live, data = C.data;+ var real = {+ raf: W.requestAnimationFrame.bind(W), caf: W.cancelAnimationFrame.bind(W),+ now: W.performance && performance.now ? performance.now.bind(performance) : Date.now, Date: W.Date,+ dpr: W.devicePixelRatio || 1, look: {},+ };+ // A link to a moment, or a look picked on this page. Plain anchor tokens joined by "_" (hosts that frame the page,+ // like an artifact viewer, pass only a plain #anchor on): #hit4 (the 4th event, as the page counts them), #t2.5,+ // light | dark, through | split, embed (framed by <showtime-xray> or an iframe: fit the width, report the height).+ // The older #event=4&t=2.5&theme=light (event = an index into all events, skipped ones too) still opens.+ var look = real.look;+ try {+ var h = String(W.location.hash || '').replace(/^#/, '');+ try { h = decodeURIComponent(h); } catch (e) { /* keep */ }+ if (h.indexOf('=') >= 0) {+ h.split('&').forEach(function (kv) {+ var i = kv.indexOf('='); if (i < 0) { if (kv === 'embed') look.embed = true; return; }+ var k = kv.slice(0, i), v = kv.slice(i + 1);+ if (k === 't' && isFinite(parseFloat(v))) look.t = parseFloat(v);+ else if (k === 'event' && /^\d+$/.test(v)) look.event = +v;+ else if (k === 'hit' && /^\d+$/.test(v) && +v > 0) look.hit = +v;+ else if (k === 'theme' && /^(light|dark)$/.test(v)) look.theme = v;+ else if (k === 'layout' && /^(through|split)$/.test(v)) look.layout = v;+ });+ } else {+ h.split(/[_~]/).forEach(function (tk) {+ var m;+ if ((m = /^hit-?(\d+)$/.exec(tk)) && +m[1] > 0) look.hit = +m[1];+ else if ((m = /^t-?(\d+(?:\.\d+)?)$/.exec(tk))) look.t = parseFloat(m[1]);+ else if (tk === 'light' || tk === 'dark') look.theme = tk;+ else if (tk === 'through' || tk === 'split') look.layout = tk;+ else if (tk === 'embed') look.embed = true;+ });+ }+ } catch (e) { /* a host without an address */ }+ var cfg = JSON.parse(data['showtime.json']);+ cfg.xray = cfg.xray || {};+ if (look.theme) cfg.xray.theme = look.theme;+ if (look.layout && L.tracked) cfg.xray.layout = look.layout;+ data['showtime.json'] = JSON.stringify(cfg);+ real.cfg = cfg;+ W.__XL_REAL__ = real;+ W.__ST_RENDER__ = { config: cfg, override: null, alpha: false, settle: 'none', layers: false, seed: cfg.seed === undefined ? 1 : cfg.seed };+ ['innerWidth', 'innerHeight'].forEach(function (k, i) {+ try { Object.defineProperty(W, k, { configurable: true, get: function () { return i ? L.H : L.W; } }); } catch (e) { /* ignore */ }+ });+ try { Object.defineProperty(W, 'devicePixelRatio', { configurable: true, get: function () { return 1; } }); } catch (e) { /* ignore */ }+ function name(u) {+ var s = String(u && u.url ? u.url : u).split('#')[0].split('?')[0];+ try { s = decodeURIComponent(s); } catch (e) { /* keep */ }+ return s.replace(/^[a-z]+:\/\/[^/]*/i, '').replace(/^(\.?\/)+/, '');+ }+ W.fetch = function (u) {+ var n = name(u);+ if (Object.prototype.hasOwnProperty.call(data, n)) {+ return Promise.resolve(new Response(data[n], { status: 200, headers: { 'Content-Type': 'application/json' } }));+ }+ return Promise.resolve(new Response('', { status: 404, statusText: 'not packed in this page' }));+ };+})();diff --git a/skills/showtime/scripts/export.mjs b/skills/showtime/scripts/export.mjsindex df23b1b..3a201a4 100644--- a/skills/showtime/scripts/export.mjs+++ b/skills/showtime/scripts/export.mjs@@ -18,10 +18,11 @@ import { fitFootage } from './lib/export/fit.mjs';import { timeIssues, socraticDoc } from './lib/questions.mjs';import { resolveShare, shareMeta, genericTitle, genericChapters } from './lib/export/share.mjs';import { packFigure, sceneInfo, TEMPLATE_TITLE } from './lib/export/figure.mjs';+import { isXrayProject, exportXrayLive } from './lib/export/xray.mjs';const SPEC = {name: 'export',- usage: 'showtime export html <project|job> [-o out.html] [--interactive] [--audio auto|embed|score|none] [--folder] [--controls full|minimal|none] [--target file|artifact] [--lang CODE] [--no-questions] [--auto-continue S]',+ usage: 'showtime export html <project|job> [-o out.html] [--interactive] [--prompt TEXT] [--audio auto|embed|score|none] [--folder] [--controls full|minimal|none] [--target file|artifact] [--lang CODE] [--no-questions] [--auto-continue S]',summary: 'Export a project as an interactive HTML video that plays in any browser, offline, from one file.',description: ['The page is packed with everything it uses (runtime, scripts, styles, fonts, images, emoji, data,',@@ -60,6 +61,9 @@ const SPEC = {'A living diagram exports its live figure with --interactive: figure.html (or the diagram template\'s) packed','into one offline file with no player; the board is live from the first frame: the viewer hovers parts, picks','steps and turns knobs (references/workflows/diagram.md, Interactive). Default name: <title>-figure.html.',+ 'An X-ray project exports its live page with --interactive: the clip with its own sound, the inside drawn by the',+ 'same page at the scrubbed frame, every event a tick to jump to (arrow keys), 0.25x around an event, light/dark',+ 'and see-through/split; no sync marker (references/workflows/xray.md, Live page). Default name: <title>-live.html.',].join('\n'),options: {output: { short: 'o', help: 'output .html file (with --folder: output folder)', metavar: 'FILE' },@@ -71,7 +75,8 @@ const SPEC = {bitrate: { help: 'embedded audio bitrate (default 96k)', metavar: 'RATE' },codec: { help: 'embedded audio codec: aac (default, plays everywhere) or opus (smaller)', metavar: 'NAME' },folder: { type: 'boolean', help: 'write a folder (index.html + assets/) instead of one file' },- interactive: { type: 'boolean', help: 'a living diagram: its live figure (figure.html) instead of the film, one offline file without a player' },+ interactive: { type: 'boolean', help: 'a living diagram: its live figure (figure.html) instead of the film; an X-ray: its live page (scrub the clip, jump event to event); one offline file without the film player' },+ prompt: { help: 'an X-ray live page: the request that made the film, shown on the page as "the line that made it" (default: showtime.json xray.made.prompt; none if neither)', metavar: 'TEXT' },controls: { help: 'player chrome: full (default), minimal, or none (for embedding)', metavar: 'KIND' },target: { help: 'file (default) or artifact: for a host that shows the page in a sandboxed frame (an HTML artifact): one file under 16 MB, no "copy link" or other file-address features (the player also detects such hosts by itself)', metavar: 'KIND' },'autoplay-muted': { type: 'boolean', help: 'start playing muted as soon as it loads (browsers allow that without a click)' },@@ -113,6 +118,7 @@ const SPEC = {'showtime export html my-video --controls none --autoplay-muted --loop # for embedding in a page','showtime export html my-video --target artifact -o launch.html # to publish as an HTML artifact','showtime export html my-diagram --interactive # the live figure: hover, steps, knobs; one file',+ 'showtime export html my-xray --interactive --prompt "X-ray this clip of my typewriter" # a live page that shows the line that made it','showtime export html my-film --subtitle "Fix the cuts" --kicker "Studio how-to · 01"','showtime export html my-explainer --auto-continue 6 # questions go on 6 s after an answer','# open at a moment: my-film.html#t=1:05 or my-film.html#chapter=3',@@ -192,6 +198,9 @@ async function main() {if (!['file', 'artifact'].includes(target)) throw new UserError(`--target must be file or artifact (got ${a.target})`);if (target === 'artifact' && folder) throw new UserError('--target artifact writes one file; leave out --folder');if (a.interactive && folder) throw new UserError('--interactive writes one file; leave out --folder', 'showtime export html <project> --interactive -o figure.html');+ // an X-ray project's live page (scripts/lib/export/xray.mjs) is <slug>-live.html; a living diagram's figure <slug>-figure.html+ const xrayLive = !!a.interactive && isXrayProject(proj.dir, cfg);+ const liveSuffix = a.interactive ? (xrayLive ? '-live' : '-figure') : '';let maxMb = a['max-mb'] === undefined ? 16 : Number(a['max-mb']);if (!(maxMb >= 0)) throw new UserError(`--max-mb must be a number >= 0 (got ${a['max-mb']})`);if (target === 'artifact' && (maxMb === 0 || maxMb > 16)) { addWarn('--target artifact: the file must stay under 16 MB (the artifact limit); --max-mb set to 16'); maxMb = 16; }@@ -235,7 +244,7 @@ async function main() {out = fs.existsSync(want) && fs.readdirSync(want).length ? freshPath(want) : want;} else if (isDir) {// -o an existing folder (or one ending in /): the file goes inside it- want = path.join(want, `${proj.slug}${a.interactive ? '-figure' : ''}.html`);+ want = path.join(want, `${proj.slug}${liveSuffix}.html`);out = freshPath(want);} else {if (!path.extname(want)) want += '.html';@@ -245,8 +254,9 @@ async function main() {} else {const dir = a.job ? resolveJobDir(a.job) : (fromJob && !a['out-dir'] ? fromJob.job : jobDir(proj.slug, a['out-dir']));if (!dir || !fs.existsSync(dir)) throw new UserError(`no job named "${a.job}"`, 'list jobs with `showtime job list`');- out = freshPath(path.join(dir, folder ? `${proj.slug}-html` : `${proj.slug}${a.interactive ? '-figure' : ''}.html`));+ out = freshPath(path.join(dir, folder ? `${proj.slug}-html` : `${proj.slug}${liveSuffix}.html`));}+ if (xrayLive) return exportXrayLive({ a, proj, out, title, target, maxMb, warnings, addWarn, T0 });if (a.interactive) return exportFigure({ a, proj, out, title, target, maxMb, warnings, addWarn, T0 });const workDir = fs.mkdtempSync(path.join(os.tmpdir(), 'showtime-export-'));@@ -379,6 +389,8 @@ async function main() {// only what the stage reads from showtime.json travels (no audio paths or other local details)const pubCfg = {};for (const k of ['width', 'height', 'fps', 'duration', 'background', 'title', 'poster', 'seed', 'chapters', 'subtitle', 'kicker']) if (cfg[k] !== undefined) pubCfg[k] = cfg[k];+ // an X-ray page reads its own block (skip, kinds, text, place, credit, receipt): without it the film plays every event+ if (cfg.xray && typeof cfg.xray === 'object') pubCfg.xray = cfg.xray;// the page reads its questions (ST.questions) to draw its pause and think beats, as in the MP4,// whether or not the player stops for themif (Array.isArray(cfg.questions)) pubCfg.questions = cfg.questions;@@ -477,6 +489,7 @@ function missingFiles(failures, addWarn) {for (const f of failures || []) {const p = f.path;if (p === '/showtime.json') continue; // the stage asks for it; a project may have none+ if (p === '/notes.json') continue; // an X-ray page asks for it; only music clips have oneif (f.status === 0) stop.push(`${p} (the server did not answer: ${f.err || 'no response'})`);else if (p.startsWith('/_st/emoji/')) addWarn(`emoji ${p.slice(11)} is not installed (it shows as a broken image): run \`showtime assets emoji <char>\``);else if (/^\/_(st|lib|assets)\//.test(p)) addWarn(`the page asks for ${p}, which does not exist (${f.status})`);diff --git a/skills/showtime/scripts/lib/export/figure.mjs b/skills/showtime/scripts/lib/export/figure.mjsindex 40bf3c4..8dd01e3 100644--- a/skills/showtime/scripts/lib/export/figure.mjs+++ b/skills/showtime/scripts/lib/export/figure.mjs@@ -89,7 +89,15 @@ export function sceneInfo(projectDir) {if (!out.title || !out.description) { // the film page's own title block (a scene that keeps its words in index.html)try {const html = fs.readFileSync(path.join(projectDir, 'index.html'), 'utf8');- const strip = (x) => { let prev; do { prev = x; x = x.replace(/<[^>]*>/g, ''); } while (x !== prev); return x.replace(/[<>]/g, ''); }; // until stable: no tag can survive+ const strip = (x) => { // one linear scan: a tag runs from "<" to the next ">"; a stray "<" or ">" is dropped+ let out = '', i = 0;+ while (i < x.length) {+ const c = x[i];+ if (c === '<') { const j = x.indexOf('>', i + 1); if (j < 0) { out += x.slice(i + 1).replace(/[<>]/g, ''); break; } i = j + 1; }+ else { if (c !== '>') out += c; i++; }+ }+ return out;+ };const text = (re) => { const m = re.exec(html); return m ? strip(m[1]).replace(/ /g, ' ').replace(/\s+/g, ' ').trim() : ''; };out.title = out.title || text(/<h1\b[^>]*class="[^"]*\bld-title\b[^"]*"[^>]*>([\s\S]*?)<\/h1>/i);out.description = out.description || text(/<div\b[^>]*class="[^"]*\bld-dek\b[^"]*"[^>]*>([\s\S]*?)<\/div>/i);diff --git a/skills/showtime/scripts/lib/export/xray.mjs b/skills/showtime/scripts/lib/export/xray.mjsnew file mode 100644index 0000000..b266c7b--- /dev/null+++ b/skills/showtime/scripts/lib/export/xray.mjs@@ -0,0 +1,286 @@+// `showtime export html <xray project> --interactive`: the X-ray's live page, one offline file.+//+// The project's own page (index.html: xray.js + mechanism.js, or the diagram layout's page) is packed as it is and+// driven by the scrub time instead of the renderer: the same stage runtime in render mode (runtime/xray-live/pre.js+// sets it up before stage.js), the same ST.onSeek drawing, at the frame the viewer picks. Around it,+// runtime/xray-live/live.js draws the controls: play with the clip's own sound, a timeline of every event (red kept,+// grey skipped), jump to an event's exact frame, 0.25x around one, light/dark, see-through/split, part labels.+//+// Media: each clip is packed once, as base64, and turned into one blob: URL in the page (a second <video> of the+// same file becomes a canvas mirror of the first, so the radiograph never drifts from the clip). The page's data+// files (xray.json, showtime.json, notes.json) travel inline and answer its fetch() calls. A Content-Security-Policy+// blocks every network request. Over --max-mb because of the clip, the clip is re-encoded for this export only+// (fit.mjs), as the film export does.+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import { skillDir, nodeModulesDir } from '../deps.mjs';+import { UserError, c, fmtBytes } from '../cli.mjs';+import { showCard, openHint } from '../delight.mjs';+import { figureFile, FIGURE_CSP } from './figure.mjs';+import { addCodePoints, pruneFontFaces } from './collect.mjs';+import { minifyJs, minifyCss } from './minify.mjs';+import { notices, stageForExport, showtimeVersion, safeJson } from './build.mjs';+import { fitFootage } from './fit.mjs';++const MB = 1000 * 1000;+const FONT_MIME = { '.woff2': 'font/woff2', '.woff': 'font/woff', '.ttf': 'font/ttf', '.otf': 'font/otf' };+const IMG_MIME = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.svg': 'image/svg+xml', '.webp': 'image/webp', '.gif': 'image/gif' };+const MEDIA_MIME = { '.webm': 'video/webm', '.mp4': 'video/mp4', '.m4v': 'video/mp4', '.mov': 'video/quicktime', '.ogv': 'video/ogg' };+// what the page reads from showtime.json: the stage's keys and the X-ray's own block (no audio paths or local details)+const PUBLIC_KEYS = ['width', 'height', 'fps', 'duration', 'background', 'title', 'poster', 'seed', 'template', 'xray'];++/** An X-ray project: showtime.json "xray" and the measured events (xray.json) next to the page. */+export function isXrayProject(dir, cfg) {+ return !!(cfg && cfg.xray && typeof cfg.xray === 'object' && !Array.isArray(cfg.xray)) && fs.existsSync(path.join(dir, 'xray.json'));+}++function liveFile(name) { return fs.readFileSync(path.join(skillDir(), 'runtime', 'xray-live', name), 'utf8'); }+// inside an inlined script the HTML parser ends it at `</script` and enters its escaped state at `<!--`: both are+// written with an escape that means the same characters in a JS string, template or regex (as figure.mjs does)+const jsSafe = (t) => t.replace(/<\/(script)/gi, (m, w) => '<\\/' + w).replace(/<!--/g, '<\\x21--');+const attrEsc = (v) => String(v).replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"');+const getAttr = (attrs, name) => { const m = new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`, 'i').exec(attrs) || new RegExp(`\\b${name}\\s*=\\s*'([^']*)'`, 'i').exec(attrs); return m ? m[1] : null; };++/**+ * The line that made it: { prompt, commands }. The prompt is explicit only (--prompt, else showtime.json+ * xray.made.prompt, else none); the commands are written with placeholders, never the project's own paths.+ */+export function madeWith(xr, live, prompt, fromProject) {+ const made = xr.made && typeof xr.made === 'object' ? xr.made : {};+ const word = (v, d) => (typeof v === 'string' && /^[\w-]{1,40}$/.test(v) ? v : d);+ // xray.made.mechanism, else xray.mechanism, else the name the project's own mechanism.js declares; the+ // typewriter only as a last guess (live.made.guessed, and the export report says so)+ const named = word(made.mechanism, word(xr.mechanism, word(fromProject, '')));+ const mech = named || 'typewriter';+ const layout = word(made.layout, live.layout === 'through' ? 'through' : live.layout);+ const theme = word(made.theme, live.theme);+ const text = typeof prompt === 'string' && prompt.trim() ? prompt.trim() : (typeof made.prompt === 'string' ? made.prompt.trim() : '');+ const job = `xray-${mech}`;+ return { prompt: text, guessed: !named, commands: [+ `showtime xray analyze clip.mp4 --job ${job}${live.tracked ? ' --track' : ''}`,+ `showtime new xray ${job}/project --mechanism ${mech} --layout ${layout} --theme ${theme}`,+ `showtime render ${job}/project --job ${job}`,+ `showtime xray verify ${job}`,+ ] };+}++/**+ * Pack the live page. -> { render(): html, media: Map(src -> {mime, bytes}), files, missing, remote, page, live }+ * render() writes the page with the media as they are now (fit.mjs may have re-encoded them in between).+ * @param o.projectDir+ * @param o.config showtime.json (the project's)+ * @param o.title the page title+ * @param o.prompt the request that made the film (shown on the page; default: showtime.json xray.made.prompt)+ * @param o.page the film page (default index.html)+ * @param o.allFonts keep every font face+ * @param o.minify shrink scripts and styles (default true)+ * @param o.noCsp leave out the Content-Security-Policy+ */+export function packXrayLive(o) {+ const projectDir = path.resolve(o.projectDir);+ const page = o.page || 'index.html';+ const pagePath = path.join(projectDir, page);+ if (!fs.existsSync(pagePath)) throw new UserError(`the X-ray project has no ${page}`, 'export the project folder `showtime new xray` made');+ let html = fs.readFileSync(pagePath, 'utf8');+ const cfg = o.config || {};+ const mini = o.minify !== false;+ const files = [], missing = [], remote = [];+ const base = '/' + page;+ const read = (url, from, binary) => {+ const f = figureFile(url, { projectDir, base: from || base });+ if (f.inline) return null;+ if (f.remote) { remote.push(f.remote); return null; }+ if (f.escapes || !fs.existsSync(f.file) || !fs.statSync(f.file).isFile()) { missing.push(f.escapes || f.url); return null; }+ const buf = fs.readFileSync(f.file);+ return { buf, text: binary ? null : buf.toString('utf8'), url: f.url, ext: path.extname(f.file).toLowerCase() };+ };++ // ---- the page's data files: they answer its fetch() calls (pre.js)+ const data = {};+ for (const n of ['xray.json', 'notes.json']) {+ const p = path.join(projectDir, n);+ if (!fs.existsSync(p)) continue;+ const t = fs.readFileSync(p, 'utf8');+ try { data[n] = JSON.stringify(JSON.parse(t)); } catch { throw new UserError(`${n} is not valid JSON`, `run \`showtime xray ${n === 'xray.json' ? 'analyze' : 'notes'}\` again and copy it next to ${page}`); }+ files.push({ url: '/' + n, bytes: Buffer.byteLength(data[n]), kind: 'data' });+ }+ const pub = {};+ for (const k of PUBLIC_KEYS) if (cfg[k] !== undefined) pub[k] = cfg[k];+ data['showtime.json'] = JSON.stringify(pub);+ const doc = JSON.parse(data['xray.json']);+ const xr = cfg.xray || {};+ const isDiagram = xr.layout === 'diagram' || /\bXRAY_DIAGRAM\b/.test(html);++ // ---- videos: one packed copy per file; a second <video> of the same file becomes a canvas mirror of the first+ const media = new Map(); // src url -> {key, mime, bytes}+ const masters = new Map(); // src url -> id of its first <video>+ html = html.replace(/<video\b([^>]*)>([\s\S]*?)<\/video>/gi, (m, attrs) => {+ const src = getAttr(attrs, 'src');+ if (!src) return m;+ const r = read(src, base, true);+ if (!r) return m;+ if (!MEDIA_MIME[r.ext]) { missing.push(r.url + ' (not a video this page can pack)'); return m; }+ if (!media.has(r.url)) { media.set(r.url, { key: 'm' + media.size, mime: MEDIA_MIME[r.ext], bytes: r.buf }); files.push({ url: r.url, bytes: r.buf.length, kind: 'video' }); }+ const key = media.get(r.url).key, id = getAttr(attrs, 'id'), cls = getAttr(attrs, 'class');+ if (masters.has(r.url)) {+ return `<canvas${id ? ` id="${attrEsc(id)}"` : ''}${cls ? ` class="${attrEsc(cls)}"` : ''} data-xl-mirror="${attrEsc(masters.get(r.url))}"></canvas>`;+ }+ masters.set(r.url, id || key);+ const rest = attrs.replace(/\s\bsrc\s*=\s*("[^"]*"|'[^']*')/i, '').replace(/\s\bdata-st\s*=\s*("[^"]*"|'[^']*')/i, '');+ return `<video${rest}${id ? '' : ` id="${key}"`} data-st="off" data-xl-src="${key}" preload="auto"></video>`;+ });+ if (!media.size) throw new UserError(`${page} shows no clip (<video src="media/...">) to pack`, 'the X-ray page plays the clip it was made from: put it in media/clip.webm');++ // ---- scripts (stage.js: the render-only cut, after pre.js) and styles, in page order+ const scripts = [], styles = [];+ let stageSeen = false;+ html = html.replace(/<script\b([^>]*)\bsrc="([^"]+)"([^>]*)>\s*<\/script>/gi, (m, a1, src) => {+ const r = read(src, base);+ if (!r) return m;+ files.push({ url: r.url, bytes: r.buf.length, kind: 'script' });+ let text = r.text;+ if (r.url === '/_st/stage.js') { stageSeen = true; text = stageForExport(text); }+ const k = `\u0000S${scripts.length}\u0000`;+ scripts.push({ text, stage: r.url === '/_st/stage.js' });+ return k;+ });+ if (!stageSeen) throw new UserError(`${page} does not load the stage runtime (/_st/stage.js)`, 'the live page drives the same ST.onSeek drawing as the film: keep <script src="/_st/stage.js"> in the page');+ html = html.replace(/<link\b[^>]*\brel="stylesheet"[^>]*>/gi, (m) => {+ const href = (/\bhref="([^"]+)"/i.exec(m) || [])[1];+ if (!href) return m;+ const r = read(href, base);+ if (!r) return m;+ files.push({ url: r.url, bytes: r.buf.length, kind: 'style' });+ const k = `\u0000C${styles.length}\u0000`;+ styles.push({ text: r.text, url: r.url });+ return k;+ });++ // ---- the live page's own parts+ const pre = liveFile('pre.js'), liveJs = liveFile('live.js'), liveCss = liveFile('live.css');+ const W = Math.round(Number(cfg.width) || 1920), H = Math.round(Number(cfg.height) || 1080), fps = Number(cfg.fps) || 30;+ const src = doc.source || {};+ const live = {+ W, H, fps, duration: Number(cfg.duration) || 0, title: o.title || cfg.title || 'X-ray',+ layout: isDiagram ? 'diagram' : (xr.layout === 'split' ? 'split' : 'through'),+ tracked: !isDiagram && !!(doc.track && Array.isArray(doc.track.H) && doc.track.H.length),+ theme: xr.theme === 'light' ? 'light' : 'dark', clipDur: Number(src.duration) || 0,+ receipt: typeof xr.receipt === 'string' ? xr.receipt : '', credit: typeof xr.credit === 'string' ? xr.credit : '',+ drawn: typeof xr.drawn === 'string' ? xr.drawn : '', poster: Number(cfg.poster) || 0,+ generator: `showtime ${showtimeVersion()}`,+ };+ let declared = '';+ try { declared = (/XRAY\.mechanism\(\s*\{[^]*?\bname\s*:\s*['"]([\w-]{1,40})['"]/.exec(fs.readFileSync(path.join(projectDir, 'mechanism.js'), 'utf8')) || [])[1] || ''; } catch { /* no mechanism.js */ }+ live.made = madeWith(xr, live, o.prompt, declared);++ // ---- fonts: faces for scripts no text uses are dropped (the page, its scripts, the data's words, the controls)+ const used = new Set();+ addCodePoints(used, html.replace(/<[^>]+>/g, ' '));+ scripts.forEach((s) => addCodePoints(used, s.text));+ addCodePoints(used, liveJs);+ Object.values(data).forEach((t) => addCodePoints(used, t)); // showtime.json, xray.json, notes.json (note names: ♯ ♭)+ let fontsDropped = 0;+ const cssOut = styles.map((st) => {+ let css = st.text;+ if (!o.allFonts) { const r = pruneFontFaces(css, used); css = r.text; fontsDropped += r.dropped; }+ css = css.replace(/url\(\s*(['"]?)([^'")]+)\1\s*\)/g, (m, q, u) => {+ const r = read(u, st.url, true);+ if (!r) return m;+ const mime = FONT_MIME[r.ext] || IMG_MIME[r.ext];+ if (!mime) { missing.push(r.url + ' (not a font or an image)'); return m; }+ files.push({ url: r.url, bytes: r.buf.length, kind: FONT_MIME[r.ext] ? 'font' : 'image' });+ return `url(data:${mime};base64,${r.buf.toString('base64')})`;+ });+ return mini ? minifyCss(css) : css;+ });+ const js = (t) => jsSafe(mini ? minifyJs(t).text : t);+ const cfgTag = `<script type="application/json" id="xl-cfg">${safeJson({ data, live })}</script>`;+ const jsOut = scripts.map((s) => (s.stage ? `${cfgTag}\n<script>\n${js(pre)}\n</script>\n<script>\n${js(s.text)}\n</script>` : `<script>\n${js(s.text)}\n</script>`));+ html = html.replace(/\u0000S(\d+)\u0000/g, (m, i) => jsOut[+i]);+ html = html.replace(/\u0000C(\d+)\u0000/g, (m, i) => `<style>\n${cssOut[+i].replace(/<\/style/gi, '<\\/style')}\n</style>`);++ // ---- head: charset, the notice, the policy, the title and the link-preview words+ const title = live.title;+ html = html.replace(/<title>[^<]*<\/title>/i, () => `<title>${attrEsc(title)}</title>`);+ if (!/<title>/i.test(html)) html = html.replace(/<head([^>]*)>/i, (m, a) => `<head${a}>\n<title>${attrEsc(title)}</title>`);+ const desc = 'An X-ray of a real clip: play it with its sound, jump from event to event and see the inside drawn on each frame.';+ const fontPkgs = [...new Set(files.filter((f) => f.kind === 'font').map((f) => (/^\/_lib\/((?:@[^/]+\/)?[^/]+)\//.exec(f.url) || [])[1]).filter(Boolean))];+ const ofl = fontPkgs.filter((p) => {+ try { return /OFL/i.test(JSON.parse(fs.readFileSync(path.join(nodeModulesDir(), ...p.split('/'), 'package.json'), 'utf8')).license || ''); } catch { return false; }+ });+ const credits = [live.credit, ofl.length ? `Fonts: ${ofl.join(', ')}, under the SIL Open Font License 1.1 (https://openfontlicense.org)` : ''].filter(Boolean);+ const notice = (`An X-ray's live page, exported with ${live.generator}; every file it uses is packed inside.\n` ++ notices(new Map(files.map((f) => [f.url, f])), { projDir: projectDir, extraCredits: credits }))+ .replace('Third-party packages (their own license headers are kept in the packed files):', 'Third-party packages packed in this file:');+ html = html.replace(/<meta\s+charset="[^"]*"\s*\/?>\s*/i, '');+ const head = `\n<meta charset="utf-8">\n<!--\n${notice.replace(/--/g, '- -')}\n-->` ++ (o.noCsp ? '' : `\n<meta http-equiv="Content-Security-Policy" content="${FIGURE_CSP}">`) ++ '\n<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">' ++ `\n<meta name="description" content="${attrEsc(desc)}">\n<meta property="og:title" content="${attrEsc(title)}">` ++ `\n<meta property="og:description" content="${attrEsc(desc)}">\n<meta property="og:type" content="website">\n<meta name="twitter:card" content="summary">`;+ html = html.replace(/<meta\s+name="viewport"[^>]*>\s*/gi, '');+ html = html.replace(/<head([^>]*)>/i, (m, a) => `<head${a}>${head}`);++ // any remaining reference to a file is a page that would reach the network or miss a file offline+ for (const m of html.matchAll(/(?<![\w-])(?:src|href)="([^"#][^"]*)"/gi)) {+ const f = figureFile(m[1], { projectDir, base });+ if (!f.inline) (f.remote ? remote : missing).push(f.remote || f.url || m[1]);+ }+ const tail = `<style data-xl>\n${mini ? minifyCss(liveCss) : liveCss}\n</style>\n`;+ const render = () => {+ const mediaTags = [...media].map(([u, m]) => `<script type="text/plain" data-xl-media="${m.key}" data-type="${m.mime}" data-src="${attrEsc(u)}">${m.bytes.toString('base64')}</script>`).join('\n');+ const end = `${tail}${mediaTags}\n<script>\n${js(liveJs)}\n</script>\n`;+ return /<\/body>/i.test(html) ? html.replace(/<\/body>/i, () => `${end}</body>`) : html + end;+ };+ return { render, media, files, fontsDropped, missing: [...new Set(missing)], remote: [...new Set(remote)], page, live };+}++/** The command: pack, fit the clip under --max-mb when it is the reason, write, report. */+export async function exportXrayLive({ a, proj, out, title, target, maxMb, warnings, addWarn, T0 }) {+ const quiet = !!a.quiet;+ const say = (m) => { if (!quiet) console.error(c.dim(m)); };+ const p = packXrayLive({ projectDir: proj.dir, config: proj.config || {}, title, prompt: a.prompt, page: a.page, allFonts: !!a['all-fonts'],+ minify: String(a.minify || 'auto').toLowerCase() !== 'off', noCsp: !!a['no-csp'] });+ if (p.remote.length) throw new UserError(`the X-ray page loads ${p.remote.slice(0, 3).join(', ')} from the network; the live page is offline`, 'use a local copy (fonts: /_st/themes/fonts/*.css)');+ if (p.missing.length) throw new UserError(`the X-ray page needs files that are not there: ${p.missing.slice(0, 6).join(', ')}`, `fix the paths in ${p.page} (relative to the project; /_st/ is built in)`);+ let html = p.render();+ const refit = [];+ const fitMode = String(a.fit || 'auto').toLowerCase();+ if (maxMb > 0 && Buffer.byteLength(html) > maxMb * MB && fitMode !== 'off') {+ const workDir = fs.mkdtempSync(path.join(os.tmpdir(), 'showtime-xray-live-'));+ try {+ const map = new Map([...p.media].map(([u, m]) => [u, m])); // fit.mjs re-encodes {mime, bytes} in place+ for (let k = 0; k < 3 && Buffer.byteLength(html) > maxMb * MB; k++) {+ const r = await fitFootage(map, { over: Buffer.byteLength(html) - maxMb * MB, workDir, say: (m) => say(` fit: ${m}`) });+ for (const it of r.items) { const i = refit.findIndex((x) => x.path === it.path); if (i >= 0) refit[i] = it; else refit.push(it); }+ if (!r.items.length) { if (r.reason) addWarn(`could not fit the clip under ${maxMb} MB: ${r.reason}`); break; }+ html = p.render();+ }+ } finally { fs.rmSync(workDir, { recursive: true, force: true }); }+ }+ const bytes = Buffer.byteLength(html);+ if (maxMb > 0 && bytes > maxMb * MB) {+ const big = p.files.slice().sort((x, y) => y.bytes - x.bytes).slice(0, 4).map((f) => `${f.url} ${fmtBytes(f.bytes)}`).join(', ');+ throw new UserError(`the live page is ${fmtBytes(bytes)}, over --max-mb ${maxMb}; largest: ${big}`,+ 'trim the clip to the part that matters (showtime footage trim), or raise --max-mb (an artifact is limited to 16 MB)');+ }+ if (p.live.made.guessed) addWarn('the project names no mechanism (showtime.json xray.made.mechanism, or XRAY.mechanism({name}) in mechanism.js): the page\'s commands say typewriter; set xray.made = {mechanism, layout, theme}');+ if (refit.length) addWarn(`the clip was re-encoded for this export only (${refit.map((r) => `${r.kbps} kb/s`).join(', ')}) to stay under ${maxMb} MB; the project's files are unchanged`);+ fs.mkdirSync(path.dirname(out), { recursive: true });+ fs.writeFileSync(out, html);+ const report = { output: out, interactive: true, kind: 'xray-live', layout: p.live.layout, tracked: p.live.tracked, page: p.page, bytes,+ mb: +(bytes / MB).toFixed(2), max_mb: maxMb, title: p.live.title, target, files: p.files.length, fonts_dropped: p.fontsDropped,+ media: [...p.media].map(([u, m]) => ({ path: u, bytes: m.bytes.length })), footage_refit: refit, made: { prompt: p.live.made.prompt, commands: p.live.made.commands, guessed: p.live.made.guessed },+ largest: p.files.slice().sort((x, y) => y.bytes - x.bytes).slice(0, 6), warnings, seconds: +((Date.now() - T0) / 1000).toFixed(1) };+ if (a.json) process.stdout.write(JSON.stringify(report, null, 2) + '\n');+ else {+ console.log(`${c.green('exported')} ${out}`);+ console.log(` the X-ray live page: ${fmtBytes(bytes)} of ${maxMb || 'unlimited'} MB, one file; it makes no network request`);+ console.log(' play the clip with its sound, click a tick or press the arrow keys to jump event to event, 0.25x around one;');+ console.log(` links like #hit4 (the 4th event) or #t2.5 open a moment; add _light or _dark${p.live.tracked ? ', _through or _split' : ''} (#hit4_light)`);+ console.log(p.live.made.prompt ? ` the page shows this prompt as the line that made it: "${p.live.made.prompt}"` : ' no prompt is embedded (pass --prompt "<the request>" to show the line that made it); the page lists the commands');+ showCard({ title: `${path.basename(out)} is ready`, file: out, facts: ['X-ray live page', fmtBytes(bytes)], next: openHint(out) });+ }+ return 0;+}diff --git a/skills/showtime/setup/package.json b/skills/showtime/setup/package.jsonindex 0a561a7..09e2527 100644--- a/skills/showtime/setup/package.json+++ b/skills/showtime/setup/package.json@@ -1,6 +1,6 @@{"name": "showtime-runtime",- "version": "0.4.3",+ "version": "0.4.4","private": true,"description": "Node dependencies for showtime (installed by setup.py into ~/.showtime/node). Versions are pinned exactly. Icon sets are not installed here: they are fetched per icon on first use (manifest.json \"icon_packs\").","license": "MIT",diff --git a/skills/showtime/templates/diagram/README.md b/skills/showtime/templates/diagram/README.mdindex baf65ef..1e6460c 100644--- a/skills/showtime/templates/diagram/README.md+++ b/skills/showtime/templates/diagram/README.md@@ -11,6 +11,9 @@ when their part matters and the camera chooses what to look at.`enter`) starts from one at its own length.- `examples/econ-lab.js`: a three-equation economy (households, firms, a central bank) as a live-first figure:steps are experiments, the rule strength and a promise's horizon are knobs; its film is its tour.+- `examples/computer.js`: what happens when you press a key, as a live figure you type on: the key goes down,+ the matrix finds it, its USB HID code waits for the poll (a knob), the CPU takes an interrupt, the editor puts the+ character in RAM, the GPU draws it and the screen shows it at its next refresh; its film is its tour.- `index.html`: the film's page (title block, corner notes, footers) and the fonts of the themes. The theme is`<html data-theme>`: `showtime new diagram <dir> --theme dark` (or `white`) sets it, with `"theme"` and`"background"` in `showtime.json`.diff --git a/skills/showtime/templates/diagram/examples/computer.js b/skills/showtime/templates/diagram/examples/computer.jsnew file mode 100644index 0000000..563e764--- /dev/null+++ b/skills/showtime/templates/diagram/examples/computer.js@@ -0,0 +1,452 @@+/* "What happens when you press a key": a computer, inside, as a LIVE figure you type on (simplified, illustrative).+ The keyboard is drawn key by key (onFrame below): the key you press goes down and lights, the key matrix finds it,+ the keyboard controller turns it into its USB HID usage id (the standard code: 'a' is 0x04), and the code waits+ for the computer's next poll, then travels down the USB cable. The CPU takes an interrupt, the OS puts the event+ in its input queue, the text editor puts the character in its text (in RAM), the GPU draws the glyph into the+ next frame, and the screen shows it at its next refresh, scanning top to bottom. Several keys are in flight at+ once when you type fast. Everything is in slow motion (SLOW below); the times are typical and illustrative, from+ one place (MS), and a real machine adds layers (drivers, a compositor) and varies.+ The live page is figure.html (`showtime export html <project> --interactive`): type anywhere on it, or click a+ key. index.html films the tour (interactive.film = 'tour': the same live actions on the film's clock), so+ CUE.duration is the tour's length. To use it: `showtime new diagram <dir> --example computer --theme white`. */+var CUE = {+ duration: 30.2,+};++(function () {+ /* ---------------- the numbers: typical, illustrative (one place) ---------------- */+ var SLOW = 30; // slow motion: 1 ms is drawn as 30 ms+ var MS = { scan: 5, os: 1, app: 2, gpu: 3, show: 10 }; // scan + debounce, USB + interrupt + OS, the app, drawing, the screen showing it+ var SCREEN_HZ = 60; // the screen's refresh rate+ var POLL = [['125', 'every 8 ms'], ['500', 'every 2 ms'], ['1000', 'every 1 ms']], POLLDEF = '1000';+ var sec = function (ms) { return ms * SLOW / 1000; }; // real ms -> seconds on screen+ var FRAME = sec(1000 / SCREEN_HZ), READY = sec(MS.scan), SWEEP = sec(MS.show), GPUT = sec(MS.gpu);+ var TT = 0.3, HOP = 0.16, NPK = 14, LINE = 24; // drawn travel (not to scale), packets in flight, characters a line shows++ /* ---------------- USB HID usage ids, Keyboard/Keypad page (0x07): the codes a USB keyboard sends ---------------- */+ var HID = {};+ 'abcdefghijklmnopqrstuvwxyz'.split('').forEach(function (c, i) { HID[c] = 0x04 + i; });+ '1234567890'.split('').forEach(function (c, i) { HID[c] = 0x1E + i; });+ var SPECIAL = { '\n': 0x28, '\b': 0x2A, '\t': 0x2B, ' ': 0x2C, '-': 0x2D, '=': 0x2E, '[': 0x2F, ']': 0x30, '\\': 0x31, ';': 0x33, "'": 0x34, '`': 0x35, ',': 0x36, '.': 0x37, '/': 0x38,+ caps: 0x39, ctlL: 0xE0, shiftL: 0xE1, altL: 0xE2, guiL: 0xE3, ctlR: 0xE4, shiftR: 0xE5, altR: 0xE6, guiR: 0xE7 };+ Object.keys(SPECIAL).forEach(function (k) { HID[k] = SPECIAL[k]; });+ var SHIFTED = { '!': '1', '@': '2', '#': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0', '_': '-', '+': '=',+ '{': '[', '}': ']', '|': '\\', ':': ';', '"': "'", '~': '`', '<': ',', '>': '.', '?': '/' };+ var NAME = { '\n': 'Enter', '\b': 'Backspace', '\t': 'Tab', ' ': 'Space', caps: 'Caps Lock', ctlL: 'Ctrl', ctlR: 'Ctrl', shiftL: 'Shift', shiftR: 'Shift',+ altL: 'Alt', altR: 'Alt', guiL: 'Cmd/Win', guiR: 'Cmd/Win' };+ var hex = function (n) { return '0x' + ('0' + n.toString(16).toUpperCase()).slice(-2); };+ // a typed character -> {key: the key's id on the board, shift, code, ch} (null: no key on a US keyboard makes it alone)+ function keyOf(ch) {+ if (HID[ch] != null) return { key: ch, shift: false, code: HID[ch], ch: ch };+ var lo = ch.toLowerCase();+ if (lo !== ch && HID[lo] != null && lo.length === 1) return { key: lo, shift: true, code: HID[lo], ch: ch };+ if (SHIFTED[ch]) return { key: SHIFTED[ch], shift: true, code: HID[SHIFTED[ch]], ch: ch };+ return null;+ }+ function codeText(k, ch) {+ if (!k) return '"' + ch + '": no single key';+ var nm = NAME[k.key] || (k.shift ? ch : k.key);+ return nm + ' → ' + hex(k.code) + (k.shift ? ' + Shift' : '');+ }++ /* ---------------- the board ---------------- */+ var S = 14, U = 2.2, KX = 0.3, KY = 30.8, KZ = 1.0, CAP = 0.6; // world scale; key pitch, the key area, the slab top, a keycap's height+ // ANSI rows in key widths: [id, width, legend]+ var ROWS = [+ [['`', 1], ['1', 1], ['2', 1], ['3', 1], ['4', 1], ['5', 1], ['6', 1], ['7', 1], ['8', 1], ['9', 1], ['0', 1], ['-', 1], ['=', 1], ['\b', 2, 'back']],+ [['\t', 1.5, 'tab'], ['q', 1], ['w', 1], ['e', 1], ['r', 1], ['t', 1], ['y', 1], ['u', 1], ['i', 1], ['o', 1], ['p', 1], ['[', 1], [']', 1], ['\\', 1.5]],+ [['caps', 1.75, 'caps'], ['a', 1], ['s', 1], ['d', 1], ['f', 1], ['g', 1], ['h', 1], ['j', 1], ['k', 1], ['l', 1], [';', 1], ["'", 1], ['\n', 2.25, 'enter']],+ [['shiftL', 2.25, '⇧'], ['z', 1], ['x', 1], ['c', 1], ['v', 1], ['b', 1], ['n', 1], ['m', 1], [',', 1], ['.', 1], ['/', 1], ['shiftR', 2.75, '⇧']],+ [['ctlL', 1.25, ''], ['altL', 1.25, ''], ['guiL', 1.75, ''], [' ', 6.25, ''], ['guiR', 1.75, ''], ['altR', 1.25, ''], ['ctlR', 1.5, '']],+ ];+ var KEYS = [], KEY = {};+ ROWS.forEach(function (row, r) {+ var off = 0;+ row.forEach(function (k) {+ var key = { id: k[0], r: r, x: KX + off * U, y: KY + r * U, w: k[1] * U, legend: k[2] != null ? k[2] : k[0].toUpperCase() };+ KEYS.push(key); KEY[k[0]] = key; off += k[1];+ });+ });+ // the monitor's panel and its screen (the y+ face, toward the keyboard)+ var MON = { x0: 10, x1: 38, y: -3, d: 1.2, z0: 3.5, z1: 15 }, MY = MON.y + MON.d;+ var SCR = { x0: MON.x0 + 0.7, x1: MON.x1 - 0.7, z0: MON.z0 + 0.7, z1: MON.z1 - 0.7, bar: 1.5 };++ /* ---------------- the model: what was typed, when (every drawing below reads these logs at t) ---------------- */+ var M = { text: '', count: 0, last: null, k: 0, q: 0, presses: [], frames: [{ at: -1e9, text: '' }], poll: [], poke: -1e9, bytes: -1e9, demo: 0 };+ var SCHED = {}; // a tag's own schedule, so keys typed fast never drop each other's later values+ // how: 'restore' marks a value that goes back to the text (RAM after its bytes were shown); 'feed' (a key's new+ // text) updates the restores after it, up to the next key's own value+ function sayAt(fx, tag, text, tone, at, how) {+ var s = SCHED[tag] || (SCHED[tag] = []), i = s.length;+ while (i > 0 && s[i - 1].at > at) i--;+ s.splice(i, 0, { at: at, text: text, tone: tone, how: how });+ if (how === 'feed') for (var r = i + 1; r < s.length && s[r].how !== 'feed'; r++) if (s[r].how === 'restore') s[r].text = text;+ for (var j = i; j < s.length; j++) {+ var e = s[j], o = { delay: e.at - fx.tau };+ if (e.text == null) fx.hide(tag, o); else fx.say(tag, e.text, e.tone, o);+ }+ if (s.length > 80) s.splice(0, s.length - 80);+ }+ function pollAt(t) { var hz = +POLLDEF, a = -1e9; M.poll.forEach(function (p) { if (p[0] <= t) { a = p[0]; hz = p[1]; } }); return { a: a, hz: hz }; }+ // the computer asks every 1/hz s; an ask reaches the keyboard TT later; the code goes with the first ask after it is ready+ function departAfter(ready, state) {+ var p = pollAt(ready), I = sec(1000 / p.hz), a = p.a < -1e8 ? 0 : p.a;+ return a + Math.ceil((ready - a - TT - 1e-9) / I) * I + TT;+ }+ function insertFrame(f) { var i = M.frames.length; while (i > 0 && M.frames[i - 1].at > f.at) i--; M.frames.splice(i, 0, f); if (M.frames.length > 400) M.frames.splice(1, 1); }+ var shownLine = function (text) { var ls = text.split('\n'); return ls[ls.length - 1].replace(/\t/g, ' ').slice(-LINE); };+ var onScreen = function (line) { return 'on screen: ' + (line ? line.replace(/ /g, '\u00a0') : '(empty line)'); };+ var buffer = function (text) { var b = text.replace(/\n/g, '\\n').replace(/\t/g, '\\t'); return '"' + (b.length > 10 ? '…' + b.slice(-9) : b) + '"'; };+ var bytes = function (text) {+ var b = unescape(encodeURIComponent(text.slice(-5))), out = [];+ for (var i = 0; i < b.length; i++) out.push(('0' + b.charCodeAt(i).toString(16).toUpperCase()).slice(-2));+ return out.length ? out.slice(-6).join(' ') : 'empty';+ };++ // one key, through the whole path: fx.tau is the moment it is pressed+ function typeKey(fx, ch, state) {+ var k = keyOf(ch), t0 = fx.tau;+ M.count++;+ if (ch === '\b') M.text = M.text.slice(0, -1); else M.text += ch;+ if (M.text.length > 2000) M.text = M.text.slice(-1000);+ var snap = M.text, ready = t0 + READY;+ if (k) M.presses.push({ at: t0, key: k.key, shift: k.shift });+ if (M.presses.length > 300) M.presses.splice(0, 100);+ var dep = departAfter(ready, state), cpuAt = dep + TT;+ var tQ = cpuAt + HOP, tE = tQ + 0.12 + HOP, tR = tE + 0.1 + HOP, tG = tR + HOP, done = tG + GPUT;+ var vs = Math.ceil((done - 1e-9) / FRAME) * FRAME, tS = vs + HOP;+ insertFrame({ at: tS, text: snap });+ var waitMs = (dep - ready) / SLOW * 1000, frameMs = (vs - done) / SLOW * 1000;+ M.last = { ch: ch, k: k, wait: waitMs, total: MS.scan + waitMs + MS.os + MS.app + MS.gpu + frameMs + MS.show, hz: pollAt(ready).hz };+ var d = function (t) { return { delay: t - t0 }; }, tok = 'pk' + (M.k++ % NPK), slot = 'queue.slot' + (1 + (M.q++ % 4)), up = [0, 0, 0.7];+ fx.send(tok, [+ { at: READY, dur: 0.01, to: 'ctrl.top' },+ { at: dep - t0, dur: TT, along: 'usb' },+ { at: cpuAt - t0, dur: HOP, along: 'p2c', off: up },+ { at: tQ - t0, dur: HOP, along: 'c2q', off: up },+ { at: tQ - t0 + HOP, dur: 0.12, to: slot, off: [0, 0, 0.3] },+ { at: tE - t0 - HOP, dur: HOP, along: 'q2e', off: up },+ { at: tE - t0 + 0.1, dur: HOP, along: 'e2r', off: up },+ { at: tR - t0, dur: HOP, along: 'r2g', off: up },+ { at: vs - t0, dur: HOP, along: 'g2s', off: [0, 0, 0.4] },+ ]);+ fx.pulse('ctrl', 'cold', d(ready));+ sayAt(fx, 'code', codeText(k, ch), 'cold', ready);+ fx.pulse('port', 'cold', d(cpuAt - 0.05));+ fx.pulse('cpu', 'warm', Object.assign(d(cpuAt), { dur: 0.6, amount: 0.6 }));+ sayAt(fx, 'irq', 'IRQ', 'warm', cpuAt); sayAt(fx, 'irq', null, null, cpuAt + 1.0);+ fx.pulse('queue', 'cold', d(tQ + HOP));+ fx.pulse('editor', 'cold', d(tE));+ fx.pulse('ram', 'cold', d(tR));+ sayAt(fx, 'buffer', buffer(snap), 'cold', tR, 'feed');+ fx.pulse('gpu', 'warm', Object.assign(d(tG), { dur: GPUT + 0.3 }));+ sayAt(fx, 'screen', onScreen(shownLine(snap)), 'mute', tS + SWEEP);+ }+ // a key that types nothing (a modifier, Caps Lock): it is found and its code goes to the OS, no character+ function modifierKey(fx, id, state) {+ var t0 = fx.tau, ready = t0 + READY, dep = departAfter(ready, state), tok = 'pk' + (M.k++ % NPK), up = [0, 0, 0.7];+ M.presses.push({ at: t0, key: id, shift: false });+ fx.send(tok, [{ at: READY, dur: 0.01, to: 'ctrl.top' }, { at: dep - t0, dur: TT, along: 'usb' }, { at: dep + TT - t0, dur: HOP, along: 'p2c', off: up },+ { at: dep + TT + HOP - t0, dur: HOP, along: 'c2q', off: up }, { at: dep + TT + 2 * HOP - t0, dur: 0.12, to: 'queue.slot1', off: [0, 0, 0.3] }]);+ sayAt(fx, 'code', (NAME[id] || id) + ' → ' + hex(HID[id]), 'cold', ready);+ fx.pulse('ctrl', 'cold', { delay: READY });+ fx.pulse('cpu', 'warm', { delay: dep + TT - t0, dur: 0.6, amount: 0.6 });+ }++ // a click on the screen: the next refresh scans a frame in again (the same text), the caret holds still a moment+ function refresh(fx) { M.poke = fx.tau; insertFrame({ at: Math.ceil(fx.tau / FRAME) * FRAME, text: M.text }); }++ /* ---------------- clicking a key: the key under the pointer (the live page only) ---------------- */+ var PTR = null, CAM = null, KEYHIT = [], CLICKED = false;+ function bindPointer() {+ var fig = document.querySelector('.fg-fig'); if (!fig || fig.__pcBound) return; fig.__pcBound = true;+ fig.addEventListener('pointerdown', function (e) { PTR = { x: e.clientX, y: e.clientY }; }, true);+ // a key click types; it does not pin the keyboard (the veil would hide the path the key takes)+ fig.addEventListener('click', function () {+ PTR = null; // a pointer is good for its own click only+ if (!CLICKED) return; CLICKED = false;+ var F = window.Figure && Figure.current; if (F && F.S.pin === 'keyboard') F.pin('keyboard');+ });+ }+ function keyUnderPointer() {+ if (!PTR || !CAM) return null;+ var svg = document.querySelector('#ld-world'); svg = svg && svg.ownerSVGElement; if (!svg || !svg.getScreenCTM()) return null;+ var pt = svg.createSVGPoint(); pt.x = PTR.x; pt.y = PTR.y; pt = pt.matrixTransform(svg.getScreenCTM().inverse());+ var x = (pt.x - CAM.ex) / CAM.z, y = (pt.y - CAM.ey) / CAM.z;+ for (var i = KEYHIT.length - 1; i >= 0; i--) if (inPoly(x, y, KEYHIT[i].poly)) return KEYHIT[i].key;+ return null;+ }+ function inPoly(x, y, poly) {+ var inside = false;+ for (var i = 0, j = poly.length - 1; i < poly.length; j = i++) {+ var a = poly[i], b = poly[j];+ if (((a[1] > y) !== (b[1] > y)) && (x < (b[0] - a[0]) * (y - a[1]) / (b[1] - a[1]) + a[0])) inside = !inside;+ }+ return inside;+ }+ function clickKey(fx, s) {+ var key = keyUnderPointer();+ if (key) { CLICKED = true; PTR = null; }+ if (!key) { var demo = 'hello '; typeKey(fx, demo.charAt(M.demo++ % demo.length), s); return; } // from the parts list: a letter of "hello"+ if (key.id === 'shiftL' || key.id === 'shiftR' || key.id === 'caps' || /^(ctl|alt|gui)/.test(key.id)) modifierKey(fx, key.id, s);+ else typeKey(fx, key.id, s);+ }++ /* ---------------- drawing what the parts library has no part for (pure in t: from the logs above) ---------------- */+ var C = Math.cos(Math.PI / 6);+ var f1 = function (n) { return String(Math.round(n * 10) / 10); };+ var mix = function (a, b, p) {+ var pa = parseInt(a.slice(1), 16), pb = parseInt(b.slice(1), 16), o = '#';+ [16, 8, 0].forEach(function (sh) { var x = Math.round(((pa >> sh) & 255) * (1 - p) + ((pb >> sh) & 255) * p); o += ('0' + x.toString(16)).slice(-2); });+ return o;+ };+ function pressAmount(t, at) { var u = t - at; return u < 0 ? 0 : u < 0.05 ? u / 0.05 : u < 0.2 ? 1 : u < 0.32 ? 1 - (u - 0.2) / 0.12 : 0; }+ function onFrame(t, o) {+ var world = document.querySelector('#ld-world'), api = window.Diagram && Diagram.current;+ if (!world || !api) return;+ var g = world.querySelector('#pc-over');+ if (!g) { g = document.createElementNS('http://www.w3.org/2000/svg', 'g'); g.setAttribute('id', 'pc-over'); world.appendChild(g); }+ bindPointer();+ CAM = o.cam;+ var L = api.theme, P = o.P, pts = function (a) { return a.map(function (p) { return f1(p[0]) + ',' + f1(p[1]); }).join(' '); };+ var poly = function (a, fill, stroke, w, extra) { return '<polygon points="' + pts(a) + '" fill="' + fill + '" stroke="' + (stroke || L.ink) + '" stroke-width="' + (w || L.lw * 0.8) + '" stroke-linejoin="round"' + (extra || '') + '/>'; };+ var cold = L.tones.cold, warm = L.tones.warm, fa = o.focus || { amount: 0 }, fc = fa.fc;+ var dim = function (ids) { return fa.amount > 0 && !(fc && ids.indexOf(fc.part) >= 0) ? 1 - (1 - (L.dimTo || 0.4)) * fa.amount : 1; };+ var out = '';++ /* the keyboard: each key a cap that goes down and lights; the key matrix under them, x-rayed while it scans */+ var recent = M.presses.filter(function (p) { return p.at <= t && t - p.at < 1.4; });+ var down = {}, lit = {};+ recent.forEach(function (p) {+ down[p.key] = Math.max(down[p.key] || 0, pressAmount(t, p.at)); lit[p.key] = Math.max(lit[p.key] || 0, 1 - (t - p.at) / 1.1);+ if (p.shift) { down.shiftL = Math.max(down.shiftL || 0, pressAmount(t, p.at - 0.04)); lit.shiftL = Math.max(lit.shiftL || 0, 1 - (t - p.at) / 1.1); }+ });+ var kb = '', hits = [];+ KEYS.forEach(function (k) {+ var gp = 0.13, x0 = k.x + gp, x1 = k.x + k.w - gp, y0 = k.y + gp, y1 = k.y + U - gp, dz = -0.38 * (down[k.id] || 0), z0 = KZ, z1 = KZ + CAP + dz, sc = 0.16;+ var lv = Math.max(0, lit[k.id] || 0), top = lv > 0 ? mix(L.faces.top, cold.flow, 0.75 * lv) : L.faces.top;+ var front = lv > 0 ? mix(L.faces.left, cold.flow, 0.6 * lv) : L.faces.left, side = lv > 0 ? mix(L.faces.right, cold.flow, 0.6 * lv) : L.faces.right;+ kb += poly([P(x1, y0, z0), P(x1, y1, z0), P(x1 - sc, y1 - sc, z1), P(x1 - sc, y0 + sc, z1)], side, null, L.lw * 0.6);+ kb += poly([P(x0, y1, z0), P(x1, y1, z0), P(x1 - sc, y1 - sc, z1), P(x0 + sc, y1 - sc, z1)], front, null, L.lw * 0.6);+ kb += poly([P(x0 + sc, y0 + sc, z1), P(x1 - sc, y0 + sc, z1), P(x1 - sc, y1 - sc, z1), P(x0 + sc, y1 - sc, z1)], top, null, L.lw * 0.6);+ if (k.legend) {+ var c = P(x0 + 0.42, (y0 + y1) / 2 + 0.28, z1), big = k.legend.length === 1;+ kb += '<text transform="matrix(' + [C, 0.5, -C, 0.5].map(function (v) { return Math.round(v * 1000) / 1000; }).join(' ') + ' ' + f1(c[0]) + ' ' + f1(c[1]) + ')" font-family="' + L.font.mono + '" font-size="' + (big ? 15 : 12) ++ '" font-weight="600" data-st-decor="1" fill="' + (lv > 0.35 ? '#FFFFFF' : L.floorInk) + '">' + k.legend.replace(/&/g, '&').replace(/</g, '<') + '</text>';+ }+ hits.push({ key: k, poly: [P(x0, y0, z1), P(x1, y0, z1), P(x1, y0, z0), P(x1, y1, z0), P(x0, y1, z0), P(x0, y1, z1)] });+ });+ KEYHIT = hits;+ // the matrix: rows sweep while the controller scans, then the pressed key's row and column light where they cross+ var mx = '', zl = KZ + CAP + 0.05;+ recent.forEach(function (p) {+ var key = KEY[p.key], u = t - p.at; if (!key || u > READY + 0.7) return;+ var a = u < READY ? 1 : 1 - (u - READY) / 0.7, sweep = Math.min(4, Math.floor((u / READY) * 5)), ry = function (r) { return KY + (r + 0.5) * U; };+ if (u < READY) mx += '<path d="M' + pts([P(KX, ry(sweep), zl), P(KX + 15 * U, ry(sweep), zl)]).replace(/ /g, 'L') + '" stroke="' + cold.flow + '" stroke-width="2" opacity="0.45" fill="none"/>';+ var cx = key.x + key.w / 2;+ mx += '<path d="M' + pts([P(KX, ry(key.r), zl), P(KX + 15 * U, ry(key.r), zl)]).replace(/ /g, 'L') + '" stroke="' + cold.flow + '" stroke-width="3.4" opacity="' + f1(0.9 * a * Math.min(1, u / READY)) + '" fill="none"/>';+ mx += '<path d="M' + pts([P(cx, KY, zl), P(cx, KY + 5 * U, zl)]).replace(/ /g, 'L') + '" stroke="' + cold.flow + '" stroke-width="3.4" opacity="' + f1(0.9 * a * Math.min(1, u / READY)) + '" fill="none"/>';+ var q = P(cx, ry(key.r), zl);+ if (u >= READY * 0.6) mx += '<circle cx="' + f1(q[0]) + '" cy="' + f1(q[1]) + '" r="' + f1(7 + 6 * Math.min(1, (u - READY * 0.6) / 0.25)) + '" fill="none" stroke="' + cold.flow + '" stroke-width="2.4" opacity="' + f1(a) + '"/>';+ });+ out += '<g opacity="' + f1(dim(['keyboard', 'ctrl'])) + '">' + kb + (mx ? '<g filter="url(#ld-glowsoft)">' + mx + '</g>' : '') + '</g>';++ /* the polls: the computer asks the keyboard for news at the polling rate (dots running up the cable) */+ var pp = api.pipes && api.pipes.usb;+ if (pp) {+ var dots = '', at = function (s) { var c = pp.cum, i = 1; while (i < c.length - 1 && c[i] < s) i++; var q = Math.max(0, Math.min(1, (s - c[i - 1]) / ((c[i] - c[i - 1]) || 1))); return [pp.scr[i - 1][0] + (pp.scr[i][0] - pp.scr[i - 1][0]) * q, pp.scr[i - 1][1] + (pp.scr[i][1] - pp.scr[i - 1][1]) * q]; };+ var segs = [[0, +POLLDEF]].concat(M.poll);+ segs.forEach(function (s, i) {+ var a0 = s[0], a1 = i + 1 < segs.length ? segs[i + 1][0] : Infinity, I = sec(1000 / s[1]);+ if (t < a0 || t - TT > a1) return;+ for (var n = Math.max(0, Math.ceil((t - TT - a0) / I)); a0 + n * I <= Math.min(t, a1); n++) {+ var f = (t - (a0 + n * I)) / TT; if (f < 0 || f > 1) continue;+ var q = at(pp.len * (1 - f));+ dots += '<circle cx="' + f1(q[0]) + '" cy="' + f1(q[1]) + '" r="2.6" fill="' + L.ground + '" stroke="' + L.tones.mute.flow + '" stroke-width="1.8"/>';+ }+ });+ out += '<g opacity="' + f1(dim(['keyboard', 'ctrl'])) + '">' + dots + '</g>';+ }++ /* the screen: the frame it shows (lines of the text, a caret); a new frame scans in top to bottom */+ var fr = M.frames, ci = 0; for (var i = 0; i < fr.length; i++) if (fr[i].at <= t) ci = i;+ var cur = fr[ci], prev = fr[Math.max(0, ci - 1)], sweep = t < cur.at + SWEEP ? (t - cur.at) / SWEEP : 1;+ var face = function (x0, z0, x1, z1) { return [P(x0, MY, z0), P(x1, MY, z0), P(x1, MY, z1), P(x0, MY, z1)]; };+ var sc = '', ztop = SCR.z1 - SCR.bar, lh = 1.9, fs = 24, cw = fs * 0.6 / S;+ sc += poly(face(SCR.x0, SCR.z0, SCR.x1, SCR.z1), L.name === 'dark' ? '#0E1726' : '#FBFCFD', L.ink, L.lw * 0.7);+ sc += poly(face(SCR.x0, ztop, SCR.x1, SCR.z1), L.name === 'dark' ? '#16233A' : '#EEF0F3', 'none', 0);+ [0, 1, 2].forEach(function (j) { var q = P(SCR.x0 + 0.8 + j * 0.7, MY, SCR.z1 - SCR.bar / 2); sc += '<ellipse cx="' + f1(q[0]) + '" cy="' + f1(q[1] + 2.5) + '" rx="3.4" ry="3.6" fill="' + L.detail + '"/>'; });+ var lines = function (text) { var ls = text.split('\n').map(function (l) { return l.replace(/\t/g, ' ').slice(-LINE); }); return ls.slice(-4); };+ var ink = L.name === 'dark' ? '#E5ECF6' : '#111827';+ var textOf = function (text, clip) {+ var s = '', ls = lines(text);+ ls.forEach(function (ln, j) {+ var b = P(SCR.x0 + 0.9, MY, ztop - 0.5 - (j + 1) * lh + 0.35);+ s += '<text transform="matrix(' + [C, 0.5, 0, 1].map(function (v) { return Math.round(v * 1000) / 1000; }).join(' ') + ' ' + f1(b[0]) + ' ' + f1(b[1]) + ')" font-family="' + L.font.mono + '" font-size="' + fs + '" font-weight="500" data-st-decor="1" fill="' + ink + '" xml:space="preserve"' + (clip ? ' clip-path="url(#' + clip + ')"' : '') + '>' ++ ln.replace(/&/g, '&').replace(/</g, '<') + '</text>';+ });+ return { s: s, n: ls.length, last: ls[ls.length - 1] || '' };+ };+ var zs = ztop - (ztop - SCR.z0) * sweep, now = textOf(cur.text, sweep < 1 ? 'pc-new' : null);+ if (sweep < 1) {+ sc += '<clipPath id="pc-new"><polygon points="' + pts(face(SCR.x0, zs, SCR.x1, ztop)) + '"/></clipPath><clipPath id="pc-old"><polygon points="' + pts(face(SCR.x0, SCR.z0, SCR.x1, zs)) + '"/></clipPath>';+ sc += textOf(prev.text, 'pc-old').s + now.s;+ sc += '<path d="M' + pts([P(SCR.x0, MY, zs), P(SCR.x1, MY, zs)]).replace(/ /g, 'L') + '" stroke="' + cold.flow + '" stroke-width="2.4" opacity="0.7" fill="none"/>';+ } else sc += now.s;+ // the caret: after the last character; steady just after a new frame or a click on the screen, else it blinks+ var shownAt = Math.max(cur.at + SWEEP, M.poke), steady = t - shownAt < 0.7, on = steady || Math.floor(t * 1.7) % 2 === 0;+ if (on && sweep >= 1) {+ var cz = ztop - 0.5 - now.n * lh, cx0 = SCR.x0 + 0.95 + now.last.length * cw;+ sc += poly([P(cx0, MY, cz + 0.1), P(cx0 + cw * 0.9, MY, cz + 0.1), P(cx0 + cw * 0.9, MY, cz + lh - 0.15), P(cx0, MY, cz + lh - 0.15)], cold.flow, 'none', 0);+ }+ out += '<g>' + sc + '</g>'; // the screen is the payoff: never veiled+ g.innerHTML = out;+ }+ /* ---------------- the scene ---------------- */+ var box = function (id, at, size, o) { return Object.assign({ id: id, part: 'box', at: at, size: size, build: -3, lift: 0 }, o || {}); };+ var node = function (id, icon, x, y, o) { return Object.assign({ id: id, part: 'node', icon: icon, at: [x, y, 0], r: 3.4, build: -3, lift: 0 }, o || {}); };+ var link = function (id, a, b, o) { return Object.assign({ id: id, kind: 'link', from: a, to: b, w: 3, layer: 'floor', build: [-3, 0.01] }, o || {}); };+ var toks = []; for (var i = 0; i < NPK; i++) toks.push({ id: 'pk' + i, tone: 'cold', shape: 'chip', size: [2.2, 1.6, 0.5], moves: [] });+ var tour = [];+ var typing = function (text, gap, last) {+ text.split('').forEach(function (ch, i) { tour.push([function (fx, s) { typeKey(fx, ch, s); }, i === text.length - 1 ? last : gap]); });+ };+ // the tour (and the film): a new line if there is text already, then "hello"; slower polls; Enter; a typo fixed+ tour.push([function (fx, s) { if (M.text && M.text.slice(-1) !== '\n') typeKey(fx, '\n', s); }, 300], [{ step: 'path' }, 1500]);+ typing('he', 1400, 1400); // two keys slowly (each code can be read), then three fast: several in flight at once+ typing('llo', 270, 2600);+ tour.push([{ hover: 'cpu' }, 2400], [{ hover: null }, 500], [{ knob: ['poll', '125'], step: 'poll' }, 1400]);+ typing(' world', 300, 2000);+ tour.push([{ hover: 'ctrl' }, 1800], [{ hover: null }, 300]);+ tour.push([{ knob: ['poll', '1000'] }, 1200]);+ typing('!', 0, 2600);+ tour.push([{ step: 'budget' }, 1600]);+ typing('\nok?', 420, 600);+ typing('\b!', 600, 2400);+ tour.push([{ hover: 'screen' }, 300], [function (fx) { refresh(fx); }, 1200], [{ hover: null }, 800]);++ window.SCENE = {+ page: {+ kicker: 'Inside a computer · a live figure', figno: 'Slow motion · typical times',+ title: 'What happens when you *press a key*',+ dek: 'From your finger to light on the screen: a code, a cable, an interrupt, a buffer, a frame.',+ footL: 'Simplified and illustrative: real machines add layers (drivers, compositor) and vary.', footR: '', steps: { at: [112, 430], width: 500 },+ },+ world: { scale: S, origin: [847, 305] },+ leaderCost: 5,+ onFrame: onFrame,+ items: [+ // the computer: a board with the CPU, the OS input queue, the text editor, RAM and the GPU+ { id: 'board', part: 'plate', at: [37, 1, -1.2], size: [28, 28, 1.2], grid: 4, faces: 'coolFaces', build: -3, lift: 0 },+ { kind: 'floortext', at: [41, 28.6, 0], text: 'COMPUTER', at0: -3, size: 42 },+ box('port', [37.4, 18.5, 0], [2.2, 3, 1.4], { faces: 'padFaces' }),+ node('cpu', 'core', 45, 20, { scale: 0.95, r: 3.6 }),+ { id: 'queue', part: 'queue', at: [50.5, 23.6, 0], n: 4, slot: 2.2, gap: 0.5, along: 'x', build: -3, lift: 0 },+ node('editor', 'hub', 59.5, 15.5, { scale: 0.95, r: 3.3 }),+ box('ram', [53, 4, 0], [7, 4.6, 0.5], { faces: 'padFaces', shapes: [0, 1, 2].map(function (j) { return { kind: 'box', at: [53.5, 4.6 + j * 1.4, 0.5], size: [6, 0.5, 2.8], bevel: 0 }; }) }),+ box('gpu', [40.5, 4.5, 0], [9, 1.8, 4.2], { bevel: 0.3, shapes: [{ kind: 'fan', face: 'y+', plane: 6.3, c: [42.9, 2.1], R: 1.6, spin: function (t) { return t * 2.2; } },+ { kind: 'fan', face: 'y+', plane: 6.3, c: [47.1, 2.1], R: 1.6, spin: function (t) { return t * 2.2 + 1; } }] }),+ // the keyboard: a slab with its controller at the back (the keys are drawn in onFrame, each on its own)+ box('keyboard', [-0.5, 27.4, 0], [34.6, 15.4, KZ], { bevel: 0.3 }),+ box('ctrl', [26, 28, KZ], [3.4, 2.2, 0.5], { bevel: 0, faces: 'padFaces' }),+ // the screen: a panel on a stand (its face is drawn in onFrame)+ box('screen', [MON.x0, MON.y, MON.z0], [MON.x1 - MON.x0, MON.d, MON.z1 - MON.z0], { bevel: 0, footprint: [20, -5, 8, 5],+ shapes: [{ kind: 'box', at: [20, -5, 0], size: [8, 5, 0.4], bevel: 0.3 }, { kind: 'box', at: [23.2, -3.4, 0.4], size: [1.6, 1, 3], bevel: 0 }],+ ports: { in: { at: [14, 0.6, -3.1], face: 'top' } } }),+ // the wires: USB from the keyboard, the path on the board, the display cable to the screen+ link('usb', 'ctrl.top', 'port.top', { layer: null, w: 3.4, via: [[27.7, 27.1, 0.3], [27.7, 24.5, 0.05], [34.5, 20, 0.05]] }),+ link('p2c', 'port.bottom', 'cpu.center'), link('c2q', 'cpu.center', 'queue.slot1'), link('q2e', 'queue.slot4', 'editor.center'),+ link('e2r', 'editor.center', 'ram.bottom'), link('r2g', 'ram.bottom', 'gpu.bottom'),+ link('g2s', 'gpu.bottom', 'screen.in', { w: 3.4, via: [[38.5, 5.4, 0], [31, -0.5, 0]] }),+ ],+ tokens: toks,+ labels: [+ { for: 'keyboard', anchor: [6, 42.8, 0.5], at: -3, title: 'Keyboard' },+ { for: 'ctrl', at: -3, title: 'Controller', elbow: [-20, -140] },+ { for: 'cpu', at: -3, title: 'CPU' },+ { for: 'queue', at: -3, title: 'OS input queue', elbow: [-170, 40] },+ { for: 'editor', at: -3, title: 'Text editor', elbow: [40, 100] },+ { for: 'ram', at: -3, title: 'RAM', elbow: [60, -70] },+ { for: 'gpu', at: -3, title: 'GPU', elbow: [30, -95] },+ { for: 'screen', at: -3, title: 'Screen' },+ ],+ tags: [+ // focus: true: the board's words stay while a part is hovered (typing with a hand on the mouse)+ { id: 'code', focus: true, for: 'ctrl', text: 'HID usage id', at: -3, tone: 'mute', off: [0, -60] },+ { id: 'usb', focus: true, pipe: 'usb', text: 'USB', at: -3, tone: 'mute', off: [10, 58] },+ { id: 'irq', focus: true, for: 'cpu', text: 'IRQ', at: 1e9, tone: 'warm', off: [-70, -40], align: 'right' },+ { id: 'buffer', focus: true, for: 'ram', text: '""', at: -3, tone: 'mute', off: [55, 18], align: 'left' },+ { id: 'screen', focus: true, for: 'screen', text: onScreen(''), at: -3, tone: 'mute', off: [-170, 150], align: 'right' },+ ],+ camera: [{ at: -10, dur: 0.01, zoom: 0.86, focus: [960, 560], to: [1205, 585] }],++ interactive: {+ film: 'tour',+ kicker: 'Live figure', title: 'What happens when you press a key',+ sub: 'Type on your keyboard (or click a key): watch the key go down, its code travel, the CPU take an interrupt, and the screen draw it. In slow motion.',+ source: 'Simplified and illustrative: real machines add layers (drivers, compositor) and vary.',+ camera: [{ at: -10, dur: 0.01, zoom: 1.0, focus: [960, 560], to: [960, 560] }],+ crop: { desk: [150, 40, 1770, 1040], phone: [290, 110, 1710, 1000] },+ type: { hint: 'Type on it', run: function (fx, ch, state) { typeKey(fx, ch, state); } },+ parts: [+ { id: 'keyboard', name: 'Keyboard', sub: 'keys over a grid of wires; click a key to type it', press: clickKey },+ { id: 'ctrl', name: 'Keyboard controller', sub: 'finds the key, sends its USB code' },+ { id: 'cpu', name: 'CPU', sub: 'takes the interrupt, runs the driver', press: function (fx) {+ fx.pulse('port', 'warm'); fx.pulse('cpu', 'warm', { delay: 0.15, dur: 0.9, amount: 0.7 });+ sayAt(fx, 'irq', 'IRQ: keyboard', 'warm', fx.tau); sayAt(fx, 'irq', null, null, fx.tau + 1.6);+ fx.tone('p2c', 'warm'); fx.tone('p2c', null, { delay: 1.4 }); fx.tone('usb', 'warm'); fx.tone('usb', null, { delay: 1.4 });+ } },+ { id: 'queue', name: 'OS input queue', sub: 'input events wait here in order' },+ { id: 'editor', name: 'Text editor', sub: 'the app: puts the character in its text' },+ { id: 'ram', name: 'RAM', sub: 'holds the text (click: its bytes)', press: function (fx) {+ M.bytes = fx.tau; fx.pulse('ram', 'cold');+ sayAt(fx, 'buffer', bytes(M.text), 'cold', fx.tau); sayAt(fx, 'buffer', buffer(M.text), 'cold', fx.tau + 2.5, 'restore');+ } },+ { id: 'gpu', name: 'GPU', sub: 'draws the glyphs into the next frame', press: function (fx) {+ var vs = Math.ceil((fx.tau + GPUT) / FRAME) * FRAME;+ fx.pulse('gpu', 'warm', { dur: GPUT + 0.3 }); fx.send('pk' + (M.k++ % NPK), [{ at: vs - fx.tau, dur: HOP, along: 'g2s', off: [0, 0, 0.4] }]);+ insertFrame({ at: vs + HOP, text: M.text });+ } },+ { id: 'screen', name: 'Screen', sub: SCREEN_HZ + ' new frames a second', press: refresh },+ ],+ knobs: { poll: { label: 'USB polling rate', hint: 'how often the computer asks the keyboard (typical rates)', options: POLL, default: POLLDEF } },+ reset: function () {},+ steps: [+ { id: 'path', kicker: '1 · Type on it', text: 'Press a key: it goes down, the keyboard finds it, and its code travels to the screen. Slowed down about 30 times.',+ run: function (fx) {+ ['keyboard', 'ctrl', 'port', 'cpu', 'queue', 'editor', 'ram', 'gpu', 'screen'].forEach(function (id, j) { fx.pulse(id, 'cold', { delay: 0.2 + j * 0.22, dur: 0.8, amount: 0.45 }); });+ } },+ { id: 'poll', kicker: '2 · The computer asks', knob: 'poll',+ text: function (s) { var o = POLL.filter(function (p) { return p[0] === String(s.poll); })[0] || POLL[2]; return 'Over USB the keyboard speaks only when asked: ' + o[1] + ' (' + o[0] + ' Hz). A pressed key waits for the next ask.'; },+ run: function (fx) {+ var hz = +fx.state.poll; M.poll.push([fx.tau, hz]); if (M.poll.length > 50) M.poll.shift();+ sayAt(fx, 'usb', 'USB · ' + hz + ' Hz', 'cold', fx.tau); fx.pulse('port', 'cold');+ } },+ { id: 'budget', kicker: '3 · Where the time goes', text: 'On this simple path a key takes about 30 ms to reach your eyes; waiting for the screen costs more than USB.',+ run: function (fx) { fx.pulse('screen', 'warm', { delay: 0.2, dur: 1.2 }); fx.pulse('keyboard', 'warm', { delay: 0.2, dur: 1.2 }); } },+ ],+ readouts: function (s, step) {+ var hz = +s.poll, poll = 1000 / hz, typical = MS.scan + poll / 2 + MS.os + MS.app + MS.gpu + 1000 / SCREEN_HZ / 2 + MS.show;+ var r1 = function (v) { return (Math.round(v * 10) / 10).toFixed(1); };+ if (step === 'budget') return { rows: [+ { part: 'keyboard', k: 'Scan + debounce', v: MS.scan + ' ms' },+ { part: 'ctrl', k: 'Wait for the poll', v: r1(poll / 2) + ' ms', u: 'on average, up to ' + r1(poll) + ' at ' + hz + ' Hz' },+ { part: 'cpu', k: 'USB, interrupt, OS', v: MS.os + ' ms' },+ { part: 'editor', k: 'The editor', v: MS.app + ' ms' },+ { part: 'gpu', k: 'The GPU draws', v: MS.gpu + ' ms' },+ { part: 'screen', k: 'Wait for a refresh', v: r1(1000 / SCREEN_HZ / 2) + ' ms', u: 'on average, at ' + SCREEN_HZ + ' Hz' },+ { part: 'screen', k: 'The screen shows it', v: MS.show + ' ms' },+ { part: 'screen', k: 'Total, about', v: Math.round(typical) + ' ms', tone: 'cold' },+ ], note: 'Typical, illustrative numbers for a simple path. Measured machines often take longer (compositors, buffering).' };+ var L0 = M.last, k = L0 && L0.k;+ return { rows: [+ { part: 'keyboard', k: 'Keys pressed', v: String(M.count) },+ { part: 'ctrl', k: L0 ? 'Last key: ' + (k ? (NAME[k.key] || L0.ch) : L0.ch) : 'Last key', v: k ? hex(k.code) : '—', u: k ? 'USB HID usage id' + (k.shift ? ', with Shift (modifier 0x02)' : '') : 'type something', tone: 'cold' },+ { part: 'ctrl', k: 'It waited for the poll', v: L0 ? r1(L0.wait) + ' ms' : '—', u: 'up to ' + r1(poll) + ' ms at ' + hz + ' Hz' },+ { part: 'screen', k: 'Press to screen, about', v: Math.round(typical) + ' ms', u: 'typical for this simple path; illustrative', tone: 'cold' },+ ], note: 'Slow motion: 1 ms is drawn as ' + SLOW + ' ms. Typical, illustrative times.' };+ },+ tour: tour,+ },+ };+})();diff --git a/skills/showtime/templates/diagram/examples/typewriter.js b/skills/showtime/templates/diagram/examples/typewriter.jsnew file mode 100644index 0000000..56141aa--- /dev/null+++ b/skills/showtime/templates/diagram/examples/typewriter.js@@ -0,0 +1,628 @@+/* "Type on it": a manual typewriter, inside, as a LIVE figure you type on (a generic machine, drawn; not a specific+ model). Every key the viewer presses goes down and lights, a packet runs along its lever to its typebar, the bar+ swings up out of the basket and strikes the print point through the lifted ribbon, the letter lands on the paper,+ the escapement lets go one tooth and the carriage steps one letter left. Space only steps; Backspace moves the+ carriage back one letter (the letter stays on the paper, as on a real machine); Return (Enter, or a click on the+ return lever) runs the carriage home against the mainspring and turns the platen up one line. Five characters+ before the margin the bell rings; at the margin the keys lock until Return. Capitals and the symbols over the+ digits take Shift: the segment drops so the upper character on the type slug strikes.++ How it is built: every keystroke is an event {t, kind, ch} in one log (seconds on the live clock); the drawing is+ a pure function of t and that log, so fast typing stacks (several bars in flight, each on its own time) and the+ film (index.html, the tour on the film's clock: interactive.film = 'tour') draws exactly what the live page does.+ The machine's drawing is the showtime X-ray library's typewriter (library/xray/typewriter/diagram.js), with a+ digit row, a backspace key, a bell and a return lever added. The line length is measured off the drawing: the+ paper's writable width (15 units, margins of 1.5) over the carriage's step (0.52) is 28 letters.+ The live page is figure.html (`showtime export html <project> --interactive`).+ To use it: `showtime new diagram <dir> --example typewriter --theme white`. */+var CUE = {+ duration: 25.2,+};++(function () {+ 'use strict';+ var C30 = Math.cos(Math.PI / 6), deg = function (a) { return a * Math.PI / 180; };+ var clamp = function (x, a, b) { return Math.min(b, Math.max(a, x)); };+ var lerp = function (a, b, p) { return a + (b - a) * p; };+ var inOutSine = function (p) { return -(Math.cos(Math.PI * p) - 1) / 2; };+ var inOutCubic = function (p) { return p < 0.5 ? 4 * p * p * p : 1 - Math.pow(-2 * p + 2, 3) / 2; };+ var outCubic = function (p) { return 1 - Math.pow(1 - p, 3); };++ // ---------------------------------------------------------------- the machine, in world units (x: its width, left+ // to right; y: back to front, the typist at +y; z: up). The X-ray library's typewriter, plus a digit row.+ var ROWS = [['1234567890-', 5.8, 21.5, 5.4], ['qwertyuiop', 7.0, 24.5, 4.6], ['asdfghjkl;', 8.2, 27.5, 3.8], ['zxcvbnm,./', 9.4, 30.5, 3.0]];+ var UPPER = { '1': '!', '2': '"', '3': '#', '4': '$', '5': '%', '6': '_', '7': '&', '8': "'", '9': '(', '0': ')', '-': '*', ';': ':', ',': ',', '.': '.', '/': '?' };+ var PITCH = 3.2, KR = 1.15;+ var KEYS = []; // [ch, x, y, z]+ ROWS.forEach(function (r) { r[0].split('').forEach(function (ch, i) { KEYS.push([ch, r[1] + i * PITCH, r[2], r[3]]); }); });+ var KEY = {}; KEYS.forEach(function (k) { KEY[k[0]] = k; });+ var BYX = KEYS.slice().sort(function (a, b) { return a[1] - b[1] || a[2] - b[2]; }), NB = BYX.length, BAR = {};+ BYX.forEach(function (k, i) { BAR[k[0]] = i; });+ var SLUG = {}; // the character typed -> [the key it is on, shifted]+ KEYS.forEach(function (k) {+ var c = k[0], up = /[a-z]/.test(c) ? c.toUpperCase() : UPPER[c];+ SLUG[c] = [c, false]; if (up && !SLUG[up]) SLUG[up] = [c, true];+ });+ var PLATEN = { x0: 7, x1: 37, y: 7, z: 8.6, r: 1.7 };+ var PP = [22, PLATEN.y + PLATEN.r, PLATEN.z]; // the print point: the front of the platen+ var SEG = { r: 4.6, z: 4.8 }; // the segment: an arc of pivots around the print point+ var LB = Math.hypot(SEG.r, PP[2] - SEG.z); // a typebar's length (pivot to type)+ var TH_REST = deg(-8), TH_HIT = Math.atan2(PP[2] - SEG.z, -SEG.r);+ var SHIFT_DROP = 0.5; // the segment's drop for the upper case+ var SPACE = { at: [13, 33.0, 1.8], size: [18, 1.7, 0.7] };+ var SHIFTK = [5.6, 30.5, 3.0], BACKK = [2.6, 21.5, 5.4];+ var ESC = [38.6, 0.9, 7.7]; // the escapement star wheel (its top face), 16 teeth+ var SPRING = [4.6, 12.4, 4.4]; // the mainspring drum (its top face)+ var SPOOLS = [[14.2, 10.8, 6.7], [29.8, 10.8, 6.7]];+ var BELL = [40.4, 12.6, 0.8]; // the bell, on the base under the carriage's right end+ // the line, measured off the drawing: the paper spans x 13..31 at rest; margins of 1.5 leave 15 units; a step is+ // 0.52, so 28 letters; at home the left margin sits on the print point (the carriage 7.5 to the right)+ var STEP = 0.52, PAPER = [13, 31], MARGIN = 1.5;+ var N = Math.floor((PAPER[1] - PAPER[0] - 2 * MARGIN) / STEP + 1e-9), BELL_AT = N - 5;+ var HOME = PP[0] - (PAPER[0] + MARGIN);+ var LF = 0.95, Z0 = 10.75; // the line feed (along the paper) and the typing line+ // times, in seconds from the moment the page hands a keystroke over (fx.tau, already 0.05 s after the key went down)+ var PK = 0.05, RISE = 0.09, FALL = 0.24, STEPD = 0.11, LIFE = 1.2;++ function barAngle(i) { return deg(168 - 156 * i / (NB - 1)); }+ function radial(i) { var a = barAngle(i); return [Math.cos(a), Math.sin(a)]; }+ function pivot(i, dz) { var d = radial(i); return [PP[0] + SEG.r * d[0], PP[1] + SEG.r * d[1], SEG.z - (dz || 0)]; }+ function tip(i, s, dz) { // the type end: s = 0 at rest, 1 on the platen+ var p = pivot(i, dz), d = radial(i), th = lerp(TH_REST, TH_HIT, s);+ return [p[0] + LB * Math.cos(th) * d[0], p[1] + LB * Math.cos(th) * d[1], p[2] + LB * Math.sin(th)];+ }+ function stemFoot(k) { return [k[1], k[2], k[3] - 2.2]; }+ function lever(k, dz) { var pv = pivot(BAR[k[0]], dz); return [stemFoot(k), [pv[0], pv[1] + 1.4, 1.5], [pv[0], pv[1], pv[2] - 0.25]]; }+ function keyId(ch) { return 'k-' + ({ ',': 'comma', '.': 'period', '-': 'dash', ';': 'semi', '/': 'slash' }[ch] || ch); }+ function paperY(z) { return 6.1 - (z - 10.1) * 0.255; }++ // a horizontal cylinder along x, as faces (the library's cylinder stands on z): the body's outline and the x+ cap+ function hull2(pts) {+ var p = pts.map(function (q, i) { return [q[0], q[1], i]; }).sort(function (a, b) { return a[0] - b[0] || a[1] - b[1]; });+ var cross = function (o, a, b) { return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]); }, lo = [], up = [], i;+ for (i = 0; i < p.length; i++) { while (lo.length >= 2 && cross(lo[lo.length - 2], lo[lo.length - 1], p[i]) <= 0) lo.pop(); lo.push(p[i]); }+ for (i = p.length - 1; i >= 0; i--) { while (up.length >= 2 && cross(up[up.length - 2], up[up.length - 1], p[i]) <= 0) up.pop(); up.push(p[i]); }+ up.pop(); lo.pop(); return lo.concat(up).map(function (q) { return q[2]; });+ }+ function hcyl(x0, x1, y, z, r, body, cap) {+ var circ = function (x, rr) { var a = []; for (var k = 0; k < 36; k++) { var th = k * Math.PI / 18; a.push([x, y + rr * Math.cos(th), z + rr * Math.sin(th)]); } return a; };+ var both = circ(x0, r).concat(circ(x1, r)), scr = both.map(function (p) { return [(p[0] - p[1]) * C30, (p[0] + p[1]) * 0.5 - p[2]]; });+ var out = hull2(scr).map(function (i) { return both[i]; });+ return [{ kind: 'face', pts: out, tone: body || 'left' }, { kind: 'face', pts: circ(x1, r), tone: cap || 'top' },+ { kind: 'face', pts: circ(x1, r * 0.42), tone: 'recess', lw: 0.6, stroke: 'detail' },+ { kind: 'line', pts: [[x0 + 0.3, y + r * 0.5, z + r * 0.87], [x1 - 0.1, y + r * 0.5, z + r * 0.87]], lw: 0.5 }];+ }++ // ---------------------------------------------------------------- where it sits: fitted into the 1920x1080 stage,+ // the carriage's two ends (home, and the margin) and the paper's top included+ var R = [200, 170, 1720, 1030], pts = [];+ [[1, 0, 0], [43, 36, 0], [1, 36, 0], [43, 0, 0], [1, 0, 10.6], [43, 0, 10.6], [-3, 4.3, 12.8], [47.4, 8.7, 6.9],+ [5.9, 4.8, 15.2], [38.5, 4.8, 15.2], [31, 36, 1.8]].forEach(function (p) { pts.push([(p[0] - p[1]) * C30, (p[0] + p[1]) * 0.5 - p[2]]); });+ var bx = pts.map(function (c) { return c[0]; }), by = pts.map(function (c) { return c[1]; });+ var bx0 = Math.min.apply(null, bx), bx1 = Math.max.apply(null, bx), by0 = Math.min.apply(null, by), by1 = Math.max.apply(null, by);+ var S = Math.min((R[2] - R[0]) / (bx1 - bx0), (R[3] - R[1]) / (by1 - by0));+ var O = [Math.round((R[0] + R[2]) / 2 - (bx0 + bx1) / 2 * S), Math.round((R[1] + R[3]) / 2 - (by0 + by1) / 2 * S)];++ // ---------------------------------------------------------------- the parts+ var items = [+ { id: 'base', layer: 'floor', shapes: [{ kind: 'box', at: [1, 0, 0], size: [42, 36, 0.8], tone: 'padFaces' }], shadow: false },+ { id: 'cover', layer: 'floor', shapes: [{ kind: 'ghost', at: [1, 0, 0.8], size: [42, 36, 9.6] }] },+ { id: 'side-l', shapes: [{ kind: 'box', at: [1, 17, 0.8], size: [1.1, 17, 4.2], tone: 'wallFaces' }, { kind: 'hatch', at: [1, 17, 0.8], size: [1.1, 17, 4.2], endFace: true }],+ bbox: [[1, 17, 0.8], [2.1, 34, 5]] },+ { id: 'side-r', shapes: [{ kind: 'box', at: [41.9, 17, 0.8], size: [1.1, 17, 4.2], tone: 'wallFaces' }, { kind: 'hatch', at: [41.9, 17, 0.8], size: [1.1, 17, 4.2], endFace: true }],+ bbox: [[41.9, 17, 0.8], [43, 34, 5]] },+ { id: 'post-l', part: 'box', at: [3.0, 2.6, 0.8], size: [1.2, 1.4, 5.4], bevel: 0 },+ { id: 'post-r', part: 'box', at: [39.8, 2.6, 0.8], size: [1.2, 1.4, 5.4], bevel: 0 },+ { id: 'rail', part: 'box', at: [2.4, 2.4, 6.2], size: [39.2, 1.8, 0.9], bevel: 0, faces: 'padFaces' },+ ];+ // the carriage (its rack), the platen with its knobs, the paper, the return lever: they ride together+ var rack = [{ kind: 'box', at: [7, 4.3, 7.15], size: [30, 0.5, 0.45], bevel: 0, tone: 'padFaces' }];+ for (var rk = 0; rk < 30; rk++) rack.push({ kind: 'line', pts: [[7.5 + rk, 4.3, 7.4], [7.9 + rk, 4.3, 7.15]], lw: 0.5 });+ items.push({ id: 'carriage', shapes: rack, bbox: [[7, 4.3, 7.15], [37, 4.8, 7.6]], front: ['rail'] });+ items.push({ id: 'paper', shapes: [{ kind: 'face', pts: [[PAPER[0], 6.1, 10.1], [PAPER[1], 6.1, 10.1], [PAPER[1], 4.8, 15.2], [PAPER[0], 4.8, 15.2]], tone: 'blade' }],+ bbox: [[PAPER[0], 4.8, 10.1], [PAPER[1], 6.1, 15.2]], behind: ['platen'] });+ items.push({ id: 'platen', shapes: hcyl(4.4, 7, PLATEN.y, PLATEN.z, 1.25, 'right', 'top').concat(hcyl(PLATEN.x0, PLATEN.x1, PLATEN.y, PLATEN.z, PLATEN.r, 'left', 'top'),+ hcyl(PLATEN.x1, 39.6, PLATEN.y, PLATEN.z, 1.25, 'right', 'top')), bbox: [[4.4, 5.3, 6.9], [39.6, 8.7, 10.3]], front: ['carriage'] });+ items.push({ id: 'retlever', shapes: [{ kind: 'line', pts: [[4.2, 7.6, 9.6], [2.6, 8.4, 11.6], [0.4, 8.8, 12.4]], lw: 2.6, stroke: 'ink' },+ { kind: 'box', at: [-0.9, 8.3, 12.1], size: [1.6, 1.0, 0.5], bevel: 0.15 }], bbox: [[-0.9, 7.6, 9.4], [4.3, 9.3, 12.6]] });+ SPOOLS.forEach(function (p, i) {+ items.push({ id: 'spool-' + 'lr'[i], shapes: [{ kind: 'cyl', at: [p[0], p[1], 0.8], r: 0.35, h: p[2] - 1.5 }, { kind: 'cyl', at: [p[0], p[1], p[2] - 0.7], r: 1.9, h: 0.7, flat: true }],+ bbox: [[p[0] - 1.9, p[1] - 1.9, 0.8], [p[0] + 1.9, p[1] + 1.9, p[2]]], footprint: [p[0] - 0.6, p[1] - 0.6, 1.2, 1.2] });+ });+ // the ribbon (between the spools, through the vibrator: drawn by onFrame), for hovering+ items.push({ id: 'ribbon', shapes: [], bbox: [[16.1, 8.9, 6.3], [27.9, 10.6, 7.6]], shadow: false });+ items.push({ id: 'esc-arbor', shapes: [{ kind: 'box', at: [ESC[0] - 0.5, ESC[1] - 0.5, 0.8], size: [1.0, 1.0, ESC[2] - 1.15], bevel: 0 }], bbox: [[ESC[0] - 0.5, ESC[1] - 0.5, 0.8], [ESC[0] + 0.5, ESC[1] + 0.5, ESC[2] - 0.35]] });+ items.push({ id: 'escapement', shapes: [{ kind: 'cyl', at: [ESC[0], ESC[1], ESC[2] - 0.35], r: 1.8, h: 0.35, flat: true }], bbox: [[ESC[0] - 1.8, ESC[1] - 1.8, ESC[2] - 0.35], [ESC[0] + 1.8, ESC[1] + 1.8, ESC[2]]], front: ['esc-arbor'] });+ items.push({ id: 'mainspring', shapes: [{ kind: 'cyl', at: [SPRING[0], SPRING[1], 0.8], r: 2.1, h: SPRING[2] - 0.8, flat: true, bands: [1.1] }],+ footprint: [SPRING[0] - 2.1, SPRING[1] - 2.1, 4.2, 4.2] });+ items.push({ id: 'bell', shapes: [{ kind: 'cyl', at: [BELL[0], BELL[1], BELL[2]], r: 0.3, h: 1.0 }, { kind: 'cyl', at: [BELL[0], BELL[1], BELL[2] + 1.0], r: 1.5, h: 1.1, cap: 0.55 }],+ bbox: [[BELL[0] - 1.5, BELL[1] - 1.5, BELL[2]], [BELL[0] + 1.5, BELL[1] + 1.5, BELL[2] + 2.1]], footprint: [BELL[0] - 1.3, BELL[1] - 1.3, 2.6, 2.6] });+ // the parts onFrame draws (typebars, segment, print point, key levers): empty items that say where they are+ items.push({ id: 'segment', shapes: [], bbox: [[PP[0] - 4.8, PP[1] - 0.3, 4.2], [PP[0] + 4.8, PP[1] + 4.6, 5.4]], shadow: false });+ items.push({ id: 'basket', shapes: [], bbox: [[PP[0] - 10.5, PP[1] + 4.6, 3.6], [PP[0] + 10.5, PP[1] + 10.5, 5.4]], shadow: false, front: ['rail', 'carriage'] });+ items.push({ id: 'pp', shapes: [], bbox: [[PP[0] - 0.7, PP[1] - 0.3, PP[2] - 0.7], [PP[0] + 0.7, PP[1] + 0.4, PP[2] + 0.7]], shadow: false });+ items.push({ id: 'levers', shapes: [], bbox: [[6, 19.3, 0.9], [38, 20.4, 2.4]], shadow: false });+ // the keyboard: one part per key (a cap on a stem), the shift and backspace keys and the space bar+ var cap = function (id, p, r) {+ items.push({ id: id, shapes: [{ kind: 'line', pts: [[p[0], p[1], p[2]], [p[0], p[1], p[2] - 2.2]], lw: 0.9, stroke: 'ink' }, { kind: 'cyl', at: p, r: r, h: 0.5, cap: 0.68 }],+ bbox: [[p[0] - r, p[1] - r, p[2] - 2.2], [p[0] + r, p[1] + r, p[2] + 0.5]] });+ };+ KEYS.forEach(function (k) { cap(keyId(k[0]), [k[1], k[2], k[3]], KR); });+ cap('k-shift', SHIFTK, KR * 1.15); cap('k-back', BACKK, KR);+ items.push({ id: 'k-space', part: 'box', at: SPACE.at, size: SPACE.size, bevel: 0.25, shadow: false });+ items.push({ id: 'keyboard', shapes: [], bbox: [[1.4, 20.3, 0.8], [39.4, 31.7, 5.9]], shadow: false });++ // ================================================================ the typed log: every keystroke, in time+ var LOG = [];+ var tail = null;+ function fresh() { return { col: 0, line: 1, typed: 0, strikes: 0, cells: [], above: [] }; }+ function lineText(cells) { var s = ''; for (var i = 0; i < cells.length; i++) { var c = cells[i]; s += c ? c.charAt(c.length - 1) : ' '; } return s.replace(/\s+$/, ''); }+ // one event on the model: it fills in what the drawing needs (columns before and after, the paper after it)+ function apply(e, st) {+ e.col0 = st.col; e.line0 = st.line;+ if (e.kind === 'ch' || e.kind === 'space') {+ if (st.col >= N) e.kind = e.kind === 'ch' ? 'lockch' : 'lock'; // the margin: the keys are locked+ else {+ if (e.kind === 'ch') { st.cells = st.cells.slice(); st.cells[st.col] = (st.cells[st.col] || '') + e.ch; st.typed++; st.strikes++; }+ else st.typed++;+ st.col++;+ e.bell = st.col === BELL_AT;+ }+ } else if (e.kind === 'back') { st.col = Math.max(0, st.col - 1); }+ else if (e.kind === 'ret') { st.above = [st.cells].concat(st.above).slice(0, 5); st.cells = []; st.col = 0; st.line++; }+ e.col1 = st.col; e.line = st.line; e.typed = st.typed; e.strikes = st.strikes;+ e.cells = st.cells; e.above = st.above;+ e.mv = e.kind === 'ret' ? { at: e.t + 0.04, dur: Math.min(0.75, 0.22 + 0.016 * e.col0) } : { at: e.t + PK, dur: STEPD };+ return e;+ }+ function replay() { tail = fresh(); LOG.forEach(function (e) { apply(e, tail); }); }+ replay();+ // a keystroke typed now drops what a step had scheduled after now (a demo word stops mid-word)+ function dropAfter(fx) {+ var n = LOG.length; while (LOG.length && LOG[LOG.length - 1].demo && LOG[LOG.length - 1].t > fx.tau + 1e-6) LOG.pop();+ if (LOG.length === n) return;+ replay();+ fx.hide('ding'); fx.hide('note');+ var last = LOG[LOG.length - 1], txt = lineText(tail.cells);+ var d = last ? Math.max(0, last.t + PK - fx.tau) : 0;+ if (txt || tail.line > 1) fx.say('line', txt ? txt.replace(/ /g, '\u00a0') : '\u21b5 line ' + tail.line, 'mute', { delay: d }); else fx.hide('line', { delay: d });+ }+ var audio = null;+ // one keystroke into the log at fx.tau + delay; the page's tags follow it (the typed line, the bell, the margin)+ var demo = false; // true while a step or the tour types (those keystrokes may be dropped)+ function add(fx, kind, ch, delay) {+ var last = LOG[LOG.length - 1], t = fx.tau + (fx.reduced ? 0 : (delay || 0));+ if (last && t <= last.t) t = last.t + 0.001;+ var e = apply({ t: t, kind: kind, ch: ch, demo: demo }, tail);+ if (kind === 'ch' && e.kind === 'ch') { var sl = SLUG[ch]; e.key = sl[0]; e.up = sl[1]; e.bar = BAR[sl[0]]; }+ if (kind === 'ch' && e.kind === 'lockch') { var sl2 = SLUG[ch]; e.key = sl2[0]; e.up = sl2[1]; }+ LOG.push(e);+ var at = { delay: e.t - fx.tau + PK }, txt = lineText(e.cells);+ if (e.kind === 'ch' || e.kind === 'ret') {+ // a new line shows its number at once (no fade) until its first letter lands+ fx.say('line', txt ? txt.replace(/ /g, '\u00a0') : '\u21b5 line ' + e.line, 'mute', at);+ }+ if (e.bell) { fx.say('ding', 'ding! ' + (N - BELL_AT) + ' to the margin', 'warm', { delay: e.t - fx.tau + PK + 0.06 }); fx.hide('ding', { delay: e.t - fx.tau + PK + 2.4 }); }+ if (e.kind === 'lock' || e.kind === 'lockch') { fx.say('note', 'margin: keys locked, press Return', 'hot', at); fx.hide('note', { delay: e.t - fx.tau + 1.8 }); }+ if (e.kind === 'ret') fx.hide('note', at);+ if (audio) audio(e, e.t - fx.tau + PK);+ return e;+ }+ function demoLine() { // the typing line holds only what a step or the tour typed+ if (!tail.col) return false;+ for (var i = LOG.length - 1; i >= 0 && LOG[i].kind !== 'ret'; i--) if (!LOG[i].demo) return false;+ return true;+ }+ function none(fx, ch) { // a character with no type on this machine: the fan stirs, nothing prints+ var e = add(fx, 'none', ch, 0);+ fx.say('note', 'no type for "' + ch + '" on this machine', 'mute', { delay: PK }); fx.hide('note', { delay: 1.6 });+ return e;+ }+ function keystroke(fx, ch, delay) {+ if (ch === '\n' || ch === '\r') return add(fx, 'ret', null, delay);+ if (ch === '\b') return add(fx, 'back', null, delay);+ if (ch === ' ' || ch === '\t' || ch === '\u00a0') return add(fx, 'space', ' ', delay);+ if (SLUG[ch]) return add(fx, 'ch', ch, delay);+ return none(fx, ch);+ }+ function word(fx, s, from, gap) { // a step's demo: typed at a steady pace from `from` seconds+ s.split('').forEach(function (ch, i) { keystroke(fx, ch, from + i * gap); });+ return from + s.length * gap;+ }+ function sync(state) { state.typed = tail.typed; state.col = tail.col; state.line = tail.line; state.text = lineText(tail.cells); }++ // ---- the sound (live page only, after the viewer typed: a short synthesized clack, the bell's ding; no file)+ var live = typeof document !== 'undefined' && document.documentElement.hasAttribute('data-st-live');+ function sound() {+ if (!live || audio) return;+ var ua = typeof navigator !== 'undefined' && navigator.userActivation;+ if (!ua || !ua.isActive) return;+ try {+ var AC = window.AudioContext || window.webkitAudioContext; if (!AC) return;+ var ac = new AC(), out = ac.createGain(); out.gain.value = 0.5; out.connect(ac.destination);+ var noise = ac.createBuffer(1, Math.round(ac.sampleRate * 0.05), ac.sampleRate), d = noise.getChannelData(0), seed = 7;+ for (var i = 0; i < d.length; i++) { seed = (seed * 16807) % 2147483647; d[i] = (seed / 2147483647 * 2 - 1) * Math.pow(1 - i / d.length, 3); }+ audio = function (e, inS) {+ if (ac.state === 'suspended') ac.resume();+ var t0 = ac.currentTime + Math.max(0, inS);+ if (e.kind === 'ch' || e.kind === 'space') {+ var src = ac.createBufferSource(), bp = ac.createBiquadFilter(), g = ac.createGain();+ src.buffer = noise; bp.type = 'bandpass'; bp.frequency.value = e.kind === 'ch' ? 2400 : 900; bp.Q.value = 1.4;+ g.gain.value = e.kind === 'ch' ? 0.35 : 0.18; src.connect(bp); bp.connect(g); g.connect(out); src.start(t0);+ }+ if (e.bell) [[2093, 0.12], [5250, 0.04]].forEach(function (p) {+ var o = ac.createOscillator(), g2 = ac.createGain(); o.frequency.value = p[0];+ g2.gain.setValueAtTime(0.0001, t0 + 0.06); g2.gain.exponentialRampToValueAtTime(p[1], t0 + 0.07); g2.gain.exponentialRampToValueAtTime(0.0001, t0 + 1.4);+ o.connect(g2); g2.connect(out); o.start(t0 + 0.06); o.stop(t0 + 1.5);+ });+ };+ } catch (err) { audio = null; }+ }++ // ================================================================ the drawing at t: a pure function of t and LOG+ function lastAt(t) { var lo = 0, hi = LOG.length - 1, r = -1; while (lo <= hi) { var m = (lo + hi) >> 1; if (LOG[m].t <= t) { r = m; lo = m + 1; } else hi = m - 1; } return r; }+ var F = { t: NaN }; // what one frame needs, computed once per t+ function frame(t) {+ if (F.t === t) return F;+ var i1 = lastAt(t), i0 = lastAt(t - LIFE), col = i0 >= 0 ? LOG[i0].col1 : 0, strikes = i0 >= 0 ? LOG[i0].strikes : 0, win = [];+ for (var i = i0 + 1; i <= i1; i++) {+ var e = LOG[i], p = inOutCubic(clamp((t - e.mv.at) / e.mv.dur, 0, 1));+ col += (e.col1 - e.col0) * p;+ if (e.kind === 'ch') strikes += outCubic(clamp((t - e.t - PK) / 0.3, 0, 1));+ win.push(e);+ }+ // the next keystrokes (a key goes down 0.05 s before its event): rises that already started+ for (var j = i1 + 1; j < LOG.length && LOG[j].t - 0.06 <= t; j++) win.push(LOG[j]);+ var dips = {}, vi = lastAt(t - PK), shift = 0;+ win.forEach(function (e) {+ var down = inOutCubic(clamp((t - e.t + 0.05) / 0.05, 0, 1)) * (1 - inOutCubic(clamp((t - e.t - 0.09) / 0.12, 0, 1)));+ if (down <= 0) return;+ var id = e.kind === 'ch' || e.kind === 'lockch' ? keyId(e.key) : e.kind === 'space' || e.kind === 'lock' ? 'k-space' : e.kind === 'back' ? 'k-back' : null;+ if (id) dips[id] = Math.max(dips[id] || 0, down);+ if (e.up) { var sh = inOutCubic(clamp((t - e.t + 0.05) / 0.05, 0, 1)) * (1 - inOutCubic(clamp((t - e.t - PK - 0.1) / 0.1, 0, 1))); dips['k-shift'] = Math.max(dips['k-shift'] || 0, down); shift = Math.max(shift, sh); }+ });+ F = { t: t, col: col, cx: HOME - col * STEP, strikes: strikes, win: win, dips: dips, shift: shift, view: vi >= 0 ? LOG[vi] : null };+ return F;+ }+ var RIDES = { carriage: 1, platen: 1, paper: 1, retlever: 1 };+ function offset(id, t) {+ var f = frame(t);+ if (RIDES[id]) return [f.cx, 0, 0];+ var d = f.dips[id]; return d ? [0, 0, -(id === 'k-space' ? 0.5 : 0.7) * d] : null;+ }+ function env(t, e) { // 0..1, exactly 1 at the strike's peak (e.t + PK)+ var d = t - e.t - PK;+ if (d < -RISE || d > FALL) return 0;+ if (d <= 0) { var a = (d + RISE) / RISE; return a * a; }+ return 1 - inOutSine(d / FALL);+ }++ // three layers in the drawing: under the parts, over them (both redrawn every frame), and the letters (the type on+ // the paper, the legends on the caps): persistent <text> nodes whose attributes change, so the page keeps its nodes+ var NS = 'http://www.w3.org/2000/svg', gU = null, gO = null, gT = null, gKeys = null, gRows = [], pools = [], keyEls = [];+ var mkEl = function (tag, parent, attrs) { var e = document.createElementNS(NS, tag); Object.keys(attrs || {}).forEach(function (a) { e.setAttribute(a, attrs[a]); }); parent.appendChild(e); return e; };+ function groups() {+ var w = document.getElementById('ld-world');+ if (!w) return false;+ if (!gU || !gU.isConnected) { gU = document.createElementNS(NS, 'g'); gU.setAttribute('id', 'tw-under'); w.insertBefore(gU, w.firstChild); }+ if (!gO || !gO.isConnected) { gO = mkEl('g', w, { id: 'tw-over' }); if (gT && gT.isConnected) w.appendChild(gT); }+ if (!gT || !gT.isConnected) {+ gT = mkEl('g', w, { id: 'tw-type', 'data-st-decor': '', 'font-size': 1, 'font-weight': 600, 'text-anchor': 'middle' });+ var paper = mkEl('g', gT, {}); gRows = []; pools = [];+ for (var r = 0; r < 5; r++) { gRows.push(mkEl('g', paper, { 'data-paper-line': r, 'data-text': '' })); pools.push([]); }+ gKeys = mkEl('g', gT, {}); keyEls = [];+ KEYS.concat([['\u2190', BACKK[0], BACKK[1], BACKK[2], 'k-back'], ['\u21e7', SHIFTK[0], SHIFTK[1], SHIFTK[2], 'k-shift']]).forEach(function (k) {+ var e = mkEl('text', gKeys, {}); e.textContent = k[4] ? k[0] : /[a-z]/.test(k[0]) ? k[0].toUpperCase() : k[0];+ keyEls.push({ el: e, k: k, id: k[4] || keyId(k[0]) });+ });+ }+ return true;+ }+ // one letter laid on a plane: ex one world unit along the line, ey one unit "down" the glyph, size in world units+ function setGlyph(e, s, ex, ey, size, col, op) {+ e.setAttribute('transform', 'matrix(' + [ex[0] * size, ex[1] * size, ey[0] * size, ey[1] * size, s[0], s[1]].map(function (v) { return Math.round(v * 1000) / 1000; }).join(' ') + ')');+ e.setAttribute('fill', col); e.setAttribute('fill-opacity', op); e.setAttribute('visibility', 'visible');+ }+ var f1 = function (v) { return (Math.round(v * 10) / 10).toString(); }, f3 = function (v) { return (Math.round(v * 1000) / 1000).toString(); };+ var HID = ' stroke-dasharray="7 5"', CEN = ' stroke-dasharray="22 5 3 5"';+ function onFrame(t, k) {+ if (!groups()) return;+ var TH = (window.Diagram && Diagram.current && Diagram.current.theme) || {};+ var dark = /dark/.test(TH.name || '') || (TH.ground && /^#[0-3]/.test(TH.ground));+ var hotC = (TH.tones && TH.tones.hot && (TH.tones.hot.flow || TH.tones.hot.face)) || '#E8552B', warmC = (TH.tones && TH.tones.warm && (TH.tones.warm.flow || TH.tones.warm.face)) || '#D9A02B';+ var inkC = TH.ink || '#3B4656', detC = TH.detail || '#8A95A5', mono = (TH.font && TH.font.mono) || 'JetBrains Mono, monospace';+ var LW = TH.lw || 1.5;+ // the type on the paper: dark on a light sheet, the theme's ink on a dark one+ var blade = String((TH.faces && TH.faces.blade) || '#ffffff'), bl = /^#[0-9a-f]{6}$/i.test(blade) ? [1, 3, 5].reduce(function (a, i) { return a + parseInt(blade.substr(i, 2), 16); }, 0) / 765 : 1;+ var typeC = bl < 0.45 ? inkC : '#20242c';+ var P = k.P, f = frame(t), u = '', o = '';+ var fa = k.focus || { amount: 0 }, fc = fa.fc, veil = fa.amount || 0;+ var lit = function (id) { return !!fc && (fc.part === id || (fc.with || []).indexOf(id) >= 0); };+ // a piece drawn here for a part: over the veil when that part is in focus, dimmed with the rest otherwise+ var put = function (id, svg, under) {+ if (!svg) return;+ if (veil > 0.01 && lit(id)) { o += svg; return; }+ if (under) { u += svg; return; }+ o += veil > 0.01 ? '<g opacity="' + f3(1 - 0.6 * veil) + '">' + svg + '</g>' : svg;+ };+ var path = function (q) { return 'M' + q.map(function (p) { var s = P(p[0], p[1], p[2]); return f1(s[0]) + ' ' + f1(s[1]); }).join('L'); };+ var stroke = function (q, col, w, extra) { return '<path d="' + path(q) + '" fill="none" stroke="' + col + '" stroke-width="' + f1(w) + '" stroke-linecap="round" stroke-linejoin="round"' + (extra || '') + '/>'; };+ var dot = function (p, r, col, extra) { var s = P(p[0], p[1], p[2]); return '<circle cx="' + f1(s[0]) + '" cy="' + f1(s[1]) + '" r="' + f1(r) + '" fill="' + col + '"' + (extra || '') + '/>'; };+ var O0 = P(0, 0, 0), vx = P(1, 0, 0), ex = [vx[0] - O0[0], vx[1] - O0[1]];+ gT.setAttribute('font-family', mono);+ var shiftDz = SHIFT_DROP * f.shift;+ // per bar: its strike now (several bars may be in flight; the same bar struck twice keeps the stronger)+ var barS = {}, fan = 0, ribbon = 0, escHot = 0, levers = {}, flash = [], bells = [], ret = null;+ f.win.forEach(function (e) {+ var s = env(t, e), age = t - e.t;+ if (e.kind === 'ch') { barS[e.bar] = Math.max(barS[e.bar] || 0, s); ribbon = Math.max(ribbon, s); levers[e.key] = Math.max(levers[e.key] || 0, age < -0.06 ? 0 : 1 - clamp((age - PK) / 0.5, 0, 1)); }+ if (e.kind === 'none') fan = Math.max(fan, s);+ if (e.kind === 'ch' || e.kind === 'space' || e.kind === 'back') escHot = Math.max(escHot, s);+ if (e.kind === 'ch' && age >= PK && age < PK + 0.22) flash.push(age - PK);+ if (e.bell && age >= PK + 0.06 && age < PK + 1.1) bells.push(age - PK - 0.06);+ if (e.kind === 'ret' && age >= -0.05 && age < 0.9) ret = { age: age, e: e };+ });+ // hidden lines: every key lever, dashed, under the keyboard; the struck ones lit, a packet running along them+ KEYS.forEach(function (key) {+ var lv = levers[key[0]] || 0;+ if (lv <= 0.004) put('levers', stroke(lever(key, shiftDz), detC, LW * 0.8, HID), true);+ });+ // the segment (a heavy arc of pivots), the rest pad, the centre line through the print point+ var arc = [], pad = [];+ for (var a = 0; a <= 48; a++) {+ var th = deg(172 - 164 * a / 48);+ arc.push([PP[0] + SEG.r * Math.cos(th), PP[1] + SEG.r * Math.sin(th), SEG.z - shiftDz]);+ pad.push([PP[0] + (SEG.r + LB * 0.99) * Math.cos(th), PP[1] + (SEG.r + LB * 0.99) * Math.sin(th), SEG.z - LB * 0.16 - shiftDz]);+ }+ put('basket', stroke(pad, detC, LW * 3, ' stroke-opacity="0.9"'), true);+ put('segment', stroke([[PP[0], PP[1] + SEG.r + LB + 1.5, SEG.z], [PP[0], PP[1] - 0.6, SEG.z]], detC, LW * 0.8, CEN), true);+ // typebars at rest in the basket, each with its slug; a character with no type stirs the whole fan+ var rest = '';+ for (var i = 0; i < NB; i++) {+ if ((barS[i] || 0) > 0.004) continue;+ var pv = pivot(i, shiftDz), tp = tip(i, 0, shiftDz), hot = fan > 0.004;+ rest += stroke([pv, tp], hot ? hotC : inkC, LW * 1.1, hot ? ' stroke-opacity="' + f3(0.35 + 0.65 * fan) + '"' : ' stroke-opacity="0.85"') + dot(tp, LW * 1.5, hot ? hotC : inkC);+ }+ put('basket', rest, true);+ put('segment', stroke(arc, f.shift > 0.02 ? hotC : inkC, LW * 3.2), true);+ // lit levers: the path key -> lever -> typebar, a packet running along it (from the key going down to the strike)+ var litL = '';+ Object.keys(levers).forEach(function (ch) {+ var lv = levers[ch]; if (lv <= 0.004) return;+ var key = KEY[ch], L3 = lever(key, shiftDz), top = [key[1], key[2], key[3] + 0.5];+ litL += stroke(L3, hotC, LW * 1.8, HID + ' stroke-opacity="' + f3(0.3 + 0.7 * lv) + '"');+ f.win.forEach(function (e) {+ if (e.kind !== 'ch' || e.key !== ch) return;+ var q = clamp((t - (e.t - 0.05)) / (PK + 0.05), 0, 1); if (q <= 0 || t - e.t > PK + 0.06) return;+ var route = [top, L3[0], L3[1], L3[2]], seglen = [], tot = 0;+ for (var r = 1; r < route.length; r++) { var dd = Math.hypot(route[r][0] - route[r - 1][0], route[r][1] - route[r - 1][1], route[r][2] - route[r - 1][2]); seglen.push(dd); tot += dd; }+ var at = function (qq) { var want = qq * tot; for (var r2 = 0; r2 < seglen.length; r2++) { if (want <= seglen[r2] || r2 === seglen.length - 1) { var pp = clamp(want / seglen[r2], 0, 1); return [lerp(route[r2][0], route[r2 + 1][0], pp), lerp(route[r2][1], route[r2 + 1][1], pp), lerp(route[r2][2], route[r2 + 1][2], pp)]; } want -= seglen[r2]; } return route[route.length - 1]; };+ var trail = []; for (var z = 0; z <= 8; z++) trail.push(at(Math.max(0, q - 0.35 + 0.35 * z / 8)));+ litL += stroke(trail, hotC, LW * 2.6, ' stroke-opacity="0.55"') + dot(at(q), LW * 3, hotC, dark ? ' filter="url(#ld-glow)"' : '');+ });+ });+ put('levers', litL);+ // struck typebars: the swing arc (dashed), a short ghost trail, the bar, the type at its tip+ var bars = '';+ Object.keys(barS).forEach(function (bi) {+ var i2 = +bi, s = barS[bi]; if (s <= 0.004) return;+ var pv2 = pivot(i2, shiftDz), sw = [];+ for (var q2 = 0; q2 <= 24; q2++) sw.push(tip(i2, q2 / 24, shiftDz));+ bars += stroke(sw, hotC, LW * 0.8, ' stroke-dasharray="3 5" stroke-opacity="' + f3(0.7 * Math.min(1, s * 2.5)) + '"');+ [[0.066, 0.14], [0.033, 0.3]].forEach(function (g) {+ var sg = 0; f.win.forEach(function (e) { if (e.kind === 'ch' && e.bar === i2) sg = Math.max(sg, env(t - g[0], e)); });+ if (Math.abs(sg - s) > 0.02) bars += stroke([pv2, tip(i2, sg, shiftDz)], hotC, LW * 1.8, ' stroke-opacity="' + g[1] + '"');+ });+ bars += '<g filter="url(#ld-glowsoft)">' + stroke([pv2, tip(i2, s, shiftDz)], hotC, LW * 2.6) + '</g>' + dot(tip(i2, s, shiftDz), LW * 2.6, hotC) + dot(pv2, LW * 1.6, hotC);+ });+ put('basket', bars);+ // the ribbon: spool to spool through the vibrator, lifted in front of the print point on a strike+ var vz = PP[2] - 1.1 + 0.9 * ribbon, rib = '';+ SPOOLS.forEach(function (p, si) {+ var rot = f.strikes * 0.32 * (si ? -1 : 1), ring = [];+ for (var q3 = 0; q3 <= 36; q3++) { var a3 = q3 * Math.PI / 18; ring.push([p[0] + 1.25 * Math.cos(a3), p[1] + 1.25 * Math.sin(a3), p[2]]); }+ rib += stroke(ring, detC, LW * 0.8);+ for (var sp = 0; sp < 3; sp++) { var th3 = rot + sp * Math.PI * 2 / 3; rib += stroke([p, [p[0] + 1.6 * Math.cos(th3), p[1] + 1.6 * Math.sin(th3), p[2]]], inkC, LW * 1.1); }+ rib += dot(p, LW * 1.4, inkC);+ });+ rib += stroke([[SPOOLS[0][0] + 1.5, SPOOLS[0][1] - 1.1, SPOOLS[0][2]], [PP[0] - 2.4, PP[1] + 0.45, vz], [PP[0] + 2.4, PP[1] + 0.45, vz], [SPOOLS[1][0] - 1.5, SPOOLS[1][1] - 1.1, SPOOLS[1][2]]],+ ribbon > 0.02 ? hotC : inkC, LW * 1.6, ' stroke-opacity="' + f3(ribbon > 0.02 ? 0.55 + 0.45 * ribbon : 0.8) + '"');+ put('ribbon', rib);+ // the paper: the typed lines laid on the sheet (it rides with the carriage); the line feed scrolls them up+ var view = f.view, cells = view ? view.cells : [], above = view ? view.above : [];+ var feed = 0;+ if (ret && view === ret.e) feed = 1 - inOutCubic(clamp((ret.age - PK) / 0.22, 0, 1));+ var upv = [0, -0.255, 1], Pu = P(upv[0], upv[1], upv[2]), ey = [-(Pu[0] - O0[0]), -(Pu[1] - O0[1])];+ var paperOp = veil > 0.01 && !lit('paper') ? 1 - 0.6 * veil : 1;+ [cells].concat(above).forEach(function (cl, r) {+ var z = Z0 + (r - feed) * LF, pool = pools[r], n = 0, g = gRows[r];+ if (!g) return;+ g.setAttribute('data-text', lineText(cl));+ g.setAttribute('opacity', f3(paperOp * (r >= 4 ? 0.45 : 1)));+ if (z <= 15.0) {+ var y = paperY(z) - 0.03;+ for (var c = 0; c < cl.length; c++) {+ var cc = cl[c]; if (!cc) continue;+ for (var l = 0; l < cc.length; l++) {+ var fresh2 = r === 0 && c === view.col0 && view.kind === 'ch' && t - view.t - PK < 0.3 && l === cc.length - 1;+ var e = pool[n] || (pool[n] = mkEl('text', g, {})); n++;+ if (e.textContent !== cc.charAt(l)) e.textContent = cc.charAt(l);+ setGlyph(e, P(PAPER[0] + MARGIN + c * STEP + f.cx, y, z - 0.27), ex, ey, 0.74, fresh2 ? hotC : typeC, 1);+ }+ }+ }+ for (var j = n; j < pool.length; j++) if (pool[j].getAttribute('visibility') !== 'hidden') pool[j].setAttribute('visibility', 'hidden');+ });+ for (var r2 = 1 + above.length; r2 < gRows.length; r2++) {+ gRows[r2].setAttribute('data-text', '');+ pools[r2].forEach(function (e) { if (e.getAttribute('visibility') !== 'hidden') e.setAttribute('visibility', 'hidden'); });+ }+ // the platen's centre line (dash-dot, it rides with the carriage) and the print point's mark, flashing on a strike+ var pq = P(PP[0], PP[1], PP[2]), mk = 9, ppS = '';+ ppS += '<path d="M' + f1(pq[0] - mk) + ' ' + f1(pq[1]) + 'H' + f1(pq[0] + mk) + 'M' + f1(pq[0]) + ' ' + f1(pq[1] - mk) + 'V' + f1(pq[1] + mk) + '" stroke="' + inkC + '" stroke-width="' + f1(LW) + '"/>';+ flash.forEach(function (d) {+ var al = 1 - d / 0.22;+ ppS += '<circle cx="' + f1(pq[0]) + '" cy="' + f1(pq[1]) + '" r="' + f1(7 + d * 110) + '" fill="none" stroke="' + hotC + '" stroke-width="' + f1(LW * 1.6) + '" opacity="' + f3(al) + '"/>';+ ppS += '<circle cx="' + f1(pq[0]) + '" cy="' + f1(pq[1]) + '" r="' + f1(6 * al + 1.5) + '" fill="' + hotC + '" opacity="' + f3(al) + '"' + (dark ? ' filter="url(#ld-glow)"' : '') + '/>';+ });+ put('platen', stroke([[2.6 + f.cx, PLATEN.y, PLATEN.z], [41.4 + f.cx, PLATEN.y, PLATEN.z]], inkC, LW * 0.7, CEN + ' stroke-opacity="0.75"'));+ put('pp', ppS);+ // the escapement: the star wheel turns with the carriage (1/16 turn a letter), lit as it lets go a tooth+ var esc = [], rot3 = f.col * Math.PI * 2 / 16, escS = '';+ for (var q4 = 0; q4 <= 64; q4++) { var th4 = rot3 + q4 * Math.PI / 32, rr = q4 % 4 === 0 ? 1.62 : (q4 % 4 === 2 ? 1.1 : 1.36); esc.push([ESC[0] + rr * Math.cos(th4), ESC[1] + rr * Math.sin(th4), ESC[2]]); }+ escS += stroke(esc, escHot > 0.02 ? hotC : inkC, LW * (escHot > 0.02 ? 1.8 : 1.3));+ escS += stroke([ESC, [ESC[0] + 0.9 * Math.cos(rot3), ESC[1] + 0.9 * Math.sin(rot3), ESC[2]]], inkC, LW * 1.1) + dot(ESC, LW * 1.4, inkC);+ escS += stroke([[ESC[0], ESC[1] + 1.4, ESC[2]], [ESC[0], 4.3, 7.4]], detC, LW * 0.8, HID);+ put('escapement', escS);+ // the mainspring: a spiral that unwinds as the carriage goes left, wound again by Return; its band to the carriage+ var sp2 = [], turn = -f.col * 0.09, msS = '';+ for (var q5 = 0; q5 <= 110; q5++) { var th5 = turn + q5 * deg(14), r5 = 0.2 + q5 * 0.0165; sp2.push([SPRING[0] + r5 * Math.cos(th5), SPRING[1] + r5 * Math.sin(th5), SPRING[2]]); }+ var winding = ret ? Math.sin(Math.PI * clamp((ret.age - 0.04) / ret.e.mv.dur, 0, 1)) : 0;+ msS += stroke(sp2, winding > 0.02 ? warmC : inkC, LW * (1 + 0.6 * winding));+ msS += stroke([[SPRING[0] + 0.2, SPRING[1] - 2.0, SPRING[2] - 0.5], [SPRING[0] + 0.2, 5.4, 6.4], [4.4 + f.cx, PLATEN.y - 0.9, PLATEN.z - 0.7]], winding > 0.02 ? warmC : detC, LW * 1.5);+ put('mainspring', msS);+ // the return lever: lit while pulled+ if (ret) {+ var rl = Math.sin(Math.PI * clamp((ret.age + 0.05) / 0.4, 0, 1));+ if (rl > 0.01) put('retlever', stroke([[4.2 + f.cx, 7.6, 9.6], [2.6 + f.cx, 8.4, 11.6], [0.4 + f.cx, 8.8, 12.4]], hotC, LW * 2.4, ' stroke-opacity="' + f3(rl) + '"'));+ }+ // the bell: rings (three arcs that spread) five letters before the margin+ var bS = '';+ bells.forEach(function (d) {+ var bq = P(BELL[0], BELL[1], BELL[2] + 2.0);+ for (var w = 0; w < 3; w++) {+ var dd = d - w * 0.12; if (dd < 0) continue;+ var al2 = clamp(1 - dd / 0.8, 0, 1), rr2 = 16 + dd * 70;+ bS += '<path d="M' + f1(bq[0] - rr2 * 0.9) + ' ' + f1(bq[1] - rr2 * 0.25) + 'A' + f1(rr2) + ' ' + f1(rr2 * 0.6) + ' 0 0 1 ' + f1(bq[0] + rr2 * 0.9) + ' ' + f1(bq[1] - rr2 * 0.25) ++ '" fill="none" stroke="' + warmC + '" stroke-width="' + f1(LW * 1.6) + '" stroke-linecap="round" opacity="' + f3(al2) + '"/>';+ }+ });+ put('bell', bS);+ // the keys: their legends on the caps (they go down with the key), lit while pressed+ var P1 = P(0, 1, 0), eyk = [P1[0] - O0[0], P1[1] - O0[1]], keyTx = '';+ gKeys.setAttribute('opacity', f3(veil > 0.01 && !lit('keyboard') ? 1 - 0.6 * veil : 1));+ keyEls.forEach(function (x) {+ var key = x.k, dz = f.dips[x.id] || 0, top = [key[1], key[2], key[3] + 0.5 - 0.7 * dz];+ setGlyph(x.el, P(top[0], top[1] + 0.28, top[2]), ex, eyk, 1.0, dz > 0.05 ? hotC : inkC, dz > 0.05 ? 1 : 0.8);+ if (dz > 0.05) keyTx += dot(top, KR * S * 0.75, hotC, ' fill-opacity="' + f3(0.18 * dz) + '"');+ });+ var spd = f.dips['k-space'] || 0;+ if (spd > 0.05) keyTx += stroke([[SPACE.at[0] + 0.6, SPACE.at[1] + 0.85, SPACE.at[2] + SPACE.size[2] - 0.5 * spd + 0.02], [SPACE.at[0] + SPACE.size[0] - 0.6, SPACE.at[1] + 0.85, SPACE.at[2] + SPACE.size[2] - 0.5 * spd + 0.02]], hotC, LW * 3, ' stroke-opacity="' + f3(spd) + '"');+ put('keyboard', keyTx);+ gU.innerHTML = u; gO.innerHTML = o;+ }++ // ---------------------------------------------------------------- the part names (callouts) and the tags+ var scr = function (p) { return [O[0] + (p[0] - p[1]) * C30 * S, O[1] + (p[0] + p[1]) * 0.5 * S - p[2] * S]; };+ var L = [];+ var lab = function (o2, title, sub, elbow, side) {+ var l = Object.assign(o2, { at: -3, title: title, sub: sub });+ l.elbow = elbow; l.side = side;+ L.push(l);+ };+ lab({ for: 'keyboard', anchor: [SHIFTK[0] - 1.2, SHIFTK[1] + 0.5, SHIFTK[2] + 0.5] }, 'Keys', 'one per letter', live ? [-60, 110] : [-30, 170], 'left'); // the film: under the step's words+ lab({ for: 'basket', anchor: tip(3, 0) }, 'Typebars', 'one per key, in a fan', [-40, -170], 'left');+ lab({ anchor: [PP[0] + 0.6, 4.8, 15.2] }, 'Carriage', 'a letter left per key', [80, -40], 'right'); // the paper's top edge, always on the sheet // on the platen wherever it is+ lab({ for: 'escapement', anchor: [ESC[0] + 1.62, ESC[1], ESC[2]] }, 'Escapement', 'a tooth a letter', [40, -45], 'right');+ lab({ for: 'mainspring', anchor: [SPRING[0] - 1.45, SPRING[1] + 1.45, SPRING[2] - 1.2] }, 'Mainspring', 'pulls the carriage left', [-220, -45], 'left');+ lab({ for: 'bell', anchor: [BELL[0] + 1.3, BELL[1] + 0.6, BELL[2] + 1.6] }, 'Bell', 'rings at ' + BELL_AT + ' of ' + N, [40, 45], 'right');+ var TAGS = [+ // the typed line, readable: its right end over the print point's column, above the paper's highest corner+ { id: 'line', anchor: [PP[0], 4.8, 15.2], text: 'line', at: 1e9, tone: 'mute', off: [0, Math.round(scr([PAPER[0] + HOME - N * STEP, 4.8, 15.2])[1] - scr([PP[0], 4.8, 15.2])[1] - 34)], align: 'right', size: 30 },+ { id: 'ding', anchor: [BELL[0], BELL[1], BELL[2] + 2.4], text: 'ding', at: 1e9, tone: 'warm', off: [0, -78] },+ { id: 'note', anchor: [43, 18, 0.8], text: 'note', at: 1e9, tone: 'hot', off: [70, 72] },+ ];++ // ---------------------------------------------------------------- the steps, each typed through the same path+ var cr = function (fx, delay) { return tail.col > 0 ? (keystroke(fx, '\n', delay), delay + 0.75) : delay; };+ // the demo phrase, a keystroke at a time: step 1 types its next letter, the tour the rest; it starts again on a+ // fresh line when anything else was typed in between+ var PHRASE = 'hello world', pi = 0, pLast = null;+ function phrase(fx, delay, all) {+ if (pLast !== LOG[LOG.length - 1] || pi >= PHRASE.length || tail.col >= N) { pi = 0; delay = cr(fx, delay); }+ do { pLast = keystroke(fx, PHRASE.charAt(pi++), delay); delay += 0.14; } while (all && pi < PHRASE.length);+ return delay;+ }+ var STEPS = [+ { id: 'key', kicker: 'One keystroke', text: 'A key pulls its lever; the typebar swings up and strikes through the ribbon; the escapement lets the carriage step one letter left.',+ narr: 'Press a key: its lever pulls the typebar up out of the basket, it strikes the paper through the inked ribbon, and the escapement lets the carriage step one letter to the left.',+ run: function (fx) { phrase(fx, 0.3, false); } },+ { id: 'bell', kicker: 'A line and the bell', text: 'Five letters before the margin the bell rings; at the margin the keys lock.',+ narr: 'Type a line: five letters before the margin the bell rings, and at the margin the keys lock until you return the carriage.',+ run: function (fx) { var d = cr(fx, 0.3); d = word(fx, 'now is the time for all good', d, 0.115); keystroke(fx, 'm', d + 0.3); } },+ { id: 'return', kicker: 'Carriage return', text: 'The lever runs the carriage home against the mainspring and turns the platen up one line.',+ narr: 'Pull the return lever: the carriage runs home, winding the mainspring again, and the platen turns the paper up one line.',+ run: function (fx) { var d = 0.3; if (tail.col === 0) d = word(fx, 'men', d, 0.13) + 0.25; keystroke(fx, '\n', d); } },+ ];+ var demoed = function (fn) { return function (fx, a, b) { demo = true; try { fn(fx, a, b); } finally { demo = false; } sync(fx.state); }; };+ STEPS.forEach(function (s) { s.run = demoed(s.run); });+ var typeWord = function (s, gap) { return demoed(function (fx) { word(fx, s, 0.05, gap || 0.14); }); };++ window.SCENE = {+ page: {+ kicker: 'Fig. 1 · Live figure', figno: 'Manual typewriter',+ title: 'How a typewriter *types*',+ dek: 'A key, a lever, a typebar, a strike; then the carriage steps one letter left.',+ footL: 'A generic manual typewriter, drawn; not a specific model.', footR: 'Line: ' + N + ' letters · bell at ' + BELL_AT, steps: { at: [112, 640], width: 430 },+ },+ world: { scale: S, origin: O },+ leaderCost: 3,+ items: items, labels: L, tags: TAGS,+ offset: offset, onFrame: onFrame,+ // the film (index.html): the machine right of the page's title column+ camera: [{ at: -10, dur: 0.01, zoom: 0.8, focus: [960, 590], to: [1160, 600] }, { at: 0.4, dur: CUE.duration - 0.4, zoom: 0.82, focus: [970, 585], to: [1150, 600] }],++ interactive: {+ film: 'tour',+ kicker: 'Live figure', title: 'Type on a typewriter',+ sub: 'A manual typewriter, inside. Type on your keyboard: the machine types it. Enter returns the carriage, Backspace steps back.',+ source: 'A generic manual typewriter, drawn; not a specific model. The line length is measured off the drawing.',+ camera: [{ at: -10, dur: 0.01, zoom: 1, focus: [960, 540], to: [960, 540] }],+ crop: { desk: [96, 84, 1846, 1040], phone: [370, 130, 1550, 1040] },+ type: { hint: 'Type on it', run: function (fx, ch, state) {+ dropAfter(fx); sound();+ // the viewer's first letter on a line the demo typed: a fresh line first, so what they type stands alone+ var d = 0; if (ch !== '\n' && ch !== '\b' && demoLine()) d = keystroke(fx, '\n', 0).mv.dur + 0.03;+ keystroke(fx, ch, d); sync(state);+ } },+ parts: [+ { id: 'keyboard', name: 'Keys', sub: 'one per letter; each pulls its own lever' },+ { id: 'k-space', name: 'Space bar', sub: 'steps the carriage, prints nothing (press it)', press: function (fx, state) { dropAfter(fx); keystroke(fx, ' ', 0); sync(state); } },+ { id: 'levers', name: 'Key levers', sub: 'carry the press back to the typebar' },+ { id: 'basket', name: 'Typebars', sub: 'one per key, fanned in the basket; it swings up to strike' },+ { id: 'segment', name: 'Segment', sub: 'the arc they pivot on; it drops for Shift' },+ { id: 'pp', name: 'Print point', sub: 'where every letter lands' },+ { id: 'ribbon', name: 'Ribbon', sub: 'inked; it lifts in front of the paper for each strike' },+ { id: 'platen', name: 'Platen', sub: 'the roller the paper wraps around' },+ { id: 'carriage', name: 'Carriage', sub: 'carries platen and paper; one letter left per key' },+ { id: 'escapement', name: 'Escapement', sub: 'lets the carriage go one tooth per letter' },+ { id: 'mainspring', name: 'Mainspring', sub: 'pulls the carriage left; Return winds it' },+ { id: 'bell', name: 'Bell', sub: 'rings ' + (N - BELL_AT) + ' letters before the margin' },+ { id: 'retlever', name: 'Return lever', sub: 'carriage home, a new line (press it)', press: function (fx, state) { dropAfter(fx); keystroke(fx, '\n', 0); sync(state); } },+ ],+ reset: function (fx) { dropAfter(fx); },+ steps: STEPS,+ readouts: function (s) {+ return {+ rows: [+ { part: 'keyboard', k: 'Characters typed', v: String(s.typed || 0), u: 'spaces included' },+ { part: 'carriage', k: 'Carriage', v: (s.col || 0) + ' of ' + N, u: 'letters along the line', tone: (s.col || 0) >= BELL_AT ? 'warm' : '' },+ { part: 'platen', k: 'Line', v: String(s.line || 1), u: 'on this sheet' },+ ],+ note: 'The bell rings at ' + BELL_AT + '; at ' + N + ' the keys lock. ' + N + ' letters: 15 units of paper between the margins over a step of ' + STEP + '.',+ };+ },+ // untouched, it types by itself (also the film: interactive.film = 'tour', CUE.duration = its length)+ tour: [+ [{ step: 'key' }, 1500],+ [demoed(function (fx) { phrase(fx, 0.05, true); }), 2000],+ [{ hover: 'basket' }, 2200], [{ hover: null }, 300],+ [{ step: 'bell' }, 6200],+ [{ step: 'return' }, 3200],+ [typeWord('Type on it.'), 3000],+ [{ hover: 'escapement' }, 2200], [{ hover: 'mainspring' }, 2200], [{ hover: null }, 600],+ [demoed(function (fx) { keystroke(fx, '\n', 0.1); }), 1800],+ ],+ },+ };+})();diff --git a/skills/showtime/templates/diagram/figure.html b/skills/showtime/templates/diagram/figure.htmlindex da0162d..97071ed 100644--- a/skills/showtime/templates/diagram/figure.html+++ b/skills/showtime/templates/diagram/figure.html@@ -8,6 +8,11 @@offline file. The board is live from the first frame: hover a part, pick a step, turn a knob. --><link rel="stylesheet" href="/_st/themes/fonts/inter.css"><link rel="stylesheet" href="/_st/themes/fonts/jetbrains-mono.css">+<!-- the dark and light themes draw in Geist and IBM Plex: an export keeps only the faces the page uses -->+<link rel="stylesheet" href="/_st/themes/fonts/geist.css">+<link rel="stylesheet" href="/_st/themes/fonts/geist-mono.css">+<link rel="stylesheet" href="/_st/themes/fonts/ibm-plex-sans.css">+<link rel="stylesheet" href="/_st/themes/fonts/ibm-plex-mono.css"><link rel="stylesheet" href="/_st/diagram.css"><link rel="stylesheet" href="/_st/figure.css"><script src="/_st/diagram.js"></script>diff --git a/skills/showtime/templates/diagram/scene.js b/skills/showtime/templates/diagram/scene.jsindex b027dd3..00f4e12 100644--- a/skills/showtime/templates/diagram/scene.js+++ b/skills/showtime/templates/diagram/scene.js@@ -24,7 +24,7 @@ window.SCENE = {{ id: 'plate', part: 'plate', at: [-2, -2, -2.4], size: [100, 64, 2.4], grid: 6 },{ id: 'floor-in', part: 'floor', at: [69, -2, 0], size: [29, 64, 0.35], build: CUE.build * 0.5, lift: 0,shapes: [{ kind: 'bloom', at: [84, 14, 0.36], r: 15, from: CUE.indoor, dur: 1.8 }] },- { kind: 'floortext', at: [13, 59.4, 0], text: 'OUTSIDE 2°C', at0: CUE.build + 0.5 },+ { kind: 'floortext', at: [13, 59.4, 0], text: 'OUTSIDE', at0: CUE.build + 0.5 },{ kind: 'floortext', at: [95.6, 61, 0.35], dir: 'y', text: 'INSIDE 21°C', at0: CUE.build + 0.7 },{ id: 'outdoor', part: 'unit', at: [10, 34, 0], size: [24, 18, 24], build: CUE.build + 0.1,@@ -53,14 +53,14 @@ window.SCENE = {{ pipe: 'suction' }, { gap: 70 }, { pipe: 'discharge' }, { gap: 26 }, { pipe: 'discharge-in' }, { gap: 120 },{ pipe: 'liquid' }, { gap: 24 }, { pipe: 'return' }, { gap: 160 }] }],labels: [- { for: 'outdoor', at: CUE.outdoor, title: 'Outdoor unit', sub: 'soaks up heat, even from 2 °C air' },+ { for: 'outdoor', at: CUE.outdoor, title: 'Outdoor unit', sub: 'soaks up heat, even from freezing air' },{ for: 'compressor', at: CUE.compressor + 0.3, title: 'Compressor', sub: 'squeezes the vapour: 4 to 15 bar' },{ for: 'indoor', at: CUE.indoor + 0.4, title: 'Indoor coil', sub: 'warms the room' },{ for: 'valve', at: CUE.valve, title: 'Expansion valve', sub: 'pressure drops, the fluid chills' },],tags: [- { pipe: 'suction', f: 0.42, tone: 'cold', text: '−5 °C', at: CUE.cold },- { pipe: 'discharge', f: 0.8, tone: 'hot', text: '70 °C', at: CUE.hot },+ { id: 't-suction', pipe: 'suction', f: 0.42, tone: 'cold', text: '−5 °C', at: CUE.cold },+ { id: 't-discharge', pipe: 'discharge', f: 0.8, tone: 'hot', text: '70 °C', at: CUE.hot },],focus: [{ part: 'compressor', with: ['suction', 'discharge'], at: CUE.compressor, until: CUE.indoor - 0.5 }],// camera keys run in order, each from where the last one left it; focus is a part, a world point or a screen point@@ -78,7 +78,39 @@ window.SCENE = {// everything built and the refrigerant flowing; the viewer hovers the parts (references/workflows/diagram.md)interactive: {title: 'How a heat pump moves heat',- sub: 'It carries heat in from the cold air outside. Hover a part to see what it does and the pipes it joins.',+ sub: 'It carries heat in from the cold air outside. Drag the outdoor unit to change the weather; hover a part to see what it does.',start: CUE.duration,+ source: 'Illustrative: the refrigerant runs about 7 °C below the outside air, and the heat out per unit of power is taken as 45% of the ideal (Carnot) limit for a 35 °C indoor coil.',+ parts: [+ { id: 'outdoor', name: 'Outdoor unit', sub: 'drag it left or right: colder or milder outside', drag: { knob: 'outside', axis: 'x', span: 360 } },+ { id: 'compressor', name: 'Compressor', sub: 'squeezes the vapour, which heats it' },+ { id: 'indoor', name: 'Indoor coil', sub: 'gives the heat to the room' },+ { id: 'valve', name: 'Expansion valve', sub: 'pressure drops, the fluid chills' },+ ],+ knobs: { outside: { label: 'Outside air', hint: 'drag the outdoor unit', min: -20, max: 15, step: 1, default: 2, format: function (v) { return v + ' °C'; } } },+ steps: [{+ id: 'weather', kicker: 'The weather', knob: 'outside',+ text: function (s) { return 'At ' + s.outside + ' °C outside, each unit of electricity moves ' + HP.cop(s.outside).toFixed(1) + ' units of heat indoors (illustrative).'; },+ run: function (fx) {+ var o = fx.state.outside;+ fx.say('t-suction', HP.fmt(o - 7), 'cold');+ fx.say('t-discharge', HP.fmt(Math.round(55 + (15 - o) * 0.6)), 'hot');+ fx.pulse('outdoor', 'cold'); fx.pulse('compressor', 'hot', { delay: 0.35 });+ },+ }],+ readouts: function (s) {+ return { rows: [+ { part: 'outdoor', k: 'Outside air', v: String(s.outside), u: '°C' },+ { part: 'outdoor', k: 'Refrigerant in', v: String(s.outside - 7), u: '°C', tone: 'cold' },+ { part: 'compressor', k: 'Heat moved per unit of power', v: HP.cop(s.outside).toFixed(1), u: '×' },+ ], note: 'Colder air: a bigger lift for the compressor, so less heat per unit of power.' };+ },+ tour: [[{ step: 'weather' }, 2200], [{ knob: ['outside', -10] }, 2600], [{ knob: ['outside', 10] }, 2600], [{ knob: ['outside', 2] }, 2600]],},};++// the illustrative numbers above, from one place: 45% of the Carnot limit for a 35 °C coil and refrigerant 7 °C below the air+var HP = {+ cop: function (outside) { var hot = 35 + 273.15, cold = outside - 7 + 273.15; return 0.45 * hot / (hot - cold); },+ fmt: function (c) { return (c < 0 ? '−' + (-c) : String(c)) + ' °C'; },+};diff --git a/skills/showtime/templates/xray/README.md b/skills/showtime/templates/xray/README.mdnew file mode 100644index 0000000..5b6a634--- /dev/null+++ b/skills/showtime/templates/xray/README.md@@ -0,0 +1,23 @@+# X-ray: a real clip, with its inside drawn in sync and a receipt that proves it++The page shows a clip someone filmed (a typewriter, a music box) and draws the inside of the object moving on the+exact frames the clip's own sound says it did. It starts as a 9.4 s sample: an Underwood 315 typing "Sounds of+changes." (Erik Pålsson / Sounds of Changes, CC BY 3.0: keep the credit if you keep the clip).++- `xray.json`: the measured events, from `showtime xray analyze <clip> --job <job> [--track ...]` (copy+ `<job>/xray/xray.json` here). With a `track`, the inside is drawn on the object itself as the camera moves.+- `media/clip.webm`: the clip (its sound plays from `audio/mix.json`; the page itself is muted). It starts at+ film frame 0: trim the clip, never shift it.+- `mechanism.js`: the inside, from the mechanism library (`library/xray/<object>/`, `--mechanism <object>`): its+ parts, how an event moves them, the four anchor points that place it on the object.+- `showtime.json` `"xray"`: `layout` (through, split, diagram), `skip` (events that are not the object's, by+ index), `kinds` (`{"18": "return"}`), `text` (what the clip shows the events produced, one character each),+ `place` (the anchors on the reference frame of the track, in the clip's pixels), `credit`, `receipt` (the+ line `showtime xray verify` prints, for the end card).+- `cue.js`: the beats (the scan, the labels, the end card); set `endcard` to the clip's length.+- `xray.js`: the runtime (layout, drawing kit, the sync marker in the bottom-right corner: never cover it).++9:16 puts the clip on top and the inside below it (in place as well when tracked), all text inside the reels+safe box; 16:9 with a track draws in place over the whole frame (`"panel": true` keeps the schematic beside it);+16:9 without one puts the inside beside the clip. Library: typewriter, musicbox, switch (and the music entries).+The guide: `references/workflows/xray.md` (`showtime guide xray`).diff --git a/skills/showtime/templates/xray/audio/mix.json b/skills/showtime/templates/xray/audio/mix.jsonnew file mode 100644index 0000000..bae9b18--- /dev/null+++ b/skills/showtime/templates/xray/audio/mix.json@@ -0,0 +1,7 @@+{+ "duration": 12.8,+ "tracks": [+ {"id": "clip", "kind": "sfx", "file": "media/clip.webm", "start": 0, "level": "raw", "fade_out": 0.4}+ ],+ "master": {"lufs": -16, "true_peak": -1}+}diff --git a/skills/showtime/templates/xray/cue.js b/skills/showtime/templates/xray/cue.jsnew file mode 100644index 0000000..2e47652--- /dev/null+++ b/skills/showtime/templates/xray/cue.js@@ -0,0 +1,9 @@+// The X-ray's beats, in seconds (`showtime new xray --duration` and `showtime retime` scale them). The clip itself+// is never retimed: it starts at 0 and its events land on their own frames. Set endcard to the clip's length.+var CUE = {+ duration: 12.8,+ scan: 0.3,+ scan_end: 1.3,+ labels: 1.5,+ endcard: 9.4,+};diff --git a/skills/showtime/templates/xray/diagram.html b/skills/showtime/templates/xray/diagram.htmlnew file mode 100644index 0000000..d3bdfa2--- /dev/null+++ b/skills/showtime/templates/xray/diagram.html@@ -0,0 +1,248 @@+<!doctype html>+<html lang="en" data-theme="dark">+<head>+<meta charset="utf-8">+<title>X-ray: living diagram</title>+<!-- The X-ray "diagram" layout: your clip beside an isometric living diagram of the object's inside, both on the+ film's clock. `showtime new xray <dir> --layout diagram` makes this page the project's index.html and copies the+ object's scene builder (library/xray/<object>/diagram.js) next to it. The theme is showtime.json+ "xray": {"theme": "dark" | "light"} (dark by default). Edit the beats in cue.js and the scene in diagram.js;+ this page lays out the clip, the counters, the sound strip, the end card and the sync marker. -->+<script src="/_st/stage.js"></script>+<link rel="stylesheet" href="/_st/themes/fonts/ibm-plex-sans.css">+<link rel="stylesheet" href="/_st/themes/fonts/ibm-plex-mono.css">+<link rel="stylesheet" href="/_st/themes/fonts/space-grotesk.css">+<link rel="stylesheet" href="/_st/themes/fonts/geist.css">+<link rel="stylesheet" href="/_st/themes/fonts/geist-mono.css">+<link rel="stylesheet" href="/_st/themes/fonts/instrument-serif.css">+<link rel="stylesheet" href="/_st/diagram.css">+<script src="/_st/diagram.js"></script>+<style>+ /* the page's own tokens follow the diagram's theme (diagram.css sets --ld-* per data-theme) */+ :root { --xd-hot: #FF6A3D; --xd-dim: rgba(159,180,207,.28); --xd-plate: rgba(10,15,25,.97); }+ :root[data-theme="light"] { --xd-hot: #B03A16; --xd-dim: rgba(42,44,48,.22); --xd-plate: rgba(244,243,239,.97); }+ html, body { margin: 0; }+ .xd { position: absolute; left: 0; top: 0; overflow: hidden; background: var(--ld-backdrop); background-color: var(--ld-ground); color: var(--ld-ink); }+ .xd .ld-stage { background: transparent; }+ #dwin { position: absolute; left: 0; top: 0; overflow: hidden; }+ #dscale { position: absolute; left: 0; top: 0; transform-origin: 0 0; }+ .xd-frame { position: absolute; inset: 44px; border: 1.5px solid var(--ld-hair); border-radius: 4px; }+ .xd .ld-kicker, .xd .ld-figno { top: 100px; }+ .xd .ld-title { font-size: 60px; }+ .xd .ld-title em, .xd .ld-kicker b { color: var(--xd-hot); }+ /* the clip: a thin frame with corner ticks, its caption under it */+ .clipbox { position: absolute; }+ .clipbox video { position: absolute; left: 0; top: 0; width: 100%; height: 100%; object-fit: fill; display: block; }+ .clipbox .edge { position: absolute; inset: -9px; border: 1.5px solid var(--ld-hair); border-radius: 3px; }+ .clipbox .edge i { position: absolute; width: 16px; height: 16px; border-color: var(--xd-hot); border-style: solid; }+ .clipbox .edge i.a { left: -2px; top: -2px; border-width: 2px 0 0 2px; } .clipbox .edge i.b { right: -2px; top: -2px; border-width: 2px 2px 0 0; }+ .clipbox .edge i.c { left: -2px; bottom: -2px; border-width: 0 0 2px 2px; } .clipbox .edge i.d { right: -2px; bottom: -2px; border-width: 0 2px 2px 0; }+ .cap { position: absolute; display: flex; justify-content: space-between; font-family: var(--ld-mono); font-weight: 500; font-size: 25px;+ letter-spacing: .1em; text-transform: uppercase; color: var(--ld-ink-2); white-space: nowrap; }+ .cap b { font-weight: 500; color: var(--xd-hot); }+ /* the sound strip: the clip's events as ticks, a playhead (drawn on a canvas, from the frame) */+ .strip { position: absolute; }+ .strip .k { position: absolute; left: 0; top: 0; font-family: var(--ld-mono); font-weight: 500; font-size: 25px; letter-spacing: .1em;+ text-transform: uppercase; color: var(--ld-ink-2); white-space: nowrap; }+ .strip .k b { font-weight: 500; color: var(--xd-hot); }+ .strip canvas { position: absolute; left: 0; }+ /* counters: keystrokes heard, the paper typed so far */+ .hud { position: absolute; display: flex; gap: 48px; align-items: flex-end; }+ .hud .n b { display: block; font-family: 'Space Grotesk', var(--ld-sans); font-weight: 600; font-size: 76px; line-height: .9; font-variant-numeric: tabular-nums; }+ :root[data-theme="light"] .hud .n b { font-family: 'Instrument Serif', serif; font-weight: 400; font-size: 88px; }+ .hud span { display: block; margin-top: 10px; font-family: var(--ld-mono); font-weight: 500; font-size: 25px; letter-spacing: .1em; text-transform: uppercase; color: var(--ld-ink-2); }+ .hud .paper { flex: 1; min-width: 0; }+ .hud .paper b { display: block; font-family: var(--ld-mono); font-weight: 500; font-size: 40px; line-height: 1.1; white-space: pre; color: var(--ld-ink);+ border-bottom: 1.5px solid var(--ld-hair); padding-bottom: 10px; }+ .hud .paper b i { color: var(--xd-hot); font-style: normal; }+ /* the end card: the figure's panel turns into the result */+ .end { position: absolute; background: var(--xd-plate); box-sizing: border-box; display: flex; flex-direction: column; justify-content: center; opacity: 0; visibility: hidden; }+ .end h2 { margin: 0; font-family: 'Space Grotesk', var(--ld-sans); font-weight: 600; font-size: 80px; line-height: 1.02; letter-spacing: -.025em; text-wrap: balance; }+ :root[data-theme="light"] .end h2 { font-family: 'Instrument Serif', serif; font-weight: 400; font-size: 92px; letter-spacing: -.01em; }+ .end h2 em { color: var(--xd-hot); font-style: normal; }+ .end p { margin: 30px 0 0; font-family: var(--ld-sans); font-size: 31px; line-height: 1.35; color: var(--ld-ink); }+ .end .receipt { margin-top: 34px; border: 1.5px solid var(--xd-hot); border-radius: 4px; padding: 16px 22px; font-family: var(--ld-mono); font-size: 27px; line-height: 1.4; color: var(--ld-ink); }+ .end .muted { color: var(--ld-ink-2); font-size: 25px; line-height: 1.45; }+ /* phone (9:16): every word inside the reels safe box (x 64-916, y 220-1440), 43 px and up */+ .portrait .cap, .portrait .hud span { font-size: 43px; letter-spacing: .02em; }+ .portrait .hud { gap: 44px; align-items: baseline; }+ .portrait .hud .n { display: flex; align-items: baseline; gap: 16px; }+ .portrait .hud .n b { font-size: 84px; }+ .portrait .hud .n span { margin-top: 0; }+ .portrait .hud .paper b { font-size: 44px; padding-bottom: 10px; }+ .portrait .end h2 { font-size: 88px; }+ .portrait .end p, .portrait .end .receipt, .portrait .end .muted { font-size: 43px; line-height: 1.3; }+ /* the sync marker: 6x6 px at (width-10, height-10), black, white only on a kept event's frame; keep it uncovered */+ #xmark { position: absolute; width: 6px; height: 6px; background: #000; z-index: 99; }+</style>+</head>+<body>+<div class="xd" id="stage">+ <div class="xd-frame" id="xframe"></div>+ <div id="dwin"><div id="dscale"><div class="ld-stage"><div id="ld"></div></div></div></div>+ <div class="ld-meta ld-kicker" id="kicker"><b>X-ray</b> · the inside, on your clip's clock</div>+ <div class="ld-meta ld-figno" id="figno">Drawn · a generic manual typewriter</div>+ <h1 class="ld-title" id="title">Every keystroke, <em>from inside</em></h1>+ <div class="clipbox" id="clipbox" data-ld-avoid>+ <video id="clip" src="media/clip.webm" muted playsinline></video>+ <div class="edge"><i class="a"></i><i class="b"></i><i class="c"></i><i class="d"></i></div>+ </div>+ <div class="cap" id="cap" data-ld-avoid><span>Your clip</span><span id="cap-r">its own sound</span></div>+ <div class="strip" id="strip" data-ld-avoid><div class="k" id="strip-k">The clip’s sound · <b>red</b> = a keystroke</div><canvas id="strip-cv"></canvas></div>+ <div class="hud" id="hud" data-ld-avoid>+ <div class="n"><b id="nk">0</b><span id="noun">keystrokes</span></div>+ <div class="paper" id="textcard"><b id="pp"> </b><span id="textlabel">on the paper</span></div>+ </div>+ <div class="end" id="end">+ <h2 id="end-h"></h2>+ <p id="end-measured"></p>+ <div class="receipt" id="receipt"></div>+ <p class="muted" id="end-drawn"></p>+ </div>+ <div id="xmark"></div>+</div>+<script src="cue.js"></script>+<script src="diagram.js"></script>+<script>+/* The page: load xray.json and showtime.json (inside ST.waitFor, so the first frame waits for them), lay the page out+ for 16:9 or 9:16, build the scene with the object's builder (XRAY_DIAGRAM[<mechanism>](ctx)) and mount it. Mounting+ after load works: Diagram.mount registers its own ST.waitFor (fonts and label layout) and its ST.onSeek handler,+ and the stage waits for gates added while earlier ones resolve. Frames, never seconds: an event's film frame is+ its xray.json `frame` + offset_frames; the strike peaks on it. */+(function () {+ 'use strict';+ var NOUN = { typewriter: ['keystroke', 'keystrokes', 'on the paper'] };+ var S = null;+ function el(id) { return document.getElementById(id); }+ function px(v) { return (Math.round(v * 10) / 10) + 'px'; }+ function box(e, x, y, w, h) { e.style.left = px(x); e.style.top = px(y); if (w != null) e.style.width = px(w); if (h != null) e.style.height = px(h); }+ function getJSON(u) { return fetch(u, { cache: 'no-store' }).then(function (r) { return r.ok ? r.json() : null; }); }+ var cue = function (k, d) { return (window.CUE && typeof window.CUE[k] === 'number') ? window.CUE[k] : d; };++ function setup(doc, conf) {+ if (!doc || !doc.events) throw new Error('xray.json is missing or has no events: run `showtime xray analyze <clip> --job <job>` and copy <job>/xray/xray.json next to index.html');+ var cfg = (conf && conf.xray) || {};+ // W, H, fps from the showtime.json just fetched (ST.info() may not have merged it yet in a preview)+ var W = (conf && conf.width) || ST.info().width, H = (conf && conf.height) || ST.info().height, fps = (conf && conf.fps) || ST.info().fps || 30;+ var portrait = H > W * 1.2, theme = cfg.theme === 'light' ? 'light' : 'dark';+ document.documentElement.setAttribute('data-theme', theme);+ var name = cfg.mechanism || 'typewriter', build = window.XRAY_DIAGRAM && window.XRAY_DIAGRAM[name];+ if (!build) throw new Error('no living-diagram scene for "' + name + '": diagram.js must set XRAY_DIAGRAM.' + name + ' = function (ctx) { return SCENE; }');+ var nouns = NOUN[name] || [cfg.one || 'event', cfg.noun || 'events', 'read from the clip'];+ // events: film frames, kinds (skip / return / ... from showtime.json), characters read off the clip+ var off = Math.round(Number(cfg.offset_frames) || 0), sfps = (doc.source && doc.source.fps) || fps, kinds = {}, text = cfg.text || '', ti = 0;+ (cfg.skip || []).forEach(function (i) { kinds[i] = 'skip'; });+ Object.keys(cfg.kinds || {}).forEach(function (i) { kinds[i] = cfg.kinds[i]; });+ if (off) el('clip').setAttribute('data-offset', String(-off / fps)); // the clip starts at film frame `off`+ var ev = doc.events.map(function (e, i) {+ var fr = (Math.abs(sfps - fps) > 0.01 ? Math.floor(e.t * fps + 1e-9) : e.frame) + off; // verify's arithmetic+ var kind = kinds[i] || 'event', o = { i: i, frame: fr, strength: e.strength, kind: kind, ch: null };+ if (kind === 'event' && ti < text.length) o.ch = text[ti++];+ return o;+ });+ var n = ev.filter(function (e) { return e.kind === 'event'; }).length;+ var stage = el('stage'); stage.style.width = W + 'px'; stage.style.height = H + 'px';+ if (portrait) stage.classList.add('portrait');+ var src = doc.source || {}, sw = src.width || 1920, sh = src.height || 1080;+ var k = W / 1920; // what the diagram runtime scales its stage by (innerWidth / 1920)+ var lay = {};+ if (!portrait) {+ // 16:9: the clip on the left (40 % of the width), the living diagram on the right+ var cw = Math.round(W * 0.4), ch = Math.round(cw * sh / sw), cx = 112, cy = 252;+ box(el('clipbox'), cx, cy, cw, ch);+ box(el('cap'), cx, cy + ch + 20, cw);+ box(el('strip'), cx, cy + ch + 78, cw, 100); el('strip-cv').style.top = '40px';+ box(el('hud'), cx, cy + ch + 186, cw);+ lay.strip = { x: 0, y: 40, w: cw, h: 54 };+ lay.ps = 1; lay.win = { x: 0, y: 0, w: W, h: H }; lay.dx0 = 0; lay.dy0 = 0;+ lay.region = [990, 185, 1830, 950];+ lay.safe = [cx + cw + 60, 150, 1856, 985];+ lay.clip = [cx + cw + 40, 46, 1874 - (cx + cw + 40), 988];+ box(el('end'), cx + cw + 40, 45, W - 45 - (cx + cw + 40), H - 90);+ el('end').style.padding = '0 84px';+ } else {+ // 9:16: the clip on top (full width), the living diagram below it, counters and the strip under the diagram+ ['kicker', 'figno', 'title', 'xframe'].forEach(function (id) { el(id).hidden = true; });+ var pw = W, ph = Math.round(W * sh / sw), py = 0;+ box(el('clipbox'), 0, py, pw, ph); el('clipbox').querySelector('.edge').hidden = true;+ box(el('cap'), 64, Math.max(232, py + ph + 16), 852); el('cap-r').hidden = true;+ var wy = Math.max(300, py + ph + 80), wh = 1316 - wy;+ lay.ps = 0.8; lay.win = { x: 0, y: wy, w: W, h: wh };+ lay.dx0 = (1920 - W / lay.ps) / 2; lay.dy0 = Math.max(0, (1080 - wh / lay.ps) / 2);+ var D = function (sx, sy) { return [sx / lay.ps + lay.dx0, sy / lay.ps + lay.dy0]; }; // window px -> design px+ var a = D(0, 0), b = D(W, wh), s0 = D(64, 8), s1 = D(916, wh - 8);+ lay.region = [a[0] + 40, a[1] + 110, b[0] - 40, b[1] - 20]; // the band on top holds the part names+ lay.safe = [s0[0], s0[1], s1[0], s1[1]];+ lay.clip = [a[0], a[1], b[0] - a[0], b[1] - a[1]];+ box(el('hud'), 64, 1334, 852); el('textlabel').style.display = 'none';+ box(el('strip'), 64, 1470, W - 128, 84); el('strip-k').hidden = true; el('strip-cv').style.top = '0px';+ lay.strip = { x: 0, y: 0, w: W - 128, h: 84 };+ box(el('end'), 0, 0, W, H); // the whole frame; its words inside y 220-1440+ el('end').style.padding = '220px 164px ' + (H - 1440) + 'px 64px';+ }+ box(el('dwin'), lay.win.x, lay.win.y, lay.win.w, lay.win.h);+ // the diagram runtime scales its stage by k itself; this wrapper turns that into the scale this layout wants+ el('dscale').style.transform = 'translate(' + px(-lay.dx0 * lay.ps) + ',' + px(-lay.dy0 * lay.ps) + ') scale(' + (lay.ps / k) + ')';+ var cv = el('strip-cv'); cv.width = Math.round(lay.strip.w); cv.height = Math.round(lay.strip.h);+ cv.style.width = px(lay.strip.w); cv.style.height = px(lay.strip.h);+ // words+ el('noun').textContent = portrait ? 'keys' : nouns[1];+ if (text) el('textlabel').textContent = nouns[2]; else el('textcard').hidden = true;+ el('cap-r').textContent = (src.duration ? (Math.round(src.duration * 10) / 10) + ' s · ' : '') + 'its own sound';+ el('end-h').innerHTML = cfg.headline || ('<em>' + n + '</em> ' + nouns[1] + ', seen from inside the machine.');+ el('end-measured').textContent = cfg.measured || ('Each ' + nouns[0] + ' lit up on the frame its sound was heard' + (text ? '; which key, read from the paper in the clip.' : '.'));+ el('receipt').textContent = cfg.receipt || (n + ' ' + nouns[1] + ' measured from the clip’s sound');+ el('end-drawn').textContent = [cfg.drawn || 'Inside view: a generic manual typewriter, drawn, not this exact machine.', cfg.credit || ''].join(' ').trim();+ var mk = el('xmark'); mk.style.left = (W - 10) + 'px'; mk.style.top = (H - 10) + 'px';+ S = { W: W, H: H, fps: fps, ev: ev, text: text, portrait: portrait, lay: lay, dur: (src.duration || cue('endcard', 9.4)), n: n };+ // the scene+ var ctx = { events: ev, fps: fps, cue: window.CUE || {}, portrait: portrait, region: lay.region, safe: lay.safe, clip: lay.clip,+ avoid: portrait ? '.xd-none' : null, theme: Diagram.themes[theme] };+ var scene = window.SCENE = build(ctx);+ var look = portrait ? { size: { label: Math.ceil(44 / lay.ps), sub: 2, tag: Math.ceil(44 / lay.ps) } } : null;+ var api = Diagram.mount(scene, { theme: theme, look: look });+ // a failure inside the diagram's own layout pass would otherwise only leave the labels unplaced: make it loud+ return api && api.ready.catch(function (e) { setTimeout(function () { throw e; }); throw e; });+ }++ function strip(f) {+ var cv = el('strip-cv'), g = cv.getContext('2d'), w = cv.width, h = cv.height, cs = getComputedStyle(document.documentElement);+ var hot = cs.getPropertyValue('--xd-hot').trim() || '#FF6A3D', ink = cs.getPropertyValue('--ld-ink').trim() || '#EEF2F8', dim = cs.getPropertyValue('--xd-dim').trim();+ g.clearRect(0, 0, w, h);+ g.fillStyle = dim; g.fillRect(0, h / 2 - 1, w, 2);+ S.ev.forEach(function (e) {+ var x = Math.round((e.frame / S.fps) / S.dur * (w - 4)) + 2, hh = Math.max(8, (0.3 + 0.7 * (e.strength || 0.5)) * (h - 6)), on = e.frame <= f;+ g.globalAlpha = e.kind === 'skip' ? (on ? 0.55 : 0.25) : (on ? 1 : 0.32);+ g.fillStyle = e.kind === 'skip' ? ink : hot; g.fillRect(x - 2, (h - hh) / 2, 4, hh);+ });+ g.globalAlpha = 1; g.fillStyle = ink;+ g.fillRect(Math.round(Math.min(1, (f / S.fps) / S.dur) * (w - 4)) + 1, 0, 2, h);+ }++ function frame(t, fr) {+ var f = typeof fr === 'number' ? fr : Math.round(t * S.fps);+ strip(f);+ var done = S.ev.filter(function (e) { return e.kind === 'event' && e.frame <= f; });+ el('nk').textContent = done.length;+ if (S.text) {+ var typed = done.filter(function (e) { return e.ch != null; }), s = typed.map(function (e) { return e.ch; }).join('');+ var last = typed.length && f - typed[typed.length - 1].frame < 12, pp = el('pp');+ pp.textContent = '';+ if (!s) pp.innerHTML = ' ';+ else { pp.appendChild(document.createTextNode(last ? s.slice(0, -1) : s)); if (last) { var i = document.createElement('i'); i.textContent = s.slice(-1); pp.appendChild(i); } }+ }+ var e0 = cue('endcard', 9.4), eo = ST.progress(t, e0, e0 + 0.2), end = el('end');+ end.style.opacity = eo; end.style.visibility = eo > 0 ? 'visible' : 'hidden';+ el('figno').style.opacity = 1 - eo;+ if (S.portrait) ['hud', 'cap'].forEach(function (id) { el(id).style.opacity = 1 - eo; el(id).style.visibility = eo < 1 ? 'visible' : 'hidden'; });+ // the sync marker: white exactly on a kept event's film frame, black otherwise (showtime xray verify reads it)+ var on = S.ev.some(function (e) { return e.frame === f && e.kind !== 'skip'; });+ el('xmark').style.background = on ? '#fff' : '#000';+ }++ ST.waitFor(Promise.all([getJSON('xray.json'), getJSON('showtime.json')]).then(function (r) { return setup(r[0], r[1]); }), 'xray.json and the living diagram');+ ST.onSeek(function (t, fr) { if (S) frame(t, fr); });+})();+</script>+</body>+</html>diff --git a/skills/showtime/templates/xray/index.html b/skills/showtime/templates/xray/index.htmlnew file mode 100644index 0000000..de7e43c--- /dev/null+++ b/skills/showtime/templates/xray/index.html@@ -0,0 +1,75 @@+<!doctype html>+<html lang="en">+<head>+<meta charset="utf-8">+<title>X-ray</title>+<script src="/_st/stage.js"></script>+<link rel="stylesheet" href="/_st/themes/fonts/bricolage-grotesque.css">+<link rel="stylesheet" href="/_st/themes/fonts/ibm-plex-mono.css">+<style>+ :root { --line: #8fd3ff; --ink: #e9f3ff; --muted: #a9bdd6; --hot: #ff4a3d; --ground: #070b16; }+ html, body { margin: 0; background: var(--ground); }+ .stage { background: var(--ground); }+ .stage { position: relative; width: 100vw; height: 100vh; overflow: hidden; color: var(--ink); font-family: "IBM Plex Mono", monospace; }+ video { position: absolute; left: 0; top: 0; object-fit: fill; }+ .scan, .xbody { position: absolute; inset: 0; }+ /* the radiograph (#clipx): the same clip, filtered by the theme (xray.js), shown only inside the tracked outline */+ #cv { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }+ .brand { position: absolute; left: 48px; top: 40px; font: 800 64px/1 "Bricolage Grotesque", sans-serif; text-shadow: 0 2px 18px var(--tagbg); }+ .brand b { color: var(--hot); }+ .tag { position: absolute; left: 50px; top: 116px; font: 500 26px/1 "IBM Plex Mono", monospace; letter-spacing: .06em; text-transform: uppercase;+ background: var(--tagbg); padding: 9px 12px; border-radius: 4px; }+ .hud { position: absolute; left: 48px; bottom: 40px; display: flex; gap: 22px; align-items: flex-end; }+ .card { background: var(--card); border: 1px solid var(--edge); border-radius: 6px; padding: 14px 18px; min-width: 150px; }+ .card b { display: block; font: 800 60px/1 "Bricolage Grotesque", sans-serif; font-variant-numeric: tabular-nums; }+ .card span { display: block; margin-top: 6px; font: 500 24px/1.2 "IBM Plex Mono", monospace; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; }+ .paper { min-width: 430px; }+ .paper b { font: 600 40px/1.25 "IBM Plex Mono", monospace; white-space: pre; }+ .paper b i { color: var(--hot); font-style: normal; }+ .end { position: absolute; top: 0; bottom: 0; right: 0; width: 900px; background: var(--card); z-index: 5; padding: 0 64px; box-sizing: border-box;+ display: flex; flex-direction: column; justify-content: center; opacity: 0; }+ .end h2 { margin: 0; font: 800 84px/1.0 "Bricolage Grotesque", sans-serif; text-wrap: balance; }+ .end h2 em { color: var(--hot); font-style: normal; }+ .end p { margin: 26px 0 0; font: 500 26px/1.45 "IBM Plex Mono", monospace; }+ .end .receipt { margin-top: 30px; border: 2px solid var(--line); border-radius: 6px; padding: 16px 20px; font: 500 26px/1.45 "IBM Plex Mono", monospace; color: var(--line); }+ .end .muted { color: var(--muted); font-size: 24px; }+ /* phone (9:16): every word inside the reels safe box (x 64-916, y 220-1440), 43 px and up */+ .portrait .brand { left: 64px; font-size: 68px; }+ .portrait .tag { left: 64px; font-size: 43px; padding: 6px 12px; letter-spacing: .02em; }+ .portrait .hud { left: 64px; right: 164px; bottom: auto; gap: 20px; }+ .portrait .card b { font-size: 84px; }+ .portrait .card span { font-size: 43px; letter-spacing: 0; }+ .portrait .paper { flex: 1; min-width: 0; }+ .portrait .paper b { font-size: 44px; line-height: 84px; }+ .portrait .end { left: 0; width: auto; top: 0; bottom: 0; background: var(--ground); padding: 220px 164px 480px 64px; }+ .portrait .end h2 { font-size: 92px; }+ .portrait .end p, .portrait .end .receipt { font-size: 43px; line-height: 1.3; }+ .portrait .end .muted { font-size: 43px; }+ /* the sync marker: 6x6 px at (width-10, height-10), black, white only on an event's frame; keep it uncovered */+ #xmark { position: absolute; width: 6px; height: 6px; background: #000; z-index: 99; }+</style>+</head>+<body>+<div class="stage" id="stage">+ <video id="clip" src="media/clip.webm" muted playsinline></video>+ <div class="scan" id="scan"><div class="xbody" id="xbody"><video id="clipx" class="xv" src="media/clip.webm" muted playsinline></video></div></div>+ <canvas id="cv"></canvas>+ <div class="brand" id="brand">X<b>-</b>RAY</div>+ <div class="tag" id="brand-tag"></div>+ <div class="hud" id="hud" style="opacity:0">+ <div class="card"><b id="nk">0</b><span id="noun">events heard</span></div>+ <div class="card paper" id="textcard"><b id="pp"> </b><span id="textlabel">read from the clip</span></div>+ </div>+ <div class="end" id="end">+ <h2 id="end-h"></h2>+ <p id="end-measured"></p>+ <div class="receipt" id="receipt"></div>+ <p class="muted" id="end-drawn"></p>+ </div>+ <div id="xmark"></div>+</div>+<script src="cue.js"></script>+<script src="xray.js"></script>+<script src="mechanism.js"></script>+</body>+</html>diff --git a/skills/showtime/templates/xray/mechanism.js b/skills/showtime/templates/xray/mechanism.jsnew file mode 100644index 0000000..135e703--- /dev/null+++ b/skills/showtime/templates/xray/mechanism.js@@ -0,0 +1,86 @@+/* A manual typewriter, inside (showtime X-ray mechanism library: typewriter).+ Design space: about 1430 x 930 units, the machine seen from the typist's side, keys at the bottom, the platen+ at the top. Each event is a keystroke: its key goes down, its typebar swings up to the print point and the+ ribbon lifts, PEAKING on the event's frame; the escapement lets the carriage step one letter on that frame.+ An event of kind "return" (showtime.json "xray": {"kinds": {"18": "return"}}) winds the mainspring back.+ Which key: only from evidence ("xray": {"text": "..."} = what the paper in the clip shows, one character per+ keystroke); without it the keystroke lights the typebar fan, never a guessed key. */+(function () {+ var deg = function (a) { return a * Math.PI / 180; };+ var ROWS = [['qwertyuiop', 493, 773, 81], ['asdfghjkl', 500, 867, 83], ['zxcvbnm,.-', 533, 953, 85.5]];+ var KEYS = [];+ ROWS.forEach(function (r) { r[0].split('').forEach(function (ch, i) { KEYS.push([ch, r[1] + i * r[3], r[2]]); }); });+ var BYX = KEYS.slice().sort(function (a, b) { return a[1] - b[1]; }), NB = BYX.length, BAR = {};+ BYX.forEach(function (k, i) { BAR[k[0]] = i; });+ var PP = [920, 286], CB = [920, 330], RX = 330, RY = 104; // print point, typebar segment centre and radii+ var SPACE = [[693, 1012], [1293, 1012], [1293, 1056], [693, 1056]];+ var BODY = [[440, 244], [1460, 267], [1547, 600], [1547, 1080], [260, 1080], [300, 613]];+ function pivot(i) { var th = deg(166 - 152 * i / (NB - 1)); return [CB[0] + RX * Math.cos(th), CB[1] + RY * Math.sin(th)]; }+ function rest(i) { var p = pivot(i); return [p[0] + 0.62 * (CB[0] - p[0]), p[1] + 0.62 * (CB[1] - p[1])]; }++ XRAY.mechanism({+ name: 'typewriter',+ noun: 'keystrokes', noun1: 'keystroke', one: 'a keystroke', textLabel: 'on the paper',+ box: [250, 140, 1700, 1080],+ // four points to place on the clip's reference frame (showtime.json "xray": {"place": {...}})+ anchors: { key_q: [493, 773], key_p: [1222, 773], key_m: [1046, 953], print_point: PP },+ outline: BODY,+ headline: function (n) { return '<em>' + n + '</em> keystrokes,<br>seen from inside<br>the machine.'; },+ measured: function (n, text) {+ return 'Each keystroke lit up on the frame its sound was heard' + (text ? '; which key, read from the paper in the clip.' : '.');+ },+ drawn: 'Inside view: a generic manual typewriter, drawn, not this exact machine.',+ // the live page: pads that play the next event of each kind from the real clip+ touch: { verb: 'Type on it', by: 'ch' },+ draw: function (k) {+ var C = k.C, g = k.g, s = k.s, hot = k.hot && k.kind === 'event', ch = hot ? k.ch : null;+ var n = k.n, bar = ch && ch !== ' ' && BAR[ch.toLowerCase()] != null ? BAR[ch.toLowerCase()] : -1;+ var rets = k.ev.filter(function (e) { return e.kind === 'return'; });+ var ret = 0; // 0..1 while a carriage return winds the spring back+ rets.forEach(function (e) { var d = k.since(e.i); if (d >= -2 && d < 25) ret = Math.max(ret, 1 - Math.abs(d - 8) / 17); });+ var panel = k.view === 'panel';+ if (panel) { // the machine's outline and its keyboard, for the panel only+ k.poly(BODY, C.faint, 2);+ KEYS.forEach(function (key) { k.ell(key[1], key[2], 26, 22, (hot && key[0] === (ch || '').toLowerCase()) ? C.hot : C.faint, 2, 0, false); });+ k.poly(SPACE, ch === ' ' && hot ? C.hot : C.faint, 2);+ }+ // key levers under the cover: every key to its typebar+ BYX.forEach(function (key, i) { if (i === bar) return; k.seg([key[1], key[2] - 30], pivot(i), panel ? C.l(.16) : C.l(.12), 1.5); });+ // the typebar fan, at rest+ for (var i = 0; i < NB; i++) { if (i === bar) continue; k.seg(pivot(i), rest(i), hot && bar < 0 && ch !== ' ' ? C.hot : C.soft, 3.5); }+ var arc = []; for (var a = 0; a < 40; a++) { var th = deg(166 - 152 * a / 39); arc.push([CB[0] + RX * Math.cos(th), CB[1] + RY * Math.sin(th)]); }+ g.strokeStyle = C.line; g.lineWidth = k.lw(3); k.path(arc); g.stroke();+ // ribbon spools (they turn a little each keystroke) and the ribbon, lifted on a strike+ k.ell(640, 352, 46, 20, C.line, 3, deg(n * 9), true); k.ell(1200, 356, 46, 20, C.line, 3, deg(n * 9), true);+ g.strokeStyle = hot ? C.hot : C.soft; g.lineWidth = k.lw(3); k.path([[640, 340], [920, 300 - (hot ? s * 12 : 0)], [1200, 344]]); g.stroke();+ // escapement star wheel (one tooth per keystroke) and the carriage rack+ k.ell(1336, 430, 40, 18, hot ? C.hot : C.line, 3.5, deg(n * 360 / 16), true);+ k.seg([700, 240], [1690, 252], C.soft, 3);+ for (var r = 0; r < 40; r++) { var x = 700 + r * 24.75, y = 240 + r * 0.3; k.seg([x, y], [x + 8, y + 9], C.faint, 2); }+ k.seg([1336, 430], [1336, 252], hot ? C.hot : C.soft, 3);+ // mainspring drum: unwinds a little per letter, winds back on a return+ var turns = n * 0.06 * (1 - ret), sp = [];+ for (var q = 0; q <= 120; q++) { var aa = deg(q * 12) + turns, rr = 6 + q * 0.32; sp.push([560 + Math.cos(aa) * rr, 430 + Math.sin(aa) * rr * 0.45]); }+ g.strokeStyle = ret > 0.05 ? C.hot : C.line; g.lineWidth = k.lw(2.5); k.path(sp); g.stroke();+ k.seg([598, 430], [700, 244], ret > 0.05 ? C.hot : C.soft, 3);+ k.seg([560, 226], [470, 150], ret > 0.05 ? C.hot : C.soft, 6);+ // the struck key, its lever and its typebar on the way up; the type hits the print point on the event frame+ if (hot) {+ g.shadowColor = C.hot; g.shadowBlur = 22 * s;+ if (ch === ' ') k.poly(SPACE, C.hot, 5, k.since(k.cur) >= 0 && k.since(k.cur) < 3 ? C.hot : null);+ else if (bar >= 0) {+ var key = BYX[bar], pv = pivot(bar), rs = rest(bar), tip = [rs[0] + (PP[0] - rs[0]) * s, rs[1] + (PP[1] - rs[1]) * s];+ k.ell(key[1], key[2], 30, 26, C.hot, 5, 0, false); k.seg([key[1], key[2] - 30], pv, C.hot, 4); k.seg(pv, tip, C.hot, 7);+ }+ if (k.since(k.cur) >= 0 && k.since(k.cur) < 3 && ch !== ' ') k.dot(PP, Math.max(6, 16 * Math.min(1.5, k.scale)), C.hot);+ g.shadowBlur = 0;+ }+ // labels (drawn where there is room for them: the panel, or in place when there is no panel)+ k.label('typebars', [1180, 330], 120, -150, hot && ch !== ' ');+ k.label('key levers', [700, 640], -180, 40, hot);+ k.label('escapement', [1376, 430], 200, 40, hot);+ k.label('mainspring', [520, 430], -160, -60, ret > 0.05);+ k.label('print point', PP, -260, -140, hot && s > 0.8);+ }+ });+})();diff --git a/skills/showtime/templates/xray/media/clip.webm b/skills/showtime/templates/xray/media/clip.webmnew file mode 100644index 0000000..2bfe549Binary files /dev/null and b/skills/showtime/templates/xray/media/clip.webm differdiff --git a/skills/showtime/templates/xray/showtime.json b/skills/showtime/templates/xray/showtime.jsonnew file mode 100644index 0000000..9a9e14e--- /dev/null+++ b/skills/showtime/templates/xray/showtime.json@@ -0,0 +1,41 @@+{+ "title": "X-ray",+ "width": 1080,+ "height": 1920,+ "fps": 30,+ "duration": 12.8,+ "background": "#070b16",+ "poster": 4.24,+ "render": {+ "poster_bake": "off"+ },+ "audio": "audio/mix.json",+ "xray": {+ "layout": "through",+ "skip": [+ 0+ ],+ "text": "Sounds of changes.",+ "place": {+ "key_q": [+ 163,+ 256+ ],+ "key_p": [+ 419,+ 256+ ],+ "key_m": [+ 355,+ 317+ ],+ "print_point": [+ 311,+ 72+ ]+ },+ "credit": "Clip: Erik Pålsson / Sounds of Changes, CC BY 3.0.",+ "marker": true,+ "offset_frames": 0+ }+}diff --git a/skills/showtime/templates/xray/xray.js b/skills/showtime/templates/xray/xray.jsnew file mode 100644index 0000000..8a95564--- /dev/null+++ b/skills/showtime/templates/xray/xray.js@@ -0,0 +1,372 @@+/* xray.js: the X-ray page's runtime. It reads xray.json (from `showtime xray analyze`) and showtime.json "xray",+ lays the page out (the clip, the inside drawn in place on the tracked object and/or in a panel, the counters,+ the sound strip, the end card) and draws the sync marker `showtime xray verify` reads. Your object's drawing+ is mechanism.js (from skills/showtime/library/xray/<object>/), the beats are cue.js: edit those, not this.++ Frames, never seconds: an event's film frame is its `frame` in xray.json (the clip starts at film frame 0); a+ strike PEAKS on that frame and starts 3 frames before it (the sound is the impact). */+(function () {+ 'use strict';+ var X = window.XRAY = { mech: null, ready: false };+ X.mechanism = function (m) { X.mech = m; };++ // palettes: dark = the radiograph (cyan lines on night), light = a blueprint on paper (navy lines); red = acting now+ var THEMES = {+ dark: { l: [143, 211, 255], i: [233, 243, 255], bg: [7, 11, 22], h: [255, 74, 61], muted: '#a9bdd6', glow: 8,+ filter: 'grayscale(1) invert(1) brightness(.62) contrast(1.7) sepia(1) hue-rotate(165deg) saturate(2.6)' },+ light: { l: [24, 62, 120], i: [18, 30, 52], bg: [244, 240, 231], h: [214, 40, 57], muted: '#55627a', glow: 0,+ filter: 'grayscale(1) brightness(1.55) contrast(.62) sepia(.25)' }+ };+ function palette(name) {+ var P = THEMES[name] || THEMES.dark, f = function (c) { return function (a) { return 'rgba(' + c.join(',') + ',' + a + ')'; }; };+ var C = { name: THEMES[name] ? name : 'dark', l: f(P.l), i: f(P.i), bg: f(P.bg), h: f(P.h), muted: P.muted, glow: P.glow, filter: P.filter };+ C.line = C.l(1); C.soft = C.l(name === 'light' ? .55 : .5); C.faint = C.l(name === 'light' ? .22 : .2); C.hot = C.h(1); C.ink = C.i(1);+ C.ground = C.bg(1);+ return C;+ }+ var C = palette('dark');+ var RISE = 3, FALL = 7; // frames before / after an event's frame++ // ------------------------------------------------------------------ 3x3 maths (row-major arrays of 9)+ function solve(A, b) {+ var n = b.length, M = A.map(function (r, i) { return r.concat([b[i]]); });+ for (var c = 0; c < n; c++) {+ var p = c;+ for (var r = c + 1; r < n; r++) if (Math.abs(M[r][c]) > Math.abs(M[p][c])) p = r;+ var tmp = M[c]; M[c] = M[p]; M[p] = tmp;+ for (r = 0; r < n; r++) {+ if (r === c) continue;+ var f = M[r][c] / M[c][c];+ for (var k = c; k <= n; k++) M[r][k] -= f * M[c][k];+ }+ }+ return M.map(function (r, i) { return r[n] / r[i]; });+ }+ function hom(src, dst) { // the homography taking 4 points onto 4 points+ var A = [], b = [];+ for (var i = 0; i < 4; i++) {+ var x = src[i][0], y = src[i][1], u = dst[i][0], v = dst[i][1];+ A.push([x, y, 1, 0, 0, 0, -u * x, -u * y]); b.push(u);+ A.push([0, 0, 0, x, y, 1, -v * x, -v * y]); b.push(v);+ }+ var h = solve(A, b); h.push(1); return h;+ }+ function mul(a, b) {+ var o = [];+ for (var r = 0; r < 3; r++) for (var c = 0; c < 3; c++) o.push(a[r * 3] * b[c] + a[r * 3 + 1] * b[3 + c] + a[r * 3 + 2] * b[6 + c]);+ return o;+ }+ function apply(h, x, y) { var w = h[6] * x + h[7] * y + h[8]; return [(h[0] * x + h[1] * y + h[2]) / w, (h[3] * x + h[4] * y + h[5]) / w]; }+ function affine(sx, sy, tx, ty) { return [sx, 0, tx, 0, sy, ty, 0, 0, 1]; }+ function fit(box, r, pad) { // box [x0,y0,x1,y1] into rect {x,y,w,h}, aspect kept, centred+ var bw = box[2] - box[0], bh = box[3] - box[1], s = Math.min((r.w - 2 * pad) / bw, (r.h - 2 * pad) / bh);+ return affine(s, s, r.x + (r.w - bw * s) / 2 - box[0] * s, r.y + (r.h - bh * s) / 2 - box[1] * s);+ }+ function bbox(pts) {+ var xs = pts.map(function (p) { return p[0]; }), ys = pts.map(function (p) { return p[1]; });+ return [Math.min.apply(null, xs), Math.min.apply(null, ys), Math.max.apply(null, xs), Math.max.apply(null, ys)];+ }+ X.math = { hom: hom, mul: mul, apply: apply, fit: fit, bbox: bbox };++ // ------------------------------------------------------------------ loading+ function getJSON(url) { return fetch(url, { cache: 'no-store' }).then(function (r) { return r.ok ? r.json() : null; }); }+ var cue = function (k, d) { return (window.CUE && typeof window.CUE[k] === 'number') ? window.CUE[k] : d; };++ function load() {+ var fonts = document.fonts ? Promise.all([+ document.fonts.load('800 60px "Bricolage Grotesque"'), document.fonts.load('500 24px "IBM Plex Mono"'),+ document.fonts.load('600 24px "IBM Plex Mono"')]).catch(function () {}) : Promise.resolve();+ var notes = getJSON('notes.json').catch(function () { return null; }); // optional: `showtime xray notes`+ return Promise.all([getJSON('xray.json'), getJSON('showtime.json'), fonts, notes]).then(function (r) {+ if (!r[0] || !r[0].events) throw new Error('xray.json is missing or has no events: run `showtime xray analyze <clip> --job <job>` and copy <job>/xray/xray.json next to index.html');+ if (!X.mech) throw new Error('no mechanism: mechanism.js must call XRAY.mechanism({...})');+ setup(r[0], (r[1] && r[1].xray) || {}, r[3]);+ });+ }++ // ------------------------------------------------------------------ layout and events+ var S = {}; // everything setup() works out+ function setup(doc, cfg, notesDoc) {+ C = palette(cfg.theme || 'dark');+ var info = ST.info(), W = info.width, H = info.height, fps = info.fps || 30;+ var src = doc.source || {}, sw = src.width || 1920, sh = src.height || 1080, sfps = src.fps || fps;+ var m = X.mech, track = doc.track && doc.track.H && doc.track.H.length ? doc.track : null;+ var portrait = H > W * 1.2, inplace = cfg.layout !== 'split' && !!track;+ var clip, panel = null, hud, lay = null;+ if (portrait) { // phone: the clip on top, the inside below, all text in the reels safe box+ var ch = W * sh / sw, cw = W;+ if (ch > H * 0.36) { ch = H * 0.36; cw = ch * sw / sh; }+ clip = { x: (W - cw) / 2, y: 0, w: cw, h: ch };+ var top = Math.max(H * 0.115, ch + 28), hudTop = H * 0.75 - 172;+ lay = { brand: top, tag: top + 76, panelTop: top + 150, hud: hudTop };+ panel = { x: 40, y: lay.panelTop, w: W - 80, h: hudTop - 16 - lay.panelTop };+ hud = 'portrait';+ } else if (inplace && cfg.panel) { // 16:9 see-through with the panel beside it ("xray": {"panel": true})+ var w3 = W * 0.6, h3 = w3 * sh / sw;+ if (h3 > H - 300) { h3 = H - 300; w3 = h3 * sw / sh; }+ clip = { x: 40, y: (H - h3) / 2, w: w3, h: h3 };+ panel = { x: clip.x + w3 + 40, y: 150, w: W - w3 - 120, h: H - 150 - 230 };+ hud = 'side';+ } else if (inplace) {+ var s = Math.max(W / sw, H / sh);+ clip = { x: (W - sw * s) / 2, y: (H - sh * s) / 2, w: sw * s, h: sh * s };+ hud = 'over';+ } else {+ var w2 = W * 0.56, h2 = w2 * sh / sw;+ clip = { x: 0, y: (H - h2) / 2, w: w2, h: h2 };+ panel = { x: w2 + 40, y: 150, w: W - w2 - 80, h: H - 150 - 230 };+ hud = 'side';+ }+ // design space (the mechanism's own coordinates) -> the clip's reference frame -> each frame -> the film+ var place = null;+ if (cfg.place && m.anchors) {+ var names = Object.keys(m.anchors).filter(function (k) { return cfg.place[k]; });+ if (names.length >= 4) {+ names = names.slice(0, 4);+ place = hom(names.map(function (k) { return m.anchors[k]; }), names.map(function (k) { return cfg.place[k]; }));+ }+ }+ if (!place && track && track.region) place = fitInto(m.box, bbox(track.region));+ var toFilm = affine(clip.w / sw, clip.h / sh, clip.x, clip.y);+ var off = Math.round(Number(cfg.offset_frames) || 0);+ if (off) ['clip', 'clipx'].forEach(function (id) { // the clip starts at film frame `off`: its clock runs behind the film's+ el(id).setAttribute('data-offset', String(-off / fps));+ });+ // events: film frames, kinds (skip / return / ... from showtime.json), characters read off the clip+ var kinds = {}, skip = cfg.skip || [];+ skip.forEach(function (i) { kinds[i] = 'skip'; });+ Object.keys(cfg.kinds || {}).forEach(function (i) { kinds[i] = cfg.kinds[i]; });+ var text = cfg.text || '', ti = 0;+ var ev = doc.events.map(function (e, i) {+ // the film frame: the clip's own frame when the film runs at the clip's rate, else floor(t * film fps)+ // + "xray".offset_frames (a trimmed or shifted clip), the same arithmetic as `showtime xray verify`+ var fr = (Math.abs(sfps - fps) > 0.01 ? Math.floor(e.t * fps + 1e-9) : e.frame) + off;+ var kind = kinds[i] || 'event', o = { i: i, frame: fr, t: e.t, strength: e.strength, band: e.band, kind: kind, ch: null };+ if (kind === 'event' && ti < text.length) o.ch = text[ti++];+ return o;+ });+ S = { off: off, W: W, H: H, fps: fps, sfps: sfps, sw: sw, sh: sh, doc: doc, cfg: cfg, m: m, track: track, inplace: inplace,+ clip: clip, panel: panel, hud: hud, lay: lay, place: place, toFilm: toFilm, ev: ev, text: text, portrait: portrait,+ panelMap: panel ? fit(m.box, panel, 24) : null, dur: info.duration, notes: [] };+ attachNotes(notesDoc);+ build();+ X.ready = true;+ }+ // notes.json (pitches, from `showtime xray notes`): each note joins the event within 3 frames of it, so a+ // mechanism can say WHICH tooth, string or key, from the sound itself+ function attachNotes(nd) {+ S.ev.forEach(function (e) { e.notes = []; });+ S.music = nd || null; // the whole notes.json: tempo map, quantized, voices, guitar, drums+ if (!nd) return;+ var src = (nd.notes && nd.notes.length ? nd.notes : (nd.drums || []));+ S.notes = src.map(function (n, j) {+ return { i: n.i != null ? n.i : j, pitch: n.pitch, name: n.name || n.piece, piece: n.piece, t: n.t,+ frame: Math.floor(n.t * S.fps + 1e-9) + S.off, end: n.end, velocity: n.velocity, confidence: n.confidence, hz: n.hz };+ });+ S.notes.forEach(function (n) {+ var best = null, bd = 4;+ S.ev.forEach(function (e) { var d = Math.abs(e.frame - n.frame); if (d < bd) { bd = d; best = e; } });+ if (best) best.notes.push(n);+ });+ }+ function fitInto(box, b) { // the design box stretched onto the region's bounding box+ return affine((b[2] - b[0]) / (box[2] - box[0]), (b[3] - b[1]) / (box[3] - box[1]),+ b[0] - box[0] * (b[2] - b[0]) / (box[2] - box[0]), b[1] - box[1] * (b[3] - b[1]) / (box[3] - box[1]));+ }++ // ------------------------------------------------------------------ the page+ function px(v) { return v.toFixed(1) + 'px'; }+ function el(id) { return document.getElementById(id); }+ function build() {+ var c = S.clip;+ ['clip', 'clipx'].forEach(function (id) {+ var v = el(id); v.style.left = px(c.x); v.style.top = px(c.y); v.style.width = px(c.w); v.style.height = px(c.h);+ });+ el('scan').hidden = !S.inplace;+ var st = el('stage'); st.className = 'stage ' + S.hud + (S.inplace ? ' inplace' : '') + ' ' + C.name;+ [['--line', C.line], ['--ink', C.ink], ['--muted', C.muted], ['--hot', C.hot], ['--ground', C.ground],+ ['--card', C.bg(.84)], ['--edge', C.l(.35)], ['--tagbg', C.bg(.74)]].forEach(function (v) { st.style.setProperty(v[0], v[1]); });+ el('clipx').style.filter = C.filter;+ var cv = el('cv'); cv.width = S.W; cv.height = S.H;+ el('brand-tag').textContent = S.cfg.tag || (S.inplace ? 'your clip · seen through' : 'your clip · the inside, in sync');+ el('noun').textContent = (S.m.noun || 'events') + (S.portrait ? '' : ' heard');+ var tc = el('textcard');+ if (S.text) el('textlabel').textContent = S.m.textLabel || 'read from the clip'; else tc.hidden = true;+ var n = S.ev.filter(function (e) { return e.kind === 'event'; }).length;+ el('end-h').innerHTML = S.cfg.headline || (S.m.headline ? S.m.headline(n) : '<em>' + n + '</em> ' + (S.m.noun || 'events') + ',<br>seen from inside.');+ el('end-measured').textContent = S.cfg.measured || (S.m.measured ? S.m.measured(n, !!S.text, S.notes.length > 0) : 'Each event lit up on the frame its sound was heard.');+ el('receipt').textContent = S.cfg.receipt || (n + ' ' + (S.m.noun || 'events') + ' measured from the clip’s sound');+ el('end-drawn').textContent = [S.cfg.drawn || S.m.drawn || 'The inside is drawn: a generic mechanism of this kind of object.',+ S.cfg.credit || ''].join(' ').trim();+ if (S.lay) {+ el('brand').style.top = px(S.lay.brand); el('brand-tag').style.top = px(S.lay.tag); el('hud').style.top = px(S.lay.hud);+ }+ var mk = el('xmark'); mk.style.left = px(S.W - 10); mk.style.top = px(S.H - 10);+ }++ // the track's frame for a film frame; "xray".track_until (seconds of the clip) holds the last good one, for a clip+ // whose end the tracker loses (the object turned over, a hand covering it)+ function trackFrame(f) {+ var sf = Math.round((f - S.off) * S.sfps / S.fps), last = S.track.H.length - 1;+ if (typeof S.cfg.track_until === 'number') last = Math.min(last, Math.floor(S.cfg.track_until * S.sfps));+ return Math.max(0, Math.min(last, sf));+ }++ // ------------------------------------------------------------------ drawing kit handed to the mechanism+ function kit(g, view, f, t, alpha) {+ var map;+ if (view === 'panel') map = S.panelMap;+ else {+ map = mul(S.toFilm, mul(S.track.H[trackFrame(f)], S.place));+ }+ var pr = function (x, y) { return apply(map, x, y); };+ var scale = Math.sqrt(Math.abs(map[0] * map[4] - map[1] * map[3]));+ var K = { g: g, view: view, f: f, t: t, alpha: alpha, C: C, ease: ST.ease, pr: pr, scale: scale, ev: S.ev, text: S.text, notes: S.notes, music: S.music, cfg: S.cfg, fps: S.fps, panel: S.panel,+ labels: view === 'panel' || !S.panel };+ K.env = function (i) { // 0..1: the motion of event i, 1 exactly on its frame+ var d = f - S.ev[i].frame;+ if (d < -RISE || d > FALL) return 0;+ if (d <= 0) { var a = (d + RISE) / RISE; return a * a; }+ return 1 - ST.ease.inOutSine(d / FALL);+ };+ K.since = function (i) { return f - S.ev[i].frame; };+ K.beat = function (tt) { // the smooth tempo map (notes.json tempo.map): beat at time tt, never a jump+ var m = S.music && S.music.tempo && S.music.tempo.map;+ if (!m || m.length < 2) return tt * ((S.music && S.music.tempo && S.music.tempo.bpm) || 120) / 60;+ if (tt <= m[0][0]) return m[0][1] + (tt - m[0][0]) * (m[1][1] - m[0][1]) / (m[1][0] - m[0][0]);+ for (var q = 1; q < m.length; q++) if (tt <= m[q][0]) return m[q - 1][1] + (tt - m[q - 1][0]) * (m[q][1] - m[q - 1][1]) / (m[q][0] - m[q - 1][0]);+ var a = m[m.length - 2], b = m[m.length - 1]; return b[1] + (tt - b[0]) * (b[1] - a[1]) / (b[0] - a[0]);+ };+ K.count = function (kind) { var n = 0; S.ev.forEach(function (e) { if (e.frame <= f && e.kind === (kind || 'event')) n++; }); return n; };+ K.cur = -1; K.s = 0;+ S.ev.forEach(function (e, i) { if (e.kind === 'skip') return; var s = K.env(i); if (s > K.s) { K.s = s; K.cur = i; } });+ K.hot = K.s > 0.05; K.ch = K.cur >= 0 ? S.ev[K.cur].ch : null; K.kind = K.cur >= 0 ? S.ev[K.cur].kind : null;+ K.n = K.count('event');+ var lw = function (w) { return Math.max(1, w * (view === 'panel' ? Math.min(1.6, scale) : Math.min(1.4, scale * 1.2))); };+ K.lw = lw;+ K.path = function (pts, close) { g.beginPath(); pts.forEach(function (p, i) { var q = pr(p[0], p[1]); if (i) g.lineTo(q[0], q[1]); else g.moveTo(q[0], q[1]); }); if (close) g.closePath(); };+ K.seg = function (a, b, col, w) { g.strokeStyle = col; g.lineWidth = lw(w); K.path([a, b]); g.stroke(); };+ K.poly = function (pts, col, w, fill) { K.path(pts, true); if (fill) { g.fillStyle = fill; g.fill(); } if (col) { g.strokeStyle = col; g.lineWidth = lw(w); g.stroke(); } };+ K.ell = function (cx, cy, rx, ry, col, w, rot, teeth) {+ var pts = [];+ for (var k = 0; k <= 72; k++) { var a = k * Math.PI / 36, r = teeth && k % 2 ? 0.8 : 1; pts.push([cx + Math.cos(a + (rot || 0)) * rx * r, cy + Math.sin(a + (rot || 0)) * ry * r]); }+ g.strokeStyle = col; g.lineWidth = lw(w); K.path(pts, true); g.stroke();+ };+ K.dot = function (p, r, col) { var q = pr(p[0], p[1]); g.fillStyle = col; g.beginPath(); g.arc(q[0], q[1], r, 0, Math.PI * 2); g.fill(); };+ K.label = function (text, at, dx, dy, on) {+ if (!K.labels) return;+ var p = pr(at[0], at[1]), r = view === 'panel' ? (S.portrait ? { x: S.W * 0.06, y: S.panel.y, w: S.W * 0.79, h: S.panel.h } : S.panel) : { x: 40, y: 170, w: S.W - 80, h: S.H - 400 };+ if (p[0] < r.x - 20 || p[0] > r.x + r.w + 20 || p[1] < r.y - 20 || p[1] > r.y + r.h + 20) return;+ var size = S.portrait ? 34 : 28;+ g.save(); g.font = '600 ' + size + 'px "IBM Plex Mono"'; g.shadowBlur = 0;+ var label = String(text).toUpperCase(), w = g.measureText(label).width + 22, hgt = size + 16;+ var x = Math.max(r.x + (dx < 0 ? w : 0), Math.min(r.x + r.w - (dx < 0 ? 0 : w), p[0] + dx));+ var y = Math.max(r.y + hgt / 2, Math.min(r.y + r.h - hgt / 2, p[1] + dy));+ g.globalAlpha = alpha * (K.labelAlpha == null ? 1 : K.labelAlpha);+ g.strokeStyle = on ? C.hot : C.i(.7); g.lineWidth = 1.5;+ g.beginPath(); g.moveTo(p[0], p[1]); g.lineTo(x, y); g.stroke();+ g.fillStyle = C.bg(.86); g.fillRect(dx < 0 ? x - w : x, y - hgt / 2, w, hgt);+ g.fillStyle = on ? C.hot : C.ink; g.textBaseline = 'middle'; g.textAlign = dx < 0 ? 'right' : 'left';+ g.fillText(label, dx < 0 ? x - 11 : x + 11, y + 1);+ g.restore();+ };+ return K;+ }++ // ------------------------------------------------------------------ per frame+ function drawView(g, view, f, t, alpha, clipRect) {+ var K = kit(g, view, f, t, alpha);+ K.labelAlpha = ST.progress(t, cue('labels', 1.4), cue('labels', 1.4) + 0.6, ST.ease.inOutCubic);+ g.save();+ if (clipRect) { g.beginPath(); g.rect(clipRect.x, clipRect.y, clipRect.w, clipRect.h); g.clip(); }+ g.globalAlpha = alpha; g.lineCap = 'round'; g.lineJoin = 'round';+ g.shadowColor = C.l(.8); g.shadowBlur = C.glow;+ S.m.draw(K);+ g.restore();+ }++ function strip(g, f, t, alpha) {+ var portrait = S.hud === 'portrait', x0, w, y0, h = portrait ? 150 : 50;+ if (portrait) { x0 = 64; w = S.W * 0.79; y0 = S.H * 0.75 + 70; }+ else if (S.hud === 'side') { x0 = S.panel.x; w = S.panel.w; y0 = S.H - 100; }+ else { x0 = S.W - 900; w = 852; y0 = S.H - 90; }+ var dur = (S.doc.source && S.doc.source.duration) || S.dur;+ g.save(); g.globalAlpha = alpha;+ g.fillStyle = C.bg(.8); g.fillRect(x0 - 16, y0 - 38, w + 32, h + 54);+ S.ev.forEach(function (e) {+ var x = x0 + (e.frame / S.fps) / dur * w, hh = Math.max(6, (0.35 + 0.65 * (e.strength || 0.5)) * h);+ var on = e.frame <= f, col = e.kind === 'skip' ? (on ? C.i(.55) : C.i(.2)) : (on ? C.hot : C.h(.32));+ g.fillStyle = col; g.fillRect(x - 2, y0 + h / 2 - hh / 2, 4, hh);+ });+ // now: a small caret over the ticks (no axis line, no full-height playhead: it must not read as a player's bar)+ var cx = x0 + Math.min(1, (f / S.fps) / dur) * w;+ g.fillStyle = C.ink; g.beginPath(); g.moveTo(cx - 9, y0 - 8); g.lineTo(cx + 9, y0 - 8); g.lineTo(cx, y0 + 4); g.closePath(); g.fill();+ g.font = '500 ' + (portrait ? 24 : 18) + 'px "IBM Plex Mono"'; g.fillStyle = C.muted; g.textBaseline = 'bottom'; g.textAlign = 'left';+ g.fillText(('the clip’s sound · red = ' + (S.m.one || 'an event')).toUpperCase(), x0, y0 - 12);+ g.restore();+ }++ function frame(t, fr) {+ if (!X.ready) return;+ var f = typeof fr === 'number' ? fr : Math.round(t * S.fps);+ var g = el('cv').getContext('2d');+ g.clearRect(0, 0, S.W, S.H);+ var a = cue('scan', 0.3), b = cue('scan_end', 1.3), p = ST.progress(t, a, b, ST.ease.inOutCubic);+ var show = ST.progress(t, a + 0.2, b, ST.ease.inOutCubic);+ // in place: the radiograph inside the object's outline, revealed by a scan line+ if (S.inplace) {+ var hf = mul(S.toFilm, S.track.H[trackFrame(f)]);+ // the see-through window: "xray".window (reference-frame pixels), else the tracked region+ var poly = (S.cfg.window || S.track.region || []).map(function (q) { var v = apply(hf, q[0], q[1]); return px(v[0]) + ' ' + px(v[1]); });+ el('xbody').style.clipPath = poly.length > 2 ? 'polygon(' + poly.join(', ') + ')' : 'none';+ var sy = S.clip.y + S.clip.h * p;+ el('scan').style.clipPath = 'inset(0 0 ' + px(Math.max(0, S.H - sy)) + ' 0)';+ // past "xray".track_until the see-through fades out instead of sliding off the object+ var hold = typeof S.cfg.track_until === 'number' ? 1 - ST.progress(f / S.fps, S.cfg.track_until, S.cfg.track_until + 0.3, ST.ease.inOutCubic) : 1;+ el('scan').style.opacity = hold.toFixed(3);+ drawView(g, 'inplace', f, t, show * hold, { x: 0, y: 0, w: S.W, h: Math.max(0, sy) });+ if (p > 0 && p < 1) scanline(g, S.clip.x, S.clip.w, sy);+ }+ if (S.panel) {+ var P = S.panel, py = P.y + P.h * p;+ g.save(); g.globalAlpha = ST.progress(t, a, a + 0.3);+ g.strokeStyle = C.l(.25); g.lineWidth = 1.5; g.strokeRect(P.x, P.y, P.w, P.h);+ g.restore();+ drawView(g, 'panel', f, t, show, { x: P.x, y: P.y, w: P.w, h: py - P.y });+ if (p > 0 && p < 1) scanline(g, P.x, P.w, py);+ }+ strip(g, f, t, ST.progress(t, a + 0.4, b + 0.4, ST.ease.inOutCubic));+ var K = kit(g, S.panel ? 'panel' : 'inplace', f, t, 1);+ el('nk').textContent = K.n;+ if (S.m.noun1) el('noun').textContent = (K.n === 1 ? S.m.noun1 : S.m.noun) + (S.portrait ? '' : ' heard');+ if (S.text) {+ var typed = S.ev.filter(function (e) { return e.kind === 'event' && e.ch != null && e.frame <= f; });+ var s = typed.map(function (e) { return e.ch; }).join(''), last = typed.length && f - typed[typed.length - 1].frame < 12;+ var pp = el('pp'); pp.textContent = '';+ if (!s) pp.innerHTML = ' ';+ else { pp.appendChild(document.createTextNode(last ? s.slice(0, -1) : s)); if (last) { var i = document.createElement('i'); i.textContent = s.slice(-1); pp.appendChild(i); } }+ }+ el('hud').style.opacity = ST.progress(t, a + 0.4, b + 0.4, ST.ease.inOutCubic);+ var e0 = cue('endcard', S.dur - 3);+ el('end').style.opacity = ST.progress(t, e0, e0 + 0.6, ST.ease.inOutCubic); // eased: no dead stop+ // the sync marker: white exactly on an event's film frame, black otherwise (showtime xray verify reads it)+ var on = S.ev.some(function (e) { return e.frame === f && e.kind !== 'skip'; });+ el('xmark').style.background = on ? '#fff' : '#000';+ }+ function scanline(g, x, w, y) {+ var gr = g.createLinearGradient(0, y - 40, 0, y + 3);+ gr.addColorStop(0, C.l(0)); gr.addColorStop(1, C.l(.9));+ g.fillStyle = gr; g.fillRect(x, y - 40, w, 43);+ }++ // the live page (`showtime export html <project> --interactive`): the same events set up again with another look,+ // {theme: 'light' | 'dark', layout: 'through' | 'split'}; the caller seeks to draw the frame+ X.restyle = function (over) {+ if (!X.ready) return false;+ setup(S.doc, Object.assign({}, S.cfg, over || {}), S.music);+ return true;+ };++ ST.waitFor(load(), 'xray.json');+ ST.onSeek(function (t, fr) { frame(t, fr); });+})();diff --git a/skills/showtime/templates/xray/xray.json b/skills/showtime/templates/xray/xray.jsonnew file mode 100644index 0000000..9d965ec--- /dev/null+++ b/skills/showtime/templates/xray/xray.json@@ -0,0 +1 @@+{"schema":"showtime.xray/1","source":{"file":"clip.webm","sha256":"d37ef9b4409d00f6ffcdf716b2cdf92a397e6207f8924f1ded2d1db693ea6dab","fps":30,"duration":9.408,"width":640,"height":360},"detector":{"engine":"envelope","min_gap":0.12,"sensitivity":0.5,"from":0.0,"to":null},"events":[{"i":0,"t":0.368,"frame":11,"strength":0.051,"band":"low"},{"i":1,"t":0.587,"frame":17,"strength":0.973,"band":"high"},{"i":2,"t":1.145,"frame":34,"strength":0.691,"band":"high"},{"i":3,"t":1.656,"frame":49,"strength":0.76,"band":"high"},{"i":4,"t":1.918,"frame":57,"strength":1.0,"band":"high"},{"i":5,"t":2.518,"frame":75,"strength":0.711,"band":"high"},{"i":6,"t":2.729,"frame":81,"strength":0.832,"band":"high"},{"i":7,"t":3.02,"frame":90,"strength":0.126,"band":"high"},{"i":8,"t":3.555,"frame":106,"strength":0.677,"band":"high"},{"i":9,"t":4.204,"frame":126,"strength":0.767,"band":"high"},{"i":10,"t":5.086,"frame":152,"strength":0.109,"band":"high"},{"i":11,"t":5.48,"frame":164,"strength":0.7,"band":"high"},{"i":12,"t":5.799,"frame":173,"strength":0.977,"band":"high"},{"i":13,"t":6.349,"frame":190,"strength":0.807,"band":"high"},{"i":14,"t":6.785,"frame":203,"strength":0.757,"band":"high"},{"i":15,"t":7.268,"frame":218,"strength":0.772,"band":"high"},{"i":16,"t":7.836,"frame":235,"strength":0.273,"band":"high"},{"i":17,"t":8.192,"frame":245,"strength":0.554,"band":"high"},{"i":18,"t":8.942,"frame":268,"strength":0.567,"band":"high"}],"dropped":[{"t":0.305,"why":"33 dB under the loud events (below the sensitivity gate)"},{"t":3.113,"why":"within min_gap of event 7"},{"t":7.008,"why":"36 dB under the loud events (below the sensitivity gate)"},{"t":7.764,"why":"34 dB under the loud events (below the sensitivity gate)"},{"t":8.577,"why":"31 dB under the loud events (below the sensitivity gate)"}],"track":{"ref_frame":15,"region":[[125.0,70.0],[500.0,75.0],[530.0,170.0],[530.0,360.0],[95.0,360.0],[100.0,175.0]],"mask":[[[60.0,200.0],[235.0,200.0],[235.0,360.0],[60.0,360.0]]],"H":[[0.9961,-0.024,7.3274,0.0206,0.9871,0.3111,1.1e-05,-3.3e-05,1.0],[0.9991,-0.0246,5.9602,0.0201,0.9895,0.5255,1.3e-05,-3.3e-05,1.0],[1.0001,-0.0229,4.7957,0.0189,0.9922,0.8,1.3e-05,-3.1e-05,1.0],[1.0008,-0.0228,4.3298,0.0183,0.9931,1.0921,1.4e-05,-3.1e-05,1.0],[1.0016,-0.0217,3.5679,0.0175,0.994,0.927,1.3e-05,-3.2e-05,1.0],[1.0033,-0.0199,2.9265,0.0162,0.9958,0.5442,1.3e-05,-2.8e-05,1.0],[1.0011,-0.0193,3.225,0.0141,0.9936,0.9279,1e-05,-2.8e-05,1.0],[0.9998,-0.0163,3.0827,0.0122,0.994,0.6429,6e-06,-2.2e-05,1.0],[0.9973,-0.0164,3.4405,0.009439,0.9875,1.9584,3e-06,-2.5e-05,1.0],[0.995,-0.0149,3.6953,0.008081,0.9866,1.8036,0.0,-2.4e-05,1.0],[0.9937,-0.0134,3.839,0.006431,0.9863,1.6683,-1e-06,-2.3e-05,1.0],[0.9953,-0.0123,3.4106,0.005799,0.9877,1.144,1e-06,-2.1e-05,1.0],[0.9938,-0.00954,3.2123,0.004103,0.9886,1.3831,-2e-06,-1.8e-05,1.0],[0.993,-0.008028,3.0099,0.002607,0.9886,1.5536,-3e-06,-1.7e-05,1.0],[0.9968,-0.005479,1.6647,0.001374,0.9949,0.6092,-1e-06,-9e-06,1.0],[1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0],[1.0003,0.002107,-0.6383,-0.001075,1.0044,-0.9403,-1e-06,4e-06,1.0],[1.0029,0.007981,-2.8295,-0.00334,1.0092,-1.3323,-2e-06,1.3e-05,1.0],[1.0058,0.00716,-4.1571,-0.003585,1.011,-2.2775,1e-06,1e-05,1.0],[1.0084,0.007865,-5.4789,-0.004663,1.0111,-2.1196,3e-06,7e-06,1.0],[1.0121,0.007741,-7.2071,-0.005887,1.014,-2.5044,5e-06,4e-06,1.0],[1.0155,0.009038,-9.0227,-0.007392,1.0192,-3.1508,5e-06,5e-06,1.0],[1.0181,0.012,-10.7536,-0.008655,1.0235,-3.2348,5e-06,9e-06,1.0],[1.0218,0.0116,-12.4679,-0.009784,1.0284,-2.9124,8e-06,9e-06,1.0],[1.029,0.0136,-15.7579,-0.0108,1.0385,-2.9329,1.3e-05,1.4e-05,1.0],[1.0304,0.0166,-17.5232,-0.012,1.0443,-2.2415,1.3e-05,2.3e-05,1.0],[1.0343,0.0178,-19.9881,-0.0128,1.0489,-1.6864,1.8e-05,2.7e-05,1.0],[1.0403,0.0165,-22.9518,-0.0133,1.054,-1.911,2.5e-05,2.3e-05,1.0],[1.0446,0.0159,-25.196,-0.0138,1.0574,-1.8594,3e-05,2.3e-05,1.0],[1.0476,0.0152,-27.3243,-0.0142,1.0594,-2.3173,3.3e-05,2e-05,1.0],[1.0526,0.0202,-29.671,-0.0146,1.0665,-3.4267,3.7e-05,3.2e-05,1.0],[1.054,0.0241,-31.021,-0.0157,1.0702,-4.494,3.6e-05,4e-05,1.0],[1.0586,0.032,-32.7503,-0.0163,1.0786,-6.0575,4e-05,5.9e-05,1.0],[1.058,0.0342,-33.2862,-0.0171,1.0805,-6.7959,4e-05,6.5e-05,1.0],[1.0579,0.0294,-33.9191,-0.0167,1.0746,-7.0884,4.1e-05,5.2e-05,1.0],[1.06,0.0267,-34.8087,-0.0159,1.074,-8.0252,4.4e-05,4.7e-05,1.0],[1.062,0.0252,-35.8596,-0.0155,1.0751,-8.9313,4.5e-05,4.3e-05,1.0],[1.0646,0.0262,-37.1004,-0.0157,1.0782,-9.7246,4.5e-05,4.5e-05,1.0],[1.0672,0.028,-37.7553,-0.0156,1.0807,-10.2769,4.7e-05,4.8e-05,1.0],[1.0683,0.0278,-37.6832,-0.0152,1.0813,-11.3256,4.7e-05,4.8e-05,1.0],[1.0668,0.0283,-37.1058,-0.0149,1.08,-12.2794,4.4e-05,4.8e-05,1.0],[1.0696,0.0298,-37.4405,-0.014,1.0818,-13.6024,4.7e-05,5.1e-05,1.0],[1.068,0.032,-37.1131,-0.0141,1.0815,-14.8114,4.3e-05,5.6e-05,1.0],[1.0671,0.0308,-36.3844,-0.0137,1.0779,-16.1325,4.2e-05,5.1e-05,1.0],[1.0659,0.0295,-35.6518,-0.013,1.0746,-18.0077,3.8e-05,5.2e-05,1.0],[1.0673,0.0288,-35.3604,-0.0122,1.0732,-19.9804,3.9e-05,4.9e-05,1.0],[1.0672,0.0309,-35.2014,-0.0122,1.0743,-21.8308,3.8e-05,5.6e-05,1.0],[1.066,0.0335,-34.9871,-0.0123,1.0759,-23.5776,3.5e-05,6.4e-05,1.0],[1.0682,0.0355,-36.1592,-0.012,1.0788,-24.9872,3.6e-05,7.2e-05,1.0],[1.0692,0.0354,-36.817,-0.0114,1.0809,-26.0013,3.6e-05,7.4e-05,1.0],[1.068,0.0333,-36.8555,-0.0108,1.0795,-25.982,3.4e-05,7e-05,1.0],[1.0688,0.0284,-36.6492,-0.009578,1.0778,-26.0372,3.6e-05,6e-05,1.0],[1.0702,0.0321,-36.6455,-0.009404,1.0804,-26.0961,3.7e-05,6.5e-05,1.0],[1.0699,0.0341,-35.9757,-0.009128,1.0817,-26.6178,3.5e-05,6.9e-05,1.0],[1.0693,0.0394,-35.3351,-0.009383,1.0852,-27.5037,3.2e-05,8.2e-05,1.0],[1.0683,0.0449,-34.6602,-0.009616,1.0874,-28.4699,2.8e-05,9.5e-05,1.0],[1.0685,0.0449,-34.4907,-0.009776,1.0867,-29.3802,3e-05,9.8e-05,1.0],[1.0667,0.0395,-34.1929,-0.009016,1.0826,-30.0959,2.9e-05,8.6e-05,1.0],[1.0666,0.0341,-34.233,-0.008233,1.0789,-30.4401,3e-05,7.5e-05,1.0],[1.0674,0.0327,-34.8604,-0.007545,1.079,-30.9958,3e-05,7.4e-05,1.0],[1.0679,0.034,-34.869,-0.006914,1.0817,-31.2929,2.9e-05,7.8e-05,1.0],[1.0709,0.0364,-35.2861,-0.006914,1.0855,-31.4373,3.2e-05,8.2e-05,1.0],[1.0672,0.0386,-33.8829,-0.007176,1.0848,-31.3893,2.6e-05,8.6e-05,1.0],[1.0682,0.0394,-32.6945,-0.006809,1.0845,-31.5099,2.9e-05,8.5e-05,1.0],[1.0664,0.0402,-31.3076,-0.007192,1.0833,-31.6402,2.6e-05,8.5e-05,1.0],[1.0663,0.0415,-30.7579,-0.007105,1.083,-32.3005,2.3e-05,8.6e-05,1.0],[1.0668,0.0426,-29.9891,-0.007173,1.0829,-32.8996,2.3e-05,8.7e-05,1.0],[1.0668,0.0429,-29.53,-0.007211,1.0835,-34.0626,2e-05,8.8e-05,1.0],[1.0625,0.041,-28.5175,-0.007359,1.0792,-34.7319,1.2e-05,8.4e-05,1.0],[1.0639,0.0416,-28.8023,-0.007152,1.0814,-35.7028,1.2e-05,8.7e-05,1.0],[1.0622,0.0408,-28.6235,-0.006997,1.0801,-36.2105,1e-05,8.7e-05,1.0],[1.0622,0.0407,-28.6618,-0.007394,1.0809,-36.6261,9e-06,8.7e-05,1.0],[1.0637,0.0448,-28.9618,-0.007628,1.0847,-37.2252,1e-05,9.6e-05,1.0],[1.0639,0.0504,-29.5699,-0.008768,1.0906,-38.0794,7e-06,0.000111,1.0],[1.0653,0.051,-29.9112,-0.008937,1.0915,-38.2908,8e-06,0.000112,1.0],[1.0634,0.0485,-30.147,-0.009526,1.0874,-38.3593,5e-06,0.000105,1.0],[1.0655,0.0434,-30.7025,-0.009505,1.0849,-39.1017,7e-06,9.2e-05,1.0],[1.067,0.0423,-31.1397,-0.009988,1.0855,-39.8285,6e-06,8.8e-05,1.0],[1.0681,0.0487,-31.2782,-0.0114,1.0902,-39.9543,6e-06,9.9e-05,1.0],[1.0696,0.0566,-31.7223,-0.0129,1.0964,-40.2163,4e-06,0.000114,1.0],[1.0662,0.0589,-30.6602,-0.014,1.0961,-39.5791,2e-06,0.000119,1.0],[1.063,0.0603,-30.0503,-0.0159,1.0951,-38.1727,-0.0,0.000119,1.0],[1.0638,0.0595,-30.3742,-0.0168,1.0951,-37.2064,2e-06,0.000117,1.0],[1.0632,0.0622,-30.7171,-0.018,1.0972,-36.0854,2e-06,0.000119,1.0],[1.0646,0.0654,-31.2035,-0.0199,1.1015,-34.7016,5e-06,0.000124,1.0],[1.0646,0.0686,-31.6497,-0.0215,1.1039,-33.6802,4e-06,0.000128,1.0],[1.0659,0.0727,-32.4735,-0.0232,1.1074,-32.9982,5e-06,0.000135,1.0],[1.066,0.0725,-32.9957,-0.0247,1.1075,-32.4951,5e-06,0.000134,1.0],[1.0695,0.0736,-34.9765,-0.026,1.1097,-32.5977,8e-06,0.000136,1.0],[1.0724,0.0732,-36.8728,-0.0269,1.112,-33.0821,1.1e-05,0.000135,1.0],[1.0769,0.077,-39.3332,-0.0282,1.1178,-33.2911,1.5e-05,0.00014,1.0],[1.0807,0.0796,-41.7535,-0.0297,1.1236,-33.242,1.7e-05,0.000143,1.0],[1.0831,0.0807,-43.0235,-0.031,1.1277,-32.3458,1.9e-05,0.000143,1.0],[1.0871,0.086,-44.4277,-0.0324,1.1353,-31.3022,2.2e-05,0.000152,1.0],[1.0894,0.0883,-45.3317,-0.0334,1.1402,-30.3808,2.5e-05,0.000157,1.0],[1.0908,0.0892,-46.4671,-0.0348,1.1434,-29.4113,2.6e-05,0.000157,1.0],[1.0959,0.0948,-48.9075,-0.0361,1.1527,-28.9994,2.8e-05,0.000168,1.0],[1.1008,0.0928,-51.2138,-0.0364,1.1555,-28.7931,3.3e-05,0.000161,1.0],[1.1041,0.0914,-53.3066,-0.0375,1.1589,-28.6619,3.4e-05,0.000158,1.0],[1.11,0.0915,-55.8233,-0.0374,1.1638,-29.1227,3.9e-05,0.00016,1.0],[1.1143,0.0915,-58.0166,-0.0376,1.1675,-29.7402,4.3e-05,0.00016,1.0],[1.1167,0.0911,-59.3952,-0.0378,1.1695,-30.299,4.4e-05,0.000158,1.0],[1.1184,0.0895,-60.282,-0.0378,1.1683,-30.6975,4.6e-05,0.000155,1.0],[1.1187,0.0917,-60.6651,-0.0377,1.1692,-31.7895,4.5e-05,0.000161,1.0],[1.1195,0.0922,-60.7299,-0.0372,1.1691,-33.0782,4.5e-05,0.000164,1.0],[1.1186,0.0927,-60.1249,-0.0361,1.1681,-34.4004,4.4e-05,0.000168,1.0],[1.1185,0.0953,-59.884,-0.0353,1.1689,-35.1426,4.5e-05,0.000175,1.0],[1.1162,0.0927,-58.6872,-0.0336,1.1644,-35.2683,4.6e-05,0.000172,1.0],[1.1132,0.0889,-57.7241,-0.0318,1.1612,-35.3828,4.6e-05,0.000167,1.0],[1.1114,0.0877,-57.1117,-0.0303,1.1606,-35.052,4.4e-05,0.000166,1.0],[1.1129,0.0878,-57.0977,-0.0287,1.1646,-34.8903,4.5e-05,0.000168,1.0],[1.115,0.0878,-57.2492,-0.0267,1.167,-35.3405,4.5e-05,0.000169,1.0],[1.1127,0.0869,-55.91,-0.025,1.1647,-35.4785,4.1e-05,0.000167,1.0],[1.1143,0.0855,-54.7362,-0.0232,1.1637,-36.3048,4.3e-05,0.000165,1.0],[1.1124,0.0819,-53.4306,-0.0213,1.1588,-37.6767,3.8e-05,0.000156,1.0],[1.1111,0.0833,-52.0102,-0.0198,1.1562,-38.9582,3.4e-05,0.000157,1.0],[1.1098,0.0781,-49.884,-0.0177,1.1493,-40.4311,3.2e-05,0.000146,1.0],[1.1091,0.0769,-48.464,-0.0153,1.1448,-42.4701,3e-05,0.000144,1.0],[1.1068,0.0755,-47.2706,-0.0133,1.1369,-44.1738,2.5e-05,0.000143,1.0],[1.1053,0.0729,-46.4185,-0.0119,1.1337,-45.7625,2.2e-05,0.000139,1.0],[1.1039,0.0654,-45.4104,-0.008973,1.1278,-46.8763,2.1e-05,0.000127,1.0],[1.1021,0.0617,-45.0787,-0.007527,1.1259,-47.4706,1.9e-05,0.000122,1.0],[1.1023,0.0589,-45.3511,-0.005483,1.1266,-47.9228,1.9e-05,0.000121,1.0],[1.1034,0.0609,-46.185,-0.004559,1.1318,-47.944,1.8e-05,0.000131,1.0],[1.1054,0.0597,-47.5402,-0.003012,1.1352,-47.9154,2e-05,0.000134,1.0],[1.1091,0.0552,-49.0012,-0.001314,1.1375,-47.6629,2.6e-05,0.000129,1.0],[1.1136,0.0544,-50.9796,0.00046,1.1412,-47.4865,3.3e-05,0.000129,1.0],[1.1187,0.046,-52.846,0.002095,1.14,-47.271,4e-05,0.000112,1.0],[1.1231,0.0456,-54.6351,0.002872,1.1434,-47.3027,4.5e-05,0.000113,1.0],[1.1237,0.0426,-54.881,0.004232,1.1419,-47.2209,4.7e-05,0.000106,1.0],[1.1246,0.043,-55.4584,0.004515,1.1436,-47.1468,4.7e-05,0.000107,1.0],[1.1261,0.0457,-56.0163,0.004937,1.1461,-47.0493,4.8e-05,0.000113,1.0],[1.125,0.0425,-55.2794,0.005144,1.1423,-46.2422,5e-05,0.000104,1.0],[1.1229,0.0446,-54.8031,0.004549,1.1426,-45.623,4.9e-05,0.00011,1.0],[1.1226,0.0425,-54.8815,0.004607,1.1403,-45.2116,4.9e-05,0.000103,1.0],[1.123,0.0433,-55.3092,0.004474,1.1402,-45.0253,4.9e-05,0.000104,1.0],[1.125,0.0439,-55.9301,0.004772,1.1427,-45.3119,5.2e-05,0.000104,1.0],[1.1279,0.0453,-57.234,0.004637,1.1456,-45.5759,5.4e-05,0.000107,1.0],[1.1307,0.0427,-58.2014,0.004703,1.1453,-45.5755,5.8e-05,9.8e-05,1.0],[1.1311,0.0452,-58.8506,0.004446,1.1474,-45.832,5.6e-05,0.000103,1.0],[1.131,0.0449,-58.9329,0.004462,1.1469,-45.7875,5.6e-05,0.000103,1.0],[1.132,0.0453,-59.4188,0.004333,1.1478,-45.9449,5.7e-05,0.000103,1.0],[1.1343,0.0451,-60.1895,0.004219,1.1497,-46.3453,6e-05,0.000103,1.0],[1.1335,0.047,-60.3216,0.004275,1.1497,-46.4796,5.8e-05,0.000107,1.0],[1.1326,0.0452,-60.2768,0.003755,1.1467,-46.3949,5.7e-05,0.000101,1.0],[1.1345,0.0484,-61.0954,0.003623,1.151,-47.1778,5.8e-05,0.000109,1.0],[1.1327,0.0466,-60.8029,0.003116,1.1487,-46.9393,5.6e-05,0.000104,1.0],[1.1333,0.0484,-61.3122,0.00283,1.15,-47.3621,5.5e-05,0.000108,1.0],[1.1334,0.048,-61.5557,0.0027,1.149,-47.9028,5.5e-05,0.000106,1.0],[1.1338,0.0494,-61.8447,0.002356,1.1497,-48.5709,5.5e-05,0.00011,1.0],[1.1374,0.0495,-62.9479,0.001788,1.1517,-49.279,5.9e-05,0.000109,1.0],[1.1372,0.0494,-63.2894,0.001771,1.1516,-49.7853,5.9e-05,0.000109,1.0],[1.1384,0.0515,-63.939,0.001546,1.1542,-50.3964,6e-05,0.000113,1.0],[1.1445,0.0526,-65.6876,0.001524,1.1594,-51.0778,6.6e-05,0.000116,1.0],[1.1442,0.0533,-66.0153,0.001394,1.1595,-51.0303,6.6e-05,0.000116,1.0],[1.1468,0.0538,-66.842,0.000861,1.1626,-51.2911,6.9e-05,0.000116,1.0],[1.1487,0.0565,-67.5886,0.000875,1.1662,-51.8607,7e-05,0.000122,1.0],[1.1496,0.0567,-68.2228,0.00065,1.168,-52.2317,7e-05,0.000123,1.0],[1.1503,0.0568,-68.4543,0.000641,1.1683,-52.2422,7.1e-05,0.000122,1.0],[1.1505,0.0596,-68.9045,0.000231,1.1709,-52.5664,7e-05,0.00013,1.0],[1.1513,0.0572,-69.2121,-2.7e-05,1.1694,-52.6834,7.1e-05,0.000123,1.0],[1.1536,0.0621,-70.2458,-0.00059,1.1739,-53.5829,7.2e-05,0.000134,1.0],[1.1591,0.0681,-71.9637,-0.001078,1.1811,-54.9661,7.9e-05,0.000147,1.0],[1.1595,0.0673,-71.9015,-0.001423,1.1792,-55.4565,8.2e-05,0.000143,1.0],[1.162,0.0627,-72.4328,-0.001695,1.1764,-56.0761,8.7e-05,0.000129,1.0],[1.1621,0.0573,-72.5748,-0.001744,1.1715,-56.6839,8.7e-05,0.000114,1.0],[1.1619,0.0592,-72.8909,-0.001998,1.1726,-57.1512,8.5e-05,0.000118,1.0],[1.1608,0.0596,-72.848,-0.002054,1.1723,-57.2521,8.4e-05,0.000119,1.0],[1.1628,0.0593,-73.5114,-0.002321,1.1738,-57.3574,8.7e-05,0.000119,1.0],[1.1657,0.06,-74.5651,-0.002508,1.1776,-58.0205,8.9e-05,0.00012,1.0],[1.1669,0.061,-75.2968,-0.002434,1.179,-58.3608,8.9e-05,0.000125,1.0],[1.1701,0.0629,-76.3487,-0.002741,1.1843,-59.1609,9.3e-05,0.000131,1.0],[1.1781,0.0668,-78.5388,-0.002299,1.1928,-60.0422,0.000105,0.000142,1.0],[1.181,0.0692,-79.8909,-0.002483,1.1982,-60.0997,0.000111,0.000148,1.0],[1.1872,0.0618,-80.7951,-0.000775,1.1978,-59.8892,0.000126,0.000132,1.0],[1.1829,0.06,-80.5323,-0.000839,1.1949,-59.068,0.000121,0.000131,1.0],[1.1843,0.061,-81.1719,-0.000568,1.1983,-58.6218,0.000123,0.000134,1.0],[1.1856,0.062,-81.6311,-0.000536,1.2009,-58.3285,0.000123,0.000134,1.0],[1.1843,0.067,-81.0349,-0.000245,1.2013,-57.8604,0.00012,0.000141,1.0],[1.1786,0.0677,-77.8145,-0.000118,1.1958,-57.0814,0.000117,0.000139,1.0],[1.1731,0.0681,-74.7787,7.6e-05,1.1888,-57.0619,0.000111,0.000138,1.0],[1.1718,0.0676,-72.7481,-3e-05,1.1857,-57.7106,0.00011,0.000134,1.0],[1.1691,0.0689,-70.4025,0.000391,1.1823,-57.9996,0.000109,0.000133,1.0],[1.1636,0.0678,-67.723,0.000662,1.1753,-58.2447,0.000103,0.00013,1.0],[1.1595,0.0689,-65.7729,0.000859,1.1722,-58.8907,9.8e-05,0.000131,1.0],[1.159,0.0677,-64.9676,0.001055,1.1714,-59.3045,9.7e-05,0.000129,1.0],[1.1599,0.0664,-64.9978,0.001297,1.173,-59.8286,9.6e-05,0.000127,1.0],[1.1628,0.0686,-65.8745,0.001718,1.1779,-60.5248,9.7e-05,0.000133,1.0],[1.1712,0.0741,-68.3432,0.001914,1.1881,-61.9467,0.000103,0.000147,1.0],[1.172,0.0715,-68.9979,0.002691,1.1891,-62.8161,0.000102,0.000143,1.0],[1.1772,0.0684,-70.7936,0.003185,1.1917,-63.2207,0.000107,0.000136,1.0],[1.1767,0.0627,-71.1777,0.00386,1.1872,-63.1474,0.000105,0.000123,1.0],[1.1783,0.0604,-71.4505,0.004699,1.1875,-63.5265,0.000108,0.000118,1.0],[1.1789,0.0618,-71.731,0.004988,1.1898,-63.5694,0.000107,0.000121,1.0],[1.1792,0.0616,-71.9156,0.004599,1.1905,-63.456,0.000107,0.000122,1.0],[1.1794,0.0619,-72.1798,0.004395,1.1908,-63.6853,0.000106,0.000123,1.0],[1.1782,0.0589,-71.629,0.004843,1.1876,-63.7119,0.000107,0.000119,1.0],[1.1784,0.0598,-71.996,0.004807,1.1877,-64.0819,0.000108,0.000122,1.0],[1.176,0.0592,-72.004,0.004558,1.1849,-64.4253,0.000105,0.000122,1.0],[1.1765,0.0568,-72.5211,0.004629,1.1834,-64.9356,0.000106,0.000118,1.0],[1.1823,0.0561,-73.8426,0.005055,1.1877,-66.0745,0.000114,0.000118,1.0],[1.1906,0.0631,-75.8665,0.005476,1.198,-67.5515,0.000124,0.000132,1.0],[1.1874,0.062,-75.2497,0.005047,1.1953,-67.3812,0.000119,0.000132,1.0],[1.1877,0.0606,-75.2093,0.005092,1.1948,-66.9034,0.000122,0.000126,1.0],[1.184,0.0569,-74.3524,0.004992,1.1895,-65.9386,0.00012,0.000116,1.0],[1.1831,0.0581,-74.1983,0.004555,1.1915,-65.3739,0.000119,0.000121,1.0],[1.184,0.056,-74.3712,0.004235,1.1917,-64.8122,0.00012,0.000114,1.0],[1.1867,0.0628,-75.3429,0.00359,1.1978,-64.6499,0.000119,0.000126,1.0],[1.1865,0.0634,-74.5646,0.003404,1.1962,-64.1422,0.000119,0.000123,1.0],[1.187,0.0643,-73.6204,0.003415,1.195,-64.3029,0.00012,0.000121,1.0],[1.1781,0.0662,-70.5389,0.002046,1.1883,-63.7048,0.00011,0.000122,1.0],[1.1777,0.0632,-69.2234,0.00167,1.1843,-63.9176,0.000111,0.000113,1.0],[1.1744,0.0659,-68.2089,0.001054,1.1811,-64.2819,0.000106,0.000116,1.0],[1.1724,0.0627,-67.2027,0.000598,1.1763,-65.0009,0.000103,0.000108,1.0],[1.1729,0.0633,-67.1329,9e-06,1.1769,-65.9985,0.000103,0.000109,1.0],[1.1732,0.0681,-67.7942,-0.001236,1.1828,-66.6009,0.000101,0.000121,1.0],[1.1862,0.0699,-70.9756,-0.00029,1.1943,-67.343,0.000118,0.000125,1.0],[1.1978,0.0716,-74.0458,0.000121,1.2049,-67.124,0.000135,0.00013,1.0],[1.2013,0.0669,-75.2431,0.001257,1.206,-66.14,0.000143,0.000121,1.0],[1.2018,0.0685,-76.6527,0.00174,1.2095,-65.4735,0.000143,0.000126,1.0],[1.2067,0.0641,-78.5861,0.001606,1.2116,-64.7731,0.00015,0.000117,1.0],[1.2102,0.0655,-80.3424,0.00129,1.2165,-64.5046,0.000152,0.00012,1.0],[1.211,0.0674,-80.9365,0.001263,1.218,-63.8408,0.000152,0.000123,1.0],[1.2122,0.07,-81.4896,0.00068,1.2204,-63.7962,0.000152,0.000127,1.0],[1.212,0.0687,-80.794,0.000808,1.2183,-63.6896,0.000153,0.000122,1.0],[1.2098,0.0691,-79.6698,0.000293,1.2154,-63.2128,0.000152,0.000119,1.0],[1.208,0.0733,-78.9919,-0.000147,1.2143,-63.4107,0.000149,0.000127,1.0],[1.2026,0.0712,-76.738,-0.001085,1.207,-63.3113,0.000144,0.00012,1.0],[1.2013,0.0706,-75.8083,-0.001039,1.2026,-64.0067,0.000143,0.000117,1.0],[1.2007,0.0683,-75.319,-0.001812,1.1999,-64.5798,0.000142,0.000111,1.0],[1.2012,0.0702,-75.6946,-0.002213,1.2014,-65.4027,0.000141,0.000113,1.0],[1.2019,0.0725,-76.2838,-0.003145,1.2033,-65.7837,0.00014,0.000119,1.0],[1.2065,0.074,-77.6335,-0.003191,1.2091,-66.6281,0.000145,0.000123,1.0],[1.2218,0.0776,-81.5445,-0.002976,1.2227,-67.7851,0.000164,0.000128,1.0],[1.2273,0.0834,-83.321,-0.00312,1.2306,-67.892,0.000171,0.00014,1.0],[1.234,0.08,-84.5224,-0.002332,1.2325,-67.1261,0.000184,0.000131,1.0],[1.2331,0.0739,-84.6055,-0.002004,1.2274,-66.0949,0.000184,0.000117,1.0],[1.234,0.0755,-85.2821,-0.002234,1.2301,-65.5508,0.000185,0.00012,1.0],[1.2353,0.0736,-85.6948,-0.002887,1.2303,-64.9796,0.000187,0.000115,1.0],[1.2372,0.074,-86.4434,-0.003532,1.2319,-64.9881,0.000188,0.000115,1.0],[1.2377,0.0778,-86.6167,-0.004513,1.2347,-64.7799,0.000189,0.000122,1.0],[1.2357,0.0791,-85.9869,-0.005205,1.2335,-64.4228,0.000188,0.000124,1.0],[1.236,0.0817,-86.2358,-0.006387,1.2353,-64.4339,0.000188,0.000128,1.0],[1.2482,0.0889,-88.8336,-0.00635,1.2486,-65.5551,0.000205,0.000142,1.0],[1.2537,0.0936,-90.2011,-0.007004,1.2539,-65.8794,0.000212,0.00015,1.0],[1.2681,0.0965,-93.6281,-0.007728,1.2637,-66.8823,0.000231,0.000149,1.0],[1.2712,0.0927,-94.1906,-0.007721,1.2622,-67.049,0.000234,0.000137,1.0],[1.2718,0.0941,-94.3685,-0.008542,1.2644,-66.8655,0.000233,0.000138,1.0],[1.2701,0.0962,-94.2805,-0.0094,1.2648,-66.3786,0.00023,0.000139,1.0],[1.2721,0.0971,-94.8227,-0.0102,1.2681,-66.0102,0.000233,0.000141,1.0],[1.2751,0.0964,-96.1789,-0.0106,1.2713,-66.13,0.000234,0.00014,1.0],[1.2773,0.0973,-97.5868,-0.0112,1.2752,-65.8777,0.000234,0.000143,1.0],[1.2794,0.0975,-98.9383,-0.0113,1.2777,-65.8399,0.000236,0.000146,1.0],[1.2817,0.0945,-100.749,-0.0113,1.2783,-66.3133,0.000238,0.000142,1.0],[1.2872,0.0934,-103.0901,-0.0115,1.2829,-66.7204,0.000245,0.000143,1.0],[1.2874,0.0926,-104.2682,-0.0113,1.2835,-66.7178,0.000246,0.000144,1.0],[1.291,0.0879,-105.4813,-0.0107,1.284,-66.8941,0.000251,0.000134,1.0],[1.2926,0.0883,-106.3423,-0.0107,1.2874,-67.0244,0.000253,0.000135,1.0],[1.2922,0.0904,-106.4169,-0.0107,1.2876,-66.9214,0.000251,0.000139,1.0],[1.293,0.0914,-106.3655,-0.0105,1.2872,-67.2769,0.000251,0.00014,1.0],[1.2909,0.0942,-105.419,-0.0104,1.2862,-67.5486,0.000249,0.000144,1.0],[1.2904,0.0943,-104.324,-0.0101,1.2845,-67.9196,0.000249,0.000145,1.0],[1.2877,0.0936,-102.9724,-0.009763,1.2799,-68.2837,0.000247,0.000142,1.0],[1.283,0.0937,-101.0312,-0.009746,1.2755,-68.4627,0.000243,0.000141,1.0],[1.2802,0.0926,-99.3451,-0.009237,1.2721,-68.7209,0.000241,0.000139,1.0],[1.2805,0.0964,-98.7448,-0.009224,1.2746,-69.3836,0.00024,0.000149,1.0],[1.2978,0.1035,-102.205,-0.008211,1.2915,-71.3771,0.000261,0.000161,1.0],[1.311,0.1059,-104.3214,-0.006282,1.3028,-72.6153,0.000279,0.000166,1.0],[1.3131,0.1002,-104.1797,-0.004685,1.299,-72.7652,0.000284,0.000149,1.0],[1.3155,0.0979,-104.8657,-0.003253,1.2985,-73.4781,0.000285,0.000142,1.0],[1.3188,0.0913,-104.8739,-0.001962,1.2984,-73.5495,0.000291,0.000127,1.0],[1.3221,0.0897,-105.1883,-0.000796,1.2999,-73.7752,0.000297,0.000117,1.0],[1.3187,0.0873,-104.3946,-0.00031,1.2975,-73.539,0.00029,0.000113,1.0],[1.3178,0.0879,-103.738,0.000992,1.2983,-73.2394,0.000288,0.000117,1.0],[1.3155,0.0884,-102.5842,0.002288,1.2972,-72.3671,0.000285,0.00012,1.0],[1.314,0.0847,-101.1215,0.003713,1.2938,-71.377,0.000286,0.000112,1.0],[1.3093,0.0843,-98.9258,0.004843,1.29,-70.0613,0.000282,0.000111,1.0],[1.3046,0.0861,-97.0101,0.005958,1.2859,-68.5436,0.000276,0.000113,1.0],[1.3019,0.0856,-94.865,0.006567,1.2841,-67.0612,0.000274,0.000113,1.0],[1.2979,0.085,-92.3428,0.00795,1.2802,-65.2575,0.000272,0.00011,1.0],[1.2911,0.0853,-89.281,0.00916,1.2751,-62.9521,0.000266,0.00011,1.0],[1.2847,0.0866,-86.1592,0.0104,1.271,-60.108,0.00026,0.00011,1.0]]}}\ No newline at end of filediff --git a/skills/showtime/tests/fixtures/xray_notes/README.md b/skills/showtime/tests/fixtures/xray_notes/README.mdnew file mode 100644index 0000000..fe3db92--- /dev/null+++ b/skills/showtime/tests/fixtures/xray_notes/README.md@@ -0,0 +1,15 @@+# Fixtures of test_xray_notes.py++Two 4 s audio excerpts and the golden notes made from them with the original basic-pitch package.++| File | Cut from | Author | License |+|---|---|---|---|+| `fp4-excerpt.ogg` | "Fingerpicking Pattern 4 p-ima chitarra.webm", Wikimedia Commons | Elenaf | CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/); the excerpt is shared under the same license |+| `piano-excerpt.ogg` | "Minuet-G-Major-BWV-Anh-114.ogv", Wikimedia Commons (Minuet in G, BWV Anh. 114, attributed to Christian Petzold) | KasraR | public domain |++Source pages: https://commons.wikimedia.org/wiki/File:Fingerpicking_Pattern_4_p-ima_chitarra.webm and+https://commons.wikimedia.org/wiki/File:Minuet-G-Major-BWV-Anh-114.ogv++Checked 13 Oct 2026 by cross-correlating each excerpt with the trimmed clip of the matching X-ray film (guitar tab, piano score):+0.998 (excerpt starts 2.0 s into the clip) and 0.978 (7.98 s). The film clips are the trims of those Commons files.+The `golden.json` files are note lists computed from the excerpts (basic-pitch 0.4.0, Apache-2.0).diff --git a/skills/showtime/tests/fixtures/xray_notes/fp4-excerpt.golden.json b/skills/showtime/tests/fixtures/xray_notes/fp4-excerpt.golden.jsonnew file mode 100644index 0000000..8038346--- /dev/null+++ b/skills/showtime/tests/fixtures/xray_notes/fp4-excerpt.golden.json@@ -0,0 +1,120 @@+{+"source": "fp4-excerpt.ogg",+"engine": "basic_pitch 0.4.0 (ONNX) note_creation.model_output_to_notes, onset 0.5 frame 0.3 min_note_len 11 melodia on, no pitch bends",+"notes": [+{+"t": 0.0116,+"end": 0.2322,+"pitch": 60,+"amp": 0.576+},+{+"t": 0.3947,+"end": 0.5921,+"pitch": 57,+"amp": 0.493+},+{+"t": 0.3947,+"end": 2.0795,+"pitch": 45,+"amp": 0.553+},+{+"t": 1.2423,+"end": 1.6022,+"pitch": 64,+"amp": 0.371+},+{+"t": 1.4048,+"end": 1.8692,+"pitch": 60,+"amp": 0.49+},+{+"t": 1.4164,+"end": 1.6834,+"pitch": 57,+"amp": 0.504+},+{+"t": 1.6486,+"end": 2.0795,+"pitch": 64,+"amp": 0.453+},+{+"t": 1.6834,+"end": 1.8344,+"pitch": 57,+"amp": 0.478+},+{+"t": 1.8692,+"end": 2.0446,+"pitch": 57,+"amp": 0.417+},+{+"t": 1.8692,+"end": 2.0679,+"pitch": 60,+"amp": 0.645+},+{+"t": 2.3233,+"end": 3.9719,+"pitch": 50,+"amp": 0.684+},+{+"t": 2.3465,+"end": 2.7877,+"pitch": 69,+"amp": 0.335+},+{+"t": 2.5439,+"end": 2.8341,+"pitch": 62,+"amp": 0.418+},+{+"t": 3.3217,+"end": 3.5539,+"pitch": 57,+"amp": 0.469+},+{+"t": 3.3333,+"end": 3.5655,+"pitch": 62,+"amp": 0.682+},+{+"t": 3.3914,+"end": 3.5772,+"pitch": 69,+"amp": 0.384+},+{+"t": 3.5655,+"end": 3.7513,+"pitch": 62,+"amp": 0.455+},+{+"t": 3.7513,+"end": 3.9603,+"pitch": 62,+"amp": 0.652+},+{+"t": 3.7629,+"end": 3.9255,+"pitch": 57,+"amp": 0.531+}+]+}\ No newline at end of filediff --git a/skills/showtime/tests/fixtures/xray_notes/fp4-excerpt.ogg b/skills/showtime/tests/fixtures/xray_notes/fp4-excerpt.oggnew file mode 100644index 0000000..c4c2164Binary files /dev/null and b/skills/showtime/tests/fixtures/xray_notes/fp4-excerpt.ogg differdiff --git a/skills/showtime/tests/fixtures/xray_notes/piano-excerpt.golden.json b/skills/showtime/tests/fixtures/xray_notes/piano-excerpt.golden.jsonnew file mode 100644index 0000000..dcd3884--- /dev/null+++ b/skills/showtime/tests/fixtures/xray_notes/piano-excerpt.golden.json@@ -0,0 +1,162 @@+{+"source": "piano-excerpt.ogg",+"engine": "basic_pitch 0.4.0 (ONNX) note_creation.model_output_to_notes, onset 0.5 frame 0.3 min_note_len 11 melodia on, no pitch bends",+"notes": [+{+"t": 0.0116,+"end": 0.1509,+"pitch": 55,+"amp": 0.494+},+{+"t": 0.0116,+"end": 0.1625,+"pitch": 72,+"amp": 0.788+},+{+"t": 0.1741,+"end": 0.5689,+"pitch": 71,+"amp": 0.708+},+{+"t": 0.4412,+"end": 0.8011,+"pitch": 69,+"amp": 0.589+},+{+"t": 0.6618,+"end": 0.894,+"pitch": 67,+"amp": 0.605+},+{+"t": 0.894,+"end": 1.2655,+"pitch": 66,+"amp": 0.487+},+{+"t": 0.9056,+"end": 1.1146,+"pitch": 78,+"amp": 0.416+},+{+"t": 0.9172,+"end": 1.2539,+"pitch": 62,+"amp": 0.572+},+{+"t": 1.3003,+"end": 1.6602,+"pitch": 67,+"amp": 0.574+},+{+"t": 1.3235,+"end": 1.6254,+"pitch": 71,+"amp": 0.289+},+{+"t": 1.3351,+"end": 1.7763,+"pitch": 59,+"amp": 0.53+},+{+"t": 1.5441,+"end": 1.8228,+"pitch": 69,+"amp": 0.603+},+{+"t": 1.7995,+"end": 2.184,+"pitch": 71,+"amp": 0.699+},+{+"t": 1.8344,+"end": 2.2884,+"pitch": 55,+"amp": 0.492+},+{+"t": 1.9866,+"end": 2.3465,+"pitch": 67,+"amp": 0.462+},+{+"t": 2.184,+"end": 2.4974,+"pitch": 71,+"amp": 0.59+},+{+"t": 2.242,+"end": 2.6251,+"pitch": 62,+"amp": 0.616+},+{+"t": 2.3349,+"end": 2.6484,+"pitch": 69,+"amp": 0.633+},+{+"t": 2.66,+"end": 2.9967,+"pitch": 50,+"amp": 0.55+},+{+"t": 2.8689,+"end": 3.0199,+"pitch": 69,+"amp": 0.417+},+{+"t": 2.8689,+"end": 3.0895,+"pitch": 60,+"amp": 0.668+},+{+"t": 3.0895,+"end": 3.5423,+"pitch": 59,+"amp": 0.535+},+{+"t": 3.2869,+"end": 3.5539,+"pitch": 57,+"amp": 0.626+},+{+"t": 3.5307,+"end": 3.9719,+"pitch": 74,+"amp": 0.702+},+{+"t": 3.5423,+"end": 3.7629,+"pitch": 83,+"amp": 0.536+},+{+"t": 3.5423,+"end": 3.9719,+"pitch": 59,+"amp": 0.65+}+]+}\ No newline at end of filediff --git a/skills/showtime/tests/fixtures/xray_notes/piano-excerpt.ogg b/skills/showtime/tests/fixtures/xray_notes/piano-excerpt.oggnew file mode 100644index 0000000..27f906aBinary files /dev/null and b/skills/showtime/tests/fixtures/xray_notes/piano-excerpt.ogg differdiff --git a/skills/showtime/tests/run_all.py b/skills/showtime/tests/run_all.pyindex 49d1ffd..1ac1437 100755--- a/skills/showtime/tests/run_all.py+++ b/skills/showtime/tests/run_all.py@@ -105,7 +105,7 @@ SHARD_WEIGHTS = {"test_foundation.py": 35, "test_job.py": 34, "test_mcp.py": 26, "test_studio.py": 26,"test_manim.py": 4, "test_skill_structure.py": 3, "test_doc.py": 3, "test_delight.py": 1,"test_film.py": 1, "test_diagram.py": 1, "test_caption_cards.py": 1, "test_runtime.py": 1, "test_chart_labels.py": 1,- "test_run_all.py": 5, "test_preview_server.py": 3, "test_packages.py": 8, "test_hosts.py": 4,+ "test_run_all.py": 5, "test_xray.py": 30, "test_xray_template.py": 12, "test_xray_live.py": 12, "test_xray_made.py": 8, "test_xray_notes.py": 20, "test_xray_drums.py": 12, "test_preview_server.py": 3, "test_packages.py": 8, "test_hosts.py": 4,}SHARD_DEFAULT_WEIGHT = 60diff --git a/skills/showtime/tests/test_bench_scoreboard.py b/skills/showtime/tests/test_bench_scoreboard.pyindex 7c06ce9..2d218cc 100644--- a/skills/showtime/tests/test_bench_scoreboard.py+++ b/skills/showtime/tests/test_bench_scoreboard.py@@ -181,7 +181,7 @@ EXAMPLES = {class Cases(unittest.TestCase):def test_every_case_loads_like_the_eval(self):cases = sorted(p.name for p in EVALS.iterdir() if (p / "prompt.md").is_file())- self.assertEqual(len(cases), 18, cases)+ self.assertEqual(len(cases), 20, cases)for name in NEW_CASES:self.assertIn(name, cases)for name in cases:diff --git a/skills/showtime/tests/test_diagram.py b/skills/showtime/tests/test_diagram.pyindex 240cd4d..2f5ba02 100644--- a/skills/showtime/tests/test_diagram.py+++ b/skills/showtime/tests/test_diagram.py@@ -137,7 +137,7 @@ class TestCli(unittest.TestCase):self.assertAlmostEqual(got, answer * 30 / base, places=2)cp = showtime("new", "diagram", TMP / "ex-nope", "--example", "nope", check=False)self.assertNotEqual(cp.returncode, 0)- self.assertIn("examples of diagram: econ-lab, enter, market", cp.stderr + cp.stdout)+ self.assertIn("examples of diagram: computer, econ-lab, enter, market", cp.stderr + cp.stdout)def test_theme_keeps_an_explicit_background(self):d = TMP / "dark-bg"diff --git a/skills/showtime/tests/test_figure_computer.py b/skills/showtime/tests/test_figure_computer.pynew file mode 100644index 0000000..87c3583--- /dev/null+++ b/skills/showtime/tests/test_figure_computer.py@@ -0,0 +1,195 @@+#!/usr/bin/env python3+"""The "press a key" living diagram: `showtime new diagram <dir> --example computer`+(templates/diagram/examples/computer.js), a live figure you type on.++ * static: the example's USB HID usage ids are the standard ones (Keyboard/Keypad page 0x07), its numbers are+ marked typical and illustrative, the honest line is on the film and the live page, it films its tour+ * browser: the exported figure (one offline file) takes typing from the keyboard: 'hi' lands on the screen tag+ and in the RAM buffer, the controller shows the last key's code (0x0C for 'i'); Backspace and Enter work; a+ click on a key types it; no console error and no network request; every state walks clean++Stdlib only. usage: python tests/test_figure_computer.py [--fast] [-v]+ --fast skip the browser check+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import json+import re+import shutil+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+EXAMPLE = SKILL / "templates" / "diagram" / "examples" / "computer.js"+sys.path.insert(0, str(SKILL / "lib"))++from st.launcher import build_env, showtime_home # noqa: E402++FAST = "--fast" in sys.argv+ENV = build_env(showtime_home())+NODE = shutil.which("node", path=ENV.get("PATH"))+TMP = Path(tempfile.mkdtemp(prefix="st-figure-computer-"))+++def showtime(*args, check=True, timeout=300):+ cp = subprocess.run([sys.executable, str(LAUNCHER)] + [str(a) for a in args], env=ENV,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",+ errors="replace", timeout=timeout)+ if check and cp.returncode != 0:+ raise AssertionError("showtime %s failed (rc=%d):\n%s\n%s" % (+ " ".join(map(str, args)), cp.returncode, cp.stdout[-3000:], cp.stderr[-3000:]))+ return cp+++def run_driver(src: str, *args, timeout=600) -> dict:+ drv = TMP / "drv.mjs"+ drv.write_text(src, encoding="utf-8")+ cp = subprocess.run([NODE, str(drv), str(SKILL)] + [str(a) for a in args], env=dict(ENV, SHOWTIME_SKILL=str(SKILL)),+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8", errors="replace", timeout=timeout)+ assert cp.returncode == 0, cp.stderr[-3000:] + cp.stdout[-2000:]+ return json.loads(cp.stdout.strip().splitlines()[-1])+++class TestComputerExample(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.js = EXAMPLE.read_text(encoding="utf-8")++ def test_hid_usage_ids_are_the_standard_ones(self):+ js = self.js+ self.assertIn("HID[c] = 0x04 + i", js, "a-z are 0x04-0x1D")+ self.assertIn("HID[c] = 0x1E + i", js, "1-9 are 0x1E-0x26 and 0 is 0x27, in the order '1234567890'")+ self.assertIn("'1234567890'", js)+ for key, code in (("'\\n'", "0x28"), ("'\\b'", "0x2A"), ("'\\t'", "0x2B"), ("' '", "0x2C"), ("'-'", "0x2D"),+ ("';'", "0x33"), ("'/'", "0x38"), ("shiftL", "0xE1")):+ self.assertRegex(js, re.escape(key) + r": " + code + r"\b", key)++ def test_honest_numbers(self):+ line = "Simplified and illustrative: real machines add layers (drivers, compositor) and vary."+ self.assertIn("footL: '%s'" % line, self.js, "the film says it")+ self.assertIn("source: '%s" % line, self.js, "the live page says it")+ self.assertIn("typical", self.js)+ self.assertIn("illustrative", self.js)+ self.assertNotRegex(self.js, r"\b[Mm]easured\b(?! machines often take longer)", "nothing is presented as measured")++ def test_films_its_tour(self):+ self.assertIn("film: 'tour'", self.js)+ self.assertRegex(self.js, r"var CUE = \{\n duration: [0-9.]+,\n\};")+ self.assertIn("type: { hint: 'Type on it', run:", self.js)++ def test_new_takes_the_example(self):+ d = TMP / "static"+ showtime("new", "diagram", d, "--example", "computer", "--theme", "white")+ cfg = json.loads((d / "showtime.json").read_text(encoding="utf-8"))+ dur = float(re.search(r"duration: ([0-9.]+),", self.js).group(1))+ self.assertEqual(cfg["duration"], dur)+ self.assertEqual(cfg["title"], "What happens when you press a key")+++FILE_DRIVER = r"""+import path from 'node:path';+import { pathToFileURL } from 'node:url';+const [SKILL, FILE] = process.argv.slice(2);+const imp = (p) => import(pathToFileURL(path.join(SKILL, 'scripts', p)).href);+const { openBrowser } = await imp('lib/stagehost.mjs');+const { browser } = await openBrowser({ headless: true });+const out = { errors: [], requests: [] };+const until = (page, fn, arg) => page.waitForFunction(fn, arg, { timeout: 30000, polling: 50 });+const tag = (page, id) => page.evaluate((x) => { const g = document.querySelector('.fg-ld [data-ld-tag="' + x + '"]');+ if (!g || g.getAttribute('visibility') !== 'visible') return null;+ return [...g.querySelectorAll('text')].filter((t) => t.getAttribute('visibility') === 'visible').map((t) => t.textContent).join(''); }, id);+const tagIs = (page, id, s) => until(page, (a) => { const g = document.querySelector('.fg-ld [data-ld-tag="' + a[0] + '"]');+ return g && g.getAttribute('visibility') === 'visible' && +g.getAttribute('opacity') > 0.9+ && [...g.querySelectorAll('text')].some((t) => t.getAttribute('visibility') === 'visible' && t.textContent.replace(/\u00a0/g, ' ') === a[1]); }, [id, s]);+try {+ const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 } });+ await ctx.addInitScript(() => { window.SHOWTIME_TEST_TOUR_AFTER = 600000; });+ const page = await ctx.newPage();+ page.on('pageerror', (e) => out.errors.push(String(e.message || e)));+ page.on('console', (m) => { if (m.type() === 'error') out.errors.push(m.text()); });+ page.on('request', (r) => { if (!/^(file|data|blob):/.test(r.url())) out.requests.push(r.url()); });+ await page.goto(pathToFileURL(FILE).href);+ await until(page, () => window.Figure && Figure.current && Figure.current.api);+ await page.evaluate(() => Figure.current.api.ready);+ out.typeField = await page.isVisible('.fg-type input');+ // real typing, from the page's keyboard+ await page.mouse.click(4, 4);+ await page.keyboard.type('hi', { delay: 90 });+ await tagIs(page, 'screen', 'on screen: hi');+ out.screen = await tag(page, 'screen');+ out.buffer = await tag(page, 'buffer');+ out.code = await tag(page, 'code');+ out.ro = await page.evaluate(() => [...document.querySelectorAll('.fg-ro .ro')].map((e) => e.querySelector('.k').textContent + ' ' + e.querySelector('.v').textContent));+ // Backspace, then Enter: a new, empty line+ await page.keyboard.press('Backspace');+ await tagIs(page, 'screen', 'on screen: h');+ await page.keyboard.press('Enter');+ await tagIs(page, 'screen', 'on screen: (empty line)');+ out.buffer2 = await tag(page, 'buffer');+ // a click on the drawn 'a' key types it+ const pt = await page.evaluate(() => {+ const svg = document.querySelector('#ld-world').ownerSVGElement, F = Figure.current, cam = F.api.camera(F.api.render ? 0 : 0);+ const U = 2.2, x = 0.3 + 1.75 * U + 0.5 * U, y = 30.8 + 2 * U + 0.5 * U, p = F.api.project(x, y, 1.6);+ const q = svg.createSVGPoint(); q.x = cam.ex + p[0] * cam.z; q.y = cam.ey + p[1] * cam.z;+ const r = q.matrixTransform(svg.getScreenCTM()); return [r.x, r.y];+ });+ await page.mouse.click(pt[0], pt[1]);+ await tagIs(page, 'screen', 'on screen: a');+ out.pin = await page.evaluate(() => Figure.current.S.pin);+ out.walk = await page.evaluate(() => Figure.current.walk());+ await ctx.close();+} finally { await browser.close(); }+console.log(JSON.stringify(out));+process.exit(0);+"""++[email protected](FAST or not NODE, "--fast: no browser")+class TestComputerFigure(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.proj = TMP / "computer"+ showtime("new", "diagram", cls.proj, "--example", "computer", "--theme", "white")+ cls.out = TMP / "computer-figure.html"+ cp = showtime("export", "html", cls.proj, "--interactive", "-o", cls.out, "--json")+ cls.report = json.loads(cp.stdout)+ cls.r = run_driver(FILE_DRIVER, cls.out)++ def test_one_offline_file(self):+ self.assertTrue(self.report["interactive"])+ self.assertLess(self.report["bytes"], 1000 * 1000)+ self.assertEqual(self.r["requests"], [], "no network request")+ self.assertEqual(self.r["errors"], [], "no console error")+ self.assertTrue(self.r["typeField"], "a Type field for phones")++ def test_typing_reaches_the_screen(self):+ self.assertEqual(self.r["screen"].replace("\u00a0", " "), "on screen: hi")+ self.assertEqual(self.r["buffer"], '"hi"', "the editor's text in RAM")+ self.assertEqual(self.r["code"], "i \u2192 0x0C", "the USB HID usage id of 'i'")+ self.assertIn("Keys pressed 2", self.r["ro"])++ def test_backspace_enter_and_a_click(self):+ self.assertEqual(self.r["buffer2"], '"h\\n"', "Backspace took the i, Enter started a line")+ self.assertEqual(self.r["pin"], "", "a key click types; it does not pin the keyboard")++ def test_every_state_walks_clean(self):+ w = self.r["walk"]+ self.assertTrue(w["typing"])+ self.assertGreaterEqual(w["presses"], 4)+ self.assertGreaterEqual(w["states"], 30)+ self.assertEqual(w["problems"], [])+++if __name__ == "__main__":+ argv = [a for a in sys.argv if a != "--fast"]+ try:+ unittest.main(argv=argv, verbosity=2 if "-v" in argv else 1)+ finally:+ shutil.rmtree(TMP, ignore_errors=True)diff --git a/skills/showtime/tests/test_figure_examples.py b/skills/showtime/tests/test_figure_examples.pynew file mode 100644index 0000000..9f79e74--- /dev/null+++ b/skills/showtime/tests/test_figure_examples.py@@ -0,0 +1,135 @@+#!/usr/bin/env python3+"""The diagram template's live-figure examples you touch (templates/diagram/examples/, runtime/figure.js).++ * typewriter: `showtime new diagram <dir> --example typewriter` makes the scene; `showtime export html --interactive`+ packs one offline figure; in a headless browser, typing "hi" and Enter on the page types it on the machine: "hi"+ lands on a fresh line of its own (after the first step's demo keystroke), stays on the paper above the new line+ after Return, the readouts count three lines, and the page logs no console error and makes no network request++Stdlib only. usage: python tests/test_figure_examples.py [--fast] [-v]+ --fast skip the browser check+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import json+import shutil+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+EXAMPLES = SKILL / "templates" / "diagram" / "examples"+sys.path.insert(0, str(SKILL / "lib"))++from st.launcher import build_env, showtime_home # noqa: E402++FAST = "--fast" in sys.argv+ENV = build_env(showtime_home())+NODE = shutil.which("node", path=ENV.get("PATH"))+TMP = Path(tempfile.mkdtemp(prefix="st-figure-examples-"))+++def showtime(*args, check=True, timeout=300):+ cp = subprocess.run([sys.executable, str(LAUNCHER)] + [str(a) for a in args], env=ENV,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",+ errors="replace", timeout=timeout)+ if check and cp.returncode != 0:+ raise AssertionError("showtime %s failed (rc=%d):\n%s\n%s" % (+ " ".join(map(str, args)), cp.returncode, cp.stdout[-3000:], cp.stderr[-3000:]))+ return cp+++_FIG = {}+++def figure(name):+ """A fresh `showtime new diagram --example <name>` project, exported once as its live figure."""+ if name not in _FIG:+ proj = TMP / ("proj-" + name)+ showtime("new", "diagram", proj, "--example", name, "--theme", "white")+ out = TMP / (name + "-figure.html")+ showtime("export", "html", proj, "--interactive", "-o", out, "--json")+ _FIG[name] = (proj, out)+ return _FIG[name]+++TYPE = r"""+import path from 'node:path';+import { pathToFileURL } from 'node:url';+const [SKILL, FILE] = process.argv.slice(2);+const { openBrowser } = await import(pathToFileURL(path.join(SKILL, 'scripts', 'lib', 'stagehost.mjs')).href);+const { browser } = await openBrowser({ headless: true });+const out = { errors: [], net: [] };+try {+ const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 } });+ await ctx.addInitScript(() => { window.SHOWTIME_TEST_TOUR_AFTER = 1e9; }); // no idle tour typing over the test+ const pg = await ctx.newPage();+ pg.on('console', (m) => { if (m.type() === 'error') out.errors.push(m.text()); });+ pg.on('pageerror', (e) => out.errors.push(String(e.message || e)));+ pg.on('request', (r) => { if (!/^(file|blob|data):/.test(r.url())) out.net.push(r.url()); });+ await pg.goto(pathToFileURL(FILE).href);+ await pg.waitForFunction(() => window.Figure && window.Figure.current && document.querySelector('[data-paper-line]'), null, { timeout: 20000, polling: 100 });+ await pg.waitForTimeout(800); // the first step's own keystroke settles+ await pg.mouse.click(4, 4);+ await pg.keyboard.type('hi', { delay: 120 });+ await pg.waitForTimeout(500);+ out.before = await pg.evaluate(() => [...document.querySelectorAll('[data-paper-line]')].map((g) => [g.getAttribute('data-paper-line'), g.getAttribute('data-text')]));+ await pg.keyboard.press('Enter');+ await pg.waitForTimeout(1200);+ out.after = await pg.evaluate(() => [...document.querySelectorAll('[data-paper-line]')].map((g) => [g.getAttribute('data-paper-line'), g.getAttribute('data-text')]));+ out.readouts = await pg.evaluate(() => [...document.querySelectorAll('.fg-ro .ro')].map((d) => [d.querySelector('.k').textContent, d.querySelector('.v').textContent]));+ out.state = await pg.evaluate(() => ({ typed: Figure.current.state.typed, col: Figure.current.state.col, line: Figure.current.state.line }));+} catch (e) { out.error = String(e && e.stack || e); }+await browser.close();+console.log(JSON.stringify(out));+"""+++class TestTypewriterStatic(unittest.TestCase):+ def test_example_is_listed(self):+ self.assertTrue((EXAMPLES / "typewriter.js").is_file())+ cli = (SKILL / "lib" / "st" / "cli_core.py").read_text(encoding="utf-8")+ self.assertIn("typewriter", cli[cli.index("--example"):], "the CLI help names the example")++ def test_scene_contract(self):+ src = (EXAMPLES / "typewriter.js").read_text(encoding="utf-8")+ for k in ("type: {", "film: 'tour'", "press:", "readouts:", "not a specific model"):+ self.assertIn(k, src)++[email protected](FAST or not NODE, "--fast: no browser")+class TestTypewriterFigure(unittest.TestCase):+ def test_typing_lands_on_the_paper(self):+ _, out = figure("typewriter")+ html = out.read_text(encoding="utf-8")+ self.assertIn("Content-Security-Policy", html, "one offline file")+ drv = TMP / "type.mjs"+ drv.write_text(TYPE, encoding="utf-8")+ cp = subprocess.run([NODE, str(drv), str(SKILL), str(out)], env=ENV, cwd=str(TMP), stdout=subprocess.PIPE,+ stderr=subprocess.PIPE, encoding="utf-8", errors="replace", timeout=300)+ self.assertEqual(cp.returncode, 0, cp.stderr[-2000:])+ r = json.loads(cp.stdout.strip().splitlines()[-1])+ self.assertNotIn("error", r, r.get("error"))+ self.assertEqual(r["errors"], [], "no console error")+ self.assertEqual(r["net"], [], "no network request")+ before = dict(r["before"])+ self.assertEqual(before.get("0"), "hi", "the typed letters are on the typing line (a fresh one after the demo's): %s" % r["before"])+ after = dict(r["after"])+ self.assertEqual(after.get("0"), "", "Return starts a new, empty line: %s" % r["after"])+ self.assertEqual(after.get("1"), "hi", "the paper keeps \"hi\" on the line above: %s" % r["after"])+ self.assertEqual(r["state"]["col"], 0, "the carriage is home")+ self.assertEqual(r["state"]["line"], 3, "the demo's line, the line typed on, the new one")+ ro = dict(r["readouts"])+ self.assertEqual(ro.get("Line"), "3", r["readouts"])+ self.assertTrue(ro.get("Carriage", "").startswith("0 of "), r["readouts"])+++if __name__ == "__main__":+ argv = [a for a in sys.argv if a != "--fast"]+ unittest.main(argv=argv, verbosity=2 if "-v" in argv else 1)diff --git a/skills/showtime/tests/test_github.py b/skills/showtime/tests/test_github.pyindex 0cc2b3f..9635f6c 100644--- a/skills/showtime/tests/test_github.py+++ b/skills/showtime/tests/test_github.py@@ -100,9 +100,9 @@ def check_body(tc: unittest.TestCase, body, where: str) -> dict:@unittest.skipIf(yaml is None, SKIP_YAML)@unittest.skipUnless(GH_DIR.is_dir(), SKIP_REPO)class IssueForms(unittest.TestCase):- def test_four_forms_parse_and_follow_the_schema(self):+ def test_five_forms_parse_and_follow_the_schema(self):names = sorted(p.name for p in FORMS.glob("*.yml") if p.name != "config.yml")- self.assertEqual(names, ["bug.yml", "docs.yml", "example-request.yml", "feature.yml"])+ self.assertEqual(names, ["bug.yml", "docs.yml", "example-request.yml", "feature.yml", "xray-gallery.yml"])for name in names:d = load(FORMS / name)for key in ("name", "description", "body"):diff --git a/skills/showtime/tests/test_job.py b/skills/showtime/tests/test_job.pyindex 2e2eb68..c5419d2 100644--- a/skills/showtime/tests/test_job.py+++ b/skills/showtime/tests/test_job.py@@ -816,6 +816,29 @@ class JobTests(unittest.TestCase):"final.mp4")+ def test_git_sha_unreadable_git_is_none(self):+ """job init in a sandbox that cannot read .git must not crash: no sha, not an error."""+ from unittest import mock+ from st.job import ledger+ repo = self.tmp / "unreadable"+ (repo / ".git").mkdir(parents=True)+ (repo / ".git" / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")+ with mock.patch.object(Path, "is_file", side_effect=PermissionError(1, "Operation not permitted")), \+ mock.patch.object(Path, "is_dir", side_effect=PermissionError(1, "Operation not permitted")):+ self.assertIsNone(ledger._git_sha(repo))++ def test_network_probe_gives_up_when_dns_hangs(self):+ """The probe stops after its timeout even when the lookup blocks (offline or sandboxed)."""+ from unittest import mock+ from st import sandbox+ with mock.patch("socket.getaddrinfo", side_effect=lambda *a, **k: time.sleep(30)):+ t0 = time.time()+ ok, detail = sandbox.probe_network("https://example.invalid/", timeout=0.5)+ self.assertFalse(ok)+ self.assertLess(time.time() - t0, 5)+ self.assertIn("no answer", detail)++if __name__ == "__main__":argv = [a for a in sys.argv if a != "--fast"]t0 = time.time()diff --git a/skills/showtime/tests/test_site_search.py b/skills/showtime/tests/test_site_search.pyindex 5d6611e..688c525 100644--- a/skills/showtime/tests/test_site_search.py+++ b/skills/showtime/tests/test_site_search.py@@ -236,7 +236,8 @@ class RealBuild(unittest.TestCase):# a page on a Choose your path card and in an act is named by its actself.assertIn("Act III \u00b7 Building the picture</a> / Components</div>",(out / "docs" / "components.html").read_text(encoding="utf-8"))- self.assertIn("In 0.4.2: ", landing)+ newest = re.search(r"^## (\d+\.\d+\.\d+)", (REPO / "docs" / "whats-new.md").read_text(encoding="utf-8"), re.M).group(1)+ self.assertIn("In %s: " % newest, landing, "the landing tile names the newest release in What's new")self.assertIn("House vocabulary", voice)h1 = re.search(r"^# (.+)$", (REPO / "CONTEXT.md").read_text(encoding="utf-8"), re.M).group(1)self.assertIn(h1, (out / "docs" / "house-vocabulary.html").read_text(encoding="utf-8"))diff --git a/skills/showtime/tests/test_xray.py b/skills/showtime/tests/test_xray.pynew file mode 100644index 0000000..762c806--- /dev/null+++ b/skills/showtime/tests/test_xray.py@@ -0,0 +1,820 @@+#!/usr/bin/env python3+"""`showtime xray analyze` and `verify` (lib/st/xray.py, cli_xray.py): the onset detector on synthetic clicks (exact+event times and frames, min-gap merging and the dropped list, a click's precursor), analyze end to end on a tiny+film, verify against lavfi films with a 6x6 sync marker flashing on chosen frames (matched, missing, extra, one frame+off, no marker), the audio lag of a delayed copy, the scroll smoothness report (stalls and jumps), the receipt and qa+hooks, and the tracker on a synthetic moving texture (seeded, with a tolerance).++Everything is generated here (no fixtures, no browser, no downloads) and deterministic.++usage: python tests/test_xray.py [--fast] [-v]+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import json+import math+import shutil+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++import numpy as np++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+sys.path.insert(0, str(SKILL / "lib"))++from st import ff, xray # noqa: E402+from st.common import ShowtimeError, write_json # noqa: E402+from st.launcher import build_env, showtime_home # noqa: E402++ENV = build_env(showtime_home())+SR = 48000+FPS = 30+W, H = 320, 180+CLICKS = [0.60, 1.23, 1.80, 2.55, 3.10, 3.97] # seconds+LEVELS = [0.50, 0.30, 0.45, 0.20, 0.50, 0.35]+++def click_wave(times, levels=None, dur=5.0, seed=7, noise=0.002, extra=()):+ """Mono float32 at 48 kHz: a low noise floor and a decaying noise burst (about 12 ms) at each time."""+ rng = np.random.RandomState(seed)+ x = rng.randn(int(dur * SR)) * noise+ levels = levels or [0.4] * len(times)+ for t, lv in list(zip(times, levels)) + list(extra):+ n = int(0.05 * SR)+ a = int(round(t * SR))+ env = np.exp(-np.arange(n) / (0.004 * SR))+ env[: int(0.0003 * SR)] *= np.linspace(0.2, 1.0, int(0.0003 * SR))+ x[a: a + n] += rng.randn(n)[: len(x[a: a + n])] * env[: len(x[a: a + n])] * lv+ return x.astype(np.float32)+++def write_wav(path: Path, x) -> Path:+ import soundfile as sf+ sf.write(str(path), x, SR, subtype="PCM_16")+ return path+++def st(*args, check=True):+ cp = subprocess.run([sys.executable, str(LAUNCHER)] + [str(a) for a in args], env=ENV, stdout=subprocess.PIPE,+ stderr=subprocess.PIPE, encoding="utf-8", errors="replace", timeout=600)+ if check and cp.returncode != 0:+ raise AssertionError("showtime %s failed (rc=%d):\n%s\n%s" % (" ".join(map(str, args)), cp.returncode,+ cp.stdout[-2000:], cp.stderr[-2000:]))+ return cp+++def make_film(path: Path, audio: Path, flashes, seconds=5.0, delay_ms=0.0, marker=True) -> Path:+ """320x180 black film at 30 fps with a 6x6 white square at (W-10, H-10) on the given frames, and `audio`."""+ vf = "format=yuv420p"+ if marker and flashes:+ enable = "+".join("eq(n,%d)" % f for f in flashes)+ vf = "drawbox=x=%d:y=%d:w=6:h=6:color=white:t=fill:enable='%s',format=yuv420p" % (W - 10, H - 10, enable)+ af = "adelay=%d|%d" % (round(delay_ms), round(delay_ms)) if delay_ms else "anull"+ ff.run_ffmpeg(["-f", "lavfi", "-i", "color=c=black:s=%dx%d:r=%d:d=%g" % (W, H, FPS, seconds), "-i", str(audio),+ "-vf", vf, "-af", af, "-c:v", "libx264", "-preset", "ultrafast", "-crf", "10",+ "-c:a", "pcm_s16le", "-shortest", str(path)])+ return path+++def within(a, b, tol):+ return abs(a - b) <= tol+++class Detector(unittest.TestCase):+ def test_frame_rule(self):+ self.assertEqual(xray.frame_of(2.183, 30), 65)+ self.assertEqual(xray.frame_of(1.0, 30), 30)+ self.assertEqual(xray.frame_of(0.1 * 3, 30), 9) # 0.30000000000000004 * 30 must not tip over+ self.assertEqual(xray.frame_of(2.0, 29.97), 59)++ def test_clicks_found_at_their_times(self):+ x = click_wave(CLICKS, LEVELS)+ ev, dropped = xray.detect(x)+ self.assertEqual(len(ev), len(CLICKS), (ev, dropped))+ for e, t in zip(ev, CLICKS):+ self.assertTrue(within(e["t"], t, 0.003), (e, t))+ self.assertEqual(xray.frame_of(e["t"], FPS), xray.frame_of(t, FPS) if abs(e["t"] - t) < 0.0005 else+ xray.frame_of(e["t"], FPS))+ self.assertEqual(e["band"], "high")+ self.assertEqual([e["i"] for e in ev], list(range(len(CLICKS))))+ strengths = [e["strength"] for e in ev]+ self.assertAlmostEqual(max(strengths), 1.0, places=3)+ self.assertTrue(strengths[3] < 0.6 < strengths[0], strengths) # 0.2 vs 0.5 amplitude++ def test_deterministic(self):+ x = click_wave(CLICKS, LEVELS)+ self.assertEqual(xray.detect(x), xray.detect(x.copy()))++ def test_loudness_does_not_move_the_events(self):+ x = click_wave(CLICKS, LEVELS)+ a, _ = xray.detect(x)+ b, _ = xray.detect(x * 0.25)+ c, _ = xray.detect(np.clip(x * 1.8, -1, 1))+ self.assertEqual([e["t"] for e in a], [e["t"] for e in b])+ self.assertEqual([e["t"] for e in a], [e["t"] for e in c])++ def test_from_to_window(self):+ x = click_wave(CLICKS, LEVELS)+ ev, _ = xray.detect(x[int(1.0 * SR): int(3.2 * SR)], t_from=1.0, t_to=3.2)+ self.assertEqual([round(e["t"], 1) for e in ev], [1.2, 1.8, 2.6, 3.1])++ def test_min_gap_merge_keeps_the_first_and_says_so(self):+ x = click_wave([1.0, 1.08, 2.0], [0.5, 0.45, 0.5])+ ev, dropped = xray.detect(x, min_gap=0.12)+ self.assertEqual(len(ev), 2, ev)+ self.assertTrue(within(ev[0]["t"], 1.0, 0.003))+ d = [x for x in dropped if "min_gap" in x["why"]]+ self.assertEqual(len(d), 1, dropped)+ self.assertTrue(within(d[0]["t"], 1.08, 0.004), d)+ self.assertIn("event 0", d[0]["why"])+ ev2, _ = xray.detect(x, min_gap=0.05)+ self.assertEqual(len(ev2), 3, ev2)++ def test_precursor_before_a_louder_click_is_not_an_event(self):+ x = click_wave([1.0, 2.0], [0.5, 0.5], extra=[(1.95, 0.04)]) # a faint tick 50 ms before the click at 2.0+ ev, dropped = xray.detect(x)+ self.assertEqual(len(ev), 2, (ev, dropped))+ self.assertTrue(within(ev[1]["t"], 2.0, 0.003), ev)++ def test_quiet_noise_is_not_an_event(self):+ rng = np.random.RandomState(3)+ x = (rng.randn(3 * SR) * 0.003).astype(np.float32)+ ev, _ = xray.detect(x)+ self.assertLessEqual(len(ev), 2, ev)+ self.assertEqual(xray.detect(np.zeros(SR, dtype=np.float32))[0], [])++ def test_low_band(self):+ t = np.arange(int(0.2 * SR)) / SR+ thump = (np.sin(2 * np.pi * 90 * t) * np.exp(-t / 0.03) * 0.6).astype(np.float32)+ x = np.zeros(2 * SR, dtype=np.float32)+ x += np.random.RandomState(1).randn(len(x)).astype(np.float32) * 0.001+ x[SR: SR + len(thump)] += thump+ # the detector high-passes at 500 Hz to follow the attack; a thump's attack still reads as one event and its band is low+ ev, _ = xray.detect(x, sensitivity=0.8)+ if ev:+ self.assertEqual(ev[0]["band"], "low")+++NOTE_TIMES = [round(0.5 + 0.12 * k, 3) for k in range(25)]+++def chord_bed(times=NOTE_TIMES, dur=4.0, seed=2):+ """A steady three-note chord with a new plucked note entering at each time (every 120 ms): sustained, dense sound."""+ rng = np.random.RandomState(seed)+ t = np.arange(int(dur * SR)) / SR+ x = sum(0.12 * np.sin(2 * np.pi * f * t) for f in (220.0, 277.2, 329.6)) + rng.randn(len(t)) * 0.003+ pitches = [880.0, 1046.5, 1318.5, 1568.0, 1174.7, 987.8]+ for k, tn in enumerate(times):+ a = int(round(tn * SR))+ u = np.arange(min(int(0.3 * SR), len(x) - a)) / SR+ x[a: a + len(u)] += 0.25 * np.sin(2 * np.pi * pitches[k % 6] * u) * np.minimum(u / 0.002, 1.0) * np.exp(-u / 0.12)+ return x.astype(np.float32)+++def errors_ms(events, times):+ ts = np.array([e["t"] for e in events]) if events else np.array([9e3])+ return [float(np.min(np.abs(ts - t))) * 1000.0 for t in times]+++class Flux(unittest.TestCase):+ """The flux engine on a sustained chord with a note entering every 120 ms (the envelope engine cannot see them):+ all 25 found, median error about 1 ms, worst under 10 ms (measured: 1 ms median, 9 ms worst, 3 extra events)."""++ def test_flux_finds_the_notes_and_envelope_does_not(self):+ x = chord_bed()+ ev, dropped = xray.detect_flux(x)+ err = errors_ms(ev, NOTE_TIMES)+ self.assertLess(max(err), 10.0, err)+ self.assertLessEqual(float(np.median(err)), 2.0, err)+ self.assertLessEqual(len(ev) - len(NOTE_TIMES), 4, [e["t"] for e in ev])+ self.assertEqual([e["i"] for e in ev], list(range(len(ev))))+ self.assertTrue(all(0.0 < e["strength"] <= 1.0 for e in ev))+ self.assertTrue(all(b["t"] - a["t"] >= 0.06 - 1e-6 for a, b in zip(ev, ev[1:])))+ env_ev, _ = xray.detect(x)+ self.assertLess(sum(1 for e in errors_ms(env_ev, NOTE_TIMES) if e < 10.0), 5, len(env_ev))+ self.assertTrue(xray.is_sustained(x))++ def test_flux_is_deterministic_and_respects_from_to(self):+ x = chord_bed()+ self.assertEqual(xray.detect_flux(x), xray.detect_flux(x.copy()))+ a, b = 1.0, 2.5+ ev, _ = xray.detect_flux(x[int(a * SR): int(b * SR)], t_from=a, t_to=b)+ self.assertTrue(ev and min(e["t"] for e in ev) >= a - 0.001 and max(e["t"] for e in ev) <= b)++ def test_auto_picks_flux_for_music_and_envelope_for_clicks(self):+ ran, mg, why, ev, _ = xray.pick_engine(chord_bed(), "auto")+ self.assertEqual((ran, mg), ("flux", 0.06))+ self.assertIn("spectral flux", why)+ self.assertGreaterEqual(len(ev), 25)+ ran, mg, why, ev, _ = xray.pick_engine(click_wave(CLICKS, LEVELS), "auto")+ self.assertEqual((ran, mg), ("envelope", 0.12))+ self.assertEqual(len(ev), len(CLICKS))+ self.assertEqual(xray.pick_engine(click_wave(CLICKS, LEVELS), "flux")[0], "flux")+ self.assertEqual(xray.pick_engine(chord_bed(), "envelope")[0], "envelope")+ with self.assertRaises(ShowtimeError):+ xray.pick_engine(chord_bed(), "magic")++ def test_analyze_records_the_engine_and_why(self):+ tmp = Path(tempfile.mkdtemp(prefix="st-xray-test-"))+ try:+ wav = write_wav(tmp / "bed.wav", chord_bed())+ clip = make_film(tmp / "bed.mkv", wav, [], seconds=4.0)+ doc = xray.analyze(clip, tmp / "o")+ self.assertEqual(doc["detector"]["engine"], "flux")+ self.assertEqual(doc["detector"]["min_gap"], 0.06)+ self.assertIn("auto", doc["detector"]["note"])+ self.assertGreaterEqual(len(doc["events"]), 25)+ self.assertEqual(xray.analyze(clip, tmp / "o2", engine="envelope")["detector"]["engine"], "envelope")+ cp = st("xray", "analyze", clip, "--job", tmp / "j", "--engine", "flux", "--json")+ self.assertEqual(json.loads(cp.stdout[cp.stdout.index("{"):])["detector"]["note"], "asked for flux")+ finally:+ shutil.rmtree(tmp, True)+++class Vfr(unittest.TestCase):+ def test_vfr_rule(self):+ self.assertTrue(xray._is_vfr({"r_fps": 30.0, "avg_fps": 24.0}))+ self.assertFalse(xray._is_vfr({"r_fps": 30.0, "avg_fps": 29.97}))+ self.assertFalse(xray._is_vfr({"r_fps": None, "avg_fps": 30.0}))++ def test_source_info_and_message(self):+ from unittest import mock+ pr = {"has_video": True, "fps": 24.0, "r_fps": 30.0, "avg_fps": 24.0, "width": 8, "height": 8, "has_audio": True, "duration": 1.0}+ with tempfile.TemporaryDirectory() as td:+ clip = Path(td) / "c.mp4"+ clip.write_bytes(b"x")+ with mock.patch.object(xray.ff, "probe", return_value=pr):+ info = xray._source_info(clip)+ self.assertTrue(info["vfr"])+ self.assertIn("showtime footage trim", xray.VFR_WARNING)+ self.assertIn("--webm", xray.VFR_WARNING)+++class TrackRow(unittest.TestCase):+ def test_a_failed_extraction_does_not_shift_the_pairing(self):+ from PIL import Image+ from unittest import mock+ from st.qa import media+ doc = {"source": {"fps": 30, "width": 64, "height": 36, "duration": 2.0}}+ n = 20+ Hs = [[1, 0, float(k), 0, 1, 0, 0, 0, 1] for k in range(n)]+ tr = {"region": [[2, 2], [30, 2], [30, 20], [2, 20]], "H": Hs}++ def fake(video, times, out_dir, names=None, width=None, **kw):+ out = []+ for nm in names:+ if nm == "f4.jpg":+ continue # this one fails+ p = Path(out_dir) / nm+ Image.new("RGB", (width, int(width * 36 / 64)), (40, 40, 40)).save(str(p))+ out.append(p)+ return out+ img = Image.new("RGB", (600, 100))+ with mock.patch.object(media, "extract_frames", side_effect=fake):+ out = xray._with_track_row(img, Path("clip.mp4"), doc, tr)+ self.assertGreater(out.height, img.height)+++class Analyze(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.tmp = Path(tempfile.mkdtemp(prefix="st-xray-test-"))+ cls.wav = write_wav(cls.tmp / "clicks.wav", click_wave(CLICKS, LEVELS))+ cls.clip = make_film(cls.tmp / "clip.mkv", cls.wav, [])++ @classmethod+ def tearDownClass(cls):+ shutil.rmtree(cls.tmp, True)++ def test_xray_json_and_check_sheet(self):+ out = self.tmp / "a1"+ doc = xray.analyze(self.clip, out)+ self.assertEqual(doc["schema"], "showtime.xray/1")+ src = doc["source"]+ self.assertEqual((src["file"], src["fps"], src["width"], src["height"]), ("clip.mkv", 30, W, H))+ self.assertEqual(len(src["sha256"]), 64)+ self.assertAlmostEqual(src["duration"], 5.0, delta=0.1)+ det = dict(doc["detector"])+ self.assertIn("envelope engine", det.pop("note"))+ self.assertEqual(det, {"engine": "envelope", "min_gap": 0.12, "sensitivity": 0.5, "from": 0.0, "to": None})+ self.assertEqual([e["frame"] for e in doc["events"]], [xray.frame_of(e["t"], 30) for e in doc["events"]])+ self.assertEqual(len(doc["events"]), len(CLICKS))+ self.assertEqual(set(doc["events"][0]), {"i", "t", "frame", "strength", "band"})+ self.assertIsNone(doc["track"])+ self.assertEqual(json.loads((out / "xray.json").read_text(encoding="utf-8")), doc)+ from PIL import Image+ with Image.open(str(out / "check.jpg")) as im:+ self.assertGreater(im.width, 1000)++ def test_cli_creates_a_job_and_prints_the_files(self):+ base = self.tmp / "cli"+ base.mkdir()+ job = base / "xjob"+ cp = st("xray", "analyze", self.clip, "--job", job, "--min-gap", "0.2", "--json")+ doc = json.loads(cp.stdout[cp.stdout.index("{"):])+ self.assertEqual(doc["detector"]["min_gap"], 0.2)+ self.assertTrue((job / "xray" / "xray.json").is_file())+ self.assertTrue((job / "xray" / "check.jpg").is_file())+ bad = st("xray", "analyze", self.clip, "--job", job, "--track", check=False)+ self.assertEqual(bad.returncode, 1)+ self.assertIn("--region", bad.stderr)+ self.assertEqual(st("xray", "analyze", self.clip, "--sensitivity", "3", check=False).returncode, 1)+++class Refusals(unittest.TestCase):+ """A clip that cannot be analyzed is refused before anything is created for it."""++ @classmethod+ def setUpClass(cls):+ cls.tmp = Path(tempfile.mkdtemp(prefix="st-xray-test-"))+ cls.silent = cls.tmp / "silent.mkv"+ ff.run_ffmpeg(["-f", "lavfi", "-i", "color=c=black:s=64x64:r=30:d=1", "-c:v", "libx264", "-pix_fmt", "yuv420p", str(cls.silent)])++ @classmethod+ def tearDownClass(cls):+ shutil.rmtree(cls.tmp, True)++ def test_no_stray_job_or_output_for_a_bad_clip(self):+ for n, (cmd, clip) in enumerate((("analyze", self.tmp / "missing.mkv"), ("notes", self.tmp / "missing.mkv"),+ ("analyze", self.silent), ("notes", self.silent))):+ job = self.tmp / ("job%d" % n)+ cp = st("xray", cmd, clip, "--job", job, check=False)+ self.assertEqual(cp.returncode, 1, cp.stderr)+ self.assertFalse(job.exists(), "%s %s left %s" % (cmd, clip.name, job))+ out = self.tmp / "direct"+ with self.assertRaises(ShowtimeError):+ xray.analyze(self.silent, out)+ self.assertFalse(out.exists())+++class Verify(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.tmp = Path(tempfile.mkdtemp(prefix="st-xray-test-"))+ cls.wav = write_wav(cls.tmp / "clicks.wav", click_wave(CLICKS, LEVELS))+ cls.clip = make_film(cls.tmp / "clip.mkv", cls.wav, [])+ cls.frames = [xray.frame_of(t, FPS) for t in CLICKS]++ @classmethod+ def tearDownClass(cls):+ shutil.rmtree(cls.tmp, True)++ def job(self, name, frames=None):+ """A job folder whose xray.json holds events on `frames` (default: the clicks' frames)."""+ j = self.tmp / name+ (j / "xray").mkdir(parents=True)+ fr = self.frames if frames is None else frames+ write_json(j / "job.json", {"schema": 1, "slug": name})+ write_json(j / "xray" / "xray.json", {+ "schema": "showtime.xray/1", "source": {"file": "clip.mkv", "sha256": "0" * 64, "fps": 30, "duration": 5.0,+ "width": W, "height": H},+ "detector": {"engine": "envelope", "min_gap": 0.12, "sensitivity": 0.5, "from": 0.0, "to": None},+ "events": [{"i": i, "t": round(f / 30.0, 3), "frame": f, "strength": 1.0, "band": "high"} for i, f in enumerate(fr)],+ "dropped": [], "track": None})+ (j / "xray" / "source.path").write_text(str(self.clip), encoding="utf-8")+ return j++ def test_all_events_on_their_frame(self):+ j = self.job("ok")+ film = make_film(j / "final.mkv", self.wav, self.frames)+ r = xray.verify(j, film)+ self.assertTrue(r["marker"])+ self.assertEqual((r["events"], r["on_frame"], r["missing"], r["extra"]), (6, 6, [], []))+ self.assertEqual(r["status"], "PASS", r)+ self.assertLess(abs(r["audio_lag_ms"]), 2.0, r)+ self.assertEqual(r["line"].split(" · ")[0], "6 of 6 events on their frame")+ self.assertIn("ms from the original", r["line"])+ self.assertEqual(json.loads((j / "xray" / "verify.json").read_text(encoding="utf-8"))["on_frame"], 6)++ def test_missing_and_extra_fail(self):+ j = self.job("bad")+ flashes = [f for f in self.frames if f != self.frames[2]] + [140]+ film = make_film(j / "final.mkv", self.wav, flashes)+ r = xray.verify(j, film)+ self.assertEqual(r["missing"], [self.frames[2]])+ self.assertEqual(r["extra"], [140])+ self.assertEqual(r["on_frame"], 5)+ self.assertEqual(r["status"], "FAIL")+ self.assertEqual(r["line"].split(" · ")[0], "5 of 6 events on their frame")+ self.assertEqual(r["findings"][0]["rule"], "xray_sync")++ def test_one_frame_off_is_matched_with_its_offset_but_not_exact(self):+ j = self.job("off")+ flashes = list(self.frames)+ flashes[1] += 1+ film = make_film(j / "final.mkv", self.wav, flashes)+ r = xray.verify(j, film)+ self.assertEqual((r["on_frame"], r["missing"], r["extra"], r["max_offset"]), (5, [], [], 1))+ self.assertEqual(r["off_by"], [{"expected": self.frames[1], "on": self.frames[1] + 1, "offset": 1}])+ self.assertEqual(r["status"], "WARN")++ def test_offset_frames_shift_the_expectation(self):+ j = self.job("offs")+ film = make_film(j / "final.mkv", self.wav, [f + 15 for f in self.frames], seconds=6.0, delay_ms=500)+ r = xray.verify(j, film, offset_frames=15)+ self.assertEqual((r["events"], r["on_frame"], r["status"]), (6, 6, "PASS"), r)+ self.assertLess(abs(r["audio_lag_ms"]), 2.0, r) # the film's sound is 15 frames late on purpose: not counted++ def test_offset_and_band_come_from_the_project_config(self):+ j = self.job("cfg")+ (j / "project").mkdir()+ write_json(j / "project" / "showtime.json", {"xray": {"marker": True, "offset_frames": 15, "band": [100, 140]}})+ film = make_film(j / "final.mkv", self.wav, [f + 15 for f in self.frames], seconds=6.0, delay_ms=500)+ r = xray.verify(j, film) # no flags: qa calls it like this+ self.assertEqual((r["on_frame"], r["status"], r["offset_frames"]), (6, "PASS", 15), r)+ self.assertEqual(r["smoothness"]["frames"], r["film_frames"]) # the band rows 100..140, read from the config+ r2 = xray.verify(j, film, offset_frames=0) # a flag wins over the file+ self.assertEqual(r2["status"], "FAIL")+ self.assertEqual(xray.project_config(self.tmp / "nowhere"), {})++ def test_skipped_events_carry_no_marker(self):+ j = self.job("skip")+ (j / "project").mkdir()+ write_json(j / "project" / "showtime.json", {"xray": {"marker": True, "skip": [0]}})+ film = make_film(j / "final.mkv", self.wav, self.frames[1:])+ r = xray.verify(j, film)+ self.assertEqual((r["events"], r["on_frame"], r["skipped"], r["status"]), (5, 5, 1, "PASS"), r)+ self.assertTrue(r["line"].startswith("5 of 5 events on their frame"), r["line"])+ film2 = make_film(j / "final2.mkv", self.wav, self.frames) # a marker on the skipped event is extra+ r2 = xray.verify(j, film2)+ self.assertEqual((r2["extra"], r2["status"]), ([self.frames[0]], "FAIL"), r2)+ r3 = xray.verify(j, film2, skip=[]) # a flag wins over the file+ self.assertEqual((r3["events"], r3["skipped"], r3["status"]), (6, 0, "PASS"), r3)++ def test_clip_fps_differs_from_the_film_fps(self):+ j = self.job("fps", [])+ doc = json.loads((j / "xray" / "xray.json").read_text(encoding="utf-8"))+ doc["source"]["fps"] = 29.97+ times = [0.60, 1.23, 2.55, 3.97]+ doc["events"] = [{"i": i, "t": t, "frame": int(t * 29.97), "strength": 1.0, "band": "high"} for i, t in enumerate(times)]+ write_json(j / "xray" / "xray.json", doc)+ film = make_film(j / "final.mkv", self.wav, [xray.frame_of(t, 30) for t in times]) # the 30 fps film's frames+ r = xray.verify(j, film)+ self.assertEqual((r["events"], r["on_frame"], r["missing"], r["extra"], r["status"]), (4, 4, [], [], "PASS"), r)+ self.assertNotEqual([int(t * 29.97) for t in times], [xray.frame_of(t, 30) for t in times]) # the fixture tells them apart+ self.assertTrue(any("floor(t * film fps)" in n for n in r["notes"]), r["notes"])++ def test_no_marker_is_unverified_warn(self):+ j = self.job("nomark")+ film = make_film(j / "final.mkv", self.wav, [], marker=False)+ r = xray.verify(j, film)+ self.assertFalse(r["marker"])+ self.assertEqual(r["status"], "WARN")+ self.assertEqual(r["findings"][0]["rule"], "xray_unverified")+ self.assertIn("not verified", r["line"])++ def test_a_corner_that_is_always_bright_is_not_a_marker(self):+ j = self.job("bright")+ film = j / "final.mkv"+ ff.run_ffmpeg(["-f", "lavfi", "-i", "color=c=black:s=%dx%d:r=%d:d=5" % (W, H, FPS), "-i", str(self.wav),+ "-vf", "drawbox=x=%d:y=%d:w=6:h=6:color=white:t=fill,format=yuv420p" % (W - 10, H - 10),+ "-c:v", "libx264", "-preset", "ultrafast", "-crf", "10", "-c:a", "pcm_s16le", "-shortest", str(film)])+ r = xray.verify(j, film)+ self.assertFalse(r["marker"])+ self.assertEqual(r["findings"][0]["rule"], "xray_unverified")++ def test_the_source_is_looked_for_by_name_inside_the_job_only(self):+ j = self.job("find")+ (j / "xray" / "source.path").unlink()+ doc = {"source": {"file": "clip.mkv"}}+ self.assertIsNone(xray.find_source(j, doc))+ (j / "work").mkdir()+ shutil.copy(str(self.clip), str(j / "work" / "clip.mkv"))+ self.assertEqual(xray.find_source(j, doc), j / "work" / "clip.mkv")+ outside = self.tmp / "decoy.mkv"+ shutil.copy(str(self.clip), str(outside))+ for name in (str(outside), "../decoy.mkv", "..", "", "*"): # absolute, up, nothing, a glob: never found outside+ self.assertIsNone(xray.find_source(j, {"source": {"file": name}}), name)++ def test_unmatched_audio_with_a_marker_warns(self):+ j = self.job("noaudio")+ other = write_wav(self.tmp / "other.wav", click_wave([0.9, 2.1, 3.3, 4.4], seed=99, noise=0.05))+ film = make_film(j / "final.mkv", other, self.frames)+ r = xray.verify(j, film)+ self.assertEqual(r["status"], "WARN", r)+ self.assertIn("xray_audio", [f["rule"] for f in r["findings"]])++ def test_bad_skip_entries_are_named(self):+ j = self.job("badskip")+ film = make_film(j / "final.mkv", self.wav, self.frames)+ r = xray.verify(j, film, skip=[99, "x"])+ self.assertTrue(any("99" in n and "'x'" in n for n in r["notes"]), r["notes"])+ self.assertEqual(r["skipped"], 0)++ def test_collided_frames_are_reported(self):+ fr = list(self.frames)+ fr[1] = fr[0]+ j = self.job("collide", frames=fr)+ film = make_film(j / "final.mkv", self.wav, fr)+ r = xray.verify(j, film)+ self.assertEqual(r["events"], 6)+ self.assertTrue(any("share a film frame" in n for n in r["notes"]), r["notes"])++ def test_delayed_audio_gives_its_lag(self):+ j = self.job("lag")+ film = make_film(j / "final.mkv", self.wav, self.frames, delay_ms=40)+ r = xray.verify(j, film)+ self.assertTrue(within(r["audio_lag_ms"], 40.0, 1.5), r)+ self.assertEqual(r["status"], "FAIL")+ self.assertTrue(any("ms from the original" in f["message"] for f in r["findings"]), r["findings"])+ film2 = make_film(j / "final2.mkv", self.wav, self.frames, delay_ms=12)+ self.assertTrue(within(xray.verify(j, film2)["audio_lag_ms"], 12.0, 1.5))++ def test_events_beyond_the_film_are_not_counted(self):+ j = self.job("short", self.frames + [900])+ film = make_film(j / "final.mkv", self.wav, self.frames)+ r = xray.verify(j, film)+ self.assertEqual((r["events"], r["outside_film"], r["status"]), (6, 1, "PASS"))++ def test_cli_exit_codes_and_receipt(self):+ j = self.job("cli", None)+ film = make_film(j / "final.mkv", self.wav, self.frames)+ cp = st("xray", "verify", j, "--video", film)+ self.assertIn("6 of 6 events on their frame", cp.stdout)+ rec = json.loads((j / "receipt.json").read_text(encoding="utf-8"))+ self.assertEqual(rec["xray"]["on_frame"], 6)+ self.assertEqual(rec["xray"]["marker"], True)+ self.assertIn("audio_lag_ms", rec["xray"])+ self.assertIn("X-ray: 6 of 6 events", __import__("st.job.receipt", fromlist=["x"]).card(rec))+ film2 = make_film(j / "bad.mkv", self.wav, self.frames[:-1])+ bad = st("xray", "verify", j, "--video", film2, check=False)+ self.assertEqual(bad.returncode, 1, bad.stdout + bad.stderr)+ self.assertIn("5 of 6", bad.stdout)++ def test_qa_runs_verify_when_the_job_has_xray_json(self):+ from st.qa import video as V+ j = self.job("qa")+ film = make_film(j / "final.mp4", self.wav, self.frames[:-1])+ rep = V.run(film, sheet=False, quiet=True, record=False)+ rules = {f["rule"]: f for f in rep["findings"]}+ self.assertIn("xray_sync", rules, [f["rule"] for f in rep["findings"]])+ self.assertEqual(rules["xray_sync"]["severity"], "FAIL")+ film2 = make_film(j / "final2.mp4", self.wav, [], marker=False)+ rules2 = {f["rule"]: f for f in V.run(film2, sheet=False, quiet=True, record=False)["findings"]}+ self.assertEqual(rules2["xray_unverified"]["severity"], "WARN")+ self.assertNotIn("xray_sync", rules2)+ for r in ("xray_sync", "xray_sync_off", "xray_unverified", "xray_choppy"):+ self.assertIn(r, V.RULES)++ def test_qa_wants_a_credit_for_the_clip(self):+ from st.qa import video as V+ j = self.job("credit")+ (j / "project").mkdir()+ cfg = j / "project" / "showtime.json"+ film = make_film(j / "final.mp4", self.wav, self.frames)+ write_json(cfg, {"xray": {"marker": True}})+ rep = V.run(film, sheet=False, quiet=True, record=False)+ miss = [f for f in rep["findings"] if f["rule"] == "xray_credit"]+ self.assertEqual([f["severity"] for f in miss], ["WARN"], rep["findings"])+ self.assertIn("xray.own: true", miss[0]["message"])+ self.assertFalse([p for p in rep["passed"] if "no attribution required" in p], rep["passed"])+ write_json(cfg, {"xray": {"credit": "Typewriter by Someone, CC BY 3.0, https://example.invalid/t " + "x" * 80}})+ rep = V.run(film, sheet=False, quiet=True, record=False)+ self.assertFalse([f for f in rep["findings"] if f["rule"] == "xray_credit"])+ self.assertTrue([p for p in rep["passed"] if p.startswith("clip credit: Typewriter by Someone")], rep["passed"])+ write_json(cfg, {"xray": {"own": True}})+ rep = V.run(film, sheet=False, quiet=True, record=False)+ self.assertFalse([f for f in rep["findings"] if f["rule"] == "xray_credit"])+ write_json(cfg, {"xray": {"credit": "Typewriter by Someone, CC BY 3.0"}})+ r = xray.verify(j, film)+ self.assertEqual(xray.receipt_section(j)["credit"], "Typewriter by Someone, CC BY 3.0")+ from st.job import receipt as RC+ self.assertIn("Clip credit: Typewriter by Someone", RC.card(dict(RC.build(j), xray=xray.receipt_section(j))))+ self.assertIn("xray_credit", V.RULES)++ def test_player_chrome_ignores_the_xray_sound_strip(self):+ from PIL import Image+ from st.qa import floor, video as V+ from test_floor import with_player++ class Fnd:+ def __init__(self):+ self.items = []++ def add(self, rule, severity, message, **extra):+ self.items.append(dict(extra, rule=rule, severity=severity, message=message))++ low = np.asarray(with_player(Image.new("L", (540, 960), 245))) # a bar at 86 % of a portrait frame+ self.assertGreaterEqual(floor.player_strip(low)[0], 0.7 * 960)+ frames = [(1.5, low), (4.5, low), (7.5, low)]+ F = Fnd()+ floor.check(F, None, 9.0, 540, 960, frames=frames)+ self.assertEqual([f["rule"] for f in F.items if f["rule"] == "player_chrome"], ["player_chrome"])+ F = Fnd()+ out = floor.check(F, None, 9.0, 540, 960, frames=frames, xray_strip=True)+ self.assertFalse([f for f in F.items if f["rule"] == "player_chrome"], F.items)+ self.assertEqual(out["notes"], ["player_chrome skipped: the x-ray sound strip"])+ high = Image.new("L", (540, 960), 245) # real chrome higher up still warns+ high.paste(with_player(Image.new("L", (540, 480), 245)), (0, 0))+ highs = np.asarray(high)+ self.assertLess(floor.player_strip(highs)[0], 0.7 * 960)+ F = Fnd()+ floor.check(F, None, 9.0, 540, 960, frames=[(1.5, highs), (4.5, highs), (7.5, highs)], xray_strip=True)+ self.assertEqual(len([f for f in F.items if f["rule"] == "player_chrome"]), 1, F.items)+ wide = np.asarray(with_player(Image.new("L", (960, 540), 245))) # landscape: the bottom 12 % only+ F = Fnd()+ floor.check(F, None, 9.0, 960, 540, frames=[(1.5, wide), (4.5, wide), (7.5, wide)], xray_strip=True)+ self.assertEqual(len([f for f in F.items if f["rule"] == "player_chrome"]), 1, "bar at 86 % is above the 88 % line")+ # the project decides: template xray, or xray.json + an "xray" block+ j = self.job("strip")+ film = j / "final.mp4"+ self.assertTrue(V._xray_strip(None, film, {"template": "xray"}))+ self.assertTrue(V._xray_strip(None, film, {"xray": {"marker": True}}))+ self.assertFalse(V._xray_strip(None, film, {}))+ plain = self.tmp / "plainstrip"+ plain.mkdir()+ write_json(plain / "job.json", {"schema": 1, "slug": "p"})+ self.assertFalse(V._xray_strip(None, plain / "final.mp4", {"xray": {"marker": True}}))++ def test_qa_without_xray_json_is_untouched(self):+ from st.qa import video as V+ j = self.tmp / "plain"+ j.mkdir()+ write_json(j / "job.json", {"schema": 1, "slug": "plain"})+ film = make_film(j / "final.mp4", self.wav, [], marker=False)+ rep = V.run(film, sheet=False, quiet=True, record=False)+ self.assertFalse([f for f in rep["findings"] if f["rule"].startswith("xray")])+ self.assertNotIn("xray", rep)+++def texture_strip(n, seed=5):+ rng = np.random.RandomState(seed)+ from scipy import ndimage+ return ndimage.gaussian_filter1d(rng.rand(n), 1.5) * 255.0+++class Smoothness(unittest.TestCase):+ def profiles(self, steps, w=200):+ strip = texture_strip(w + int(sum(steps)) + 50)+ pos, out = 0.0, [strip[:w]]+ for s in steps:+ pos += s+ i = int(round(pos))+ out.append(strip[i: i + w])+ return np.array(out)++ def test_even_scroll_is_clean(self):+ r = xray.smoothness_of_profiles(self.profiles([4] * 60))+ self.assertEqual((r["stalls"], r["jumps"]), (0, 0), r)+ self.assertAlmostEqual(r["median_px"], 4.0, delta=0.3)++ def test_stalls_and_jumps_are_counted(self):+ steps = [4] * 60+ steps[20] = steps[21] = 0 # two stalled frames+ for i in range(40, 60):+ steps[i] = 10 # a sudden speed-up: one jump+ r = xray.smoothness_of_profiles(self.profiles(steps))+ self.assertEqual(r["stalls"], 2, r)+ self.assertEqual(r["jumps"], 1, r)+ self.assertEqual(r["stall_frames"], [21, 22])+ self.assertEqual(r["jump_frames"], [41])++ def test_a_band_that_barely_moves_has_no_stalls(self):+ r = xray.smoothness_of_profiles(self.profiles([0] * 30))+ self.assertEqual((r["stalls"], r["jumps"]), (0, 0))++ def test_band_of_a_film(self):+ w, h, n = 160, 24, 40+ strip = texture_strip(w + 6 * n + 20, seed=9)+ pos = 0+ raw = bytearray()+ for k in range(n):+ if k not in (10, 11):+ pos += 6+ raw += np.tile(strip[pos: pos + w], (h, 1)).clip(0, 255).astype(np.uint8).tobytes()+ film = Path(tempfile.mkdtemp(prefix="st-xray-test-")) / "scroll.mkv"+ subprocess.run([ff.ffmpeg_path(), "-hide_banner", "-loglevel", "error", "-y", "-f", "rawvideo", "-pix_fmt", "gray",+ "-s", "%dx%d" % (w, h), "-r", "30", "-i", "-", "-c:v", "ffv1", str(film)], input=bytes(raw), check=True)+ try:+ r = xray.smoothness(film, [0, 0, w, h])+ self.assertEqual(r["frames"], n)+ self.assertEqual(r["stalls"], 2, r)+ self.assertGreaterEqual(r["median_px"], 5.5)+ with self.assertRaises(ShowtimeError):+ xray.smoothness(film, [0, 0, 400, 10])+ finally:+ shutil.rmtree(film.parent, True)+++def have_cv2():+ try:+ import cv2 # noqa: F401+ return True+ except ImportError:+ return False++[email protected](have_cv2(), "opencv is not installed")+class Tracking(unittest.TestCase):+ N, REF = 36, 14++ @staticmethod+ def base():+ import cv2+ rng = np.random.RandomState(11)+ img = rng.rand(300, 400).astype(np.float32)+ img = cv2.GaussianBlur(img, (0, 0), 2.2)+ img = (img - img.min()) / (img.max() - img.min())+ return (img * 255).astype(np.uint8)++ @classmethod+ def affines(cls):+ """Ground truth: frame k = base warped by a drift of (1.6, 0.8) px per frame, a slow turn and a 0.1% zoom."""+ out = []+ for k in range(cls.N):+ ang = math.radians(0.12 * k)+ sc = 1.0 + 0.001 * k+ c, s = sc * math.cos(ang), sc * math.sin(ang)+ cx, cy = 200.0, 150.0+ A = np.array([[c, -s, cx - c * cx + s * cy + 1.6 * k], [s, c, cy - s * cx - c * cy + 0.8 * k], [0, 0, 1.0]])+ out.append(A)+ return out++ def frames(self):+ import cv2+ b = self.base()+ return [cv2.warpAffine(b, A[:2], (400, 300), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REFLECT) for A in self.affines()]++ def test_homographies_follow_the_texture(self):+ As = self.affines()+ region = np.array([[120, 80], [280, 85], [275, 215], [125, 210]], dtype=np.float64)+ hole = np.array([[190, 130], [220, 130], [220, 160], [190, 160]], dtype=np.float64)+ Hs = xray.track_frames(self.frames(), self.REF, region, [hole])+ self.assertEqual(len(Hs), self.N)+ self.assertTrue(np.allclose(Hs[self.REF], np.eye(3)))+ pts = np.hstack([region, np.ones((4, 1))])+ worst = 0.0+ for k in range(self.N):+ truth = (As[k] @ np.linalg.inv(As[self.REF]) @ pts.T).T+ got = (Hs[k] @ pts.T).T+ got = got[:, :2] / got[:, 2:3]+ worst = max(worst, float(np.abs(got - truth[:, :2]).max()))+ self.assertLess(worst, 1.5, "worst corner error %.2f px" % worst)++ def test_deterministic(self):+ region = np.array([[120, 80], [280, 85], [275, 215], [125, 210]], dtype=np.float64)+ fr = self.frames()+ a = xray.track_frames(fr, self.REF, region, [])+ b = xray.track_frames(fr, self.REF, region, [])+ self.assertTrue(all(np.array_equal(x, y) for x, y in zip(a, b)))++ def test_track_clip_writes_the_schema_object(self):+ tmp = Path(tempfile.mkdtemp(prefix="st-xray-test-"))+ try:+ raw = b"".join(f.tobytes() for f in self.frames())+ clip = tmp / "obj.mkv"+ subprocess.run([ff.ffmpeg_path(), "-hide_banner", "-loglevel", "error", "-y", "-f", "rawvideo", "-pix_fmt", "gray",+ "-s", "400x300", "-r", "30", "-i", "-", "-c:v", "ffv1", str(clip)], input=raw, check=True)+ src = {"width": 400, "height": 300, "fps": 30}+ region = [[120, 80], [280, 85], [275, 215], [125, 210]]+ tr = xray.track_clip(clip, src, region, self.REF / 30.0 + 0.001, [[[190, 130], [220, 130], [220, 160]]])+ self.assertEqual(tr["ref_frame"], self.REF)+ self.assertEqual(tr["region"], [[float(a), float(b)] for a, b in region])+ self.assertEqual(len(tr["H"]), self.N)+ self.assertEqual(len(tr["H"][0]), 9)+ self.assertEqual(len(tr["mask"]), 1)+ H = np.array(tr["H"][self.REF]).reshape(3, 3)+ self.assertTrue(np.allclose(H, np.eye(3), atol=1e-4))+ A = self.affines()+ truth = A[self.N - 1] @ np.linalg.inv(A[self.REF])+ Hl = np.array(tr["H"][self.N - 1]).reshape(3, 3)+ p = np.array([200.0, 150.0, 1.0])+ self.assertLess(float(np.abs((Hl @ p)[:2] / (Hl @ p)[2] - (truth @ p)[:2]).max()), 1.5)+ finally:+ shutil.rmtree(tmp, True)++ def test_a_clip_too_long_for_memory_is_refused_with_the_way_out(self):+ tmp = Path(tempfile.mkdtemp(prefix="st-xray-test-"))+ keep = xray.TRACK_MAX_BYTES+ try:+ raw = b"".join(f.tobytes() for f in self.frames())+ clip = tmp / "obj.mkv"+ subprocess.run([ff.ffmpeg_path(), "-hide_banner", "-loglevel", "error", "-y", "-f", "rawvideo", "-pix_fmt", "gray",+ "-s", "400x300", "-r", "30", "-i", "-", "-c:v", "ffv1", str(clip)], input=raw, check=True)+ xray.TRACK_MAX_BYTES = 400 * 300 * 5 # room for a handful of frames only+ src = {"width": 400, "height": 300, "fps": 30, "duration": self.N / 30.0}+ with self.assertRaises(ShowtimeError) as cm:+ xray.track_clip(clip, src, [[120, 80], [280, 85], [275, 215], [125, 210]], 0.5, [])+ self.assertIn("footage trim", cm.exception.hint)+ finally:+ xray.TRACK_MAX_BYTES = keep+ shutil.rmtree(tmp, True)++ def test_bad_region_is_a_clear_error(self):+ with self.assertRaises(ShowtimeError):+ xray.parse_points("10,10 20,20")+ with self.assertRaises(ShowtimeError):+ xray.parse_points("10,10 a,b 30,30")+ self.assertEqual(xray.parse_points("1,2 3,4 5,6.5"), [[1.0, 2.0], [3.0, 4.0], [5.0, 6.5]])+++if __name__ == "__main__":+ argv = [a for a in sys.argv if a != "--fast"]+ unittest.main(argv=argv)diff --git a/skills/showtime/tests/test_xray_drums.py b/skills/showtime/tests/test_xray_drums.pynew file mode 100644index 0000000..415c55c--- /dev/null+++ b/skills/showtime/tests/test_xray_drums.py@@ -0,0 +1,188 @@+#!/usr/bin/env python3+"""`showtime xray notes --instrument drums` (lib/st/xray_drums.py): a synthetic kit (kick = decaying 55 Hz sine with a click,+snare = noise burst + 190 Hz, closed/open hats and a crash = high-passed noise of different lengths, a tom = a 110 Hz tone+with a pitch drop) playing a known two-bar groove at 80..160 bpm -> the pieces and onsets come back. It is a classifier of+sound types: the accuracy here is on this synthetic kit, a hat under a (white-noise) snare is not seen and is not in the groove.++usage: python tests/test_xray_drums.py [-v]+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import json+import subprocess+import sys+import tempfile+import unittest+import xml.dom.minidom+from pathlib import Path++import numpy as np+from scipy import signal++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+sys.path.insert(0, str(SKILL / "lib"))+sys.path.insert(0, str(TESTS_DIR))++from st import ff, xray_drums as xd, xray_music as xm # noqa: E402+from st.launcher import build_env, showtime_home # noqa: E402+from test_xray_notes import read_midi # noqa: E402++SR = xd.SR+++def _hp(x, fc):+ return signal.sosfilt(signal.butter(4, fc, "hp", fs=SR, output="sos"), x)+++def kick():+ t = np.arange(int(.4 * SR)) / SR+ y = 0.8 * np.sin(2 * np.pi * 55 * t) * np.exp(-t / 0.12)+ n = int(.002 * SR)+ y[:n] += 0.3 * np.random.RandomState(1).randn(n)+ return y+++def snare():+ t = np.arange(int(.3 * SR)) / SR+ return 0.4 * np.random.RandomState(2).randn(len(t)) * np.exp(-t / 0.06) + 0.3 * np.sin(2 * np.pi * 190 * t) * np.exp(-t / 0.08)+++def noise_hit(tau, dur, amp=0.25, fc=6000, seed=3):+ t = np.arange(int(dur * SR)) / SR+ return amp * _hp(np.random.RandomState(seed).randn(len(t)), fc) * np.exp(-t / tau)+++def tom(f=110):+ t = np.arange(int(.5 * SR)) / SR+ return 0.7 * np.sin(2 * np.pi * (f * (1 + 0.2 * np.exp(-t / 0.03))) * t) * np.exp(-t / 0.15)+++SOUNDS = {"kick": kick, "snare": snare, "hihat_closed": lambda: noise_hit(0.015, 0.1), "hihat_open": lambda: noise_hit(0.12, 0.6),+ "crash": lambda: noise_hit(0.4, 1.4, 0.3), "tom": tom}+++def groove(bpm=100):+ """Two bars on eighths plus a crash on the next downbeat: hats on every eighth except under the snare, an open hat on the+ last eighth of bar 1, a tom on the 6th eighth of bar 2."""+ e = 60.0 / bpm / 2+ truth = []++ def at(i, p):+ truth.append((0.4 + i * e, p))+ for i in range(16):+ if i not in (2, 6, 10, 14):+ at(i, "hihat_open" if i == 7 else "hihat_closed")+ for i in (0, 4, 6, 8, 12, 16):+ at(i, "kick")+ for i in (2, 6, 10, 14):+ at(i, "snare")+ at(16, "crash")+ at(13, "tom")+ return sorted(truth)+++def render(truth, total, noise=0.003):+ x = np.zeros(int(total * SR))+ for t, p in truth:+ y = SOUNDS[p]()+ a = int(t * SR)+ x[a:a + len(y)] += y[:len(x) - a]+ return (x + np.random.RandomState(0).randn(len(x)) * noise).astype(np.float32)+++def score(truth, hits, tol=0.03):+ got = [(h["t"], h["piece"]) for h in hits]+ used, tp = set(), 0+ for t, p in truth:+ m = [k for k, (a, b) in enumerate(got) if k not in used and b == p and abs(a - t) <= tol]+ if m:+ used.add(m[0])+ tp += 1+ return tp, len(truth), len(got) - tp+++class Kit(unittest.TestCase):+ def test_groove_at_several_tempos(self):+ for bpm in (80, 100, 130, 160):+ truth = groove(bpm)+ hits = xd.detect_hits(render(truth, 0.4 + 16 * 60.0 / bpm / 2 + 2.5))+ tp, n, extra = score(truth, hits)+ self.assertGreaterEqual(tp, n - 1, "%d bpm: %d of %d" % (bpm, tp, n)) # at most one hit missed+ self.assertLessEqual(extra, 1, "%d bpm: %d extra" % (bpm, extra))++ def test_each_piece_found(self):+ truth = groove(100)+ pieces = {h["piece"] for h in xd.detect_hits(render(truth, 8.0))}+ self.assertEqual(pieces, {"kick", "snare", "hihat_closed", "hihat_open", "crash", "tom"})++ def test_kick_and_hat_on_one_onset_are_two_hits(self):+ truth = [(0.5, "kick"), (0.5, "hihat_closed"), (1.2, "hihat_closed"), (1.9, "kick")]+ hits = xd.detect_hits(render(truth, 3.0))+ at = sorted(h["piece"] for h in hits if abs(h["t"] - 0.5) < 0.03)+ self.assertEqual(at, ["hihat_closed", "kick"])++ def test_silence_has_no_hits(self):+ self.assertEqual(xd.detect_hits(np.zeros(SR * 2, np.float32)), [])+++class Files(unittest.TestCase):+ def test_command_end_to_end(self):+ import soundfile as sf+ truth = groove(100)+ x = render(truth, 8.0)+ with tempfile.TemporaryDirectory() as td:+ td = Path(td)+ sf.write(str(td / "a.wav"), x, SR, subtype="PCM_16")+ ff.run_ffmpeg(["-f", "lavfi", "-i", "color=c=black:s=160x90:r=30:d=8", "-i", str(td / "a.wav"), "-c:v", "libx264",+ "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "pcm_s16le", "-shortest", str(td / "d.mkv")])+ cp = subprocess.run([sys.executable, str(LAUNCHER), "xray", "notes", str(td / "d.mkv"), "--job", str(td / "job"), "--instrument", "drums"],+ env=build_env(showtime_home()), stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8", timeout=600)+ self.assertEqual(cp.returncode, 0, cp.stderr[-1500:])+ d = td / "job" / "xray"+ doc = json.loads((d / "notes.json").read_text())+ self.assertEqual(doc["instrument"], "drums")+ self.assertIn("classifier", doc["note"])+ self.assertEqual(doc["notes"], [])+ tp, n, extra = score(truth, [{"t": h["t"], "piece": h["piece"]} for h in doc["drums"]])+ self.assertGreaterEqual(tp, n - 1)+ for h in doc["drums"]:+ self.assertEqual(h["frame"], int(np.floor(h["t"] * 30 + 1e-9)))+ self.assertEqual(len(doc["quantized"]), len(doc["drums"]))+ self.assertAlmostEqual(doc["tempo"]["bpm"], 100.0, delta=8.0)+ # sixteenth grid: the groove is on eighths, from beat 0.5 on (the first hit sits at 0.4 s)+ b = sorted({q["beat"] for q in doc["quantized"]})+ self.assertTrue(all(abs(v * 2 - round(v * 2)) < 1e-9 for v in b), b)+ # MIDI: channel 10, the GM drum notes+ raw = (d / "notes.mid").read_bytes()+ ppq, tempo, ev = read_midi(raw.replace(b"\x99", b"\x90").replace(b"\x89", b"\x80"))+ self.assertIn(b"\x99", raw)+ self.assertEqual({p for *_, p, _v in ev}, {36, 38, 42, 46, 49, 45, 48, 50} & {h["gm"] for h in doc["drums"]})+ # MusicXML: well formed, a percussion staff, one unpitched note per hit+ dom = xml.dom.minidom.parseString((d / "notes.musicxml").read_bytes())+ self.assertEqual(dom.getElementsByTagName("sign")[-1].firstChild.data, "percussion")+ hits = [n for n in dom.getElementsByTagName("note") if n.getElementsByTagName("unpitched")]+ self.assertEqual(len(hits) - len([n for n in hits if n.getElementsByTagName("tie") and n.getElementsByTagName("tie")[0].getAttribute("type") == "stop"]),+ len(doc["drums"]))+ self.assertTrue(any(n.getElementsByTagName("notehead") for n in hits))+ self.assertGreater((d / "check-notes.jpg").stat().st_size, 5000)+++class Score(unittest.TestCase):+ def test_measures_are_full(self):+ hits = [{"i": 0, "piece": "kick", "gm": 36}, {"i": 1, "piece": "hihat_closed", "gm": 42}, {"i": 2, "piece": "snare", "gm": 38},+ {"i": 3, "piece": "tom", "gm": 48}]+ quant = [{"beat": 0.0}, {"beat": 0.0}, {"beat": 1.5}, {"beat": 5.25}]+ dom = xml.dom.minidom.parseString(xd.musicxml(hits, quant, 100.0).encode())+ for m in dom.getElementsByTagName("measure"):+ tot = sum(int(n.getElementsByTagName("duration")[0].firstChild.data) for n in m.getElementsByTagName("note")+ if not n.getElementsByTagName("chord"))+ self.assertEqual(tot, xd.MEASURE)+ self.assertEqual(len(dom.getElementsByTagName("measure")), 2)+++if __name__ == "__main__":+ unittest.main(verbosity=2 if "-v" in sys.argv else 1, argv=[a for a in sys.argv if a != "-v"])diff --git a/skills/showtime/tests/test_xray_live.py b/skills/showtime/tests/test_xray_live.pynew file mode 100644index 0000000..789863d--- /dev/null+++ b/skills/showtime/tests/test_xray_live.py@@ -0,0 +1,332 @@+#!/usr/bin/env python3+"""The X-ray live page: `showtime export html <xray project> --interactive` (scripts/lib/export/xray.mjs,+runtime/xray-live/pre.js, live.js, live.css).++ * static: the live page's parts exist; pre.js runs the stage in render mode and answers the page's fetch() from the+ packed data; live.js reaches no network and no wall-clock randomness; the sync marker is hidden; export.mjs sends+ an X-ray project's --interactive to the live page and a living diagram's to its figure+ * CLI: the template sample exports one offline file (a Content-Security-Policy, the clip packed once, the second+ copy a canvas mirror, no file left to fetch), under 16 MB, named <slug>-live.html; the diagram layout too+ * browser: the page starts, jumping to event i lands on its frame with the kit's strike at its peak (cur = i,+ s = 1) for several events, the marker is not shown, 0.25x plays around the event and ends on it, the theme+ switches on the same frame; no console error, no request off the file+ * touch: #hit4 opens on the 4th event; the typewriter's pads are its keys (a letter plays the next one, the+ plain letter is a pad, Shift+S is 0.25x); dragging the picture scrubs; a click on it plays the next event; a+ link names the hit; framed with #embed, the page reports its height to the host; --prompt travels++Stdlib only. usage: python tests/test_xray_live.py [--fast] [-v]+ --fast skip the browser check+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import json+import re+import shutil+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+LIVE = SKILL / "runtime" / "xray-live"+sys.path.insert(0, str(SKILL / "lib"))++from st.launcher import build_env, showtime_home # noqa: E402++FAST = "--fast" in sys.argv+ENV = build_env(showtime_home())+NODE = shutil.which("node", path=ENV.get("PATH"))+TMP = Path(tempfile.mkdtemp(prefix="st-xray-live-"))+++def showtime(*args, check=True, timeout=300):+ cp = subprocess.run([sys.executable, str(LAUNCHER)] + [str(a) for a in args], env=ENV,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",+ errors="replace", timeout=timeout)+ if check and cp.returncode != 0:+ raise AssertionError("showtime %s failed (rc=%d):\n%s\n%s" % (+ " ".join(map(str, args)), cp.returncode, cp.stdout[-3000:], cp.stderr[-3000:]))+ return cp+++_SAMPLE = {}+++def sample(layout=None):+ """A fresh `showtime new xray` project (the typewriter sample), exported once as a live page."""+ key = layout or "through"+ if key not in _SAMPLE:+ proj = TMP / ("proj-" + key)+ showtime("new", "xray", proj, *(["--layout", layout] if layout else []))+ out = TMP / ("live-" + key + ".html")+ cp = showtime("export", "html", proj, "--interactive", "-o", out, "--json")+ _SAMPLE[key] = (proj, out, json.loads(cp.stdout))+ return _SAMPLE[key]+++class TestStatic(unittest.TestCase):+ def test_parts_exist(self):+ for n in ("pre.js", "live.js", "live.css", "embed.js"):+ self.assertTrue((LIVE / n).is_file(), n)+ self.assertTrue((SKILL / "scripts" / "lib" / "export" / "xray.mjs").is_file())++ def test_pre_runs_the_stage_in_render_mode_offline(self):+ pre = (LIVE / "pre.js").read_text(encoding="utf-8")+ self.assertIn("__ST_RENDER__", pre, "the film's own clock: render mode, the same frames as the MP4")+ self.assertRegex(pre, r"W\.fetch\s*=\s*function", "the page's fetch() answers from the packed data")+ self.assertIn("status: 404", pre, "a file that was not packed is a 404, never a network request")+ self.assertIn("__XL_REAL__", pre, "the controls keep the real clocks")++ def test_live_reaches_no_network_and_no_wall_clock(self):+ js = (LIVE / "live.js").read_text(encoding="utf-8")+ code = re.sub(r"/\*[\s\S]*?\*/|//[^\n]*", "", js)+ for bad in ("XMLHttpRequest", "fetch(", "sendBeacon", "WebSocket", "Math.random", "Date.now", "import("):+ self.assertNotIn(bad, code, bad)+ self.assertIn("X.mech.draw = function", code, "the mechanism's own drawing, wrapped, never redrawn here")+ self.assertIn("ST.seek(", code, "frames are drawn by the stage, on the scrubbed time")++ def test_links_are_plain_anchors(self):+ pre = (LIVE / "pre.js").read_text(encoding="utf-8")+ self.assertIn("hit-?(\\d+)", pre, "#hit4: a plain anchor, the form framed hosts pass on")+ self.assertIn("k === 'event'", pre, "the older #event=4 still opens")+ js = (LIVE / "live.js").read_text(encoding="utf-8")+ self.assertNotRegex(re.sub(r"/\*[\s\S]*?\*/|//[^\n]*", "", js), r"'#t='|'&theme='", "links are written as plain tokens")++ def test_embed_element(self):+ js = (LIVE / "embed.js").read_text(encoding="utf-8")+ self.assertIn("customElements.define('showtime-xray'", js)+ self.assertIn("'#embed'", js, "the frame opens on the page's embed layout")+ self.assertIn("showtime-xray:size", js)++ def test_marker_hidden(self):+ css = (LIVE / "live.css").read_text(encoding="utf-8")+ self.assertRegex(css, r"#xmark\s*\{\s*display:\s*none\s*!important", "the sync marker is for verify, not viewers")++ def test_export_routes_xray_projects(self):+ src = (SKILL / "scripts" / "export.mjs").read_text(encoding="utf-8")+ self.assertIn("isXrayProject(proj.dir, cfg)", src)+ self.assertRegex(src, r"if \(xrayLive\) return exportXrayLive\(")+ self.assertLess(src.index("if (xrayLive) return exportXrayLive("), src.index("if (a.interactive) return exportFigure("))+++class TestCli(unittest.TestCase):+ def test_sample_exports_one_offline_file(self):+ proj, out, rep = sample()+ self.assertTrue(out.is_file())+ self.assertEqual(rep["kind"], "xray-live")+ self.assertTrue(rep["tracked"], "the sample clip is tracked: see-through and split")+ self.assertLess(rep["bytes"], 16 * 1000 * 1000)+ html = out.read_text(encoding="utf-8")+ self.assertRegex(html, r'<meta http-equiv="Content-Security-Policy" content="default-src \'none\'')+ self.assertEqual(html.count("data-xl-media="), 1, "the clip is packed once")+ self.assertIn("data-xl-mirror=", html, "the radiograph copy of the clip is a canvas mirror, never a second file")+ self.assertIn('id="xmark"', html, "the marker element stays (xray.js sets it); live.css hides it")+ self.assertNotRegex(html, r'(?<![\w-])src="(?!data:|blob:)[^"]+"', "no file left to fetch")+ cfg = json.loads(re.search(r'<script type="application/json" id="xl-cfg">(.*?)</script>', html, re.S).group(1))+ self.assertIn("xray.json", cfg["data"])+ self.assertIn("xray", json.loads(cfg["data"]["showtime.json"]), "the page's own settings travel (skip, text, credit)")+ self.assertNotIn("audio", json.loads(cfg["data"]["showtime.json"]), "no local paths")++ def test_default_name(self):+ proj, _, _ = sample()+ jobs = TMP / "jobs"+ jobs.mkdir(exist_ok=True)+ cp = showtime("export", "html", proj, "--interactive", "--out-dir", jobs, "--json")+ self.assertTrue(json.loads(cp.stdout)["output"].endswith("-live.html"))++ def test_diagram_layout(self):+ _, out, rep = sample("diagram")+ self.assertEqual(rep["layout"], "diagram")+ self.assertIn("XRAY_DIAGRAM", out.read_text(encoding="utf-8"))+++DRIVER = r"""+import path from 'node:path';+import { pathToFileURL } from 'node:url';+const [SKILL, FILE] = process.argv.slice(2);+const { openBrowser } = await import(pathToFileURL(path.join(SKILL, 'scripts', 'lib', 'stagehost.mjs')).href);+const { browser } = await openBrowser({ headless: true, args: ['--autoplay-policy=no-user-gesture-required'] });+const out = { logs: [], net: [], jumps: [] };+try {+ const ctx = await browser.newContext({ viewport: { width: 390, height: 844 } });+ const pg = await ctx.newPage();+ pg.on('console', (m) => { if (m.type() === 'error' || m.type() === 'warning') out.logs.push(m.text()); });+ pg.on('pageerror', (e) => out.logs.push(String(e.message || e)));+ pg.on('request', (r) => { if (!/^(file|blob|data):/.test(r.url())) out.net.push(r.url()); });+ await pg.goto(pathToFileURL(FILE).href);+ const st = await pg.evaluate('xrayLive.ready');+ out.start = { frame: st.frame, kept: st.kept, parts: st.parts };+ for (const i of [st.kept[0], st.kept[Math.floor(st.kept.length / 2)], st.kept[st.kept.length - 1]]) {+ const s = await pg.evaluate(`xrayLive.jump(${i})`);+ out.jumps.push({ i, frame: s.frame, evframe: s.events.find((e) => e.i === i).frame, cur: s.kit && s.kit.cur, s: s.kit && s.kit.s });+ }+ // what a viewer does: a key, a click on a tick, the pointer on a part label, space+ await pg.evaluate(`xrayLive.jump(${st.kept[0]})`);+ await pg.keyboard.press('ArrowRight');+ await pg.waitForFunction(() => !window.xrayLive.state().playing && window.xrayLive.state().frame > 0, null, { polling: 100 });+ await pg.waitForTimeout(300);+ out.key = await pg.evaluate(`({ frame: xrayLive.state().frame, want: xrayLive.state().events.find((e) => e.i === ${st.kept[1]}).frame })`);+ const nth = Math.floor(st.kept.length / 2);+ await pg.locator('.xl-tick:not(.is-skip)').nth(nth).click();+ await pg.waitForTimeout(400);+ out.tick = await pg.evaluate(`({ frame: xrayLive.state().frame, want: xrayLive.state().events.find((e) => e.i === ${st.kept[nth]}).frame })`);+ const lab = await pg.evaluate('xrayLive.seek(2.5).then((s) => s.labels[0] || null)');+ if (lab) {+ const box = await pg.evaluate(() => { const r = document.querySelector('.xl-scale').getBoundingClientRect(); return { x: r.left, y: r.top, k: r.width / document.getElementById('stage').offsetWidth }; });+ await pg.mouse.move(box.x + (lab.x + lab.w / 2) * box.k, box.y + (lab.y + lab.h / 2) * box.k);+ await pg.waitForTimeout(300);+ out.hover = { want: lab.name.toLowerCase(), hot: await pg.evaluate('xrayLive.state().hot') };+ await pg.mouse.move(1, 1);+ }+ await pg.locator('.xl-play').focus(); await pg.locator('.xl-play').blur();+ await pg.keyboard.press(' ');+ await pg.waitForTimeout(500);+ out.spacePlays = await pg.evaluate('xrayLive.state().playing');+ await pg.evaluate('xrayLive.pause()');+ out.marker = await pg.evaluate("getComputedStyle(document.getElementById('xmark')).display");+ out.scrollW = await pg.evaluate('document.documentElement.scrollWidth');+ const mid = st.kept[Math.floor(st.kept.length / 2)];+ await pg.evaluate(`xrayLive.jump(${mid})`);+ out.slow = await pg.evaluate('xrayLive.slow().then(() => ({ rate: xrayLive.state().rate, slow: xrayLive.state().slow }))');+ // function predicates polled on a timer: the page's CSP has no eval, and the stage's render mode queues rAF+ await pg.waitForFunction(() => !window.xrayLive.state().slow && !window.xrayLive.state().playing, null, { timeout: 20000, polling: 100 });+ out.slowEnd = await pg.evaluate('xrayLive.state().frame');+ out.midFrame = await pg.evaluate(`xrayLive.state().events.find((e) => e.i === ${mid}).frame`);+ await pg.evaluate('xrayLive.theme("light")');+ await pg.waitForTimeout(300);+ await pg.waitForFunction(() => window.xrayLive && window.xrayLive.state().ready && window.xrayLive.state().theme === 'light', null, { timeout: 20000, polling: 100 });+ out.light = await pg.evaluate('({ frame: xrayLive.state().frame, theme: xrayLive.state().theme })');+} catch (e) { out.error = String(e && e.stack || e); }+await browser.close();+console.log(JSON.stringify(out));+"""+++TOUCH = r"""+import fs from 'node:fs';+import path from 'node:path';+import { pathToFileURL } from 'node:url';+const [SKILL, FILE, HOST] = process.argv.slice(2);+const { openBrowser } = await import(pathToFileURL(path.join(SKILL, 'scripts', 'lib', 'stagehost.mjs')).href);+const { browser } = await openBrowser({ headless: true, args: ['--autoplay-policy=no-user-gesture-required', '--allow-file-access-from-files'] });+const out = { logs: [] };+const until = (pg, fn, ms = 5000) => pg.waitForFunction(fn, null, { timeout: ms, polling: 30 }).then(() => true, () => false);+try {+ const ctx = await browser.newContext({ viewport: { width: 1280, height: 860 } });+ const pg = await ctx.newPage();+ pg.on('console', (m) => { if (m.type() === 'error' || m.type() === 'warning') out.logs.push(m.text()); });+ pg.on('pageerror', (e) => out.logs.push(String(e.message || e)));+ await pg.goto(pathToFileURL(FILE).href + '#hit4');+ const st = await pg.evaluate('xrayLive.ready');+ out.hit4 = { frame: st.frame, want: st.events.find((e) => e.i === st.kept[3]).frame };+ out.pads = st.pads;+ out.made = await pg.evaluate("[...document.querySelectorAll('.xl-made textarea')].map((t) => t.value)");+ const pad = st.pads.keys.find((k) => k.key.length === 1 && /[a-z]/.test(k.key));+ await pg.mouse.click(2, 2);+ await pg.keyboard.press(pad.key);+ out.keyStrike = await until(pg, () => window.xrayLive.state().strike);+ await until(pg, () => !window.xrayLive.state().strike && !window.xrayLive.state().playing, 10000);+ out.afterKey = await pg.evaluate('xrayLive.state().frame');+ out.padFrames = await pg.evaluate(`xrayLive.state().events.filter((e) => e.ch && e.ch.toLowerCase() === ${JSON.stringify(pad.key)}).map((e) => e.frame)`);+ const vb = await pg.locator('.xl-view').boundingBox();+ const f0 = await pg.evaluate('xrayLive.state().frame');+ await pg.mouse.move(vb.x + vb.width * 0.2, vb.y + vb.height * 0.5);+ await pg.mouse.down(); await pg.mouse.move(vb.x + vb.width * 0.5, vb.y + vb.height * 0.52, { steps: 8 }); await pg.mouse.up();+ await pg.waitForTimeout(500);+ out.drag = { f0, f1: await pg.evaluate('xrayLive.state().frame'), playing: await pg.evaluate('xrayLive.state().playing') };+ await pg.mouse.click(vb.x + 4, vb.y + vb.height - 4);+ out.clickStrike = await until(pg, () => window.xrayLive.state().strike);+ await until(pg, () => !window.xrayLive.state().strike && !window.xrayLive.state().playing, 10000);+ await pg.keyboard.press('Shift+S');+ out.shiftS = await until(pg, () => window.xrayLive.state().slow);+ await pg.evaluate('xrayLive.pause()');+ await pg.evaluate(`xrayLive.jump(${st.kept[2]})`);+ out.link = await pg.evaluate('xrayLive.link()');+ // framed with #embed: the host hears the page's height+ const host = await ctx.newPage();+ host.on('pageerror', (e) => out.logs.push('host: ' + String(e.message || e)));+ await host.goto(pathToFileURL(HOST).href);+ out.embedHeight = await host.waitForFunction(() => window.__h > 0 && window.__h, null, { timeout: 30000, polling: 100 }).then((h) => h.jsonValue(), () => 0);+} catch (e) { out.error = String(e && e.stack || e); }+await browser.close();+console.log(JSON.stringify(out));+"""++[email protected](FAST or not NODE, "--fast: no browser")+class TestTouch(unittest.TestCase):+ def test_play_it(self):+ proj, _, _ = sample()+ out = TMP / "live-touch.html"+ rep = json.loads(showtime("export", "html", proj, "--interactive", "-o", out, "--json",+ "--prompt", "X-ray this clip of my typewriter.").stdout)+ self.assertEqual((rep.get("made") or {}).get("prompt"), "X-ray this clip of my typewriter.")+ host = TMP / "host.html"+ host.write_text('<!doctype html><meta charset="utf-8"><body style="margin:0;width:900px">'+ '<iframe id="f" style="width:900px;height:400px;border:0" src="%s#embed_hit2"></iframe>'+ '<script>addEventListener("message",function(e){if(e.data&&e.data.type==="showtime-xray:size")window.__h=e.data.height;});</script>'+ % out.as_uri(), encoding="utf-8")+ drv = TMP / "touch.mjs"+ drv.write_text(TOUCH, encoding="utf-8")+ cp = subprocess.run([NODE, str(drv), str(SKILL), str(out), str(host)], env=ENV, cwd=str(TMP), stdout=subprocess.PIPE,+ stderr=subprocess.PIPE, encoding="utf-8", errors="replace", timeout=300)+ self.assertEqual(cp.returncode, 0, cp.stderr[-2000:])+ r = json.loads(cp.stdout.strip().splitlines()[-1])+ self.assertNotIn("error", r, r.get("error"))+ self.assertEqual(r["logs"], [], "no console error")+ self.assertEqual(r["hit4"]["frame"], r["hit4"]["want"], "#hit4 opens on the 4th event's frame")+ self.assertEqual(r["pads"]["by"], "ch", "a typewriter's pads are its keys")+ self.assertEqual(r["pads"]["verb"], "Type on it")+ self.assertEqual(r["made"][0], "X-ray this clip of my typewriter.", "the line that made it is on the page")+ self.assertTrue(r["keyStrike"], "a plain letter plays that key's next keystroke")+ self.assertTrue(any(0 <= r["afterKey"] - f <= 10 for f in r["padFrames"]), "it rests just after that key's keystroke: %s" % r)+ self.assertNotEqual(r["drag"]["f0"], r["drag"]["f1"], "dragging the picture scrubs")+ self.assertFalse(r["drag"]["playing"])+ self.assertTrue(r["clickStrike"], "a click on the picture plays the next event")+ self.assertTrue(r["shiftS"], "Shift+S is 0.25x")+ self.assertTrue(r["link"].endswith("#hit3"), "a link names the hit: %s" % r["link"])+ self.assertGreater(r["embedHeight"], 200, "framed with #embed, the page reports its height")++[email protected](FAST or not NODE, "--fast: no browser")+class TestBrowser(unittest.TestCase):+ def test_jump_lands_on_the_peak(self):+ _, out, _ = sample()+ drv = TMP / "drv.mjs"+ drv.write_text(DRIVER, encoding="utf-8")+ cp = subprocess.run([NODE, str(drv), str(SKILL), str(out)], env=ENV, cwd=str(TMP), stdout=subprocess.PIPE,+ stderr=subprocess.PIPE, encoding="utf-8", errors="replace", timeout=300)+ self.assertEqual(cp.returncode, 0, cp.stderr[-2000:])+ r = json.loads(cp.stdout.strip().splitlines()[-1])+ self.assertNotIn("error", r, r.get("error"))+ self.assertEqual(r["logs"], [], "no console error")+ self.assertEqual(r["net"], [], "no request off the file")+ self.assertGreaterEqual(len(r["start"]["kept"]), 3)+ for j in r["jumps"]:+ self.assertEqual(j["frame"], j["evframe"], j)+ self.assertEqual(j["cur"], j["i"], "the kit's acting event is the one jumped to: %s" % j)+ self.assertEqual(j["s"], 1, "the strike peaks on its event's frame: %s" % j)+ self.assertEqual(r["key"]["frame"], r["key"]["want"], "→ lands on the next event's frame")+ self.assertEqual(r["tick"]["frame"], r["tick"]["want"], "a click on a tick lands on its event's frame")+ self.assertIn("hover", r, "the sample draws part labels")+ self.assertEqual(r["hover"]["hot"], r["hover"]["want"], "the pointer on a part label lights it")+ self.assertTrue(r["spacePlays"], "space plays")+ self.assertEqual(r["marker"], "none")+ self.assertLessEqual(r["scrollW"], 390, "no sideways scroll on a phone")+ self.assertEqual(r["slow"], {"rate": 0.25, "slow": True})+ self.assertEqual(r["slowEnd"], r["midFrame"], "0.25x ends on the event's frame")+ self.assertEqual(r["light"]["theme"], "light")+ self.assertEqual(r["light"]["frame"], r["midFrame"], "the theme switches on the same frame")+++if __name__ == "__main__":+ argv = [a for a in sys.argv if a != "--fast"]+ try:+ unittest.main(argv=argv, verbosity=2 if "-v" in argv else 1)+ finally:+ shutil.rmtree(TMP, ignore_errors=True)diff --git a/skills/showtime/tests/test_xray_made.py b/skills/showtime/tests/test_xray_made.pynew file mode 100644index 0000000..5c1c963--- /dev/null+++ b/skills/showtime/tests/test_xray_made.py@@ -0,0 +1,204 @@+#!/usr/bin/env python3+"""The line that made an X-ray: `showtime new xray` records xray.made, the live export embeds a prompt and+placeholder commands, and the site's X-ray page builds the live buttons, prompts and embed snippets.++ * new: xray.made = {mechanism, layout, theme} and nothing else (no paths, job names or regions)+ * export: --prompt (else showtime.json xray.made.prompt, else none) and commands with placeholders in the page's+ config; no absolute path anywhere in them; the report states what was embedded; FIGURE_CSP has no frame-ancestors+ * site: build_xray with a live entry writes "Play it live", the prompt with its copy mechanism, the Embed snippet++Stdlib only (the site part needs markdown-it-py). usage: python tests/test_xray_made.py [-v]+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import importlib.util+import json+import re+import shutil+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+REPO = SKILL.parents[1]+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+sys.path.insert(0, str(SKILL / "lib"))++from st.launcher import build_env, showtime_home # noqa: E402++ENV = build_env(showtime_home())+TMP = Path(tempfile.mkdtemp(prefix="st-xray-made-"))+++def showtime(*args, check=True, timeout=300):+ cp = subprocess.run([sys.executable, str(LAUNCHER)] + [str(a) for a in args], env=ENV,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",+ errors="replace", timeout=timeout)+ if check and cp.returncode != 0:+ raise AssertionError("showtime %s failed (rc=%d):\n%s\n%s" % (+ " ".join(map(str, args)), cp.returncode, cp.stdout[-3000:], cp.stderr[-3000:]))+ return cp+++def live_config(html: str) -> dict:+ m = re.search(r'<script type="application/json" id="xl-cfg">(.*?)</script>', html, re.S)+ assert m, "the page has no xl-cfg block"+ return json.loads(m.group(1))["live"]+++class TestMade(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.proj = TMP / "proj-split"+ showtime("new", "xray", cls.proj, "--layout", "split", "--theme", "light")+ cls.cfg = json.loads((cls.proj / "showtime.json").read_text(encoding="utf-8"))++ def test_new_records_three_words(self):+ self.assertEqual(self.cfg["xray"]["made"], {"mechanism": "typewriter", "layout": "split", "theme": "light"})++ def test_mechanism_is_recorded(self):+ proj = TMP / "proj-box"+ showtime("new", "xray", proj, "--mechanism", "musicbox")+ made = json.loads((proj / "showtime.json").read_text(encoding="utf-8"))["xray"]["made"]+ self.assertEqual(made, {"mechanism": "musicbox", "layout": "through", "theme": "dark"})+ self.assertNotIn(str(TMP), json.dumps(made))++ def test_export_embeds_the_prompt_and_placeholder_commands(self):+ out = TMP / "live-prompt.html"+ cp = showtime("export", "html", self.proj, "--interactive", "-o", out, "--json",+ "--prompt", "X-ray this clip of my typewriter.")+ report = json.loads(cp.stdout)+ self.assertEqual(report["made"]["prompt"], "X-ray this clip of my typewriter.")+ made = live_config(out.read_text(encoding="utf-8"))["made"]+ self.assertEqual(made["prompt"], "X-ray this clip of my typewriter.")+ self.assertEqual(made["commands"], [+ "showtime xray analyze clip.mp4 --job xray-typewriter --track", # the sample is tracked+ "showtime new xray xray-typewriter/project --mechanism typewriter --layout split --theme light",+ "showtime render xray-typewriter/project --job xray-typewriter",+ "showtime xray verify xray-typewriter"])+ for line in made["commands"]:+ self.assertNotIn(str(TMP), line)+ self.assertNotRegex(line, r"(^|\s)(/|[A-Za-z]:\\)")++ def test_mechanism_comes_from_the_project_when_made_is_absent(self):+ proj = TMP / "proj-nomade"+ showtime("new", "xray", proj, "--mechanism", "musicbox")+ p = proj / "showtime.json"+ cfg = json.loads(p.read_text(encoding="utf-8"))+ del cfg["xray"]["made"]+ p.write_text(json.dumps(cfg), encoding="utf-8")+ out = TMP / "live-nomade.html"+ report = json.loads(showtime("export", "html", proj, "--interactive", "-o", out, "--json").stdout)+ made = live_config(out.read_text(encoding="utf-8"))["made"]+ self.assertIn("--mechanism musicbox", made["commands"][1])+ self.assertIn("xray-musicbox/project", made["commands"][1])+ self.assertFalse(made["guessed"])+ self.assertFalse([w for w in report["warnings"] if "names no mechanism" in w])+ mj = proj / "mechanism.js" # a name that cannot be read: the typewriter is a guess, and the report says so+ mj.write_text(re.sub(r"name:\s*'[^']*'", "name: 'No Name'", mj.read_text(encoding="utf-8"), count=1), encoding="utf-8")+ out2 = TMP / "live-guess.html"+ report = json.loads(showtime("export", "html", proj, "--interactive", "-o", out2, "--json").stdout)+ self.assertTrue(live_config(out2.read_text(encoding="utf-8"))["made"]["guessed"])+ self.assertTrue([w for w in report["warnings"] if "names no mechanism" in w])++ def test_no_prompt_means_none_and_the_report_says_so(self):+ out = TMP / "live-none.html"+ cp = showtime("export", "html", self.proj, "--interactive", "-o", out)+ self.assertIn("no prompt is embedded", cp.stdout + cp.stderr)+ self.assertEqual(live_config(out.read_text(encoding="utf-8"))["made"]["prompt"], "")++ def test_prompt_from_showtime_json_and_never_from_job_json(self):+ proj = TMP / "proj-cfgprompt"+ shutil.copytree(self.proj, proj)+ p = proj / "showtime.json"+ cfg = json.loads(p.read_text(encoding="utf-8"))+ cfg["xray"]["made"]["prompt"] = "From the project file."+ p.write_text(json.dumps(cfg), encoding="utf-8")+ (TMP / "job.json").write_text(json.dumps({"request": "SECRET job request"}), encoding="utf-8")+ out = TMP / "live-cfg.html"+ showtime("export", "html", proj, "--interactive", "-o", out)+ html = out.read_text(encoding="utf-8")+ self.assertEqual(live_config(html)["made"]["prompt"], "From the project file.")+ self.assertNotIn("SECRET job request", html)++ def test_framing_is_not_blocked(self):+ fig = (SKILL / "scripts" / "lib" / "export" / "figure.mjs").read_text(encoding="utf-8")+ csp = re.search(r"FIGURE_CSP = \"([^\"]*)\"", fig).group(1)+ self.assertNotIn("frame-ancestors", csp)+ self.assertNotIn("frame-ancestors", (SKILL / "scripts" / "lib" / "export" / "xray.mjs").read_text(encoding="utf-8"))+++try:+ import markdown_it # noqa: F401+ _spec = importlib.util.spec_from_file_location("site_build", REPO / "site" / "build.py")+ build = importlib.util.module_from_spec(_spec)+ _spec.loader.exec_module(build)+except ImportError: # pragma: no cover+ build = None++[email protected](build is None, "needs markdown-it-py (showtime's venv has it)")+class TestSite(unittest.TestCase):+ def test_xray_page_has_live_prompt_and_embed(self):+ out, media = TMP / "site", TMP / "media" / "examples" / "_xray"+ media.mkdir(parents=True)+ hero = build.XRAY["hero"]+ entry = next(e for e in build.XRAY["entries"] if e["id"] == hero)+ for name in (entry["video"], entry["poster"], entry["live"]):+ (media / name).write_bytes(b"x")+ site = build.Site(out, [TMP / "media"], "owner/repo", None, "main")+ build.build_xray(site)+ text = (out / build.XRAY_PAGE).read_text(encoding="utf-8")+ self.assertIn('class="btn primary xlive xlive-hero"', text)+ self.assertIn('data-live="../media/xray/%s"' % entry["live"], text)+ self.assertEqual(text.count("Play it live"), 1, "only entries whose live page is in this build get a button")+ self.assertIn('<p class="ask">“%s”</p>' % build.esc(entry["prompt"]), text)+ self.assertIn("Make your own with one line", text)+ self.assertIn("<showtime-xray src="%smedia/xray/%s"" % (build.site_url("owner/repo"), entry["live"]), text)+ self.assertIn("%smedia/xray/%s#embed" % (build.site_url("owner/repo"), entry["live"]), text)+ self.assertTrue((out / "media" / "xray" / entry["live"]).is_file())++ def test_credit_links_are_http_only(self):+ e = {"credit": "Clip", "license": "CC BY", "license_url": "javascript:alert(1)", "credit_url": " data:text/html,x", "author": "showtime"}+ text = build.xray_credit(e)+ self.assertNotIn("javascript:", text)+ self.assertNotIn("data:", text)+ self.assertNotIn("<a href", text.split("posted by")[0])+ self.assertIn('<a href="HTTPS://example.org/l">', build.xray_credit(dict(e, license_url="HTTPS://example.org/l")))++ def test_credit_links_the_clip_and_states_the_film_license(self):+ e = {"credit": "Clip: A & B", "license": "CC BY-SA 3.0", "license_url": "https://example.org/l",+ "credit_url": "https://commons.wikimedia.org/wiki/File:A_<b>.webm", "film_license": "This film is under the same license (share-alike).",+ "author": "showtime"}+ text = build.xray_credit(e)+ self.assertIn('<a href="https://commons.wikimedia.org/wiki/File:A_<b>.webm">Source</a>', text)+ self.assertIn("This film is under the same license (share-alike).", text)+ plain = build.xray_credit({"credit": "Clip: C", "license": "CC BY 3.0", "author": "showtime"})+ self.assertNotIn("Source", plain)++ def test_every_clip_is_linked_and_share_alike_ones_state_the_film_license(self):+ for e in build.XRAY["entries"]:+ self.assertTrue(e["credit_url"].startswith("https://commons.wikimedia.org/wiki/File:"), e["id"])+ self.assertEqual("BY-SA" in e["license"], bool(e.get("film_license")), e["id"])++ def test_embed_frame_has_room_for_the_controls(self):+ src = (REPO / "site" / "build.py").read_text(encoding="utf-8")+ self.assertIn('width="960" height="860"', src)+ self.assertIn('width="420" height="1067"', src)+ self.assertIn('allow="autoplay; clipboard-write"', src)++ def test_every_entry_names_its_live_page_and_one_prompt(self):+ for e in build.XRAY["entries"]:+ self.assertEqual(e["live"], e["id"] + "-live.html")+ self.assertTrue(e["prompt"].strip().endswith((".", "?")), e["id"])+ if e["aspect"] == "9:16":+ self.assertTrue(e["id"].endswith("9x16"), e["id"])+++if __name__ == "__main__":+ unittest.main(argv=[sys.argv[0]] + [a for a in sys.argv[1:] if a != "--fast"])diff --git a/skills/showtime/tests/test_xray_notes.py b/skills/showtime/tests/test_xray_notes.pynew file mode 100644index 0000000..dd2be47--- /dev/null+++ b/skills/showtime/tests/test_xray_notes.py@@ -0,0 +1,288 @@+#!/usr/bin/env python3+"""`showtime xray notes` (lib/st/xray_music.py): the numpy port of basic-pitch's note creation against golden notes made+with the original package (two 4 s excerpts in tests/fixtures/xray_notes), synthetic tones at known pitches and times,+the tempo map on jittered onsets, quantizing, key, guitar strings and frets, the MIDI and MusicXML writers read back,+the missing-onnxruntime error and the command end to end on a tiny film.++usage: python tests/test_xray_notes.py [-v]+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import json+import struct+import sys+import tempfile+import unittest+import xml.dom.minidom+from pathlib import Path+from unittest import mock++import numpy as np++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+FIX = TESTS_DIR / "fixtures" / "xray_notes"+sys.path.insert(0, str(SKILL / "lib"))++from st import ff, xray_music as xm # noqa: E402+from st.common import ShowtimeError # noqa: E402++SR = xm.SR+++def tone(midi, dur=0.9):+ f = 440.0 * 2 ** ((midi - 69) / 12.0)+ t = np.arange(int(dur * SR)) / SR+ env = np.minimum(1.0, t / 0.005) * np.exp(-t / 0.5)+ return sum(a * np.sin(2 * np.pi * f * k * t) for k, a in enumerate([1, .5, .3, .2], 1)) * env * 0.25+++def synth(spec, total):+ x = np.zeros(int(total * SR), np.float32)+ for m, on in spec:+ y = tone(m)+ a = int(on * SR)+ x[a:a + len(y)] += y+ return x+++def note(t, end, pitch, vel=0.7, conf=0.9):+ return {"t": t, "end": end, "pitch": pitch, "velocity": vel, "confidence": conf}+++class Golden(unittest.TestCase):+ def check(self, name):+ from st.audio import wav+ gold = json.loads((FIX / ("%s-excerpt.golden.json" % name)).read_text())["notes"]+ x = wav.load(FIX / ("%s-excerpt.ogg" % name), sr=SR, mono=True)+ notes = xm.detect_notes(np.asarray(x, np.float32))+ self.assertLessEqual(abs(len(notes) - len(gold)), max(1, round(0.02 * len(gold))), (len(notes), len(gold)))+ used = set()+ for g in gold:+ hit = [i for i, n in enumerate(notes) if i not in used and n["pitch"] == g["pitch"] and abs(n["t"] - g["t"]) <= xm.FRAME_S + 1e-6]+ self.assertTrue(hit, "no note for %r" % g)+ used.add(hit[0])++ def test_guitar_excerpt(self):+ self.check("fp4")++ def test_piano_excerpt(self):+ self.check("piano")+++class Synthetic(unittest.TestCase):+ def test_pitches_and_onsets_come_back(self):+ spec = [(60, 0.5), (64, 1.5), (72, 2.5), (55, 3.5), (62, 4.5), (69, 5.5)]+ notes = xm.detect_notes(synth(spec, 7.0))+ for m, on in spec:+ self.assertTrue([n for n in notes if n["pitch"] == m and abs(n["t"] - on) <= 0.035], "missing %d at %g in %r" % (m, on, notes))+ strong = [n for n in notes if n["velocity"] > 0.5]+ self.assertEqual(sorted(n["pitch"] for n in strong), sorted(m for m, _ in spec))++ def test_silence_has_no_notes(self):+ self.assertEqual(xm.detect_notes(np.zeros(SR * 2, np.float32)), [])++ def test_a_few_milliseconds_of_audio_is_an_error(self):+ with self.assertRaises(ShowtimeError) as cm:+ xm.detect_notes(np.zeros(200, np.float32))+ self.assertIn("no usable audio", str(cm.exception))++ def test_missing_onnxruntime(self):+ with mock.patch.dict(sys.modules, {"onnxruntime": None}):+ with self.assertRaises(ShowtimeError) as cm:+ xm.detect_notes(np.zeros(SR, np.float32))+ self.assertIn("onnxruntime", str(cm.exception))+ self.assertTrue(cm.exception.hint)+++class Tempo(unittest.TestCase):+ def test_smooth_on_jittered_onsets(self):+ rng = np.random.RandomState(3)+ true = np.arange(40) * 0.5 # 120 bpm, eighth notes+ on = true + rng.uniform(-0.015, 0.015, len(true))+ tm = xm.tempo_map(on)+ self.assertAlmostEqual(tm["bpm"], 120.0, delta=4.0)+ b = np.array(xm.beat_of(true, tm["map"]))+ self.assertLess(np.max(np.abs(b - true * 2.0)), 0.06) # beats are quarter notes: 2 per second+ mt = np.array([k[0] for k in tm["map"]])+ mb = np.array([k[1] for k in tm["map"]])+ slope = np.diff(mb) / np.diff(mt)+ self.assertTrue(np.all(slope > 0))+ self.assertLess(np.max(np.abs(np.diff(slope[1:-1]))), 0.35) # beats per second never jumps++ def test_follows_a_slowdown(self):+ t = np.cumsum(np.linspace(0.4, 0.6, 40))+ tm = xm.tempo_map(t)+ s = np.diff([k[1] for k in tm["map"]]) / np.diff([k[0] for k in tm["map"]])+ self.assertGreater(s[2], s[-3])++ def test_beat_time_roundtrip(self):+ tm = xm.tempo_map(np.arange(20) * 0.5)+ for t in (0.1, 2.3, 9.7, 12.0):+ self.assertAlmostEqual(xm.time_of(xm.beat_of(t, tm["map"]), tm["map"]), t, places=6)++ def test_quantize_to_eighths(self):+ tmap = [[0.0, 0.0], [10.0, 20.0]] # 2 beats per second+ q = xm.quantize([note(0.01, 0.52, 60), note(0.74, 1.76, 62), note(2.0, 2.05, 64)], tmap)+ self.assertEqual([(r["beat"], r["dur_beats"]) for r in q], [(0.0, 1.0), (1.5, 2.0), (4.0, 0.5)])+++class Harmony(unittest.TestCase):+ def test_c_major_scale(self):+ sc = [60, 62, 64, 65, 67, 69, 71, 72]+ notes = [note(i * 0.5, i * 0.5 + 0.5, p) for i, p in enumerate(sc)]+ notes[-1]["end"] = 6.0+ self.assertEqual(xm.detect_key(notes), "C major")++ def test_a_minor(self):+ notes = [note(i * 0.5, i * 0.5 + 0.5, p) for i, p in enumerate([57, 59, 60, 62, 64, 65, 68, 69])]+ notes[0]["end"] = 3.0+ notes[-1]["end"] = 6.0+ self.assertEqual(xm.detect_key(notes), "A minor")++ def test_chords(self):+ tmap = [[0.0, 0.0], [10.0, 10.0]]+ ns = [note(0, 2, 48), note(0, 2, 52), note(0, 2, 55), note(2, 4, 43), note(2, 4, 47), note(2, 4, 50), note(2, 4, 53)]+ ch = xm.chords(ns, tmap)+ self.assertEqual([c["name"] for c in ch], ["C", "G7"])+ self.assertEqual((ch[0]["t"], ch[0]["end"]), (0.0, 2.0))++ def test_voices(self):+ ns = [note(0.0, 1, 72, 0.8), note(0.01, 1, 76, 0.5), note(0.0, 1, 48, 0.6), note(1.0, 2, 50, 0.6), note(1.0, 2, 67, 0.7, 0.2),+ note(2.0, 3, 69, 0.9)]+ v = xm.voices(ns)+ self.assertEqual(v["melody"], [0, 5]) # loudest at each onset; the 0.2-confidence note is no melody+ self.assertEqual(v["bass"], [2, 3])+++class Guitar(unittest.TestCase):+ def test_mapping(self):+ rows = xm.guitar_map([note(0, 0.5, 64), note(1, 1.5, 45), note(2, 2.5, 60), note(3, 3.5, 40), note(4, 4.5, 30)])+ by = {r["i"]: r for r in rows}+ self.assertEqual((by[0]["string"], by[0]["fret"], by[0]["finger"]), (1, 0, "a"))+ self.assertEqual((by[1]["string"], by[1]["fret"], by[1]["finger"]), (5, 0, "p"))+ self.assertEqual((by[2]["string"], by[2]["fret"]), (2, 1))+ self.assertEqual((by[3]["string"], by[3]["fret"]), (6, 0))+ self.assertNotIn(4, by) # below the neck: left out++ def test_ringing_string_is_avoided(self):+ rows = xm.guitar_map([note(0, 2.0, 59), note(0.5, 1.0, 59)])+ self.assertNotEqual(rows[0]["string"], rows[1]["string"])+++def read_midi(data: bytes):+ """A tiny reader: (ppq, tempo_us, [(start_tick, end_tick, pitch, velocity)])."""+ assert data[:4] == b"MThd"+ ln, fmt, ntrk, ppq = struct.unpack(">IHHH", data[4:14])+ assert (ln, fmt, ntrk) == (6, 0, 1)+ assert data[14:18] == b"MTrk"+ size = struct.unpack(">I", data[18:22])[0]+ trk = data[22:22 + size]+ assert len(trk) == size+ i, tick, tempo, on, out = 0, 0, None, {}, []+ while i < len(trk):+ d = 0+ while True:+ b = trk[i]+ i += 1+ d = (d << 7) | (b & 0x7F)+ if not b & 0x80:+ break+ tick += d+ st = trk[i]+ if st == 0xFF:+ typ, n = trk[i + 1], trk[i + 2]+ if typ == 0x51:+ tempo = int.from_bytes(trk[i + 3:i + 6], "big")+ i += 3 + n+ if typ == 0x2F:+ break+ elif st & 0xF0 == 0x90:+ on[trk[i + 1]] = (tick, trk[i + 2])+ i += 3+ elif st & 0xF0 == 0x80:+ s, v = on.pop(trk[i + 1])+ out.append((s, tick, trk[i + 1], v))+ i += 3+ else:+ raise AssertionError("event %x" % st)+ assert not on+ return ppq, tempo, sorted(out)+++class Writers(unittest.TestCase):+ NOTES = [note(0.0, 0.5, 60, 0.5), note(0.5, 1.0, 64, 0.8), note(1.0, 2.0, 67, 1.0), note(1.0, 2.0, 48, 0.25)]++ def test_midi_reads_back(self):+ ppq, tempo, ev = read_midi(xm.midi_bytes(self.NOTES, 120.0))+ self.assertEqual((ppq, tempo), (480, 500000))+ self.assertEqual([(s, e, p) for s, e, p, _ in ev], [(0, 480, 60), (480, 960, 64), (960, 1920, 48), (960, 1920, 67)])+ self.assertEqual([v for *_, v in ev], [64, 102, 32, 127])++ def test_musicxml(self):+ tmap = [[0.0, 0.0], [10.0, 20.0]]+ ns = self.NOTES + [note(2.0, 4.6, 72, 0.7)] # a long note: tied over the bar line+ q = xm.quantize(ns, tmap)+ for two in (False, True):+ xml = xm.musicxml(ns, q, 120.0, "C major", bass=[3], two_staves=two)+ dom = xml.encode("utf-8")+ doc = xml_parse(dom)+ pitched = [n for n in doc.getElementsByTagName("note") if n.getElementsByTagName("pitch")]+ tied_stops = [n for n in pitched if n.getElementsByTagName("tie") and n.getElementsByTagName("tie")[0].getAttribute("type") == "stop"]+ self.assertEqual(len(pitched) - len(tied_stops), len(ns))+ self.assertEqual(len(doc.getElementsByTagName("part")), 1)+ if two:+ self.assertEqual(doc.getElementsByTagName("staves")[0].firstChild.data, "2")+ for m in doc.getElementsByTagName("measure"): # every staff fills its measure exactly+ tot = {}+ for n in m.getElementsByTagName("note"):+ if n.getElementsByTagName("chord"):+ continue+ stf = n.getElementsByTagName("staff")+ k = stf[0].firstChild.data if stf else "1"+ tot[k] = tot.get(k, 0) + int(n.getElementsByTagName("duration")[0].firstChild.data)+ self.assertTrue(all(v == xm.MEASURE for v in tot.values()), tot)+++def xml_parse(b: bytes):+ return xml.dom.minidom.parseString(b)+++class Command(unittest.TestCase):+ def test_end_to_end(self):+ import soundfile as sf+ from st.launcher import build_env, showtime_home+ import subprocess+ spec = [(60, 0.4), (64, 0.9), (67, 1.4), (72, 1.9)]+ x = synth(spec, 3.0)+ with tempfile.TemporaryDirectory() as td:+ td = Path(td)+ sf.write(str(td / "a.wav"), x, SR, subtype="PCM_16")+ ff.run_ffmpeg(["-f", "lavfi", "-i", "color=c=black:s=160x90:r=30:d=3", "-i", str(td / "a.wav"), "-c:v", "libx264",+ "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", str(td / "f.mp4")])+ job = td / "job"+ cp = subprocess.run([sys.executable, str(LAUNCHER), "xray", "notes", str(td / "f.mp4"), "--job", str(job), "--instrument", "guitar"],+ env=build_env(showtime_home()), stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8", timeout=600)+ self.assertEqual(cp.returncode, 0, cp.stderr[-1500:])+ d = job / "xray"+ doc = json.loads((d / "notes.json").read_text())+ self.assertEqual(doc["schema"], "showtime.xray.notes/1")+ self.assertEqual(doc["engine"], "basic-pitch-onnx")+ self.assertEqual(doc["source"]["fps"], 30)+ got = {(n["pitch"]) for n in doc["notes"] if n["velocity"] > 0.5}+ self.assertEqual(got, {60, 64, 67, 72})+ for n in doc["notes"]:+ self.assertEqual(n["frame"], int(np.floor(n["t"] * 30 + 1e-9)))+ self.assertEqual(len(doc["quantized"]), len(doc["notes"]))+ self.assertTrue(doc["guitar"])+ self.assertEqual(doc["key"].split()[1] in ("major", "minor"), True)+ read_midi((d / "notes.mid").read_bytes())+ xml_parse((d / "notes.musicxml").read_bytes())+ self.assertGreater((d / "check-notes.jpg").stat().st_size, 5000)+++if __name__ == "__main__":+ unittest.main(verbosity=2 if "-v" in sys.argv else 1, argv=[a for a in sys.argv if a != "-v"])diff --git a/skills/showtime/tests/test_xray_template.py b/skills/showtime/tests/test_xray_template.pynew file mode 100644index 0000000..5391bc1--- /dev/null+++ b/skills/showtime/tests/test_xray_template.py@@ -0,0 +1,198 @@+#!/usr/bin/env python3+"""The xray template and the mechanism library: templates/xray, skills/showtime/library/xray, `showtime new xray`.++ * static: the template's sample is consistent (xray.json events carry frame = floor(t * fps), the sample clip is+ the analyzed file, the sync marker sits at (W-10, H-10) and skipped events never light it); every library+ entry registers a mechanism with its design box, four anchors and a draw function, and has a README with the+ clip it was verified on; the guide is routed from SKILL.md+ * CLI: `new xray` copies a runnable project (no layout variants left behind); --mechanism copies a library entry+ and drops the sample clip's evidence for another object; an unknown mechanism, or --mechanism on another+ template, is an error; --layout sets showtime.json "xray".layout++Stdlib only. usage: python tests/test_xray_template.py [--fast] [-v]+"""+from __future__ import annotations++import _isolate # noqa: F401 (run from a scratch folder: never write into the repository)++import hashlib+import json+import math+import re+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++TESTS_DIR = Path(__file__).resolve().parent+SKILL = TESTS_DIR.parent+LAUNCHER = SKILL / "lib" / "st" / "launcher.py"+TEMPLATE = SKILL / "templates" / "xray"+LIBRARY = SKILL / "library" / "xray"+sys.path.insert(0, str(SKILL / "lib"))++from st.launcher import build_env, showtime_home # noqa: E402++ENV = build_env(showtime_home())+TMP = Path(tempfile.mkdtemp(prefix="st-xray-tpl-"))+++def showtime(*args, check=True, timeout=300):+ cp = subprocess.run([sys.executable, str(LAUNCHER)] + [str(a) for a in args], env=ENV,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",+ errors="replace", timeout=timeout)+ if check and cp.returncode != 0:+ raise AssertionError("showtime %s failed (rc=%d):\n%s\n%s" % (+ " ".join(map(str, args)), cp.returncode, cp.stdout[-3000:], cp.stderr[-3000:]))+ return cp+++class TestStatic(unittest.TestCase):+ def test_sample_is_consistent(self):+ doc = json.loads((TEMPLATE / "xray.json").read_text(encoding="utf-8"))+ self.assertEqual(doc["schema"], "showtime.xray/1")+ src = doc["source"]+ clip = TEMPLATE / "media" / src["file"]+ self.assertTrue(clip.is_file(), clip)+ self.assertEqual(hashlib.sha256(clip.read_bytes()).hexdigest(), src["sha256"], "the sample clip is the analyzed file")+ self.assertLess(clip.stat().st_size, 256 * 1024, "binaries over 256 KiB need a manual review")+ for e in doc["events"]:+ self.assertEqual(e["frame"], math.floor(e["t"] * src["fps"]), e)+ track = doc["track"]+ self.assertLessEqual(abs(len(track["H"]) - src["duration"] * src["fps"]), 2, "one homography per frame")+ self.assertTrue(all(len(h) == 9 for h in track["H"]))+ cfg = json.loads((TEMPLATE / "showtime.json").read_text(encoding="utf-8"))+ xr = cfg["xray"]+ kept = [e for e in doc["events"] if e["i"] not in xr["skip"]]+ self.assertEqual(len(xr["text"]), len(kept), "one character of the paper per kept keystroke")+ self.assertEqual(cfg["render"]["poster_bake"], "off", "a baked poster would put a stray marker on frame 0")+ self.assertGreaterEqual(float(re.search(r"endcard:\s*([0-9.]+)", (TEMPLATE / "cue.js").read_text()).group(1)),+ src["duration"] - 0.05, "the end card starts after the clip")++ def test_marker_contract(self):+ html = (TEMPLATE / "index.html").read_text(encoding="utf-8")+ self.assertIn('id="xmark"', html)+ self.assertRegex(html, r"#xmark\s*\{[^}]*width:\s*6px;[^}]*height:\s*6px;[^}]*background:\s*#000;[^}]*z-index:\s*99")+ js = (TEMPLATE / "xray.js").read_text(encoding="utf-8")+ self.assertIn("px(S.W - 10)", js)+ self.assertIn("px(S.H - 10)", js)+ self.assertRegex(js, r"e\.frame === f && e\.kind !== 'skip'")+ for bad in ("Math.random", "Date.now", "setTimeout", "setInterval", "performance.now"):+ self.assertNotIn(bad, js)++ def test_one_cue_table(self):+ tables = [p.name for p in TEMPLATE.glob("*.js") if re.search(r"\bvar CUE\s*=\s*\{", p.read_text(encoding="utf-8"))]+ self.assertEqual(tables, ["cue.js"])++ def test_library_entries(self):+ names = sorted(p.parent.name for p in LIBRARY.glob("*/mechanism.js"))+ self.assertIn("typewriter", names)+ self.assertTrue((LIBRARY / "README.md").is_file())+ for n in names:+ js = (LIBRARY / n / "mechanism.js").read_text(encoding="utf-8")+ self.assertIn("XRAY.mechanism(", js, n)+ for key in ("name:", "box:", "anchors:", "draw:", "drawn:"):+ self.assertIn(key, js, (n, key))+ anchors = re.search(r"anchors:\s*\{([^}]*)\}", js).group(1)+ self.assertGreaterEqual(len(re.findall(r"\w+:\s*(?:\[|[A-Z])", anchors)), 4, n)+ readme = (LIBRARY / n / "README.md").read_text(encoding="utf-8")+ self.assertIn("Verified on", readme, n)+ self.assertIn("Measured", readme, n)+ for bad in ("Math.random", "Date.now"):+ self.assertNotIn(bad, js, n)+ self.assertEqual((TEMPLATE / "mechanism.js").read_text(encoding="utf-8"),+ (LIBRARY / "typewriter" / "mechanism.js").read_text(encoding="utf-8"),+ "the template's mechanism is the library's typewriter")++ def test_anchors_not_collinear(self):+ """Four anchors place a mechanism with a homography: three of them in a line make it degenerate (nothing+ draws in place). Checked with node on each mechanism's own anchors."""+ import shutil as _sh+ node = _sh.which("node", path=ENV.get("PATH"))+ if not node:+ self.skipTest("node not found")+ for p in sorted(LIBRARY.glob("*/mechanism.js")):+ js = ("var out=null;var XRAY={mechanism:function(m){out=m;}};" + p.read_text(encoding="utf-8") ++ ";var a=Object.keys(out.anchors).slice(0,4).map(function(k){return out.anchors[k];});var bad=[];"+ "for(var i=0;i<4;i++)for(var j=i+1;j<4;j++)for(var k=j+1;k<4;k++){var A=a[i],B=a[j],C=a[k];"+ "var cr=(B[0]-A[0])*(C[1]-A[1])-(B[1]-A[1])*(C[0]-A[0]);if(Math.abs(cr)<1e-6)bad.push([i,j,k]);}"+ "console.log(JSON.stringify({n:a.length,bad:bad}));")+ cp = subprocess.run([node, "-e", js], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")+ self.assertEqual(cp.returncode, 0, (p, cp.stderr[-500:]))+ res = json.loads(cp.stdout.strip().splitlines()[-1])+ self.assertEqual(res["n"], 4, p)+ self.assertEqual(res["bad"], [], "%s: three anchors in a line" % p.parent.name)++ def test_guide_routed(self):+ skill = (SKILL / "SKILL.md").read_text(encoding="utf-8")+ self.assertIn("| `xray` |", skill)+ self.assertTrue((SKILL / "references" / "workflows" / "xray.md").is_file())+++class TestCLI(unittest.TestCase):+ def test_new_xray(self):+ d = TMP / "plain"+ showtime("new", "xray", d)+ for f in ("index.html", "xray.js", "mechanism.js", "cue.js", "xray.json", "media/clip.webm", "audio/mix.json"):+ self.assertTrue((d / f).is_file(), f)+ self.assertFalse((d / "diagram.html").exists())+ self.assertFalse((d / "README.md").exists())+ self.assertEqual(json.loads((d / "showtime.json").read_text())["xray"]["layout"], "through")++ def test_layout_and_mechanism(self):+ d = TMP / "split"+ showtime("new", "xray", d, "--layout", "split", "--mechanism", "typewriter")+ xr = json.loads((d / "showtime.json").read_text())["xray"]+ self.assertEqual(xr["layout"], "split")+ self.assertEqual(xr["text"], "Sounds of changes.", "the typewriter keeps the sample's evidence")+ others = [p.parent.name for p in LIBRARY.glob("*/mechanism.js") if p.parent.name != "typewriter"]+ if others:+ d2 = TMP / "other"+ cp = showtime("new", "xray", d2, "--mechanism", others[0])+ self.assertEqual((d2 / "mechanism.js").read_text(), (LIBRARY / others[0] / "mechanism.js").read_text())+ xr2 = json.loads((d2 / "showtime.json").read_text())["xray"]+ for k in ("text", "place", "skip"):+ self.assertNotIn(k, xr2, "another object never inherits the typewriter clip's evidence")+ self.assertIn("still the typewriter sample", cp.stdout + cp.stderr)++ def test_diagram_without_scene_falls_back_to_split(self):+ d = TMP / "nodiag"+ cp = showtime("new", "xray", d, "--layout", "diagram", "--mechanism", "piano")+ self.assertEqual(json.loads((d / "showtime.json").read_text())["xray"]["layout"], "split")+ self.assertFalse((d / "diagram.js").exists())+ self.assertNotIn("XRAY_DIAGRAM", (d / "index.html").read_text(encoding="utf-8"))+ self.assertIn("split", cp.stdout + cp.stderr)++ def test_offset_frames_in_both_pages(self):+ for name in ("xray.js", "diagram.html"):+ t = (TEMPLATE / name).read_text(encoding="utf-8")+ self.assertIn("offset_frames", t, name)+ self.assertIn("> 0.01", t, name) # verify's retime threshold+ self.assertIn("data-offset", t, name)++ def test_theme(self):+ d = TMP / "light"+ showtime("new", "xray", d, "--theme", "light")+ cfg = json.loads((d / "showtime.json").read_text())+ self.assertEqual(cfg["xray"]["theme"], "light")+ self.assertEqual(cfg["background"], "#f4f0e7")+ cp = showtime("new", "xray", TMP / "white", "--theme", "white", check=False)+ self.assertNotEqual(cp.returncode, 0)+ js = (TEMPLATE / "xray.js").read_text(encoding="utf-8")+ self.assertIn("light: {", js)+ for p in [TEMPLATE / "xray.js"] + sorted(LIBRARY.glob("*/mechanism.js")):+ self.assertNotRegex(p.read_text(encoding="utf-8"), r"'rgba\(\d", "%s: colours come from the theme (C.l/C.i/C.bg/C.h)" % p.name)++ def test_errors(self):+ cp = showtime("new", "xray", TMP / "bad", "--mechanism", "no-such-thing", check=False)+ self.assertNotEqual(cp.returncode, 0)+ self.assertIn("typewriter", cp.stdout + cp.stderr)+ cp = showtime("new", "dom", TMP / "bad2", "--mechanism", "typewriter", check=False)+ self.assertNotEqual(cp.returncode, 0)+ self.assertIn("xray template", cp.stdout + cp.stderr)+++if __name__ == "__main__":+ argv = [a for a in sys.argv if a != "--fast"]+ unittest.main(argv=argv, verbosity=2 if "-v" in argv else 1)