@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,282 +0,0 @@
1
- /**
2
- * Meter and time signatures: bar/beat arithmetic, simple vs. compound meters,
3
- * metric-accent hierarchy, and tuplet subdivision.
4
- *
5
- * Positions and durations are measured in quarter-note beats, matching the rest
6
- * of the library's beat convention (four quarter-note beats per bar in 4/4).
7
- */
8
-
9
- /**
10
- * A time signature as a numerator over a note-value denominator.
11
- *
12
- * @category Rhythm & Meter
13
- */
14
- export type TimeSignature = {
15
- numerator: number;
16
- denominator: number;
17
- /**
18
- * Optional additive grouping of the bar's main pulses, as the felt-beat
19
- * lengths in pulses — e.g. `[2, 2, 3]` for a 2+2+3 reading of 7/8, or
20
- * `[3, 2]` for 5/8. The entries must be positive integers summing to
21
- * {@link pulsesPerBar}. When present, the head pulse of each group (other
22
- * than the downbeat) is treated as a secondary strong pulse by
23
- * {@link metricWeight}/{@link isStrongBeat}; when absent, all main pulses
24
- * are weighted equally as flat, evenly divided pulses.
25
- */
26
- grouping?: number[];
27
- };
28
-
29
- /**
30
- * A position expressed as a bar index and a quarter-note offset within the bar.
31
- *
32
- * @category Rhythm & Meter
33
- */
34
- export type BarPosition = {
35
- bar: number;
36
- /** Quarter-note offset from the start of the bar. */
37
- beat: number;
38
- };
39
-
40
- const EPS = 1e-9;
41
-
42
- /** Whether `value` is an integer multiple of `unit` (within a float tolerance). */
43
- function isMultiple(value: number, unit: number): boolean {
44
- if (unit === 0) {
45
- return false;
46
- }
47
- const ratio = value / unit;
48
- return Math.abs(ratio - Math.round(ratio)) < EPS;
49
- }
50
-
51
- /**
52
- * Parse a time signature such as `"4/4"` or `"6/8"`.
53
- *
54
- * @param text The signature text.
55
- * @returns The parsed time signature.
56
- * @throws If the text is not `n/d` with positive integers.
57
- * @example
58
- * ```ts
59
- * import { parseTimeSignature } from '@libraz/libcantus';
60
- * parseTimeSignature('6/8'); // { numerator: 6, denominator: 8 }
61
- * ```
62
- * @category Rhythm & Meter
63
- */
64
- export function parseTimeSignature(text: string): TimeSignature {
65
- const match = /^\s*(\d+)\s*\/\s*(\d+)\s*$/.exec(text);
66
- if (!match) {
67
- throw new Error(`Invalid time signature: ${text}`);
68
- }
69
- const numerator = Number.parseInt(match[1] ?? '', 10);
70
- const denominator = Number.parseInt(match[2] ?? '', 10);
71
- if (numerator <= 0 || denominator <= 0) {
72
- throw new Error(`Invalid time signature: ${text}`);
73
- }
74
- return { numerator, denominator };
75
- }
76
-
77
- /**
78
- * Render a time signature as `"n/d"`.
79
- *
80
- * @category Rhythm & Meter
81
- */
82
- export function formatTimeSignature(ts: TimeSignature): string {
83
- return `${ts.numerator}/${ts.denominator}`;
84
- }
85
-
86
- /**
87
- * Whether a meter is compound: its main pulses each divide into three, as in
88
- * 6/8, 9/8, 12/8, or 6/4. Compound meters are those whose numerator is a
89
- * multiple of three greater than three, independent of the denominator. Meters
90
- * like 3/8 and 3/4 (simple triples, numerator 3) are not compound.
91
- *
92
- * @param ts The time signature.
93
- * @returns True for compound meters.
94
- * @category Rhythm & Meter
95
- */
96
- export function isCompound(ts: TimeSignature): boolean {
97
- return ts.numerator % 3 === 0 && ts.numerator > 3;
98
- }
99
-
100
- /** Length of one denominator unit in quarter-note beats. */
101
- function unitBeats(ts: TimeSignature): number {
102
- return 4 / ts.denominator;
103
- }
104
-
105
- /**
106
- * Length of a bar in quarter-note beats.
107
- *
108
- * @param ts The time signature.
109
- * @returns The bar length in quarter notes.
110
- * @category Rhythm & Meter
111
- */
112
- export function beatsPerBar(ts: TimeSignature): number {
113
- return ts.numerator * unitBeats(ts);
114
- }
115
-
116
- /** Length of one main pulse (a dotted value in compound meters) in quarter notes. */
117
- function pulseBeats(ts: TimeSignature): number {
118
- return isCompound(ts) ? 3 * unitBeats(ts) : unitBeats(ts);
119
- }
120
-
121
- /**
122
- * Number of main pulses (felt beats) per bar: the numerator for simple meters,
123
- * a third of it for compound meters.
124
- *
125
- * For additive/irregular meters such as 7/8 or 5/8 this counts the raw pulses
126
- * (7 and 5 respectively), which are otherwise felt as flat, evenly divided and
127
- * equally accented pulses. To recover a 2+2+3 or 3+2 felt-beat grouping, set
128
- * {@link TimeSignature.grouping}; {@link metricWeight} then accents each
129
- * group's head pulse.
130
- *
131
- * @param ts The time signature.
132
- * @returns The pulse count per bar.
133
- * @category Rhythm & Meter
134
- */
135
- export function pulsesPerBar(ts: TimeSignature): number {
136
- return beatsPerBar(ts) / pulseBeats(ts);
137
- }
138
-
139
- /**
140
- * Whether `grouping` is a valid additive grouping for a bar of `pulses` main
141
- * pulses: a non-empty list of positive integers summing to `pulses`.
142
- */
143
- function isValidGrouping(grouping: number[], pulses: number): boolean {
144
- if (grouping.length === 0) {
145
- return false;
146
- }
147
- let sum = 0;
148
- for (const g of grouping) {
149
- if (!Number.isInteger(g) || g <= 0) {
150
- return false;
151
- }
152
- sum += g;
153
- }
154
- return sum === pulses;
155
- }
156
-
157
- /** Whether `pulseIndex` is the head pulse of one of the additive groups. */
158
- function isGroupHead(grouping: number[], pulseIndex: number): boolean {
159
- let acc = 0;
160
- for (const g of grouping) {
161
- if (acc === pulseIndex) {
162
- return true;
163
- }
164
- acc += g;
165
- }
166
- return false;
167
- }
168
-
169
- /**
170
- * Convert an absolute quarter-note position to a bar index and in-bar offset.
171
- *
172
- * @param beatInQuarters Absolute position in quarter-note beats.
173
- * @param ts The time signature.
174
- * @returns The bar and in-bar quarter-note offset.
175
- * @example
176
- * ```ts
177
- * import { parseTimeSignature, beatToBarPosition } from '@libraz/libcantus';
178
- * const ts = parseTimeSignature('4/4');
179
- * beatToBarPosition(5, ts); // { bar: 1, beat: 1 }
180
- * ```
181
- * @category Rhythm & Meter
182
- */
183
- export function beatToBarPosition(beatInQuarters: number, ts: TimeSignature): BarPosition {
184
- const barLen = beatsPerBar(ts);
185
- const bar = Math.floor(beatInQuarters / barLen);
186
- return { bar, beat: beatInQuarters - bar * barLen };
187
- }
188
-
189
- /**
190
- * Convert a bar index and in-bar offset back to an absolute quarter-note
191
- * position.
192
- *
193
- * @param pos The bar position.
194
- * @param ts The time signature.
195
- * @returns The absolute position in quarter-note beats.
196
- * @category Rhythm & Meter
197
- */
198
- export function barPositionToBeat(pos: BarPosition, ts: TimeSignature): number {
199
- return pos.bar * beatsPerBar(ts) + pos.beat;
200
- }
201
-
202
- /**
203
- * Metric weight of a position within its bar, on a 0–3 scale:
204
- * 3 the downbeat, 2 a secondary strong pulse, 1 any other main pulse, and 0 an
205
- * off-pulse subdivision.
206
- *
207
- * The secondary strong pulse is the bar's midpoint in even simple/compound
208
- * meters. For an additive/irregular meter with {@link TimeSignature.grouping}
209
- * set (e.g. `[2, 2, 3]` for 7/8), each group's head pulse other than the
210
- * downbeat is the secondary strong pulse instead; without a grouping every
211
- * non-downbeat main pulse of such a meter weighs 1 (flat, evenly divided
212
- * pulses).
213
- *
214
- * @param beatInQuarters Absolute or in-bar quarter-note position.
215
- * @param ts The time signature.
216
- * @returns The metric weight (0–3).
217
- * @throws If `ts.grouping` is present but is not a positive-integer list
218
- * summing to {@link pulsesPerBar}.
219
- * @example
220
- * ```ts
221
- * import { parseTimeSignature, metricWeight } from '@libraz/libcantus';
222
- * const ts = parseTimeSignature('4/4');
223
- * metricWeight(0, ts); // 3 (the downbeat)
224
- * ```
225
- * @category Rhythm & Meter
226
- */
227
- export function metricWeight(beatInQuarters: number, ts: TimeSignature): number {
228
- const { beat } = beatToBarPosition(beatInQuarters, ts);
229
- const pulse = pulseBeats(ts);
230
- if (!isMultiple(beat, pulse)) {
231
- return 0;
232
- }
233
- const pulses = pulsesPerBar(ts);
234
- const pulseIndex = Math.round(beat / pulse) % pulses;
235
- if (pulseIndex === 0) {
236
- return 3;
237
- }
238
- const grouping = ts.grouping;
239
- if (grouping !== undefined) {
240
- if (!isValidGrouping(grouping, pulses)) {
241
- throw new Error(
242
- `Invalid grouping [${grouping.join(', ')}] for ${formatTimeSignature(ts)}: ` +
243
- `entries must be positive integers summing to ${pulses}`,
244
- );
245
- }
246
- return isGroupHead(grouping, pulseIndex) ? 2 : 1;
247
- }
248
- if (pulses % 2 === 0 && pulseIndex === pulses / 2) {
249
- return 2;
250
- }
251
- return 1;
252
- }
253
-
254
- /**
255
- * Whether a position is metrically accented (weight 2 or more — a downbeat or a
256
- * secondary strong pulse).
257
- *
258
- * @param beatInQuarters Absolute or in-bar quarter-note position.
259
- * @param ts The time signature.
260
- * @returns True on strong beats.
261
- * @category Rhythm & Meter
262
- */
263
- export function isStrongBeat(beatInQuarters: number, ts: TimeSignature): boolean {
264
- return metricWeight(beatInQuarters, ts) >= 2;
265
- }
266
-
267
- /**
268
- * Subdivide a span into `count` equal tuplet durations (e.g. an eighth-note
269
- * triplet is `tuplet(1, 3)`).
270
- *
271
- * @param totalBeats Total span in quarter-note beats.
272
- * @param count Number of equal parts.
273
- * @returns `count` equal durations summing to `totalBeats`.
274
- * @throws If `count` is not a positive integer.
275
- * @category Rhythm & Meter
276
- */
277
- export function tuplet(totalBeats: number, count: number): number[] {
278
- if (!Number.isInteger(count) || count <= 0) {
279
- throw new Error(`Invalid tuplet count: ${count}`);
280
- }
281
- return new Array<number>(count).fill(totalBeats / count);
282
- }
@@ -1,311 +0,0 @@
1
- /**
2
- * Pitch spelling: letter-name notes, enharmonic-aware conversion, and spelled
3
- * intervals.
4
- *
5
- * The rest of the library works in pitch classes (0..11), which cannot tell a
6
- * G# from an Ab or an augmented fourth from a diminished fifth. This module adds
7
- * an explicit spelling layer — a note is a diatonic letter plus a chromatic
8
- * alteration — so interval quality (P/M/m/A/d) and enharmonic identity are
9
- * preserved.
10
- */
11
-
12
- /** Semitone offset of each natural letter above C: C D E F G A B. */
13
- const LETTER_SEMITONES = [0, 2, 4, 5, 7, 9, 11] as const;
14
-
15
- /** Letter glyphs indexed by letter number (0 = C .. 6 = B). */
16
- const LETTER_NAMES = ['C', 'D', 'E', 'F', 'G', 'A', 'B'] as const;
17
-
18
- /**
19
- * A spelled note: a diatonic letter, a chromatic alteration, and an optional
20
- * octave.
21
- *
22
- * - `letter` is 0..6 for C..B.
23
- * - `alter` is the number of semitones of alteration: -2 double-flat, -1 flat,
24
- * 0 natural, +1 sharp, +2 double-sharp.
25
- * - `octave` follows scientific pitch notation (middle C = C4 = MIDI 60). When
26
- * absent, the note denotes a bare pitch class.
27
- *
28
- * @category Pitch & Intervals
29
- */
30
- export type Note = {
31
- letter: number;
32
- alter: number;
33
- octave?: number;
34
- };
35
-
36
- /**
37
- * A spelled interval: a diatonic number, a quality label, and its semitone span.
38
- *
39
- * @category Pitch & Intervals
40
- */
41
- export type SpelledInterval = {
42
- /** Diatonic size: 1 = unison, 2 = second, ... 8 = octave, and beyond. */
43
- number: number;
44
- /** Quality label: 'P', 'M', 'm', or repeated 'A'/'d' for (multiply) aug/dim. */
45
- quality: string;
46
- /** Signed semitone distance from the first note to the second. */
47
- semitones: number;
48
- };
49
-
50
- /** Reduce any integer to a pitch class in [0, 11]. */
51
- function mod12(n: number): number {
52
- return ((n % 12) + 12) % 12;
53
- }
54
-
55
- /** Reduce any integer to [0, 7). */
56
- function mod7(n: number): number {
57
- return ((n % 7) + 7) % 7;
58
- }
59
-
60
- /**
61
- * Parse scientific pitch notation into a {@link Note}.
62
- *
63
- * Accepts a letter (case-insensitive) followed by any number of same-direction
64
- * accidentals and an optional octave, e.g. `"C"`, `"C#4"`, `"Bb"`, `"F##3"`,
65
- * `"Ebb2"`. Both `#` and `x` (double-sharp) are accepted for sharps; `b` for
66
- * flats. Mixing sharps and flats (e.g. `"C#b"`) is rejected.
67
- *
68
- * @param text The note text.
69
- * @returns The parsed note.
70
- * @throws If the text is not a valid note.
71
- * @example
72
- * ```ts
73
- * import { parseNote, noteToMidi } from '@libraz/libcantus';
74
- * const n = parseNote('C#4');
75
- * noteToMidi(n); // 61
76
- * ```
77
- * @category Pitch & Intervals
78
- */
79
- export function parseNote(text: string): Note {
80
- const match = /^([A-Ga-g])([#x]*|b*)(-?\d+)?$/.exec(text.trim());
81
- if (!match) {
82
- throw new Error(`Invalid note: ${text}`);
83
- }
84
- const letterGlyph = (match[1] ?? '').toUpperCase();
85
- const letter = LETTER_NAMES.indexOf(letterGlyph as (typeof LETTER_NAMES)[number]);
86
- let alter = 0;
87
- for (const ch of match[2] ?? '') {
88
- if (ch === '#') {
89
- alter += 1;
90
- } else if (ch === 'x') {
91
- alter += 2;
92
- } else if (ch === 'b') {
93
- alter -= 1;
94
- }
95
- }
96
- const note: Note = { letter, alter };
97
- if (match[3] !== undefined) {
98
- note.octave = Number.parseInt(match[3], 10);
99
- }
100
- return note;
101
- }
102
-
103
- /** Render an alteration as accidental glyphs (`##`, `b`, empty for natural). */
104
- function formatAlter(alter: number): string {
105
- if (alter > 0) {
106
- return '#'.repeat(alter);
107
- }
108
- if (alter < 0) {
109
- return 'b'.repeat(-alter);
110
- }
111
- return '';
112
- }
113
-
114
- /**
115
- * Render a {@link Note} as scientific pitch notation.
116
- *
117
- * @param note The note to format.
118
- * @returns The note text, including the octave when present.
119
- * @example
120
- * ```ts
121
- * import { formatNote, parseNote } from '@libraz/libcantus';
122
- * formatNote(parseNote('C#4')); // 'C#4'
123
- * ```
124
- * @category Pitch & Intervals
125
- */
126
- export function formatNote(note: Note): string {
127
- const glyph = LETTER_NAMES[mod7(note.letter)] ?? 'C';
128
- const octave = note.octave === undefined ? '' : String(note.octave);
129
- return `${glyph}${formatAlter(note.alter)}${octave}`;
130
- }
131
-
132
- /**
133
- * Pitch class (0..11) of a note, ignoring octave.
134
- *
135
- * @param note The note.
136
- * @returns The pitch class.
137
- * @example
138
- * ```ts
139
- * import { noteToPitchClass, parseNote } from '@libraz/libcantus';
140
- * noteToPitchClass(parseNote('Db')); // 1
141
- * ```
142
- * @category Pitch & Intervals
143
- */
144
- export function noteToPitchClass(note: Note): number {
145
- const natural = LETTER_SEMITONES[mod7(note.letter)] ?? 0;
146
- return mod12(natural + note.alter);
147
- }
148
-
149
- /**
150
- * MIDI number of a note (middle C = C4 = 60).
151
- *
152
- * @param note The note; must carry an octave.
153
- * @returns The MIDI number.
154
- * @throws If the note has no octave.
155
- * @example
156
- * ```ts
157
- * import { noteToMidi, parseNote } from '@libraz/libcantus';
158
- * noteToMidi(parseNote('A4')); // 69
159
- * ```
160
- * @category Pitch & Intervals
161
- */
162
- export function noteToMidi(note: Note): number {
163
- if (note.octave === undefined) {
164
- throw new Error('noteToMidi requires an octave');
165
- }
166
- const natural = LETTER_SEMITONES[mod7(note.letter)] ?? 0;
167
- return (note.octave + 1) * 12 + natural + note.alter;
168
- }
169
-
170
- /** Preferred spelling when naming a black key from a bare MIDI number. */
171
- const SHARP_SPELLING: readonly [number, number][] = [
172
- [0, 0],
173
- [0, 1],
174
- [1, 0],
175
- [1, 1],
176
- [2, 0],
177
- [3, 0],
178
- [3, 1],
179
- [4, 0],
180
- [4, 1],
181
- [5, 0],
182
- [5, 1],
183
- [6, 0],
184
- ];
185
-
186
- const FLAT_SPELLING: readonly [number, number][] = [
187
- [0, 0],
188
- [1, -1],
189
- [1, 0],
190
- [2, -1],
191
- [2, 0],
192
- [3, 0],
193
- [4, -1],
194
- [4, 0],
195
- [5, -1],
196
- [5, 0],
197
- [6, -1],
198
- [6, 0],
199
- ];
200
-
201
- /**
202
- * Name a MIDI number as a {@link Note}, choosing sharp or flat spelling.
203
- *
204
- * The input is rounded to the nearest integer but is not clamped to the MIDI
205
- * range 0..127: out-of-range values extrapolate linearly (e.g. `-1` -> `B-2`,
206
- * `128` -> `G#9`) and remain an exact inverse of {@link noteToMidi}.
207
- *
208
- * @param midi The MIDI number.
209
- * @param spelling Whether to prefer sharps or flats for black keys.
210
- * @returns The spelled note, with octave.
211
- * @example
212
- * ```ts
213
- * import { midiToNote, formatNote } from '@libraz/libcantus';
214
- * formatNote(midiToNote(61)); // 'C#4'
215
- * formatNote(midiToNote(61, 'flat')); // 'Db4'
216
- * ```
217
- * @category Pitch & Intervals
218
- */
219
- export function midiToNote(midi: number, spelling: 'sharp' | 'flat' = 'sharp'): Note {
220
- const rounded = Math.round(midi);
221
- const pc = mod12(rounded);
222
- const octave = Math.floor(rounded / 12) - 1;
223
- const table = spelling === 'sharp' ? SHARP_SPELLING : FLAT_SPELLING;
224
- const entry = table[pc] ?? [0, 0];
225
- return { letter: entry[0], alter: entry[1], octave };
226
- }
227
-
228
- /** Reference semitone span of a perfect/major simple interval by diatonic number. */
229
- const SIMPLE_REFERENCE = [0, 0, 2, 4, 5, 7, 9, 11] as const;
230
-
231
- /** Whether a simple diatonic number (1..7 within an octave) is a perfect kind. */
232
- function isPerfectNumber(simpleNumber: number): boolean {
233
- return simpleNumber === 1 || simpleNumber === 4 || simpleNumber === 5;
234
- }
235
-
236
- /** Quality label from a diatonic number and its actual semitone span. */
237
- function qualityFromSpan(numberValue: number, semitones: number): string {
238
- const octaves = Math.floor((numberValue - 1) / 7);
239
- const simple = numberValue - 7 * octaves;
240
- const reference = (SIMPLE_REFERENCE[simple] ?? 0) + 12 * octaves;
241
- const delta = semitones - reference;
242
- if (isPerfectNumber(simple)) {
243
- if (delta === 0) {
244
- return 'P';
245
- }
246
- return delta > 0 ? 'A'.repeat(delta) : 'd'.repeat(-delta);
247
- }
248
- if (delta === 0) {
249
- return 'M';
250
- }
251
- if (delta === -1) {
252
- return 'm';
253
- }
254
- return delta > 0 ? 'A'.repeat(delta) : 'd'.repeat(-delta - 1);
255
- }
256
-
257
- /** Diatonic ladder index of a note (letter + 7 * octave when octave-bearing). */
258
- function diatonicIndex(note: Note): number {
259
- return mod7(note.letter) + 7 * (note.octave ?? 0);
260
- }
261
-
262
- /**
263
- * The spelled interval from one note to another.
264
- *
265
- * When both notes carry octaves, the interval reflects their full signed
266
- * distance (so `C4 -> C5` is a perfect octave). When either lacks an octave the
267
- * interval is measured within a single ascending octave from `a` to `b`.
268
- *
269
- * @param a The lower/first note.
270
- * @param b The upper/second note.
271
- * @returns The diatonic number, quality, and semitone span.
272
- * @example
273
- * ```ts
274
- * import { spelledInterval, parseNote } from '@libraz/libcantus';
275
- * spelledInterval(parseNote('C4'), parseNote('G4'));
276
- * // { number: 5, quality: 'P', semitones: 7 }
277
- * ```
278
- * @category Pitch & Intervals
279
- */
280
- export function spelledInterval(a: Note, b: Note): SpelledInterval {
281
- const octaved = a.octave !== undefined && b.octave !== undefined;
282
- let letterSteps: number;
283
- let semitones: number;
284
- if (octaved) {
285
- letterSteps = diatonicIndex(b) - diatonicIndex(a);
286
- // Signed pitch distance from a to b, so a note below a yields a negative span.
287
- semitones = noteToMidi(b) - noteToMidi(a);
288
- } else {
289
- letterSteps = mod7(b.letter - a.letter);
290
- let rawSemis = mod12(noteToPitchClass(b) - noteToPitchClass(a));
291
- // Lift the chromatic span into the octave nearest the diatonic reference so
292
- // wraparound intervals stay consistent with the diatonic number instead of
293
- // collapsing modulo 12. The correction is bidirectional: an upward wrap
294
- // (e.g. Ab -> G# = augmented seventh) adds an octave, while a same-letter or
295
- // downward chromatic step (e.g. E -> Eb, F# -> F, C -> Cb = descending
296
- // diminished/augmented unison) subtracts one so the sign follows pitch
297
- // direction instead of returning a garbage stack of augmentations.
298
- const reference = SIMPLE_REFERENCE[letterSteps + 1] ?? 0;
299
- if (reference - rawSemis > 6) {
300
- rawSemis += 12;
301
- } else if (rawSemis - reference > 6) {
302
- rawSemis -= 12;
303
- }
304
- semitones = rawSemis;
305
- }
306
- const absSteps = Math.abs(letterSteps);
307
- const number = absSteps + 1;
308
- // Quality comes from the absolute chromatic span; semitones keeps its sign.
309
- const quality = qualityFromSpan(number, Math.abs(semitones));
310
- return { number, quality, semitones };
311
- }
@@ -1,53 +0,0 @@
1
- /**
2
- * Deterministic seeded pseudo-random number generation shared across the
3
- * generative modules (rhythm, drums, bass, groove, counter-melody). A single
4
- * mulberry32 core exposes the small set of samplers those modules need; given a
5
- * seed the stream is fully reproducible.
6
- */
7
-
8
- /**
9
- * A deterministic PRNG with the sampling helpers the generators need.
10
- *
11
- * @category Utilities
12
- */
13
- export type Rng = {
14
- /** Next float in [0, 1). */
15
- next: () => number;
16
- /** True with probability `p`. */
17
- prob: (p: number) => boolean;
18
- /** Integer in the inclusive range [lo, hi]. */
19
- range: (lo: number, hi: number) => number;
20
- /** Float in [lo, hi). */
21
- float: (lo: number, hi: number) => number;
22
- };
23
-
24
- /**
25
- * Create a seeded PRNG (mulberry32) exposing the shared samplers.
26
- *
27
- * @param seed The 32-bit seed; the same seed always yields the same stream.
28
- * @returns The seeded generator.
29
- * @example
30
- * ```ts
31
- * import { createRng } from '@libraz/libcantus';
32
- * const rng = createRng(42);
33
- * rng.next(); // deterministic float in [0, 1)
34
- * rng.range(1, 6); // deterministic integer in [1, 6]
35
- * ```
36
- * @category Utilities
37
- */
38
- export function createRng(seed: number): Rng {
39
- let state = seed >>> 0;
40
- const next = () => {
41
- state = (state + 0x6d2b79f5) >>> 0;
42
- let t = state;
43
- t = Math.imul(t ^ (t >>> 15), t | 1);
44
- t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
45
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
46
- };
47
- return {
48
- next,
49
- prob: (p) => next() < p,
50
- range: (lo, hi) => lo + Math.floor(next() * (hi - lo + 1)),
51
- float: (lo, hi) => lo + next() * (hi - lo),
52
- };
53
- }