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

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
@@ -152,7 +152,7 @@ function clampSeekPosition(time, duration) {
152
152
  }
153
153
 
154
154
  // src/arrangement/Arrangement.ts
155
- import { isLoadingClip as isLoadingClip5 } from "@waveform-playlist/core";
155
+ import { isDecidedPanLaw as isDecidedPanLaw2, isLoadingClip as isLoadingClip6, trackKind as trackKind2 } from "@waveform-playlist/core";
156
156
 
157
157
  // src/arrangement/attach.ts
158
158
  var ARRANGEMENT_ATTACH_DECODE = /* @__PURE__ */ Symbol.for(
@@ -706,6 +706,13 @@ function sameValue(a, b) {
706
706
  }
707
707
 
708
708
  // src/arrangement/mixer.ts
709
+ import {
710
+ DEFAULT_PAN_LAW,
711
+ audioPanLaw,
712
+ isDecidedPanLaw,
713
+ isLoadingClip as isLoadingClip3,
714
+ trackKind
715
+ } from "@waveform-playlist/core";
709
716
  var EMPTY_MIXER = /* @__PURE__ */ new Map();
710
717
  var DEFAULT_VOLUME = 1;
711
718
  var DEFAULT_PAN = 0;
@@ -714,9 +721,33 @@ function mixerValuesOf(track) {
714
721
  volume: track.volume ?? DEFAULT_VOLUME,
715
722
  muted: track.muted ?? false,
716
723
  soloed: track.soloed ?? false,
717
- pan: track.pan ?? DEFAULT_PAN
724
+ pan: track.pan ?? DEFAULT_PAN,
725
+ panLaw: enteringPanLaw(track)
718
726
  };
719
727
  }
728
+ function decidedByAudio(track) {
729
+ if (trackKind(track) === "midi") return null;
730
+ return audioPanLaw({
731
+ clips: track.clips.filter((clip) => !isLoadingClip3(clip))
732
+ });
733
+ }
734
+ function enteringPanLaw(track) {
735
+ if (trackKind(track) === "midi") return DEFAULT_PAN_LAW;
736
+ if (isDecidedPanLaw(track.panLaw)) return track.panLaw;
737
+ return decidedByAudio(track) ?? DEFAULT_PAN_LAW;
738
+ }
739
+ function decidePanLaws(mixer, tracks) {
740
+ let next = null;
741
+ for (const track of tracks) {
742
+ const entry = mixer.get(track.id);
743
+ if (!entry || entry.panLaw !== DEFAULT_PAN_LAW) continue;
744
+ const panLaw = decidedByAudio(track);
745
+ if (!panLaw) continue;
746
+ if (!next) next = new Map(mixer);
747
+ next.set(track.id, { ...entry, panLaw });
748
+ }
749
+ return next ?? mixer;
750
+ }
720
751
  function seedMixer(mixer, tracks) {
721
752
  let next = null;
722
753
  for (const track of tracks) {
@@ -753,7 +784,7 @@ function withoutEntry(mixer, trackId) {
753
784
  return next;
754
785
  }
755
786
  function sameValues(a, b) {
756
- return a.volume === b.volume && a.muted === b.muted && a.soloed === b.soloed && a.pan === b.pan;
787
+ return a.volume === b.volume && a.muted === b.muted && a.soloed === b.soloed && a.pan === b.pan && a.panLaw === b.panLaw;
757
788
  }
758
789
  function sameMixerValues(a, b) {
759
790
  if (a === b) return true;
@@ -775,12 +806,14 @@ function mixerChanges(before, after) {
775
806
  if (was.muted !== now.muted) values.muted = now.muted;
776
807
  if (was.soloed !== now.soloed) values.soloed = now.soloed;
777
808
  if (was.pan !== now.pan) values.pan = now.pan;
809
+ if (was.panLaw !== now.panLaw) values.panLaw = now.panLaw;
778
810
  if (Object.keys(values).length > 0) changes.push({ trackId, values });
779
811
  }
780
812
  return changes;
781
813
  }
782
814
  function withMixerValues(track, values) {
783
- if (!values || track.volume === values.volume && track.muted === values.muted && track.soloed === values.soloed && track.pan === values.pan) {
815
+ if (!values || track.volume === values.volume && track.muted === values.muted && track.soloed === values.soloed && track.pan === values.pan && // A track that carries no law is an 'auto' track: no copy for that alone.
816
+ (track.panLaw ?? DEFAULT_PAN_LAW) === values.panLaw) {
784
817
  return track;
785
818
  }
786
819
  return { ...track, ...values };
@@ -941,6 +974,27 @@ function stateWithMixerValues(trackId, patch) {
941
974
  return next;
942
975
  };
943
976
  }
977
+ function stateWithDecidedPanLaws() {
978
+ const decided = /* @__PURE__ */ new Map();
979
+ const states = /* @__PURE__ */ new Map();
980
+ return (state) => {
981
+ const known = states.get(state);
982
+ if (known) return known;
983
+ let byTracks = decided.get(state.mixer);
984
+ if (!byTracks) {
985
+ byTracks = /* @__PURE__ */ new Map();
986
+ decided.set(state.mixer, byTracks);
987
+ }
988
+ let mixer = byTracks.get(state.tracks);
989
+ if (!mixer) {
990
+ mixer = decidePanLaws(state.mixer, state.tracks);
991
+ byTracks.set(state.tracks, mixer);
992
+ }
993
+ const next = mixer === state.mixer ? state : { ...state, mixer };
994
+ states.set(state, next);
995
+ return next;
996
+ };
997
+ }
944
998
  function writeThrough(history, base, current, write) {
945
999
  const nextBase = base && { ...base, state: write(base.state) };
946
1000
  return {
@@ -950,9 +1004,9 @@ function writeThrough(history, base, current, write) {
950
1004
  }
951
1005
 
952
1006
  // src/arrangement/ticks.ts
953
- import { isLoadingClip as isLoadingClip3 } from "@waveform-playlist/core";
1007
+ import { isLoadingClip as isLoadingClip4 } from "@waveform-playlist/core";
954
1008
  function forLoadedClips(change) {
955
- return (clip) => isLoadingClip3(clip) ? clip : change(clip);
1009
+ return (clip) => isLoadingClip4(clip) ? clip : change(clip);
956
1010
  }
957
1011
  function withClips(track, change) {
958
1012
  let changed = false;
@@ -995,10 +1049,10 @@ function rescaleTick(tick, fromPpqn, toPpqn) {
995
1049
  }
996
1050
 
997
1051
  // src/arrangement/validate.ts
998
- import { assertTrackKind, isLoadingClip as isLoadingClip4 } from "@waveform-playlist/core";
1052
+ import { assertTrackKind, isLoadingClip as isLoadingClip5 } from "@waveform-playlist/core";
999
1053
  function assertClipsOfTrackKind(track) {
1000
1054
  const clips = track.clips.map(
1001
- (clip) => isLoadingClip4(clip) ? { id: clip.id, name: clip.name } : clip
1055
+ (clip) => isLoadingClip5(clip) ? { id: clip.id, name: clip.name } : clip
1002
1056
  );
1003
1057
  assertTrackKind({ ...track, clips });
1004
1058
  }
@@ -1409,6 +1463,23 @@ var ArrangementImpl = class {
1409
1463
  setTrackPan(trackId, pan, options) {
1410
1464
  this._setMixerValues(trackId, { pan }, options);
1411
1465
  }
1466
+ /**
1467
+ * Set a track's pan law (#684): a mixer value like the four above. It
1468
+ * takes a decided law; anything else, `'auto'` included, throws before
1469
+ * anything changes (a decided track does not go back to `'auto'`). May be
1470
+ * called before the track holds audio: the law is then decided, and the
1471
+ * first audio does not change it. A MIDI track has no law to set.
1472
+ */
1473
+ setTrackPanLaw(trackId, panLaw, options) {
1474
+ if (!isDecidedPanLaw2(panLaw)) {
1475
+ throw new TypeError(
1476
+ `setTrackPanLaw: panLaw must be 'mono' or 'stereo', got ${JSON.stringify(panLaw) ?? String(panLaw)}`
1477
+ );
1478
+ }
1479
+ const track = this._tracks.find((t) => t.id === trackId);
1480
+ if (!track || trackKind2(track) === "midi") return;
1481
+ this._setMixerValues(trackId, { panLaw }, options);
1482
+ }
1412
1483
  // ---------------------------------------------------------------------------
1413
1484
  // Recording mute (the track a take is recording onto)
1414
1485
  // ---------------------------------------------------------------------------
@@ -1489,7 +1560,7 @@ var ArrangementImpl = class {
1489
1560
  ...track,
1490
1561
  clips: track.clips.map((clip) => {
1491
1562
  if (held?.has(clip)) return clip;
1492
- if (isLoadingClip5(clip)) return { ...clip };
1563
+ if (isLoadingClip6(clip)) return { ...clip };
1493
1564
  return { ...clip, startTick: clip.startTick ?? this._tickAt(clip.startSample) };
1494
1565
  })
1495
1566
  };
@@ -1706,8 +1777,58 @@ var ArrangementImpl = class {
1706
1777
  state: withStateTracks(state, pass.stored(state.tracks))
1707
1778
  };
1708
1779
  }
1780
+ this._decidePanLaws();
1709
1781
  return this._tracks === previous ? null : previous;
1710
1782
  }
1783
+ /**
1784
+ * First audio decides a track's pan law (#684): in the current state and
1785
+ * in every state the arrangement can return to, a track whose law is
1786
+ * `'auto'` and that holds decoded audio gets the law of that audio, each
1787
+ * state from its own tracks. Part of the change that brought the audio:
1788
+ * not an undo step. `mixerVersion` counts it when the current mix changed.
1789
+ *
1790
+ * A saved step that no longer differs from the state next to it is
1791
+ * dropped, as `_writeMixerThrough` does (a law set on a track whose only
1792
+ * clip was loading, and the clip turns out to have that law): its undo, or
1793
+ * its redo, would do nothing. A gesture whose own step went has no step to
1794
+ * join, and its next change pushes a new one; a gesture whose step is still
1795
+ * the top one goes on.
1796
+ */
1797
+ _decidePanLaws() {
1798
+ const decide = stateWithDecidedPanLaws();
1799
+ const current = this._currentState();
1800
+ const decided = decide(current);
1801
+ if (decided !== current) {
1802
+ this._mixer = decided.mixer;
1803
+ this._mixerVersion++;
1804
+ }
1805
+ const changedIn = (before, after) => after.undo.some((state, i) => state !== before.undo[i]) || after.redo.some((state, i) => state !== before.redo[i]);
1806
+ let base = this._transaction?.state ?? null;
1807
+ if (this._transaction && base) {
1808
+ const decidedBase = decide(base);
1809
+ if (decidedBase !== base) {
1810
+ base = decidedBase;
1811
+ this._transaction = { ...this._transaction, state: decidedBase };
1812
+ }
1813
+ }
1814
+ const mapped = mapHistoryStates(this._history, decide);
1815
+ const savedChanged = changedIn(this._history, mapped);
1816
+ if (savedChanged || decided !== current) {
1817
+ const top = mapped.undo[mapped.undo.length - 1];
1818
+ const kept = dropRepeats(mapped, base ?? decided);
1819
+ if (savedChanged || kept !== mapped) this._history = kept;
1820
+ if (this._gesture && kept !== mapped) {
1821
+ const newTop = kept.undo[kept.undo.length - 1];
1822
+ if (!(newTop !== void 0 && top !== void 0 && sameState(newTop, top))) {
1823
+ this._gesture = null;
1824
+ }
1825
+ }
1826
+ }
1827
+ if (this._gesture) {
1828
+ const before = mapHistoryStates(this._gesture.before, decide);
1829
+ if (changedIn(this._gesture.before, before)) this._gesture = { ...this._gesture, before };
1830
+ }
1831
+ }
1711
1832
  /** A load finished or failed by itself (not inside one of this arrangement's methods). */
1712
1833
  _onSourceSettled() {
1713
1834
  const previous = this._switchLoaded();
@@ -1777,7 +1898,7 @@ function createArrangement(options = {}) {
1777
1898
  }
1778
1899
 
1779
1900
  // src/adapterSync.ts
1780
- import { isLoadingClip as isLoadingClip6, TRACK_VIEW_FIELDS } from "@waveform-playlist/core";
1901
+ import { isLoadingClip as isLoadingClip7, TRACK_VIEW_FIELDS } from "@waveform-playlist/core";
1781
1902
  var AdapterSync = class {
1782
1903
  constructor(_arrangement, _adapter) {
1783
1904
  this._arrangement = _arrangement;
@@ -1868,6 +1989,13 @@ var AdapterSync = class {
1868
1989
  }
1869
1990
  if (values.soloed !== void 0) this._adapter?.setTrackSolo(trackId, values.soloed);
1870
1991
  if (values.pan !== void 0) this._adapter?.setTrackPan(trackId, values.pan);
1992
+ if (values.panLaw !== void 0) this._sendPanLaw(trackId, values.panLaw);
1993
+ }
1994
+ /** The adapter's setter for a pan law, or, for an adapter without one, the track again (#684). */
1995
+ _sendPanLaw(trackId, panLaw) {
1996
+ if (!this._adapter) return;
1997
+ if (this._adapter.setTrackPanLaw) this._adapter.setTrackPanLaw(trackId, panLaw);
1998
+ else this.sendTrack(trackId);
1871
1999
  }
1872
2000
  /**
1873
2001
  * After an undo, a redo or an abort brought back another mix: the setter
@@ -2006,7 +2134,7 @@ var AdapterSync = class {
2006
2134
  _loaded(track) {
2007
2135
  const known = this._loadedMemo.get(track);
2008
2136
  if (known) return known;
2009
- const clips = track.clips.filter((clip) => !isLoadingClip6(clip));
2137
+ const clips = track.clips.filter((clip) => !isLoadingClip7(clip));
2010
2138
  const loaded = clips.length === track.clips.length ? track : { ...track, clips };
2011
2139
  this._loadedMemo.set(track, loaded);
2012
2140
  return loaded;
@@ -2447,6 +2575,16 @@ var PlaylistEngine = class {
2447
2575
  const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan, options));
2448
2576
  if (!changed && !this._disposed) this._adapter?.setTrackPan(trackId, pan);
2449
2577
  }
2578
+ /**
2579
+ * Set a track's pan law (#684): the arrangement's `setTrackPanLaw`.
2580
+ * Unlike the four setters above, nothing is sent to the adapter when the
2581
+ * arrangement changed nothing (a track it does not hold, the law the
2582
+ * track already has): for an adapter without `setTrackPanLaw` the call
2583
+ * would be a whole-track `updateTrack`.
2584
+ */
2585
+ setTrackPanLaw(trackId, panLaw, options) {
2586
+ this._arrangement.setTrackPanLaw(trackId, panLaw, options);
2587
+ }
2450
2588
  /**
2451
2589
  * Mark a track as the one a take is recording onto, or clear it when the
2452
2590
  * take ends: the arrangement's `setTrackRecording`. That track plays muted