From b354ea9f882a2b2938b4af133f84cbc43f197959 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 11 Oct 2026 09:06:33 +0000 Subject: [PATCH] Replace sheet reading with a fingering editor. Drag Baroque charts onto a line, reorder them, set a duration, or remove a note. The AppImage no longer packages oemer, ONNX, or the models. Version 0.2.0. Co-authored-by: catpersonisme --- README.md | 42 ++-- electron-builder.yml | 2 - electron/main.cjs | 14 +- package-lock.json | 125 +---------- package.json | 9 +- scripts/build-appimage.sh | 104 +-------- scripts/patch_oemer_unet.py | 115 ---------- scripts/run_oemer.py | 84 ------- src/app/api/recognize/route.ts | 52 ----- src/app/layout.tsx | 4 +- src/app/page.tsx | 4 +- src/components/converter.tsx | 272 ----------------------- src/components/editor.tsx | 385 +++++++++++++++++++++++++++++++++ src/components/note-editor.tsx | 285 ------------------------ src/components/tab-score.tsx | 273 ----------------------- src/lib/line.ts | 99 +++++++++ src/lib/musicxml.ts | 338 ----------------------------- src/lib/recognize.ts | 201 ----------------- src/lib/samples.ts | 92 -------- src/lib/text-tab.ts | 38 ---- tests/converter.test.ts | 149 +++++-------- tests/fixtures/lead.musicxml | 113 ---------- 22 files changed, 569 insertions(+), 2231 deletions(-) delete mode 100644 scripts/patch_oemer_unet.py delete mode 100755 scripts/run_oemer.py delete mode 100644 src/app/api/recognize/route.ts delete mode 100644 src/components/converter.tsx create mode 100644 src/components/editor.tsx delete mode 100644 src/components/note-editor.tsx delete mode 100644 src/components/tab-score.tsx create mode 100644 src/lib/line.ts delete mode 100644 src/lib/musicxml.ts delete mode 100644 src/lib/recognize.ts delete mode 100644 src/lib/samples.ts delete mode 100644 src/lib/text-tab.ts delete mode 100644 tests/fixtures/lead.musicxml diff --git a/README.md b/README.md index 6719290..944809d 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,23 @@ -# Recorder Tab Converter +# Recorder fingering editor -A soprano recorder tool: a photo or scan of sheet music becomes a lead-line fingering tab, with note values, rests, and bar lines still visible. Baroque fingering is the default. A German toggle changes only F, F♯, and B♭. +A soprano recorder tool for arranging fingering charts. Drag a chart onto a line, reorder it, set how long the note lasts, or take it off. Baroque fingering is the chart. The foot, with the double holes, is at the top of each chart, and the mouthpiece is at the bottom. -The usual way to run it is one Linux AppImage. That file contains the window, the fingering charts, the sample tunes, and [oemer](https://github.com/BreezeWhite/oemer) for offline reading. Nothing is uploaded and no account is required. +The usual way to run it is one Linux AppImage. That file contains the window and the fingering charts. It does not read sheet music, and it does not include oemer, ONNX, or model files. ## Run the AppImage ```bash -chmod +x Recorder-Tab-Converter-0.1.2.AppImage -./Recorder-Tab-Converter-0.1.2.AppImage +chmod +x Recorder-Tab-Converter-0.2.0.AppImage +./Recorder-Tab-Converter-0.2.0.AppImage ``` If the file will not start because FUSE is missing: ```bash -APPIMAGE_EXTRACT_AND_RUN=1 ./Recorder-Tab-Converter-0.1.2.AppImage +APPIMAGE_EXTRACT_AND_RUN=1 ./Recorder-Tab-Converter-0.2.0.AppImage ``` -Open **Twinkle, Twinkle, Little Star** or **Ode to Joy** to see tabs immediately. Or drop a PNG, JPG, or WEBP of a page. The note list under the tab is editable. Fix a bad pitch or duration and the fingerings update. If a photo cannot be read, the app says so and does not invent notes. +Open **Empty line**, or start from **Twinkle, opening** or **Ode, held note**. Drag any chart from C5 to D7 onto the line. Drag a placed chart to move it. Choose a duration under the chart. Remove takes a note off. ## Build the AppImage @@ -27,15 +27,13 @@ From a checkout, one command: ./scripts/build-appimage.sh ``` -That installs Node packages, downloads a relocatable Python plus oemer and its ONNX models, builds the app, and writes: +That installs Node packages, builds the app, and writes: ```text -dist/Recorder-Tab-Converter-0.1.2.AppImage +dist/Recorder-Tab-Converter-0.2.0.AppImage ``` -The U-Net checkpoint from oemer 0.1.8 has ConvTranspose pads `[0, 0, -1, -1]`. The build rewrites those nodes to pads `[0, 0, 0, 0]` and `output_padding` `[1, 1]` before packaging. That is the same output size, and it is what current ONNX Runtime will load. The rewrite does not change fingerings. - -The build needs network access the first time. The AppImage is not committed; keep it under 600MB. +The AppImage is not committed. Keep it under 600MB. ## Develop in a browser @@ -45,24 +43,10 @@ npm test npm run dev ``` -The dev server listens on [http://127.0.0.1:3847](http://127.0.0.1:3847). Photo reading uses the Python at `OEMER_PYTHON`, which `npm run dev` points at `build/python` from the AppImage build. That prefix needs CPU `onnxruntime`, not `onnxruntime-gpu`. +The dev server listens on [http://127.0.0.1:3847](http://127.0.0.1:3847). ## Fingerings -Baroque (English) fingerings are the first choice on the [Dolmetsch Online chart for recorders in C](https://dolmetsch.com/textfingeringchart.htm) (Brian Blood). The soprano is treated in concert pitch: it sounds as written, low C is C5, and the chart stops at D7. Double holes 6 and 7, forked F, and the second-octave thumb pinch are included. Alternate fingerings on that chart are not shown. Notes outside C5–D7 are flagged instead of guessed. +Baroque (English) fingerings are the first choice on the [Dolmetsch Online chart for recorders in C](https://dolmetsch.com/textfingeringchart.htm) (Brian Blood). The soprano is treated in concert pitch: it sounds as written, low C is C5, and the chart stops at D7. Double holes 6 and 7, forked F, and the second-octave thumb pinch are included. Alternate fingerings on that chart are not shown. -Each chart is a narrow recorder drawn foot-up: holes 7 and 6 at the top, then 5 through 1, then the thumb, with the mouthpiece at the bottom. A filled hole is closed. The lead is a horizontal row of those charts, grouped by bar. An arrow after a chart is one more beat. A ×2 mark appears only when the music has a repeat. - -German mode changes three notes: - -- **F** — Dolmetsch’s German fingering, thumb plus holes 1–4 (`0 123 4`). -- **F♯** — thumb, 1, 2, 3, 5, 6, and half-hole 7. -- **B♭** — thumb, 1, 3, 4, and 6. - -The F♯ and B♭ shapes follow the school German description in Zach VanderGraaff’s [Baroque vs German recorder](https://dynamicmusicroom.com/baroque-vs-german-recorder/) article. The octave above uses those same front holes with the Dolmetsch second-octave thumb. Every other note stays Baroque. - -## Recognition limits - -Reading is done locally by oemer 0.1.8 (ONNX Runtime on CPU). It looks for one lead line: the first part, the top staff, and the highest note of a chord. It misses notes, especially on phone photos, dense piano scores, and handwriting. The result is a draft in the note list. A failed read leaves that list usable and does not fill in a melody. - -Lead-only means other parts, lower staves, and lower chord tones are dropped on purpose. Grace notes are omitted so the beat count stays put. Tied notes are shown separately so bar lines do not slide. +Each chart is drawn foot-up: holes 7 and 6 at the top, then 5 through 1, then the thumb, with the mouthpiece at the bottom. A filled hole is closed. An arrow after a placed chart is one more beat in 4/4. diff --git a/electron-builder.yml b/electron-builder.yml index ede3712..1fe35b2 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -16,8 +16,6 @@ extraResources: to: standalone - from: .next/standalone/node_modules to: standalone/node_modules - - from: build/python - to: python linux: target: - AppImage diff --git a/electron/main.cjs b/electron/main.cjs index efd8daf..1ebd0a6 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -48,12 +48,11 @@ function stopServer() { async function open() { const resources = process.resourcesPath; const standalone = path.join(resources, "standalone"); - const python = path.join(resources, "python", "bin", "python3"); const serverJs = path.join(standalone, "server.js"); - if (!fs.existsSync(serverJs) || !fs.existsSync(python)) { + if (!fs.existsSync(serverJs)) { dialog.showErrorBox( - "Recorder Tab Converter", - "The bundled app or the local reader is missing from this AppImage.", + "Recorder fingering editor", + "The bundled app is missing from this AppImage.", ); app.quit(); return; @@ -69,7 +68,6 @@ async function open() { ELECTRON_RUN_AS_NODE: "1", PORT: String(port), HOSTNAME: "127.0.0.1", - OEMER_PYTHON: python, NODE_ENV: "production", }, stdio: ["ignore", "pipe", "pipe"], @@ -85,7 +83,7 @@ async function open() { await waitFor(url, 45_000); } catch (error) { dialog.showErrorBox( - "Recorder Tab Converter", + "Recorder fingering editor", `${error instanceof Error ? error.message : "The app server did not start."}\nLog: ${logPath}`, ); stopServer(); @@ -99,7 +97,7 @@ async function open() { minWidth: 390, minHeight: 640, show: false, - title: "Recorder Tab Converter", + title: "Recorder fingering editor", backgroundColor: "#f3ecdf", autoHideMenuBar: true, webPreferences: { @@ -115,7 +113,7 @@ async function open() { } app.whenReady().then(open).catch((error) => { - dialog.showErrorBox("Recorder Tab Converter", error instanceof Error ? error.message : String(error)); + dialog.showErrorBox("Recorder fingering editor", error instanceof Error ? error.message : String(error)); app.quit(); }); diff --git a/package-lock.json b/package-lock.json index df76635..7ae7285 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,16 @@ { "name": "recorder-tab-converter", - "version": "0.1.2", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "recorder-tab-converter", - "version": "0.1.2", + "version": "0.2.0", "dependencies": { "@base-ui/react": "^1.8.0", "class-variance-authority": "^0.7.1", "cn": "^0.4.0", - "fast-xml-parser": "^5.11.2", "lucide-react": "^1.53.0", "next": "16.4.0", "next-themes": "^0.4.6", @@ -2200,18 +2199,6 @@ "url": "https://paulmillr.com/funding/" } }, - "node_modules/@nodable/entities": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/@nodable/entities/-/entities-3.1.0.tgz", - "integrity": "sha512-LsS/DjHr+uDM647Gru/cA8+J3a3HfhttwCKLyuoyN7yXTFCxKBtSgMQBvrW9yNPa2/zDRUNuGPaH5QUFoa4arQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/nodable" - } - ], - "license": "MIT" - }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -4134,18 +4121,6 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, - "node_modules/anynum": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/anynum/-/anynum-1.0.1.tgz", - "integrity": "sha512-N6//FLET/tXYNM/F6ABca1oH6fWB+KlTt909Le28WMDBk8oaT4vY17DCrwg2MvmuqUKt3Ni4N5dGJ/EoBgcO6A==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" - } - ], - "license": "MIT" - }, "node_modules/app-builder-lib": { "version": "26.15.3", "resolved": "https://registry.npmjs.org/app-builder-lib/-/app-builder-lib-26.15.3.tgz", @@ -7019,45 +6994,6 @@ ], "license": "BSD-3-Clause" }, - "node_modules/fast-xml-builder": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/fast-xml-builder/-/fast-xml-builder-1.3.1.tgz", - "integrity": "sha512-pIM/1n3ntFXKYrUZwW7QCK0gAW7XY+wzj1YMIV3tLDvPj/V+zTGJK5e3/4WJfwj0qWw2ElNXiTixda/R+3YSug==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" - } - ], - "license": "MIT", - "dependencies": { - "path-expression-matcher": "^1.6.2", - "xml-naming": "^0.3.0" - } - }, - "node_modules/fast-xml-parser": { - "version": "5.11.2", - "resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-5.11.2.tgz", - "integrity": "sha512-R9iDuNrQYeQut46cn2r2wHKn4HYzVDvDm5J1wW+koZewykv0yuO2HChTYeZtrULyHIDM9cj9TUXloCsueCQUog==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" - } - ], - "license": "MIT", - "dependencies": { - "@nodable/entities": "^3.0.1", - "fast-xml-builder": "^1.2.0", - "is-unsafe": "^2.0.0", - "path-expression-matcher": "^1.6.2", - "strnum": "^2.4.2", - "xml-naming": "^0.3.0" - }, - "bin": { - "fxparser": "src/cli/cli.js" - } - }, "node_modules/fastq": { "version": "1.20.3", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.20.3.tgz", @@ -8481,18 +8417,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/is-unsafe": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/is-unsafe/-/is-unsafe-2.0.2.tgz", - "integrity": "sha512-HgbIHPBH0KHHCcjLfGsCvhtPTVxjaAZlXjwdz7/GQC40SjSe4sfQsar8J5VFo8JOSbarkpV0OLG95bbaNd9aAQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" - } - ], - "license": "MIT" - }, "node_modules/is-weakmap": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/is-weakmap/-/is-weakmap-2.0.2.tgz", @@ -10152,21 +10076,6 @@ "node": ">=8" } }, - "node_modules/path-expression-matcher": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/path-expression-matcher/-/path-expression-matcher-1.6.2.tgz", - "integrity": "sha512-enSlaiat05iasnzmgNxRj8reFdj3puY2QpNgP1aPIaVfT6nn9ICuPoFlKHk8EN22HcwewshO+mN2DGbkCEOtqQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" - } - ], - "license": "MIT", - "engines": { - "node": ">=14.0.0" - } - }, "node_modules/path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -12028,21 +11937,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/strnum": { - "version": "2.4.2", - "resolved": "https://registry.npmjs.org/strnum/-/strnum-2.4.2.tgz", - "integrity": "sha512-rDG3Ah4TV0k1hWvLSzkZtMmLN9+eS+h3knq4MP6A42Y3Yh5qGNnOUs1jJkoSr8FG5dsL28c7KgkIBzSEykqtuw==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" - } - ], - "license": "MIT", - "dependencies": { - "anynum": "^1.0.1" - } - }, "node_modules/styled-jsx": { "version": "5.1.6", "resolved": "https://registry.npmjs.org/styled-jsx/-/styled-jsx-5.1.6.tgz", @@ -13495,21 +13389,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/xml-naming": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/xml-naming/-/xml-naming-0.3.0.tgz", - "integrity": "sha512-ghig2TBE/H11aOVgmahA3MhimvkBr6JIYknH/Dhdk10nXwdbIqBJsbfMxpvFPG8bAw77gN29aQWvKpmVoPlvPQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" - } - ], - "license": "MIT", - "engines": { - "node": ">=16.0.0" - } - }, "node_modules/xmlbuilder": { "version": "15.1.1", "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-15.1.1.tgz", diff --git a/package.json b/package.json index d1c7c34..57b61bd 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "recorder-tab-converter", - "version": "0.1.2", + "version": "0.2.0", "private": true, - "description": "Turn a sheet-music image into soprano recorder fingering tabs, with rhythm kept.", + "description": "Arrange soprano recorder fingering charts by dragging them onto a line.", "main": "electron/main.cjs", "scripts": { - "dev": "OEMER_PYTHON=\"${OEMER_PYTHON:-$PWD/build/python/bin/python3}\" next dev -H 0.0.0.0 -p 3847", + "dev": "next dev -H 0.0.0.0 -p 3847", "build": "next build", - "start": "OEMER_PYTHON=\"${OEMER_PYTHON:-$PWD/build/python/bin/python3}\" next start -H 0.0.0.0 -p 3847", + "start": "next start -H 0.0.0.0 -p 3847", "lint": "eslint", "test": "vitest run", "appimage": "bash scripts/build-appimage.sh" @@ -16,7 +16,6 @@ "@base-ui/react": "^1.8.0", "class-variance-authority": "^0.7.1", "cn": "^0.4.0", - "fast-xml-parser": "^5.11.2", "lucide-react": "^1.53.0", "next": "16.4.0", "next-themes": "^0.4.6", diff --git a/scripts/build-appimage.sh b/scripts/build-appimage.sh index f5e6fe5..0fa132f 100755 --- a/scripts/build-appimage.sh +++ b/scripts/build-appimage.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# One-command AppImage build. Needs network the first time (Python, models, Electron). +# One-command AppImage of the fingering editor. No Python, oemer, or ONNX models. set -euo pipefail cd "$(dirname "$0")/.." ROOT="$(pwd)" @@ -7,113 +7,17 @@ ROOT="$(pwd)" echo "== Install Node packages ==" npm install -mkdir -p "$ROOT/build/cache" -TARBALL="$ROOT/build/cache/cpython.tar.gz" -URL="https://github.com/astral-sh/python-build-standalone/releases/download/20261003/cpython-3.12.15%2B20261003-x86_64-unknown-linux-gnu-install_only.tar.gz" - -PY="$ROOT/build/python/bin/python3" -if ! "$PY" -c "import oemer.ete, onnxruntime" >/dev/null 2>&1; then - echo "== Download a relocatable Python and oemer ==" - if [[ ! -f "$TARBALL" ]]; then - curl -fL --retry 3 -o "$TARBALL" "$URL" - fi - rm -rf "$ROOT/build/python" - tar -xzf "$TARBALL" -C "$ROOT/build" - "$PY" -m pip install --upgrade pip - "$PY" -m pip install \ - "opencv-python-headless>=4.10,<5" \ - "onnxruntime>=1.20" \ - numpy scipy scikit-learn pillow matplotlib typing_extensions - "$PY" -m pip install --no-deps oemer -fi - -SITE="$("$PY" -c "import oemer, pathlib; print(pathlib.Path(oemer.__file__).parent)")" -mkdir -p "$SITE/checkpoints/unet_big" "$SITE/checkpoints/seg_net" -if [[ ! -f "$SITE/checkpoints/unet_big/model.onnx" ]]; then - curl -fL --retry 3 -o "$SITE/checkpoints/unet_big/model.onnx" \ - "https://github.com/BreezeWhite/oemer/releases/download/checkpoints/1st_model.onnx" -fi -if [[ ! -f "$SITE/checkpoints/seg_net/model.onnx" ]]; then - curl -fL --retry 3 -o "$SITE/checkpoints/seg_net/model.onnx" \ - "https://github.com/BreezeWhite/oemer/releases/download/checkpoints/2nd_model.onnx" -fi - -echo "== Rewrite U-Net ConvTranspose pads ==" -# oemer 0.1.8's unet has ConvTranspose pads [0, 0, -1, -1]. ONNX Runtime -# 1.30+ rejects those. The rewrite keeps the same output size. onnx itself -# is not packaged. -if ! "$PY" -m pip --version >/dev/null 2>&1; then - if [[ ! -f "$TARBALL" ]]; then - echo "pip was removed and the Python tarball is missing, so the U-Net cannot be rewritten." >&2 - exit 1 - fi - PIP_RESTORE="$ROOT/build/cache/pip-restore" - rm -rf "$PIP_RESTORE" - mkdir -p "$PIP_RESTORE" - tar -xzf "$TARBALL" -C "$PIP_RESTORE" --wildcards \ - 'python/lib/python3.12/site-packages/pip' \ - 'python/lib/python3.12/site-packages/pip-*.dist-info' - rm -rf "$ROOT/build/python/lib/python3.12/site-packages/pip" \ - "$ROOT/build/python/lib/python3.12/site-packages/pip-"*.dist-info - cp -a "$PIP_RESTORE/python/lib/python3.12/site-packages/pip" \ - "$ROOT/build/python/lib/python3.12/site-packages/" - cp -a "$PIP_RESTORE"/python/lib/python3.12/site-packages/pip-*.dist-info \ - "$ROOT/build/python/lib/python3.12/site-packages/" - rm -rf "$PIP_RESTORE" -fi -PATCH_PY="$ROOT/build/cache/onnx-patch" -rm -rf "$PATCH_PY" -"$PY" -m pip install --disable-pip-version-check --target "$PATCH_PY" 'onnx>=1.16,<2' -PYTHONPATH="$PATCH_PY${PYTHONPATH:+:$PYTHONPATH}" \ - "$PY" "$ROOT/scripts/patch_oemer_unet.py" \ - "$SITE/checkpoints/unet_big/model.onnx" -rm -rf "$PATCH_PY" - -MPLBACKEND=Agg "$PY" -c "import oemer.ete, cv2; from cv2.typing import RotatedRect; print('oemer', cv2.__version__)" - -echo "== Trim the Python prefix ==" -rm -rf \ - "$ROOT/build/python/lib/python3.12/test" \ - "$ROOT/build/python/lib/python3.12/ensurepip" \ - "$ROOT/build/python/lib/python3.12/idlelib" \ - "$ROOT/build/python/lib/python3.12/turtledemo" \ - "$ROOT/build/python/lib/python3.12/site-packages/pip" \ - "$ROOT/build/python/lib/python3.12/site-packages/pip-"*.dist-info \ - "$ROOT/build/python/lib/tcl9.0" \ - "$ROOT/build/python/lib/tcl9" \ - "$ROOT/build/python/lib/tk9.0" \ - "$ROOT/build/python/lib/itcl4.3.8" \ - "$ROOT/build/python/lib/libtcl9tk9.0.so" \ - "$ROOT/build/python/lib/libtcl9.0.so" \ - "$SITE/checkpoints/unet_big/weights.h5" \ - "$SITE/checkpoints/seg_net/weights.h5" || true -find "$ROOT/build/python" -type d -name __pycache__ -prune -exec rm -rf {} + || true -find "$ROOT/build/python" -type d -name tests -path '*/site-packages/*' -prune -exec rm -rf {} + || true -find "$ROOT/build/python" -name '*.a' -delete || true -# Leave the CPython binary and libpython intact. Stripping those breaks the interpreter. -find "$ROOT/build/python/lib/python3.12/site-packages" -type f -name '*.so' \ - -exec strip --strip-unneeded {} + || true - if [[ ! -f "$ROOT/electron/icon.png" ]]; then - "$PY" - <<'PY' -from PIL import Image, ImageDraw -image = Image.new("RGB", (512, 512), "#f3ecdf") -draw = ImageDraw.Draw(image) -draw.rounded_rectangle((206, 36, 306, 470), radius=48, fill="#e4cfae", outline="#6a4328", width=8) -draw.ellipse((156, 92, 214, 150), outline="#6a4328", width=6) -for y in (160, 210, 260, 330, 390, 450): - draw.ellipse((228, y, 284, y + 48), fill="#1c3830") -image.save("electron/icon.png") -PY + echo "electron/icon.png is missing." >&2 + exit 1 fi echo "== Build the Next.js server ==" npm run build -rm -rf .next/standalone/public .next/standalone/.next/static .next/standalone/scripts +rm -rf .next/standalone/public .next/standalone/.next/static mkdir -p .next/standalone/.next cp -a public .next/standalone/public cp -a .next/static .next/standalone/.next/static -cp -a scripts .next/standalone/scripts echo "== Package the AppImage ==" export CSC_IDENTITY_AUTO_DISCOVERY=false diff --git a/scripts/patch_oemer_unet.py b/scripts/patch_oemer_unet.py deleted file mode 100644 index 238a488..0000000 --- a/scripts/patch_oemer_unet.py +++ /dev/null @@ -1,115 +0,0 @@ -#!/usr/bin/env python3 -"""Rewrite oemer U-Net ConvTranspose pads so ONNX Runtime can load the model. - -oemer 0.1.8's 1st_model.onnx ships ConvTranspose nodes with pads -[0, 0, -1, -1]. ONNX Runtime 1.30 and newer reject those during shape -inference. pads [0, 0, 0, 0] with output_padding [1, 1] is the same -output size for these stride-2, kernel-1 nodes. Weights are left as they are. -""" - -from __future__ import annotations - -import sys -from pathlib import Path - -import onnx -from onnx import helper - -BAD_PADS = [0, 0, -1, -1] -FIXED_PADS = [0, 0, 0, 0] -FIXED_OUTPUT_PADDING = [1, 1] -CRASH_NODE = "model/conv2d_transpose_1/conv2d_transpose" - - -def _attr(node: onnx.NodeProto, name: str) -> onnx.AttributeProto | None: - for attr in node.attribute: - if attr.name == name: - return attr - return None - - -def _ints(node: onnx.NodeProto, name: str) -> list[int] | None: - attr = _attr(node, name) - if attr is None: - return None - return list(attr.ints) - - -def _set_ints(attr: onnx.AttributeProto, values: list[int]) -> None: - del attr.ints[:] - attr.ints.extend(values) - - -def rewrite(model_path: Path) -> int: - model = onnx.load(model_path) - changed = 0 - for node in model.graph.node: - if node.op_type != "ConvTranspose": - continue - pads = _ints(node, "pads") - if pads is None or pads == FIXED_PADS: - continue - if pads != BAD_PADS: - if any(value < 0 for value in pads): - raise SystemExit( - f"{model_path}: {node.name} has unexpected negative pads {pads}" - ) - continue - pads_attr = _attr(node, "pads") - if pads_attr is None: - raise SystemExit(f"{model_path}: {node.name} is missing pads") - _set_ints(pads_attr, FIXED_PADS) - output_padding = _attr(node, "output_padding") - if output_padding is None: - node.attribute.append( - helper.make_attribute("output_padding", FIXED_OUTPUT_PADDING) - ) - else: - _set_ints(output_padding, FIXED_OUTPUT_PADDING) - changed += 1 - print( - f"rewrote {node.name}: pads {FIXED_PADS} output_padding {FIXED_OUTPUT_PADDING}" - ) - if changed: - onnx.save(model, model_path) - verify(model_path) - print(f"rewrote {changed} ConvTranspose node(s) in {model_path}") - return changed - - -def verify(model_path: Path) -> None: - model = onnx.load(model_path) - found = False - for node in model.graph.node: - if node.op_type != "ConvTranspose": - continue - pads = _ints(node, "pads") or [] - if any(value < 0 for value in pads): - raise SystemExit(f"{model_path}: {node.name} still has negative pads {pads}") - if node.name != CRASH_NODE: - continue - found = True - output_padding = _ints(node, "output_padding") - if pads != FIXED_PADS or output_padding != FIXED_OUTPUT_PADDING: - raise SystemExit( - f"{model_path}: {node.name} is {pads} / {output_padding}, " - f"expected {FIXED_PADS} / {FIXED_OUTPUT_PADDING}" - ) - if not found: - raise SystemExit(f"{model_path}: missing {CRASH_NODE}") - - -def main() -> int: - if len(sys.argv) != 2: - print("usage: patch_oemer_unet.py MODEL.onnx", file=sys.stderr) - return 2 - path = Path(sys.argv[1]) - if not path.is_file(): - print(f"missing model: {path}", file=sys.stderr) - return 1 - rewrite(path) - return 0 - - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/scripts/run_oemer.py b/scripts/run_oemer.py deleted file mode 100755 index 55a001e..0000000 --- a/scripts/run_oemer.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python3 -"""Read one sheet-music image with oemer and print the MusicXML path. - -oemer asks ONNX Runtime for CUDA first. This wrapper forces the CPU provider -so the AppImage does not need a GPU. -""" - -from __future__ import annotations - -import os -import sys -import traceback - - -def force_cpu() -> None: - import onnxruntime as rt - - original = rt.InferenceSession - - def cpu_session(model, providers=None, **kwargs): # type: ignore[no-untyped-def] - kwargs.pop("providers", None) - return original(model, providers=["CPUExecutionProvider"], **kwargs) - - rt.InferenceSession = cpu_session # type: ignore[method-assign] - - -def prepare_png(src: str, dest: str) -> None: - from PIL import Image, ImageOps - - with Image.open(src) as image: - rgb = ImageOps.exif_transpose(image).convert("RGB") - if min(rgb.size) < 80: - raise ValueError("This image is too small to read as sheet music.") - rgb.thumbnail((2200, 2200)) - rgb.save(dest, "PNG") - - -def recognize(png: str, out_dir: str) -> str: - from argparse import Namespace - - from oemer.ete import clear_data, extract - - def once(deskew: bool) -> str: - clear_data() - args = Namespace( - img_path=png, - output_path=out_dir, - use_tf=False, - save_cache=False, - without_deskew=not deskew, - ) - return extract(args) - - try: - return once(True) - except (Exception, SystemExit): - traceback.print_exc() - return once(False) - - -def main() -> int: - if len(sys.argv) != 3: - print("usage: run_oemer.py IMAGE OUT_DIR", file=sys.stderr) - return 2 - src, out_dir = sys.argv[1], sys.argv[2] - os.makedirs(out_dir, exist_ok=True) - png = os.path.join(out_dir, "page.png") - try: - prepare_png(src, png) - force_cpu() - musicxml = recognize(png, out_dir) - except Exception as exc: # noqa: BLE001 — surface a single readable failure - print(f"{exc.__class__.__name__}: {exc}", file=sys.stderr) - traceback.print_exc() - return 1 - if not musicxml or not os.path.isfile(musicxml): - print("oemer did not write MusicXML", file=sys.stderr) - return 1 - print(f"MUSICXML={musicxml}") - return 0 - - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/src/app/api/recognize/route.ts b/src/app/api/recognize/route.ts deleted file mode 100644 index 2f725b4..0000000 --- a/src/app/api/recognize/route.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { connection } from "next/server"; -import { recognizeImage, recognizerInfo } from "@/lib/recognize"; - -const TYPES = new Set(["image/png", "image/jpeg", "image/webp"]); - -export async function GET() { - await connection(); - return Response.json(recognizerInfo()); -} - -export async function POST(request: Request) { - let form: FormData; - try { - form = await request.formData(); - } catch { - return Response.json({ error: "The upload could not be read. No notes were invented." }, { status: 400 }); - } - - const image = form.get("image"); - if (!(image instanceof File)) { - return Response.json({ error: "Choose a PNG, JPG, or WEBP image first." }, { status: 400 }); - } - if (image.size === 0) { - return Response.json( - { error: "That file is empty. No notes were invented." }, - { status: 400 }, - ); - } - const named = /\.(png|jpe?g|webp)$/i.test(image.name); - if (image.type && !TYPES.has(image.type) && !named) { - return Response.json( - { error: "Use a PNG, JPG, or WEBP photo. That file is not one of those. No notes were invented." }, - { status: 415 }, - ); - } - if (image.size > 8 * 1024 * 1024) { - return Response.json( - { error: "That image is over 8 MB. Crop the line or export a smaller JPEG." }, - { status: 413 }, - ); - } - - const result = await recognizeImage(image); - if (!result.ok) { - return Response.json({ error: result.error, recognizer: result.recognizer }, { status: result.status }); - } - return Response.json({ - score: result.score, - warnings: result.warnings, - recognizer: result.recognizer, - }); -} diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 6c43e25..a0aad8f 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -16,9 +16,9 @@ const display = Fraunces({ }); export const metadata: Metadata = { - title: "Recorder Tab Converter", + title: "Recorder fingering editor", description: - "Turn a lead melody into soprano recorder fingering tabs, with rhythm, rests, and bar lines kept.", + "Drag soprano recorder fingering charts onto a line, reorder them, and set how long each note lasts.", }; export default function RootLayout({ children }: LayoutProps<"/">) { diff --git a/src/app/page.tsx b/src/app/page.tsx index 5912f1c..ce72db9 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,9 +1,9 @@ -import { Converter } from "@/components/converter"; +import { Editor } from "@/components/editor"; export default function Home() { return (
- +
); } diff --git a/src/components/converter.tsx b/src/components/converter.tsx deleted file mode 100644 index 3c9022f..0000000 --- a/src/components/converter.tsx +++ /dev/null @@ -1,272 +0,0 @@ -"use client"; - -import { NoteEditor } from "@/components/note-editor"; -import { ScoreViews } from "@/components/tab-score"; -import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; -import { Button } from "@/components/ui/button"; -import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; -import type { FingeringSystem } from "@/lib/fingering"; -import { SAMPLES } from "@/lib/samples"; -import { blankScore, type Score } from "@/lib/score"; -import { Loader2, Music2, Upload } from "lucide-react"; -import { useEffect, useId, useState } from "react"; - -type Status = "empty" | "loading" | "ready" | "error"; - -type RecognizerInfo = { - available: boolean; - name: string; - detail: string; -}; - -export function Converter() { - const inputId = useId(); - const [score, setScore] = useState(null); - const [system, setSystem] = useState("baroque"); - const [status, setStatus] = useState("empty"); - const [error, setError] = useState(null); - const [warnings, setWarnings] = useState([]); - const [recognizer, setRecognizer] = useState(null); - const [dragOver, setDragOver] = useState(false); - - useEffect(() => { - let cancelled = false; - fetch("/api/recognize") - .then((response) => response.json()) - .then((info: RecognizerInfo) => { - if (!cancelled) setRecognizer(info); - }) - .catch(() => { - if (!cancelled) { - setRecognizer({ - available: false, - name: "oemer", - detail: "Could not check the local reader. Samples and the editor still work.", - }); - } - }); - return () => { - cancelled = true; - }; - }, []); - - function loadScore(next: Score) { - setScore(structuredClone(next)); - setWarnings([]); - setError(null); - setStatus("ready"); - } - - async function onFile(file: File) { - setError(null); - const allowed = - ["image/png", "image/jpeg", "image/webp"].includes(file.type) || - /\.(png|jpe?g|webp)$/i.test(file.name); - if (file.size === 0) { - setError("That file is empty. No notes were invented."); - setScore((current) => current ?? blankScore()); - setStatus("error"); - return; - } - if (!allowed) { - setError("Use a PNG, JPG, or WEBP photo. That file is not one of those. No notes were invented."); - setScore((current) => current ?? blankScore()); - setStatus("error"); - return; - } - if (file.size > 8 * 1024 * 1024) { - setError("That image is over 8 MB. Crop the line or export a smaller JPEG."); - setScore((current) => current ?? blankScore()); - setStatus("error"); - return; - } - - setStatus("loading"); - try { - const body = new FormData(); - body.set("image", file); - const response = await fetch("/api/recognize", { method: "POST", body }); - const data = (await response.json()) as { - error?: string; - score?: Score; - warnings?: string[]; - }; - if (!response.ok || data.error || !data.score) { - setError(data.error ?? "This image could not be read. No notes were invented."); - setWarnings([]); - setScore((current) => current ?? blankScore()); - setStatus("error"); - return; - } - setScore(data.score); - setWarnings(data.warnings ?? []); - setStatus("ready"); - } catch { - setError("The reader did not answer. No notes were invented. You can still enter the line by hand."); - setScore((current) => current ?? blankScore()); - setStatus("error"); - } - } - - return ( -
-
-

Soprano in C · concert pitch

-

Recorder Tab Converter

-

- Turn a lead melody into fingering tabs a soprano player can read. Each chart has the foot at - the top and the mouthpiece at the bottom, with the rhythm lined up in bars. Chords keep the - top note. A second staff is ignored. -

-
- -
{ - event.preventDefault(); - setDragOver(true); - }} - onDragLeave={() => setDragOver(false)} - onDrop={(event) => { - event.preventDefault(); - setDragOver(false); - const file = event.dataTransfer.files[0]; - if (file) void onFile(file); - }} - > -
-
-

Photo or scan

-

- {recognizer?.detail ?? "Checking the local reader…"} -

-
-
- - { - const file = event.target.files?.[0]; - event.target.value = ""; - if (file) void onFile(file); - }} - /> -
-
- {status === "loading" ? ( -

- oemer is reading the page on this computer. A dense photo can take a few minutes. Nothing - is uploaded. -

- ) : ( -

- Drop a PNG, JPG, or WEBP here, or open a sample below if you want to see the tabs before a - photo works. -

- )} -
- - {error ? ( - - The page was not read - {error} - - ) : null} - - {warnings.length > 0 ? ( - - What the reader kept - -
    - {warnings.map((warning) => ( -
  • {warning}
  • - ))} -
-
-
- ) : null} - -
- {SAMPLES.map((sample) => ( - - ))} -
- - {score ? ( - <> -
-
-

{score.title}

-

{score.attribution}

-
-
-

Fingering

- { - const picked = values[values.length - 1]; - if (picked === "baroque" || picked === "german") setSystem(picked); - }} - aria-label="Fingering system" - > - Baroque - German - -

- Baroque is the Dolmetsch chart and the default. German only changes F, F♯, and B♭. -

-
-
-
-
-

Note list

-

- Pitch, rest, and duration. Change anything the photo got wrong. The tabs follow this - list, including the bar lines. -

- -
-
- -
-
- - ) : ( -
-

No line loaded yet

-

- Open Twinkle or Ode to Joy to see fingerings and rhythm immediately, or start an empty - bar and type the notes yourself. -

- -
- )} -
- ); -} diff --git a/src/components/editor.tsx b/src/components/editor.tsx new file mode 100644 index 0000000..1972ab9 --- /dev/null +++ b/src/components/editor.tsx @@ -0,0 +1,385 @@ +"use client"; + +import { BeatArrow, FingeringDiagram } from "@/components/fingering-diagram"; +import { Button } from "@/components/ui/button"; +import { DURATION_PRESETS } from "@/lib/duration"; +import { CHART_NOTES, fingeringFor } from "@/lib/fingering"; +import { + cloneLine, + createNote, + dropIndexFromPoint, + insertNote, + LINE_EXAMPLES, + moveNote, + removeNote, + setDuration, + type LineNote, +} from "@/lib/line"; +import { extraBeatArrows } from "@/lib/score"; +import { ChevronLeft, ChevronRight, X } from "lucide-react"; +import { useEffect, useRef, useState, type PointerEvent as ReactPointerEvent, type RefObject } from "react"; + +const FOUR_FOUR = { beats: 4, beatType: 4 }; + +type Drag = + | { + source: "palette"; + pitch: string; + pointerId: number; + startX: number; + startY: number; + x: number; + y: number; + armed: boolean; + } + | { + source: "line"; + id: string; + pitch: string; + pointerId: number; + startX: number; + startY: number; + x: number; + y: number; + armed: boolean; + }; + +export function Editor() { + const [notes, setNotes] = useState([]); + const [title, setTitle] = useState("Empty line"); + const [drag, setDrag] = useState(null); + const [dropIndex, setDropIndex] = useState(null); + const dragRef = useRef(null); + const lineRef = useRef(null); + const paletteRef = useRef(null); + + function updateDrag(next: Drag | null) { + dragRef.current = next; + setDrag(next); + } + + useEffect(() => { + function pointIndex(clientX: number, clientY: number) { + const line = lineRef.current; + if (!line) return -1; + const rect = line.getBoundingClientRect(); + const centers = [...line.querySelectorAll("[data-note-id]")].map((element) => { + const box = element.getBoundingClientRect(); + return box.left + box.width / 2; + }); + return dropIndexFromPoint(clientX, clientY, rect, centers); + } + + function onMove(event: PointerEvent) { + const current = dragRef.current; + if (!current || event.pointerId !== current.pointerId) return; + const dx = event.clientX - current.startX; + const dy = event.clientY - current.startY; + if (!current.armed) { + const touchScroll = + event.pointerType === "touch" && + current.source === "palette" && + Math.abs(dx) > 12 && + Math.abs(dx) > Math.abs(dy); + if (touchScroll) { + updateDrag(null); + setDropIndex(null); + return; + } + if (Math.hypot(dx, dy) < 8) return; + } + if (lineRef.current) { + const rect = lineRef.current.getBoundingClientRect(); + if (event.clientX > rect.right - 28) lineRef.current.scrollLeft += 14; + if (event.clientX < rect.left + 28) lineRef.current.scrollLeft -= 14; + } + const next = { ...current, x: event.clientX, y: event.clientY, armed: true }; + updateDrag(next); + const index = pointIndex(event.clientX, event.clientY); + setDropIndex(index >= 0 ? index : null); + } + + function onUp(event: PointerEvent) { + const current = dragRef.current; + if (!current || event.pointerId !== current.pointerId) return; + const index = current.armed ? pointIndex(event.clientX, event.clientY) : -1; + if (current.armed && index >= 0) { + if (current.source === "palette") { + setNotes((existing) => insertNote(existing, index, createNote(current.pitch))); + setTitle("Your line"); + } else { + setNotes((existing) => moveNote(existing, current.id, index)); + } + } + updateDrag(null); + setDropIndex(null); + } + + window.addEventListener("pointermove", onMove); + window.addEventListener("pointerup", onUp); + window.addEventListener("pointercancel", onUp); + return () => { + window.removeEventListener("pointermove", onMove); + window.removeEventListener("pointerup", onUp); + window.removeEventListener("pointercancel", onUp); + }; + }, []); + + function beginDrag(event: ReactPointerEvent, next: Drag) { + if (event.button !== 0 && event.pointerType === "mouse") return; + updateDrag(next); + } + + const ghostPitch = drag?.armed ? drag.pitch : null; + + return ( +
+
+

Soprano in C · Baroque

+

Recorder fingering editor

+

+ Drag a chart from the palette onto the line. Drag a placed chart sideways to reorder it, set + how long it lasts, or take it off. The foot, with the double holes, stays at the top. +

+
+ +
+ + {LINE_EXAMPLES.map((example) => ( + + ))} +
+ +
+
+
+

Palette

+

+ C5 through D7. Drag a chart downward onto the line. Swipe the row sideways to see the rest. +

+
+ +
+
+ {CHART_NOTES.map((pitch) => ( +
+ beginDrag(event, { + source: "palette", + pitch, + pointerId: event.pointerId, + startX: event.clientX, + startY: event.clientY, + x: event.clientX, + y: event.clientY, + armed: false, + }) + } + > + + {pitch} +
+ ))} +
+
+ +
+
+
+

{title}

+

+ 4/4. An arrow after a chart is one more beat. A dotted value keeps its name under the chart. +

+
+ +
+
+ {notes.length === 0 ? ( +

+ Drop a fingering here. The line is empty. +

+ ) : ( +
+ {notes.map((note, index) => ( +
+ {dropIndex === index ? : null} + + beginDrag(event, { + source: "line", + id: note.id, + pitch: note.pitch, + pointerId: event.pointerId, + startX: event.clientX, + startY: event.clientY, + x: event.clientX, + y: event.clientY, + armed: false, + }) + } + onDuration={(quarters) => { + const preset = DURATION_PRESETS.find((item) => item.quarters === quarters); + if (!preset) return; + setNotes((existing) => setDuration(existing, note.id, preset)); + }} + onRemove={() => setNotes((existing) => removeNote(existing, note.id))} + /> +
+ ))} + {dropIndex === notes.length ? : null} +
+ )} +
+
+ +
    +
  • Foot at the top, mouthpiece at the bottom
  • +
  • Filled hole: closed
  • +
  • Empty hole: open
  • +
  • One of a pair filled: half-hole
  • +
  • Thumb half-filled: pinch
  • +
  • Arrow: one more beat
  • +
+ + {ghostPitch ? ( +
+ +
+ ) : null} +
+ ); +} + +function PlacedNote({ + note, + dimmed, + onPointerDown, + onDuration, + onRemove, +}: { + note: LineNote; + dimmed: boolean; + onPointerDown: (event: ReactPointerEvent) => void; + onDuration: (quarters: number) => void; + onRemove: () => void; +}) { + const fingering = fingeringFor(note.pitch, "baroque"); + const arrows = extraBeatArrows(note.quarters, FOUR_FOUR); + return ( +
+
+ + {arrows > 0 ? ( + + {Array.from({ length: arrows }, (_, index) => ( + + ))} + + ) : null} +
+

{note.pitch}

+ + + +
+ ); +} + +function DropCaret() { + return
; +} + +function ScrollButtons({ + target, + label, +}: { + target: RefObject; + label: string; +}) { + return ( +
+ + +
+ ); +} diff --git a/src/components/note-editor.tsx b/src/components/note-editor.tsx deleted file mode 100644 index 9b2b405..0000000 --- a/src/components/note-editor.tsx +++ /dev/null @@ -1,285 +0,0 @@ -"use client"; - -import { Button } from "@/components/ui/button"; -import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; -import { - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue, -} from "@/components/ui/select"; -import { DURATION_PRESETS, quartersMatch } from "@/lib/duration"; -import { CHART_NOTES } from "@/lib/fingering"; -import { - barStatus, - newId, - parseSignature, - signatureKey, - TIME_SIGNATURES, - type Score, - type ScoreEvent, -} from "@/lib/score"; -import { ChevronDown, ChevronUp, Plus, Trash2 } from "lucide-react"; - -const fieldClass = - "h-10 min-w-0 rounded-lg border border-input bg-card px-2 text-sm text-foreground outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50"; - -export function NoteEditor({ - score, - onChange, -}: { - score: Score; - onChange: (score: Score) => void; -}) { - const signatures = TIME_SIGNATURES.some( - (item) => - item.beats === score.timeSignature.beats && item.beatType === score.timeSignature.beatType, - ) - ? TIME_SIGNATURES - : [score.timeSignature, ...TIME_SIGNATURES]; - - function updateEvent(measureId: string, eventId: string, patch: Partial) { - onChange({ - ...score, - measures: score.measures.map((measure) => - measure.id !== measureId - ? measure - : { - ...measure, - events: measure.events.map((event) => - event.id === eventId ? { ...event, ...patch } : event, - ), - }, - ), - }); - } - - function move(measureIndex: number, eventIndex: number, direction: -1 | 1) { - const measures = score.measures.map((measure) => ({ ...measure, events: [...measure.events] })); - const measure = measures[measureIndex]; - const target = eventIndex + direction; - if (target >= 0 && target < measure.events.length) { - const [item] = measure.events.splice(eventIndex, 1); - measure.events.splice(target, 0, item); - } else if (direction === -1 && measureIndex > 0) { - const [item] = measure.events.splice(eventIndex, 1); - measures[measureIndex - 1].events.push(item); - } else if (direction === 1 && measureIndex < measures.length - 1) { - const [item] = measure.events.splice(eventIndex, 1); - measures[measureIndex + 1].events.unshift(item); - } else { - return; - } - onChange({ ...score, measures }); - } - - function add(measureId: string, kind: "note" | "rest") { - const measure = score.measures.find((item) => item.id === measureId); - const previous = [...(measure?.events ?? [])].reverse().find((event) => event.kind === "note"); - const event: ScoreEvent = { - id: newId("e"), - kind, - pitch: kind === "note" ? (previous?.pitch ?? "C5") : null, - quarters: 1, - label: "quarter", - tied: false, - }; - onChange({ - ...score, - measures: score.measures.map((item) => - item.id === measureId ? { ...item, events: [...item.events, event] } : item, - ), - }); - } - - return ( -
-
- - onChange({ ...score, title: event.target.value })} - className="h-10" - /> -
-
- - -
- - {score.measures.map((measure, measureIndex) => { - const status = barStatus(measure, score.timeSignature); - return ( -
- Bar {measureIndex + 1} -

- {status.text} -

- {measure.events.map((event, eventIndex) => ( - { - if (value === "rest") { - updateEvent(measure.id, event.id, { kind: "rest", pitch: null }); - } else { - updateEvent(measure.id, event.id, { kind: "note", pitch: value }); - } - }} - onDuration={(quarters) => { - const preset = DURATION_PRESETS.find((item) => quartersMatch(item.quarters, quarters)); - updateEvent(measure.id, event.id, { - quarters, - label: preset?.label ?? event.label, - }); - }} - onDelete={() => - onChange({ - ...score, - measures: score.measures.map((item) => - item.id === measure.id - ? { ...item, events: item.events.filter((row) => row.id !== event.id) } - : item, - ), - }) - } - onUp={() => move(measureIndex, eventIndex, -1)} - onDown={() => move(measureIndex, eventIndex, 1)} - /> - ))} -
- - - {score.measures.length > 1 ? ( - - ) : null} -
-
- ); - })} - - -
- ); -} - -function EventRow({ - event, - onPitch, - onDuration, - onDelete, - onUp, - onDown, -}: { - event: ScoreEvent; - onPitch: (value: string) => void; - onDuration: (quarters: number) => void; - onDelete: () => void; - onUp: () => void; - onDown: () => void; -}) { - const pitches = [...CHART_NOTES] as string[]; - if (event.pitch && !pitches.includes(event.pitch)) pitches.unshift(event.pitch); - const durations = [...DURATION_PRESETS]; - if (!durations.some((item) => quartersMatch(item.quarters, event.quarters))) { - durations.push({ quarters: event.quarters, label: event.label }); - } - - return ( -
- - - - -
- - - -
-
- ); -} diff --git a/src/components/tab-score.tsx b/src/components/tab-score.tsx deleted file mode 100644 index 2503153..0000000 --- a/src/components/tab-score.tsx +++ /dev/null @@ -1,273 +0,0 @@ -"use client"; - -import { BeatArrow, FingeringDiagram, RestMark } from "@/components/fingering-diagram"; -import { Badge } from "@/components/ui/badge"; -import { Button } from "@/components/ui/button"; -import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; -import { - CHART_NOTES, - fingeringCode, - fingeringFor, - germanDiffers, - type FingeringSystem, -} from "@/lib/fingering"; -import { - extraBeatArrows, - formatBeat, - leadRows, - signatureKey, - startBeats, - type Measure, - type Score, - type ScoreEvent, - type TimeSignature, -} from "@/lib/score"; -import { formatTextTab } from "@/lib/text-tab"; -import { Copy } from "lucide-react"; -import { toast } from "sonner"; - -export function ScoreViews({ score, system }: { score: Score; system: FingeringSystem }) { - const text = formatTextTab(score, system); - const outside = score.measures - .flatMap((measure) => measure.events) - .filter((event) => event.kind === "note" && event.pitch && !fingeringFor(event.pitch, system)).length; - - return ( - -
- - Player tabs - Text - Chart - -

{signatureKey(score.timeSignature)}

-
- - -

- Each note is a narrow recorder with the foot at the top and the mouthpiece at the bottom. - Filled holes are closed. Notes sit in a row, grouped by bar. An arrow is one more beat on - the note before it. The duration is also written under the chart. -

- {outside > 0 ? ( -

- {outside} {outside === 1 ? "note is" : "notes are"} outside C5–D7, so {outside === 1 ? "it has" : "they have"} no - fingering. -

- ) : null} -
- {leadRows(score.measures).map((row, rowIndex) => ( -
-
- {row.map((measure) => ( - item.id === measure.id) + 1} - /> - ))} -
-
- ))} -
- - {score.measures.some((measure) => - measure.events.some((event) => event.pitch === "D7"), - ) ? ( -

- Dolmetsch’s primary top D leaves hole 7 open. The same page notes that covering 7a and 7b - is a common fix if that D is sharp. -

- ) : null} -
- - -
- -
-
-          {text}
-        
-
- - -

- Every note from C5 to D7, foot at the top. {system === "baroque" ? "Baroque is showing." : "German changes are badged."} -

-
    - {CHART_NOTES.map((note) => { - const fingering = fingeringFor(note, system); - return ( -
  • - -

    {note}

    -

    - {fingering ? fingeringCode(fingering) : "—"} -

    - {system === "german" && germanDiffers(note) ? German : null} -
  • - ); - })} -
-

- Top D (D7) is the chart’s primary fingering, with hole 7 open. Dolmetsch notes that covering - 7a and 7b is a common fix if the note is sharp on your instrument. -

- -
-
- ); -} - -function MeasureGroup({ - measure, - system, - timeSignature, - barNumber, -}: { - measure: Measure; - system: FingeringSystem; - timeSignature: TimeSignature; - barNumber: number; -}) { - const beats = startBeats(measure.events, timeSignature); - return ( -
-
- {measure.events.length === 0 ? ( -

Empty bar

- ) : ( - groupByBeat(measure.events, beats).map((group) => ( -
- {group.map(({ event, beat }) => ( - - ))} -
- )) - )} -
- {measure.repeatTimes ? ( -

- ×{measure.repeatTimes} -

- ) : null} -
- ); -} - -function groupByBeat(events: ScoreEvent[], beats: number[]) { - const groups: { event: ScoreEvent; beat: number }[][] = []; - events.forEach((event, index) => { - const beat = beats[index] ?? 1; - const bucket = Math.floor(beat - 1 + 1e-6); - const current = groups[groups.length - 1]; - const currentBucket = current ? Math.floor(current[0].beat - 1 + 1e-6) : -1; - if (!current || currentBucket !== bucket) groups.push([{ event, beat }]); - else current.push({ event, beat }); - }); - return groups; -} - -function LeadNote({ - event, - beat, - system, - timeSignature, -}: { - event: ScoreEvent; - beat: number; - system: FingeringSystem; - timeSignature: TimeSignature; -}) { - const fingering = event.kind === "note" && event.pitch ? fingeringFor(event.pitch, system) : null; - const arrows = extraBeatArrows(event.quarters, timeSignature); - const name = event.kind === "rest" ? "rest" : (event.pitch ?? "?"); - return ( -
-
- {event.kind === "rest" ? ( - - ) : ( - - )} - {arrows > 0 ? ( - - {Array.from({ length: arrows }, (_, index) => ( - - ))} - - ) : null} -
-

{name}

-

- {event.label} - {event.tied ? " · tie" : ""} -

-

beat {formatBeat(beat)}

-
- ); -} - -function FingeringLegend() { - return ( -
    -
  • Foot at the top, mouthpiece at the bottom
  • -
  • Filled hole: closed
  • -
  • Empty hole: open
  • -
  • One of a pair filled: half-hole
  • -
  • Thumb half-filled: pinch
  • -
  • Arrow: one more beat
  • -
  • ×2 only when the music repeats
  • -
- ); -} - -function FingeringSources() { - return ( -
- Where these fingerings come from -
-

- Baroque (English) fingerings are the first row on the Dolmetsch Online chart for recorders - in C, prepared by Brian Blood. Holes are numbered 0 (thumb) through 7, and 6 and 7 are the - double holes on a modern soprano. This app uses concert pitch: the instrument sounds as - written, so low C is C5 and the encoded range stops at D7. -

-

- German fingering changes three notes. Low F is Dolmetsch’s German fingering, thumb plus - holes 1–4, with 5, 6, and 7 open. F♯ is thumb plus 1, 2, 3, 5, 6, and half-hole 7. B♭ is - thumb plus 1, 3, 4, and 6. Those two accidentals follow the school German pattern described - by Zach VanderGraaff in Dynamic Music Room. The octave above uses the same front holes with - the Dolmetsch second-octave thumb. Every other note stays on the Baroque chart. -

-

Alternate fingerings from the Dolmetsch chart are not shown, and nothing outside C5–D7 is guessed.

-
-
- ); -} diff --git a/src/lib/line.ts b/src/lib/line.ts new file mode 100644 index 0000000..d1434a4 --- /dev/null +++ b/src/lib/line.ts @@ -0,0 +1,99 @@ +import { DURATION_PRESETS, type DurationPreset } from "@/lib/duration"; +import { newId } from "@/lib/score"; + +export type LineNote = { + id: string; + pitch: string; + quarters: number; + label: string; +}; + +const QUARTER = DURATION_PRESETS.find((preset) => preset.quarters === 1) ?? DURATION_PRESETS[3]; + +export function createNote(pitch: string, preset: DurationPreset = QUARTER, id = newId("n")): LineNote { + return { id, pitch, quarters: preset.quarters, label: preset.label }; +} + +export function insertNote(notes: LineNote[], index: number, note: LineNote): LineNote[] { + const next = notes.slice(); + const at = Math.max(0, Math.min(index, next.length)); + next.splice(at, 0, note); + return next; +} + +/** `toIndex` is the slot in the current list, counting the note that is moving. */ +export function moveNote(notes: LineNote[], id: string, toIndex: number): LineNote[] { + const from = notes.findIndex((note) => note.id === id); + if (from < 0) return notes; + const next = notes.filter((note) => note.id !== id); + const adjusted = toIndex > from ? toIndex - 1 : toIndex; + const at = Math.max(0, Math.min(adjusted, next.length)); + next.splice(at, 0, notes[from]); + return next; +} + +export function removeNote(notes: LineNote[], id: string): LineNote[] { + return notes.filter((note) => note.id !== id); +} + +export function setDuration(notes: LineNote[], id: string, preset: DurationPreset): LineNote[] { + return notes.map((note) => + note.id === id ? { ...note, quarters: preset.quarters, label: preset.label } : note, + ); +} + +export function cloneLine(notes: LineNote[]): LineNote[] { + return notes.map((note) => ({ ...note, id: newId("n") })); +} + +/** -1 when the pointer is outside the line. Otherwise the insertion slot from left to right. */ +export function dropIndexFromPoint( + pointerX: number, + pointerY: number, + line: { left: number; right: number; top: number; bottom: number }, + noteCenters: number[], + slack = 36, +): number { + const inside = + pointerX >= line.left - slack && + pointerX <= line.right + slack && + pointerY >= line.top - slack && + pointerY <= line.bottom + slack; + if (!inside) return -1; + for (let index = 0; index < noteCenters.length; index += 1) { + if (pointerX < noteCenters[index]) return index; + } + return noteCenters.length; +} + +function preset(quarters: number, label: string): DurationPreset { + return { quarters, label }; +} + +/** Two short lines to edit. They are not a full tune. */ +export const LINE_EXAMPLES = [ + { + id: "twinkle", + title: "Twinkle, opening", + blurb: "Seven notes. The last one is held.", + notes: [ + createNote("C5", preset(1, "quarter"), "twinkle-1"), + createNote("C5", preset(1, "quarter"), "twinkle-2"), + createNote("G5", preset(1, "quarter"), "twinkle-3"), + createNote("G5", preset(1, "quarter"), "twinkle-4"), + createNote("A5", preset(1, "quarter"), "twinkle-5"), + createNote("A5", preset(1, "quarter"), "twinkle-6"), + createNote("G5", preset(2, "half"), "twinkle-7"), + ], + }, + { + id: "ode", + title: "Ode, held note", + blurb: "A dotted quarter, an eighth, and a half note.", + notes: [ + createNote("E5", preset(1.5, "dotted quarter"), "ode-1"), + createNote("D5", preset(0.5, "eighth"), "ode-2"), + createNote("D5", preset(2, "half"), "ode-3"), + ], + }, +] as const; diff --git a/src/lib/musicxml.ts b/src/lib/musicxml.ts deleted file mode 100644 index 986ae15..0000000 --- a/src/lib/musicxml.ts +++ /dev/null @@ -1,338 +0,0 @@ -import { XMLParser } from "fast-xml-parser"; -import { durationLabel } from "@/lib/duration"; -import { parsePitch } from "@/lib/pitch"; -import { newId, type Measure, type Score, type ScoreEvent, type TimeSignature } from "@/lib/score"; - -type XNode = Record; - -export type ParseSuccess = { - ok: true; - score: Score; - warnings: string[]; -}; - -export type ParseFailure = { - ok: false; - error: string; -}; - -const parser = new XMLParser({ - preserveOrder: true, - ignoreAttributes: false, - attributeNamePrefix: "@_", - removeNSPrefix: true, - trimValues: true, -}); - -function isNode(value: unknown): value is XNode { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -function tagOf(node: XNode): string | null { - return Object.keys(node).find((key) => key !== ":@" ) ?? null; -} - -function children(node: XNode): XNode[] { - const tag = tagOf(node); - if (!tag) return []; - const value = node[tag]; - if (!Array.isArray(value)) return []; - return value.filter(isNode); -} - -function textOf(node: XNode): string { - const tag = tagOf(node); - if (!tag) return ""; - const value = node[tag]; - if (typeof value === "string" || typeof value === "number") return String(value).trim(); - if (!Array.isArray(value)) return ""; - for (const item of value) { - if (isNode(item) && "#text" in item && item["#text"] != null) return String(item["#text"]).trim(); - } - return ""; -} - -function attrs(node: XNode): Record { - const raw = node[":@"]; - if (!isNode(raw)) return {}; - const result: Record = {}; - for (const [key, value] of Object.entries(raw)) result[key.replace(/^@_/, "")] = String(value); - return result; -} - -function findChild(nodes: XNode[], name: string): XNode | undefined { - return nodes.find((node) => tagOf(node) === name); -} - -function childText(nodes: XNode[], name: string): string | null { - const child = findChild(nodes, name); - if (!child) return null; - const text = textOf(child); - return text === "" ? null : text; -} - -function hasTag(nodes: XNode[], name: string): boolean { - return nodes.some((node) => tagOf(node) === name); -} - -function findDeep(nodes: XNode[], name: string): XNode | undefined { - for (const node of nodes) { - if (tagOf(node) === name) return node; - const nested = findDeep(children(node), name); - if (nested) return nested; - } - return undefined; -} - -function tieStarts(nodes: XNode[]): boolean { - for (const node of nodes) { - const tag = tagOf(node); - if (tag === "tie" && attrs(node).type === "start") return true; - if (tag === "notations") { - for (const inner of children(node)) { - if (tagOf(inner) === "tied" && attrs(inner).type === "start") return true; - } - } - } - return false; -} - -function pitchFrom(noteKids: XNode[]): { name: string; midi: number } | null { - const pitch = findChild(noteKids, "pitch"); - if (!pitch) return null; - const kids = children(pitch); - const step = childText(kids, "step"); - const octave = childText(kids, "octave"); - if (!step || !octave) return null; - const alter = Number(childText(kids, "alter") ?? "0"); - const accidental = - alter === 1 ? "#" : alter === -1 ? "b" : alter === 2 ? "##" : alter === -2 ? "bb" : ""; - if (![0, 1, -1, 2, -2].includes(alter)) return null; - const parsed = parsePitch(`${step}${accidental}${octave}`); - return parsed ? { name: parsed.name, midi: parsed.midi } : null; -} - -type Candidate = { - onset: number; - kind: "note" | "rest"; - midi: number; - pitch: string | null; - quarters: number; - label: string; - tied: boolean; -}; - -function onsetKey(value: number): number { - return Math.round(value * 1000) / 1000; -} - -export function parseMusicXml(xml: string): ParseSuccess | ParseFailure { - const cleaned = xml.replace(//gi, ""); - let root: unknown; - try { - root = parser.parse(cleaned); - } catch { - return { ok: false, error: "The recognizer returned text that is not MusicXML." }; - } - if (!Array.isArray(root)) return { ok: false, error: "The recognizer returned text that is not MusicXML." }; - - const scoreNode = findDeep(root.filter(isNode), "score-partwise"); - if (!scoreNode) { - return { - ok: false, - error: "The recognizer did not return a partwise score. No notes were invented.", - }; - } - - const top = children(scoreNode); - const work = findChild(top, "work"); - const title = - (work && childText(children(work), "work-title")) || - childText(top, "movement-title") || - "Uploaded page"; - - const parts = top.filter((node) => tagOf(node) === "part"); - if (parts.length === 0) { - return { ok: false, error: "The score has no part to read as a lead line." }; - } - - const warnings: string[] = []; - if (parts.length > 1) warnings.push("Only the first part was read. Other parts were left out."); - - let divisions = 1; - let timeSignature: TimeSignature = { beats: 4, beatType: 4 }; - let sawTime = false; - let timeChanged = false; - let sawLowerStaff = false; - let reduced = false; - let sawGrace = false; - let sawTie = false; - - const measures: Measure[] = []; - - for (const [measureIndex, measureNode] of children(parts[0]).filter((node) => tagOf(node) === "measure").entries()) { - let cursor = 0; - let lastDuration = 0; - let repeatForward = false; - let repeatTimes: number | undefined; - const grouped = new Map(); - - for (const element of children(measureNode)) { - const tag = tagOf(element); - if (tag === "barline") { - const repeat = findChild(children(element), "repeat"); - if (repeat) { - const repeatAttrs = attrs(repeat); - if (repeatAttrs.direction === "forward") repeatForward = true; - if (repeatAttrs.direction === "backward") { - const times = Number(repeatAttrs.times); - repeatTimes = Number.isFinite(times) && times >= 2 ? times : 2; - } - } - continue; - } - if (tag === "attributes") { - const kids = children(element); - const nextDivisions = childText(kids, "divisions"); - if (nextDivisions && Number(nextDivisions) > 0) divisions = Number(nextDivisions); - const time = findChild(kids, "time"); - if (time) { - const timeKids = children(time); - const beats = Number(childText(timeKids, "beats")); - const beatType = Number(childText(timeKids, "beat-type")); - if (beats > 0 && beatType > 0) { - if (!sawTime) { - timeSignature = { beats, beatType }; - sawTime = true; - } else if (timeSignature.beats !== beats || timeSignature.beatType !== beatType) { - timeChanged = true; - } - } - } - continue; - } - if (tag === "backup" || tag === "forward") { - const delta = Number(childText(children(element), "duration") ?? "0"); - cursor += tag === "backup" ? -delta : delta; - continue; - } - if (tag !== "note") continue; - - const kids = children(element); - if (hasTag(kids, "grace")) { - sawGrace = true; - continue; - } - const isChord = hasTag(kids, "chord"); - const duration = Number(childText(kids, "duration") ?? "0"); - const staff = childText(kids, "staff"); - if (staff && staff !== "1") { - sawLowerStaff = true; - if (!isChord) { - cursor += duration; - lastDuration = duration; - } - continue; - } - - const onset = onsetKey(isChord ? cursor - lastDuration : cursor); - if (!isChord) { - cursor += duration; - lastDuration = duration; - } - - const type = childText(kids, "type"); - const dots = kids.filter((node) => tagOf(node) === "dot").length; - const modification = findChild(kids, "time-modification"); - let tuplet = false; - if (modification) { - const actual = Number(childText(children(modification), "actual-notes")); - const normal = Number(childText(children(modification), "normal-notes")); - tuplet = actual === 3 && normal === 2; - } - const quarters = divisions > 0 ? duration / divisions : 0; - const tied = tieStarts(kids); - if (tied) sawTie = true; - - if (hasTag(kids, "rest")) { - const list = grouped.get(onset) ?? []; - list.push({ - onset, - kind: "rest", - midi: -1, - pitch: null, - quarters, - label: durationLabel(quarters, type, dots, tuplet), - tied: false, - }); - grouped.set(onset, list); - continue; - } - - const pitch = pitchFrom(kids); - if (!pitch) continue; - const list = grouped.get(onset) ?? []; - list.push({ - onset, - kind: "note", - midi: pitch.midi, - pitch: pitch.name, - quarters, - label: durationLabel(quarters, type, dots, tuplet), - tied, - }); - grouped.set(onset, list); - } - - const events: ScoreEvent[] = []; - for (const onset of [...grouped.keys()].sort((a, b) => a - b)) { - const candidates = grouped.get(onset) ?? []; - const notes = candidates.filter((candidate) => candidate.kind === "note"); - if (notes.length > 1) reduced = true; - const chosen = notes.length > 0 ? notes.reduce((best, note) => (note.midi > best.midi ? note : best)) : candidates[0]; - if (!chosen || chosen.quarters <= 0) continue; - events.push({ - id: newId("e"), - kind: chosen.kind, - pitch: chosen.pitch, - quarters: chosen.quarters, - label: chosen.label, - tied: chosen.tied, - }); - } - - measures.push({ - id: newId("m"), - events, - ...(repeatForward ? { repeatForward: true } : {}), - ...(repeatTimes ? { repeatTimes } : {}), - }); - void measureIndex; - } - - if (sawLowerStaff) warnings.push("Notes on lower staves were left out. The tab follows the top staff."); - if (reduced) warnings.push("Chords and overlapping voices were reduced to the highest note."); - if (sawGrace) warnings.push("Grace notes were left out so the beat stays aligned."); - if (timeChanged) warnings.push("A later time signature was ignored. The tab uses the first one."); - if (sawTie) warnings.push("Tied notes stay separate so the bar lines do not move."); - - const eventCount = measures.reduce((total, measure) => total + measure.events.length, 0); - if (eventCount === 0) { - return { - ok: false, - error: "The recognizer finished, but it did not find a lead note or a rest. No notes were invented.", - }; - } - - return { - ok: true, - warnings, - score: { - title, - attribution: "Read from your image. Correct anything that looks wrong before you play.", - timeSignature, - measures: measures.length > 0 ? measures : [{ id: newId("m"), events: [] }], - origin: "upload", - }, - }; -} diff --git a/src/lib/recognize.ts b/src/lib/recognize.ts deleted file mode 100644 index 5a7cf1e..0000000 --- a/src/lib/recognize.ts +++ /dev/null @@ -1,201 +0,0 @@ -import { spawn } from "node:child_process"; -import { existsSync, readdirSync } from "node:fs"; -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import path from "node:path"; -import { parseMusicXml } from "@/lib/musicxml"; -import type { Score } from "@/lib/score"; - -export type RecognizerInfo = { - available: boolean; - name: string; - detail: string; -}; - -export type RecognizeSuccess = { - ok: true; - score: Score; - warnings: string[]; - recognizer: string; -}; - -export type RecognizeFailure = { - ok: false; - error: string; - status: number; - recognizer: string; -}; - -export function resolvePython(): string | null { - const fromEnv = process.env.OEMER_PYTHON; - if (fromEnv && existsSync(fromEnv)) return fromEnv; - return null; -} - -function modelsReady(python: string): boolean { - const lib = path.resolve(path.dirname(python), "..", "lib"); - if (!existsSync(lib)) return false; - for (const entry of readdirSync(lib)) { - const model = path.join( - lib, - entry, - "site-packages", - "oemer", - "checkpoints", - "unet_big", - "model.onnx", - ); - if (existsSync(model)) return true; - } - return false; -} - -export function recognizerInfo(): RecognizerInfo { - const python = resolvePython(); - if (!python) { - return { - available: false, - name: "oemer", - detail: - "The local reader (oemer) is not installed. Sample tunes and the note editor still work. No photo is turned into guessed notes.", - }; - } - if (!modelsReady(python)) { - return { - available: false, - name: "oemer 0.1.8", - detail: - "oemer is installed, but its model files are missing, so a photo cannot be read. Sample tunes and the editor still work.", - }; - } - return { - available: true, - name: "oemer 0.1.8", - detail: - "Photos are read on this computer by oemer. It misses notes. Correct the list; nothing is sent away, and a failed read does not invent notes.", - }; -} - -function scriptPath(): string { - const candidates = [ - path.join(process.cwd(), "scripts", "run_oemer.py"), - path.join(process.cwd(), "..", "scripts", "run_oemer.py"), - ]; - return candidates.find((candidate) => existsSync(candidate)) ?? candidates[0]; -} - -function runProcess( - command: string, - args: string[], - timeoutMs: number, -): Promise<{ code: number; stdout: string; stderr: string }> { - return new Promise((resolve, reject) => { - const child = spawn(command, args, { - env: { - ...process.env, - MPLBACKEND: "Agg", - PYTHONUNBUFFERED: "1", - }, - }); - let stdout = ""; - let stderr = ""; - const timer = setTimeout(() => { - child.kill("SIGKILL"); - reject(new Error("timeout")); - }, timeoutMs); - child.stdout.setEncoding("utf8"); - child.stderr.setEncoding("utf8"); - child.stdout.on("data", (chunk) => { - stdout += chunk; - }); - child.stderr.on("data", (chunk) => { - stderr += chunk; - }); - child.on("error", (error) => { - clearTimeout(timer); - reject(error); - }); - child.on("close", (code) => { - clearTimeout(timer); - resolve({ code: code ?? 1, stdout, stderr }); - }); - }); -} - -function shortFailure(stderr: string, stdout: string): string { - const lines = `${stderr}\n${stdout}` - .split("\n") - .map((line) => line.trim()) - .filter(Boolean) - .filter((line) => !line.startsWith("MUSICXML=")); - const errorLine = [...lines].reverse().find((line) => /Error|Exception|assert|too small|could not/i.test(line)); - const text = errorLine ?? lines.at(-1) ?? "The recognizer stopped without a result."; - return text.slice(0, 280); -} - -export async function recognizeImage(file: File): Promise { - const info = recognizerInfo(); - if (!info.available) { - return { ok: false, error: info.detail, status: 503, recognizer: info.name }; - } - const python = resolvePython(); - if (!python) { - return { ok: false, error: info.detail, status: 503, recognizer: info.name }; - } - - const directory = await mkdtemp(path.join(tmpdir(), "recorder-tab-")); - try { - const inputPath = path.join(directory, "upload.bin"); - await writeFile(inputPath, Buffer.from(await file.arrayBuffer())); - const script = scriptPath(); - let result: { code: number; stdout: string; stderr: string }; - try { - result = await runProcess(python, [script, inputPath, directory], 240_000); - } catch (error) { - const timedOut = error instanceof Error && error.message === "timeout"; - return { - ok: false, - status: 422, - recognizer: info.name, - error: timedOut - ? "Reading this page took too long and was stopped. No notes were invented. Try a clearer crop of one line, or enter the notes by hand." - : "The local reader could not be started. No notes were invented.", - }; - } - - const marker = result.stdout - .split("\n") - .map((line) => line.trim()) - .find((line) => line.startsWith("MUSICXML=")); - if (result.code !== 0 || !marker) { - return { - ok: false, - status: 422, - recognizer: info.name, - error: `This image could not be read. ${shortFailure(result.stderr, result.stdout)} No notes were invented.`, - }; - } - - const xmlPath = marker.slice("MUSICXML=".length); - const xml = await readFile(xmlPath, "utf8"); - const parsed = parseMusicXml(xml); - if (!parsed.ok) { - return { ok: false, status: 422, recognizer: info.name, error: parsed.error }; - } - - const title = file.name.replace(/\.[^.]+$/, "").trim(); - return { - ok: true, - recognizer: info.name, - warnings: parsed.warnings, - score: { - ...parsed.score, - title: title || parsed.score.title, - origin: "upload", - attribution: `Read locally by ${info.name}. Correct any wrong pitch or rhythm before you play.`, - }, - }; - } finally { - await rm(directory, { recursive: true, force: true }); - } -} diff --git a/src/lib/samples.ts b/src/lib/samples.ts deleted file mode 100644 index f8af92c..0000000 --- a/src/lib/samples.ts +++ /dev/null @@ -1,92 +0,0 @@ -import type { Score, ScoreEvent } from "@/lib/score"; - -const DURATION: Record = { - w: { quarters: 4, label: "whole" }, - dh: { quarters: 3, label: "dotted half" }, - h: { quarters: 2, label: "half" }, - dq: { quarters: 1.5, label: "dotted quarter" }, - q: { quarters: 1, label: "quarter" }, - e: { quarters: 0.5, label: "eighth" }, - s: { quarters: 0.25, label: "sixteenth" }, -}; - -function events(barId: string, specs: string[]): ScoreEvent[] { - return specs.map((spec, index) => { - const [token, durationKey = "q"] = spec.split(":"); - const duration = DURATION[durationKey] ?? DURATION.q; - const rest = token === "r"; - return { - id: `${barId}-e${index + 1}`, - kind: rest ? "rest" : "note", - pitch: rest ? null : token, - quarters: duration.quarters, - label: duration.label, - tied: false, - }; - }); -} - -function measures(prefix: string, bars: string[][]) { - return bars.map((bar, index) => { - const id = `${prefix}-m${index + 1}`; - return { id, events: events(id, bar) }; - }); -} - -/** Traditional melody at concert pitch, starting on the soprano’s low C. */ -export const twinkle: Score = { - title: "Twinkle, Twinkle, Little Star", - attribution: "Traditional, public domain. First verse, concert pitch for soprano in C.", - timeSignature: { beats: 4, beatType: 4 }, - origin: "sample", - measures: measures("twinkle", [ - ["C5", "C5", "G5", "G5"], - ["A5", "A5", "G5:h"], - ["F5", "F5", "E5", "E5"], - ["D5", "D5", "C5:h"], - ["G5", "G5", "F5", "F5"], - ["E5", "E5", "D5:h"], - ["G5", "G5", "F5", "F5"], - ["E5", "E5", "D5:h"], - ["C5", "C5", "G5", "G5"], - ["A5", "A5", "G5:h"], - ["F5", "F5", "E5", "E5"], - ["D5", "D5", "C5:h"], - ]), -}; - -/** - * Ode to Joy theme in C major at concert pitch. - * The symphony states it in D; this is the familiar C-major teaching key, - * which sits on the soprano without leaving the first octave. - */ -export const odeToJoy: Score = { - title: "Ode to Joy", - attribution: - "Ludwig van Beethoven, public domain. Theme in C major, concert pitch for soprano in C.", - timeSignature: { beats: 4, beatType: 4 }, - origin: "sample", - measures: measures("ode", [ - ["E5", "E5", "F5", "G5"], - ["G5", "F5", "E5", "D5"], - ["C5", "C5", "D5", "E5"], - ["E5:dq", "D5:e", "D5:h"], - ["E5", "E5", "F5", "G5"], - ["G5", "F5", "E5", "D5"], - ["C5", "C5", "D5", "E5"], - ["D5:dq", "C5:e", "C5:h"], - ]), -}; - -export const SAMPLES = [ - { - id: "twinkle", - score: twinkle, - blurb: "First verse in C. Forked F, half notes, and four-square bars.", - }, - { - id: "ode", - score: odeToJoy, - blurb: "The hymn theme in C, with dotted quarters and held notes.", - }, -] as const; diff --git a/src/lib/text-tab.ts b/src/lib/text-tab.ts deleted file mode 100644 index c07546d..0000000 --- a/src/lib/text-tab.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { fingeringCode, fingeringFor, type FingeringSystem } from "@/lib/fingering"; -import { formatBeat, signatureKey, startBeats, type Score } from "@/lib/score"; - -export function formatTextTab(score: Score, system: FingeringSystem): string { - const lines: string[] = [ - score.title, - "Soprano recorder in C · concert pitch · sounds as written", - system === "baroque" - ? "Fingering: Baroque (English), Dolmetsch Online chart for recorders in C" - : "Fingering: German on F, F♯, and B♭; other notes stay on the Dolmetsch Baroque chart", - `Time: ${signatureKey(score.timeSignature)}`, - "", - ]; - - score.measures.forEach((measure, measureIndex) => { - const beats = startBeats(measure.events, score.timeSignature); - lines.push(`Bar ${measureIndex + 1}`); - if (measure.events.length === 0) lines.push(" (empty)"); - measure.events.forEach((event, eventIndex) => { - const beat = formatBeat(beats[eventIndex] ?? 1).padEnd(4, " "); - const name = (event.kind === "rest" ? "rest" : (event.pitch ?? "?")).padEnd(6, " "); - const duration = event.label.padEnd(16, " "); - let fingers = "—"; - if (event.kind === "note" && event.pitch) { - const fingering = fingeringFor(event.pitch, system); - fingers = fingering ? fingeringCode(fingering) : "outside C5–D7 chart"; - } - const tie = event.tied ? " tie" : ""; - lines.push(` beat ${beat} ${name} ${duration} ${fingers}${tie}`); - }); - lines.push(""); - }); - - lines.push("Legend: 0 closed thumb, x pinched thumb, - open thumb."); - lines.push("Digits are closed holes. 6a without 6b, or 7a without 7b, is a half-hole."); - lines.push("bell means cover the foot. Notes outside C5–D7 have no fingering."); - return lines.join("\n"); -} diff --git a/tests/converter.test.ts b/tests/converter.test.ts index 84b98df..626ec80 100644 --- a/tests/converter.test.ts +++ b/tests/converter.test.ts @@ -1,12 +1,18 @@ -import { readFileSync } from "node:fs"; -import path from "node:path"; import { describe, expect, it } from "vitest"; import { fingeringCode, fingeringFor, fingeringMarks, CHART_NOTES } from "@/lib/fingering"; -import { parseMusicXml } from "@/lib/musicxml"; +import { DURATION_PRESETS } from "@/lib/duration"; +import { + cloneLine, + createNote, + dropIndexFromPoint, + insertNote, + LINE_EXAMPLES, + moveNote, + removeNote, + setDuration, +} from "@/lib/line"; import { parsePitch } from "@/lib/pitch"; -import { barStatus, expectedQuarters, extraBeatArrows, leadRows } from "@/lib/score"; -import { odeToJoy, twinkle } from "@/lib/samples"; -import { formatTextTab } from "@/lib/text-tab"; +import { extraBeatArrows } from "@/lib/score"; const BAROQUE: Record = { C5: "0 1 2 3 4 5 6a 6b 7a 7b", @@ -63,7 +69,7 @@ describe("soprano chart", () => { expect(fingeringCode(fingeringFor("Db5", "baroque")!)).toBe(BAROQUE["C#5"]); }); - it("applies only the cited German changes", () => { + it("keeps the cited German changes beside the Baroque chart", () => { expect(fingeringCode(fingeringFor("F5", "german")!)).toBe("0 1 2 3 4"); expect(fingeringCode(fingeringFor("F#5", "german")!)).toBe("0 1 2 3 5 6a 6b 7a"); expect(fingeringCode(fingeringFor("Bb5", "german")!)).toBe("0 1 3 4 6a 6b"); @@ -73,98 +79,47 @@ describe("soprano chart", () => { }); }); -describe("sample melodies", () => { - it("keeps Twinkle in 4/4 with forked F and full bars", () => { - expect(twinkle.measures).toHaveLength(12); - expect(twinkle.measures[0].events.map((event) => event.pitch)).toEqual(["C5", "C5", "G5", "G5"]); - expect(twinkle.measures[2].events[0].pitch).toBe("F5"); - for (const measure of twinkle.measures) { - expect(barStatus(measure, twinkle.timeSignature).ok).toBe(true); - } - expect(formatTextTab(twinkle, "baroque")).toContain("0 1 2 3 4 6a 6b 7a 7b"); +describe("fingering line", () => { + const quarter = DURATION_PRESETS.find((preset) => preset.label === "quarter")!; + const half = DURATION_PRESETS.find((preset) => preset.label === "half")!; + + it("inserts, reorders, removes, and changes duration without touching other notes", () => { + const c = createNote("C5", quarter, "c"); + const e = createNote("E5", quarter, "e"); + const g = createNote("G5", quarter, "g"); + const inserted = insertNote(insertNote([c], 1, g), 1, e); + expect(inserted.map((note) => note.pitch)).toEqual(["C5", "E5", "G5"]); + expect(moveNote(inserted, "c", 3).map((note) => note.pitch)).toEqual(["E5", "G5", "C5"]); + expect(moveNote(inserted, "g", 0).map((note) => note.pitch)).toEqual(["G5", "C5", "E5"]); + expect(moveNote(inserted, "e", 1).map((note) => note.id)).toEqual(["c", "e", "g"]); + const held = setDuration(inserted, "e", half); + expect(held.find((note) => note.id === "e")).toMatchObject({ quarters: 2, label: "half" }); + expect(held.find((note) => note.id === "c")?.quarters).toBe(1); + expect(removeNote(held, "e").map((note) => note.pitch)).toEqual(["C5", "G5"]); + }); + + it("places a drop before the note whose center is to the right of the pointer", () => { + const line = { left: 0, right: 400, top: 0, bottom: 200 }; + expect(dropIndexFromPoint(20, 40, line, [50, 150, 250])).toBe(0); + expect(dropIndexFromPoint(80, 40, line, [50, 150, 250])).toBe(1); + expect(dropIndexFromPoint(300, 40, line, [50, 150, 250])).toBe(3); + expect(dropIndexFromPoint(20, 400, line, [50])).toBe(-1); + expect(dropIndexFromPoint(10, 10, line, [])).toBe(0); }); - it("keeps Ode to Joy dotted rhythm inside the bar", () => { - expect(expectedQuarters(odeToJoy.timeSignature)).toBe(4); - const bar = odeToJoy.measures[3]; - expect(bar.events.map((event) => event.label)).toEqual(["dotted quarter", "eighth", "half"]); - expect(bar.events.reduce((sum, event) => sum + event.quarters, 0)).toBe(4); - for (const measure of odeToJoy.measures) { - expect(barStatus(measure, odeToJoy.timeSignature).ok).toBe(true); + it("offers two short editable examples and an empty start", () => { + expect(LINE_EXAMPLES).toHaveLength(2); + for (const example of LINE_EXAMPLES) { + expect(example.notes.length).toBeGreaterThan(0); + expect(example.notes.length).toBeLessThanOrEqual(8); + for (const note of example.notes) { + expect(fingeringFor(note.pitch, "baroque")).not.toBeNull(); + } } - }); -}); - -describe("lead-line MusicXML", () => { - const xml = readFileSync(path.join(__dirname, "fixtures", "lead.musicxml"), "utf8"); - const parsed = parseMusicXml(xml); - - it("keeps timing, the top note, and the top staff", () => { - expect(parsed.ok).toBe(true); - if (!parsed.ok) return; - expect(parsed.score.title).toBe("Probe"); - expect(parsed.score.timeSignature).toEqual({ beats: 4, beatType: 4 }); - const [first, second, third, fourth] = parsed.score.measures; - expect(first.events.map((event) => [event.kind, event.pitch, event.label])).toEqual([ - ["note", "E5", "quarter"], - ["rest", null, "quarter"], - ["note", "G5", "dotted quarter"], - ["note", "A4", "eighth"], - ]); - expect(second.events.map((event) => [event.pitch, event.label, event.tied])).toEqual([ - ["E5", "half", false], - [null, "half", false], - ]); - expect(third.events.map((event) => event.pitch)).toEqual(["G5"]); - expect(fourth.events.map((event) => event.pitch)).toEqual(["C6", "D6"]); - expect(parsed.warnings.join(" ")).toMatch(/first part/); - expect(parsed.warnings.join(" ")).toMatch(/lower staves/); - expect(parsed.warnings.join(" ")).toMatch(/highest note/); - expect(parsed.warnings.join(" ")).toMatch(/Grace notes/); - expect(parsed.score.measures.some((measure) => measure.repeatTimes || measure.repeatForward)).toBe(false); - }); - - it("shows a repeat count only when the barline repeats", () => { - const xml = ` - - Lead - - - - 1 - C51quarter - 3half - - - D54whole - - - - `; - const parsed = parseMusicXml(xml); - expect(parsed.ok).toBe(true); - if (!parsed.ok) return; - expect(parsed.score.measures[0].repeatForward).toBe(true); - expect(parsed.score.measures[0].repeatTimes).toBeUndefined(); - expect(parsed.score.measures[1].repeatTimes).toBe(2); - expect(parsed.score.measures[1].events.map((event) => event.label)).toEqual(["whole"]); - }); -}); - -describe("lead layout", () => { - it("counts extra beats without adding an arrow for a dotted quarter", () => { - const fourFour = { beats: 4, beatType: 4 }; - expect(extraBeatArrows(1, fourFour)).toBe(0); - expect(extraBeatArrows(0.5, fourFour)).toBe(0); - expect(extraBeatArrows(1.5, fourFour)).toBe(0); - expect(extraBeatArrows(2, fourFour)).toBe(1); - expect(extraBeatArrows(3, fourFour)).toBe(2); - expect(extraBeatArrows(4, fourFour)).toBe(3); - }); - - it("keeps Twinkle in three rows and does not invent a repeat", () => { - expect(leadRows(twinkle.measures)).toHaveLength(3); - expect(twinkle.measures.some((measure) => measure.repeatTimes)).toBe(false); - expect(odeToJoy.measures.some((measure) => measure.repeatTimes)).toBe(false); + const copy = cloneLine([...LINE_EXAMPLES[0].notes]); + expect(copy.map((note) => note.pitch)).toEqual(LINE_EXAMPLES[0].notes.map((note) => note.pitch)); + expect(copy[0].id).not.toBe(LINE_EXAMPLES[0].notes[0].id); + expect(extraBeatArrows(LINE_EXAMPLES[0].notes[6].quarters, { beats: 4, beatType: 4 })).toBe(1); + expect(extraBeatArrows(LINE_EXAMPLES[1].notes[0].quarters, { beats: 4, beatType: 4 })).toBe(0); }); }); diff --git a/tests/fixtures/lead.musicxml b/tests/fixtures/lead.musicxml deleted file mode 100644 index 3158c4e..0000000 --- a/tests/fixtures/lead.musicxml +++ /dev/null @@ -1,113 +0,0 @@ - - - Probe - - Lead - Other - - - - - 2 - - - - E5 - 2 - quarter - - - - 2 - quarter - - - G5 - 3 - quarter - - - - A4 - 1 - eighth - - - - - C5 - 4 - half - - - - - E5 - 4 - half - - - - 4 - half - - - - - - F5 - eighth - - - G5 - 2 - 1 - 1 - quarter - - - C4 - 2 - 2 - 2 - quarter - - - - - C6 - 2 - 1 - quarter - - - D6 - 2 - 1 - quarter - - 4 - - C5 - 2 - 2 - quarter - - - D5 - 2 - 2 - quarter - - - - - - - C3 - 2 - quarter - - - -