Files
Recorder-Tabber/tests/converter.test.ts
T
Cursor Agentandchen 229a8d4723
AppImage release / appimage (push) Successful in 1m36s
Draw fingering charts foot-up, in grouped rows.
The foot and the double holes sit at the top of each narrow chart, and the mouthpiece sits at the bottom, matching hand-drawn tabs. Which holes are open or closed is unchanged. A repeat mark is shown only when the score repeats. Version 0.1.2.

Co-authored-by: catpersonisme <catpersonisme@gmail.com>
2026-10-11 08:30:50 +00:00

171 lines
7.3 KiB
TypeScript

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 { parsePitch } from "@/lib/pitch";
import { barStatus, expectedQuarters, extraBeatArrows, leadRows } from "@/lib/score";
import { odeToJoy, twinkle } from "@/lib/samples";
import { formatTextTab } from "@/lib/text-tab";
const BAROQUE: Record<string, string> = {
C5: "0 1 2 3 4 5 6a 6b 7a 7b",
"C#5": "0 1 2 3 4 5 6a 6b 7a",
D5: "0 1 2 3 4 5 6a 6b",
Eb5: "0 1 2 3 4 5 6a",
E5: "0 1 2 3 4 5",
F5: "0 1 2 3 4 6a 6b 7a 7b",
"F#5": "0 1 2 3 5 6a 6b",
G5: "0 1 2 3",
Ab5: "0 1 2 4 5 6a",
A5: "0 1 2",
Bb5: "0 1 3 4",
B5: "0 1",
C6: "0 2",
"C#6": "- 1 2",
D6: "- 2",
Eb6: "- 2 3 4 5 6a 6b",
E6: "x 1 2 3 4 5",
F6: "x 1 2 3 4 6a 6b",
"F#6": "x 1 2 3 5",
G6: "x 1 2 3",
Ab6: "x 1 2 4",
A6: "x 1 2",
Bb6: "x 1 2 5 6a 6b",
B6: "x 1 2 4 5",
C7: "x 1 4 5",
"C#7": "x 1 3 4 6a 6b bell",
D7: "x 1 3 4 6a 6b",
};
describe("soprano chart", () => {
it("covers every semitone from C5 through D7 and no others", () => {
const midis = CHART_NOTES.map((note) => parsePitch(note)?.midi);
expect(midis).toEqual(Array.from({ length: 27 }, (_, index) => 72 + index));
});
it("matches the Dolmetsch first-choice Baroque rows", () => {
for (const [note, code] of Object.entries(BAROQUE)) {
const fingering = fingeringFor(note, "baroque");
expect(fingering, note).not.toBeNull();
expect(fingeringCode(fingering!), note).toBe(code);
}
});
it("marks fork, half-holes, pinch, and bell without guessing outside the range", () => {
expect(fingeringMarks(fingeringFor("F5", "baroque")!)).toContain("fork");
expect(fingeringMarks(fingeringFor("Eb5", "baroque")!)).toContain("half 6");
expect(fingeringMarks(fingeringFor("C#5", "baroque")!)).toContain("half 7");
expect(fingeringMarks(fingeringFor("E6", "baroque")!)).toContain("pinch");
expect(fingeringMarks(fingeringFor("C#7", "baroque")!)).toContain("cover bell");
expect(fingeringFor("B4", "baroque")).toBeNull();
expect(fingeringFor("E7", "baroque")).toBeNull();
expect(fingeringCode(fingeringFor("Db5", "baroque")!)).toBe(BAROQUE["C#5"]);
});
it("applies only the cited German changes", () => {
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");
expect(fingeringCode(fingeringFor("F6", "german")!)).toBe("x 1 2 3 4");
expect(fingeringCode(fingeringFor("G5", "german")!)).toBe(fingeringCode(fingeringFor("G5", "baroque")!));
expect(fingeringCode(fingeringFor("E6", "german")!)).toBe(fingeringCode(fingeringFor("E6", "baroque")!));
});
});
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");
});
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);
}
});
});
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 = `<?xml version="1.0"?>
<score-partwise version="4.0">
<part-list><score-part id="P1"><part-name>Lead</part-name></score-part></part-list>
<part id="P1">
<measure number="1">
<barline location="left"><repeat direction="forward"/></barline>
<attributes><divisions>1</divisions><time><beats>4</beats><beat-type>4</beat-type></time></attributes>
<note><pitch><step>C</step><octave>5</octave></pitch><duration>1</duration><type>quarter</type></note>
<note><rest/><duration>3</duration><type>half</type><dot/></note>
</measure>
<measure number="2">
<note><pitch><step>D</step><octave>5</octave></pitch><duration>4</duration><type>whole</type></note>
<barline location="right"><repeat direction="backward" times="2"/></barline>
</measure>
</part>
</score-partwise>`;
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);
});
});