barmark 0.5.0 → 0.6.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,30 @@ Releases follow the process in [RELEASING.md](RELEASING.md).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.6.0] - 2026-11-01
13
+
14
+ Transposition, and scale degrees for numeral views. No grammar change:
15
+ 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
+ - **`transposeChart(text, { to } | { semitones })`** returns the chart in
21
+ another key as canonical text, with diagnostics. Chord roots, slash
22
+ basses, note sets (octaves included) and every `key:` line move, so a
23
+ modulation moves too; text lines never change. Chords are spelled by
24
+ their interval from the first key, so each keeps its place in the key,
25
+ and double sharps and flats are written as the plain note. Given
26
+ semitones, the new key takes the signature with fewer accidentals (F#
27
+ over Gb on a tie; minor keys and modes by their own signatures); a chart
28
+ with no `key:` spells roots from one table. Lines with errors stay as
29
+ written, with a warning, as does a note pushed outside octaves 0–8. A
30
+ target key for a chart with no `key:`, or a target that isn't a key, is
31
+ an error and returns the text unchanged.
32
+ - **`degreeOf(root, key)`**: a root's scale degree above the key's tonic,
33
+ measured against the major scale for every mode (in A minor, C is b3),
34
+ for numeral views such as `@barmark/render`'s.
35
+
12
36
  ## [0.5.0] - 2026-11-01
13
37
 
14
38
  Charts can say where the tempo slows down or speeds up. A grammar
@@ -118,7 +142,8 @@ The first working release: the reference parser and printer for the
118
142
 
119
143
  - **A placeholder package that holds the `barmark` name** while the spec and parser are written.
120
144
 
121
- [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.5.0...HEAD
145
+ [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.6.0...HEAD
146
+ [0.6.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.5.0...barmark-v0.6.0
122
147
  [0.5.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.4.0...barmark-v0.5.0
123
148
  [0.4.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.3.0...barmark-v0.4.0
124
149
  [0.3.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.2.0...barmark-v0.3.0
package/README.md CHANGED
@@ -35,7 +35,7 @@ declarations included and no runtime dependencies.
35
35
  ## Use
36
36
 
37
37
  ```ts
38
- import { parseChart, printChart, parseChord, harteToBarmark } from "barmark";
38
+ import { degreeOf, harteToBarmark, parseChart, parseChord, printChart, transposeChart } from "barmark";
39
39
 
40
40
  const chart = parseChart(text);
41
41
  chart.title; // "Example Song"
@@ -45,6 +45,11 @@ chart.diagnostics; // errors and warnings, each with a line number
45
45
 
46
46
  printChart(text); // the chart in canonical form (same meaning, standard spelling)
47
47
 
48
+ transposeChart("key: D\n| D | A | Bm | G |", { to: "Eb" }).text; // "key: Eb\n| Eb | Bb | Cm | Ab |\n"
49
+ transposeChart(text, { semitones: -2 }); // { text, diagnostics }
50
+
51
+ degreeOf("C", { tonic: "D", mode: "major" }); // { degree: 7, alter: -1 }: C is b7 in D
52
+
48
53
  const chord = parseChord("Bm7(b5)");
49
54
  if (chord.ok) chord.value.pitches; // ["B", "D", "F", "A"]
50
55
  if (chord.ok) chord.value.harte; // "B:hdim7"
@@ -56,6 +61,15 @@ harteToBarmark("A:sus4(b7,9)"); // "A9sus4"
56
61
  play order and reported, and the rest of the chart still reads.
57
62
  - **`printChart`** changes spelling and layout only: lines with errors and
58
63
  lyrics print exactly as written.
64
+ - **`transposeChart`** moves every chord root, slash bass, note set and
65
+ `key:` line, and never the words. Chords are spelled by their interval
66
+ from the chart's first key, so each keeps its place in the key: C in D
67
+ becomes Db in Eb, not C#. Given semitones, the new key takes the
68
+ signature with fewer accidentals (Eb, not D#; F# wins the tie with Gb).
69
+ A chart with no `key:` spells roots C C# D Eb E F F# G Ab A Bb B. Lines
70
+ with errors stay as written, with a warning. The output is canonical.
71
+ - **`degreeOf`** gives a root's degree above a key's tonic, against the
72
+ major scale for every mode: in A minor, C is b3.
59
73
  - **Harte labels** ([Harte et al., 2005](https://ismir2005.ismir.net/proceedings/1080.pdf))
60
74
  convert both ways. Every label in the McGill Billboard and Isophonics
61
75
  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
7
  export { printCell, printChart, printLine, 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
7
  export { printCell, printChart, printLine, 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,5 @@ 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
+ /** `printChart` over an existing parse, with each line it prints passed through `edit` first. */
27
+ export declare function printParsed(text: string, parsed: ChartParse, edit: (l: Line) => Line): string;
@@ -106,7 +106,10 @@ 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), (l) => l);
110
+ }
111
+ /** `printChart` over an existing parse, with each line it prints passed through `edit` first. */
112
+ export function printParsed(text, parsed, edit) {
110
113
  const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
111
114
  const errored = new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line));
112
115
  // Each cell's beat count on its first play, which is when its slot count is checked.
@@ -143,7 +146,7 @@ export function printChart(text) {
143
146
  if (!l)
144
147
  out.push(t.startsWith("```") ? t : raw.trimEnd());
145
148
  else
146
- out.push(l.kind === "text" || errored.has(l.line) ? raw.trimEnd() : printLine(l, beatsFor(l)));
149
+ out.push(l.kind === "text" || errored.has(l.line) ? raw.trimEnd() : printLine(edit(l), beatsFor(l)));
147
150
  }
148
151
  while (out.at(-1) === "")
149
152
  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.6.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": {