barmark 0.20.0 → 0.22.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 +153 -1
- package/README.md +11 -5
- package/dist/chart/document.js +21 -7
- package/dist/chart/expand.d.ts +23 -4
- package/dist/chart/expand.js +156 -42
- package/dist/chart/index.d.ts +1 -0
- package/dist/chart/index.js +1 -0
- package/dist/chart/keys.d.ts +2 -0
- package/dist/chart/keys.js +2 -0
- package/dist/chart/lines.js +25 -10
- package/dist/chart/lyricRows.js +20 -5
- package/dist/chart/parse.d.ts +3 -1
- package/dist/chart/parse.js +75 -30
- package/dist/chart/parts.d.ts +3 -1
- package/dist/chart/parts.js +180 -31
- package/dist/chart/pedal.d.ts +5 -1
- package/dist/chart/pedal.js +53 -20
- package/dist/chart/pickup.d.ts +15 -0
- package/dist/chart/pickup.js +41 -0
- package/dist/chart/print.js +62 -15
- package/dist/chart/shares.d.ts +7 -0
- package/dist/chart/shares.js +20 -0
- package/dist/chart/systems.js +22 -6
- package/dist/chart/types.d.ts +18 -4
- package/dist/chart/verses.d.ts +14 -3
- package/dist/chart/verses.js +363 -101
- package/dist/chords/index.d.ts +1 -0
- package/dist/chords/index.js +1 -0
- package/dist/chords/name.d.ts +8 -0
- package/dist/chords/name.js +177 -0
- package/dist/chords/suffixes.d.ts +2 -0
- package/dist/chords/suffixes.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { inScale, spellPitch } from "../chart/keys.js";
|
|
2
|
+
import { noteSemitone, transpose } from "./intervals.js";
|
|
3
|
+
import { enumerateSuffixes, intervalSetKey } from "./suffixes.js";
|
|
4
|
+
const mod12 = (n) => ((n % 12) + 12) % 12;
|
|
5
|
+
const iv = (degree, alter = 0) => ({ degree, alter });
|
|
6
|
+
/** Each semitone count above a root, every way the vocabulary spells it. */
|
|
7
|
+
const SPELLINGS = [
|
|
8
|
+
[iv(1)],
|
|
9
|
+
[iv(9, -1)],
|
|
10
|
+
[iv(9)],
|
|
11
|
+
[iv(3, -1), iv(9, 1)],
|
|
12
|
+
[iv(3)],
|
|
13
|
+
[iv(11)],
|
|
14
|
+
[iv(5, -1), iv(11, 1)],
|
|
15
|
+
[iv(5)],
|
|
16
|
+
[iv(5, 1), iv(13, -1)],
|
|
17
|
+
[iv(13), iv(7, -2)],
|
|
18
|
+
[iv(7, -1)],
|
|
19
|
+
[iv(7)],
|
|
20
|
+
];
|
|
21
|
+
/** Every spelling of a set of semitone counts above a root. */
|
|
22
|
+
function spellings(offsets) {
|
|
23
|
+
let out = [[]];
|
|
24
|
+
for (const o of offsets)
|
|
25
|
+
out = out.flatMap((s) => (SPELLINGS[o] ?? []).map((i) => [...s, i]));
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
/** Every suffix naming these semitone counts above a root, clusters left out. */
|
|
29
|
+
function named(offsets) {
|
|
30
|
+
const table = enumerateSuffixes();
|
|
31
|
+
return spellings(offsets).flatMap((intervals) => (table.get(intervalSetKey(intervals)) ?? []).filter((suffix) => !clashes(suffix)));
|
|
32
|
+
}
|
|
33
|
+
/** Every suffix naming the notes from each sounding root, with the fifth supplied where it is missing. */
|
|
34
|
+
function candidates(set) {
|
|
35
|
+
return set.flatMap((root) => {
|
|
36
|
+
const offsets = set.map((p) => mod12(p - root));
|
|
37
|
+
const found = named(offsets).map((suffix) => ({ root, suffix, supplied: false }));
|
|
38
|
+
if (set.length < 3 || offsets.includes(7))
|
|
39
|
+
return found;
|
|
40
|
+
return [
|
|
41
|
+
...found,
|
|
42
|
+
...named([...offsets, 7]).map((suffix) => ({ root, suffix, supplied: true })),
|
|
43
|
+
];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Suffixes the table spells but no one writes: a degree held both plain and
|
|
48
|
+
* altered (`b9` with `9`, `b7` with `7`), a `(7)` on a sixth chord, any tone
|
|
49
|
+
* on `dim` or `dim7`, an altered tone on `5`, `6` or `6/9`.
|
|
50
|
+
*/
|
|
51
|
+
function clashes(suffix) {
|
|
52
|
+
return degreeClash(suffix) || unwritten(suffix);
|
|
53
|
+
}
|
|
54
|
+
function degreeClash({ intervals }) {
|
|
55
|
+
const plain = new Set(intervals.filter((i) => i.alter === 0).map((i) => (i.degree - 1) % 7));
|
|
56
|
+
return intervals.some((i) => i.alter !== 0 && plain.has((i.degree - 1) % 7));
|
|
57
|
+
}
|
|
58
|
+
const altered = (tone) => /[#b]/.test(tone);
|
|
59
|
+
function unwritten({ parts }) {
|
|
60
|
+
const sixth = parts.extension === "6" || parts.extension === "6/9";
|
|
61
|
+
if (parts.quality === "dim")
|
|
62
|
+
return parts.tones.length > 0;
|
|
63
|
+
if (sixth && parts.tones.includes("7"))
|
|
64
|
+
return true;
|
|
65
|
+
return (sixth || parts.quality === "5") && parts.tones.some(altered);
|
|
66
|
+
}
|
|
67
|
+
/** Tones in parentheses, the half-diminished `m7(b5)`'s b5 not counted. */
|
|
68
|
+
function addedTones({ parts }) {
|
|
69
|
+
const halfDiminished = parts.quality === "m" && parts.extension === "7";
|
|
70
|
+
return parts.tones.filter((t) => !(halfDiminished && t === "b5")).length;
|
|
71
|
+
}
|
|
72
|
+
/** Triads, sevenths, sixths, sus, extended chords, then added tones. */
|
|
73
|
+
function family(suffix) {
|
|
74
|
+
const { extension, sus } = suffix.parts;
|
|
75
|
+
if (addedTones(suffix) > 0)
|
|
76
|
+
return 5;
|
|
77
|
+
if (sus)
|
|
78
|
+
return 3;
|
|
79
|
+
if (extension === null)
|
|
80
|
+
return 0;
|
|
81
|
+
if (extension === "7" || extension === "maj7")
|
|
82
|
+
return 1;
|
|
83
|
+
if (extension === "6" || extension === "6/9")
|
|
84
|
+
return 2;
|
|
85
|
+
return 4;
|
|
86
|
+
}
|
|
87
|
+
/** A tone the plain-tone tie-break puts last: `(2)`, `(4)`, `(6)` lose to `(9)`, `(11)`, `(13)`. */
|
|
88
|
+
const PLAIN_TONES = ["2", "4", "6"];
|
|
89
|
+
/**
|
|
90
|
+
* The order spec 5.6 ranks candidates by: cost (added tones, a slash, a
|
|
91
|
+
* supplied fifth), then family, root in the bass, plain tones, the shorter
|
|
92
|
+
* suffix and the root nearest above the bass.
|
|
93
|
+
*/
|
|
94
|
+
function order(c, bass) {
|
|
95
|
+
const slash = c.root === bass ? 0 : 1;
|
|
96
|
+
const cost = addedTones(c.suffix) + slash + (c.supplied ? 1 : 0);
|
|
97
|
+
const plainTone = c.suffix.parts.tones.some((t) => PLAIN_TONES.includes(t)) ? 1 : 0;
|
|
98
|
+
return [cost, family(c.suffix), slash, plainTone, c.suffix.suffix.length, mod12(c.root - bass)];
|
|
99
|
+
}
|
|
100
|
+
function before(a, b) {
|
|
101
|
+
for (let i = 0; i < a.length; i++) {
|
|
102
|
+
const d = (a[i] ?? 0) - (b[i] ?? 0);
|
|
103
|
+
if (d !== 0)
|
|
104
|
+
return d < 0;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
function cheapest(found, bass) {
|
|
109
|
+
let best = null;
|
|
110
|
+
for (const c of found) {
|
|
111
|
+
const rank = order(c, bass);
|
|
112
|
+
if (!best || before(rank, best.rank))
|
|
113
|
+
best = { c, rank };
|
|
114
|
+
}
|
|
115
|
+
return best?.c ?? null;
|
|
116
|
+
}
|
|
117
|
+
const doubled = (note) => /##|bb/.test(note);
|
|
118
|
+
/** A double accidental, or an E#, B#, Cb or Fb the key would not write itself. */
|
|
119
|
+
const faulty = (note, key) => doubled(note) || (/^(?:E#|B#|Cb|Fb)$/.test(note) && spellPitch(noteSemitone(note), key) !== note);
|
|
120
|
+
/** The bass by its interval from the root; by pitch when that spelling is faulty. */
|
|
121
|
+
function bassName(c, root, bass, s) {
|
|
122
|
+
const interval = c.suffix.intervals.find((i) => noteSemitone(transpose("C", i)) === mod12(bass - c.root));
|
|
123
|
+
const spelled = interval && transpose(root, interval);
|
|
124
|
+
if (!spelled || doubled(spelled) || (!s.trusted && faulty(spelled, s.key)))
|
|
125
|
+
return spellPitch(bass, s.key);
|
|
126
|
+
return spelled;
|
|
127
|
+
}
|
|
128
|
+
function write(c, root, bass, s) {
|
|
129
|
+
const slash = c.root === bass ? "" : `/${bassName(c, root, bass, s)}`;
|
|
130
|
+
return `${root}${c.suffix.suffix}${slash}`;
|
|
131
|
+
}
|
|
132
|
+
/** Faulty spellings among the root, its third and the bass. */
|
|
133
|
+
function faults(c, root, bass, s) {
|
|
134
|
+
const thirds = c.suffix.intervals.filter((i) => i.degree === 3).map((i) => transpose(root, i));
|
|
135
|
+
const notes = [root, ...thirds, ...(c.root === bass ? [] : [bassName(c, root, bass, s)])];
|
|
136
|
+
return notes.filter((n) => faulty(n, s.key)).length;
|
|
137
|
+
}
|
|
138
|
+
const LETTERS = "CDEFGAB";
|
|
139
|
+
/** The pitch class's other spelling with at most one accidental: `Db` for `C#`, `Fb` for `E`. */
|
|
140
|
+
function enharmonic(pc, first) {
|
|
141
|
+
for (const letter of LETTERS) {
|
|
142
|
+
const alter = mod12(pc - noteSemitone(letter) + 6) - 6;
|
|
143
|
+
const name = `${letter}${alter < 0 ? "b" : alter > 0 ? "#" : ""}`;
|
|
144
|
+
if (Math.abs(alter) <= 1 && name !== first)
|
|
145
|
+
return name;
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The root by `spellPitch`; a root with no key or outside the key's scale takes
|
|
151
|
+
* its enharmonic spelling when that has fewer faults.
|
|
152
|
+
*/
|
|
153
|
+
function spell(c, bass, key) {
|
|
154
|
+
const s = { key, trusted: key !== null && inScale(c.root, key) };
|
|
155
|
+
const first = spellPitch(c.root, key);
|
|
156
|
+
const other = s.trusted ? undefined : enharmonic(c.root, first);
|
|
157
|
+
if (other !== undefined && faults(c, other, bass, s) < faults(c, first, bass, s))
|
|
158
|
+
return write(c, other, bass, s);
|
|
159
|
+
return write(c, first, bass, s);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* A chord symbol for sounding pitch classes over a bass, spelled for the key;
|
|
163
|
+
* null when none fits (spec 5.6). Pitch classes are taken mod 12 and
|
|
164
|
+
* deduplicated; the bass is added when missing. Two pitch classes name only a
|
|
165
|
+
* power chord. The suggestion grammar 4's linter may make for a note set.
|
|
166
|
+
*/
|
|
167
|
+
export function nameNotes(pitchClasses, bass, key) {
|
|
168
|
+
const b = mod12(bass);
|
|
169
|
+
const set = [...new Set([...pitchClasses.map(mod12), b])];
|
|
170
|
+
if (set.length < 2)
|
|
171
|
+
return null;
|
|
172
|
+
let found = candidates(set);
|
|
173
|
+
if (set.length === 2)
|
|
174
|
+
found = found.filter((c) => c.suffix.suffix === "5");
|
|
175
|
+
const best = cheapest(found, b);
|
|
176
|
+
return best && spell(best, b, key);
|
|
177
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Interval } from "./types.js";
|
|
2
|
+
import { type ChordParts } from "./vocabulary.js";
|
|
2
3
|
export type Suffix = {
|
|
3
4
|
suffix: string;
|
|
4
5
|
intervals: Interval[];
|
|
5
6
|
tones: number;
|
|
7
|
+
parts: ChordParts;
|
|
6
8
|
};
|
|
7
9
|
/**
|
|
8
10
|
* Every chord suffix Appendix A allows (with up to three parenthesized tones),
|
package/dist/chords/suffixes.js
CHANGED
|
@@ -34,7 +34,7 @@ export function enumerateSuffixes() {
|
|
|
34
34
|
continue;
|
|
35
35
|
const key = intervalSetKey(intervals);
|
|
36
36
|
const list = found.get(key) ?? [];
|
|
37
|
-
list.push({ suffix: printSuffix(parts), intervals, tones: tones.length });
|
|
37
|
+
list.push({ suffix: printSuffix(parts), intervals, tones: tones.length, parts });
|
|
38
38
|
found.set(key, list);
|
|
39
39
|
}
|
|
40
40
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barmark",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.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": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
],
|
|
32
32
|
"author": "Kevin Peckham",
|
|
33
33
|
"license": "MIT",
|
|
34
|
-
"homepage": "https://
|
|
34
|
+
"homepage": "https://www.barmark.dev",
|
|
35
35
|
"repository": {
|
|
36
36
|
"type": "git",
|
|
37
37
|
"url": "git+https://github.com/kevinpeckham/barmark.git",
|