@webmusic/kernel 0.1.0 → 0.4.0

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 (197) hide show
  1. package/LICENSE +202 -21
  2. package/NOTICE +2 -0
  3. package/README.md +138 -101
  4. package/dist/audio-context.cjs.map +1 -1
  5. package/dist/build-receipt.json +3 -3
  6. package/dist/chunk-27LH6QLZ.cjs +277 -0
  7. package/dist/chunk-27LH6QLZ.cjs.map +1 -0
  8. package/dist/chunk-3INDOVUC.cjs.map +1 -1
  9. package/dist/chunk-3RR2VIKA.js +393 -0
  10. package/dist/chunk-3RR2VIKA.js.map +1 -0
  11. package/dist/chunk-3YTKIKKX.cjs +378 -0
  12. package/dist/chunk-3YTKIKKX.cjs.map +1 -0
  13. package/dist/chunk-4RG4WQKC.js +360 -0
  14. package/dist/chunk-4RG4WQKC.js.map +1 -0
  15. package/dist/chunk-5AHVQXTF.cjs +620 -0
  16. package/dist/chunk-5AHVQXTF.cjs.map +1 -0
  17. package/dist/chunk-624KWJWZ.js +170 -0
  18. package/dist/chunk-624KWJWZ.js.map +1 -0
  19. package/dist/chunk-6HGFEKD3.cjs +851 -0
  20. package/dist/chunk-6HGFEKD3.cjs.map +1 -0
  21. package/dist/chunk-7ZTZZ5PN.cjs.map +1 -1
  22. package/dist/chunk-ALWV4P7D.js +726 -0
  23. package/dist/chunk-ALWV4P7D.js.map +1 -0
  24. package/dist/chunk-BADJJ6DV.cjs +143 -0
  25. package/dist/chunk-BADJJ6DV.cjs.map +1 -0
  26. package/dist/chunk-C3LMS36L.cjs +393 -0
  27. package/dist/chunk-C3LMS36L.cjs.map +1 -0
  28. package/dist/chunk-C3YWUZW3.cjs.map +1 -1
  29. package/dist/chunk-CR6OUWBJ.cjs +726 -0
  30. package/dist/chunk-CR6OUWBJ.cjs.map +1 -0
  31. package/dist/chunk-CYWDZLWC.js +2141 -0
  32. package/dist/chunk-CYWDZLWC.js.map +1 -0
  33. package/dist/chunk-DDOGX2D5.js +378 -0
  34. package/dist/chunk-DDOGX2D5.js.map +1 -0
  35. package/dist/chunk-DVN6E5U3.js +620 -0
  36. package/dist/chunk-DVN6E5U3.js.map +1 -0
  37. package/dist/chunk-EURMRU2S.js +208 -0
  38. package/dist/chunk-EURMRU2S.js.map +1 -0
  39. package/dist/chunk-EXDCK2VL.cjs +20 -0
  40. package/dist/chunk-EXDCK2VL.cjs.map +1 -0
  41. package/dist/chunk-G5EIUP52.js +114 -0
  42. package/dist/chunk-G5EIUP52.js.map +1 -0
  43. package/dist/chunk-GB4DGKLZ.js +143 -0
  44. package/dist/chunk-GB4DGKLZ.js.map +1 -0
  45. package/dist/chunk-GPOEAYOW.js +2147 -0
  46. package/dist/chunk-GPOEAYOW.js.map +1 -0
  47. package/dist/chunk-I4YMA5VM.cjs +120 -0
  48. package/dist/chunk-I4YMA5VM.cjs.map +1 -0
  49. package/dist/chunk-IVWHMDCB.cjs +4383 -0
  50. package/dist/chunk-IVWHMDCB.cjs.map +1 -0
  51. package/dist/chunk-JFCBJMA7.cjs +2147 -0
  52. package/dist/chunk-JFCBJMA7.cjs.map +1 -0
  53. package/dist/chunk-JLVSAZ3K.cjs +813 -0
  54. package/dist/chunk-JLVSAZ3K.cjs.map +1 -0
  55. package/dist/chunk-K7FYH6UG.cjs +208 -0
  56. package/dist/chunk-K7FYH6UG.cjs.map +1 -0
  57. package/dist/chunk-L4MYCETM.cjs +970 -0
  58. package/dist/chunk-L4MYCETM.cjs.map +1 -0
  59. package/dist/chunk-MNCMYNYL.js +970 -0
  60. package/dist/chunk-MNCMYNYL.js.map +1 -0
  61. package/dist/chunk-NT2BZ3QM.js +120 -0
  62. package/dist/chunk-NT2BZ3QM.js.map +1 -0
  63. package/dist/chunk-O7SA5GLC.js +277 -0
  64. package/dist/chunk-O7SA5GLC.js.map +1 -0
  65. package/dist/chunk-OCKAHITL.cjs +548 -0
  66. package/dist/chunk-OCKAHITL.cjs.map +1 -0
  67. package/dist/{chunk-HOKRTYPV.js → chunk-OPBHDUZK.js} +39 -1
  68. package/dist/chunk-OPBHDUZK.js.map +1 -0
  69. package/dist/chunk-OTXYWCLA.js +206 -0
  70. package/dist/chunk-OTXYWCLA.js.map +1 -0
  71. package/dist/chunk-P6ZA63J7.js +548 -0
  72. package/dist/chunk-P6ZA63J7.js.map +1 -0
  73. package/dist/chunk-QLF3BOST.js +20 -0
  74. package/dist/chunk-QLF3BOST.js.map +1 -0
  75. package/dist/chunk-RCWFRZDR.js +4383 -0
  76. package/dist/chunk-RCWFRZDR.js.map +1 -0
  77. package/dist/chunk-RZBLPM3L.cjs +206 -0
  78. package/dist/chunk-RZBLPM3L.cjs.map +1 -0
  79. package/dist/chunk-TA3TNJZU.cjs +360 -0
  80. package/dist/chunk-TA3TNJZU.cjs.map +1 -0
  81. package/dist/chunk-TZSRPTQL.cjs +2141 -0
  82. package/dist/chunk-TZSRPTQL.cjs.map +1 -0
  83. package/dist/chunk-UMTPJ76S.cjs.map +1 -1
  84. package/dist/chunk-UY77FXDW.js +813 -0
  85. package/dist/chunk-UY77FXDW.js.map +1 -0
  86. package/dist/chunk-VFU3S7OC.js +851 -0
  87. package/dist/chunk-VFU3S7OC.js.map +1 -0
  88. package/dist/chunk-VY6WWZSW.cjs.map +1 -1
  89. package/dist/chunk-WN5DZOVT.cjs +170 -0
  90. package/dist/chunk-WN5DZOVT.cjs.map +1 -0
  91. package/dist/{chunk-HZQX4J2J.cjs → chunk-WOPW6WLR.cjs} +40 -2
  92. package/dist/chunk-WOPW6WLR.cjs.map +1 -0
  93. package/dist/chunk-XBSIYUHM.cjs +114 -0
  94. package/dist/chunk-XBSIYUHM.cjs.map +1 -0
  95. package/dist/chunk-ZMU72HJS.cjs.map +1 -1
  96. package/dist/context-DSa0ymyN.d.cts +664 -0
  97. package/dist/context-DaxfAg3b.d.ts +664 -0
  98. package/dist/effect-chain-Cu03O5Y0.d.ts +91 -0
  99. package/dist/effect-chain-Do75T7uK.d.cts +91 -0
  100. package/dist/effect.cjs +16 -1
  101. package/dist/effect.cjs.map +1 -1
  102. package/dist/effect.d.cts +56 -14
  103. package/dist/effect.d.ts +56 -14
  104. package/dist/effect.js +16 -1
  105. package/dist/elements.cjs +37 -2
  106. package/dist/elements.cjs.map +1 -1
  107. package/dist/elements.d.cts +197 -8
  108. package/dist/elements.d.ts +197 -8
  109. package/dist/elements.js +36 -1
  110. package/dist/events.cjs.map +1 -1
  111. package/dist/index.cjs +556 -9
  112. package/dist/index.cjs.map +1 -1
  113. package/dist/index.d.cts +17 -5
  114. package/dist/index.d.ts +17 -5
  115. package/dist/index.js +567 -20
  116. package/dist/message.cjs +104 -0
  117. package/dist/message.cjs.map +1 -0
  118. package/dist/message.d.cts +738 -0
  119. package/dist/message.d.ts +738 -0
  120. package/dist/message.js +104 -0
  121. package/dist/meter.cjs +2 -2
  122. package/dist/meter.cjs.map +1 -1
  123. package/dist/meter.d.cts +9 -1
  124. package/dist/meter.d.ts +9 -1
  125. package/dist/meter.js +1 -1
  126. package/dist/module-control-C25BMtH7.d.ts +61 -0
  127. package/dist/module-control-yH6nTJM9.d.cts +61 -0
  128. package/dist/module-plugin-BnPmq1VP.d.ts +198 -0
  129. package/dist/module-plugin-CHb-5W0Y.d.cts +198 -0
  130. package/dist/module.cjs +263 -0
  131. package/dist/module.cjs.map +1 -0
  132. package/dist/module.d.cts +1112 -0
  133. package/dist/module.d.ts +1112 -0
  134. package/dist/module.js +263 -0
  135. package/dist/parameter-DwQzFmBR.d.cts +172 -0
  136. package/dist/parameter-DwQzFmBR.d.ts +172 -0
  137. package/dist/player.cjs.map +1 -1
  138. package/dist/processor-worklet.cjs +18 -0
  139. package/dist/processor-worklet.cjs.map +1 -0
  140. package/dist/processor-worklet.d.cts +166 -0
  141. package/dist/processor-worklet.d.ts +166 -0
  142. package/dist/processor-worklet.js +18 -0
  143. package/dist/processor-worklet.js.map +1 -0
  144. package/dist/processor.cjs +51 -0
  145. package/dist/processor.cjs.map +1 -0
  146. package/dist/processor.d.cts +397 -0
  147. package/dist/processor.d.ts +397 -0
  148. package/dist/processor.js +51 -0
  149. package/dist/processor.js.map +1 -0
  150. package/dist/project-types-BU4awMnL.d.ts +323 -0
  151. package/dist/project-types-DufCV8Ma.d.cts +323 -0
  152. package/dist/ring-C-i9N90a.d.cts +139 -0
  153. package/dist/ring-DkcMt2Eg.d.ts +139 -0
  154. package/dist/sequence.cjs +68 -0
  155. package/dist/sequence.cjs.map +1 -0
  156. package/dist/sequence.d.cts +303 -0
  157. package/dist/sequence.d.ts +303 -0
  158. package/dist/sequence.js +68 -0
  159. package/dist/sequence.js.map +1 -0
  160. package/dist/session.cjs +16 -0
  161. package/dist/session.cjs.map +1 -0
  162. package/dist/session.d.cts +407 -0
  163. package/dist/session.d.ts +407 -0
  164. package/dist/session.js +16 -0
  165. package/dist/session.js.map +1 -0
  166. package/dist/tick.cjs.map +1 -1
  167. package/dist/transport.cjs +50 -2
  168. package/dist/transport.cjs.map +1 -1
  169. package/dist/transport.d.cts +145 -1
  170. package/dist/transport.d.ts +145 -1
  171. package/dist/transport.js +51 -3
  172. package/dist/worker.cjs.map +1 -1
  173. package/package.json +62 -11
  174. package/dist/chunk-53YD73OY.js +0 -162
  175. package/dist/chunk-53YD73OY.js.map +0 -1
  176. package/dist/chunk-BR63OD53.cjs +0 -1
  177. package/dist/chunk-BR63OD53.cjs.map +0 -1
  178. package/dist/chunk-HOKRTYPV.js.map +0 -1
  179. package/dist/chunk-HZQX4J2J.cjs.map +0 -1
  180. package/dist/chunk-JNXB74G3.cjs +0 -987
  181. package/dist/chunk-JNXB74G3.cjs.map +0 -1
  182. package/dist/chunk-SM5YPDDI.cjs +0 -162
  183. package/dist/chunk-SM5YPDDI.cjs.map +0 -1
  184. package/dist/chunk-TEM2I2NN.js +0 -1
  185. package/dist/chunk-VVZX7DQ6.cjs +0 -254
  186. package/dist/chunk-VVZX7DQ6.cjs.map +0 -1
  187. package/dist/chunk-WNFGDB7B.js +0 -254
  188. package/dist/chunk-WNFGDB7B.js.map +0 -1
  189. package/dist/chunk-XBJBY4J5.js +0 -987
  190. package/dist/chunk-XBJBY4J5.js.map +0 -1
  191. package/dist/sync.cjs +0 -12
  192. package/dist/sync.cjs.map +0 -1
  193. package/dist/sync.d.cts +0 -304
  194. package/dist/sync.d.ts +0 -304
  195. package/dist/sync.js +0 -12
  196. /package/dist/{chunk-TEM2I2NN.js.map → message.js.map} +0 -0
  197. /package/dist/{sync.js.map → module.js.map} +0 -0
@@ -0,0 +1,851 @@
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
+ var _chunkK7FYH6UGcjs = require('./chunk-K7FYH6UG.cjs');
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ var _chunkCR6OUWBJcjs = require('./chunk-CR6OUWBJ.cjs');
16
+
17
+
18
+
19
+
20
+ var _chunkI4YMA5VMcjs = require('./chunk-I4YMA5VM.cjs');
21
+
22
+
23
+ var _chunkTA3TNJZUcjs = require('./chunk-TA3TNJZU.cjs');
24
+
25
+
26
+
27
+
28
+
29
+
30
+ var _chunk7ZTZZ5PNcjs = require('./chunk-7ZTZZ5PN.cjs');
31
+
32
+ // src/sequence/data.ts
33
+ var SEQUENCE_SCHEMA = "webmusic.sequence";
34
+ var SEQUENCE_VERSION = 1;
35
+ var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
36
+ function identifier(value, what) {
37
+ if (typeof value !== "string" || !value.trim() || value !== value.trim()) throw new TypeError(`${what} must be a nonempty trimmed string.`);
38
+ return value;
39
+ }
40
+ function span(value, what) {
41
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) throw new RangeError(`${what} must be a finite number that is not negative.`);
42
+ return value;
43
+ }
44
+ function json(value, what, depth = 0) {
45
+ if (value === null || typeof value === "boolean" || typeof value === "string") return value;
46
+ if (typeof value === "number") {
47
+ if (!Number.isFinite(value)) throw new RangeError(`${what} must hold finite numbers.`);
48
+ return value;
49
+ }
50
+ if (depth > 32) throw new RangeError(`${what} is nested too deeply.`);
51
+ if (Array.isArray(value)) return Object.freeze(value.map((item) => json(item, what, depth + 1)));
52
+ if (isObject(value)) {
53
+ const copy = {};
54
+ for (const [key, item] of Object.entries(value)) if (item !== void 0) copy[key] = json(item, what, depth + 1);
55
+ return Object.freeze(copy);
56
+ }
57
+ throw new TypeError(`${what} must be JSON.`);
58
+ }
59
+ var byTime = (a, b) => a.time - b.time || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
60
+ function event(input, where) {
61
+ if (!isObject(input)) throw new TypeError(`${where} must be an object.`);
62
+ const id = identifier(input.id, `${where} ID`);
63
+ const time = span(input.time, `${where} time`);
64
+ const value = json(input.value === void 0 ? null : input.value, `${where} value`);
65
+ return Object.freeze(input.duration === void 0 ? { id, time, value } : { id, time, duration: span(input.duration, `${where} duration`), value });
66
+ }
67
+ function track(input, where) {
68
+ if (!isObject(input)) throw new TypeError(`${where} must be an object.`);
69
+ const id = identifier(input.id, `${where} ID`);
70
+ const kind = identifier(input.kind, `${where} kind`);
71
+ if (input.label !== void 0 && typeof input.label !== "string") throw new TypeError(`${where} label must be text.`);
72
+ if (input.muted !== void 0 && typeof input.muted !== "boolean") throw new TypeError(`${where} muted must be true or false.`);
73
+ if (input.events !== void 0 && !Array.isArray(input.events)) throw new TypeError(`${where} events must be a list.`);
74
+ const events = (_nullishCoalesce(input.events, () => ( []))).map((item, index) => event(item, `${where} event ${index}`));
75
+ const seen = /* @__PURE__ */ new Set();
76
+ for (const item of events) {
77
+ if (seen.has(item.id)) throw new Error(`${where} has two events with the ID ${item.id}.`);
78
+ seen.add(item.id);
79
+ }
80
+ return Object.freeze({
81
+ id,
82
+ kind,
83
+ ...input.label === void 0 ? {} : { label: input.label },
84
+ ...input.muted ? { muted: true } : {},
85
+ events: Object.freeze(events.sort(byTime))
86
+ });
87
+ }
88
+ function parseSequence(input) {
89
+ if (!isObject(input)) throw new TypeError("A sequence must be an object.");
90
+ if (input.schema !== SEQUENCE_SCHEMA) throw new TypeError(`A sequence's schema must be ${SEQUENCE_SCHEMA}.`);
91
+ if (input.version !== SEQUENCE_VERSION) throw new RangeError(`Sequence version ${String(input.version)} is not supported; this build reads version ${SEQUENCE_VERSION}.`);
92
+ if (input.unit !== "ticks" && input.unit !== "seconds") throw new TypeError("A sequence's unit must be ticks or seconds.");
93
+ if (!Array.isArray(input.tracks)) throw new TypeError("A sequence's tracks must be a list.");
94
+ const tracks = input.tracks.map((item, index) => track(item, `Track ${index}`));
95
+ const seen = /* @__PURE__ */ new Set();
96
+ for (const item of tracks) {
97
+ if (seen.has(item.id)) throw new Error(`A sequence has two tracks with the ID ${item.id}.`);
98
+ seen.add(item.id);
99
+ }
100
+ return Object.freeze({
101
+ schema: SEQUENCE_SCHEMA,
102
+ version: SEQUENCE_VERSION,
103
+ unit: input.unit,
104
+ ...input.length === void 0 ? {} : { length: span(input.length, "A sequence's length") },
105
+ tracks: Object.freeze(tracks)
106
+ });
107
+ }
108
+ function withIds(existing, added) {
109
+ let next = 0;
110
+ const used = /* @__PURE__ */ new Set();
111
+ const note = (id) => {
112
+ used.add(id);
113
+ const match = /^e(\d+)$/.exec(id);
114
+ if (match) next = Math.max(next, Number(match[1]));
115
+ };
116
+ for (const item of existing) note(item.id);
117
+ for (const item of added) if (item.id !== void 0) note(item.id);
118
+ return added.map((item) => {
119
+ if (item.id !== void 0) return item;
120
+ let id;
121
+ do {
122
+ id = `e${++next}`;
123
+ } while (used.has(id));
124
+ used.add(id);
125
+ return { ...item, id };
126
+ });
127
+ }
128
+ function createSequence(init = {}) {
129
+ return parseSequence({
130
+ schema: SEQUENCE_SCHEMA,
131
+ version: SEQUENCE_VERSION,
132
+ unit: _nullishCoalesce(init.unit, () => ( "ticks")),
133
+ ...init.length === void 0 ? {} : { length: init.length },
134
+ tracks: (_nullishCoalesce(init.tracks, () => ( []))).map((item) => ({ ...item, events: withIds([], _nullishCoalesce(item.events, () => ( []))) }))
135
+ });
136
+ }
137
+ function trackOf(sequence, trackId) {
138
+ const found = sequence.tracks.find((item) => item.id === trackId);
139
+ if (!found) throw new Error(`The sequence has no track ${trackId}.`);
140
+ return found;
141
+ }
142
+ function replaceTrack(sequence, trackId, change) {
143
+ trackOf(sequence, trackId);
144
+ return parseSequence({ ...sequence, tracks: sequence.tracks.map((item) => item.id === trackId ? change(item) : item) });
145
+ }
146
+ function addTrack(sequence, init, index = sequence.tracks.length) {
147
+ const tracks = [...sequence.tracks];
148
+ tracks.splice(Math.max(0, Math.min(tracks.length, Math.trunc(index))), 0, { ...init, events: withIds([], _nullishCoalesce(init.events, () => ( []))) });
149
+ return parseSequence({ ...sequence, tracks });
150
+ }
151
+ function removeTrack(sequence, trackId) {
152
+ trackOf(sequence, trackId);
153
+ return parseSequence({ ...sequence, tracks: sequence.tracks.filter((item) => item.id !== trackId) });
154
+ }
155
+ function updateTrack(sequence, trackId, change) {
156
+ return replaceTrack(sequence, trackId, (item) => ({ ...item, ...change }));
157
+ }
158
+ function insertEvents(sequence, trackId, events) {
159
+ return replaceTrack(sequence, trackId, (item) => ({ ...item, events: [...item.events, ...withIds(item.events, events)] }));
160
+ }
161
+ function removeEvents(sequence, trackId, ids) {
162
+ const gone = new Set(ids);
163
+ return replaceTrack(sequence, trackId, (item) => ({ ...item, events: item.events.filter((entry) => !gone.has(entry.id)) }));
164
+ }
165
+ function updateEvents(sequence, trackId, ids, change) {
166
+ const chosen = ids === void 0 ? void 0 : new Set(ids);
167
+ return replaceTrack(sequence, trackId, (item) => ({ ...item, events: item.events.map((entry) => {
168
+ if (chosen && !chosen.has(entry.id)) return entry;
169
+ return { ...entry, ...typeof change === "function" ? change(entry) : change, id: entry.id };
170
+ }) }));
171
+ }
172
+ function shiftEvents(sequence, trackId, ids, delta) {
173
+ if (!Number.isFinite(delta)) throw new RangeError("A shift must be a finite number.");
174
+ return updateEvents(sequence, trackId, ids, (entry) => ({ time: Math.max(0, entry.time + delta) }));
175
+ }
176
+ function quantizeEvents(sequence, trackId, ids, grid, options = {}) {
177
+ if (!(grid > 0) || !Number.isFinite(grid)) throw new RangeError("A quantize grid must be a positive number.");
178
+ const strength = _nullishCoalesce(options.strength, () => ( 1));
179
+ if (!(strength >= 0 && strength <= 1)) throw new RangeError("Quantize strength must be from 0 to 1.");
180
+ return updateEvents(sequence, trackId, ids, (entry) => {
181
+ const time = Math.max(0, entry.time + (Math.round(entry.time / grid) * grid - entry.time) * strength);
182
+ return options.durations && entry.duration !== void 0 ? { time, duration: Math.max(grid, Math.round(entry.duration / grid) * grid) } : { time };
183
+ });
184
+ }
185
+ function setSequenceLength(sequence, length) {
186
+ const { length: _previous, ...rest } = sequence;
187
+ return parseSequence(length === void 0 ? rest : { ...rest, length });
188
+ }
189
+ function sequenceEnd(sequence) {
190
+ if (sequence.length !== void 0) return sequence.length;
191
+ let end = 0;
192
+ for (const item of sequence.tracks) for (const entry of item.events) end = Math.max(end, entry.time + (_nullishCoalesce(entry.duration, () => ( 0))));
193
+ return end;
194
+ }
195
+ function eventIndexAt(events, time, inclusive = true) {
196
+ let low = 0, high = events.length;
197
+ while (low < high) {
198
+ const middle = low + high >>> 1;
199
+ const before = inclusive ? events[middle].time < time : events[middle].time <= time;
200
+ if (before) low = middle + 1;
201
+ else high = middle;
202
+ }
203
+ return low;
204
+ }
205
+ function eventsBetween(track2, start, end) {
206
+ return track2.events.slice(eventIndexAt(track2.events, start), eventIndexAt(track2.events, end));
207
+ }
208
+ function sliceSequence(sequence, start, end) {
209
+ if (!(end >= start) || !(start >= 0) || !Number.isFinite(end)) throw new RangeError("A slice needs 0 <= start <= end.");
210
+ return parseSequence({
211
+ ...sequence,
212
+ length: end - start,
213
+ tracks: sequence.tracks.map((item) => ({ ...item, events: eventsBetween(item, start, end).map((entry) => ({
214
+ ...entry,
215
+ time: entry.time - start,
216
+ ...entry.duration === void 0 ? {} : { duration: Math.min(entry.duration, end - entry.time) }
217
+ })) }))
218
+ });
219
+ }
220
+ function sequenceSeconds(sequence, time, tempo) {
221
+ return sequence.unit === "ticks" ? time * _chunkTA3TNJZUcjs.tickSeconds.call(void 0, tempo) : time;
222
+ }
223
+ function sequenceTime(sequence, seconds, tempo) {
224
+ return sequence.unit === "ticks" ? seconds / _chunkTA3TNJZUcjs.tickSeconds.call(void 0, tempo) : seconds;
225
+ }
226
+ function sequenceNote(value) {
227
+ if (typeof value === "number") return value >= 0 && value <= 127 ? { pitch: value } : void 0;
228
+ if (!isObject(value)) return void 0;
229
+ const { pitch, velocity } = value;
230
+ if (typeof pitch !== "number" || !(pitch >= 0 && pitch <= 127)) return void 0;
231
+ if (velocity === void 0) return { pitch };
232
+ return typeof velocity === "number" && velocity >= 0 && velocity <= 1 ? { pitch, velocity } : void 0;
233
+ }
234
+
235
+ // src/sequence/document.ts
236
+ var _sequence, _revision, _disposed, _limit, _undo, _redo, _listeners, _onListenerError, _snapshot, _SequenceDocument_instances, alive_fn, install_fn;
237
+ var SequenceDocument = class {
238
+ /** `initial` is validated as untrusted JSON; an empty sequence in ticks when omitted. */
239
+ constructor(initial, options = {}) {
240
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _SequenceDocument_instances);
241
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _sequence);
242
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _revision, 0);
243
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _disposed, false);
244
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _limit);
245
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _undo, []);
246
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _redo, []);
247
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _listeners, /* @__PURE__ */ new Set());
248
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _onListenerError);
249
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _snapshot);
250
+ const limit = _nullishCoalesce(options.historyLimit, () => ( 100));
251
+ if (!Number.isInteger(limit) || limit < 0) throw new RangeError("historyLimit must be an integer that is not negative.");
252
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _limit, limit);
253
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _onListenerError, options.onListenerError);
254
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _sequence, initial === void 0 ? createSequence() : parseSequence(initial));
255
+ }
256
+ get disposed() {
257
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed);
258
+ }
259
+ /** The committed state; the same object until the next change. */
260
+ get snapshot() {
261
+ return _nullishCoalesce(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _snapshot), () => ( _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _snapshot, Object.freeze({
262
+ sequence: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _sequence),
263
+ revision: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _revision),
264
+ canUndo: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _undo).length > 0,
265
+ canRedo: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _redo).length > 0
266
+ }))));
267
+ }
268
+ /**
269
+ * Replace the sequence with `next`, or with what `next` makes of the
270
+ * current one. The result is validated before anything changes; a change
271
+ * that throws leaves the document as it was. Returning the current
272
+ * sequence commits nothing. Returns the revision now current.
273
+ */
274
+ commit(next) {
275
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequenceDocument_instances, alive_fn).call(this);
276
+ const candidate = typeof next === "function" ? next(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _sequence)) : next;
277
+ if (candidate === _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _sequence)) return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _revision);
278
+ const sequence = parseSequence(candidate);
279
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _limit) > 0) {
280
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _undo).push(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _sequence));
281
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _undo).length > _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _limit)) _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _undo).shift();
282
+ }
283
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _redo).length = 0;
284
+ return _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequenceDocument_instances, install_fn).call(this, sequence);
285
+ }
286
+ /** Return to the sequence before the last commit. False when there is none. */
287
+ undo() {
288
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequenceDocument_instances, alive_fn).call(this);
289
+ const previous = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _undo).pop();
290
+ if (!previous) return false;
291
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _redo).push(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _sequence));
292
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequenceDocument_instances, install_fn).call(this, previous);
293
+ return true;
294
+ }
295
+ /** Reapply the commit last undone. False when there is none. */
296
+ redo() {
297
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequenceDocument_instances, alive_fn).call(this);
298
+ const next = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _redo).pop();
299
+ if (!next) return false;
300
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _undo).push(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _sequence));
301
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequenceDocument_instances, install_fn).call(this, next);
302
+ return true;
303
+ }
304
+ /** Called after every change, with the new state already readable from `snapshot`. */
305
+ subscribe(listener) {
306
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequenceDocument_instances, alive_fn).call(this);
307
+ if (typeof listener !== "function") throw new TypeError("Listener must be a function.");
308
+ const entry = { listener };
309
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _listeners).add(entry);
310
+ return () => {
311
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _listeners).delete(entry);
312
+ };
313
+ }
314
+ /** Drop history and listeners. The last snapshot stays readable; changes throw. */
315
+ dispose() {
316
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed)) return;
317
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _disposed, true);
318
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _undo).length = 0;
319
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _redo).length = 0;
320
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _listeners).clear();
321
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _snapshot, void 0);
322
+ }
323
+ };
324
+ _sequence = new WeakMap();
325
+ _revision = new WeakMap();
326
+ _disposed = new WeakMap();
327
+ _limit = new WeakMap();
328
+ _undo = new WeakMap();
329
+ _redo = new WeakMap();
330
+ _listeners = new WeakMap();
331
+ _onListenerError = new WeakMap();
332
+ _snapshot = new WeakMap();
333
+ _SequenceDocument_instances = new WeakSet();
334
+ alive_fn = function() {
335
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed)) throw new Error("SequenceDocument is disposed.");
336
+ };
337
+ install_fn = function(sequence) {
338
+ var _a;
339
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _sequence, sequence);
340
+ _chunk7ZTZZ5PNcjs.__privateWrapper.call(void 0, this, _revision)._++;
341
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _snapshot, void 0);
342
+ for (const entry of [..._chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _listeners)]) {
343
+ if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _listeners).has(entry)) continue;
344
+ try {
345
+ entry.listener();
346
+ } catch (error) {
347
+ try {
348
+ (_a = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _onListenerError)) == null ? void 0 : _a.call(this, error);
349
+ } catch (e) {
350
+ }
351
+ }
352
+ }
353
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _revision);
354
+ };
355
+
356
+ // src/sequence/player.ts
357
+ var LATE_LIMIT = 0.1;
358
+ var _options, _catchUp, _loop, _offset, _running, _disposed2, _cancel, _release, _mark, _live, _ended, _pausedAt, _SequencePlayer_instances, alive_fn2, guard_fn, break_fn, schedule_fn, next_fn, emit_fn;
359
+ var SequencePlayer = class {
360
+ constructor(options) {
361
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _SequencePlayer_instances);
362
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _options);
363
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _catchUp);
364
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _loop);
365
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _offset);
366
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _running, false);
367
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _disposed2, false);
368
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _cancel);
369
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _release);
370
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _mark);
371
+ /** Whether sinks may hold work from a timeline that is still current. */
372
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _live, false);
373
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _ended, false);
374
+ /** Where the transport stood when it was last seen not playing. */
375
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _pausedAt);
376
+ if (typeof _optionalChain([options, 'optionalAccess', _2 => _2.read]) !== "function") throw new TypeError("A sequence player needs read().");
377
+ if (!options.time || !options.clock || !options.sinks) throw new TypeError("A sequence player needs a time source, a clock and sinks.");
378
+ const offset = _nullishCoalesce(options.offset, () => ( 0)), catchUp = _nullishCoalesce(options.catchUp, () => ( 0.02));
379
+ if (!Number.isFinite(offset)) throw new RangeError("offset must be finite.");
380
+ if (!(catchUp >= 0) || !Number.isFinite(catchUp)) throw new RangeError("catchUp must be a finite number that is not negative.");
381
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _options, options);
382
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _loop, _nullishCoalesce(options.loop, () => ( false)));
383
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _offset, offset);
384
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _catchUp, catchUp);
385
+ }
386
+ get running() {
387
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running);
388
+ }
389
+ get disposed() {
390
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed2);
391
+ }
392
+ get loop() {
393
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _loop);
394
+ }
395
+ set loop(value) {
396
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, alive_fn2).call(this);
397
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _loop) === value) return;
398
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _loop, value);
399
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ended, false);
400
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
401
+ }
402
+ get offset() {
403
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _offset);
404
+ }
405
+ set offset(value) {
406
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, alive_fn2).call(this);
407
+ if (!Number.isFinite(value)) throw new RangeError("offset must be finite.");
408
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _offset) === value) return;
409
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _offset, value);
410
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, break_fn).call(this);
411
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
412
+ }
413
+ /** Follow the transport: events play while it plays. Starting twice changes nothing. */
414
+ start() {
415
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, alive_fn2).call(this);
416
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) return;
417
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running, true);
418
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _release, _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options).time.subscribe(() => {
419
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
420
+ }));
421
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
422
+ }
423
+ /** Stop following the transport and release what the sinks hold. */
424
+ stop() {
425
+ var _a, _b;
426
+ if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) return;
427
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running, false);
428
+ (_a = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _release)) == null ? void 0 : _a.call(this);
429
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _release, void 0);
430
+ (_b = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _cancel)) == null ? void 0 : _b.call(this);
431
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _cancel, void 0);
432
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, break_fn).call(this);
433
+ }
434
+ /** The sequence changed: schedule again from what `read()` returns now. Sounding events finish. */
435
+ refresh() {
436
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, alive_fn2).call(this);
437
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ended, false);
438
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
439
+ }
440
+ dispose() {
441
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed2)) return;
442
+ this.stop();
443
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _disposed2, true);
444
+ }
445
+ };
446
+ _options = new WeakMap();
447
+ _catchUp = new WeakMap();
448
+ _loop = new WeakMap();
449
+ _offset = new WeakMap();
450
+ _running = new WeakMap();
451
+ _disposed2 = new WeakMap();
452
+ _cancel = new WeakMap();
453
+ _release = new WeakMap();
454
+ _mark = new WeakMap();
455
+ _live = new WeakMap();
456
+ _ended = new WeakMap();
457
+ _pausedAt = new WeakMap();
458
+ _SequencePlayer_instances = new WeakSet();
459
+ alive_fn2 = function() {
460
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed2)) throw new Error("SequencePlayer is disposed.");
461
+ };
462
+ guard_fn = function(work) {
463
+ try {
464
+ work();
465
+ } catch (error) {
466
+ try {
467
+ _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options), 'access', _3 => _3.onError, 'optionalCall', _4 => _4(error)]);
468
+ } catch (e2) {
469
+ }
470
+ }
471
+ };
472
+ /** Forget the continuation point and tell the sinks the timeline broke. */
473
+ break_fn = function() {
474
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _mark, void 0);
475
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ended, false);
476
+ if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _live)) return;
477
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _live, false);
478
+ const now = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options).clock.now();
479
+ for (const sink of new Set(Object.values(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options).sinks))) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, guard_fn).call(this, () => _optionalChain([sink, 'access', _5 => _5.invalidate, 'optionalCall', _6 => _6(now)]));
480
+ };
481
+ schedule_fn = function() {
482
+ var _a;
483
+ (_a = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _cancel)) == null ? void 0 : _a.call(this);
484
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _cancel, void 0);
485
+ const { time, clock } = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options);
486
+ if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) {
487
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, break_fn).call(this);
488
+ return;
489
+ }
490
+ if (!time.playing()) {
491
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _pausedAt, time.position());
492
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, break_fn).call(this);
493
+ return;
494
+ }
495
+ const resumed = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _pausedAt) !== void 0 && Math.abs(time.position() - _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _pausedAt)) < 1e-6;
496
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _pausedAt, void 0);
497
+ let sequence;
498
+ try {
499
+ sequence = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options).read();
500
+ } catch (error) {
501
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, guard_fn).call(this, () => {
502
+ throw error;
503
+ });
504
+ return;
505
+ }
506
+ const scale = sequenceSeconds(sequence, 1, time.tempo());
507
+ const end = sequenceEnd(sequence);
508
+ const length = end * scale;
509
+ const looping = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _loop) && length >= 1e-3;
510
+ const mark = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mark);
511
+ let recent;
512
+ if (mark) {
513
+ const when2 = time.timeAt(mark.position);
514
+ if (when2 === void 0 || Math.abs(when2 - mark.when) > 1e-4 || mark.scale !== scale) {
515
+ recent = mark;
516
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, break_fn).call(this);
517
+ }
518
+ }
519
+ let pass, cursor, inclusive, exact;
520
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mark)) {
521
+ pass = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mark).pass;
522
+ cursor = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mark).time;
523
+ inclusive = false;
524
+ exact = cursor;
525
+ } else {
526
+ const local = time.position() - _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _offset);
527
+ pass = looping && local > 0 ? Math.floor(local / length) : 0;
528
+ const within = Math.max(0, local - pass * length);
529
+ exact = within / scale;
530
+ cursor = Math.max(0, (within - (resumed ? 0 : _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _catchUp))) / scale - 1e-9);
531
+ inclusive = true;
532
+ }
533
+ let next = _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, next_fn).call(this, sequence, cursor, inclusive, end);
534
+ if (next !== void 0 && recent && next < exact - 1e-9 && next === recent.time && clock.now() - recent.fired < _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _catchUp)) {
535
+ next = _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, next_fn).call(this, sequence, next, false, end);
536
+ }
537
+ if (next === void 0 && looping) {
538
+ const wrapped = _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, next_fn).call(this, sequence, 0, true, end);
539
+ if (wrapped === void 0) return;
540
+ pass++;
541
+ next = wrapped;
542
+ }
543
+ if (next === void 0) {
544
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _ended) || !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options).onEnd) return;
545
+ const when2 = time.timeAt(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _offset) + length);
546
+ if (when2 === void 0) return;
547
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _cancel, clock.schedule(when2, (at2) => {
548
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _cancel, void 0);
549
+ if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running) || _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _ended)) return;
550
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ended, true);
551
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, guard_fn).call(this, () => _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options).onEnd(at2));
552
+ }));
553
+ return;
554
+ }
555
+ const at = next, position = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _offset) + pass * length + at * scale;
556
+ const when = time.timeAt(position);
557
+ if (when === void 0) return;
558
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mark) && clock.now() - when > LATE_LIMIT) {
559
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, break_fn).call(this);
560
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
561
+ return;
562
+ }
563
+ const group = pass;
564
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _cancel, clock.schedule(when, (fired) => {
565
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _cancel, void 0);
566
+ if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running)) return;
567
+ if (clock.now() - when > LATE_LIMIT) {
568
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, break_fn).call(this);
569
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
570
+ return;
571
+ }
572
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _mark, { pass: group, time: at, position, when, scale, fired: clock.now() });
573
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _live, true);
574
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, emit_fn).call(this, sequence, at, fired, group, position, scale);
575
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, schedule_fn).call(this);
576
+ }));
577
+ };
578
+ /** The earliest start among unmuted tracks at or after `cursor`, before the sequence's end. */
579
+ next_fn = function(sequence, cursor, inclusive, end) {
580
+ let found;
581
+ for (const track2 of sequence.tracks) {
582
+ if (track2.muted) continue;
583
+ const entry = track2.events[eventIndexAt(track2.events, cursor, inclusive)];
584
+ if (!entry) continue;
585
+ if (sequence.length !== void 0 && entry.time >= end) continue;
586
+ if (found === void 0 || entry.time < found) found = entry.time;
587
+ }
588
+ return found;
589
+ };
590
+ emit_fn = function(sequence, at, fired, pass, position, scale) {
591
+ const { time, sinks } = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _options);
592
+ for (const track2 of sequence.tracks) {
593
+ if (track2.muted) continue;
594
+ const sink = _nullishCoalesce(sinks[track2.kind], () => ( sinks["*"]));
595
+ if (!sink) continue;
596
+ const events = track2.events;
597
+ for (let index = eventIndexAt(events, at); index < events.length && events[index].time === at; index++) {
598
+ const event2 = events[index];
599
+ let duration;
600
+ if (event2.duration !== void 0) {
601
+ const until = time.timeAt(position + event2.duration * scale);
602
+ const from = time.timeAt(position);
603
+ duration = until !== void 0 && from !== void 0 ? Math.max(0, until - from) : event2.duration * scale;
604
+ }
605
+ const played = Object.freeze(duration === void 0 ? { track: track2, event: event2, time: fired, pass } : { track: track2, event: event2, time: fired, duration, pass });
606
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _SequencePlayer_instances, guard_fn).call(this, () => sink.event(played));
607
+ }
608
+ }
609
+ };
610
+ function createNoteSink(options) {
611
+ const { port, clock } = options;
612
+ const sourceId = _nullishCoalesce(options.sourceId, () => ( port.id));
613
+ const defaultDuration = _nullishCoalesce(options.defaultDuration, () => ( 0.1)), defaultVelocity = _nullishCoalesce(options.defaultVelocity, () => ( 0.8));
614
+ const pending = /* @__PURE__ */ new Map();
615
+ let count = 0;
616
+ let disposed = false;
617
+ const release = (id, time) => {
618
+ const held = pending.get(id);
619
+ if (!held) return;
620
+ pending.delete(id);
621
+ held.cancel();
622
+ if (!port.disposed && port.held.some((event2) => event2.occurrenceId === id)) port.emit({ ...held.on, velocity: 0, gate: "off", time: Math.max(time, held.on.time) });
623
+ };
624
+ return {
625
+ event(played) {
626
+ if (disposed || port.disposed) return;
627
+ const note = sequenceNote(played.event.value);
628
+ if (!note) return;
629
+ const occurrenceId = `${sourceId}-${played.track.id}-${++count}`;
630
+ const on = {
631
+ occurrenceId,
632
+ sourceId,
633
+ pitch: { midi: note.pitch },
634
+ velocity: _nullishCoalesce(note.velocity, () => ( defaultVelocity)),
635
+ gate: "on",
636
+ axis: "audio-context-seconds",
637
+ time: played.time,
638
+ epoch: _nullishCoalesce(_optionalChain([options, 'access', _7 => _7.epoch, 'optionalCall', _8 => _8()]), () => ( 0))
639
+ };
640
+ port.emit(on);
641
+ const end = played.time + Math.max(1e-3, _nullishCoalesce(played.duration, () => ( defaultDuration)));
642
+ pending.set(occurrenceId, { on, cancel: clock.schedule(end, (at) => release(occurrenceId, at)) });
643
+ },
644
+ invalidate(time) {
645
+ for (const id of [...pending.keys()]) release(id, time);
646
+ },
647
+ dispose() {
648
+ if (disposed) return;
649
+ for (const id of [...pending.keys()]) release(id, clock.now());
650
+ disposed = true;
651
+ }
652
+ };
653
+ }
654
+ function createMessageSink(send) {
655
+ return {
656
+ event(played) {
657
+ const value = played.event.value;
658
+ if (typeof value === "number") send(played.track, _chunkCR6OUWBJcjs.message.call(void 0, "float", [_chunkCR6OUWBJcjs.floatAtom.call(void 0, value)], played.time), played);
659
+ else if (typeof value === "string" && value.trim()) send(played.track, _chunkCR6OUWBJcjs.parseMessage.call(void 0, value, played.time), played);
660
+ }
661
+ };
662
+ }
663
+
664
+ // src/sequence/sequencer.ts
665
+ var SEQUENCE_DOCUMENT = _chunkI4YMA5VMcjs.createCapabilityKey.call(void 0, "webmusic.sequence-document");
666
+ function sequenceDocumentOf(module) {
667
+ return _optionalChain([module, 'optionalAccess', _9 => _9.capabilities, 'optionalAccess', _10 => _10.get, 'call', _11 => _11(SEQUENCE_DOCUMENT)]);
668
+ }
669
+ var number = (atom) => _chunkCR6OUWBJcjs.isNumberAtom.call(void 0, atom) ? atom.value : void 0;
670
+ var word = (atom) => _optionalChain([atom, 'optionalAccess', _12 => _12.type]) === "symbol" ? atom.value : _chunkCR6OUWBJcjs.isNumberAtom.call(void 0, atom) ? String(atom.value) : void 0;
671
+ function ensureTrack(sequence, id, kind) {
672
+ const found = sequence.tracks.find((track2) => track2.id === id);
673
+ if (!found) return addTrack(sequence, { id, kind });
674
+ if (found.kind !== kind) throw new Error(`Track ${id} holds ${found.kind} events, not ${kind}.`);
675
+ return sequence;
676
+ }
677
+ var _player, _noteSink, _unsubscribe, _capabilities, _running2, _Sequencer_instances, follow_fn, receive_fn;
678
+ var _Sequencer = class _Sequencer extends _chunkCR6OUWBJcjs.GraphModule {
679
+ constructor(options = {}) {
680
+ super(_Sequencer.type, options);
681
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _Sequencer_instances);
682
+ this.inputs = { control: this.input("control", "start, stop, loop, note, event, length, clear", (value) => _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _Sequencer_instances, receive_fn).call(this, value)) };
683
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _player);
684
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _noteSink);
685
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _unsubscribe);
686
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _capabilities);
687
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _running2);
688
+ const musical = this.musicalTime;
689
+ if (!musical) throw new TypeError("A sequencer needs the musical time of a Session; this graph has none.");
690
+ this.document = new SequenceDocument(options.sequence === void 0 ? void 0 : options.sequence);
691
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running2, typeof options.running === "boolean" ? options.running : true);
692
+ this.notes = new (0, _chunkK7FYH6UGcjs.NoteOutputPort)({ id: "notes", onListenerError: (error) => this.fail(error) });
693
+ this.addPort(this.notes, { id: "notes", label: "notes", kind: "note", direction: "output" });
694
+ this.outputs = { events: this.output("events", "track message\u2026"), done: this.output("done", "bang at the end") };
695
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _capabilities, new (0, _chunkI4YMA5VMcjs.ModuleCapabilities)().with(SEQUENCE_DOCUMENT, this.document));
696
+ const clock = this.clock;
697
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _noteSink, createNoteSink({ port: this.notes, clock, sourceId: this.id, epoch: () => _nullishCoalesce(_optionalChain([this, 'access', _13 => _13.context, 'optionalAccess', _14 => _14.session, 'optionalAccess', _15 => _15.scheduling, 'access', _16 => _16.epoch]), () => ( 0)) }));
698
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _player, new SequencePlayer({
699
+ read: () => this.document.snapshot.sequence,
700
+ time: musical,
701
+ clock,
702
+ sinks: {
703
+ note: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _noteSink),
704
+ message: createMessageSink((track2, value) => this.outputs.events.send(_chunkCR6OUWBJcjs.message.call(void 0, track2.id, _chunkCR6OUWBJcjs.atomsOf.call(void 0, value), value.time)))
705
+ },
706
+ loop: options.loop === true,
707
+ onEnd: (time) => this.outputs.done.send(_chunkCR6OUWBJcjs.bang.call(void 0, time)),
708
+ onError: (error) => this.fail(error)
709
+ }));
710
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _unsubscribe, this.document.subscribe(() => {
711
+ if (!this.disposed) _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).refresh();
712
+ }));
713
+ }
714
+ get capabilities() {
715
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _capabilities);
716
+ }
717
+ /** Whether it follows the transport. */
718
+ get running() {
719
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running2);
720
+ }
721
+ get loop() {
722
+ return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).loop;
723
+ }
724
+ set loop(value) {
725
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).loop = value;
726
+ }
727
+ /** Follow the transport. Activates the module if it is not yet active. */
728
+ start() {
729
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running2, true);
730
+ if (!this.active) this.activate();
731
+ else _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _Sequencer_instances, follow_fn).call(this);
732
+ }
733
+ stop() {
734
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running2, false);
735
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _Sequencer_instances, follow_fn).call(this);
736
+ }
737
+ exportOptions() {
738
+ return { sequence: this.document.snapshot.sequence, running: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running2), loop: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).loop };
739
+ }
740
+ onActivate() {
741
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _Sequencer_instances, follow_fn).call(this);
742
+ }
743
+ onDispose() {
744
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _unsubscribe).call(this);
745
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).dispose();
746
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _noteSink).dispose();
747
+ this.notes.dispose();
748
+ this.document.dispose();
749
+ }
750
+ };
751
+ _player = new WeakMap();
752
+ _noteSink = new WeakMap();
753
+ _unsubscribe = new WeakMap();
754
+ _capabilities = new WeakMap();
755
+ _running2 = new WeakMap();
756
+ _Sequencer_instances = new WeakSet();
757
+ follow_fn = function() {
758
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _running2) && this.active && !this.disposed) _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).start();
759
+ else _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).stop();
760
+ };
761
+ receive_fn = function(value) {
762
+ const atoms = value.atoms;
763
+ const document = this.document;
764
+ switch (value.selector) {
765
+ case "start":
766
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running2, true);
767
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _Sequencer_instances, follow_fn).call(this);
768
+ return;
769
+ case "stop":
770
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running2, false);
771
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _Sequencer_instances, follow_fn).call(this);
772
+ return;
773
+ case "int":
774
+ case "float":
775
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _running2, number(atoms[0]) !== 0);
776
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _Sequencer_instances, follow_fn).call(this);
777
+ return;
778
+ case "loop":
779
+ _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _player).loop = number(atoms[0]) !== 0;
780
+ return;
781
+ case "clear":
782
+ document.commit((current) => ({ ...current, tracks: current.tracks.map((track2) => ({ ...track2, events: [] })) }));
783
+ return;
784
+ case "length":
785
+ document.commit((current) => setSequenceLength(current, number(atoms[0])));
786
+ return;
787
+ case "note": {
788
+ const track2 = word(atoms[0]), time = number(atoms[1]), duration = number(atoms[2]), pitch = number(atoms[3]);
789
+ if (track2 === void 0 || time === void 0 || duration === void 0 || pitch === void 0) throw new Error("sequencer: note needs track time duration pitch [velocity].");
790
+ const velocity = number(atoms[4]);
791
+ document.commit((current) => insertEvents(ensureTrack(current, track2, "note"), track2, [{
792
+ time,
793
+ duration,
794
+ value: velocity === void 0 ? { pitch } : { pitch, velocity: Math.min(1, Math.max(0, velocity / 127)) }
795
+ }]));
796
+ return;
797
+ }
798
+ case "event": {
799
+ const track2 = word(atoms[0]), time = number(atoms[1]);
800
+ if (track2 === void 0 || time === void 0 || atoms.length < 3) throw new Error("sequencer: event needs track time message\u2026.");
801
+ const text = _chunkCR6OUWBJcjs.formatMessage.call(void 0, _chunkCR6OUWBJcjs.messageFromAtoms.call(void 0, atoms.slice(2)));
802
+ document.commit((current) => insertEvents(ensureTrack(current, track2, "message"), track2, [{ time, value: text }]));
803
+ return;
804
+ }
805
+ default:
806
+ throw new Error(`A sequencer does not understand ${value.selector}.`);
807
+ }
808
+ };
809
+ _Sequencer.type = "webmusic.sequencer";
810
+ var Sequencer = _Sequencer;
811
+ function sequencerPlugin(environment = {}) {
812
+ return _chunkCR6OUWBJcjs.modulePlugin.call(void 0, Sequencer.type, (options) => {
813
+ try {
814
+ return new Sequencer(options);
815
+ } catch (error) {
816
+ throw new (0, _chunkI4YMA5VMcjs.ProjectModuleUnavailableError)(error instanceof Error ? error.message : String(error), { cause: error });
817
+ }
818
+ }, environment);
819
+ }
820
+
821
+
822
+
823
+
824
+
825
+
826
+
827
+
828
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
844
+
845
+
846
+
847
+
848
+
849
+
850
+ exports.SEQUENCE_SCHEMA = SEQUENCE_SCHEMA; exports.SEQUENCE_VERSION = SEQUENCE_VERSION; exports.parseSequence = parseSequence; exports.createSequence = createSequence; exports.addTrack = addTrack; exports.removeTrack = removeTrack; exports.updateTrack = updateTrack; exports.insertEvents = insertEvents; exports.removeEvents = removeEvents; exports.updateEvents = updateEvents; exports.shiftEvents = shiftEvents; exports.quantizeEvents = quantizeEvents; exports.setSequenceLength = setSequenceLength; exports.sequenceEnd = sequenceEnd; exports.eventIndexAt = eventIndexAt; exports.eventsBetween = eventsBetween; exports.sliceSequence = sliceSequence; exports.sequenceSeconds = sequenceSeconds; exports.sequenceTime = sequenceTime; exports.sequenceNote = sequenceNote; exports.SequenceDocument = SequenceDocument; exports.SequencePlayer = SequencePlayer; exports.createNoteSink = createNoteSink; exports.createMessageSink = createMessageSink; exports.SEQUENCE_DOCUMENT = SEQUENCE_DOCUMENT; exports.sequenceDocumentOf = sequenceDocumentOf; exports.Sequencer = Sequencer; exports.sequencerPlugin = sequencerPlugin;
851
+ //# sourceMappingURL=chunk-6HGFEKD3.cjs.map