1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
import {
lastFrameTime as _lastFrameTime,
parseTimecode as _parseTimecode,
roundToFrame as _roundToFrame,
snappedSeekTime as _snappedSeekTime,
TICKS_PER_SECOND as _TICKS_PER_SECOND,
mediaTimeFromSeconds as _mediaTimeFromSeconds,
mediaTimeToSeconds as _mediaTimeToSeconds,
type FrameRate,
type TimeCodeFormat,
} from "opencut-wasm";
/**
* Integer-tick time. Mirrors `MediaTime(i64)` in `rust/crates/time/src/media_time.rs`.
*
* `opencut-wasm` exposes `MediaTime` as a bare `number` alias because tsify
* collapses tuple structs. The brand here is the TS-side discipline that
* recovers the invariant: a `MediaTime` is an integer count of ticks, and the
* only legal way to construct one from a fractional `number` is `roundMediaTime`
* (or `mediaTimeFromSeconds`, which rounds inside the wasm boundary).
*
* Reading is free — `MediaTime` is assignable to `number`. Writing is gated —
* a bare `number` is not assignable to `MediaTime`.
*/
export type MediaTime = number & { readonly __mediaTime: unique symbol };
export const TICKS_PER_SECOND = _TICKS_PER_SECOND();
export const ZERO_MEDIA_TIME = 0 as MediaTime;
/**
* Construct a `MediaTime` from a known-integer tick count. Asserts in dev that
* the value is actually an integer; cast in release. Use `roundMediaTime` when
* the input may be fractional.
*/
export function mediaTime({ ticks }: { ticks: number }): MediaTime {
if (process.env.NODE_ENV !== "production" && !Number.isInteger(ticks)) {
throw new Error(
`mediaTime() requires an integer tick count, got ${ticks}. Use roundMediaTime() for fractional values.`,
);
}
return ticks as MediaTime;
}
/**
* Project a fractional value onto the integer-tick lattice.
*
* Rounds half away from zero (`-1.5 → -2`, `1.5 → 2`) and normalises `-0` to
* `0`. The away-from-zero rule matches Rust's `.round()` and avoids the
* `Math.round(-0.5) === -0` quirk that propagates `-0` into stored data.
*/
export function roundMediaTime({ time }: { time: number }): MediaTime {
const roundedMagnitude = Math.round(Math.abs(time));
if (roundedMagnitude === 0) {
return 0 as MediaTime;
}
return (time < 0 ? -roundedMagnitude : roundedMagnitude) as MediaTime;
}
export function mediaTimeFromSeconds({
seconds,
}: {
seconds: number;
}): MediaTime {
const result = _mediaTimeFromSeconds({ seconds });
if (result === undefined) {
throw new Error(
`mediaTimeFromSeconds: rust returned undefined for seconds=${seconds}`,
);
}
return result as MediaTime;
}
export function mediaTimeToSeconds({ time }: { time: MediaTime }): number {
return _mediaTimeToSeconds({ time });
}
/**
* Sum `MediaTime` values. Inputs are integer ticks, so the sum is integer too;
* the cast is a no-op at runtime, only re-establishes the brand for the type
* system.
*/
export function addMediaTime({
a,
b,
}: {
a: MediaTime;
b: MediaTime;
}): MediaTime {
return (a + b) as MediaTime;
}
export function subMediaTime({
a,
b,
}: {
a: MediaTime;
b: MediaTime;
}): MediaTime {
return (a - b) as MediaTime;
}
export function maxMediaTime({
a,
b,
}: {
a: MediaTime;
b: MediaTime;
}): MediaTime {
return (a > b ? a : b) as MediaTime;
}
export function minMediaTime({
a,
b,
}: {
a: MediaTime;
b: MediaTime;
}): MediaTime {
return (a < b ? a : b) as MediaTime;
}
export function clampMediaTime({
time,
min,
max,
}: {
time: MediaTime;
min: MediaTime;
max: MediaTime;
}): MediaTime {
if (time < min) return min;
if (time > max) return max;
return time;
}
export function roundFrameTime({
time,
fps,
}: {
time: MediaTime;
fps: FrameRate;
}): MediaTime {
return (_roundToFrame({ time, rate: fps }) ?? time) as MediaTime;
}
export function roundFrameTicks({
ticks,
fps,
}: {
ticks: number;
fps: FrameRate;
}): number {
return _roundToFrame({ time: ticks, rate: fps }) ?? ticks;
}
export function snapSeekMediaTime({
time,
duration,
fps,
}: {
time: MediaTime;
duration: MediaTime;
fps: FrameRate;
}): MediaTime {
return (_snappedSeekTime({ time, duration, rate: fps }) ?? time) as MediaTime;
}
export function lastFrameMediaTime({
duration,
fps,
}: {
duration: MediaTime;
fps: FrameRate;
}): MediaTime {
return (_lastFrameTime({ duration, rate: fps }) ?? duration) as MediaTime;
}
export function parseMediaTimecode({
timeCode,
format,
fps,
}: {
timeCode: string;
format: TimeCodeFormat;
fps: FrameRate;
}): MediaTime | null {
const parsedTime = _parseTimecode({ timeCode, format, rate: fps });
return parsedTime == null ? null : (parsedTime as MediaTime);
}
|