tui-cap 0.1.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.
@@ -0,0 +1,115 @@
1
+ // Single source of truth for the GUI's playback / timeline / export timing math.
2
+ //
3
+ // This mirrors the canonical, unit-tested server-side math in `src/animation.ts`
4
+ // (`applyTimeline` / `cfrCounts`). The browser can't import the TypeScript module
5
+ // directly, so the logic is duplicated here once — and `test/playback-timing.test.ts`
6
+ // asserts byte-for-byte parity between the two across a grid of inputs so they can
7
+ // never silently drift. Keep any change here in lock-step with `src/animation.ts`.
8
+
9
+ /** Playback-only visibility floor (ms): the play loop never holds a frame for
10
+ * less than this so a sub-perception burst still registers on screen. NOT part
11
+ * of the readout/timeline/export totals — those use the raw effective hold. */
12
+ export const PLAYBACK_MIN_MS = 40;
13
+
14
+ /** Fallback hold (ms) when a frame carries no captured timing. */
15
+ export const UNIFORM_FRAME_MS = 1000;
16
+
17
+ /** Default cap on any single frame's hold (ms); mirrors animation.DEFAULT_IDLE_CAP_MS. */
18
+ export const DEFAULT_IDLE_CAP_MS = 1500;
19
+
20
+ /**
21
+ * Effective on-screen hold (ms) for one raw duration under the given settings.
22
+ * An override wins outright (no cap, no speed); smooth-typing then retimes a
23
+ * flagged typing frame to `typedChars / typingCps` (no cap, no speed);
24
+ * otherwise the raw hold is capped to `idleCapMs` then divided by `speed`.
25
+ * Equivalent to `animation.applyTimeline([rawMs], { idleCapMs, speed, minMs: 0,
26
+ * smoothTyping, typingCps, isTyping:[isTyping], typedChars:[typedChars] })[0]`.
27
+ */
28
+ export function effectiveDuration(rawMs, settings = {}) {
29
+ const {
30
+ idleCapMs = DEFAULT_IDLE_CAP_MS,
31
+ speed = 1,
32
+ override,
33
+ smoothTyping = false,
34
+ typingCps = 30,
35
+ isTyping = false,
36
+ typedChars = 0,
37
+ } = settings;
38
+ if (typeof override === 'number' && isFinite(override)) return Math.max(0, override);
39
+ if (smoothTyping && isTyping && typedChars > 0) {
40
+ const cps = typingCps > 0 ? typingCps : 30;
41
+ return (typedChars / cps) * 1000;
42
+ }
43
+ let v = Math.max(0, rawMs);
44
+ if (idleCapMs > 0) v = Math.min(v, idleCapMs);
45
+ const sp = speed > 0 ? speed : 1;
46
+ return v / sp;
47
+ }
48
+
49
+ /** Raw on-screen hold for a frame record, before caps/speed/overrides. */
50
+ export function rawDurationMs(frame) {
51
+ const d = frame && frame.durationMs;
52
+ return typeof d === 'number' && isFinite(d) ? d : UNIFORM_FRAME_MS;
53
+ }
54
+
55
+ /**
56
+ * Effective per-frame durations (ms) for a list of frame records.
57
+ * `settings`: `{ idleCapMs, speed, overrides: { [index]: ms }, smoothTyping,
58
+ * typingCps }`. A per-frame override (keyed by stringified index) bypasses cap +
59
+ * speed; smooth-typing retimes frames whose record carries `isTyping`/
60
+ * `typedChars`.
61
+ */
62
+ export function effectiveDurations(frames, settings = {}) {
63
+ const {
64
+ idleCapMs = DEFAULT_IDLE_CAP_MS,
65
+ speed = 1,
66
+ overrides = {},
67
+ smoothTyping = false,
68
+ typingCps = 30,
69
+ } = settings;
70
+ return frames.map((f, i) => {
71
+ const ov = overrides[String(i)];
72
+ return effectiveDuration(rawDurationMs(f), {
73
+ idleCapMs,
74
+ speed,
75
+ override: ov,
76
+ smoothTyping,
77
+ typingCps,
78
+ isTyping: !!(f && f.isTyping),
79
+ typedChars: f && typeof f.typedChars === 'number' ? f.typedChars : 0,
80
+ });
81
+ });
82
+ }
83
+
84
+ /** Playback hold (ms): the effective duration floored to PLAYBACK_MIN_MS. */
85
+ export function playbackHold(effMs) {
86
+ return Math.max(PLAYBACK_MIN_MS, Math.max(0, effMs));
87
+ }
88
+
89
+ /** CFR repeat count for a frame held `ms` at `fps` (at least one). Mirrors
90
+ * animation.cfrCounts' per-frame value. */
91
+ export function cfrCount(ms, fps) {
92
+ const f = fps > 0 ? fps : 1;
93
+ return Math.max(1, Math.round((Math.max(0, ms) / 1000) * f));
94
+ }
95
+
96
+ /** Readout / timeline total (ms): plain sum of effective holds over `indices`. */
97
+ export function readoutTotalMs(eff, indices) {
98
+ return indices.reduce((a, i) => a + Math.max(0, eff[i]), 0);
99
+ }
100
+
101
+ /** Playback total (ms): sum of floored holds — what the play loop targets. */
102
+ export function playbackTotalMs(eff, indices) {
103
+ return indices.reduce((a, i) => a + playbackHold(eff[i]), 0);
104
+ }
105
+
106
+ /** CFR output-frame total over `indices` at `fps`. */
107
+ export function cfrTotalFrames(eff, indices, fps) {
108
+ return indices.reduce((a, i) => a + cfrCount(eff[i], fps), 0);
109
+ }
110
+
111
+ /** CFR export wall-clock (ms): output frames / fps. */
112
+ export function cfrTotalMs(eff, indices, fps) {
113
+ const f = fps > 0 ? fps : 1;
114
+ return (cfrTotalFrames(eff, indices, f) / f) * 1000;
115
+ }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Vanilagy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.