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