@tonaljs/key 4.12.4 → 4.14.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/README.md +117 -3
- package/dist/index.d.mts +204 -1
- package/dist/index.d.ts +204 -1
- package/dist/index.js +683 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +683 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -5
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,547 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { chordScale, chordScales, get, substitutes as substitutes$1, tokenize, tritoneSubstitute } from "@tonaljs/chord";
|
|
2
|
+
import { transpose, transposeFifths } from "@tonaljs/note";
|
|
2
3
|
import { accToAlt, altToAcc, note } from "@tonaljs/pitch-note";
|
|
3
|
-
import { get } from "@tonaljs/roman-numeral";
|
|
4
|
+
import { get as get$1 } from "@tonaljs/roman-numeral";
|
|
5
|
+
import { distance } from "@tonaljs/pitch-distance";
|
|
6
|
+
import { interval } from "@tonaljs/pitch-interval";
|
|
7
|
+
import { fromRomanNumerals } from "@tonaljs/progression";
|
|
8
|
+
import { get as get$2 } from "@tonaljs/scale-type";
|
|
9
|
+
// analyze.ts
|
|
10
|
+
const MODES = [
|
|
11
|
+
"dorian",
|
|
12
|
+
"phrygian",
|
|
13
|
+
"lydian",
|
|
14
|
+
"mixolydian",
|
|
15
|
+
"locrian"
|
|
16
|
+
];
|
|
17
|
+
const SOURCE_ORDER = [
|
|
18
|
+
"diatonic",
|
|
19
|
+
"secondary dominant",
|
|
20
|
+
"related ii",
|
|
21
|
+
"borrowed",
|
|
22
|
+
"tritone substitute",
|
|
23
|
+
"backdoor",
|
|
24
|
+
"passing diminished",
|
|
25
|
+
"common-tone diminished",
|
|
26
|
+
"chromatic"
|
|
27
|
+
];
|
|
28
|
+
const NEEDS_NEIGHBOUR = [
|
|
29
|
+
"related ii",
|
|
30
|
+
"backdoor",
|
|
31
|
+
"passing diminished",
|
|
32
|
+
"common-tone diminished"
|
|
33
|
+
];
|
|
34
|
+
const chroma = (name) => note(name).chroma;
|
|
35
|
+
const mod12 = (n) => (n % 12 + 12) % 12;
|
|
36
|
+
function parse(name) {
|
|
37
|
+
if (typeof name !== "string") return void 0;
|
|
38
|
+
const [root, type] = tokenize(name);
|
|
39
|
+
if (!root) return void 0;
|
|
40
|
+
const symbol = root + type;
|
|
41
|
+
const chord = get(symbol);
|
|
42
|
+
if (chord.empty || !chord.notes.length) return void 0;
|
|
43
|
+
return {
|
|
44
|
+
name,
|
|
45
|
+
root: chord.tonic ?? root,
|
|
46
|
+
type,
|
|
47
|
+
quality: chord.quality,
|
|
48
|
+
family: chord.family,
|
|
49
|
+
intervals: chord.intervals,
|
|
50
|
+
notes: chord.notes,
|
|
51
|
+
chromas: chord.notes.map(chroma),
|
|
52
|
+
symbol
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function scaleNotes(tonic, type) {
|
|
56
|
+
return get$2(type).intervals.map((ivl) => transpose(tonic, ivl));
|
|
57
|
+
}
|
|
58
|
+
function keyForms(key, majorKey, minorKey) {
|
|
59
|
+
if (typeof key !== "string") return void 0;
|
|
60
|
+
const [tonicName, type = "major", ...rest] = key.trim().split(/\s+/);
|
|
61
|
+
if (rest.length || type !== "major" && type !== "minor") return void 0;
|
|
62
|
+
const tonic = note(tonicName).pc;
|
|
63
|
+
if (!tonic) return void 0;
|
|
64
|
+
const major = majorKey(tonic);
|
|
65
|
+
const minor = minorKey(tonic);
|
|
66
|
+
const fromKey = (name, scale) => ({
|
|
67
|
+
name: `${tonic} ${name}`,
|
|
68
|
+
notes: scale.scale,
|
|
69
|
+
functions: scale.chordsHarmonicFunction
|
|
70
|
+
});
|
|
71
|
+
const majorSource = fromKey("major", major);
|
|
72
|
+
const minorSources = [
|
|
73
|
+
fromKey("minor", minor.natural),
|
|
74
|
+
fromKey("harmonic minor", minor.harmonic),
|
|
75
|
+
fromKey("melodic minor", minor.melodic)
|
|
76
|
+
];
|
|
77
|
+
const main = type === "major" ? major.scale : minor.natural.scale;
|
|
78
|
+
const outside = (notes) => notes.filter((n) => !main.includes(n)).length;
|
|
79
|
+
const flats = (notes) => notes.reduce((sum, n) => sum + note(n).alt, 0);
|
|
80
|
+
const modes = MODES.map((mode) => ({
|
|
81
|
+
name: `${tonic} ${mode}`,
|
|
82
|
+
notes: scaleNotes(tonic, mode),
|
|
83
|
+
functions: [],
|
|
84
|
+
mode: true
|
|
85
|
+
})).sort((a, b) => outside(a.notes) - outside(b.notes) || flats(a.notes) - flats(b.notes));
|
|
86
|
+
if (type === "major") return {
|
|
87
|
+
tonic,
|
|
88
|
+
minor: false,
|
|
89
|
+
own: [majorSource],
|
|
90
|
+
parallel: [...minorSources, ...modes],
|
|
91
|
+
triads: [
|
|
92
|
+
"",
|
|
93
|
+
"m",
|
|
94
|
+
"m",
|
|
95
|
+
"",
|
|
96
|
+
"",
|
|
97
|
+
"m",
|
|
98
|
+
"dim"
|
|
99
|
+
],
|
|
100
|
+
grades: major.grades,
|
|
101
|
+
scale: major.scale
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
tonic,
|
|
105
|
+
minor: true,
|
|
106
|
+
own: minorSources,
|
|
107
|
+
parallel: [majorSource, ...modes],
|
|
108
|
+
triads: [
|
|
109
|
+
"m",
|
|
110
|
+
"dim",
|
|
111
|
+
"",
|
|
112
|
+
"m",
|
|
113
|
+
"",
|
|
114
|
+
"",
|
|
115
|
+
""
|
|
116
|
+
],
|
|
117
|
+
grades: minor.natural.grades,
|
|
118
|
+
scale: minor.natural.scale
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function degreeOf(tonic, root) {
|
|
122
|
+
return get$1(interval(distance(tonic, root))).name ?? "";
|
|
123
|
+
}
|
|
124
|
+
const isLower = (chord) => chord.quality === "Minor" || chord.quality === "Diminished";
|
|
125
|
+
const isAscii = (s) => /^[\x20-\x7e]*$/.test(s);
|
|
126
|
+
function typeAfter(chord) {
|
|
127
|
+
if (!isLower(chord)) return chord.type;
|
|
128
|
+
const target = get("C" + chord.type).chroma;
|
|
129
|
+
const works = (suffix) => {
|
|
130
|
+
const [back] = fromRomanNumerals("C", ["i" + suffix]);
|
|
131
|
+
return !!back && get(back).chroma === target;
|
|
132
|
+
};
|
|
133
|
+
const strip = (s) => s.replace(/^(m(?!aj)|-)/, "");
|
|
134
|
+
const aliases = get("C" + chord.type).aliases.filter(isAscii);
|
|
135
|
+
const written = strip(chord.type);
|
|
136
|
+
if (written !== chord.type && works(written)) return written;
|
|
137
|
+
const first = aliases.filter((a) => strip(a) !== a).map(strip).find(works);
|
|
138
|
+
if (first !== void 0) return first;
|
|
139
|
+
return [...aliases].sort((a, b) => a.length - b.length).find(works) ?? chord.type;
|
|
140
|
+
}
|
|
141
|
+
function numeralOf(tonic, chord) {
|
|
142
|
+
const degree = degreeOf(tonic, chord.root);
|
|
143
|
+
if (!degree) return "";
|
|
144
|
+
return (isLower(chord) ? degree.toLowerCase() : degree) + typeAfter(chord);
|
|
145
|
+
}
|
|
146
|
+
const inScale = (chord, notes) => chord.notes.every((n) => notes.includes(n));
|
|
147
|
+
const isDominant = (chord) => chord.family === "dominant";
|
|
148
|
+
const isMajorTriad = (chord) => chord.intervals.length === 3 && chord.intervals.includes("3M") && chord.intervals.includes("5P");
|
|
149
|
+
const isDim7 = (chord) => chord.intervals.length === 4 && [
|
|
150
|
+
"3m",
|
|
151
|
+
"5d",
|
|
152
|
+
"7d"
|
|
153
|
+
].every((i) => chord.intervals.includes(i));
|
|
154
|
+
const isTwo = (chord) => chord.intervals.includes("3m") && chord.intervals.includes("7m");
|
|
155
|
+
function targets(key) {
|
|
156
|
+
return key.scale.map((root, i) => {
|
|
157
|
+
const triad = key.triads[i];
|
|
158
|
+
const grade = key.grades[i];
|
|
159
|
+
const minor = triad === "m";
|
|
160
|
+
return {
|
|
161
|
+
root,
|
|
162
|
+
minor,
|
|
163
|
+
numeral: minor ? grade.toLowerCase() : grade,
|
|
164
|
+
i
|
|
165
|
+
};
|
|
166
|
+
}).filter((t) => t.i > 0 && key.triads[t.i] !== "dim").map(({ root, numeral, minor }) => ({
|
|
167
|
+
root,
|
|
168
|
+
numeral,
|
|
169
|
+
minor
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
function tonicTarget(key) {
|
|
173
|
+
return {
|
|
174
|
+
root: key.tonic,
|
|
175
|
+
numeral: key.minor ? "i" : "I",
|
|
176
|
+
minor: key.minor
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function targetOn(key, root, chord) {
|
|
180
|
+
const index = key.scale.findIndex((n) => chroma(n) === chroma(root));
|
|
181
|
+
if (index === -1) return "";
|
|
182
|
+
const grade = key.grades[index];
|
|
183
|
+
return (chord ? isLower(chord) : key.triads[index] === "m") ? grade.toLowerCase() : grade;
|
|
184
|
+
}
|
|
185
|
+
function resolution(targetRoot, next) {
|
|
186
|
+
if (!next) return "";
|
|
187
|
+
return chroma(next.root) === chroma(targetRoot) ? "functioning" : "non-functioning";
|
|
188
|
+
}
|
|
189
|
+
const lydianDominant = (chord, key) => chordScales(chord.symbol).includes("lydian dominant") ? `${chord.root} lydian dominant` : chordScale(chord.symbol, { key });
|
|
190
|
+
function secondaryDominants(key, chord) {
|
|
191
|
+
if (!isDominant(chord) && !isMajorTriad(chord)) return [];
|
|
192
|
+
return targets(key).filter((t) => transpose(t.root, "5P") === chord.root);
|
|
193
|
+
}
|
|
194
|
+
function analyze(key, chordName, options = {}) {
|
|
195
|
+
const chord = parse(chordName);
|
|
196
|
+
if (!chord) return [];
|
|
197
|
+
const next = parse(options.next);
|
|
198
|
+
const previous = parse(options.previous);
|
|
199
|
+
const keyName = `${key.tonic} ${key.minor ? "minor" : "major"}`;
|
|
200
|
+
const degree = numeralOf(key.tonic, chord);
|
|
201
|
+
const readings = [];
|
|
202
|
+
const add = ({ source, ...reading }) => readings.push({
|
|
203
|
+
chord: chordName,
|
|
204
|
+
numeral: degree,
|
|
205
|
+
degree,
|
|
206
|
+
source,
|
|
207
|
+
function: "",
|
|
208
|
+
scale: "",
|
|
209
|
+
target: "",
|
|
210
|
+
resolution: "",
|
|
211
|
+
chordScale: chordScale(chord.symbol, { key: keyName }),
|
|
212
|
+
...reading
|
|
213
|
+
});
|
|
214
|
+
const own = key.own.find((s) => inScale(chord, s.notes) && s.notes.includes(chord.root));
|
|
215
|
+
if (own) {
|
|
216
|
+
const index = own.notes.indexOf(chord.root);
|
|
217
|
+
let fn = own.functions[index] ?? "";
|
|
218
|
+
if (!key.minor && index === 5 && next && chroma(next.root) === chroma(key.scale[4])) fn = "SD";
|
|
219
|
+
add({
|
|
220
|
+
source: "diatonic",
|
|
221
|
+
function: fn,
|
|
222
|
+
scale: own.name,
|
|
223
|
+
chordScale: chordScale(chord.symbol, { key: own.name })
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
if (!own) {
|
|
227
|
+
for (const t of secondaryDominants(key, chord)) add({
|
|
228
|
+
source: "secondary dominant",
|
|
229
|
+
numeral: `V${chord.type}/${t.numeral}`,
|
|
230
|
+
function: "D",
|
|
231
|
+
target: t.numeral,
|
|
232
|
+
resolution: resolution(t.root, next)
|
|
233
|
+
});
|
|
234
|
+
if (isDominant(chord)) for (const t of [tonicTarget(key), ...targets(key)]) {
|
|
235
|
+
if (mod12(chroma(t.root) + 1) !== chroma(chord.root)) continue;
|
|
236
|
+
add({
|
|
237
|
+
source: "tritone substitute",
|
|
238
|
+
numeral: `subV${chord.type}/${t.numeral}`,
|
|
239
|
+
function: "D",
|
|
240
|
+
target: t.numeral,
|
|
241
|
+
resolution: resolution(t.root, next),
|
|
242
|
+
chordScale: lydianDominant(chord, keyName)
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (next && isTwo(chord) && transpose(next.root, "5P") === chord.root) {
|
|
247
|
+
const nextTargets = key.own.some((s) => inScale(next, s.notes) && s.notes.includes(next.root)) ? [] : secondaryDominants(key, next);
|
|
248
|
+
for (const t of nextTargets) add({
|
|
249
|
+
source: "related ii",
|
|
250
|
+
numeral: `ii${typeAfter(chord)}/${t.numeral}`,
|
|
251
|
+
function: "SD",
|
|
252
|
+
target: t.numeral,
|
|
253
|
+
resolution: "functioning",
|
|
254
|
+
chordScale: chordScale(chord.symbol, { key: `${t.root} ${t.minor ? "minor" : "major"}` })
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
if (next && isDominant(chord) && mod12(chroma(key.tonic) - 2) === chroma(chord.root) && chroma(next.root) === chroma(key.tonic)) add({
|
|
258
|
+
source: "backdoor",
|
|
259
|
+
function: "D",
|
|
260
|
+
target: key.minor ? "i" : "I",
|
|
261
|
+
resolution: "functioning",
|
|
262
|
+
chordScale: lydianDominant(chord, keyName)
|
|
263
|
+
});
|
|
264
|
+
if (isDim7(chord)) {
|
|
265
|
+
const onDegree = (c) => key.own.some((s) => s.notes.some((n) => chroma(n) === chroma(c.root)));
|
|
266
|
+
if (next && onDegree(next) && chord.chromas.includes(mod12(chroma(next.root) - 1))) {
|
|
267
|
+
const target = targetOn(key, next.root, next);
|
|
268
|
+
add({
|
|
269
|
+
source: "passing diminished",
|
|
270
|
+
numeral: chroma(next.root) === chroma(key.tonic) ? `vii${typeAfter(chord)}` : `vii${typeAfter(chord)}/${target}`,
|
|
271
|
+
function: "D",
|
|
272
|
+
target,
|
|
273
|
+
resolution: "functioning",
|
|
274
|
+
chordScale: chordScale(chord.symbol)
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
if (previous && next && onDegree(next) && chroma(previous.root) === mod12(chroma(chord.root) + 1) && chroma(next.root) === mod12(chroma(chord.root) - 1)) add({
|
|
278
|
+
source: "passing diminished",
|
|
279
|
+
target: targetOn(key, next.root, next),
|
|
280
|
+
resolution: "functioning",
|
|
281
|
+
chordScale: chordScale(chord.symbol)
|
|
282
|
+
});
|
|
283
|
+
const embellished = [next, previous].find((c) => c && onDegree(c) && chroma(c.root) === chroma(chord.root));
|
|
284
|
+
if (embellished) add({
|
|
285
|
+
source: "common-tone diminished",
|
|
286
|
+
target: targetOn(key, embellished.root, embellished),
|
|
287
|
+
resolution: next ? chroma(next.root) === chroma(chord.root) ? "functioning" : "non-functioning" : "",
|
|
288
|
+
chordScale: chordScale(chord.symbol)
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
if (!own) {
|
|
292
|
+
const source = key.parallel.find((s) => (!s.mode || !readings.length) && inScale(chord, s.notes) && s.notes.includes(chord.root));
|
|
293
|
+
if (source) {
|
|
294
|
+
const index = source.notes.indexOf(chord.root);
|
|
295
|
+
add({
|
|
296
|
+
source: "borrowed",
|
|
297
|
+
function: source.functions[index] ?? "",
|
|
298
|
+
scale: source.name,
|
|
299
|
+
chordScale: chordScale(chord.symbol, { key: source.name })
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (!readings.length) add({ source: "chromatic" });
|
|
304
|
+
const afterTwo = previous && isTwo(previous) && transpose(chord.root, "5P") === previous.root;
|
|
305
|
+
const confirmed = (r) => r.resolution === "functioning" || NEEDS_NEIGHBOUR.includes(r.source) || !!afterTwo && r.source === "secondary dominant";
|
|
306
|
+
return readings.map((r, i) => ({
|
|
307
|
+
r,
|
|
308
|
+
i
|
|
309
|
+
})).sort((a, b) => Number(confirmed(b.r)) - Number(confirmed(a.r)) || SOURCE_ORDER.indexOf(a.r.source) - SOURCE_ORDER.indexOf(b.r.source) || a.i - b.i).map(({ r }) => r);
|
|
310
|
+
}
|
|
311
|
+
// substitutes.ts
|
|
312
|
+
const pcs = (name) => get(name).notes.map((n) => note(n).chroma).sort((a, b) => a - b).join(",");
|
|
313
|
+
const INTO_MAJOR = [
|
|
314
|
+
[
|
|
315
|
+
"2M",
|
|
316
|
+
"m7b5",
|
|
317
|
+
"minor",
|
|
318
|
+
["ii7"]
|
|
319
|
+
],
|
|
320
|
+
[
|
|
321
|
+
"3m",
|
|
322
|
+
"maj7",
|
|
323
|
+
"minor",
|
|
324
|
+
["IV", "V7"]
|
|
325
|
+
],
|
|
326
|
+
[
|
|
327
|
+
"4P",
|
|
328
|
+
"m7",
|
|
329
|
+
"minor",
|
|
330
|
+
["IV"]
|
|
331
|
+
],
|
|
332
|
+
[
|
|
333
|
+
"4P",
|
|
334
|
+
"m6",
|
|
335
|
+
"minor",
|
|
336
|
+
["IV"]
|
|
337
|
+
],
|
|
338
|
+
[
|
|
339
|
+
"5P",
|
|
340
|
+
"m7",
|
|
341
|
+
"minor",
|
|
342
|
+
["V7"]
|
|
343
|
+
],
|
|
344
|
+
[
|
|
345
|
+
"6m",
|
|
346
|
+
"maj7",
|
|
347
|
+
"minor",
|
|
348
|
+
["iv"]
|
|
349
|
+
],
|
|
350
|
+
[
|
|
351
|
+
"7m",
|
|
352
|
+
"7",
|
|
353
|
+
"minor",
|
|
354
|
+
["iv"]
|
|
355
|
+
],
|
|
356
|
+
[
|
|
357
|
+
"2m",
|
|
358
|
+
"maj7",
|
|
359
|
+
"phrygian",
|
|
360
|
+
["iv"]
|
|
361
|
+
],
|
|
362
|
+
[
|
|
363
|
+
"7m",
|
|
364
|
+
"maj7",
|
|
365
|
+
"mixolydian",
|
|
366
|
+
["IV"]
|
|
367
|
+
],
|
|
368
|
+
[
|
|
369
|
+
"1P",
|
|
370
|
+
"maj7#11",
|
|
371
|
+
"lydian",
|
|
372
|
+
["I"]
|
|
373
|
+
]
|
|
374
|
+
];
|
|
375
|
+
const INTO_MINOR = [
|
|
376
|
+
[
|
|
377
|
+
"1P",
|
|
378
|
+
"maj7",
|
|
379
|
+
"major",
|
|
380
|
+
["i"]
|
|
381
|
+
],
|
|
382
|
+
[
|
|
383
|
+
"2M",
|
|
384
|
+
"m7",
|
|
385
|
+
"major",
|
|
386
|
+
["ii7b5"]
|
|
387
|
+
],
|
|
388
|
+
[
|
|
389
|
+
"4P",
|
|
390
|
+
"maj7",
|
|
391
|
+
"major",
|
|
392
|
+
["IV7"]
|
|
393
|
+
]
|
|
394
|
+
];
|
|
395
|
+
function borrowedChordsOf(key, fns) {
|
|
396
|
+
const forms = keyForms(key, fns.majorKey, fns.minorKey);
|
|
397
|
+
if (!forms) return [];
|
|
398
|
+
const { tonic } = forms;
|
|
399
|
+
return (forms.minor ? INTO_MINOR : INTO_MAJOR).map(([ivl, type, from, replaces]) => {
|
|
400
|
+
const chord = note(transpose(tonic, ivl)).pc + type;
|
|
401
|
+
return {
|
|
402
|
+
chord,
|
|
403
|
+
numeral: numeralOf(tonic, parse(chord)),
|
|
404
|
+
from: `${tonic} ${from}`,
|
|
405
|
+
replaces: replaces.slice()
|
|
406
|
+
};
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
function plays(tonic, chordName, numeral) {
|
|
410
|
+
const r = get$1(numeral);
|
|
411
|
+
const chord = get(chordName);
|
|
412
|
+
if (r.empty || chord.empty || !chord.tonic) return false;
|
|
413
|
+
const expected = transpose(tonic, r.interval);
|
|
414
|
+
if (note(expected).chroma !== note(chord.tonic).chroma) return false;
|
|
415
|
+
const ivls = chord.intervals;
|
|
416
|
+
const third = r.major ? "3M" : "3m";
|
|
417
|
+
if (!ivls.includes(third)) return false;
|
|
418
|
+
if (r.major && r.chordType === "7") return ivls.includes("7m");
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
function keySubstitutes(key, chordName, options, fns) {
|
|
422
|
+
const forms = keyForms(key, fns.majorKey, fns.minorKey);
|
|
423
|
+
const parsed = parse(chordName);
|
|
424
|
+
if (!forms || !parsed) return [];
|
|
425
|
+
const { kinds } = options;
|
|
426
|
+
const wants = (kind) => !kinds || kinds.includes(kind);
|
|
427
|
+
let minor = options.minor;
|
|
428
|
+
if (minor === void 0) {
|
|
429
|
+
const target = note(transpose(parsed.root, "4P")).pc;
|
|
430
|
+
const i = forms.scale.indexOf(target);
|
|
431
|
+
if (i !== -1 && forms.triads[i] === "m") minor = true;
|
|
432
|
+
}
|
|
433
|
+
const chordKinds = kinds?.filter((k) => k !== "diatonic" && k !== "borrowed");
|
|
434
|
+
const result = substitutes$1(chordName, {
|
|
435
|
+
next: options.next,
|
|
436
|
+
kinds: chordKinds,
|
|
437
|
+
minor
|
|
438
|
+
}).map((s) => ({
|
|
439
|
+
chords: s.chords,
|
|
440
|
+
kind: s.kind
|
|
441
|
+
}));
|
|
442
|
+
const scale = forms.minor ? fns.minorKey(forms.tonic).natural : fns.majorKey(forms.tonic);
|
|
443
|
+
if (wants("diatonic")) {
|
|
444
|
+
const chords = parsed.intervals.length === 3 ? scale.triads : scale.chords;
|
|
445
|
+
const degree = scale.scale.indexOf(note(parsed.root).pc);
|
|
446
|
+
const inKey = parsed.notes.every((n) => scale.scale.includes(note(n).pc));
|
|
447
|
+
if (degree !== -1 && inKey) {
|
|
448
|
+
const fn = scale.chordsHarmonicFunction[degree];
|
|
449
|
+
scale.chordsHarmonicFunction.forEach((f, i) => {
|
|
450
|
+
if (i !== degree && f === fn) result.push({
|
|
451
|
+
chords: [chords[i]],
|
|
452
|
+
kind: "diatonic"
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (wants("borrowed")) {
|
|
458
|
+
for (const b of borrowedChordsOf(key, fns)) if (!(pcs(b.chord) === pcs(parsed.symbol)) && b.replaces.some((n) => plays(forms.tonic, parsed.symbol, n))) result.push({
|
|
459
|
+
chords: [b.chord],
|
|
460
|
+
kind: "borrowed"
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
return result;
|
|
464
|
+
}
|
|
465
|
+
// pivot.ts
|
|
466
|
+
const identity = (name) => {
|
|
467
|
+
const chord = get(name);
|
|
468
|
+
return `${note(chord.tonic ?? "").chroma}|${chord.chroma}`;
|
|
469
|
+
};
|
|
470
|
+
function keyChords(key, sevenths, fns) {
|
|
471
|
+
const [tonic, type = "major", ...rest] = key.trim().split(/\s+/);
|
|
472
|
+
if (rest.length || type !== "major" && type !== "minor") return void 0;
|
|
473
|
+
if (note(tonic).empty) return void 0;
|
|
474
|
+
const minor = type === "minor" ? fns.minorKey(tonic) : void 0;
|
|
475
|
+
const scales = minor ? [
|
|
476
|
+
minor.natural,
|
|
477
|
+
minor.harmonic,
|
|
478
|
+
minor.melodic
|
|
479
|
+
] : [fns.majorKey(tonic)];
|
|
480
|
+
const chords = [];
|
|
481
|
+
const seen = /* @__PURE__ */ new Set();
|
|
482
|
+
for (const scale of scales) for (const chord of sevenths ? scale.chords : scale.triads) {
|
|
483
|
+
const id = identity(chord);
|
|
484
|
+
if (!seen.has(id)) {
|
|
485
|
+
seen.add(id);
|
|
486
|
+
chords.push(chord);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return chords;
|
|
490
|
+
}
|
|
491
|
+
function pivotChordsOf(from, to, options, fns) {
|
|
492
|
+
if (typeof from !== "string" || typeof to !== "string") return [];
|
|
493
|
+
const sevenths = !!options.sevenths;
|
|
494
|
+
const a = keyChords(from, sevenths, fns);
|
|
495
|
+
const b = keyChords(to, sevenths, fns);
|
|
496
|
+
if (!a || !b) return [];
|
|
497
|
+
const inB = new Map(b.map((c) => [identity(c), c]));
|
|
498
|
+
const inA = new Set(a.map(identity));
|
|
499
|
+
const diatonic = (key, chord) => fns.analyzeChord(key, chord).find((r) => r.source === "diatonic");
|
|
500
|
+
const functional = (key, chord) => fns.analyzeChord(key, chord).find((r) => r.source !== "chromatic" && (r.source !== "borrowed" || / (major|minor)$/.test(r.scale)));
|
|
501
|
+
const result = [];
|
|
502
|
+
for (const chord of a) {
|
|
503
|
+
const shared = inB.get(identity(chord));
|
|
504
|
+
if (!shared) continue;
|
|
505
|
+
const ra = diatonic(from, chord);
|
|
506
|
+
const rb = diatonic(to, shared);
|
|
507
|
+
if (!ra || !rb) continue;
|
|
508
|
+
result.push({
|
|
509
|
+
chord: shared,
|
|
510
|
+
from: ra.numeral,
|
|
511
|
+
to: rb.numeral,
|
|
512
|
+
fromSource: "diatonic",
|
|
513
|
+
toSource: "diatonic"
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
if (!options.chromatic) return result;
|
|
517
|
+
for (const chord of a) {
|
|
518
|
+
if (inB.has(identity(chord))) continue;
|
|
519
|
+
const ra = diatonic(from, chord);
|
|
520
|
+
const rb = functional(to, chord);
|
|
521
|
+
if (!ra || !rb) continue;
|
|
522
|
+
result.push({
|
|
523
|
+
chord,
|
|
524
|
+
from: ra.numeral,
|
|
525
|
+
to: rb.numeral,
|
|
526
|
+
fromSource: "diatonic",
|
|
527
|
+
toSource: rb.source
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
for (const chord of b) {
|
|
531
|
+
if (inA.has(identity(chord))) continue;
|
|
532
|
+
const ra = functional(from, chord);
|
|
533
|
+
const rb = diatonic(to, chord);
|
|
534
|
+
if (!ra || !rb) continue;
|
|
535
|
+
result.push({
|
|
536
|
+
chord,
|
|
537
|
+
from: ra.numeral,
|
|
538
|
+
to: rb.numeral,
|
|
539
|
+
fromSource: ra.source,
|
|
540
|
+
toSource: "diatonic"
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
4
545
|
// index.ts
|
|
5
546
|
const Empty = Object.freeze([]);
|
|
6
547
|
const NoKey = {
|
|
@@ -46,12 +587,12 @@ const NoMinorKey = {
|
|
|
46
587
|
const mapScaleToType = (scale, list, sep = "") => list.map((type, i) => `${scale[i]}${sep}${type}`);
|
|
47
588
|
function keyScale(grades, triads, chordTypes, harmonicFunctions, chordScales) {
|
|
48
589
|
return (tonic) => {
|
|
49
|
-
const intervals = grades.map((gr) => get(gr).interval || "");
|
|
590
|
+
const intervals = grades.map((gr) => get$1(gr).interval || "");
|
|
50
591
|
const scale = intervals.map((interval) => transpose(tonic, interval));
|
|
51
592
|
const chords = mapScaleToType(scale, chordTypes);
|
|
52
593
|
const secondaryDominants = scale.map((note) => transpose(note, "5P")).map((note) => scale.includes(note) && !chords.includes(note + "7") ? note + "7" : "");
|
|
53
594
|
const secondaryDominantSupertonics = supertonics(secondaryDominants, triads);
|
|
54
|
-
const substituteDominants = secondaryDominants.map((chord) => chord ?
|
|
595
|
+
const substituteDominants = secondaryDominants.map((chord) => chord ? tritoneSubstitute(chord) : "");
|
|
55
596
|
const substituteDominantSupertonics = substituteSupertonics(substituteDominants);
|
|
56
597
|
return {
|
|
57
598
|
tonic,
|
|
@@ -71,10 +612,6 @@ function keyScale(grades, triads, chordTypes, harmonicFunctions, chordScales) {
|
|
|
71
612
|
};
|
|
72
613
|
};
|
|
73
614
|
}
|
|
74
|
-
function substituteRoot(dominantRoot) {
|
|
75
|
-
const root = transpose(dominantRoot, "5d");
|
|
76
|
-
return note(root).acc.length > 1 ? simplify(root) : root;
|
|
77
|
-
}
|
|
78
615
|
const supertonicRoot = (dominant) => transpose(dominant.slice(0, -1), "5P");
|
|
79
616
|
const supertonics = (dominants, targetTriads) => dominants.map((chord, index) => {
|
|
80
617
|
if (!chord) return "";
|
|
@@ -212,12 +749,149 @@ function majorTonicFromKeySignature(sig) {
|
|
|
212
749
|
else if (typeof sig === "string" && /^(b*|#*)$/.test(sig)) return transposeFifths("C", accToAlt(sig));
|
|
213
750
|
return null;
|
|
214
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* Analyse a chord in a key: every reading of what the chord does there,
|
|
754
|
+
* ranked, the first being the default. Each reading has its numeral as the
|
|
755
|
+
* books write it ("V7/ii", "subV7/I", "ii7/V", "bVIImaj7"), the chord's plain
|
|
756
|
+
* numeral on the tonic (`degree`), its source, harmonic function ("T", "SD",
|
|
757
|
+
* "D" or ""), the scale it comes from, the numeral it leads to (`target`),
|
|
758
|
+
* whether the next chord goes there (`resolution`) and a chord scale chosen
|
|
759
|
+
* by function. Returns `[]` for an invalid key or chord; a valid chord always
|
|
760
|
+
* has at least one reading.
|
|
761
|
+
*
|
|
762
|
+
* The key is "C major", "A minor" or a tonic alone ("C", major). A minor key
|
|
763
|
+
* has the chords of its natural, harmonic and melodic forms. A slash chord is
|
|
764
|
+
* read without its bass. The readings, by source:
|
|
765
|
+
*
|
|
766
|
+
* - "diatonic": every note in a scale of the key, root on a degree.
|
|
767
|
+
* - "secondary dominant": a dominant or major triad a fifth above a degree
|
|
768
|
+
* whose chord is major or minor, not the tonic ("A7" in C is "V7/ii").
|
|
769
|
+
* - "tritone substitute": a dominant a half step above one of those degrees
|
|
770
|
+
* or the tonic ("Db7" in C is "subV7/I"); chord scale lydian dominant.
|
|
771
|
+
* - "related ii": a m7 or m7b5 a fifth above a next chord that is a
|
|
772
|
+
* secondary dominant ("Am7" before "D7" in C is "ii7/V").
|
|
773
|
+
* - "borrowed": in the parallel key (major or the minor forms), or in a
|
|
774
|
+
* parallel mode when nothing else reads the chord ("Fm7" in C is "iv7").
|
|
775
|
+
* - "backdoor": a dominant a whole step below the tonic, before the tonic.
|
|
776
|
+
* - "passing diminished": a dim7 a half step below a next chord on a degree
|
|
777
|
+
* ("C#o7" before "Dm7" is "viio7/ii"), or between two chords a whole step
|
|
778
|
+
* apart going down ("Em7 Ebo7 Dm7").
|
|
779
|
+
* - "common-tone diminished": a dim7 on the root of the chord before or after.
|
|
780
|
+
* - "chromatic": when nothing else matches.
|
|
781
|
+
*
|
|
782
|
+
* Ranking: readings the neighbours confirm first (the next chord on the
|
|
783
|
+
* target, a related ii, a backdoor or diminished chord, a secondary dominant
|
|
784
|
+
* after its related ii), then by source in the order above. The vi of a
|
|
785
|
+
* major key before the V is "SD" instead of "T".
|
|
786
|
+
*
|
|
787
|
+
* @param key - the key, like "C major" or "A minor"
|
|
788
|
+
* @param chord - the chord symbol, like "A7"
|
|
789
|
+
* @param options - `previous` and `next`, the chords around it
|
|
790
|
+
* @example
|
|
791
|
+
* Key.analyzeChord("C major", "A7", { next: "Dm7" })[0].numeral; // => "V7/ii"
|
|
792
|
+
* Key.analyzeChord("C major", "Bb7", { next: "C" }).map((r) => r.source); // => ["backdoor", "borrowed", "tritone substitute"]
|
|
793
|
+
* Key.analyzeChord("C major", "Am7", { next: "D7" }).map((r) => r.numeral); // => ["ii7/V", "vi7"]
|
|
794
|
+
* Key.analyzeChord("A minor", "E7")[0].scale; // => "A harmonic minor"
|
|
795
|
+
*/
|
|
796
|
+
function analyzeChord(key, chord, options) {
|
|
797
|
+
const forms = keyForms(key, MajorScale, minorKey);
|
|
798
|
+
return forms ? analyze(forms, chord, options) : [];
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Get the chords a key borrows from its parallel scales (modal interchange),
|
|
802
|
+
* as Pease lists them, with their numeral, the scale they come from and the
|
|
803
|
+
* numerals of the key's chords they replace. In a major key (C):
|
|
804
|
+
*
|
|
805
|
+
* - from C minor: Dm7b5 (ii7b5) for ii7; Ebmaj7 (bIIImaj7) for IV or V7;
|
|
806
|
+
* Fm7 and Fm6 (iv7, iv6) for IV; Gm7 (v7) for V7; Abmaj7 (bVImaj7) and
|
|
807
|
+
* Bb7 (bVII7) for iv;
|
|
808
|
+
* - from C phrygian: Dbmaj7 (bIImaj7) for iv;
|
|
809
|
+
* - from C mixolydian: Bbmaj7 (bVIImaj7) for IV;
|
|
810
|
+
* - from C lydian: Cmaj7#11 (Imaj7#11), a tonic alias.
|
|
811
|
+
*
|
|
812
|
+
* In a minor key, from the parallel major (Jazzology, and Pease's Ionian
|
|
813
|
+
* into Dorian): Imaj7 for i (the Picardy 3rd, the most common), iim7 for
|
|
814
|
+
* ii7b5, and IVmaj7 for IV7. It returns `[]` for an invalid key.
|
|
815
|
+
*
|
|
816
|
+
* @param key - a key name, like "C major" (or "C") or "A minor"
|
|
817
|
+
* @example
|
|
818
|
+
* Key.borrowedChords("C major")[0]; // => { chord: "Dm7b5", numeral: "ii7b5", from: "C minor", replaces: ["ii7"] }
|
|
819
|
+
* Key.borrowedChords("C major").map((b) => b.chord); // => ["Dm7b5", "Ebmaj7", "Fm7", "Fm6", "Gm7", "Abmaj7", "Bb7", "Dbmaj7", "Bbmaj7", "Cmaj7#11"]
|
|
820
|
+
* Key.borrowedChords("A minor").map((b) => b.chord); // => ["Amaj7", "Bm7", "Dmaj7"]
|
|
821
|
+
*/
|
|
822
|
+
function borrowedChords(key) {
|
|
823
|
+
return borrowedChordsOf(key, {
|
|
824
|
+
majorKey: MajorScale,
|
|
825
|
+
minorKey
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Get the substitutes of a chord in a key: `Chord.substitutes`, then the
|
|
830
|
+
* key's own, each as `{ chords, kind }`:
|
|
831
|
+
*
|
|
832
|
+
* - "diatonic": the key's chords with the same harmonic function
|
|
833
|
+
* (`chordsHarmonicFunction`), triads for a triad and 7th chords otherwise:
|
|
834
|
+
* Em7 and Am7 for Cmaj7 in C, Dm7 for Fmaj7, Bm7b5 for G7. Only for a chord
|
|
835
|
+
* of the key: its root a degree and its notes in the scale (the natural
|
|
836
|
+
* minor's, in a minor key);
|
|
837
|
+
* - "borrowed": the chords of `Key.borrowedChords` that replace the chord's
|
|
838
|
+
* numeral (by root, 3rd, and a dominant for "V7").
|
|
839
|
+
*
|
|
840
|
+
* The related ii of a dominant is an m7b5 when its target (a 4th up) is a
|
|
841
|
+
* minor chord of the key, unless `minor` says otherwise: Bm7b5 for E7 in C.
|
|
842
|
+
* `kinds` keeps only those kinds. It returns `[]` for an invalid key or a
|
|
843
|
+
* chord without root.
|
|
844
|
+
*
|
|
845
|
+
* @param key - a key name, like "C major" (or "C") or "A minor"
|
|
846
|
+
* @param chord - the chord symbol, like "G7"
|
|
847
|
+
* @param options - `next`: the chord after; `kinds`; `minor`
|
|
848
|
+
* @example
|
|
849
|
+
* Key.substitutes("C major", "Fmaj7").map((s) => `${s.kind}: ${s.chords}`);
|
|
850
|
+
* // => ["contained: Am", "diatonic: Dm7", "borrowed: Ebmaj7", "borrowed: Fm7", "borrowed: Fm6", "borrowed: Bbmaj7"]
|
|
851
|
+
* Key.substitutes("C major", "E7", { kinds: ["related ii"] }); // => [{ chords: ["Bm7b5", "E7"], kind: "related ii" }]
|
|
852
|
+
*/
|
|
853
|
+
function substitutes(key, chord, options = {}) {
|
|
854
|
+
return keySubstitutes(key, chord, options, {
|
|
855
|
+
majorKey: MajorScale,
|
|
856
|
+
minorKey
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Get the chords two keys share (pivot chords, to modulate from one to the
|
|
861
|
+
* other), with the numeral of each in both keys (as `Key.analyzeChord`
|
|
862
|
+
* writes it). Triads by default, 7th chords with `sevenths`. A minor key has
|
|
863
|
+
* the chords of its natural, harmonic and melodic forms. Chords match by
|
|
864
|
+
* root and type, whatever their spelling, and are spelled as in the second
|
|
865
|
+
* key, in the order of the first key's chords.
|
|
866
|
+
*
|
|
867
|
+
* With `chromatic`, then the chords of one key that have a functional
|
|
868
|
+
* reading in the other: the first reading of `Key.analyzeChord` that isn't
|
|
869
|
+
* "chromatic" or borrowed from a mode (borrowed from the parallel major or
|
|
870
|
+
* minor counts). D7 is V7/V in C and V7 in G. The chords of the first key
|
|
871
|
+
* come first, then the second's. `fromSource` and `toSource` say where each
|
|
872
|
+
* reading comes from. It returns `[]` for an invalid key.
|
|
873
|
+
*
|
|
874
|
+
* @param from - a key name, like "C major" (or "C") or "A minor"
|
|
875
|
+
* @param to - the other key
|
|
876
|
+
* @param options - `sevenths`, `chromatic`
|
|
877
|
+
* @example
|
|
878
|
+
* Key.pivotChords("C major", "G major").map((p) => `${p.chord}: ${p.from} ${p.to}`); // => ["C: I IV", "Em: iii vi", "G: V I", "Am: vi ii"]
|
|
879
|
+
* Key.pivotChords("C major", "G major", { sevenths: true }).map((p) => p.chord); // => ["Cmaj7", "Em7", "Am7"]
|
|
880
|
+
* Key.pivotChords("C major", "G major", { chromatic: true }).slice(4).map((p) => `${p.chord}: ${p.from} ${p.to}`); // => ["Dm: ii v", "F: IV bVII", "D: V/V V"]
|
|
881
|
+
*/
|
|
882
|
+
function pivotChords(from, to, options = {}) {
|
|
883
|
+
return pivotChordsOf(from, to, options, {
|
|
884
|
+
majorKey: MajorScale,
|
|
885
|
+
minorKey,
|
|
886
|
+
analyzeChord
|
|
887
|
+
});
|
|
888
|
+
}
|
|
215
889
|
/** @deprecated Use the named exports instead. */
|
|
216
890
|
var key_default = {
|
|
217
891
|
majorKey,
|
|
218
892
|
majorTonicFromKeySignature,
|
|
219
893
|
minorKey
|
|
220
894
|
};
|
|
221
|
-
export { key_default as default, majorKey, majorKeyChords, majorTonicFromKeySignature, minorKey, minorKeyChords };
|
|
895
|
+
export { analyzeChord, borrowedChords, key_default as default, majorKey, majorKeyChords, majorTonicFromKeySignature, minorKey, minorKeyChords, pivotChords, substitutes };
|
|
222
896
|
|
|
223
897
|
//# sourceMappingURL=index.mjs.map
|