@libraz/libcantus 0.9.3 → 0.9.5

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 (201) hide show
  1. package/README.md +29 -20
  2. package/README_ja.md +268 -0
  3. package/dist/analyze/index.cjs +39 -1947
  4. package/dist/analyze/index.cjs.map +1 -1
  5. package/dist/analyze/index.d.cts +161 -91
  6. package/dist/analyze/index.d.ts +161 -91
  7. package/dist/analyze/index.js +19 -8
  8. package/dist/chunk-2A2QMA74.cjs +261 -0
  9. package/dist/chunk-2A2QMA74.cjs.map +1 -0
  10. package/dist/chunk-2PTFGVD7.js +261 -0
  11. package/dist/chunk-2PTFGVD7.js.map +1 -0
  12. package/dist/chunk-357ZDPVU.cjs +162 -0
  13. package/dist/chunk-357ZDPVU.cjs.map +1 -0
  14. package/dist/chunk-3KX76RLG.js +1548 -0
  15. package/dist/chunk-3KX76RLG.js.map +1 -0
  16. package/dist/chunk-4VZ5MZAM.cjs +889 -0
  17. package/dist/chunk-4VZ5MZAM.cjs.map +1 -0
  18. package/dist/{chunk-C5XYKUVG.js → chunk-7EONYVI5.js} +471 -171
  19. package/dist/chunk-7EONYVI5.js.map +1 -0
  20. package/dist/{chunk-IT2O4XFE.js → chunk-7JEP3MFY.js} +42 -34
  21. package/dist/chunk-7JEP3MFY.js.map +1 -0
  22. package/dist/{chunk-4MHA432G.js → chunk-AID75RN3.js} +572 -250
  23. package/dist/chunk-AID75RN3.js.map +1 -0
  24. package/dist/chunk-BSLTLGM3.cjs +497 -0
  25. package/dist/chunk-BSLTLGM3.cjs.map +1 -0
  26. package/dist/chunk-CU6VG2YM.cjs +109 -0
  27. package/dist/chunk-CU6VG2YM.cjs.map +1 -0
  28. package/dist/{chunk-EH6EM37U.js → chunk-DZMHIGI4.js} +81 -59
  29. package/dist/chunk-DZMHIGI4.js.map +1 -0
  30. package/dist/{chunk-AKLYPWIN.js → chunk-EDOTEABK.js} +45 -19
  31. package/dist/chunk-EDOTEABK.js.map +1 -0
  32. package/dist/{chunk-2ZWMSJH6.js → chunk-HN2OEUN2.js} +7 -4
  33. package/dist/chunk-HN2OEUN2.js.map +1 -0
  34. package/dist/{chunk-MIWO425M.js → chunk-KBTTEX7Q.js} +128 -43
  35. package/dist/chunk-KBTTEX7Q.js.map +1 -0
  36. package/dist/chunk-KQN2ZYND.cjs +724 -0
  37. package/dist/chunk-KQN2ZYND.cjs.map +1 -0
  38. package/dist/chunk-MPTVJIVG.cjs +3176 -0
  39. package/dist/chunk-MPTVJIVG.cjs.map +1 -0
  40. package/dist/chunk-MTMZTMWM.cjs +53 -0
  41. package/dist/chunk-MTMZTMWM.cjs.map +1 -0
  42. package/dist/{chunk-CHMXELDS.js → chunk-NNB67XA2.js} +372 -197
  43. package/dist/chunk-NNB67XA2.js.map +1 -0
  44. package/dist/chunk-OL3IHHSJ.cjs +1204 -0
  45. package/dist/chunk-OL3IHHSJ.cjs.map +1 -0
  46. package/dist/chunk-PI3UXRWK.cjs +528 -0
  47. package/dist/chunk-PI3UXRWK.cjs.map +1 -0
  48. package/dist/chunk-R2VVFAIN.js +528 -0
  49. package/dist/chunk-R2VVFAIN.js.map +1 -0
  50. package/dist/chunk-T64C7ZD7.cjs +1 -0
  51. package/dist/chunk-T64C7ZD7.cjs.map +1 -0
  52. package/dist/chunk-WFDXOLUB.cjs +337 -0
  53. package/dist/chunk-WFDXOLUB.cjs.map +1 -0
  54. package/dist/chunk-WTAN4ACG.cjs +1548 -0
  55. package/dist/chunk-WTAN4ACG.cjs.map +1 -0
  56. package/dist/chunk-YFJXS3AU.cjs +33 -0
  57. package/dist/chunk-YFJXS3AU.cjs.map +1 -0
  58. package/dist/{chunk-GOAW4VYL.js → chunk-YUE6TJ4M.js} +315 -145
  59. package/dist/chunk-YUE6TJ4M.js.map +1 -0
  60. package/dist/{chunk-62QZKCJQ.js → chunk-ZGLJM4A4.js} +10 -6
  61. package/dist/chunk-ZGLJM4A4.js.map +1 -0
  62. package/dist/core/index.cjs +140 -647
  63. package/dist/core/index.cjs.map +1 -1
  64. package/dist/core/index.d.cts +313 -41
  65. package/dist/core/index.d.ts +313 -41
  66. package/dist/core/index.js +64 -16
  67. package/dist/{function-DVH4Eusr.d.ts → function-CxixTB02.d.cts} +122 -10
  68. package/dist/{function-DYt4iKsn.d.cts → function-DTCt675z.d.ts} +122 -10
  69. package/dist/generate/index.cjs +28 -4085
  70. package/dist/generate/index.cjs.map +1 -1
  71. package/dist/generate/index.d.cts +374 -66
  72. package/dist/generate/index.d.ts +374 -66
  73. package/dist/generate/index.js +23 -9
  74. package/dist/{index-Jc0Ryi79.d.ts → index-B2Ulxxpo.d.cts} +21 -5
  75. package/dist/{index-CpOsP1y9.d.cts → index-BkYJ0xMq.d.cts} +18 -17
  76. package/dist/{index-CxqBQtH1.d.cts → index-BuaB-ypt.d.ts} +21 -5
  77. package/dist/index-CAN7v-kw.d.ts +199 -0
  78. package/dist/{index-Bt4tOUD9.d.ts → index-COw1Ky9g.d.ts} +93 -8
  79. package/dist/{index-Dikst44v.d.cts → index-Cfx8xgH0.d.ts} +14 -11
  80. package/dist/{index-DxvARIP2.d.ts → index-DRtIhlJj.d.ts} +18 -17
  81. package/dist/{index-Cl7mhOFM.d.cts → index-DXd9JikN.d.cts} +92 -10
  82. package/dist/{index-Cl7mhOFM.d.ts → index-DXd9JikN.d.ts} +92 -10
  83. package/dist/index-alrRninU.d.cts +199 -0
  84. package/dist/{index-BNlGP86A.d.cts → index-oTQbYYIs.d.cts} +93 -8
  85. package/dist/index-tR9jTn26.d.cts +275 -0
  86. package/dist/index-tR9jTn26.d.ts +275 -0
  87. package/dist/{index-Bg5dYMmZ.d.ts → index-x2vcsduE.d.cts} +14 -11
  88. package/dist/index.cjs +429 -7383
  89. package/dist/index.cjs.map +1 -1
  90. package/dist/index.d.cts +15 -15
  91. package/dist/index.d.ts +15 -15
  92. package/dist/index.js +121 -27
  93. package/dist/model/index.cjs +8 -2562
  94. package/dist/model/index.cjs.map +1 -1
  95. package/dist/model/index.d.cts +471 -81
  96. package/dist/model/index.d.ts +471 -81
  97. package/dist/model/index.js +6 -7
  98. package/dist/styled-Bl0ZK28h.d.ts +543 -0
  99. package/dist/styled-Crv6SA4S.d.cts +543 -0
  100. package/dist/theory/index.cjs +172 -1822
  101. package/dist/theory/index.cjs.map +1 -1
  102. package/dist/theory/index.d.cts +151 -233
  103. package/dist/theory/index.d.ts +151 -233
  104. package/dist/theory/index.js +30 -7
  105. package/dist/{types-MGP29r3t.d.cts → types-BN6_Q9Kl.d.cts} +11 -2
  106. package/dist/{types-MGP29r3t.d.ts → types-BN6_Q9Kl.d.ts} +11 -2
  107. package/package.json +34 -13
  108. package/dist/chunk-2ZWMSJH6.js.map +0 -1
  109. package/dist/chunk-4MHA432G.js.map +0 -1
  110. package/dist/chunk-62QZKCJQ.js.map +0 -1
  111. package/dist/chunk-AKLYPWIN.js.map +0 -1
  112. package/dist/chunk-C5XYKUVG.js.map +0 -1
  113. package/dist/chunk-CHMXELDS.js.map +0 -1
  114. package/dist/chunk-DYWFNC4A.js +0 -152
  115. package/dist/chunk-DYWFNC4A.js.map +0 -1
  116. package/dist/chunk-EH6EM37U.js.map +0 -1
  117. package/dist/chunk-FBQK3ZQN.js +0 -131
  118. package/dist/chunk-FBQK3ZQN.js.map +0 -1
  119. package/dist/chunk-GOAW4VYL.js.map +0 -1
  120. package/dist/chunk-I5R7ZBJS.js +0 -202
  121. package/dist/chunk-I5R7ZBJS.js.map +0 -1
  122. package/dist/chunk-IT2O4XFE.js.map +0 -1
  123. package/dist/chunk-MIWO425M.js.map +0 -1
  124. package/dist/chunk-SGU7SPLK.js +0 -924
  125. package/dist/chunk-SGU7SPLK.js.map +0 -1
  126. package/dist/index-CGMZNCx6.d.cts +0 -140
  127. package/dist/index-CGMZNCx6.d.ts +0 -140
  128. package/dist/index-CnZCo29u.d.cts +0 -108
  129. package/dist/index-JnF4v9bx.d.ts +0 -108
  130. package/dist/styled-DZ7GjShb.d.ts +0 -282
  131. package/dist/styled-ZTbdaVFM.d.cts +0 -282
  132. package/src/analyze/arrange/index.ts +0 -21
  133. package/src/analyze/arrange/internal.ts +0 -127
  134. package/src/analyze/arrange/tension.ts +0 -199
  135. package/src/analyze/arrange/tracks.ts +0 -312
  136. package/src/analyze/detect/index.ts +0 -272
  137. package/src/analyze/functional/borrowed.ts +0 -84
  138. package/src/analyze/functional/cadence.ts +0 -66
  139. package/src/analyze/functional/function.ts +0 -184
  140. package/src/analyze/functional/index.ts +0 -23
  141. package/src/analyze/functional/internal.ts +0 -29
  142. package/src/analyze/functional/roman.ts +0 -402
  143. package/src/analyze/index.ts +0 -49
  144. package/src/analyze/timeline/index.ts +0 -451
  145. package/src/analyze/voice/index.ts +0 -339
  146. package/src/core/event-index/index.ts +0 -104
  147. package/src/core/index.ts +0 -61
  148. package/src/core/interval/index.ts +0 -81
  149. package/src/core/meter/index.ts +0 -302
  150. package/src/core/pitch/index.ts +0 -311
  151. package/src/core/random/index.ts +0 -76
  152. package/src/core/tuning/index.ts +0 -186
  153. package/src/core/types.ts +0 -29
  154. package/src/core/validation/index.ts +0 -119
  155. package/src/generate/bass/index.ts +0 -373
  156. package/src/generate/countermelody/index.ts +0 -470
  157. package/src/generate/drums/beat.ts +0 -266
  158. package/src/generate/drums/euclid.ts +0 -65
  159. package/src/generate/drums/fills.ts +0 -356
  160. package/src/generate/drums/ghost.ts +0 -119
  161. package/src/generate/drums/hihat.ts +0 -285
  162. package/src/generate/drums/hit.ts +0 -31
  163. package/src/generate/drums/index.ts +0 -361
  164. package/src/generate/drums/internal.ts +0 -257
  165. package/src/generate/drums/kick.ts +0 -160
  166. package/src/generate/drums/percussion.ts +0 -121
  167. package/src/generate/drums/rng.ts +0 -7
  168. package/src/generate/drums/swing.ts +0 -71
  169. package/src/generate/groove/index.ts +0 -351
  170. package/src/generate/harmonize/index.ts +0 -451
  171. package/src/generate/index.ts +0 -58
  172. package/src/generate/motif/index.ts +0 -422
  173. package/src/generate/progression/index.ts +0 -362
  174. package/src/generate/reharmony/index.ts +0 -323
  175. package/src/generate/rhythm/index.ts +0 -175
  176. package/src/index.ts +0 -13
  177. package/src/model/chord.ts +0 -442
  178. package/src/model/index.ts +0 -15
  179. package/src/model/interval.ts +0 -82
  180. package/src/model/key.ts +0 -266
  181. package/src/model/note.ts +0 -176
  182. package/src/model/progression.ts +0 -161
  183. package/src/model/shared.ts +0 -12
  184. package/src/theory/chord/index.ts +0 -375
  185. package/src/theory/chordscale/index.ts +0 -374
  186. package/src/theory/counterpoint/index.ts +0 -230
  187. package/src/theory/harmony/index.ts +0 -77
  188. package/src/theory/index.ts +0 -112
  189. package/src/theory/safety/index.ts +0 -488
  190. package/src/theory/scale/degrees.ts +0 -123
  191. package/src/theory/scale/index.ts +0 -28
  192. package/src/theory/scale/key.ts +0 -56
  193. package/src/theory/scale/masks.ts +0 -153
  194. package/src/theory/spelling/index.ts +0 -249
  195. package/src/theory/symbol/index.ts +0 -268
  196. package/src/theory/voicing/index.ts +0 -5
  197. package/src/theory/voicing/internal.ts +0 -168
  198. package/src/theory/voicing/leading.ts +0 -112
  199. package/src/theory/voicing/satb.ts +0 -216
  200. package/src/theory/voicing/styled.ts +0 -199
  201. package/tsconfig.json +0 -23
@@ -0,0 +1,261 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+ var _chunkPI3UXRWKcjs = require('./chunk-PI3UXRWK.cjs');
15
+
16
+ // src/core/meter/index.ts
17
+ var EPS = 1e-9;
18
+ function isMultiple(value, unit) {
19
+ if (unit === 0) {
20
+ return false;
21
+ }
22
+ const ratio = value / unit;
23
+ return Math.abs(ratio - Math.round(ratio)) < EPS;
24
+ }
25
+ function parseTimeSignature(text) {
26
+ const match = /^\s*((?:\d+\s*\+\s*)*\d+)\s*\/\s*(\d+)\s*$/.exec(text);
27
+ if (!match) {
28
+ throw new (0, _chunkPI3UXRWKcjs.InvalidInputError)(`Invalid time signature: ${text}`);
29
+ }
30
+ const groupTokens = (_nullishCoalesce(match[1], () => ( ""))).split("+").map((token) => Number.parseInt(token.trim(), 10));
31
+ const numerator = groupTokens.reduce((sum, value) => sum + value, 0);
32
+ const denominator = Number.parseInt(_nullishCoalesce(match[2], () => ( "")), 10);
33
+ if (!Number.isSafeInteger(numerator) || !Number.isSafeInteger(denominator) || numerator <= 0 || denominator <= 0) {
34
+ throw new (0, _chunkPI3UXRWKcjs.InvalidInputError)(`Invalid time signature: ${text}`);
35
+ }
36
+ const grouping = groupTokens.length > 1 ? groupTokens : void 0;
37
+ return _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, { numerator, denominator, grouping }, `time signature ${text}`);
38
+ }
39
+ function formatTimeSignature(ts, opts = {}) {
40
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
41
+ if (opts.grouping === true && ts.grouping !== void 0) {
42
+ return `${ts.grouping.join("+")}/${ts.denominator}`;
43
+ }
44
+ return `${ts.numerator}/${ts.denominator}`;
45
+ }
46
+ function isCompound(ts) {
47
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
48
+ return _chunkPI3UXRWKcjs.isCompoundNumerator.call(void 0, ts.numerator) && !_chunkPI3UXRWKcjs.isAdditiveReading.call(void 0, ts);
49
+ }
50
+ function beatsPerBar(ts) {
51
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
52
+ return _chunkPI3UXRWKcjs.barBeatsOf.call(void 0, ts);
53
+ }
54
+ function pulsesPerBar(ts) {
55
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
56
+ return _chunkPI3UXRWKcjs.pulseCountOf.call(void 0, ts);
57
+ }
58
+ function isGroupHead(grouping, pulseIndex) {
59
+ let acc = 0;
60
+ for (const g of grouping) {
61
+ if (acc === pulseIndex) {
62
+ return true;
63
+ }
64
+ acc += g;
65
+ }
66
+ return false;
67
+ }
68
+ function beatToBarPosition(beatInQuarters, ts) {
69
+ _chunkPI3UXRWKcjs.assertFiniteNumber.call(void 0, beatInQuarters, "beat");
70
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
71
+ const barLen = _chunkPI3UXRWKcjs.barBeatsOf.call(void 0, ts);
72
+ const bar = Math.floor(beatInQuarters / barLen);
73
+ return { bar, beat: beatInQuarters - bar * barLen };
74
+ }
75
+ function pulseBeats(ts) {
76
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
77
+ return _chunkPI3UXRWKcjs.pulseBeatsOf.call(void 0, ts);
78
+ }
79
+ function barPositionToPulse(pos, ts) {
80
+ _chunkPI3UXRWKcjs.assertFiniteNumber.call(void 0, pos.beat, "position.beat");
81
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
82
+ return pos.beat / _chunkPI3UXRWKcjs.pulseBeatsOf.call(void 0, ts) + 1;
83
+ }
84
+ function formatBarPosition(beatInQuarters, ts, decimals = 2) {
85
+ _chunkPI3UXRWKcjs.assertInteger.call(void 0, decimals, "decimals", 0, 100);
86
+ const position = beatToBarPosition(beatInQuarters, ts);
87
+ const pulse = barPositionToPulse(position, ts);
88
+ const rounded = Number(pulse.toFixed(decimals));
89
+ if (rounded >= pulsesPerBar(ts) + 1) {
90
+ return `${position.bar + 2}.1`;
91
+ }
92
+ const wholePulse = Math.floor(rounded);
93
+ const fraction = Number((rounded - wholePulse).toFixed(decimals));
94
+ return fraction === 0 ? `${position.bar + 1}.${wholePulse}` : `${position.bar + 1}.${wholePulse}+${fraction}`;
95
+ }
96
+ function barPositionToBeat(pos, ts) {
97
+ _chunkPI3UXRWKcjs.assertInteger.call(void 0, pos.bar, "bar position bar");
98
+ _chunkPI3UXRWKcjs.assertFiniteNumber.call(void 0, pos.beat, "bar position beat");
99
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
100
+ return pos.bar * _chunkPI3UXRWKcjs.barBeatsOf.call(void 0, ts) + pos.beat;
101
+ }
102
+ function metricWeight(beatInQuarters, ts) {
103
+ _chunkPI3UXRWKcjs.assertFiniteNumber.call(void 0, beatInQuarters, "beat");
104
+ _chunkPI3UXRWKcjs.assertTimeSignature.call(void 0, ts);
105
+ const barLen = _chunkPI3UXRWKcjs.barBeatsOf.call(void 0, ts);
106
+ const beat = beatInQuarters - Math.floor(beatInQuarters / barLen) * barLen;
107
+ const pulse = _chunkPI3UXRWKcjs.pulseBeatsOf.call(void 0, ts);
108
+ if (!isMultiple(beat, pulse)) {
109
+ return 0;
110
+ }
111
+ const pulses = _chunkPI3UXRWKcjs.pulseCountOf.call(void 0, ts);
112
+ const pulseIndex = Math.round(beat / pulse) % pulses;
113
+ if (pulseIndex === 0) {
114
+ return 3;
115
+ }
116
+ const grouping = ts.grouping;
117
+ if (grouping !== void 0) {
118
+ return isGroupHead(grouping, pulseIndex) ? 2 : 1;
119
+ }
120
+ if (pulses % 2 === 0 && pulseIndex === pulses / 2) {
121
+ return 2;
122
+ }
123
+ return 1;
124
+ }
125
+ function isStrongBeat(beatInQuarters, ts) {
126
+ return metricWeight(beatInQuarters, ts) >= 2;
127
+ }
128
+ function tuplet(totalBeats, count) {
129
+ _chunkPI3UXRWKcjs.assertRange.call(void 0, totalBeats, 0, Number.MAX_SAFE_INTEGER, "tuplet total beats");
130
+ _chunkPI3UXRWKcjs.assertPositiveInt.call(void 0, count, "tuplet count");
131
+ return new Array(count).fill(totalBeats / count);
132
+ }
133
+
134
+ // src/core/event-index/index.ts
135
+ var EPS2 = 1e-9;
136
+ function upperBound(values, beat) {
137
+ let low = 0;
138
+ let high = values.length;
139
+ while (low < high) {
140
+ const middle = Math.floor((low + high) / 2);
141
+ if ((_nullishCoalesce(_optionalChain([values, 'access', _ => _[middle], 'optionalAccess', _2 => _2.note, 'access', _3 => _3.startBeat]), () => ( Number.POSITIVE_INFINITY))) <= beat) {
142
+ low = middle + 1;
143
+ } else {
144
+ high = middle;
145
+ }
146
+ }
147
+ return low;
148
+ }
149
+ function createNoteEventIndex(events, options = {}) {
150
+ _chunkPI3UXRWKcjs.assertNoteEvents.call(void 0, events, _nullishCoalesce(options.name, () => ( "note events")), options);
151
+ const tieBreak = _nullishCoalesce(options.tieBreak, () => ( "highest"));
152
+ const notes = Object.freeze(
153
+ events.map((note, originalIndex) => ({
154
+ note: Object.freeze({ ...note }),
155
+ originalIndex,
156
+ endBeat: note.startBeat + note.durationBeat
157
+ })).sort((a, b) => a.note.startBeat - b.note.startBeat || a.originalIndex - b.originalIndex)
158
+ );
159
+ let treeSize = 1;
160
+ while (treeSize < notes.length) treeSize *= 2;
161
+ const maxEndTree = new Array(treeSize * 2).fill(Number.NEGATIVE_INFINITY);
162
+ for (let index = 0; index < notes.length; index += 1) {
163
+ maxEndTree[treeSize + index] = _nullishCoalesce(_optionalChain([notes, 'access', _4 => _4[index], 'optionalAccess', _5 => _5.endBeat]), () => ( Number.NEGATIVE_INFINITY));
164
+ }
165
+ for (let index = treeSize - 1; index > 0; index -= 1) {
166
+ maxEndTree[index] = Math.max(
167
+ _nullishCoalesce(maxEndTree[index * 2], () => ( Number.NEGATIVE_INFINITY)),
168
+ _nullishCoalesce(maxEndTree[index * 2 + 1], () => ( Number.NEGATIVE_INFINITY))
169
+ );
170
+ }
171
+ const latestActiveIndex = (exclusive, beat) => {
172
+ const find = (node, start, end) => {
173
+ if (start >= exclusive || (_nullishCoalesce(maxEndTree[node], () => ( Number.NEGATIVE_INFINITY))) <= beat + EPS2) {
174
+ return -1;
175
+ }
176
+ if (end - start === 1) return start < notes.length ? start : -1;
177
+ const mid = Math.floor((start + end) / 2);
178
+ const right = find(node * 2 + 1, mid, end);
179
+ return right >= 0 ? right : find(node * 2, start, mid);
180
+ };
181
+ return find(1, 0, treeSize);
182
+ };
183
+ return {
184
+ notes,
185
+ at(beat) {
186
+ const index = latestActiveIndex(upperBound(notes, beat + EPS2), beat);
187
+ if (index < 0) return void 0;
188
+ const onset = _optionalChain([notes, 'access', _6 => _6[index], 'optionalAccess', _7 => _7.note, 'access', _8 => _8.startBeat]);
189
+ let best;
190
+ for (let candidateIndex = index; candidateIndex >= 0; candidateIndex -= 1) {
191
+ const indexed = notes[candidateIndex];
192
+ if (indexed !== void 0 && indexed.note.startBeat !== onset) break;
193
+ if (indexed === void 0 || indexed.note.durationBeat <= 0 || indexed.note.startBeat - EPS2 > beat || beat >= indexed.endBeat - EPS2) {
194
+ continue;
195
+ }
196
+ if (best === void 0) {
197
+ best = indexed;
198
+ continue;
199
+ }
200
+ if (tieBreak === "last") continue;
201
+ const higher = indexed.note.pitch > best.note.pitch;
202
+ if (tieBreak === "highest" ? higher : !higher) {
203
+ best = indexed;
204
+ }
205
+ }
206
+ return best;
207
+ },
208
+ attacksAt(beat) {
209
+ let index = upperBound(notes, beat + EPS2) - 1;
210
+ while (index >= 0) {
211
+ const candidate = notes[index];
212
+ if (candidate === void 0 || Math.abs(candidate.note.startBeat - beat) >= EPS2) {
213
+ return false;
214
+ }
215
+ if (candidate.note.durationBeat > 0) {
216
+ return true;
217
+ }
218
+ index -= 1;
219
+ }
220
+ return false;
221
+ },
222
+ onsetsBetween(startBeat, endBeat) {
223
+ const result = [];
224
+ let index = upperBound(notes, startBeat + EPS2);
225
+ while (index < notes.length) {
226
+ const indexed = notes[index];
227
+ const onset = _optionalChain([indexed, 'optionalAccess', _9 => _9.note, 'access', _10 => _10.startBeat]);
228
+ if (indexed === void 0 || onset === void 0 || onset >= endBeat - EPS2) {
229
+ break;
230
+ }
231
+ index += 1;
232
+ if (indexed.note.durationBeat <= 0) {
233
+ continue;
234
+ }
235
+ const previous = result[result.length - 1];
236
+ if (previous === void 0 || Math.abs(previous - onset) >= EPS2) {
237
+ result.push(onset);
238
+ }
239
+ }
240
+ return result;
241
+ }
242
+ };
243
+ }
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+ exports.parseTimeSignature = parseTimeSignature; exports.formatTimeSignature = formatTimeSignature; exports.isCompound = isCompound; exports.beatsPerBar = beatsPerBar; exports.pulsesPerBar = pulsesPerBar; exports.beatToBarPosition = beatToBarPosition; exports.pulseBeats = pulseBeats; exports.barPositionToPulse = barPositionToPulse; exports.formatBarPosition = formatBarPosition; exports.barPositionToBeat = barPositionToBeat; exports.metricWeight = metricWeight; exports.isStrongBeat = isStrongBeat; exports.tuplet = tuplet; exports.createNoteEventIndex = createNoteEventIndex;
261
+ //# sourceMappingURL=chunk-2A2QMA74.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/home/runner/work/libcantus/libcantus/dist/chunk-2A2QMA74.cjs","../src/core/meter/index.ts","../src/core/event-index/index.ts"],"names":["EPS"],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACA;ACqDA,IAAM,IAAA,EAAM,IAAA;AAGZ,SAAS,UAAA,CAAW,KAAA,EAAe,IAAA,EAAuB;AACxD,EAAA,GAAA,CAAI,KAAA,IAAS,CAAA,EAAG;AACd,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,MAAM,MAAA,EAAQ,MAAA,EAAQ,IAAA;AACtB,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,MAAA,EAAQ,IAAA,CAAK,KAAA,CAAM,KAAK,CAAC,EAAA,EAAI,GAAA;AAC/C;AAeO,SAAS,kBAAA,CAAmB,IAAA,EAA6B;AAC9D,EAAA,MAAM,MAAA,EA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;AACnB,QAAA;AACF,MAAA;AACO,MAAA;AACT,IAAA;AACF,EAAA;AACF;AF8DgE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/libcantus/libcantus/dist/chunk-2A2QMA74.cjs","sourcesContent":[null,"/**\n * Meter and time signatures: bar/beat arithmetic, simple vs. compound meters,\n * metric-accent hierarchy, and tuplet subdivision.\n *\n * Positions and durations are measured in quarter-note beats, matching the rest\n * of the library's beat convention (four quarter-note beats per bar in 4/4).\n */\n\nimport { InvalidInputError } from '../errors/index.js';\nimport {\n assertFiniteNumber,\n assertInteger,\n assertPositiveInt,\n assertRange,\n assertTimeSignature,\n} from '../validation/index.js';\nimport {\n barBeatsOf,\n isAdditiveReading,\n isCompoundNumerator,\n pulseBeatsOf,\n pulseCountOf,\n} from './internal.js';\n\n/**\n * A time signature as a numerator over a note-value denominator.\n *\n * @category Rhythm & Meter\n */\nexport type TimeSignature = {\n numerator: number;\n denominator: number;\n /**\n * Optional additive grouping of the bar's main pulses, as the felt-beat\n * lengths in pulses — e.g. `[2, 2, 3]` for a 2+2+3 reading of 7/8, or\n * `[3, 2]` for 5/8. The entries must be positive integers summing to the\n * numerator.\n *\n * A compound numerator accepts both readings. 9/8 groups its three dotted\n * pulses as `[1, 1, 1]`, or reads additively as `[2, 2, 2, 3]` quavers; a\n * grouping that sums to the numerator selects the additive reading, so\n * aksak and other additive metres are expressible. {@link pulsesPerBar}\n * and {@link isCompound} follow whichever reading the grouping selects.\n *\n * When present, the head pulse of each group (other than the downbeat) is\n * treated as a secondary strong pulse by\n * {@link metricWeight}/{@link isStrongBeat}; when absent, all main pulses\n * are weighted equally as flat, evenly divided pulses.\n */\n grouping?: number[];\n};\n\n/**\n * A position expressed as a bar index and a quarter-note offset within the bar.\n *\n * @category Rhythm & Meter\n */\nexport type BarPosition = {\n /** 0-based bar index. {@link formatBarPosition} renders it 1-based. */\n bar: number;\n /**\n * Quarter-note offset from the start of the bar — not the felt beat. In 6/8\n * the second felt beat is at `beat: 1.5`; {@link barPositionToPulse} converts\n * to felt-beat numbering.\n */\n beat: number;\n};\n\nconst EPS = 1e-9;\n\n/** Whether `value` is an integer multiple of `unit` (within a float tolerance). */\nfunction isMultiple(value: number, unit: number): boolean {\n if (unit === 0) {\n return false;\n }\n const ratio = value / unit;\n return Math.abs(ratio - Math.round(ratio)) < EPS;\n}\n\n/**\n * Parse a time signature such as `\"4/4\"` or `\"6/8\"`.\n *\n * @param text The signature text.\n * @returns The parsed time signature.\n * @throws If the text is not `n/d` with positive integers.\n * @example\n * ```ts\n * import { parseTimeSignature } from '@libraz/libcantus';\n * parseTimeSignature('6/8'); // { numerator: 6, denominator: 8 }\n * ```\n * @category Rhythm & Meter\n */\nexport function parseTimeSignature(text: string): TimeSignature {\n const match = /^\\s*((?:\\d+\\s*\\+\\s*)*\\d+)\\s*\\/\\s*(\\d+)\\s*$/.exec(text);\n if (!match) {\n throw new InvalidInputError(`Invalid time signature: ${text}`);\n }\n const groupTokens = (match[1] ?? '').split('+').map((token) => Number.parseInt(token.trim(), 10));\n const numerator = groupTokens.reduce((sum, value) => sum + value, 0);\n const denominator = Number.parseInt(match[2] ?? '', 10);\n if (\n !Number.isSafeInteger(numerator) ||\n !Number.isSafeInteger(denominator) ||\n numerator <= 0 ||\n denominator <= 0\n ) {\n throw new InvalidInputError(`Invalid time signature: ${text}`);\n }\n // The same signature every consumer validates, validated here too: parsing\n // a signature the rest of the library rejects only moves the failure.\n const grouping = groupTokens.length > 1 ? groupTokens : undefined;\n return assertTimeSignature({ numerator, denominator, grouping }, `time signature ${text}`);\n}\n\n/**\n * Render a time signature as `\"n/d\"`, or as `\"a+b+c/d\"` when it carries an\n * additive grouping and `grouping` is requested.\n *\n * The plain form drops the grouping, so a round trip through it reads 7/8 as\n * flat rather than as 2+2+3; ask for the additive form to keep it.\n *\n * @param ts The time signature.\n * @param opts Set `grouping: true` to render an additive grouping.\n * @returns The formatted signature.\n * @example\n * ```ts\n * import { formatTimeSignature } from '@libraz/libcantus';\n * const aksak = { numerator: 7, denominator: 8, grouping: [2, 2, 3] };\n * formatTimeSignature(aksak); // '7/8'\n * formatTimeSignature(aksak, { grouping: true }); // '2+2+3/8'\n * ```\n * @category Rhythm & Meter\n */\nexport function formatTimeSignature(ts: TimeSignature, opts: { grouping?: boolean } = {}): string {\n assertTimeSignature(ts);\n if (opts.grouping === true && ts.grouping !== undefined) {\n return `${ts.grouping.join('+')}/${ts.denominator}`;\n }\n return `${ts.numerator}/${ts.denominator}`;\n}\n\n/**\n * Whether a meter is compound: its main pulses each divide into three, as in\n * 6/8, 9/8, 12/8, or 6/4. Compound meters are those whose numerator is a\n * multiple of three greater than three, independent of the denominator. Meters\n * like 3/8 and 3/4 (simple triples, numerator 3) are not compound, and neither\n * is a signature whose {@link TimeSignature.grouping} selects the additive\n * reading (9/8 as 2+2+2+3).\n *\n * @param ts The time signature.\n * @returns True for compound meters.\n * @category Rhythm & Meter\n */\nexport function isCompound(ts: TimeSignature): boolean {\n assertTimeSignature(ts);\n return isCompoundNumerator(ts.numerator) && !isAdditiveReading(ts);\n}\n\n/**\n * Length of a bar in quarter-note beats.\n *\n * @param ts The time signature.\n * @returns The bar length in quarter notes.\n * @category Rhythm & Meter\n */\nexport function beatsPerBar(ts: TimeSignature): number {\n assertTimeSignature(ts);\n return barBeatsOf(ts);\n}\n\n/**\n * Number of main pulses (felt beats) per bar: the numerator for simple meters,\n * a third of it for compound meters.\n *\n * For additive/irregular meters such as 7/8 or 5/8 this counts the raw pulses\n * (7 and 5 respectively), which are otherwise felt as flat, evenly divided and\n * equally accented pulses. To recover a 2+2+3 or 3+2 felt-beat grouping, set\n * {@link TimeSignature.grouping}; {@link metricWeight} then accents each\n * group's head pulse.\n *\n * @param ts The time signature.\n * @returns The pulse count per bar.\n * @category Rhythm & Meter\n */\nexport function pulsesPerBar(ts: TimeSignature): number {\n assertTimeSignature(ts);\n return pulseCountOf(ts);\n}\n\n/** Whether `pulseIndex` is the head pulse of one of the additive groups. */\nfunction isGroupHead(grouping: number[], pulseIndex: number): boolean {\n let acc = 0;\n for (const g of grouping) {\n if (acc === pulseIndex) {\n return true;\n }\n acc += g;\n }\n return false;\n}\n\n/**\n * Convert an absolute quarter-note position to a bar index and in-bar offset.\n *\n * @param beatInQuarters Absolute position in quarter-note beats.\n * @param ts The time signature.\n * @returns The bar and in-bar quarter-note offset.\n * @example\n * ```ts\n * import { parseTimeSignature, beatToBarPosition } from '@libraz/libcantus';\n * const ts = parseTimeSignature('4/4');\n * beatToBarPosition(5, ts); // { bar: 1, beat: 1 }\n * ```\n * @category Rhythm & Meter\n */\nexport function beatToBarPosition(beatInQuarters: number, ts: TimeSignature): BarPosition {\n assertFiniteNumber(beatInQuarters, 'beat');\n assertTimeSignature(ts);\n const barLen = barBeatsOf(ts);\n const bar = Math.floor(beatInQuarters / barLen);\n return { bar, beat: beatInQuarters - bar * barLen };\n}\n\n/**\n * Length of one felt beat (main pulse) in quarter-note beats.\n *\n * A quarter note in simple meters, a dotted quarter in compound ones, and one\n * denominator unit in an additive metre. Position displays, click tracks, and\n * score export are all written against this length.\n *\n * @param ts The time signature.\n * @returns The pulse length in quarter-note beats.\n * @example\n * ```ts\n * import { parseTimeSignature, pulseBeats } from '@libraz/libcantus';\n * pulseBeats(parseTimeSignature('6/8')); // 1.5\n * ```\n * @category Rhythm & Meter\n */\nexport function pulseBeats(ts: TimeSignature): number {\n assertTimeSignature(ts);\n return pulseBeatsOf(ts);\n}\n\n/**\n * The felt-beat number of a bar position, 1-based.\n *\n * {@link BarPosition.beat} is a quarter-note offset, so in 6/8 the second felt\n * beat reads as 1.5. This is the number a musician says and a position display\n * shows.\n *\n * @param pos The bar position.\n * @param ts The time signature.\n * @returns The 1-based felt-beat number, fractional between pulses.\n * @example\n * ```ts\n * import { barPositionToPulse, parseTimeSignature } from '@libraz/libcantus';\n * barPositionToPulse({ bar: 0, beat: 1.5 }, parseTimeSignature('6/8')); // 2\n * ```\n * @category Rhythm & Meter\n */\nexport function barPositionToPulse(pos: BarPosition, ts: TimeSignature): number {\n assertFiniteNumber(pos.beat, 'position.beat');\n assertTimeSignature(ts);\n return pos.beat / pulseBeatsOf(ts) + 1;\n}\n\n/**\n * Render an absolute position as the `bar.beat` a DAW or a score shows:\n * 1-based bar, 1-based felt beat.\n *\n * @param beatInQuarters Absolute position in quarter-note beats.\n * @param ts The time signature.\n * @param decimals Digits of the fractional beat to keep.\n * @returns The formatted position, e.g. `'3.2'` for bar 3, felt beat 2.\n * @example\n * ```ts\n * import { formatBarPosition, parseTimeSignature } from '@libraz/libcantus';\n * formatBarPosition(7.5, parseTimeSignature('6/8')); // '3.2'\n * ```\n * @category Rhythm & Meter\n */\nexport function formatBarPosition(beatInQuarters: number, ts: TimeSignature, decimals = 2): string {\n assertInteger(decimals, 'decimals', 0, 100);\n const position = beatToBarPosition(beatInQuarters, ts);\n const pulse = barPositionToPulse(position, ts);\n const rounded = Number(pulse.toFixed(decimals));\n if (rounded >= pulsesPerBar(ts) + 1) {\n return `${position.bar + 2}.1`;\n }\n const wholePulse = Math.floor(rounded);\n const fraction = Number((rounded - wholePulse).toFixed(decimals));\n return fraction === 0\n ? `${position.bar + 1}.${wholePulse}`\n : `${position.bar + 1}.${wholePulse}+${fraction}`;\n}\n\n/**\n * Convert a bar index and in-bar offset back to an absolute quarter-note\n * position.\n *\n * @param pos The bar position.\n * @param ts The time signature.\n * @returns The absolute position in quarter-note beats.\n * @category Rhythm & Meter\n */\nexport function barPositionToBeat(pos: BarPosition, ts: TimeSignature): number {\n assertInteger(pos.bar, 'bar position bar');\n assertFiniteNumber(pos.beat, 'bar position beat');\n assertTimeSignature(ts);\n return pos.bar * barBeatsOf(ts) + pos.beat;\n}\n\n/**\n * Metric weight of a position within its bar, on a 0–3 scale:\n * 3 the downbeat, 2 a secondary strong pulse, 1 any other main pulse, and 0 an\n * off-pulse subdivision.\n *\n * The secondary strong pulse is the bar's midpoint in even simple/compound\n * meters. For an additive/irregular meter with {@link TimeSignature.grouping}\n * set (e.g. `[2, 2, 3]` for 7/8), each group's head pulse other than the\n * downbeat is the secondary strong pulse instead; without a grouping every\n * non-downbeat main pulse of such a meter weighs 1 (flat, evenly divided\n * pulses).\n *\n * @param beatInQuarters Absolute or in-bar quarter-note position.\n * @param ts The time signature.\n * @returns The metric weight (0–3).\n * @throws If the time signature is malformed, including a grouping that is not\n * a positive-integer list summing to the pulse count or the numerator.\n * @example\n * ```ts\n * import { parseTimeSignature, metricWeight } from '@libraz/libcantus';\n * const ts = parseTimeSignature('4/4');\n * metricWeight(0, ts); // 3 (the downbeat)\n * ```\n * @category Rhythm & Meter\n */\nexport function metricWeight(beatInQuarters: number, ts: TimeSignature): number {\n assertFiniteNumber(beatInQuarters, 'beat');\n assertTimeSignature(ts);\n const barLen = barBeatsOf(ts);\n const beat = beatInQuarters - Math.floor(beatInQuarters / barLen) * barLen;\n const pulse = pulseBeatsOf(ts);\n if (!isMultiple(beat, pulse)) {\n return 0;\n }\n const pulses = pulseCountOf(ts);\n const pulseIndex = Math.round(beat / pulse) % pulses;\n if (pulseIndex === 0) {\n return 3;\n }\n const grouping = ts.grouping;\n if (grouping !== undefined) {\n return isGroupHead(grouping, pulseIndex) ? 2 : 1;\n }\n if (pulses % 2 === 0 && pulseIndex === pulses / 2) {\n return 2;\n }\n return 1;\n}\n\n/**\n * Whether a position is metrically accented (weight 2 or more — a downbeat or a\n * secondary strong pulse).\n *\n * @param beatInQuarters Absolute or in-bar quarter-note position.\n * @param ts The time signature.\n * @returns True on strong beats.\n * @category Rhythm & Meter\n */\nexport function isStrongBeat(beatInQuarters: number, ts: TimeSignature): boolean {\n return metricWeight(beatInQuarters, ts) >= 2;\n}\n\n/**\n * Subdivide a span into `count` equal tuplet durations (e.g. an eighth-note\n * triplet is `tuplet(1, 3)`).\n *\n * @param totalBeats Total span in quarter-note beats.\n * @param count Number of equal parts.\n * @returns `count` equal durations summing to `totalBeats`.\n * @throws If `count` is not a positive integer.\n * @category Rhythm & Meter\n */\nexport function tuplet(totalBeats: number, count: number): number[] {\n assertRange(totalBeats, 0, Number.MAX_SAFE_INTEGER, 'tuplet total beats');\n assertPositiveInt(count, 'tuplet count');\n return new Array<number>(count).fill(totalBeats / count);\n}\n","import type { NoteEvent } from '../types.js';\nimport { assertNoteEvents } from '../validation/index.js';\n\n/**\n * A validated note retaining its position in the caller's original array.\n *\n * @category Core\n */\nexport type IndexedNoteEvent = {\n note: Readonly<NoteEvent>;\n originalIndex: number;\n endBeat: number;\n};\n\n/**\n * Which note wins when several sound from the same onset.\n *\n * `'highest'` and `'lowest'` name a voice, so the answer does not depend on the\n * order the caller happened to store the chord in; `'last'` takes the one\n * latest in the input array.\n *\n * @category Core\n */\nexport type OnsetTieBreak = 'highest' | 'lowest' | 'last';\n\n/**\n * Binary-searchable, stable-sorted index over note onsets and active spans.\n *\n * @category Core\n */\nexport type NoteEventIndex = {\n notes: readonly IndexedNoteEvent[];\n /**\n * Latest-onset note sounding at `beat`. Simultaneous onsets are resolved by\n * the index's {@link OnsetTieBreak}.\n */\n at: (beat: number) => IndexedNoteEvent | undefined;\n /** Whether one or more sounding notes attack at `beat`. */\n attacksAt: (beat: number) => boolean;\n /** Unique sounding-note attack beats strictly inside `(startBeat, endBeat)`. */\n onsetsBetween: (startBeat: number, endBeat: number) => number[];\n};\n\nconst EPS = 1e-9;\n\nfunction upperBound(values: readonly IndexedNoteEvent[], beat: number): number {\n let low = 0;\n let high = values.length;\n while (low < high) {\n const middle = Math.floor((low + high) / 2);\n if ((values[middle]?.note.startBeat ?? Number.POSITIVE_INFINITY) <= beat) {\n low = middle + 1;\n } else {\n high = middle;\n }\n }\n return low;\n}\n\n/**\n * Validate and stable-sort note events once, then expose logarithmic onset and\n * active-note lookups. Non-positive-duration notes may be retained for callers\n * that intentionally filter them later, but never count as sounding.\n *\n * @category Core\n */\nexport function createNoteEventIndex(\n events: readonly NoteEvent[],\n options: {\n allowNonPositiveDuration?: boolean;\n tieBreak?: OnsetTieBreak;\n budget?: number;\n /** What the events are, for the validation error message. */\n name?: string;\n } = {},\n): NoteEventIndex {\n assertNoteEvents(events, options.name ?? 'note events', options);\n const tieBreak = options.tieBreak ?? 'highest';\n const notes = Object.freeze(\n events\n .map((note, originalIndex) => ({\n note: Object.freeze({ ...note }),\n originalIndex,\n endBeat: note.startBeat + note.durationBeat,\n }))\n .sort((a, b) => a.note.startBeat - b.note.startBeat || a.originalIndex - b.originalIndex),\n );\n // Segment-tree maxima find the latest active onset in logarithmic time even\n // when an early, very long note would defeat a prefix-max backwards scan.\n let treeSize = 1;\n while (treeSize < notes.length) treeSize *= 2;\n const maxEndTree = new Array<number>(treeSize * 2).fill(Number.NEGATIVE_INFINITY);\n for (let index = 0; index < notes.length; index += 1) {\n maxEndTree[treeSize + index] = notes[index]?.endBeat ?? Number.NEGATIVE_INFINITY;\n }\n for (let index = treeSize - 1; index > 0; index -= 1) {\n maxEndTree[index] = Math.max(\n maxEndTree[index * 2] ?? Number.NEGATIVE_INFINITY,\n maxEndTree[index * 2 + 1] ?? Number.NEGATIVE_INFINITY,\n );\n }\n const latestActiveIndex = (exclusive: number, beat: number): number => {\n const find = (node: number, start: number, end: number): number => {\n if (start >= exclusive || (maxEndTree[node] ?? Number.NEGATIVE_INFINITY) <= beat + EPS) {\n return -1;\n }\n if (end - start === 1) return start < notes.length ? start : -1;\n const mid = Math.floor((start + end) / 2);\n const right = find(node * 2 + 1, mid, end);\n return right >= 0 ? right : find(node * 2, start, mid);\n };\n return find(1, 0, treeSize);\n };\n\n return {\n notes,\n at(beat) {\n const index = latestActiveIndex(upperBound(notes, beat + EPS), beat);\n if (index < 0) return undefined;\n const onset = notes[index]?.note.startBeat;\n let best: IndexedNoteEvent | undefined;\n for (let candidateIndex = index; candidateIndex >= 0; candidateIndex -= 1) {\n const indexed = notes[candidateIndex];\n if (indexed !== undefined && indexed.note.startBeat !== onset) break;\n if (\n indexed === undefined ||\n indexed.note.durationBeat <= 0 ||\n indexed.note.startBeat - EPS > beat ||\n beat >= indexed.endBeat - EPS\n ) {\n continue;\n }\n if (best === undefined) {\n best = indexed;\n continue;\n }\n if (tieBreak === 'last') continue;\n const higher = indexed.note.pitch > best.note.pitch;\n if (tieBreak === 'highest' ? higher : !higher) {\n best = indexed;\n }\n }\n return best;\n },\n attacksAt(beat) {\n // Only a sounding note attacks: a zero-length artefact must not make\n // `attacksAt(b)` true on a beat where `at(b)` finds nothing.\n let index = upperBound(notes, beat + EPS) - 1;\n while (index >= 0) {\n const candidate = notes[index];\n if (candidate === undefined || Math.abs(candidate.note.startBeat - beat) >= EPS) {\n return false;\n }\n if (candidate.note.durationBeat > 0) {\n return true;\n }\n index -= 1;\n }\n return false;\n },\n onsetsBetween(startBeat, endBeat) {\n const result: number[] = [];\n let index = upperBound(notes, startBeat + EPS);\n while (index < notes.length) {\n const indexed = notes[index];\n const onset = indexed?.note.startBeat;\n if (indexed === undefined || onset === undefined || onset >= endBeat - EPS) {\n break;\n }\n index += 1;\n if (indexed.note.durationBeat <= 0) {\n continue;\n }\n const previous = result[result.length - 1];\n if (previous === undefined || Math.abs(previous - onset) >= EPS) {\n result.push(onset);\n }\n }\n return result;\n },\n };\n}\n"]}
@@ -0,0 +1,261 @@
1
+ import {
2
+ InvalidInputError,
3
+ assertFiniteNumber,
4
+ assertInteger,
5
+ assertNoteEvents,
6
+ assertPositiveInt,
7
+ assertRange,
8
+ assertTimeSignature,
9
+ barBeatsOf,
10
+ isAdditiveReading,
11
+ isCompoundNumerator,
12
+ pulseBeatsOf,
13
+ pulseCountOf
14
+ } from "./chunk-R2VVFAIN.js";
15
+
16
+ // src/core/meter/index.ts
17
+ var EPS = 1e-9;
18
+ function isMultiple(value, unit) {
19
+ if (unit === 0) {
20
+ return false;
21
+ }
22
+ const ratio = value / unit;
23
+ return Math.abs(ratio - Math.round(ratio)) < EPS;
24
+ }
25
+ function parseTimeSignature(text) {
26
+ const match = /^\s*((?:\d+\s*\+\s*)*\d+)\s*\/\s*(\d+)\s*$/.exec(text);
27
+ if (!match) {
28
+ throw new InvalidInputError(`Invalid time signature: ${text}`);
29
+ }
30
+ const groupTokens = (match[1] ?? "").split("+").map((token) => Number.parseInt(token.trim(), 10));
31
+ const numerator = groupTokens.reduce((sum, value) => sum + value, 0);
32
+ const denominator = Number.parseInt(match[2] ?? "", 10);
33
+ if (!Number.isSafeInteger(numerator) || !Number.isSafeInteger(denominator) || numerator <= 0 || denominator <= 0) {
34
+ throw new InvalidInputError(`Invalid time signature: ${text}`);
35
+ }
36
+ const grouping = groupTokens.length > 1 ? groupTokens : void 0;
37
+ return assertTimeSignature({ numerator, denominator, grouping }, `time signature ${text}`);
38
+ }
39
+ function formatTimeSignature(ts, opts = {}) {
40
+ assertTimeSignature(ts);
41
+ if (opts.grouping === true && ts.grouping !== void 0) {
42
+ return `${ts.grouping.join("+")}/${ts.denominator}`;
43
+ }
44
+ return `${ts.numerator}/${ts.denominator}`;
45
+ }
46
+ function isCompound(ts) {
47
+ assertTimeSignature(ts);
48
+ return isCompoundNumerator(ts.numerator) && !isAdditiveReading(ts);
49
+ }
50
+ function beatsPerBar(ts) {
51
+ assertTimeSignature(ts);
52
+ return barBeatsOf(ts);
53
+ }
54
+ function pulsesPerBar(ts) {
55
+ assertTimeSignature(ts);
56
+ return pulseCountOf(ts);
57
+ }
58
+ function isGroupHead(grouping, pulseIndex) {
59
+ let acc = 0;
60
+ for (const g of grouping) {
61
+ if (acc === pulseIndex) {
62
+ return true;
63
+ }
64
+ acc += g;
65
+ }
66
+ return false;
67
+ }
68
+ function beatToBarPosition(beatInQuarters, ts) {
69
+ assertFiniteNumber(beatInQuarters, "beat");
70
+ assertTimeSignature(ts);
71
+ const barLen = barBeatsOf(ts);
72
+ const bar = Math.floor(beatInQuarters / barLen);
73
+ return { bar, beat: beatInQuarters - bar * barLen };
74
+ }
75
+ function pulseBeats(ts) {
76
+ assertTimeSignature(ts);
77
+ return pulseBeatsOf(ts);
78
+ }
79
+ function barPositionToPulse(pos, ts) {
80
+ assertFiniteNumber(pos.beat, "position.beat");
81
+ assertTimeSignature(ts);
82
+ return pos.beat / pulseBeatsOf(ts) + 1;
83
+ }
84
+ function formatBarPosition(beatInQuarters, ts, decimals = 2) {
85
+ assertInteger(decimals, "decimals", 0, 100);
86
+ const position = beatToBarPosition(beatInQuarters, ts);
87
+ const pulse = barPositionToPulse(position, ts);
88
+ const rounded = Number(pulse.toFixed(decimals));
89
+ if (rounded >= pulsesPerBar(ts) + 1) {
90
+ return `${position.bar + 2}.1`;
91
+ }
92
+ const wholePulse = Math.floor(rounded);
93
+ const fraction = Number((rounded - wholePulse).toFixed(decimals));
94
+ return fraction === 0 ? `${position.bar + 1}.${wholePulse}` : `${position.bar + 1}.${wholePulse}+${fraction}`;
95
+ }
96
+ function barPositionToBeat(pos, ts) {
97
+ assertInteger(pos.bar, "bar position bar");
98
+ assertFiniteNumber(pos.beat, "bar position beat");
99
+ assertTimeSignature(ts);
100
+ return pos.bar * barBeatsOf(ts) + pos.beat;
101
+ }
102
+ function metricWeight(beatInQuarters, ts) {
103
+ assertFiniteNumber(beatInQuarters, "beat");
104
+ assertTimeSignature(ts);
105
+ const barLen = barBeatsOf(ts);
106
+ const beat = beatInQuarters - Math.floor(beatInQuarters / barLen) * barLen;
107
+ const pulse = pulseBeatsOf(ts);
108
+ if (!isMultiple(beat, pulse)) {
109
+ return 0;
110
+ }
111
+ const pulses = pulseCountOf(ts);
112
+ const pulseIndex = Math.round(beat / pulse) % pulses;
113
+ if (pulseIndex === 0) {
114
+ return 3;
115
+ }
116
+ const grouping = ts.grouping;
117
+ if (grouping !== void 0) {
118
+ return isGroupHead(grouping, pulseIndex) ? 2 : 1;
119
+ }
120
+ if (pulses % 2 === 0 && pulseIndex === pulses / 2) {
121
+ return 2;
122
+ }
123
+ return 1;
124
+ }
125
+ function isStrongBeat(beatInQuarters, ts) {
126
+ return metricWeight(beatInQuarters, ts) >= 2;
127
+ }
128
+ function tuplet(totalBeats, count) {
129
+ assertRange(totalBeats, 0, Number.MAX_SAFE_INTEGER, "tuplet total beats");
130
+ assertPositiveInt(count, "tuplet count");
131
+ return new Array(count).fill(totalBeats / count);
132
+ }
133
+
134
+ // src/core/event-index/index.ts
135
+ var EPS2 = 1e-9;
136
+ function upperBound(values, beat) {
137
+ let low = 0;
138
+ let high = values.length;
139
+ while (low < high) {
140
+ const middle = Math.floor((low + high) / 2);
141
+ if ((values[middle]?.note.startBeat ?? Number.POSITIVE_INFINITY) <= beat) {
142
+ low = middle + 1;
143
+ } else {
144
+ high = middle;
145
+ }
146
+ }
147
+ return low;
148
+ }
149
+ function createNoteEventIndex(events, options = {}) {
150
+ assertNoteEvents(events, options.name ?? "note events", options);
151
+ const tieBreak = options.tieBreak ?? "highest";
152
+ const notes = Object.freeze(
153
+ events.map((note, originalIndex) => ({
154
+ note: Object.freeze({ ...note }),
155
+ originalIndex,
156
+ endBeat: note.startBeat + note.durationBeat
157
+ })).sort((a, b) => a.note.startBeat - b.note.startBeat || a.originalIndex - b.originalIndex)
158
+ );
159
+ let treeSize = 1;
160
+ while (treeSize < notes.length) treeSize *= 2;
161
+ const maxEndTree = new Array(treeSize * 2).fill(Number.NEGATIVE_INFINITY);
162
+ for (let index = 0; index < notes.length; index += 1) {
163
+ maxEndTree[treeSize + index] = notes[index]?.endBeat ?? Number.NEGATIVE_INFINITY;
164
+ }
165
+ for (let index = treeSize - 1; index > 0; index -= 1) {
166
+ maxEndTree[index] = Math.max(
167
+ maxEndTree[index * 2] ?? Number.NEGATIVE_INFINITY,
168
+ maxEndTree[index * 2 + 1] ?? Number.NEGATIVE_INFINITY
169
+ );
170
+ }
171
+ const latestActiveIndex = (exclusive, beat) => {
172
+ const find = (node, start, end) => {
173
+ if (start >= exclusive || (maxEndTree[node] ?? Number.NEGATIVE_INFINITY) <= beat + EPS2) {
174
+ return -1;
175
+ }
176
+ if (end - start === 1) return start < notes.length ? start : -1;
177
+ const mid = Math.floor((start + end) / 2);
178
+ const right = find(node * 2 + 1, mid, end);
179
+ return right >= 0 ? right : find(node * 2, start, mid);
180
+ };
181
+ return find(1, 0, treeSize);
182
+ };
183
+ return {
184
+ notes,
185
+ at(beat) {
186
+ const index = latestActiveIndex(upperBound(notes, beat + EPS2), beat);
187
+ if (index < 0) return void 0;
188
+ const onset = notes[index]?.note.startBeat;
189
+ let best;
190
+ for (let candidateIndex = index; candidateIndex >= 0; candidateIndex -= 1) {
191
+ const indexed = notes[candidateIndex];
192
+ if (indexed !== void 0 && indexed.note.startBeat !== onset) break;
193
+ if (indexed === void 0 || indexed.note.durationBeat <= 0 || indexed.note.startBeat - EPS2 > beat || beat >= indexed.endBeat - EPS2) {
194
+ continue;
195
+ }
196
+ if (best === void 0) {
197
+ best = indexed;
198
+ continue;
199
+ }
200
+ if (tieBreak === "last") continue;
201
+ const higher = indexed.note.pitch > best.note.pitch;
202
+ if (tieBreak === "highest" ? higher : !higher) {
203
+ best = indexed;
204
+ }
205
+ }
206
+ return best;
207
+ },
208
+ attacksAt(beat) {
209
+ let index = upperBound(notes, beat + EPS2) - 1;
210
+ while (index >= 0) {
211
+ const candidate = notes[index];
212
+ if (candidate === void 0 || Math.abs(candidate.note.startBeat - beat) >= EPS2) {
213
+ return false;
214
+ }
215
+ if (candidate.note.durationBeat > 0) {
216
+ return true;
217
+ }
218
+ index -= 1;
219
+ }
220
+ return false;
221
+ },
222
+ onsetsBetween(startBeat, endBeat) {
223
+ const result = [];
224
+ let index = upperBound(notes, startBeat + EPS2);
225
+ while (index < notes.length) {
226
+ const indexed = notes[index];
227
+ const onset = indexed?.note.startBeat;
228
+ if (indexed === void 0 || onset === void 0 || onset >= endBeat - EPS2) {
229
+ break;
230
+ }
231
+ index += 1;
232
+ if (indexed.note.durationBeat <= 0) {
233
+ continue;
234
+ }
235
+ const previous = result[result.length - 1];
236
+ if (previous === void 0 || Math.abs(previous - onset) >= EPS2) {
237
+ result.push(onset);
238
+ }
239
+ }
240
+ return result;
241
+ }
242
+ };
243
+ }
244
+
245
+ export {
246
+ parseTimeSignature,
247
+ formatTimeSignature,
248
+ isCompound,
249
+ beatsPerBar,
250
+ pulsesPerBar,
251
+ beatToBarPosition,
252
+ pulseBeats,
253
+ barPositionToPulse,
254
+ formatBarPosition,
255
+ barPositionToBeat,
256
+ metricWeight,
257
+ isStrongBeat,
258
+ tuplet,
259
+ createNoteEventIndex
260
+ };
261
+ //# sourceMappingURL=chunk-2PTFGVD7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/meter/index.ts","../src/core/event-index/index.ts"],"sourcesContent":["/**\n * Meter and time signatures: bar/beat arithmetic, simple vs. compound meters,\n * metric-accent hierarchy, and tuplet subdivision.\n *\n * Positions and durations are measured in quarter-note beats, matching the rest\n * of the library's beat convention (four quarter-note beats per bar in 4/4).\n */\n\nimport { InvalidInputError } from '../errors/index.js';\nimport {\n assertFiniteNumber,\n assertInteger,\n assertPositiveInt,\n assertRange,\n assertTimeSignature,\n} from '../validation/index.js';\nimport {\n barBeatsOf,\n isAdditiveReading,\n isCompoundNumerator,\n pulseBeatsOf,\n pulseCountOf,\n} from './internal.js';\n\n/**\n * A time signature as a numerator over a note-value denominator.\n *\n * @category Rhythm & Meter\n */\nexport type TimeSignature = {\n numerator: number;\n denominator: number;\n /**\n * Optional additive grouping of the bar's main pulses, as the felt-beat\n * lengths in pulses — e.g. `[2, 2, 3]` for a 2+2+3 reading of 7/8, or\n * `[3, 2]` for 5/8. The entries must be positive integers summing to the\n * numerator.\n *\n * A compound numerator accepts both readings. 9/8 groups its three dotted\n * pulses as `[1, 1, 1]`, or reads additively as `[2, 2, 2, 3]` quavers; a\n * grouping that sums to the numerator selects the additive reading, so\n * aksak and other additive metres are expressible. {@link pulsesPerBar}\n * and {@link isCompound} follow whichever reading the grouping selects.\n *\n * When present, the head pulse of each group (other than the downbeat) is\n * treated as a secondary strong pulse by\n * {@link metricWeight}/{@link isStrongBeat}; when absent, all main pulses\n * are weighted equally as flat, evenly divided pulses.\n */\n grouping?: number[];\n};\n\n/**\n * A position expressed as a bar index and a quarter-note offset within the bar.\n *\n * @category Rhythm & Meter\n */\nexport type BarPosition = {\n /** 0-based bar index. {@link formatBarPosition} renders it 1-based. */\n bar: number;\n /**\n * Quarter-note offset from the start of the bar — not the felt beat. In 6/8\n * the second felt beat is at `beat: 1.5`; {@link barPositionToPulse} converts\n * to felt-beat numbering.\n */\n beat: number;\n};\n\nconst EPS = 1e-9;\n\n/** Whether `value` is an integer multiple of `unit` (within a float tolerance). */\nfunction isMultiple(value: number, unit: number): boolean {\n if (unit === 0) {\n return false;\n }\n const ratio = value / unit;\n return Math.abs(ratio - Math.round(ratio)) < EPS;\n}\n\n/**\n * Parse a time signature such as `\"4/4\"` or `\"6/8\"`.\n *\n * @param text The signature text.\n * @returns The parsed time signature.\n * @throws If the text is not `n/d` with positive integers.\n * @example\n * ```ts\n * import { parseTimeSignature } from '@libraz/libcantus';\n * parseTimeSignature('6/8'); // { numerator: 6, denominator: 8 }\n * ```\n * @category Rhythm & Meter\n */\nexport function parseTimeSignature(text: string): TimeSignature {\n const match = /^\\s*((?:\\d+\\s*\\+\\s*)*\\d+)\\s*\\/\\s*(\\d+)\\s*$/.exec(text);\n if (!match) {\n throw new InvalidInputError(`Invalid time signature: ${text}`);\n }\n const groupTokens = (match[1] ?? '').split('+').map((token) => Number.parseInt(token.trim(), 10));\n const numerator = groupTokens.reduce((sum, value) => sum + value, 0);\n const denominator = Number.parseInt(match[2] ?? '', 10);\n if (\n !Number.isSafeInteger(numerator) ||\n !Number.isSafeInteger(denominator) ||\n numerator <= 0 ||\n denominator <= 0\n ) {\n throw new InvalidInputError(`Invalid time signature: ${text}`);\n }\n // The same signature every consumer validates, validated here too: parsing\n // a signature the rest of the library rejects only moves the failure.\n const grouping = groupTokens.length > 1 ? groupTokens : undefined;\n return assertTimeSignature({ numerator, denominator, grouping }, `time signature ${text}`);\n}\n\n/**\n * Render a time signature as `\"n/d\"`, or as `\"a+b+c/d\"` when it carries an\n * additive grouping and `grouping` is requested.\n *\n * The plain form drops the grouping, so a round trip through it reads 7/8 as\n * flat rather than as 2+2+3; ask for the additive form to keep it.\n *\n * @param ts The time signature.\n * @param opts Set `grouping: true` to render an additive grouping.\n * @returns The formatted signature.\n * @example\n * ```ts\n * import { formatTimeSignature } from '@libraz/libcantus';\n * const aksak = { numerator: 7, denominator: 8, grouping: [2, 2, 3] };\n * formatTimeSignature(aksak); // '7/8'\n * formatTimeSignature(aksak, { grouping: true }); // '2+2+3/8'\n * ```\n * @category Rhythm & Meter\n */\nexport function formatTimeSignature(ts: TimeSignature, opts: { grouping?: boolean } = {}): string {\n assertTimeSignature(ts);\n if (opts.grouping === true && ts.grouping !== undefined) {\n return `${ts.grouping.join('+')}/${ts.denominator}`;\n }\n return `${ts.numerator}/${ts.denominator}`;\n}\n\n/**\n * Whether a meter is compound: its main pulses each divide into three, as in\n * 6/8, 9/8, 12/8, or 6/4. Compound meters are those whose numerator is a\n * multiple of three greater than three, independent of the denominator. Meters\n * like 3/8 and 3/4 (simple triples, numerator 3) are not compound, and neither\n * is a signature whose {@link TimeSignature.grouping} selects the additive\n * reading (9/8 as 2+2+2+3).\n *\n * @param ts The time signature.\n * @returns True for compound meters.\n * @category Rhythm & Meter\n */\nexport function isCompound(ts: TimeSignature): boolean {\n assertTimeSignature(ts);\n return isCompoundNumerator(ts.numerator) && !isAdditiveReading(ts);\n}\n\n/**\n * Length of a bar in quarter-note beats.\n *\n * @param ts The time signature.\n * @returns The bar length in quarter notes.\n * @category Rhythm & Meter\n */\nexport function beatsPerBar(ts: TimeSignature): number {\n assertTimeSignature(ts);\n return barBeatsOf(ts);\n}\n\n/**\n * Number of main pulses (felt beats) per bar: the numerator for simple meters,\n * a third of it for compound meters.\n *\n * For additive/irregular meters such as 7/8 or 5/8 this counts the raw pulses\n * (7 and 5 respectively), which are otherwise felt as flat, evenly divided and\n * equally accented pulses. To recover a 2+2+3 or 3+2 felt-beat grouping, set\n * {@link TimeSignature.grouping}; {@link metricWeight} then accents each\n * group's head pulse.\n *\n * @param ts The time signature.\n * @returns The pulse count per bar.\n * @category Rhythm & Meter\n */\nexport function pulsesPerBar(ts: TimeSignature): number {\n assertTimeSignature(ts);\n return pulseCountOf(ts);\n}\n\n/** Whether `pulseIndex` is the head pulse of one of the additive groups. */\nfunction isGroupHead(grouping: number[], pulseIndex: number): boolean {\n let acc = 0;\n for (const g of grouping) {\n if (acc === pulseIndex) {\n return true;\n }\n acc += g;\n }\n return false;\n}\n\n/**\n * Convert an absolute quarter-note position to a bar index and in-bar offset.\n *\n * @param beatInQuarters Absolute position in quarter-note beats.\n * @param ts The time signature.\n * @returns The bar and in-bar quarter-note offset.\n * @example\n * ```ts\n * import { parseTimeSignature, beatToBarPosition } from '@libraz/libcantus';\n * const ts = parseTimeSignature('4/4');\n * beatToBarPosition(5, ts); // { bar: 1, beat: 1 }\n * ```\n * @category Rhythm & Meter\n */\nexport function beatToBarPosition(beatInQuarters: number, ts: TimeSignature): BarPosition {\n assertFiniteNumber(beatInQuarters, 'beat');\n assertTimeSignature(ts);\n const barLen = barBeatsOf(ts);\n const bar = Math.floor(beatInQuarters / barLen);\n return { bar, beat: beatInQuarters - bar * barLen };\n}\n\n/**\n * Length of one felt beat (main pulse) in quarter-note beats.\n *\n * A quarter note in simple meters, a dotted quarter in compound ones, and one\n * denominator unit in an additive metre. Position displays, click tracks, and\n * score export are all written against this length.\n *\n * @param ts The time signature.\n * @returns The pulse length in quarter-note beats.\n * @example\n * ```ts\n * import { parseTimeSignature, pulseBeats } from '@libraz/libcantus';\n * pulseBeats(parseTimeSignature('6/8')); // 1.5\n * ```\n * @category Rhythm & Meter\n */\nexport function pulseBeats(ts: TimeSignature): number {\n assertTimeSignature(ts);\n return pulseBeatsOf(ts);\n}\n\n/**\n * The felt-beat number of a bar position, 1-based.\n *\n * {@link BarPosition.beat} is a quarter-note offset, so in 6/8 the second felt\n * beat reads as 1.5. This is the number a musician says and a position display\n * shows.\n *\n * @param pos The bar position.\n * @param ts The time signature.\n * @returns The 1-based felt-beat number, fractional between pulses.\n * @example\n * ```ts\n * import { barPositionToPulse, parseTimeSignature } from '@libraz/libcantus';\n * barPositionToPulse({ bar: 0, beat: 1.5 }, parseTimeSignature('6/8')); // 2\n * ```\n * @category Rhythm & Meter\n */\nexport function barPositionToPulse(pos: BarPosition, ts: TimeSignature): number {\n assertFiniteNumber(pos.beat, 'position.beat');\n assertTimeSignature(ts);\n return pos.beat / pulseBeatsOf(ts) + 1;\n}\n\n/**\n * Render an absolute position as the `bar.beat` a DAW or a score shows:\n * 1-based bar, 1-based felt beat.\n *\n * @param beatInQuarters Absolute position in quarter-note beats.\n * @param ts The time signature.\n * @param decimals Digits of the fractional beat to keep.\n * @returns The formatted position, e.g. `'3.2'` for bar 3, felt beat 2.\n * @example\n * ```ts\n * import { formatBarPosition, parseTimeSignature } from '@libraz/libcantus';\n * formatBarPosition(7.5, parseTimeSignature('6/8')); // '3.2'\n * ```\n * @category Rhythm & Meter\n */\nexport function formatBarPosition(beatInQuarters: number, ts: TimeSignature, decimals = 2): string {\n assertInteger(decimals, 'decimals', 0, 100);\n const position = beatToBarPosition(beatInQuarters, ts);\n const pulse = barPositionToPulse(position, ts);\n const rounded = Number(pulse.toFixed(decimals));\n if (rounded >= pulsesPerBar(ts) + 1) {\n return `${position.bar + 2}.1`;\n }\n const wholePulse = Math.floor(rounded);\n const fraction = Number((rounded - wholePulse).toFixed(decimals));\n return fraction === 0\n ? `${position.bar + 1}.${wholePulse}`\n : `${position.bar + 1}.${wholePulse}+${fraction}`;\n}\n\n/**\n * Convert a bar index and in-bar offset back to an absolute quarter-note\n * position.\n *\n * @param pos The bar position.\n * @param ts The time signature.\n * @returns The absolute position in quarter-note beats.\n * @category Rhythm & Meter\n */\nexport function barPositionToBeat(pos: BarPosition, ts: TimeSignature): number {\n assertInteger(pos.bar, 'bar position bar');\n assertFiniteNumber(pos.beat, 'bar position beat');\n assertTimeSignature(ts);\n return pos.bar * barBeatsOf(ts) + pos.beat;\n}\n\n/**\n * Metric weight of a position within its bar, on a 0–3 scale:\n * 3 the downbeat, 2 a secondary strong pulse, 1 any other main pulse, and 0 an\n * off-pulse subdivision.\n *\n * The secondary strong pulse is the bar's midpoint in even simple/compound\n * meters. For an additive/irregular meter with {@link TimeSignature.grouping}\n * set (e.g. `[2, 2, 3]` for 7/8), each group's head pulse other than the\n * downbeat is the secondary strong pulse instead; without a grouping every\n * non-downbeat main pulse of such a meter weighs 1 (flat, evenly divided\n * pulses).\n *\n * @param beatInQuarters Absolute or in-bar quarter-note position.\n * @param ts The time signature.\n * @returns The metric weight (0–3).\n * @throws If the time signature is malformed, including a grouping that is not\n * a positive-integer list summing to the pulse count or the numerator.\n * @example\n * ```ts\n * import { parseTimeSignature, metricWeight } from '@libraz/libcantus';\n * const ts = parseTimeSignature('4/4');\n * metricWeight(0, ts); // 3 (the downbeat)\n * ```\n * @category Rhythm & Meter\n */\nexport function metricWeight(beatInQuarters: number, ts: TimeSignature): number {\n assertFiniteNumber(beatInQuarters, 'beat');\n assertTimeSignature(ts);\n const barLen = barBeatsOf(ts);\n const beat = beatInQuarters - Math.floor(beatInQuarters / barLen) * barLen;\n const pulse = pulseBeatsOf(ts);\n if (!isMultiple(beat, pulse)) {\n return 0;\n }\n const pulses = pulseCountOf(ts);\n const pulseIndex = Math.round(beat / pulse) % pulses;\n if (pulseIndex === 0) {\n return 3;\n }\n const grouping = ts.grouping;\n if (grouping !== undefined) {\n return isGroupHead(grouping, pulseIndex) ? 2 : 1;\n }\n if (pulses % 2 === 0 && pulseIndex === pulses / 2) {\n return 2;\n }\n return 1;\n}\n\n/**\n * Whether a position is metrically accented (weight 2 or more — a downbeat or a\n * secondary strong pulse).\n *\n * @param beatInQuarters Absolute or in-bar quarter-note position.\n * @param ts The time signature.\n * @returns True on strong beats.\n * @category Rhythm & Meter\n */\nexport function isStrongBeat(beatInQuarters: number, ts: TimeSignature): boolean {\n return metricWeight(beatInQuarters, ts) >= 2;\n}\n\n/**\n * Subdivide a span into `count` equal tuplet durations (e.g. an eighth-note\n * triplet is `tuplet(1, 3)`).\n *\n * @param totalBeats Total span in quarter-note beats.\n * @param count Number of equal parts.\n * @returns `count` equal durations summing to `totalBeats`.\n * @throws If `count` is not a positive integer.\n * @category Rhythm & Meter\n */\nexport function tuplet(totalBeats: number, count: number): number[] {\n assertRange(totalBeats, 0, Number.MAX_SAFE_INTEGER, 'tuplet total beats');\n assertPositiveInt(count, 'tuplet count');\n return new Array<number>(count).fill(totalBeats / count);\n}\n","import type { NoteEvent } from '../types.js';\nimport { assertNoteEvents } from '../validation/index.js';\n\n/**\n * A validated note retaining its position in the caller's original array.\n *\n * @category Core\n */\nexport type IndexedNoteEvent = {\n note: Readonly<NoteEvent>;\n originalIndex: number;\n endBeat: number;\n};\n\n/**\n * Which note wins when several sound from the same onset.\n *\n * `'highest'` and `'lowest'` name a voice, so the answer does not depend on the\n * order the caller happened to store the chord in; `'last'` takes the one\n * latest in the input array.\n *\n * @category Core\n */\nexport type OnsetTieBreak = 'highest' | 'lowest' | 'last';\n\n/**\n * Binary-searchable, stable-sorted index over note onsets and active spans.\n *\n * @category Core\n */\nexport type NoteEventIndex = {\n notes: readonly IndexedNoteEvent[];\n /**\n * Latest-onset note sounding at `beat`. Simultaneous onsets are resolved by\n * the index's {@link OnsetTieBreak}.\n */\n at: (beat: number) => IndexedNoteEvent | undefined;\n /** Whether one or more sounding notes attack at `beat`. */\n attacksAt: (beat: number) => boolean;\n /** Unique sounding-note attack beats strictly inside `(startBeat, endBeat)`. */\n onsetsBetween: (startBeat: number, endBeat: number) => number[];\n};\n\nconst EPS = 1e-9;\n\nfunction upperBound(values: readonly IndexedNoteEvent[], beat: number): number {\n let low = 0;\n let high = values.length;\n while (low < high) {\n const middle = Math.floor((low + high) / 2);\n if ((values[middle]?.note.startBeat ?? Number.POSITIVE_INFINITY) <= beat) {\n low = middle + 1;\n } else {\n high = middle;\n }\n }\n return low;\n}\n\n/**\n * Validate and stable-sort note events once, then expose logarithmic onset and\n * active-note lookups. Non-positive-duration notes may be retained for callers\n * that intentionally filter them later, but never count as sounding.\n *\n * @category Core\n */\nexport function createNoteEventIndex(\n events: readonly NoteEvent[],\n options: {\n allowNonPositiveDuration?: boolean;\n tieBreak?: OnsetTieBreak;\n budget?: number;\n /** What the events are, for the validation error message. */\n name?: string;\n } = {},\n): NoteEventIndex {\n assertNoteEvents(events, options.name ?? 'note events', options);\n const tieBreak = options.tieBreak ?? 'highest';\n const notes = Object.freeze(\n events\n .map((note, originalIndex) => ({\n note: Object.freeze({ ...note }),\n originalIndex,\n endBeat: note.startBeat + note.durationBeat,\n }))\n .sort((a, b) => a.note.startBeat - b.note.startBeat || a.originalIndex - b.originalIndex),\n );\n // Segment-tree maxima find the latest active onset in logarithmic time even\n // when an early, very long note would defeat a prefix-max backwards scan.\n let treeSize = 1;\n while (treeSize < notes.length) treeSize *= 2;\n const maxEndTree = new Array<number>(treeSize * 2).fill(Number.NEGATIVE_INFINITY);\n for (let index = 0; index < notes.length; index += 1) {\n maxEndTree[treeSize + index] = notes[index]?.endBeat ?? Number.NEGATIVE_INFINITY;\n }\n for (let index = treeSize - 1; index > 0; index -= 1) {\n maxEndTree[index] = Math.max(\n maxEndTree[index * 2] ?? Number.NEGATIVE_INFINITY,\n maxEndTree[index * 2 + 1] ?? Number.NEGATIVE_INFINITY,\n );\n }\n const latestActiveIndex = (exclusive: number, beat: number): number => {\n const find = (node: number, start: number, end: number): number => {\n if (start >= exclusive || (maxEndTree[node] ?? Number.NEGATIVE_INFINITY) <= beat + EPS) {\n return -1;\n }\n if (end - start === 1) return start < notes.length ? start : -1;\n const mid = Math.floor((start + end) / 2);\n const right = find(node * 2 + 1, mid, end);\n return right >= 0 ? right : find(node * 2, start, mid);\n };\n return find(1, 0, treeSize);\n };\n\n return {\n notes,\n at(beat) {\n const index = latestActiveIndex(upperBound(notes, beat + EPS), beat);\n if (index < 0) return undefined;\n const onset = notes[index]?.note.startBeat;\n let best: IndexedNoteEvent | undefined;\n for (let candidateIndex = index; candidateIndex >= 0; candidateIndex -= 1) {\n const indexed = notes[candidateIndex];\n if (indexed !== undefined && indexed.note.startBeat !== onset) break;\n if (\n indexed === undefined ||\n indexed.note.durationBeat <= 0 ||\n indexed.note.startBeat - EPS > beat ||\n beat >= indexed.endBeat - EPS\n ) {\n continue;\n }\n if (best === undefined) {\n best = indexed;\n continue;\n }\n if (tieBreak === 'last') continue;\n const higher = indexed.note.pitch > best.note.pitch;\n if (tieBreak === 'highest' ? higher : !higher) {\n best = indexed;\n }\n }\n return best;\n },\n attacksAt(beat) {\n // Only a sounding note attacks: a zero-length artefact must not make\n // `attacksAt(b)` true on a beat where `at(b)` finds nothing.\n let index = upperBound(notes, beat + EPS) - 1;\n while (index >= 0) {\n const candidate = notes[index];\n if (candidate === undefined || Math.abs(candidate.note.startBeat - beat) >= EPS) {\n return false;\n }\n if (candidate.note.durationBeat > 0) {\n return true;\n }\n index -= 1;\n }\n return false;\n },\n onsetsBetween(startBeat, endBeat) {\n const result: number[] = [];\n let index = upperBound(notes, startBeat + EPS);\n while (index < notes.length) {\n const indexed = notes[index];\n const onset = indexed?.note.startBeat;\n if (indexed === undefined || onset === undefined || onset >= endBeat - EPS) {\n break;\n }\n index += 1;\n if (indexed.note.durationBeat <= 0) {\n continue;\n }\n const previous = result[result.length - 1];\n if (previous === undefined || Math.abs(previous - onset) >= EPS) {\n result.push(onset);\n }\n }\n return result;\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAoEA,IAAM,MAAM;AAGZ,SAAS,WAAW,OAAe,MAAuB;AACxD,MAAI,SAAS,GAAG;AACd,WAAO;AAAA,EACT;AACA,QAAM,QAAQ,QAAQ;AACtB,SAAO,KAAK,IAAI,QAAQ,KAAK,MAAM,KAAK,CAAC,IAAI;AAC/C;AAeO,SAAS,mBAAmB,MAA6B;AAC9D,QAAM,QAAQ,6CAA6C,KAAK,IAAI;AACpE,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,kBAAkB,2B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