@lewismoten/gamemaker-sandbox-engine 3.1.0 → 3.3.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 (108) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/NOTICE.md +58 -753
  3. package/README.md +94 -10
  4. package/dist/assets/{music.worklet-NqOeNo6T.js → music.worklet-aD2x7RRM.js} +23 -21
  5. package/dist/audio/cmfLoad.js +10 -0
  6. package/dist/audio/cmfPlayer.js +201 -0
  7. package/dist/audio/context.js +75 -0
  8. package/dist/audio/engine.js +173 -0
  9. package/dist/audio/mix.js +40 -0
  10. package/dist/audio/opl.js +245 -0
  11. package/dist/audio/output.js +40 -0
  12. package/dist/core/gameFiles.js +89 -0
  13. package/dist/engine/character.js +187 -0
  14. package/dist/engine/funcKeys.js +164 -0
  15. package/dist/engine/game.js +133 -0
  16. package/dist/engine/keys.js +68 -0
  17. package/dist/engine/machine.js +192 -0
  18. package/dist/engine/menus.js +135 -0
  19. package/dist/engine/monsters.js +259 -0
  20. package/dist/engine/playGain.js +10 -0
  21. package/dist/engine/playerLook.js +48 -0
  22. package/dist/engine/recorder.js +127 -0
  23. package/dist/engine/render.js +45 -0
  24. package/dist/engine/scene.js +324 -0
  25. package/dist/engine/scroll.js +79 -0
  26. package/dist/engine/showGraphic.js +107 -0
  27. package/dist/engine/slowPc.js +98 -0
  28. package/dist/engine/slowPcMusic.js +46 -0
  29. package/dist/engine/touch.js +61 -0
  30. package/dist/engine/world.js +205 -0
  31. package/dist/formats/binary.js +120 -0
  32. package/dist/formats/blocks.js +103 -0
  33. package/dist/formats/character.js +83 -0
  34. package/dist/formats/cmf.js +37 -0
  35. package/dist/formats/cmfEdit.js +154 -0
  36. package/dist/formats/cmfPreview.js +89 -0
  37. package/dist/formats/cmfSong.js +190 -0
  38. package/dist/formats/fli.js +340 -0
  39. package/dist/formats/fliFrames.js +34 -0
  40. package/dist/formats/fliLimits.js +54 -0
  41. package/dist/formats/game.js +198 -0
  42. package/dist/formats/gif.js +125 -0
  43. package/dist/formats/gifProgressive.js +99 -0
  44. package/dist/formats/graphicScripts.js +44 -0
  45. package/dist/formats/highscore.js +30 -0
  46. package/dist/formats/jpeg.js +359 -0
  47. package/dist/formats/map.js +38 -0
  48. package/dist/formats/monster.js +69 -0
  49. package/dist/formats/palette.js +58 -0
  50. package/dist/formats/recording.js +17 -0
  51. package/dist/formats/sound.js +44 -0
  52. package/dist/formats/voc.js +303 -0
  53. package/dist/gfx/colorVision.js +45 -0
  54. package/dist/gfx/cp437Glyphs.js +337 -0
  55. package/dist/gfx/font8x8.js +10 -0
  56. package/dist/gfx/screen.js +151 -0
  57. package/dist/index.js +15 -0
  58. package/dist/input/chords.js +46 -0
  59. package/dist/input/deskMouse.js +24 -0
  60. package/dist/input/keyboard.js +82 -0
  61. package/dist/input/scancodes.js +143 -0
  62. package/dist/player/fileExtensions.js +118 -0
  63. package/dist/player/patchRules.js +129 -0
  64. package/dist/player/patchTable.js +1753 -0
  65. package/dist/text/cp437.js +44 -0
  66. package/dist/types/audio/cmfPlayer.d.ts +1 -1
  67. package/dist/types/audio/engine.d.ts +21 -0
  68. package/dist/types/audio/mix.d.ts +14 -0
  69. package/dist/types/audio/music.worklet.d.ts +8 -0
  70. package/dist/types/engine/character.d.ts +3 -3
  71. package/dist/types/engine/funcKeys.d.ts +8 -6
  72. package/dist/types/engine/game.d.ts +9 -4
  73. package/dist/types/engine/keys.d.ts +2 -2
  74. package/dist/types/engine/machine.d.ts +8 -4
  75. package/dist/types/engine/menus.d.ts +4 -4
  76. package/dist/types/engine/monsters.d.ts +1 -1
  77. package/dist/types/engine/playerLook.d.ts +20 -2
  78. package/dist/types/engine/recorder.d.ts +8 -1
  79. package/dist/types/engine/render.d.ts +2 -2
  80. package/dist/types/engine/scene.d.ts +41 -9
  81. package/dist/types/engine/scroll.d.ts +9 -1
  82. package/dist/types/engine/showGraphic.d.ts +2 -2
  83. package/dist/types/engine/slowPc.d.ts +64 -2
  84. package/dist/types/engine/slowPcMusic.d.ts +18 -0
  85. package/dist/types/engine/touch.d.ts +2 -2
  86. package/dist/types/engine/world.d.ts +28 -6
  87. package/dist/types/formats/cmfEdit.d.ts +53 -0
  88. package/dist/types/formats/cmfPreview.d.ts +19 -0
  89. package/dist/types/formats/cmfSong.d.ts +74 -0
  90. package/dist/types/formats/fli.d.ts +11 -0
  91. package/dist/types/formats/fliFrames.d.ts +18 -0
  92. package/dist/types/formats/fliLimits.d.ts +13 -0
  93. package/dist/types/formats/game.d.ts +7 -0
  94. package/dist/types/formats/gif.d.ts +1 -1
  95. package/dist/types/formats/gifProgressive.d.ts +46 -0
  96. package/dist/types/formats/jpeg.d.ts +20 -0
  97. package/dist/types/gfx/colorVision.d.ts +26 -0
  98. package/dist/types/gfx/screen.d.ts +4 -0
  99. package/dist/types/index.d.ts +4 -0
  100. package/dist/types/input/keyboard.d.ts +1 -1
  101. package/dist/types/{dosPlayer → player}/patchRules.d.ts +1 -1
  102. package/dist/types/{dosPlayer → player}/patchTable.d.ts +8 -5
  103. package/dist/types/text/cp437.d.ts +6 -0
  104. package/package.json +322 -9
  105. package/dist/engine.js +0 -5436
  106. package/dist/types/engine/package.d.ts +0 -4
  107. /package/dist/types/{project → core}/gameFiles.d.ts +0 -0
  108. /package/dist/types/{dosPlayer → player}/fileExtensions.d.ts +0 -0
@@ -0,0 +1,83 @@
1
+ import { Reader as d, Writer as A } from "./binary.js";
2
+ const r = 20, u = 10, a = 10, c = r + 10, S = 32767, s = 3784, b = 0, q = 1, v = 2, C = 3, I = 4, Q = ["Idle", "Die", "Hurt", "Pick up", "Drop"];
3
+ function k(t) {
4
+ return { x: t.i16(), y: t.i16(), orient: t.u16(), pic1: t.u16(), pic2: t.u16(), pause: t.u16(), extra: t.bytesOf(5) };
5
+ }
6
+ function w(t, e) {
7
+ t.i16(e.x).i16(e.y).u16(e.orient).u16(e.pic1).u16(e.pic2).u16(e.pause).bytes(e.extra, 5);
8
+ }
9
+ function M(t) {
10
+ const e = [];
11
+ for (let n = 0; n < u; n++) e.push(k(t));
12
+ return {
13
+ frames: e,
14
+ key: t.u16(),
15
+ sound: t.i8(),
16
+ on: t.i8(),
17
+ bloc: t.u8(),
18
+ ground: t.i8(),
19
+ jstkBut: t.i8(),
20
+ jstkMov: t.i8(),
21
+ norepeat: t.i8(),
22
+ momentum: t.i8(),
23
+ xtra: t.bytesOf(5)
24
+ };
25
+ }
26
+ function R(t, e) {
27
+ for (const n of e.frames) w(t, n);
28
+ t.u16(e.key).i8(e.sound).i8(e.on).u8(e.bloc).i8(e.ground).i8(e.jstkBut).i8(e.jstkMov).i8(e.norepeat).i8(e.momentum).bytes(e.xtra, 5);
29
+ }
30
+ function U(t) {
31
+ const e = new d(t), n = [];
32
+ for (let o = 0; o < r; o++) n.push(M(e));
33
+ const i = [e.i16(), e.i16(), e.i16()], p = [e.i16(), e.i16(), e.i16()], y = e.i8(), E = e.i8(), l = e.bytesOf(a), f = [];
34
+ for (let o = 0; o < c; o++) f.push(e.i16());
35
+ const x = e.bytesOf(s);
36
+ return { sequences: n, x: i, y: p, cframe: y, cseq: E, inv: l, meter: f, rest: x };
37
+ }
38
+ function j(t) {
39
+ const e = new A(s * 2);
40
+ for (const n of t.sequences) R(e, n);
41
+ for (const n of t.x) e.i16(n);
42
+ for (const n of t.y) e.i16(n);
43
+ e.i8(t.cframe).i8(t.cseq).bytes(t.inv, a);
44
+ for (const n of t.meter) e.i16(n);
45
+ return e.bytes(t.rest, s), e.toBytes();
46
+ }
47
+ function m(t = !1) {
48
+ return { x: 0, y: 0, orient: 0, pic1: 50, pic2: t ? 51 : 50, pause: 1, extra: new Uint8Array(5) };
49
+ }
50
+ function D() {
51
+ const t = [];
52
+ for (let n = 0; n < r; n++) {
53
+ const i = Array.from({ length: u }, () => m(!0));
54
+ n === b && (i[0] = { ...m(), pic1: 0, pic2: 1, pause: 2 }), t.push({ frames: i, key: 0, sound: 30, on: 0, bloc: 150, ground: 0, jstkBut: 0, jstkMov: 0, norepeat: 0, momentum: 0, xtra: new Uint8Array(5) });
55
+ }
56
+ const e = new Array(c).fill(S);
57
+ e[0] = 100, e[1] = 3;
58
+ for (let n = r; n < c; n++) e[n] = 1;
59
+ return { sequences: t, x: [0, 0, 0], y: [0, 0, 0], cframe: 0, cseq: 0, inv: new Uint8Array(a).fill(150), meter: e, rest: new Uint8Array(s) };
60
+ }
61
+ function O(t) {
62
+ const e = t.frames.findIndex((n) => n.pic2 === 51);
63
+ return e < 0 ? u : e;
64
+ }
65
+ export {
66
+ s as CHR_RECORD_SIZE,
67
+ q as DIESEQ,
68
+ I as DROPSEQ,
69
+ v as HURTSEQ,
70
+ b as IDLESEQ,
71
+ S as INF_REPEAT,
72
+ u as MAXFRAME,
73
+ a as MAXINV,
74
+ r as MAXSEQ,
75
+ c as METERS,
76
+ C as PICKUPSEQ,
77
+ Q as SEQUENCE_NAMES,
78
+ m as emptyFrame,
79
+ O as frameCount,
80
+ D as newCharacter,
81
+ U as parseCharacter,
82
+ j as serializeCharacter
83
+ };
@@ -0,0 +1,37 @@
1
+ import { Reader as k } from "./binary.js";
2
+ const h = 146;
3
+ function U(t) {
4
+ return t[0] === 67 && t[1] === 84 && t[2] === 77 && t[3] === 70;
5
+ }
6
+ function w(t) {
7
+ if (!U(t)) throw new Error("Not a CMF file");
8
+ const n = new k(t, 4), i = n.u16(), l = n.u16(), o = n.u16(), u = n.u16(), a = n.u16();
9
+ n.skip(6);
10
+ const f = n.bytesOf(16), r = n.u16(), e = n.u16(), c = [];
11
+ for (let s = 0; s < r; s++) c.push(new k(t, l + s * 16).bytesOf(16));
12
+ return { version: i, ticksPerBeat: u, clockTicks: a, channelUsed: f, instruments: c, basicTempo: e, music: t.subarray(o) };
13
+ }
14
+ function p(t) {
15
+ const n = [t & 127];
16
+ for (t >>>= 7; t > 0; t >>>= 7) n.unshift(t & 127 | 128);
17
+ return n;
18
+ }
19
+ function E(t) {
20
+ const i = t.instruments.slice(0, 128), l = new Uint8Array(16), o = [];
21
+ let u = 0;
22
+ for (const c of [...t.events].sort((s, m) => s.tick - m.tick)) {
23
+ const s = Math.max(u, Math.round(c.tick));
24
+ o.push(...p(s - u), ...c.data), u = s;
25
+ const m = c.data[0];
26
+ m >= 128 && m < 240 && (l[m & 15] = 1);
27
+ }
28
+ o.push(0, 255, 47, 0);
29
+ const a = 40, f = a + i.length * 16, r = new Uint8Array(f + o.length), e = new DataView(r.buffer);
30
+ return r.set([67, 84, 77, 70]), e.setUint16(4, 257, !0), e.setUint16(6, a, !0), e.setUint16(8, f, !0), e.setUint16(10, t.ticksPerBeat ?? 48, !0), e.setUint16(12, h, !0), r.set(l, 20), e.setUint16(36, i.length, !0), e.setUint16(38, t.basicTempo ?? 488, !0), i.forEach((c, s) => r.set(c.subarray(0, 16), a + s * 16)), r.set(o, f), r;
31
+ }
32
+ export {
33
+ h as CMF_GAMEMAKER_CLOCK,
34
+ E as encodeCmf,
35
+ U as isCmf,
36
+ w as parseCmf
37
+ };
@@ -0,0 +1,154 @@
1
+ import { RHYTHM_CHANNELS as h, streamEvents as p } from "./cmfSong.js";
2
+ const v = { off: -1e9, mode: -6e8, program: -5e8, on: 1e9 };
3
+ function r(e, t) {
4
+ return v[t] + ++e.counter;
5
+ }
6
+ function d(e, t) {
7
+ return e.notes.filter((o) => o.on.ch === t).sort((o, n) => o.on.tick - n.on.tick || o.on.seq - n.on.seq);
8
+ }
9
+ function b(e, t, o, n, f, i = l) {
10
+ const c = { type: "on", tick: Math.max(0, Math.round(n)), seq: r(e, "on"), ch: t, a: o, b: a(i) }, m = { type: "off", tick: c.tick + Math.max(1, Math.round(f)), seq: r(e, "off"), ch: t, a: o, b: 0 }, u = { on: c, off: m };
11
+ return e.notes.push(u), s(e), u;
12
+ }
13
+ const l = 100;
14
+ function a(e) {
15
+ return Math.max(1, Math.min(127, Math.round(Number.isFinite(e) ? e : l)));
16
+ }
17
+ function x(e, t) {
18
+ const o = a(t);
19
+ for (const n of e) n.on.b = o;
20
+ }
21
+ function y(e, t, o = 0) {
22
+ let n = 0;
23
+ for (const f of e) {
24
+ const i = a(f.on.b * t + o);
25
+ i !== f.on.b && n++, f.on.b = i;
26
+ }
27
+ return n;
28
+ }
29
+ function E(e, t) {
30
+ return 63 - Math.floor((Math.min(127, e) + 128) * (63 - (t & 63)) / 256);
31
+ }
32
+ function C(e, t, o, n, f) {
33
+ o = Math.max(0, Math.round(o)), n = Math.max(1, Math.round(n));
34
+ const i = o !== t.on.tick;
35
+ i && (t.on.tick = o, t.on.seq = r(e, "on"), t.on.status = void 0), f !== t.on.a && (t.on.a = f, t.on.status = void 0);
36
+ const c = o + n;
37
+ t.off ? (t.off.tick !== c || i || t.off.a !== f) && (t.off.tick = c, t.off.seq = r(e, "off"), t.off.status = void 0, t.off.fn = void 0, t.off.b = 0) : t.off = { type: "off", tick: c, seq: r(e, "off"), ch: t.on.ch, a: f, b: 0 }, t.off.a = f, s(e);
38
+ }
39
+ function N(e, t) {
40
+ const o = new Set(t);
41
+ e.notes = e.notes.filter((n) => !o.has(n));
42
+ }
43
+ function s(e) {
44
+ let t = 0;
45
+ for (const o of e.notes) t = Math.max(t, o.off ? o.off.tick : o.on.tick);
46
+ for (const o of e.events) t = Math.max(t, o.tick);
47
+ e.end.tick = Math.max(e.end.tick, t);
48
+ }
49
+ function M(e) {
50
+ let t = 0;
51
+ for (const o of e.notes) t = Math.max(t, o.off ? o.off.tick : o.on.tick);
52
+ for (const o of e.events) t = Math.max(t, o.tick);
53
+ return t;
54
+ }
55
+ function S(e, t) {
56
+ e.end.tick = Math.max(M(e), Math.round(t), 1);
57
+ }
58
+ function w(e) {
59
+ let t;
60
+ const o = (n) => !t || n.tick < t.tick || n.tick === t.tick && n.seq < t.seq;
61
+ for (const n of e.events) n.type === "mode" && o(n) && (t = n);
62
+ for (const n of e.notes) o(n.on) && (t = n.on);
63
+ return !!t && t.type === "mode" && t.a !== 0;
64
+ }
65
+ function P(e) {
66
+ return e.events.filter((t) => t.type === "mode").sort((t, o) => t.tick - o.tick || t.seq - o.seq);
67
+ }
68
+ function T(e, t) {
69
+ const o = p(e), n = o.find((i) => i.type === "on"), f = o.filter((i) => i.type === "mode" && (!n || i.tick < n.tick || i.tick === n.tick && i.seq < n.seq));
70
+ f.length ? f[f.length - 1].a = t ? 1 : 0 : e.events.push({ type: "mode", tick: 0, seq: r(e, "mode"), ch: 0, a: t ? 1 : 0, b: 0 });
71
+ }
72
+ function k(e, t) {
73
+ return e.events.filter((o) => o.type === "program" && o.ch === t).sort((o, n) => o.tick - n.tick || o.seq - n.seq);
74
+ }
75
+ function I(e, t) {
76
+ const n = d(e, t)[0]?.on;
77
+ let f = t;
78
+ for (const i of k(e, t)) {
79
+ if (n && (i.tick > n.tick || i.tick === n.tick && i.seq > n.seq)) break;
80
+ f = i.a;
81
+ }
82
+ return f;
83
+ }
84
+ function L(e, t, o) {
85
+ const n = d(e, t)[0]?.on, f = k(e, t).filter((i) => !n || i.tick < n.tick || i.tick === n.tick && i.seq < n.seq);
86
+ f.length ? f[f.length - 1].a = o : e.events.push({ type: "program", tick: 0, seq: r(e, "program"), ch: t, a: o, b: 0 });
87
+ }
88
+ function V(e, t, o, n) {
89
+ o = Math.max(0, Math.round(o));
90
+ const f = e.events.find((c) => c.type === "program" && c.ch === t && c.tick === o);
91
+ if (f)
92
+ return f.a = n, f;
93
+ const i = { type: "program", tick: o, seq: r(e, "program"), ch: t, a: n, b: 0 };
94
+ return e.events.push(i), s(e), i;
95
+ }
96
+ function A(e, t) {
97
+ e.events = e.events.filter((o) => o !== t);
98
+ }
99
+ function H(e) {
100
+ const t = /* @__PURE__ */ new Set();
101
+ for (const o of e.notes) t.add(o.on.ch);
102
+ for (const o of e.events) (o.type === "program" || o.type === "raw") && t.add(o.ch);
103
+ return [...t].sort((o, n) => o - n);
104
+ }
105
+ function O(e, t) {
106
+ e.notes = e.notes.filter((o) => o.on.ch !== t), e.events = e.events.filter((o) => o.type === "mode" || o.ch !== t);
107
+ }
108
+ function R(e, t) {
109
+ const o = (n) => Math.round(n * t);
110
+ for (const n of e.notes) {
111
+ const f = o(n.on.tick);
112
+ n.on.tick = f, n.off && (n.off.tick = Math.max(f + 1, o(n.off.tick)));
113
+ }
114
+ for (const n of e.events) n.tick = o(n.tick);
115
+ e.end.tick = o(e.end.tick), e.leadIn = o(e.leadIn), e.ticksPerBeat = Math.max(1, o(e.ticksPerBeat)), s(e);
116
+ }
117
+ function B(e) {
118
+ const t = e.events.length;
119
+ return e.events = e.events.filter((o) => o.type === "program" || o.type === "mode" || o.prefix !== void 0 || o.type === "off" && o.status === !1), t - e.events.length;
120
+ }
121
+ function D(e) {
122
+ return e.events.filter((t) => t.type === "raw" && t.prefix === void 0 || t.type === "off" && t.status !== !1).length;
123
+ }
124
+ function F(e, t) {
125
+ return t ? e < 6 || h.includes(e) : e < 9;
126
+ }
127
+ export {
128
+ l as DEFAULT_VELOCITY,
129
+ b as addNote,
130
+ d as channelNotes,
131
+ F as channelSounds,
132
+ a as clampVelocity,
133
+ N as deleteNotes,
134
+ s as extendEnd,
135
+ D as ignoredCount,
136
+ M as lastEventTick,
137
+ P as modeChanges,
138
+ k as programChanges,
139
+ O as removeChannel,
140
+ A as removeEvent,
141
+ R as scaleTime,
142
+ y as scaleVelocities,
143
+ S as setEnd,
144
+ C as setNote,
145
+ V as setProgramAt,
146
+ T as setStartMode,
147
+ L as setStartProgram,
148
+ x as setVelocity,
149
+ w as startMode,
150
+ I as startProgram,
151
+ B as stripIgnored,
152
+ H as usedChannels,
153
+ E as velocityLevel
154
+ };
@@ -0,0 +1,89 @@
1
+ import { DEFAULT_VELOCITY as q, clampVelocity as E } from "./cmfEdit.js";
2
+ import { RHYTHM_CHANNELS as g, writeSong as w, streamEvents as T, noteEnd as I } from "./cmfSong.js";
3
+ function x(c, s = {}) {
4
+ const u = s.muted ?? /* @__PURE__ */ new Set(), i = Math.max(1, c.end.tick), r = [];
5
+ if (s.loop) {
6
+ const [t, n] = s.loop, o = Math.max(t, Math.min(s.from ?? t, n - 1));
7
+ o > t && r.push({ from: o, to: n, gap: 0, prelude: !0 }), r.push({ from: t, to: n, gap: 0, prelude: !0 });
8
+ } else {
9
+ const t = Math.max(0, Math.min(s.from ?? 0, i - 1));
10
+ if (t > 0) {
11
+ r.push({ from: t, to: i, gap: 0, prelude: !0 });
12
+ let n = i - t;
13
+ for (let o = 0; o < 12 && n < 146 * 300; o++)
14
+ r.push({ from: 0, to: i, gap: c.leadIn, prelude: !1 }), n += i + c.leadIn;
15
+ } else r.push({ from: 0, to: i, gap: 0, prelude: !1 });
16
+ }
17
+ const m = T(c), a = {
18
+ ...c,
19
+ notes: [],
20
+ events: [],
21
+ end: { tick: 0, bytes: [] },
22
+ leadIn: 0,
23
+ leadInBytes: void 0,
24
+ hadEvents: new Array(16).fill(!0)
25
+ };
26
+ let p = 0;
27
+ const l = (t) => a.events.push({ ...t, seq: p++, status: void 0, delta: void 0 });
28
+ let f = 0;
29
+ const d = [];
30
+ for (const t of r) {
31
+ f += t.gap, d.push(f);
32
+ const n = f - t.from;
33
+ if (t.prelude) {
34
+ let o;
35
+ const b = /* @__PURE__ */ new Map();
36
+ for (const e of m) {
37
+ if (e.tick > t.from || e.tick === t.from && e.type === "on") break;
38
+ e.type === "mode" && (o = e), e.type === "program" && b.set(e.ch, e.a);
39
+ }
40
+ l({ type: "mode", tick: f, ch: o?.ch ?? 0, a: o ? o.a : 0, b: 0 });
41
+ for (let e = 0; e < 16; e++) l({ type: "program", tick: f, ch: e, a: b.get(e) ?? e, b: 0 });
42
+ const k = /* @__PURE__ */ new Map();
43
+ for (const e of c.notes) {
44
+ if (u.has(e.on.ch) || e.on.tick >= t.from || I(c, e) <= t.from) continue;
45
+ const v = k.get(e.on.ch);
46
+ (!v || e.on.tick > v.on.tick) && k.set(e.on.ch, e);
47
+ }
48
+ for (const e of k.values())
49
+ l({ type: "on", tick: f, ch: e.on.ch, a: e.on.a, b: e.on.b }), e.off && e.off.tick < t.to && l({ type: "off", tick: e.off.tick + n, ch: e.on.ch, a: e.on.a, b: 0 });
50
+ }
51
+ for (const o of m)
52
+ o.tick < t.from || o.tick >= t.to || o.type !== "raw" && (o.type === "on" && u.has(o.ch) || l({ type: o.type, tick: o.tick + n, ch: o.ch, a: o.a, b: o.b, fn: o.fn, bytes: o.bytes }));
53
+ for (const o of c.notes)
54
+ o.on.tick < t.to && I(c, o) > t.to && !u.has(o.on.ch) && l({ type: "off", tick: t.to + n, ch: o.on.ch, a: o.on.a, b: 0 });
55
+ f += t.to - t.from;
56
+ }
57
+ a.events.sort((t, n) => t.tick - n.tick || t.seq - n.seq), a.end = { tick: f, bytes: [] }, a.clockTicks = c.clockTicks;
58
+ const h = f, y = r.length === 1 && !s.loop && r[0].from === 0;
59
+ y && (a.leadIn = c.leadIn);
60
+ const M = (t) => {
61
+ if (y && t >= h) {
62
+ const n = (t - h) % (h + c.leadIn);
63
+ return n < c.leadIn ? -1 : n - c.leadIn;
64
+ }
65
+ t = (t % h + h) % h;
66
+ for (let n = r.length - 1; n >= 0; n--) {
67
+ if (t >= d[n]) return Math.min(r[n].to, r[n].from + (t - d[n]));
68
+ if (t >= d[n] - r[n].gap) return -1;
69
+ }
70
+ return r[0].from;
71
+ };
72
+ return { bytes: w(a), length: h, songTick: M };
73
+ }
74
+ function H(c, s, u, i, r = 70, m = q) {
75
+ const a = g.includes(s), p = {
76
+ ...c,
77
+ notes: [],
78
+ events: [],
79
+ end: { tick: r + 600, bytes: [] },
80
+ leadIn: 0,
81
+ leadInBytes: void 0,
82
+ hadEvents: new Array(16).fill(!0)
83
+ };
84
+ return p.events.push({ type: "mode", tick: 0, seq: 0, ch: 0, a: a ? 1 : 0, b: 0 }), p.events.push({ type: "program", tick: 0, seq: 1, ch: s, a: u, b: 0 }), p.events.push({ type: "on", tick: 0, seq: 2, ch: s, a: i, b: E(m) }), p.events.push({ type: "off", tick: r, seq: 3, ch: s, a: i, b: 0 }), w(p);
85
+ }
86
+ export {
87
+ H as auditionSong,
88
+ x as buildPreview
89
+ };
@@ -0,0 +1,190 @@
1
+ import { isCmf as S } from "./cmf.js";
2
+ const O = [11, 12, 13, 14, 15], P = { 11: "Bass drum", 12: "Snare drum", 13: "Tom-tom", 14: "Cymbal", 15: "Hi-hat" };
3
+ function T(t) {
4
+ const s = [t & 127];
5
+ for (t = Math.floor(t / 128); t > 0; t = Math.floor(t / 128)) s.unshift(t & 127 | 128);
6
+ return s;
7
+ }
8
+ function v(t, s) {
9
+ let f = t[s] ?? 0;
10
+ if (f & 128) {
11
+ f &= 127;
12
+ let l;
13
+ do
14
+ s++, l = t[s] ?? 0, f = f * 128 + (l & 127);
15
+ while (l & 128 && s < t.length);
16
+ }
17
+ return { value: f, next: s + 1 };
18
+ }
19
+ const N = (t) => t ? v(Uint8Array.from(t), 0).value : void 0;
20
+ function R(t, s) {
21
+ return s.off ? s.off.tick : t.end.tick;
22
+ }
23
+ function _(t) {
24
+ if (!S(t)) throw new Error("Not a CMF file");
25
+ const s = new DataView(t.buffer, t.byteOffset, t.byteLength), f = (a) => a + 1 < t.length ? s.getUint16(a, !0) : 0, l = f(6), r = f(8), w = f(36), o = Math.max(40, Math.min(l, t.length)), E = new Uint8Array(o);
26
+ E.set(t.subarray(0, o));
27
+ const u = [];
28
+ for (let a = 0; a < Math.min(w, 128); a++) {
29
+ const p = new Uint8Array(16);
30
+ p.set(t.subarray(l + a * 16, l + a * 16 + 16)), u.push(p);
31
+ }
32
+ const g = l + w * 16, h = r > g ? t.slice(g, r) : new Uint8Array(0), i = t.subarray(r), c = {
33
+ header: E,
34
+ ticksPerBeat: f(10),
35
+ clockTicks: f(12),
36
+ channelUsed: t.slice(20, 36),
37
+ hadEvents: new Array(16).fill(!1),
38
+ instruments: u,
39
+ gap: h,
40
+ leadIn: 0,
41
+ notes: [],
42
+ events: [],
43
+ end: { tick: 0, bytes: [] },
44
+ counter: 0
45
+ };
46
+ if (i.length === 0) return c;
47
+ const y = [];
48
+ let n = 0;
49
+ const b = v(i, 0);
50
+ c.leadIn = b.value, c.leadInBytes = [...i.subarray(0, b.next)], n = b.next;
51
+ let d = 0, e = 8, x = 0, M, m = 0;
52
+ for (; ; ) {
53
+ const a = n;
54
+ if (n >= i.length) {
55
+ c.end = { tick: d, delta: M, bytes: [] };
56
+ break;
57
+ }
58
+ let p = !1, A;
59
+ const B = i[n];
60
+ ((B & 240) === 144 || (B & 240) === 128) && (x = B & 15, e = B >> 4, n++, p = !0, A = B);
61
+ const U = i[n];
62
+ let k;
63
+ if (n >= i.length) k = void 0;
64
+ else if (U < 128)
65
+ if (n + 1 >= i.length) k = void 0;
66
+ else {
67
+ const $ = i[n + 1];
68
+ k = e === 8 || $ === 0 ? { type: "off", tick: d, seq: m, ch: x, a: U, b: $, fn: e, status: p } : { type: "on", tick: d, seq: m, ch: x, a: U, b: $, status: p }, n += 2;
69
+ }
70
+ else {
71
+ const $ = U & 240, L = (I) => {
72
+ const D = [...i.subarray(n, n + I)];
73
+ return n += I, D;
74
+ };
75
+ if ($ === 176 && i[n + 1] === 103 && n + 2 < i.length)
76
+ k = { type: "mode", tick: d, seq: m, ch: U & 15, a: i[n + 2], b: 0 }, n += 3;
77
+ else if ($ === 192 && n + 1 < i.length)
78
+ k = { type: "program", tick: d, seq: m, ch: U & 15, a: i[n + 1], b: 0 }, n += 2;
79
+ else if (($ === 160 || $ === 176 || $ === 224) && n + 2 < i.length) k = { type: "raw", tick: d, seq: m, ch: U & 15, a: 0, b: 0, bytes: L(3) };
80
+ else if ($ === 208 && n + 1 < i.length) k = { type: "raw", tick: d, seq: m, ch: U & 15, a: 0, b: 0, bytes: L(2) };
81
+ else if ((U === 240 || U === 247) && n + 1 < i.length) {
82
+ const I = v(i, n + 1), D = I.next + I.value + 1;
83
+ D <= i.length && (k = { type: "raw", tick: d, seq: m, ch: 0, a: 0, b: 0, bytes: L(D - n) });
84
+ }
85
+ k && p && (k.prefix = A);
86
+ }
87
+ if (!k) {
88
+ c.end = { tick: d, delta: M, bytes: [...i.subarray(a)] };
89
+ break;
90
+ }
91
+ if (k.delta = M, y.push(k), m++, n >= i.length) {
92
+ c.end = { tick: d, bytes: [] };
93
+ break;
94
+ }
95
+ const H = v(i, n);
96
+ M = [...i.subarray(n, H.next)], d += H.value, n = H.next;
97
+ }
98
+ const q = /* @__PURE__ */ new Map();
99
+ for (const a of y)
100
+ if (c.hadEvents[a.ch] = !0, a.type === "on") {
101
+ const p = { on: a };
102
+ c.notes.push(p);
103
+ const A = a.ch * 128 + a.a, B = q.get(A);
104
+ B ? B.push(p) : q.set(A, [p]);
105
+ } else if (a.type === "off") {
106
+ const p = q.get(a.ch * 128 + a.a)?.shift();
107
+ p ? p.off = a : c.events.push(a);
108
+ } else c.events.push(a);
109
+ return c.counter = m, c;
110
+ }
111
+ function V(t) {
112
+ const s = [...t.events];
113
+ for (const f of t.notes)
114
+ s.push(f.on), f.off && (f.off.tick < f.on.tick && (f.off.tick = f.on.tick), f.off.tick === f.on.tick && f.off.seq < f.on.seq && (f.off.seq = f.on.seq + 0.5), s.push(f.off));
115
+ return s.sort((f, l) => f.tick - l.tick || f.seq - l.seq);
116
+ }
117
+ function F(t) {
118
+ const s = V(t);
119
+ s.length && s[0].tick > 0 && s.unshift({ type: "off", tick: 0, seq: -1 / 0, ch: 0, a: 0, b: 0, fn: 8 });
120
+ const f = s.length ? s[s.length - 1].tick : 0;
121
+ let l = Math.max(t.end.tick, f), r = t.leadIn;
122
+ l + r === 0 && (s.length || t.end.bytes.length) && (s.length ? l = 1 : r = 1);
123
+ const w = (e, x) => x && N(x) === e ? x : T(e), o = [], E = s.length || t.end.bytes.length || t.leadInBytes;
124
+ E && o.push(...w(r, t.leadInBytes));
125
+ let u = -1, g = -1, h = 0;
126
+ if (s.forEach((e, x) => {
127
+ if (x > 0 && o.push(...w(e.tick - h, e.delta)), h = e.tick, e.type === "on" || e.type === "off") {
128
+ let M, m = e.b;
129
+ e.type === "on" ? M = 9 : (M = e.fn ?? (u === 8 && g === e.ch ? 8 : 9), M === 9 && (m = 0));
130
+ const q = u >= 0, a = !q || u !== M || g !== e.ch;
131
+ (e.status === !0 || a && (q || e.status !== !1)) && o.push(M << 4 | e.ch), u = M, g = e.ch, o.push(e.a & 127, m & 255);
132
+ return;
133
+ }
134
+ e.prefix !== void 0 && (o.push(e.prefix), u = e.prefix >> 4, g = e.prefix & 15), e.type === "program" ? o.push(192 | e.ch, e.a & 255) : e.type === "mode" ? o.push(176 | e.ch, 103, e.a & 255) : o.push(...e.bytes ?? []);
135
+ }), E) {
136
+ const e = l - f;
137
+ s.length ? t.end.bytes.length === 0 && e === (N(t.end.delta) ?? 0) ? t.end.delta && o.push(...t.end.delta) : (o.push(...w(e, t.end.delta)), o.push(...t.end.bytes.length ? t.end.bytes : [255, 47, 0])) : o.push(...t.end.bytes.length ? t.end.bytes : [255, 47, 0]);
138
+ }
139
+ const i = t.header.length, c = t.instruments.slice(0, 128), y = i + c.length * 16 + t.gap.length, n = new Uint8Array(y + o.length);
140
+ n.set(t.header);
141
+ const b = new DataView(n.buffer);
142
+ b.setUint16(6, i, !0), b.setUint16(8, y, !0), b.setUint16(10, Math.max(1, Math.min(65535, Math.round(t.ticksPerBeat))), !0), b.setUint16(12, t.clockTicks, !0);
143
+ const d = new Array(16).fill(!1);
144
+ for (const e of s) d[e.ch] = !0;
145
+ for (let e = 0; e < 16; e++) n[20 + e] = d[e] === t.hadEvents[e] ? t.channelUsed[e] : d[e] ? 1 : 0;
146
+ return b.setUint16(36, c.length, !0), c.forEach((e, x) => n.set(e.subarray(0, 16), i + x * 16)), n.set(t.gap, i + c.length * 16), n.set(o, y), n;
147
+ }
148
+ function Y(t, s, f = !1) {
149
+ const l = [];
150
+ let r = 0, w = 0, o = 0, E = 8;
151
+ const u = (h) => t[h] ?? 255, g = () => {
152
+ const h = v(t, r);
153
+ return r = h.next, h.value;
154
+ };
155
+ if (!t.length) return l;
156
+ for (let h = 0; h < s; h++) {
157
+ r === 0 && (w = 1, g()), w--;
158
+ let i = 0;
159
+ for (; w <= 0; ) {
160
+ if (i++ > 1e5)
161
+ return l.push(`${h}:hang`), l;
162
+ const c = u(r);
163
+ ((c & 240) === 144 || (c & 240) === 128) && (o = c & 15, E = c >> 4, r++);
164
+ const y = u(r);
165
+ if (y < 128) {
166
+ const n = u(r + 1), b = E === 8 || n === 0;
167
+ l.push(`${h}:${b ? "off" : "on"} ${o} ${y}${f && !b ? `@${n}` : ""}`), r += 2;
168
+ } else {
169
+ const n = y & 240;
170
+ n === 160 || n === 224 ? r += 3 : n === 176 ? (u(r + 1) === 103 && l.push(`${h}:mode ${u(r + 2)}`), r += 3) : n === 192 ? (l.push(`${h}:program ${y & 15} ${u(r + 1)}`), r += 2) : n === 208 ? r += 2 : y === 240 || y === 247 ? (r++, r += g(), r++) : (l.push(`${h}:restart`), r = 0);
171
+ }
172
+ w = g();
173
+ }
174
+ }
175
+ return l;
176
+ }
177
+ function j(t) {
178
+ const s = new DataView(t.buffer, t.byteOffset, t.byteLength);
179
+ return t.subarray(s.getUint16(8, !0));
180
+ }
181
+ export {
182
+ P as DRUM_NAMES,
183
+ O as RHYTHM_CHANNELS,
184
+ j as musicOf,
185
+ R as noteEnd,
186
+ _ as readSong,
187
+ V as streamEvents,
188
+ Y as traceMusic,
189
+ F as writeSong
190
+ };