@libraz/libcantus 0.9.0 → 0.9.2

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 (155) hide show
  1. package/README.md +25 -2
  2. package/dist/analyze/index.cjs +1697 -0
  3. package/dist/analyze/index.cjs.map +1 -0
  4. package/dist/analyze/index.d.cts +429 -0
  5. package/dist/analyze/index.d.ts +429 -0
  6. package/dist/analyze/index.js +51 -0
  7. package/dist/analyze/index.js.map +1 -0
  8. package/dist/chunk-5HTNMQW4.js +653 -0
  9. package/dist/chunk-5HTNMQW4.js.map +1 -0
  10. package/dist/chunk-7Y7WF64D.js +410 -0
  11. package/dist/chunk-7Y7WF64D.js.map +1 -0
  12. package/dist/chunk-A4532ZFR.js +259 -0
  13. package/dist/chunk-A4532ZFR.js.map +1 -0
  14. package/dist/chunk-BHUT2S5G.js +22 -0
  15. package/dist/chunk-BHUT2S5G.js.map +1 -0
  16. package/dist/chunk-CCBLVAPK.js +42 -0
  17. package/dist/chunk-CCBLVAPK.js.map +1 -0
  18. package/dist/chunk-COKTX2G5.js +907 -0
  19. package/dist/chunk-COKTX2G5.js.map +1 -0
  20. package/dist/chunk-DYWFNC4A.js +152 -0
  21. package/dist/chunk-DYWFNC4A.js.map +1 -0
  22. package/dist/chunk-GQKN5KDT.js +159 -0
  23. package/dist/chunk-GQKN5KDT.js.map +1 -0
  24. package/dist/chunk-LN7ZZMD2.js +530 -0
  25. package/dist/chunk-LN7ZZMD2.js.map +1 -0
  26. package/dist/chunk-MSWFZF7J.js +118 -0
  27. package/dist/chunk-MSWFZF7J.js.map +1 -0
  28. package/dist/chunk-R3S2LUBH.js +2722 -0
  29. package/dist/chunk-R3S2LUBH.js.map +1 -0
  30. package/dist/chunk-SDM3VJJ5.js +41 -0
  31. package/dist/chunk-SDM3VJJ5.js.map +1 -0
  32. package/dist/chunk-XTC3X6MS.js +816 -0
  33. package/dist/chunk-XTC3X6MS.js.map +1 -0
  34. package/dist/chunk-ZXW5YIWK.js +55 -0
  35. package/dist/chunk-ZXW5YIWK.js.map +1 -0
  36. package/dist/core/index.cjs +432 -0
  37. package/dist/core/index.cjs.map +1 -0
  38. package/dist/core/index.d.cts +210 -0
  39. package/dist/core/index.d.ts +210 -0
  40. package/dist/core/index.js +73 -0
  41. package/dist/core/index.js.map +1 -0
  42. package/dist/function-2bJW1Zp_.d.ts +187 -0
  43. package/dist/function-r2sh-jOO.d.cts +187 -0
  44. package/dist/generate/index.cjs +3787 -0
  45. package/dist/generate/index.cjs.map +1 -0
  46. package/dist/generate/index.d.cts +980 -0
  47. package/dist/generate/index.d.ts +980 -0
  48. package/dist/generate/index.js +52 -0
  49. package/dist/generate/index.js.map +1 -0
  50. package/dist/index-BMPxwjwV.d.ts +151 -0
  51. package/dist/index-BOBXBNwP.d.cts +157 -0
  52. package/dist/index-BdeQ1r_p.d.cts +56 -0
  53. package/dist/index-BreoJRw0.d.ts +127 -0
  54. package/dist/index-CGMZNCx6.d.cts +140 -0
  55. package/dist/index-CGMZNCx6.d.ts +140 -0
  56. package/dist/index-CM5W4rcm.d.ts +157 -0
  57. package/dist/index-CP3wAfEH.d.ts +56 -0
  58. package/dist/index-Cl7mhOFM.d.cts +164 -0
  59. package/dist/index-Cl7mhOFM.d.ts +164 -0
  60. package/dist/index-DZ1M21do.d.cts +127 -0
  61. package/dist/index-D_ZTUNKp.d.cts +151 -0
  62. package/dist/index.cjs +5277 -4893
  63. package/dist/index.cjs.map +1 -1
  64. package/dist/index.d.cts +14 -2621
  65. package/dist/index.d.ts +14 -2621
  66. package/dist/index.js +170 -6152
  67. package/dist/index.js.map +1 -1
  68. package/dist/model/index.cjs +2435 -0
  69. package/dist/model/index.cjs.map +1 -0
  70. package/dist/model/index.d.cts +630 -0
  71. package/dist/model/index.d.ts +630 -0
  72. package/dist/model/index.js +21 -0
  73. package/dist/model/index.js.map +1 -0
  74. package/dist/styled-BjXtOFNk.d.cts +282 -0
  75. package/dist/styled-D7BAx3S3.d.ts +282 -0
  76. package/dist/theory/index.cjs +1677 -0
  77. package/dist/theory/index.cjs.map +1 -0
  78. package/dist/theory/index.d.cts +535 -0
  79. package/dist/theory/index.d.ts +535 -0
  80. package/dist/theory/index.js +149 -0
  81. package/dist/theory/index.js.map +1 -0
  82. package/dist/types-MGP29r3t.d.cts +30 -0
  83. package/dist/types-MGP29r3t.d.ts +30 -0
  84. package/package.json +31 -1
  85. package/src/analyze/arrange/index.ts +21 -0
  86. package/src/analyze/arrange/internal.ts +125 -0
  87. package/src/analyze/arrange/tension.ts +187 -0
  88. package/src/analyze/arrange/tracks.ts +279 -0
  89. package/src/{detect → analyze/detect}/index.ts +82 -23
  90. package/src/analyze/functional/borrowed.ts +84 -0
  91. package/src/analyze/functional/cadence.ts +66 -0
  92. package/src/analyze/functional/function.ts +184 -0
  93. package/src/analyze/functional/index.ts +23 -0
  94. package/src/analyze/functional/internal.ts +29 -0
  95. package/src/analyze/functional/roman.ts +393 -0
  96. package/src/analyze/index.ts +49 -0
  97. package/src/{timeline → analyze/timeline}/index.ts +87 -24
  98. package/src/{analysis → analyze/voice}/index.ts +81 -12
  99. package/src/core/index.ts +47 -0
  100. package/src/{interval → core/interval}/index.ts +15 -1
  101. package/src/{meter → core/meter}/index.ts +108 -5
  102. package/src/{pitch → core/pitch}/index.ts +58 -3
  103. package/src/{random → core/random}/index.ts +13 -1
  104. package/src/{tuning → core/tuning}/index.ts +28 -1
  105. package/src/{bass → generate/bass}/index.ts +66 -17
  106. package/src/{countermelody → generate/countermelody}/index.ts +54 -24
  107. package/src/{drums → generate/drums}/fills.ts +31 -20
  108. package/src/{drums → generate/drums}/index.ts +127 -9
  109. package/src/{drums → generate/drums}/internal.ts +7 -1
  110. package/src/{drums → generate/drums}/rng.ts +2 -2
  111. package/src/{drums → generate/drums}/swing.ts +7 -2
  112. package/src/{groove → generate/groove}/index.ts +118 -22
  113. package/src/{harmonize → generate/harmonize}/index.ts +96 -23
  114. package/src/generate/index.ts +59 -0
  115. package/src/{motif → generate/motif}/index.ts +94 -17
  116. package/src/{progression → generate/progression}/index.ts +61 -27
  117. package/src/{reharmony → generate/reharmony}/index.ts +46 -15
  118. package/src/{rhythm → generate/rhythm}/index.ts +47 -13
  119. package/src/index.ts +130 -116
  120. package/src/model/chord.ts +423 -0
  121. package/src/model/index.ts +5 -947
  122. package/src/model/interval.ts +82 -0
  123. package/src/model/key.ts +266 -0
  124. package/src/model/note.ts +176 -0
  125. package/src/model/progression.ts +161 -0
  126. package/src/model/shared.ts +12 -0
  127. package/src/{chord → theory/chord}/index.ts +85 -4
  128. package/src/{chordscale → theory/chordscale}/index.ts +67 -9
  129. package/src/{counterpoint → theory/counterpoint}/index.ts +23 -2
  130. package/src/{harmony → theory/harmony}/index.ts +19 -2
  131. package/src/theory/index.ts +107 -0
  132. package/src/{safety → theory/safety}/index.ts +59 -12
  133. package/src/theory/scale/degrees.ts +123 -0
  134. package/src/theory/scale/index.ts +28 -0
  135. package/src/theory/scale/key.ts +56 -0
  136. package/src/theory/scale/masks.ts +153 -0
  137. package/src/{spelling → theory/spelling}/index.ts +21 -3
  138. package/src/{symbol → theory/symbol}/index.ts +91 -36
  139. package/src/theory/voicing/index.ts +5 -0
  140. package/src/theory/voicing/internal.ts +168 -0
  141. package/src/theory/voicing/leading.ts +112 -0
  142. package/src/theory/voicing/satb.ts +195 -0
  143. package/src/theory/voicing/styled.ts +176 -0
  144. package/src/arrange/index.ts +0 -409
  145. package/src/functional/index.ts +0 -602
  146. package/src/scale/index.ts +0 -215
  147. package/src/voicing/index.ts +0 -523
  148. /package/src/{types.ts → core/types.ts} +0 -0
  149. /package/src/{drums → generate/drums}/beat.ts +0 -0
  150. /package/src/{drums → generate/drums}/euclid.ts +0 -0
  151. /package/src/{drums → generate/drums}/ghost.ts +0 -0
  152. /package/src/{drums → generate/drums}/hihat.ts +0 -0
  153. /package/src/{drums → generate/drums}/hit.ts +0 -0
  154. /package/src/{drums → generate/drums}/kick.ts +0 -0
  155. /package/src/{drums → generate/drums}/percussion.ts +0 -0
@@ -8,950 +8,8 @@
8
8
  * mutable data.
9
9
  */
10
10
 
11
- import {
12
- type Chord as ChordData,
13
- type ChordQuality,
14
- chordFromDegree,
15
- chordPitchClasses,
16
- diatonicSeventh,
17
- diatonicTriad,
18
- makeChord,
19
- } from '../chord/index.js';
20
- import {
21
- availableTensions,
22
- avoidNotes,
23
- type ChordScaleMatch,
24
- chordScales,
25
- } from '../chordscale/index.js';
26
- import { detectChord, detectChordBest } from '../detect/index.js';
27
- import {
28
- analyzeChord,
29
- type BorrowedSource,
30
- borrowedSource,
31
- type Cadence,
32
- type ChordAnalysis,
33
- chordToRoman,
34
- detectCadence,
35
- functionOf,
36
- type HarmonicFunction,
37
- isBorrowedChord,
38
- isMinorKey,
39
- romanToChord,
40
- } from '../functional/index.js';
41
- import {
42
- formatNote,
43
- midiToNote,
44
- type Note as NoteData,
45
- noteToMidi,
46
- noteToPitchClass,
47
- parseNote,
48
- type SpelledInterval,
49
- spelledInterval,
50
- } from '../pitch/index.js';
51
- import { negativeHarmonyMirror } from '../reharmony/index.js';
52
- import {
53
- isScaleTone,
54
- majorKey,
55
- minorKey,
56
- scaleByName,
57
- scaleTonesInDegreeOrder,
58
- } from '../scale/index.js';
59
- import { spellChord, spellScale } from '../spelling/index.js';
60
- import { formatChordSymbol, parseChordSymbol } from '../symbol/index.js';
61
- import type { KeyScale } from '../types.js';
62
- import {
63
- type StyledVoicingOptions,
64
- type VoicingOptions,
65
- voiceChord,
66
- voiceChordStyled,
67
- voiceProgression,
68
- } from '../voicing/index.js';
69
-
70
- /** Reduce any integer to a pitch class in [0, 11]. */
71
- function mod12(n: number): number {
72
- return ((n % 12) + 12) % 12;
73
- }
74
-
75
- /** Defensive copy of a plain note. */
76
- function copyNote(data: NoteData): NoteData {
77
- const copy: NoteData = { letter: data.letter, alter: data.alter };
78
- if (data.octave !== undefined) {
79
- copy.octave = data.octave;
80
- }
81
- return copy;
82
- }
83
-
84
- /** Defensive copy of a plain chord. */
85
- function copyChord(data: ChordData): ChordData {
86
- const copy: ChordData = {
87
- rootPc: data.rootPc,
88
- quality: data.quality,
89
- intervals: [...data.intervals],
90
- };
91
- if (data.bassPc !== undefined) {
92
- copy.bassPc = data.bassPc;
93
- }
94
- return copy;
95
- }
96
-
97
- /** Spell a bare pitch class as an octave-less note with a sharp/flat preference. */
98
- function spellPitchClassBare(pc: number, spelling: 'sharp' | 'flat'): NoteData {
99
- const spelled = midiToNote(60 + mod12(pc), spelling);
100
- return { letter: spelled.letter, alter: spelled.alter };
101
- }
102
-
103
- /**
104
- * An immutable spelled note: a diatonic letter plus a chromatic alteration and
105
- * an optional octave. Wraps the plain note object and delegates to the pitch
106
- * module; every transformation returns a new instance.
107
- */
108
- export class Note {
109
- readonly #data: NoteData;
110
-
111
- /**
112
- * Wrap a plain note object.
113
- *
114
- * @param data The spelled note; it is copied, never retained or mutated.
115
- */
116
- constructor(data: NoteData) {
117
- this.#data = copyNote(data);
118
- }
119
-
120
- /**
121
- * Parse scientific pitch notation (e.g. `'C#4'`, `'Bb'`, `'F##3'`).
122
- *
123
- * @param name The note text.
124
- * @returns The parsed note.
125
- * @throws If the text is not a valid note.
126
- */
127
- static of(name: string): Note {
128
- return new Note(parseNote(name));
129
- }
130
-
131
- /**
132
- * Name a MIDI number (middle C = C4 = 60) as a note.
133
- *
134
- * @param midi The MIDI number.
135
- * @param spelling Whether to prefer sharps or flats for black keys.
136
- * @returns The spelled note, with octave.
137
- */
138
- static fromMidi(midi: number, spelling: 'sharp' | 'flat' = 'sharp'): Note {
139
- return new Note(midiToNote(midi, spelling));
140
- }
141
-
142
- /**
143
- * Wrap an existing plain note object.
144
- *
145
- * @param data The plain note.
146
- * @returns The wrapped note.
147
- */
148
- static fromData(data: NoteData): Note {
149
- return new Note(data);
150
- }
151
-
152
- /** The note rendered as scientific pitch notation, e.g. `'G4'` or `'Bb'`. */
153
- get name(): string {
154
- return formatNote(this.#data);
155
- }
156
-
157
- /** The pitch class (0..11), ignoring octave. */
158
- get pitchClass(): number {
159
- return noteToPitchClass(this.#data);
160
- }
161
-
162
- /**
163
- * The MIDI number (middle C = C4 = 60).
164
- *
165
- * @throws If the note has no octave and therefore no fixed pitch.
166
- */
167
- get midi(): number {
168
- if (this.#data.octave === undefined) {
169
- throw new Error(`note ${this.name} has no octave, so it has no MIDI number`);
170
- }
171
- return noteToMidi(this.#data);
172
- }
173
-
174
- /** The diatonic letter number: 0..6 for C..B. */
175
- get letter(): number {
176
- return this.#data.letter;
177
- }
178
-
179
- /** The chromatic alteration in semitones: -1 flat, 0 natural, +1 sharp, ... */
180
- get alter(): number {
181
- return this.#data.alter;
182
- }
183
-
184
- /** The octave (scientific pitch notation), or undefined for a bare pitch class. */
185
- get octave(): number | undefined {
186
- return this.#data.octave;
187
- }
188
-
189
- /** A copy of the underlying plain note object. */
190
- get data(): NoteData {
191
- return copyNote(this.#data);
192
- }
193
-
194
- /**
195
- * Transpose by a signed number of semitones.
196
- *
197
- * When the note carries an octave the transposition happens in MIDI space and
198
- * the result is spelled with a sharp preference. An octave-less note stays
199
- * octave-less: only its pitch class is moved.
200
- *
201
- * @param semitones The signed semitone offset.
202
- * @returns The transposed note.
203
- */
204
- transpose(semitones: number): Note {
205
- if (this.#data.octave !== undefined) {
206
- return Note.fromMidi(this.midi + semitones);
207
- }
208
- return new Note(spellPitchClassBare(this.pitchClass + semitones, 'sharp'));
209
- }
210
-
211
- /**
212
- * The spelled interval from this note to another.
213
- *
214
- * @param other The second note.
215
- * @returns The diatonic number, quality, and semitone span.
216
- */
217
- intervalTo(other: Note): SpelledInterval {
218
- return spelledInterval(this.#data, other.#data);
219
- }
220
-
221
- /**
222
- * Whether another note has the same letter, alteration, and octave.
223
- *
224
- * @param other The note to compare.
225
- * @returns True if the spellings are identical.
226
- */
227
- equals(other: Note): boolean {
228
- return (
229
- this.#data.letter === other.#data.letter &&
230
- this.#data.alter === other.#data.alter &&
231
- this.#data.octave === other.#data.octave
232
- );
233
- }
234
- }
235
-
236
- /**
237
- * An immutable spelled interval value: a diatonic number, a quality label, and
238
- * a signed semitone span. A thin convenience wrapper over the pitch module's
239
- * plain interval result.
240
- */
241
- export class Interval {
242
- readonly #number: number;
243
- readonly #quality: string;
244
- readonly #semitones: number;
245
-
246
- private constructor(numberValue: number, quality: string, semitones: number) {
247
- this.#number = numberValue;
248
- this.#quality = quality;
249
- this.#semitones = semitones;
250
- }
251
-
252
- /**
253
- * The spelled interval between two notes.
254
- *
255
- * @param a The first note.
256
- * @param b The second note.
257
- * @returns The interval from `a` to `b`.
258
- */
259
- static between(a: Note, b: Note): Interval {
260
- const spelled = a.intervalTo(b);
261
- return new Interval(spelled.number, spelled.quality, spelled.semitones);
262
- }
263
-
264
- /**
265
- * Build an interval from explicit components.
266
- *
267
- * @param numberValue Diatonic size: 1 = unison, 2 = second, ... 8 = octave.
268
- * @param quality Quality label: `'P'`, `'M'`, `'m'`, or repeated `'A'`/`'d'`.
269
- * @param semitones Signed semitone span.
270
- * @returns The interval.
271
- */
272
- static of(numberValue: number, quality: string, semitones: number): Interval {
273
- return new Interval(numberValue, quality, semitones);
274
- }
275
-
276
- /** Diatonic size: 1 = unison, 2 = second, ... 8 = octave, and beyond. */
277
- get number(): number {
278
- return this.#number;
279
- }
280
-
281
- /** Quality label: `'P'`, `'M'`, `'m'`, or repeated `'A'`/`'d'`. */
282
- get quality(): string {
283
- return this.#quality;
284
- }
285
-
286
- /** Signed semitone span. */
287
- get semitones(): number {
288
- return this.#semitones;
289
- }
290
-
291
- /** A readable label composed of quality and number, e.g. `'P5'` or `'M3'`. */
292
- get name(): string {
293
- return `${this.#quality}${this.#number}`;
294
- }
295
- }
296
-
297
- /** Resolve a string or numeric key root into a spelled tonic note. */
298
- function resolveTonic(root: string | number, spelling: 'sharp' | 'flat'): Note {
299
- return typeof root === 'string' ? Note.of(root) : new Note(spellPitchClassBare(root, spelling));
300
- }
301
-
302
- /**
303
- * An immutable key/scale: a `KeyScale` (root pitch class plus mode mask) paired
304
- * with a spelled tonic that anchors letter-name spelling. Acts as the factory
305
- * for key-aware chords.
306
- */
307
- export class Key {
308
- readonly #scale: KeyScale;
309
- readonly #tonic: Note;
310
-
311
- /**
312
- * Wrap a key/scale and its spelled tonic.
313
- *
314
- * @param scale The key/scale; its root is normalized to a pitch class.
315
- * @param tonic The spelled tonic anchoring letter-name spelling.
316
- */
317
- constructor(scale: KeyScale, tonic: Note) {
318
- this.#scale = { rootPc: mod12(scale.rootPc), modeMask12: scale.modeMask12 };
319
- this.#tonic = tonic;
320
- }
321
-
322
- /**
323
- * A major key.
324
- *
325
- * @param root Tonic as a note name (e.g. `'Eb'`) or a pitch class; numeric
326
- * roots are spelled with a sharp preference.
327
- * @returns The major key.
328
- */
329
- static major(root: string | number): Key {
330
- const tonic = resolveTonic(root, 'sharp');
331
- return new Key(majorKey(tonic.pitchClass), tonic);
332
- }
333
-
334
- /**
335
- * A natural-minor key.
336
- *
337
- * @param root Tonic as a note name or a pitch class; numeric roots are
338
- * spelled with a flat preference.
339
- * @returns The minor key.
340
- */
341
- static minor(root: string | number): Key {
342
- const tonic = resolveTonic(root, 'flat');
343
- return new Key(minorKey(tonic.pitchClass), tonic);
344
- }
345
-
346
- /**
347
- * A key on a named scale (e.g. `'dorian'`, `'harmonicMinor'`).
348
- *
349
- * @param name The scale name, a key of the scale module's named-scale table.
350
- * @param root Tonic as a note name or a pitch class.
351
- * @returns The key.
352
- * @throws If the name is not a known scale.
353
- */
354
- static named(name: string, root: string | number): Key {
355
- const tonic = resolveTonic(root, 'sharp');
356
- return new Key(scaleByName(name, tonic.pitchClass), tonic);
357
- }
358
-
359
- /**
360
- * Wrap an existing `KeyScale`, synthesizing a spelled tonic when none is
361
- * given (flat-preferred for minor-third scales, sharp-preferred otherwise).
362
- *
363
- * @param scale The key/scale to wrap.
364
- * @param tonic Optional spelled tonic.
365
- * @returns The key.
366
- */
367
- static of(scale: KeyScale, tonic?: Note): Key {
368
- const spelled =
369
- tonic ?? new Note(spellPitchClassBare(scale.rootPc, isMinorKey(scale) ? 'flat' : 'sharp'));
370
- return new Key(scale, spelled);
371
- }
372
-
373
- /** A copy of the underlying plain `KeyScale`. */
374
- get scale(): KeyScale {
375
- return { rootPc: this.#scale.rootPc, modeMask12: this.#scale.modeMask12 };
376
- }
377
-
378
- /** The spelled tonic. */
379
- get tonic(): Note {
380
- return this.#tonic;
381
- }
382
-
383
- /** The tonic pitch class (0..11). */
384
- get rootPc(): number {
385
- return this.#scale.rootPc;
386
- }
387
-
388
- /** Whether the scale has a minor third and no major third. */
389
- get isMinor(): boolean {
390
- return isMinorKey(this.#scale);
391
- }
392
-
393
- /**
394
- * The scale's pitch classes in ascending scale-degree order (degree 0 first).
395
- *
396
- * @returns One pitch class per scale degree.
397
- */
398
- pitchClasses(): number[] {
399
- return scaleTonesInDegreeOrder(this.#scale);
400
- }
401
-
402
- /**
403
- * The spelled scale, one note per degree (e.g. C D E F G A B for C major).
404
- *
405
- * @returns Spelled octave-less notes in scale-degree order.
406
- */
407
- notes(): Note[] {
408
- return spellScale(this.#tonic.data, this.#scale).map((note) => new Note(note));
409
- }
410
-
411
- /**
412
- * Alias of {@link Key.notes}.
413
- *
414
- * @returns Spelled octave-less notes in scale-degree order.
415
- */
416
- spell(): Note[] {
417
- return this.notes();
418
- }
419
-
420
- /**
421
- * The spelled scale as letter-name strings.
422
- *
423
- * @returns One name per scale degree.
424
- */
425
- noteNames(): string[] {
426
- return this.notes().map((note) => note.name);
427
- }
428
-
429
- /**
430
- * Build a chord on a scale degree, carrying this key as context.
431
- *
432
- * With an explicit quality the quality's interval template is attached to the
433
- * degree's diatonic root; without one the scale-correct diatonic triad is
434
- * stacked (e.g. a diminished triad on the leading tone of a major key).
435
- *
436
- * @param degree 0-based scale degree of the chord root.
437
- * @param quality Optional chord quality.
438
- * @returns The chord, with this key attached.
439
- */
440
- chord(degree: number, quality?: ChordQuality): Chord {
441
- const data =
442
- quality === undefined
443
- ? diatonicTriad(degree, this.#scale)
444
- : chordFromDegree(degree, quality, this.#scale);
445
- return new Chord(data, this);
446
- }
447
-
448
- /**
449
- * The diatonic triad on a scale degree, carrying this key as context.
450
- *
451
- * @param degree 0-based scale degree of the chord root.
452
- * @returns The triad, with this key attached.
453
- */
454
- diatonicTriad(degree: number): Chord {
455
- return new Chord(diatonicTriad(degree, this.#scale), this);
456
- }
457
-
458
- /**
459
- * The diatonic seventh chord on a scale degree, carrying this key as context.
460
- *
461
- * @param degree 0-based scale degree of the chord root.
462
- * @returns The seventh chord, with this key attached.
463
- */
464
- diatonicSeventh(degree: number): Chord {
465
- return new Chord(diatonicSeventh(degree, this.#scale), this);
466
- }
467
-
468
- /**
469
- * Build the chord denoted by a Roman numeral in this key (including applied
470
- * chords such as `'V7/V'`), carrying this key as context.
471
- *
472
- * @param text The Roman numeral.
473
- * @returns The chord, with this key attached.
474
- * @throws If the numeral is not valid.
475
- */
476
- roman(text: string): Chord {
477
- return new Chord(romanToChord(text, this.#scale), this);
478
- }
479
-
480
- /**
481
- * Whether a pitch belongs to the scale.
482
- *
483
- * @param x A MIDI pitch, bare pitch class, or note.
484
- * @returns True if the pitch class is a scale tone.
485
- */
486
- contains(x: number | Note): boolean {
487
- return isScaleTone(typeof x === 'number' ? x : x.pitchClass, this.#scale);
488
- }
489
- }
490
-
491
- /**
492
- * An immutable chord: a root pitch class, quality, interval template, and
493
- * optional slash bass, optionally carrying a {@link Key} context. Analysis
494
- * methods (`roman`, `function`, `analyze`, ...) use an explicitly passed key
495
- * first and fall back to the carried context.
496
- */
497
- export class Chord {
498
- readonly #data: ChordData;
499
- readonly #key: Key | undefined;
500
-
501
- /**
502
- * Wrap a plain chord object.
503
- *
504
- * @param data The chord; it is copied, never retained or mutated.
505
- * @param key Optional key context for analysis methods.
506
- */
507
- constructor(data: ChordData, key?: Key) {
508
- this.#data = copyChord(data);
509
- this.#key = key;
510
- }
511
-
512
- /**
513
- * Build a chord from a root and quality.
514
- *
515
- * @param root Root as a note name (e.g. `'Eb'`) or a pitch class.
516
- * @param quality The chord quality.
517
- * @param bass Optional slash-chord bass pitch class.
518
- * @returns The chord (without key context).
519
- */
520
- static of(root: string | number, quality: ChordQuality, bass?: number): Chord {
521
- const rootPc = typeof root === 'string' ? Note.of(root).pitchClass : root;
522
- return new Chord(makeChord(rootPc, quality, bass));
523
- }
524
-
525
- /**
526
- * Wrap an existing plain chord object.
527
- *
528
- * @param data The plain chord.
529
- * @returns The wrapped chord (without key context).
530
- */
531
- static from(data: ChordData): Chord {
532
- return new Chord(data);
533
- }
534
-
535
- /**
536
- * Parse a lead-sheet chord symbol (e.g. `'Cmaj7'`, `'F#m7b5'`, `'C/G'`).
537
- *
538
- * @param symbol The chord symbol.
539
- * @returns The chord (without key context).
540
- * @throws If the root or quality is not recognized.
541
- */
542
- static parse(symbol: string): Chord {
543
- return new Chord(parseChordSymbol(symbol));
544
- }
545
-
546
- /**
547
- * Identify the chords matching a set of pitches, best interpretation first.
548
- *
549
- * @param pitches MIDI pitches or bare pitch classes.
550
- * @returns Ranked chord interpretations (may be empty).
551
- */
552
- static detect(pitches: number[]): Chord[] {
553
- return detectChord(pitches).map(
554
- (match) => new Chord(makeChord(match.rootPc, match.quality, match.bassPc)),
555
- );
556
- }
557
-
558
- /**
559
- * The single best chord interpretation of a pitch set.
560
- *
561
- * @param pitches MIDI pitches or bare pitch classes.
562
- * @returns The top-ranked chord, or null when nothing matches.
563
- */
564
- static detectBest(pitches: number[]): Chord | null {
565
- const best = detectChordBest(pitches);
566
- return best === null ? null : new Chord(best);
567
- }
568
-
569
- /** The root pitch class (0..11). */
570
- get rootPc(): number {
571
- return this.#data.rootPc;
572
- }
573
-
574
- /** The chord quality. */
575
- get quality(): ChordQuality {
576
- return this.#data.quality;
577
- }
578
-
579
- /** A copy of the semitone offsets above the root. */
580
- get intervals(): number[] {
581
- return [...this.#data.intervals];
582
- }
583
-
584
- /** The slash-chord bass pitch class, or undefined in root position. */
585
- get bassPc(): number | undefined {
586
- return this.#data.bassPc;
587
- }
588
-
589
- /** A copy of the underlying plain chord object. */
590
- get data(): ChordData {
591
- return copyChord(this.#data);
592
- }
593
-
594
- /** The carried key context, if any. */
595
- get key(): Key | undefined {
596
- return this.#key;
597
- }
598
-
599
- /**
600
- * A copy of this chord carrying the given key context.
601
- *
602
- * @param key The key context to attach.
603
- * @returns The new chord.
604
- */
605
- withKey(key: Key): Chord {
606
- return new Chord(this.#data, key);
607
- }
608
-
609
- /**
610
- * The chord's sorted, deduplicated pitch classes.
611
- *
612
- * @returns Pitch classes ascending in [0, 11].
613
- */
614
- pitchClasses(): number[] {
615
- return chordPitchClasses(this.#data);
616
- }
617
-
618
- /**
619
- * The chord's Roman numeral in a key.
620
- *
621
- * @param key Key to analyze in; falls back to the carried context.
622
- * @returns The Roman numeral string.
623
- * @throws If no key is given and none is carried.
624
- */
625
- roman(key?: Key): string {
626
- return chordToRoman(this.#data, this.#resolveKey(key).scale);
627
- }
628
-
629
- /**
630
- * The chord's harmonic function (tonic / subdominant / dominant) in a key.
631
- *
632
- * @param key Key to analyze in; falls back to the carried context.
633
- * @returns The harmonic function.
634
- * @throws If no key is given and none is carried.
635
- */
636
- function(key?: Key): HarmonicFunction {
637
- return functionOf(this.#data, this.#resolveKey(key).scale);
638
- }
639
-
640
- /**
641
- * Full functional analysis: function, borrowing, and Roman numeral.
642
- *
643
- * @param key Key to analyze in; falls back to the carried context.
644
- * @returns The chord analysis.
645
- * @throws If no key is given and none is carried.
646
- */
647
- analyze(key?: Key): ChordAnalysis {
648
- return analyzeChord(this.#data, this.#resolveKey(key).scale);
649
- }
650
-
651
- /**
652
- * Whether the chord is borrowed from the parallel mode (modal interchange).
653
- *
654
- * @param key Key to analyze in; falls back to the carried context.
655
- * @returns True if the chord is borrowed.
656
- * @throws If no key is given and none is carried.
657
- */
658
- isBorrowed(key?: Key): boolean {
659
- return isBorrowedChord(this.#data, this.#resolveKey(key).scale);
660
- }
661
-
662
- /**
663
- * The origin of a non-diatonic chord (parallel mode or Neapolitan), or null.
664
- *
665
- * @param key Key to analyze in; falls back to the carried context.
666
- * @returns The borrowing source, or null.
667
- * @throws If no key is given and none is carried.
668
- */
669
- borrowedSource(key?: Key): BorrowedSource {
670
- return borrowedSource(this.#data, this.#resolveKey(key).scale);
671
- }
672
-
673
- /**
674
- * The chord rendered as a lead-sheet symbol (e.g. `'Cmaj7'`, `'F#m7'`, `'C/G'`).
675
- *
676
- * @param opts Set `flats: true` to spell the root/bass with flats.
677
- * @returns The chord symbol.
678
- */
679
- symbol(opts?: { flats?: boolean }): string {
680
- return formatChordSymbol(this.#data, opts);
681
- }
682
-
683
- /**
684
- * Realize the chord as one MIDI pitch per voice, ascending.
685
- *
686
- * @param opts Voicing options; defaults to four SATB voices.
687
- * @returns MIDI pitches, ascending, one per voice.
688
- * @throws If no voicing fits the given ranges.
689
- */
690
- voice(opts?: VoicingOptions): number[] {
691
- return voiceChord(this.#data, opts);
692
- }
693
-
694
- /**
695
- * Realize the chord as a single styled voicing (`close`, `drop2`, `drop3`,
696
- * `shell`, or `rootless`), optionally constraining the top voice.
697
- *
698
- * @param opts Styled-voicing options; defaults to a close-position voicing.
699
- * @returns MIDI pitches, ascending.
700
- */
701
- styledVoicing(opts?: StyledVoicingOptions): number[] {
702
- return voiceChordStyled(this.#data, opts);
703
- }
704
-
705
- /**
706
- * The negative-harmony mirror of the chord about the key's tonic–dominant
707
- * axis (major becomes minor and vice versa).
708
- *
709
- * @param key Key providing the reflection axis; falls back to the carried
710
- * context.
711
- * @returns The mirrored chord, keeping any key context.
712
- * @throws If no key is given and none is carried.
713
- */
714
- negativeHarmony(key?: Key): Chord {
715
- return new Chord(negativeHarmonyMirror(this.#data, this.#resolveKey(key).scale), this.#key);
716
- }
717
-
718
- /**
719
- * The n-th inversion: a copy whose bass is the chord tone `n` steps above the
720
- * root in the interval template (`invert(1)` puts the third in the bass).
721
- * `n` wraps around the template length; negative values count backwards.
722
- *
723
- * @param n The inversion number.
724
- * @returns The inverted chord, keeping any key context.
725
- * @throws If the chord has no intervals.
726
- */
727
- invert(n: number): Chord {
728
- const intervals = this.#data.intervals;
729
- const length = intervals.length;
730
- if (length === 0) {
731
- throw new Error('cannot invert a chord with no intervals');
732
- }
733
- const index = ((n % length) + length) % length;
734
- const data = copyChord(this.#data);
735
- data.bassPc = mod12(this.#data.rootPc + (intervals[index] ?? 0));
736
- return new Chord(data, this.#key);
737
- }
738
-
739
- /**
740
- * The named scales that fit over this chord, best fit first, rooted on the
741
- * chord root.
742
- *
743
- * @returns The matching scales.
744
- */
745
- scales(): ChordScaleMatch[] {
746
- return chordScales(this.#data);
747
- }
748
-
749
- /**
750
- * The available tensions (usable non-chord, non-avoid scale tones) of a
751
- * scale over this chord.
752
- *
753
- * @param scaleName A named scale, rooted on the chord root.
754
- * @returns Tension pitch classes, ascending in [0, 11].
755
- */
756
- tensions(scaleName: string): number[] {
757
- return availableTensions(this.#data, scaleName);
758
- }
759
-
760
- /**
761
- * The avoid notes (scale tones a semitone above a chord tone) of a scale
762
- * over this chord.
763
- *
764
- * @param scaleName A named scale, rooted on the chord root.
765
- * @returns Avoid-note pitch classes, ascending in [0, 11].
766
- */
767
- avoidNotes(scaleName: string): number[] {
768
- return avoidNotes(this.#data, scaleName);
769
- }
770
-
771
- /**
772
- * Spell the chord tones with letter names, root first, in the key's spelling.
773
- *
774
- * @param key Key providing the spelled tonic; falls back to the carried
775
- * context.
776
- * @returns Spelled octave-less notes in the chord's own (tertian) order.
777
- * @throws If no key is given and none is carried.
778
- */
779
- spell(key?: Key): Note[] {
780
- const resolved = this.#resolveKey(key);
781
- return spellChord(this.#data, resolved.tonic.data, resolved.scale).map(
782
- (note) => new Note(note),
783
- );
784
- }
785
-
786
- /**
787
- * Start a progression with this chord followed by others, carrying this
788
- * chord's key context (if any).
789
- *
790
- * @param others The chords following this one.
791
- * @returns The progression.
792
- */
793
- progressionTo(...others: Chord[]): Progression {
794
- return new Progression([this, ...others], this.#key);
795
- }
796
-
797
- /**
798
- * Whether another chord has the same root, quality, intervals, and bass.
799
- * Key context is not compared.
800
- *
801
- * @param other The chord to compare.
802
- * @returns True if the chord data is identical.
803
- */
804
- equals(other: Chord): boolean {
805
- const a = this.#data;
806
- const b = other.#data;
807
- return (
808
- a.rootPc === b.rootPc &&
809
- a.quality === b.quality &&
810
- a.bassPc === b.bassPc &&
811
- a.intervals.length === b.intervals.length &&
812
- a.intervals.every((interval, i) => interval === b.intervals[i])
813
- );
814
- }
815
-
816
- /**
817
- * The plain chord data, for JSON serialization.
818
- *
819
- * @returns A copy of the underlying plain chord object.
820
- */
821
- toJSON(): ChordData {
822
- return this.data;
823
- }
824
-
825
- /** Resolve the key for an analysis method: explicit first, then carried. */
826
- #resolveKey(key?: Key): Key {
827
- const resolved = key ?? this.#key;
828
- if (resolved === undefined) {
829
- throw new Error('chord has no key context; pass a Key or attach one with withKey()');
830
- }
831
- return resolved;
832
- }
833
- }
834
-
835
- /**
836
- * An immutable ordered sequence of chords, optionally carrying a {@link Key}
837
- * context shared by its analysis methods.
838
- */
839
- export class Progression {
840
- readonly #chords: readonly Chord[];
841
- readonly #key: Key | undefined;
842
-
843
- /**
844
- * Wrap a chord sequence.
845
- *
846
- * @param chords The chords in order; the array is copied.
847
- * @param key Optional key context for analysis methods.
848
- */
849
- constructor(chords: readonly Chord[], key?: Key) {
850
- this.#chords = [...chords];
851
- this.#key = key;
852
- }
853
-
854
- /** A copy of the chord sequence. */
855
- get chords(): readonly Chord[] {
856
- return [...this.#chords];
857
- }
858
-
859
- /** The carried key context, if any. */
860
- get key(): Key | undefined {
861
- return this.#key;
862
- }
863
-
864
- /** The number of chords. */
865
- get length(): number {
866
- return this.#chords.length;
867
- }
868
-
869
- /**
870
- * A copy of this progression with a chord appended.
871
- *
872
- * @param chord The chord to append.
873
- * @returns The new progression.
874
- */
875
- add(chord: Chord): Progression {
876
- return new Progression([...this.#chords, chord], this.#key);
877
- }
878
-
879
- /**
880
- * A copy of this progression carrying the given key context.
881
- *
882
- * @param key The key context to attach.
883
- * @returns The new progression.
884
- */
885
- withKey(key: Key): Progression {
886
- return new Progression(this.#chords, key);
887
- }
888
-
889
- /**
890
- * Voice the progression with smooth voice leading.
891
- *
892
- * @param opts Voicing options; defaults to four SATB voices.
893
- * @returns One ascending voicing (MIDI pitches) per chord.
894
- * @throws If any chord admits no voicing within the given ranges.
895
- */
896
- voice(opts?: VoicingOptions): number[][] {
897
- return voiceProgression(
898
- this.#chords.map((chord) => chord.data),
899
- opts,
900
- );
901
- }
902
-
903
- /**
904
- * The Roman numeral of each chord in a key.
905
- *
906
- * @param key Key to analyze in; falls back to the carried context.
907
- * @returns One numeral per chord.
908
- * @throws If no key is given and none is carried.
909
- */
910
- roman(key?: Key): string[] {
911
- const resolved = this.#resolveKey(key);
912
- return this.#chords.map((chord) => chord.roman(resolved));
913
- }
914
-
915
- /**
916
- * The harmonic function of each chord in a key.
917
- *
918
- * @param key Key to analyze in; falls back to the carried context.
919
- * @returns One function per chord.
920
- * @throws If no key is given and none is carried.
921
- */
922
- functions(key?: Key): HarmonicFunction[] {
923
- const resolved = this.#resolveKey(key);
924
- return this.#chords.map((chord) => chord.function(resolved));
925
- }
926
-
927
- /**
928
- * Analyze every chord and classify the closing cadence.
929
- *
930
- * The cadence is detected on the final chord pair and is null when the
931
- * progression has fewer than two chords.
932
- *
933
- * @param key Key to analyze in; falls back to the carried context.
934
- * @returns Per-chord analyses and the closing cadence.
935
- * @throws If no key is given and none is carried.
936
- */
937
- analyze(key?: Key): { chords: ChordAnalysis[]; cadence: Cadence } {
938
- const resolved = this.#resolveKey(key);
939
- const chords = this.#chords.map((chord) => chord.analyze(resolved));
940
- const from = this.#chords[this.#chords.length - 2];
941
- const to = this.#chords[this.#chords.length - 1];
942
- const cadence =
943
- from !== undefined && to !== undefined
944
- ? detectCadence(from.data, to.data, resolved.scale)
945
- : null;
946
- return { chords, cadence };
947
- }
948
-
949
- /** Resolve the key for an analysis method: explicit first, then carried. */
950
- #resolveKey(key?: Key): Key {
951
- const resolved = key ?? this.#key;
952
- if (resolved === undefined) {
953
- throw new Error('progression has no key context; pass a Key or attach one with withKey()');
954
- }
955
- return resolved;
956
- }
957
- }
11
+ export { Chord } from './chord.js';
12
+ export { Interval } from './interval.js';
13
+ export { Key } from './key.js';
14
+ export { Note } from './note.js';
15
+ export { Progression } from './progression.js';