@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,423 +0,0 @@
1
- import { detectChord, detectChordBest } from '../analyze/detect/index.js';
2
- import {
3
- analyzeChord,
4
- type BorrowedSource,
5
- borrowedSource,
6
- type ChordAnalysis,
7
- chordToRoman,
8
- functionOf,
9
- type HarmonicFunction,
10
- isBorrowedChord,
11
- } from '../analyze/functional/index.js';
12
- import { negativeHarmonyMirror } from '../generate/reharmony/index.js';
13
- import {
14
- type Chord as ChordData,
15
- type ChordQuality,
16
- chordPitchClasses,
17
- makeChord,
18
- } from '../theory/chord/index.js';
19
- import {
20
- availableTensions,
21
- avoidNotes,
22
- type ChordScaleMatch,
23
- chordScales,
24
- } from '../theory/chordscale/index.js';
25
- import { spellChord } from '../theory/spelling/index.js';
26
- import { formatChordSymbol, parseChordSymbol } from '../theory/symbol/index.js';
27
- import {
28
- type StyledVoicingOptions,
29
- type VoicingOptions,
30
- voiceChord,
31
- voiceChordStyled,
32
- } from '../theory/voicing/index.js';
33
- import type { Key } from './key.js';
34
- import { Note } from './note.js';
35
- import { Progression } from './progression.js';
36
- import { mod12 } from './shared.js';
37
-
38
- /**
39
- * Defensive copy of a plain chord.
40
- *
41
- * Any enharmonic spelling hints (`rootSpelling`/`bassSpelling`, populated by
42
- * `parseChordSymbol`) are carried through so a flat-named chord round-trips
43
- * through the class API; a hint that no longer matches its pitch class is
44
- * simply ignored by the formatter.
45
- */
46
- function copyChord(data: ChordData): ChordData {
47
- const copy: ChordData = {
48
- rootPc: data.rootPc,
49
- quality: data.quality,
50
- intervals: [...data.intervals],
51
- };
52
- if (data.bassPc !== undefined) {
53
- copy.bassPc = data.bassPc;
54
- }
55
- if (data.rootSpelling !== undefined) {
56
- copy.rootSpelling = { letter: data.rootSpelling.letter, alter: data.rootSpelling.alter };
57
- }
58
- if (data.bassSpelling !== undefined) {
59
- copy.bassSpelling = { letter: data.bassSpelling.letter, alter: data.bassSpelling.alter };
60
- }
61
- return copy;
62
- }
63
-
64
- /**
65
- * An immutable chord: a root pitch class, quality, interval template, and
66
- * optional slash bass, optionally carrying a {@link Key} context. Analysis
67
- * methods (`roman`, `function`, `analyze`, ...) use an explicitly passed key
68
- * first and fall back to the carried context.
69
- *
70
- * @category Class API
71
- * @example
72
- * ```ts
73
- * import { Chord } from '@libraz/libcantus';
74
- * Chord.parse('Cmaj7').invert(1).symbol(); // 'Cmaj7/E' (third in the bass)
75
- * ```
76
- */
77
- export class Chord {
78
- readonly #data: ChordData;
79
- readonly #key: Key | undefined;
80
-
81
- /**
82
- * Wrap a plain chord object.
83
- *
84
- * @param data The chord; it is copied, never retained or mutated.
85
- * @param key Optional key context for analysis methods.
86
- */
87
- constructor(data: ChordData, key?: Key) {
88
- this.#data = copyChord(data);
89
- this.#key = key;
90
- }
91
-
92
- /**
93
- * Build a chord from a root and quality.
94
- *
95
- * @param root Root as a note name (e.g. `'Eb'`) or a pitch class.
96
- * @param quality The chord quality.
97
- * @param bass Optional slash-chord bass pitch class.
98
- * @returns The chord (without key context).
99
- */
100
- static of(root: string | number, quality: ChordQuality, bass?: number): Chord {
101
- const rootPc = typeof root === 'string' ? Note.of(root).pitchClass : root;
102
- return new Chord(makeChord(rootPc, quality, bass));
103
- }
104
-
105
- /**
106
- * Wrap an existing plain chord object.
107
- *
108
- * @param data The plain chord.
109
- * @returns The wrapped chord (without key context).
110
- */
111
- static from(data: ChordData): Chord {
112
- return new Chord(data);
113
- }
114
-
115
- /**
116
- * Parse a lead-sheet chord symbol (e.g. `'Cmaj7'`, `'F#m7b5'`, `'C/G'`).
117
- *
118
- * @param symbol The chord symbol.
119
- * @returns The chord (without key context).
120
- * @throws If the root or quality is not recognized.
121
- */
122
- static parse(symbol: string): Chord {
123
- return new Chord(parseChordSymbol(symbol));
124
- }
125
-
126
- /**
127
- * Identify the chords matching a set of pitches, best interpretation first.
128
- *
129
- * @param pitches MIDI pitches or bare pitch classes.
130
- * @returns Ranked chord interpretations (may be empty).
131
- */
132
- static detect(pitches: number[]): Chord[] {
133
- return detectChord(pitches).map(
134
- (match) => new Chord(makeChord(match.rootPc, match.quality, match.bassPc)),
135
- );
136
- }
137
-
138
- /**
139
- * The single best chord interpretation of a pitch set.
140
- *
141
- * @param pitches MIDI pitches or bare pitch classes.
142
- * @returns The top-ranked chord, or null when nothing matches.
143
- */
144
- static detectBest(pitches: number[]): Chord | null {
145
- const best = detectChordBest(pitches);
146
- return best === null ? null : new Chord(best);
147
- }
148
-
149
- /** The root pitch class (0..11). */
150
- get rootPc(): number {
151
- return this.#data.rootPc;
152
- }
153
-
154
- /** The chord quality. */
155
- get quality(): ChordQuality {
156
- return this.#data.quality;
157
- }
158
-
159
- /** A copy of the semitone offsets above the root. */
160
- get intervals(): number[] {
161
- return [...this.#data.intervals];
162
- }
163
-
164
- /** The slash-chord bass pitch class, or undefined in root position. */
165
- get bassPc(): number | undefined {
166
- return this.#data.bassPc;
167
- }
168
-
169
- /** A copy of the underlying plain chord object. */
170
- get data(): ChordData {
171
- return copyChord(this.#data);
172
- }
173
-
174
- /** The carried key context, if any. */
175
- get key(): Key | undefined {
176
- return this.#key;
177
- }
178
-
179
- /**
180
- * A copy of this chord carrying the given key context.
181
- *
182
- * @param key The key context to attach.
183
- * @returns The new chord.
184
- */
185
- withKey(key: Key): Chord {
186
- return new Chord(this.#data, key);
187
- }
188
-
189
- /**
190
- * The chord's sorted, deduplicated pitch classes.
191
- *
192
- * @returns Pitch classes ascending in [0, 11].
193
- */
194
- pitchClasses(): number[] {
195
- return chordPitchClasses(this.#data);
196
- }
197
-
198
- /**
199
- * The chord's Roman numeral in a key.
200
- *
201
- * @param key Key to analyze in; falls back to the carried context.
202
- * @returns The Roman numeral string.
203
- * @throws If no key is given and none is carried.
204
- */
205
- roman(key?: Key): string {
206
- return chordToRoman(this.#data, this.#resolveKey(key).scale);
207
- }
208
-
209
- /**
210
- * The chord's harmonic function (tonic / subdominant / dominant) in a key.
211
- *
212
- * @param key Key to analyze in; falls back to the carried context.
213
- * @returns The harmonic function.
214
- * @throws If no key is given and none is carried.
215
- */
216
- function(key?: Key): HarmonicFunction {
217
- return functionOf(this.#data, this.#resolveKey(key).scale);
218
- }
219
-
220
- /**
221
- * Full functional analysis: function, borrowing, and Roman numeral.
222
- *
223
- * @param key Key to analyze in; falls back to the carried context.
224
- * @returns The chord analysis.
225
- * @throws If no key is given and none is carried.
226
- */
227
- analyze(key?: Key): ChordAnalysis {
228
- return analyzeChord(this.#data, this.#resolveKey(key).scale);
229
- }
230
-
231
- /**
232
- * Whether the chord is borrowed from the parallel mode (modal interchange).
233
- *
234
- * @param key Key to analyze in; falls back to the carried context.
235
- * @returns True if the chord is borrowed.
236
- * @throws If no key is given and none is carried.
237
- */
238
- isBorrowed(key?: Key): boolean {
239
- return isBorrowedChord(this.#data, this.#resolveKey(key).scale);
240
- }
241
-
242
- /**
243
- * The origin of a non-diatonic chord (parallel mode or Neapolitan), or null.
244
- *
245
- * @param key Key to analyze in; falls back to the carried context.
246
- * @returns The borrowing source, or null.
247
- * @throws If no key is given and none is carried.
248
- */
249
- borrowedSource(key?: Key): BorrowedSource {
250
- return borrowedSource(this.#data, this.#resolveKey(key).scale);
251
- }
252
-
253
- /**
254
- * The chord rendered as a lead-sheet symbol (e.g. `'Cmaj7'`, `'F#m7'`, `'C/G'`).
255
- *
256
- * @param opts Set `flats: true` to spell the root/bass with flats.
257
- * @returns The chord symbol.
258
- */
259
- symbol(opts?: { flats?: boolean }): string {
260
- return formatChordSymbol(this.#data, opts);
261
- }
262
-
263
- /**
264
- * Realize the chord as one MIDI pitch per voice, ascending.
265
- *
266
- * @param opts Voicing options; defaults to four SATB voices.
267
- * @returns MIDI pitches, ascending, one per voice.
268
- * @throws If no voicing fits the given ranges.
269
- */
270
- voice(opts?: VoicingOptions): number[] {
271
- return voiceChord(this.#data, opts);
272
- }
273
-
274
- /**
275
- * Realize the chord as a single styled voicing (`close`, `drop2`, `drop3`,
276
- * `shell`, or `rootless`), optionally constraining the top voice.
277
- *
278
- * @param opts Styled-voicing options; defaults to a close-position voicing.
279
- * @returns MIDI pitches, ascending.
280
- */
281
- styledVoicing(opts?: StyledVoicingOptions): number[] {
282
- return voiceChordStyled(this.#data, opts);
283
- }
284
-
285
- /**
286
- * The negative-harmony mirror of the chord about the key's tonic–dominant
287
- * axis (major becomes minor and vice versa).
288
- *
289
- * @param key Key providing the reflection axis; falls back to the carried
290
- * context.
291
- * @returns The mirrored chord, keeping any key context.
292
- * @throws If no key is given and none is carried.
293
- */
294
- negativeHarmony(key?: Key): Chord {
295
- const resolved = this.#resolveKey(key);
296
- // Retain the key that anchored the reflection (explicit first, then carried)
297
- // so a later no-arg analysis method still has a key context.
298
- return new Chord(negativeHarmonyMirror(this.#data, resolved.scale), key ?? this.#key);
299
- }
300
-
301
- /**
302
- * The n-th inversion: a copy whose bass is the chord tone `n` steps above the
303
- * root in the interval template (`invert(1)` puts the third in the bass).
304
- * `n` wraps around the template length; negative values count backwards.
305
- *
306
- * `invert(0)` (and any `n` that wraps to it) is root position, so it carries
307
- * no slash bass and equals the original chord.
308
- *
309
- * @param n The inversion number.
310
- * @returns The inverted chord, keeping any key context.
311
- * @throws If the chord has no intervals.
312
- */
313
- invert(n: number): Chord {
314
- const intervals = this.#data.intervals;
315
- const length = intervals.length;
316
- if (length === 0) {
317
- throw new Error('cannot invert a chord with no intervals');
318
- }
319
- const index = ((n % length) + length) % length;
320
- const data = copyChord(this.#data);
321
- if (index === 0) {
322
- delete data.bassPc;
323
- } else {
324
- data.bassPc = mod12(this.#data.rootPc + (intervals[index] ?? 0));
325
- }
326
- return new Chord(data, this.#key);
327
- }
328
-
329
- /**
330
- * The named scales that fit over this chord, best fit first, rooted on the
331
- * chord root.
332
- *
333
- * @returns The matching scales.
334
- */
335
- scales(): ChordScaleMatch[] {
336
- return chordScales(this.#data);
337
- }
338
-
339
- /**
340
- * The available tensions (usable non-chord, non-avoid scale tones) of a
341
- * scale over this chord.
342
- *
343
- * @param scaleName A named scale, rooted on the chord root.
344
- * @returns Tension pitch classes, ascending in [0, 11].
345
- */
346
- tensions(scaleName: string): number[] {
347
- return availableTensions(this.#data, scaleName);
348
- }
349
-
350
- /**
351
- * The avoid notes (scale tones a semitone above a chord tone) of a scale
352
- * over this chord.
353
- *
354
- * @param scaleName A named scale, rooted on the chord root.
355
- * @returns Avoid-note pitch classes, ascending in [0, 11].
356
- */
357
- avoidNotes(scaleName: string): number[] {
358
- return avoidNotes(this.#data, scaleName);
359
- }
360
-
361
- /**
362
- * Spell the chord tones with letter names, root first, in the key's spelling.
363
- *
364
- * @param key Key providing the spelled tonic; falls back to the carried
365
- * context.
366
- * @returns Spelled octave-less notes in the chord's own (tertian) order.
367
- * @throws If no key is given and none is carried.
368
- */
369
- spell(key?: Key): Note[] {
370
- const resolved = this.#resolveKey(key);
371
- return spellChord(this.#data, resolved.tonic.data, resolved.scale).map(
372
- (note) => new Note(note),
373
- );
374
- }
375
-
376
- /**
377
- * Start a progression with this chord followed by others, carrying this
378
- * chord's key context (if any).
379
- *
380
- * @param others The chords following this one.
381
- * @returns The progression.
382
- */
383
- progressionTo(...others: Chord[]): Progression {
384
- return new Progression([this, ...others], this.#key);
385
- }
386
-
387
- /**
388
- * Whether another chord has the same root, quality, intervals, and bass.
389
- * Key context is not compared.
390
- *
391
- * @param other The chord to compare.
392
- * @returns True if the chord data is identical.
393
- */
394
- equals(other: Chord): boolean {
395
- const a = this.#data;
396
- const b = other.#data;
397
- return (
398
- a.rootPc === b.rootPc &&
399
- a.quality === b.quality &&
400
- a.bassPc === b.bassPc &&
401
- a.intervals.length === b.intervals.length &&
402
- a.intervals.every((interval, i) => interval === b.intervals[i])
403
- );
404
- }
405
-
406
- /**
407
- * The plain chord data, for JSON serialization.
408
- *
409
- * @returns A copy of the underlying plain chord object.
410
- */
411
- toJSON(): ChordData {
412
- return this.data;
413
- }
414
-
415
- /** Resolve the key for an analysis method: explicit first, then carried. */
416
- #resolveKey(key?: Key): Key {
417
- const resolved = key ?? this.#key;
418
- if (resolved === undefined) {
419
- throw new Error('chord has no key context; pass a Key or attach one with withKey()');
420
- }
421
- return resolved;
422
- }
423
- }
@@ -1,15 +0,0 @@
1
- /**
2
- * Fluent, immutable object model over the functional core.
3
- *
4
- * Each class wraps one of the library's plain data types (spelled notes,
5
- * chords, key/scales) and delegates every operation to the existing pure
6
- * functions. All instances are immutable: fields are read-only, transforming
7
- * methods return new instances, and getters hand out defensive copies of any
8
- * mutable data.
9
- */
10
-
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';
@@ -1,82 +0,0 @@
1
- import type { SpelledInterval } from '../core/pitch/index.js';
2
- import type { Note } from './note.js';
3
-
4
- /**
5
- * An immutable spelled interval value: a diatonic number, a quality label, and
6
- * a signed semitone span. A thin convenience wrapper over the pitch module's
7
- * plain interval result.
8
- *
9
- * @category Class API
10
- * @example
11
- * ```ts
12
- * import { Interval, Note } from '@libraz/libcantus';
13
- * Interval.between(Note.of('C4'), Note.of('G4')).name; // 'P5'
14
- * ```
15
- */
16
- export class Interval {
17
- readonly #number: number;
18
- readonly #quality: string;
19
- readonly #semitones: number;
20
-
21
- private constructor(numberValue: number, quality: string, semitones: number) {
22
- this.#number = numberValue;
23
- this.#quality = quality;
24
- this.#semitones = semitones;
25
- }
26
-
27
- /**
28
- * The spelled interval between two notes.
29
- *
30
- * @param a The first note.
31
- * @param b The second note.
32
- * @returns The interval from `a` to `b`.
33
- */
34
- static between(a: Note, b: Note): Interval {
35
- const spelled = a.intervalTo(b);
36
- return new Interval(spelled.number, spelled.quality, spelled.semitones);
37
- }
38
-
39
- /**
40
- * Build an interval from explicit components.
41
- *
42
- * @param numberValue Diatonic size: 1 = unison, 2 = second, ... 8 = octave.
43
- * @param quality Quality label: `'P'`, `'M'`, `'m'`, or repeated `'A'`/`'d'`.
44
- * @param semitones Signed semitone span.
45
- * @returns The interval.
46
- */
47
- static of(numberValue: number, quality: string, semitones: number): Interval {
48
- return new Interval(numberValue, quality, semitones);
49
- }
50
-
51
- /** Diatonic size: 1 = unison, 2 = second, ... 8 = octave, and beyond. */
52
- get number(): number {
53
- return this.#number;
54
- }
55
-
56
- /** Quality label: `'P'`, `'M'`, `'m'`, or repeated `'A'`/`'d'`. */
57
- get quality(): string {
58
- return this.#quality;
59
- }
60
-
61
- /** Signed semitone span. */
62
- get semitones(): number {
63
- return this.#semitones;
64
- }
65
-
66
- /** A readable label composed of quality and number, e.g. `'P5'` or `'M3'`. */
67
- get name(): string {
68
- return `${this.#quality}${this.#number}`;
69
- }
70
-
71
- /**
72
- * The plain interval data, for JSON serialization.
73
- *
74
- * Private class fields do not serialize, so an explicit `toJSON` keeps
75
- * `JSON.stringify(interval)` from collapsing to `{}`.
76
- *
77
- * @returns The diatonic number, quality, and semitone span.
78
- */
79
- toJSON(): SpelledInterval {
80
- return { number: this.#number, quality: this.#quality, semitones: this.#semitones };
81
- }
82
- }