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

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
@@ -153,10 +153,10 @@ import { isLoadingClip as isLoadingClip5 } from "@waveform-playlist/core";
153
153
 
154
154
  // src/arrangement/attach.ts
155
155
  var ARRANGEMENT_ATTACH_DECODE = /* @__PURE__ */ Symbol.for(
156
- "@waveform-playlist/engine/arrangement/attach-decode/v1"
156
+ "@waveform-playlist/engine/arrangement/attach-decode/v2"
157
157
  );
158
158
  var ARRANGEMENT_ATTACH_TIME_BASE = /* @__PURE__ */ Symbol.for(
159
- "@waveform-playlist/engine/arrangement/time-base/v1"
159
+ "@waveform-playlist/engine/arrangement/time-base/v2"
160
160
  );
161
161
  function attachPoints(value) {
162
162
  return typeof value === "object" && value !== null ? value : {};
@@ -702,56 +702,6 @@ function sameValue(a, b) {
702
702
  return true;
703
703
  }
704
704
 
705
- // src/arrangement/history.ts
706
- var EMPTY_HISTORY = { undo: [], redo: [] };
707
- function pushStep(history, tracks, limit) {
708
- const undo = [...history.undo, tracks];
709
- return { undo: undo.length > limit ? undo.slice(undo.length - limit) : undo, redo: [] };
710
- }
711
- function undoStep(history, current) {
712
- if (history.undo.length === 0) return null;
713
- const tracks = history.undo[history.undo.length - 1];
714
- return {
715
- history: { undo: history.undo.slice(0, -1), redo: [...history.redo, current] },
716
- tracks
717
- };
718
- }
719
- function redoStep(history, current) {
720
- if (history.redo.length === 0) return null;
721
- const tracks = history.redo[history.redo.length - 1];
722
- return {
723
- history: { undo: [...history.undo, current], redo: history.redo.slice(0, -1) },
724
- tracks
725
- };
726
- }
727
- function beginBase(tracks) {
728
- return { tracks, changed: false };
729
- }
730
- function withoutTrack(tracks, trackId) {
731
- return tracks.some((track) => track.id === trackId) ? tracks.filter((track) => track.id !== trackId) : tracks;
732
- }
733
- function sameTracks(a, b) {
734
- return a === b || a.length === b.length && a.every((track, i) => track === b[i]);
735
- }
736
- function purgeTrack(history, current, trackId) {
737
- const nextCurrent = withoutTrack(current, trackId);
738
- const undo = [];
739
- for (const step of history.undo) {
740
- const stripped = withoutTrack(step, trackId);
741
- if (undo.length === 0 || !sameTracks(undo[undo.length - 1], stripped)) undo.push(stripped);
742
- }
743
- while (undo.length > 0 && sameTracks(undo[undo.length - 1], nextCurrent)) undo.pop();
744
- const redoFromCurrent = [];
745
- for (let i = history.redo.length - 1; i >= 0; i--) {
746
- const stripped = withoutTrack(history.redo[i], trackId);
747
- const before = redoFromCurrent.length > 0 ? redoFromCurrent[redoFromCurrent.length - 1] : nextCurrent;
748
- if (!sameTracks(before, stripped)) redoFromCurrent.push(stripped);
749
- }
750
- const redo = redoFromCurrent.reverse();
751
- const unchanged = undo.length === history.undo.length && redo.length === history.redo.length && undo.every((step, i) => step === history.undo[i]) && redo.every((step, i) => step === history.redo[i]);
752
- return { history: unchanged ? history : { undo, redo }, current: nextCurrent };
753
- }
754
-
755
705
  // src/arrangement/mixer.ts
756
706
  var EMPTY_MIXER = /* @__PURE__ */ new Map();
757
707
  var DEFAULT_VOLUME = 1;
@@ -776,20 +726,55 @@ function seedMixer(mixer, tracks) {
776
726
  function setMixerValues(mixer, trackId, patch) {
777
727
  const entry = mixer.get(trackId);
778
728
  if (!entry) return mixer;
779
- return new Map(mixer).set(trackId, { ...entry, ...patch });
729
+ const next = { ...entry, ...patch };
730
+ if (sameValues(entry, next)) return mixer;
731
+ return new Map(mixer).set(trackId, next);
780
732
  }
781
733
  function carryMixerEntry(mixer, fromId, toId) {
782
734
  const entry = mixer.get(fromId);
783
- if (!entry || mixer.get(toId) === entry) return mixer;
784
- return new Map(mixer).set(toId, entry);
735
+ if (!entry || fromId === toId) return mixer;
736
+ return new Map(
737
+ [...mixer].filter(([id]) => id !== toId).map(([id, values]) => [id === fromId ? toId : id, values])
738
+ );
739
+ }
740
+ function mixerOf(mixer, tracks) {
741
+ const ids = new Set(tracks.map((track) => track.id));
742
+ const seeded = seedMixer(mixer, tracks);
743
+ if (seeded.size === ids.size) return seeded;
744
+ return new Map([...seeded].filter(([id]) => ids.has(id)));
745
+ }
746
+ function withoutEntry(mixer, trackId) {
747
+ if (!mixer.has(trackId)) return mixer;
748
+ const next = new Map(mixer);
749
+ next.delete(trackId);
750
+ return next;
785
751
  }
786
- function retainMixer(mixer, states) {
787
- const held = /* @__PURE__ */ new Set();
788
- for (const tracks of states) {
789
- for (const track of tracks) held.add(track.id);
752
+ function sameValues(a, b) {
753
+ return a.volume === b.volume && a.muted === b.muted && a.soloed === b.soloed && a.pan === b.pan;
754
+ }
755
+ function sameMixerValues(a, b) {
756
+ if (a === b) return true;
757
+ if (a.size !== b.size) return false;
758
+ for (const [id, values] of a) {
759
+ const other = b.get(id);
760
+ if (!other || !sameValues(values, other)) return false;
790
761
  }
791
- if ([...mixer.keys()].every((id) => held.has(id))) return mixer;
792
- return new Map([...mixer].filter(([id]) => held.has(id)));
762
+ return true;
763
+ }
764
+ function mixerChanges(before, after) {
765
+ if (before === after) return [];
766
+ const changes = [];
767
+ for (const [trackId, now] of after) {
768
+ const was = before.get(trackId);
769
+ if (!was || was === now) continue;
770
+ const values = {};
771
+ if (was.volume !== now.volume) values.volume = now.volume;
772
+ if (was.muted !== now.muted) values.muted = now.muted;
773
+ if (was.soloed !== now.soloed) values.soloed = now.soloed;
774
+ if (was.pan !== now.pan) values.pan = now.pan;
775
+ if (Object.keys(values).length > 0) changes.push({ trackId, values });
776
+ }
777
+ return changes;
793
778
  }
794
779
  function withMixerValues(track, values) {
795
780
  if (!values || track.volume === values.volume && track.muted === values.muted && track.soloed === values.soloed && track.pan === values.pan) {
@@ -798,6 +783,120 @@ function withMixerValues(track, values) {
798
783
  return { ...track, ...values };
799
784
  }
800
785
 
786
+ // src/arrangement/history.ts
787
+ var EMPTY_HISTORY = { undo: [], redo: [] };
788
+ function pushStep(history, state, limit) {
789
+ const undo = [...history.undo, state];
790
+ return { undo: undo.length > limit ? undo.slice(undo.length - limit) : undo, redo: [] };
791
+ }
792
+ function undoStep(history, current) {
793
+ if (history.undo.length === 0) return null;
794
+ const state = history.undo[history.undo.length - 1];
795
+ return {
796
+ history: { undo: history.undo.slice(0, -1), redo: [...history.redo, current] },
797
+ state
798
+ };
799
+ }
800
+ function redoStep(history, current) {
801
+ if (history.redo.length === 0) return null;
802
+ const state = history.redo[history.redo.length - 1];
803
+ return {
804
+ history: { undo: [...history.undo, current], redo: history.redo.slice(0, -1) },
805
+ state
806
+ };
807
+ }
808
+ function beginBase(state) {
809
+ return { state, changed: false };
810
+ }
811
+ function withStateTracks(state, tracks) {
812
+ return tracks === state.tracks ? state : { ...state, tracks };
813
+ }
814
+ function mapHistoryTracks(history, map) {
815
+ const each = (state) => withStateTracks(state, map(state.tracks));
816
+ return { undo: history.undo.map(each), redo: history.redo.map(each) };
817
+ }
818
+ function withoutTrack(tracks, trackId) {
819
+ return tracks.some((track) => track.id === trackId) ? tracks.filter((track) => track.id !== trackId) : tracks;
820
+ }
821
+ function sameTracks(a, b) {
822
+ return a === b || a.length === b.length && a.every((track, i) => track === b[i]);
823
+ }
824
+ function sameState(a, b) {
825
+ return a === b || sameTracks(a.tracks, b.tracks) && sameMixerValues(a.mixer, b.mixer);
826
+ }
827
+ function stateWithoutTrack(trackId) {
828
+ const mixers = /* @__PURE__ */ new Map();
829
+ const mixerWithout = (mixer) => {
830
+ const known = mixers.get(mixer);
831
+ if (known) return known;
832
+ const next = withoutEntry(mixer, trackId);
833
+ mixers.set(mixer, next);
834
+ return next;
835
+ };
836
+ return (state) => {
837
+ const tracks = withoutTrack(state.tracks, trackId);
838
+ const mixer = mixerWithout(state.mixer);
839
+ return tracks === state.tracks && mixer === state.mixer ? state : { tracks, mixer };
840
+ };
841
+ }
842
+ function dropRepeats(history, current) {
843
+ const undo = [];
844
+ for (const step of history.undo) {
845
+ if (undo.length === 0 || !sameState(undo[undo.length - 1], step)) undo.push(step);
846
+ }
847
+ while (undo.length > 0 && sameState(undo[undo.length - 1], current)) undo.pop();
848
+ const redoFromCurrent = [];
849
+ for (let i = history.redo.length - 1; i >= 0; i--) {
850
+ const step = history.redo[i];
851
+ const before = redoFromCurrent.length > 0 ? redoFromCurrent[redoFromCurrent.length - 1] : current;
852
+ if (!sameState(before, step)) redoFromCurrent.push(step);
853
+ }
854
+ if (undo.length === history.undo.length && redoFromCurrent.length === history.redo.length) {
855
+ return history;
856
+ }
857
+ return { undo, redo: redoFromCurrent.reverse() };
858
+ }
859
+ function sameSteps(a, b) {
860
+ return a === b || a.undo.length === b.undo.length && a.redo.length === b.redo.length && a.undo.every((step, i) => step === b.undo[i]) && a.redo.every((step, i) => step === b.redo[i]);
861
+ }
862
+ function mapHistoryStates(history, map) {
863
+ return { undo: history.undo.map(map), redo: history.redo.map(map) };
864
+ }
865
+ function purgeTrack(history, current, trackId, base = null) {
866
+ const strip = stateWithoutTrack(trackId);
867
+ const nextCurrent = strip(current);
868
+ const nextBase = base && strip(base);
869
+ const stripped = dropRepeats(mapHistoryStates(history, strip), nextBase ?? nextCurrent);
870
+ return {
871
+ history: sameSteps(stripped, history) ? history : stripped,
872
+ current: nextCurrent,
873
+ base: nextBase
874
+ };
875
+ }
876
+ function stateWithMixerValues(trackId, patch) {
877
+ const mixers = /* @__PURE__ */ new Map();
878
+ const states = /* @__PURE__ */ new Map();
879
+ return (state) => {
880
+ const known = states.get(state);
881
+ if (known) return known;
882
+ let mixer = mixers.get(state.mixer);
883
+ if (!mixer) {
884
+ mixer = setMixerValues(state.mixer, trackId, patch);
885
+ mixers.set(state.mixer, mixer);
886
+ }
887
+ const next = mixer === state.mixer ? state : { ...state, mixer };
888
+ states.set(state, next);
889
+ return next;
890
+ };
891
+ }
892
+ function writeThrough(history, base, current, write) {
893
+ const nextBase = base && { ...base, state: write(base.state) };
894
+ return {
895
+ history: dropRepeats(mapHistoryStates(history, write), nextBase?.state ?? current),
896
+ base: nextBase
897
+ };
898
+ }
899
+
801
900
  // src/arrangement/ticks.ts
802
901
  import { isLoadingClip as isLoadingClip3 } from "@waveform-playlist/core";
803
902
  function forLoadedClips(change) {
@@ -878,14 +977,30 @@ function duplicateTrackId(trackId) {
878
977
  // src/arrangement/Arrangement.ts
879
978
  var DEFAULT_MIN_DURATION_SECONDS = 0.1;
880
979
  var DEFAULT_UNDO_LIMIT = 100;
980
+ var EMPTY_RECORDING = /* @__PURE__ */ new Set();
881
981
  var ArrangementImpl = class {
882
982
  constructor(options = {}) {
883
983
  this._tracks = [];
884
984
  this._tracksVersion = 0;
885
985
  this._mixerVersion = 0;
886
986
  this._mixer = EMPTY_MIXER;
987
+ /**
988
+ * The ids of the tracks a take is recording onto: outside the history and
989
+ * every saved state (#843). Replaced on a change, never written to.
990
+ */
991
+ this._recordingTrackIds = EMPTY_RECORDING;
887
992
  this._history = EMPTY_HISTORY;
888
993
  this._transaction = null;
994
+ /**
995
+ * The gesture of the top undo step, while a change of that gesture may
996
+ * still join it: its id, and the history from before its step was pushed
997
+ * (put back when the gesture returns to where it began, with the redo
998
+ * states and any step the undo limit evicted). Set when a gesture change
999
+ * pushes the step, cleared by anything else that touches the history
1000
+ * (#843). `before` is kept in step with the history: the time base, the
1001
+ * switch to loaded clips and `undoable: false` reach it too.
1002
+ */
1003
+ this._gesture = null;
889
1004
  this._subscribers = /* @__PURE__ */ new Set();
890
1005
  this._listeners = /* @__PURE__ */ new Set();
891
1006
  this._pending = [];
@@ -988,17 +1103,19 @@ var ArrangementImpl = class {
988
1103
  }
989
1104
  const prepare = tickPreparer(timeBase, this._tickPpqn);
990
1105
  const tracks = prepare(this._tracks);
991
- const history = {
992
- undo: this._history.undo.map(prepare),
993
- redo: this._history.redo.map(prepare)
1106
+ const history = mapHistoryTracks(this._history, prepare);
1107
+ const gesture = this._gesture && {
1108
+ ...this._gesture,
1109
+ before: mapHistoryTracks(this._gesture.before, prepare)
994
1110
  };
995
1111
  const transaction = this._transaction && {
996
1112
  ...this._transaction,
997
- tracks: prepare(this._transaction.tracks)
1113
+ state: withStateTracks(this._transaction.state, prepare(this._transaction.state.tracks))
998
1114
  };
999
1115
  this._timeBase = timeBase;
1000
1116
  this._tickPpqn = timeBase.ppqn;
1001
1117
  this._history = history;
1118
+ this._gesture = gesture;
1002
1119
  this._transaction = transaction;
1003
1120
  if (tracks !== this._tracks) {
1004
1121
  this._tracks = tracks;
@@ -1020,22 +1137,24 @@ var ArrangementImpl = class {
1020
1137
  }
1021
1138
  undo() {
1022
1139
  this._abortOpenTransaction();
1023
- const step = undoStep(this._history, this._tracks);
1140
+ this._gesture = null;
1141
+ const step = undoStep(this._history, this._currentState());
1024
1142
  if (!step) return;
1025
1143
  this._history = step.history;
1026
- this._restoreTracks(step.tracks);
1144
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1027
1145
  }
1028
1146
  redo() {
1029
1147
  this._abortOpenTransaction();
1030
- const step = redoStep(this._history, this._tracks);
1148
+ this._gesture = null;
1149
+ const step = redoStep(this._history, this._currentState());
1031
1150
  if (!step) return;
1032
1151
  this._history = step.history;
1033
- this._restoreTracks(step.tracks);
1152
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1034
1153
  }
1035
1154
  clearHistory() {
1036
1155
  const hadHistory = this.canUndo || this.canRedo;
1037
1156
  this._history = EMPTY_HISTORY;
1038
- this._mixer = retainMixer(this._mixer, this._heldStates());
1157
+ this._gesture = null;
1039
1158
  if (hadHistory) this._emit({ type: "history" });
1040
1159
  }
1041
1160
  beginTransaction() {
@@ -1045,7 +1164,8 @@ var ArrangementImpl = class {
1045
1164
  );
1046
1165
  }
1047
1166
  const replaced = this._transaction !== null;
1048
- this._transaction = beginBase(this._tracks);
1167
+ this._transaction = beginBase(this._currentState());
1168
+ this._gesture = null;
1049
1169
  if (replaced) this._resyncSourcesQuietly();
1050
1170
  }
1051
1171
  commitTransaction() {
@@ -1055,8 +1175,9 @@ var ArrangementImpl = class {
1055
1175
  return;
1056
1176
  }
1057
1177
  this._transaction = null;
1058
- if (transaction.changed && !sameTracks(transaction.tracks, this._tracks)) {
1059
- this._pushStep(transaction.tracks);
1178
+ this._gesture = null;
1179
+ if (transaction.changed && !sameState(transaction.state, this._currentState())) {
1180
+ this._pushStep(transaction.state);
1060
1181
  this._emit({ type: "history" });
1061
1182
  }
1062
1183
  }
@@ -1081,9 +1202,9 @@ var ArrangementImpl = class {
1081
1202
  tracks.forEach(assertUniqueClipIds);
1082
1203
  this._transaction = null;
1083
1204
  this._history = EMPTY_HISTORY;
1084
- const kept = retainMixer(this._mixer, [this._tracks]);
1205
+ this._gesture = null;
1085
1206
  this._tracks = tracks.map((track) => this._ingest(track));
1086
- this._mixer = retainMixer(seedMixer(kept, this._tracks), [this._tracks]);
1207
+ this._mixer = mixerOf(this._mixer, this._tracks);
1087
1208
  this._tracksVersion++;
1088
1209
  this._emit({ type: "set" });
1089
1210
  }
@@ -1111,6 +1232,7 @@ var ArrangementImpl = class {
1111
1232
  if (!this._tracks.some((t) => t.id === trackId)) return;
1112
1233
  this._pushUndoSnapshot();
1113
1234
  this._tracks = this._tracks.filter((t) => t.id !== trackId);
1235
+ this._mixer = withoutEntry(this._mixer, trackId);
1114
1236
  this._tracksVersion++;
1115
1237
  this._emit({ type: "remove", trackId });
1116
1238
  }
@@ -1157,7 +1279,6 @@ var ArrangementImpl = class {
1157
1279
  const enriched = this._ingest(track, new Set(current.clips));
1158
1280
  this._tracks = this._tracks.map((t) => t.id === trackId ? enriched : t);
1159
1281
  this._mixer = seedMixer(carryMixerEntry(this._mixer, trackId, enriched.id), [enriched]);
1160
- if (enriched.id !== trackId) this._mixer = retainMixer(this._mixer, this._heldStates());
1161
1282
  this._tracksVersion++;
1162
1283
  this._emit({ type: "update", trackId, previous: current });
1163
1284
  }
@@ -1194,19 +1315,46 @@ var ArrangementImpl = class {
1194
1315
  //
1195
1316
  // Each replaces the Map and one entry, bumps mixerVersion and announces
1196
1317
  // the change. NOT structural changes: no track object is replaced,
1197
- // tracksVersion stays (#501), and they are not undo steps. An id that is
1198
- // not in the current tracks does nothing.
1199
- setTrackVolume(trackId, volume) {
1200
- this._setMixerValues(trackId, { volume });
1318
+ // tracksVersion stays (#501). Each call is an undo step; inside an open
1319
+ // transaction it is part of it, like any other change. Calls with one
1320
+ // `gesture` id, one after another, are one step; `undoable: false` is no
1321
+ // step and is written into every saved state (#843). A value that does
1322
+ // not change, or an id that is not in the current tracks, does nothing.
1323
+ setTrackVolume(trackId, volume, options) {
1324
+ this._setMixerValues(trackId, { volume }, options);
1325
+ }
1326
+ setTrackMute(trackId, muted, options) {
1327
+ this._setMixerValues(trackId, { muted }, options);
1201
1328
  }
1202
- setTrackMute(trackId, muted) {
1203
- this._setMixerValues(trackId, { muted });
1329
+ setTrackSolo(trackId, soloed, options) {
1330
+ this._setMixerValues(trackId, { soloed }, options);
1204
1331
  }
1205
- setTrackSolo(trackId, soloed) {
1206
- this._setMixerValues(trackId, { soloed });
1332
+ setTrackPan(trackId, pan, options) {
1333
+ this._setMixerValues(trackId, { pan }, options);
1207
1334
  }
1208
- setTrackPan(trackId, pan) {
1209
- this._setMixerValues(trackId, { pan });
1335
+ // ---------------------------------------------------------------------------
1336
+ // Recording mute (the track a take is recording onto)
1337
+ // ---------------------------------------------------------------------------
1338
+ //
1339
+ // The track a take is recording onto plays muted: the engine's projection
1340
+ // and the adapter get `muted: stored || recording`, and the stored mute in
1341
+ // `getMixer()` is not touched. The set is outside the history: the take's
1342
+ // mute is no undo step, and undo, redo, abort, `setTracks` and
1343
+ // `clearHistory` leave it alone (#843).
1344
+ /** The ids of the tracks a take is recording onto. The same Set until it changes; never written to. */
1345
+ get recordingTrackIds() {
1346
+ return this._recordingTrackIds;
1347
+ }
1348
+ /**
1349
+ * Mark a track as the one a take is recording onto, or clear it. An id
1350
+ * that is not in the current tracks is recorded too, and mutes nothing
1351
+ * until a track of that id enters. The same value does nothing.
1352
+ */
1353
+ setTrackRecording(trackId, recording) {
1354
+ if (this._recordingTrackIds.has(trackId) === recording) return;
1355
+ this._recordingTrackIds = recording ? /* @__PURE__ */ new Set([...this._recordingTrackIds, trackId]) : new Set([...this._recordingTrackIds].filter((id) => id !== trackId));
1356
+ if (this._mixer.has(trackId)) this._mixerVersion++;
1357
+ this._emit({ type: "recording", trackId });
1210
1358
  }
1211
1359
  // ---------------------------------------------------------------------------
1212
1360
  // Tempo
@@ -1269,37 +1417,97 @@ var ArrangementImpl = class {
1269
1417
  })
1270
1418
  };
1271
1419
  }
1272
- _setMixerValues(trackId, values) {
1420
+ _setMixerValues(trackId, values, options = {}) {
1273
1421
  if (!this._tracks.some((t) => t.id === trackId)) return;
1274
1422
  const next = setMixerValues(this._mixer, trackId, values);
1275
1423
  if (next === this._mixer) return;
1276
- this._mixer = next;
1424
+ if (options.undoable === false) {
1425
+ this._writeMixerThrough(trackId, values, next);
1426
+ } else {
1427
+ this._recordMixerChange(options.gesture);
1428
+ this._mixer = next;
1429
+ this._dropReturnedGestureStep();
1430
+ }
1277
1431
  this._mixerVersion++;
1278
1432
  this._emit({ type: "mixer", trackId, values });
1279
1433
  }
1280
- /** Every tracks array the arrangement can still return to, and the current one. */
1434
+ /**
1435
+ * The undo step of a mixer change about to be made: part of an open
1436
+ * transaction, the top step when it was pushed by the same gesture and
1437
+ * nothing has touched the history since, or else a new step (which becomes
1438
+ * that gesture's).
1439
+ */
1440
+ _recordMixerChange(gesture) {
1441
+ if (this._transaction) {
1442
+ this._markTransactionChanged();
1443
+ return;
1444
+ }
1445
+ if (gesture !== void 0 && this._gesture?.id === gesture) return;
1446
+ const before = this._history;
1447
+ this._pushStep(this._currentState());
1448
+ if (gesture !== void 0 && this.canUndo) this._gesture = { id: gesture, before };
1449
+ }
1450
+ /**
1451
+ * A gesture that came back to the state its step holds leaves no step, and
1452
+ * the history is the one it found: the redo states its first change
1453
+ * cleared come back (as with a transaction that changes nothing).
1454
+ */
1455
+ _dropReturnedGestureStep() {
1456
+ const gesture = this._gesture;
1457
+ if (!gesture) return;
1458
+ const { undo } = this._history;
1459
+ if (!sameState(undo[undo.length - 1], this._currentState())) return;
1460
+ this._history = dropRepeats(gesture.before, this._currentState());
1461
+ this._gesture = null;
1462
+ }
1463
+ /**
1464
+ * `undoable: false`: the value in the current mix and in every saved state
1465
+ * that has an entry for the track, so that no undo, redo or abort brings
1466
+ * the old one back. A step that no longer differs from the state next to
1467
+ * it goes; when that was the step a gesture joins and no equal step is
1468
+ * left on top, the gesture's next change pushes a new one.
1469
+ */
1470
+ _writeMixerThrough(trackId, values, next) {
1471
+ this._mixer = next;
1472
+ const write = stateWithMixerValues(trackId, values);
1473
+ const { undo } = this._history;
1474
+ const top = undo.length > 0 ? write(undo[undo.length - 1]) : void 0;
1475
+ const written = writeThrough(this._history, this._transaction, this._currentState(), write);
1476
+ this._history = written.history;
1477
+ this._transaction = written.base;
1478
+ if (this._gesture) {
1479
+ const newTop = this._history.undo[this._history.undo.length - 1];
1480
+ this._gesture = newTop !== void 0 && top !== void 0 && sameState(newTop, top) ? { ...this._gesture, before: mapHistoryStates(this._gesture.before, write) } : null;
1481
+ }
1482
+ }
1483
+ /** The tracks and the mix as they are now: what an undo step or a transaction's base saves. */
1484
+ _currentState() {
1485
+ return { tracks: this._tracks, mixer: this._mixer };
1486
+ }
1487
+ /** Every tracks array the arrangement can still return to, and the current one (for the sources). */
1281
1488
  _heldStates() {
1282
1489
  return [
1283
1490
  this._tracks,
1284
- ...this._history.undo,
1285
- ...this._history.redo,
1286
- ...this._transaction ? [this._transaction.tracks] : []
1491
+ ...this._history.undo.map((state) => state.tracks),
1492
+ ...this._history.redo.map((state) => state.tracks),
1493
+ ...this._gesture ? this._gesture.before.undo.map((state) => state.tracks) : [],
1494
+ ...this._gesture ? this._gesture.before.redo.map((state) => state.tracks) : [],
1495
+ ...this._transaction ? [this._transaction.state.tracks] : []
1287
1496
  ];
1288
1497
  }
1289
1498
  /** `removeTrack` with `undoable: false`: out of the current tracks and out of every stored state. */
1290
1499
  _removeTrackEverywhere(trackId) {
1291
1500
  const wasThere = this._tracks.some((t) => t.id === trackId);
1292
- const purged = purgeTrack(this._history, this._tracks, trackId);
1501
+ const base = this._transaction?.state ?? null;
1502
+ const purged = purgeTrack(this._history, this._currentState(), trackId, base);
1293
1503
  const historyChanged = purged.history !== this._history;
1294
1504
  this._history = purged.history;
1295
- if (this._transaction) {
1296
- this._transaction = {
1297
- ...this._transaction,
1298
- tracks: withoutTrack(this._transaction.tracks, trackId)
1299
- };
1505
+ this._gesture = null;
1506
+ if (this._transaction && purged.base) {
1507
+ this._transaction = { ...this._transaction, state: purged.base };
1300
1508
  }
1301
- this._tracks = purged.current;
1302
- this._mixer = retainMixer(this._mixer, this._heldStates());
1509
+ this._tracks = purged.current.tracks;
1510
+ this._mixer = purged.current.mixer;
1303
1511
  if (wasThere) {
1304
1512
  this._tracksVersion++;
1305
1513
  this._emit({ type: "remove", trackId });
@@ -1312,32 +1520,55 @@ var ArrangementImpl = class {
1312
1520
  /** The state before a change: an undo step, or the mark that an open transaction changed. */
1313
1521
  _pushUndoSnapshot() {
1314
1522
  if (this._transaction) {
1315
- if (!this._transaction.changed) {
1316
- this._transaction = { ...this._transaction, changed: true };
1317
- }
1523
+ this._markTransactionChanged();
1318
1524
  return;
1319
1525
  }
1320
- this._pushStep(this._tracks);
1526
+ this._pushStep(this._currentState());
1527
+ }
1528
+ /** An open transaction has changed something. */
1529
+ _markTransactionChanged() {
1530
+ if (this._transaction && !this._transaction.changed) {
1531
+ this._transaction = { ...this._transaction, changed: true };
1532
+ }
1321
1533
  }
1322
- /** Add an undo step. The history may let go of states with it (the redo stack, a step past the limit). */
1323
- _pushStep(tracks) {
1324
- const letsGo = this._history.redo.length > 0 || this._history.undo.length >= this.undoLimit;
1325
- this._history = pushStep(this._history, tracks, this.undoLimit);
1326
- if (letsGo) this._mixer = retainMixer(this._mixer, this._heldStates());
1534
+ /** Add an undo step. No gesture joins it (a gesture change that pushes sets its id after). */
1535
+ _pushStep(state) {
1536
+ this._history = pushStep(this._history, state, this.undoLimit);
1537
+ this._gesture = null;
1327
1538
  }
1328
- _restoreTracks(snapshot) {
1539
+ /**
1540
+ * Make a saved state the current one (an undo, a redo, an abort) and
1541
+ * announce it: `'restore'` when the tracks array is another one (with the
1542
+ * mix from before, which may differ too), `'restoreMix'` when only mixer
1543
+ * values differ. `mixerVersion` counts it when a track that is in both
1544
+ * states has other values; an entry that comes or goes with its track is
1545
+ * not a mixer change. Returns false when nothing differed and nothing was
1546
+ * announced.
1547
+ */
1548
+ _restoreState(state) {
1329
1549
  const previous = this._tracks;
1330
- this._tracks = snapshot;
1331
- this._tracksVersion++;
1332
- this._emit({ type: "restore", previous });
1550
+ const previousMixer = this._mixer;
1551
+ const tracksChanged = state.tracks !== previous;
1552
+ const mixChanged = mixerChanges(previousMixer, state.mixer).length > 0;
1553
+ this._tracks = state.tracks;
1554
+ this._mixer = state.mixer;
1555
+ if (mixChanged) this._mixerVersion++;
1556
+ if (tracksChanged) {
1557
+ this._tracksVersion++;
1558
+ this._emit({ type: "restore", previous, previousMixer });
1559
+ } else if (mixChanged) {
1560
+ this._emit({ type: "restoreMix", previousMixer });
1561
+ }
1562
+ return tracksChanged || mixChanged;
1333
1563
  }
1334
- /** Ends an open transaction, restoring the tracks it began from if it changed them. */
1564
+ /** Ends an open transaction, restoring the state it began from if it changed anything. */
1335
1565
  _abortOpenTransaction() {
1336
1566
  const transaction = this._transaction;
1337
1567
  if (!transaction) return;
1338
1568
  this._transaction = null;
1569
+ this._gesture = null;
1339
1570
  if (transaction.changed) {
1340
- this._restoreTracks(transaction.tracks);
1571
+ this._restoreState(transaction.state);
1341
1572
  }
1342
1573
  }
1343
1574
  /** Declare the sources every state names, and switch the clips whose audio is there. */
@@ -1361,12 +1592,19 @@ var ArrangementImpl = class {
1361
1592
  const pass = this._sources.beginPass((startSample) => this._tickAt(startSample));
1362
1593
  const previous = this._tracks;
1363
1594
  this._tracks = pass.current(previous);
1364
- this._history = {
1365
- undo: this._history.undo.map(pass.stored),
1366
- redo: this._history.redo.map(pass.stored)
1367
- };
1595
+ this._history = mapHistoryTracks(this._history, pass.stored);
1596
+ if (this._gesture) {
1597
+ this._gesture = {
1598
+ ...this._gesture,
1599
+ before: mapHistoryTracks(this._gesture.before, pass.stored)
1600
+ };
1601
+ }
1368
1602
  if (this._transaction) {
1369
- this._transaction = { ...this._transaction, tracks: pass.stored(this._transaction.tracks) };
1603
+ const { state } = this._transaction;
1604
+ this._transaction = {
1605
+ ...this._transaction,
1606
+ state: withStateTracks(state, pass.stored(state.tracks))
1607
+ };
1370
1608
  }
1371
1609
  return this._tracks === previous ? null : previous;
1372
1610
  }
@@ -1392,11 +1630,9 @@ var ArrangementImpl = class {
1392
1630
  * always received that error).
1393
1631
  */
1394
1632
  _emit(change) {
1395
- const switched = change.type === "mixer" ? null : this._syncSources();
1633
+ const mixOnly = change.type === "mixer" || change.type === "restoreMix" || change.type === "recording";
1634
+ const switched = mixOnly ? null : this._syncSources();
1396
1635
  freezeTracksInDevelopment(this._tracks);
1397
- if (change.type === "remove" || change.type === "restore") {
1398
- this._mixer = retainMixer(this._mixer, this._heldStates());
1399
- }
1400
1636
  const at = {
1401
1637
  tracksVersion: this._tracksVersion,
1402
1638
  mixerVersion: this._mixerVersion
@@ -1470,8 +1706,8 @@ var AdapterSync = class {
1470
1706
  }
1471
1707
  /**
1472
1708
  * The arrangement's tracks, each without its loading clips and with the
1473
- * current mixer values in its fields: what every adapter track call and
1474
- * `getState().tracks` use. The same
1709
+ * current mixer values in its fields (muted while a take records onto it): what every
1710
+ * adapter track call and `getState().tracks` use. The same
1475
1711
  * array until the tracks or the mix change, and the same track object
1476
1712
  * while neither that track nor its mix changed.
1477
1713
  */
@@ -1512,13 +1748,30 @@ var AdapterSync = class {
1512
1748
  }
1513
1749
  return true;
1514
1750
  }
1515
- /** A mixer value changed: the adapter's setter for it. */
1751
+ /**
1752
+ * A mixer value changed: the adapter's setter for it. The mute sent is the
1753
+ * one the track plays with: muted while a take records onto the track,
1754
+ * whatever the value given.
1755
+ */
1516
1756
  sendMixerValues(trackId, values) {
1517
1757
  if (values.volume !== void 0) this._adapter?.setTrackVolume(trackId, values.volume);
1518
- if (values.muted !== void 0) this._adapter?.setTrackMute(trackId, values.muted);
1758
+ if (values.muted !== void 0) {
1759
+ this._adapter?.setTrackMute(trackId, values.muted || this._recordingNow(trackId));
1760
+ }
1519
1761
  if (values.soloed !== void 0) this._adapter?.setTrackSolo(trackId, values.soloed);
1520
1762
  if (values.pan !== void 0) this._adapter?.setTrackPan(trackId, values.pan);
1521
1763
  }
1764
+ /**
1765
+ * After an undo, a redo or an abort brought back another mix: the setter
1766
+ * for each field that differs, of each current track that had an entry in
1767
+ * `previousMixer` too. A track that entered with the restore was sent
1768
+ * whole, with its mix in its fields.
1769
+ */
1770
+ sendMixerDiff(previousMixer) {
1771
+ for (const { trackId, values } of mixerChanges(previousMixer, this._arrangement.getMixer())) {
1772
+ this.sendMixerValues(trackId, values);
1773
+ }
1774
+ }
1522
1775
  /**
1523
1776
  * The adapter call for a change to the tracks.
1524
1777
  *
@@ -1649,13 +1902,26 @@ var AdapterSync = class {
1649
1902
  this._loadedMemo.set(track, loaded);
1650
1903
  return loaded;
1651
1904
  }
1652
- /** One track with its current mixer values in its fields (memoized per track object). */
1905
+ /**
1906
+ * True when a take is recording onto the track and it is a current track
1907
+ * (it has an entry in the current mix): it plays muted. A recording id that
1908
+ * is not in the tracks mutes nothing until it enters.
1909
+ */
1910
+ _recordingNow(trackId) {
1911
+ return this._arrangement.recordingTrackIds.has(trackId) && this._arrangement.getMixer().has(trackId);
1912
+ }
1913
+ /**
1914
+ * One track with its current mixer values in its fields, muted while a
1915
+ * take records onto it (memoized per track object).
1916
+ */
1653
1917
  _withMix(track) {
1654
1918
  const values = this._arrangement.getMixer().get(track.id);
1919
+ const recording = this._recordingNow(track.id);
1655
1920
  const memo = this._withMixMemo.get(track);
1656
- if (memo && memo.values === values) return memo.merged;
1657
- const merged = freezeTrackInDevelopment(withMixerValues(track, values));
1658
- this._withMixMemo.set(track, { values, merged });
1921
+ if (memo && memo.values === values && memo.recording === recording) return memo.merged;
1922
+ const playing = recording && values && !values.muted ? { ...values, muted: true } : values;
1923
+ const merged = freezeTrackInDevelopment(withMixerValues(track, playing));
1924
+ this._withMixMemo.set(track, { values, recording, merged });
1659
1925
  return merged;
1660
1926
  }
1661
1927
  };
@@ -2030,9 +2296,13 @@ var PlaylistEngine = class {
2030
2296
  //
2031
2297
  // Each setter writes the value into the arrangement's mixer part (so a later
2032
2298
  // adapter rebuild via setTracks keeps the value, and mixerVersion is bumped);
2033
- // the engine's listener then calls the adapter and emits statechange. For a
2034
- // track the arrangement does not hold nothing changes, and the adapter is
2035
- // still called from here (a consumer may target a track that has not been
2299
+ // the engine's listener then calls the adapter and emits statechange. Each
2300
+ // call is an undo step, unless `options` says otherwise: the same `gesture`
2301
+ // id joins one step, `undoable: false` is no step (`MixerChangeOptions`).
2302
+ //
2303
+ // When the arrangement changes nothing (a track it does not hold, or the
2304
+ // value the track already has), the adapter is still called from here,
2305
+ // with no statechange (a consumer may target a track that has not been
2036
2306
  // ingested yet). These are NOT structural
2037
2307
  // changes, so they deliberately do NOT bump tracksVersion — that would make
2038
2308
  // tracksVersion-gated consumers re-run expensive structural work (audio-graph
@@ -2040,22 +2310,48 @@ var PlaylistEngine = class {
2040
2310
  // cache a track snapshot (e.g. @dawcore/components' <daw-editor>) watch
2041
2311
  // mixerVersion to refresh that cache cheaply, so an unrelated setTracks
2042
2312
  // rebuild no longer reverts live mixer state (#501).
2043
- setTrackVolume(trackId, volume) {
2044
- const changed = this._mixerChanged(() => this._arrangement.setTrackVolume(trackId, volume));
2313
+ setTrackVolume(trackId, volume, options) {
2314
+ const changed = this._mixerChanged(
2315
+ () => this._arrangement.setTrackVolume(trackId, volume, options)
2316
+ );
2045
2317
  if (!changed && !this._disposed) this._adapter?.setTrackVolume(trackId, volume);
2046
2318
  }
2047
- setTrackMute(trackId, muted) {
2048
- const changed = this._mixerChanged(() => this._arrangement.setTrackMute(trackId, muted));
2049
- if (!changed && !this._disposed) this._adapter?.setTrackMute(trackId, muted);
2319
+ setTrackMute(trackId, muted, options) {
2320
+ const changed = this._mixerChanged(
2321
+ () => this._arrangement.setTrackMute(trackId, muted, options)
2322
+ );
2323
+ if (!changed && !this._disposed) this._adapterSync.sendMixerValues(trackId, { muted });
2050
2324
  }
2051
- setTrackSolo(trackId, soloed) {
2052
- const changed = this._mixerChanged(() => this._arrangement.setTrackSolo(trackId, soloed));
2325
+ setTrackSolo(trackId, soloed, options) {
2326
+ const changed = this._mixerChanged(
2327
+ () => this._arrangement.setTrackSolo(trackId, soloed, options)
2328
+ );
2053
2329
  if (!changed && !this._disposed) this._adapter?.setTrackSolo(trackId, soloed);
2054
2330
  }
2055
- setTrackPan(trackId, pan) {
2056
- const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan));
2331
+ setTrackPan(trackId, pan, options) {
2332
+ const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan, options));
2057
2333
  if (!changed && !this._disposed) this._adapter?.setTrackPan(trackId, pan);
2058
2334
  }
2335
+ /**
2336
+ * Mark a track as the one a take is recording onto, or clear it when the
2337
+ * take ends: the arrangement's `setTrackRecording`. That track plays muted
2338
+ * (here and in the adapter) while its mute in the mix stays as it is; the
2339
+ * take's mute is not an undo step. The arrangement's `'recording'`
2340
+ * announcement brings the adapter in step, so there is no adapter call of
2341
+ * its own here; a disposed engine changes the arrangement only.
2342
+ */
2343
+ setTrackRecording(trackId, recording) {
2344
+ this._arrangement.setTrackRecording(trackId, recording);
2345
+ }
2346
+ /**
2347
+ * The tracks a take is recording onto, read from the arrangement (the
2348
+ * owner; no copy). The same set object until it changes; never write to
2349
+ * it, use `setTrackRecording`. Such a track plays muted, so a UI can
2350
+ * disable its mute control while the id is in the set.
2351
+ */
2352
+ get recordingTrackIds() {
2353
+ return this._arrangement.recordingTrackIds;
2354
+ }
2059
2355
  // ---------------------------------------------------------------------------
2060
2356
  // Zoom
2061
2357
  // ---------------------------------------------------------------------------
@@ -2128,6 +2424,14 @@ var PlaylistEngine = class {
2128
2424
  case "mixer":
2129
2425
  this._adapterSync.sendMixerValues(change.trackId, change.values);
2130
2426
  break;
2427
+ case "restoreMix":
2428
+ this._adapterSync.sendMixerDiff(change.previousMixer);
2429
+ break;
2430
+ case "recording": {
2431
+ const values = this._arrangement.getMixer().get(change.trackId);
2432
+ if (values) this._adapterSync.sendMixerValues(change.trackId, { muted: values.muted });
2433
+ break;
2434
+ }
2131
2435
  case "reorder":
2132
2436
  // not audible: adapter nodes are keyed by track id
2133
2437
  case "history":
@@ -2140,6 +2444,7 @@ var PlaylistEngine = class {
2140
2444
  this._followLoad(change, at);
2141
2445
  } else {
2142
2446
  this._adapterSync.follow(change, at);
2447
+ if (change.type === "restore") this._adapterSync.sendMixerDiff(change.previousMixer);
2143
2448
  }
2144
2449
  }
2145
2450
  this._emitStateChange();