@libraz/libcantus 0.9.3 → 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 (198) hide show
  1. package/README.md +2 -2
  2. package/dist/analyze/index.cjs +39 -1947
  3. package/dist/analyze/index.cjs.map +1 -1
  4. package/dist/analyze/index.d.cts +197 -37
  5. package/dist/analyze/index.d.ts +197 -37
  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-4MHA432G.js → chunk-7NKDPF74.js} +403 -180
  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-MIWO425M.js → chunk-D2FAJBYL.js} +101 -34
  18. package/dist/chunk-D2FAJBYL.js.map +1 -0
  19. package/dist/{chunk-SGU7SPLK.js → chunk-EHVEBWM5.js} +1056 -531
  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-IT2O4XFE.js → chunk-GQSGS2N4.js} +43 -24
  26. package/dist/chunk-GQSGS2N4.js.map +1 -0
  27. package/dist/{chunk-62QZKCJQ.js → chunk-HKGYCQ34.js} +10 -6
  28. package/dist/chunk-HKGYCQ34.js.map +1 -0
  29. package/dist/{chunk-AKLYPWIN.js → chunk-I24MV65U.js} +29 -6
  30. package/dist/chunk-I24MV65U.js.map +1 -0
  31. package/dist/{chunk-CHMXELDS.js → chunk-KMHOICIY.js} +311 -181
  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-2ZWMSJH6.js → chunk-NKIBQ6PT.js} +2 -2
  36. package/dist/chunk-NKIBQ6PT.js.map +1 -0
  37. package/dist/chunk-NP4NA2FB.cjs +106 -0
  38. package/dist/chunk-NP4NA2FB.cjs.map +1 -0
  39. package/dist/chunk-NVE34YGZ.cjs +1449 -0
  40. package/dist/chunk-NVE34YGZ.cjs.map +1 -0
  41. package/dist/{chunk-EH6EM37U.js → chunk-NYYWMV2C.js} +61 -49
  42. package/dist/chunk-NYYWMV2C.js.map +1 -0
  43. package/dist/{chunk-GOAW4VYL.js → chunk-Q2XUP227.js} +243 -117
  44. package/dist/chunk-Q2XUP227.js.map +1 -0
  45. package/dist/chunk-Q6OP4U43.cjs +327 -0
  46. package/dist/chunk-Q6OP4U43.cjs.map +1 -0
  47. package/dist/chunk-RC6GBMCD.cjs +845 -0
  48. package/dist/chunk-RC6GBMCD.cjs.map +1 -0
  49. package/dist/{chunk-C5XYKUVG.js → chunk-RK35RIN7.js} +370 -157
  50. package/dist/chunk-RK35RIN7.js.map +1 -0
  51. package/dist/chunk-RUMU3UA6.cjs +53 -0
  52. package/dist/chunk-RUMU3UA6.cjs.map +1 -0
  53. package/dist/chunk-T64C7ZD7.cjs +1 -0
  54. package/dist/chunk-T64C7ZD7.cjs.map +1 -0
  55. package/dist/chunk-XEBG3G53.js +498 -0
  56. package/dist/chunk-XEBG3G53.js.map +1 -0
  57. package/dist/{chunk-I5R7ZBJS.js → chunk-YLEMGEFH.js} +103 -59
  58. package/dist/chunk-YLEMGEFH.js.map +1 -0
  59. package/dist/chunk-ZEJR4TKL.cjs +173 -0
  60. package/dist/chunk-ZEJR4TKL.cjs.map +1 -0
  61. package/dist/core/index.cjs +130 -647
  62. package/dist/core/index.cjs.map +1 -1
  63. package/dist/core/index.d.cts +301 -39
  64. package/dist/core/index.d.ts +301 -39
  65. package/dist/core/index.js +54 -16
  66. package/dist/{function-DYt4iKsn.d.cts → function-BtiZb9i9.d.ts} +28 -7
  67. package/dist/{function-DVH4Eusr.d.ts → function-DNugAOrt.d.cts} +28 -7
  68. package/dist/generate/index.cjs +28 -4085
  69. package/dist/generate/index.cjs.map +1 -1
  70. package/dist/generate/index.d.cts +370 -62
  71. package/dist/generate/index.d.ts +370 -62
  72. package/dist/generate/index.js +23 -9
  73. package/dist/{index-CxqBQtH1.d.cts → index-6VR8-kit.d.ts} +19 -4
  74. package/dist/index-70p168VM.d.ts +192 -0
  75. package/dist/{index-CpOsP1y9.d.cts → index-Be74mcon.d.cts} +18 -17
  76. package/dist/index-BtXT6UQ0.d.cts +192 -0
  77. package/dist/{index-DxvARIP2.d.ts → index-C5UBbQ4G.d.ts} +18 -17
  78. package/dist/{index-Bt4tOUD9.d.ts → index-C83mZI1P.d.ts} +93 -8
  79. package/dist/{index-Bg5dYMmZ.d.ts → index-D6b6na69.d.cts} +14 -11
  80. package/dist/{index-Cl7mhOFM.d.cts → index-DXd9JikN.d.cts} +92 -10
  81. package/dist/{index-Cl7mhOFM.d.ts → index-DXd9JikN.d.ts} +92 -10
  82. package/dist/index-OG65ocOr.d.cts +272 -0
  83. package/dist/index-OG65ocOr.d.ts +272 -0
  84. package/dist/{index-Jc0Ryi79.d.ts → index-tJhkmQgE.d.cts} +19 -4
  85. package/dist/{index-Dikst44v.d.cts → index-tSQh-xwY.d.ts} +14 -11
  86. package/dist/{index-BNlGP86A.d.cts → index-y_ak9_Hi.d.cts} +93 -8
  87. package/dist/index.cjs +419 -7383
  88. package/dist/index.cjs.map +1 -1
  89. package/dist/index.d.cts +14 -14
  90. package/dist/index.d.ts +14 -14
  91. package/dist/index.js +111 -27
  92. package/dist/model/index.cjs +8 -2562
  93. package/dist/model/index.cjs.map +1 -1
  94. package/dist/model/index.d.cts +433 -75
  95. package/dist/model/index.d.ts +433 -75
  96. package/dist/model/index.js +6 -7
  97. package/dist/styled-DIRkk9Uz.d.ts +506 -0
  98. package/dist/styled-DJbkJQfj.d.cts +506 -0
  99. package/dist/theory/index.cjs +172 -1822
  100. package/dist/theory/index.cjs.map +1 -1
  101. package/dist/theory/index.d.cts +151 -233
  102. package/dist/theory/index.d.ts +151 -233
  103. package/dist/theory/index.js +30 -7
  104. package/dist/{types-MGP29r3t.d.cts → types-BN6_Q9Kl.d.cts} +11 -2
  105. package/dist/{types-MGP29r3t.d.ts → types-BN6_Q9Kl.d.ts} +11 -2
  106. package/package.json +24 -6
  107. package/dist/chunk-2ZWMSJH6.js.map +0 -1
  108. package/dist/chunk-4MHA432G.js.map +0 -1
  109. package/dist/chunk-62QZKCJQ.js.map +0 -1
  110. package/dist/chunk-AKLYPWIN.js.map +0 -1
  111. package/dist/chunk-C5XYKUVG.js.map +0 -1
  112. package/dist/chunk-CHMXELDS.js.map +0 -1
  113. package/dist/chunk-DYWFNC4A.js +0 -152
  114. package/dist/chunk-DYWFNC4A.js.map +0 -1
  115. package/dist/chunk-EH6EM37U.js.map +0 -1
  116. package/dist/chunk-FBQK3ZQN.js +0 -131
  117. package/dist/chunk-FBQK3ZQN.js.map +0 -1
  118. package/dist/chunk-GOAW4VYL.js.map +0 -1
  119. package/dist/chunk-I5R7ZBJS.js.map +0 -1
  120. package/dist/chunk-IT2O4XFE.js.map +0 -1
  121. package/dist/chunk-MIWO425M.js.map +0 -1
  122. package/dist/chunk-SGU7SPLK.js.map +0 -1
  123. package/dist/index-CGMZNCx6.d.cts +0 -140
  124. package/dist/index-CGMZNCx6.d.ts +0 -140
  125. package/dist/index-CnZCo29u.d.cts +0 -108
  126. package/dist/index-JnF4v9bx.d.ts +0 -108
  127. package/dist/styled-DZ7GjShb.d.ts +0 -282
  128. package/dist/styled-ZTbdaVFM.d.cts +0 -282
  129. package/src/analyze/arrange/index.ts +0 -21
  130. package/src/analyze/arrange/internal.ts +0 -127
  131. package/src/analyze/arrange/tension.ts +0 -199
  132. package/src/analyze/arrange/tracks.ts +0 -312
  133. package/src/analyze/detect/index.ts +0 -272
  134. package/src/analyze/functional/borrowed.ts +0 -84
  135. package/src/analyze/functional/cadence.ts +0 -66
  136. package/src/analyze/functional/function.ts +0 -184
  137. package/src/analyze/functional/index.ts +0 -23
  138. package/src/analyze/functional/internal.ts +0 -29
  139. package/src/analyze/functional/roman.ts +0 -402
  140. package/src/analyze/index.ts +0 -49
  141. package/src/analyze/timeline/index.ts +0 -451
  142. package/src/analyze/voice/index.ts +0 -339
  143. package/src/core/event-index/index.ts +0 -104
  144. package/src/core/index.ts +0 -61
  145. package/src/core/interval/index.ts +0 -81
  146. package/src/core/meter/index.ts +0 -302
  147. package/src/core/pitch/index.ts +0 -311
  148. package/src/core/random/index.ts +0 -76
  149. package/src/core/tuning/index.ts +0 -186
  150. package/src/core/types.ts +0 -29
  151. package/src/core/validation/index.ts +0 -119
  152. package/src/generate/bass/index.ts +0 -373
  153. package/src/generate/countermelody/index.ts +0 -470
  154. package/src/generate/drums/beat.ts +0 -266
  155. package/src/generate/drums/euclid.ts +0 -65
  156. package/src/generate/drums/fills.ts +0 -356
  157. package/src/generate/drums/ghost.ts +0 -119
  158. package/src/generate/drums/hihat.ts +0 -285
  159. package/src/generate/drums/hit.ts +0 -31
  160. package/src/generate/drums/index.ts +0 -361
  161. package/src/generate/drums/internal.ts +0 -257
  162. package/src/generate/drums/kick.ts +0 -160
  163. package/src/generate/drums/percussion.ts +0 -121
  164. package/src/generate/drums/rng.ts +0 -7
  165. package/src/generate/drums/swing.ts +0 -71
  166. package/src/generate/groove/index.ts +0 -351
  167. package/src/generate/harmonize/index.ts +0 -451
  168. package/src/generate/index.ts +0 -58
  169. package/src/generate/motif/index.ts +0 -422
  170. package/src/generate/progression/index.ts +0 -362
  171. package/src/generate/reharmony/index.ts +0 -323
  172. package/src/generate/rhythm/index.ts +0 -175
  173. package/src/index.ts +0 -13
  174. package/src/model/chord.ts +0 -442
  175. package/src/model/index.ts +0 -15
  176. package/src/model/interval.ts +0 -82
  177. package/src/model/key.ts +0 -266
  178. package/src/model/note.ts +0 -176
  179. package/src/model/progression.ts +0 -161
  180. package/src/model/shared.ts +0 -12
  181. package/src/theory/chord/index.ts +0 -375
  182. package/src/theory/chordscale/index.ts +0 -374
  183. package/src/theory/counterpoint/index.ts +0 -230
  184. package/src/theory/harmony/index.ts +0 -77
  185. package/src/theory/index.ts +0 -112
  186. package/src/theory/safety/index.ts +0 -488
  187. package/src/theory/scale/degrees.ts +0 -123
  188. package/src/theory/scale/index.ts +0 -28
  189. package/src/theory/scale/key.ts +0 -56
  190. package/src/theory/scale/masks.ts +0 -153
  191. package/src/theory/spelling/index.ts +0 -249
  192. package/src/theory/symbol/index.ts +0 -268
  193. package/src/theory/voicing/index.ts +0 -5
  194. package/src/theory/voicing/internal.ts +0 -168
  195. package/src/theory/voicing/leading.ts +0 -112
  196. package/src/theory/voicing/satb.ts +0 -216
  197. package/src/theory/voicing/styled.ts +0 -199
  198. package/tsconfig.json +0 -23
@@ -1,362 +0,0 @@
1
- import { createRng } from '../../core/random/index.js';
2
- import type { KeyScale } from '../../core/types.js';
3
- import { assertGenerationBudget, assertPositiveInt } from '../../core/validation/index.js';
4
- import type { ChordQuality, ChordSpan } from '../../theory/chord/index.js';
5
- import { diatonicTriad } from '../../theory/chord/index.js';
6
- import { scaleTonesInDegreeOrder } from '../../theory/scale/index.js';
7
-
8
- export type { ChordSpan } from '../../theory/chord/index.js';
9
-
10
- /**
11
- * Broad production style a progression preset suits.
12
- *
13
- * @category Composition
14
- */
15
- export type ProgStyle = 'minimal' | 'dance' | 'idol' | 'rock';
16
-
17
- /**
18
- * Harmonic-function role of a progression as a whole.
19
- *
20
- * @category Composition
21
- */
22
- export type ProgFunction = 'loop' | 'tensionBuild' | 'cadenceStrong' | 'stable';
23
-
24
- /**
25
- * A named chord-progression preset expressed in scale degrees.
26
- *
27
- * @category Composition
28
- */
29
- export type ProgressionPreset = {
30
- id: string;
31
- name: string;
32
- degrees: number[];
33
- functional: ProgFunction;
34
- styles: ProgStyle[];
35
- };
36
-
37
- /**
38
- * Options controlling {@link generateProgression}.
39
- *
40
- * @category Composition
41
- */
42
- export type ProgressionOptions = {
43
- key: KeyScale;
44
- style: ProgStyle;
45
- bars: number;
46
- presetId?: string;
47
- ext?: ChordQuality | 'auto';
48
- reharmonize?: boolean;
49
- /**
50
- * Seed for the deterministic preset choice and reharmonization.
51
- *
52
- * @defaultValue 0
53
- */
54
- seed?: number;
55
- };
56
-
57
- const PRESETS: ProgressionPreset[] = [
58
- {
59
- id: 'fourChordPop',
60
- name: 'Four Chord Pop',
61
- degrees: [0, 4, 5, 3],
62
- functional: 'loop',
63
- styles: ['minimal', 'dance', 'idol'],
64
- },
65
- {
66
- id: 'pop1',
67
- name: 'Pop 1',
68
- degrees: [0, 5, 3, 4],
69
- functional: 'loop',
70
- styles: ['minimal', 'dance', 'idol'],
71
- },
72
- {
73
- id: 'axis',
74
- name: 'Axis',
75
- degrees: [5, 3, 0, 4],
76
- functional: 'loop',
77
- styles: ['minimal', 'dance', 'idol', 'rock'],
78
- },
79
- {
80
- id: 'pop2',
81
- name: 'Pop 2',
82
- degrees: [3, 0, 4, 5],
83
- functional: 'loop',
84
- styles: ['minimal', 'dance', 'idol'],
85
- },
86
- {
87
- id: 'classic',
88
- name: 'Classic',
89
- degrees: [0, 3, 4, 0],
90
- functional: 'cadenceStrong',
91
- styles: ['dance', 'idol', 'rock'],
92
- },
93
- {
94
- id: 'pop3',
95
- name: 'Pop 3',
96
- degrees: [0, 3, 5, 4],
97
- functional: 'loop',
98
- styles: ['minimal', 'dance', 'idol'],
99
- },
100
- {
101
- id: 'royalRoad',
102
- name: 'Royal Road',
103
- degrees: [3, 4, 2, 5],
104
- functional: 'tensionBuild',
105
- styles: ['dance', 'idol'],
106
- },
107
- {
108
- id: 'minor1',
109
- name: 'Minor 1',
110
- degrees: [5, 4, 3, 4],
111
- functional: 'tensionBuild',
112
- styles: ['idol', 'rock'],
113
- },
114
- {
115
- id: 'minor2',
116
- name: 'Minor 2',
117
- degrees: [5, 3, 4, 0],
118
- functional: 'tensionBuild',
119
- styles: ['idol', 'rock'],
120
- },
121
- {
122
- id: 'pop4',
123
- name: 'Pop 4',
124
- degrees: [0, 4, 2, 3],
125
- functional: 'loop',
126
- styles: ['minimal', 'dance', 'idol'],
127
- },
128
- {
129
- id: 'pop5',
130
- name: 'Pop 5',
131
- degrees: [0, 2, 3, 4],
132
- functional: 'stable',
133
- styles: ['minimal', 'dance', 'idol'],
134
- },
135
- {
136
- id: 'rock1',
137
- name: 'Rock 1',
138
- degrees: [0, 10, 3, 0],
139
- functional: 'tensionBuild',
140
- styles: ['rock'],
141
- },
142
- {
143
- id: 'rock2',
144
- name: 'Rock 2',
145
- degrees: [0, 3, 10, 0],
146
- functional: 'tensionBuild',
147
- styles: ['rock'],
148
- },
149
- {
150
- id: 'extended4',
151
- name: 'Extended 4',
152
- degrees: [0, 4, 5, 2],
153
- functional: 'stable',
154
- styles: ['minimal', 'dance'],
155
- },
156
- {
157
- id: 'minor3',
158
- name: 'Minor 3',
159
- degrees: [5, 0, 4, 3],
160
- functional: 'loop',
161
- styles: ['dance', 'idol'],
162
- },
163
- {
164
- id: 'aeolianPop',
165
- name: 'Aeolian Pop',
166
- degrees: [5, 8, 10, 0],
167
- functional: 'tensionBuild',
168
- styles: ['minimal', 'dance', 'idol', 'rock'],
169
- },
170
- {
171
- id: 'animeHighEnergy1',
172
- name: 'Anime High Energy 1',
173
- degrees: [5, 2, 3, 0],
174
- functional: 'loop',
175
- styles: ['dance', 'idol'],
176
- },
177
- {
178
- id: 'jazzPop',
179
- name: 'Jazz Pop',
180
- degrees: [1, 4, 0, 5],
181
- functional: 'cadenceStrong',
182
- styles: ['minimal', 'dance'],
183
- },
184
- {
185
- id: 'animeHighEnergy2',
186
- name: 'Anime High Energy 2',
187
- degrees: [5, 1, 4, 0],
188
- functional: 'cadenceStrong',
189
- styles: ['dance', 'idol'],
190
- },
191
- {
192
- id: 'cityPop',
193
- name: 'City Pop',
194
- degrees: [0, 5, 1, 4],
195
- functional: 'stable',
196
- styles: ['minimal', 'dance'],
197
- },
198
- {
199
- id: 'extended5',
200
- name: 'Extended 5',
201
- degrees: [0, 4, 5, 2, 3],
202
- functional: 'loop',
203
- styles: ['minimal', 'dance', 'idol'],
204
- },
205
- {
206
- id: 'neapolitanPop',
207
- name: 'Neapolitan Pop',
208
- degrees: [5, 12, 13, 4, 0],
209
- functional: 'cadenceStrong',
210
- styles: ['minimal', 'dance', 'idol'],
211
- },
212
- ];
213
-
214
- /** Chromatic semitone offset from the tonic for borrowed (non-diatonic) degrees. */
215
- const BORROWED_OFFSET: Record<number, number> = {
216
- 8: 8, // bVI
217
- 10: 10, // bVII
218
- 11: 3, // bIII
219
- 12: 5, // iv
220
- 13: 1, // bII
221
- 14: 6, // #IV
222
- };
223
-
224
- /**
225
- * All built-in progression presets.
226
- *
227
- * @category Composition
228
- */
229
- export function progressions(): ProgressionPreset[] {
230
- return PRESETS.map((p) => ({ ...p, degrees: [...p.degrees], styles: [...p.styles] }));
231
- }
232
-
233
- /**
234
- * Presets whose style list includes the given style.
235
- *
236
- * @category Composition
237
- */
238
- export function progressionsByStyle(style: ProgStyle): ProgressionPreset[] {
239
- return progressions().filter((p) => p.styles.includes(style));
240
- }
241
-
242
- /** Root pitch class of a scale degree in the given key, including borrowed degrees. */
243
- function degreeToRootPc(degree: number, key: KeyScale): number {
244
- if (degree >= 0 && degree <= 6) {
245
- const tones = scaleTonesInDegreeOrder(key);
246
- return tones.length > 0 ? (tones[degree % tones.length] ?? key.rootPc % 12) : key.rootPc % 12;
247
- }
248
- const offset = BORROWED_OFFSET[degree] ?? 0;
249
- return ((((key.rootPc % 12) + offset) % 12) + 12) % 12;
250
- }
251
-
252
- /**
253
- * Diatonic (or borrowed) triad quality of a degree in the given key.
254
- *
255
- * Diatonic degrees (0-6) take their scale-correct triad quality, so non-major
256
- * keys yield diatonic chords. Borrowed degrees keep their fixed chromatic
257
- * qualities (`#IV` diminished, `iv` minor, the rest major).
258
- */
259
- function autoQuality(degree: number, key: KeyScale): ChordQuality {
260
- if (degree >= 0 && degree <= 6) {
261
- return diatonicTriad(degree, key).quality;
262
- }
263
- if (degree === 14) {
264
- return 'dim';
265
- }
266
- if (degree === 12) {
267
- return 'min';
268
- }
269
- return 'maj';
270
- }
271
-
272
- /**
273
- * Generate a chord progression laid out one chord per bar.
274
- *
275
- * A preset is chosen by `presetId` when given, otherwise deterministically from
276
- * the presets matching `style`, seeded by `seed`. An unknown `presetId` is a
277
- * caller error and throws rather than silently falling back to a random preset.
278
- * The preset's degrees cycle to
279
- * fill `bars`; each bar is four beats, so `startBeat` is `barIndex * 4`. Chord
280
- * roots come from the key's diatonic scale-degree mapping. When `ext` is
281
- * omitted or `'auto'`, each chord takes its diatonic triad quality; otherwise
282
- * `ext` is forced on every chord. When `reharmonize` is set, some chords are
283
- * deterministically replaced with the secondary dominant (V7) of the following
284
- * chord, flagged with `secondaryDominant`.
285
- *
286
- * @param opts Generation options.
287
- * @returns One chord per bar in timeline order.
288
- * @throws If `presetId` is given but does not match any built-in preset.
289
- *
290
- * @example
291
- * ```ts
292
- * import { generateProgression, majorKey } from '@libraz/libcantus';
293
- * const chords = generateProgression({ key: majorKey(0), style: 'dance', bars: 4 });
294
- * // Deterministic for a given seed (defaults to 0); one ChordSpan per bar.
295
- * ```
296
- *
297
- * @category Composition
298
- */
299
- export function generateProgression(opts: ProgressionOptions): ChordSpan[] {
300
- assertPositiveInt(opts.bars, 'progression bars');
301
- assertGenerationBudget(opts.bars, 'progression chords');
302
- const seed = opts.seed ?? 0;
303
- let preset: ProgressionPreset | undefined;
304
- if (opts.presetId !== undefined) {
305
- preset = PRESETS.find((p) => p.id === opts.presetId);
306
- if (preset === undefined) {
307
- throw new Error(`Unknown progression preset: ${opts.presetId}`);
308
- }
309
- }
310
- if (preset === undefined) {
311
- const pool = PRESETS.filter((p) => p.styles.includes(opts.style));
312
- const candidates = pool.length > 0 ? pool : PRESETS;
313
- const rng = createRng(seed);
314
- const index = Math.floor(rng.next() * candidates.length) % candidates.length;
315
- preset = candidates[index] ?? PRESETS[0];
316
- }
317
- const degrees = preset?.degrees ?? [0];
318
- const chords: ChordSpan[] = [];
319
- for (let bar = 0; bar < opts.bars; bar += 1) {
320
- const degree = degrees[bar % degrees.length] ?? 0;
321
- const quality =
322
- opts.ext !== undefined && opts.ext !== 'auto' ? opts.ext : autoQuality(degree, opts.key);
323
- const chord: ChordSpan = {
324
- rootPc: degreeToRootPc(degree, opts.key),
325
- quality,
326
- startBeat: bar * 4,
327
- degree: degree >= 0 && degree <= 6 ? degree : undefined,
328
- };
329
- chords.push(chord);
330
- }
331
-
332
- if (opts.reharmonize) {
333
- const tonicPc = (((opts.key.rootPc % 12) + 12) % 12) as number;
334
- const rrng = createRng((seed ^ 0x9e3779b9) >>> 0);
335
- for (let i = 0; i < chords.length - 1; i += 1) {
336
- const cur = chords[i];
337
- const next = chords[i + 1];
338
- if (!cur || !next) {
339
- continue;
340
- }
341
- const domRoot = (next.rootPc + 7) % 12;
342
- const targetsTonic = (next.rootPc - tonicPc + 12) % 12 === 0;
343
- const alreadySecondary = cur.rootPc === domRoot && cur.quality === 'dom7';
344
- // A secondary dominant inserted at i-1 resolves onto this chord; replacing
345
- // it here would orphan that dominant.
346
- const isResolutionTarget = chords[i - 1]?.secondaryDominant === true;
347
- if (targetsTonic || alreadySecondary || isResolutionTarget) {
348
- continue;
349
- }
350
- if (rrng.next() < 0.5) {
351
- chords[i] = {
352
- rootPc: domRoot,
353
- quality: 'dom7',
354
- startBeat: cur.startBeat,
355
- secondaryDominant: true,
356
- };
357
- }
358
- }
359
- }
360
-
361
- return chords;
362
- }
@@ -1,323 +0,0 @@
1
- /**
2
- * Reharmonization: chord substitution, modal-interchange palettes, and negative
3
- * harmony. These transforms take an existing chord (or a key) and propose
4
- * alternative harmonies that preserve a chosen relationship — a shared function,
5
- * a common tone, a tonicizing dominant, or a reflection across the key axis.
6
- *
7
- * Roots are pitch classes (0..11); no spelled key signature is required.
8
- */
9
-
10
- import { detectChordBest } from '../../analyze/detect/index.js';
11
- import {
12
- type BorrowedSource,
13
- borrowedSource,
14
- chordToRoman,
15
- functionOf,
16
- type HarmonicFunction,
17
- isDiatonic,
18
- parallelKey,
19
- } from '../../analyze/functional/index.js';
20
- import type { KeyScale } from '../../core/types.js';
21
- import {
22
- type Chord,
23
- type ChordQuality,
24
- chordPitchClasses,
25
- diatonicTriad,
26
- makeChord,
27
- } from '../../theory/chord/index.js';
28
- import { scaleTonesInDegreeOrder } from '../../theory/scale/index.js';
29
-
30
- /** Reduce a value to a pitch class in [0, 11]. */
31
- function mod12(n: number): number {
32
- return ((n % 12) + 12) % 12;
33
- }
34
-
35
- /**
36
- * The kind of substitution relationship a candidate realizes.
37
- *
38
- * @category Reharmonization
39
- */
40
- export type SubstitutionType =
41
- | 'tritone'
42
- | 'relative'
43
- | 'borrowed'
44
- | 'chromatic-mediant'
45
- | 'secondary-dominant';
46
-
47
- /**
48
- * A proposed chord substitution with its relationship, numeral, and function.
49
- *
50
- * @category Reharmonization
51
- */
52
- export type Substitution = {
53
- chord: Chord;
54
- type: SubstitutionType;
55
- roman: string;
56
- function: HarmonicFunction;
57
- };
58
-
59
- /**
60
- * Options controlling which substitutions {@link substituteChord} returns.
61
- *
62
- * @category Reharmonization
63
- */
64
- export type SubstituteOptions = {
65
- /**
66
- * Melody pitch classes that must remain chord tones. When given, only
67
- * substitutions whose pitch classes contain every listed pitch class are
68
- * kept, so the melody stays consonant against the new harmony.
69
- */
70
- melodyPcs?: number[];
71
- };
72
-
73
- /** Whether the chord's template carries a given interval (mod 12). */
74
- function hasInterval(chord: Chord, semitone: number): boolean {
75
- return chord.intervals.some((iv) => mod12(iv) === semitone);
76
- }
77
-
78
- /** Whether a chord is a dominant type: a major third with a minor seventh. */
79
- function isDominantType(chord: Chord): boolean {
80
- return hasInterval(chord, 4) && hasInterval(chord, 10);
81
- }
82
-
83
- /**
84
- * A chord that can be tonicized: it has a perfect fifth and a third but is not
85
- * itself a dominant seventh (which is an unstable, tonicizing sonority rather
86
- * than a stable target).
87
- */
88
- function isStableTarget(chord: Chord): boolean {
89
- const hasFifth = hasInterval(chord, 7);
90
- const hasThird = hasInterval(chord, 3) || hasInterval(chord, 4);
91
- return hasFifth && hasThird && !isDominantType(chord);
92
- }
93
-
94
- /** Count the pitch classes shared between two pitch-class sets. */
95
- function commonToneCount(a: number[], b: number[]): number {
96
- const set = new Set(b);
97
- return a.filter((pc) => set.has(pc)).length;
98
- }
99
-
100
- /** The diatonic triads of a key, one per scale degree. */
101
- function diatonicTriadsOf(key: KeyScale): Chord[] {
102
- const degrees = scaleTonesInDegreeOrder(key).length;
103
- const triads: Chord[] = [];
104
- for (let degree = 0; degree < degrees; degree += 1) {
105
- triads.push(diatonicTriad(degree, key));
106
- }
107
- return triads;
108
- }
109
-
110
- /** Whether two chords share the same root pitch class and quality. */
111
- function sameChord(a: Chord, b: Chord): boolean {
112
- return mod12(a.rootPc) === mod12(b.rootPc) && a.quality === b.quality;
113
- }
114
-
115
- /** Root offsets (semitones) that form a third above or below a root. */
116
- const THIRD_OFFSETS = [3, 4, 8, 9] as const;
117
-
118
- /**
119
- * Propose substitutions for a chord within a key.
120
- *
121
- * Candidates are generated by several relationships and deduplicated against the
122
- * original chord and each other:
123
- *
124
- * - `tritone`: for a dominant-type chord, the dominant seventh a tritone away.
125
- * - `secondary-dominant`: for a stable target, the dominant seventh a fifth
126
- * above its root (tonicizing it).
127
- * - `relative`: a diatonic triad a third away that shares two common tones.
128
- * - `borrowed`: a triad from the parallel mode with the same harmonic function.
129
- * - `chromatic-mediant`: a major/minor triad a third away sharing one common
130
- * tone.
131
- *
132
- * Each result carries its Roman numeral and harmonic function in `key`. When
133
- * `opts.melodyPcs` is given, only substitutions whose pitch classes contain all
134
- * of those pitch classes are returned.
135
- *
136
- * @param chord The chord to reharmonize.
137
- * @param key The prevailing key.
138
- * @param opts Optional melody-preservation constraint.
139
- * @returns The deduplicated substitution candidates.
140
- * @example
141
- * ```ts
142
- * import { substituteChord, makeChord, majorKey } from '@libraz/libcantus';
143
- * const subs = substituteChord(makeChord(7, 'dom7'), majorKey(0));
144
- * // includes the tritone sub Db7 (rootPc 1, quality 'dom7')
145
- * ```
146
- * @category Reharmonization
147
- */
148
- export function substituteChord(
149
- chord: Chord,
150
- key: KeyScale,
151
- opts?: SubstituteOptions,
152
- ): Substitution[] {
153
- const original = chordPitchClasses(chord);
154
- const candidates: { chord: Chord; type: SubstitutionType }[] = [];
155
-
156
- // Tritone substitution: only for dominant-type chords.
157
- if (isDominantType(chord)) {
158
- candidates.push({ chord: makeChord(mod12(chord.rootPc + 6), 'dom7'), type: 'tritone' });
159
- }
160
-
161
- // Secondary dominant: the V7 tonicizing a stable target.
162
- if (isStableTarget(chord)) {
163
- candidates.push({
164
- chord: makeChord(mod12(chord.rootPc + 7), 'dom7'),
165
- type: 'secondary-dominant',
166
- });
167
- }
168
-
169
- // Relative: a diatonic triad a third away sharing two common tones.
170
- for (const triad of diatonicTriadsOf(key)) {
171
- const offset = mod12(triad.rootPc - chord.rootPc);
172
- if (
173
- (THIRD_OFFSETS as readonly number[]).includes(offset) &&
174
- commonToneCount(original, chordPitchClasses(triad)) === 2
175
- ) {
176
- candidates.push({ chord: triad, type: 'relative' });
177
- }
178
- }
179
-
180
- // Borrowed: a parallel-mode triad sharing the original's harmonic function.
181
- const targetFunction = functionOf(chord, key);
182
- for (const triad of diatonicTriadsOf(parallelKey(key))) {
183
- if (functionOf(triad, key) === targetFunction && !isDiatonic(triad, key)) {
184
- candidates.push({ chord: triad, type: 'borrowed' });
185
- }
186
- }
187
-
188
- // Chromatic mediants: major/minor triads a third away sharing one common tone.
189
- for (const offset of THIRD_OFFSETS) {
190
- for (const quality of ['maj', 'min'] as const) {
191
- const mediant = makeChord(mod12(chord.rootPc + offset), quality);
192
- if (
193
- commonToneCount(original, chordPitchClasses(mediant)) === 1 &&
194
- !isDiatonic(mediant, key)
195
- ) {
196
- candidates.push({ chord: mediant, type: 'chromatic-mediant' });
197
- }
198
- }
199
- }
200
-
201
- const melodyPcs = opts?.melodyPcs?.map(mod12);
202
- const seen = new Set<string>();
203
- const results: Substitution[] = [];
204
- for (const candidate of candidates) {
205
- if (sameChord(candidate.chord, chord)) {
206
- continue;
207
- }
208
- const dedupeKey = `${mod12(candidate.chord.rootPc)}:${candidate.chord.quality}`;
209
- if (seen.has(dedupeKey)) {
210
- continue;
211
- }
212
- seen.add(dedupeKey);
213
- if (melodyPcs !== undefined) {
214
- const pcs = new Set(chordPitchClasses(candidate.chord));
215
- if (!melodyPcs.every((pc) => pcs.has(pc))) {
216
- continue;
217
- }
218
- }
219
- results.push({
220
- chord: candidate.chord,
221
- type: candidate.type,
222
- roman: chordToRoman(candidate.chord, key),
223
- function: functionOf(candidate.chord, key),
224
- });
225
- }
226
- return results;
227
- }
228
-
229
- /**
230
- * A borrowed chord in the modal-interchange palette of a key.
231
- *
232
- * @category Reharmonization
233
- */
234
- export type BorrowedChord = {
235
- chord: Chord;
236
- roman: string;
237
- source: BorrowedSource;
238
- };
239
-
240
- /**
241
- * The modal-interchange palette of a key.
242
- *
243
- * Lists the diatonic triads of the {@link parallelKey} that are not diatonic to
244
- * `key` (the classic borrowed chords such as `iv`, `bVI`, and `bVII` in a major
245
- * key), plus the Neapolitan (a major triad on the flat second degree). Each
246
- * carries its Roman numeral and {@link borrowedSource} relative to `key`.
247
- *
248
- * @param key The prevailing key.
249
- * @returns The borrowed-chord palette.
250
- * @category Reharmonization
251
- */
252
- export function modalInterchangePalette(key: KeyScale): BorrowedChord[] {
253
- const palette: BorrowedChord[] = [];
254
- const seen = new Set<string>();
255
- const add = (chord: Chord) => {
256
- const dedupeKey = `${mod12(chord.rootPc)}:${chord.quality}`;
257
- if (seen.has(dedupeKey)) {
258
- return;
259
- }
260
- seen.add(dedupeKey);
261
- palette.push({
262
- chord,
263
- roman: chordToRoman(chord, key),
264
- source: borrowedSource(chord, key),
265
- });
266
- };
267
-
268
- for (const triad of diatonicTriadsOf(parallelKey(key))) {
269
- if (!isDiatonic(triad, key)) {
270
- add(triad);
271
- }
272
- }
273
- // The Neapolitan (major triad on b2) sits outside both parallel modes, so it
274
- // is added explicitly — unless b2 is already diatonic (e.g. in a Phrygian key,
275
- // where it is a native chord rather than a borrowing).
276
- const neapolitan = makeChord(mod12(key.rootPc + 1), 'maj');
277
- if (!isDiatonic(neapolitan, key)) {
278
- add(neapolitan);
279
- }
280
- return palette;
281
- }
282
-
283
- /**
284
- * Reflect a chord across the tonic–dominant axis of a key (negative harmony).
285
- *
286
- * Every pitch class `p` maps to `(2*T + 7 - p) mod 12`, where `T` is the key
287
- * tonic — the reflection that swaps the tonic with the dominant. The mirrored
288
- * pitch classes (including the bass, if set) are reassembled into a chord by
289
- * {@link detectChordBest}; when no chord is recognized, the mirrored root and
290
- * the original quality are used as a fallback.
291
- *
292
- * @param chord The chord to reflect.
293
- * @param key The prevailing key.
294
- * @returns The negative-harmony counterpart of the chord.
295
- * @example
296
- * ```ts
297
- * import { negativeHarmonyMirror, makeChord, majorKey } from '@libraz/libcantus';
298
- * negativeHarmonyMirror(makeChord(7, 'maj'), majorKey(0));
299
- * // G major reflected across C's axis => F minor (rootPc 5, quality 'min')
300
- * ```
301
- * @category Reharmonization
302
- */
303
- export function negativeHarmonyMirror(chord: Chord, key: KeyScale): Chord {
304
- const tonic = mod12(key.rootPc);
305
- const mirror = (p: number) => mod12(2 * tonic + 7 - p);
306
- const pcs = chordPitchClasses(chord).map(mirror);
307
- const bassPc = chord.bassPc !== undefined ? mirror(chord.bassPc) : undefined;
308
- // Place the mirrored bass an octave below so it is recognized as the lowest
309
- // note and preserved through detection.
310
- const pitches = bassPc !== undefined ? [bassPc - 12, ...pcs] : pcs;
311
- const detected = detectChordBest(pitches);
312
- if (detected) {
313
- // Without an input bass the mirrored tones are octave-less, so detection may
314
- // read the lowest pitch class as a slash bass and report a spurious
315
- // inversion. Restore the recognized chord to root position in that case.
316
- if (bassPc === undefined && detected.bassPc !== undefined) {
317
- return makeChord(detected.rootPc, detected.quality);
318
- }
319
- return detected;
320
- }
321
- const quality: ChordQuality = chord.quality;
322
- return makeChord(mirror(chord.rootPc), quality, bassPc);
323
- }