@nodetool-ai/timeline 0.8.2 → 0.8.4-a13.1
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.
- package/README.md +1 -0
- package/dist/.nodetool-build-stamp +1 -1
- package/dist/animation/compile.d.ts.map +1 -1
- package/dist/animation/compile.js +5 -1
- package/dist/animation/compile.js.map +1 -1
- package/dist/audioFade.d.ts +4 -2
- package/dist/audioFade.d.ts.map +1 -1
- package/dist/audioFade.js +4 -2
- package/dist/audioFade.js.map +1 -1
- package/dist/clipAudio.d.ts +32 -0
- package/dist/clipAudio.d.ts.map +1 -0
- package/dist/clipAudio.js +70 -0
- package/dist/clipAudio.js.map +1 -0
- package/dist/clipSpeed.d.ts +17 -0
- package/dist/clipSpeed.d.ts.map +1 -0
- package/dist/clipSpeed.js +58 -0
- package/dist/clipSpeed.js.map +1 -0
- package/dist/defaults.d.ts +6 -1
- package/dist/defaults.d.ts.map +1 -1
- package/dist/defaults.js +38 -0
- package/dist/defaults.js.map +1 -1
- package/dist/dropResolve.d.ts +14 -6
- package/dist/dropResolve.d.ts.map +1 -1
- package/dist/dropResolve.js +113 -37
- package/dist/dropResolve.js.map +1 -1
- package/dist/finish-storyboard.d.ts.map +1 -1
- package/dist/finish-storyboard.js +63 -43
- package/dist/finish-storyboard.js.map +1 -1
- package/dist/fonts/google-fonts-node.d.ts.map +1 -1
- package/dist/fonts/google-fonts-node.js +2 -1
- package/dist/fonts/google-fonts-node.js.map +1 -1
- package/dist/fonts/google-fonts.d.ts +6 -0
- package/dist/fonts/google-fonts.d.ts.map +1 -1
- package/dist/fonts/google-fonts.js +7 -1
- package/dist/fonts/google-fonts.js.map +1 -1
- package/dist/fonts/register-node.d.ts.map +1 -1
- package/dist/fonts/register-node.js +2 -1
- package/dist/fonts/register-node.js.map +1 -1
- package/dist/group.d.ts +16 -4
- package/dist/group.d.ts.map +1 -1
- package/dist/group.js +53 -6
- package/dist/group.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/interchange.d.ts +8 -0
- package/dist/interchange.d.ts.map +1 -1
- package/dist/interchange.js +225 -27
- package/dist/interchange.js.map +1 -1
- package/dist/interchangeImport.d.ts +88 -0
- package/dist/interchangeImport.d.ts.map +1 -0
- package/dist/interchangeImport.js +498 -0
- package/dist/interchangeImport.js.map +1 -0
- package/dist/keyframes.d.ts +12 -1
- package/dist/keyframes.d.ts.map +1 -1
- package/dist/keyframes.js +78 -21
- package/dist/keyframes.js.map +1 -1
- package/dist/linked.js +1 -1
- package/dist/mediaTrack.d.ts +11 -0
- package/dist/mediaTrack.d.ts.map +1 -1
- package/dist/mediaTrack.js +25 -0
- package/dist/mediaTrack.js.map +1 -1
- package/dist/midi/edit.d.ts +5 -0
- package/dist/midi/edit.d.ts.map +1 -1
- package/dist/midi/edit.js +2 -1
- package/dist/midi/edit.js.map +1 -1
- package/dist/midi/grid.d.ts.map +1 -1
- package/dist/midi/grid.js +8 -4
- package/dist/midi/grid.js.map +1 -1
- package/dist/midi/tempo.d.ts.map +1 -1
- package/dist/midi/tempo.js +8 -0
- package/dist/midi/tempo.js.map +1 -1
- package/dist/ops/apply.d.ts +9 -0
- package/dist/ops/apply.d.ts.map +1 -1
- package/dist/ops/apply.js +1822 -1426
- package/dist/ops/apply.js.map +1 -1
- package/dist/ops/op.d.ts +3 -1
- package/dist/ops/op.d.ts.map +1 -1
- package/dist/ops/op.js.map +1 -1
- package/dist/ops/parse.d.ts.map +1 -1
- package/dist/ops/parse.js +318 -357
- package/dist/ops/parse.js.map +1 -1
- package/dist/render/cpuLegacyEffects.d.ts.map +1 -1
- package/dist/render/cpuLegacyEffects.js +58 -18
- package/dist/render/cpuLegacyEffects.js.map +1 -1
- package/dist/render/draw.d.ts +43 -1
- package/dist/render/draw.d.ts.map +1 -1
- package/dist/render/draw.js +123 -3
- package/dist/render/draw.js.map +1 -1
- package/dist/render/frameCompositor.d.ts +31 -3
- package/dist/render/frameCompositor.d.ts.map +1 -1
- package/dist/render/frameCompositor.js +58 -10
- package/dist/render/frameCompositor.js.map +1 -1
- package/dist/render/headlessRender.d.ts +85 -0
- package/dist/render/headlessRender.d.ts.map +1 -0
- package/dist/render/headlessRender.js +26 -0
- package/dist/render/headlessRender.js.map +1 -0
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.d.ts.map +1 -1
- package/dist/render/index.js +1 -0
- package/dist/render/index.js.map +1 -1
- package/dist/render/sceneModel.d.ts +20 -8
- package/dist/render/sceneModel.d.ts.map +1 -1
- package/dist/render/sceneModel.js +265 -89
- package/dist/render/sceneModel.js.map +1 -1
- package/dist/render/temporal.d.ts +9 -1
- package/dist/render/temporal.d.ts.map +1 -1
- package/dist/render/temporal.js +18 -1
- package/dist/render/temporal.js.map +1 -1
- package/dist/render/textLayout.d.ts +6 -0
- package/dist/render/textLayout.d.ts.map +1 -1
- package/dist/render/textLayout.js +13 -1
- package/dist/render/textLayout.js.map +1 -1
- package/dist/rippleEdit.d.ts +20 -3
- package/dist/rippleEdit.d.ts.map +1 -1
- package/dist/rippleEdit.js +89 -17
- package/dist/rippleEdit.js.map +1 -1
- package/dist/scene.d.ts +1 -0
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +1 -0
- package/dist/scene.js.map +1 -1
- package/dist/shotLink.d.ts +47 -0
- package/dist/shotLink.d.ts.map +1 -0
- package/dist/shotLink.js +92 -0
- package/dist/shotLink.js.map +1 -0
- package/dist/splitClip.d.ts.map +1 -1
- package/dist/splitClip.js +16 -2
- package/dist/splitClip.js.map +1 -1
- package/dist/storyboard.js +1 -1
- package/dist/takes.d.ts.map +1 -1
- package/dist/takes.js +15 -2
- package/dist/takes.js.map +1 -1
- package/dist/testing/host-ops.d.ts +16 -0
- package/dist/testing/host-ops.d.ts.map +1 -0
- package/dist/testing/host-ops.js +378 -0
- package/dist/testing/host-ops.js.map +1 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +3 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/ops.d.ts +30 -0
- package/dist/testing/ops.d.ts.map +1 -0
- package/dist/testing/ops.js +814 -0
- package/dist/testing/ops.js.map +1 -0
- package/dist/transitionAtCut.d.ts +29 -5
- package/dist/transitionAtCut.d.ts.map +1 -1
- package/dist/transitionAtCut.js +69 -33
- package/dist/transitionAtCut.js.map +1 -1
- package/dist/trimClip.d.ts +5 -8
- package/dist/trimClip.d.ts.map +1 -1
- package/dist/trimClip.js +38 -2
- package/dist/trimClip.js.map +1 -1
- package/dist/types.d.ts +62 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +29 -0
- package/dist/types.js.map +1 -1
- package/package.json +11 -5
package/dist/ops/apply.js
CHANGED
|
@@ -22,21 +22,24 @@ import { isCropUsable } from "../crop.js";
|
|
|
22
22
|
import { DEFAULT_MEDIA_CLIP_DURATION_MS, DEFAULT_MODEL3D_CLIP_DURATION_MS, DEFAULT_MODEL3D_CLIP_NAME, DEFAULT_TEXT_CLIP_COLOR, DEFAULT_TEXT_CLIP_DURATION_MS, clipFitsTrack, makeClip, makeClipVersion, makeTrack, mediaTypeForContentType, trackTypeForMediaType } from "../defaults.js";
|
|
23
23
|
import { assertAuthorableFontFamily } from "../fonts/catalog.js";
|
|
24
24
|
import { selectGeneratedMatteVersion } from "../generatedMatte.js";
|
|
25
|
-
import { groupDescendantIds, isGroupClip,
|
|
26
|
-
import { isMediaTrackStale,
|
|
25
|
+
import { groupDescendantIds, isGroupClip, moveUnitIds, trimGroup, ungroup } from "../group.js";
|
|
26
|
+
import { isMediaTrackStale, splitClipMediaTracks, resliceTracksForTrimmedClip } from "../mediaTrack.js";
|
|
27
27
|
import { quantizeNotes, scaleVelocity, transposeNotes } from "../midi/edit.js";
|
|
28
28
|
import { DEFAULT_MIDI_INSTRUMENT } from "../midi/instrument.js";
|
|
29
29
|
import { createMidiNote, sortNotes, validateNotes } from "../midi/notes.js";
|
|
30
30
|
import { findInstrumentPreset, MIDI_INSTRUMENT_PRESETS } from "../midi/presets.js";
|
|
31
31
|
import { DEFAULT_TEMPO, rescaleClipsForTempo, resolveTempo } from "../midi/tempo.js";
|
|
32
|
+
import { msToTicks } from "../midi/ticks.js";
|
|
32
33
|
import { computeModel3DBakeHash } from "../model3dBake.js";
|
|
33
34
|
import { addReframeKeyframe, clearReframe, mediaTrackCanDriveReframe } from "../reframe.js";
|
|
34
35
|
import { adaptSequenceFormat } from "../retarget.js";
|
|
36
|
+
import { sourceRate } from "../sourceRate.js";
|
|
35
37
|
import { splitClip } from "../splitClip.js";
|
|
36
38
|
import { activeTakeIdOf, deleteTake as deleteTakeOnClip, renameTake as renameTakeOnClip, selectTake } from "../takes.js";
|
|
37
39
|
import { moveTrackOrder } from "../trackOrder.js";
|
|
38
|
-
import { applyTransitionAtCutCandidate, planTransitionAtCut } from "../transitionAtCut.js";
|
|
39
|
-
import { trimClip } from "../trimClip.js";
|
|
40
|
+
import { applyTransitionAtCutCandidate, transitionGrowthMs, transitionGrowthUnit, transitionLengthAtCut, planTransitionAtCut } from "../transitionAtCut.js";
|
|
41
|
+
import { retimeCaption, trimClip } from "../trimClip.js";
|
|
42
|
+
import { retimeClipSpeed } from "../clipSpeed.js";
|
|
40
43
|
import { serializeClip, serializeMediaTrack, serializeTrack } from "./serialize.js";
|
|
41
44
|
/** Units a failed lookup names before it stops and points at get_state. */
|
|
42
45
|
const MAX_LISTED_UNITS = 12;
|
|
@@ -84,6 +87,33 @@ function resolveShortId(units, target, kind) {
|
|
|
84
87
|
}
|
|
85
88
|
return matches[0];
|
|
86
89
|
}
|
|
90
|
+
/** A unit by exact id, or by an exact unique 12-character prefix of one. */
|
|
91
|
+
function findByIdOrShortId(units, target, kind) {
|
|
92
|
+
return (units.find((unit) => unit.id === target) ??
|
|
93
|
+
resolveShortId(units, target, kind));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The beat one `target` names: an exact id, an exact unique 12-character id
|
|
97
|
+
* prefix, or a 1-based position written as digits only. Anything else is
|
|
98
|
+
* refused, so a hex id that happens to start with a digit is never read as a
|
|
99
|
+
* position. Exported because the browser bridge resolves the same names.
|
|
100
|
+
*/
|
|
101
|
+
export function resolveBeatTarget(beats, target) {
|
|
102
|
+
const byId = findByIdOrShortId(beats, target, "beat");
|
|
103
|
+
if (byId) {
|
|
104
|
+
return byId;
|
|
105
|
+
}
|
|
106
|
+
const byPosition = /^\d+$/.test(target)
|
|
107
|
+
? beats[Number.parseInt(target, 10) - 1]
|
|
108
|
+
: undefined;
|
|
109
|
+
if (byPosition) {
|
|
110
|
+
return byPosition;
|
|
111
|
+
}
|
|
112
|
+
throw new Error(`No beat matches "${target}". Use a beat id or its 1-based position. ` +
|
|
113
|
+
(beats.length
|
|
114
|
+
? `This plan has ${beats.length} beats.`
|
|
115
|
+
: "This sequence has no beat plan yet; run ui_timeline_plan_beats first."));
|
|
116
|
+
}
|
|
87
117
|
/**
|
|
88
118
|
* Name the units a caller could have meant, capped: a 200-clip sequence
|
|
89
119
|
* listing all of them is one an agent stops reading.
|
|
@@ -125,6 +155,7 @@ const CLIP_PARAM_KEYS = [
|
|
|
125
155
|
"fadeOutMs",
|
|
126
156
|
"fadeInShape",
|
|
127
157
|
"fadeOutShape",
|
|
158
|
+
"audioEffects",
|
|
128
159
|
"blendMode",
|
|
129
160
|
"borderRadius",
|
|
130
161
|
"crop",
|
|
@@ -205,8 +236,29 @@ class OpScope {
|
|
|
205
236
|
this.tracks.push(track);
|
|
206
237
|
return track;
|
|
207
238
|
}
|
|
239
|
+
/** The first unlocked track of `type`, or a new one: a lock refuses writes. */
|
|
208
240
|
findOrCreateTrack(type, name) {
|
|
209
|
-
return (this.tracks.find((t) => t.type === type
|
|
241
|
+
return (this.tracks.find((t) => t.type === type && !t.locked) ??
|
|
242
|
+
this.addTrack(type, name));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Refuse an edit that writes to a locked clip, a clip on a locked track, or
|
|
246
|
+
* a locked destination track. Every op that changes a clip or places one
|
|
247
|
+
* asks here, so the lock means the same thing on every path.
|
|
248
|
+
*/
|
|
249
|
+
assertWritable(clips, destinationTrackId) {
|
|
250
|
+
const tracks = new Map(this.tracks.map((track) => [track.id, track]));
|
|
251
|
+
for (const clip of clips) {
|
|
252
|
+
if (clip.locked || tracks.get(clip.trackId)?.locked) {
|
|
253
|
+
throw new Error(`Clip "${clip.name}" or its track is locked. Unlock it before editing.`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (destinationTrackId !== undefined) {
|
|
257
|
+
const track = tracks.get(destinationTrackId);
|
|
258
|
+
if (track?.locked) {
|
|
259
|
+
throw new Error(`Track "${track.name}" is locked. Unlock it or choose another track.`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
210
262
|
}
|
|
211
263
|
resolveTrack(idOrName) {
|
|
212
264
|
const byId = this.tracks.find((t) => t.id === idOrName);
|
|
@@ -278,20 +330,13 @@ class OpScope {
|
|
|
278
330
|
return resolveClipTarget(this.state, target);
|
|
279
331
|
}
|
|
280
332
|
editTargets(clip) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
candidate.linkId === clip.linkId));
|
|
289
|
-
const tracks = new Map(this.tracks.map((track) => [track.id, track]));
|
|
290
|
-
for (const target of targets) {
|
|
291
|
-
if (target.locked || tracks.get(target.trackId)?.locked) {
|
|
292
|
-
throw new Error(`Clip "${target.name}" or its track is locked. Unlock it before editing.`);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
333
|
+
// A group's children and, when links are followed, every partner of the
|
|
334
|
+
// clip or of a child: the whole unit the editor would move.
|
|
335
|
+
const unit = moveUnitIds(this.clips, [clip.id], {
|
|
336
|
+
followLinks: this.ctx.followLinks !== false
|
|
337
|
+
});
|
|
338
|
+
const targets = this.clips.filter((candidate) => unit.has(candidate.id));
|
|
339
|
+
this.assertWritable(targets);
|
|
295
340
|
return targets;
|
|
296
341
|
}
|
|
297
342
|
assertPictureTrack(track) {
|
|
@@ -307,12 +352,59 @@ class OpScope {
|
|
|
307
352
|
}
|
|
308
353
|
return next;
|
|
309
354
|
}
|
|
355
|
+
/**
|
|
356
|
+
* How far a clip's source reaches, for capping a trim that grows it: the
|
|
357
|
+
* asset's own duration when the host knows it. An imported audio or video
|
|
358
|
+
* clip whose length nobody knows may not grow past its current out-point,
|
|
359
|
+
* the rule the editor's keyboard trim applies. Undefined means no cap — a
|
|
360
|
+
* picture with no running time, or a generated clip a regeneration can
|
|
361
|
+
* lengthen.
|
|
362
|
+
*/
|
|
363
|
+
async sourceLimitMs(clip) {
|
|
364
|
+
if (clip.mediaType !== "audio" && clip.mediaType !== "video") {
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
if (!clip.currentAssetId) {
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
const known = await this.knownSourceMs(clip);
|
|
371
|
+
if (known !== undefined) {
|
|
372
|
+
return known;
|
|
373
|
+
}
|
|
374
|
+
if (clip.sourceType !== "imported") {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
return (clip.outPointMs ??
|
|
378
|
+
(clip.inPointMs ?? 0) + clip.durationMs * sourceRate(clip));
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* The length of a clip's audio or video asset when the host knows it.
|
|
382
|
+
* Undefined for anything else, including an asset it cannot read.
|
|
383
|
+
*/
|
|
384
|
+
async knownSourceMs(clip) {
|
|
385
|
+
if (clip.mediaType !== "audio" && clip.mediaType !== "video") {
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
const assetId = clip.currentAssetId;
|
|
389
|
+
if (!assetId) {
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
let known;
|
|
393
|
+
try {
|
|
394
|
+
known = (await this.ctx.resolveAsset?.(assetId))?.durationMs;
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
// An unreadable asset is an unknown length.
|
|
398
|
+
known = undefined;
|
|
399
|
+
}
|
|
400
|
+
return known !== undefined && known > 0 ? known : undefined;
|
|
401
|
+
}
|
|
310
402
|
/**
|
|
311
403
|
* The body of `trim_clip`, shared with `set_clip_params`: a caller that
|
|
312
404
|
* sends `durationMs` alongside a style change means the same edit either
|
|
313
405
|
* way, and two copies of the group-trim rule would drift.
|
|
314
406
|
*/
|
|
315
|
-
applyTrim(clip, patch) {
|
|
407
|
+
applyTrim(clip, patch, maxSourceMs) {
|
|
316
408
|
if (patch.durationMs === undefined &&
|
|
317
409
|
patch.inPointMs === undefined &&
|
|
318
410
|
patch.outPointMs === undefined) {
|
|
@@ -322,18 +414,51 @@ class OpScope {
|
|
|
322
414
|
throw new Error("inPointMs cannot be negative.");
|
|
323
415
|
}
|
|
324
416
|
const prospective = patch.durationMs !== undefined && !isGroupClip(clip)
|
|
325
|
-
?
|
|
417
|
+
? trimSourceBounded(clip, patch.durationMs - clip.durationMs, maxSourceMs)
|
|
326
418
|
: clip;
|
|
327
419
|
const sourceIn = patch.inPointMs ?? prospective.inPointMs ?? 0;
|
|
328
420
|
const sourceOut = patch.outPointMs ?? prospective.outPointMs;
|
|
329
421
|
if (sourceOut !== undefined && sourceOut <= sourceIn) {
|
|
330
422
|
throw new Error("outPointMs must be greater than inPointMs.");
|
|
331
423
|
}
|
|
424
|
+
if (maxSourceMs !== undefined &&
|
|
425
|
+
patch.outPointMs !== undefined &&
|
|
426
|
+
patch.outPointMs > maxSourceMs) {
|
|
427
|
+
throw new Error(`outPointMs ${patch.outPointMs} is past the end of the source (${maxSourceMs}ms).`);
|
|
428
|
+
}
|
|
332
429
|
if (isGroupClip(clip) &&
|
|
333
430
|
(patch.inPointMs !== undefined || patch.outPointMs !== undefined)) {
|
|
334
431
|
throw new Error("Groups carry no source media; trim their duration instead.");
|
|
335
432
|
}
|
|
336
433
|
const targets = this.editTargets(clip);
|
|
434
|
+
// An in-point alone is a slip: the window keeps its length and moves
|
|
435
|
+
// through the source, so the out-point moves with it — and a linked
|
|
436
|
+
// partner slips by the same timeline amount, or picture and sound part.
|
|
437
|
+
if (patch.inPointMs !== undefined &&
|
|
438
|
+
patch.durationMs === undefined &&
|
|
439
|
+
patch.outPointMs === undefined) {
|
|
440
|
+
const slipMs = (patch.inPointMs - (clip.inPointMs ?? 0)) / sourceRate(clip);
|
|
441
|
+
const slipped = targets.map((target) => {
|
|
442
|
+
const inPointMs = target.id === clip.id
|
|
443
|
+
? patch.inPointMs
|
|
444
|
+
: (target.inPointMs ?? 0) + slipMs * sourceRate(target);
|
|
445
|
+
if (inPointMs < 0) {
|
|
446
|
+
throw new Error(`Linked clip "${target.name}" has no source before its in-point to slip into.`);
|
|
447
|
+
}
|
|
448
|
+
return { target, inPointMs };
|
|
449
|
+
});
|
|
450
|
+
for (const { target, inPointMs } of slipped) {
|
|
451
|
+
target.inPointMs = inPointMs;
|
|
452
|
+
target.outPointMs = inPointMs + target.durationMs * sourceRate(target);
|
|
453
|
+
// Words are timed on the clip's clock; the media moved under it.
|
|
454
|
+
if (target.caption) {
|
|
455
|
+
target.caption = retimeCaption(target.caption, -slipMs);
|
|
456
|
+
}
|
|
457
|
+
this.mediaTracks = resliceTracksForTrimmedClip(this.mediaTracks, target);
|
|
458
|
+
this.touch(target.id);
|
|
459
|
+
}
|
|
460
|
+
return clip;
|
|
461
|
+
}
|
|
337
462
|
if (targets.length > 1 &&
|
|
338
463
|
patch.durationMs !== undefined &&
|
|
339
464
|
!isGroupClip(clip)) {
|
|
@@ -342,7 +467,9 @@ class OpScope {
|
|
|
342
467
|
if (target.durationMs + delta <= 0) {
|
|
343
468
|
throw new Error("Linked trim would remove a clip. Shorten the trim or unlink it first.");
|
|
344
469
|
}
|
|
345
|
-
const trimmed =
|
|
470
|
+
const trimmed = target.id === clip.id
|
|
471
|
+
? trimSourceBounded(target, delta, maxSourceMs)
|
|
472
|
+
: trimClip(target, "end", delta);
|
|
346
473
|
if (target.id === clip.id) {
|
|
347
474
|
if (patch.inPointMs !== undefined) {
|
|
348
475
|
trimmed.inPointMs = patch.inPointMs;
|
|
@@ -381,7 +508,7 @@ class OpScope {
|
|
|
381
508
|
if (patch.durationMs <= 0) {
|
|
382
509
|
throw new Error(`durationMs must be greater than 0 (got ${patch.durationMs}); delete the clip instead of trimming it to nothing`);
|
|
383
510
|
}
|
|
384
|
-
next = this.replaceClip(clip,
|
|
511
|
+
next = this.replaceClip(clip, trimSourceBounded(clip, patch.durationMs - clip.durationMs, maxSourceMs));
|
|
385
512
|
}
|
|
386
513
|
if (patch.inPointMs !== undefined) {
|
|
387
514
|
next.inPointMs = patch.inPointMs;
|
|
@@ -397,12 +524,50 @@ class OpScope {
|
|
|
397
524
|
this.mediaTracks = resliceTracksForTrimmedClip(this.mediaTracks, next);
|
|
398
525
|
return next;
|
|
399
526
|
}
|
|
527
|
+
/**
|
|
528
|
+
* Move a clip's head by `deltaMs` (positive grows it earlier), for the clip
|
|
529
|
+
* and every linked partner, keeping each one's end where it was. A group
|
|
530
|
+
* carries its children with the edge (D4).
|
|
531
|
+
*/
|
|
532
|
+
applyHeadTrim(clip, deltaMs) {
|
|
533
|
+
const targets = this.editTargets(clip);
|
|
534
|
+
if (isGroupClip(clip)) {
|
|
535
|
+
this.clips = trimGroup(this.clips, clip.id, "start", deltaMs);
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
// Every partner is trimmed before any is written, so one that runs out
|
|
539
|
+
// of source leaves the whole group as it was.
|
|
540
|
+
const trimmed = targets.map((target) => ({
|
|
541
|
+
target,
|
|
542
|
+
next: trimClip(target, "start", deltaMs)
|
|
543
|
+
}));
|
|
544
|
+
for (const { target, next } of trimmed) {
|
|
545
|
+
this.replaceClip(target, next);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
for (const target of targets) {
|
|
549
|
+
const next = this.resolveClip(target.id);
|
|
550
|
+
this.mediaTracks = resliceTracksForTrimmedClip(this.mediaTracks, next);
|
|
551
|
+
this.touch(next.id);
|
|
552
|
+
}
|
|
553
|
+
return this.resolveClip(clip.id);
|
|
554
|
+
}
|
|
400
555
|
/** The body of `move_clip`, shared with `set_clip_params`. */
|
|
401
556
|
applyMove(clip, patch) {
|
|
402
557
|
if (patch.startMs === undefined && patch.trackId === undefined) {
|
|
403
558
|
return clip;
|
|
404
559
|
}
|
|
405
560
|
const targets = this.editTargets(clip);
|
|
561
|
+
// The destination is checked before anything moves, so a refused track
|
|
562
|
+
// leaves the clip and its partners where they were.
|
|
563
|
+
let destination;
|
|
564
|
+
if (patch.trackId !== undefined) {
|
|
565
|
+
destination = this.resolveTrack(patch.trackId);
|
|
566
|
+
this.assertWritable([], destination.id);
|
|
567
|
+
if (!clipFitsTrack(clip.mediaType, destination.type)) {
|
|
568
|
+
throw new Error(`Clip "${clip.name}" is a ${clip.mediaType} clip and cannot go on ${destination.type} track "${destination.name}".`);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
406
571
|
if (targets.length > 1 &&
|
|
407
572
|
patch.startMs !== undefined &&
|
|
408
573
|
!isGroupClip(clip)) {
|
|
@@ -412,31 +577,34 @@ class OpScope {
|
|
|
412
577
|
target.startMs += delta;
|
|
413
578
|
this.touch(target.id);
|
|
414
579
|
}
|
|
415
|
-
if (
|
|
416
|
-
clip.trackId =
|
|
580
|
+
if (destination) {
|
|
581
|
+
clip.trackId = destination.id;
|
|
417
582
|
}
|
|
418
583
|
return clip;
|
|
419
584
|
}
|
|
420
585
|
// Moving a group moves what it holds by the same delta (D4). Children keep
|
|
421
586
|
// their own tracks, so their z-order is untouched (I9) — only the group
|
|
422
587
|
// itself takes a new `trackId`.
|
|
423
|
-
let moved = clip;
|
|
424
588
|
if (isGroupClip(clip) && patch.startMs !== undefined) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
589
|
+
// One delta for the whole unit, clamped by its earliest member, so a
|
|
590
|
+
// child's J-cut partner keeps its offset at zero.
|
|
591
|
+
const minimum = Math.min(...targets.map((target) => target.startMs));
|
|
592
|
+
const delta = Math.max(-minimum, patch.startMs - clip.startMs);
|
|
593
|
+
for (const target of targets) {
|
|
594
|
+
target.startMs += delta;
|
|
595
|
+
}
|
|
428
596
|
this.touch(...targets.map((target) => target.id));
|
|
429
597
|
}
|
|
430
598
|
else if (patch.startMs !== undefined) {
|
|
431
599
|
clip.startMs = Math.max(0, patch.startMs);
|
|
432
600
|
}
|
|
433
|
-
if (
|
|
434
|
-
|
|
601
|
+
if (destination) {
|
|
602
|
+
clip.trackId = destination.id;
|
|
435
603
|
}
|
|
436
604
|
if (patch.startMs !== undefined || patch.trackId !== undefined) {
|
|
437
|
-
this.touch(
|
|
605
|
+
this.touch(clip.id);
|
|
438
606
|
}
|
|
439
|
-
return
|
|
607
|
+
return clip;
|
|
440
608
|
}
|
|
441
609
|
/** Resolve a marker by id, or by case-insensitive label. */
|
|
442
610
|
resolveMarker(target) {
|
|
@@ -493,6 +661,23 @@ class OpScope {
|
|
|
493
661
|
return serializeTrack(this.state, track);
|
|
494
662
|
}
|
|
495
663
|
}
|
|
664
|
+
/**
|
|
665
|
+
* `trimClip` on the end edge, capped at the source length when one is known.
|
|
666
|
+
* The refusal names the limit, which the engine's own message does not.
|
|
667
|
+
*/
|
|
668
|
+
function trimSourceBounded(clip, deltaMs, maxSourceMs) {
|
|
669
|
+
try {
|
|
670
|
+
return trimClip(clip, "end", deltaMs, maxSourceMs);
|
|
671
|
+
}
|
|
672
|
+
catch (error) {
|
|
673
|
+
if (maxSourceMs !== undefined &&
|
|
674
|
+
error instanceof Error &&
|
|
675
|
+
error.message.includes("beyond source out-point")) {
|
|
676
|
+
throw new Error(`Clip "${clip.name}" cannot be longer than its source: the source ends at ${maxSourceMs}ms.`);
|
|
677
|
+
}
|
|
678
|
+
throw error;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
496
681
|
function rejectUnknownClipParams(patch) {
|
|
497
682
|
for (const key of Object.keys(patch)) {
|
|
498
683
|
if (CLIP_PARAM_KEYS.includes(key)) {
|
|
@@ -620,1473 +805,1655 @@ async function buildCustomAnimation(scope, clip, input) {
|
|
|
620
805
|
custom
|
|
621
806
|
};
|
|
622
807
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
clips: scope.clips.map((c) => scope.clipOut(c)),
|
|
640
|
-
mediaTracks: scope.mediaTracks.map(serializeMediaTrack),
|
|
641
|
-
markers: state.markers.map((m) => ({ ...m }))
|
|
642
|
-
};
|
|
643
|
-
}
|
|
644
|
-
case "add_track":
|
|
645
|
-
case "move_track":
|
|
646
|
-
case "delete_track":
|
|
647
|
-
return runTrackOp(scope, op);
|
|
648
|
-
case "add_text_clip": {
|
|
649
|
-
const track = op.trackId
|
|
650
|
-
? scope.resolveTrack(op.trackId)
|
|
651
|
-
: scope.findOrCreateTrack("overlay", "Text");
|
|
652
|
-
// `style` wins over a top-level twin: a caller that sent both meant the
|
|
653
|
-
// bag it named.
|
|
654
|
-
scope.assertPictureTrack(track);
|
|
655
|
-
const s = { ...(op.loose ?? {}), ...(op.style ?? {}) };
|
|
656
|
-
const clip = makeClip({
|
|
657
|
-
id: scope.ctx.newId("clip"),
|
|
658
|
-
trackId: track.id,
|
|
659
|
-
name: op.name ?? op.text,
|
|
660
|
-
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
661
|
-
durationMs: op.durationMs ?? DEFAULT_TEXT_CLIP_DURATION_MS,
|
|
662
|
-
mediaType: "text",
|
|
663
|
-
sourceType: "imported",
|
|
664
|
-
status: "generated",
|
|
665
|
-
textStyle: textStyleWithDefaults(op.text, s)
|
|
808
|
+
function runMidiNoteEdit(scope, op) {
|
|
809
|
+
const clip = scope.resolveMidiClip(op.clip);
|
|
810
|
+
scope.assertWritable([clip]);
|
|
811
|
+
const before = clip.notes ?? [];
|
|
812
|
+
const notes = op.op === "transpose_clip"
|
|
813
|
+
? transposeNotes(before, op.semitones)
|
|
814
|
+
: op.op === "scale_velocity"
|
|
815
|
+
? scaleVelocity(before, op.factor)
|
|
816
|
+
: quantizeNotes(before, {
|
|
817
|
+
division: op.division,
|
|
818
|
+
strength: op.strength,
|
|
819
|
+
target: op.target,
|
|
820
|
+
// The grid is anchored at `tempo.offsetMs`, as the editor draws it.
|
|
821
|
+
phaseTick: msToTicks(resolveTempo(scope.state).offsetMs -
|
|
822
|
+
clip.startMs +
|
|
823
|
+
(clip.inPointMs ?? 0), resolveTempo(scope.state).bpm)
|
|
666
824
|
});
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
825
|
+
clip.notes = notes;
|
|
826
|
+
scope.touch(clip.id);
|
|
827
|
+
const result = {
|
|
828
|
+
ok: true,
|
|
829
|
+
clip: scope.clipOut(clip),
|
|
830
|
+
noteCount: notes.length
|
|
831
|
+
};
|
|
832
|
+
if (op.op === "quantize_notes") {
|
|
833
|
+
result.movedNoteCount = notes.filter((note, i) => note.startTick !== before[i]?.startTick ||
|
|
834
|
+
note.durationTick !== before[i]?.durationTick).length;
|
|
835
|
+
}
|
|
836
|
+
return result;
|
|
837
|
+
}
|
|
838
|
+
function runBeatEdit(scope, op) {
|
|
839
|
+
const beats = scope.state.setup?.beats ?? [];
|
|
840
|
+
const found = resolveBeatTarget(beats, op.beat);
|
|
841
|
+
const next = { ...found };
|
|
842
|
+
if (op.op === "update_beat") {
|
|
843
|
+
if (op.prompt !== undefined) {
|
|
844
|
+
next.prompt = op.prompt;
|
|
677
845
|
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
if (!resolveAsset) {
|
|
681
|
-
throw new Error("This timeline surface cannot look up assets, so an existing asset cannot be placed here.");
|
|
682
|
-
}
|
|
683
|
-
const found = await resolveAsset(op.asset);
|
|
684
|
-
if (!found) {
|
|
685
|
-
throw new Error(`No asset found for "${op.asset}". Pass an asset id or an asset:// URI from list_assets.`);
|
|
686
|
-
}
|
|
687
|
-
const mediaType = found.contentType.toLowerCase().startsWith("model/") ||
|
|
688
|
-
/\.(glb|gltf)$/i.test(found.name)
|
|
689
|
-
? "model3d"
|
|
690
|
-
: mediaTypeForContentType(found.contentType);
|
|
691
|
-
if (!mediaType) {
|
|
692
|
-
throw new Error(`Asset "${found.name}" is ${found.contentType}, which is not video, image, or audio and cannot go on a timeline.`);
|
|
693
|
-
}
|
|
694
|
-
if (mediaType !== "image" &&
|
|
695
|
-
mediaType !== "model3d" &&
|
|
696
|
-
op.durationMs === undefined &&
|
|
697
|
-
!found.durationMs &&
|
|
698
|
-
!scope.ctx.allowUnknownMediaDuration) {
|
|
699
|
-
throw new Error(`Asset "${found.name}" has no known duration. Supply durationMs or import media that can be probed.`);
|
|
700
|
-
}
|
|
701
|
-
const track = op.trackId
|
|
702
|
-
? scope.resolveTrack(op.trackId)
|
|
703
|
-
: scope.findOrCreateTrack(mediaType === "model3d" ? "video" : trackTypeForMediaType(mediaType));
|
|
704
|
-
if (!clipFitsTrack(mediaType, track.type)) {
|
|
705
|
-
throw new Error(`Asset "${found.name}" is not compatible with ${track.type} track "${track.name}".`);
|
|
706
|
-
}
|
|
707
|
-
const init = {
|
|
708
|
-
id: scope.ctx.newId("clip"),
|
|
709
|
-
trackId: track.id,
|
|
710
|
-
name: op.name ?? found.name,
|
|
711
|
-
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
712
|
-
durationMs: op.durationMs ?? found.durationMs ?? DEFAULT_MEDIA_CLIP_DURATION_MS,
|
|
713
|
-
mediaType,
|
|
714
|
-
sourceType: "imported",
|
|
715
|
-
status: "generated",
|
|
716
|
-
currentAssetId: found.id
|
|
717
|
-
};
|
|
718
|
-
if (mediaType === "model3d") {
|
|
719
|
-
init.model3dStyle = model3dStyleWithPatch(undefined, undefined);
|
|
720
|
-
}
|
|
721
|
-
if (found.thumbnailAssetId) {
|
|
722
|
-
init.thumbnailAssetId = found.thumbnailAssetId;
|
|
723
|
-
}
|
|
724
|
-
const clip = makeClip(init);
|
|
725
|
-
if (op.transform) {
|
|
726
|
-
clip.transform = mergeClipTransform(clip.transform, op.transform);
|
|
727
|
-
}
|
|
728
|
-
scope.clips.push(clip);
|
|
729
|
-
state.selectedClipIds = [clip.id];
|
|
730
|
-
scope.touch(clip.id);
|
|
731
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
846
|
+
if (op.durationMs !== undefined) {
|
|
847
|
+
next.duration_ms = Math.max(1, Math.round(op.durationMs));
|
|
732
848
|
}
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
? scope.resolveTrack(op.trackId)
|
|
736
|
-
: scope.findOrCreateTrack("overlay", "Shapes");
|
|
737
|
-
scope.assertPictureTrack(track);
|
|
738
|
-
const shapeArg = resolveShapeArg(op.shape, op.shapeStyle, op.loose ?? {});
|
|
739
|
-
const clip = makeClip({
|
|
740
|
-
id: scope.ctx.newId("clip"),
|
|
741
|
-
trackId: track.id,
|
|
742
|
-
name: op.name ?? capitalize(shapeArg.kind),
|
|
743
|
-
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
744
|
-
durationMs: op.durationMs ?? DEFAULT_TEXT_CLIP_DURATION_MS,
|
|
745
|
-
mediaType: "shape",
|
|
746
|
-
sourceType: "imported",
|
|
747
|
-
status: "generated",
|
|
748
|
-
shapeStyle: shapeStyleWithDefaults(shapeArg)
|
|
749
|
-
});
|
|
750
|
-
if (op.opacity !== undefined) {
|
|
751
|
-
clip.opacity = op.opacity;
|
|
752
|
-
}
|
|
753
|
-
if (op.transform) {
|
|
754
|
-
clip.transform = mergeClipTransform(clip.transform, op.transform);
|
|
755
|
-
}
|
|
756
|
-
scope.clips.push(clip);
|
|
757
|
-
state.selectedClipIds = [clip.id];
|
|
758
|
-
scope.touch(clip.id);
|
|
759
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
849
|
+
if (op.transition !== undefined) {
|
|
850
|
+
next.transition = op.transition ?? undefined;
|
|
760
851
|
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
if (!assetId) {
|
|
764
|
-
throw new Error("add_model3d_clip needs the glTF's asset id — a 3D clip draws its " +
|
|
765
|
-
"asset the way an image clip draws its image. list_assets prints " +
|
|
766
|
-
"the ids.");
|
|
767
|
-
}
|
|
768
|
-
// 3D is picture (D1), so it lands where a title lands: the first overlay
|
|
769
|
-
// track, or one made for it.
|
|
770
|
-
const track = op.trackId
|
|
771
|
-
? scope.resolveTrack(op.trackId)
|
|
772
|
-
: scope.findOrCreateTrack("overlay", "3D");
|
|
773
|
-
scope.assertPictureTrack(track);
|
|
774
|
-
const clip = makeClip({
|
|
775
|
-
id: scope.ctx.newId("clip"),
|
|
776
|
-
trackId: track.id,
|
|
777
|
-
name: DEFAULT_MODEL3D_CLIP_NAME,
|
|
778
|
-
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
779
|
-
durationMs: op.durationMs ?? DEFAULT_MODEL3D_CLIP_DURATION_MS,
|
|
780
|
-
mediaType: "model3d",
|
|
781
|
-
sourceType: "imported",
|
|
782
|
-
status: "generated",
|
|
783
|
-
currentAssetId: assetId,
|
|
784
|
-
model3dStyle: model3dStyleWithPatch(undefined, op.style)
|
|
785
|
-
});
|
|
786
|
-
if (op.transform) {
|
|
787
|
-
clip.transform = mergeClipTransform(clip.transform, op.transform);
|
|
788
|
-
}
|
|
789
|
-
scope.clips.push(clip);
|
|
790
|
-
state.selectedClipIds = [clip.id];
|
|
791
|
-
scope.touch(clip.id);
|
|
792
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
852
|
+
if (op.voiceover !== undefined) {
|
|
853
|
+
next.voiceover = op.voiceover;
|
|
793
854
|
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
if (clip.mediaType !== "model3d") {
|
|
797
|
-
throw new Error(`Clip "${clip.name}" is a ${clip.mediaType} clip, not a 3D clip — ` +
|
|
798
|
-
"model3dStyle names a camera, an animation and lighting for a " +
|
|
799
|
-
"glTF, and nothing else reads it.");
|
|
800
|
-
}
|
|
801
|
-
clip.model3dStyle = model3dStyleWithPatch(clip.model3dStyle, op.patch);
|
|
802
|
-
scope.touch(clip.id);
|
|
803
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
855
|
+
if (op.music !== undefined) {
|
|
856
|
+
next.music = op.music;
|
|
804
857
|
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
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
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
858
|
+
}
|
|
859
|
+
const previous = scope.state.setup;
|
|
860
|
+
scope.state.setup = {
|
|
861
|
+
...previous,
|
|
862
|
+
stage: previous?.stage ?? "review",
|
|
863
|
+
brief: previous?.brief ?? "",
|
|
864
|
+
format: previous?.format,
|
|
865
|
+
voiceover: previous?.voiceover,
|
|
866
|
+
beats: op.op === "remove_beat"
|
|
867
|
+
? beats.filter((b) => b.id !== found.id)
|
|
868
|
+
: beats.map((b) => (b.id === found.id ? next : b))
|
|
869
|
+
};
|
|
870
|
+
return op.op === "remove_beat"
|
|
871
|
+
? { ok: true, removed: { ...found } }
|
|
872
|
+
: { ok: true, beat: { ...next } };
|
|
873
|
+
}
|
|
874
|
+
/** One handler per op, so each is checked against its own op type. */
|
|
875
|
+
const OP_HANDLERS = {
|
|
876
|
+
get_state: (scope) => {
|
|
877
|
+
const state = scope.state;
|
|
878
|
+
const durationMs = scope.clips.reduce((m, c) => Math.max(m, c.startMs + c.durationMs), 0);
|
|
879
|
+
return {
|
|
880
|
+
ok: true,
|
|
881
|
+
fps: state.fps,
|
|
882
|
+
width: state.width,
|
|
883
|
+
height: state.height,
|
|
884
|
+
durationMs,
|
|
885
|
+
playheadMs: state.playheadMs,
|
|
886
|
+
selectedClipIds: [...state.selectedClipIds],
|
|
887
|
+
tempo: state.tempo,
|
|
888
|
+
tracks: scope.tracks.map((t) => scope.trackOut(t)),
|
|
889
|
+
clips: scope.clips.map((c) => scope.clipOut(c)),
|
|
890
|
+
mediaTracks: scope.mediaTracks.map(serializeMediaTrack),
|
|
891
|
+
markers: state.markers.map((m) => ({ ...m }))
|
|
892
|
+
};
|
|
893
|
+
},
|
|
894
|
+
add_track: runTrackOp,
|
|
895
|
+
move_track: runTrackOp,
|
|
896
|
+
delete_track: runTrackOp,
|
|
897
|
+
add_text_clip: (scope, op) => {
|
|
898
|
+
const state = scope.state;
|
|
899
|
+
const track = op.trackId
|
|
900
|
+
? scope.resolveTrack(op.trackId)
|
|
901
|
+
: scope.findOrCreateTrack("overlay", "Text");
|
|
902
|
+
// `style` wins over a top-level twin: a caller that sent both meant the
|
|
903
|
+
// bag it named.
|
|
904
|
+
scope.assertPictureTrack(track);
|
|
905
|
+
scope.assertWritable([], track.id);
|
|
906
|
+
const s = { ...(op.loose ?? {}), ...(op.style ?? {}) };
|
|
907
|
+
const clip = makeClip({
|
|
908
|
+
id: scope.ctx.newId("clip"),
|
|
909
|
+
trackId: track.id,
|
|
910
|
+
name: op.name ?? op.text,
|
|
911
|
+
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
912
|
+
durationMs: op.durationMs ?? DEFAULT_TEXT_CLIP_DURATION_MS,
|
|
913
|
+
mediaType: "text",
|
|
914
|
+
sourceType: "imported",
|
|
915
|
+
status: "generated",
|
|
916
|
+
textStyle: textStyleWithDefaults(op.text, s)
|
|
917
|
+
});
|
|
918
|
+
if (op.opacity !== undefined) {
|
|
919
|
+
clip.opacity = op.opacity;
|
|
920
|
+
}
|
|
921
|
+
if (op.transform) {
|
|
922
|
+
clip.transform = mergeClipTransform(clip.transform, op.transform);
|
|
923
|
+
}
|
|
924
|
+
scope.clips.push(clip);
|
|
925
|
+
state.selectedClipIds = [clip.id];
|
|
926
|
+
scope.touch(clip.id);
|
|
927
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
928
|
+
},
|
|
929
|
+
add_media_clip: async (scope, op) => {
|
|
930
|
+
const state = scope.state;
|
|
931
|
+
const resolveAsset = scope.ctx.resolveAsset;
|
|
932
|
+
if (!resolveAsset) {
|
|
933
|
+
throw new Error("This timeline surface cannot look up assets, so an existing asset cannot be placed here.");
|
|
934
|
+
}
|
|
935
|
+
const found = await resolveAsset(op.asset);
|
|
936
|
+
if (!found) {
|
|
937
|
+
throw new Error(`No asset found for "${op.asset}". Pass an asset id or an asset:// URI from list_assets.`);
|
|
938
|
+
}
|
|
939
|
+
const mediaType = found.contentType.toLowerCase().startsWith("model/") ||
|
|
940
|
+
/\.(glb|gltf)$/i.test(found.name)
|
|
941
|
+
? "model3d"
|
|
942
|
+
: mediaTypeForContentType(found.contentType);
|
|
943
|
+
if (!mediaType) {
|
|
944
|
+
throw new Error(`Asset "${found.name}" is ${found.contentType}, which is not video, image, or audio and cannot go on a timeline.`);
|
|
945
|
+
}
|
|
946
|
+
if (mediaType !== "image" &&
|
|
947
|
+
mediaType !== "model3d" &&
|
|
948
|
+
op.durationMs === undefined &&
|
|
949
|
+
!found.durationMs &&
|
|
950
|
+
!scope.ctx.allowUnknownMediaDuration) {
|
|
951
|
+
throw new Error(`Asset "${found.name}" has no known duration. Supply durationMs or import media that can be probed.`);
|
|
952
|
+
}
|
|
953
|
+
const track = op.trackId
|
|
954
|
+
? scope.resolveTrack(op.trackId)
|
|
955
|
+
: scope.findOrCreateTrack(mediaType === "model3d" ? "video" : trackTypeForMediaType(mediaType));
|
|
956
|
+
if (!clipFitsTrack(mediaType, track.type)) {
|
|
957
|
+
throw new Error(`Asset "${found.name}" is not compatible with ${track.type} track "${track.name}".`);
|
|
958
|
+
}
|
|
959
|
+
scope.assertWritable([], track.id);
|
|
960
|
+
const init = {
|
|
961
|
+
id: scope.ctx.newId("clip"),
|
|
962
|
+
trackId: track.id,
|
|
963
|
+
name: op.name ?? found.name,
|
|
964
|
+
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
965
|
+
durationMs: op.durationMs ?? found.durationMs ?? DEFAULT_MEDIA_CLIP_DURATION_MS,
|
|
966
|
+
mediaType,
|
|
967
|
+
sourceType: "imported",
|
|
968
|
+
status: "generated",
|
|
969
|
+
currentAssetId: found.id
|
|
970
|
+
};
|
|
971
|
+
if (mediaType === "model3d") {
|
|
972
|
+
init.model3dStyle = model3dStyleWithPatch(undefined, undefined);
|
|
973
|
+
}
|
|
974
|
+
if (found.thumbnailAssetId) {
|
|
975
|
+
init.thumbnailAssetId = found.thumbnailAssetId;
|
|
976
|
+
}
|
|
977
|
+
const clip = makeClip(init);
|
|
978
|
+
if (op.transform) {
|
|
979
|
+
clip.transform = mergeClipTransform(clip.transform, op.transform);
|
|
980
|
+
}
|
|
981
|
+
scope.clips.push(clip);
|
|
982
|
+
state.selectedClipIds = [clip.id];
|
|
983
|
+
scope.touch(clip.id);
|
|
984
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
985
|
+
},
|
|
986
|
+
add_shape_clip: (scope, op) => {
|
|
987
|
+
const state = scope.state;
|
|
988
|
+
const track = op.trackId
|
|
989
|
+
? scope.resolveTrack(op.trackId)
|
|
990
|
+
: scope.findOrCreateTrack("overlay", "Shapes");
|
|
991
|
+
scope.assertPictureTrack(track);
|
|
992
|
+
scope.assertWritable([], track.id);
|
|
993
|
+
const shapeArg = resolveShapeArg(op.shape, op.shapeStyle, op.loose ?? {});
|
|
994
|
+
const clip = makeClip({
|
|
995
|
+
id: scope.ctx.newId("clip"),
|
|
996
|
+
trackId: track.id,
|
|
997
|
+
name: op.name ?? capitalize(shapeArg.kind),
|
|
998
|
+
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
999
|
+
durationMs: op.durationMs ?? DEFAULT_TEXT_CLIP_DURATION_MS,
|
|
1000
|
+
mediaType: "shape",
|
|
1001
|
+
sourceType: "imported",
|
|
1002
|
+
status: "generated",
|
|
1003
|
+
shapeStyle: shapeStyleWithDefaults(shapeArg)
|
|
1004
|
+
});
|
|
1005
|
+
if (op.opacity !== undefined) {
|
|
1006
|
+
clip.opacity = op.opacity;
|
|
1007
|
+
}
|
|
1008
|
+
if (op.transform) {
|
|
1009
|
+
clip.transform = mergeClipTransform(clip.transform, op.transform);
|
|
1010
|
+
}
|
|
1011
|
+
scope.clips.push(clip);
|
|
1012
|
+
state.selectedClipIds = [clip.id];
|
|
1013
|
+
scope.touch(clip.id);
|
|
1014
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1015
|
+
},
|
|
1016
|
+
add_model3d_clip: async (scope, op) => {
|
|
1017
|
+
const state = scope.state;
|
|
1018
|
+
const assetId = op.assetId.trim();
|
|
1019
|
+
if (!assetId) {
|
|
1020
|
+
throw new Error("add_model3d_clip needs the glTF's asset id — a 3D clip draws its " +
|
|
1021
|
+
"asset the way an image clip draws its image. list_assets prints " +
|
|
1022
|
+
"the ids.");
|
|
1023
|
+
}
|
|
1024
|
+
// 3D is picture (D1), so it lands where a title lands: the first overlay
|
|
1025
|
+
// track, or one made for it.
|
|
1026
|
+
const track = op.trackId
|
|
1027
|
+
? scope.resolveTrack(op.trackId)
|
|
1028
|
+
: scope.findOrCreateTrack("overlay", "3D");
|
|
1029
|
+
scope.assertPictureTrack(track);
|
|
1030
|
+
scope.assertWritable([], track.id);
|
|
1031
|
+
// The id is stored on the clip and read back by every renderer, so a
|
|
1032
|
+
// short or `asset://` form resolves to the asset's full id here. A
|
|
1033
|
+
// 12-character prefix nothing resolves would store a dangling id.
|
|
1034
|
+
const found = await scope.ctx.resolveAsset?.(assetId);
|
|
1035
|
+
if (!found && isShortResourceId(assetId)) {
|
|
1036
|
+
throw new Error(`No asset found for "${assetId}". Pass the glTF's asset id from list_assets.`);
|
|
1037
|
+
}
|
|
1038
|
+
const currentAssetId = found?.id ?? assetId;
|
|
1039
|
+
const clip = makeClip({
|
|
1040
|
+
id: scope.ctx.newId("clip"),
|
|
1041
|
+
trackId: track.id,
|
|
1042
|
+
name: DEFAULT_MODEL3D_CLIP_NAME,
|
|
1043
|
+
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
1044
|
+
durationMs: op.durationMs ?? DEFAULT_MODEL3D_CLIP_DURATION_MS,
|
|
1045
|
+
mediaType: "model3d",
|
|
1046
|
+
sourceType: "imported",
|
|
1047
|
+
status: "generated",
|
|
1048
|
+
currentAssetId,
|
|
1049
|
+
model3dStyle: model3dStyleWithPatch(undefined, op.style)
|
|
1050
|
+
});
|
|
1051
|
+
if (op.transform) {
|
|
1052
|
+
clip.transform = mergeClipTransform(clip.transform, op.transform);
|
|
1053
|
+
}
|
|
1054
|
+
scope.clips.push(clip);
|
|
1055
|
+
state.selectedClipIds = [clip.id];
|
|
1056
|
+
scope.touch(clip.id);
|
|
1057
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1058
|
+
},
|
|
1059
|
+
set_model3d_style: (scope, op) => {
|
|
1060
|
+
const clip = scope.resolveClip(op.target);
|
|
1061
|
+
if (clip.mediaType !== "model3d") {
|
|
1062
|
+
throw new Error(`Clip "${clip.name}" is a ${clip.mediaType} clip, not a 3D clip — ` +
|
|
1063
|
+
"model3dStyle names a camera, an animation and lighting for a " +
|
|
1064
|
+
"glTF, and nothing else reads it.");
|
|
1065
|
+
}
|
|
1066
|
+
clip.model3dStyle = model3dStyleWithPatch(clip.model3dStyle, op.patch);
|
|
1067
|
+
scope.touch(clip.id);
|
|
1068
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1069
|
+
},
|
|
1070
|
+
bake_model3d_clip: async (scope, op) => {
|
|
1071
|
+
const state = scope.state;
|
|
1072
|
+
const clip = scope.resolveClip(op.target);
|
|
1073
|
+
if (clip.mediaType !== "model3d") {
|
|
1074
|
+
throw new Error(`Clip "${clip.name}" is a ${clip.mediaType} clip, not a 3D clip — ` +
|
|
1075
|
+
"only a 3D clip has a camera, lighting and a glTF to render.");
|
|
1076
|
+
}
|
|
1077
|
+
const style = clip.model3dStyle;
|
|
1078
|
+
if (!style) {
|
|
1079
|
+
throw new Error(`Clip "${clip.name}" has no model3dStyle, so nothing names the ` +
|
|
1080
|
+
"camera, lighting or animation a bake would render.");
|
|
1081
|
+
}
|
|
1082
|
+
if (!clip.currentAssetId) {
|
|
1083
|
+
throw new Error(`Clip "${clip.name}" has no glTF asset to bake — a 3D clip draws ` +
|
|
1084
|
+
"its asset the way an image clip draws its image.");
|
|
1085
|
+
}
|
|
1086
|
+
const sequence = {
|
|
1087
|
+
fps: state.fps,
|
|
1088
|
+
width: state.width,
|
|
1089
|
+
height: state.height
|
|
1090
|
+
};
|
|
1091
|
+
const dependencyHash = computeModel3DBakeHash(clip, sequence);
|
|
1092
|
+
if (!scope.ctx.bakeModel3DClip) {
|
|
888
1093
|
return {
|
|
889
1094
|
ok: true,
|
|
890
|
-
clip: scope.clipOut(
|
|
891
|
-
|
|
1095
|
+
clip: scope.clipOut(clip),
|
|
1096
|
+
bakeStarted: false,
|
|
1097
|
+
note: "This surface has no Blender renderer, so nothing was baked."
|
|
892
1098
|
};
|
|
893
1099
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
scope.
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
.filter((target) => at > target.startMs && at < target.startMs + target.durationMs);
|
|
943
|
-
const leftLink = targets.length > 1 ? scope.ctx.newId("link") : undefined;
|
|
944
|
-
const rightLink = targets.length > 1 ? scope.ctx.newId("link") : undefined;
|
|
945
|
-
const halves = [];
|
|
946
|
-
for (const target of targets) {
|
|
947
|
-
const [left, right] = splitClip(target, at);
|
|
948
|
-
left.id = scope.ctx.newId("clip");
|
|
949
|
-
right.id = scope.ctx.newId("clip");
|
|
950
|
-
if (targets.length > 1) {
|
|
951
|
-
left.linkId = leftLink;
|
|
952
|
-
right.linkId = rightLink;
|
|
953
|
-
}
|
|
954
|
-
const idx = scope.clips.findIndex((c) => c.id === target.id);
|
|
955
|
-
scope.clips.splice(idx, 1, left, right);
|
|
956
|
-
state.selectedClipIds = state.selectedClipIds.filter((id) => id !== target.id);
|
|
957
|
-
scope.touch(target.id, left.id, right.id);
|
|
958
|
-
scope.mediaTracks = resliceTracksForSplitClip(scope.mediaTracks, target.id, left, right, () => scope.ctx.newId("track"));
|
|
959
|
-
halves.push(left, right);
|
|
960
|
-
}
|
|
961
|
-
return { ok: true, clips: halves.map((half) => scope.clipOut(half)) };
|
|
1100
|
+
const baked = await scope.ctx.bakeModel3DClip({
|
|
1101
|
+
clip,
|
|
1102
|
+
sequence,
|
|
1103
|
+
dependencyHash
|
|
1104
|
+
});
|
|
1105
|
+
const now = (scope.ctx.now ?? (() => new Date().toISOString()))();
|
|
1106
|
+
clip.model3dStyle = {
|
|
1107
|
+
...style,
|
|
1108
|
+
bake: { assetId: baked.assetId, dependencyHash }
|
|
1109
|
+
};
|
|
1110
|
+
// The bake joins the clip's own version history, so an earlier one can
|
|
1111
|
+
// be restored the way an earlier generation can.
|
|
1112
|
+
clip.versions = [
|
|
1113
|
+
...(clip.versions ?? []),
|
|
1114
|
+
makeClipVersion({
|
|
1115
|
+
id: scope.ctx.newId("version"),
|
|
1116
|
+
createdAt: now,
|
|
1117
|
+
workflowUpdatedAt: now,
|
|
1118
|
+
jobId: baked.jobId ?? "",
|
|
1119
|
+
assetId: baked.assetId,
|
|
1120
|
+
dependencyHash
|
|
1121
|
+
})
|
|
1122
|
+
];
|
|
1123
|
+
scope.touch(clip.id);
|
|
1124
|
+
return { ok: true, clip: scope.clipOut(clip), bakeStarted: true };
|
|
1125
|
+
},
|
|
1126
|
+
add_group: (scope, op) => {
|
|
1127
|
+
const state = scope.state;
|
|
1128
|
+
// Resolve every child before anything is written: a half-applied group
|
|
1129
|
+
// leaves the caller with an empty group and no idea which of its clips
|
|
1130
|
+
// moved.
|
|
1131
|
+
const targets = (op.children ?? []).map((ref) => scope.resolveClip(ref));
|
|
1132
|
+
const track = op.trackId
|
|
1133
|
+
? scope.resolveTrack(op.trackId)
|
|
1134
|
+
: scope.findOrCreateTrack("overlay", "Groups");
|
|
1135
|
+
scope.assertWritable(targets, track.id);
|
|
1136
|
+
const group = makeClip({
|
|
1137
|
+
id: scope.ctx.newId("clip"),
|
|
1138
|
+
trackId: track.id,
|
|
1139
|
+
name: op.name,
|
|
1140
|
+
startMs: op.startMs,
|
|
1141
|
+
durationMs: op.durationMs,
|
|
1142
|
+
mediaType: "group",
|
|
1143
|
+
sourceType: "imported",
|
|
1144
|
+
status: "generated"
|
|
1145
|
+
});
|
|
1146
|
+
if (op.transform) {
|
|
1147
|
+
group.transform = mergeClipTransform(group.transform, op.transform);
|
|
962
1148
|
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
outPointMs: op.outPointMs
|
|
968
|
-
});
|
|
969
|
-
scope.mediaTracks = resliceTracksForTrimmedClip(scope.mediaTracks, trimmed);
|
|
970
|
-
return { ok: true, clip: scope.clipOut(trimmed) };
|
|
1149
|
+
scope.clips.push(group);
|
|
1150
|
+
for (const child of targets) {
|
|
1151
|
+
child.parentId = group.id;
|
|
1152
|
+
scope.touch(child.id);
|
|
971
1153
|
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1154
|
+
state.selectedClipIds = [group.id];
|
|
1155
|
+
scope.touch(group.id);
|
|
1156
|
+
return {
|
|
1157
|
+
ok: true,
|
|
1158
|
+
clip: scope.clipOut(group),
|
|
1159
|
+
children: targets.map((c) => c.id)
|
|
1160
|
+
};
|
|
1161
|
+
},
|
|
1162
|
+
generate_clip: (scope, op) => {
|
|
1163
|
+
const state = scope.state;
|
|
1164
|
+
const mediaType = op.kind === "text-to-video"
|
|
1165
|
+
? "video"
|
|
1166
|
+
: op.kind === "text-to-image"
|
|
1167
|
+
? "image"
|
|
1168
|
+
: "audio";
|
|
1169
|
+
const track = op.trackId
|
|
1170
|
+
? scope.resolveTrack(op.trackId)
|
|
1171
|
+
: op.kind === "text-to-audio"
|
|
1172
|
+
? scope.findOrCreateTrack("audio")
|
|
1173
|
+
: scope.findOrCreateTrack("video");
|
|
1174
|
+
if (!clipFitsTrack(mediaType, track.type)) {
|
|
1175
|
+
throw new Error(`A ${op.kind} clip is ${mediaType} and cannot go on ${track.type} track "${track.name}".`);
|
|
1176
|
+
}
|
|
1177
|
+
scope.assertWritable([], track.id);
|
|
1178
|
+
const generationStarted = op.autoGenerate !== false;
|
|
1179
|
+
const clip = makeClip({
|
|
1180
|
+
id: scope.ctx.newId("clip"),
|
|
1181
|
+
trackId: track.id,
|
|
1182
|
+
name: op.prompt,
|
|
1183
|
+
startMs: op.startMs ?? scope.trackEndMs(track.id),
|
|
1184
|
+
durationMs: op.durationMs ?? (op.kind === "text-to-audio" ? 3000 : 5000),
|
|
1185
|
+
mediaType,
|
|
1186
|
+
sourceType: "generated",
|
|
1187
|
+
bindingKind: op.kind,
|
|
1188
|
+
status: generationStarted ? "generating" : "draft",
|
|
1189
|
+
prompt: op.prompt,
|
|
1190
|
+
provider: op.provider,
|
|
1191
|
+
model: op.model,
|
|
1192
|
+
voice: op.voice,
|
|
1193
|
+
width: op.width,
|
|
1194
|
+
height: op.height,
|
|
1195
|
+
aspectRatio: op.aspectRatio,
|
|
1196
|
+
resolution: op.resolution
|
|
1197
|
+
});
|
|
1198
|
+
scope.clips.push(clip);
|
|
1199
|
+
state.selectedClipIds = [clip.id];
|
|
1200
|
+
scope.touch(clip.id);
|
|
1201
|
+
const result = { ok: true, clip: scope.clipOut(clip), generationStarted };
|
|
1202
|
+
if (!generationStarted) {
|
|
1203
|
+
result.note = "Generation not started (autoGenerate=false).";
|
|
1204
|
+
}
|
|
1205
|
+
return result;
|
|
1206
|
+
},
|
|
1207
|
+
split_clip: (scope, op) => {
|
|
1208
|
+
const state = scope.state;
|
|
1209
|
+
const clip = scope.resolveClip(op.target);
|
|
1210
|
+
const at = op.atMs ?? state.playheadMs;
|
|
1211
|
+
if (at <= clip.startMs || at >= clip.startMs + clip.durationMs) {
|
|
1212
|
+
throw new Error(`Split time ${at}ms is outside clip "${clip.name}".`);
|
|
1213
|
+
}
|
|
1214
|
+
const targets = scope
|
|
1215
|
+
.editTargets(clip)
|
|
1216
|
+
.filter((target) => at > target.startMs && at < target.startMs + target.durationMs);
|
|
1217
|
+
const leftLink = targets.length > 1 ? scope.ctx.newId("link") : undefined;
|
|
1218
|
+
const rightLink = targets.length > 1 ? scope.ctx.newId("link") : undefined;
|
|
1219
|
+
const halves = [];
|
|
1220
|
+
for (const target of targets) {
|
|
1221
|
+
const [cutLeft, cutRight] = splitClip(target, at);
|
|
1222
|
+
cutLeft.id = scope.ctx.newId("clip");
|
|
1223
|
+
cutRight.id = scope.ctx.newId("clip");
|
|
1224
|
+
if (targets.length > 1) {
|
|
1225
|
+
cutLeft.linkId = leftLink;
|
|
1226
|
+
cutRight.linkId = rightLink;
|
|
1227
|
+
}
|
|
1228
|
+
const split = splitClipMediaTracks(scope.mediaTracks, target.id, cutLeft, cutRight, () => scope.ctx.newId("track"));
|
|
1229
|
+
const { left, right } = split;
|
|
1230
|
+
scope.mediaTracks = split.mediaTracks;
|
|
1231
|
+
const idx = scope.clips.findIndex((c) => c.id === target.id);
|
|
1232
|
+
scope.clips.splice(idx, 1, left, right);
|
|
1233
|
+
state.selectedClipIds = state.selectedClipIds.filter((id) => id !== target.id);
|
|
1234
|
+
scope.touch(target.id, left.id, right.id);
|
|
1235
|
+
halves.push(left, right);
|
|
1236
|
+
}
|
|
1237
|
+
return { ok: true, clips: halves.map((half) => scope.clipOut(half)) };
|
|
1238
|
+
},
|
|
1239
|
+
trim_clip: async (scope, op) => {
|
|
1240
|
+
const clip = scope.resolveClip(op.target);
|
|
1241
|
+
const trimmed = scope.applyTrim(clip, {
|
|
1242
|
+
durationMs: op.durationMs,
|
|
1243
|
+
inPointMs: op.inPointMs,
|
|
1244
|
+
outPointMs: op.outPointMs
|
|
1245
|
+
}, await scope.sourceLimitMs(clip));
|
|
1246
|
+
scope.mediaTracks = resliceTracksForTrimmedClip(scope.mediaTracks, trimmed);
|
|
1247
|
+
return { ok: true, clip: scope.clipOut(trimmed) };
|
|
1248
|
+
},
|
|
1249
|
+
move_clip: (scope, op) => {
|
|
1250
|
+
const moved = scope.applyMove(scope.resolveClip(op.target), {
|
|
1251
|
+
startMs: op.startMs,
|
|
1252
|
+
trackId: op.trackId
|
|
1253
|
+
});
|
|
1254
|
+
return { ok: true, clip: scope.clipOut(moved) };
|
|
1255
|
+
},
|
|
1256
|
+
delete_clip: (scope, op) => {
|
|
1257
|
+
const state = scope.state;
|
|
1258
|
+
const clip = scope.resolveClip(op.target);
|
|
1259
|
+
scope.editTargets(clip);
|
|
1260
|
+
// Deleting a group deletes the parent, not the picture: its children stay
|
|
1261
|
+
// where they are and stop inheriting (D4). Leaving them with a `parentId`
|
|
1262
|
+
// nothing answers is what the validator calls a dangling parent.
|
|
1263
|
+
const wasChild = scope.clips
|
|
1264
|
+
.filter((c) => c.parentId === clip.id)
|
|
1265
|
+
.map((c) => c.id);
|
|
1266
|
+
const remaining = isGroupClip(clip)
|
|
1267
|
+
? ungroup(scope.clips, clip.id)
|
|
1268
|
+
: scope.clips;
|
|
1269
|
+
const out = scope.clipOut(clip);
|
|
1270
|
+
scope.clips = remaining.filter((c) => c.id !== clip.id);
|
|
1271
|
+
if (clip.linkId) {
|
|
1272
|
+
const remainingLinked = scope.clips.filter((candidate) => candidate.linkId === clip.linkId);
|
|
1273
|
+
if (remainingLinked.length === 1) {
|
|
1274
|
+
delete remainingLinked[0].linkId;
|
|
1275
|
+
scope.touch(remainingLinked[0].id);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
state.selectedClipIds = state.selectedClipIds.filter((id) => id !== clip.id);
|
|
1279
|
+
scope.touch(clip.id, ...wasChild);
|
|
1280
|
+
return { ok: true, deleted: out };
|
|
1281
|
+
},
|
|
1282
|
+
duplicate_clip: (scope, op) => {
|
|
1283
|
+
const src = scope.resolveClip(op.target);
|
|
1284
|
+
const offsetMs = src.durationMs + (op.gapMs ?? 0);
|
|
1285
|
+
if (!Number.isFinite(offsetMs) || src.startMs + offsetMs < 0) {
|
|
1286
|
+
throw new Error(`gapMs ${op.gapMs} would place the copy of "${src.name}" before zero.`);
|
|
1287
|
+
}
|
|
1288
|
+
// A group's copy carries copies of what it holds, at the same offset, so
|
|
1289
|
+
// the duplicate draws what the original draws.
|
|
1290
|
+
const descendants = isGroupClip(src)
|
|
1291
|
+
? groupDescendantIds(scope.clips, src.id)
|
|
1292
|
+
: new Set();
|
|
1293
|
+
const sources = [
|
|
1294
|
+
src,
|
|
1295
|
+
...scope.clips.filter((clip) => descendants.has(clip.id))
|
|
1296
|
+
];
|
|
1297
|
+
for (const clip of sources) {
|
|
1298
|
+
scope.assertWritable([], clip.trackId);
|
|
1299
|
+
}
|
|
1300
|
+
const freshIds = new Map(sources.map((clip) => [clip.id, scope.ctx.newId("clip")]));
|
|
1301
|
+
// A link whose members are all copied stays a link among the copies,
|
|
1302
|
+
// under a fresh id; a lone half is copied unlinked.
|
|
1303
|
+
const linkCounts = new Map();
|
|
1304
|
+
for (const clip of sources) {
|
|
1305
|
+
if (clip.linkId !== undefined && clip.id !== src.id) {
|
|
1306
|
+
linkCounts.set(clip.linkId, (linkCounts.get(clip.linkId) ?? 0) + 1);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
const freshLinks = new Map();
|
|
1310
|
+
const copies = sources.map((clip) => {
|
|
1311
|
+
const copy = structuredClone(clip);
|
|
1312
|
+
copy.id = freshIds.get(clip.id);
|
|
1313
|
+
copy.startMs += offsetMs;
|
|
1314
|
+
copy.locked = false;
|
|
1315
|
+
if (copy.parentId !== undefined && clip.id !== src.id) {
|
|
1316
|
+
copy.parentId = freshIds.get(copy.parentId) ?? copy.parentId;
|
|
1317
|
+
}
|
|
1318
|
+
const linkId = clip.linkId;
|
|
1319
|
+
if (linkId !== undefined &&
|
|
1320
|
+
clip.id !== src.id &&
|
|
1321
|
+
(linkCounts.get(linkId) ?? 0) >= 2) {
|
|
1322
|
+
let fresh = freshLinks.get(linkId);
|
|
1323
|
+
if (fresh === undefined) {
|
|
1324
|
+
fresh = scope.ctx.newId("link");
|
|
1325
|
+
freshLinks.set(linkId, fresh);
|
|
998
1326
|
}
|
|
1327
|
+
copy.linkId = fresh;
|
|
1328
|
+
}
|
|
1329
|
+
else {
|
|
1330
|
+
delete copy.linkId;
|
|
1331
|
+
}
|
|
1332
|
+
// A generated clip's copy is a variation to regenerate, so it starts
|
|
1333
|
+
// as a draft. Imported media keeps its asset and draws the same frames.
|
|
1334
|
+
if (clip.sourceType === "generated") {
|
|
1335
|
+
copy.status = "draft";
|
|
1336
|
+
copy.versions = [];
|
|
1337
|
+
delete copy.currentAssetId;
|
|
1338
|
+
delete copy.lastGeneratedHash;
|
|
1339
|
+
delete copy.activeTakeId;
|
|
999
1340
|
}
|
|
1000
|
-
state.selectedClipIds = state.selectedClipIds.filter((id) => id !== clip.id);
|
|
1001
|
-
scope.touch(clip.id, ...wasChild);
|
|
1002
|
-
return { ok: true, deleted: out };
|
|
1003
|
-
}
|
|
1004
|
-
case "duplicate_clip": {
|
|
1005
|
-
const src = scope.resolveClip(op.target);
|
|
1006
|
-
const copy = structuredClone(src);
|
|
1007
|
-
copy.id = scope.ctx.newId("clip");
|
|
1008
|
-
copy.startMs += src.durationMs + (op.gapMs ?? 0);
|
|
1009
|
-
copy.status = "draft";
|
|
1010
|
-
copy.locked = false;
|
|
1011
|
-
copy.versions = [];
|
|
1012
|
-
delete copy.currentAssetId;
|
|
1013
|
-
delete copy.lastGeneratedHash;
|
|
1014
|
-
delete copy.activeTakeId;
|
|
1015
|
-
delete copy.linkId;
|
|
1016
1341
|
copy.animations = copy.animations?.map((animation) => ({
|
|
1017
1342
|
...animation,
|
|
1018
1343
|
id: scope.ctx.newId("anim")
|
|
1019
1344
|
}));
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
if (
|
|
1055
|
-
|
|
1056
|
-
}
|
|
1057
|
-
if (patch.speedMultiplier !== undefined) {
|
|
1058
|
-
clip.speedMultiplier = patch.speedMultiplier;
|
|
1059
|
-
}
|
|
1060
|
-
if (patch.volumeDb !== undefined) {
|
|
1061
|
-
clip.volumeDb = patch.volumeDb;
|
|
1062
|
-
}
|
|
1063
|
-
if (patch.fadeInMs !== undefined) {
|
|
1064
|
-
clip.fadeInMs = patch.fadeInMs;
|
|
1065
|
-
}
|
|
1066
|
-
if (patch.fadeOutMs !== undefined) {
|
|
1067
|
-
clip.fadeOutMs = patch.fadeOutMs;
|
|
1068
|
-
}
|
|
1069
|
-
if (patch.fadeInShape !== undefined) {
|
|
1070
|
-
clip.fadeInShape = patch.fadeInShape;
|
|
1071
|
-
}
|
|
1072
|
-
if (patch.fadeOutShape !== undefined) {
|
|
1073
|
-
clip.fadeOutShape = patch.fadeOutShape;
|
|
1074
|
-
}
|
|
1075
|
-
if (patch.blendMode !== undefined) {
|
|
1076
|
-
clip.blendMode = patch.blendMode;
|
|
1077
|
-
}
|
|
1078
|
-
if (patch.borderRadius !== undefined) {
|
|
1079
|
-
clip.borderRadius = patch.borderRadius;
|
|
1080
|
-
}
|
|
1081
|
-
if (patch.crop !== undefined) {
|
|
1082
|
-
// Null is how a caller puts the whole source back; leaving the field
|
|
1083
|
-
// with all-zero insets would store a crop that means nothing.
|
|
1084
|
-
if (patch.crop === null) {
|
|
1085
|
-
delete clip.crop;
|
|
1086
|
-
}
|
|
1087
|
-
else {
|
|
1088
|
-
if (!isCropUsable(patch.crop)) {
|
|
1089
|
-
throw new Error(`Crop on clip "${clip.name}" keeps no picture: left + right and ` +
|
|
1090
|
-
"top + bottom must each stay below 1.");
|
|
1091
|
-
}
|
|
1092
|
-
clip.crop = patch.crop;
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
if (patch.hidden !== undefined) {
|
|
1096
|
-
clip.hidden = patch.hidden;
|
|
1097
|
-
}
|
|
1098
|
-
if (patch.muted !== undefined) {
|
|
1099
|
-
clip.muted = patch.muted;
|
|
1100
|
-
}
|
|
1101
|
-
if (patch.locked !== undefined) {
|
|
1102
|
-
clip.locked = patch.locked;
|
|
1103
|
-
}
|
|
1104
|
-
if (patch.textStyle !== undefined) {
|
|
1105
|
-
const parsed = textStyleParams.safeParse({
|
|
1106
|
-
color: DEFAULT_TEXT_CLIP_COLOR,
|
|
1107
|
-
...clip.textStyle,
|
|
1108
|
-
...patch.textStyle
|
|
1109
|
-
});
|
|
1110
|
-
if (!parsed.success) {
|
|
1111
|
-
throw new Error(`Clip "${clip.name}" has no text style yet, so this patch still needs ${parsed.error.issues.map((issue) => issue.path.join(".")).join(", ")}.`);
|
|
1112
|
-
}
|
|
1113
|
-
assertAuthorableFontFamily(parsed.data.fontFamily);
|
|
1114
|
-
clip.textStyle = parsed.data;
|
|
1115
|
-
}
|
|
1116
|
-
if (patch.shapeStyle !== undefined) {
|
|
1117
|
-
clip.shapeStyle = shapeStyleWithDefaults(patch.shapeStyle);
|
|
1118
|
-
}
|
|
1119
|
-
if (patch.captionStyle !== undefined) {
|
|
1120
|
-
// The style rides on the clip's caption, so a clip with no words to
|
|
1121
|
-
// draw has nowhere to put it. Say so rather than storing a look nothing
|
|
1122
|
-
// renders.
|
|
1123
|
-
if (!clip.caption) {
|
|
1124
|
-
throw new Error(`Clip "${clip.name}" carries no caption to style.`);
|
|
1125
|
-
}
|
|
1126
|
-
clip.caption = { ...clip.caption, style: patch.captionStyle };
|
|
1345
|
+
return copy;
|
|
1346
|
+
});
|
|
1347
|
+
scope.clips.push(...copies);
|
|
1348
|
+
scope.touch(...copies.map((copy) => copy.id));
|
|
1349
|
+
const result = {
|
|
1350
|
+
ok: true,
|
|
1351
|
+
clip: scope.clipOut(copies[0])
|
|
1352
|
+
};
|
|
1353
|
+
if (copies.length > 1) {
|
|
1354
|
+
result.children = copies.slice(1).map((copy) => copy.id);
|
|
1355
|
+
}
|
|
1356
|
+
return result;
|
|
1357
|
+
},
|
|
1358
|
+
set_clip_params: async (scope, op) => {
|
|
1359
|
+
let clip = scope.resolveClip(op.target);
|
|
1360
|
+
const patch = { ...op.patch };
|
|
1361
|
+
rejectUnknownClipParams(patch);
|
|
1362
|
+
// Timing belongs to move_clip and trim_clip, but a caller sending it here
|
|
1363
|
+
// means one edit either way — so apply it through the same code rather
|
|
1364
|
+
// than dropping it or making them call twice.
|
|
1365
|
+
clip = scope.applyTrim(clip, {
|
|
1366
|
+
durationMs: patch.durationMs,
|
|
1367
|
+
inPointMs: patch.inPointMs,
|
|
1368
|
+
outPointMs: patch.outPointMs
|
|
1369
|
+
}, patch.durationMs === undefined && patch.outPointMs === undefined
|
|
1370
|
+
? undefined
|
|
1371
|
+
: await scope.sourceLimitMs(clip));
|
|
1372
|
+
clip = scope.applyMove(clip, {
|
|
1373
|
+
startMs: patch.startMs,
|
|
1374
|
+
trackId: patch.trackId
|
|
1375
|
+
});
|
|
1376
|
+
if (patch.fontSizePx !== undefined) {
|
|
1377
|
+
// Shorthand for the one text field callers reach for by name.
|
|
1378
|
+
const style = patch.textStyle ?? clip.textStyle;
|
|
1379
|
+
if (!style) {
|
|
1380
|
+
throw new Error(`Clip "${clip.name}" carries no text to size; fontSizePx applies to a text clip's textStyle.`);
|
|
1127
1381
|
}
|
|
1128
|
-
|
|
1129
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1382
|
+
patch.textStyle = { ...style, fontSizePx: patch.fontSizePx };
|
|
1130
1383
|
}
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
scope.touch(clip.id);
|
|
1134
|
-
if (op.parentId === null) {
|
|
1135
|
-
delete clip.parentId;
|
|
1136
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1137
|
-
}
|
|
1138
|
-
const parent = scope.resolveClip(op.parentId);
|
|
1139
|
-
if (parent.mediaType !== "group") {
|
|
1140
|
-
throw new Error(`"${parent.name}" is a ${parent.mediaType} clip, not a group — parent to a clip created with add_group. ${scope.validUnits(scope.clips.filter((c) => c.mediaType === "group"), "group")}`);
|
|
1141
|
-
}
|
|
1142
|
-
// A cycle renders unparented and warns, so refusing it here is the only
|
|
1143
|
-
// place it can still be fixed.
|
|
1144
|
-
let cursor = parent;
|
|
1145
|
-
while (cursor) {
|
|
1146
|
-
if (cursor.id === clip.id) {
|
|
1147
|
-
throw new Error(`"${parent.name}" is inside "${clip.name}" — parenting them would make a cycle.`);
|
|
1148
|
-
}
|
|
1149
|
-
const next = cursor.parentId;
|
|
1150
|
-
cursor = next ? scope.clips.find((c) => c.id === next) : undefined;
|
|
1151
|
-
}
|
|
1152
|
-
clip.parentId = parent.id;
|
|
1153
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1384
|
+
if (patch.name !== undefined) {
|
|
1385
|
+
clip.name = patch.name;
|
|
1154
1386
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
if (op.transition === null) {
|
|
1158
|
-
delete clip.transitionIn;
|
|
1159
|
-
}
|
|
1160
|
-
else {
|
|
1161
|
-
clip.transitionIn = buildTransition(op.transition);
|
|
1162
|
-
}
|
|
1163
|
-
scope.touch(clip.id);
|
|
1164
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1387
|
+
if (patch.opacity !== undefined) {
|
|
1388
|
+
clip.opacity = patch.opacity;
|
|
1165
1389
|
}
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1390
|
+
if (patch.transform !== undefined) {
|
|
1391
|
+
clip.transform = mergeClipTransform(clip.transform, patch.transform);
|
|
1392
|
+
}
|
|
1393
|
+
if (patch.speedMultiplier !== undefined) {
|
|
1394
|
+
// The editor's rule: the source window stays, the duration scales, and
|
|
1395
|
+
// linked partners change speed with the clip.
|
|
1396
|
+
const unit = scope.editTargets(clip);
|
|
1397
|
+
scope.clips = retimeClipSpeed(scope.clips, new Set(unit.map((member) => member.id)), patch.speedMultiplier);
|
|
1398
|
+
for (const member of unit) {
|
|
1399
|
+
scope.touch(member.id);
|
|
1174
1400
|
}
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
candidate: planned.candidate,
|
|
1180
|
-
operation: planned.candidate.operation,
|
|
1181
|
-
description: applied.description
|
|
1182
|
-
};
|
|
1401
|
+
clip = scope.resolveClip(clip.id);
|
|
1402
|
+
}
|
|
1403
|
+
if (patch.volumeDb !== undefined) {
|
|
1404
|
+
clip.volumeDb = patch.volumeDb;
|
|
1183
1405
|
}
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1406
|
+
if (patch.fadeInMs !== undefined) {
|
|
1407
|
+
clip.fadeInMs = patch.fadeInMs;
|
|
1408
|
+
}
|
|
1409
|
+
if (patch.fadeOutMs !== undefined) {
|
|
1410
|
+
clip.fadeOutMs = patch.fadeOutMs;
|
|
1411
|
+
}
|
|
1412
|
+
if (patch.fadeInShape !== undefined) {
|
|
1413
|
+
clip.fadeInShape = patch.fadeInShape;
|
|
1414
|
+
}
|
|
1415
|
+
if (patch.fadeOutShape !== undefined) {
|
|
1416
|
+
clip.fadeOutShape = patch.fadeOutShape;
|
|
1417
|
+
}
|
|
1418
|
+
if (patch.audioEffects !== undefined) {
|
|
1419
|
+
clip.audioEffects = patch.audioEffects;
|
|
1420
|
+
}
|
|
1421
|
+
if (patch.blendMode !== undefined) {
|
|
1422
|
+
clip.blendMode = patch.blendMode;
|
|
1423
|
+
}
|
|
1424
|
+
if (patch.borderRadius !== undefined) {
|
|
1425
|
+
clip.borderRadius = patch.borderRadius;
|
|
1426
|
+
}
|
|
1427
|
+
if (patch.crop !== undefined) {
|
|
1428
|
+
// Null is how a caller puts the whole source back; leaving the field
|
|
1429
|
+
// with all-zero insets would store a crop that means nothing.
|
|
1430
|
+
if (patch.crop === null) {
|
|
1431
|
+
delete clip.crop;
|
|
1188
1432
|
}
|
|
1189
1433
|
else {
|
|
1190
|
-
if (
|
|
1191
|
-
throw new Error("
|
|
1434
|
+
if (!isCropUsable(patch.crop)) {
|
|
1435
|
+
throw new Error(`Crop on clip "${clip.name}" keeps no picture: left + right and ` +
|
|
1436
|
+
"top + bottom must each stay below 1.");
|
|
1192
1437
|
}
|
|
1193
|
-
clip.
|
|
1438
|
+
clip.crop = patch.crop;
|
|
1194
1439
|
}
|
|
1195
|
-
scope.touch(clip.id);
|
|
1196
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1197
1440
|
}
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
scope.touch(clip.id);
|
|
1201
|
-
if (op.matte === null) {
|
|
1202
|
-
delete clip.matte;
|
|
1203
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1204
|
-
}
|
|
1205
|
-
const source = scope.resolveClip(op.matte.source);
|
|
1206
|
-
if (source.id === clip.id) {
|
|
1207
|
-
throw new Error(`"${clip.name}" cannot be its own matte source — name another clip.`);
|
|
1208
|
-
}
|
|
1209
|
-
const matteOut = {
|
|
1210
|
-
sourceClipId: source.id,
|
|
1211
|
-
mode: op.matte.mode
|
|
1212
|
-
};
|
|
1213
|
-
if (op.matte.invert !== undefined) {
|
|
1214
|
-
matteOut.invert = op.matte.invert;
|
|
1215
|
-
}
|
|
1216
|
-
clip.matte = matteOut;
|
|
1217
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1441
|
+
if (patch.hidden !== undefined) {
|
|
1442
|
+
clip.hidden = patch.hidden;
|
|
1218
1443
|
}
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
scope.touch(clip.id);
|
|
1222
|
-
if (op.clear === true) {
|
|
1223
|
-
delete clip.generatedMatte;
|
|
1224
|
-
return { ok: true, clip: scope.clipOut(clip), generatedMatte: null };
|
|
1225
|
-
}
|
|
1226
|
-
if (!clip.generatedMatte) {
|
|
1227
|
-
throw new Error(`"${clip.name}" carries no generated matte. Run isolate_subject on ` +
|
|
1228
|
-
"it first; this op only adjusts one that exists.");
|
|
1229
|
-
}
|
|
1230
|
-
if (op.selectVersionAssetId !== undefined) {
|
|
1231
|
-
clip.generatedMatte = selectGeneratedMatteVersion(clip, op.selectVersionAssetId).generatedMatte;
|
|
1232
|
-
}
|
|
1233
|
-
// The knobs are the user's, so an absent field leaves the stored value
|
|
1234
|
-
// alone rather than resetting it to a default the caller did not name.
|
|
1235
|
-
const matte = clip.generatedMatte;
|
|
1236
|
-
if (op.invert !== undefined) {
|
|
1237
|
-
matte.invert = op.invert;
|
|
1238
|
-
}
|
|
1239
|
-
if (op.strength !== undefined) {
|
|
1240
|
-
matte.strength = Math.min(1, Math.max(0, op.strength));
|
|
1241
|
-
}
|
|
1242
|
-
if (op.featherPx !== undefined) {
|
|
1243
|
-
matte.featherPx = Math.max(0, op.featherPx);
|
|
1244
|
-
}
|
|
1245
|
-
return { ok: true, clip: scope.clipOut(clip), generatedMatte: matte };
|
|
1444
|
+
if (patch.muted !== undefined) {
|
|
1445
|
+
clip.muted = patch.muted;
|
|
1246
1446
|
}
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
if (op.timeRemap === null) {
|
|
1250
|
-
delete clip.timeRemap;
|
|
1251
|
-
}
|
|
1252
|
-
else {
|
|
1253
|
-
clip.timeRemap = buildTimeRemap(op.timeRemap);
|
|
1254
|
-
}
|
|
1255
|
-
scope.touch(clip.id);
|
|
1256
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1447
|
+
if (patch.locked !== undefined) {
|
|
1448
|
+
clip.locked = patch.locked;
|
|
1257
1449
|
}
|
|
1258
|
-
|
|
1259
|
-
const
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
}
|
|
1264
|
-
|
|
1265
|
-
clip.
|
|
1266
|
-
}
|
|
1267
|
-
|
|
1450
|
+
if (patch.textStyle !== undefined) {
|
|
1451
|
+
const parsed = textStyleParams.safeParse({
|
|
1452
|
+
color: DEFAULT_TEXT_CLIP_COLOR,
|
|
1453
|
+
...clip.textStyle,
|
|
1454
|
+
...patch.textStyle
|
|
1455
|
+
});
|
|
1456
|
+
if (!parsed.success) {
|
|
1457
|
+
throw new Error(`Clip "${clip.name}" has no text style yet, so this patch still needs ${parsed.error.issues.map((issue) => issue.path.join(".")).join(", ")}.`);
|
|
1458
|
+
}
|
|
1459
|
+
assertAuthorableFontFamily(parsed.data.fontFamily);
|
|
1460
|
+
clip.textStyle = parsed.data;
|
|
1461
|
+
}
|
|
1462
|
+
if (patch.shapeStyle !== undefined) {
|
|
1463
|
+
clip.shapeStyle = shapeStyleWithDefaults(patch.shapeStyle);
|
|
1464
|
+
}
|
|
1465
|
+
if (patch.captionStyle !== undefined) {
|
|
1466
|
+
// The style rides on the clip's caption, so a clip with no words to
|
|
1467
|
+
// draw has nowhere to put it. Say so rather than storing a look nothing
|
|
1468
|
+
// renders.
|
|
1469
|
+
if (!clip.caption) {
|
|
1470
|
+
throw new Error(`Clip "${clip.name}" carries no caption to style.`);
|
|
1471
|
+
}
|
|
1472
|
+
clip.caption = { ...clip.caption, style: patch.captionStyle };
|
|
1473
|
+
}
|
|
1474
|
+
scope.touch(clip.id);
|
|
1475
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1476
|
+
},
|
|
1477
|
+
set_parent: (scope, op) => {
|
|
1478
|
+
const clip = scope.resolveClip(op.target);
|
|
1479
|
+
scope.assertWritable([clip]);
|
|
1480
|
+
scope.touch(clip.id);
|
|
1481
|
+
if (op.parentId === null) {
|
|
1482
|
+
delete clip.parentId;
|
|
1268
1483
|
return { ok: true, clip: scope.clipOut(clip) };
|
|
1269
1484
|
}
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
if (
|
|
1279
|
-
clip.
|
|
1280
|
-
}
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1485
|
+
const parent = scope.resolveClip(op.parentId);
|
|
1486
|
+
if (parent.mediaType !== "group") {
|
|
1487
|
+
throw new Error(`"${parent.name}" is a ${parent.mediaType} clip, not a group — parent to a clip created with add_group. ${scope.validUnits(scope.clips.filter((c) => c.mediaType === "group"), "group")}`);
|
|
1488
|
+
}
|
|
1489
|
+
// A cycle renders unparented and warns, so refusing it here is the only
|
|
1490
|
+
// place it can still be fixed.
|
|
1491
|
+
let cursor = parent;
|
|
1492
|
+
while (cursor) {
|
|
1493
|
+
if (cursor.id === clip.id) {
|
|
1494
|
+
throw new Error(`"${parent.name}" is inside "${clip.name}" — parenting them would make a cycle.`);
|
|
1495
|
+
}
|
|
1496
|
+
const next = cursor.parentId;
|
|
1497
|
+
cursor = next ? scope.clips.find((c) => c.id === next) : undefined;
|
|
1498
|
+
}
|
|
1499
|
+
clip.parentId = parent.id;
|
|
1500
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1501
|
+
},
|
|
1502
|
+
set_transition: (scope, op) => {
|
|
1503
|
+
const clip = scope.resolveClip(op.target);
|
|
1504
|
+
scope.assertWritable([clip]);
|
|
1505
|
+
if (op.transition === null) {
|
|
1506
|
+
delete clip.transitionIn;
|
|
1507
|
+
}
|
|
1508
|
+
else {
|
|
1509
|
+
clip.transitionIn = buildTransition(op.transition);
|
|
1510
|
+
}
|
|
1511
|
+
scope.touch(clip.id);
|
|
1512
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1513
|
+
},
|
|
1514
|
+
apply_transition_at_cut: async (scope, op) => {
|
|
1515
|
+
const outgoing = scope.resolveClip(op.outgoingClipId);
|
|
1516
|
+
const incoming = scope.resolveClip(op.incomingClipId);
|
|
1517
|
+
const planned = planTransitionAtCut(scope.clips, {
|
|
1518
|
+
...op,
|
|
1519
|
+
outgoingClipId: outgoing.id,
|
|
1520
|
+
incomingClipId: incoming.id
|
|
1521
|
+
});
|
|
1522
|
+
if (!planned.ok) {
|
|
1523
|
+
throw new Error(planned.error);
|
|
1524
|
+
}
|
|
1525
|
+
scope.assertWritable([incoming]);
|
|
1526
|
+
// The outgoing clip grows under the transition with the partners that
|
|
1527
|
+
// end with it, so each is a write the lock refuses. Like the editor, it
|
|
1528
|
+
// is capped at a source length the host knows and free otherwise.
|
|
1529
|
+
const growth = transitionGrowthMs(outgoing, incoming, transitionLengthAtCut(outgoing, incoming, planned.candidate.durationMs));
|
|
1530
|
+
const unit = growth > 0 ? transitionGrowthUnit(scope.clips, outgoing) : [];
|
|
1531
|
+
scope.assertWritable(unit);
|
|
1532
|
+
const limits = new Map();
|
|
1533
|
+
for (const member of unit) {
|
|
1534
|
+
limits.set(member.id, await scope.knownSourceMs(member));
|
|
1535
|
+
}
|
|
1536
|
+
const applied = applyTransitionAtCutCandidate(scope.clips, planned.candidate, { sourceDurationMs: (clip) => limits.get(clip.id) });
|
|
1537
|
+
if (!applied.ok) {
|
|
1538
|
+
throw new Error(applied.error);
|
|
1539
|
+
}
|
|
1540
|
+
const before = new Map(scope.clips.map((clip) => [clip.id, clip]));
|
|
1541
|
+
scope.clips = applied.clips;
|
|
1542
|
+
for (const clip of scope.clips) {
|
|
1543
|
+
if (before.get(clip.id) !== clip) {
|
|
1544
|
+
scope.mediaTracks = resliceTracksForTrimmedClip(scope.mediaTracks, clip);
|
|
1545
|
+
scope.touch(clip.id);
|
|
1320
1546
|
}
|
|
1321
|
-
scope.touch(clip.id);
|
|
1322
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1323
1547
|
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
? typewriterTiming(clip.textStyle?.text ?? "", clip.durationMs - (input.delayMs ?? 0), input.durationMs, input.stagger)
|
|
1351
|
-
: null;
|
|
1352
|
-
built.push({
|
|
1353
|
-
id: scope.ctx.newId("anim"),
|
|
1354
|
-
role: input.role,
|
|
1355
|
-
enabled: input.enabled,
|
|
1356
|
-
preset: input.preset,
|
|
1357
|
-
durationMs: timing?.durationMs ?? input.durationMs ?? preset.defaultDurationMs,
|
|
1358
|
-
delayMs: input.delayMs,
|
|
1359
|
-
easing: input.easing,
|
|
1360
|
-
params: input.params,
|
|
1361
|
-
stagger: timing?.stagger ?? input.stagger,
|
|
1362
|
-
caret: input.preset === "typewriter" ? input.caret : undefined
|
|
1363
|
-
});
|
|
1364
|
-
}
|
|
1365
|
-
clip.animations =
|
|
1366
|
-
op.mode === "add" ? [...(clip.animations ?? []), ...built] : built;
|
|
1367
|
-
scope.touch(clip.id);
|
|
1548
|
+
return {
|
|
1549
|
+
ok: true,
|
|
1550
|
+
candidate: planned.candidate,
|
|
1551
|
+
operation: planned.candidate.operation,
|
|
1552
|
+
description: applied.description
|
|
1553
|
+
};
|
|
1554
|
+
},
|
|
1555
|
+
set_mask: (scope, op) => {
|
|
1556
|
+
const clip = scope.resolveClip(op.target);
|
|
1557
|
+
if (op.mask === null) {
|
|
1558
|
+
delete clip.mask;
|
|
1559
|
+
}
|
|
1560
|
+
else {
|
|
1561
|
+
if (op.mask.kind === "path" && !op.mask.d?.trim()) {
|
|
1562
|
+
throw new Error("A path mask needs `d` with SVG path data.");
|
|
1563
|
+
}
|
|
1564
|
+
clip.mask = buildMask(op.mask, scope.ctx.parseSvgPath);
|
|
1565
|
+
}
|
|
1566
|
+
scope.touch(clip.id);
|
|
1567
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1568
|
+
},
|
|
1569
|
+
set_matte: (scope, op) => {
|
|
1570
|
+
const clip = scope.resolveClip(op.target);
|
|
1571
|
+
scope.touch(clip.id);
|
|
1572
|
+
if (op.matte === null) {
|
|
1573
|
+
delete clip.matte;
|
|
1368
1574
|
return { ok: true, clip: scope.clipOut(clip) };
|
|
1369
1575
|
}
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
clip.animations = outcome.animations;
|
|
1374
|
-
scope.touch(clip.id);
|
|
1375
|
-
return {
|
|
1376
|
-
ok: true,
|
|
1377
|
-
clip: scope.clipOut(clip),
|
|
1378
|
-
animationId: outcome.animationId,
|
|
1379
|
-
keyframeCount: outcome.animation.custom?.curves[0]?.keyframes.length ?? 0,
|
|
1380
|
-
replaced: outcome.replaced
|
|
1381
|
-
};
|
|
1576
|
+
const source = scope.resolveClip(op.matte.source);
|
|
1577
|
+
if (source.id === clip.id) {
|
|
1578
|
+
throw new Error(`"${clip.name}" cannot be its own matte source — name another clip.`);
|
|
1382
1579
|
}
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1580
|
+
const matteOut = {
|
|
1581
|
+
sourceClipId: source.id,
|
|
1582
|
+
mode: op.matte.mode
|
|
1583
|
+
};
|
|
1584
|
+
if (op.matte.invert !== undefined) {
|
|
1585
|
+
matteOut.invert = op.matte.invert;
|
|
1586
|
+
}
|
|
1587
|
+
clip.matte = matteOut;
|
|
1588
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1589
|
+
},
|
|
1590
|
+
set_generated_matte: (scope, op) => {
|
|
1591
|
+
const clip = scope.resolveClip(op.target);
|
|
1592
|
+
scope.touch(clip.id);
|
|
1593
|
+
if (op.clear === true) {
|
|
1594
|
+
delete clip.generatedMatte;
|
|
1595
|
+
return { ok: true, clip: scope.clipOut(clip), generatedMatte: null };
|
|
1596
|
+
}
|
|
1597
|
+
if (!clip.generatedMatte) {
|
|
1598
|
+
throw new Error(`"${clip.name}" carries no generated matte. Run isolate_subject on ` +
|
|
1599
|
+
"it first; this op only adjusts one that exists.");
|
|
1600
|
+
}
|
|
1601
|
+
if (op.selectVersionAssetId !== undefined) {
|
|
1602
|
+
clip.generatedMatte = selectGeneratedMatteVersion(clip, op.selectVersionAssetId).generatedMatte;
|
|
1603
|
+
}
|
|
1604
|
+
// The knobs are the user's, so an absent field leaves the stored value
|
|
1605
|
+
// alone rather than resetting it to a default the caller did not name.
|
|
1606
|
+
const matte = clip.generatedMatte;
|
|
1607
|
+
if (op.invert !== undefined) {
|
|
1608
|
+
matte.invert = op.invert;
|
|
1609
|
+
}
|
|
1610
|
+
if (op.strength !== undefined) {
|
|
1611
|
+
matte.strength = Math.min(1, Math.max(0, op.strength));
|
|
1612
|
+
}
|
|
1613
|
+
if (op.featherPx !== undefined) {
|
|
1614
|
+
matte.featherPx = Math.max(0, op.featherPx);
|
|
1615
|
+
}
|
|
1616
|
+
return { ok: true, clip: scope.clipOut(clip), generatedMatte: matte };
|
|
1617
|
+
},
|
|
1618
|
+
set_time_remap: (scope, op) => {
|
|
1619
|
+
const clip = scope.resolveClip(op.target);
|
|
1620
|
+
scope.assertWritable([clip]);
|
|
1621
|
+
if (op.timeRemap === null) {
|
|
1622
|
+
delete clip.timeRemap;
|
|
1623
|
+
}
|
|
1624
|
+
else {
|
|
1625
|
+
clip.timeRemap = buildTimeRemap(op.timeRemap);
|
|
1626
|
+
}
|
|
1627
|
+
scope.touch(clip.id);
|
|
1628
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1629
|
+
},
|
|
1630
|
+
set_effects: (scope, op) => {
|
|
1631
|
+
const clip = scope.resolveClip(op.target);
|
|
1632
|
+
const list = op.effects.map(buildEffect);
|
|
1633
|
+
if (list.length === 0) {
|
|
1634
|
+
delete clip.effects;
|
|
1635
|
+
}
|
|
1636
|
+
else {
|
|
1637
|
+
clip.effects = list;
|
|
1638
|
+
}
|
|
1639
|
+
scope.touch(clip.id);
|
|
1640
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1641
|
+
},
|
|
1642
|
+
set_clip_binding: (scope, op) => {
|
|
1643
|
+
const clip = scope.resolveClip(op.target);
|
|
1644
|
+
if (clip.sourceType !== "generated") {
|
|
1645
|
+
throw new Error(`"${clip.name}" is not a generated clip — ui_timeline_set_clip_binding only applies to clips created with ui_timeline_generate_clip.`);
|
|
1646
|
+
}
|
|
1647
|
+
if (op.prompt !== undefined) {
|
|
1648
|
+
clip.prompt = op.prompt;
|
|
1649
|
+
}
|
|
1650
|
+
if (op.negativePrompt !== undefined) {
|
|
1651
|
+
clip.negativePrompt = op.negativePrompt;
|
|
1652
|
+
}
|
|
1653
|
+
if (op.provider !== undefined) {
|
|
1654
|
+
clip.provider = op.provider;
|
|
1655
|
+
}
|
|
1656
|
+
if (op.model !== undefined) {
|
|
1657
|
+
clip.model = op.model;
|
|
1658
|
+
}
|
|
1659
|
+
if (op.voice !== undefined) {
|
|
1660
|
+
clip.voice = op.voice;
|
|
1661
|
+
}
|
|
1662
|
+
if (op.width !== undefined) {
|
|
1663
|
+
clip.width = op.width;
|
|
1664
|
+
}
|
|
1665
|
+
if (op.height !== undefined) {
|
|
1666
|
+
clip.height = op.height;
|
|
1667
|
+
}
|
|
1668
|
+
if (op.aspectRatio !== undefined) {
|
|
1669
|
+
clip.aspectRatio = op.aspectRatio;
|
|
1670
|
+
}
|
|
1671
|
+
if (op.resolution !== undefined) {
|
|
1672
|
+
clip.resolution = op.resolution;
|
|
1673
|
+
}
|
|
1674
|
+
if (op.strength !== undefined) {
|
|
1675
|
+
clip.strength = op.strength;
|
|
1676
|
+
}
|
|
1677
|
+
if (op.numInferenceSteps !== undefined) {
|
|
1678
|
+
clip.numInferenceSteps = op.numInferenceSteps;
|
|
1679
|
+
}
|
|
1680
|
+
if (op.seed !== undefined) {
|
|
1681
|
+
clip.seed = op.seed;
|
|
1682
|
+
}
|
|
1683
|
+
const bindingChanged = Object.keys(op).some((key) => key !== "op" &&
|
|
1684
|
+
key !== "target" &&
|
|
1685
|
+
key !== "regenerate" &&
|
|
1686
|
+
op[key] !== undefined);
|
|
1687
|
+
if (bindingChanged && (clip.lastGeneratedHash || clip.currentAssetId)) {
|
|
1688
|
+
clip.status = "stale";
|
|
1689
|
+
}
|
|
1690
|
+
scope.touch(clip.id);
|
|
1691
|
+
// No op starts a generation job: the browser runs one itself after this
|
|
1692
|
+
// edit lands. Marking the clip queued here would promise a job that no
|
|
1693
|
+
// runner on this surface will ever start.
|
|
1694
|
+
if (op.regenerate) {
|
|
1400
1695
|
return {
|
|
1401
1696
|
ok: true,
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1697
|
+
clip: scope.clipOut(clip),
|
|
1698
|
+
regenerationStarted: false,
|
|
1699
|
+
note: "The binding was saved, but this surface cannot run generation jobs. Regenerate the clip from the timeline editor."
|
|
1405
1700
|
};
|
|
1406
1701
|
}
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1702
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1703
|
+
},
|
|
1704
|
+
animate_clip: async (scope, op) => {
|
|
1705
|
+
const clip = scope.resolveClip(op.target);
|
|
1706
|
+
if (clip.mediaType !== "text" &&
|
|
1707
|
+
op.animations.some((input) => input.stagger !== undefined)) {
|
|
1708
|
+
throw new Error(`Stagger applies only to text clips; "${clip.name}" is ${clip.mediaType}.`);
|
|
1709
|
+
}
|
|
1710
|
+
const built = [];
|
|
1711
|
+
for (const input of op.animations) {
|
|
1712
|
+
if (input.preset === CUSTOM_ANIMATION_PRESET_ID) {
|
|
1713
|
+
// `{preset: "custom", custom: {curves}}` reads as naturally as the
|
|
1714
|
+
// flat form, so lift it rather than refusing it.
|
|
1715
|
+
built.push(await buildCustomAnimation(scope, clip, liftCustom(input)));
|
|
1716
|
+
continue;
|
|
1717
|
+
}
|
|
1718
|
+
const preset = ANIMATION_PRESETS.find((p) => p.id === input.preset);
|
|
1719
|
+
if (!preset) {
|
|
1720
|
+
const ids = ANIMATION_PRESETS.map((p) => p.id).join(", ");
|
|
1721
|
+
throw new Error(`Unknown animation preset "${input.preset}". Valid presets: ${ids}, ${CUSTOM_ANIMATION_PRESET_ID}.`);
|
|
1722
|
+
}
|
|
1723
|
+
if (!preset.roles.includes(input.role)) {
|
|
1724
|
+
throw new Error(`Preset "${input.preset}" does not support role "${input.role}". Valid roles for "${input.preset}": ${preset.roles.join(", ")}.`);
|
|
1725
|
+
}
|
|
1726
|
+
if (input.preset === "typewriter" && clip.mediaType !== "text") {
|
|
1727
|
+
throw new Error('Preset "typewriter" requires a text clip.');
|
|
1728
|
+
}
|
|
1729
|
+
const timing = input.preset === "typewriter"
|
|
1730
|
+
? typewriterTiming(clip.textStyle?.text ?? "", clip.durationMs - (input.delayMs ?? 0), input.durationMs, input.stagger)
|
|
1731
|
+
: null;
|
|
1732
|
+
built.push({
|
|
1733
|
+
id: scope.ctx.newId("anim"),
|
|
1734
|
+
role: input.role,
|
|
1735
|
+
enabled: input.enabled,
|
|
1736
|
+
preset: input.preset,
|
|
1737
|
+
durationMs: timing?.durationMs ?? input.durationMs ?? preset.defaultDurationMs,
|
|
1738
|
+
delayMs: input.delayMs,
|
|
1739
|
+
easing: input.easing,
|
|
1740
|
+
params: input.params,
|
|
1741
|
+
stagger: timing?.stagger ?? input.stagger,
|
|
1742
|
+
caret: input.preset === "typewriter" ? input.caret : undefined
|
|
1743
|
+
});
|
|
1419
1744
|
}
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1745
|
+
clip.animations =
|
|
1746
|
+
op.mode === "add" ? [...(clip.animations ?? []), ...built] : built;
|
|
1747
|
+
scope.touch(clip.id);
|
|
1748
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1749
|
+
},
|
|
1750
|
+
set_baked_animation: (scope, op) => {
|
|
1751
|
+
const clip = scope.resolveClip(op.target);
|
|
1752
|
+
const outcome = buildBakedAnimation(clip, op.animation, () => scope.ctx.newId("anim"));
|
|
1753
|
+
clip.animations = outcome.animations;
|
|
1754
|
+
scope.touch(clip.id);
|
|
1755
|
+
return {
|
|
1756
|
+
ok: true,
|
|
1757
|
+
clip: scope.clipOut(clip),
|
|
1758
|
+
animationId: outcome.animationId,
|
|
1759
|
+
keyframeCount: outcome.animation.custom?.curves[0]?.keyframes.length ?? 0,
|
|
1760
|
+
replaced: outcome.replaced
|
|
1761
|
+
};
|
|
1762
|
+
},
|
|
1763
|
+
clear_animations: (scope, op) => {
|
|
1764
|
+
const clip = scope.resolveClip(op.target);
|
|
1765
|
+
clip.animations = op.role
|
|
1766
|
+
? (clip.animations ?? []).filter((a) => a.role !== op.role)
|
|
1767
|
+
: [];
|
|
1768
|
+
scope.touch(clip.id);
|
|
1769
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
1770
|
+
},
|
|
1771
|
+
list_animation_presets: (_scope) => {
|
|
1772
|
+
const presets = ANIMATION_PRESETS.map((p) => ({
|
|
1773
|
+
id: p.id,
|
|
1774
|
+
roles: p.roles,
|
|
1775
|
+
defaultDurationMs: p.defaultDurationMs,
|
|
1776
|
+
defaultEasing: p.defaultEasing,
|
|
1777
|
+
params: p.params,
|
|
1778
|
+
describe: p.describe
|
|
1779
|
+
}));
|
|
1780
|
+
return {
|
|
1781
|
+
ok: true,
|
|
1782
|
+
presets,
|
|
1783
|
+
custom: CUSTOM_ANIMATION_CONTRACT,
|
|
1784
|
+
properties: CUSTOM_ANIMATION_CONTRACT.properties
|
|
1785
|
+
};
|
|
1786
|
+
},
|
|
1787
|
+
select_clip: (scope, op) => {
|
|
1788
|
+
const state = scope.state;
|
|
1789
|
+
if (!op.target) {
|
|
1790
|
+
state.selectedClipIds = [];
|
|
1791
|
+
return { ok: true, selected: null };
|
|
1792
|
+
}
|
|
1793
|
+
const clip = scope.resolveClip(op.target);
|
|
1794
|
+
state.selectedClipIds = [clip.id];
|
|
1795
|
+
return { ok: true, selected: scope.clipOut(clip) };
|
|
1796
|
+
},
|
|
1797
|
+
seek: (scope, op) => {
|
|
1798
|
+
const state = scope.state;
|
|
1799
|
+
state.playheadMs = Math.max(0, op.timeMs);
|
|
1800
|
+
return { ok: true, playheadMs: state.playheadMs };
|
|
1801
|
+
},
|
|
1802
|
+
add_marker: (scope, op) => {
|
|
1803
|
+
const state = scope.state;
|
|
1804
|
+
if (op.timeMs < 0) {
|
|
1805
|
+
throw new Error(`A marker cannot sit before zero; got ${op.timeMs}ms.`);
|
|
1806
|
+
}
|
|
1807
|
+
const marker = {
|
|
1808
|
+
id: scope.ctx.newId("marker"),
|
|
1809
|
+
timeMs: Math.round(op.timeMs),
|
|
1810
|
+
label: op.label ?? ""
|
|
1811
|
+
};
|
|
1812
|
+
if (op.color !== undefined) {
|
|
1813
|
+
marker.color = op.color;
|
|
1814
|
+
}
|
|
1815
|
+
if (op.note !== undefined) {
|
|
1816
|
+
marker.note = op.note;
|
|
1817
|
+
}
|
|
1818
|
+
state.markers.push(marker);
|
|
1819
|
+
return { ok: true, marker: { ...marker } };
|
|
1820
|
+
},
|
|
1821
|
+
delete_marker: (scope, op) => {
|
|
1822
|
+
const state = scope.state;
|
|
1823
|
+
const marker = scope.resolveMarker(op.target);
|
|
1824
|
+
state.markers = state.markers.filter((m) => m.id !== marker.id);
|
|
1825
|
+
return { ok: true, deleted: { ...marker } };
|
|
1826
|
+
},
|
|
1827
|
+
set_markers_from_beats: (scope, op) => {
|
|
1828
|
+
const state = scope.state;
|
|
1829
|
+
const grid = buildBeatGrid({
|
|
1830
|
+
onsetsMs: op.onsets_ms,
|
|
1831
|
+
bpm: op.bpm,
|
|
1832
|
+
offsetMs: op.offset_ms,
|
|
1833
|
+
count: op.count
|
|
1834
|
+
});
|
|
1835
|
+
const stem = (op.label ?? "Beat").trim() || "Beat";
|
|
1836
|
+
const taken = new Set(state.markers.map((m) => m.timeMs));
|
|
1837
|
+
const added = [];
|
|
1838
|
+
const skipped = [];
|
|
1839
|
+
for (const [index, timeMs] of grid.entries()) {
|
|
1840
|
+
if (taken.has(timeMs)) {
|
|
1841
|
+
skipped.push(timeMs);
|
|
1842
|
+
continue;
|
|
1423
1843
|
}
|
|
1424
1844
|
const marker = {
|
|
1425
1845
|
id: scope.ctx.newId("marker"),
|
|
1426
|
-
timeMs
|
|
1427
|
-
label:
|
|
1846
|
+
timeMs,
|
|
1847
|
+
label: `${stem} ${index + 1}`
|
|
1428
1848
|
};
|
|
1429
|
-
if (op.color !== undefined) {
|
|
1430
|
-
marker.color = op.color;
|
|
1431
|
-
}
|
|
1432
|
-
if (op.note !== undefined) {
|
|
1433
|
-
marker.note = op.note;
|
|
1434
|
-
}
|
|
1435
1849
|
state.markers.push(marker);
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
case "delete_marker": {
|
|
1439
|
-
const marker = scope.resolveMarker(op.target);
|
|
1440
|
-
state.markers = state.markers.filter((m) => m.id !== marker.id);
|
|
1441
|
-
return { ok: true, deleted: { ...marker } };
|
|
1442
|
-
}
|
|
1443
|
-
case "set_markers_from_beats": {
|
|
1444
|
-
const grid = buildBeatGrid({
|
|
1445
|
-
onsetsMs: op.onsets_ms,
|
|
1446
|
-
bpm: op.bpm,
|
|
1447
|
-
offsetMs: op.offset_ms,
|
|
1448
|
-
count: op.count
|
|
1449
|
-
});
|
|
1450
|
-
const stem = (op.label ?? "Beat").trim() || "Beat";
|
|
1451
|
-
const taken = new Set(state.markers.map((m) => m.timeMs));
|
|
1452
|
-
const added = [];
|
|
1453
|
-
const skipped = [];
|
|
1454
|
-
for (const [index, timeMs] of grid.entries()) {
|
|
1455
|
-
if (taken.has(timeMs)) {
|
|
1456
|
-
skipped.push(timeMs);
|
|
1457
|
-
continue;
|
|
1458
|
-
}
|
|
1459
|
-
const marker = {
|
|
1460
|
-
id: scope.ctx.newId("marker"),
|
|
1461
|
-
timeMs,
|
|
1462
|
-
label: `${stem} ${index + 1}`
|
|
1463
|
-
};
|
|
1464
|
-
state.markers.push(marker);
|
|
1465
|
-
taken.add(timeMs);
|
|
1466
|
-
added.push(marker);
|
|
1467
|
-
}
|
|
1468
|
-
return {
|
|
1469
|
-
ok: true,
|
|
1470
|
-
grid: {
|
|
1471
|
-
count: grid.length,
|
|
1472
|
-
firstMs: grid[0],
|
|
1473
|
-
lastMs: grid[grid.length - 1]
|
|
1474
|
-
},
|
|
1475
|
-
added: added.map((m) => ({ ...m })),
|
|
1476
|
-
skipped_times_ms: skipped,
|
|
1477
|
-
markers: state.markers.length
|
|
1478
|
-
};
|
|
1850
|
+
taken.add(timeMs);
|
|
1851
|
+
added.push(marker);
|
|
1479
1852
|
}
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
:
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1853
|
+
return {
|
|
1854
|
+
ok: true,
|
|
1855
|
+
grid: {
|
|
1856
|
+
count: grid.length,
|
|
1857
|
+
firstMs: grid[0],
|
|
1858
|
+
lastMs: grid[grid.length - 1]
|
|
1859
|
+
},
|
|
1860
|
+
added: added.map((m) => ({ ...m })),
|
|
1861
|
+
skipped_times_ms: skipped,
|
|
1862
|
+
markers: state.markers.length
|
|
1863
|
+
};
|
|
1864
|
+
},
|
|
1865
|
+
snap_to_beats: (scope, op) => {
|
|
1866
|
+
const named = op.targets === undefined || op.targets === "all" ? undefined : op.targets;
|
|
1867
|
+
const resolved = scope.resolveSnapTargets(named);
|
|
1868
|
+
const missing = resolved.missing;
|
|
1869
|
+
// Each clip is snapped through move/trim, which carry a group's children
|
|
1870
|
+
// and a link's partners. Snapping those as well would move them again,
|
|
1871
|
+
// so one root per group and per link is snapped and the rest follow it.
|
|
1872
|
+
const targetedIds = new Set(resolved.clips.map((clip) => clip.id));
|
|
1873
|
+
const parentOf = new Map(scope.clips.map((clip) => [clip.id, clip.parentId]));
|
|
1874
|
+
const followLinks = scope.ctx.followLinks !== false;
|
|
1875
|
+
const seenLinks = new Set();
|
|
1876
|
+
const targeted = resolved.clips.filter((clip) => {
|
|
1877
|
+
const visited = new Set();
|
|
1878
|
+
let parentId = clip.parentId;
|
|
1879
|
+
while (parentId !== undefined && !visited.has(parentId)) {
|
|
1880
|
+
if (targetedIds.has(parentId)) {
|
|
1881
|
+
return false;
|
|
1882
|
+
}
|
|
1883
|
+
visited.add(parentId);
|
|
1884
|
+
parentId = parentOf.get(parentId);
|
|
1503
1885
|
}
|
|
1504
|
-
if (
|
|
1505
|
-
|
|
1886
|
+
if (followLinks && clip.linkId !== undefined) {
|
|
1887
|
+
if (seenLinks.has(clip.linkId)) {
|
|
1888
|
+
return false;
|
|
1889
|
+
}
|
|
1890
|
+
seenLinks.add(clip.linkId);
|
|
1506
1891
|
}
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1892
|
+
return true;
|
|
1893
|
+
});
|
|
1894
|
+
const offsetMs = op.offset_ms ?? 0;
|
|
1895
|
+
// A tempo grid has to reach the last boundary being snapped, so its
|
|
1896
|
+
// length comes from the targets rather than from the caller.
|
|
1897
|
+
const reachMs = targeted.reduce((end, clip) => Math.max(end, clip.startMs + clip.durationMs), 0);
|
|
1898
|
+
const grid = buildBeatGrid({
|
|
1899
|
+
onsetsMs: op.onsets_ms,
|
|
1900
|
+
bpm: op.bpm,
|
|
1901
|
+
offsetMs: op.offset_ms,
|
|
1902
|
+
count: op.bpm === undefined
|
|
1903
|
+
? undefined
|
|
1904
|
+
: beatCountToCover(op.bpm, offsetMs, reachMs)
|
|
1905
|
+
});
|
|
1906
|
+
const options = {};
|
|
1907
|
+
if (op.tolerance_ms !== undefined) {
|
|
1908
|
+
options.toleranceMs = op.tolerance_ms;
|
|
1909
|
+
}
|
|
1910
|
+
if (op.mode !== undefined) {
|
|
1911
|
+
options.mode = op.mode;
|
|
1912
|
+
}
|
|
1913
|
+
if (op.action !== undefined) {
|
|
1914
|
+
options.action = op.action;
|
|
1915
|
+
}
|
|
1916
|
+
const result = snapClipsToGrid(targeted.map((clip) => ({
|
|
1917
|
+
id: clip.id,
|
|
1918
|
+
startMs: clip.startMs,
|
|
1919
|
+
durationMs: clip.durationMs
|
|
1920
|
+
})), grid, options);
|
|
1921
|
+
const byId = new Map(targeted.map((clip) => [clip.id, clip]));
|
|
1922
|
+
const reported = result.clips.map((entry) => {
|
|
1923
|
+
const clip = byId.get(entry.clipId);
|
|
1924
|
+
if (entry.snapped && clip) {
|
|
1925
|
+
// Through the same ops the caller would use: a group carries its
|
|
1926
|
+
// children (D4) and a trim carries the source points, neither of
|
|
1927
|
+
// which a raw startMs/durationMs write does.
|
|
1928
|
+
try {
|
|
1929
|
+
if (entry.after.durationMs === entry.before.durationMs) {
|
|
1930
|
+
scope.applyMove(clip, { startMs: entry.after.startMs });
|
|
1931
|
+
}
|
|
1932
|
+
else {
|
|
1933
|
+
// A trim moves only the edge that snapped: a snapped start is a
|
|
1934
|
+
// head trim, so the in-point follows it and the out-point stays.
|
|
1935
|
+
let trimmed = clip;
|
|
1936
|
+
const headDelta = entry.before.startMs - entry.after.startMs;
|
|
1937
|
+
if (headDelta !== 0) {
|
|
1938
|
+
trimmed = scope.applyHeadTrim(trimmed, headDelta);
|
|
1522
1939
|
}
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1940
|
+
const tailDelta = entry.after.endMs - entry.before.endMs;
|
|
1941
|
+
if (tailDelta !== 0) {
|
|
1942
|
+
scope.applyTrim(trimmed, {
|
|
1943
|
+
durationMs: trimmed.durationMs + tailDelta
|
|
1526
1944
|
});
|
|
1527
|
-
scope.applyMove(trimmed, { startMs: entry.after.startMs });
|
|
1528
1945
|
}
|
|
1529
1946
|
}
|
|
1530
|
-
catch (error) {
|
|
1531
|
-
return {
|
|
1532
|
-
...entry,
|
|
1533
|
-
snapped: false,
|
|
1534
|
-
after: entry.before,
|
|
1535
|
-
delta: { startMs: 0, endMs: 0 },
|
|
1536
|
-
reason: error instanceof Error ? error.message : String(error),
|
|
1537
|
-
clipName: clip.name
|
|
1538
|
-
};
|
|
1539
|
-
}
|
|
1540
|
-
}
|
|
1541
|
-
return { ...entry, clipName: clip?.name ?? null };
|
|
1542
|
-
});
|
|
1543
|
-
// A name nothing matched is a skip like any other: the caller has to see
|
|
1544
|
-
// it in the same list, not infer it from a shorter one.
|
|
1545
|
-
for (const target of missing) {
|
|
1546
|
-
reported.push({
|
|
1547
|
-
clipId: target,
|
|
1548
|
-
clipName: null,
|
|
1549
|
-
snapped: false,
|
|
1550
|
-
before: { startMs: 0, endMs: 0, durationMs: 0 },
|
|
1551
|
-
after: { startMs: 0, endMs: 0, durationMs: 0 },
|
|
1552
|
-
delta: { startMs: 0, endMs: 0 },
|
|
1553
|
-
reason: `no clip matches "${target}"`
|
|
1554
|
-
});
|
|
1555
|
-
}
|
|
1556
|
-
return {
|
|
1557
|
-
ok: true,
|
|
1558
|
-
grid: {
|
|
1559
|
-
count: grid.length,
|
|
1560
|
-
firstMs: grid[0],
|
|
1561
|
-
lastMs: grid[grid.length - 1]
|
|
1562
|
-
},
|
|
1563
|
-
toleranceMs: result.toleranceMs,
|
|
1564
|
-
mode: result.mode,
|
|
1565
|
-
action: result.action,
|
|
1566
|
-
snapped: reported.filter((entry) => entry.snapped).length,
|
|
1567
|
-
skipped: reported.filter((entry) => !entry.snapped).length,
|
|
1568
|
-
clips: reported
|
|
1569
|
-
};
|
|
1570
|
-
}
|
|
1571
|
-
case "insert_composition": {
|
|
1572
|
-
const loader = scope.ctx.loadComposition;
|
|
1573
|
-
if (!loader) {
|
|
1574
|
-
throw new Error("This surface has no composition library, so insert_composition cannot resolve a template.");
|
|
1575
|
-
}
|
|
1576
|
-
const composition = await loader.get(op.composition_id);
|
|
1577
|
-
if (!composition) {
|
|
1578
|
-
const available = await loader.listIds();
|
|
1579
|
-
throw new Error(`No composition with id "${op.composition_id}". ` +
|
|
1580
|
-
(available.length > 0
|
|
1581
|
-
? `Available: ${available.join(", ")}.`
|
|
1582
|
-
: "This install has none — save one with save_composition."));
|
|
1583
|
-
}
|
|
1584
|
-
const minted = instantiateComposition(composition, {
|
|
1585
|
-
startMs: op.startMs,
|
|
1586
|
-
params: op.params,
|
|
1587
|
-
newId: () => scope.ctx.newId("clip")
|
|
1588
|
-
});
|
|
1589
|
-
// Template track ids are names, not document ids. Two clips overlapping
|
|
1590
|
-
// on one track auto-dissolve into each other, so each template track
|
|
1591
|
-
// becomes a track of its own — created front-most first, because the
|
|
1592
|
-
// lowest track index draws on top (I9).
|
|
1593
|
-
const templateTracks = [];
|
|
1594
|
-
for (const child of composition.children) {
|
|
1595
|
-
if (!templateTracks.includes(child.trackId)) {
|
|
1596
|
-
templateTracks.push(child.trackId);
|
|
1597
1947
|
}
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
const groupTrack = op.trackId
|
|
1608
|
-
? scope.resolveTrack(op.trackId)
|
|
1609
|
-
: (scope.tracks.find((t) => t.type === "overlay" && t.name === composition.name) ?? scope.addTrack("overlay", composition.name));
|
|
1610
|
-
const [group, ...children] = minted;
|
|
1611
|
-
group.trackId = groupTrack.id;
|
|
1612
|
-
for (const [index, child] of children.entries()) {
|
|
1613
|
-
child.trackId =
|
|
1614
|
-
mapped.get(composition.children[index].trackId) ?? groupTrack.id;
|
|
1615
|
-
}
|
|
1616
|
-
scope.clips.push(group, ...children);
|
|
1617
|
-
state.selectedClipIds = [group.id];
|
|
1618
|
-
scope.touch(group.id, ...children.map((c) => c.id));
|
|
1619
|
-
return {
|
|
1620
|
-
ok: true,
|
|
1621
|
-
compositionId: composition.id,
|
|
1622
|
-
clip: scope.clipOut(group),
|
|
1623
|
-
children: children.map((child) => scope.clipOut(child)),
|
|
1624
|
-
params: group.compositionParams ?? {}
|
|
1625
|
-
};
|
|
1626
|
-
}
|
|
1627
|
-
case "list_takes": {
|
|
1628
|
-
const clip = scope.resolveClip(op.target);
|
|
1629
|
-
const versions = clip.versions ?? [];
|
|
1630
|
-
const activeId = activeTakeIdOf(clip);
|
|
1631
|
-
return {
|
|
1632
|
-
ok: true,
|
|
1633
|
-
takes: versions.map((v) => ({
|
|
1634
|
-
id: v.id,
|
|
1635
|
-
label: v.label,
|
|
1636
|
-
source: v.source,
|
|
1637
|
-
provider: v.provider,
|
|
1638
|
-
model: v.model,
|
|
1639
|
-
createdAt: v.createdAt,
|
|
1640
|
-
costCredits: v.costCredits,
|
|
1641
|
-
durationMs: v.durationMs,
|
|
1642
|
-
status: v.status,
|
|
1643
|
-
favorite: v.favorite,
|
|
1644
|
-
active: v.id === activeId
|
|
1645
|
-
})),
|
|
1646
|
-
activeTakeId: activeId ?? null
|
|
1647
|
-
};
|
|
1648
|
-
}
|
|
1649
|
-
case "select_take": {
|
|
1650
|
-
const clip = scope.resolveClip(op.target);
|
|
1651
|
-
if (clip.mediaType === "model3d") {
|
|
1652
|
-
throw new Error(`Clip "${clip.name}" is a 3D clip — its version history includes ` +
|
|
1653
|
-
"bake renders that must not replace the glTF source. Take " +
|
|
1654
|
-
"selection is not available for model3d clips yet.");
|
|
1655
|
-
}
|
|
1656
|
-
const version = (clip.versions ?? []).find((v) => v.id === op.takeId);
|
|
1657
|
-
if (!version) {
|
|
1658
|
-
throw new Error(`No take "${op.takeId}" on "${clip.name}". ${scope.validUnits((clip.versions ?? []).map((v) => ({
|
|
1659
|
-
id: v.id,
|
|
1660
|
-
name: v.label ?? v.id
|
|
1661
|
-
})), "take")}`);
|
|
1662
|
-
}
|
|
1663
|
-
if (version.status !== "success") {
|
|
1664
|
-
throw new Error(`Take "${op.takeId}" on "${clip.name}" did not finish successfully ` +
|
|
1665
|
-
`(status: ${version.status}) — only a successful take can be selected.`);
|
|
1666
|
-
}
|
|
1667
|
-
const next = selectTake(clip, op.takeId);
|
|
1668
|
-
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
1669
|
-
scope.touch(clip.id);
|
|
1670
|
-
return { ok: true, clip: scope.clipOut(next) };
|
|
1671
|
-
}
|
|
1672
|
-
case "rename_take": {
|
|
1673
|
-
const clip = scope.resolveClip(op.target);
|
|
1674
|
-
if (!(clip.versions ?? []).some((v) => v.id === op.takeId)) {
|
|
1675
|
-
throw new Error(`No take "${op.takeId}" on "${clip.name}". ${scope.validUnits((clip.versions ?? []).map((v) => ({
|
|
1676
|
-
id: v.id,
|
|
1677
|
-
name: v.label ?? v.id
|
|
1678
|
-
})), "take")}`);
|
|
1679
|
-
}
|
|
1680
|
-
const next = renameTakeOnClip(clip, op.takeId, op.label);
|
|
1681
|
-
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
1682
|
-
scope.touch(clip.id);
|
|
1683
|
-
return { ok: true, clip: scope.clipOut(next) };
|
|
1684
|
-
}
|
|
1685
|
-
case "delete_take": {
|
|
1686
|
-
const clip = scope.resolveClip(op.target);
|
|
1687
|
-
const { clip: next, error } = deleteTakeOnClip(clip, op.takeId);
|
|
1688
|
-
if (error) {
|
|
1689
|
-
throw new Error(error);
|
|
1690
|
-
}
|
|
1691
|
-
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
1692
|
-
scope.touch(clip.id);
|
|
1693
|
-
return { ok: true, clip: scope.clipOut(next) };
|
|
1694
|
-
}
|
|
1695
|
-
// Subject/object tracks (P0 AI Video, Phase 2). The async provider call
|
|
1696
|
-
// that fills a track's samples is the `track_object` capability, not an
|
|
1697
|
-
// op — these four are the structural/synchronous surface over it.
|
|
1698
|
-
case "list_tracks": {
|
|
1699
|
-
const clipId = op.target ? scope.resolveClip(op.target).id : undefined;
|
|
1700
|
-
const tracks = scope.mediaTracks.filter((t) => clipId === undefined || t.clipId === clipId);
|
|
1701
|
-
return {
|
|
1702
|
-
ok: true,
|
|
1703
|
-
tracks: tracks.map((t) => ({
|
|
1704
|
-
id: t.id,
|
|
1705
|
-
clipId: t.clipId,
|
|
1706
|
-
name: t.name,
|
|
1707
|
-
kind: t.kind,
|
|
1708
|
-
status: t.status,
|
|
1709
|
-
sourceStartMs: t.sourceStartMs,
|
|
1710
|
-
sourceEndMs: t.sourceEndMs,
|
|
1711
|
-
sampleCount: t.samples.length,
|
|
1712
|
-
confidence: t.confidence
|
|
1713
|
-
}))
|
|
1714
|
-
};
|
|
1715
|
-
}
|
|
1716
|
-
case "delete_track_object": {
|
|
1717
|
-
const track = scope.mediaTracks.find((t) => t.id === op.trackId);
|
|
1718
|
-
if (!track) {
|
|
1719
|
-
throw new Error(`No track "${op.trackId}". ${scope.validUnits(scope.mediaTracks.map((t) => ({ id: t.id, name: t.name })), "track")}`);
|
|
1720
|
-
}
|
|
1721
|
-
scope.mediaTracks = scope.mediaTracks.filter((t) => t.id !== op.trackId);
|
|
1722
|
-
const unboundClipIds = [];
|
|
1723
|
-
scope.clips = scope.clips.map((c) => {
|
|
1724
|
-
if (c.trackBinding?.trackId !== op.trackId) {
|
|
1725
|
-
return c;
|
|
1948
|
+
catch (error) {
|
|
1949
|
+
return {
|
|
1950
|
+
...entry,
|
|
1951
|
+
snapped: false,
|
|
1952
|
+
after: entry.before,
|
|
1953
|
+
delta: { startMs: 0, endMs: 0 },
|
|
1954
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
1955
|
+
clipName: clip.name
|
|
1956
|
+
};
|
|
1726
1957
|
}
|
|
1727
|
-
unboundClipIds.push(c.id);
|
|
1728
|
-
const { trackBinding: _dropped, ...rest } = c;
|
|
1729
|
-
return rest;
|
|
1730
|
-
});
|
|
1731
|
-
scope.touch(...unboundClipIds);
|
|
1732
|
-
return { ok: true, deleted: { id: track.id, name: track.name } };
|
|
1733
|
-
}
|
|
1734
|
-
case "bind_to_track": {
|
|
1735
|
-
const clip = scope.resolveClip(op.target);
|
|
1736
|
-
const track = scope.mediaTracks.find((t) => t.id === op.trackId);
|
|
1737
|
-
if (!track) {
|
|
1738
|
-
throw new Error(`No track "${op.trackId}". ${scope.validUnits(scope.mediaTracks.map((t) => ({ id: t.id, name: t.name })), "track")}`);
|
|
1739
|
-
}
|
|
1740
|
-
if (op.mode !== "position" && op.mode !== "position_scale") {
|
|
1741
|
-
throw new Error(`bind_to_track mode "${op.mode}" is not implemented yet — only ` +
|
|
1742
|
-
`"position" and "position_scale" affect rendering in this build. ` +
|
|
1743
|
-
"The field accepts the other modes so a document can name the " +
|
|
1744
|
-
"intent without a schema migration later, but binding to one " +
|
|
1745
|
-
"today would be a no-op, so the call is refused instead.");
|
|
1746
|
-
}
|
|
1747
|
-
const binding = { trackId: op.trackId, mode: op.mode };
|
|
1748
|
-
if (op.offset !== undefined) {
|
|
1749
|
-
binding.offset = op.offset;
|
|
1750
|
-
}
|
|
1751
|
-
if (op.scale !== undefined) {
|
|
1752
|
-
binding.scale = op.scale;
|
|
1753
|
-
}
|
|
1754
|
-
if (op.rotationOffset !== undefined) {
|
|
1755
|
-
binding.rotationOffset = op.rotationOffset;
|
|
1756
1958
|
}
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
const { trackBinding: _dropped, ...next } = clip;
|
|
1771
|
-
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
1772
|
-
scope.touch(clip.id);
|
|
1773
|
-
return { ok: true, clip: scope.clipOut(next) };
|
|
1774
|
-
}
|
|
1775
|
-
case "add_midi_clip": {
|
|
1776
|
-
const track = op.track
|
|
1777
|
-
? scope.resolveMidiTrack(op.track)
|
|
1778
|
-
: scope.findOrCreateTrack("midi");
|
|
1779
|
-
const name = op.name ?? "Phrase";
|
|
1780
|
-
const clip = makeClip({
|
|
1781
|
-
id: scope.ctx.newId("clip"),
|
|
1782
|
-
trackId: track.id,
|
|
1783
|
-
name,
|
|
1784
|
-
startMs: op.start_ms ?? scope.trackEndMs(track.id),
|
|
1785
|
-
durationMs: op.duration_ms,
|
|
1786
|
-
mediaType: "midi",
|
|
1787
|
-
sourceType: "imported",
|
|
1788
|
-
status: "generated",
|
|
1789
|
-
notes: scope.buildNotes(op.notes, name)
|
|
1959
|
+
return { ...entry, clipName: clip?.name ?? null };
|
|
1960
|
+
});
|
|
1961
|
+
// A name nothing matched is a skip like any other: the caller has to see
|
|
1962
|
+
// it in the same list, not infer it from a shorter one.
|
|
1963
|
+
for (const target of missing) {
|
|
1964
|
+
reported.push({
|
|
1965
|
+
clipId: target,
|
|
1966
|
+
clipName: null,
|
|
1967
|
+
snapped: false,
|
|
1968
|
+
before: { startMs: 0, endMs: 0, durationMs: 0 },
|
|
1969
|
+
after: { startMs: 0, endMs: 0, durationMs: 0 },
|
|
1970
|
+
delta: { startMs: 0, endMs: 0 },
|
|
1971
|
+
reason: `no clip matches "${target}"`
|
|
1790
1972
|
});
|
|
1791
|
-
scope.clips.push(clip);
|
|
1792
|
-
scope.state.selectedClipIds = [clip.id];
|
|
1793
|
-
scope.touch(clip.id);
|
|
1794
|
-
return { ok: true, clip: scope.clipOut(clip) };
|
|
1795
1973
|
}
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1974
|
+
return {
|
|
1975
|
+
ok: true,
|
|
1976
|
+
grid: {
|
|
1977
|
+
count: grid.length,
|
|
1978
|
+
firstMs: grid[0],
|
|
1979
|
+
lastMs: grid[grid.length - 1]
|
|
1980
|
+
},
|
|
1981
|
+
toleranceMs: result.toleranceMs,
|
|
1982
|
+
mode: result.mode,
|
|
1983
|
+
action: result.action,
|
|
1984
|
+
snapped: reported.filter((entry) => entry.snapped).length,
|
|
1985
|
+
skipped: reported.filter((entry) => !entry.snapped).length,
|
|
1986
|
+
clips: reported
|
|
1987
|
+
};
|
|
1988
|
+
},
|
|
1989
|
+
insert_composition: async (scope, op) => {
|
|
1990
|
+
const state = scope.state;
|
|
1991
|
+
const loader = scope.ctx.loadComposition;
|
|
1992
|
+
if (!loader) {
|
|
1993
|
+
throw new Error("This surface has no composition library, so insert_composition cannot resolve a template.");
|
|
1994
|
+
}
|
|
1995
|
+
const composition = await loader.get(op.composition_id);
|
|
1996
|
+
if (!composition) {
|
|
1997
|
+
const available = await loader.listIds();
|
|
1998
|
+
throw new Error(`No composition with id "${op.composition_id}". ` +
|
|
1999
|
+
(available.length > 0
|
|
2000
|
+
? `Available: ${available.join(", ")}.`
|
|
2001
|
+
: "This install has none — save one with save_composition."));
|
|
2002
|
+
}
|
|
2003
|
+
const minted = instantiateComposition(composition, {
|
|
2004
|
+
startMs: op.startMs,
|
|
2005
|
+
params: op.params,
|
|
2006
|
+
newId: () => scope.ctx.newId("clip")
|
|
2007
|
+
});
|
|
2008
|
+
// Template track ids are names, not document ids. Two clips overlapping
|
|
2009
|
+
// on one track auto-dissolve into each other, so each template track
|
|
2010
|
+
// becomes a track of its own — created front-most first, because the
|
|
2011
|
+
// lowest track index draws on top (I9).
|
|
2012
|
+
const templateTracks = [];
|
|
2013
|
+
for (const child of composition.children) {
|
|
2014
|
+
if (!templateTracks.includes(child.trackId)) {
|
|
2015
|
+
templateTracks.push(child.trackId);
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
const mapped = new Map();
|
|
2019
|
+
for (const name of [...templateTracks].reverse()) {
|
|
2020
|
+
const existing = scope.tracks.find((t) => t.type === "overlay" && t.name === name && !t.locked);
|
|
2021
|
+
mapped.set(name, (existing ?? scope.addTrack("overlay", name)).id);
|
|
2022
|
+
}
|
|
2023
|
+
// The group draws nothing, but it still occupies its track's timeline,
|
|
2024
|
+
// and a group sharing a track with one of its children reads as an
|
|
2025
|
+
// overlap. It gets a track named after the composition instead.
|
|
2026
|
+
const groupTrack = op.trackId
|
|
2027
|
+
? scope.resolveTrack(op.trackId)
|
|
2028
|
+
: (scope.tracks.find((t) => t.type === "overlay" && t.name === composition.name && !t.locked) ?? scope.addTrack("overlay", composition.name));
|
|
2029
|
+
scope.assertWritable([], groupTrack.id);
|
|
2030
|
+
const [group, ...children] = minted;
|
|
2031
|
+
group.trackId = groupTrack.id;
|
|
2032
|
+
for (const [index, child] of children.entries()) {
|
|
2033
|
+
child.trackId =
|
|
2034
|
+
mapped.get(composition.children[index].trackId) ?? groupTrack.id;
|
|
2035
|
+
}
|
|
2036
|
+
scope.clips.push(group, ...children);
|
|
2037
|
+
state.selectedClipIds = [group.id];
|
|
2038
|
+
scope.touch(group.id, ...children.map((c) => c.id));
|
|
2039
|
+
return {
|
|
2040
|
+
ok: true,
|
|
2041
|
+
compositionId: composition.id,
|
|
2042
|
+
clip: scope.clipOut(group),
|
|
2043
|
+
children: children.map((child) => scope.clipOut(child)),
|
|
2044
|
+
params: group.compositionParams ?? {}
|
|
2045
|
+
};
|
|
2046
|
+
},
|
|
2047
|
+
list_takes: (scope, op) => {
|
|
2048
|
+
const clip = scope.resolveClip(op.target);
|
|
2049
|
+
const versions = clip.versions ?? [];
|
|
2050
|
+
const activeId = activeTakeIdOf(clip);
|
|
2051
|
+
return {
|
|
2052
|
+
ok: true,
|
|
2053
|
+
takes: versions.map((v) => ({
|
|
2054
|
+
id: v.id,
|
|
2055
|
+
label: v.label,
|
|
2056
|
+
source: v.source,
|
|
2057
|
+
provider: v.provider,
|
|
2058
|
+
model: v.model,
|
|
2059
|
+
createdAt: v.createdAt,
|
|
2060
|
+
costCredits: v.costCredits,
|
|
2061
|
+
durationMs: v.durationMs,
|
|
2062
|
+
status: v.status,
|
|
2063
|
+
favorite: v.favorite,
|
|
2064
|
+
active: v.id === activeId
|
|
2065
|
+
})),
|
|
2066
|
+
activeTakeId: activeId ?? null
|
|
2067
|
+
};
|
|
2068
|
+
},
|
|
2069
|
+
select_take: (scope, op) => {
|
|
2070
|
+
const clip = scope.resolveClip(op.target);
|
|
2071
|
+
scope.assertWritable([clip]);
|
|
2072
|
+
if (clip.mediaType === "model3d") {
|
|
2073
|
+
throw new Error(`Clip "${clip.name}" is a 3D clip — its version history includes ` +
|
|
2074
|
+
"bake renders that must not replace the glTF source. Take " +
|
|
2075
|
+
"selection is not available for model3d clips yet.");
|
|
2076
|
+
}
|
|
2077
|
+
const version = findByIdOrShortId(clip.versions ?? [], op.takeId, "take");
|
|
2078
|
+
if (!version) {
|
|
2079
|
+
throw new Error(`No take "${op.takeId}" on "${clip.name}". ${scope.validUnits((clip.versions ?? []).map((v) => ({
|
|
2080
|
+
id: v.id,
|
|
2081
|
+
name: v.label ?? v.id
|
|
2082
|
+
})), "take")}`);
|
|
2083
|
+
}
|
|
2084
|
+
if (version.status !== "success") {
|
|
2085
|
+
throw new Error(`Take "${op.takeId}" on "${clip.name}" did not finish successfully ` +
|
|
2086
|
+
`(status: ${version.status}) — only a successful take can be selected.`);
|
|
2087
|
+
}
|
|
2088
|
+
const next = selectTake(clip, version.id);
|
|
2089
|
+
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
2090
|
+
scope.touch(clip.id);
|
|
2091
|
+
return { ok: true, clip: scope.clipOut(next) };
|
|
2092
|
+
},
|
|
2093
|
+
rename_take: (scope, op) => {
|
|
2094
|
+
const clip = scope.resolveClip(op.target);
|
|
2095
|
+
scope.assertWritable([clip]);
|
|
2096
|
+
const take = findByIdOrShortId(clip.versions ?? [], op.takeId, "take");
|
|
2097
|
+
if (!take) {
|
|
2098
|
+
throw new Error(`No take "${op.takeId}" on "${clip.name}". ${scope.validUnits((clip.versions ?? []).map((v) => ({
|
|
2099
|
+
id: v.id,
|
|
2100
|
+
name: v.label ?? v.id
|
|
2101
|
+
})), "take")}`);
|
|
2102
|
+
}
|
|
2103
|
+
const next = renameTakeOnClip(clip, take.id, op.label);
|
|
2104
|
+
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
2105
|
+
scope.touch(clip.id);
|
|
2106
|
+
return { ok: true, clip: scope.clipOut(next) };
|
|
2107
|
+
},
|
|
2108
|
+
delete_take: (scope, op) => {
|
|
2109
|
+
const clip = scope.resolveClip(op.target);
|
|
2110
|
+
scope.assertWritable([clip]);
|
|
2111
|
+
const take = findByIdOrShortId(clip.versions ?? [], op.takeId, "take");
|
|
2112
|
+
const { clip: next, error } = deleteTakeOnClip(clip, take?.id ?? op.takeId);
|
|
2113
|
+
if (error) {
|
|
2114
|
+
throw new Error(error);
|
|
2115
|
+
}
|
|
2116
|
+
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
2117
|
+
scope.touch(clip.id);
|
|
2118
|
+
return { ok: true, clip: scope.clipOut(next) };
|
|
2119
|
+
},
|
|
2120
|
+
// Subject/object tracks (P0 AI Video, Phase 2). The async provider call
|
|
2121
|
+
// that fills a track's samples is the `track_object` capability, not an
|
|
2122
|
+
// op — these four are the structural/synchronous surface over it.
|
|
2123
|
+
list_tracks: (scope, op) => {
|
|
2124
|
+
const clipId = op.target ? scope.resolveClip(op.target).id : undefined;
|
|
2125
|
+
const tracks = scope.mediaTracks.filter((t) => clipId === undefined || t.clipId === clipId);
|
|
2126
|
+
return {
|
|
2127
|
+
ok: true,
|
|
2128
|
+
tracks: tracks.map((t) => ({
|
|
2129
|
+
id: t.id,
|
|
2130
|
+
clipId: t.clipId,
|
|
2131
|
+
name: t.name,
|
|
2132
|
+
kind: t.kind,
|
|
2133
|
+
status: t.status,
|
|
2134
|
+
sourceStartMs: t.sourceStartMs,
|
|
2135
|
+
sourceEndMs: t.sourceEndMs,
|
|
2136
|
+
sampleCount: t.samples.length,
|
|
2137
|
+
confidence: t.confidence
|
|
2138
|
+
}))
|
|
2139
|
+
};
|
|
2140
|
+
},
|
|
2141
|
+
delete_track_object: (scope, op) => {
|
|
2142
|
+
const track = findByIdOrShortId(scope.mediaTracks, op.trackId, "track");
|
|
2143
|
+
if (!track) {
|
|
2144
|
+
throw new Error(`No track "${op.trackId}". ${scope.validUnits(scope.mediaTracks.map((t) => ({ id: t.id, name: t.name })), "track")}`);
|
|
2145
|
+
}
|
|
2146
|
+
scope.mediaTracks = scope.mediaTracks.filter((t) => t.id !== track.id);
|
|
2147
|
+
const unboundClipIds = [];
|
|
2148
|
+
scope.clips = scope.clips.map((c) => {
|
|
2149
|
+
if (c.trackBinding?.trackId !== track.id) {
|
|
2150
|
+
return c;
|
|
2151
|
+
}
|
|
2152
|
+
unboundClipIds.push(c.id);
|
|
2153
|
+
const { trackBinding: _dropped, ...rest } = c;
|
|
2154
|
+
return rest;
|
|
2155
|
+
});
|
|
2156
|
+
scope.touch(...unboundClipIds);
|
|
2157
|
+
return { ok: true, deleted: { id: track.id, name: track.name } };
|
|
2158
|
+
},
|
|
2159
|
+
bind_to_track: (scope, op) => {
|
|
2160
|
+
const clip = scope.resolveClip(op.target);
|
|
2161
|
+
const track = findByIdOrShortId(scope.mediaTracks, op.trackId, "track");
|
|
2162
|
+
if (!track) {
|
|
2163
|
+
throw new Error(`No track "${op.trackId}". ${scope.validUnits(scope.mediaTracks.map((t) => ({ id: t.id, name: t.name })), "track")}`);
|
|
2164
|
+
}
|
|
2165
|
+
if (op.mode !== "position" && op.mode !== "position_scale") {
|
|
2166
|
+
throw new Error(`bind_to_track mode "${op.mode}" is not implemented yet — only ` +
|
|
2167
|
+
`"position" and "position_scale" affect rendering in this build. ` +
|
|
2168
|
+
"The field accepts the other modes so a document can name the " +
|
|
2169
|
+
"intent without a schema migration later, but binding to one " +
|
|
2170
|
+
"today would be a no-op, so the call is refused instead.");
|
|
2171
|
+
}
|
|
2172
|
+
const binding = { trackId: track.id, mode: op.mode };
|
|
2173
|
+
if (op.offset !== undefined) {
|
|
2174
|
+
binding.offset = op.offset;
|
|
2175
|
+
}
|
|
2176
|
+
if (op.scale !== undefined) {
|
|
2177
|
+
binding.scale = op.scale;
|
|
2178
|
+
}
|
|
2179
|
+
if (op.rotationOffset !== undefined) {
|
|
2180
|
+
binding.rotationOffset = op.rotationOffset;
|
|
2181
|
+
}
|
|
2182
|
+
if (op.smoothing !== undefined) {
|
|
2183
|
+
binding.smoothing = op.smoothing;
|
|
2184
|
+
}
|
|
2185
|
+
const next = { ...clip, trackBinding: binding };
|
|
2186
|
+
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
2187
|
+
scope.touch(clip.id);
|
|
2188
|
+
return { ok: true, clip: scope.clipOut(next) };
|
|
2189
|
+
},
|
|
2190
|
+
unbind_track: (scope, op) => {
|
|
2191
|
+
const clip = scope.resolveClip(op.target);
|
|
2192
|
+
if (!clip.trackBinding) {
|
|
1800
2193
|
return { ok: true, clip: scope.clipOut(clip) };
|
|
1801
2194
|
}
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
scope.
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
2195
|
+
const { trackBinding: _dropped, ...next } = clip;
|
|
2196
|
+
scope.clips = scope.clips.map((c) => (c.id === clip.id ? next : c));
|
|
2197
|
+
scope.touch(clip.id);
|
|
2198
|
+
return { ok: true, clip: scope.clipOut(next) };
|
|
2199
|
+
},
|
|
2200
|
+
add_midi_clip: (scope, op) => {
|
|
2201
|
+
const track = op.track
|
|
2202
|
+
? scope.resolveMidiTrack(op.track)
|
|
2203
|
+
: scope.findOrCreateTrack("midi");
|
|
2204
|
+
scope.assertWritable([], track.id);
|
|
2205
|
+
const name = op.name ?? "Phrase";
|
|
2206
|
+
const clip = makeClip({
|
|
2207
|
+
id: scope.ctx.newId("clip"),
|
|
2208
|
+
trackId: track.id,
|
|
2209
|
+
name,
|
|
2210
|
+
startMs: op.start_ms ?? scope.trackEndMs(track.id),
|
|
2211
|
+
durationMs: op.duration_ms,
|
|
2212
|
+
mediaType: "midi",
|
|
2213
|
+
sourceType: "imported",
|
|
2214
|
+
status: "generated",
|
|
2215
|
+
notes: scope.buildNotes(op.notes, name)
|
|
2216
|
+
});
|
|
2217
|
+
scope.clips.push(clip);
|
|
2218
|
+
scope.state.selectedClipIds = [clip.id];
|
|
2219
|
+
scope.touch(clip.id);
|
|
2220
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
2221
|
+
},
|
|
2222
|
+
set_notes: (scope, op) => {
|
|
2223
|
+
const clip = scope.resolveMidiClip(op.clip);
|
|
2224
|
+
scope.assertWritable([clip]);
|
|
2225
|
+
clip.notes = scope.buildNotes(op.notes, clip.name);
|
|
2226
|
+
scope.touch(clip.id);
|
|
2227
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
2228
|
+
},
|
|
2229
|
+
set_tempo: (scope, op) => {
|
|
2230
|
+
const previous = resolveTempo(scope.state);
|
|
2231
|
+
const tempo = {
|
|
2232
|
+
bpm: op.bpm,
|
|
2233
|
+
offsetMs: op.offset_ms ?? previous.offsetMs,
|
|
2234
|
+
timeSignature: {
|
|
2235
|
+
beatsPerBar: op.beats_per_bar ?? previous.timeSignature.beatsPerBar,
|
|
2236
|
+
beatUnit: op.beat_unit ?? previous.timeSignature.beatUnit
|
|
1829
2237
|
}
|
|
1830
|
-
|
|
1831
|
-
|
|
2238
|
+
};
|
|
2239
|
+
scope.clips = rescaleClipsForTempo(scope.clips, scope.tracks, previous, tempo);
|
|
2240
|
+
scope.state.tempo = tempo;
|
|
2241
|
+
const rescaledClipIds = scope.clips
|
|
2242
|
+
.filter((c) => c.mediaType === "midi")
|
|
2243
|
+
.map((c) => c.id);
|
|
2244
|
+
scope.touch(...rescaledClipIds);
|
|
2245
|
+
return { ok: true, tempo, previousTempo: previous, rescaledClipIds };
|
|
2246
|
+
},
|
|
2247
|
+
set_track_instrument: async (scope, op) => {
|
|
2248
|
+
const track = scope.resolveMidiTrack(op.track);
|
|
2249
|
+
scope.assertWritable([], track.id);
|
|
2250
|
+
let instrument;
|
|
2251
|
+
if ("preset" in op.instrument) {
|
|
2252
|
+
const preset = findInstrumentPreset(op.instrument.preset);
|
|
2253
|
+
if (!preset) {
|
|
2254
|
+
throw new Error(`No instrument preset named "${op.instrument.preset}". Valid presets: ${MIDI_INSTRUMENT_PRESETS.map((p) => `${p.id} ("${p.name}")`).join(", ")}.`);
|
|
2255
|
+
}
|
|
2256
|
+
instrument = structuredClone(preset.instrument);
|
|
2257
|
+
}
|
|
2258
|
+
else {
|
|
2259
|
+
instrument = structuredClone(op.instrument);
|
|
2260
|
+
}
|
|
2261
|
+
if (instrument.type === "sampler" && instrument.zones.length) {
|
|
2262
|
+
const resolveAsset = scope.ctx.resolveAsset;
|
|
2263
|
+
if (!resolveAsset) {
|
|
2264
|
+
throw new Error("Sample assets cannot be resolved on this host");
|
|
1832
2265
|
}
|
|
1833
|
-
|
|
1834
|
-
const
|
|
1835
|
-
if (!
|
|
1836
|
-
throw new Error(
|
|
2266
|
+
instrument.zones = await Promise.all(instrument.zones.map(async (zone) => {
|
|
2267
|
+
const asset = await resolveAsset(zone.assetId);
|
|
2268
|
+
if (!asset || !asset.contentType.startsWith("audio/")) {
|
|
2269
|
+
throw new Error(`Audio sample unavailable: ${zone.name}`);
|
|
1837
2270
|
}
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
if (!asset || !asset.contentType.startsWith("audio/")) {
|
|
1841
|
-
throw new Error(`Audio sample unavailable: ${zone.name}`);
|
|
1842
|
-
}
|
|
1843
|
-
return { ...zone, assetId: asset.id };
|
|
1844
|
-
}));
|
|
1845
|
-
}
|
|
1846
|
-
track.instrument = instrument;
|
|
1847
|
-
return { ok: true, track: scope.trackOut(track) };
|
|
1848
|
-
}
|
|
1849
|
-
case "transpose_clip":
|
|
1850
|
-
case "quantize_notes":
|
|
1851
|
-
case "scale_velocity": {
|
|
1852
|
-
const clip = scope.resolveMidiClip(op.clip);
|
|
1853
|
-
const before = clip.notes ?? [];
|
|
1854
|
-
const notes = op.op === "transpose_clip"
|
|
1855
|
-
? transposeNotes(before, op.semitones)
|
|
1856
|
-
: op.op === "scale_velocity"
|
|
1857
|
-
? scaleVelocity(before, op.factor)
|
|
1858
|
-
: quantizeNotes(before, {
|
|
1859
|
-
division: op.division,
|
|
1860
|
-
strength: op.strength,
|
|
1861
|
-
target: op.target
|
|
1862
|
-
});
|
|
1863
|
-
clip.notes = notes;
|
|
1864
|
-
scope.touch(clip.id);
|
|
1865
|
-
const result = {
|
|
1866
|
-
ok: true,
|
|
1867
|
-
clip: scope.clipOut(clip),
|
|
1868
|
-
noteCount: notes.length
|
|
1869
|
-
};
|
|
1870
|
-
if (op.op === "quantize_notes") {
|
|
1871
|
-
result.movedNoteCount = notes.filter((note, i) => note.startTick !== before[i]?.startTick ||
|
|
1872
|
-
note.durationTick !== before[i]?.durationTick).length;
|
|
1873
|
-
}
|
|
1874
|
-
return result;
|
|
2271
|
+
return { ...zone, assetId: asset.id };
|
|
2272
|
+
}));
|
|
1875
2273
|
}
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
2274
|
+
track.instrument = instrument;
|
|
2275
|
+
return { ok: true, track: scope.trackOut(track) };
|
|
2276
|
+
},
|
|
2277
|
+
transpose_clip: runMidiNoteEdit,
|
|
2278
|
+
quantize_notes: runMidiNoteEdit,
|
|
2279
|
+
scale_velocity: runMidiNoteEdit,
|
|
2280
|
+
set_reframe_subject: (scope, op) => {
|
|
2281
|
+
const clip = scope.resolveClip(op.clip_id);
|
|
2282
|
+
const track = findByIdOrShortId(scope.mediaTracks.filter((t) => t.clipId === clip.id), op.track_id, "track");
|
|
2283
|
+
if (!track) {
|
|
2284
|
+
throw new Error(`No track "${op.track_id}" belongs to clip "${clip.name}". ${scope.validUnits(scope.mediaTracks.filter((t) => t.clipId === clip.id), "track")}`);
|
|
2285
|
+
}
|
|
2286
|
+
if (track.status !== "ready") {
|
|
2287
|
+
throw new Error(`Track "${track.name}" is ${track.status}; Smart Reframe can only follow a ready track.`);
|
|
2288
|
+
}
|
|
2289
|
+
if (!mediaTrackCanDriveReframe(track) || isMediaTrackStale(track, clip)) {
|
|
2290
|
+
throw new Error(`Track "${track.name}" has no current analysis for clip "${clip.name}".`);
|
|
2291
|
+
}
|
|
2292
|
+
const previous = clip.reframe ?? {
|
|
2293
|
+
mode: "track"
|
|
2294
|
+
};
|
|
2295
|
+
clip.reframe = {
|
|
2296
|
+
...previous,
|
|
2297
|
+
mode: "track",
|
|
2298
|
+
trackId: track.id,
|
|
2299
|
+
sourceAssetId: clip.currentAssetId,
|
|
2300
|
+
safeMargin: op.safe_margin ?? previous.safeMargin,
|
|
2301
|
+
smoothing: op.smoothing ?? previous.smoothing
|
|
2302
|
+
};
|
|
2303
|
+
scope.touch(clip.id);
|
|
2304
|
+
return { ok: true, clip: scope.clipOut(clip) };
|
|
2305
|
+
},
|
|
2306
|
+
add_reframe_keyframe: (scope, op) => {
|
|
2307
|
+
const clip = scope.resolveClip(op.clip_id);
|
|
2308
|
+
clip.reframe ??= { mode: "auto" };
|
|
2309
|
+
const keyframe = {
|
|
2310
|
+
sourceMs: op.source_ms,
|
|
2311
|
+
x: op.x,
|
|
2312
|
+
y: op.y,
|
|
2313
|
+
zoom: op.zoom
|
|
2314
|
+
};
|
|
2315
|
+
clip.reframe = addReframeKeyframe(clip.reframe, keyframe);
|
|
2316
|
+
scope.touch(clip.id);
|
|
2317
|
+
return { ok: true, clip: scope.clipOut(clip), keyframe };
|
|
2318
|
+
},
|
|
2319
|
+
clear_reframe: (scope, op) => {
|
|
2320
|
+
const clip = scope.resolveClip(op.clip_id);
|
|
2321
|
+
const next = clearReframe(clip);
|
|
2322
|
+
scope.replaceClip(clip, next);
|
|
2323
|
+
scope.touch(clip.id);
|
|
2324
|
+
return { ok: true, clip: scope.clipOut(next) };
|
|
2325
|
+
},
|
|
2326
|
+
retarget_format: async (scope, op) => {
|
|
2327
|
+
// Keys and values may be short ids; the adaptation reads full ones.
|
|
2328
|
+
const trackIdByClipId = {};
|
|
2329
|
+
for (const [clipId, trackId] of Object.entries(op.track_ids ?? {})) {
|
|
2330
|
+
const clip = findByIdOrShortId(scope.clips, clipId, "clip");
|
|
2331
|
+
if (!clip) {
|
|
2332
|
+
throw new Error(`track_ids names unknown clip "${clipId}". ${scope.validUnits(scope.clips, "clip")}`);
|
|
2333
|
+
}
|
|
2334
|
+
const track = findByIdOrShortId(scope.mediaTracks.filter((t) => t.clipId === clip.id), trackId, "track");
|
|
1879
2335
|
if (!track) {
|
|
1880
|
-
throw new Error(`
|
|
2336
|
+
throw new Error(`Track "${trackId}" does not belong to clip "${clip.name}". ${scope.validUnits(scope.mediaTracks.filter((t) => t.clipId === clip.id), "track")}`);
|
|
1881
2337
|
}
|
|
1882
2338
|
if (track.status !== "ready") {
|
|
1883
|
-
throw new Error(`Track "${track.name}" is ${track.status};
|
|
1884
|
-
}
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
}
|
|
1902
|
-
case "add_reframe_keyframe": {
|
|
1903
|
-
const clip = scope.resolveClip(op.clip_id);
|
|
1904
|
-
clip.reframe ??= { mode: "auto" };
|
|
1905
|
-
const keyframe = {
|
|
1906
|
-
sourceMs: op.source_ms,
|
|
1907
|
-
x: op.x,
|
|
1908
|
-
y: op.y,
|
|
1909
|
-
zoom: op.zoom
|
|
1910
|
-
};
|
|
1911
|
-
clip.reframe = addReframeKeyframe(clip.reframe, keyframe);
|
|
1912
|
-
scope.touch(clip.id);
|
|
1913
|
-
return { ok: true, clip: scope.clipOut(clip), keyframe };
|
|
1914
|
-
}
|
|
1915
|
-
case "clear_reframe": {
|
|
1916
|
-
const clip = scope.resolveClip(op.clip_id);
|
|
1917
|
-
const next = clearReframe(clip);
|
|
1918
|
-
scope.replaceClip(clip, next);
|
|
1919
|
-
scope.touch(clip.id);
|
|
1920
|
-
return { ok: true, clip: scope.clipOut(next) };
|
|
1921
|
-
}
|
|
1922
|
-
case "retarget_format": {
|
|
1923
|
-
for (const [clipId, trackId] of Object.entries(op.track_ids ?? {})) {
|
|
1924
|
-
const clip = scope.clips.find((c) => c.id === clipId);
|
|
1925
|
-
if (!clip) {
|
|
1926
|
-
throw new Error(`track_ids names unknown clip "${clipId}". ${scope.validUnits(scope.clips, "clip")}`);
|
|
1927
|
-
}
|
|
1928
|
-
const track = scope.mediaTracks.find((t) => t.id === trackId && t.clipId === clip.id);
|
|
1929
|
-
if (!track) {
|
|
1930
|
-
throw new Error(`Track "${trackId}" does not belong to clip "${clip.name}". ${scope.validUnits(scope.mediaTracks.filter((t) => t.clipId === clip.id), "track")}`);
|
|
1931
|
-
}
|
|
1932
|
-
if (track.status !== "ready") {
|
|
1933
|
-
throw new Error(`Track "${track.name}" is ${track.status}; a format adaptation can only follow a ready track.`);
|
|
1934
|
-
}
|
|
1935
|
-
}
|
|
1936
|
-
const now = scope.ctx.now?.() ?? new Date().toISOString();
|
|
1937
|
-
const { selectedClipIds: _selected, playheadMs: _playhead, setup: _setup, ...document } = scope.state;
|
|
1938
|
-
const source = {
|
|
1939
|
-
...document,
|
|
1940
|
-
id: scope.ctx.sequence?.id ?? "seq_eval",
|
|
1941
|
-
projectId: scope.ctx.sequence?.projectId ?? "",
|
|
1942
|
-
name: scope.ctx.sequence?.name ?? "Sequence",
|
|
1943
|
-
durationMs: scope.clips.reduce((end, c) => Math.max(end, c.startMs + c.durationMs), 0),
|
|
1944
|
-
createdAt: now,
|
|
1945
|
-
updatedAt: now,
|
|
1946
|
-
camera2d: scope.state.camera2d ?? null
|
|
1947
|
-
};
|
|
1948
|
-
if (scope.state.setup) {
|
|
1949
|
-
source.setup = scope.state.setup;
|
|
1950
|
-
}
|
|
1951
|
-
const adapted = adaptSequenceFormat(source, op.aspect_ratio, {
|
|
1952
|
-
strategy: op.strategy,
|
|
1953
|
-
safeMargin: op.safe_margin,
|
|
1954
|
-
trackIdByClipId: op.track_ids
|
|
1955
|
-
});
|
|
1956
|
-
const stored = scope.ctx.retargetFormat
|
|
1957
|
-
? await scope.ctx.retargetFormat(adapted.sequence)
|
|
1958
|
-
: { sequenceId: adapted.sequence.id, name: adapted.sequence.name };
|
|
1959
|
-
return {
|
|
1960
|
-
ok: true,
|
|
1961
|
-
sourceSequenceId: source.id,
|
|
1962
|
-
sequenceId: stored.sequenceId,
|
|
1963
|
-
name: stored.name ?? adapted.sequence.name,
|
|
1964
|
-
width: adapted.sequence.width,
|
|
1965
|
-
height: adapted.sequence.height,
|
|
1966
|
-
strategy: op.strategy,
|
|
1967
|
-
croppedClipIds: adapted.croppedClipIds
|
|
1968
|
-
};
|
|
1969
|
-
}
|
|
1970
|
-
case "stagger_animations": {
|
|
1971
|
-
const clips = op.clip_ids.map((id) => scope.resolveClip(id));
|
|
1972
|
-
const ids = clips.map((c) => c.id);
|
|
1973
|
-
if (new Set(ids).size !== ids.length) {
|
|
1974
|
-
throw new Error("clip_ids must contain distinct clip IDs.");
|
|
1975
|
-
}
|
|
1976
|
-
for (const clip of clips) {
|
|
1977
|
-
if (!clip.animations?.length) {
|
|
1978
|
-
throw new Error(`Clip "${clip.name}" has no animations to stagger.`);
|
|
1979
|
-
}
|
|
1980
|
-
}
|
|
1981
|
-
scope.clips = staggerClipAnimations(scope.clips, ids, op.offset_ms);
|
|
1982
|
-
scope.touch(...ids);
|
|
1983
|
-
return {
|
|
1984
|
-
ok: true,
|
|
1985
|
-
clips: ids.map((id) => scope.clipOut(scope.resolveClip(id)))
|
|
1986
|
-
};
|
|
1987
|
-
}
|
|
1988
|
-
case "set_setup": {
|
|
1989
|
-
const previous = scope.state.setup;
|
|
1990
|
-
scope.state.setup = {
|
|
1991
|
-
...previous,
|
|
1992
|
-
stage: op.stage ?? previous?.stage ?? "idea",
|
|
1993
|
-
brief: op.brief ?? previous?.brief ?? "",
|
|
1994
|
-
format: op.format ?? previous?.format,
|
|
1995
|
-
voiceover: op.voiceover ?? previous?.voiceover,
|
|
1996
|
-
beats: previous?.beats
|
|
1997
|
-
};
|
|
1998
|
-
return { ok: true, setup: structuredClone(scope.state.setup) };
|
|
1999
|
-
}
|
|
2000
|
-
case "plan_beats": {
|
|
2001
|
-
if (!op.beats?.length) {
|
|
2002
|
-
throw new Error("This surface has no Director: pass `beats` with the plan you want. Each beat needs a prompt and a durationMs.");
|
|
2003
|
-
}
|
|
2004
|
-
const beats = op.beats.map((beat) => ({
|
|
2005
|
-
id: scope.ctx.newId("beat"),
|
|
2006
|
-
prompt: beat.prompt,
|
|
2007
|
-
duration_ms: Math.max(1, Math.round(beat.durationMs)),
|
|
2008
|
-
transition: beat.transition,
|
|
2009
|
-
voiceover: beat.voiceover,
|
|
2010
|
-
music: beat.music
|
|
2011
|
-
}));
|
|
2012
|
-
const previous = scope.state.setup;
|
|
2013
|
-
scope.state.setup = {
|
|
2014
|
-
...previous,
|
|
2015
|
-
stage: "review",
|
|
2016
|
-
brief: previous?.brief ?? "",
|
|
2017
|
-
format: previous?.format,
|
|
2018
|
-
voiceover: previous?.voiceover,
|
|
2019
|
-
beats
|
|
2020
|
-
};
|
|
2021
|
-
return {
|
|
2022
|
-
ok: true,
|
|
2023
|
-
beats: structuredClone(beats),
|
|
2024
|
-
clipsCreated: 0,
|
|
2025
|
-
jobsStarted: 0
|
|
2026
|
-
};
|
|
2339
|
+
throw new Error(`Track "${track.name}" is ${track.status}; a format adaptation can only follow a ready track.`);
|
|
2340
|
+
}
|
|
2341
|
+
trackIdByClipId[clip.id] = track.id;
|
|
2342
|
+
}
|
|
2343
|
+
const now = scope.ctx.now?.() ?? new Date().toISOString();
|
|
2344
|
+
const { selectedClipIds: _selected, playheadMs: _playhead, setup: _setup, ...document } = scope.state;
|
|
2345
|
+
const source = {
|
|
2346
|
+
...document,
|
|
2347
|
+
id: scope.ctx.sequence?.id ?? "seq_eval",
|
|
2348
|
+
projectId: scope.ctx.sequence?.projectId ?? "",
|
|
2349
|
+
name: scope.ctx.sequence?.name ?? "Sequence",
|
|
2350
|
+
durationMs: scope.clips.reduce((end, c) => Math.max(end, c.startMs + c.durationMs), 0),
|
|
2351
|
+
createdAt: now,
|
|
2352
|
+
updatedAt: now,
|
|
2353
|
+
camera2d: scope.state.camera2d ?? null
|
|
2354
|
+
};
|
|
2355
|
+
if (scope.state.setup) {
|
|
2356
|
+
source.setup = scope.state.setup;
|
|
2027
2357
|
}
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
next.voiceover = op.voiceover;
|
|
2053
|
-
}
|
|
2054
|
-
if (op.music !== undefined) {
|
|
2055
|
-
next.music = op.music;
|
|
2056
|
-
}
|
|
2057
|
-
}
|
|
2058
|
-
const previous = scope.state.setup;
|
|
2059
|
-
scope.state.setup = {
|
|
2060
|
-
...previous,
|
|
2061
|
-
stage: previous?.stage ?? "review",
|
|
2062
|
-
brief: previous?.brief ?? "",
|
|
2063
|
-
format: previous?.format,
|
|
2064
|
-
voiceover: previous?.voiceover,
|
|
2065
|
-
beats: op.op === "remove_beat"
|
|
2066
|
-
? beats.filter((b) => b.id !== found.id)
|
|
2067
|
-
: beats.map((b) => (b.id === found.id ? next : b))
|
|
2068
|
-
};
|
|
2069
|
-
return op.op === "remove_beat"
|
|
2070
|
-
? { ok: true, removed: { ...found } }
|
|
2071
|
-
: { ok: true, beat: { ...next } };
|
|
2358
|
+
const adapted = adaptSequenceFormat(source, op.aspect_ratio, {
|
|
2359
|
+
strategy: op.strategy,
|
|
2360
|
+
safeMargin: op.safe_margin,
|
|
2361
|
+
trackIdByClipId: op.track_ids ? trackIdByClipId : undefined
|
|
2362
|
+
});
|
|
2363
|
+
const stored = scope.ctx.retargetFormat
|
|
2364
|
+
? await scope.ctx.retargetFormat(adapted.sequence)
|
|
2365
|
+
: { sequenceId: adapted.sequence.id, name: adapted.sequence.name };
|
|
2366
|
+
return {
|
|
2367
|
+
ok: true,
|
|
2368
|
+
sourceSequenceId: source.id,
|
|
2369
|
+
sequenceId: stored.sequenceId,
|
|
2370
|
+
name: stored.name ?? adapted.sequence.name,
|
|
2371
|
+
width: adapted.sequence.width,
|
|
2372
|
+
height: adapted.sequence.height,
|
|
2373
|
+
strategy: op.strategy,
|
|
2374
|
+
croppedClipIds: adapted.croppedClipIds
|
|
2375
|
+
};
|
|
2376
|
+
},
|
|
2377
|
+
stagger_animations: (scope, op) => {
|
|
2378
|
+
const clips = op.clip_ids.map((id) => scope.resolveClip(id));
|
|
2379
|
+
const ids = clips.map((c) => c.id);
|
|
2380
|
+
if (new Set(ids).size !== ids.length) {
|
|
2381
|
+
throw new Error("clip_ids must contain distinct clip IDs.");
|
|
2072
2382
|
}
|
|
2073
|
-
|
|
2074
|
-
if (!
|
|
2075
|
-
throw new Error("
|
|
2076
|
-
}
|
|
2077
|
-
const outcome = await scope.ctx.generateFromBeats(scope.state, op);
|
|
2078
|
-
if (outcome.error) {
|
|
2079
|
-
throw new Error(outcome.error);
|
|
2383
|
+
for (const clip of clips) {
|
|
2384
|
+
if (!clip.animations?.length) {
|
|
2385
|
+
throw new Error(`Clip "${clip.name}" has no animations to stagger.`);
|
|
2080
2386
|
}
|
|
2081
|
-
Object.assign(scope.state, outcome.state);
|
|
2082
|
-
scope.touch(...outcome.changedClipIds);
|
|
2083
|
-
return outcome.result;
|
|
2084
|
-
}
|
|
2085
|
-
default: {
|
|
2086
|
-
const unknown = op;
|
|
2087
|
-
throw new Error(`Unknown timeline op "${unknown.op}".`);
|
|
2088
2387
|
}
|
|
2388
|
+
scope.clips = staggerClipAnimations(scope.clips, ids, op.offset_ms);
|
|
2389
|
+
scope.touch(...ids);
|
|
2390
|
+
return {
|
|
2391
|
+
ok: true,
|
|
2392
|
+
clips: ids.map((id) => scope.clipOut(scope.resolveClip(id)))
|
|
2393
|
+
};
|
|
2394
|
+
},
|
|
2395
|
+
set_setup: (scope, op) => {
|
|
2396
|
+
const previous = scope.state.setup;
|
|
2397
|
+
scope.state.setup = {
|
|
2398
|
+
...previous,
|
|
2399
|
+
stage: op.stage ?? previous?.stage ?? "idea",
|
|
2400
|
+
brief: op.brief ?? previous?.brief ?? "",
|
|
2401
|
+
format: op.format ?? previous?.format,
|
|
2402
|
+
voiceover: op.voiceover ?? previous?.voiceover,
|
|
2403
|
+
beats: previous?.beats
|
|
2404
|
+
};
|
|
2405
|
+
return { ok: true, setup: structuredClone(scope.state.setup) };
|
|
2406
|
+
},
|
|
2407
|
+
plan_beats: (scope, op) => {
|
|
2408
|
+
if (!op.beats?.length) {
|
|
2409
|
+
throw new Error("This surface has no Director: pass `beats` with the plan you want. Each beat needs a prompt and a durationMs.");
|
|
2410
|
+
}
|
|
2411
|
+
const beats = op.beats.map((beat) => ({
|
|
2412
|
+
id: scope.ctx.newId("beat"),
|
|
2413
|
+
prompt: beat.prompt,
|
|
2414
|
+
duration_ms: Math.max(1, Math.round(beat.durationMs)),
|
|
2415
|
+
transition: beat.transition,
|
|
2416
|
+
voiceover: beat.voiceover,
|
|
2417
|
+
music: beat.music
|
|
2418
|
+
}));
|
|
2419
|
+
const previous = scope.state.setup;
|
|
2420
|
+
scope.state.setup = {
|
|
2421
|
+
...previous,
|
|
2422
|
+
stage: "review",
|
|
2423
|
+
brief: previous?.brief ?? "",
|
|
2424
|
+
format: previous?.format,
|
|
2425
|
+
voiceover: previous?.voiceover,
|
|
2426
|
+
beats
|
|
2427
|
+
};
|
|
2428
|
+
return {
|
|
2429
|
+
ok: true,
|
|
2430
|
+
beats: structuredClone(beats),
|
|
2431
|
+
clipsCreated: 0,
|
|
2432
|
+
jobsStarted: 0
|
|
2433
|
+
};
|
|
2434
|
+
},
|
|
2435
|
+
update_beat: runBeatEdit,
|
|
2436
|
+
remove_beat: runBeatEdit,
|
|
2437
|
+
generate_from_beats: async (scope, op) => {
|
|
2438
|
+
if (!scope.ctx.generateFromBeats) {
|
|
2439
|
+
throw new Error("This surface cannot generate from beats: no generation host is wired.");
|
|
2440
|
+
}
|
|
2441
|
+
const outcome = await scope.ctx.generateFromBeats(scope.state, op);
|
|
2442
|
+
if (outcome.error) {
|
|
2443
|
+
throw new Error(outcome.error);
|
|
2444
|
+
}
|
|
2445
|
+
Object.assign(scope.state, outcome.state);
|
|
2446
|
+
scope.touch(...outcome.changedClipIds);
|
|
2447
|
+
return outcome.result;
|
|
2089
2448
|
}
|
|
2449
|
+
};
|
|
2450
|
+
/** Dispatch one op against `scope`. Throws on refusal; the caller maps it. */
|
|
2451
|
+
async function runOp(scope, op) {
|
|
2452
|
+
if (!Object.hasOwn(OP_HANDLERS, op.op)) {
|
|
2453
|
+
throw new Error(`Unknown timeline op "${op.op}".`);
|
|
2454
|
+
}
|
|
2455
|
+
const handler = OP_HANDLERS[op.op];
|
|
2456
|
+
return handler(scope, op);
|
|
2090
2457
|
}
|
|
2091
2458
|
/** Deep copy of the document, so a failed op leaves the caller's state alone. */
|
|
2092
2459
|
function cloneState(state) {
|
|
@@ -2180,8 +2547,14 @@ function runTrackOp(scope, op) {
|
|
|
2180
2547
|
case "delete_track": {
|
|
2181
2548
|
const { target, deleteClips } = resolveDeleteTrackArgs(op);
|
|
2182
2549
|
const track = scope.resolveTrack(target);
|
|
2550
|
+
// The editor's removeTrack refuses a locked track, and a locked clip is
|
|
2551
|
+
// not deleted by deleting the track under it.
|
|
2552
|
+
scope.assertWritable([], track.id);
|
|
2183
2553
|
const onIt = scope.clips.filter((c) => c.trackId === track.id);
|
|
2184
|
-
if (
|
|
2554
|
+
if (deleteClips) {
|
|
2555
|
+
scope.assertWritable(onIt);
|
|
2556
|
+
}
|
|
2557
|
+
else if (onIt.length > 0) {
|
|
2185
2558
|
throw new Error(`Track "${track.name}" still holds ${onIt.length} clip(s): ` +
|
|
2186
2559
|
`${onIt.map((c) => c.id).join(", ")}. Move them first, or pass ` +
|
|
2187
2560
|
"deleteClips: true to delete them with the track.");
|
|
@@ -2200,6 +2573,29 @@ function runTrackOp(scope, op) {
|
|
|
2200
2573
|
scope.touch(clip.id);
|
|
2201
2574
|
}
|
|
2202
2575
|
}
|
|
2576
|
+
// A link left with one member is no link, the rule delete_clip applies.
|
|
2577
|
+
const removedLinks = new Set(onIt.flatMap((clip) => (clip.linkId ? [clip.linkId] : [])));
|
|
2578
|
+
for (const linkId of removedLinks) {
|
|
2579
|
+
const remainingLinked = scope.clips.filter((c) => c.linkId === linkId);
|
|
2580
|
+
if (remainingLinked.length === 1) {
|
|
2581
|
+
const [orphan] = remainingLinked;
|
|
2582
|
+
delete orphan.linkId;
|
|
2583
|
+
scope.touch(orphan.id);
|
|
2584
|
+
}
|
|
2585
|
+
}
|
|
2586
|
+
// Subject tracks belong to a clip; they go with it, and a clip bound to
|
|
2587
|
+
// one of them is released the way delete_track_object releases it.
|
|
2588
|
+
const droppedMediaTracks = new Set(scope.mediaTracks.filter((t) => removed.has(t.clipId)).map((t) => t.id));
|
|
2589
|
+
if (droppedMediaTracks.size > 0) {
|
|
2590
|
+
scope.mediaTracks = scope.mediaTracks.filter((t) => !droppedMediaTracks.has(t.id));
|
|
2591
|
+
}
|
|
2592
|
+
for (const clip of scope.clips) {
|
|
2593
|
+
if (clip.trackBinding &&
|
|
2594
|
+
droppedMediaTracks.has(clip.trackBinding.trackId)) {
|
|
2595
|
+
delete clip.trackBinding;
|
|
2596
|
+
scope.touch(clip.id);
|
|
2597
|
+
}
|
|
2598
|
+
}
|
|
2203
2599
|
scope.touch(...removedClipIds);
|
|
2204
2600
|
state.selectedClipIds = state.selectedClipIds.filter((id) => !removed.has(id));
|
|
2205
2601
|
const remaining = scope.tracks.filter((t) => t.id !== track.id);
|