@tonaljs/key 4.11.8 → 4.12.1

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 CHANGED
@@ -62,6 +62,8 @@ Key.majorKey("C"); // =>
62
62
  }
63
63
  ```
64
64
 
65
+ A substitute dominant is spelled a diminished fifth above its secondary dominant (`Gb7` for `C7`), unless that needs a double flat or sharp: then it takes the simplest name, as a lead sheet writes it. So in Eb major the substitute of `Eb7` is `A7`, not `Bbb7`.
66
+
65
67
  The supertonics are the ii chords of the ii-V into each degree:
66
68
 
67
69
  - A secondary dominant's ii takes its quality from the target. Into a minor or diminished chord it's half-diminished (`Em7b5 A7 Dm`), and into a major or augmented chord it's a minor seventh (`Gm7 C7 F`).
@@ -94,8 +96,8 @@ Key.minorKey("C"); // =>
94
96
  chordScales: ["C minor", "D locrian", "Eb major", "F dorian", "G phrygian", "Ab lydian", "Bb mixolydian"],
95
97
  secondaryDominants: ["G7", "", "", "C7", "D7", "Eb7", "F7"],
96
98
  secondaryDominantSupertonics: ["Dm7b5", "", "", "Gm7b5", "Am7b5", "Bbm7", "Cm7"],
97
- substituteDominants: ["Db7", "", "", "Gb7", "Ab7", "Bbb7", "Cb7"],
98
- substituteDominantSupertonics: ["Abm7", "", "", "Dbm7", "Ebm7", "Fbm7", "Gbm7"],
99
+ substituteDominants: ["Db7", "", "", "Gb7", "Ab7", "A7", "Cb7"],
100
+ substituteDominantSupertonics: ["Abm7", "", "", "Dbm7", "Ebm7", "Em7", "Gbm7"],
99
101
  },
100
102
  harmonic: {
101
103
  tonic: "C",
@@ -105,11 +107,11 @@ Key.minorKey("C"); // =>
105
107
  triads: ["Cm", "Ddim", "Ebaug", "Fm", "G", "Ab", "Bdim"],
106
108
  chords: ["CmMaj7", "Dm7b5", "Eb+maj7", "Fm7", "G7", "Abmaj7", "Bo7"],
107
109
  chordsHarmonicFunction: ["T", "SD", "T", "SD", "D", "SD", "D"],
108
- chordScales: ["C harmonic minor", "D locrian 6", "Eb major augmented", "F lydian diminished", "G phrygian dominant", "Ab lydian #9", "B ultralocrian"],
110
+ chordScales: ["C harmonic minor", "D locrian 6", "Eb major augmented", "F dorian #4", "G phrygian dominant", "Ab lydian #9", "B ultralocrian"],
109
111
  secondaryDominants: ["", "", "", "C7", "D7", "Eb7", ""],
110
112
  secondaryDominantSupertonics: ["", "", "", "Gm7b5", "Am7", "Bbm7", ""],
111
- substituteDominants: ["", "", "", "Gb7", "Ab7", "Bbb7", ""],
112
- substituteDominantSupertonics: ["", "", "", "Dbm7", "Ebm7", "Fbm7", ""],
113
+ substituteDominants: ["", "", "", "Gb7", "Ab7", "A7", ""],
114
+ substituteDominantSupertonics: ["", "", "", "Dbm7", "Ebm7", "Em7", ""],
113
115
  },
114
116
  melodic: {
115
117
  tonic: "C",
@@ -166,7 +168,7 @@ Return a list of all available chords for a given minor key:
166
168
  Key.minorKeyChords("C")
167
169
  .map((chord) => chord.name)
168
170
  .join(", ");
169
- // => "Cm7, Dm7b5, Ebmaj7, Fm7, Gm7, Abmaj7, Bb7, G7, C7, D7, Eb7, F7, Gm7b5, Am7b5, Bbm7, Db7, Gb7, Ab7, Bbb7, Cb7, Abm7, Dbm7, Ebm7, Fbm7, Gbm7, CmMaj7, Eb+maj7, Bo7, Am7, Cm6, Dm7, Bm7b5, A7, Em7b5"
171
+ // => "Cm7, Dm7b5, Ebmaj7, Fm7, Gm7, Abmaj7, Bb7, G7, C7, D7, Eb7, F7, Gm7b5, Am7b5, Bbm7, Db7, Gb7, Ab7, A7, Cb7, Abm7, Dbm7, Ebm7, Em7, Gbm7, CmMaj7, Eb+maj7, Bo7, Am7, Cm6, Dm7, Bm7b5, Em7b5"
170
172
  ```
171
173
 
172
174
  ## How to...
package/dist/index.d.mts CHANGED
@@ -27,7 +27,7 @@ interface KeyScale {
27
27
  readonly secondaryDominants: readonly string[];
28
28
  /** The ii chord that precedes each secondary dominant ("" if none): a minor seventh, or a half-diminished chord when the target is minor. */
29
29
  readonly secondaryDominantSupertonics: readonly string[];
30
- /** Tritone substitution of each secondary dominant ("" if none): the dominant seventh a tritone away. */
30
+ /** Tritone substitution of each secondary dominant ("" if none): the dominant seventh a tritone away, spelled a diminished fifth above it (Gb7 for C7) unless that needs a double flat or sharp (A7, not Bbb7, for Eb7). */
31
31
  readonly substituteDominants: readonly string[];
32
32
  /** The minor seventh ii chord that precedes each substitute dominant ("" if none). */
33
33
  readonly substituteDominantSupertonics: readonly string[];
package/dist/index.d.ts CHANGED
@@ -27,7 +27,7 @@ interface KeyScale {
27
27
  readonly secondaryDominants: readonly string[];
28
28
  /** The ii chord that precedes each secondary dominant ("" if none): a minor seventh, or a half-diminished chord when the target is minor. */
29
29
  readonly secondaryDominantSupertonics: readonly string[];
30
- /** Tritone substitution of each secondary dominant ("" if none): the dominant seventh a tritone away. */
30
+ /** Tritone substitution of each secondary dominant ("" if none): the dominant seventh a tritone away, spelled a diminished fifth above it (Gb7 for C7) unless that needs a double flat or sharp (A7, not Bbb7, for Eb7). */
31
31
  readonly substituteDominants: readonly string[];
32
32
  /** The minor seventh ii chord that precedes each substitute dominant ("" if none). */
33
33
  readonly substituteDominantSupertonics: readonly string[];
package/dist/index.js CHANGED
@@ -55,11 +55,7 @@ function keyScale(grades, triads, chordTypes, harmonicFunctions, chordScales) {
55
55
  const chords = mapScaleToType(scale, chordTypes);
56
56
  const secondaryDominants = scale.map((note) => (0, _tonaljs_note.transpose)(note, "5P")).map((note) => scale.includes(note) && !chords.includes(note + "7") ? note + "7" : "");
57
57
  const secondaryDominantSupertonics = supertonics(secondaryDominants, triads);
58
- const substituteDominants = secondaryDominants.map((chord) => {
59
- if (!chord) return "";
60
- const domRoot = chord.slice(0, -1);
61
- return (0, _tonaljs_note.transpose)(domRoot, "5d") + "7";
62
- });
58
+ const substituteDominants = secondaryDominants.map((chord) => chord ? substituteRoot(chord.slice(0, -1)) + "7" : "");
63
59
  const substituteDominantSupertonics = substituteSupertonics(substituteDominants);
64
60
  return {
65
61
  tonic,
@@ -79,6 +75,10 @@ function keyScale(grades, triads, chordTypes, harmonicFunctions, chordScales) {
79
75
  };
80
76
  };
81
77
  }
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
82
  const supertonicRoot = (dominant) => (0, _tonaljs_note.transpose)(dominant.slice(0, -1), "5P");
83
83
  const supertonics = (dominants, targetTriads) => dominants.map((chord, index) => {
84
84
  if (!chord) return "";
@@ -94,7 +94,7 @@ const distInFifths = (from, to) => {
94
94
  };
95
95
  const MajorScale = keyScale("I II III IV V VI VII".split(" "), " m m m dim".split(" "), "maj7 m7 m7 maj7 7 m7 m7b5".split(" "), "T SD T SD D T D".split(" "), "major,dorian,phrygian,lydian,mixolydian,minor,locrian".split(","));
96
96
  const NaturalScale = keyScale("I II bIII IV V bVI bVII".split(" "), "m dim m m ".split(" "), "m7 m7b5 maj7 m7 m7 maj7 7".split(" "), "T SD T SD D SD SD".split(" "), "minor,locrian,major,dorian,phrygian,lydian,mixolydian".split(","));
97
- const HarmonicScale = keyScale("I II bIII IV V bVI VII".split(" "), "m dim aug m dim".split(" "), "mMaj7 m7b5 +maj7 m7 7 maj7 o7".split(" "), "T SD T SD D SD D".split(" "), "harmonic minor,locrian 6,major augmented,lydian diminished,phrygian dominant,lydian #9,ultralocrian".split(","));
97
+ const HarmonicScale = keyScale("I II bIII IV V bVI VII".split(" "), "m dim aug m dim".split(" "), "mMaj7 m7b5 +maj7 m7 7 maj7 o7".split(" "), "T SD T SD D SD D".split(" "), "harmonic minor,locrian 6,major augmented,dorian #4,phrygian dominant,lydian #9,ultralocrian".split(","));
98
98
  const MelodicScale = keyScale("I II bIII IV V VI VII".split(" "), "m m aug dim dim".split(" "), "m6 m7 +maj7 7 7 m7b5 m7b5".split(" "), "T SD T SD D ".split(" "), "melodic minor,dorian b2,lydian augmented,lydian dominant,mixolydian b6,locrian #2,altered".split(","));
99
99
  /**
100
100
  * Get a major key: its signature, scale, triads, seventh chords, harmonic
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["roman","transpose","note","altToAcc","transposeFifths","accToAlt"],"sources":["../index.ts"],"sourcesContent":["import { transpose, transposeFifths } from \"@tonaljs/note\";\nimport { accToAlt, altToAcc, note } from \"@tonaljs/pitch-note\";\nimport { get as roman } from \"@tonaljs/roman-numeral\";\n\nconst Empty: readonly string[] = Object.freeze([] as string[]);\n\nexport interface Key {\n readonly type: \"major\" | \"minor\";\n /** The tonic pitch class, like \"F\". Empty for an invalid key. */\n readonly tonic: string;\n /** Number of accidentals in the key signature: positive for sharps, negative for flats. */\n readonly alteration: number;\n /** The key signature as accidentals: \"\" (none), \"bb\" (two flats), \"###\" (three sharps). */\n readonly keySignature: string;\n}\n\nconst NoKey: Key = {\n type: \"major\",\n tonic: \"\",\n alteration: 0,\n keySignature: \"\",\n};\n\nexport interface KeyScale {\n readonly tonic: string;\n /** Roman numeral of each degree, like [\"I\", \"II\", \"bIII\", ...]. */\n readonly grades: readonly string[];\n /** Interval from the tonic to each degree, like [\"1P\", \"2M\", \"3M\", ...]. */\n readonly intervals: readonly string[];\n /** Notes of the scale, one per degree. */\n readonly scale: readonly string[];\n /** Triad of each degree, like [\"F\", \"Gm\", \"Am\", ...]. */\n readonly triads: readonly string[];\n /** Seventh chord of each degree, like [\"Fmaj7\", \"Gm7\", ...]. */\n readonly chords: readonly string[];\n /** Harmonic function of each chord: \"T\" (tonic), \"SD\" (subdominant) or \"D\" (dominant). Empty string if none. */\n readonly chordsHarmonicFunction: readonly string[];\n /** Scale that fits each chord, like [\"F major\", \"G dorian\", ...]. */\n readonly chordScales: readonly string[];\n /** Dominant seventh chord that resolves to each degree (\"\" if the degree has none), like [\"\", \"D7\", \"E7\", ...] for F major. */\n readonly secondaryDominants: readonly string[];\n /** The ii chord that precedes each secondary dominant (\"\" if none): a minor seventh, or a half-diminished chord when the target is minor. */\n readonly secondaryDominantSupertonics: readonly string[];\n /** Tritone substitution of each secondary dominant (\"\" if none): the dominant seventh a tritone away. */\n readonly substituteDominants: readonly string[];\n /** The minor seventh ii chord that precedes each substitute dominant (\"\" if none). */\n readonly substituteDominantSupertonics: readonly string[];\n\n /** @deprecated use secondaryDominantSupertonics */\n readonly secondaryDominantsMinorRelative: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoKeyScale: KeyScale = {\n tonic: \"\",\n grades: Empty,\n intervals: Empty,\n scale: Empty,\n triads: Empty,\n chords: Empty,\n chordsHarmonicFunction: Empty,\n chordScales: Empty,\n secondaryDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n substituteDominants: Empty,\n substituteDominantSupertonics: Empty,\n secondaryDominantsMinorRelative: Empty,\n};\n\nexport interface MajorKey extends Key, KeyScale {\n readonly type: \"major\";\n /** Tonic of the relative minor key, like \"D\" for \"F\". */\n readonly minorRelative: string;\n readonly scale: readonly string[];\n readonly substituteDominants: readonly string[];\n readonly secondaryDominantSupertonics: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoMajorKey: MajorKey = {\n ...NoKey,\n ...NoKeyScale,\n type: \"major\",\n minorRelative: \"\",\n scale: Empty,\n substituteDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n};\n\nexport interface MinorKey extends Key {\n readonly type: \"minor\";\n /** Tonic of the relative major key, like \"C\" for \"A\". */\n readonly relativeMajor: string;\n /** Natural minor (aeolian) scale and its chords. */\n readonly natural: KeyScale;\n /** Harmonic minor scale and its chords. */\n readonly harmonic: KeyScale;\n /** Melodic minor (ascending) scale and its chords. */\n readonly melodic: KeyScale;\n}\n\nconst NoMinorKey: MinorKey = {\n ...NoKey,\n type: \"minor\",\n relativeMajor: \"\",\n natural: NoKeyScale,\n harmonic: NoKeyScale,\n melodic: NoKeyScale,\n};\n\nexport type KeyChord = {\n /** Chord symbol, like \"Dm7\". */\n name: string;\n /** Functions the chord plays in the key: \"T\", \"SD\", \"D\", or \"V/II\", \"ii/V\", \"subV/II\", \"subii/II\" for secondary and substitute dominants. */\n roles: string[];\n};\n\nconst mapScaleToType = (scale: string[], list: string[], sep = \"\") =>\n list.map((type, i) => `${scale[i]}${sep}${type}`);\n\nfunction keyScale(\n grades: string[],\n triads: string[],\n chordTypes: string[],\n harmonicFunctions: string[],\n chordScales: string[],\n) {\n return (tonic: string): KeyScale => {\n const intervals = grades.map((gr) => roman(gr).interval || \"\");\n const scale = intervals.map((interval) => transpose(tonic, interval));\n const chords = mapScaleToType(scale, chordTypes);\n const secondaryDominants = scale\n .map((note) => transpose(note, \"5P\"))\n .map((note) =>\n // A secondary dominant is a V chord which:\n // 1. is not diatonic to the key,\n // 2. it must have a diatonic root.\n scale.includes(note) && !chords.includes(note + \"7\") ? note + \"7\" : \"\",\n );\n\n const secondaryDominantSupertonics = supertonics(\n secondaryDominants,\n triads,\n );\n const substituteDominants = secondaryDominants.map((chord) => {\n if (!chord) return \"\";\n const domRoot = chord.slice(0, -1);\n const subRoot = transpose(domRoot, \"5d\");\n return subRoot + \"7\";\n });\n const substituteDominantSupertonics =\n substituteSupertonics(substituteDominants);\n\n return {\n tonic,\n grades,\n intervals,\n scale,\n triads: mapScaleToType(scale, triads),\n chords,\n chordsHarmonicFunction: harmonicFunctions.slice(),\n chordScales: mapScaleToType(scale, chordScales, \" \"),\n secondaryDominants,\n secondaryDominantSupertonics,\n substituteDominants,\n substituteDominantSupertonics,\n\n // deprecated aliases\n secondaryDominantsMinorRelative: secondaryDominantSupertonics,\n substituteDominantsMinorRelative: substituteDominantSupertonics,\n };\n };\n}\n\n// The root of the ii chord of a dominant: a fifth above its root\nconst supertonicRoot = (dominant: string) =>\n transpose(dominant.slice(0, -1), \"5P\");\n\n// The ii of a secondary dominant takes its quality from the target: a ii-V\n// into a minor or diminished chord uses a half-diminished ii (Em7b5 A7 Dm),\n// and into a major or augmented chord a minor seventh ii (Gm7 C7 F)\nconst supertonics = (dominants: string[], targetTriads: string[]) =>\n dominants.map((chord, index) => {\n if (!chord) return \"\";\n const target = targetTriads[index];\n const isMinor = target === \"m\" || target === \"dim\";\n return supertonicRoot(chord) + (isMinor ? \"m7b5\" : \"m7\");\n });\n\n// A substitute dominant doesn't resolve to the key it points to (Eb7 resolves\n// to Dm, not Ab), so its ii is always a minor seventh: Bbm7 Eb7 Dm\nconst substituteSupertonics = (dominants: string[]) =>\n dominants.map((chord) => (chord ? supertonicRoot(chord) + \"m7\" : \"\"));\n\nconst distInFifths = (from: string, to: string) => {\n const f = note(from);\n const t = note(to);\n return f.empty || t.empty ? 0 : t.coord[0] - f.coord[0];\n};\n\nconst MajorScale = keyScale(\n \"I II III IV V VI VII\".split(\" \"),\n \" m m m dim\".split(\" \"),\n \"maj7 m7 m7 maj7 7 m7 m7b5\".split(\" \"),\n \"T SD T SD D T D\".split(\" \"),\n \"major,dorian,phrygian,lydian,mixolydian,minor,locrian\".split(\",\"),\n);\nconst NaturalScale = keyScale(\n \"I II bIII IV V bVI bVII\".split(\" \"),\n \"m dim m m \".split(\" \"),\n \"m7 m7b5 maj7 m7 m7 maj7 7\".split(\" \"),\n \"T SD T SD D SD SD\".split(\" \"),\n \"minor,locrian,major,dorian,phrygian,lydian,mixolydian\".split(\",\"),\n);\nconst HarmonicScale = keyScale(\n \"I II bIII IV V bVI VII\".split(\" \"),\n \"m dim aug m dim\".split(\" \"),\n \"mMaj7 m7b5 +maj7 m7 7 maj7 o7\".split(\" \"),\n \"T SD T SD D SD D\".split(\" \"),\n \"harmonic minor,locrian 6,major augmented,lydian diminished,phrygian dominant,lydian #9,ultralocrian\".split(\n \",\",\n ),\n);\nconst MelodicScale = keyScale(\n \"I II bIII IV V VI VII\".split(\" \"),\n \"m m aug dim dim\".split(\" \"),\n \"m6 m7 +maj7 7 7 m7b5 m7b5\".split(\" \"),\n \"T SD T SD D \".split(\" \"),\n \"melodic minor,dorian b2,lydian augmented,lydian dominant,mixolydian b6,locrian #2,altered\".split(\n \",\",\n ),\n);\n\n/**\n * Get a major key: its signature, scale, triads, seventh chords, harmonic\n * functions and secondary and substitute dominants. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tonic - the tonic note or pitch class, like \"F\" or \"Bb\"\n * @example\n * Key.majorKey(\"F\").scale; // => [\"F\", \"G\", \"A\", \"Bb\", \"C\", \"D\", \"E\"]\n * Key.majorKey(\"F\").chords; // => [\"Fmaj7\", \"Gm7\", \"Am7\", \"Bbmaj7\", \"C7\", \"Dm7\", \"Em7b5\"]\n * Key.majorKey(\"F\").keySignature; // => \"b\"\n */\nexport function majorKey(tonic: string): MajorKey {\n const pc = note(tonic).pc;\n if (!pc) return NoMajorKey;\n\n const keyScale = MajorScale(pc);\n const alteration = distInFifths(\"C\", pc);\n\n return {\n ...keyScale,\n type: \"major\",\n minorRelative: transpose(pc, \"-3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n };\n}\n\n/**\n * Get the chords available in a major key, each with the roles it plays:\n * diatonic chords (\"T\", \"SD\", \"D\") and secondary and substitute dominants\n * with their supertonics (\"V/II\", \"ii/V\", \"subV/II\"...). Returns an empty\n * array for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"C\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.majorKeyChords(\"C\").slice(0, 3); // => [{ name: \"Cmaj7\", roles: [\"T\"] }, { name: \"Dm7\", roles: [\"SD\"] }, { name: \"Em7\", roles: [\"T\"] }]\n */\nexport function majorKeyChords(tonic: string): KeyChord[] {\n const key = majorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key, chords);\n return chords;\n}\n\n/**\n * Get the chords available in a minor key, each with the roles it plays. It\n * merges the chords of the natural, harmonic and melodic scales, so a chord\n * that appears in several scales has all its roles. Returns an empty array\n * for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"A\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.minorKeyChords(\"A\").slice(0, 2); // => [{ name: \"Am7\", roles: [\"T\", \"ii/bVII\"] }, { name: \"Bm7b5\", roles: [\"SD\", \"ii/I\"] }]\n */\nexport function minorKeyChords(tonic: string): KeyChord[] {\n const key = minorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key.natural, chords);\n keyChordsOf(key.harmonic, chords);\n keyChordsOf(key.melodic, chords);\n return chords;\n}\n\nfunction keyChordsOf(key: KeyScale, chords: KeyChord[]) {\n const updateChord = (name: string, newRole: string) => {\n if (!name) return;\n let keyChord = chords.find((chord) => chord.name === name);\n if (!keyChord) {\n keyChord = { name, roles: [] };\n chords.push(keyChord);\n }\n if (newRole && !keyChord.roles.includes(newRole)) {\n keyChord.roles.push(newRole);\n }\n };\n\n key.chords.forEach((chordName, index) =>\n updateChord(chordName, key.chordsHarmonicFunction[index]),\n );\n key.secondaryDominants.forEach((chordName, index) =>\n updateChord(chordName, `V/${key.grades[index]}`),\n );\n key.secondaryDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `ii/${key.grades[index]}`),\n );\n key.substituteDominants.forEach((chordName, index) =>\n updateChord(chordName, `subV/${key.grades[index]}`),\n );\n key.substituteDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `subii/${key.grades[index]}`),\n );\n}\n\n/**\n * Get a minor key: its signature, relative major and the scale, chords and\n * dominants of its natural, harmonic and melodic forms. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tnc - the tonic note or pitch class, like \"A\"\n * @example\n * Key.minorKey(\"A\").relativeMajor; // => \"C\"\n * Key.minorKey(\"A\").harmonic.scale; // => [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G#\"]\n * Key.minorKey(\"A\").natural.chords; // => [\"Am7\", \"Bm7b5\", \"Cmaj7\", \"Dm7\", \"Em7\", \"Fmaj7\", \"G7\"]\n */\nexport function minorKey(tnc: string): MinorKey {\n const pc = note(tnc).pc;\n if (!pc) return NoMinorKey;\n\n const alteration = distInFifths(\"C\", pc) - 3;\n return {\n type: \"minor\",\n tonic: pc,\n relativeMajor: transpose(pc, \"3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n natural: NaturalScale(pc),\n harmonic: HarmonicScale(pc),\n melodic: MelodicScale(pc),\n };\n}\n\n/**\n * Get the tonic of the major key with the given key signature, or null if\n * the signature is not valid.\n *\n * @param sig - accidentals (\"\", \"bb\", \"###\") or a number of fifths (positive for sharps, negative for flats)\n * @example\n * Key.majorTonicFromKeySignature(\"###\"); // => \"A\"\n * Key.majorTonicFromKeySignature(-2); // => \"Bb\"\n * Key.majorTonicFromKeySignature(\"\"); // => \"C\"\n */\nexport function majorTonicFromKeySignature(\n sig: string | number,\n): string | null {\n if (typeof sig === \"number\") {\n return transposeFifths(\"C\", sig);\n } else if (typeof sig === \"string\" && /^(b*|#*)$/.test(sig)) {\n return transposeFifths(\"C\", accToAlt(sig));\n }\n return null;\n}\n\n/** @deprecated Use the named exports instead. */\nexport default { majorKey, majorTonicFromKeySignature, minorKey };\n"],"mappings":";;;;;;;;AAIA,MAAM,QAA2B,OAAO,OAAO,CAAC,CAAa;AAY7D,MAAM,QAAa;CACjB,MAAM;CACN,OAAO;CACP,YAAY;CACZ,cAAc;AAChB;AAiCA,MAAM,aAAuB;CAC3B,OAAO;CACP,QAAQ;CACR,WAAW;CACX,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,wBAAwB;CACxB,aAAa;CACb,oBAAoB;CACpB,8BAA8B;CAC9B,kCAAkC;CAClC,qBAAqB;CACrB,+BAA+B;CAC/B,iCAAiC;AACnC;AAaA,MAAM,aAAuB;CAC3B,GAAG;CACH,GAAG;CACH,MAAM;CACN,eAAe;CACf,OAAO;CACP,qBAAqB;CACrB,8BAA8B;CAC9B,kCAAkC;AACpC;AAcA,MAAM,aAAuB;CAC3B,GAAG;CACH,MA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AAO,QAAQ,CACrD;AACF;;;;;;;;;;;;AAaA,SAAgB,SAAS,KAAuB;CAC9C,MAAM,MAAA,GAAKD,oBAAAA,KAAAA,CAAK,GAAG,CAAC,CAAC;CACrB,IAAI,CAAC,IAAI,OAAO;CAEhB,MAAM,aAAa,aAAa,KAAK,EAAE,IAAI;CAC3C,OAAO;EACL,MAAM;EACN,OAAO;EACP,gBAAA,GAAeD,cAAAA,UAAAA,CAAU,IAAI,IAAI;EACjC;EACA,eAAA,GAAcE,oBAAAA,SAAAA,CAAS,UAAU;EACjC,SAAS,aAAa,EAAE;EACxB,UAAU,cAAc,EAAE;EAC1B,SAAS,aAAa,EAAE;CAC1B;AACF;;;;;;;;;;;AAYA,SAAgB,2BACd,KACe;CACf,IAAI,OAAO,QAAQ,UACjB,QAAA,GAAOC,cAAAA,gBAAAA,CAAgB,KAAK,GAAG;MAC1B,IAAI,OAAO,QAAQ,YAAY,YAAY,KAAK,GAAG,GACxD,QAAA,GAAOA,cAAAA,gBAAAA,CAAgB,MAAA,GAAKC,oBAAAA,SAAAA,CAAS,GAAG,CAAC;CAE3C,OAAO;AACT;;AAGA,IAAA,cAAe;CAAE;CAAU;CAA4B;AAAS"}
1
+ {"version":3,"file":"index.js","names":["roman","transpose","note","simplify","altToAcc","transposeFifths","accToAlt"],"sources":["../index.ts"],"sourcesContent":["import { simplify, transpose, transposeFifths } from \"@tonaljs/note\";\nimport { accToAlt, altToAcc, note } from \"@tonaljs/pitch-note\";\nimport { get as roman } from \"@tonaljs/roman-numeral\";\n\nconst Empty: readonly string[] = Object.freeze([] as string[]);\n\nexport interface Key {\n readonly type: \"major\" | \"minor\";\n /** The tonic pitch class, like \"F\". Empty for an invalid key. */\n readonly tonic: string;\n /** Number of accidentals in the key signature: positive for sharps, negative for flats. */\n readonly alteration: number;\n /** The key signature as accidentals: \"\" (none), \"bb\" (two flats), \"###\" (three sharps). */\n readonly keySignature: string;\n}\n\nconst NoKey: Key = {\n type: \"major\",\n tonic: \"\",\n alteration: 0,\n keySignature: \"\",\n};\n\nexport interface KeyScale {\n readonly tonic: string;\n /** Roman numeral of each degree, like [\"I\", \"II\", \"bIII\", ...]. */\n readonly grades: readonly string[];\n /** Interval from the tonic to each degree, like [\"1P\", \"2M\", \"3M\", ...]. */\n readonly intervals: readonly string[];\n /** Notes of the scale, one per degree. */\n readonly scale: readonly string[];\n /** Triad of each degree, like [\"F\", \"Gm\", \"Am\", ...]. */\n readonly triads: readonly string[];\n /** Seventh chord of each degree, like [\"Fmaj7\", \"Gm7\", ...]. */\n readonly chords: readonly string[];\n /** Harmonic function of each chord: \"T\" (tonic), \"SD\" (subdominant) or \"D\" (dominant). Empty string if none. */\n readonly chordsHarmonicFunction: readonly string[];\n /** Scale that fits each chord, like [\"F major\", \"G dorian\", ...]. */\n readonly chordScales: readonly string[];\n /** Dominant seventh chord that resolves to each degree (\"\" if the degree has none), like [\"\", \"D7\", \"E7\", ...] for F major. */\n readonly secondaryDominants: readonly string[];\n /** The ii chord that precedes each secondary dominant (\"\" if none): a minor seventh, or a half-diminished chord when the target is minor. */\n readonly secondaryDominantSupertonics: readonly string[];\n /** Tritone substitution of each secondary dominant (\"\" if none): the dominant seventh a tritone away, spelled a diminished fifth above it (Gb7 for C7) unless that needs a double flat or sharp (A7, not Bbb7, for Eb7). */\n readonly substituteDominants: readonly string[];\n /** The minor seventh ii chord that precedes each substitute dominant (\"\" if none). */\n readonly substituteDominantSupertonics: readonly string[];\n\n /** @deprecated use secondaryDominantSupertonics */\n readonly secondaryDominantsMinorRelative: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoKeyScale: KeyScale = {\n tonic: \"\",\n grades: Empty,\n intervals: Empty,\n scale: Empty,\n triads: Empty,\n chords: Empty,\n chordsHarmonicFunction: Empty,\n chordScales: Empty,\n secondaryDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n substituteDominants: Empty,\n substituteDominantSupertonics: Empty,\n secondaryDominantsMinorRelative: Empty,\n};\n\nexport interface MajorKey extends Key, KeyScale {\n readonly type: \"major\";\n /** Tonic of the relative minor key, like \"D\" for \"F\". */\n readonly minorRelative: string;\n readonly scale: readonly string[];\n readonly substituteDominants: readonly string[];\n readonly secondaryDominantSupertonics: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoMajorKey: MajorKey = {\n ...NoKey,\n ...NoKeyScale,\n type: \"major\",\n minorRelative: \"\",\n scale: Empty,\n substituteDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n};\n\nexport interface MinorKey extends Key {\n readonly type: \"minor\";\n /** Tonic of the relative major key, like \"C\" for \"A\". */\n readonly relativeMajor: string;\n /** Natural minor (aeolian) scale and its chords. */\n readonly natural: KeyScale;\n /** Harmonic minor scale and its chords. */\n readonly harmonic: KeyScale;\n /** Melodic minor (ascending) scale and its chords. */\n readonly melodic: KeyScale;\n}\n\nconst NoMinorKey: MinorKey = {\n ...NoKey,\n type: \"minor\",\n relativeMajor: \"\",\n natural: NoKeyScale,\n harmonic: NoKeyScale,\n melodic: NoKeyScale,\n};\n\nexport type KeyChord = {\n /** Chord symbol, like \"Dm7\". */\n name: string;\n /** Functions the chord plays in the key: \"T\", \"SD\", \"D\", or \"V/II\", \"ii/V\", \"subV/II\", \"subii/II\" for secondary and substitute dominants. */\n roles: string[];\n};\n\nconst mapScaleToType = (scale: string[], list: string[], sep = \"\") =>\n list.map((type, i) => `${scale[i]}${sep}${type}`);\n\nfunction keyScale(\n grades: string[],\n triads: string[],\n chordTypes: string[],\n harmonicFunctions: string[],\n chordScales: string[],\n) {\n return (tonic: string): KeyScale => {\n const intervals = grades.map((gr) => roman(gr).interval || \"\");\n const scale = intervals.map((interval) => transpose(tonic, interval));\n const chords = mapScaleToType(scale, chordTypes);\n const secondaryDominants = scale\n .map((note) => transpose(note, \"5P\"))\n .map((note) =>\n // A secondary dominant is a V chord which:\n // 1. is not diatonic to the key,\n // 2. it must have a diatonic root.\n scale.includes(note) && !chords.includes(note + \"7\") ? note + \"7\" : \"\",\n );\n\n const secondaryDominantSupertonics = supertonics(\n secondaryDominants,\n triads,\n );\n const substituteDominants = secondaryDominants.map((chord) =>\n chord ? substituteRoot(chord.slice(0, -1)) + \"7\" : \"\",\n );\n const substituteDominantSupertonics =\n substituteSupertonics(substituteDominants);\n\n return {\n tonic,\n grades,\n intervals,\n scale,\n triads: mapScaleToType(scale, triads),\n chords,\n chordsHarmonicFunction: harmonicFunctions.slice(),\n chordScales: mapScaleToType(scale, chordScales, \" \"),\n secondaryDominants,\n secondaryDominantSupertonics,\n substituteDominants,\n substituteDominantSupertonics,\n\n // deprecated aliases\n secondaryDominantsMinorRelative: secondaryDominantSupertonics,\n substituteDominantsMinorRelative: substituteDominantSupertonics,\n };\n };\n}\n\n// The root of a substitute dominant: a diminished fifth above the dominant's\n// root (Gb for C), or its simplest enharmonic when that has a double\n// accidental, as a lead sheet writes it (A, not Bbb, for Eb)\nfunction substituteRoot(dominantRoot: string) {\n const root = transpose(dominantRoot, \"5d\");\n return note(root).acc.length > 1 ? simplify(root) : root;\n}\n\n// The root of the ii chord of a dominant: a fifth above its root\nconst supertonicRoot = (dominant: string) =>\n transpose(dominant.slice(0, -1), \"5P\");\n\n// The ii of a secondary dominant takes its quality from the target: a ii-V\n// into a minor or diminished chord uses a half-diminished ii (Em7b5 A7 Dm),\n// and into a major or augmented chord a minor seventh ii (Gm7 C7 F)\nconst supertonics = (dominants: string[], targetTriads: string[]) =>\n dominants.map((chord, index) => {\n if (!chord) return \"\";\n const target = targetTriads[index];\n const isMinor = target === \"m\" || target === \"dim\";\n return supertonicRoot(chord) + (isMinor ? \"m7b5\" : \"m7\");\n });\n\n// A substitute dominant doesn't resolve to the key it points to (Eb7 resolves\n// to Dm, not Ab), so its ii is always a minor seventh: Bbm7 Eb7 Dm\nconst substituteSupertonics = (dominants: string[]) =>\n dominants.map((chord) => (chord ? supertonicRoot(chord) + \"m7\" : \"\"));\n\nconst distInFifths = (from: string, to: string) => {\n const f = note(from);\n const t = note(to);\n return f.empty || t.empty ? 0 : t.coord[0] - f.coord[0];\n};\n\nconst MajorScale = keyScale(\n \"I II III IV V VI VII\".split(\" \"),\n \" m m m dim\".split(\" \"),\n \"maj7 m7 m7 maj7 7 m7 m7b5\".split(\" \"),\n \"T SD T SD D T D\".split(\" \"),\n \"major,dorian,phrygian,lydian,mixolydian,minor,locrian\".split(\",\"),\n);\nconst NaturalScale = keyScale(\n \"I II bIII IV V bVI bVII\".split(\" \"),\n \"m dim m m \".split(\" \"),\n \"m7 m7b5 maj7 m7 m7 maj7 7\".split(\" \"),\n \"T SD T SD D SD SD\".split(\" \"),\n \"minor,locrian,major,dorian,phrygian,lydian,mixolydian\".split(\",\"),\n);\nconst HarmonicScale = keyScale(\n \"I II bIII IV V bVI VII\".split(\" \"),\n \"m dim aug m dim\".split(\" \"),\n \"mMaj7 m7b5 +maj7 m7 7 maj7 o7\".split(\" \"),\n \"T SD T SD D SD D\".split(\" \"),\n \"harmonic minor,locrian 6,major augmented,dorian #4,phrygian dominant,lydian #9,ultralocrian\".split(\n \",\",\n ),\n);\nconst MelodicScale = keyScale(\n \"I II bIII IV V VI VII\".split(\" \"),\n \"m m aug dim dim\".split(\" \"),\n \"m6 m7 +maj7 7 7 m7b5 m7b5\".split(\" \"),\n \"T SD T SD D \".split(\" \"),\n \"melodic minor,dorian b2,lydian augmented,lydian dominant,mixolydian b6,locrian #2,altered\".split(\n \",\",\n ),\n);\n\n/**\n * Get a major key: its signature, scale, triads, seventh chords, harmonic\n * functions and secondary and substitute dominants. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tonic - the tonic note or pitch class, like \"F\" or \"Bb\"\n * @example\n * Key.majorKey(\"F\").scale; // => [\"F\", \"G\", \"A\", \"Bb\", \"C\", \"D\", \"E\"]\n * Key.majorKey(\"F\").chords; // => [\"Fmaj7\", \"Gm7\", \"Am7\", \"Bbmaj7\", \"C7\", \"Dm7\", \"Em7b5\"]\n * Key.majorKey(\"F\").keySignature; // => \"b\"\n */\nexport function majorKey(tonic: string): MajorKey {\n const pc = note(tonic).pc;\n if (!pc) return NoMajorKey;\n\n const keyScale = MajorScale(pc);\n const alteration = distInFifths(\"C\", pc);\n\n return {\n ...keyScale,\n type: \"major\",\n minorRelative: transpose(pc, \"-3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n };\n}\n\n/**\n * Get the chords available in a major key, each with the roles it plays:\n * diatonic chords (\"T\", \"SD\", \"D\") and secondary and substitute dominants\n * with their supertonics (\"V/II\", \"ii/V\", \"subV/II\"...). Returns an empty\n * array for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"C\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.majorKeyChords(\"C\").slice(0, 3); // => [{ name: \"Cmaj7\", roles: [\"T\"] }, { name: \"Dm7\", roles: [\"SD\"] }, { name: \"Em7\", roles: [\"T\"] }]\n */\nexport function majorKeyChords(tonic: string): KeyChord[] {\n const key = majorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key, chords);\n return chords;\n}\n\n/**\n * Get the chords available in a minor key, each with the roles it plays. It\n * merges the chords of the natural, harmonic and melodic scales, so a chord\n * that appears in several scales has all its roles. Returns an empty array\n * for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"A\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.minorKeyChords(\"A\").slice(0, 2); // => [{ name: \"Am7\", roles: [\"T\", \"ii/bVII\"] }, { name: \"Bm7b5\", roles: [\"SD\", \"ii/I\"] }]\n */\nexport function minorKeyChords(tonic: string): KeyChord[] {\n const key = minorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key.natural, chords);\n keyChordsOf(key.harmonic, chords);\n keyChordsOf(key.melodic, chords);\n return chords;\n}\n\nfunction keyChordsOf(key: KeyScale, chords: KeyChord[]) {\n const updateChord = (name: string, newRole: string) => {\n if (!name) return;\n let keyChord = chords.find((chord) => chord.name === name);\n if (!keyChord) {\n keyChord = { name, roles: [] };\n chords.push(keyChord);\n }\n if (newRole && !keyChord.roles.includes(newRole)) {\n keyChord.roles.push(newRole);\n }\n };\n\n key.chords.forEach((chordName, index) =>\n updateChord(chordName, key.chordsHarmonicFunction[index]),\n );\n key.secondaryDominants.forEach((chordName, index) =>\n updateChord(chordName, `V/${key.grades[index]}`),\n );\n key.secondaryDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `ii/${key.grades[index]}`),\n );\n key.substituteDominants.forEach((chordName, index) =>\n updateChord(chordName, `subV/${key.grades[index]}`),\n );\n key.substituteDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `subii/${key.grades[index]}`),\n );\n}\n\n/**\n * Get a minor key: its signature, relative major and the scale, chords and\n * dominants of its natural, harmonic and melodic forms. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tnc - the tonic note or pitch class, like \"A\"\n * @example\n * Key.minorKey(\"A\").relativeMajor; // => \"C\"\n * Key.minorKey(\"A\").harmonic.scale; // => [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G#\"]\n * Key.minorKey(\"A\").natural.chords; // => [\"Am7\", \"Bm7b5\", \"Cmaj7\", \"Dm7\", \"Em7\", \"Fmaj7\", \"G7\"]\n */\nexport function minorKey(tnc: string): MinorKey {\n const pc = note(tnc).pc;\n if (!pc) return NoMinorKey;\n\n const alteration = distInFifths(\"C\", pc) - 3;\n return {\n type: \"minor\",\n tonic: pc,\n relativeMajor: transpose(pc, \"3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n natural: NaturalScale(pc),\n harmonic: HarmonicScale(pc),\n melodic: MelodicScale(pc),\n };\n}\n\n/**\n * Get the tonic of the major key with the given key signature, or null if\n * the signature is not valid.\n *\n * @param sig - accidentals (\"\", \"bb\", \"###\") or a number of fifths (positive for sharps, negative for flats)\n * @example\n * Key.majorTonicFromKeySignature(\"###\"); // => \"A\"\n * Key.majorTonicFromKeySignature(-2); // => \"Bb\"\n * Key.majorTonicFromKeySignature(\"\"); // => \"C\"\n */\nexport function majorTonicFromKeySignature(\n sig: string | number,\n): string | null {\n if (typeof sig === \"number\") {\n return transposeFifths(\"C\", sig);\n } else if (typeof sig === \"string\" && /^(b*|#*)$/.test(sig)) {\n return transposeFifths(\"C\", accToAlt(sig));\n }\n return null;\n}\n\n/** @deprecated Use the named exports instead. */\nexport default { majorKey, majorTonicFromKeySignature, minorKey };\n"],"mappings":";;;;;;;;AAIA,MAAM,QAA2B,OAAO,OAAO,CAAC,CAAa;AAY7D,MAAM,QAAa;CACjB,MAAM;CACN,OAAO;CACP,YAAY;CACZ,cAAc;AAChB;AAiCA,MAAM,aAAuB;CAC3B,OAAO;CACP,QAAQ;CACR,WAAW;CACX,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,wBAAwB;CACxB,aAAa;CACb,oBAAoB;CACpB,8BAA8B;CAC9B,kCAAkC;CAClC,qBAAqB;CACrB,+BAA+B;CAC/B,iCAAiC;AACnC;AAaA,MAAM,aAAuB;CAC3B,GAAG;CACH,GAAG;CACH,MAAM;CACN,eAAe;CACf,OAAO;CACP,qBAAqB;CACrB,8BAA8B;CAC9B,kCAAkC;AACpC;AAcA,MAAM,aAAuB;CAC3B,GAAG;CACH,MAAM;CACN,eAAe;CACf,SAAS;CACT,UAAU;CACV,SAAS;AACX;AASA,MAAM,kBAAkB,OAAiB,MAAgB,MAAM,OAC7D,KAAK,KAAK,MAAM,MAAM,GAAG,MAAM,KAAK,MAAM,MAAM;AAElD,SAAS,SACP,QACA,QACA,YACA,mBACA,aACA;CACA,QAAQ,UAA4B;EAClC,MAAM,YAAY,OAAO,KAAK,QAAA,GAAOA,uBAAAA,IAAAA,CAAM,EAAE,CAAC,CAAC,YAAY,EAAE;EAC7D,MAAM,QAAQ,UAAU,KAAK,cAAA,GAAaC,cAAAA,UAAAA,CAAU,OAAO,QAA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AA,UAAAA,CAAU,IAAI,IAAI;EACjC;EACA,eAAA,GAAcG,oBAAAA,SAAAA,CAAS,UAAU;EACjC,SAAS,aAAa,EAAE;EACxB,UAAU,cAAc,EAAE;EAC1B,SAAS,aAAa,EAAE;CAC1B;AACF;;;;;;;;;;;AAYA,SAAgB,2BACd,KACe;CACf,IAAI,OAAO,QAAQ,UACjB,QAAA,GAAOC,cAAAA,gBAAAA,CAAgB,KAAK,GAAG;MAC1B,IAAI,OAAO,QAAQ,YAAY,YAAY,KAAK,GAAG,GACxD,QAAA,GAAOA,cAAAA,gBAAAA,CAAgB,MAAA,GAAKC,oBAAAA,SAAAA,CAAS,GAAG,CAAC;CAE3C,OAAO;AACT;;AAGA,IAAA,cAAe;CAAE;CAAU;CAA4B;AAAS"}
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { transpose, transposeFifths } from "@tonaljs/note";
1
+ import { simplify, transpose, transposeFifths } from "@tonaljs/note";
2
2
  import { accToAlt, altToAcc, note } from "@tonaljs/pitch-note";
3
3
  import { get } from "@tonaljs/roman-numeral";
4
4
  // index.ts
@@ -51,11 +51,7 @@ function keyScale(grades, triads, chordTypes, harmonicFunctions, chordScales) {
51
51
  const chords = mapScaleToType(scale, chordTypes);
52
52
  const secondaryDominants = scale.map((note) => transpose(note, "5P")).map((note) => scale.includes(note) && !chords.includes(note + "7") ? note + "7" : "");
53
53
  const secondaryDominantSupertonics = supertonics(secondaryDominants, triads);
54
- const substituteDominants = secondaryDominants.map((chord) => {
55
- if (!chord) return "";
56
- const domRoot = chord.slice(0, -1);
57
- return transpose(domRoot, "5d") + "7";
58
- });
54
+ const substituteDominants = secondaryDominants.map((chord) => chord ? substituteRoot(chord.slice(0, -1)) + "7" : "");
59
55
  const substituteDominantSupertonics = substituteSupertonics(substituteDominants);
60
56
  return {
61
57
  tonic,
@@ -75,6 +71,10 @@ function keyScale(grades, triads, chordTypes, harmonicFunctions, chordScales) {
75
71
  };
76
72
  };
77
73
  }
74
+ function substituteRoot(dominantRoot) {
75
+ const root = transpose(dominantRoot, "5d");
76
+ return note(root).acc.length > 1 ? simplify(root) : root;
77
+ }
78
78
  const supertonicRoot = (dominant) => transpose(dominant.slice(0, -1), "5P");
79
79
  const supertonics = (dominants, targetTriads) => dominants.map((chord, index) => {
80
80
  if (!chord) return "";
@@ -90,7 +90,7 @@ const distInFifths = (from, to) => {
90
90
  };
91
91
  const MajorScale = keyScale("I II III IV V VI VII".split(" "), " m m m dim".split(" "), "maj7 m7 m7 maj7 7 m7 m7b5".split(" "), "T SD T SD D T D".split(" "), "major,dorian,phrygian,lydian,mixolydian,minor,locrian".split(","));
92
92
  const NaturalScale = keyScale("I II bIII IV V bVI bVII".split(" "), "m dim m m ".split(" "), "m7 m7b5 maj7 m7 m7 maj7 7".split(" "), "T SD T SD D SD SD".split(" "), "minor,locrian,major,dorian,phrygian,lydian,mixolydian".split(","));
93
- const HarmonicScale = keyScale("I II bIII IV V bVI VII".split(" "), "m dim aug m dim".split(" "), "mMaj7 m7b5 +maj7 m7 7 maj7 o7".split(" "), "T SD T SD D SD D".split(" "), "harmonic minor,locrian 6,major augmented,lydian diminished,phrygian dominant,lydian #9,ultralocrian".split(","));
93
+ const HarmonicScale = keyScale("I II bIII IV V bVI VII".split(" "), "m dim aug m dim".split(" "), "mMaj7 m7b5 +maj7 m7 7 maj7 o7".split(" "), "T SD T SD D SD D".split(" "), "harmonic minor,locrian 6,major augmented,dorian #4,phrygian dominant,lydian #9,ultralocrian".split(","));
94
94
  const MelodicScale = keyScale("I II bIII IV V VI VII".split(" "), "m m aug dim dim".split(" "), "m6 m7 +maj7 7 7 m7b5 m7b5".split(" "), "T SD T SD D ".split(" "), "melodic minor,dorian b2,lydian augmented,lydian dominant,mixolydian b6,locrian #2,altered".split(","));
95
95
  /**
96
96
  * Get a major key: its signature, scale, triads, seventh chords, harmonic
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["roman"],"sources":["../index.ts"],"sourcesContent":["import { transpose, transposeFifths } from \"@tonaljs/note\";\nimport { accToAlt, altToAcc, note } from \"@tonaljs/pitch-note\";\nimport { get as roman } from \"@tonaljs/roman-numeral\";\n\nconst Empty: readonly string[] = Object.freeze([] as string[]);\n\nexport interface Key {\n readonly type: \"major\" | \"minor\";\n /** The tonic pitch class, like \"F\". Empty for an invalid key. */\n readonly tonic: string;\n /** Number of accidentals in the key signature: positive for sharps, negative for flats. */\n readonly alteration: number;\n /** The key signature as accidentals: \"\" (none), \"bb\" (two flats), \"###\" (three sharps). */\n readonly keySignature: string;\n}\n\nconst NoKey: Key = {\n type: \"major\",\n tonic: \"\",\n alteration: 0,\n keySignature: \"\",\n};\n\nexport interface KeyScale {\n readonly tonic: string;\n /** Roman numeral of each degree, like [\"I\", \"II\", \"bIII\", ...]. */\n readonly grades: readonly string[];\n /** Interval from the tonic to each degree, like [\"1P\", \"2M\", \"3M\", ...]. */\n readonly intervals: readonly string[];\n /** Notes of the scale, one per degree. */\n readonly scale: readonly string[];\n /** Triad of each degree, like [\"F\", \"Gm\", \"Am\", ...]. */\n readonly triads: readonly string[];\n /** Seventh chord of each degree, like [\"Fmaj7\", \"Gm7\", ...]. */\n readonly chords: readonly string[];\n /** Harmonic function of each chord: \"T\" (tonic), \"SD\" (subdominant) or \"D\" (dominant). Empty string if none. */\n readonly chordsHarmonicFunction: readonly string[];\n /** Scale that fits each chord, like [\"F major\", \"G dorian\", ...]. */\n readonly chordScales: readonly string[];\n /** Dominant seventh chord that resolves to each degree (\"\" if the degree has none), like [\"\", \"D7\", \"E7\", ...] for F major. */\n readonly secondaryDominants: readonly string[];\n /** The ii chord that precedes each secondary dominant (\"\" if none): a minor seventh, or a half-diminished chord when the target is minor. */\n readonly secondaryDominantSupertonics: readonly string[];\n /** Tritone substitution of each secondary dominant (\"\" if none): the dominant seventh a tritone away. */\n readonly substituteDominants: readonly string[];\n /** The minor seventh ii chord that precedes each substitute dominant (\"\" if none). */\n readonly substituteDominantSupertonics: readonly string[];\n\n /** @deprecated use secondaryDominantSupertonics */\n readonly secondaryDominantsMinorRelative: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoKeyScale: KeyScale = {\n tonic: \"\",\n grades: Empty,\n intervals: Empty,\n scale: Empty,\n triads: Empty,\n chords: Empty,\n chordsHarmonicFunction: Empty,\n chordScales: Empty,\n secondaryDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n substituteDominants: Empty,\n substituteDominantSupertonics: Empty,\n secondaryDominantsMinorRelative: Empty,\n};\n\nexport interface MajorKey extends Key, KeyScale {\n readonly type: \"major\";\n /** Tonic of the relative minor key, like \"D\" for \"F\". */\n readonly minorRelative: string;\n readonly scale: readonly string[];\n readonly substituteDominants: readonly string[];\n readonly secondaryDominantSupertonics: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoMajorKey: MajorKey = {\n ...NoKey,\n ...NoKeyScale,\n type: \"major\",\n minorRelative: \"\",\n scale: Empty,\n substituteDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n};\n\nexport interface MinorKey extends Key {\n readonly type: \"minor\";\n /** Tonic of the relative major key, like \"C\" for \"A\". */\n readonly relativeMajor: string;\n /** Natural minor (aeolian) scale and its chords. */\n readonly natural: KeyScale;\n /** Harmonic minor scale and its chords. */\n readonly harmonic: KeyScale;\n /** Melodic minor (ascending) scale and its chords. */\n readonly melodic: KeyScale;\n}\n\nconst NoMinorKey: MinorKey = {\n ...NoKey,\n type: \"minor\",\n relativeMajor: \"\",\n natural: NoKeyScale,\n harmonic: NoKeyScale,\n melodic: NoKeyScale,\n};\n\nexport type KeyChord = {\n /** Chord symbol, like \"Dm7\". */\n name: string;\n /** Functions the chord plays in the key: \"T\", \"SD\", \"D\", or \"V/II\", \"ii/V\", \"subV/II\", \"subii/II\" for secondary and substitute dominants. */\n roles: string[];\n};\n\nconst mapScaleToType = (scale: string[], list: string[], sep = \"\") =>\n list.map((type, i) => `${scale[i]}${sep}${type}`);\n\nfunction keyScale(\n grades: string[],\n triads: string[],\n chordTypes: string[],\n harmonicFunctions: string[],\n chordScales: string[],\n) {\n return (tonic: string): KeyScale => {\n const intervals = grades.map((gr) => roman(gr).interval || \"\");\n const scale = intervals.map((interval) => transpose(tonic, interval));\n const chords = mapScaleToType(scale, chordTypes);\n const secondaryDominants = scale\n .map((note) => transpose(note, \"5P\"))\n .map((note) =>\n // A secondary dominant is a V chord which:\n // 1. is not diatonic to the key,\n // 2. it must have a diatonic root.\n scale.includes(note) && !chords.includes(note + \"7\") ? note + \"7\" : \"\",\n );\n\n const secondaryDominantSupertonics = supertonics(\n secondaryDominants,\n triads,\n );\n const substituteDominants = secondaryDominants.map((chord) => {\n if (!chord) return \"\";\n const domRoot = chord.slice(0, -1);\n const subRoot = transpose(domRoot, \"5d\");\n return subRoot + \"7\";\n });\n const substituteDominantSupertonics =\n substituteSupertonics(substituteDominants);\n\n return {\n tonic,\n grades,\n intervals,\n scale,\n triads: mapScaleToType(scale, triads),\n chords,\n chordsHarmonicFunction: harmonicFunctions.slice(),\n chordScales: mapScaleToType(scale, chordScales, \" \"),\n secondaryDominants,\n secondaryDominantSupertonics,\n substituteDominants,\n substituteDominantSupertonics,\n\n // deprecated aliases\n secondaryDominantsMinorRelative: secondaryDominantSupertonics,\n substituteDominantsMinorRelative: substituteDominantSupertonics,\n };\n };\n}\n\n// The root of the ii chord of a dominant: a fifth above its root\nconst supertonicRoot = (dominant: string) =>\n transpose(dominant.slice(0, -1), \"5P\");\n\n// The ii of a secondary dominant takes its quality from the target: a ii-V\n// into a minor or diminished chord uses a half-diminished ii (Em7b5 A7 Dm),\n// and into a major or augmented chord a minor seventh ii (Gm7 C7 F)\nconst supertonics = (dominants: string[], targetTriads: string[]) =>\n dominants.map((chord, index) => {\n if (!chord) return \"\";\n const target = targetTriads[index];\n const isMinor = target === \"m\" || target === \"dim\";\n return supertonicRoot(chord) + (isMinor ? \"m7b5\" : \"m7\");\n });\n\n// A substitute dominant doesn't resolve to the key it points to (Eb7 resolves\n// to Dm, not Ab), so its ii is always a minor seventh: Bbm7 Eb7 Dm\nconst substituteSupertonics = (dominants: string[]) =>\n dominants.map((chord) => (chord ? supertonicRoot(chord) + \"m7\" : \"\"));\n\nconst distInFifths = (from: string, to: string) => {\n const f = note(from);\n const t = note(to);\n return f.empty || t.empty ? 0 : t.coord[0] - f.coord[0];\n};\n\nconst MajorScale = keyScale(\n \"I II III IV V VI VII\".split(\" \"),\n \" m m m dim\".split(\" \"),\n \"maj7 m7 m7 maj7 7 m7 m7b5\".split(\" \"),\n \"T SD T SD D T D\".split(\" \"),\n \"major,dorian,phrygian,lydian,mixolydian,minor,locrian\".split(\",\"),\n);\nconst NaturalScale = keyScale(\n \"I II bIII IV V bVI bVII\".split(\" \"),\n \"m dim m m \".split(\" \"),\n \"m7 m7b5 maj7 m7 m7 maj7 7\".split(\" \"),\n \"T SD T SD D SD SD\".split(\" \"),\n \"minor,locrian,major,dorian,phrygian,lydian,mixolydian\".split(\",\"),\n);\nconst HarmonicScale = keyScale(\n \"I II bIII IV V bVI VII\".split(\" \"),\n \"m dim aug m dim\".split(\" \"),\n \"mMaj7 m7b5 +maj7 m7 7 maj7 o7\".split(\" \"),\n \"T SD T SD D SD D\".split(\" \"),\n \"harmonic minor,locrian 6,major augmented,lydian diminished,phrygian dominant,lydian #9,ultralocrian\".split(\n \",\",\n ),\n);\nconst MelodicScale = keyScale(\n \"I II bIII IV V VI VII\".split(\" \"),\n \"m m aug dim dim\".split(\" \"),\n \"m6 m7 +maj7 7 7 m7b5 m7b5\".split(\" \"),\n \"T SD T SD D \".split(\" \"),\n \"melodic minor,dorian b2,lydian augmented,lydian dominant,mixolydian b6,locrian #2,altered\".split(\n \",\",\n ),\n);\n\n/**\n * Get a major key: its signature, scale, triads, seventh chords, harmonic\n * functions and secondary and substitute dominants. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tonic - the tonic note or pitch class, like \"F\" or \"Bb\"\n * @example\n * Key.majorKey(\"F\").scale; // => [\"F\", \"G\", \"A\", \"Bb\", \"C\", \"D\", \"E\"]\n * Key.majorKey(\"F\").chords; // => [\"Fmaj7\", \"Gm7\", \"Am7\", \"Bbmaj7\", \"C7\", \"Dm7\", \"Em7b5\"]\n * Key.majorKey(\"F\").keySignature; // => \"b\"\n */\nexport function majorKey(tonic: string): MajorKey {\n const pc = note(tonic).pc;\n if (!pc) return NoMajorKey;\n\n const keyScale = MajorScale(pc);\n const alteration = distInFifths(\"C\", pc);\n\n return {\n ...keyScale,\n type: \"major\",\n minorRelative: transpose(pc, \"-3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n };\n}\n\n/**\n * Get the chords available in a major key, each with the roles it plays:\n * diatonic chords (\"T\", \"SD\", \"D\") and secondary and substitute dominants\n * with their supertonics (\"V/II\", \"ii/V\", \"subV/II\"...). Returns an empty\n * array for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"C\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.majorKeyChords(\"C\").slice(0, 3); // => [{ name: \"Cmaj7\", roles: [\"T\"] }, { name: \"Dm7\", roles: [\"SD\"] }, { name: \"Em7\", roles: [\"T\"] }]\n */\nexport function majorKeyChords(tonic: string): KeyChord[] {\n const key = majorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key, chords);\n return chords;\n}\n\n/**\n * Get the chords available in a minor key, each with the roles it plays. It\n * merges the chords of the natural, harmonic and melodic scales, so a chord\n * that appears in several scales has all its roles. Returns an empty array\n * for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"A\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.minorKeyChords(\"A\").slice(0, 2); // => [{ name: \"Am7\", roles: [\"T\", \"ii/bVII\"] }, { name: \"Bm7b5\", roles: [\"SD\", \"ii/I\"] }]\n */\nexport function minorKeyChords(tonic: string): KeyChord[] {\n const key = minorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key.natural, chords);\n keyChordsOf(key.harmonic, chords);\n keyChordsOf(key.melodic, chords);\n return chords;\n}\n\nfunction keyChordsOf(key: KeyScale, chords: KeyChord[]) {\n const updateChord = (name: string, newRole: string) => {\n if (!name) return;\n let keyChord = chords.find((chord) => chord.name === name);\n if (!keyChord) {\n keyChord = { name, roles: [] };\n chords.push(keyChord);\n }\n if (newRole && !keyChord.roles.includes(newRole)) {\n keyChord.roles.push(newRole);\n }\n };\n\n key.chords.forEach((chordName, index) =>\n updateChord(chordName, key.chordsHarmonicFunction[index]),\n );\n key.secondaryDominants.forEach((chordName, index) =>\n updateChord(chordName, `V/${key.grades[index]}`),\n );\n key.secondaryDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `ii/${key.grades[index]}`),\n );\n key.substituteDominants.forEach((chordName, index) =>\n updateChord(chordName, `subV/${key.grades[index]}`),\n );\n key.substituteDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `subii/${key.grades[index]}`),\n );\n}\n\n/**\n * Get a minor key: its signature, relative major and the scale, chords and\n * dominants of its natural, harmonic and melodic forms. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tnc - the tonic note or pitch class, like \"A\"\n * @example\n * Key.minorKey(\"A\").relativeMajor; // => \"C\"\n * Key.minorKey(\"A\").harmonic.scale; // => [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G#\"]\n * Key.minorKey(\"A\").natural.chords; // => [\"Am7\", \"Bm7b5\", \"Cmaj7\", \"Dm7\", \"Em7\", \"Fmaj7\", \"G7\"]\n */\nexport function minorKey(tnc: string): MinorKey {\n const pc = note(tnc).pc;\n if (!pc) return NoMinorKey;\n\n const alteration = distInFifths(\"C\", pc) - 3;\n return {\n type: \"minor\",\n tonic: pc,\n relativeMajor: transpose(pc, \"3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n natural: NaturalScale(pc),\n harmonic: HarmonicScale(pc),\n melodic: MelodicScale(pc),\n };\n}\n\n/**\n * Get the tonic of the major key with the given key signature, or null if\n * the signature is not valid.\n *\n * @param sig - accidentals (\"\", \"bb\", \"###\") or a number of fifths (positive for sharps, negative for flats)\n * @example\n * Key.majorTonicFromKeySignature(\"###\"); // => \"A\"\n * Key.majorTonicFromKeySignature(-2); // => \"Bb\"\n * Key.majorTonicFromKeySignature(\"\"); // => \"C\"\n */\nexport function majorTonicFromKeySignature(\n sig: string | number,\n): string | null {\n if (typeof sig === \"number\") {\n return transposeFifths(\"C\", sig);\n } else if (typeof sig === \"string\" && /^(b*|#*)$/.test(sig)) {\n return transposeFifths(\"C\", accToAlt(sig));\n }\n return null;\n}\n\n/** @deprecated Use the named exports instead. */\nexport default { majorKey, majorTonicFromKeySignature, minorKey };\n"],"mappings":";;;;AAIA,MAAM,QAA2B,OAAO,OAAO,CAAC,CAAa;AAY7D,MAAM,QAAa;CACjB,MAAM;CACN,OAAO;CACP,YAAY;CACZ,cAAc;AAChB;AAiCA,MAAM,aAAuB;CAC3B,OAAO;CACP,QAAQ;CACR,WAAW;CACX,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,wBAAwB;CACxB,aAAa;CACb,oBAAoB;CACpB,8BAA8B;CAC9B,kCAAkC;CAClC,qBAAqB;CACrB,+BAA+B;CAC/B,iCAAiC;AACnC;AAaA,MAAM,aAAuB;CAC3B,GAAG;CACH,GAAG;CACH,MAAM;CACN,eAAe;CACf,OAAO;CACP,qBAAqB;CACrB,8BAA8B;CAC9B,kCAAkC;AACpC;AAcA,MAAM,aAAuB;CAC3B,GAAG;CACH,MAAM;CACN,eAAe;CACf,SAAS;CACT,UAAU;CACV,SAAS;AACX;AASA,MAAM,kBAAkB,OAAiB,MAAgB,MAAM,OAC7D,KAAK,KAAK,MAAM,MAAM,GAAG,MAAM,KAAK,MAAM,MAAM;AAElD,SAAS,SACP,QACA,QACA,YACA,mBACA,aACA;CACA,QAAQ,UAA4B;EA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KAAK,GAAG;MAC1B,IAAI,OAAO,QAAQ,YAAY,YAAY,KAAK,GAAG,GACxD,OAAO,gBAAgB,KAAK,SAAS,GAAG,CAAC;CAE3C,OAAO;AACT;;AAGA,IAAA,cAAe;CAAE;CAAU;CAA4B;AAAS"}
1
+ {"version":3,"file":"index.mjs","names":["roman"],"sources":["../index.ts"],"sourcesContent":["import { simplify, transpose, transposeFifths } from \"@tonaljs/note\";\nimport { accToAlt, altToAcc, note } from \"@tonaljs/pitch-note\";\nimport { get as roman } from \"@tonaljs/roman-numeral\";\n\nconst Empty: readonly string[] = Object.freeze([] as string[]);\n\nexport interface Key {\n readonly type: \"major\" | \"minor\";\n /** The tonic pitch class, like \"F\". Empty for an invalid key. */\n readonly tonic: string;\n /** Number of accidentals in the key signature: positive for sharps, negative for flats. */\n readonly alteration: number;\n /** The key signature as accidentals: \"\" (none), \"bb\" (two flats), \"###\" (three sharps). */\n readonly keySignature: string;\n}\n\nconst NoKey: Key = {\n type: \"major\",\n tonic: \"\",\n alteration: 0,\n keySignature: \"\",\n};\n\nexport interface KeyScale {\n readonly tonic: string;\n /** Roman numeral of each degree, like [\"I\", \"II\", \"bIII\", ...]. */\n readonly grades: readonly string[];\n /** Interval from the tonic to each degree, like [\"1P\", \"2M\", \"3M\", ...]. */\n readonly intervals: readonly string[];\n /** Notes of the scale, one per degree. */\n readonly scale: readonly string[];\n /** Triad of each degree, like [\"F\", \"Gm\", \"Am\", ...]. */\n readonly triads: readonly string[];\n /** Seventh chord of each degree, like [\"Fmaj7\", \"Gm7\", ...]. */\n readonly chords: readonly string[];\n /** Harmonic function of each chord: \"T\" (tonic), \"SD\" (subdominant) or \"D\" (dominant). Empty string if none. */\n readonly chordsHarmonicFunction: readonly string[];\n /** Scale that fits each chord, like [\"F major\", \"G dorian\", ...]. */\n readonly chordScales: readonly string[];\n /** Dominant seventh chord that resolves to each degree (\"\" if the degree has none), like [\"\", \"D7\", \"E7\", ...] for F major. */\n readonly secondaryDominants: readonly string[];\n /** The ii chord that precedes each secondary dominant (\"\" if none): a minor seventh, or a half-diminished chord when the target is minor. */\n readonly secondaryDominantSupertonics: readonly string[];\n /** Tritone substitution of each secondary dominant (\"\" if none): the dominant seventh a tritone away, spelled a diminished fifth above it (Gb7 for C7) unless that needs a double flat or sharp (A7, not Bbb7, for Eb7). */\n readonly substituteDominants: readonly string[];\n /** The minor seventh ii chord that precedes each substitute dominant (\"\" if none). */\n readonly substituteDominantSupertonics: readonly string[];\n\n /** @deprecated use secondaryDominantSupertonics */\n readonly secondaryDominantsMinorRelative: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoKeyScale: KeyScale = {\n tonic: \"\",\n grades: Empty,\n intervals: Empty,\n scale: Empty,\n triads: Empty,\n chords: Empty,\n chordsHarmonicFunction: Empty,\n chordScales: Empty,\n secondaryDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n substituteDominants: Empty,\n substituteDominantSupertonics: Empty,\n secondaryDominantsMinorRelative: Empty,\n};\n\nexport interface MajorKey extends Key, KeyScale {\n readonly type: \"major\";\n /** Tonic of the relative minor key, like \"D\" for \"F\". */\n readonly minorRelative: string;\n readonly scale: readonly string[];\n readonly substituteDominants: readonly string[];\n readonly secondaryDominantSupertonics: readonly string[];\n /** @deprecated use substituteDominantSupertonics */\n readonly substituteDominantsMinorRelative: readonly string[];\n}\n\nconst NoMajorKey: MajorKey = {\n ...NoKey,\n ...NoKeyScale,\n type: \"major\",\n minorRelative: \"\",\n scale: Empty,\n substituteDominants: Empty,\n secondaryDominantSupertonics: Empty,\n substituteDominantsMinorRelative: Empty,\n};\n\nexport interface MinorKey extends Key {\n readonly type: \"minor\";\n /** Tonic of the relative major key, like \"C\" for \"A\". */\n readonly relativeMajor: string;\n /** Natural minor (aeolian) scale and its chords. */\n readonly natural: KeyScale;\n /** Harmonic minor scale and its chords. */\n readonly harmonic: KeyScale;\n /** Melodic minor (ascending) scale and its chords. */\n readonly melodic: KeyScale;\n}\n\nconst NoMinorKey: MinorKey = {\n ...NoKey,\n type: \"minor\",\n relativeMajor: \"\",\n natural: NoKeyScale,\n harmonic: NoKeyScale,\n melodic: NoKeyScale,\n};\n\nexport type KeyChord = {\n /** Chord symbol, like \"Dm7\". */\n name: string;\n /** Functions the chord plays in the key: \"T\", \"SD\", \"D\", or \"V/II\", \"ii/V\", \"subV/II\", \"subii/II\" for secondary and substitute dominants. */\n roles: string[];\n};\n\nconst mapScaleToType = (scale: string[], list: string[], sep = \"\") =>\n list.map((type, i) => `${scale[i]}${sep}${type}`);\n\nfunction keyScale(\n grades: string[],\n triads: string[],\n chordTypes: string[],\n harmonicFunctions: string[],\n chordScales: string[],\n) {\n return (tonic: string): KeyScale => {\n const intervals = grades.map((gr) => roman(gr).interval || \"\");\n const scale = intervals.map((interval) => transpose(tonic, interval));\n const chords = mapScaleToType(scale, chordTypes);\n const secondaryDominants = scale\n .map((note) => transpose(note, \"5P\"))\n .map((note) =>\n // A secondary dominant is a V chord which:\n // 1. is not diatonic to the key,\n // 2. it must have a diatonic root.\n scale.includes(note) && !chords.includes(note + \"7\") ? note + \"7\" : \"\",\n );\n\n const secondaryDominantSupertonics = supertonics(\n secondaryDominants,\n triads,\n );\n const substituteDominants = secondaryDominants.map((chord) =>\n chord ? substituteRoot(chord.slice(0, -1)) + \"7\" : \"\",\n );\n const substituteDominantSupertonics =\n substituteSupertonics(substituteDominants);\n\n return {\n tonic,\n grades,\n intervals,\n scale,\n triads: mapScaleToType(scale, triads),\n chords,\n chordsHarmonicFunction: harmonicFunctions.slice(),\n chordScales: mapScaleToType(scale, chordScales, \" \"),\n secondaryDominants,\n secondaryDominantSupertonics,\n substituteDominants,\n substituteDominantSupertonics,\n\n // deprecated aliases\n secondaryDominantsMinorRelative: secondaryDominantSupertonics,\n substituteDominantsMinorRelative: substituteDominantSupertonics,\n };\n };\n}\n\n// The root of a substitute dominant: a diminished fifth above the dominant's\n// root (Gb for C), or its simplest enharmonic when that has a double\n// accidental, as a lead sheet writes it (A, not Bbb, for Eb)\nfunction substituteRoot(dominantRoot: string) {\n const root = transpose(dominantRoot, \"5d\");\n return note(root).acc.length > 1 ? simplify(root) : root;\n}\n\n// The root of the ii chord of a dominant: a fifth above its root\nconst supertonicRoot = (dominant: string) =>\n transpose(dominant.slice(0, -1), \"5P\");\n\n// The ii of a secondary dominant takes its quality from the target: a ii-V\n// into a minor or diminished chord uses a half-diminished ii (Em7b5 A7 Dm),\n// and into a major or augmented chord a minor seventh ii (Gm7 C7 F)\nconst supertonics = (dominants: string[], targetTriads: string[]) =>\n dominants.map((chord, index) => {\n if (!chord) return \"\";\n const target = targetTriads[index];\n const isMinor = target === \"m\" || target === \"dim\";\n return supertonicRoot(chord) + (isMinor ? \"m7b5\" : \"m7\");\n });\n\n// A substitute dominant doesn't resolve to the key it points to (Eb7 resolves\n// to Dm, not Ab), so its ii is always a minor seventh: Bbm7 Eb7 Dm\nconst substituteSupertonics = (dominants: string[]) =>\n dominants.map((chord) => (chord ? supertonicRoot(chord) + \"m7\" : \"\"));\n\nconst distInFifths = (from: string, to: string) => {\n const f = note(from);\n const t = note(to);\n return f.empty || t.empty ? 0 : t.coord[0] - f.coord[0];\n};\n\nconst MajorScale = keyScale(\n \"I II III IV V VI VII\".split(\" \"),\n \" m m m dim\".split(\" \"),\n \"maj7 m7 m7 maj7 7 m7 m7b5\".split(\" \"),\n \"T SD T SD D T D\".split(\" \"),\n \"major,dorian,phrygian,lydian,mixolydian,minor,locrian\".split(\",\"),\n);\nconst NaturalScale = keyScale(\n \"I II bIII IV V bVI bVII\".split(\" \"),\n \"m dim m m \".split(\" \"),\n \"m7 m7b5 maj7 m7 m7 maj7 7\".split(\" \"),\n \"T SD T SD D SD SD\".split(\" \"),\n \"minor,locrian,major,dorian,phrygian,lydian,mixolydian\".split(\",\"),\n);\nconst HarmonicScale = keyScale(\n \"I II bIII IV V bVI VII\".split(\" \"),\n \"m dim aug m dim\".split(\" \"),\n \"mMaj7 m7b5 +maj7 m7 7 maj7 o7\".split(\" \"),\n \"T SD T SD D SD D\".split(\" \"),\n \"harmonic minor,locrian 6,major augmented,dorian #4,phrygian dominant,lydian #9,ultralocrian\".split(\n \",\",\n ),\n);\nconst MelodicScale = keyScale(\n \"I II bIII IV V VI VII\".split(\" \"),\n \"m m aug dim dim\".split(\" \"),\n \"m6 m7 +maj7 7 7 m7b5 m7b5\".split(\" \"),\n \"T SD T SD D \".split(\" \"),\n \"melodic minor,dorian b2,lydian augmented,lydian dominant,mixolydian b6,locrian #2,altered\".split(\n \",\",\n ),\n);\n\n/**\n * Get a major key: its signature, scale, triads, seventh chords, harmonic\n * functions and secondary and substitute dominants. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tonic - the tonic note or pitch class, like \"F\" or \"Bb\"\n * @example\n * Key.majorKey(\"F\").scale; // => [\"F\", \"G\", \"A\", \"Bb\", \"C\", \"D\", \"E\"]\n * Key.majorKey(\"F\").chords; // => [\"Fmaj7\", \"Gm7\", \"Am7\", \"Bbmaj7\", \"C7\", \"Dm7\", \"Em7b5\"]\n * Key.majorKey(\"F\").keySignature; // => \"b\"\n */\nexport function majorKey(tonic: string): MajorKey {\n const pc = note(tonic).pc;\n if (!pc) return NoMajorKey;\n\n const keyScale = MajorScale(pc);\n const alteration = distInFifths(\"C\", pc);\n\n return {\n ...keyScale,\n type: \"major\",\n minorRelative: transpose(pc, \"-3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n };\n}\n\n/**\n * Get the chords available in a major key, each with the roles it plays:\n * diatonic chords (\"T\", \"SD\", \"D\") and secondary and substitute dominants\n * with their supertonics (\"V/II\", \"ii/V\", \"subV/II\"...). Returns an empty\n * array for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"C\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.majorKeyChords(\"C\").slice(0, 3); // => [{ name: \"Cmaj7\", roles: [\"T\"] }, { name: \"Dm7\", roles: [\"SD\"] }, { name: \"Em7\", roles: [\"T\"] }]\n */\nexport function majorKeyChords(tonic: string): KeyChord[] {\n const key = majorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key, chords);\n return chords;\n}\n\n/**\n * Get the chords available in a minor key, each with the roles it plays. It\n * merges the chords of the natural, harmonic and melodic scales, so a chord\n * that appears in several scales has all its roles. Returns an empty array\n * for an invalid tonic.\n *\n * @param tonic - the tonic note or pitch class, like \"A\"\n * @returns array of { name: string, roles: string[] }\n * @example\n * Key.minorKeyChords(\"A\").slice(0, 2); // => [{ name: \"Am7\", roles: [\"T\", \"ii/bVII\"] }, { name: \"Bm7b5\", roles: [\"SD\", \"ii/I\"] }]\n */\nexport function minorKeyChords(tonic: string): KeyChord[] {\n const key = minorKey(tonic);\n const chords: KeyChord[] = [];\n keyChordsOf(key.natural, chords);\n keyChordsOf(key.harmonic, chords);\n keyChordsOf(key.melodic, chords);\n return chords;\n}\n\nfunction keyChordsOf(key: KeyScale, chords: KeyChord[]) {\n const updateChord = (name: string, newRole: string) => {\n if (!name) return;\n let keyChord = chords.find((chord) => chord.name === name);\n if (!keyChord) {\n keyChord = { name, roles: [] };\n chords.push(keyChord);\n }\n if (newRole && !keyChord.roles.includes(newRole)) {\n keyChord.roles.push(newRole);\n }\n };\n\n key.chords.forEach((chordName, index) =>\n updateChord(chordName, key.chordsHarmonicFunction[index]),\n );\n key.secondaryDominants.forEach((chordName, index) =>\n updateChord(chordName, `V/${key.grades[index]}`),\n );\n key.secondaryDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `ii/${key.grades[index]}`),\n );\n key.substituteDominants.forEach((chordName, index) =>\n updateChord(chordName, `subV/${key.grades[index]}`),\n );\n key.substituteDominantSupertonics.forEach((chordName, index) =>\n updateChord(chordName, `subii/${key.grades[index]}`),\n );\n}\n\n/**\n * Get a minor key: its signature, relative major and the scale, chords and\n * dominants of its natural, harmonic and melodic forms. For an invalid tonic\n * it returns a key with empty strings and arrays.\n *\n * @param tnc - the tonic note or pitch class, like \"A\"\n * @example\n * Key.minorKey(\"A\").relativeMajor; // => \"C\"\n * Key.minorKey(\"A\").harmonic.scale; // => [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G#\"]\n * Key.minorKey(\"A\").natural.chords; // => [\"Am7\", \"Bm7b5\", \"Cmaj7\", \"Dm7\", \"Em7\", \"Fmaj7\", \"G7\"]\n */\nexport function minorKey(tnc: string): MinorKey {\n const pc = note(tnc).pc;\n if (!pc) return NoMinorKey;\n\n const alteration = distInFifths(\"C\", pc) - 3;\n return {\n type: \"minor\",\n tonic: pc,\n relativeMajor: transpose(pc, \"3m\"),\n alteration,\n keySignature: altToAcc(alteration),\n natural: NaturalScale(pc),\n harmonic: HarmonicScale(pc),\n melodic: MelodicScale(pc),\n };\n}\n\n/**\n * Get the tonic of the major key with the given key signature, or null if\n * the signature is not valid.\n *\n * @param sig - accidentals (\"\", \"bb\", \"###\") or a number of fifths (positive for sharps, negative for flats)\n * @example\n * Key.majorTonicFromKeySignature(\"###\"); // => \"A\"\n * Key.majorTonicFromKeySignature(-2); // => \"Bb\"\n * Key.majorTonicFromKeySignature(\"\"); // => \"C\"\n */\nexport function majorTonicFromKeySignature(\n sig: string | number,\n): string | null {\n if (typeof sig === \"number\") {\n return transposeFifths(\"C\", sig);\n } else if (typeof sig === \"string\" && /^(b*|#*)$/.test(sig)) {\n return transposeFifths(\"C\", accToAlt(sig));\n }\n return null;\n}\n\n/** @deprecated Use the named exports instead. */\nexport default { majorKey, majorTonicFromKeySignature, minorKey };\n"],"mappings":";;;;AAIA,MAAM,QAA2B,OAAO,OAAO,CAAC,CAAa;AAY7D,MAAM,QAAa;CACjB,MAAM;CACN,OAAO;CACP,YAAY;CACZ,cAAc;AAChB;AAiCA,MAAM,aAAuB;CAC3B,OAAO;CACP,QAAQ;CACR,WAAW;CACX,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,wBAAwB;CACxB,aAAa;CACb,oBAAoB;CACpB,8B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,QAAQ,IAAI,OAAO,QAAQ,CACpD;CACA,IAAI,8BAA8B,SAAS,WAAW,UACpD,YAAY,WAAW,SAAS,IAAI,OAAO,QAAQ,CACrD;AACF;;;;;;;;;;;;AAaA,SAAgB,SAAS,KAAuB;CAC9C,MAAM,KAAK,KAAK,GAAG,CAAC,CAAC;CACrB,IAAI,CAAC,IAAI,OAAO;CAEhB,MAAM,aAAa,aAAa,KAAK,EAAE,IAAI;CAC3C,OAAO;EACL,MAAM;EACN,OAAO;EACP,eAAe,UAAU,IAAI,IAAI;EACjC;EACA,cAAc,SAAS,UAAU;EACjC,SAAS,aAAa,EAAE;EACxB,UAAU,cAAc,EAAE;EAC1B,SAAS,aAAa,EAAE;CAC1B;AACF;;;;;;;;;;;AAYA,SAAgB,2BACd,KACe;CACf,IAAI,OAAO,QAAQ,UACjB,OAAO,gBAAgB,KAAK,GAAG;MAC1B,IAAI,OAAO,QAAQ,YAAY,YAAY,KAAK,GAAG,GACxD,OAAO,gBAAgB,KAAK,SAAS,GAAG,CAAC;CAE3C,OAAO;AACT;;AAGA,IAAA,cAAe;CAAE;CAAU;CAA4B;AAAS"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonaljs/key",
3
- "version": "4.11.8",
3
+ "version": "4.12.1",
4
4
  "description": "Functions to work with musical keys",
5
5
  "keywords": [
6
6
  "key",
@@ -15,13 +15,13 @@
15
15
  ],
16
16
  "types": "dist/index.d.ts",
17
17
  "dependencies": {
18
- "@tonaljs/pitch-note": "6.1.3",
19
- "@tonaljs/note": "4.12.7",
20
- "@tonaljs/roman-numeral": "4.10.1"
18
+ "@tonaljs/pitch-note": "6.2.0",
19
+ "@tonaljs/note": "4.12.8",
20
+ "@tonaljs/roman-numeral": "4.10.2"
21
21
  },
22
22
  "devDependencies": {
23
- "@tonaljs/chord": "6.3.2",
24
- "@tonaljs/scale": "4.14.2"
23
+ "@tonaljs/chord": "6.5.1",
24
+ "@tonaljs/scale": "4.15.2"
25
25
  },
26
26
  "author": "danigb@gmail.com",
27
27
  "license": "MIT",