Compare commits

...
Author SHA1 Message Date
Dylan M. Taylor bf838221ca Merge remote-tracking branch 'origin/main' into HEAD
# Conflicts:
#	apps/soundcraft/src/control_server.rs
2026-11-17 08:07:01 -05:00
Dylan M. Taylor 16bd7327f3 docs: prefer craft-libs for reusable formats, codecs, protocols and UI (#1592) 2026-11-16 21:59:50 -05:00
Dylan M. Taylor 9676d492db refactor(score): use shared MusicXML and SVG writers with contributor tie fixes (#1598) 2026-11-16 21:59:45 -05:00
FredApps 648ed9db9a Merge pull request #1589 from storytold/extract/shared-mjpeg
video: decode Motion JPEG with craft-video's shared mjpeg codec
2026-11-16 13:42:06 +01:00
FredApps d0479e609c video: pin craft-video to craft-libs main ddbe7823 (Adobe APP14 transform fix, craft-libs#1847) 2026-11-16 13:26:44 +01:00
FredApps fc4dddd7c7 video: decode Motion JPEG with craft-video's shared mjpeg codec
Movie frames from MJPEG tracks were decoded with the image crate's JPEG
decoder, and the synthetic MJPEG fixtures were encoded with its encoder.
Both now go through craft_video::mjpeg (storytold/craft-libs#1757), a
frame-level wrapper over craft-jpeg. The picture size is still checked
from the header against MAX_PIXELS before any pixel is decoded, and grey
and 12-bit pictures map to RGBA8. soundcraft-video no longer depends on
the image crate.

craft-video is pinned at craft-libs main 341020b2 with the mjpeg feature.

Refs storytold/craft-libs#778, storytold/craft-libs#781.
2026-11-16 11:23:42 +01:00
Dylan M. Taylor 94544a9a5f chore: drop worktree links added by mistake 2026-11-03 11:02:57 +00:00
Dylan M. Taylor 26bdcdf6fb fix(control): require a per-launch token on every control request 2026-11-03 11:02:27 +00:00
17 changed files with 714 additions and 473 deletions
+6 -2
View File
@@ -105,8 +105,8 @@ User-visible behaviour = a command in `crates/engine/src/cmd/*` (id, label, menu
Parity is measured, not guessed: `crates/engine/catalog/menus.txt` lists the incumbent's menu leaves (names only). `cargo xtask parity` writes `docs/parity-checklist.md` (generated; the hand-written assessment is `docs/target-app-parity.md`); a test enforces a floor that only rises.
## Running and looking at the app
- `cargo run --release -p soundcraft -- --demo --control 0` (demo session + control channel on a free port; the port is printed).
- Drive it: JSON lines over TCP, e.g. `{"id":1,"method":"engine.execute","params":{"command":"track.new","params":{"count":2,"format":"stereo"}}}`, then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`.
- `cargo run --release -p soundcraft -- --demo --control 0` (demo session + control channel on a free port; the port and the launch's `SOUNDCRAFT_CONTROL_TOKEN` are printed, or export the token first so both sides share it).
- Drive it: JSON lines over TCP, e.g. `{"id":1,"method":"engine.execute","params":{"command":"track.new","params":{"count":2,"format":"stereo"}},"token":"…"}`, then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"},"token":"…"}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`.
- Offscreen UI render (no window, no focus stealing): `cargo run -p soundcraft-ui-egui --example ui_shot -- out.png [script.jsonl]`.
- Headless: `soundcraft-cli run --demo --cmd 'mix.volume={"track":"Kick","db":-6}' --bounce out.wav`; MCP: `soundcraft-cli mcp [--connect PORT]`.
- **Language / localization:** the interface is English by default; Brazilian Portuguese
@@ -150,3 +150,7 @@ Push to the `release` branch to build signed installers for macOS (universal DMG
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
New workspace crates include `[lints] workspace = true` in `Cargo.toml`. Production crate roots declare the crate-level lint attributes before modules; crate-level `//!` documentation may precede them.
## Shared Craft libraries
Before adding or changing reusable file-format, codec, protocol or UI code, check [craft-libs](https://github.com/storytold/craft-libs) and existing contributor work; reuse its public APIs or extend the appropriate shared library instead of adding an app-local duplicate, keeping app-specific policy and model mapping local and preserving compatibility, security limits and regression coverage.
Generated
+363 -13
View File
@@ -138,6 +138,24 @@ dependencies = [
"memchr",
]
[[package]]
name = "aligned"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee4508988c62edf04abd8d92897fca0c2995d907ce1dfeaf369dac3716a40685"
dependencies = [
"as-slice",
]
[[package]]
name = "aligned-vec"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc890384c8602f339876ded803c97ad529f3842aba97f6392b3dba0dd171769b"
dependencies = [
"equator",
]
[[package]]
name = "allocator-api2"
version = "0.2.21"
@@ -256,6 +274,12 @@ version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "arbitrary"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed"
[[package]]
name = "arboard"
version = "3.7.0"
@@ -277,6 +301,17 @@ dependencies = [
"x11rb",
]
[[package]]
name = "arg_enum_proc_macro"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "arrayref"
version = "0.3.9"
@@ -295,6 +330,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
[[package]]
name = "as-slice"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516"
dependencies = [
"stable_deref_trait",
]
[[package]]
name = "ash"
version = "0.38.0+1.3.281"
@@ -497,6 +541,40 @@ version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "av-scenechange"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f321d77c20e19b92c39e7471cf986812cbb46659d2af674adc4331ef3f18394"
dependencies = [
"aligned",
"anyhow",
"arg_enum_proc_macro",
"arrayvec",
"log",
"num-rational",
"num-traits",
"pastey",
"rayon",
"thiserror 2.0.21",
"v_frame",
"y4m",
]
[[package]]
name = "av1-grain"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cfddb07216410377231960af4fcab838eaa12e013417781b78bd95ee22077f8"
dependencies = [
"anyhow",
"arrayvec",
"log",
"nom 8.0.0",
"num-rational",
"v_frame",
]
[[package]]
name = "base64"
version = "0.23.1"
@@ -569,6 +647,15 @@ version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
[[package]]
name = "bitstream-io"
version = "4.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7eff00be299a18769011411c9def0d827e8f2d7bf0c3dbf53633147a8867fd1f"
dependencies = [
"no_std_io2",
]
[[package]]
name = "block-buffer"
version = "0.10.4"
@@ -633,6 +720,12 @@ dependencies = [
"syn 3.0.9",
]
[[package]]
name = "built"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c0e531d93d39c34eef561e929e8a7f86d77a5af08aac4f6d6e39976c51858e9"
[[package]]
name = "bumpalo"
version = "3.20.3"
@@ -771,7 +864,7 @@ checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
dependencies = [
"cfg-if",
"cpufeatures 0.3.1",
"rand_core",
"rand_core 0.10.1",
]
[[package]]
@@ -1066,7 +1159,7 @@ name = "craft-audio"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
dependencies = [
"craft-media",
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c)",
"log",
"serde",
"symphonia",
@@ -1111,6 +1204,14 @@ dependencies = [
"writeable",
]
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=ddbe78231b5b23273b3d191f04b1a38f4c0445b3#ddbe78231b5b23273b3d191f04b1a38f4c0445b3"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-media"
version = "0.1.0"
@@ -1120,6 +1221,11 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-media"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=ddbe78231b5b23273b3d191f04b1a38f4c0445b3#ddbe78231b5b23273b3d191f04b1a38f4c0445b3"
[[package]]
name = "craft-midi"
version = "0.1.0"
@@ -1139,6 +1245,14 @@ dependencies = [
"encoding_rs",
]
[[package]]
name = "craft-score"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=9595e0ca9bff15b04c0096458c63d96d8b090b63#9595e0ca9bff15b04c0096458c63d96d8b090b63"
dependencies = [
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=9595e0ca9bff15b04c0096458c63d96d8b090b63)",
]
[[package]]
name = "craft-scraft-format"
version = "0.1.0"
@@ -1213,13 +1327,25 @@ dependencies = [
[[package]]
name = "craft-video"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c"
source = "git+https://github.com/storytold/craft-libs?rev=ddbe78231b5b23273b3d191f04b1a38f4c0445b3#ddbe78231b5b23273b3d191f04b1a38f4c0445b3"
dependencies = [
"craft-media",
"craft-jpeg",
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ddbe78231b5b23273b3d191f04b1a38f4c0445b3)",
"rav1e",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=9595e0ca9bff15b04c0096458c63d96d8b090b63#9595e0ca9bff15b04c0096458c63d96d8b090b63"
dependencies = [
"quick-xml 0.41.0",
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-xml"
version = "0.1.0"
@@ -1664,6 +1790,26 @@ name = "epaint_default_fonts"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
[[package]]
name = "equator"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4711b213838dfee0117e3be6ac926007d7f433d7bbe33595975d4190cb07e6fc"
dependencies = [
"equator-macro",
]
[[package]]
name = "equator-macro"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44f23cf4b44bfce11a86ace86f8a73ffdec849c9fd00a386a53d278bd9e81fb3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "equivalent"
version = "1.0.3"
@@ -1981,7 +2127,7 @@ dependencies = [
"js-sys",
"libc",
"r-efi 6.0.0",
"rand_core",
"rand_core 0.10.1",
"wasm-bindgen",
]
@@ -2416,8 +2562,6 @@ dependencies = [
"num-traits",
"png",
"tiff",
"zune-core",
"zune-jpeg",
]
[[package]]
@@ -2430,6 +2574,17 @@ dependencies = [
"hashbrown 0.17.1",
]
[[package]]
name = "interpolate_name"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "is-docker"
version = "0.2.0"
@@ -2464,6 +2619,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.15.0"
@@ -2619,6 +2783,16 @@ version = "0.2.191"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
[[package]]
name = "libfuzzer-sys"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2"
dependencies = [
"arbitrary",
"cc",
]
[[package]]
name = "libloading"
version = "0.8.9"
@@ -2711,6 +2885,16 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b"
[[package]]
name = "maybe-rayon"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519"
dependencies = [
"cfg-if",
"rayon",
]
[[package]]
name = "memchr"
version = "2.8.3"
@@ -2863,6 +3047,12 @@ dependencies = [
"jni-sys 0.3.1",
]
[[package]]
name = "new_debug_unreachable"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
[[package]]
name = "nix"
version = "0.31.3"
@@ -2875,6 +3065,15 @@ dependencies = [
"libc",
]
[[package]]
name = "no_std_io2"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003"
dependencies = [
"memchr",
]
[[package]]
name = "nohash-hasher"
version = "0.2.0"
@@ -2900,6 +3099,22 @@ dependencies = [
"memchr",
]
[[package]]
name = "noop_proc_macro"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8"
[[package]]
name = "num-bigint"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.6"
@@ -2935,6 +3150,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -3426,6 +3652,18 @@ dependencies = [
"windows-link",
]
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pastey"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec"
[[package]]
name = "peniko"
version = "0.6.1"
@@ -3651,6 +3889,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "presser"
version = "0.3.1"
@@ -3699,6 +3946,19 @@ name = "profiling"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5"
dependencies = [
"profiling-procmacros",
]
[[package]]
name = "profiling-procmacros"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb"
dependencies = [
"quote",
"syn 2.0.119",
]
[[package]]
name = "proptest"
@@ -3712,7 +3972,7 @@ dependencies = [
"chacha20",
"core_detect",
"num-traits",
"rand",
"rand 0.10.3",
"rand_xorshift",
"regex-syntax",
"rusty-fork",
@@ -3777,6 +4037,16 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rand"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41"
dependencies = [
"rand_chacha",
"rand_core 0.9.5",
]
[[package]]
name = "rand"
version = "0.10.3"
@@ -3784,7 +4054,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"
dependencies = [
"getrandom 0.4.3",
"rand_core",
"rand_core 0.10.1",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core 0.9.5",
]
[[package]]
name = "rand_core"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
dependencies = [
"getrandom 0.3.4",
]
[[package]]
@@ -3799,7 +4088,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf"
dependencies = [
"rand_core",
"rand_core 0.10.1",
]
[[package]]
@@ -3808,6 +4097,40 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08"
[[package]]
name = "rav1e"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b6dd56e85d9483277cde964fd1bdb0428de4fec5ebba7540995639a21cb32b"
dependencies = [
"aligned-vec",
"arbitrary",
"arg_enum_proc_macro",
"arrayvec",
"av-scenechange",
"av1-grain",
"bitstream-io",
"built",
"cfg-if",
"interpolate_name",
"itertools 0.14.0",
"libc",
"libfuzzer-sys",
"log",
"maybe-rayon",
"new_debug_unreachable",
"noop_proc_macro",
"num-derive",
"num-traits",
"paste",
"profiling",
"rand 0.9.5",
"rand_chacha",
"simd_helpers",
"thiserror 2.0.21",
"v_frame",
]
[[package]]
name = "raw-window-handle"
version = "0.6.2"
@@ -4323,6 +4646,15 @@ dependencies = [
"simdutf8",
]
[[package]]
name = "simd_helpers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6"
dependencies = [
"quote",
]
[[package]]
name = "simdutf8"
version = "0.1.5"
@@ -4448,6 +4780,7 @@ dependencies = [
"craft-update",
"eframe",
"egui",
"getrandom 0.3.4",
"log",
"muda",
"objc2 0.6.5",
@@ -4533,8 +4866,9 @@ version = "0.20.1"
dependencies = [
"craft-audio",
"craft-docpath",
"craft-score",
"craft-scraft-format",
"craft-xml",
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10)",
"log",
"proptest",
"serde",
@@ -4641,9 +4975,8 @@ dependencies = [
name = "soundcraft-video"
version = "0.20.1"
dependencies = [
"craft-media",
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c)",
"craft-video",
"image",
"proptest",
"serde",
"thiserror 2.0.21",
@@ -5393,6 +5726,17 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "v_frame"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "666b7727c8875d6ab5db9533418d7c764233ac9c0cff1d469aec8fa127597be2"
dependencies = [
"aligned-vec",
"num-traits",
"wasm-bindgen",
]
[[package]]
name = "vello_common"
version = "0.2.0"
@@ -6320,6 +6664,12 @@ dependencies = [
"serde_json",
]
[[package]]
name = "y4m"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448"
[[package]]
name = "yoke"
version = "0.8.4"
+2 -1
View File
@@ -11,6 +11,7 @@ rust-version = "1.95"
repository = "https://github.com/storytold/soundcraft"
[workspace.dependencies]
craft-score = { git = "https://github.com/storytold/craft-libs", rev = "9595e0ca9bff15b04c0096458c63d96d8b090b63" }
craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10" }
# internal
@@ -55,7 +56,7 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l
# Video containers and bitstreams, shared with the other craft apps.
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" }
# H.264 and ProRes codecs shared with the other Craft apps.
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c", default-features = false, features = ["h264-decode", "prores"] }
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ddbe78231b5b23273b3d191f04b1a38f4c0445b3", default-features = false, features = ["h264-decode", "prores", "mjpeg"] }
# Audio file formats, codecs and waveform overviews, shared with the other craft apps. It shares
# types with craft-media, so both stay on one rev.
# Standard MIDI Files and the note/controller model, shared with the other craft apps.
+2 -1
View File
@@ -213,7 +213,8 @@ soundcraft-cli run --demo \
--cmd 'mix.insert={"track":"Bass","plugin":"eq_7band"}' \
--bounce mix.wav
# Drive the running app.
# Drive the running app (the token keeps other local accounts off the loopback port).
export SOUNDCRAFT_CONTROL_TOKEN=$(head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')
soundcraft --demo --control 7801 &
soundcraft-cli app --port 7801 transport.play
soundcraft-cli app --port 7801 ui.screenshot '{"path":"shot.png"}'
+2
View File
@@ -26,6 +26,8 @@ egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"
serde_json.workspace = true
log.workspace = true
chrono.workspace = true
# The per-launch control token (already in the graph through egui's ahash).
getrandom = "0.3"
[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "linux"))))'.dependencies]
winit = { version = "0.30", default-features = false, features = ["x11"] }
+74 -11
View File
@@ -5,8 +5,13 @@
//!
//! Lines are bounded ([`MAX_LINE`]): a longer one is refused and the connection closed, without
//! buffering the rest. So is the number of connections served at once ([`MAX_CONNECTIONS`]).
//!
//! Every request carries the launch's secret as `"token"` (`SOUNDCRAFT_CONTROL_TOKEN`, or one made
//! here and printed after the port): the port is loopback, which any local account can reach, and
//! a request acts with this process's authority.
use serde_json::{Value, json};
use soundcraft_automation::CONTROL_TOKEN_VAR;
use soundcraft_ui_egui::ControlRequest;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
@@ -30,9 +35,20 @@ const MAX_CONNECTIONS: usize = 16;
const MAX_QUEUED_REQUESTS: usize = MAX_CONNECTIONS;
/// Bind the loopback control listener and return the request receiver. Port 0 picks a free one and the actual port is
/// printed; bind or spawn failures are logged instead of panicking.
pub fn start(port: u16, ctx: egui::Context) -> Receiver<ControlRequest> {
/// printed; bind or spawn failures are logged instead of panicking. Requests must carry `token`; with `None` one is
/// made for this launch and printed after the port, so only the launching process learns it.
pub fn start(port: u16, token: Option<String>, ctx: egui::Context) -> Receiver<ControlRequest> {
let (tx, rx) = sync_channel::<ControlRequest>(MAX_QUEUED_REQUESTS);
let (token, made) = match token {
Some(t) => (t, false),
None => match new_token() {
Ok(t) => (t, true),
Err(e) => {
log::error!("control server not started: no {CONTROL_TOKEN_VAR} and no random token: {e}");
return rx;
}
},
};
let listener = match TcpListener::bind(("127.0.0.1", port)) {
Ok(l) => l,
Err(e) => {
@@ -43,13 +59,29 @@ pub fn start(port: u16, ctx: egui::Context) -> Receiver<ControlRequest> {
let actual = listener.local_addr().map(|a| a.port()).unwrap_or(port);
log::info!("control server listening on 127.0.0.1:{actual}");
println!("SOUNDCRAFT_CONTROL_PORT={actual}");
let spawned = std::thread::Builder::new().name("soundcraft-control".into()).spawn(move || accept_loop(listener, tx, ctx));
if made {
println!("{CONTROL_TOKEN_VAR}={token}");
}
let spawned = std::thread::Builder::new().name("soundcraft-control".into()).spawn(move || accept_loop(listener, tx, ctx, Arc::from(token)));
if let Err(e) = spawned {
log::error!("control thread failed: {e}");
}
rx
}
/// 32 random bytes as hex, from the OS.
fn new_token() -> Result<String, getrandom::Error> {
let mut bytes = [0u8; 32];
getrandom::fill(&mut bytes)?;
Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())
}
/// `token` equals `expected`, compared in time that doesn't depend on where they differ.
fn token_matches(token: Option<&str>, expected: &str) -> bool {
let Some(token) = token else { return false };
token.len() == expected.len() && token.bytes().zip(expected.bytes()).fold(0u8, |acc, (a, b)| acc | (a ^ b)) == 0
}
/// A connection's place among [`MAX_CONNECTIONS`], given back when the connection ends (also when
/// its thread can't be started, or panics).
struct Slot(Arc<AtomicUsize>);
@@ -62,7 +94,7 @@ impl Drop for Slot {
/// Accept until the listener closes, admitting at most [`MAX_CONNECTIONS`] peers; excess peers get one error line.
/// Each admitted connection is served on its own thread, releasing its slot on exit.
fn accept_loop(listener: TcpListener, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
fn accept_loop(listener: TcpListener, tx: SyncSender<ControlRequest>, ctx: egui::Context, token: Arc<str>) {
let open = Arc::new(AtomicUsize::new(0));
for mut stream in listener.incoming().flatten() {
if open.fetch_add(1, Ordering::SeqCst) >= MAX_CONNECTIONS {
@@ -73,9 +105,10 @@ fn accept_loop(listener: TcpListener, tx: SyncSender<ControlRequest>, ctx: egui:
let slot = Slot(Arc::clone(&open));
let tx = tx.clone();
let ctx = ctx.clone();
let token = Arc::clone(&token);
let _ = std::thread::Builder::new().name("soundcraft-control-conn".into()).spawn(move || {
let _slot = slot;
serve(stream, tx, ctx);
serve(stream, tx, ctx, &token);
});
}
}
@@ -104,10 +137,11 @@ fn read_bounded_line(reader: &mut impl BufRead, buf: &mut Vec<u8>, max: usize) -
Ok(true)
}
/// Answer newline-delimited JSON requests for one connection: bound each line, forward it to the UI thread with a 90
/// Answer newline-delimited JSON requests for one connection: bound each line, forward it to the UI thread with a 90
/// s reply deadline, and stop at EOF, invalid UTF-8, a closed request queue or a line that isn't a request (see
/// [`parse_request`]). That line gets one error reply first.
fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
/// [`parse_request`]). A request that is not a request, a wrong `token`, or bad JSON gets one error reply first.
fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context, token: &str) {
let Ok(read) = stream.try_clone() else { return };
let mut out = stream;
let mut reader = BufReader::new(read);
@@ -126,7 +160,7 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
if line.trim().is_empty() {
continue;
}
let reply = match parse_request(line) {
let reply = match parse_request(line, token) {
Ok((id, method, params)) => {
let (req, rrx) = ControlRequest::new(method, params);
match tx.try_send(req) {
@@ -158,9 +192,13 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
/// A request line's `(id, method, params)`. The line must be a JSON object with a string `method`;
/// otherwise the error says what it is instead.
fn parse_request(line: &str) -> Result<(Value, String, Value), String> {
fn parse_request(line: &str, token: &str) -> Result<(Value, String, Value), String> {
let msg = serde_json::from_str::<Value>(line).map_err(|e| format!("bad JSON: {e}"))?;
let Some(msg) = msg.as_object() else { return Err("not a request: expected a JSON object".into()) };
if !token_matches(msg.get("token").and_then(Value::as_str), token) {
let id = msg.get("id").cloned().unwrap_or(Value::Null);
return Err(format!("unauthorized: the request's `token` is not this app's {CONTROL_TOKEN_VAR} (id {id})"));
}
let Some(method) = msg.get("method").and_then(Value::as_str) else { return Err("not a request: no string `method`".into()) };
Ok((msg.get("id").cloned().unwrap_or(Value::Null), method.to_string(), msg.get("params").cloned().unwrap_or(json!({}))))
}
@@ -199,12 +237,14 @@ mod tests {
assert!(state + request.to_string().len() <= MAX_LINE);
}
const TOKEN: &str = "s3cret";
/// The control server on a loopback port, with a stand-in UI that echoes each method.
fn serve_echo() -> std::net::SocketAddr {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = sync_channel::<ControlRequest>(MAX_QUEUED_REQUESTS);
std::thread::spawn(move || accept_loop(listener, tx, egui::Context::default()));
std::thread::spawn(move || accept_loop(listener, tx, egui::Context::default(), Arc::from(TOKEN)));
std::thread::spawn(move || {
for req in rx {
let _ = req.reply.send(json!({"ok": true, "method": req.method}));
@@ -218,7 +258,7 @@ mod tests {
let addr = serve_echo();
let mut c = TcpStream::connect(addr).unwrap();
c.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
c.write_all(b"{\"id\":3,\"method\":\"ping\"}\r\n\n{\"id\":4,\"method\":\"pong\"}\n").unwrap();
c.write_all(b"{\"id\":3,\"method\":\"ping\",\"token\":\"s3cret\"}\r\n\n{\"id\":4,\"method\":\"pong\",\"token\":\"s3cret\"}\n").unwrap();
let mut r = BufReader::new(c);
for (id, method) in [(3, "ping"), (4, "pong")] {
let mut line = String::new();
@@ -228,6 +268,29 @@ mod tests {
}
}
#[test]
fn a_request_without_the_token_is_refused_and_the_connection_closed() {
let addr = serve_echo();
for request in [r#"{"id":1,"method":"ping"}"#, r#"{"id":1,"method":"ping","token":"s3cre"}"#, r#"{"id":1,"method":"ping","token":"S3CRET"}"#]
{
let mut c = TcpStream::connect(addr).unwrap();
c.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
writeln!(c, "{request}").unwrap();
let mut r = BufReader::new(c);
let mut line = String::new();
r.read_line(&mut line).unwrap();
let v: Value = serde_json::from_str(&line).unwrap();
assert_eq!((v["id"].as_i64(), v["ok"].as_bool()), (Some(1), Some(false)), "{line}");
assert!(v["error"].as_str().unwrap_or("").starts_with("unauthorized"), "{line}");
assert!(v.get("method").is_none(), "the request was not forwarded: {line}");
line.clear();
assert_eq!(r.read_line(&mut line).unwrap(), 0, "the connection is closed after a refusal");
}
assert!(token_matches(Some(TOKEN), TOKEN));
assert!(!token_matches(None, TOKEN));
assert_eq!(new_token().unwrap().len(), 64);
}
#[test]
fn extra_connections_are_refused() {
let addr = serve_echo();
+4 -3
View File
@@ -3,8 +3,9 @@
//! Usage: `soundcraft [--demo] [--control <port>] [--no-audio] [session.scraft | audio files…]`; `soundcraft --help` lists the flags and `SOUNDCRAFT_CONTROL_PORT`.
//!
//! `--control <port>` (or `SOUNDCRAFT_CONTROL_PORT`) starts a localhost JSON-lines control server
//! (`0` picks a free port and prints it): `{"id":1,"method":"ui.inspect","params":{}}` →
//! `{"id":1,"ok":true,"result":…}`. See `soundcraft_ui_egui::control` for the methods.
//! (`0` picks a free port and prints it): `{"id":1,"method":"ui.inspect","params":{},"token":"…"}` →
//! `{"id":1,"ok":true,"result":…}`, where `token` is `SOUNDCRAFT_CONTROL_TOKEN` (or the one made for
//! this launch, printed after the port). See `soundcraft_ui_egui::control` for the methods.
//!
//! `log` records go to standard error and `<settings dir>/logs/soundcraft.log` (`RUST_LOG` sets the
//! levels); see [`logging`].
@@ -264,7 +265,7 @@ fn main() -> eframe::Result {
app.extra.ui_scale = prefs.ui_scale;
app.ui = prefs;
if let Some(port) = control_port {
let rx = control_server::start(port, cc.egui_ctx.clone());
let rx = control_server::start(port, soundcraft_automation::control_token(), cc.egui_ctx.clone());
app = app.with_control(rx);
}
for f in &files {
+16 -3
View File
@@ -151,18 +151,31 @@ impl RemoteError {
type Connection = (TcpStream, BufReader<TcpStream>);
/// The variable carrying the control channel's secret: the app refuses every request whose
/// `token` differs from it, so another local account can't drive the app through the loopback
/// port. The app makes one per launch when the variable is unset (printed after the port line).
pub const CONTROL_TOKEN_VAR: &str = "SOUNDCRAFT_CONTROL_TOKEN";
/// [`CONTROL_TOKEN_VAR`] from the environment; a blank value counts as unset.
pub fn control_token() -> Option<String> {
std::env::var(CONTROL_TOKEN_VAR).ok().map(|t| t.trim().to_owned()).filter(|t| !t.is_empty())
}
/// A TCP client for a running app's control channel.
pub struct Remote {
/// The target as `host:port`, for connecting and for messages.
addr: String,
/// Sent with every request; without one the app refuses, naming the variable.
token: Option<String>,
conn: Option<Connection>,
next_id: u64,
}
impl Remote {
/// A client for the app at `target`; it connects on the first request.
/// A client for the app at `target`, authenticating with [`control_token`]; it connects on the
/// first request.
pub fn new(target: &ControlTarget) -> Self {
Remote { addr: target.to_string(), conn: None, next_id: 1 }
Remote { addr: target.to_string(), token: control_token(), conn: None, next_id: 1 }
}
/// The open connection, connecting first when there is none.
@@ -178,7 +191,7 @@ impl Remote {
fn roundtrip(&mut self, method: &str, params: &Value) -> Result<Value, (RemoteError, bool)> {
let id = self.next_id;
self.next_id += 1;
let line = json!({"id": id, "method": method, "params": params}).to_string();
let line = json!({"id": id, "method": method, "params": params, "token": self.token}).to_string();
let (w, r) = self.connection().map_err(|e| (e, false))?;
writeln!(w, "{line}").map_err(|e| (e.into(), false))?;
let mut buf = String::new();
+1
View File
@@ -8,6 +8,7 @@ rust-version.workspace = true
repository.workspace = true
[dependencies]
craft-score.workspace = true
craft-xml.workspace = true
soundcraft-time.workspace = true
craft-audio.workspace = true
+175 -416
View File
@@ -1,19 +1,20 @@
//! Notation: MIDI tracks laid out as measures of notes and rests, written as MusicXML
//! (File › Export › Sibelius, File › Send To › Sibelius) or engraved to SVG (File › Print Score).
//!
//! Notes are quantized to sixteenths. Each part is a single voice: notes that start together form
//! a chord, overlapping notes are cut at the next onset, and anything crossing a bar line is split
//! and tied.
//! Notes are quantized to sixteenths. Each part is a sequence of chords split at note onsets,
//! note ends and bar lines. Per-pitch ties preserve the duration of each note, including
//! overlapping notes. A chord keeps at most [`MAX_PITCHES_PER_CHORD`] pitches.
pub use craft_score::{Item, Measure, Part};
use soundcraft_model::{ClipContent, Session, TrackId, TrackKind};
use soundcraft_time::TICKS_PER_QUARTER;
use std::fmt::Write as _;
/// Ticks per score unit (a sixteenth).
const UNIT: i64 = TICKS_PER_QUARTER / 4;
/// Upper bound on engraved measures, so a hostile session cannot make a huge document.
const MAX_MEASURES: usize = 2000;
/// Upper bound on output size, so a hostile session cannot make a huge document.
#[cfg(test)]
const MAX_OUTPUT_BYTES: usize = 256 * 1024 * 1024; // 256 MiB
/// Upper bound on items per measure, so a hostile session cannot make a huge document.
const MAX_ITEMS_PER_MEASURE: usize = 128;
@@ -25,14 +26,6 @@ const MAX_NOTES_PER_PART: usize = 100_000;
/// Note values a single notehead can show, in sixteenths (longest first).
const VALUES: [u32; 8] = [16, 12, 8, 6, 4, 3, 2, 1];
/// The output has grown past the byte budget: refuse to grow it further.
fn over_budget(buf: &str, budget: usize) -> Result<(), String> {
if buf.len() > budget {
return Err(format!("score too large: over {} bytes", budget));
}
Ok(())
}
/// Score Setup (File › Score Setup…). Stored in the session's edit flags/values.
#[derive(Debug, Clone, PartialEq)]
pub struct ScoreSetup {
@@ -67,32 +60,6 @@ impl ScoreSetup {
}
}
/// One measure: its meter and its items (notes, chords or rests), in sixteenths.
#[derive(Debug, Clone, PartialEq)]
pub struct Measure {
pub numerator: u32,
pub denominator: u32,
pub items: Vec<Item>,
}
/// A notehead group (chord) or a rest (no pitches).
#[derive(Debug, Clone, PartialEq)]
pub struct Item {
pub pitches: Vec<u8>,
/// Duration in sixteenths; always one of [`VALUES`].
pub units: u32,
pub tie_start: bool,
pub tie_stop: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Part {
pub name: String,
/// True when the part reads best in bass clef.
pub bass: bool,
pub measures: Vec<Measure>,
}
/// Lay out the MIDI (and instrument) tracks `tracks` (all of them when empty).
pub fn layout(s: &Session, tracks: &[TrackId]) -> Vec<Part> {
let sr = s.sample_rate;
@@ -179,70 +146,106 @@ fn split_units(mut units: i64) -> Vec<u32> {
}
fn fill(bars: &[(i64, i64, u32, u32)], notes: &[(i64, i64, u8)]) -> Vec<Measure> {
// Onsets → chords, each held until its longest *retained* note ends or the next onset. A pitch
// the cap rejects must not extend the onset end.
let mut onsets: std::collections::BTreeMap<i64, (i64, Vec<u8>)> = std::collections::BTreeMap::new();
for (a, b, p) in notes {
let (ends, pitches) = onsets.entry(*a).or_insert((*b, Vec::new()));
let new_pitch = !pitches.contains(p);
if new_pitch && pitches.len() >= MAX_PITCHES_PER_CHORD {
// Chord full and this pitch is absent: it is dropped and must not extend the onset end.
#[derive(Default)]
struct Events {
starts: Vec<(usize, u8)>,
ends: Vec<(usize, u8)>,
}
let limit = bars.last().map_or(0, |(start, length, _, _)| start.saturating_add(*length));
let mut events: std::collections::BTreeMap<i64, Events> = std::collections::BTreeMap::new();
for (id, &(a, b, pitch)) in notes.iter().enumerate() {
if a >= b || a >= limit || b <= 0 || pitch > 127 {
continue;
}
// A retained pitch (already in the chord, or one the cap accepts) may extend the onset
// end, even when it reappears with a longer duration.
*ends = (*ends).max(*b);
if new_pitch {
pitches.push(*p);
}
events.entry(a.max(0)).or_default().starts.push((id, pitch));
events.entry(b.min(limit)).or_default().ends.push((id, pitch));
}
let starts: Vec<i64> = onsets.keys().copied().collect();
let mut spans: Vec<(i64, i64, Vec<u8>)> = Vec::new();
for (i, (a, (b, mut ps))) in onsets.into_iter().enumerate() {
let end = starts.get(i + 1).map_or(b, |n| b.min(*n));
ps.sort_unstable();
spans.push((a, end, ps));
let events: Vec<_> = events.into_iter().collect();
// Only admitted notes may end activity; rejected starts must not silence later notes.
let mut admitted = std::collections::BTreeSet::new();
let mut active = [0usize; 128];
let mut spans = Vec::new();
for pair in events.windows(2) {
let [(from, ev), (to, next)] = pair else { continue };
for (id, pitch) in &ev.ends {
if !admitted.remove(id) {
continue;
}
if let Some(count) = active.get_mut(usize::from(*pitch)) {
*count = count.saturating_sub(1);
}
}
let tied_in: Vec<u8> = active
.iter()
.enumerate()
.filter(|(p, n)| **n > 0 && !ev.starts.iter().any(|(_, pitch)| *pitch == *p as u8))
.map(|(p, _)| p as u8)
.collect();
for (id, pitch) in &ev.starts {
let idx = usize::from(*pitch);
if active[idx] == 0 && active.iter().filter(|n| **n > 0).count() >= MAX_PITCHES_PER_CHORD {
continue;
}
admitted.insert(*id);
active[idx] = active[idx].saturating_add(1);
}
let pitches: Vec<u8> = active.iter().enumerate().filter(|(_, n)| **n > 0).map(|(p, _)| p as u8).collect();
let mut ending = [0usize; 128];
for (id, pitch) in &next.ends {
if !admitted.contains(id) {
continue;
}
if let Some(count) = ending.get_mut(usize::from(*pitch)) {
*count = count.saturating_add(1);
}
}
let tied_out: Vec<u8> = pitches
.iter()
.copied()
.filter(|p| {
active.get(usize::from(*p)).zip(ending.get(usize::from(*p))).is_some_and(|(a, b)| a > b)
&& !next.starts.iter().any(|(_, pitch)| pitch == p)
})
.collect();
if !pitches.is_empty() {
spans.push((*from, *to, pitches, tied_in, tied_out));
}
}
let mut measures = Vec::new();
for (ms, ml, num, den) in bars {
let me = ms + ml;
let mut items = Vec::new();
let mut cursor = *ms;
let emit = |from: i64, to: i64, pitches: &[u8], tied_in: bool, tied_out: bool, items: &mut Vec<Item>| {
let emit = |from: i64, to: i64, pitches: &[u8], tied_in: &[u8], tied_out: &[u8], items: &mut Vec<Item>| {
let parts = split_units(to - from);
let n = parts.len();
for (k, u) in parts.into_iter().enumerate() {
let rest = pitches.is_empty();
items.push(Item {
pitches: pitches.to_vec(),
units: u,
tie_stop: !rest && (tied_in || k > 0),
tie_start: !rest && (tied_out || k + 1 < n),
tie_stop: pitches.iter().copied().filter(|p| k > 0 || tied_in.contains(p)).collect(),
tie_start: pitches.iter().copied().filter(|p| k + 1 < n || tied_out.contains(p)).collect(),
});
}
};
for (a, b, ps) in spans.iter().filter(|(a, b, _)| *b > *ms && *a < me) {
for (a, b, ps, tied_in, tied_out) in spans.iter().filter(|(a, b, ..)| *b > *ms && *a < me) {
let from = (*a).max(*ms).max(cursor);
let to = (*b).min(me);
if to <= from {
continue;
}
if from > cursor {
emit(cursor, from, &[], false, false, &mut items);
emit(cursor, from, &[], &[], &[], &mut items);
}
emit(from, to, ps, *a < *ms, *b > me, &mut items);
emit(from, to, ps, if from > *a { ps } else { tied_in }, if to < *b { ps } else { tied_out }, &mut items);
cursor = to;
// Cap items per measure to avoid pathological memory use.
if items.len() > MAX_ITEMS_PER_MEASURE {
break;
}
}
if cursor < me {
emit(cursor, me, &[], false, false, &mut items);
if cursor < me && items.len() <= MAX_ITEMS_PER_MEASURE {
emit(cursor, me, &[], &[], &[], &mut items);
}
// Cap the item count without shortening the measure: whatever suffix the cap discards is
// replaced with representable rests, so the bar's duration (and the SVG layout denominator)
// stays right. Shrinking the item list further makes room for those rests.
if items.len() > MAX_ITEMS_PER_MEASURE {
let bar_len = me.saturating_sub(*ms);
let mut keep = MAX_ITEMS_PER_MEASURE;
@@ -251,12 +254,11 @@ fn fill(bars: &[(i64, i64, u32, u32)], notes: &[(i64, i64, u8)]) -> Vec<Measure>
let rests = split_units(bar_len.saturating_sub(kept));
if keep.saturating_add(rests.len()) <= MAX_ITEMS_PER_MEASURE || keep == 0 {
items.truncate(keep);
items.extend(rests.into_iter().map(|units| Item { pitches: Vec::new(), units, tie_start: false, tie_stop: false }));
items.extend(rests.into_iter().map(|units| Item { pitches: Vec::new(), units, tie_start: Vec::new(), tie_stop: Vec::new() }));
break;
}
keep -= 1;
}
// Safety net for a hostile meter so wide that even whole-bar rests exceed the cap.
items.truncate(MAX_ITEMS_PER_MEASURE);
}
measures.push(Measure { numerator: *num, denominator: *den, items });
@@ -265,354 +267,120 @@ fn fill(bars: &[(i64, i64, u32, u32)], notes: &[(i64, i64, u8)]) -> Vec<Measure>
}
/// (step, alter, octave) spelled with sharps.
fn spell(pitch: u8) -> (char, i32, i32) {
const STEPS: [(char, i32); 12] =
[('C', 0), ('C', 1), ('D', 0), ('D', 1), ('E', 0), ('F', 0), ('F', 1), ('G', 0), ('G', 1), ('A', 0), ('A', 1), ('B', 0)];
let (step, alter) = STEPS[usize::from(pitch % 12)];
(step, alter, i32::from(pitch / 12) - 1)
}
fn xml_escape(s: &str) -> String {
// Keep the score export's existing literal whitespace and quote spelling. The
// shared attribute writer owns XML legality/escaping; only its generated
// whitespace references are restored (input ampersands were escaped first).
craft_xml::esc_attr(s).replace("&#9;", "\t").replace("&#10;", "\n").replace("&#13;", "\r")
}
fn note_type(units: u32) -> (&'static str, bool) {
match units {
16 => ("whole", false),
12 => ("half", true),
8 => ("half", false),
6 => ("quarter", true),
4 => ("quarter", false),
3 => ("eighth", true),
2 => ("eighth", false),
_ => ("16th", false),
fn shared_setup(setup: &ScoreSetup) -> craft_score::ScoreSetup {
craft_score::ScoreSetup {
software: "SoundCraft".into(),
title: setup.title.clone(),
composer: setup.composer.clone(),
bars_per_system: setup.bars_per_system,
show_track_names: setup.show_track_names,
}
}
/// MusicXML 4.0 (partwise) for `parts`.
/// MusicXML 4.0 (partwise), written by the shared score serializer.
pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> Result<String, String> {
musicxml_budgeted(parts, setup, MAX_OUTPUT_BYTES)
craft_score::musicxml(parts, &shared_setup(setup))
}
/// [`musicxml`] with an explicit byte budget (kept separate so tests can pass a tiny budget
/// instead of building a 256 MiB score).
fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result<String, String> {
let mut x = String::new();
x.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
x.push_str("<!DOCTYPE score-partwise PUBLIC \"-//Recordare//DTD MusicXML 4.0 Partwise//EN\" \"http://www.musicxml.org/dtds/partwise.dtd\">\n");
x.push_str("<score-partwise version=\"4.0\">\n");
// The header (and an oversized title/composer, escaped) can itself exceed the budget even with
// no measures, so check before the per-measure loop too.
over_budget(&x, budget)?;
let _ = writeln!(x, " <work><work-title>{}</work-title></work>", xml_escape(&setup.title));
x.push_str(" <identification>\n");
if !setup.composer.is_empty() {
let _ = writeln!(x, " <creator type=\"composer\">{}</creator>", xml_escape(&setup.composer));
}
x.push_str(" <encoding><software>SoundCraft</software></encoding>\n </identification>\n <part-list>\n");
for (i, p) in parts.iter().enumerate() {
let _ = writeln!(x, " <score-part id=\"P{}\"><part-name>{}</part-name></score-part>", i + 1, xml_escape(&p.name));
}
x.push_str(" </part-list>\n");
over_budget(&x, budget)?;
for (i, p) in parts.iter().enumerate() {
let _ = writeln!(x, " <part id=\"P{}\">", i + 1);
let mut meter = (0, 0);
for (mi, m) in p.measures.iter().enumerate() {
let _ = writeln!(x, " <measure number=\"{}\">", mi + 1);
if mi == 0 || meter != (m.numerator, m.denominator) {
x.push_str(" <attributes>");
if mi == 0 {
x.push_str("<divisions>4</divisions><key><fifths>0</fifths></key>");
}
let _ = write!(x, "<time><beats>{}</beats><beat-type>{}</beat-type></time>", m.numerator, m.denominator);
if mi == 0 {
x.push_str(if p.bass { "<clef><sign>F</sign><line>4</line></clef>" } else { "<clef><sign>G</sign><line>2</line></clef>" });
}
x.push_str("</attributes>\n");
meter = (m.numerator, m.denominator);
}
for it in &m.items {
let (ty, dot) = note_type(it.units);
if it.pitches.is_empty() {
let _ = writeln!(
x,
" <note><rest/><duration>{}</duration><voice>1</voice><type>{ty}</type>{}</note>",
it.units,
if dot { "<dot/>" } else { "" }
);
continue;
}
for (k, pitch) in it.pitches.iter().enumerate() {
let (step, alter, octave) = spell(*pitch);
x.push_str(" <note>");
if k > 0 {
x.push_str("<chord/>");
}
let _ = write!(x, "<pitch><step>{step}</step>");
if alter != 0 {
let _ = write!(x, "<alter>{alter}</alter>");
}
let _ = write!(x, "<octave>{octave}</octave></pitch><duration>{}</duration>", it.units);
if it.tie_stop {
x.push_str("<tie type=\"stop\"/>");
}
if it.tie_start {
x.push_str("<tie type=\"start\"/>");
}
let _ = write!(x, "<voice>1</voice><type>{ty}</type>");
if dot {
x.push_str("<dot/>");
}
if alter != 0 {
x.push_str("<accidental>sharp</accidental>");
}
if it.tie_stop || it.tie_start {
x.push_str("<notations>");
if it.tie_stop {
x.push_str("<tied type=\"stop\"/>");
}
if it.tie_start {
x.push_str("<tied type=\"start\"/>");
}
x.push_str("</notations>");
}
x.push_str("</note>\n");
}
}
x.push_str(" </measure>\n");
over_budget(&x, budget)?;
}
x.push_str(" </part>\n");
}
x.push_str("</score-partwise>\n");
over_budget(&x, budget)?;
Ok(x)
}
/// Staff position of `pitch` in half-spaces above the bottom line (treble E4 / bass G2).
fn staff_step(pitch: u8, bass: bool) -> i32 {
let (step, _, octave) = spell(pitch);
let idx = "CDEFGAB".find(step).map_or(0, |i| i as i32);
let diatonic = octave * 7 + idx;
diatonic - if bass { 2 * 7 + 4 } else { 4 * 7 + 2 }
}
/// An engraved score as a printable SVG page (A4 portrait width, as tall as needed).
/// A printable SVG page, written by the shared score serializer.
pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result<String, String> {
svg_budgeted(parts, setup, MAX_OUTPUT_BYTES)
craft_score::svg(parts, &shared_setup(setup))
}
/// [`svg`] with an explicit byte budget (kept separate so tests can pass a tiny budget instead of
/// building a 256 MiB score).
#[cfg(test)]
fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result<String, String> {
craft_score::musicxml_budgeted(parts, &shared_setup(setup), budget)
}
#[cfg(test)]
fn svg_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result<String, String> {
const W: f64 = 794.0;
const MARGIN: f64 = 48.0;
const SP: f64 = 7.0; // staff space
const STAFF_GAP: f64 = 70.0;
const SYSTEM_GAP: f64 = 40.0;
let n_measures = parts.iter().map(|p| p.measures.len()).max().unwrap_or(0);
// The layout range the setup offers (1 to 16): a caller's own value can't multiply the
// barlines drawn per system beyond it.
let per = setup.bars_per_system.clamp(1, 16) as usize;
let systems = n_measures.div_ceil(per).max(1);
let label_w = if setup.show_track_names { 70.0 } else { 0.0 };
let staff_x0 = MARGIN + label_w + 34.0;
let staff_x1 = W - MARGIN;
let sys_h = parts.len().max(1) as f64 * STAFF_GAP + SYSTEM_GAP;
let top = 110.0;
let h = top + systems as f64 * sys_h + MARGIN;
let mut o = String::new();
let _ =
writeln!(o, "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{W}\" height=\"{h:.0}\" viewBox=\"0 0 {W} {h:.0}\" font-family=\"serif\">");
let _ = writeln!(o, "<rect width=\"100%\" height=\"100%\" fill=\"white\"/>");
let _ = writeln!(o, "<text x=\"{}\" y=\"56\" font-size=\"24\" text-anchor=\"middle\">{}</text>", W / 2.0, xml_escape(&setup.title));
if !setup.composer.is_empty() {
let _ = writeln!(o, "<text x=\"{}\" y=\"84\" font-size=\"13\" text-anchor=\"end\">{}</text>", W - MARGIN, xml_escape(&setup.composer));
over_budget(&o, budget)?;
}
over_budget(&o, budget)?;
let bar_w = (staff_x1 - staff_x0) / per as f64;
for sys in 0..systems {
let y_sys = top + sys as f64 * sys_h;
for (pi, p) in parts.iter().enumerate() {
let y0 = y_sys + pi as f64 * STAFF_GAP; // top line
let bottom = y0 + 4.0 * SP;
if setup.show_track_names && sys == 0 {
let _ = writeln!(o, "<text x=\"{MARGIN}\" y=\"{:.1}\" font-size=\"11\">{}</text>", y0 + 2.0 * SP + 4.0, xml_escape(&p.name));
}
for l in 0..5 {
let y = y0 + f64::from(l) * SP;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{y:.1}\" x2=\"{staff_x1:.1}\" y2=\"{y:.1}\" stroke=\"black\" stroke-width=\"0.8\"/>",
staff_x0 - 30.0
);
}
let clef = if p.bass { "𝄢" } else { "𝄞" };
let _ = writeln!(
o,
"<text x=\"{:.1}\" y=\"{:.1}\" font-size=\"{}\">{clef}</text>",
staff_x0 - 29.0,
bottom + if p.bass { -SP } else { SP * 0.6 },
if p.bass { 26 } else { 36 }
);
for k in 0..per {
let mi = sys * per + k;
let x0 = staff_x0 + k as f64 * bar_w;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{y0:.1}\" x2=\"{:.1}\" y2=\"{bottom:.1}\" stroke=\"black\" stroke-width=\"1\"/>",
x0 + bar_w,
x0 + bar_w
);
let Some(m) = p.measures.get(mi) else { continue };
if pi == 0 {
let _ = writeln!(o, "<text x=\"{:.1}\" y=\"{:.1}\" font-size=\"9\">{}</text>", x0 + 2.0, y0 - 8.0, mi + 1);
}
let total: u32 = m.items.iter().map(|i| i.units).sum::<u32>().max(1);
let mut acc = 0u32;
for it in &m.items {
let x = x0 + 10.0 + (bar_w - 16.0) * f64::from(acc) / f64::from(total);
acc += it.units;
let (_, dot) = note_type(it.units);
if it.pitches.is_empty() {
rest(&mut o, x, y0, SP, it.units);
if dot {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"1.3\"/>", x + 7.0, y0 + 1.5 * SP);
}
continue;
}
let steps: Vec<i32> = it.pitches.iter().map(|p2| staff_step(*p2, p.bass)).collect();
let filled = it.units < 8;
for (pitch, st) in it.pitches.iter().zip(&steps) {
let y = bottom - f64::from(*st) * SP / 2.0;
// Ledger lines.
let mut l = if *st < 0 { -2 } else { 10 };
while (*st < 0 && l >= *st) || (*st > 8 && l <= *st) {
let ly = bottom - f64::from(l) * SP / 2.0;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{ly:.1}\" x2=\"{:.1}\" y2=\"{ly:.1}\" stroke=\"black\" stroke-width=\"0.8\"/>",
x - 7.0,
x + 7.0
);
l += if *st < 0 { -2 } else { 2 };
}
let fill = if filled { "black" } else { "white" };
let _ = writeln!(
o,
"<ellipse cx=\"{x:.1}\" cy=\"{y:.1}\" rx=\"4.6\" ry=\"3.3\" transform=\"rotate(-20 {x:.1} {y:.1})\" fill=\"{fill}\" stroke=\"black\" stroke-width=\"1.1\"/>"
);
if spell(*pitch).1 != 0 {
let _ = writeln!(o, "<text x=\"{:.1}\" y=\"{:.1}\" font-size=\"12\">♯</text>", x - 14.0, y + 4.0);
}
if dot {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"1.3\"/>", x + 8.0, y - 1.5);
}
}
if it.units < 16 {
let (lo, hi) = (steps.iter().copied().min().unwrap_or(4), steps.iter().copied().max().unwrap_or(4));
let up = (lo + hi) < 8;
let (sx, y_from, y_to) = if up {
(x + 4.3, bottom - f64::from(lo) * SP / 2.0, bottom - f64::from(hi) * SP / 2.0 - 3.5 * SP)
} else {
(x - 4.3, bottom - f64::from(hi) * SP / 2.0, bottom - f64::from(lo) * SP / 2.0 + 3.5 * SP)
};
let _ = writeln!(
o,
"<line x1=\"{sx:.1}\" y1=\"{y_from:.1}\" x2=\"{sx:.1}\" y2=\"{y_to:.1}\" stroke=\"black\" stroke-width=\"1\"/>"
);
let flags = match it.units {
1 => 2,
2 | 3 => 1,
_ => 0,
};
for f in 0..flags {
let fy = y_to + f64::from(f) * if up { 5.0 } else { -5.0 };
let dy = if up { 9.0 } else { -9.0 };
let _ = writeln!(
o,
"<path d=\"M{sx:.1} {fy:.1} q 7 {:.1} 5 {dy:.1}\" fill=\"none\" stroke=\"black\" stroke-width=\"1.4\"/>",
dy * 0.5
);
}
}
if it.tie_start {
let y = bottom - f64::from(steps[0]) * SP / 2.0 + 6.0;
let _ = writeln!(o, "<path d=\"M{:.1} {y:.1} q 8 5 16 0\" fill=\"none\" stroke=\"black\" stroke-width=\"0.9\"/>", x + 3.0);
}
}
// Check each measure, so a run of rest-only or missing measures that only draws
// bar lines cannot grow the output past the budget untouched.
over_budget(&o, budget)?;
}
// The staff markup (lines, clef, track names) itself grows with the part count even
// without any items, so check once per staff too.
over_budget(&o, budget)?;
}
// System bracket.
let y_last = y_sys + (parts.len().max(1) - 1) as f64 * STAFF_GAP + 4.0 * SP;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{y_sys:.1}\" x2=\"{:.1}\" y2=\"{y_last:.1}\" stroke=\"black\" stroke-width=\"1.5\"/>",
staff_x0 - 30.0,
staff_x0 - 30.0
);
over_budget(&o, budget)?;
}
o.push_str("</svg>\n");
over_budget(&o, budget)?;
Ok(o)
}
fn rest(o: &mut String, x: f64, y0: f64, sp: f64, units: u32) {
let mid = y0 + 2.0 * sp;
match units {
16 => {
let _ = writeln!(o, "<rect x=\"{:.1}\" y=\"{:.1}\" width=\"9\" height=\"{:.1}\"/>", x - 4.5, y0 + sp, sp / 2.0);
}
8 | 12 => {
let _ = writeln!(o, "<rect x=\"{:.1}\" y=\"{:.1}\" width=\"9\" height=\"{:.1}\"/>", x - 4.5, mid - sp / 2.0, sp / 2.0);
}
4 | 6 => {
let _ = writeln!(
o,
"<path d=\"M{:.1} {:.1} l 4 5 l -4 4 l 4 5 q -6 -2 -2 5\" fill=\"none\" stroke=\"black\" stroke-width=\"1.8\"/>",
x - 2.0,
mid - 9.0
);
}
_ => {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"2\"/>", x - 2.0, mid - 3.0);
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{:.1}\" x2=\"{:.1}\" y2=\"{:.1}\" stroke=\"black\" stroke-width=\"1.2\"/>",
x + 2.5,
mid - 4.0,
x - 1.0,
mid + 7.0
);
if units == 1 {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"2\"/>", x - 3.5, mid + 2.0);
}
}
}
craft_score::svg_budgeted(parts, &shared_setup(setup), budget)
}
#[cfg(test)]
mod tests {
use super::*;
proptest::proptest! {
#[test]
fn score_preserves_pitch_activity(notes in proptest::collection::vec((0i64..32, 1i64..32, 48u8..64), 0..20)) {
let notes: Vec<_> = notes.into_iter().map(|(a, length, pitch)| (a, a + length, pitch)).collect();
let measures = fill(&[(0, 16, 4, 4), (16, 16, 4, 4), (32, 16, 4, 4), (48, 16, 4, 4)], &notes);
let mut at = 0i64;
for m in measures {
proptest::prop_assert_eq!(m.items.iter().map(|i| i.units).sum::<u32>(), 16);
for item in m.items {
for tick in at..at + i64::from(item.units) {
let expected: Vec<u8> = (48..64).filter(|pitch| notes.iter().any(|(a, b, p)| p == pitch && *a <= tick && tick < *b)).collect();
proptest::prop_assert_eq!(&item.pitches, &expected);
}
let end = at + i64::from(item.units);
let tied_in: Vec<_> = item.pitches.iter().copied().filter(|p| {
notes.iter().any(|(a, b, pitch)| pitch == p && *a < at && at < *b)
&& !notes.iter().any(|(a, _, pitch)| pitch == p && *a == at)
}).collect();
let tied_out: Vec<_> = item.pitches.iter().copied().filter(|p| {
end < 64 && notes.iter().any(|(a, b, pitch)| pitch == p && *a < end && end < *b)
&& !notes.iter().any(|(a, _, pitch)| pitch == p && *a == end)
}).collect();
proptest::prop_assert_eq!(item.tie_stop, tied_in);
proptest::prop_assert_eq!(item.tie_start, tied_out);
at = end;
}
}
proptest::prop_assert_eq!(at, 64);
}
}
#[test]
fn shared_xml_escaping_preserves_score_bytes() {
let input = "<&>\"'\t\n\r\0\u{1f}\u{fffe}\u{ffff}é𝄞&#9;&#10;&#13;";
assert_eq!(xml_escape(input), "&lt;&amp;&gt;&quot;'\t\n\ré𝄞&amp;#9;&amp;#10;&amp;#13;");
assert_eq!(xml_escape(""), "");
assert_eq!(xml_escape("Already &amp; escaped"), "Already &amp;amp; escaped");
fn rejected_note_end_does_not_silence_later_admitted_pitch() {
let mut notes: Vec<_> = (48..64).map(|pitch| (0, 30, pitch)).collect();
notes.extend([(10, 50, 64), (30, 80, 64)]);
let measures = fill(&[(0, 80, 20, 4)], &notes);
let mut at = 0;
for item in &measures[0].items {
let end = at + i64::from(item.units);
for tick in at..end {
assert_eq!(item.pitches.contains(&64), (30..80).contains(&tick));
}
if item.pitches.contains(&64) {
assert_eq!(item.tie_stop.contains(&64), at > 30);
assert_eq!(item.tie_start.contains(&64), end < 80);
}
at = end;
}
assert_eq!(at, 80);
}
#[test]
fn unequal_chord_durations_are_preserved_in_layout_and_exports() {
let measures = fill(&[(0, 16, 4, 4)], &[(0, 4, 60), (0, 16, 64)]);
let items = &measures[0].items;
assert_eq!(items.iter().map(|i| i.units).sum::<u32>(), 16);
assert_eq!(items.iter().filter(|i| i.pitches.contains(&60)).map(|i| i.units).sum::<u32>(), 4);
assert_eq!(items.iter().filter(|i| i.pitches.contains(&64)).map(|i| i.units).sum::<u32>(), 16);
assert_eq!(items[0].tie_start, vec![64]);
assert_eq!(items[1].tie_stop, vec![64]);
let parts = vec![Part { name: "Keys".into(), bass: false, measures }];
let setup = ScoreSetup::from_session(&Session::default());
let xml = musicxml(&parts, &setup).unwrap();
let notes: Vec<_> = xml.split("<note>").skip(1).map(|n| n.split("</note>").next().unwrap()).collect();
assert!(notes[0].contains("<step>C</step>") && notes[0].contains("<duration>4</duration>"));
assert!(!notes[0].contains("<tie "));
assert!(notes[1].contains("<tie type=\"start\"/>") && notes[1].contains("<chord/>"));
assert!(notes[2].contains("<duration>12</duration>") && notes[2].contains("<tie type=\"stop\"/>"));
assert_eq!(svg(&parts, &setup).unwrap().matches("stroke-width=\"0.9\"").count(), 1);
}
#[test]
fn overlapping_notes_continue_and_repeated_notes_are_not_tied() {
let m = fill(&[(0, 16, 4, 4), (16, 16, 4, 4)], &[(0, 20, 60), (4, 8, 64), (20, 24, 60)]);
let items: Vec<_> = m.iter().flat_map(|m| &m.items).collect();
assert_eq!(items.iter().filter(|i| i.pitches.contains(&60)).map(|i| i.units).sum::<u32>(), 24);
assert_eq!(items.iter().filter(|i| i.pitches.contains(&64)).map(|i| i.units).sum::<u32>(), 4);
assert_eq!(m[1].items[0].tie_stop, vec![60]);
assert!(m[1].items[0].tie_start.is_empty());
assert!(m[1].items[1].tie_stop.is_empty());
}
#[test]
@@ -623,15 +391,6 @@ mod tests {
assert!(split_units(0).is_empty());
}
#[test]
fn spelling_and_staff_steps() {
assert_eq!(spell(60), ('C', 0, 4));
assert_eq!(spell(61), ('C', 1, 4));
assert_eq!(staff_step(64, false), 0); // E4: bottom treble line
assert_eq!(staff_step(77, false), 8); // F5: top treble line
assert_eq!(staff_step(43, true), 0); // G2: bottom bass line
}
#[test]
fn measures_fill_exactly_and_tie_across_bars() {
// 4/4: one bar = 16 units. A note from unit 12 to 20 crosses the bar line.
@@ -642,8 +401,8 @@ mod tests {
assert_eq!(meas.items.iter().map(|i| i.units).sum::<u32>(), 16);
}
assert_eq!(m[0].items[0].pitches, vec![60, 64]);
assert!(m[0].items.last().is_some_and(|i| i.tie_start));
assert!(m[1].items[0].tie_stop);
assert!(m[0].items.last().is_some_and(|i| !i.tie_start.is_empty()));
assert_eq!(m[1].items[0].tie_stop, vec![67]);
}
#[test]
@@ -661,7 +420,7 @@ mod tests {
/// Note onsets (pitch count of every chord that is not a tie continuation).
fn note_starts(parts: &[Part]) -> usize {
parts.iter().flat_map(|p| &p.measures).flat_map(|m| &m.items).filter(|i| !i.tie_stop).map(|i| i.pitches.len()).sum()
parts.iter().flat_map(|p| &p.measures).flat_map(|m| &m.items).filter(|i| i.tie_stop.is_empty()).map(|i| i.pitches.len()).sum()
}
#[test]
-1
View File
@@ -18,7 +18,6 @@ craft-media.workspace = true
craft-video.workspace = true
thiserror.workspace = true
serde.workspace = true
image = { workspace = true, features = ["jpeg"] }
[dev-dependencies]
proptest.workspace = true
+29 -14
View File
@@ -418,20 +418,14 @@ impl Movie {
Ok(Frame { width: f.width, height: f.height, rgba, index, time })
}
Codec::Jpeg => {
// The frame's own size, from its header, before a pixel is decoded: a frame may differ from
// the track's size (as before), but not exceed `MAX_PIXELS`.
let reader = image::ImageReader::with_format(std::io::Cursor::new(&data), image::ImageFormat::Jpeg);
let (w, h) = reader.into_dimensions().map_err(|e| VideoError::Decode(e.to_string()))?;
if u64::from(w) * u64::from(h) > MAX_PIXELS {
return Err(VideoError::Decode("picture too large".into()));
}
let img = image::load_from_memory_with_format(&data, image::ImageFormat::Jpeg).map_err(|e| VideoError::Decode(e.to_string()))?;
let rgba = img.to_rgba8();
let (width, height) = rgba.dimensions();
if u64::from(width) * u64::from(height) > MAX_PIXELS {
return Err(VideoError::Decode("picture too large".into()));
}
Ok(Frame { width, height, rgba: rgba.into_raw(), index, time })
// The frame's own size is checked from its header before a pixel is decoded: a frame may
// differ from the track's size (as before), but not exceed `MAX_PIXELS`.
let decoded = craft_video::mjpeg::Decoder::new().max_pixels(MAX_PIXELS).decode(&data).map_err(|e| match e {
craft_video::mjpeg::Error::Limit(_) => VideoError::Decode("picture too large".into()),
e => VideoError::Decode(e.to_string()),
})?;
let rgba = mjpeg_rgba(&decoded)?;
Ok(Frame { width: decoded.width, height: decoded.height, rgba, index, time })
}
_ => Err(VideoError::Unsupported("not an intra-frame codec".into())),
}
@@ -563,3 +557,24 @@ impl Movie {
Err(VideoError::Decode(last_err.unwrap_or_else(|| format!("frame {index} was not produced"))))
}
}
/// A decoded Motion JPEG picture as RGBA8: grey is spread over R, G and B, and 12-bit samples
/// (scaled to 16 bits by the decoder) keep their top 8 bits.
pub(crate) fn mjpeg_rgba(frame: &craft_video::mjpeg::Frame) -> Result<Vec<u8>> {
let channels = usize::from(frame.channels);
if channels != 1 && channels != 3 {
return Err(VideoError::Decode(format!("{channels}-channel Motion JPEG picture")));
}
let samples: Vec<u8> = match &frame.samples {
craft_video::mjpeg::Samples::U8(p) => p.clone(),
craft_video::mjpeg::Samples::U16(p) => p.iter().map(|&v| (v >> 8) as u8).collect(),
};
Ok(samples
.chunks_exact(channels)
.flat_map(|px| match *px {
[g] => [g, g, g, 255],
[r, g, b] => [r, g, b, 255],
_ => [0, 0, 0, 255],
})
.collect())
}
+4 -4
View File
@@ -1,6 +1,6 @@
//! Tiny synthetic movies, generated in code (for tests, demos and screenshots; nothing binary is
//! committed). H.264 pictures are made of I_PCM macroblocks, so their decoded samples are known
//! exactly; ProRes comes from the bundled encoder and Motion JPEG from the `image` encoder. All
//! exactly; ProRes comes from the bundled encoder and Motion JPEG from craft-video's `mjpeg` encoder. All
//! are muxed with the bundled MP4/MOV writer.
use crate::bitstream::{BitWriter, escape_rbsp};
@@ -174,10 +174,10 @@ pub fn mjpeg_mov(n: usize) -> Result<Vec<u8>> {
let (w, h) = (32u32, 24u32);
let mut mw = Mp4Writer::new(Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).map_err(err)?;
let t = mw.add_track(TrackConfig::new(SampleEntry::jpeg(w as u16, h as u16), 10)).map_err(err)?;
let encoder = craft_video::mjpeg::Encoder::new(craft_video::mjpeg::EncoderConfig { quality: 95, ..Default::default() }).map_err(err)?;
for i in 0..n.min(10_000) {
let img = image::RgbImage::from_pixel(w, h, image::Rgb([(30 * (i % 9)) as u8; 3]));
let mut jpg = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpg, 95).encode_image(&img).map_err(err)?;
let pixels = vec![(30 * (i % 9)) as u8; (w * h * 3) as usize];
let jpg = encoder.encode(w, h, 3, &pixels).map_err(err)?;
mw.write_sample(t, WriteSample { data: &jpg, duration: 1, composition_offset: 0, is_sync: true }).map_err(err)?;
}
Ok(mw.finish().map_err(err)?.into_inner())
+25 -2
View File
@@ -1,6 +1,6 @@
//! Movie tests on fixtures generated in process (no binary fixtures): H.264 made of I_PCM
//! macroblocks (exact, known colours), ProRes from the bundled encoder and Motion JPEG from the
//! `image` encoder, muxed with the bundled MP4/MOV writer. When `ffmpeg` with libx264 is on PATH,
//! macroblocks (exact, known colours), ProRes from the bundled encoder and Motion JPEG from craft-video's
//! `mjpeg` encoder, muxed with the bundled MP4/MOV writer. When `ffmpeg` with libx264 is on PATH,
//! real High-profile streams with B-frames are checked too (skipped otherwise).
use crate::isobmff::{Brand, ByteSource, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions};
@@ -345,3 +345,26 @@ fn test_pattern_moves() {
let f17 = m.frame_at(17.5 / 24.0).unwrap();
assert_eq!(white_x(&f17), Some(17));
}
#[test]
fn mjpeg_grey_twelve_bit_and_odd_channel_pictures_map_to_rgba() {
use craft_video::mjpeg::{Frame as Jpeg, Samples};
let grey = Jpeg { width: 2, height: 1, channels: 1, samples: Samples::U8(vec![7, 200]), damaged: false };
assert_eq!(crate::movie::mjpeg_rgba(&grey).unwrap(), [7, 7, 7, 255, 200, 200, 200, 255]);
// 12-bit samples arrive scaled to 16 bits; the top 8 bits are kept.
let deep = Jpeg { width: 1, height: 1, channels: 3, samples: Samples::U16(vec![0xFFFF, 0x8000, 0x00FF]), damaged: false };
assert_eq!(crate::movie::mjpeg_rgba(&deep).unwrap(), [255, 128, 0, 255]);
let odd = Jpeg { width: 1, height: 1, channels: 2, samples: Samples::U8(vec![1, 2]), damaged: false };
assert!(matches!(crate::movie::mjpeg_rgba(&odd), Err(VideoError::Decode(e)) if e.contains("2-channel")));
}
#[test]
fn grey_mjpeg_packets_decode_through_the_shared_decoder() {
// A one-channel packet, as some cameras write, decodes to equal R, G and B.
let enc = craft_video::mjpeg::Encoder::new(craft_video::mjpeg::EncoderConfig { quality: 95, ..Default::default() }).unwrap();
let packet = enc.encode(16, 8, 1, &[90; 16 * 8]).unwrap();
let frame = craft_video::mjpeg::Decoder::new().decode(&packet).unwrap();
let rgba = crate::movie::mjpeg_rgba(&frame).unwrap();
assert_eq!(rgba.len(), 16 * 8 * 4);
assert!(rgba.as_chunks::<4>().0.iter().all(|p| p[0] == p[1] && p[1] == p[2] && p[0].abs_diff(90) <= 2 && p[3] == 255));
}
+2
View File
@@ -80,6 +80,8 @@ link stays. An existing file that is not an input is replaced without asking.
|---|---|---|
| `SOUNDCRAFT_CONTROL_PORT` | `app --port` (CLI), `--control` (desktop app) | A port or `host:port`, an IPv6 host in brackets (`[::1]:7979`). The desktop app takes the port of a loopback `host:port` (`127.0.0.1`, `localhost`). Its control server listens on `127.0.0.1` only, so it ignores another host from the variable, with a log warning, and starts no control server; given to `--control`, another host is a usage error |
| `SOUNDCRAFT_CONTROL_TOKEN` | none | The secret every control request carries as `token`. The desktop app refuses requests without it; unset, it makes one per launch and prints it after the port line (`SOUNDCRAFT_CONTROL_TOKEN=…`). `app` and `mcp --connect` send the variable's value |
- A flag wins over its variable.
- An empty variable counts as unset.
- A bad value is a usage error that names the variable.
+8 -2
View File
@@ -6,7 +6,7 @@ input), and every command is reachable from:
| Surface | How |
|---|---|
| Desktop app control channel | `soundcraft --control <port>` (`0` = pick a free port; printed as `SOUNDCRAFT_CONTROL_PORT=…`) |
| Desktop app control channel | `soundcraft --control <port>` (`0` = pick a free port; printed as `SOUNDCRAFT_CONTROL_PORT=…`, then the launch's `SOUNDCRAFT_CONTROL_TOKEN=…` unless the variable was set) |
| CLI against a running app | `soundcraft-cli app --port <port> <method or command id> [JSON]` (see [cli.md](cli.md)) |
| Headless CLI | `soundcraft-cli run --demo --cmd 'mix.volume={"track":"Kick","db":-6}' --bounce out.wav` |
| MCP (headless) | `soundcraft-cli mcp [--demo]` |
@@ -29,12 +29,17 @@ input delivery or screen-reader acceptance. Native screenshots use the presented
Newline-delimited JSON over TCP on `127.0.0.1`. One request per line, one reply per line:
```json
{"id": 1, "method": "engine.execute", "params": {"command": "track.new", "params": {"count": 2, "format": "Stereo"}}}
{"id": 1, "method": "engine.execute", "params": {"command": "track.new", "params": {"count": 2, "format": "Stereo"}}, "token": "…"}
{"id": 1, "ok": true, "result": {"tracks": [12, 13]}}
```
Errors: `{"id": 1, "ok": false, "error": "bad parameters for `mix.volume`: no track named `Nope`"}`. A line that is not a JSON object with a string `method` gets one error reply, and the connection is closed.
`token` is the launch's secret: `SOUNDCRAFT_CONTROL_TOKEN` when the app was started with it, otherwise
the one the app made and printed after the port line. Any local account can reach the loopback port,
so a request without the right token is refused (`"error": "unauthorized: …"`) and its connection
closed. `soundcraft-cli app` and `mcp --connect` read the variable.
## Methods
| Method | Params | What it does |
@@ -71,6 +76,7 @@ Scripted input (`ui.click`, `ui.drag`, `ui.scroll`, `ui.zoom`, `ui.key`, `ui.tex
## Examples
```sh
export SOUNDCRAFT_CONTROL_TOKEN=$(head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')
P=$(soundcraft --demo --control 0 | sed -n 's/SOUNDCRAFT_CONTROL_PORT=//p') # or read stderr
soundcraft-cli app --port $P mix.solo '{"track":"Bass"}'
soundcraft-cli app --port $P transport.play
+1
View File
@@ -8,6 +8,7 @@ Claude and other agents can make music with it. The server is the `mcp` subcomma
soundcraft-cli mcp # headless engine (no window, no audio device)
soundcraft-cli mcp --demo # headless, starting from the demo session
soundcraft-cli mcp --automation-read-root DIR --automation-write-root DIR # headless, confined to folders
export SOUNDCRAFT_CONTROL_TOKEN=… # the secret both sides share (see control-protocol.md)
soundcraft --control 7801 & # the desktop app…
soundcraft-cli mcp --connect 7801 # …driven over MCP: same tools plus screenshots and clicks
```