@waveform-playlist/engine 14.0.0-next.3 → 14.0.0-next.5

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,
@@ -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);
@@ -1246,6 +1298,21 @@ var ArrangementImpl = class {
1246
1298
  this._tracksVersion++;
1247
1299
  this._emit({ type: "remove", trackId });
1248
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
+ }
1249
1316
  /**
1250
1317
  * Move a track to a new position in the track order. Purely organizational —
1251
1318
  * track order is not audible.
@@ -1527,6 +1594,29 @@ var ArrangementImpl = class {
1527
1594
  this._resyncSourcesQuietly();
1528
1595
  }
1529
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
+ }
1530
1620
  /** The state before a change: an undo step, or the mark that an open transaction changed. */
1531
1621
  _pushUndoSnapshot() {
1532
1622
  if (this._transaction) {
@@ -1687,7 +1777,7 @@ function createArrangement(options = {}) {
1687
1777
  }
1688
1778
 
1689
1779
  // src/adapterSync.ts
1690
- import { isLoadingClip as isLoadingClip6 } from "@waveform-playlist/core";
1780
+ import { isLoadingClip as isLoadingClip6, TRACK_VIEW_FIELDS } from "@waveform-playlist/core";
1691
1781
  var AdapterSync = class {
1692
1782
  constructor(_arrangement, _adapter) {
1693
1783
  this._arrangement = _arrangement;
@@ -1733,6 +1823,14 @@ var AdapterSync = class {
1733
1823
  this._projection = { tracksVersion, mixerVersion, tracks };
1734
1824
  return tracks;
1735
1825
  }
1826
+ /**
1827
+ * `tracks()` as the adapter is given them: without the view fields. Made
1828
+ * for each send; nothing depends on the adapter's tracks keeping their
1829
+ * identity between sends.
1830
+ */
1831
+ _playoutTracks() {
1832
+ return freezeTracksInDevelopment(this.tracks().map((track) => this._playout(track)));
1833
+ }
1736
1834
  /** When an engine attaches: send the tracks that are there. With none, the adapter is in step as it is. */
1737
1835
  sendInitialTracks() {
1738
1836
  if (this._arrangement.getTracks().length > 0) {
@@ -1744,7 +1842,7 @@ var AdapterSync = class {
1744
1842
  /** Send the adapter every track, and remember up to which change that covers. */
1745
1843
  sendAllTracks() {
1746
1844
  const version = this._arrangement.tracksVersion;
1747
- this._adapter?.setTracks(this.tracks());
1845
+ this._adapter?.setTracks(this._playoutTracks());
1748
1846
  this._syncedTracksVersion = version;
1749
1847
  }
1750
1848
  /** Send the arrangement's current track to the adapter. False when there is no such track. */
@@ -1752,7 +1850,7 @@ var AdapterSync = class {
1752
1850
  const track = this._arrangement.getTracks().find((t) => t.id === trackId);
1753
1851
  if (!track) return false;
1754
1852
  if (this._adapter?.updateTrack) {
1755
- this._adapter.updateTrack(trackId, this._project(track));
1853
+ this._adapter.updateTrack(trackId, this._playout(this._project(track)));
1756
1854
  } else {
1757
1855
  this.sendAllTracks();
1758
1856
  }
@@ -1853,7 +1951,7 @@ var AdapterSync = class {
1853
1951
  _addTrack(trackId) {
1854
1952
  const track = this._arrangement.getTracks().find((t) => t.id === trackId);
1855
1953
  if (track && this._adapter?.addTrack) {
1856
- this._adapter.addTrack(this._project(track));
1954
+ this._adapter.addTrack(this._playout(this._project(track)));
1857
1955
  } else {
1858
1956
  this.sendAllTracks();
1859
1957
  }
@@ -1893,9 +1991,10 @@ var AdapterSync = class {
1893
1991
  if (before.clips.some((clip, i) => clip !== now.clips[i])) return false;
1894
1992
  const keys = /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(now)]);
1895
1993
  keys.delete("clips");
1994
+ for (const field of TRACK_VIEW_FIELDS) keys.delete(field);
1896
1995
  return [...keys].every((key) => Object.is(before[key], now[key]));
1897
1996
  }
1898
- /** What an adapter and `getState()` are given for one of the arrangement's tracks. */
1997
+ /** What `getState()` is given for one of the arrangement's tracks (an adapter: `_playout` of it). */
1899
1998
  _project(track) {
1900
1999
  return this._withMix(this._loaded(track));
1901
2000
  }
@@ -1920,6 +2019,12 @@ var AdapterSync = class {
1920
2019
  _recordingNow(trackId) {
1921
2020
  return this._arrangement.recordingTrackIds.has(trackId) && this._arrangement.getMixer().has(trackId);
1922
2021
  }
2022
+ /** A projected track without its view fields: a copy that shares its clips. */
2023
+ _playout(track) {
2024
+ const copy = { ...track };
2025
+ for (const field of TRACK_VIEW_FIELDS) delete copy[field];
2026
+ return freezeTrackInDevelopment(copy);
2027
+ }
1923
2028
  /**
1924
2029
  * One track with its current mixer values in its fields, muted while a
1925
2030
  * take records onto it (memoized per track object).
@@ -2398,8 +2503,9 @@ var PlaylistEngine = class {
2398
2503
  // ---------------------------------------------------------------------------
2399
2504
  // Lifecycle
2400
2505
  // ---------------------------------------------------------------------------
2401
- dispose() {
2506
+ dispose(options = {}) {
2402
2507
  if (this._disposed) return;
2508
+ if (options.keepAdapter && this._isPlaying) this.stop();
2403
2509
  this._disposed = true;
2404
2510
  this._stopFollowingArrangement();
2405
2511
  this._detachFromArrangement();
@@ -2408,7 +2514,8 @@ var PlaylistEngine = class {
2408
2514
  this._isPlaying = false;
2409
2515
  this._adapter?.onPlaybackEnded?.(null);
2410
2516
  try {
2411
- this._adapter?.dispose();
2517
+ if (options.keepAdapter) this._adapter?.setTracks([]);
2518
+ else this._adapter?.dispose();
2412
2519
  } catch (err) {
2413
2520
  console.warn("[waveform-playlist/engine] Error disposing adapter:", err);
2414
2521
  }
@@ -2544,6 +2651,7 @@ export {
2544
2651
  defaultLoadSource,
2545
2652
  findClosestZoomIndex,
2546
2653
  getVisibleChunkIndices,
2654
+ isArrangement,
2547
2655
  shouldUpdateViewport,
2548
2656
  splitClip
2549
2657
  };