barmark 0.0.3 → 0.2.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 +79 -0
- package/LICENSE +21 -0
- package/README.md +74 -8
- 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 +282 -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 +135 -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 +74 -0
- package/dist/chords/parse.d.ts +2 -0
- package/dist/chords/parse.js +117 -0
- package/dist/chords/print.d.ts +7 -0
- package/dist/chords/print.js +16 -0
- package/dist/chords/suffixes.d.ts +13 -0
- package/dist/chords/suffixes.js +49 -0
- package/dist/chords/types.d.ts +33 -0
- package/dist/chords/types.js +1 -0
- package/dist/chords/vocabulary.d.ts +40 -0
- package/dist/chords/vocabulary.js +207 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +31 -8
- package/index.js +0 -11
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { ChordRecord } from "../chords/types.js";
|
|
2
|
+
export type Severity = "error" | "warning";
|
|
3
|
+
export type ChartDiagnostic = {
|
|
4
|
+
line: number;
|
|
5
|
+
severity: Severity;
|
|
6
|
+
message: string;
|
|
7
|
+
token: string;
|
|
8
|
+
suggestion?: string;
|
|
9
|
+
};
|
|
10
|
+
export type Meter = {
|
|
11
|
+
n: number;
|
|
12
|
+
d: number;
|
|
13
|
+
};
|
|
14
|
+
export type Mode = "major" | "minor" | "ionian" | "dorian" | "phrygian" | "lydian" | "mixolydian" | "aeolian" | "locrian";
|
|
15
|
+
/** The tonic is spelled with ASCII `#` and `b`. */
|
|
16
|
+
export type Key = {
|
|
17
|
+
tonic: string;
|
|
18
|
+
mode: Mode;
|
|
19
|
+
};
|
|
20
|
+
export type Feel = "straight" | "swing" | "shuffle" | "swing 16";
|
|
21
|
+
export type Directives = {
|
|
22
|
+
key?: Key;
|
|
23
|
+
time?: Meter;
|
|
24
|
+
tempo?: number;
|
|
25
|
+
feel?: Feel;
|
|
26
|
+
};
|
|
27
|
+
/** A chord slot holds a chord symbol or a note set; `chord.kind` tells which. */
|
|
28
|
+
export type SimpleSlot = {
|
|
29
|
+
kind: "chord";
|
|
30
|
+
chord: ChordRecord;
|
|
31
|
+
} | {
|
|
32
|
+
kind: "hold";
|
|
33
|
+
} | {
|
|
34
|
+
kind: "nc";
|
|
35
|
+
} | {
|
|
36
|
+
kind: "silence";
|
|
37
|
+
} | {
|
|
38
|
+
kind: "unknown";
|
|
39
|
+
};
|
|
40
|
+
export type Slot = SimpleSlot | {
|
|
41
|
+
kind: "group";
|
|
42
|
+
members: SimpleSlot[];
|
|
43
|
+
};
|
|
44
|
+
/** `meter` is a one-bar `[n/d]` override. */
|
|
45
|
+
export type Cell = {
|
|
46
|
+
kind: "slots";
|
|
47
|
+
meter: Meter | null;
|
|
48
|
+
slots: Slot[];
|
|
49
|
+
} | {
|
|
50
|
+
kind: "simile";
|
|
51
|
+
} | {
|
|
52
|
+
kind: "fermata";
|
|
53
|
+
};
|
|
54
|
+
export type Repeat = number | "open" | null;
|
|
55
|
+
export type Line = {
|
|
56
|
+
kind: "title";
|
|
57
|
+
line: number;
|
|
58
|
+
text: string;
|
|
59
|
+
} | {
|
|
60
|
+
kind: "section";
|
|
61
|
+
line: number;
|
|
62
|
+
name: string;
|
|
63
|
+
repeat: Repeat;
|
|
64
|
+
elided: boolean;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "directive";
|
|
67
|
+
line: number;
|
|
68
|
+
set: Directives;
|
|
69
|
+
} | {
|
|
70
|
+
kind: "bars";
|
|
71
|
+
line: number;
|
|
72
|
+
opens: boolean;
|
|
73
|
+
closes: boolean;
|
|
74
|
+
cells: Cell[];
|
|
75
|
+
repeat: Repeat;
|
|
76
|
+
} | {
|
|
77
|
+
kind: "text";
|
|
78
|
+
line: number;
|
|
79
|
+
text: string;
|
|
80
|
+
}
|
|
81
|
+
/** A bar line dropped for an error, kept as written so its section still counts as written out. */
|
|
82
|
+
| {
|
|
83
|
+
kind: "invalid";
|
|
84
|
+
line: number;
|
|
85
|
+
text: string;
|
|
86
|
+
};
|
|
87
|
+
export type PlayedBar = {
|
|
88
|
+
/** 1-based measure number in play order; fermatas are not counted. */
|
|
89
|
+
index: number;
|
|
90
|
+
line: number;
|
|
91
|
+
/** Heading text of the section occurrence, as written. */
|
|
92
|
+
section: string | null;
|
|
93
|
+
meter: Meter;
|
|
94
|
+
key: Key | null;
|
|
95
|
+
tempo: number | null;
|
|
96
|
+
feel: Feel;
|
|
97
|
+
/** `%` resolved to the copied slots; holds stay holds. */
|
|
98
|
+
slots: Slot[];
|
|
99
|
+
};
|
|
100
|
+
/** An open repeat shows one pass; the number of passes is unknown. */
|
|
101
|
+
export type ChartEvent = {
|
|
102
|
+
kind: "bar";
|
|
103
|
+
bar: PlayedBar;
|
|
104
|
+
} | {
|
|
105
|
+
kind: "fermata";
|
|
106
|
+
line: number;
|
|
107
|
+
} | {
|
|
108
|
+
kind: "open";
|
|
109
|
+
line: number;
|
|
110
|
+
events: ChartEvent[];
|
|
111
|
+
};
|
|
112
|
+
export type SectionOccurrence = {
|
|
113
|
+
name: string;
|
|
114
|
+
line: number;
|
|
115
|
+
replayOf: number | null;
|
|
116
|
+
/** Null when the occurrence plays no bars. */
|
|
117
|
+
firstBar: number | null;
|
|
118
|
+
lastBar: number | null;
|
|
119
|
+
elided: boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Which distinct section this plays, numbered in first-appearance order
|
|
122
|
+
* from 0: every replay shares its written section's number.
|
|
123
|
+
*/
|
|
124
|
+
form: number;
|
|
125
|
+
};
|
|
126
|
+
export type ChartParse = {
|
|
127
|
+
title: string | null;
|
|
128
|
+
lines: Line[];
|
|
129
|
+
events: ChartEvent[];
|
|
130
|
+
/** `events` flattened, with an open repeat's pass included once. */
|
|
131
|
+
bars: PlayedBar[];
|
|
132
|
+
sections: SectionOccurrence[];
|
|
133
|
+
/** Sorted by line, then errors before warnings. */
|
|
134
|
+
diagnostics: ChartDiagnostic[];
|
|
135
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ChordRecord, Interval } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The Harte label for a parsed chord or note set: the largest shorthand
|
|
4
|
+
* contained in the chord, the remaining intervals in parentheses, and the
|
|
5
|
+
* bass as an interval when it is not the root. Note sets without a marked
|
|
6
|
+
* root use their bass as the Harte root, because Harte requires one.
|
|
7
|
+
*/
|
|
8
|
+
export declare function toHarte(r: ChordRecord): string;
|
|
9
|
+
/** A Harte label's root, intervals (shorthand at its intended meaning, list applied) and bass. */
|
|
10
|
+
export declare function parseHarte(label: string): {
|
|
11
|
+
root: string;
|
|
12
|
+
intervals: Interval[];
|
|
13
|
+
bass: string;
|
|
14
|
+
} | "N" | "X" | null;
|
|
15
|
+
/** A barmark slot for a Harte label: a named chord when one has exactly its notes, else a note set. */
|
|
16
|
+
export declare function harteToBarmark(label: string): string | null;
|
|
@@ -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,74 @@
|
|
|
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
|
+
parts: null,
|
|
71
|
+
};
|
|
72
|
+
value.harte = toHarte(value);
|
|
73
|
+
return { ok: true, warnings, value };
|
|
74
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { toHarte } from "./harte.js";
|
|
2
|
+
import { noteSemitone, transpose } from "./intervals.js";
|
|
3
|
+
import { canonicalParts, 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
|
+
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
|
+
parts: canonicalParts(parts),
|
|
114
|
+
};
|
|
115
|
+
value.harte = toHarte(value);
|
|
116
|
+
return { ok: true, warnings, value };
|
|
117
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ChordParts } from "./vocabulary.js";
|
|
2
|
+
/** The parts as the canonical spelling has them: the same parts, tones sorted. */
|
|
3
|
+
export declare function canonicalParts(parts: ChordParts): ChordParts;
|
|
4
|
+
/** The canonical text after the root: quality, extension, sus, sorted tones. */
|
|
5
|
+
export declare function printSuffix(parts: Omit<ChordParts, "root" | "bass">): string;
|
|
6
|
+
/** The canonical spelling: root, suffix, bass. */
|
|
7
|
+
export declare function printChord(parts: ChordParts): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { byDegree, toneInterval } from "./vocabulary.js";
|
|
2
|
+
/** The tones in canonical order (by degree), as the canonical spelling lists them. */
|
|
3
|
+
const sortTones = (tones) => [...tones].sort((a, b) => byDegree(toneInterval(a), toneInterval(b)));
|
|
4
|
+
/** The parts as the canonical spelling has them: the same parts, tones sorted. */
|
|
5
|
+
export function canonicalParts(parts) {
|
|
6
|
+
return { ...parts, tones: sortTones(parts.tones) };
|
|
7
|
+
}
|
|
8
|
+
/** The canonical text after the root: quality, extension, sus, sorted tones. */
|
|
9
|
+
export function printSuffix(parts) {
|
|
10
|
+
const tones = sortTones(parts.tones);
|
|
11
|
+
return `${parts.quality}${parts.extension ?? ""}${parts.sus ?? ""}${tones.length ? `(${tones.join(",")})` : ""}`;
|
|
12
|
+
}
|
|
13
|
+
/** The canonical spelling: root, suffix, bass. */
|
|
14
|
+
export function printChord(parts) {
|
|
15
|
+
return `${parts.root}${printSuffix(parts)}${parts.bass ? `/${parts.bass}` : ""}`;
|
|
16
|
+
}
|
|
@@ -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("|");
|