barmark 0.0.2 → 0.1.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 +52 -0
- package/LICENSE +21 -0
- package/README.md +74 -7
- package/dist/chart/barLine.d.ts +26 -0
- package/dist/chart/barLine.js +242 -0
- package/dist/chart/directives.d.ts +14 -0
- package/dist/chart/directives.js +108 -0
- package/dist/chart/document.d.ts +7 -0
- package/dist/chart/document.js +116 -0
- package/dist/chart/expand.d.ts +14 -0
- package/dist/chart/expand.js +271 -0
- package/dist/chart/index.d.ts +7 -0
- package/dist/chart/index.js +6 -0
- package/dist/chart/parse.d.ts +3 -0
- package/dist/chart/parse.js +17 -0
- package/dist/chart/print.d.ts +25 -0
- package/dist/chart/print.js +144 -0
- package/dist/chart/types.d.ts +130 -0
- package/dist/chart/types.js +1 -0
- package/dist/chords/harte.d.ts +16 -0
- package/dist/chords/harte.js +122 -0
- package/dist/chords/index.d.ts +6 -0
- package/dist/chords/index.js +4 -0
- package/dist/chords/intervals.d.ts +11 -0
- package/dist/chords/intervals.js +60 -0
- package/dist/chords/noteSet.d.ts +3 -0
- package/dist/chords/noteSet.js +73 -0
- package/dist/chords/parse.d.ts +6 -0
- package/dist/chords/parse.js +116 -0
- package/dist/chords/print.d.ts +5 -0
- package/dist/chords/print.js +10 -0
- package/dist/chords/suffixes.d.ts +13 -0
- package/dist/chords/suffixes.js +49 -0
- package/dist/chords/types.d.ts +30 -0
- package/dist/chords/types.js +1 -0
- package/dist/chords/vocabulary.d.ts +41 -0
- package/dist/chords/vocabulary.js +207 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +36 -6
- package/index.js +0 -11
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { formatInterval, intervalBetween, intervalKey, parseInterval, transpose, } from "./intervals.js";
|
|
2
|
+
import { printSuffix } from "./print.js";
|
|
3
|
+
import { enumerateSuffixes, intervalSetKey } from "./suffixes.js";
|
|
4
|
+
import { byDegree, chordIntervals, HARTE_SHORTHANDS } from "./vocabulary.js";
|
|
5
|
+
/**
|
|
6
|
+
* The Harte label for a parsed chord or note set: the largest shorthand
|
|
7
|
+
* contained in the chord, the remaining intervals in parentheses, and the
|
|
8
|
+
* bass as an interval when it is not the root. Note sets without a marked
|
|
9
|
+
* root use their bass as the Harte root, because Harte requires one.
|
|
10
|
+
*/
|
|
11
|
+
export function toHarte(r) {
|
|
12
|
+
const root = r.root ?? r.bass;
|
|
13
|
+
const keys = new Set(r.intervals.map(intervalKey));
|
|
14
|
+
const written = new Set(r.intervals.map(formatInterval));
|
|
15
|
+
// Most intervals wins; ties go to the shorthand naming more degrees exactly as
|
|
16
|
+
// written (sus4 over sus2 in a 9sus4, whose 9 only shares the 2's pitch class).
|
|
17
|
+
let best = { name: "1", used: new Set([intervalKey({ degree: 1, alter: 0 })]), exact: 1 };
|
|
18
|
+
for (const [name, ivs] of HARTE_SHORTHANDS) {
|
|
19
|
+
const used = new Set(ivs.map(intervalKey));
|
|
20
|
+
if (![...used].every((k) => keys.has(k)))
|
|
21
|
+
continue;
|
|
22
|
+
const exact = ivs.filter((i) => written.has(formatInterval(i))).length;
|
|
23
|
+
if (used.size > best.used.size || (used.size === best.used.size && exact > best.exact)) {
|
|
24
|
+
best = { name, used, exact };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const { name: shorthand, used } = best;
|
|
28
|
+
const added = r.intervals.filter((i) => !used.has(intervalKey(i))).sort(byDegree);
|
|
29
|
+
const list = added.length ? `(${added.map(formatInterval).join(",")})` : "";
|
|
30
|
+
const bass = r.bass === root ? "" : `/${formatInterval(intervalBetween(root, r.bass))}`;
|
|
31
|
+
return `${root}:${shorthand}${list}${bass}`;
|
|
32
|
+
}
|
|
33
|
+
const shape = (quality, extension = null, tones = [], sus = null) => ({ quality, extension, sus, tones });
|
|
34
|
+
const INTENDED = {
|
|
35
|
+
maj: shape(""),
|
|
36
|
+
min: shape("m"),
|
|
37
|
+
dim: shape("dim"),
|
|
38
|
+
aug: shape("aug"),
|
|
39
|
+
maj7: shape("", "maj7"),
|
|
40
|
+
min7: shape("m", "7"),
|
|
41
|
+
"7": shape("", "7"),
|
|
42
|
+
dim7: shape("dim", "7"),
|
|
43
|
+
hdim7: shape("m", "7", ["b5"]),
|
|
44
|
+
minmaj7: shape("m", "maj7"),
|
|
45
|
+
maj6: shape("", "6"),
|
|
46
|
+
min6: shape("m", "6"),
|
|
47
|
+
"9": shape("", "9"),
|
|
48
|
+
maj9: shape("", "maj9"),
|
|
49
|
+
min9: shape("m", "9"),
|
|
50
|
+
sus2: shape("", null, [], "sus2"),
|
|
51
|
+
sus4: shape("", null, [], "sus4"),
|
|
52
|
+
"11": shape("", "11"),
|
|
53
|
+
min11: shape("m", "11"),
|
|
54
|
+
"13": shape("", "13"),
|
|
55
|
+
min13: shape("m", "13"),
|
|
56
|
+
maj13: shape("", "maj13"),
|
|
57
|
+
"5": shape("5"),
|
|
58
|
+
};
|
|
59
|
+
/** Own keys only: a label like `C:constructor` must not find Object.prototype. */
|
|
60
|
+
const intendedShape = (name) => Object.hasOwn(INTENDED, name) ? INTENDED[name] : undefined;
|
|
61
|
+
const HARTE = /^([A-G][#b]*)(?::([^(/]*)(?:\(([^)]*)\))?)?(?:\/([#b]*\d+))?$/;
|
|
62
|
+
/** A Harte label's root, intervals (shorthand at its intended meaning, list applied) and bass. */
|
|
63
|
+
export function parseHarte(label) {
|
|
64
|
+
if (label === "N" || label === "X")
|
|
65
|
+
return label;
|
|
66
|
+
const m = HARTE.exec(label);
|
|
67
|
+
if (!m)
|
|
68
|
+
return null;
|
|
69
|
+
const [, root = "", shorthand = "maj", list, bassText] = m;
|
|
70
|
+
let intervals;
|
|
71
|
+
if (shorthand === "1")
|
|
72
|
+
intervals = [{ degree: 1, alter: 0 }];
|
|
73
|
+
else if (shorthand === "")
|
|
74
|
+
intervals = [];
|
|
75
|
+
else {
|
|
76
|
+
const intended = intendedShape(shorthand);
|
|
77
|
+
if (intended === undefined)
|
|
78
|
+
return null;
|
|
79
|
+
intervals = chordIntervals({ ...intended, root: "C", bass: null }, "").intervals;
|
|
80
|
+
}
|
|
81
|
+
for (const item of list ? list.split(",").map((x) => x.trim()) : []) {
|
|
82
|
+
const omit = item.startsWith("*");
|
|
83
|
+
const i = parseInterval(omit ? item.slice(1) : item);
|
|
84
|
+
if (!i)
|
|
85
|
+
return null;
|
|
86
|
+
intervals = intervals.filter((x) => intervalKey(x) !== intervalKey(i));
|
|
87
|
+
if (!omit)
|
|
88
|
+
intervals.push(i);
|
|
89
|
+
}
|
|
90
|
+
const bassInterval = bassText ? parseInterval(bassText) : null;
|
|
91
|
+
if (bassText && !bassInterval)
|
|
92
|
+
return null;
|
|
93
|
+
const bass = bassInterval ? transpose(root, bassInterval) : root;
|
|
94
|
+
return { root, intervals: intervals.sort(byDegree), bass };
|
|
95
|
+
}
|
|
96
|
+
const SINGLE_ACCIDENTAL = /^[A-G][#b]?$/;
|
|
97
|
+
/** A barmark slot for a Harte label: a named chord when one has exactly its notes, else a note set. */
|
|
98
|
+
export function harteToBarmark(label) {
|
|
99
|
+
const h = parseHarte(label);
|
|
100
|
+
if (h === "N")
|
|
101
|
+
return "N.C.";
|
|
102
|
+
if (h === "X")
|
|
103
|
+
return "?";
|
|
104
|
+
if (!h || !SINGLE_ACCIDENTAL.test(h.root))
|
|
105
|
+
return null;
|
|
106
|
+
const rootSounds = h.intervals.some((i) => i.degree === 1 && i.alter === 0);
|
|
107
|
+
const intended = /^[^:]+:([^(/]+)(?:\/|$)/.exec(label)?.[1];
|
|
108
|
+
const directShape = intended !== undefined ? intendedShape(intended) : undefined;
|
|
109
|
+
const direct = directShape ? printSuffix(directShape) : undefined;
|
|
110
|
+
const candidates = rootSounds ? enumerateSuffixes().get(intervalSetKey(h.intervals)) : undefined;
|
|
111
|
+
// Same notes, several names (A11 and A9sus4 under the eleventh rule): take the
|
|
112
|
+
// one naming most degrees as the label wrote them, then the shortest.
|
|
113
|
+
const written = new Set(h.intervals.map(formatInterval));
|
|
114
|
+
const exact = (c) => c.intervals.filter((i) => written.has(formatInterval(i))).length;
|
|
115
|
+
const suffix = direct ?? candidates?.reduce((best, c) => (exact(c) > exact(best) ? c : best)).suffix;
|
|
116
|
+
if (suffix !== undefined && SINGLE_ACCIDENTAL.test(h.bass)) {
|
|
117
|
+
return `${h.root}${suffix}${h.bass === h.root ? "" : `/${h.bass}`}`;
|
|
118
|
+
}
|
|
119
|
+
const others = h.intervals.map((i) => transpose(h.root, i)).filter((n) => n !== h.bass);
|
|
120
|
+
const notes = [h.bass, ...new Set(others)].map((n) => rootSounds && n === h.root && n !== h.bass ? `${n}!` : n);
|
|
121
|
+
return `{${notes.join(" ")}}`;
|
|
122
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { harteToBarmark, parseHarte, toHarte } from "./harte.js";
|
|
2
|
+
export { parseNoteSet } from "./noteSet.js";
|
|
3
|
+
export { parseChord } from "./parse.js";
|
|
4
|
+
export { printChord } from "./print.js";
|
|
5
|
+
export type { ChordRecord, Diagnostic, Interval, ParseResult } from "./types.js";
|
|
6
|
+
export type { ChordParts } from "./vocabulary.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Interval } from "./types.js";
|
|
2
|
+
export declare function parseInterval(s: string): Interval | null;
|
|
3
|
+
export declare function formatInterval(i: Interval): string;
|
|
4
|
+
/** Semitones above C (0–11) of a spelled note. */
|
|
5
|
+
export declare function noteSemitone(note: string): number;
|
|
6
|
+
/** The note `i` above `root`, spelled on the letter the degree names. */
|
|
7
|
+
export declare function transpose(root: string, i: Interval): string;
|
|
8
|
+
/** The spelled pitch class an interval names: 2 and 9 share a key, b3 and #9 do not. */
|
|
9
|
+
export declare function intervalKey(i: Interval): string;
|
|
10
|
+
/** The simple interval (degree 1–7) from one spelled note up to another. */
|
|
11
|
+
export declare function intervalBetween(from: string, to: string): Interval;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const LETTERS = "CDEFGAB";
|
|
2
|
+
/** Semitones above the root for each step's major or perfect interval. */
|
|
3
|
+
const STEP_SEMITONES = [0, 2, 4, 5, 7, 9, 11];
|
|
4
|
+
const LETTER_SEMITONES = [0, 2, 4, 5, 7, 9, 11];
|
|
5
|
+
const mod = (n, m) => ((n % m) + m) % m;
|
|
6
|
+
const accidentals = (alter) => (alter < 0 ? "b".repeat(-alter) : "#".repeat(alter));
|
|
7
|
+
export function parseInterval(s) {
|
|
8
|
+
const m = /^(b*|#*)([1-9]|1[0-3])$/.exec(s);
|
|
9
|
+
if (!m)
|
|
10
|
+
return null;
|
|
11
|
+
const acc = m[1] ?? "";
|
|
12
|
+
return { degree: Number(m[2]), alter: acc.startsWith("#") ? acc.length : -acc.length };
|
|
13
|
+
}
|
|
14
|
+
export function formatInterval(i) {
|
|
15
|
+
return `${accidentals(i.alter)}${i.degree}`;
|
|
16
|
+
}
|
|
17
|
+
/** A spelled note's letter index (C = 0) and its alteration, or null. */
|
|
18
|
+
function readNote(note) {
|
|
19
|
+
const m = /^([A-G])(b*|#*)$/.exec(note);
|
|
20
|
+
if (!m)
|
|
21
|
+
return null;
|
|
22
|
+
const acc = m[2] ?? "";
|
|
23
|
+
return {
|
|
24
|
+
step: LETTERS.indexOf(m[1] ?? ""),
|
|
25
|
+
alter: acc.startsWith("#") ? acc.length : -acc.length,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Semitones above C (0–11) of a spelled note. */
|
|
29
|
+
export function noteSemitone(note) {
|
|
30
|
+
const n = readNote(note);
|
|
31
|
+
if (!n)
|
|
32
|
+
throw new Error(`not a note: ${note}`);
|
|
33
|
+
return mod((LETTER_SEMITONES[n.step] ?? 0) + n.alter, 12);
|
|
34
|
+
}
|
|
35
|
+
/** The note `i` above `root`, spelled on the letter the degree names. */
|
|
36
|
+
export function transpose(root, i) {
|
|
37
|
+
const r = readNote(root);
|
|
38
|
+
if (!r)
|
|
39
|
+
throw new Error(`not a note: ${root}`);
|
|
40
|
+
const step = (i.degree - 1) % 7;
|
|
41
|
+
const letter = mod(r.step + step, 7);
|
|
42
|
+
const target = noteSemitone(root) + (STEP_SEMITONES[step] ?? 0) + i.alter;
|
|
43
|
+
const alter = mod(target - (LETTER_SEMITONES[letter] ?? 0) + 6, 12) - 6;
|
|
44
|
+
return `${LETTERS[letter]}${accidentals(alter)}`;
|
|
45
|
+
}
|
|
46
|
+
/** The spelled pitch class an interval names: 2 and 9 share a key, b3 and #9 do not. */
|
|
47
|
+
export function intervalKey(i) {
|
|
48
|
+
const step = (i.degree - 1) % 7;
|
|
49
|
+
return `${step}:${mod((STEP_SEMITONES[step] ?? 0) + i.alter, 12)}`;
|
|
50
|
+
}
|
|
51
|
+
/** The simple interval (degree 1–7) from one spelled note up to another. */
|
|
52
|
+
export function intervalBetween(from, to) {
|
|
53
|
+
const a = readNote(from);
|
|
54
|
+
const b = readNote(to);
|
|
55
|
+
if (!a || !b)
|
|
56
|
+
throw new Error(`not notes: ${from}, ${to}`);
|
|
57
|
+
const step = mod(b.step - a.step, 7);
|
|
58
|
+
const semis = mod(noteSemitone(to) - noteSemitone(from), 12);
|
|
59
|
+
return { degree: step + 1, alter: mod(semis - (STEP_SEMITONES[step] ?? 0) + 6, 12) - 6 };
|
|
60
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { toHarte } from "./harte.js";
|
|
2
|
+
import { intervalBetween, noteSemitone } from "./intervals.js";
|
|
3
|
+
import { byDegree } from "./vocabulary.js";
|
|
4
|
+
const NOTE = /^([A-G](?:#*|b*))([0-8])?(!)?$/;
|
|
5
|
+
/** Pitch number with the octave belonging to the letter: B#3 sounds as C4. */
|
|
6
|
+
const pitchOf = (n) => {
|
|
7
|
+
const accidentals = n.name.slice(1);
|
|
8
|
+
const alter = accidentals.startsWith("#") ? accidentals.length : -accidentals.length;
|
|
9
|
+
return 12 * ((n.octave ?? 0) + 1) + noteSemitone(n.name[0] ?? "C") + alter;
|
|
10
|
+
};
|
|
11
|
+
const label = (n) => `${n.name}${n.octave ?? ""}`;
|
|
12
|
+
/** A brace note set (grammar section 4): exact notes, low to high, an optional `!` root. */
|
|
13
|
+
export function parseNoteSet(text) {
|
|
14
|
+
const fail = (message, token = text) => ({
|
|
15
|
+
ok: false,
|
|
16
|
+
error: { message: `${text}: ${message}`, token },
|
|
17
|
+
});
|
|
18
|
+
const inner = /^\{(.*)\}$/.exec(text.replace(/♯/g, "#").replace(/♭/g, "b"))?.[1];
|
|
19
|
+
if (inner === undefined)
|
|
20
|
+
return fail("a note set is written in braces");
|
|
21
|
+
const tokens = inner.trim().split(/\s+/).filter(Boolean);
|
|
22
|
+
if (tokens.length === 0)
|
|
23
|
+
return fail("a note set needs at least one note");
|
|
24
|
+
let notes = [];
|
|
25
|
+
for (const t of tokens) {
|
|
26
|
+
const m = NOTE.exec(t);
|
|
27
|
+
if (!m) {
|
|
28
|
+
return fail(/^[A-G](?:#*|b*)\d+!?$/.test(t) ? `"${t}": octaves run 0–8` : `"${t}" is not a note name`, t);
|
|
29
|
+
}
|
|
30
|
+
notes.push({ name: m[1] ?? "", octave: m[2] ? Number(m[2]) : null, root: !!m[3] });
|
|
31
|
+
}
|
|
32
|
+
if (notes.filter((n) => n.root).length > 1)
|
|
33
|
+
return fail("at most one note is marked ! as the root");
|
|
34
|
+
const withOctave = notes.filter((n) => n.octave !== null).length;
|
|
35
|
+
if (withOctave !== 0 && withOctave !== notes.length) {
|
|
36
|
+
return fail("every note has an octave, or none does");
|
|
37
|
+
}
|
|
38
|
+
const warnings = [];
|
|
39
|
+
if (withOctave) {
|
|
40
|
+
const sorted = [...notes].sort((a, b) => pitchOf(a) - pitchOf(b));
|
|
41
|
+
if (sorted.some((n, i) => n !== notes[i])) {
|
|
42
|
+
warnings.push({ message: `${text}: notes are not in ascending order`, token: text });
|
|
43
|
+
}
|
|
44
|
+
notes = sorted;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
const seen = new Set();
|
|
48
|
+
for (const n of notes) {
|
|
49
|
+
if (seen.has(n.name))
|
|
50
|
+
warnings.push({ message: `${text}: ${n.name} appears twice`, token: n.name });
|
|
51
|
+
seen.add(n.name);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const bass = notes[0]?.name ?? "";
|
|
55
|
+
const root = notes.find((n) => n.root)?.name ?? null;
|
|
56
|
+
const from = root ?? bass;
|
|
57
|
+
const intervals = [
|
|
58
|
+
...new Map(notes.map((n) => [n.name, intervalBetween(from, n.name)])).values(),
|
|
59
|
+
].sort(byDegree);
|
|
60
|
+
const value = {
|
|
61
|
+
kind: "notes",
|
|
62
|
+
source: text,
|
|
63
|
+
canonical: `{${notes.map((n) => `${label(n)}${n.root ? "!" : ""}`).join(" ")}}`,
|
|
64
|
+
root,
|
|
65
|
+
intervals,
|
|
66
|
+
pitches: notes.map(label),
|
|
67
|
+
bass,
|
|
68
|
+
alteredOpen: false,
|
|
69
|
+
harte: "",
|
|
70
|
+
};
|
|
71
|
+
value.harte = toHarte(value);
|
|
72
|
+
return { ok: true, warnings, value };
|
|
73
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Diagnostic, ParseResult } from "./types.js";
|
|
2
|
+
/** Appendix A.5 and A.6: Unicode, rejections, then aliases into canonical vocabulary. */
|
|
3
|
+
export declare function normalizeSymbol(symbol: string): {
|
|
4
|
+
text: string;
|
|
5
|
+
} | Diagnostic;
|
|
6
|
+
export declare function parseChord(symbol: string): ParseResult;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { toHarte } from "./harte.js";
|
|
2
|
+
import { noteSemitone, transpose } from "./intervals.js";
|
|
3
|
+
import { printChord } from "./print.js";
|
|
4
|
+
import { ALIASES, chordIntervals, EXTENSIONS, rejection, TONES, validateParts, } from "./vocabulary.js";
|
|
5
|
+
const ROOT = /^[A-G][#b]?/;
|
|
6
|
+
const INLINE_ALTERATION = /^(b5|#5|b9|#9|#11|b13)/;
|
|
7
|
+
/** Splits a symbol into its parts, or names the text it could not read. */
|
|
8
|
+
function tokenize(symbol) {
|
|
9
|
+
const root = ROOT.exec(symbol)?.[0];
|
|
10
|
+
if (!root)
|
|
11
|
+
return "a chord starts with a root A–G";
|
|
12
|
+
let rest = symbol.slice(root.length);
|
|
13
|
+
let quality = "";
|
|
14
|
+
if (rest.startsWith("mmaj")) {
|
|
15
|
+
quality = "m";
|
|
16
|
+
rest = rest.slice(1);
|
|
17
|
+
}
|
|
18
|
+
else if (rest.startsWith("m") && !rest.startsWith("maj")) {
|
|
19
|
+
quality = "m";
|
|
20
|
+
rest = rest.slice(1);
|
|
21
|
+
}
|
|
22
|
+
else if (rest.startsWith("dim") || rest.startsWith("aug")) {
|
|
23
|
+
quality = rest.slice(0, 3);
|
|
24
|
+
rest = rest.slice(3);
|
|
25
|
+
}
|
|
26
|
+
else if (rest.startsWith("5")) {
|
|
27
|
+
quality = "5";
|
|
28
|
+
rest = rest.slice(1);
|
|
29
|
+
}
|
|
30
|
+
const ext = EXTENSIONS.find(([name]) => rest.startsWith(name))?.[0] ?? null;
|
|
31
|
+
if (ext)
|
|
32
|
+
rest = rest.slice(ext.length);
|
|
33
|
+
const tones = [];
|
|
34
|
+
if (ext) {
|
|
35
|
+
for (let m = INLINE_ALTERATION.exec(rest); m; m = INLINE_ALTERATION.exec(rest)) {
|
|
36
|
+
tones.push(m[0]);
|
|
37
|
+
rest = rest.slice(m[0].length);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let sus = null;
|
|
41
|
+
if (rest.startsWith("sus2") || rest.startsWith("sus4")) {
|
|
42
|
+
sus = rest.slice(0, 4);
|
|
43
|
+
rest = rest.slice(4);
|
|
44
|
+
}
|
|
45
|
+
const paren = /^\(([^)]*)\)/.exec(rest);
|
|
46
|
+
if (paren) {
|
|
47
|
+
for (const t of (paren[1] ?? "").split(",").map((x) => x.trim())) {
|
|
48
|
+
if (!TONES.includes(t))
|
|
49
|
+
return `"${t}" is not a tone a parenthesis can add`;
|
|
50
|
+
tones.push(t);
|
|
51
|
+
}
|
|
52
|
+
rest = rest.slice(paren[0].length);
|
|
53
|
+
}
|
|
54
|
+
let bass = null;
|
|
55
|
+
const slash = /^\/([A-G][#b]?)$/.exec(rest);
|
|
56
|
+
if (slash) {
|
|
57
|
+
bass = slash[1] ?? null;
|
|
58
|
+
rest = "";
|
|
59
|
+
}
|
|
60
|
+
if (rest)
|
|
61
|
+
return `cannot read "${rest}"`;
|
|
62
|
+
return { root, quality, extension: ext, sus, tones, bass };
|
|
63
|
+
}
|
|
64
|
+
/** Appendix A.5 and A.6: Unicode, rejections, then aliases into canonical vocabulary. */
|
|
65
|
+
export function normalizeSymbol(symbol) {
|
|
66
|
+
const text = symbol.replace(/♯/g, "#").replace(/♭/g, "b");
|
|
67
|
+
const rejected = rejection(text, noteSemitone);
|
|
68
|
+
if (rejected)
|
|
69
|
+
return rejected;
|
|
70
|
+
const root = ROOT.exec(text)?.[0] ?? "";
|
|
71
|
+
let rest = text.slice(root.length);
|
|
72
|
+
for (const [pattern, replacement] of ALIASES)
|
|
73
|
+
rest = rest.replace(pattern, replacement);
|
|
74
|
+
const added = [...rest.matchAll(/add(2|4|9|11)/g)].map((m) => m[1] ?? "");
|
|
75
|
+
if (added.length) {
|
|
76
|
+
rest = rest.replace(/add(2|4|9|11)/g, "");
|
|
77
|
+
const close = rest.indexOf(")");
|
|
78
|
+
if (close >= 0)
|
|
79
|
+
rest = `${rest.slice(0, close)},${added.join(",")}${rest.slice(close)}`;
|
|
80
|
+
else {
|
|
81
|
+
const slash = /\/[A-G][#b]?$/.exec(rest);
|
|
82
|
+
const at = slash ? slash.index : rest.length;
|
|
83
|
+
rest = `${rest.slice(0, at)}(${added.join(",")})${rest.slice(at)}`;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { text: root + rest };
|
|
87
|
+
}
|
|
88
|
+
export function parseChord(symbol) {
|
|
89
|
+
const normal = normalizeSymbol(symbol);
|
|
90
|
+
if (!("text" in normal))
|
|
91
|
+
return { ok: false, error: normal };
|
|
92
|
+
const parts = tokenize(normal.text);
|
|
93
|
+
if (typeof parts === "string") {
|
|
94
|
+
return { ok: false, error: { message: `${symbol}: ${parts}`, token: symbol } };
|
|
95
|
+
}
|
|
96
|
+
const invalid = validateParts(parts, symbol);
|
|
97
|
+
if (invalid)
|
|
98
|
+
return { ok: false, error: invalid };
|
|
99
|
+
const { intervals, warnings, alteredOpen } = chordIntervals(parts, symbol);
|
|
100
|
+
const pitches = intervals.map((i) => transpose(parts.root, i));
|
|
101
|
+
if (parts.bass && !pitches.includes(parts.bass))
|
|
102
|
+
pitches.push(parts.bass);
|
|
103
|
+
const value = {
|
|
104
|
+
kind: "chord",
|
|
105
|
+
source: symbol,
|
|
106
|
+
canonical: printChord(parts),
|
|
107
|
+
root: parts.root,
|
|
108
|
+
intervals,
|
|
109
|
+
pitches,
|
|
110
|
+
bass: parts.bass ?? parts.root,
|
|
111
|
+
alteredOpen,
|
|
112
|
+
harte: "",
|
|
113
|
+
};
|
|
114
|
+
value.harte = toHarte(value);
|
|
115
|
+
return { ok: true, warnings, value };
|
|
116
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ChordParts } from "./vocabulary.js";
|
|
2
|
+
/** The canonical text after the root: quality, extension, sus, sorted tones. */
|
|
3
|
+
export declare function printSuffix(parts: Omit<ChordParts, "root" | "bass">): string;
|
|
4
|
+
/** The canonical spelling: root, suffix, bass. */
|
|
5
|
+
export declare function printChord(parts: ChordParts): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { byDegree, toneInterval } from "./vocabulary.js";
|
|
2
|
+
/** The canonical text after the root: quality, extension, sus, sorted tones. */
|
|
3
|
+
export function printSuffix(parts) {
|
|
4
|
+
const tones = [...parts.tones].sort((a, b) => byDegree(toneInterval(a), toneInterval(b)));
|
|
5
|
+
return `${parts.quality}${parts.extension ?? ""}${parts.sus ?? ""}${tones.length ? `(${tones.join(",")})` : ""}`;
|
|
6
|
+
}
|
|
7
|
+
/** The canonical spelling: root, suffix, bass. */
|
|
8
|
+
export function printChord(parts) {
|
|
9
|
+
return `${parts.root}${printSuffix(parts)}${parts.bass ? `/${parts.bass}` : ""}`;
|
|
10
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Interval } from "./types.js";
|
|
2
|
+
export type Suffix = {
|
|
3
|
+
suffix: string;
|
|
4
|
+
intervals: Interval[];
|
|
5
|
+
tones: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Every chord suffix Appendix A allows (with up to three parenthesized tones),
|
|
9
|
+
* grouped by its sorted spelled pitch classes, shortest spelling first.
|
|
10
|
+
* `7alt` is left out: it claims altered tones its pitches don't show.
|
|
11
|
+
*/
|
|
12
|
+
export declare function enumerateSuffixes(): Map<string, Suffix[]>;
|
|
13
|
+
export declare const intervalSetKey: (intervals: Interval[]) => string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { intervalKey } from "./intervals.js";
|
|
2
|
+
import { printSuffix } from "./print.js";
|
|
3
|
+
import { chordIntervals, SUS_ALLOWED_WITH, TONES, VALID_EXTENSIONS, } from "./vocabulary.js";
|
|
4
|
+
let suffixes = null;
|
|
5
|
+
/**
|
|
6
|
+
* Every chord suffix Appendix A allows (with up to three parenthesized tones),
|
|
7
|
+
* grouped by its sorted spelled pitch classes, shortest spelling first.
|
|
8
|
+
* `7alt` is left out: it claims altered tones its pitches don't show.
|
|
9
|
+
*/
|
|
10
|
+
export function enumerateSuffixes() {
|
|
11
|
+
if (suffixes)
|
|
12
|
+
return suffixes;
|
|
13
|
+
const found = new Map();
|
|
14
|
+
const combos = [[]];
|
|
15
|
+
for (let i = 0; i < TONES.length; i++) {
|
|
16
|
+
combos.push([TONES[i] ?? ""]);
|
|
17
|
+
for (let j = i + 1; j < TONES.length; j++) {
|
|
18
|
+
combos.push([TONES[i] ?? "", TONES[j] ?? ""]);
|
|
19
|
+
for (let k = j + 1; k < TONES.length; k++)
|
|
20
|
+
combos.push([TONES[i] ?? "", TONES[j] ?? "", TONES[k] ?? ""]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
for (const quality of Object.keys(VALID_EXTENSIONS)) {
|
|
24
|
+
for (const extension of VALID_EXTENSIONS[quality]) {
|
|
25
|
+
if (extension === "7alt")
|
|
26
|
+
continue;
|
|
27
|
+
const susOptions = quality === "" && SUS_ALLOWED_WITH.includes(extension) ? [null, "sus2", "sus4"] : [null];
|
|
28
|
+
for (const sus of susOptions) {
|
|
29
|
+
for (const tones of combos) {
|
|
30
|
+
const parts = { root: "", quality, extension, sus, tones, bass: null };
|
|
31
|
+
const { intervals, warnings } = chordIntervals(parts, "");
|
|
32
|
+
// Skip warnings and spellings that double a pitch class (11(4), 9(2)).
|
|
33
|
+
if (warnings.length || new Set(intervals.map(intervalKey)).size !== intervals.length)
|
|
34
|
+
continue;
|
|
35
|
+
const key = intervalSetKey(intervals);
|
|
36
|
+
const list = found.get(key) ?? [];
|
|
37
|
+
list.push({ suffix: printSuffix(parts), intervals, tones: tones.length });
|
|
38
|
+
found.set(key, list);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const list of found.values()) {
|
|
44
|
+
list.sort((a, b) => a.tones - b.tones || a.suffix.length - b.suffix.length || (a.suffix < b.suffix ? -1 : 1));
|
|
45
|
+
}
|
|
46
|
+
suffixes = found;
|
|
47
|
+
return found;
|
|
48
|
+
}
|
|
49
|
+
export const intervalSetKey = (intervals) => [...new Set(intervals.map(intervalKey))].sort().join("|");
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** A scale degree above a root: `b3` is `{ degree: 3, alter: -1 }`. Degrees above 7 stay as written. */
|
|
2
|
+
export type Interval = {
|
|
3
|
+
degree: number;
|
|
4
|
+
alter: number;
|
|
5
|
+
};
|
|
6
|
+
export type Diagnostic = {
|
|
7
|
+
message: string;
|
|
8
|
+
token: string;
|
|
9
|
+
suggestion?: string;
|
|
10
|
+
};
|
|
11
|
+
/** One slot's harmony as a machine reads it (grammar Appendix A.7). */
|
|
12
|
+
export type ChordRecord = {
|
|
13
|
+
kind: "chord" | "notes";
|
|
14
|
+
source: string;
|
|
15
|
+
canonical: string;
|
|
16
|
+
root: string | null;
|
|
17
|
+
intervals: Interval[];
|
|
18
|
+
pitches: string[];
|
|
19
|
+
bass: string;
|
|
20
|
+
alteredOpen: boolean;
|
|
21
|
+
harte: string;
|
|
22
|
+
};
|
|
23
|
+
export type ParseResult = {
|
|
24
|
+
ok: true;
|
|
25
|
+
value: ChordRecord;
|
|
26
|
+
warnings: Diagnostic[];
|
|
27
|
+
} | {
|
|
28
|
+
ok: false;
|
|
29
|
+
error: Diagnostic;
|
|
30
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Diagnostic, Interval } from "./types.js";
|
|
2
|
+
export type Quality = "" | "m" | "dim" | "aug" | "5";
|
|
3
|
+
export type Extension = "6" | "6/9" | "7" | "maj7" | "9" | "maj9" | "11" | "13" | "maj13" | "7alt";
|
|
4
|
+
export type Sus = "sus2" | "sus4";
|
|
5
|
+
export type ChordParts = {
|
|
6
|
+
root: string;
|
|
7
|
+
quality: Quality;
|
|
8
|
+
extension: Extension | null;
|
|
9
|
+
sus: Sus | null;
|
|
10
|
+
tones: string[];
|
|
11
|
+
bass: string | null;
|
|
12
|
+
};
|
|
13
|
+
export declare const QUALITIES: Record<Quality, Interval[]>;
|
|
14
|
+
/** Longest first, so the tokenizer can take the first prefix that matches. */
|
|
15
|
+
export declare const EXTENSIONS: [Extension, Interval[]][];
|
|
16
|
+
/** Appendix A.3. */
|
|
17
|
+
export declare const VALID_EXTENSIONS: Record<Quality, (Extension | null)[]>;
|
|
18
|
+
/** `sus` is allowed only on the major quality, with these extensions. */
|
|
19
|
+
export declare const SUS_ALLOWED_WITH: (Extension | null)[];
|
|
20
|
+
/** Tones a parenthesis may hold. Tone `7` means the interval b7. */
|
|
21
|
+
export declare const TONES: string[];
|
|
22
|
+
export declare const toneInterval: (tone: string) => Interval;
|
|
23
|
+
/** Degree order, then flat before natural before sharp. */
|
|
24
|
+
export declare const byDegree: (a: Interval, b: Interval) => number;
|
|
25
|
+
export declare function validateParts(parts: ChordParts, symbol: string): Diagnostic | null;
|
|
26
|
+
/** The chord's intervals after every rule in Appendix A.2, without the slash bass. */
|
|
27
|
+
export declare function chordIntervals(parts: ChordParts, symbol: string): {
|
|
28
|
+
intervals: Interval[];
|
|
29
|
+
warnings: Diagnostic[];
|
|
30
|
+
alteredOpen: boolean;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Appendix A.5, applied in this order after ♯/♭ become #/b. Each pattern
|
|
34
|
+
* reads the text after the root, so a rule can never touch the root's
|
|
35
|
+
* own accidental.
|
|
36
|
+
*/
|
|
37
|
+
export declare const ALIASES: [RegExp, string][];
|
|
38
|
+
/** Appendix A.6: a rejection with its suggestion, or null when the symbol is not one. */
|
|
39
|
+
export declare function rejection(symbol: string, semitoneOf: (note: string) => number): Diagnostic | null;
|
|
40
|
+
/** Harte (2005) shorthands with Harte's own definitions, in tie-break order. */
|
|
41
|
+
export declare const HARTE_SHORTHANDS: [string, Interval[]][];
|