@waveform-playlist/engine 14.0.0-next.2 → 14.0.0-next.4

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/dist/index.mjs CHANGED
@@ -46,20 +46,23 @@ function splitClip(clip, splitSample) {
46
46
  const rightDuration = clip.durationSamples - leftDuration;
47
47
  const leftName = clip.name ? `${clip.name} (1)` : void 0;
48
48
  const rightName = clip.name ? `${clip.name} (2)` : void 0;
49
- const left = createClip({
50
- startSample: clip.startSample,
51
- durationSamples: leftDuration,
52
- offsetSamples: clip.offsetSamples,
53
- sampleRate: clip.sampleRate,
54
- sourceDurationSamples: clip.sourceDurationSamples,
55
- gain: clip.gain,
56
- name: leftName,
57
- color: clip.color,
58
- fadeIn: clip.fadeIn,
59
- audioBuffer: clip.audioBuffer,
60
- waveformData: clip.waveformData,
61
- src: clip.src
62
- });
49
+ const left = {
50
+ ...createClip({
51
+ startSample: clip.startSample,
52
+ durationSamples: leftDuration,
53
+ offsetSamples: clip.offsetSamples,
54
+ sampleRate: clip.sampleRate,
55
+ sourceDurationSamples: clip.sourceDurationSamples,
56
+ gain: clip.gain,
57
+ name: leftName,
58
+ color: clip.color,
59
+ fadeIn: clip.fadeIn,
60
+ audioBuffer: clip.audioBuffer,
61
+ waveformData: clip.waveformData,
62
+ src: clip.src
63
+ }),
64
+ id: clip.id
65
+ };
63
66
  const right = createClip({
64
67
  startSample: splitSample,
65
68
  durationSamples: rightDuration,
@@ -153,10 +156,10 @@ import { isLoadingClip as isLoadingClip5 } from "@waveform-playlist/core";
153
156
 
154
157
  // src/arrangement/attach.ts
155
158
  var ARRANGEMENT_ATTACH_DECODE = /* @__PURE__ */ Symbol.for(
156
- "@waveform-playlist/engine/arrangement/attach-decode/v2"
159
+ "@waveform-playlist/engine/arrangement/attach-decode/v3"
157
160
  );
158
161
  var ARRANGEMENT_ATTACH_TIME_BASE = /* @__PURE__ */ Symbol.for(
159
- "@waveform-playlist/engine/arrangement/time-base/v2"
162
+ "@waveform-playlist/engine/arrangement/time-base/v3"
160
163
  );
161
164
  function attachPoints(value) {
162
165
  return typeof value === "object" && value !== null ? value : {};
@@ -863,7 +866,56 @@ function mapHistoryStates(history, map) {
863
866
  return { undo: history.undo.map(map), redo: history.redo.map(map) };
864
867
  }
865
868
  function purgeTrack(history, current, trackId, base = null) {
866
- const strip = stateWithoutTrack(trackId);
869
+ return purgeStates(history, current, stateWithoutTrack(trackId), base);
870
+ }
871
+ function purgeClip(history, current, trackId, clipId, base = null) {
872
+ return purgeStates(
873
+ history,
874
+ current,
875
+ stateWithoutClip(history, current, trackId, clipId, base),
876
+ base
877
+ );
878
+ }
879
+ function stateWithoutClip(history, current, trackId, clipId, base = null) {
880
+ const inOrder = [
881
+ ...history.undo,
882
+ ...base ? [base] : [],
883
+ current,
884
+ ...[...history.redo].reverse()
885
+ ];
886
+ const mapped = /* @__PURE__ */ new Map();
887
+ let before;
888
+ let stripped = false;
889
+ for (const state of inOrder) {
890
+ const known = mapped.get(state);
891
+ if (known) {
892
+ before = known.tracks.find((t) => t.id === trackId);
893
+ continue;
894
+ }
895
+ const track = state.tracks.find((t) => t.id === trackId);
896
+ let next = track;
897
+ if (track?.clips.some((c) => c.id === clipId)) {
898
+ next = { ...track, clips: track.clips.filter((c) => c.id !== clipId) };
899
+ stripped = true;
900
+ }
901
+ if (stripped && next && before && next !== before && sameTrackFields(before, next))
902
+ next = before;
903
+ mapped.set(
904
+ state,
905
+ next === track ? state : { tracks: state.tracks.map((t) => t === track ? next : t), mixer: state.mixer }
906
+ );
907
+ before = next;
908
+ }
909
+ return (state) => mapped.get(state) ?? state;
910
+ }
911
+ function sameTrackFields(a, b) {
912
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
913
+ for (const key of keys) {
914
+ if (key !== "clips" && a[key] !== b[key]) return false;
915
+ }
916
+ return a.clips.length === b.clips.length && a.clips.every((c, i) => c === b.clips[i]);
917
+ }
918
+ function purgeStates(history, current, strip, base) {
867
919
  const nextCurrent = strip(current);
868
920
  const nextBase = base && strip(base);
869
921
  const stripped = dropRepeats(mapHistoryStates(history, strip), nextBase ?? nextCurrent);
@@ -978,6 +1030,15 @@ function duplicateTrackId(trackId) {
978
1030
  var DEFAULT_MIN_DURATION_SECONDS = 0.1;
979
1031
  var DEFAULT_UNDO_LIMIT = 100;
980
1032
  var EMPTY_RECORDING = /* @__PURE__ */ new Set();
1033
+ function insertIndex(index, length) {
1034
+ if (index === void 0) return length;
1035
+ if (typeof index !== "number" || !Number.isFinite(index)) {
1036
+ throw new TypeError(
1037
+ `[waveform-playlist] addTrack: index must be a finite number, got ${String(index)}`
1038
+ );
1039
+ }
1040
+ return Math.max(0, Math.min(Math.trunc(index), length));
1041
+ }
981
1042
  var ArrangementImpl = class {
982
1043
  constructor(options = {}) {
983
1044
  this._tracks = [];
@@ -1208,13 +1269,14 @@ var ArrangementImpl = class {
1208
1269
  this._tracksVersion++;
1209
1270
  this._emit({ type: "set" });
1210
1271
  }
1211
- addTrack(track) {
1272
+ addTrack(track, options) {
1273
+ const index = insertIndex(options?.index, this._tracks.length);
1212
1274
  assertClipsOfTrackKind(track);
1213
1275
  assertUniqueClipIds(track);
1214
1276
  if (this._tracks.some((t) => t.id === track.id)) throw duplicateTrackId(track.id);
1215
1277
  this._pushUndoSnapshot();
1216
1278
  const enriched = this._ingest(track);
1217
- this._tracks = [...this._tracks, enriched];
1279
+ this._tracks = [...this._tracks.slice(0, index), enriched, ...this._tracks.slice(index)];
1218
1280
  this._mixer = seedMixer(this._mixer, [enriched]);
1219
1281
  this._tracksVersion++;
1220
1282
  this._emit({ type: "add", trackId: track.id });
@@ -1236,6 +1298,21 @@ var ArrangementImpl = class {
1236
1298
  this._tracksVersion++;
1237
1299
  this._emit({ type: "remove", trackId });
1238
1300
  }
1301
+ /**
1302
+ * Remove a clip from a track: an undo step. With `undoable: false` there is
1303
+ * no step, and the clip is also taken out of every undo and redo state, so
1304
+ * that no undo or redo brings it back (#848: a `<daw-clip>` the page removed
1305
+ * while its track was detached).
1306
+ */
1307
+ removeClip(trackId, clipId, options) {
1308
+ if (options?.undoable === false) {
1309
+ this._removeClipEverywhere(trackId, clipId);
1310
+ return;
1311
+ }
1312
+ const current = this._tracks.find((t) => t.id === trackId);
1313
+ if (!current?.clips.some((c) => c.id === clipId)) return;
1314
+ this.updateTrack(trackId, (t) => ({ ...t, clips: t.clips.filter((c) => c.id !== clipId) }));
1315
+ }
1239
1316
  /**
1240
1317
  * Move a track to a new position in the track order. Purely organizational —
1241
1318
  * track order is not audible.
@@ -1517,6 +1594,29 @@ var ArrangementImpl = class {
1517
1594
  this._resyncSourcesQuietly();
1518
1595
  }
1519
1596
  }
1597
+ /** `removeClip` with `undoable: false`: out of the current tracks and out of every stored state. */
1598
+ _removeClipEverywhere(trackId, clipId) {
1599
+ const previous = this._tracks.find((t) => t.id === trackId);
1600
+ const base = this._transaction?.state ?? null;
1601
+ const purged = purgeClip(this._history, this._currentState(), trackId, clipId, base);
1602
+ const historyChanged = purged.history !== this._history;
1603
+ const baseChanged = purged.base !== base;
1604
+ this._history = purged.history;
1605
+ if (this._transaction && purged.base) {
1606
+ this._transaction = { ...this._transaction, state: purged.base };
1607
+ }
1608
+ if (purged.current.tracks !== this._tracks && previous) {
1609
+ this._gesture = null;
1610
+ this._tracks = purged.current.tracks;
1611
+ this._tracksVersion++;
1612
+ this._emit({ type: "update", trackId, previous });
1613
+ } else if (historyChanged) {
1614
+ this._gesture = null;
1615
+ this._emit({ type: "history" });
1616
+ } else if (baseChanged) {
1617
+ this._resyncSourcesQuietly();
1618
+ }
1619
+ }
1520
1620
  /** The state before a change: an undo step, or the mark that an open transaction changed. */
1521
1621
  _pushUndoSnapshot() {
1522
1622
  if (this._transaction) {
@@ -2071,8 +2171,8 @@ var PlaylistEngine = class {
2071
2171
  setTracks(tracks) {
2072
2172
  this._arrangement.setTracks(tracks);
2073
2173
  }
2074
- addTrack(track) {
2075
- this._arrangement.addTrack(track);
2174
+ addTrack(track, options) {
2175
+ this._arrangement.addTrack(track, options);
2076
2176
  }
2077
2177
  removeTrack(trackId) {
2078
2178
  this._arrangement.removeTrack(trackId);
@@ -2388,8 +2488,9 @@ var PlaylistEngine = class {
2388
2488
  // ---------------------------------------------------------------------------
2389
2489
  // Lifecycle
2390
2490
  // ---------------------------------------------------------------------------
2391
- dispose() {
2491
+ dispose(options = {}) {
2392
2492
  if (this._disposed) return;
2493
+ if (options.keepAdapter && this._isPlaying) this.stop();
2393
2494
  this._disposed = true;
2394
2495
  this._stopFollowingArrangement();
2395
2496
  this._detachFromArrangement();
@@ -2398,7 +2499,8 @@ var PlaylistEngine = class {
2398
2499
  this._isPlaying = false;
2399
2500
  this._adapter?.onPlaybackEnded?.(null);
2400
2501
  try {
2401
- this._adapter?.dispose();
2502
+ if (options.keepAdapter) this._adapter?.setTracks([]);
2503
+ else this._adapter?.dispose();
2402
2504
  } catch (err) {
2403
2505
  console.warn("[waveform-playlist/engine] Error disposing adapter:", err);
2404
2506
  }
@@ -2534,6 +2636,7 @@ export {
2534
2636
  defaultLoadSource,
2535
2637
  findClosestZoomIndex,
2536
2638
  getVisibleChunkIndices,
2639
+ isArrangement,
2537
2640
  shouldUpdateViewport,
2538
2641
  splitClip
2539
2642
  };