barmark 0.4.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 +46 -1
- package/README.md +15 -1
- package/dist/chart/degree.d.ts +8 -0
- package/dist/chart/degree.js +12 -0
- package/dist/chart/directives.js +16 -1
- package/dist/chart/expand.js +31 -4
- package/dist/chart/index.d.ts +2 -0
- package/dist/chart/index.js +2 -0
- package/dist/chart/print.d.ts +3 -1
- package/dist/chart/print.js +12 -2
- package/dist/chart/transpose.d.ts +19 -0
- package/dist/chart/transpose.js +145 -0
- package/dist/chart/types.d.ts +16 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,49 @@ 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
|
+
|
|
36
|
+
## [0.5.0] - 2026-11-01
|
|
37
|
+
|
|
38
|
+
Charts can say where the tempo slows down or speeds up. A grammar
|
|
39
|
+
decision (§7.2); no chart that read before changes meaning. Implements the
|
|
40
|
+
[grammar v0.1 draft](https://github.com/kevinpeckham/barmark/blob/main/docs/grammar-v0.1-draft.md).
|
|
41
|
+
|
|
42
|
+
### Added
|
|
43
|
+
|
|
44
|
+
- Gradual tempo changes (grammar §7.2). `tempo:` also takes `rit.`,
|
|
45
|
+
`accel.`, either one `to` a tempo (`tempo: rit. to 90`), and
|
|
46
|
+
`a tempo`; `rall.`, `ritardando`, `rallentando` and `accelerando`
|
|
47
|
+
read too. A change runs from the next bar to the next `tempo:` or the
|
|
48
|
+
end of the chart. Each played bar inside one has `tempoChange`
|
|
49
|
+
(`{ kind, from, to }`), so a host knows the beat is not steady there;
|
|
50
|
+
`tempo` stays the steady tempo. The printer writes `rit.`, `accel.`
|
|
51
|
+
and `a tempo`.
|
|
52
|
+
- Two warnings: `a tempo` with no change in force, and `rit. to` a
|
|
53
|
+
tempo that isn't slower (or `accel. to` one that isn't faster).
|
|
54
|
+
|
|
12
55
|
## [0.4.0] - 2026-11-01
|
|
13
56
|
|
|
14
57
|
Charts written in Logic Pro's chord spelling read as they are. A grammar
|
|
@@ -99,7 +142,9 @@ The first working release: the reference parser and printer for the
|
|
|
99
142
|
|
|
100
143
|
- **A placeholder package that holds the `barmark` name** while the spec and parser are written.
|
|
101
144
|
|
|
102
|
-
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.
|
|
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
|
|
147
|
+
[0.5.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.4.0...barmark-v0.5.0
|
|
103
148
|
[0.4.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.3.0...barmark-v0.4.0
|
|
104
149
|
[0.3.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.2.0...barmark-v0.3.0
|
|
105
150
|
[0.2.0]: https://github.com/kevinpeckham/barmark/compare/v0.1.0...barmark-v0.2.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 {
|
|
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
|
+
}
|
package/dist/chart/directives.js
CHANGED
|
@@ -43,6 +43,18 @@ function parseTempo(text) {
|
|
|
43
43
|
const n = m ? Number(m[1]) : 0;
|
|
44
44
|
return n >= 1 && n <= 999 ? n : null;
|
|
45
45
|
}
|
|
46
|
+
const CHANGE = /^(?:(rit|rall|ritardando|rallentando)|accel|accelerando)\.?(?:\s+to\s+(.+))?$/i;
|
|
47
|
+
/** `rit.`, `accel.` (and their long and `rall.` spellings), optionally `to` a tempo; or `a tempo`. */
|
|
48
|
+
function parseTempoChange(text) {
|
|
49
|
+
if (/^a\s+tempo$/i.test(text))
|
|
50
|
+
return { kind: "a tempo" };
|
|
51
|
+
const m = CHANGE.exec(text);
|
|
52
|
+
if (!m)
|
|
53
|
+
return null;
|
|
54
|
+
const kind = m[1] ? "rit" : "accel";
|
|
55
|
+
const to = m[2] === undefined ? null : parseTempo(m[2]);
|
|
56
|
+
return m[2] !== undefined && to === null ? null : { kind, to };
|
|
57
|
+
}
|
|
46
58
|
function parseFeel(text) {
|
|
47
59
|
const lower = text.toLowerCase();
|
|
48
60
|
return FEELS.find((f) => f === lower) ?? null;
|
|
@@ -91,10 +103,13 @@ export function parseDirectiveLine(text, line) {
|
|
|
91
103
|
}
|
|
92
104
|
else if (name === "tempo") {
|
|
93
105
|
const tempo = parseTempo(value);
|
|
106
|
+
const change = tempo ? null : parseTempoChange(value);
|
|
94
107
|
if (tempo)
|
|
95
108
|
set.tempo = tempo;
|
|
109
|
+
else if (change)
|
|
110
|
+
set.tempoChange = change;
|
|
96
111
|
else
|
|
97
|
-
error("tempo must be beats per minute
|
|
112
|
+
error("tempo must be beats per minute (1 to 999), rit., accel. or a tempo", value);
|
|
98
113
|
}
|
|
99
114
|
else {
|
|
100
115
|
const feel = parseFeel(value);
|
package/dist/chart/expand.js
CHANGED
|
@@ -111,15 +111,41 @@ export function expand(lines) {
|
|
|
111
111
|
key: null,
|
|
112
112
|
time: { n: 4, d: 4 },
|
|
113
113
|
tempo: null,
|
|
114
|
+
change: null,
|
|
114
115
|
feel: "straight",
|
|
115
116
|
};
|
|
116
|
-
const
|
|
117
|
+
const warned = new Set();
|
|
118
|
+
const warn = (line, message, token) => {
|
|
119
|
+
if (warned.has(line))
|
|
120
|
+
return;
|
|
121
|
+
warned.add(line);
|
|
122
|
+
diagnostics.push({ line, severity: "warning", message, token });
|
|
123
|
+
};
|
|
124
|
+
/** A rit. or accel. runs from the next bar to the next `tempo:`; `a tempo` ends it. */
|
|
125
|
+
const changeTempo = (c, line) => {
|
|
126
|
+
if (c.kind === "a tempo") {
|
|
127
|
+
if (!state.change)
|
|
128
|
+
warn(line, "a tempo, but no rit. or accel. is in force", "a tempo");
|
|
129
|
+
state.change = null;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const from = state.tempo;
|
|
133
|
+
const wrongWay = c.kind === "rit" ? (to, f) => to >= f : (to, f) => to <= f;
|
|
134
|
+
if (c.to !== null && from !== null && wrongWay(c.to, from))
|
|
135
|
+
warn(line, `${c.kind}. to ${c.to} is not ${c.kind === "rit" ? "slower" : "faster"} than ${from}`, `${c.kind}. to ${c.to}`);
|
|
136
|
+
state.change = { kind: c.kind, from, to: c.to };
|
|
137
|
+
};
|
|
138
|
+
const apply = (set, line) => {
|
|
117
139
|
if (set.key)
|
|
118
140
|
state.key = set.key;
|
|
119
141
|
if (set.time)
|
|
120
142
|
state.time = set.time;
|
|
121
|
-
if (set.tempo)
|
|
143
|
+
if (set.tempo) {
|
|
122
144
|
state.tempo = set.tempo;
|
|
145
|
+
state.change = null;
|
|
146
|
+
}
|
|
147
|
+
if (set.tempoChange)
|
|
148
|
+
changeTempo(set.tempoChange, line);
|
|
123
149
|
if (set.feel)
|
|
124
150
|
state.feel = set.feel;
|
|
125
151
|
};
|
|
@@ -159,7 +185,7 @@ export function expand(lines) {
|
|
|
159
185
|
token: `${k}`,
|
|
160
186
|
});
|
|
161
187
|
}
|
|
162
|
-
const { key, tempo, feel } = state;
|
|
188
|
+
const { key, tempo, change: tempoChange, feel } = state;
|
|
163
189
|
out.push({
|
|
164
190
|
kind: "bar",
|
|
165
191
|
bar: {
|
|
@@ -169,6 +195,7 @@ export function expand(lines) {
|
|
|
169
195
|
meter,
|
|
170
196
|
key,
|
|
171
197
|
tempo,
|
|
198
|
+
tempoChange,
|
|
172
199
|
feel,
|
|
173
200
|
slots: p.slots,
|
|
174
201
|
},
|
|
@@ -199,7 +226,7 @@ export function expand(lines) {
|
|
|
199
226
|
};
|
|
200
227
|
const playLine = ({ line: l, repeat }, out) => {
|
|
201
228
|
if (l.kind === "directive")
|
|
202
|
-
apply(l.set);
|
|
229
|
+
apply(l.set, l.line);
|
|
203
230
|
if (l.kind !== "bars")
|
|
204
231
|
return;
|
|
205
232
|
times(repeat, l.line, out, (into) => {
|
package/dist/chart/index.d.ts
CHANGED
|
@@ -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";
|
package/dist/chart/index.js
CHANGED
|
@@ -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";
|
package/dist/chart/print.d.ts
CHANGED
|
@@ -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;
|
package/dist/chart/print.js
CHANGED
|
@@ -63,6 +63,11 @@ function printKey(k) {
|
|
|
63
63
|
return `${k.tonic}m`;
|
|
64
64
|
return `${k.tonic} ${k.mode}`;
|
|
65
65
|
}
|
|
66
|
+
function printTempoChange(c) {
|
|
67
|
+
if (c.kind === "a tempo")
|
|
68
|
+
return "a tempo";
|
|
69
|
+
return `${c.kind}.${c.to === null ? "" : ` to ${c.to}`}`;
|
|
70
|
+
}
|
|
66
71
|
function printDirectives(d) {
|
|
67
72
|
const parts = [];
|
|
68
73
|
if (d.key)
|
|
@@ -71,6 +76,8 @@ function printDirectives(d) {
|
|
|
71
76
|
parts.push(`time: ${d.time.n}/${d.time.d}`);
|
|
72
77
|
if (d.tempo !== undefined)
|
|
73
78
|
parts.push(`tempo: ${d.tempo}`);
|
|
79
|
+
if (d.tempoChange)
|
|
80
|
+
parts.push(`tempo: ${printTempoChange(d.tempoChange)}`);
|
|
74
81
|
if (d.feel)
|
|
75
82
|
parts.push(`feel: ${d.feel}`);
|
|
76
83
|
return parts.join(", ");
|
|
@@ -99,7 +106,10 @@ export function printLine(l, beats = []) {
|
|
|
99
106
|
* fences stay. Never throws.
|
|
100
107
|
*/
|
|
101
108
|
export function printChart(text) {
|
|
102
|
-
|
|
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) {
|
|
103
113
|
const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
|
|
104
114
|
const errored = new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line));
|
|
105
115
|
// Each cell's beat count on its first play, which is when its slot count is checked.
|
|
@@ -136,7 +146,7 @@ export function printChart(text) {
|
|
|
136
146
|
if (!l)
|
|
137
147
|
out.push(t.startsWith("```") ? t : raw.trimEnd());
|
|
138
148
|
else
|
|
139
|
-
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)));
|
|
140
150
|
}
|
|
141
151
|
while (out.at(-1) === "")
|
|
142
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/dist/chart/types.d.ts
CHANGED
|
@@ -18,10 +18,19 @@ export type Key = {
|
|
|
18
18
|
mode: Mode;
|
|
19
19
|
};
|
|
20
20
|
export type Feel = "straight" | "swing" | "shuffle" | "swing 16";
|
|
21
|
+
/** A gradual change of tempo (rit., accel.) to an optional target, or the return from one (a tempo). */
|
|
22
|
+
export type TempoChange = {
|
|
23
|
+
kind: "rit" | "accel";
|
|
24
|
+
to: number | null;
|
|
25
|
+
} | {
|
|
26
|
+
kind: "a tempo";
|
|
27
|
+
};
|
|
28
|
+
/** `tempo:` gives either `tempo` (a steady tempo) or `tempoChange`, never both. */
|
|
21
29
|
export type Directives = {
|
|
22
30
|
key?: Key;
|
|
23
31
|
time?: Meter;
|
|
24
32
|
tempo?: number;
|
|
33
|
+
tempoChange?: TempoChange;
|
|
25
34
|
feel?: Feel;
|
|
26
35
|
};
|
|
27
36
|
/** A chord slot holds a chord symbol or a note set; `chord.kind` tells which. */
|
|
@@ -92,7 +101,14 @@ export type PlayedBar = {
|
|
|
92
101
|
section: string | null;
|
|
93
102
|
meter: Meter;
|
|
94
103
|
key: Key | null;
|
|
104
|
+
/** The steady tempo: where a change starts from, and what `a tempo` returns to. */
|
|
95
105
|
tempo: number | null;
|
|
106
|
+
/** Set on every bar inside a rit. or accel.; `from` is the steady tempo, `to` the target if written. */
|
|
107
|
+
tempoChange: {
|
|
108
|
+
kind: "rit" | "accel";
|
|
109
|
+
from: number | null;
|
|
110
|
+
to: number | null;
|
|
111
|
+
} | null;
|
|
96
112
|
feel: Feel;
|
|
97
113
|
/** `%` resolved to the copied slots; holds stay holds. */
|
|
98
114
|
slots: Slot[];
|