barmark 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,48 @@ Releases follow the process in [RELEASING.md](RELEASING.md).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.7.0] - 2026-11-02
13
+
14
+ Print a chart you have already parsed, and conformance fixtures from real
15
+ charts. No grammar change: every chart reads as before. Implements the
16
+ [grammar v0.1 draft](https://github.com/kevinpeckham/barmark/blob/main/docs/grammar-v0.1-draft.md).
17
+
18
+ ### Added
19
+
20
+ - **`printParsed(text, parsed)`**: the canonical text from a `parseChart`
21
+ result you already have, so a caller that needs both the parse and the
22
+ printed chart reads the text once.
23
+ - The stem-bench conformance fixtures (`tests/conformance/`): the four
24
+ public Badverbs charts exactly as stored in Stem Shovel, with expected
25
+ readings derived from the band's declared section starts and
26
+ stem-bench's independently-built verified bar lists (Study 16), plus
27
+ the grammar's trap rules as tests. A parser change that alters what a
28
+ real chart means now fails CI before it ships.
29
+
30
+ ## [0.6.0] - 2026-11-01
31
+
32
+ Transposition, and scale degrees for numeral views. No grammar change:
33
+ every chart reads as before. Implements the
34
+ [grammar v0.1 draft](https://github.com/kevinpeckham/barmark/blob/main/docs/grammar-v0.1-draft.md).
35
+
36
+ ### Added
37
+
38
+ - **`transposeChart(text, { to } | { semitones })`** returns the chart in
39
+ another key as canonical text, with diagnostics. Chord roots, slash
40
+ basses, note sets (octaves included) and every `key:` line move, so a
41
+ modulation moves too; text lines never change. Chords are spelled by
42
+ their interval from the first key, so each keeps its place in the key,
43
+ and double sharps and flats are written as the plain note. Given
44
+ semitones, the new key takes the signature with fewer accidentals (F#
45
+ over Gb on a tie; minor keys and modes by their own signatures); a chart
46
+ with no `key:` spells roots from one table. Lines with errors stay as
47
+ written, with a warning, as does a note pushed outside octaves 0–8. A
48
+ target key for a chart with no `key:`, or a target that isn't a key, is
49
+ an error and returns the text unchanged.
50
+ - **`degreeOf(root, key)`**: a root's scale degree above the key's tonic,
51
+ measured against the major scale for every mode (in A minor, C is b3),
52
+ for numeral views such as `@barmark/render`'s.
53
+
12
54
  ## [0.5.0] - 2026-11-01
13
55
 
14
56
  Charts can say where the tempo slows down or speeds up. A grammar
@@ -118,7 +160,9 @@ The first working release: the reference parser and printer for the
118
160
 
119
161
  - **A placeholder package that holds the `barmark` name** while the spec and parser are written.
120
162
 
121
- [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.5.0...HEAD
163
+ [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.7.0...HEAD
164
+ [0.7.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.6.0...barmark-v0.7.0
165
+ [0.6.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.5.0...barmark-v0.6.0
122
166
  [0.5.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.4.0...barmark-v0.5.0
123
167
  [0.4.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.3.0...barmark-v0.4.0
124
168
  [0.3.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.2.0...barmark-v0.3.0
package/README.md CHANGED
@@ -35,7 +35,15 @@ declarations included and no runtime dependencies.
35
35
  ## Use
36
36
 
37
37
  ```ts
38
- import { parseChart, printChart, parseChord, harteToBarmark } from "barmark";
38
+ import {
39
+ degreeOf,
40
+ harteToBarmark,
41
+ parseChart,
42
+ parseChord,
43
+ printChart,
44
+ printParsed,
45
+ transposeChart,
46
+ } from "barmark";
39
47
 
40
48
  const chart = parseChart(text);
41
49
  chart.title; // "Example Song"
@@ -44,6 +52,12 @@ chart.sections; // each section as played, with its first and last bar
44
52
  chart.diagnostics; // errors and warnings, each with a line number
45
53
 
46
54
  printChart(text); // the chart in canonical form (same meaning, standard spelling)
55
+ printParsed(text, chart); // the same, from a parse you already have
56
+
57
+ transposeChart("key: D\n| D | A | Bm | G |", { to: "Eb" }).text; // "key: Eb\n| Eb | Bb | Cm | Ab |\n"
58
+ transposeChart(text, { semitones: -2 }); // { text, diagnostics }
59
+
60
+ degreeOf("C", { tonic: "D", mode: "major" }); // { degree: 7, alter: -1 }: C is b7 in D
47
61
 
48
62
  const chord = parseChord("Bm7(b5)");
49
63
  if (chord.ok) chord.value.pitches; // ["B", "D", "F", "A"]
@@ -55,7 +69,17 @@ harteToBarmark("A:sus4(b7,9)"); // "A9sus4"
55
69
  - **`parseChart`** never throws. A line with an error is left out of the
56
70
  play order and reported, and the rest of the chart still reads.
57
71
  - **`printChart`** changes spelling and layout only: lines with errors and
58
- lyrics print exactly as written.
72
+ lyrics print exactly as written. **`printParsed`** does the same from an
73
+ existing `parseChart` result, for a caller that needs both.
74
+ - **`transposeChart`** moves every chord root, slash bass, note set and
75
+ `key:` line, and never the words. Chords are spelled by their interval
76
+ from the chart's first key, so each keeps its place in the key: C in D
77
+ becomes Db in Eb, not C#. Given semitones, the new key takes the
78
+ signature with fewer accidentals (Eb, not D#; F# wins the tie with Gb).
79
+ A chart with no `key:` spells roots C C# D Eb E F F# G Ab A Bb B. Lines
80
+ with errors stay as written, with a warning. The output is canonical.
81
+ - **`degreeOf`** gives a root's degree above a key's tonic, against the
82
+ major scale for every mode: in A minor, C is b3.
59
83
  - **Harte labels** ([Harte et al., 2005](https://ismir2005.ismir.net/proceedings/1080.pdf))
60
84
  convert both ways. Every label in the McGill Billboard and Isophonics
61
85
  Beatles datasets converts to barmark with exactly the same notes.
@@ -0,0 +1,8 @@
1
+ import type { Interval } from "../chords/types.js";
2
+ import type { Key } from "./types.js";
3
+ /**
4
+ * A root's scale degree in a key: the interval above the tonic, measured
5
+ * against the major scale for every key and mode, so in A minor C is b3 and
6
+ * in A mixolydian G is b7. Null if either isn't a note.
7
+ */
8
+ export declare function degreeOf(root: string, key: Key): Interval | null;
@@ -0,0 +1,12 @@
1
+ import { intervalBetween } from "../chords/intervals.js";
2
+ const NOTE = /^[A-G](?:#*|b*)$/;
3
+ /**
4
+ * A root's scale degree in a key: the interval above the tonic, measured
5
+ * against the major scale for every key and mode, so in A minor C is b3 and
6
+ * in A mixolydian G is b7. Null if either isn't a note.
7
+ */
8
+ export function degreeOf(root, key) {
9
+ if (!NOTE.test(root) || !NOTE.test(key.tonic))
10
+ return null;
11
+ return intervalBetween(key.tonic, root);
12
+ }
@@ -1,7 +1,9 @@
1
1
  export { parseBarLine } from "./barLine.js";
2
+ export { degreeOf } from "./degree.js";
2
3
  export { beatsOf, DIRECTIVE_NAMES, parseDirectiveLine, parseMeter } from "./directives.js";
3
4
  export { readDocument } from "./document.js";
4
5
  export { expand, flattenBars, MAX_EXPANDED_BARS } from "./expand.js";
5
6
  export { parseChart } from "./parse.js";
6
- export { printCell, printChart, printLine, printSlot, reduceSlots } from "./print.js";
7
+ export { printCell, printChart, printLine, printParsed, printSlot, reduceSlots } from "./print.js";
8
+ export { type TransposeBy, transposeChart } from "./transpose.js";
7
9
  export type * from "./types.js";
@@ -1,6 +1,8 @@
1
1
  export { parseBarLine } from "./barLine.js";
2
+ export { degreeOf } from "./degree.js";
2
3
  export { beatsOf, DIRECTIVE_NAMES, parseDirectiveLine, parseMeter } from "./directives.js";
3
4
  export { readDocument } from "./document.js";
4
5
  export { expand, flattenBars, MAX_EXPANDED_BARS } from "./expand.js";
5
6
  export { parseChart } from "./parse.js";
6
- export { printCell, printChart, printLine, printSlot, reduceSlots } from "./print.js";
7
+ export { printCell, printChart, printLine, printParsed, printSlot, reduceSlots } from "./print.js";
8
+ export { transposeChart } from "./transpose.js";
@@ -1,4 +1,4 @@
1
- import type { Cell, Line, Slot } from "./types.js";
1
+ import type { Cell, ChartParse, Line, Slot } from "./types.js";
2
2
  /**
3
3
  * The fewest slots with the same timeline: `G / D /` is `G D`, `-- --` is `--`.
4
4
  * Only a slot with no attack of its own merges (`/`, a repeated `--` or
@@ -23,3 +23,8 @@ export declare function printLine(l: Line, beats?: (number | undefined)[]): stri
23
23
  * fences stay. Never throws.
24
24
  */
25
25
  export declare function printChart(text: string): string;
26
+ /**
27
+ * `printChart` over a parse you already have (`parseChart(text)`), so the
28
+ * text isn't read twice. `edit`, if given, changes each line before it prints.
29
+ */
30
+ export declare function printParsed(text: string, parsed: ChartParse, edit?: (l: Line) => Line): string;
@@ -106,7 +106,13 @@ export function printLine(l, beats = []) {
106
106
  * fences stay. Never throws.
107
107
  */
108
108
  export function printChart(text) {
109
- const parsed = parseChart(text);
109
+ return printParsed(text, parseChart(text));
110
+ }
111
+ /**
112
+ * `printChart` over a parse you already have (`parseChart(text)`), so the
113
+ * text isn't read twice. `edit`, if given, changes each line before it prints.
114
+ */
115
+ export function printParsed(text, parsed, edit = (l) => l) {
110
116
  const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
111
117
  const errored = new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line));
112
118
  // Each cell's beat count on its first play, which is when its slot count is checked.
@@ -143,7 +149,7 @@ export function printChart(text) {
143
149
  if (!l)
144
150
  out.push(t.startsWith("```") ? t : raw.trimEnd());
145
151
  else
146
- out.push(l.kind === "text" || errored.has(l.line) ? raw.trimEnd() : printLine(l, beatsFor(l)));
152
+ out.push(l.kind === "text" || errored.has(l.line) ? raw.trimEnd() : printLine(edit(l), beatsFor(l)));
147
153
  }
148
154
  while (out.at(-1) === "")
149
155
  out.pop();
@@ -0,0 +1,19 @@
1
+ import type { ChartDiagnostic } from "./types.js";
2
+ /** What to transpose by: a target key (the chart needs a `key:`), or a number of semitones. */
3
+ export type TransposeBy = {
4
+ to: string;
5
+ } | {
6
+ semitones: number;
7
+ };
8
+ /**
9
+ * The chart in another key, as canonical text (spec §8). Chord roots, slash
10
+ * basses, note sets and every `key:` line move; text never does. Spelling
11
+ * follows the interval from the chart's first key, so every chord keeps its
12
+ * place in the key. Lines with errors stay as written, with a warning; a
13
+ * target that isn't a key, or a target key for a chart with none, returns
14
+ * the text unchanged with an error. Never throws.
15
+ */
16
+ export declare function transposeChart(text: string, by: TransposeBy): {
17
+ text: string;
18
+ diagnostics: ChartDiagnostic[];
19
+ };
@@ -0,0 +1,145 @@
1
+ import { intervalBetween, noteSemitone, transpose } from "../chords/intervals.js";
2
+ import { parseNoteSet } from "../chords/noteSet.js";
3
+ import { parseChord } from "../chords/parse.js";
4
+ import { printChord } from "../chords/print.js";
5
+ import { parseDirectiveLine } from "./directives.js";
6
+ import { parseChart } from "./parse.js";
7
+ import { printParsed } from "./print.js";
8
+ const SHARPS = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
9
+ const FLATS = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
10
+ /** A chart with no key spells each root from this table. */
11
+ const TABLE = ["C", "C#", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"];
12
+ /** Each letter's place on the circle of fifths, and each mode's offset from its parent major. */
13
+ const FIFTHS = { F: -1, C: 0, G: 1, D: 2, A: 3, E: 4, B: 5 };
14
+ const MODE_FIFTHS = {
15
+ lydian: 1,
16
+ major: 0,
17
+ ionian: 0,
18
+ mixolydian: -1,
19
+ dorian: -2,
20
+ minor: -3,
21
+ aeolian: -3,
22
+ phrygian: -4,
23
+ locrian: -5,
24
+ };
25
+ const mod = (n, m) => ((n % m) + m) % m;
26
+ const alterOf = (note) => (note.includes("#") ? 1 : -1) * (note.length - 1);
27
+ /** Double sharps and flats are written as the plain note: Fx is G, Ebb is D. */
28
+ function plain(note) {
29
+ if (!/##|bb/.test(note))
30
+ return note;
31
+ return (note.includes("#") ? SHARPS : FLATS)[noteSemitone(note)] ?? note;
32
+ }
33
+ /** Sharps (positive) or flats (negative) in a key's signature. */
34
+ const signature = (tonic, mode) => (FIFTHS[tonic[0] ?? "C"] ?? 0) + 7 * alterOf(tonic) + MODE_FIFTHS[mode];
35
+ /** The key `semitones` away, spelled with the fewest accidentals; sharps win a tie (F# over Gb). */
36
+ function nearestKey(key, semitones) {
37
+ const pc = mod(noteSemitone(key.tonic) + semitones, 12);
38
+ const [a = "C", b = "C"] = [SHARPS[pc], FLATS[pc]];
39
+ const sa = signature(a, key.mode);
40
+ const sb = signature(b, key.mode);
41
+ return Math.abs(sb) < Math.abs(sa) || (Math.abs(sb) === Math.abs(sa) && sb > sa) ? b : a;
42
+ }
43
+ function firstKey(parsed) {
44
+ for (const l of parsed.lines)
45
+ if (l.kind === "directive" && l.set.key)
46
+ return l.set.key;
47
+ return undefined;
48
+ }
49
+ const fail = (message, token) => ({
50
+ line: 1,
51
+ severity: "error",
52
+ message,
53
+ token,
54
+ });
55
+ /** The move from the first key to `to`, the nearer way round. */
56
+ function moveTo(key, to) {
57
+ const target = parseDirectiveLine(`key: ${to}`, 1);
58
+ const tonic = target?.set.key?.tonic;
59
+ if (!tonic || target.diagnostics.length || Object.keys(target.set).length !== 1)
60
+ return fail(`can't transpose to "${to}": it isn't a key`, to);
61
+ if (!key)
62
+ return fail("transposing to a key needs a key: line in the chart", to);
63
+ const semis = mod(noteSemitone(tonic) - noteSemitone(key.tonic), 12);
64
+ const interval = intervalBetween(key.tonic, tonic);
65
+ return { spell: (n) => plain(transpose(n, interval)), semitones: semis > 6 ? semis - 12 : semis };
66
+ }
67
+ function moveBy(key, semitones) {
68
+ if (!Number.isInteger(semitones))
69
+ return fail("transpose by a whole number of semitones", String(semitones));
70
+ if (!key)
71
+ return { spell: (n) => TABLE[mod(noteSemitone(n) + semitones, 12)] ?? n, semitones };
72
+ const interval = intervalBetween(key.tonic, nearestKey(key, semitones));
73
+ return { spell: (n) => plain(transpose(n, interval)), semitones };
74
+ }
75
+ /** Pitch number (C4 = 60) of a spelled note in an octave, with the octave belonging to the letter. */
76
+ const pitch = (name, octave) => 12 * (octave + 1) + noteSemitone(name[0] ?? "C") + alterOf(name);
77
+ function moveNoteSet(chord, move, warn) {
78
+ const tokens = chord.canonical.slice(1, -1).split(" ");
79
+ const moved = tokens.map((t) => {
80
+ const m = /^([A-G](?:#*|b*))(\d)?(!)?$/.exec(t);
81
+ if (!m?.[1])
82
+ return t;
83
+ const name = move.spell(m[1]);
84
+ if (m[2] === undefined)
85
+ return `${name}${m[3] ?? ""}`;
86
+ const octave = (pitch(m[1], Number(m[2])) + move.semitones - pitch(name, 0)) / 12;
87
+ if (octave >= 0 && octave <= 8)
88
+ return `${name}${octave}${m[3] ?? ""}`;
89
+ warn(`${m[1]}${m[2]}`);
90
+ return t;
91
+ });
92
+ const r = parseNoteSet(`{${moved.join(" ")}}`);
93
+ return r.ok ? r.value : chord;
94
+ }
95
+ function moveChord(chord, move, warn) {
96
+ if (!chord.parts)
97
+ return moveNoteSet(chord, move, warn);
98
+ const { root, bass } = chord.parts;
99
+ const r = parseChord(printChord({ ...chord.parts, root: move.spell(root), bass: bass && move.spell(bass) }));
100
+ return r.ok ? r.value : chord;
101
+ }
102
+ function moveCells(cells, move, warn) {
103
+ const simple = (s) => s.kind === "chord" ? { kind: "chord", chord: moveChord(s.chord, move, warn) } : s;
104
+ const slot = (s) => s.kind === "group" ? { kind: "group", members: s.members.map(simple) } : simple(s);
105
+ return cells.map((c) => (c.kind === "slots" ? { ...c, slots: c.slots.map(slot) } : c));
106
+ }
107
+ function moveLine(l, move, warn) {
108
+ if (l.kind === "bars")
109
+ return { ...l, cells: moveCells(l.cells, move, (t) => warn(l.line, t)) };
110
+ if (l.kind === "directive" && l.set.key)
111
+ return { ...l, set: { ...l.set, key: { ...l.set.key, tonic: move.spell(l.set.key.tonic) } } };
112
+ return l;
113
+ }
114
+ /**
115
+ * The chart in another key, as canonical text (spec §8). Chord roots, slash
116
+ * basses, note sets and every `key:` line move; text never does. Spelling
117
+ * follows the interval from the chart's first key, so every chord keeps its
118
+ * place in the key. Lines with errors stay as written, with a warning; a
119
+ * target that isn't a key, or a target key for a chart with none, returns
120
+ * the text unchanged with an error. Never throws.
121
+ */
122
+ export function transposeChart(text, by) {
123
+ const parsed = parseChart(text);
124
+ const key = firstKey(parsed);
125
+ const move = "to" in by ? moveTo(key, by.to) : moveBy(key, by.semitones);
126
+ if ("severity" in move)
127
+ return { text, diagnostics: [move] };
128
+ const physical = text.replace(/^/, "").split(/\r?\n/);
129
+ const diagnostics = [];
130
+ for (const line of new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line)))
131
+ diagnostics.push({
132
+ line,
133
+ severity: "warning",
134
+ message: "not transposed: this line has an error",
135
+ token: physical[line - 1]?.trim() ?? "",
136
+ });
137
+ const warn = (line, token) => diagnostics.push({
138
+ line,
139
+ severity: "warning",
140
+ message: `${token} can't move: octaves run 0–8, so it stays as written`,
141
+ token,
142
+ });
143
+ const out = printParsed(text, parsed, (l) => moveLine(l, move, warn));
144
+ return { text: out, diagnostics: diagnostics.sort((a, b) => a.line - b.line) };
145
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barmark",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Markdown-inspired plain-text notation for chord charts: a parser, a canonical printer and Harte-label conversion.",
5
5
  "type": "module",
6
6
  "exports": {