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