Skip to content

Art and animation, written as code. Illustrations, loops, interactive web art, launch-videos and scored films in dozens of styles, identical on every render.

music craft: craft.ts measures how a piece is written (melody contour, leap recovery, motif returns, phrase ends, climax, ranges; cadences, voice leading, low-interval mud; syncopation, density, groove consistency, chord-change alignment; tension curve; mood fit), genre-aware, advisory; music.mjs craft + check prints it, only impossible ranges fail; 53 unit checks

Alex Greenshpun3ad9e490c14f
diff --git a/skills/anidoodle/engine/src/canvas-core/music/craft.ts b/skills/anidoodle/engine/src/canvas-core/music/craft.ts
new file mode 100644
index 0000000..eb09554
--- /dev/null
+++ b/skills/anidoodle/engine/src/canvas-core/music/craft.ts
@@ -0,0 +1,326 @@
+// CRAFT: measures how a composed piece is WRITTEN (not how it sounds): melody, harmony, rhythm,
+// tension and mood fit, straight from the notes, in milliseconds, no render. Advisory by design:
+// every finding names what it saw, where, and a fix; thresholds are genre-aware (craftTables.ts).
+// Only real errors (a note an acoustic instrument cannot play) are level "error". The composer
+// revises with it (references/music/compose.md, step 7); the theory is references/music/theory/.
+import { beatsPerBar, isCompound, sectionSpans, type Piece, type Note, type Role } from "./plan";
+import { MODES, pcOf, nameOf, type ModeId } from "./theory";
+import { MOODS, type MoodId } from "./tables";
+import { LOW_INTERVAL_LIMIT, INST_RANGE, IC_DISSONANCE, ROOT_TENSION, CRAFT_PROFILE, type CraftProfile } from "./craftTables";
+
+export type Finding = { level: "error" | "warn" | "info"; area: "melody" | "harmony" | "rhythm" | "tension" | "mood" | "range"; msg: string; fix?: string };
+const E = 1e-6;
+const mean = (a: number[]) => (a.length ? a.reduce((x, y) => x + y, 0) / a.length : 0);
+const pct = (x: number) => `${Math.round(x * 100)} %`;
+const mod12 = (x: number) => ((x % 12) + 12) % 12;
+
+/** The low-interval chart is loose by about 2 semitones (instrument-dependent), so craft flags a close interval 3+ semitones under its limit. */
+export const MUD_SLACK = 2;
+// ------------------------------------------------------------------ piece context
+type Span = { id: string; kind: string; a: number; b: number };
+type Ctx = { p: Piece; bpb: number; compound: boolean; spans: Span[]; notes: Note[]; mel: Note[]; profile: CraftProfile; keyAt: (t: number) => { tonic: number; mode: ModeId }; end: number };
+/** One voice: the top note of each onset (bass: the bottom). */
+export const topVoice = (ns: Note[], low = false) => { const by = new Map<number, Note>(); for (const n of ns) { const k = Math.round(n.t * 96), o = by.get(k); if (!o || (low ? n.p < o.p : n.p > o.p)) by.set(k, n); } return [...by.values()].sort((a, b) => a.t - b.t); };
+
+const context = (p: Piece): Ctx => {
+ const bpb = beatsPerBar(p.plan.meter), plan = sectionSpans(p.plan);
+ const spans: Span[] = p.arrangement?.length ? p.arrangement.map((x) => ({ id: x.id, kind: x.kind, a: x.from * bpb, b: (x.from + x.bars) * bpb })) : plan.map((x) => ({ id: x.s.id, kind: x.s.id, a: x.a, b: x.b }));
+ const notes = p.parts.flatMap((pt) => pt.notes.map((n) => ({ ...n, inst: pt.inst, part: pt.id }))) as (Note & { inst: string; part: string })[];
+ const keyAt = (t: number) => { const s = plan.find((x) => t >= x.a - E && t < x.b - E) ?? plan[plan.length - 1]; return { tonic: pcOf(s.s.key.replace(/m$/, "")), mode: s.s.mode }; };
+ const end = Math.max(spans[spans.length - 1]?.b ?? 0, ...notes.map((n) => n.t + n.d));
+ return { p, bpb, compound: isCompound(p.plan.meter), spans, notes, mel: topVoice(notes.filter((n) => n.role === "melody")), profile: CRAFT_PROFILE[p.plan.style] ?? CRAFT_PROFILE.folk, keyAt, end };
+};
+const where = (c: Ctx, t: number) => { const bar = Math.floor(t / c.bpb + E); return `bar ${bar + 1} beat ${+((t - bar * c.bpb) + 1).toFixed(2)}`; };
+const sectionOf = (c: Ctx, t: number) => c.spans.find((s) => t >= s.a - E && t < s.b - E);
+
+/** Metric weight of a position (GTTM-style hierarchy, Lerdahl & Jackendoff 1983): downbeat 4, mid-bar 3 (4/4, 12/8), beat 2, eighth 1, sixteenth 0, finer -1. */
+export const metricWeight = (t: number, bpb: number, compound: boolean) => {
+ const pos = (((t % bpb) + bpb) % bpb), on = (step: number) => Math.abs(pos / step - Math.round(pos / step)) < 1e-4;
+ if (on(bpb) || Math.abs(pos - bpb) < 1e-4) return 4;
+ if ((bpb === 4) && on(2)) return 3;
+ if (on(1)) return 2;
+ if (compound ? on(1 / 3) : on(0.5)) return 1;
+ if (compound ? on(1 / 6) : on(0.25)) return 0;
+ return -1;
+};
+
+/** Longuet-Higgins & Lee (1984) style syncopation: an onset on a weak position followed by silence (a rest or its own tie) over a stronger one. Returns mean syncopation per onset and the syncopated share. */
+export const syncopation = (onsets: number[], bpb: number, compound: boolean) => {
+ const ts = [...new Set(onsets.map((t) => Math.round(t * 96) / 96))].sort((a, b) => a - b), g = compound ? 1 / 6 : 0.25;
+ let sum = 0, n = 0;
+ for (let i = 0; i < ts.length - 1; i++) {
+ const t = ts[i], nx = ts[i + 1], w = metricWeight(t, bpb, compound); let mx = -2;
+ for (let k = Math.floor(t / g + E) + 1; k * g < nx - E && k * g < t + bpb; k++) mx = Math.max(mx, metricWeight(k * g, bpb, compound));
+ if (mx > w) { sum += mx - w; n++; }
+ }
+ return { index: ts.length > 1 ? sum / (ts.length - 1) : 0, share: ts.length > 1 ? n / (ts.length - 1) : 0 };
+};
+
+// ------------------------------------------------------------------ harmony helpers
+const sounding = (c: Ctx, t: number, roles: Role[]) => c.notes.filter((n) => roles.includes(n.role) && n.t <= t + E && n.t + n.d > t + E);
+/** The harmony's pitch classes at t: every accompaniment and bass note of the current harmony span (so broken chords and arpeggios count whole), else what sounds. */
+const chordPcs = (c: Ctx, t: number) => {
+ const hs = c.p.harmony, i = hs.reduce((k, h, j) => (h.t <= t + E ? j : k), -1);
+ if (i < 0) return new Set(sounding(c, t, ["accomp", "inner", "bass"]).map((n) => mod12(n.p)));
+ const a = hs[i].t, b = hs.slice(i + 1).find((h) => h.t > a + E)?.t ?? Infinity;
+ return new Set(c.notes.filter((n) => (n.role === "accomp" || n.role === "inner" || n.role === "bass") && ((n.t >= a - E && n.t < b - E) || (n.t < a && n.t + n.d > a + E))).map((n) => mod12(n.p)));
+};
+/** The root of the harmony at t: the lowest bass note sounding (or starting within the first beat), else the lowest chord note. */
+const rootAt = (c: Ctx, t: number) => {
+ const b = sounding(c, t + 0.01, ["bass"]); const bs = b.length ? b : c.notes.filter((n) => n.role === "bass" && n.t >= t - E && n.t < t + 1);
+ const pool = bs.length ? bs : sounding(c, t + 0.01, ["accomp", "inner"]);
+ return pool.length ? mod12(Math.min(...pool.map((n) => n.p))) : -1;
+};
+const changes = (c: Ctx) => { const hs = c.p.harmony.slice().sort((a, b) => a.t - b.t), out: { t: number; name: string }[] = []; for (const h of hs) if (!out.length || out[out.length - 1].name !== h.name) out.push(h); return out; };
+const DEG = ["1", "b2", "2", "b3", "3", "4", "#4", "5", "b6", "6", "b7", "7"];
+
+export const classifyCadence = (prev: number, last: number): string => {
+ if (last === 0) return prev === 7 || prev === 11 ? "authentic" : prev === 5 ? "plagal" : prev === 10 || prev === 8 || prev === 1 || prev === 2 ? "modal" : prev === 0 ? "held" : "arrival";
+ if (last === 7) return "half"; if (prev === 7 && (last === 9 || last === 8)) return "deceptive"; return "open";
+};
+
+// ------------------------------------------------------------------ melody
+const phrasesOf = (c: Ctx) => {
+ const out: Note[][] = []; let cur: Note[] = [];
+ c.mel.forEach((n, i) => { const nx = c.mel[i + 1]; cur.push(n);
+ const gap = nx ? nx.t - (n.t + n.d) : Infinity, cross = nx && sectionOf(c, nx.t) !== sectionOf(c, n.t);
+ if (!nx || gap >= 0.75 - E || cross || (n.d >= 2 && nx)) { if (cur.length >= 2) out.push(cur); cur = []; } });
+ return out;
+};
+/** Huron (1996) contour class: first, mean of the middle, last. */
+export const contourClass = (ph: Note[]) => {
+ const f = ph[0].p, l = ph[ph.length - 1].p, mid = ph.length > 2 ? mean(ph.slice(1, -1).map((n) => n.p)) : (f + l) / 2, s = (x: number) => (x > 1 ? 1 : x < -1 ? -1 : 0);
+ const a = s(mid - f), b = s(l - mid);
+ return a > 0 && b < 0 ? "arch" : a < 0 && b > 0 ? "bowl" : a >= 0 && b >= 0 && (a || b) ? "ascending" : a <= 0 && b <= 0 && (a || b) ? "descending" : "level";
+};
+export type Development = "exact" | "sequence" | "varied" | "new";
+/** How a phrase relates to the phrases before it (Schoenberg, Fundamentals of Musical Composition, 1967: repetition, sequence, variation): its opening rhythm and intervals compared to each earlier phrase's. */
+export const development = (ph: Note[], earlier: Note[][]): Development => {
+ const ioi = (x: Note[]) => x.slice(1).map((n, i) => Math.round((n.t - x[i].t) * 12)), iv = (x: Note[]) => x.slice(1).map((n, i) => n.p - x[i].p);
+ const rank: Record<Development, number> = { exact: 3, sequence: 2, varied: 1, new: 0 }; let best: Development = "new";
+ for (const q of earlier) { const L = Math.min(ph.length, q.length) - 1; if (L < 2) continue; // a two-note fragment matches anything: compare cells of 3+ notes
+ const r1 = ioi(ph), r2 = ioi(q), i1 = iv(ph), i2 = iv(q); let rs = 0, is = 0, cs = 0;
+ for (let k = 0; k < L; k++) { if (r1[k] === r2[k]) rs++; if (i1[k] === i2[k]) is++; if (Math.sign(i1[k]) === Math.sign(i2[k])) cs++; }
+ rs /= L; is /= L; cs /= L; const same = ph.length === q.length;
+ const d: Development = rs === 1 && is === 1 && same ? (ph[0].p === q[0].p ? "exact" : "sequence") : rs >= 0.6 || is >= 0.6 || (cs >= 0.75 && rs >= 0.4) ? "varied" : "new";
+ if (rank[d] > rank[best]) best = d; }
+ return best;
+};
+/** Motif repetition: the share of phrases that bring back an earlier one (exact, as a sequence, or varied), and the share of bars that are exact copies of an earlier bar. */
+export const repetition = (mel: Note[], bpb: number, phrases: Note[][] = [mel]) => {
+ // two segmentations: phrases (split at rests) and bar-length cells (a motif often lives in a bar inside a long phrase); the one that hears more returns wins
+ const profile = (segs: Note[][]) => { const dev: Record<Development, number> = { exact: 0, sequence: 0, varied: 0, new: 0 }; segs.forEach((x, i) => { if (i) dev[development(x, segs.slice(0, i))]++; }); const k = Math.max(1, segs.length - 1); return { dev, k, index: segs.length > 1 ? (dev.exact + dev.sequence + dev.varied) / k : 0 }; };
+ const byBar = new Map<number, Note[]>(); for (const x of mel) { const b = Math.floor(x.t / bpb + E); (byBar.get(b) ?? byBar.set(b, []).get(b)!).push(x); }
+ const A = profile(phrases), B = profile([...byBar].sort((a, b) => a[0] - b[0]).map(([, v]) => v).filter((v) => v.length >= 3)), win = B.index > A.index ? B : A, dev = win.dev, n = win.k;
+ const bars = new Map<number, string>(); for (const n of mel) { const b = Math.floor(n.t / bpb + E); bars.set(b, `${bars.get(b) ?? ""}${Math.round((n.t - b * bpb) * 12)}:${n.p}:${Math.round(n.d * 12)} `); }
+ const seen = new Set<string>(); let copies = 0; for (const [, s] of [...bars].sort((a, b) => a[0] - b[0])) { if (seen.has(s)) copies++; seen.add(s); }
+ return { index: win.index, exact: dev.exact / n, dev, exactBarCopies: bars.size ? copies / bars.size : 0 };
+};
+
+type NCT = "passing" | "neighbour" | "appoggiatura" | "suspension" | "escape" | "other";
+/** Non-chord tones by approach and departure (Aldwell & Schachter, Harmony and Voice Leading, ch. 9-10 terms). */
+const classifyNct = (prev: Note | undefined, n: Note, next: Note | undefined): NCT => {
+ const a = prev ? n.p - prev.p : NaN, b = next ? next.p - n.p : NaN, step = (x: number) => Math.abs(x) >= 1 && Math.abs(x) <= 2;
+ if (prev && prev.p === n.p && step(b) && b < 0) return "suspension";
+ if (step(a) && step(b)) return Math.sign(a) === Math.sign(b) ? "passing" : "neighbour";
+ if (Math.abs(a) > 2 && step(b) && Math.sign(a) !== Math.sign(b)) return "appoggiatura";
+ if (step(a) && Math.abs(b) > 2) return "escape";
+ return "other";
+};
+
+// ------------------------------------------------------------------ the report
+export type CraftReport = {
+ melody: { phrases: number; contours: Record<string, number>; leaps: number; leapsRecovered: number; stepShare: number; range: [string, string] | null; repetition: number; development: Record<Development, number>; exactBarCopies: number; stableEnds: number; lastNote: string | null; climax: { note: string; at: number; times: number } | null; strongBeatChordTones: number; ncts: Record<NCT, number> };
+ harmony: { cadences: { id: string; type: string; motion: string }[]; parallels: number; octaveDoublings: number; innerJumps: number; meanMotion: number; leadUnderChords: number; mud: { at: string; notes: string }[] };
+ rhythm: { syncopation: { melody: number; bass: number; drums: number }; density: { id: string; perBeat: number; layers: number }[]; grooveConsistency: number | null; chordChangesOnBeats: number; repeatedSections: string[] };
+ tension: { sections: { id: string; value: number }[]; peakAt: number; bars: number[] };
+ mood: { mood: MoodId; checks: { what: string; value: string; want: string; ok: boolean }[] };
+ scores: { melody: number; harmony: number; rhythm: number; tension: number; mood: number };
+ findings: Finding[];
+};
+
+export const craftReport = (p: Piece, o: { centroidHz?: number } = {}): CraftReport => {
+ const c = context(p), F: Finding[] = [], add = (f: Finding) => F.push(f), { bpb, compound, mel, profile } = c;
+ const loop = Boolean(p.plan.loop), moodId = (Array.isArray(p.plan.sections[0].mood) ? p.plan.sections[0].mood[0] : p.plan.sections[0].mood) as MoodId;
+ const openOk = profile.openEnding || ["tension", "dread", "eerie", "curious"].includes(moodId);
+
+ // ---------------- melody
+ const phrases = phrasesOf(c), contours: Record<string, number> = {};
+ for (const ph of phrases) { const k = contourClass(ph); contours[k] = (contours[k] ?? 0) + 1; }
+ let leaps = 0, rec = 0, steps = 0, moves = 0; const unrecovered: number[] = [];
+ for (const ph of phrases) for (let i = 1; i < ph.length; i++) {
+ const iv = ph[i].p - ph[i - 1].p; if (iv) { moves++; if (Math.abs(iv) <= 2) steps++; }
+ if (Math.abs(iv) >= 5) { leaps++; const nx = ph[i + 1], pcs = chordPcs(c, ph[i].t);
+ // turned back, or arpeggiating the harmony (the next leap outlines the same chord: a consonant skip, e.g. a fanfare)
+ const arp = nx && Math.abs(nx.p - ph[i].p) >= 3 && Math.sign(nx.p - ph[i].p) === Math.sign(iv) && [ph[i - 1], ph[i], nx].every((n) => pcs.has(mod12(n.p)));
+ if (!nx || Math.sign(nx.p - ph[i].p) === -Math.sign(iv) || nx.p === ph[i].p || arp) rec++; else unrecovered.push(ph[i].t); }
+ }
+ const stepShare = moves ? steps / moves : 1, recShare = leaps ? rec / leaps : 1;
+ if (leaps >= 3 && recShare < 0.6) add({ level: profile.arpeggioMelody ? "info" : "warn", area: "melody", msg: `${leaps - rec} of ${leaps} leaps (a 4th or more) keep going the same way (first at ${where(c, unrecovered[0])})`, fix: "after a leap, turn back by step: the ear expects the gap filled (von Hippel & Huron 2000). Arpeggio idioms are exempt" });
+ if (moves >= 8 && stepShare < 0.35 && !profile.arpeggioMelody) add({ level: "warn", area: "melody", msg: `only ${pct(stepShare)} of melodic moves are steps`, fix: "a singable line is mostly steps (60-80 % in folk and song corpora) with a few leaps for shape; fill leaps in with steps" });
+ if (phrases.length >= 3) { const top = Object.entries(contours).sort((a, b) => b[1] - a[1])[0]; if (top[0] === "level" && top[1] / phrases.length > 0.5) add({ level: "warn", area: "melody", msg: `${top[1]} of ${phrases.length} phrases are level (no rise and fall)`, fix: "give phrases a shape: an arch (rise to a high point, fall home) is the commonest and most satisfying (Huron 1996)" }); }
+ const rep = repetition(mel, bpb, phrases);
+ if (phrases.length >= 4 && rep.index < 0.25) add({ level: "warn", area: "melody", msg: `only ${pct(rep.index)} of phrases (or bars) bring back an earlier one (exact ${rep.dev.exact}, sequence ${rep.dev.sequence}, varied ${rep.dev.varied}, new ${rep.dev.new})`, fix: "state a 2-5 note motif, then bring it back developed (same rhythm new pitches, a sequence a step up, inverted, fragmented): a tune the ear can hold returns to its idea" });
+ // loop idioms (lo-fi, hip-hop, house) repeat on purpose: repetition is the groove's pleasure there, so only near-total copying is flagged
+ if (phrases.length >= 4 && rep.exact > (profile.groove ? 0.85 : 0.6)) add({ level: "warn", area: "melody", msg: `${pct(rep.exact)} of phrases (or bars) are exact copies of an earlier one`, fix: "repeat the motif, but vary it: change the last interval, displace it by an eighth, sequence it a step up, answer it in another register" });
+ // phrase ends and strong beats vs the harmony
+ /** a chord tone; outside common-practice idioms the added 6th and 9th count too (Levine, The Jazz Theory Book, 1995: 6 and 9 are colour tones, not dissonances to resolve) */
+ const isCt = (n: Note) => { const pcs = chordPcs(c, n.t), pc = mod12(n.p); if (pcs.has(pc)) return true; if (profile.parallels === "forbid") return false; const r = rootAt(c, n.t); return r >= 0 && [2, 9].includes(mod12(pc - r)); };
+ let stable = 0; for (const ph of phrases) { const n = ph[ph.length - 1]; if (isCt(n)) stable++; }
+ if (phrases.length >= 3 && stable / phrases.length < 0.6) add({ level: "warn", area: "melody", msg: `only ${stable} of ${phrases.length} phrases end on a chord tone`, fix: "end phrases on chord tones (answers on 1, 3 or 5 of the key); save the unresolved end for a question phrase" });
+ const last = mel[mel.length - 1]; let lastNote: string | null = null;
+ if (last) { const k = c.keyAt(last.t), deg = mod12(last.p - k.tonic), iv = MODES[k.mode].intervals, triad = [0, iv[2], iv[4]].filter((x) => x !== undefined);
+ lastNote = `${nameOf(last.p)} (${DEG[deg]})`;
+ if (!loop && !triad.includes(deg)) add({ level: openOk ? "info" : "warn", area: "melody", msg: `the melody's last note is ${lastNote}, off the home triad`, fix: openOk ? "an open ending suits this mood; keep it if you meant it" : "end on 1 (or 3/5) of the key, or make the unresolved ending a choice the picture asks for" }); }
+ let strong = 0, strongCt = 0; const ncts: Record<NCT, number> = { passing: 0, neighbour: 0, appoggiatura: 0, suspension: 0, escape: 0, other: 0 };
+ mel.forEach((n, i) => { const pcs = chordPcs(c, n.t); if (!pcs.size) return; const ct = isCt(n);
+ if (metricWeight(n.t, bpb, compound) >= 2) { strong++; if (ct) strongCt++; }
+ if (!ct && n.d >= (compound ? 1 / 3 : 0.25) - E) ncts[classifyNct(mel[i - 1], n, mel[i + 1])]++; });
+ const sbct = strong ? strongCt / strong : 1;
+ if (strong >= 8 && sbct < 0.5) add({ level: "warn", area: "melody", msg: `only ${pct(sbct)} of the melody's strong-beat notes are chord tones`, fix: "put chord tones on the beats; keep dissonance for passing and neighbour tones, or as an appoggiatura/suspension that resolves down by step" });
+ if (ncts.other >= 4 && ncts.other > (ncts.passing + ncts.neighbour + ncts.appoggiatura + ncts.suspension)) add({ level: "info", area: "melody", msg: `${ncts.other} non-chord tones are neither approached nor left by step`, fix: "a dissonance reads as intended when a step leads into or out of it" });
+ // range and climax
+ let climax: CraftReport["melody"]["climax"] = null;
+ if (mel.length) { const hi = Math.max(...mel.map((n) => n.p)), first = mel.find((n) => n.p === hi)!, times = mel.filter((n) => n.p === hi).length, at = (first.t - (c.spans[0]?.a ?? 0)) / Math.max(1, c.end - (c.spans[0]?.a ?? 0));
+ climax = { note: nameOf(hi), at, times };
+ if (mel.length >= 16 && !loop && at < 0.3) add({ level: "warn", area: "melody", msg: `the melody peaks (${nameOf(hi)}) at ${pct(at)} of the piece`, fix: "save the highest note for the second half (often around two thirds in): the climax is where the piece arrives" });
+ if (mel.length >= 16 && times > Math.max(3, mel.length / 12)) add({ level: "info", area: "melody", msg: `the top note ${nameOf(hi)} is heard ${times} times`, fix: "one high point per phrase group: a climax heard often stops being one" }); }
+
+ // ---------------- range per part
+ for (const pt of p.parts) { const r = INST_RANGE[pt.inst]; const ns = pt.notes.filter((n) => n.role !== "drum"); if (!r || !ns.length || pt.role === "drum") continue;
+ const lo = Math.min(...ns.map((n) => n.p)), hi = Math.max(...ns.map((n) => n.p));
+ const sweet: [number, number] = pt.role === "bass" && r.play ? [r.play[0], r.sweet[1]] : r.sweet, over = r.play ? Math.max(r.play[0] - lo, hi - r.play[1]) : 0;
+ if (r.play && over > 0) add({ level: over >= 3 ? "error" : "warn", area: "range", msg: `${pt.id} (${pt.inst}) plays ${nameOf(lo)}-${nameOf(hi)}, outside what a ${pt.inst} can play (${nameOf(r.play[0])}-${nameOf(r.play[1])})`, fix: over >= 3 ? "move the part by an octave or give it to an instrument that owns that register" : "a semitone or two past the standard range: extended-range models exist, but moving those notes an octave is safer" });
+ else { const out = ns.filter((n) => n.p < sweet[0] || n.p > sweet[1]).length / ns.length; if (out > 0.25) add({ level: "warn", area: "range", msg: `${pct(out)} of ${pt.id} (${pt.inst}) sits outside its sweet range ${nameOf(sweet[0])}-${nameOf(sweet[1])}`, fix: "move it toward the sweet range (an octave), or pick a voice that lives there" }); } }
+
+ // ---------------- harmony: cadences
+ const ch = changes(c), cadences: CraftReport["harmony"]["cadences"] = [];
+ for (const s of c.spans) { const inS = ch.filter((h) => h.t < s.b - E), lastH = inS[inS.length - 1]; if (!lastH) continue;
+ const k = c.keyAt(s.b - 0.01), lr = rootAt(c, Math.max(lastH.t, s.a)); let pr = -1;
+ for (let i = inS.length - 2; i >= 0; i--) { const r = rootAt(c, inS[i].t); if (r !== lr) { pr = r; break; } }
+ if (lr < 0) continue; const l = mod12(lr - k.tonic), pv = pr < 0 ? l : mod12(pr - k.tonic);
+ cadences.push({ id: s.id, type: classifyCadence(pv, l), motion: `${DEG[pv]}-${DEG[l]}` }); }
+ const lastCad = cadences[cadences.length - 1];
+ if (!loop && lastCad && !["authentic", "plagal", "modal", "held", "arrival"].includes(lastCad.type)) add({ level: openOk ? "info" : "warn", area: "harmony", msg: `the piece ends ${lastCad.type} (${lastCad.motion}), away from home`, fix: openOk ? "an open ending suits this idiom; keep it if you meant it" : "land the last section on the tonic (V-I, IV-I, bVII-I), or the ending sounds cut off" });
+ if (loop && lastCad && ["authentic", "plagal"].includes(lastCad.type)) add({ level: "info", area: "harmony", msg: `the loop's last section cadences home (${lastCad.motion})`, fix: "a loop should lean back into bar 1 (end on V, IV or a turnaround), not close" });
+ if (cadences.length >= 4 && !cadences.slice(0, -1).some((x) => x.type !== "open" && x.type !== "held")) add({ level: "warn", area: "harmony", msg: "no section before the last ends on a cadence (half, authentic, plagal, deceptive)", fix: "let phrases breathe: end the first sections on V (a question) or a deceptive vi, keep the full close for the end" });
+
+ // ---------------- harmony: voice leading of the chord part (+ the bass and the melody as outer voices)
+ // the chord part: the accompaniment part with the most block chords (3+ notes on one onset; arpeggios and single lines are not voicings)
+ const groups = (pt: (typeof p.parts)[number]) => { const by = new Map<number, number[]>(); for (const n of pt.notes) { const k = Math.round(n.t * 96); by.set(k, [...(by.get(k) ?? []), n.p]); } return [...by].filter(([, ps]) => ps.length >= 2).sort((a, b) => a[0] - b[0]).map(([k, ps]) => ({ t: k / 96, ps: [...new Set(ps)].sort((a, b) => a - b) })); };
+ const cands = p.parts.filter((pt) => pt.role === "accomp" || pt.role === "inner").map((pt) => ({ pt, ev: groups(pt) })), big = (x: (typeof cands)[number]) => x.ev.filter((e) => e.ps.length >= 3).length;
+ const main = cands.sort((a, b) => big(b) - big(a) || b.ev.length - a.ev.length)[0], events = main?.ev ?? [];
+ const blockTop = (t: number) => { let top = -1; for (const x of cands) for (const n of x.pt.notes) if (n.t <= t + E && n.t + n.d > t + E && x.ev.some((e) => Math.abs(e.t - n.t) < 1e-3)) top = Math.max(top, n.p); return top; };
+ // parallel 5ths between any two voices, and parallel octaves between the OUTER voices (bass and melody); octaves inside one
+ // chord part are doubling, which orchestration uses on purpose (Adler 2016), except in a choir, whose voices are independent
+ let parallels = 0, inner = 0, doublings = 0, motions: number[] = []; const parEx: string[] = [], choir = main?.pt.inst === "choir";
+ const par = (a1: number, b1: number, a2: number, b2: number, octaves = true) => { const i1 = mod12(b1 - a1), i2 = mod12(b2 - a2); return (i1 === 7 || (octaves && i1 === 0)) && i1 === i2 && a1 !== a2 && Math.sign(a2 - a1) === Math.sign(b2 - b1); };
+ for (let i = 1; i < events.length; i++) { const A = events[i - 1].ps, B = events[i].ps; if (A.join() === B.join()) continue;
+ if (A.length !== B.length) for (const b of B) motions.push(Math.min(...A.map((a) => Math.abs(b - a))));
+ if (A.length === B.length) { for (let v = 0; v < A.length; v++) { const mv = Math.abs(B[v] - A[v]); motions.push(mv); if (v > 0 && v < A.length - 1 && mv > 7) inner++; }
+ for (let x = 0; x < A.length; x++) for (let y = x + 1; y < A.length; y++) { if (par(A[x], A[y], B[x], B[y], choir)) { parallels++; if (parEx.length < 2) parEx.push(where(c, events[i].t)); } else if (par(A[x], A[y], B[x], B[y])) doublings++; } }
+ const bA = sounding(c, events[i - 1].t + 0.01, ["bass"]), bB = sounding(c, events[i].t + 0.01, ["bass"]), mA = sounding(c, events[i - 1].t + 0.01, ["melody"]), mB = sounding(c, events[i].t + 0.01, ["melody"]);
+ if (bA.length && bB.length && mA.length && mB.length) { const lo1 = Math.min(...bA.map((n) => n.p)), lo2 = Math.min(...bB.map((n) => n.p)), hi1 = Math.max(...mA.map((n) => n.p)), hi2 = Math.max(...mB.map((n) => n.p)); if (par(lo1, hi1, lo2, hi2)) { parallels++; if (parEx.length < 2) parEx.push(`${where(c, events[i].t)} (bass-melody)`); } } }
+ const meanMotion = mean(motions);
+ if (parallels) add({ level: profile.parallels === "forbid" ? "warn" : "info", area: "harmony", msg: `${parallels} parallel 5th/octave move(s) (${parEx.join(", ")})`, fix: profile.parallels === "forbid" ? "in this idiom move the voices in contrary or oblique motion, or hold a common tone" : "fine in this idiom (planing, power chords); keep it if it is the sound you want" });
+ if (inner >= 2) add({ level: profile.parallels === "forbid" ? "warn" : "info", area: "harmony", msg: `${inner} inner-voice jump(s) larger than a 5th between chords`, fix: "revoice: keep common tones, move each inner voice to the nearest chord tone" });
+ if (motions.length >= 8 && meanMotion > 4) add({ level: "info", area: "harmony", msg: `the chord voices move ${meanMotion.toFixed(1)} semitones each on average per change`, fix: "smooth voice leading averages 1-3: invert chords so voices move by step" });
+ // lead under the chords
+ let under = 0; for (const n of mel) { const top = blockTop(n.t + 0.01); if (top >= 0 && n.p < top) under++; }
+ const leadUnder = mel.length ? under / mel.length : 0;
+ if (mel.length >= 8 && leadUnder > 0.3) add({ level: "warn", area: "harmony", msg: `the melody sits under the chord voicing's top note ${pct(leadUnder)} of the time`, fix: "crossed voices mask the tune: voice the chords lower or move the melody up (a tenor-register theme works only in a distinct, louder timbre: see the masking guard)" });
+ // mud: low close intervals (low interval limits)
+ const mud = new Map<string, number>(); const pitched = c.notes.filter((n) => n.role !== "drum");
+ for (const t of [...new Set(pitched.map((n) => Math.round(n.t * 96) / 96))]) { const ps = [...new Set(pitched.filter((n) => n.t <= t + E && n.t + n.d > t + E).map((n) => n.p))].sort((a, b) => a - b);
+ for (let i = 1; i < ps.length; i++) { const lim = LOW_INTERVAL_LIMIT[ps[i] - ps[i - 1]]; if (lim !== undefined && ps[i - 1] < lim - MUD_SLACK) { const k = `${nameOf(ps[i - 1])}+${nameOf(ps[i])}`; if (!mud.has(k)) mud.set(k, t); } } }
+ const mudList = [...mud].map(([notes, t]) => ({ at: where(c, t), notes }));
+ if (mudList.length) add({ level: "warn", area: "harmony", msg: `${mudList.length} close interval(s) below their low limit: ${mudList.slice(0, 3).map((m) => `${m.notes} at ${m.at}`).join("; ")}`, fix: "low down keep only octaves and 5ths (thirds from about C3 up, seconds from about E3 up): spread the voicing or lift the upper note an octave" });
+
+ // ---------------- rhythm
+ const onsetsOf = (f: (n: Note & { inst?: string }) => boolean) => c.notes.filter(f).map((n) => n.t);
+ const syn = { melody: syncopation(mel.map((n) => n.t), bpb, compound).index, bass: syncopation(onsetsOf((n) => n.role === "bass"), bpb, compound).index, drums: syncopation(onsetsOf((n) => n.role === "drum" && /kick|snare/.test((n as { inst?: string }).inst ?? "")), bpb, compound).index };
+ const hasDrums = c.notes.some((n) => n.role === "drum");
+ if (profile.groove && hasDrums && syn.bass + syn.drums < 0.02 && syn.melody < 0.05) add({ level: "warn", area: "rhythm", msg: "no syncopation in the melody, bass or kick/snare", fix: "a groove idiom wants some: push a bass note or a melody note an eighth early (an anticipation) or tie it over the beat; medium syncopation grooves most (Witek et al. 2014)" });
+ if (syn.melody > 1.2) add({ level: "info", area: "rhythm", msg: `the melody is heavily syncopated (${syn.melody.toFixed(2)})`, fix: "if the tune stops sounding anchored, land the motif's first or last note on a beat" });
+ const density = c.spans.map((s) => { const ns = c.notes.filter((n) => n.t >= s.a - E && n.t < s.b - E), ts = new Set(ns.map((n) => Math.round(n.t * 48))); return { id: s.id, perBeat: ts.size / Math.max(E, s.b - s.a), layers: new Set(ns.map((n) => (n as { part?: string }).part)).size }; });
+ if (density.length >= 3) { const d = density.map((x) => x.perBeat), m = mean(d); if (d.every((x) => Math.abs(x - m) <= 0.15 * m) && new Set(density.map((x) => x.layers)).size === 1) add({ level: "warn", area: "rhythm", msg: "every section has the same density and the same layers", fix: "contrast sections: thin one (drums out, one line), build another (more onsets, a new layer); a section that only repeats is dead air" }); }
+ // groove consistency: kick+snare pattern per bar vs the section's commonest bar
+ const gc: number[] = [];
+ for (const s of c.spans) { const bars = new Map<number, Set<number>>();
+ for (const n of c.notes) if (n.role === "drum" && /kick|snare/.test((n as { inst?: string }).inst ?? "") && n.t >= s.a - E && n.t < s.b - E) { const b = Math.floor(n.t / bpb + E); (bars.get(b) ?? bars.set(b, new Set()).get(b)!).add(Math.round((n.t - b * bpb) * 12)); }
+ if (bars.size < 2) continue; const keys = [...bars.values()].map((x) => [...x].sort((a, b) => a - b).join()), cnt = new Map<string, number>(); for (const k of keys) cnt.set(k, (cnt.get(k) ?? 0) + 1);
+ const modeK = [...cnt].sort((a, b) => b[1] - a[1])[0][0], ref = new Set(modeK.split(",").filter(Boolean).map(Number));
+ for (const x of bars.values()) { const inter = [...x].filter((v) => ref.has(v)).length; gc.push(inter / (x.size + ref.size - inter || 1)); } }
+ const grooveConsistency = gc.length ? mean(gc) : null;
+ if (grooveConsistency !== null && grooveConsistency < 0.45) add({ level: "warn", area: "rhythm", msg: `the kick/snare pattern changes almost every bar (consistency ${grooveConsistency.toFixed(2)})`, fix: "a groove is a repeated pattern with small variations: keep the kick and backbeat stable, vary the hats and the fill bar" });
+ const chg = ch.filter((h) => h.t > E), onBeat = chg.filter((h) => metricWeight(h.t, bpb, compound) >= 2).length, cob = chg.length ? onBeat / chg.length : 1;
+ if (chg.length >= 4 && cob < 0.75) add({ level: "warn", area: "rhythm", msg: `${pct(1 - cob)} of chord changes fall between beats`, fix: "change chords on strong beats (an eighth-note anticipation is fine); off-beat changes blur the metre" });
+ // dead air: a section that repeats the previous one note for note in every pitched part
+ const repeated: string[] = [], sig = (s: Span) => c.notes.filter((n) => n.role !== "drum" && n.t >= s.a - E && n.t < s.b - E).map((n) => `${(n as { part?: string }).part}:${Math.round((n.t - s.a) * 48)}:${n.p}:${Math.round(n.d * 48)}`).sort().join("|");
+ for (let i = 1; i < c.spans.length; i++) { const A = c.spans[i - 1], B = c.spans[i]; if (B.b - B.a >= 4 * bpb - E && Math.abs((A.b - A.a) - (B.b - B.a)) < E && sig(A) && sig(A) === sig(B)) repeated.push(B.id); }
+ if (repeated.length) add({ level: "warn", area: "rhythm", msg: `section(s) ${repeated.join(", ")} repeat the previous section note for note`, fix: "vary the repeat: the motif an octave up, a counter line, thinner or fuller layers, a new last bar" });
+
+ // ---------------- tension curve (per bar)
+ const nBars = Math.max(1, Math.ceil(c.end / bpb - E)), melRange = mel.length ? [Math.min(...mel.map((n) => n.p)), Math.max(...mel.map((n) => n.p))] : [60, 72];
+ const raw = Array.from({ length: nBars }, (_, b) => { const a = b * bpb, z = a + bpb, ns = c.notes.filter((n) => n.t >= a - E && n.t < z - E);
+ const h = ch.filter((x) => x.t <= a + E).pop(), k = c.keyAt(a), r = rootAt(c, h ? Math.max(h.t, a) : a), fn = r < 0 ? 0 : ROOT_TENSION[mod12(r - k.tonic)];
+ const pcs = [...new Set(sounding(c, a + 0.01, ["accomp", "inner", "bass", "melody", "color"]).map((n) => mod12(n.p)))]; let ds = 0, pr = 0; for (let i = 0; i < pcs.length; i++) for (let j = i + 1; j < pcs.length; j++) { const ic = Math.min(mod12(pcs[j] - pcs[i]), 12 - mod12(pcs[j] - pcs[i])); ds += IC_DISSONANCE[ic]; pr++; }
+ const mb = mel.filter((n) => n.t >= a - E && n.t < z - E), reg = mb.length ? (mean(mb.map((n) => n.p)) - melRange[0]) / Math.max(1, melRange[1] - melRange[0]) : 0;
+ return { fn, diss: pr ? ds / pr : 0, dens: new Set(ns.map((n) => Math.round(n.t * 48))).size / bpb, reg, loud: ns.length ? mean(ns.map((n) => n.v)) * Math.min(1, new Set(ns.map((n) => (n as { part?: string }).part)).size / 6) : 0 }; });
+ const mx = (f: (x: (typeof raw)[number]) => number) => Math.max(E, ...raw.map(f));
+ const md = mx((x) => x.dens), ml = mx((x) => x.loud);
+ const bars = raw.map((x) => +(0.25 * x.fn + 0.2 * x.diss + 0.25 * (x.dens / md) + 0.15 * x.reg + 0.15 * (x.loud / ml)).toFixed(3));
+ const tsec = c.spans.map((s) => ({ id: s.id, value: +mean(bars.slice(Math.max(0, Math.floor(s.a / bpb + E)), Math.ceil(s.b / bpb - E))).toFixed(2) }));
+ const peakBar = bars.indexOf(Math.max(...bars)), peakAt = (peakBar + 0.5) / nBars;
+ if (tsec.length >= 3) { const vs = tsec.map((x) => x.value), rng = Math.max(...vs) - Math.min(...vs);
+ if (rng < 0.1) add({ level: "warn", area: "tension", msg: `the tension curve is flat (sections span ${rng.toFixed(2)})`, fix: "plan the arc before the notes: rise (denser, higher, leaning chords), a dip before the peak, then release" });
+ const peakS = vs.indexOf(Math.max(...vs));
+ if (!loop && peakS === 0) add({ level: "warn", area: "tension", msg: `the first section (${tsec[0].id}) is the most intense`, fix: "start below the peak so the piece has somewhere to go" });
+ if (!loop && !openOk && vs[vs.length - 1] >= Math.max(...vs) - 1e-9) add({ level: "warn", area: "tension", msg: `the last section (${tsec[tsec.length - 1].id}) is the most intense: no release`, fix: "after the peak, release: fewer layers, home chord, the motif's last word" }); }
+
+ // ---------------- mood fit (MOODS: tables.ts, grounded in Gabrielsson & Lindström 2010)
+ const M = MOODS[moodId], checks: CraftReport["mood"]["checks"] = [];
+ if (M) { const tempo = p.plan.tempo, inR = (x: number, r: [number, number], slack = 0) => x >= r[0] * (1 - slack) && x <= r[1] * (1 + slack);
+ checks.push({ what: "tempo", value: `${tempo}`, want: `${M.tempo[0]}-${M.tempo[1]}`, ok: inR(tempo, M.tempo, 0.1) });
+ const modes = [...new Set(p.plan.sections.map((s) => s.mode))], fam = (m: ModeId) => (MODES[m].intervals.includes(4) ? "major" : MODES[m].intervals.includes(3) ? "minor" : "other");
+ // the major/minor family is the emotion cue (Hevner 1935; Gabrielsson & Lindström 2010); the exact mode is colour, so a mode of the right family passes
+ checks.push({ what: "mode", value: modes.join("/"), want: M.modes.join("/"), ok: modes.some((m) => M.modes.includes(m) || M.modes.some((w) => fam(w) === fam(m) && fam(m) !== "other")) });
+ if (mel.length) { const med = mel.map((n) => n.p).sort((a, b) => a - b)[Math.floor(mel.length / 2)]; checks.push({ what: "melody register", value: nameOf(med), want: `${nameOf(M.register[0])}-${nameOf(M.register[1])}`, ok: med >= M.register[0] - 3 && med <= M.register[1] + 3 }); }
+ // pitched onsets: a hat's 16ths are groove, not the mood's pace of events
+ const allOn = new Set(c.notes.filter((n) => n.role !== "drum").map((n) => Math.round(n.t * 48))).size / Math.max(E, c.end); checks.push({ what: "note onsets/beat", value: allOn.toFixed(2), want: `${M.onsetsPerBeat[0]}-${M.onsetsPerBeat[1]}`, ok: inR(allOn, M.onsetsPerBeat, 0.25) });
+ if (chg.length >= 2) { const hr = (c.end / bpb) / (chg.length + 1); checks.push({ what: "bars per chord", value: hr.toFixed(2), want: `${M.harmonicRhythmBars[0]}-${M.harmonicRhythmBars[1]}`, ok: hr >= M.harmonicRhythmBars[0] / 2 && hr <= M.harmonicRhythmBars[1] * 2 }); }
+ if (o.centroidHz !== undefined && M.centroidHz) checks.push({ what: "brightness (centroid Hz)", value: `${Math.round(o.centroidHz)}`, want: `${M.centroidHz[0]}-${M.centroidHz[1]}`, ok: inR(o.centroidHz, M.centroidHz, 0.2) });
+ const bad = checks.filter((x) => !x.ok);
+ if (bad.length) add({ level: "warn", area: "mood", msg: `for "${moodId}": ${bad.map((x) => `${x.what} ${x.value} (typical ${x.want})`).join(", ")}`, fix: "either change these toward the mood (tempo, mode, register, density are the strongest emotion cues: Gabrielsson & Lindström 2010), or declare the mood you are actually writing" }); }
+
+ const s01 = (xs: number[]) => +Math.max(0, Math.min(1, mean(xs))).toFixed(2);
+ const scores = {
+ melody: s01([recShare, Math.min(1, stepShare / 0.6), Math.min(1, rep.index / 0.5), 1 - Math.max(0, rep.exact - 0.4), phrases.length ? stable / phrases.length : 1, sbct]),
+ harmony: s01([mudList.length ? Math.max(0, 1 - mudList.length / 6) : 1, profile.parallels === "forbid" ? Math.max(0, 1 - parallels / 4) : 1, 1 - leadUnder, meanMotion ? Math.min(1, 3 / meanMotion) : 1]),
+ rhythm: s01([cob, grooveConsistency ?? 1, repeated.length ? 0.5 : 1, profile.groove && hasDrums ? Math.min(1, (syn.bass + syn.drums + syn.melody) / 0.15) : 1]),
+ tension: s01([tsec.length >= 3 ? Math.min(1, (Math.max(...tsec.map((x) => x.value)) - Math.min(...tsec.map((x) => x.value))) / 0.2) : 1, loop || peakAt >= 0.3 ? 1 : 0.5]),
+ mood: checks.length ? +(checks.filter((x) => x.ok).length / checks.length).toFixed(2) : 1,
+ };
+ return {
+ melody: { phrases: phrases.length, contours, leaps, leapsRecovered: rec, stepShare, range: mel.length ? [nameOf(Math.min(...mel.map((n) => n.p))), nameOf(Math.max(...mel.map((n) => n.p)))] : null, repetition: rep.index, development: rep.dev, exactBarCopies: rep.exactBarCopies, stableEnds: phrases.length ? stable / phrases.length : 1, lastNote, climax, strongBeatChordTones: sbct, ncts },
+ harmony: { cadences, parallels, octaveDoublings: doublings, innerJumps: inner, meanMotion, leadUnderChords: leadUnder, mud: mudList },
+ rhythm: { syncopation: syn, density, grooveConsistency, chordChangesOnBeats: cob, repeatedSections: repeated },
+ tension: { sections: tsec, peakAt, bars }, mood: { mood: moodId, checks }, scores, findings: F,
+ };
+};
+
+/** The report as text for tools/music.mjs (check and craft). */
+export const craftText = (r: CraftReport) => {
+ const f2 = (x: number) => x.toFixed(2), spark = "▁▂▃▄▅▆▇█", m = r.melody, h = r.harmony, y = r.rhythm;
+ const o = [`craft melody ${f2(r.scores.melody)} | harmony ${f2(r.scores.harmony)} | rhythm ${f2(r.scores.rhythm)} | tension ${f2(r.scores.tension)} | mood ${f2(r.scores.mood)} (advisory: scores guide revision; only errors fail)`,
+ ` melody ${m.phrases} phrases (${Object.entries(m.contours).map(([k, v]) => `${k} ${v}`).join(", ") || "-"}) | steps ${pct(m.stepShare)} | leaps ${m.leaps}, turned back ${m.leapsRecovered} | range ${m.range ? m.range.join("-") : "-"} | motif returns ${pct(m.repetition)} (exact ${m.development.exact}, sequence ${m.development.sequence}, varied ${m.development.varied}, new ${m.development.new}) | phrase ends on chord tones ${pct(m.stableEnds)} | strong beats on chord tones ${pct(m.strongBeatChordTones)}`,
+ ` climax ${m.climax ? `${m.climax.note} at ${pct(m.climax.at)} (x${m.climax.times})` : "-"} | last note ${m.lastNote ?? "-"} | non-chord tones: ${Object.entries(m.ncts).filter(([, v]) => v).map(([k, v]) => `${k} ${v}`).join(", ") || "none"}`,
+ ` harmony cadences ${h.cadences.map((x) => `${x.id} ${x.type} (${x.motion})`).join(", ") || "-"}`,
+ ` voice motion ${h.meanMotion.toFixed(1)} st/voice | parallel 5ths/8ves ${h.parallels} (octave doublings ${h.octaveDoublings}) | inner jumps ${h.innerJumps} | melody under chords ${pct(h.leadUnderChords)} | mud ${h.mud.length}`,
+ ` rhythm syncopation melody ${f2(y.syncopation.melody)} bass ${f2(y.syncopation.bass)} kick/snare ${f2(y.syncopation.drums)} | groove consistency ${y.grooveConsistency === null ? "-" : f2(y.grooveConsistency)} | chord changes on beats ${pct(y.chordChangesOnBeats)}`,
+ ` density (onsets/beat, layers) ${y.density.map((d) => `${d.id} ${d.perBeat.toFixed(1)}/${d.layers}`).join(" ")}`,
+ ` tension ${r.tension.sections.map((s) => `${s.id} ${spark[Math.min(7, Math.floor(s.value * 8))]}${f2(s.value)}`).join(" ")} | bars ${r.tension.bars.map((v) => spark[Math.min(7, Math.floor(v * 8))]).join("")} | peak at ${pct(r.tension.peakAt)}`,
+ ` mood ${r.mood.mood}: ${r.mood.checks.map((c) => `${c.what} ${c.value} ${c.ok ? "ok" : `(typical ${c.want})`}`).join(" | ")}`];
+ for (const f of r.findings) o.push(`${f.level === "error" ? "ERROR " : f.level === "warn" ? "CRAFT " : "note "} ${f.area}: ${f.msg}${f.fix ? `\n fix: ${f.fix}` : ""}`);
+ return o.join("\n");
+};
diff --git a/skills/anidoodle/engine/src/canvas-core/music/craftTables.ts b/skills/anidoodle/engine/src/canvas-core/music/craftTables.ts
new file mode 100644
index 0000000..aa90c18
--- /dev/null
+++ b/skills/anidoodle/engine/src/canvas-core/music/craftTables.ts
@@ -0,0 +1,84 @@
+// CRAFT TABLES: the data behind craft.ts, with where each number comes from. Guides, not laws:
+// every threshold is a starting point that craft.ts turns into an advisory, never a silent rewrite.
+// Theory pages: references/music/theory/*.md.
+import type { InstId } from "./plan";
+import type { StyleId } from "./tables";
+
+/**
+ * Low interval limits: the lowest BOTTOM note (MIDI, C4 = 60) at which an interval of n semitones
+ * still sounds clear rather than muddy. The chart circulated in Berklee arranging teaching (e.g.
+ * Pease & Pullig, Modern Jazz Voicings, 2001); the physics is Plomp & Levelt (1965): two partials
+ * inside one critical band (about 100 Hz wide below 500 Hz) roughen, so close intervals low down beat.
+ * Unisons and octaves have no limit. Loose by +-2 semitones: a concert grand gets away with more.
+ */
+export const LOW_INTERVAL_LIMIT: Record<number, number> = {
+ 1: 52 /* m2 E3 */, 2: 51 /* M2 Eb3 */, 3: 48 /* m3 C3 */, 4: 46 /* M3 Bb2 */, 5: 46 /* P4 Bb2 */, 6: 47 /* tritone B2 */,
+ 7: 34 /* P5 Bb1 */, 8: 41 /* m6 F2 */, 9: 41 /* M6 F2 */, 10: 41 /* m7 F2 */, 11: 41 /* M7 F2 */, 13: 40 /* m9 E2 */, 14: 39 /* M9 Eb2 */,
+};
+
+/**
+ * Instrument ranges (MIDI, sounding pitch). `play`: what the instrument can physically play (a note
+ * outside is a real error for an acoustic instrument); `sweet`: where it sounds its best (outside is
+ * a warning). Sources: Adler, The Study of Orchestration (4th ed., 2016); Rimsky-Korsakov, Principles
+ * of Orchestration (1913); maker specs for Rhodes/Wurlitzer/marimba. Synth voices have no physical
+ * limit (`play` null): only the sweet spot is advised.
+ */
+export const INST_RANGE: Partial<Record<InstId, { play: [number, number] | null; sweet: [number, number] }>> = {
+ piano: { play: [21, 108], sweet: [36, 96] },
+ musicBox: { play: null /* combs vary from 18 to 144 teeth */, sweet: [60, 96] },
+ bell: { play: [48, 108], sweet: [60, 98] },
+ celesta: { play: [60, 108], sweet: [72, 103] },
+ marimba: { play: [36, 96], sweet: [45, 88] },
+ vibes: { play: [53, 89], sweet: [53, 86] },
+ glockenspiel: { play: [79, 108], sweet: [79, 103] },
+ harp: { play: [23, 104], sweet: [36, 91] },
+ guitar: { play: [38, 88], sweet: [40, 81] },
+ strings: { play: [28, 103], sweet: [36, 93] },
+ bowedSolo: { play: [36, 103], sweet: [43, 91] },
+ brass: { play: [28, 86], sweet: [40, 79] },
+ woodwind: { play: [50, 98], sweet: [55, 91] },
+ choir: { play: [36, 86], sweet: [43, 79] },
+ organ: { play: [24, 108], sweet: [36, 96] },
+ pipeOrgan: { play: [12, 108], sweet: [24, 96] },
+ timpani: { play: [36, 60], sweet: [38, 57] },
+ bass: { play: [23, 72], sweet: [28, 55] },
+ ePiano: { play: [28, 100], sweet: [40, 91] },
+ wurlitzer: { play: [33, 96], sweet: [40, 88] },
+ sub: { play: null, sweet: [24, 52] },
+ warmPad: { play: null, sweet: [43, 91] },
+ softPluck: { play: null, sweet: [48, 96] },
+ leadSynth: { play: null, sweet: [52, 96] },
+ pulse: { play: null, sweet: [48, 96] },
+ triangle: { play: null, sweet: [28, 76] },
+ fmBell: { play: null, sweet: [55, 103] },
+};
+
+/**
+ * Interval-class dissonance 0..1 from Huron (1994), "Interval-class content of equally tempered
+ * pitch-class sets", Music Perception 11(3): consonance ratings ic1 -1.428, ic2 -0.582, ic3 0.594,
+ * ic4 0.386, ic5 1.240, ic6 -0.453, rescaled so ic5 (fourth/fifth) = 0 and ic1 (semitone) = 1.
+ */
+export const IC_DISSONANCE = [0, 1, 0.683, 0.242, 0.32, 0, 0.635];
+
+/** Harmonic-function tension of a chord root, by semitones above the key's tonic (Lerdahl, Tonal Pitch Space, 2001, simplified: home 0, dominant high, borrowed and chromatic roots in between). */
+export const ROOT_TENSION = [0, 0.7, 0.55, 0.45, 0.35, 0.5, 0.8, 0.8, 0.45, 0.3, 0.5, 0.85];
+
+/** What a style allows. Genre-aware so the checks advise, never impose one idiom on another. */
+export type CraftProfile = {
+ /** parallel 5ths/8ves between voices: "forbid" in common-practice idioms (warn), "allow" where planing and power chords are the idiom (info) */
+ parallels: "forbid" | "allow";
+ /** melodies built from arpeggios and ostinatos: unrecovered leaps are the idiom, not a fault */
+ arpeggioMelody: boolean;
+ /** a groove idiom: expects some syncopation in the bass and drums (Witek et al. 2014: medium syncopation grooves most) */
+ groove: boolean;
+ /** an ending away from home is idiomatic (drones, suspense, ambient washes) */
+ openEnding: boolean;
+};
+const P = (parallels: "forbid" | "allow", arpeggioMelody = false, groove = false, openEnding = false): CraftProfile => ({ parallels, arpeggioMelody, groove, openEnding });
+export const CRAFT_PROFILE: Record<StyleId, CraftProfile> = {
+ musicBox: P("forbid", true), nocturne: P("forbid"), lullaby: P("forbid"), choral: P("forbid"), orchestral: P("forbid"), cinematic: P("forbid"),
+ folk: P("allow"), minimalist: P("allow", true, false, true), ambient: P("allow", false, false, true), suspense: P("allow", false, false, true),
+ jazz: P("allow", false, true), lofi: P("allow", false, true), lofiElectronic: P("allow", false, true), hipHop: P("allow", false, true),
+ house: P("allow", true, true), drive: P("allow", true, true), synthwave: P("allow", true, true), rock: P("allow", false, true),
+ playful: P("allow", false, true), world: P("allow", false, true), chiptune: P("allow", true, true),
+};
diff --git a/skills/anidoodle/engine/src/canvas-core/music/index.ts b/skills/anidoodle/engine/src/canvas-core/music/index.ts
index 33e3f37..fe96de7 100644
--- a/skills/anidoodle/engine/src/canvas-core/music/index.ts
+++ b/skills/anidoodle/engine/src/canvas-core/music/index.ts
@@ -13,6 +13,8 @@ export * from "./vocab";
export * from "./vocabTrim";
export * from "./compose";
export * from "./novelty";
+export * from "./craft";
+export * from "./craftTables";
export * from "./calibration";
export * as instruments from "./instruments";
export * from "./sfx";
diff --git a/skills/anidoodle/engine/test/craft.test.ts b/skills/anidoodle/engine/test/craft.test.ts
new file mode 100644
index 0000000..8c13848
--- /dev/null
+++ b/skills/anidoodle/engine/test/craft.test.ts
@@ -0,0 +1,133 @@
+// CRAFT TESTS (music/craft.ts): every metric on a small good example and a small bad one, so a
+// threshold that drifts or a metric that stops seeing is caught with no render and no human.
+import { craftReport, contourClass, classifyCadence, syncopation, metricWeight, development, type Finding } from "../src/canvas-core/music/craft";
+import { line, type Piece, type Part, type Role, type InstId, type Meter, type Note } from "../src/canvas-core/music/plan";
+import type { StyleId, MoodId } from "../src/canvas-core/music/tables";
+import type { ModeId } from "../src/canvas-core/music/theory";
+
+type P = { id: string; inst: InstId; role: Role; src: string };
+const piece = (o: { style?: StyleId; mood?: MoodId; key?: string; mode?: ModeId; meter?: Meter; tempo?: number; bars: number; parts: P[]; harmony?: [number, string][]; sections?: [string, number][]; loop?: boolean }): Piece => {
+ const meter = o.meter ?? "4/4", bpb = meter === "3/4" ? 3 : meter === "6/8" ? 2 : 4;
+ const parts: Part[] = o.parts.map((x) => ({ id: x.id, inst: x.inst, role: x.role, notes: line(0, x.src, { role: x.role, bpb }) }));
+ let from = 0; const arrangement = (o.sections ?? [["a", o.bars]]).map(([id, bars]) => { const a = { id, kind: id, from, bars }; from += bars; return a; });
+ return { title: "t", seed: 1, tail: 1, parts, harmony: (o.harmony ?? [[0, "I"]]).map(([t, name]) => ({ t, name })), arrangement,
+ plan: { style: o.style ?? "folk", tempo: o.tempo ?? 90, meter, loop: o.loop, sections: [{ id: "a", bars: o.bars, mood: o.mood ?? "calm", key: o.key ?? "C", mode: o.mode ?? "major", melody: ["stepwise"], dyn: [0.6, 0.6] }] } };
+};
+const has = (fs: Finding[], area: string, re: RegExp, level?: Finding["level"]) => fs.some((f) => f.area === area && re.test(f.msg) && (!level || f.level === level));
+const n = (p: number, t: number): Note => ({ p, t, d: 1, v: 0.7, role: "melody" });
+
+export const name = "craft (melody, harmony, rhythm, tension, mood metrics)";
+export const run = (ok: (cond: boolean, label: string) => void) => {
+ const C = "[C3 E3 G3]:4", G = "[B2 D3 G3]:4", F = "[A2 C3 F3]:4";
+ const bassC = "C2:4", bassG = "G1:4", bassF = "F1:4";
+
+ // ---------------- melody: contour classes (Huron 1996)
+ ok(contourClass([n(60, 0), n(64, 1), n(67, 2), n(64, 3), n(60, 4)]) === "arch", "contour: C E G E C is an arch");
+ ok(contourClass([n(67, 0), n(65, 1), n(64, 2), n(62, 3), n(60, 4)]) === "descending", "contour: G F E D C descends");
+ ok(contourClass([n(60, 0), n(60, 1), n(61, 2), n(60, 3)]) === "level", "contour: a line within a semitone is level");
+
+ // ---------------- melody: leap recovery
+ const leapy = piece({ bars: 4, harmony: [[0, "C"], [4, "G"], [8, "C"], [12, "G"]], parts: [
+ { id: "lead", inst: "piano", role: "melody", src: "C4:1 A4:1 D5:1 G5:1 | C4:1 A4:1 D5:1 G5:1 | C4:1 A4:1 D5:1 G5:1 | C4:1 A4:1 D5:1 G5:1" },
+ { id: "chords", inst: "piano", role: "accomp", src: [C, G, C, G].join(" | ") }, { id: "bass", inst: "bass", role: "bass", src: [bassC, bassG, bassC, bassG].join(" | ") }] });
+ const recovered = piece({ bars: 4, harmony: [[0, "C"], [4, "G"], [8, "C"], [12, "G"]], parts: [
+ { id: "lead", inst: "piano", role: "melody", src: "C4:1 A4:1 G4:1 F4:1 | E4:1 C5:1 B4:1 A4:1 | G4:1 E5:1 D5:1 C5:1 | B4:2 G4:2" },
+ { id: "chords", inst: "piano", role: "accomp", src: [C, G, C, G].join(" | ") }, { id: "bass", inst: "bass", role: "bass", src: [bassC, bassG, bassC, bassG].join(" | ") }] });
+ const rl = craftReport(leapy), rr = craftReport(recovered);
+ ok(has(rl.findings, "melody", /leaps/, "warn"), `leaps that keep climbing warn (${rl.melody.leaps} leaps, ${rl.melody.leapsRecovered} turned back)`);
+ ok(!has(rr.findings, "melody", /leaps/) && rr.melody.leapsRecovered === rr.melody.leaps, "leaps filled in by step pass");
+ ok(rr.melody.stepShare > rl.melody.stepShare, `step share: stepwise line ${rr.melody.stepShare.toFixed(2)} > leapy line ${rl.melody.stepShare.toFixed(2)}`);
+ // arpeggio idiom exemption: the same climb is a note, not a warning, in chiptune
+ ok(has(craftReport({ ...leapy, plan: { ...leapy.plan, style: "chiptune" } }).findings, "melody", /leaps/, "info"), "arpeggio idioms (chiptune) get a note, not a warning");
+
+ // ---------------- melody: motif development (Schoenberg: repetition, sequence, variation)
+ ok(development([n(60, 0), n(62, 1), n(64, 2)], [[n(60, 0), n(62, 1), n(64, 2)]]) === "exact", "development: the same cell is exact");
+ ok(development([n(62, 0), n(64, 1), n(66, 2)], [[n(60, 0), n(62, 1), n(64, 2)]]) === "sequence", "development: the cell a step up is a sequence");
+ ok(development([n(62, 0), n(59, 1), n(64, 2)], [[n(60, 0), n(62, 1), n(64, 2)]]) === "varied", "development: same rhythm, new intervals is varied");
+ ok(development([n(62, 0), n(59, 0.5), n(71, 2.5)], [[n(60, 0), n(62, 1), n(64, 2)]]) === "new", "development: new rhythm and intervals is new");
+ const motif = "E4:.5 G4:.5 A4:1 r:2", seqd = piece({ bars: 4, parts: [{ id: "lead", inst: "piano", role: "melody", src: [motif, "F4:.5 A4:.5 B4:1 r:2", "G4:.5 B4:.5 C5:1 r:2", "E4:.5 G4:.5 C5:1 r:2"].join(" | ") }] });
+ const random = piece({ bars: 4, parts: [{ id: "lead", inst: "piano", role: "melody", src: ["E4:1.5 D5:.5 r:2", "B3:.25 G4:.25 F4:2.5 r:1", "C5:1 A3:1 r:2", "F4:.75 E5:.25 G5:.5 r:2.5"].join(" | ") }] });
+ const same = piece({ bars: 5, parts: [{ id: "lead", inst: "piano", role: "melody", src: Array(5).fill(motif).join(" | ") }] });
+ ok(craftReport(seqd).melody.repetition >= 0.66 && !has(craftReport(seqd).findings, "melody", /bring back|exact copies/), `a developed motif returns (${craftReport(seqd).melody.repetition.toFixed(2)}) without a warning`);
+ ok(has(craftReport(random).findings, "melody", /bring back/), "a line where nothing returns warns");
+ ok(has(craftReport(same).findings, "melody", /exact copies/), "a photocopied motif warns");
+
+ // ---------------- melody: phrase endings, strong beats, climax
+ const ends = (last: string) => piece({ bars: 4, harmony: [[0, "C"], [4, "G"], [8, "F"], [12, "C"]], parts: [
+ { id: "lead", inst: "piano", role: "melody", src: `E4:1 F4:1 G4:1 r:1 | D4:1 C4:1 B3:1 r:1 | C4:1 A3:1 ${last}:1 r:1 | ${last}:1 D4:1 ${last}:1 r:1` },
+ { id: "chords", inst: "piano", role: "accomp", src: [C, G, F, C].join(" | ") }, { id: "bass", inst: "bass", role: "bass", src: [bassC, bassG, bassF, bassC].join(" | ") }] });
+ ok(craftReport(ends("C4")).melody.stableEnds === 1, "phrases ending on chord tones: 100 %");
+ ok(craftReport(ends("B3")).melody.stableEnds < 0.6 && has(craftReport(ends("B3")).findings, "melody", /end on a chord tone/), "phrases ending off the chord warn");
+ ok(has(craftReport(ends("B3")).findings, "melody", /last note/), "a last note off the home triad is named");
+ const peak = (early: boolean) => piece({ bars: 8, parts: [{ id: "lead", inst: "piano", role: "melody", src: Array.from({ length: 8 }, (_, i) => (i === (early ? 0 : 5) ? "C4:.5 D4:.5 E4:.5 A5:.5 G4:1 F4:1" : "C4:.5 D4:.5 E4:.5 F4:.5 E4:1 D4:1")).join(" | ") }] });
+ ok(has(craftReport(peak(true)).findings, "melody", /peaks/), "the climax in the first bar warns");
+ ok(!has(craftReport(peak(false)).findings, "melody", /peaks/) && Math.abs(craftReport(peak(false)).melody.climax!.at - 0.72) < 0.1, `the climax at ${Math.round(craftReport(peak(false)).melody.climax!.at * 100)} % passes`);
+
+ // ---------------- range per instrument
+ const vib = (src: string) => craftReport(piece({ bars: 1, parts: [{ id: "chords", inst: "vibes", role: "accomp", src }] })).findings;
+ ok(has(vib("[C3 E3 G3]:4"), "range", /outside what/, "error"), "vibes at C3 (5 st below F3) is an error");
+ ok(has(vib("[E3 A3 C4]:4"), "range", /outside what/, "warn"), "vibes at E3 (1 st below) is a warning: extended models exist");
+ ok(!vib("[A3 C4 E4]:4").some((f) => f.area === "range"), "vibes in range: no range finding");
+ ok(!craftReport(piece({ bars: 1, parts: [{ id: "bass", inst: "bass", role: "bass", src: "E1:4" }] })).findings.some((f) => f.area === "range"), "a bass on E1 is its home, not out of range");
+
+ // ---------------- harmony: cadences
+ ok(classifyCadence(7, 0) === "authentic" && classifyCadence(5, 0) === "plagal" && classifyCadence(10, 0) === "modal", "cadences: V-I authentic, IV-I plagal, bVII-I modal");
+ ok(classifyCadence(0, 7) === "half" && classifyCadence(7, 9) === "deceptive", "cadences: ending on V is half, V-vi deceptive");
+ const endOn = (fin: [string, string]) => piece({ bars: 4, sections: [["a", 2], ["b", 2]], harmony: [[0, "C"], [4, "F"], [8, "G"], [12, fin[0]]], parts: [
+ { id: "chords", inst: "piano", role: "accomp", src: [C, F, G, fin[1]].join(" | ") }, { id: "bass", inst: "bass", role: "bass", src: [bassC, bassF, bassG, fin[0] === "C" ? bassC : bassG].join(" | ") },
+ { id: "lead", inst: "piano", role: "melody", src: "E4:4 | F4:4 | D4:4 | C4:4" }] });
+ const home = craftReport(endOn(["C", C])), away = craftReport(endOn(["G", G]));
+ ok(home.harmony.cadences.at(-1)!.type === "authentic" && !has(home.findings, "harmony", /ends/), "a piece ending V-I is home");
+ ok(has(away.findings, "harmony", /ends half/, "warn"), "a piece ending on V warns (away from home)");
+ ok(has(craftReport({ ...endOn(["G", G]), plan: { ...endOn(["G", G]).plan, style: "suspense" } }).findings, "harmony", /ends/, "info"), "suspense may end open (a note, not a warning)");
+
+ // ---------------- harmony: voice leading (parallels, doublings, style-aware)
+ const pv = (style: StyleId, b: string) => craftReport(piece({ style, bars: 2, harmony: [[0, "C"], [4, "D"]], parts: [{ id: "chords", inst: "choir", role: "accomp", src: `[C4 G4 E5]:4 | ${b}:4` }] })).findings;
+ ok(has(pv("choral", "[D4 A4 F5]"), "harmony", /parallel/, "warn"), "choral: parallel 5ths C-G to D-A warn");
+ ok(!has(pv("choral", "[B3 G4 D5]"), "harmony", /parallel/), "choral: contrary/oblique motion passes");
+ ok(has(pv("rock", "[D4 A4 F5]"), "harmony", /parallel/, "info"), "rock: the same fifths are the idiom (a note)");
+ const dbl = craftReport(piece({ style: "orchestral", bars: 2, parts: [{ id: "chords", inst: "strings", role: "accomp", src: "[E3 G3 C4 E4]:4 | [F3 A3 C4 F4]:4" }] }));
+ ok(dbl.harmony.parallels === 0 && dbl.harmony.octaveDoublings === 1, "orchestral: an octave-doubled voice moving is doubling, not a parallel");
+ ok(craftReport(piece({ bars: 2, parts: [{ id: "chords", inst: "piano", role: "accomp", src: "[C4 E4 G4]:4 | [C5 E5 G5]:4" }] })).harmony.meanMotion > craftReport(piece({ bars: 2, parts: [{ id: "chords", inst: "piano", role: "accomp", src: "[C4 E4 G4]:4 | [C4 F4 A4]:4" }] })).harmony.meanMotion, "voice motion: a leap of the whole chord moves more than an inversion");
+
+ // ---------------- harmony: mud (low interval limits)
+ const mud = (src: string) => craftReport(piece({ bars: 1, parts: [{ id: "chords", inst: "piano", role: "accomp", src }] })).harmony.mud.length;
+ ok(mud("[C2 E2 G2]:4") > 0, "a close C2-E2 third is mud");
+ ok(mud("[C2 G2 E3]:4") === 0, "the same chord spread (C2 G2 E3) is clear");
+ ok(mud("[C2 C3]:4") === 0, "octaves are never mud");
+
+ // ---------------- harmony: the melody under the chords
+ ok(has(craftReport(piece({ bars: 2, parts: [{ id: "chords", inst: "piano", role: "accomp", src: "[C4 E4 G4 C5]:4 | [C4 E4 G4 C5]:4" }, { id: "lead", inst: "piano", role: "melody", src: "E4:1 F4:1 G4:1 A4:1 | G4:1 F4:1 E4:1 D4:1" }] })).findings, "harmony", /under/), "a melody buried under the chord voicing warns");
+
+ // ---------------- rhythm: metric weights, syncopation
+ ok(metricWeight(0, 4, false) === 4 && metricWeight(2, 4, false) === 3 && metricWeight(1, 4, false) === 2 && metricWeight(0.5, 4, false) === 1 && metricWeight(0.25, 4, false) === 0, "metric weights 4/4: downbeat 4, mid-bar 3, beat 2, 8th 1, 16th 0");
+ ok(metricWeight(1 / 3, 2, true) === 1 && metricWeight(1, 2, true) === 2, "metric weights 6/8: dotted-quarter beats 2, eighths 1");
+ ok(syncopation([0, 1, 2, 3, 4], 4, false).index === 0, "syncopation: quarter notes on the beat = 0");
+ ok(syncopation([0, 1.5, 3, 4], 4, false).index > 0 && syncopation([0, 1.5, 3, 4], 4, false).share > 0.3, "syncopation: an 'and of 2' held over beat 3 counts");
+ const groove = (kick: string) => craftReport(piece({ style: "hipHop", bars: 4, parts: [{ id: "kick", inst: "kick", role: "drum", src: kick }, { id: "bass", inst: "bass", role: "bass", src: "C2:4 | C2:4 | C2:4 | C2:4" }, { id: "lead", inst: "piano", role: "melody", src: "C4:4 | D4:4 | E4:4 | C4:4" }] }));
+ ok(has(groove("C4:1 C4:1 C4:1 C4:1 | C4:1 C4:1 C4:1 C4:1 | C4:1 C4:1 C4:1 C4:1 | C4:1 C4:1 C4:1 C4:1").findings, "rhythm", /no syncopation/), "hip-hop with every hit on the beat: no syncopation warns");
+ ok(!has(groove("C4:1.5 C4:.5 r:2 | C4:1.5 C4:1 C4:1.5 | C4:1.5 C4:.5 r:2 | C4:1.5 C4:1 C4:1.5").findings, "rhythm", /no syncopation/), "an anticipated kick grooves");
+ ok(has(groove("C4:4 | r:.75 C4:.25 C4:3 | C4:.5 C4:.5 C4:.5 C4:2.5 | r:3.5 C4:.5").findings, "rhythm", /changes almost every bar/), "a kick pattern that never repeats warns (groove consistency)");
+ ok(has(craftReport(piece({ bars: 2, harmony: [[0.5, "C"], [2.5, "F"], [4.5, "G"], [6.5, "C"]], parts: [{ id: "chords", inst: "piano", role: "accomp", src: "r:.5 [C3 E3 G3]:2 [A2 C3 F3]:1.5 | r:.5 [B2 D3 G3]:2 [C3 E3 G3]:1.5" }] })).findings, "rhythm", /between beats/), "chord changes on off-beats warn");
+ const rep = craftReport(piece({ bars: 8, sections: [["a", 4], ["b", 4]], parts: [{ id: "lead", inst: "piano", role: "melody", src: Array(8).fill("C4:1 E4:1 G4:1 E4:1").join(" | ") }] }));
+ ok(rep.rhythm.repeatedSections.includes("b") && has(rep.findings, "rhythm", /note for note/), "a section repeating the previous note for note is dead air");
+
+ // ---------------- tension: flat vs shaped
+ const sec = (x: string) => Array(2).fill(x).join(" | ");
+ const flat = craftReport(piece({ bars: 6, sections: [["a", 2], ["b", 2], ["c", 2]], parts: [{ id: "lead", inst: "piano", role: "melody", src: [sec("C4:1 D4:1 E4:1 D4:1"), sec("C4:1 E4:1 D4:1 E4:1"), sec("D4:1 C4:1 E4:1 C4:1")].join(" | ") }, { id: "chords", inst: "piano", role: "accomp", src: Array(6).fill(C).join(" | ") }] }));
+ ok(has(flat.findings, "tension", /flat/), `a piece with no arc warns (sections ${flat.tension.sections.map((s) => s.value).join(", ")})`);
+ const arc = craftReport(piece({ bars: 6, sections: [["a", 2], ["b", 2], ["c", 2]], harmony: [[0, "C"], [8, "G"], [16, "C"]], parts: [
+ { id: "lead", inst: "piano", role: "melody", src: [sec("C4:2 D4:2"), sec("G4:.5 A4:.5 B4:.5 C5:.5 D5:.5 E5:.5 F5:.5 G5:.5"), sec("E4:4")].join(" | ") },
+ { id: "chords", inst: "piano", role: "accomp", src: [C, C, "[B2 D3 F3 G3]:4", "[B2 D3 F3 G3]:4", C, C].join(" | ") }, { id: "bass", inst: "bass", role: "bass", src: [bassC, bassC, "G1:.5 G1:.5 G1:.5 G1:.5 G1:.5 G1:.5 G1:.5 G1:.5", "G1:.5 G1:.5 G1:.5 G1:.5 G1:.5 G1:.5 G1:.5 G1:.5", bassC, bassC].join(" | ") }] }));
+ ok(!arc.findings.some((f) => f.area === "tension") && arc.tension.sections[1].value > arc.tension.sections[0].value && arc.tension.sections[1].value > arc.tension.sections[2].value, `rise, peak, release (${arc.tension.sections.map((s) => s.value).join(" < > ")}) passes`);
+
+ // ---------------- mood fit
+ const moody = (tempo: number, mode: ModeId) => craftReport(piece({ mood: "calm", tempo, mode, bars: 2, parts: [{ id: "lead", inst: "piano", role: "melody", src: "E5:1 D5:.5 C5:.5 D5:1 E5:1 | D5:1 C5:.5 D5:.5 E5:1 C5:1" }, { id: "chords", inst: "piano", role: "accomp", src: `${C} | ${C}` }, { id: "bass", inst: "bass", role: "bass", src: "C2:4 | C2:4" }] }));
+ ok(!has(moody(72, "major").findings, "mood", /./), "calm at 72 bpm in major fits");
+ ok(has(moody(150, "major").findings, "mood", /tempo 150/), "calm at 150 bpm warns on tempo");
+ ok(has(moody(72, "phrygian").findings, "mood", /mode phrygian/), "calm in phrygian (minor family) warns on mode");
+ ok(!has(moody(72, "lydian").findings, "mood", /mode/), "calm in lydian (major family) passes: the family is the cue");
+
+ // ---------------- only real errors are errors
+ ok(craftReport(leapy).findings.every((f) => f.level !== "error"), "craft never errors on taste: only impossible ranges are errors");
+};
diff --git a/skills/anidoodle/engine/tools/music.mjs b/skills/anidoodle/engine/tools/music.mjs
index d8d316d..5002272 100644
--- a/skills/anidoodle/engine/tools/music.mjs
+++ b/skills/anidoodle/engine/tools/music.mjs
@@ -7,7 +7,8 @@
// returning either); or the name of a shipped demo (listening references, never a film's score).
//
// node tools/music.mjs vocab [style] # the style vocabularies: palette, grooves, harmony, melody, arrangement
-// node tools/music.mjs check <piece> [--fit --seconds N] # everything: key/mode, warnings, master, ghost/reverb/masking guards, stems, novelty
+// node tools/music.mjs check <piece> [--fit --seconds N] # everything: key/mode, warnings, master, ghost/reverb/masking guards, stems, craft, novelty
+// node tools/music.mjs craft <piece> [more ...] [--json] # how it is WRITTEN: melody, harmony, rhythm, tension, mood fit (no render, advisory; errors fail)
// node tools/music.mjs novelty <piece> [more ...] # vs every shipped piece (and pairwise when several); FAILS above the threshold or on a reused fragment
// node tools/music.mjs list # shipped demos and fixtures
// node tools/music.mjs render <piece> <out.wav|out.mp3> [--seconds 45] [--flat] [--tempo 66] [--fit] [--loop] [--stems]
@@ -129,11 +130,19 @@ const main = async () => {
}
if (fail) process.exitCode = 1; return;
}
+ if (cmd === "craft") {
+ let fail = false;
+ for (const ref of args.filter((a) => !a.startsWith("--"))) { const piece = (await getPiece(M, ref)).make(), r = M.craftReport(piece);
+ if (flag("--json")) console.log(JSON.stringify({ piece: ref, ...r }, null, 1)); else console.log(`${ref}\n${M.craftText(r)}`);
+ fail = fail || r.findings.some((f) => f.level === "error"); }
+ if (fail) process.exitCode = 1; return;
+ }
if (cmd === "check") {
const pc = await getPiece(M, args[0]); let piece = pc.make(), tempo = piece.plan.tempo, seconds = val("--seconds", undefined);
if (flag("--fit")) { const f = M.fitScore(piece, seconds); piece = f.piece; tempo = f.tempo; }
const r = piece.plan.loop ? M.renderLoop(piece, SR) : M.renderPiece(piece, SR, { seconds, tempo });
const ok = [report(M, r, piece), printStems(M, piece, tempo, seconds)?.ok !== false];
+ { const cr = M.craftReport(piece, { centroidHz: M.centroid([r.L, r.R], SR).mean }); console.log(M.craftText(cr)); ok.push(!cr.findings.some((f) => f.level === "error")); }
if (!pc.demo) { const fam = [args[0]]; ok.push(printNovelty(M, args[0], M.novelty(piece, M.DEMOS, fam), "shipped pieces")); }
console.log(ok.every(Boolean) ? "CHECK PASS: now a human listens (an mp3 or wav on a page)" : "CHECK FAIL: fix the items above, then run check again");
if (!ok.every(Boolean)) process.exitCode = 1; return;