@libraz/libcantus 0.9.2 → 0.9.4

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.
Files changed (200) hide show
  1. package/README.md +6 -5
  2. package/dist/analyze/index.cjs +41 -1676
  3. package/dist/analyze/index.cjs.map +1 -1
  4. package/dist/analyze/index.d.cts +194 -122
  5. package/dist/analyze/index.d.ts +194 -122
  6. package/dist/analyze/index.js +19 -8
  7. package/dist/chunk-36N73KZ3.cjs +246 -0
  8. package/dist/chunk-36N73KZ3.cjs.map +1 -0
  9. package/dist/chunk-4HLLZPXT.cjs +3077 -0
  10. package/dist/chunk-4HLLZPXT.cjs.map +1 -0
  11. package/dist/chunk-7MGZ7D62.cjs +498 -0
  12. package/dist/chunk-7MGZ7D62.cjs.map +1 -0
  13. package/dist/{chunk-R3S2LUBH.js → chunk-7NKDPF74.js} +660 -305
  14. package/dist/chunk-7NKDPF74.js.map +1 -0
  15. package/dist/chunk-CAN76NCU.cjs +679 -0
  16. package/dist/chunk-CAN76NCU.cjs.map +1 -0
  17. package/dist/{chunk-7Y7WF64D.js → chunk-D2FAJBYL.js} +104 -35
  18. package/dist/chunk-D2FAJBYL.js.map +1 -0
  19. package/dist/{chunk-COKTX2G5.js → chunk-EHVEBWM5.js} +1052 -510
  20. package/dist/chunk-EHVEBWM5.js.map +1 -0
  21. package/dist/chunk-FCANXBAT.cjs +30 -0
  22. package/dist/chunk-FCANXBAT.cjs.map +1 -0
  23. package/dist/chunk-FNDWXRRI.cjs +479 -0
  24. package/dist/chunk-FNDWXRRI.cjs.map +1 -0
  25. package/dist/{chunk-GQKN5KDT.js → chunk-GQSGS2N4.js} +43 -29
  26. package/dist/chunk-GQSGS2N4.js.map +1 -0
  27. package/dist/chunk-HKGYCQ34.js +53 -0
  28. package/dist/chunk-HKGYCQ34.js.map +1 -0
  29. package/dist/chunk-I24MV65U.js +106 -0
  30. package/dist/chunk-I24MV65U.js.map +1 -0
  31. package/dist/{chunk-LN7ZZMD2.js → chunk-KMHOICIY.js} +322 -173
  32. package/dist/chunk-KMHOICIY.js.map +1 -0
  33. package/dist/chunk-LW53AHQU.cjs +1117 -0
  34. package/dist/chunk-LW53AHQU.cjs.map +1 -0
  35. package/dist/chunk-N6IHQUOX.js +1 -0
  36. package/dist/chunk-N6IHQUOX.js.map +1 -0
  37. package/dist/chunk-NKIBQ6PT.js +30 -0
  38. package/dist/chunk-NKIBQ6PT.js.map +1 -0
  39. package/dist/chunk-NP4NA2FB.cjs +106 -0
  40. package/dist/chunk-NP4NA2FB.cjs.map +1 -0
  41. package/dist/chunk-NVE34YGZ.cjs +1449 -0
  42. package/dist/chunk-NVE34YGZ.cjs.map +1 -0
  43. package/dist/{chunk-A4532ZFR.js → chunk-NYYWMV2C.js} +115 -47
  44. package/dist/chunk-NYYWMV2C.js.map +1 -0
  45. package/dist/{chunk-5HTNMQW4.js → chunk-Q2XUP227.js} +310 -118
  46. package/dist/chunk-Q2XUP227.js.map +1 -0
  47. package/dist/chunk-Q6OP4U43.cjs +327 -0
  48. package/dist/chunk-Q6OP4U43.cjs.map +1 -0
  49. package/dist/chunk-RC6GBMCD.cjs +845 -0
  50. package/dist/chunk-RC6GBMCD.cjs.map +1 -0
  51. package/dist/{chunk-XTC3X6MS.js → chunk-RK35RIN7.js} +453 -152
  52. package/dist/chunk-RK35RIN7.js.map +1 -0
  53. package/dist/chunk-RUMU3UA6.cjs +53 -0
  54. package/dist/chunk-RUMU3UA6.cjs.map +1 -0
  55. package/dist/chunk-T64C7ZD7.cjs +1 -0
  56. package/dist/chunk-T64C7ZD7.cjs.map +1 -0
  57. package/dist/chunk-XEBG3G53.js +498 -0
  58. package/dist/chunk-XEBG3G53.js.map +1 -0
  59. package/dist/chunk-YLEMGEFH.js +246 -0
  60. package/dist/chunk-YLEMGEFH.js.map +1 -0
  61. package/dist/chunk-ZEJR4TKL.cjs +173 -0
  62. package/dist/chunk-ZEJR4TKL.cjs.map +1 -0
  63. package/dist/core/index.cjs +130 -431
  64. package/dist/core/index.cjs.map +1 -1
  65. package/dist/core/index.d.cts +331 -17
  66. package/dist/core/index.d.ts +331 -17
  67. package/dist/core/index.js +71 -13
  68. package/dist/{function-2bJW1Zp_.d.ts → function-BtiZb9i9.d.ts} +28 -7
  69. package/dist/{function-r2sh-jOO.d.cts → function-DNugAOrt.d.cts} +28 -7
  70. package/dist/generate/index.cjs +30 -3751
  71. package/dist/generate/index.cjs.map +1 -1
  72. package/dist/generate/index.d.cts +373 -59
  73. package/dist/generate/index.d.ts +373 -59
  74. package/dist/generate/index.js +23 -9
  75. package/dist/{index-DZ1M21do.d.cts → index-6VR8-kit.d.ts} +20 -4
  76. package/dist/index-70p168VM.d.ts +192 -0
  77. package/dist/{index-D_ZTUNKp.d.cts → index-Be74mcon.d.cts} +18 -17
  78. package/dist/index-BtXT6UQ0.d.cts +192 -0
  79. package/dist/{index-BMPxwjwV.d.ts → index-C5UBbQ4G.d.ts} +18 -17
  80. package/dist/{index-BOBXBNwP.d.cts → index-C83mZI1P.d.ts} +93 -8
  81. package/dist/{index-BdeQ1r_p.d.cts → index-D6b6na69.d.cts} +14 -11
  82. package/dist/{index-Cl7mhOFM.d.cts → index-DXd9JikN.d.cts} +92 -10
  83. package/dist/{index-Cl7mhOFM.d.ts → index-DXd9JikN.d.ts} +92 -10
  84. package/dist/index-OG65ocOr.d.cts +272 -0
  85. package/dist/index-OG65ocOr.d.ts +272 -0
  86. package/dist/{index-BreoJRw0.d.ts → index-tJhkmQgE.d.cts} +20 -4
  87. package/dist/{index-CP3wAfEH.d.ts → index-tSQh-xwY.d.ts} +14 -11
  88. package/dist/{index-CM5W4rcm.d.ts → index-y_ak9_Hi.d.cts} +93 -8
  89. package/dist/index.cjs +419 -6848
  90. package/dist/index.cjs.map +1 -1
  91. package/dist/index.d.cts +14 -13
  92. package/dist/index.d.ts +14 -13
  93. package/dist/index.js +132 -27
  94. package/dist/model/index.cjs +8 -2423
  95. package/dist/model/index.cjs.map +1 -1
  96. package/dist/model/index.d.cts +433 -74
  97. package/dist/model/index.d.ts +433 -74
  98. package/dist/model/index.js +6 -7
  99. package/dist/styled-DIRkk9Uz.d.ts +506 -0
  100. package/dist/styled-DJbkJQfj.d.cts +506 -0
  101. package/dist/theory/index.cjs +172 -1676
  102. package/dist/theory/index.cjs.map +1 -1
  103. package/dist/theory/index.d.cts +151 -233
  104. package/dist/theory/index.d.ts +151 -233
  105. package/dist/theory/index.js +31 -7
  106. package/dist/{types-MGP29r3t.d.cts → types-BN6_Q9Kl.d.cts} +11 -2
  107. package/dist/{types-MGP29r3t.d.ts → types-BN6_Q9Kl.d.ts} +11 -2
  108. package/package.json +73 -24
  109. package/dist/chunk-5HTNMQW4.js.map +0 -1
  110. package/dist/chunk-7Y7WF64D.js.map +0 -1
  111. package/dist/chunk-A4532ZFR.js.map +0 -1
  112. package/dist/chunk-BHUT2S5G.js +0 -22
  113. package/dist/chunk-BHUT2S5G.js.map +0 -1
  114. package/dist/chunk-CCBLVAPK.js +0 -42
  115. package/dist/chunk-CCBLVAPK.js.map +0 -1
  116. package/dist/chunk-COKTX2G5.js.map +0 -1
  117. package/dist/chunk-DYWFNC4A.js +0 -152
  118. package/dist/chunk-DYWFNC4A.js.map +0 -1
  119. package/dist/chunk-GQKN5KDT.js.map +0 -1
  120. package/dist/chunk-LN7ZZMD2.js.map +0 -1
  121. package/dist/chunk-MSWFZF7J.js +0 -118
  122. package/dist/chunk-MSWFZF7J.js.map +0 -1
  123. package/dist/chunk-R3S2LUBH.js.map +0 -1
  124. package/dist/chunk-SDM3VJJ5.js +0 -41
  125. package/dist/chunk-SDM3VJJ5.js.map +0 -1
  126. package/dist/chunk-XTC3X6MS.js.map +0 -1
  127. package/dist/chunk-ZXW5YIWK.js +0 -55
  128. package/dist/chunk-ZXW5YIWK.js.map +0 -1
  129. package/dist/index-CGMZNCx6.d.cts +0 -140
  130. package/dist/index-CGMZNCx6.d.ts +0 -140
  131. package/dist/styled-BjXtOFNk.d.cts +0 -282
  132. package/dist/styled-D7BAx3S3.d.ts +0 -282
  133. package/src/analyze/arrange/index.ts +0 -21
  134. package/src/analyze/arrange/internal.ts +0 -125
  135. package/src/analyze/arrange/tension.ts +0 -187
  136. package/src/analyze/arrange/tracks.ts +0 -279
  137. package/src/analyze/detect/index.ts +0 -235
  138. package/src/analyze/functional/borrowed.ts +0 -84
  139. package/src/analyze/functional/cadence.ts +0 -66
  140. package/src/analyze/functional/function.ts +0 -184
  141. package/src/analyze/functional/index.ts +0 -23
  142. package/src/analyze/functional/internal.ts +0 -29
  143. package/src/analyze/functional/roman.ts +0 -393
  144. package/src/analyze/index.ts +0 -49
  145. package/src/analyze/timeline/index.ts +0 -415
  146. package/src/analyze/voice/index.ts +0 -330
  147. package/src/core/index.ts +0 -47
  148. package/src/core/interval/index.ts +0 -81
  149. package/src/core/meter/index.ts +0 -282
  150. package/src/core/pitch/index.ts +0 -311
  151. package/src/core/random/index.ts +0 -53
  152. package/src/core/tuning/index.ts +0 -156
  153. package/src/core/types.ts +0 -29
  154. package/src/generate/bass/index.ts +0 -350
  155. package/src/generate/countermelody/index.ts +0 -379
  156. package/src/generate/drums/beat.ts +0 -266
  157. package/src/generate/drums/euclid.ts +0 -58
  158. package/src/generate/drums/fills.ts +0 -356
  159. package/src/generate/drums/ghost.ts +0 -119
  160. package/src/generate/drums/hihat.ts +0 -285
  161. package/src/generate/drums/hit.ts +0 -31
  162. package/src/generate/drums/index.ts +0 -329
  163. package/src/generate/drums/internal.ts +0 -257
  164. package/src/generate/drums/kick.ts +0 -160
  165. package/src/generate/drums/percussion.ts +0 -121
  166. package/src/generate/drums/rng.ts +0 -7
  167. package/src/generate/drums/swing.ts +0 -71
  168. package/src/generate/groove/index.ts +0 -326
  169. package/src/generate/harmonize/index.ts +0 -440
  170. package/src/generate/index.ts +0 -59
  171. package/src/generate/motif/index.ts +0 -398
  172. package/src/generate/progression/index.ts +0 -370
  173. package/src/generate/reharmony/index.ts +0 -330
  174. package/src/generate/rhythm/index.ts +0 -165
  175. package/src/index.ts +0 -247
  176. package/src/model/chord.ts +0 -423
  177. package/src/model/index.ts +0 -15
  178. package/src/model/interval.ts +0 -82
  179. package/src/model/key.ts +0 -266
  180. package/src/model/note.ts +0 -176
  181. package/src/model/progression.ts +0 -161
  182. package/src/model/shared.ts +0 -12
  183. package/src/theory/chord/index.ts +0 -373
  184. package/src/theory/chordscale/index.ts +0 -374
  185. package/src/theory/counterpoint/index.ts +0 -230
  186. package/src/theory/harmony/index.ts +0 -92
  187. package/src/theory/index.ts +0 -107
  188. package/src/theory/safety/index.ts +0 -419
  189. package/src/theory/scale/degrees.ts +0 -123
  190. package/src/theory/scale/index.ts +0 -28
  191. package/src/theory/scale/key.ts +0 -56
  192. package/src/theory/scale/masks.ts +0 -153
  193. package/src/theory/spelling/index.ts +0 -182
  194. package/src/theory/symbol/index.ts +0 -268
  195. package/src/theory/voicing/index.ts +0 -5
  196. package/src/theory/voicing/internal.ts +0 -168
  197. package/src/theory/voicing/leading.ts +0 -112
  198. package/src/theory/voicing/satb.ts +0 -195
  199. package/src/theory/voicing/styled.ts +0 -176
  200. package/tsconfig.json +0 -23
@@ -1,235 +0,0 @@
1
- /**
2
- * Recognition: infer a chord name from a set of pitches, or a key from a set of
3
- * pitch classes. This is the inverse direction of the chord/scale builders,
4
- * which only go name -> notes.
5
- */
6
-
7
- import type { KeyScale } from '../../core/types.js';
8
- import type { Chord, ChordQuality } from '../../theory/chord/index.js';
9
- import { chordPitchClasses, chordQualities, makeChord } from '../../theory/chord/index.js';
10
- import {
11
- HARMONIC_MINOR_MASK,
12
- MAJOR_MASK,
13
- MELODIC_MINOR_MASK,
14
- majorKey,
15
- minorKey,
16
- NATURAL_MINOR_MASK,
17
- } from '../../theory/scale/index.js';
18
-
19
- /**
20
- * A candidate chord interpretation of a pitch set.
21
- *
22
- * @category Recognition
23
- */
24
- export type ChordMatch = {
25
- rootPc: number;
26
- quality: ChordQuality;
27
- /** Chord tones absent from the input (an incomplete voicing). */
28
- missingPcs: number[];
29
- /** Input pitch classes not belonging to the chord. */
30
- extraPcs: number[];
31
- /** True when the input pitch-class set equals the chord exactly. */
32
- exact: boolean;
33
- /** Inversion implied by the lowest note: 0 root position, 1 first, ... */
34
- inversion: number;
35
- /** Bass pitch class when inverted (the lowest note is not the root). */
36
- bassPc?: number;
37
- };
38
-
39
- /**
40
- * A candidate key interpretation of a pitch-class set.
41
- *
42
- * @category Recognition
43
- */
44
- export type KeyMatch = {
45
- key: KeyScale;
46
- mode: 'major' | 'minor';
47
- /** Fraction of input pitch classes that are in the scale, in [0, 1]. */
48
- fit: number;
49
- };
50
-
51
- /** Reduce any integer to a pitch class in [0, 11]. */
52
- function pitchClass(value: number): number {
53
- return ((Math.trunc(value) % 12) + 12) % 12;
54
- }
55
-
56
- /** Unique pitch classes of the input, sorted ascending. */
57
- function uniquePitchClasses(pitches: number[]): number[] {
58
- return [...new Set(pitches.map(pitchClass))].sort((a, b) => a - b);
59
- }
60
-
61
- /**
62
- * Identify chords matching a set of pitches.
63
- *
64
- * Every input pitch class is tried as a root against every known chord quality.
65
- * A match is reported when all of the chord's tones are present in the input, or
66
- * when the only absent tone is the perfect fifth (a common omission in shell
67
- * voicings); matches are ranked best-first by fewest extra notes, then fewest
68
- * missing notes, then most specific (largest) chord. An exact match (no extras,
69
- * no missing) is flagged and ranked first.
70
- *
71
- * @param pitches MIDI pitches or bare pitch classes (octave-agnostic).
72
- * @returns Ranked chord interpretations (may be empty).
73
- * @example
74
- * ```ts
75
- * import { detectChord } from '@libraz/libcantus';
76
- * const matches = detectChord([60, 64, 67]); // C E G
77
- * matches[0]; // { rootPc: 0, quality: 'maj', exact: true, ... }
78
- * ```
79
- * @category Recognition
80
- */
81
- export function detectChord(pitches: number[]): ChordMatch[] {
82
- const input = uniquePitchClasses(pitches);
83
- if (input.length === 0) {
84
- return [];
85
- }
86
- const bassPc = pitchClass(Math.min(...pitches));
87
- const inputSet = new Set(input);
88
- const matches: ChordMatch[] = [];
89
- const qualities = chordQualities();
90
- for (const rootPc of input) {
91
- for (const quality of qualities) {
92
- const chord = makeChord(rootPc, quality);
93
- const tones = chordPitchClasses(chord);
94
- const toneSet = new Set(tones);
95
- const missingPcs = tones.filter((pc) => !inputSet.has(pc));
96
- // Accept an exact-tone match, or one whose only absent tone is the perfect
97
- // fifth; any other missing tone (third, sus tone, seventh, ...) rejects.
98
- const fifthPc = pitchClass(rootPc + 7);
99
- if (missingPcs.length > 1 || (missingPcs.length === 1 && missingPcs[0] !== fifthPc)) {
100
- continue;
101
- }
102
- const extraPcs = input.filter((pc) => !toneSet.has(pc));
103
- const bassIndex = chord.intervals.findIndex((iv) => pitchClass(rootPc + iv) === bassPc);
104
- const inversion = bassIndex > 0 ? bassIndex : 0;
105
- const match: ChordMatch = {
106
- rootPc,
107
- quality,
108
- missingPcs,
109
- extraPcs,
110
- exact: extraPcs.length === 0 && missingPcs.length === 0,
111
- inversion,
112
- };
113
- if (inversion > 0) {
114
- match.bassPc = bassPc;
115
- }
116
- matches.push(match);
117
- }
118
- }
119
- matches.sort((a, b) => {
120
- if (a.extraPcs.length !== b.extraPcs.length) {
121
- return a.extraPcs.length - b.extraPcs.length;
122
- }
123
- if (a.missingPcs.length !== b.missingPcs.length) {
124
- return a.missingPcs.length - b.missingPcs.length;
125
- }
126
- // Prefer root position (the bass is the chord root) on a tie.
127
- if ((a.inversion === 0) !== (b.inversion === 0)) {
128
- return a.inversion === 0 ? -1 : 1;
129
- }
130
- const aSize = chordPitchClasses(makeChord(a.rootPc, a.quality)).length;
131
- const bSize = chordPitchClasses(makeChord(b.rootPc, b.quality)).length;
132
- return bSize - aSize;
133
- });
134
- return matches;
135
- }
136
-
137
- /**
138
- * The single best chord interpretation of a pitch set, or null if none.
139
- *
140
- * @param pitches MIDI pitches or bare pitch classes.
141
- * @returns The top-ranked chord, or null when nothing matches.
142
- * @example
143
- * ```ts
144
- * import { detectChordBest } from '@libraz/libcantus';
145
- * detectChordBest([60, 64, 67]); // C major triad: { rootPc: 0, quality: 'maj', ... }
146
- * ```
147
- * @category Recognition
148
- */
149
- export function detectChordBest(pitches: number[]): Chord | null {
150
- const best = detectChord(pitches)[0];
151
- if (!best) {
152
- return null;
153
- }
154
- return makeChord(best.rootPc, best.quality, best.bassPc);
155
- }
156
-
157
- /**
158
- * Minor-scale variants scored for each minor-key candidate. Scoring against all
159
- * three lets the raised sixth and seventh (e.g. the leading tone G# in A minor)
160
- * count toward their own tonic instead of only penalizing it.
161
- */
162
- const MINOR_MASK_VARIANTS = [NATURAL_MINOR_MASK, HARMONIC_MINOR_MASK, MELODIC_MINOR_MASK];
163
-
164
- /**
165
- * Rank major and minor keys by how well they contain a set of pitch classes.
166
- *
167
- * The tonic is weighted so that, among equally-fitting keys, the one whose root
168
- * appears in the input is preferred. Minor candidates are scored against the
169
- * natural, harmonic, and melodic minor variants and keep the best of the three,
170
- * so a minor cadence containing the leading tone still resolves to its own
171
- * tonic; the returned key is always the natural-minor scale. Returns all 24
172
- * keys ranked best-first, or an empty array for an empty input (mirroring
173
- * {@link detectChord}).
174
- *
175
- * @param pitches MIDI pitches or bare pitch classes.
176
- * @returns Ranked key interpretations (empty for an empty input).
177
- * @example
178
- * ```ts
179
- * import { detectKey } from '@libraz/libcantus';
180
- * const keys = detectKey([60, 62, 64, 65, 67, 69, 71]); // C major scale
181
- * keys[0].mode; // 'major', with keys[0].key.rootPc === 0
182
- * ```
183
- * @category Recognition
184
- */
185
- export function detectKey(pitches: number[]): KeyMatch[] {
186
- const input = uniquePitchClasses(pitches);
187
- if (input.length === 0) {
188
- return [];
189
- }
190
- const counts = new Map<number, number>();
191
- for (const pc of pitches.map(pitchClass)) {
192
- counts.set(pc, (counts.get(pc) ?? 0) + 1);
193
- }
194
- const total = pitches.length;
195
- const results: (KeyMatch & { score: number })[] = [];
196
- for (let tonic = 0; tonic < 12; tonic += 1) {
197
- for (const mode of ['major', 'minor'] as const) {
198
- const key = mode === 'major' ? majorKey(tonic) : minorKey(tonic);
199
- const masks = mode === 'major' ? [MAJOR_MASK] : MINOR_MASK_VARIANTS;
200
- // Score each scale variant and keep the best; ties keep the earlier
201
- // (more diatonic) variant.
202
- let inScale = 0;
203
- let weighted = Number.NEGATIVE_INFINITY;
204
- for (const mask of masks) {
205
- let variantInScale = 0;
206
- let variantWeighted = 0;
207
- for (const pc of input) {
208
- const offset = (pc - tonic + 12) % 12;
209
- if ((mask >> offset) & 1) {
210
- variantInScale += 1;
211
- }
212
- }
213
- for (const [pc, count] of counts) {
214
- const offset = (pc - tonic + 12) % 12;
215
- if ((mask >> offset) & 1) {
216
- variantWeighted += count;
217
- }
218
- }
219
- if (variantWeighted > weighted) {
220
- weighted = variantWeighted;
221
- inScale = variantInScale;
222
- }
223
- }
224
- weighted += (counts.get(tonic) ?? 0) * 0.5;
225
- results.push({
226
- key,
227
- mode,
228
- fit: inScale / input.length,
229
- score: weighted / total,
230
- });
231
- }
232
- }
233
- results.sort((a, b) => b.score - a.score);
234
- return results.map(({ key, mode, fit }) => ({ key, mode, fit }));
235
- }
@@ -1,84 +0,0 @@
1
- /**
2
- * Borrowed chords (modal interchange) and the Neapolitan.
3
- *
4
- * Roots are pitch classes measured against the key tonic, so borrowed and
5
- * chromatic chords are handled by their semitone offset rather than requiring a
6
- * spelled key signature.
7
- */
8
-
9
- import type { KeyScale } from '../../core/types.js';
10
- import type { Chord } from '../../theory/chord/index.js';
11
- import { HARMONIC_MINOR_MASK } from '../../theory/scale/index.js';
12
- import { isDiatonic, isMinorKey, parallelKey } from './function.js';
13
- import { isNeapolitan, mod12 } from './internal.js';
14
-
15
- /**
16
- * The origin of a recognized non-diatonic chord, or null when none applies.
17
- *
18
- * @category Functional Harmony
19
- */
20
- export type BorrowedSource = 'parallel-minor' | 'parallel-major' | 'neapolitan' | null;
21
-
22
- /**
23
- * In a minor key, whether a non-diatonic chord is explained by the harmonic
24
- * minor scale on the same tonic (major V, V7, the raised-leading-tone viio).
25
- * Such chords are in-key chromatic alterations, not modal interchange.
26
- */
27
- function isHarmonicMinorAlteration(chord: Chord, key: KeyScale): boolean {
28
- return (
29
- isMinorKey(key) &&
30
- isDiatonic(chord, { rootPc: mod12(key.rootPc), modeMask12: HARMONIC_MINOR_MASK })
31
- );
32
- }
33
-
34
- /**
35
- * Whether a chord is borrowed from the parallel mode (modal interchange).
36
- *
37
- * True when the chord is not diatonic to `key` but is diatonic to its
38
- * {@link parallelKey} — e.g. iv, bVI, or bVII in a major key, or the Picardy
39
- * tonic and major IV in a minor key. Two non-diatonic families are excluded:
40
- * chords diatonic to neither mode (they are chromatic, not borrowed), and, in
41
- * a minor key, chords explained by the harmonic minor scale (the major
42
- * dominant and raised-leading-tone chords), which are in-key alterations
43
- * rather than interchange even though they happen to fit the parallel major.
44
- *
45
- * @param chord The chord to test.
46
- * @param key The prevailing key.
47
- * @returns True if the chord is borrowed from the parallel mode.
48
- * @category Functional Harmony
49
- */
50
- export function isBorrowedChord(chord: Chord, key: KeyScale): boolean {
51
- if (isDiatonic(chord, key)) {
52
- return false;
53
- }
54
- if (isHarmonicMinorAlteration(chord, key)) {
55
- return false;
56
- }
57
- return isDiatonic(chord, parallelKey(key));
58
- }
59
-
60
- /**
61
- * Identify where a non-diatonic chord comes from.
62
- *
63
- * The Neapolitan (major triad on b2) is recognized first, since it sits
64
- * outside both parallel modes; parallel-mode borrowing follows the
65
- * {@link isBorrowedChord} rules. Diatonic chords, harmonic-minor alterations,
66
- * and unrecognized chromatic chords all yield null.
67
- *
68
- * @param chord The chord to classify.
69
- * @param key The prevailing key.
70
- * @returns The borrowing source, or null.
71
- * @category Functional Harmony
72
- */
73
- export function borrowedSource(chord: Chord, key: KeyScale): BorrowedSource {
74
- if (isDiatonic(chord, key)) {
75
- return null;
76
- }
77
- if (isNeapolitan(chord, key)) {
78
- return 'neapolitan';
79
- }
80
- if (isBorrowedChord(chord, key)) {
81
- return isMinorKey(key) ? 'parallel-major' : 'parallel-minor';
82
- }
83
- return null;
84
- }
@@ -1,66 +0,0 @@
1
- /**
2
- * Cadence detection between two successive chords.
3
- *
4
- * Roots are pitch classes measured against the key tonic, so borrowed and
5
- * chromatic chords are handled by their semitone offset rather than requiring a
6
- * spelled key signature.
7
- */
8
-
9
- import type { KeyScale } from '../../core/types.js';
10
- import type { Chord } from '../../theory/chord/index.js';
11
- import { isMinorKey } from './function.js';
12
- import { mod12 } from './internal.js';
13
-
14
- /**
15
- * A recognized cadence type, or null when a chord pair forms none.
16
- *
17
- * @category Functional Harmony
18
- */
19
- export type Cadence = 'authentic' | 'plagal' | 'half' | 'deceptive' | null;
20
-
21
- /**
22
- * Classify the cadence formed by moving from one chord to the next.
23
- *
24
- * - authentic: V (dominant a fifth above the tonic) to I
25
- * - plagal: IV (a fourth above the tonic) to I
26
- * - deceptive: V to the submediant (diatonic vi or borrowed bVI in major, VI in
27
- * minor)
28
- * - half: any chord other than the dominant itself to V
29
- *
30
- * A static V-to-V repeat with no root motion is not a cadence and yields null.
31
- *
32
- * @param from The penultimate chord.
33
- * @param to The final chord.
34
- * @param key The prevailing key.
35
- * @returns The cadence type, or null.
36
- * @example
37
- * ```ts
38
- * import { detectCadence, makeChord, majorKey } from '@libraz/libcantus';
39
- * detectCadence(makeChord(7, 'maj'), makeChord(0, 'maj'), majorKey(0)); // => 'authentic'
40
- * ```
41
- * @category Functional Harmony
42
- */
43
- export function detectCadence(from: Chord, to: Chord, key: KeyScale): Cadence {
44
- const tonic = mod12(key.rootPc);
45
- const fromOffset = mod12(from.rootPc - tonic);
46
- const toOffset = mod12(to.rootPc - tonic);
47
- // Deceptive targets: the diatonic submediant (vi at offset 9 in major, VI at
48
- // offset 8 in minor) plus, in a major key, the borrowed flat-submediant bVI
49
- // at offset 8.
50
- const deceptiveTargets = isMinorKey(key) ? [8] : [8, 9];
51
- if (fromOffset === 7 && toOffset === 0) {
52
- return 'authentic';
53
- }
54
- if (fromOffset === 5 && toOffset === 0) {
55
- return 'plagal';
56
- }
57
- if (fromOffset === 7 && deceptiveTargets.includes(toOffset)) {
58
- return 'deceptive';
59
- }
60
- // A move to the dominant is a half cadence, but a no-root-motion V-to-V repeat
61
- // is not a cadence at all.
62
- if (toOffset === 7 && fromOffset !== 7) {
63
- return 'half';
64
- }
65
- return null;
66
- }
@@ -1,184 +0,0 @@
1
- /**
2
- * Harmonic function (tonic / subdominant / dominant), diatonicity, parallel-key
3
- * mirroring, and full chord analysis.
4
- *
5
- * Roots are pitch classes measured against the key tonic, so borrowed and
6
- * chromatic chords are handled by their semitone offset rather than requiring a
7
- * spelled key signature.
8
- */
9
-
10
- import type { KeyScale } from '../../core/types.js';
11
- import type { Chord, ChordQuality } from '../../theory/chord/index.js';
12
- import { chordPitchClasses, makeChord } from '../../theory/chord/index.js';
13
- import { isScaleTone, MAJOR_MASK, NATURAL_MINOR_MASK } from '../../theory/scale/index.js';
14
- import { type BorrowedSource, borrowedSource } from './borrowed.js';
15
- import { degreeRootPc, isNeapolitan, mod12 } from './internal.js';
16
- import { chordToRoman } from './roman.js';
17
-
18
- /**
19
- * The three broad harmonic functions of tonal music.
20
- *
21
- * @category Functional Harmony
22
- */
23
- export type HarmonicFunction = 'tonic' | 'subdominant' | 'dominant';
24
-
25
- /** Harmonic function of each semitone offset above the tonic (major context). */
26
- const FUNCTION_BY_OFFSET: readonly HarmonicFunction[] = [
27
- 'tonic', // 0 I
28
- 'subdominant', // 1 bII (Neapolitan)
29
- 'subdominant', // 2 ii
30
- 'tonic', // 3 bIII
31
- 'tonic', // 4 iii
32
- 'subdominant', // 5 IV
33
- 'dominant', // 6 #IV / bV
34
- 'dominant', // 7 V
35
- 'subdominant', // 8 bVI
36
- 'tonic', // 9 vi
37
- 'subdominant', // 10 bVII
38
- 'dominant', // 11 vii
39
- ];
40
-
41
- /**
42
- * Whether a key's scale has a minor third and no major third (a minor key).
43
- *
44
- * @category Functional Harmony
45
- */
46
- export function isMinorKey(key: KeyScale): boolean {
47
- const hasMinorThird = (key.modeMask12 >> 3) & 1;
48
- const hasMajorThird = (key.modeMask12 >> 4) & 1;
49
- return Boolean(hasMinorThird) && !hasMajorThird;
50
- }
51
-
52
- /**
53
- * The harmonic function of a chord in a key, from its root's offset above the
54
- * tonic. The mapping follows common-practice major-key function and is a useful
55
- * approximation in minor and for borrowed chords.
56
- *
57
- * @param chord The chord.
58
- * @param key The prevailing key.
59
- * @returns The harmonic function.
60
- * @category Functional Harmony
61
- */
62
- export function functionOf(chord: Chord, key: KeyScale): HarmonicFunction {
63
- return FUNCTION_BY_OFFSET[mod12(chord.rootPc - key.rootPc)] ?? 'tonic';
64
- }
65
-
66
- /**
67
- * The result of {@link analyzeChord}: function, borrowing, and Roman numeral.
68
- *
69
- * @category Functional Harmony
70
- */
71
- export type ChordAnalysis = {
72
- function: HarmonicFunction;
73
- borrowed: boolean;
74
- source: BorrowedSource;
75
- roman: string;
76
- };
77
-
78
- /**
79
- * Whether every pitch class of a chord belongs to the key's scale.
80
- *
81
- * The test is strict against the key's own mode mask: in a natural-minor key
82
- * the harmonic-minor dominant (major V) is *not* diatonic, since the raised
83
- * leading tone lies outside the mask. Borrowing predicates treat that case as
84
- * an in-key alteration separately (see {@link isBorrowedChord}).
85
- *
86
- * @param chord The chord to test.
87
- * @param key The prevailing key.
88
- * @returns True if all chord pitch classes are scale tones.
89
- * @category Functional Harmony
90
- */
91
- export function isDiatonic(chord: Chord, key: KeyScale): boolean {
92
- return chordPitchClasses(chord).every((pc) => isScaleTone(pc, key));
93
- }
94
-
95
- /**
96
- * The parallel key: same tonic, opposite mode.
97
- *
98
- * A key with a minor third (natural/harmonic/melodic minor, dorian, phrygian)
99
- * maps to the parallel major; any other key maps to the parallel natural minor.
100
- *
101
- * @param key The key to mirror.
102
- * @returns The parallel major or natural-minor key on the same tonic.
103
- * @category Functional Harmony
104
- */
105
- export function parallelKey(key: KeyScale): KeyScale {
106
- return {
107
- rootPc: mod12(key.rootPc),
108
- modeMask12: isMinorKey(key) ? MAJOR_MASK : NATURAL_MINOR_MASK,
109
- };
110
- }
111
-
112
- /** Diminished-family qualities: diminished triad, dim7, half-diminished. */
113
- function isDiminishedQuality(quality: ChordQuality): boolean {
114
- return quality === 'dim' || quality === 'dim7' || quality === 'm7b5';
115
- }
116
-
117
- /** Whether the chord's interval template carries a major third above the root. */
118
- function hasMajorThird(chord: Chord): boolean {
119
- return chord.intervals.some((interval) => mod12(interval) === 4);
120
- }
121
-
122
- /**
123
- * Quality-aware harmonic function, refining the offset table where chord
124
- * quality disambiguates: diminished chords on the leading tone or the raised
125
- * subdominant resolve by semitone and act as dominants; the Neapolitan and the
126
- * major-third chords on bVI/bVII borrowed from the parallel minor act as
127
- * subdominant (predominant) harmony. All other chords keep {@link functionOf}.
128
- */
129
- function qualityAwareFunction(chord: Chord, key: KeyScale): HarmonicFunction {
130
- const offset = mod12(chord.rootPc - key.rootPc);
131
- if (isDiminishedQuality(chord.quality) && (offset === 11 || offset === 6)) {
132
- return 'dominant';
133
- }
134
- if (isNeapolitan(chord, key)) {
135
- return 'subdominant';
136
- }
137
- if (!isMinorKey(key) && hasMajorThird(chord) && (offset === 8 || offset === 10)) {
138
- return 'subdominant';
139
- }
140
- return functionOf(chord, key);
141
- }
142
-
143
- /**
144
- * Analyze a chord in a key: harmonic function, borrowing, and Roman numeral.
145
- *
146
- * The function is quality-aware (see the predicates behind it), the source
147
- * follows {@link borrowedSource}, and the numeral comes from
148
- * {@link chordToRoman}. `borrowed` is true whenever a source is identified —
149
- * including the Neapolitan, which the stricter parallel-mode predicate
150
- * {@link isBorrowedChord} does not count.
151
- *
152
- * @param chord The chord to analyze.
153
- * @param key The prevailing key.
154
- * @returns The chord analysis.
155
- * @example
156
- * ```ts
157
- * import { analyzeChord, makeChord, majorKey } from '@libraz/libcantus';
158
- * analyzeChord(makeChord(7, 'dom7'), majorKey(0));
159
- * // { function: 'dominant', borrowed: false, source: null, roman: 'V7' }
160
- * ```
161
- * @category Functional Harmony
162
- */
163
- export function analyzeChord(chord: Chord, key: KeyScale): ChordAnalysis {
164
- const source = borrowedSource(chord, key);
165
- return {
166
- function: qualityAwareFunction(chord, key),
167
- borrowed: source !== null,
168
- source,
169
- roman: chordToRoman(chord, key),
170
- };
171
- }
172
-
173
- /**
174
- * The secondary dominant (V7) that tonicizes a scale degree.
175
- *
176
- * @param targetDegree 0-based scale degree to tonicize.
177
- * @param key The prevailing key.
178
- * @returns A dominant-seventh chord a fifth above the target's root.
179
- * @category Functional Harmony
180
- */
181
- export function secondaryDominant(targetDegree: number, key: KeyScale): Chord {
182
- const targetRoot = degreeRootPc(targetDegree + 1, key);
183
- return makeChord(mod12(targetRoot + 7), 'dom7');
184
- }
@@ -1,23 +0,0 @@
1
- /**
2
- * Functional harmony: Roman-numeral parsing and formatting, harmonic function
3
- * (tonic / subdominant / dominant), cadence detection, and borrowed chords.
4
- *
5
- * Roots are pitch classes measured against the key tonic, so borrowed and
6
- * chromatic chords are handled by their semitone offset rather than requiring a
7
- * spelled key signature.
8
- */
9
-
10
- export type { BorrowedSource } from './borrowed.js';
11
- export { borrowedSource, isBorrowedChord } from './borrowed.js';
12
- export type { Cadence } from './cadence.js';
13
- export { detectCadence } from './cadence.js';
14
- export type { ChordAnalysis, HarmonicFunction } from './function.js';
15
- export {
16
- analyzeChord,
17
- functionOf,
18
- isDiatonic,
19
- isMinorKey,
20
- parallelKey,
21
- secondaryDominant,
22
- } from './function.js';
23
- export { chordToRoman, romanToChord } from './roman.js';
@@ -1,29 +0,0 @@
1
- /**
2
- * Shared helpers for the functional-harmony modules.
3
- *
4
- * Roots are pitch classes measured against the key tonic, so borrowed and
5
- * chromatic chords are handled by their semitone offset rather than requiring a
6
- * spelled key signature.
7
- */
8
-
9
- import type { KeyScale } from '../../core/types.js';
10
- import type { Chord } from '../../theory/chord/index.js';
11
- import { scaleTonesInDegreeOrder } from '../../theory/scale/index.js';
12
-
13
- export function mod12(n: number): number {
14
- return ((n % 12) + 12) % 12;
15
- }
16
-
17
- /** Diatonic pitch class of a 1-based scale degree in a key. */
18
- export function degreeRootPc(degreeNumber: number, key: KeyScale): number {
19
- const tones = scaleTonesInDegreeOrder(key);
20
- if (tones.length === 0) {
21
- return mod12(key.rootPc);
22
- }
23
- return tones[(degreeNumber - 1) % tones.length] ?? mod12(key.rootPc);
24
- }
25
-
26
- /** Whether a chord is the Neapolitan: a major triad on the flat second degree. */
27
- export function isNeapolitan(chord: Chord, key: KeyScale): boolean {
28
- return mod12(chord.rootPc - key.rootPc) === 1 && chord.quality === 'maj';
29
- }