@waveform-playlist/engine 14.0.0-next.1 → 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.js CHANGED
@@ -194,10 +194,10 @@ var import_core7 = require("@waveform-playlist/core");
194
194
 
195
195
  // src/arrangement/attach.ts
196
196
  var ARRANGEMENT_ATTACH_DECODE = /* @__PURE__ */ Symbol.for(
197
- "@waveform-playlist/engine/arrangement/attach-decode/v1"
197
+ "@waveform-playlist/engine/arrangement/attach-decode/v2"
198
198
  );
199
199
  var ARRANGEMENT_ATTACH_TIME_BASE = /* @__PURE__ */ Symbol.for(
200
- "@waveform-playlist/engine/arrangement/time-base/v1"
200
+ "@waveform-playlist/engine/arrangement/time-base/v2"
201
201
  );
202
202
  function attachPoints(value) {
203
203
  return typeof value === "object" && value !== null ? value : {};
@@ -743,56 +743,6 @@ function sameValue(a, b) {
743
743
  return true;
744
744
  }
745
745
 
746
- // src/arrangement/history.ts
747
- var EMPTY_HISTORY = { undo: [], redo: [] };
748
- function pushStep(history, tracks, limit) {
749
- const undo = [...history.undo, tracks];
750
- return { undo: undo.length > limit ? undo.slice(undo.length - limit) : undo, redo: [] };
751
- }
752
- function undoStep(history, current) {
753
- if (history.undo.length === 0) return null;
754
- const tracks = history.undo[history.undo.length - 1];
755
- return {
756
- history: { undo: history.undo.slice(0, -1), redo: [...history.redo, current] },
757
- tracks
758
- };
759
- }
760
- function redoStep(history, current) {
761
- if (history.redo.length === 0) return null;
762
- const tracks = history.redo[history.redo.length - 1];
763
- return {
764
- history: { undo: [...history.undo, current], redo: history.redo.slice(0, -1) },
765
- tracks
766
- };
767
- }
768
- function beginBase(tracks) {
769
- return { tracks, changed: false };
770
- }
771
- function withoutTrack(tracks, trackId) {
772
- return tracks.some((track) => track.id === trackId) ? tracks.filter((track) => track.id !== trackId) : tracks;
773
- }
774
- function sameTracks(a, b) {
775
- return a === b || a.length === b.length && a.every((track, i) => track === b[i]);
776
- }
777
- function purgeTrack(history, current, trackId) {
778
- const nextCurrent = withoutTrack(current, trackId);
779
- const undo = [];
780
- for (const step of history.undo) {
781
- const stripped = withoutTrack(step, trackId);
782
- if (undo.length === 0 || !sameTracks(undo[undo.length - 1], stripped)) undo.push(stripped);
783
- }
784
- while (undo.length > 0 && sameTracks(undo[undo.length - 1], nextCurrent)) undo.pop();
785
- const redoFromCurrent = [];
786
- for (let i = history.redo.length - 1; i >= 0; i--) {
787
- const stripped = withoutTrack(history.redo[i], trackId);
788
- const before = redoFromCurrent.length > 0 ? redoFromCurrent[redoFromCurrent.length - 1] : nextCurrent;
789
- if (!sameTracks(before, stripped)) redoFromCurrent.push(stripped);
790
- }
791
- const redo = redoFromCurrent.reverse();
792
- 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]);
793
- return { history: unchanged ? history : { undo, redo }, current: nextCurrent };
794
- }
795
-
796
746
  // src/arrangement/mixer.ts
797
747
  var EMPTY_MIXER = /* @__PURE__ */ new Map();
798
748
  var DEFAULT_VOLUME = 1;
@@ -817,20 +767,55 @@ function seedMixer(mixer, tracks) {
817
767
  function setMixerValues(mixer, trackId, patch) {
818
768
  const entry = mixer.get(trackId);
819
769
  if (!entry) return mixer;
820
- return new Map(mixer).set(trackId, { ...entry, ...patch });
770
+ const next = { ...entry, ...patch };
771
+ if (sameValues(entry, next)) return mixer;
772
+ return new Map(mixer).set(trackId, next);
821
773
  }
822
774
  function carryMixerEntry(mixer, fromId, toId) {
823
775
  const entry = mixer.get(fromId);
824
- if (!entry || mixer.get(toId) === entry) return mixer;
825
- return new Map(mixer).set(toId, entry);
776
+ if (!entry || fromId === toId) return mixer;
777
+ return new Map(
778
+ [...mixer].filter(([id]) => id !== toId).map(([id, values]) => [id === fromId ? toId : id, values])
779
+ );
780
+ }
781
+ function mixerOf(mixer, tracks) {
782
+ const ids = new Set(tracks.map((track) => track.id));
783
+ const seeded = seedMixer(mixer, tracks);
784
+ if (seeded.size === ids.size) return seeded;
785
+ return new Map([...seeded].filter(([id]) => ids.has(id)));
786
+ }
787
+ function withoutEntry(mixer, trackId) {
788
+ if (!mixer.has(trackId)) return mixer;
789
+ const next = new Map(mixer);
790
+ next.delete(trackId);
791
+ return next;
826
792
  }
827
- function retainMixer(mixer, states) {
828
- const held = /* @__PURE__ */ new Set();
829
- for (const tracks of states) {
830
- for (const track of tracks) held.add(track.id);
793
+ function sameValues(a, b) {
794
+ return a.volume === b.volume && a.muted === b.muted && a.soloed === b.soloed && a.pan === b.pan;
795
+ }
796
+ function sameMixerValues(a, b) {
797
+ if (a === b) return true;
798
+ if (a.size !== b.size) return false;
799
+ for (const [id, values] of a) {
800
+ const other = b.get(id);
801
+ if (!other || !sameValues(values, other)) return false;
831
802
  }
832
- if ([...mixer.keys()].every((id) => held.has(id))) return mixer;
833
- return new Map([...mixer].filter(([id]) => held.has(id)));
803
+ return true;
804
+ }
805
+ function mixerChanges(before, after) {
806
+ if (before === after) return [];
807
+ const changes = [];
808
+ for (const [trackId, now] of after) {
809
+ const was = before.get(trackId);
810
+ if (!was || was === now) continue;
811
+ const values = {};
812
+ if (was.volume !== now.volume) values.volume = now.volume;
813
+ if (was.muted !== now.muted) values.muted = now.muted;
814
+ if (was.soloed !== now.soloed) values.soloed = now.soloed;
815
+ if (was.pan !== now.pan) values.pan = now.pan;
816
+ if (Object.keys(values).length > 0) changes.push({ trackId, values });
817
+ }
818
+ return changes;
834
819
  }
835
820
  function withMixerValues(track, values) {
836
821
  if (!values || track.volume === values.volume && track.muted === values.muted && track.soloed === values.soloed && track.pan === values.pan) {
@@ -839,6 +824,120 @@ function withMixerValues(track, values) {
839
824
  return { ...track, ...values };
840
825
  }
841
826
 
827
+ // src/arrangement/history.ts
828
+ var EMPTY_HISTORY = { undo: [], redo: [] };
829
+ function pushStep(history, state, limit) {
830
+ const undo = [...history.undo, state];
831
+ return { undo: undo.length > limit ? undo.slice(undo.length - limit) : undo, redo: [] };
832
+ }
833
+ function undoStep(history, current) {
834
+ if (history.undo.length === 0) return null;
835
+ const state = history.undo[history.undo.length - 1];
836
+ return {
837
+ history: { undo: history.undo.slice(0, -1), redo: [...history.redo, current] },
838
+ state
839
+ };
840
+ }
841
+ function redoStep(history, current) {
842
+ if (history.redo.length === 0) return null;
843
+ const state = history.redo[history.redo.length - 1];
844
+ return {
845
+ history: { undo: [...history.undo, current], redo: history.redo.slice(0, -1) },
846
+ state
847
+ };
848
+ }
849
+ function beginBase(state) {
850
+ return { state, changed: false };
851
+ }
852
+ function withStateTracks(state, tracks) {
853
+ return tracks === state.tracks ? state : { ...state, tracks };
854
+ }
855
+ function mapHistoryTracks(history, map) {
856
+ const each = (state) => withStateTracks(state, map(state.tracks));
857
+ return { undo: history.undo.map(each), redo: history.redo.map(each) };
858
+ }
859
+ function withoutTrack(tracks, trackId) {
860
+ return tracks.some((track) => track.id === trackId) ? tracks.filter((track) => track.id !== trackId) : tracks;
861
+ }
862
+ function sameTracks(a, b) {
863
+ return a === b || a.length === b.length && a.every((track, i) => track === b[i]);
864
+ }
865
+ function sameState(a, b) {
866
+ return a === b || sameTracks(a.tracks, b.tracks) && sameMixerValues(a.mixer, b.mixer);
867
+ }
868
+ function stateWithoutTrack(trackId) {
869
+ const mixers = /* @__PURE__ */ new Map();
870
+ const mixerWithout = (mixer) => {
871
+ const known = mixers.get(mixer);
872
+ if (known) return known;
873
+ const next = withoutEntry(mixer, trackId);
874
+ mixers.set(mixer, next);
875
+ return next;
876
+ };
877
+ return (state) => {
878
+ const tracks = withoutTrack(state.tracks, trackId);
879
+ const mixer = mixerWithout(state.mixer);
880
+ return tracks === state.tracks && mixer === state.mixer ? state : { tracks, mixer };
881
+ };
882
+ }
883
+ function dropRepeats(history, current) {
884
+ const undo = [];
885
+ for (const step of history.undo) {
886
+ if (undo.length === 0 || !sameState(undo[undo.length - 1], step)) undo.push(step);
887
+ }
888
+ while (undo.length > 0 && sameState(undo[undo.length - 1], current)) undo.pop();
889
+ const redoFromCurrent = [];
890
+ for (let i = history.redo.length - 1; i >= 0; i--) {
891
+ const step = history.redo[i];
892
+ const before = redoFromCurrent.length > 0 ? redoFromCurrent[redoFromCurrent.length - 1] : current;
893
+ if (!sameState(before, step)) redoFromCurrent.push(step);
894
+ }
895
+ if (undo.length === history.undo.length && redoFromCurrent.length === history.redo.length) {
896
+ return history;
897
+ }
898
+ return { undo, redo: redoFromCurrent.reverse() };
899
+ }
900
+ function sameSteps(a, b) {
901
+ 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]);
902
+ }
903
+ function mapHistoryStates(history, map) {
904
+ return { undo: history.undo.map(map), redo: history.redo.map(map) };
905
+ }
906
+ function purgeTrack(history, current, trackId, base = null) {
907
+ const strip = stateWithoutTrack(trackId);
908
+ const nextCurrent = strip(current);
909
+ const nextBase = base && strip(base);
910
+ const stripped = dropRepeats(mapHistoryStates(history, strip), nextBase ?? nextCurrent);
911
+ return {
912
+ history: sameSteps(stripped, history) ? history : stripped,
913
+ current: nextCurrent,
914
+ base: nextBase
915
+ };
916
+ }
917
+ function stateWithMixerValues(trackId, patch) {
918
+ const mixers = /* @__PURE__ */ new Map();
919
+ const states = /* @__PURE__ */ new Map();
920
+ return (state) => {
921
+ const known = states.get(state);
922
+ if (known) return known;
923
+ let mixer = mixers.get(state.mixer);
924
+ if (!mixer) {
925
+ mixer = setMixerValues(state.mixer, trackId, patch);
926
+ mixers.set(state.mixer, mixer);
927
+ }
928
+ const next = mixer === state.mixer ? state : { ...state, mixer };
929
+ states.set(state, next);
930
+ return next;
931
+ };
932
+ }
933
+ function writeThrough(history, base, current, write) {
934
+ const nextBase = base && { ...base, state: write(base.state) };
935
+ return {
936
+ history: dropRepeats(mapHistoryStates(history, write), nextBase?.state ?? current),
937
+ base: nextBase
938
+ };
939
+ }
940
+
842
941
  // src/arrangement/ticks.ts
843
942
  var import_core5 = require("@waveform-playlist/core");
844
943
  function forLoadedClips(change) {
@@ -919,14 +1018,30 @@ function duplicateTrackId(trackId) {
919
1018
  // src/arrangement/Arrangement.ts
920
1019
  var DEFAULT_MIN_DURATION_SECONDS = 0.1;
921
1020
  var DEFAULT_UNDO_LIMIT = 100;
1021
+ var EMPTY_RECORDING = /* @__PURE__ */ new Set();
922
1022
  var ArrangementImpl = class {
923
1023
  constructor(options = {}) {
924
1024
  this._tracks = [];
925
1025
  this._tracksVersion = 0;
926
1026
  this._mixerVersion = 0;
927
1027
  this._mixer = EMPTY_MIXER;
1028
+ /**
1029
+ * The ids of the tracks a take is recording onto: outside the history and
1030
+ * every saved state (#843). Replaced on a change, never written to.
1031
+ */
1032
+ this._recordingTrackIds = EMPTY_RECORDING;
928
1033
  this._history = EMPTY_HISTORY;
929
1034
  this._transaction = null;
1035
+ /**
1036
+ * The gesture of the top undo step, while a change of that gesture may
1037
+ * still join it: its id, and the history from before its step was pushed
1038
+ * (put back when the gesture returns to where it began, with the redo
1039
+ * states and any step the undo limit evicted). Set when a gesture change
1040
+ * pushes the step, cleared by anything else that touches the history
1041
+ * (#843). `before` is kept in step with the history: the time base, the
1042
+ * switch to loaded clips and `undoable: false` reach it too.
1043
+ */
1044
+ this._gesture = null;
930
1045
  this._subscribers = /* @__PURE__ */ new Set();
931
1046
  this._listeners = /* @__PURE__ */ new Set();
932
1047
  this._pending = [];
@@ -1029,17 +1144,19 @@ var ArrangementImpl = class {
1029
1144
  }
1030
1145
  const prepare = tickPreparer(timeBase, this._tickPpqn);
1031
1146
  const tracks = prepare(this._tracks);
1032
- const history = {
1033
- undo: this._history.undo.map(prepare),
1034
- redo: this._history.redo.map(prepare)
1147
+ const history = mapHistoryTracks(this._history, prepare);
1148
+ const gesture = this._gesture && {
1149
+ ...this._gesture,
1150
+ before: mapHistoryTracks(this._gesture.before, prepare)
1035
1151
  };
1036
1152
  const transaction = this._transaction && {
1037
1153
  ...this._transaction,
1038
- tracks: prepare(this._transaction.tracks)
1154
+ state: withStateTracks(this._transaction.state, prepare(this._transaction.state.tracks))
1039
1155
  };
1040
1156
  this._timeBase = timeBase;
1041
1157
  this._tickPpqn = timeBase.ppqn;
1042
1158
  this._history = history;
1159
+ this._gesture = gesture;
1043
1160
  this._transaction = transaction;
1044
1161
  if (tracks !== this._tracks) {
1045
1162
  this._tracks = tracks;
@@ -1061,22 +1178,24 @@ var ArrangementImpl = class {
1061
1178
  }
1062
1179
  undo() {
1063
1180
  this._abortOpenTransaction();
1064
- const step = undoStep(this._history, this._tracks);
1181
+ this._gesture = null;
1182
+ const step = undoStep(this._history, this._currentState());
1065
1183
  if (!step) return;
1066
1184
  this._history = step.history;
1067
- this._restoreTracks(step.tracks);
1185
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1068
1186
  }
1069
1187
  redo() {
1070
1188
  this._abortOpenTransaction();
1071
- const step = redoStep(this._history, this._tracks);
1189
+ this._gesture = null;
1190
+ const step = redoStep(this._history, this._currentState());
1072
1191
  if (!step) return;
1073
1192
  this._history = step.history;
1074
- this._restoreTracks(step.tracks);
1193
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1075
1194
  }
1076
1195
  clearHistory() {
1077
1196
  const hadHistory = this.canUndo || this.canRedo;
1078
1197
  this._history = EMPTY_HISTORY;
1079
- this._mixer = retainMixer(this._mixer, this._heldStates());
1198
+ this._gesture = null;
1080
1199
  if (hadHistory) this._emit({ type: "history" });
1081
1200
  }
1082
1201
  beginTransaction() {
@@ -1086,7 +1205,8 @@ var ArrangementImpl = class {
1086
1205
  );
1087
1206
  }
1088
1207
  const replaced = this._transaction !== null;
1089
- this._transaction = beginBase(this._tracks);
1208
+ this._transaction = beginBase(this._currentState());
1209
+ this._gesture = null;
1090
1210
  if (replaced) this._resyncSourcesQuietly();
1091
1211
  }
1092
1212
  commitTransaction() {
@@ -1096,8 +1216,9 @@ var ArrangementImpl = class {
1096
1216
  return;
1097
1217
  }
1098
1218
  this._transaction = null;
1099
- if (transaction.changed && !sameTracks(transaction.tracks, this._tracks)) {
1100
- this._pushStep(transaction.tracks);
1219
+ this._gesture = null;
1220
+ if (transaction.changed && !sameState(transaction.state, this._currentState())) {
1221
+ this._pushStep(transaction.state);
1101
1222
  this._emit({ type: "history" });
1102
1223
  }
1103
1224
  }
@@ -1122,9 +1243,9 @@ var ArrangementImpl = class {
1122
1243
  tracks.forEach(assertUniqueClipIds);
1123
1244
  this._transaction = null;
1124
1245
  this._history = EMPTY_HISTORY;
1125
- const kept = retainMixer(this._mixer, [this._tracks]);
1246
+ this._gesture = null;
1126
1247
  this._tracks = tracks.map((track) => this._ingest(track));
1127
- this._mixer = retainMixer(seedMixer(kept, this._tracks), [this._tracks]);
1248
+ this._mixer = mixerOf(this._mixer, this._tracks);
1128
1249
  this._tracksVersion++;
1129
1250
  this._emit({ type: "set" });
1130
1251
  }
@@ -1152,6 +1273,7 @@ var ArrangementImpl = class {
1152
1273
  if (!this._tracks.some((t) => t.id === trackId)) return;
1153
1274
  this._pushUndoSnapshot();
1154
1275
  this._tracks = this._tracks.filter((t) => t.id !== trackId);
1276
+ this._mixer = withoutEntry(this._mixer, trackId);
1155
1277
  this._tracksVersion++;
1156
1278
  this._emit({ type: "remove", trackId });
1157
1279
  }
@@ -1198,7 +1320,6 @@ var ArrangementImpl = class {
1198
1320
  const enriched = this._ingest(track, new Set(current.clips));
1199
1321
  this._tracks = this._tracks.map((t) => t.id === trackId ? enriched : t);
1200
1322
  this._mixer = seedMixer(carryMixerEntry(this._mixer, trackId, enriched.id), [enriched]);
1201
- if (enriched.id !== trackId) this._mixer = retainMixer(this._mixer, this._heldStates());
1202
1323
  this._tracksVersion++;
1203
1324
  this._emit({ type: "update", trackId, previous: current });
1204
1325
  }
@@ -1235,19 +1356,46 @@ var ArrangementImpl = class {
1235
1356
  //
1236
1357
  // Each replaces the Map and one entry, bumps mixerVersion and announces
1237
1358
  // the change. NOT structural changes: no track object is replaced,
1238
- // tracksVersion stays (#501), and they are not undo steps. An id that is
1239
- // not in the current tracks does nothing.
1240
- setTrackVolume(trackId, volume) {
1241
- this._setMixerValues(trackId, { volume });
1359
+ // tracksVersion stays (#501). Each call is an undo step; inside an open
1360
+ // transaction it is part of it, like any other change. Calls with one
1361
+ // `gesture` id, one after another, are one step; `undoable: false` is no
1362
+ // step and is written into every saved state (#843). A value that does
1363
+ // not change, or an id that is not in the current tracks, does nothing.
1364
+ setTrackVolume(trackId, volume, options) {
1365
+ this._setMixerValues(trackId, { volume }, options);
1366
+ }
1367
+ setTrackMute(trackId, muted, options) {
1368
+ this._setMixerValues(trackId, { muted }, options);
1242
1369
  }
1243
- setTrackMute(trackId, muted) {
1244
- this._setMixerValues(trackId, { muted });
1370
+ setTrackSolo(trackId, soloed, options) {
1371
+ this._setMixerValues(trackId, { soloed }, options);
1245
1372
  }
1246
- setTrackSolo(trackId, soloed) {
1247
- this._setMixerValues(trackId, { soloed });
1373
+ setTrackPan(trackId, pan, options) {
1374
+ this._setMixerValues(trackId, { pan }, options);
1248
1375
  }
1249
- setTrackPan(trackId, pan) {
1250
- this._setMixerValues(trackId, { pan });
1376
+ // ---------------------------------------------------------------------------
1377
+ // Recording mute (the track a take is recording onto)
1378
+ // ---------------------------------------------------------------------------
1379
+ //
1380
+ // The track a take is recording onto plays muted: the engine's projection
1381
+ // and the adapter get `muted: stored || recording`, and the stored mute in
1382
+ // `getMixer()` is not touched. The set is outside the history: the take's
1383
+ // mute is no undo step, and undo, redo, abort, `setTracks` and
1384
+ // `clearHistory` leave it alone (#843).
1385
+ /** The ids of the tracks a take is recording onto. The same Set until it changes; never written to. */
1386
+ get recordingTrackIds() {
1387
+ return this._recordingTrackIds;
1388
+ }
1389
+ /**
1390
+ * Mark a track as the one a take is recording onto, or clear it. An id
1391
+ * that is not in the current tracks is recorded too, and mutes nothing
1392
+ * until a track of that id enters. The same value does nothing.
1393
+ */
1394
+ setTrackRecording(trackId, recording) {
1395
+ if (this._recordingTrackIds.has(trackId) === recording) return;
1396
+ this._recordingTrackIds = recording ? /* @__PURE__ */ new Set([...this._recordingTrackIds, trackId]) : new Set([...this._recordingTrackIds].filter((id) => id !== trackId));
1397
+ if (this._mixer.has(trackId)) this._mixerVersion++;
1398
+ this._emit({ type: "recording", trackId });
1251
1399
  }
1252
1400
  // ---------------------------------------------------------------------------
1253
1401
  // Tempo
@@ -1310,37 +1458,97 @@ var ArrangementImpl = class {
1310
1458
  })
1311
1459
  };
1312
1460
  }
1313
- _setMixerValues(trackId, values) {
1461
+ _setMixerValues(trackId, values, options = {}) {
1314
1462
  if (!this._tracks.some((t) => t.id === trackId)) return;
1315
1463
  const next = setMixerValues(this._mixer, trackId, values);
1316
1464
  if (next === this._mixer) return;
1317
- this._mixer = next;
1465
+ if (options.undoable === false) {
1466
+ this._writeMixerThrough(trackId, values, next);
1467
+ } else {
1468
+ this._recordMixerChange(options.gesture);
1469
+ this._mixer = next;
1470
+ this._dropReturnedGestureStep();
1471
+ }
1318
1472
  this._mixerVersion++;
1319
1473
  this._emit({ type: "mixer", trackId, values });
1320
1474
  }
1321
- /** Every tracks array the arrangement can still return to, and the current one. */
1475
+ /**
1476
+ * The undo step of a mixer change about to be made: part of an open
1477
+ * transaction, the top step when it was pushed by the same gesture and
1478
+ * nothing has touched the history since, or else a new step (which becomes
1479
+ * that gesture's).
1480
+ */
1481
+ _recordMixerChange(gesture) {
1482
+ if (this._transaction) {
1483
+ this._markTransactionChanged();
1484
+ return;
1485
+ }
1486
+ if (gesture !== void 0 && this._gesture?.id === gesture) return;
1487
+ const before = this._history;
1488
+ this._pushStep(this._currentState());
1489
+ if (gesture !== void 0 && this.canUndo) this._gesture = { id: gesture, before };
1490
+ }
1491
+ /**
1492
+ * A gesture that came back to the state its step holds leaves no step, and
1493
+ * the history is the one it found: the redo states its first change
1494
+ * cleared come back (as with a transaction that changes nothing).
1495
+ */
1496
+ _dropReturnedGestureStep() {
1497
+ const gesture = this._gesture;
1498
+ if (!gesture) return;
1499
+ const { undo } = this._history;
1500
+ if (!sameState(undo[undo.length - 1], this._currentState())) return;
1501
+ this._history = dropRepeats(gesture.before, this._currentState());
1502
+ this._gesture = null;
1503
+ }
1504
+ /**
1505
+ * `undoable: false`: the value in the current mix and in every saved state
1506
+ * that has an entry for the track, so that no undo, redo or abort brings
1507
+ * the old one back. A step that no longer differs from the state next to
1508
+ * it goes; when that was the step a gesture joins and no equal step is
1509
+ * left on top, the gesture's next change pushes a new one.
1510
+ */
1511
+ _writeMixerThrough(trackId, values, next) {
1512
+ this._mixer = next;
1513
+ const write = stateWithMixerValues(trackId, values);
1514
+ const { undo } = this._history;
1515
+ const top = undo.length > 0 ? write(undo[undo.length - 1]) : void 0;
1516
+ const written = writeThrough(this._history, this._transaction, this._currentState(), write);
1517
+ this._history = written.history;
1518
+ this._transaction = written.base;
1519
+ if (this._gesture) {
1520
+ const newTop = this._history.undo[this._history.undo.length - 1];
1521
+ this._gesture = newTop !== void 0 && top !== void 0 && sameState(newTop, top) ? { ...this._gesture, before: mapHistoryStates(this._gesture.before, write) } : null;
1522
+ }
1523
+ }
1524
+ /** The tracks and the mix as they are now: what an undo step or a transaction's base saves. */
1525
+ _currentState() {
1526
+ return { tracks: this._tracks, mixer: this._mixer };
1527
+ }
1528
+ /** Every tracks array the arrangement can still return to, and the current one (for the sources). */
1322
1529
  _heldStates() {
1323
1530
  return [
1324
1531
  this._tracks,
1325
- ...this._history.undo,
1326
- ...this._history.redo,
1327
- ...this._transaction ? [this._transaction.tracks] : []
1532
+ ...this._history.undo.map((state) => state.tracks),
1533
+ ...this._history.redo.map((state) => state.tracks),
1534
+ ...this._gesture ? this._gesture.before.undo.map((state) => state.tracks) : [],
1535
+ ...this._gesture ? this._gesture.before.redo.map((state) => state.tracks) : [],
1536
+ ...this._transaction ? [this._transaction.state.tracks] : []
1328
1537
  ];
1329
1538
  }
1330
1539
  /** `removeTrack` with `undoable: false`: out of the current tracks and out of every stored state. */
1331
1540
  _removeTrackEverywhere(trackId) {
1332
1541
  const wasThere = this._tracks.some((t) => t.id === trackId);
1333
- const purged = purgeTrack(this._history, this._tracks, trackId);
1542
+ const base = this._transaction?.state ?? null;
1543
+ const purged = purgeTrack(this._history, this._currentState(), trackId, base);
1334
1544
  const historyChanged = purged.history !== this._history;
1335
1545
  this._history = purged.history;
1336
- if (this._transaction) {
1337
- this._transaction = {
1338
- ...this._transaction,
1339
- tracks: withoutTrack(this._transaction.tracks, trackId)
1340
- };
1546
+ this._gesture = null;
1547
+ if (this._transaction && purged.base) {
1548
+ this._transaction = { ...this._transaction, state: purged.base };
1341
1549
  }
1342
- this._tracks = purged.current;
1343
- this._mixer = retainMixer(this._mixer, this._heldStates());
1550
+ this._tracks = purged.current.tracks;
1551
+ this._mixer = purged.current.mixer;
1344
1552
  if (wasThere) {
1345
1553
  this._tracksVersion++;
1346
1554
  this._emit({ type: "remove", trackId });
@@ -1353,32 +1561,55 @@ var ArrangementImpl = class {
1353
1561
  /** The state before a change: an undo step, or the mark that an open transaction changed. */
1354
1562
  _pushUndoSnapshot() {
1355
1563
  if (this._transaction) {
1356
- if (!this._transaction.changed) {
1357
- this._transaction = { ...this._transaction, changed: true };
1358
- }
1564
+ this._markTransactionChanged();
1359
1565
  return;
1360
1566
  }
1361
- this._pushStep(this._tracks);
1567
+ this._pushStep(this._currentState());
1568
+ }
1569
+ /** An open transaction has changed something. */
1570
+ _markTransactionChanged() {
1571
+ if (this._transaction && !this._transaction.changed) {
1572
+ this._transaction = { ...this._transaction, changed: true };
1573
+ }
1362
1574
  }
1363
- /** Add an undo step. The history may let go of states with it (the redo stack, a step past the limit). */
1364
- _pushStep(tracks) {
1365
- const letsGo = this._history.redo.length > 0 || this._history.undo.length >= this.undoLimit;
1366
- this._history = pushStep(this._history, tracks, this.undoLimit);
1367
- if (letsGo) this._mixer = retainMixer(this._mixer, this._heldStates());
1575
+ /** Add an undo step. No gesture joins it (a gesture change that pushes sets its id after). */
1576
+ _pushStep(state) {
1577
+ this._history = pushStep(this._history, state, this.undoLimit);
1578
+ this._gesture = null;
1368
1579
  }
1369
- _restoreTracks(snapshot) {
1580
+ /**
1581
+ * Make a saved state the current one (an undo, a redo, an abort) and
1582
+ * announce it: `'restore'` when the tracks array is another one (with the
1583
+ * mix from before, which may differ too), `'restoreMix'` when only mixer
1584
+ * values differ. `mixerVersion` counts it when a track that is in both
1585
+ * states has other values; an entry that comes or goes with its track is
1586
+ * not a mixer change. Returns false when nothing differed and nothing was
1587
+ * announced.
1588
+ */
1589
+ _restoreState(state) {
1370
1590
  const previous = this._tracks;
1371
- this._tracks = snapshot;
1372
- this._tracksVersion++;
1373
- this._emit({ type: "restore", previous });
1591
+ const previousMixer = this._mixer;
1592
+ const tracksChanged = state.tracks !== previous;
1593
+ const mixChanged = mixerChanges(previousMixer, state.mixer).length > 0;
1594
+ this._tracks = state.tracks;
1595
+ this._mixer = state.mixer;
1596
+ if (mixChanged) this._mixerVersion++;
1597
+ if (tracksChanged) {
1598
+ this._tracksVersion++;
1599
+ this._emit({ type: "restore", previous, previousMixer });
1600
+ } else if (mixChanged) {
1601
+ this._emit({ type: "restoreMix", previousMixer });
1602
+ }
1603
+ return tracksChanged || mixChanged;
1374
1604
  }
1375
- /** Ends an open transaction, restoring the tracks it began from if it changed them. */
1605
+ /** Ends an open transaction, restoring the state it began from if it changed anything. */
1376
1606
  _abortOpenTransaction() {
1377
1607
  const transaction = this._transaction;
1378
1608
  if (!transaction) return;
1379
1609
  this._transaction = null;
1610
+ this._gesture = null;
1380
1611
  if (transaction.changed) {
1381
- this._restoreTracks(transaction.tracks);
1612
+ this._restoreState(transaction.state);
1382
1613
  }
1383
1614
  }
1384
1615
  /** Declare the sources every state names, and switch the clips whose audio is there. */
@@ -1402,12 +1633,19 @@ var ArrangementImpl = class {
1402
1633
  const pass = this._sources.beginPass((startSample) => this._tickAt(startSample));
1403
1634
  const previous = this._tracks;
1404
1635
  this._tracks = pass.current(previous);
1405
- this._history = {
1406
- undo: this._history.undo.map(pass.stored),
1407
- redo: this._history.redo.map(pass.stored)
1408
- };
1636
+ this._history = mapHistoryTracks(this._history, pass.stored);
1637
+ if (this._gesture) {
1638
+ this._gesture = {
1639
+ ...this._gesture,
1640
+ before: mapHistoryTracks(this._gesture.before, pass.stored)
1641
+ };
1642
+ }
1409
1643
  if (this._transaction) {
1410
- this._transaction = { ...this._transaction, tracks: pass.stored(this._transaction.tracks) };
1644
+ const { state } = this._transaction;
1645
+ this._transaction = {
1646
+ ...this._transaction,
1647
+ state: withStateTracks(state, pass.stored(state.tracks))
1648
+ };
1411
1649
  }
1412
1650
  return this._tracks === previous ? null : previous;
1413
1651
  }
@@ -1433,11 +1671,9 @@ var ArrangementImpl = class {
1433
1671
  * always received that error).
1434
1672
  */
1435
1673
  _emit(change) {
1436
- const switched = change.type === "mixer" ? null : this._syncSources();
1674
+ const mixOnly = change.type === "mixer" || change.type === "restoreMix" || change.type === "recording";
1675
+ const switched = mixOnly ? null : this._syncSources();
1437
1676
  freezeTracksInDevelopment(this._tracks);
1438
- if (change.type === "remove" || change.type === "restore") {
1439
- this._mixer = retainMixer(this._mixer, this._heldStates());
1440
- }
1441
1677
  const at = {
1442
1678
  tracksVersion: this._tracksVersion,
1443
1679
  mixerVersion: this._mixerVersion
@@ -1511,8 +1747,8 @@ var AdapterSync = class {
1511
1747
  }
1512
1748
  /**
1513
1749
  * The arrangement's tracks, each without its loading clips and with the
1514
- * current mixer values in its fields: what every adapter track call and
1515
- * `getState().tracks` use. The same
1750
+ * current mixer values in its fields (muted while a take records onto it): what every
1751
+ * adapter track call and `getState().tracks` use. The same
1516
1752
  * array until the tracks or the mix change, and the same track object
1517
1753
  * while neither that track nor its mix changed.
1518
1754
  */
@@ -1553,13 +1789,30 @@ var AdapterSync = class {
1553
1789
  }
1554
1790
  return true;
1555
1791
  }
1556
- /** A mixer value changed: the adapter's setter for it. */
1792
+ /**
1793
+ * A mixer value changed: the adapter's setter for it. The mute sent is the
1794
+ * one the track plays with: muted while a take records onto the track,
1795
+ * whatever the value given.
1796
+ */
1557
1797
  sendMixerValues(trackId, values) {
1558
1798
  if (values.volume !== void 0) this._adapter?.setTrackVolume(trackId, values.volume);
1559
- if (values.muted !== void 0) this._adapter?.setTrackMute(trackId, values.muted);
1799
+ if (values.muted !== void 0) {
1800
+ this._adapter?.setTrackMute(trackId, values.muted || this._recordingNow(trackId));
1801
+ }
1560
1802
  if (values.soloed !== void 0) this._adapter?.setTrackSolo(trackId, values.soloed);
1561
1803
  if (values.pan !== void 0) this._adapter?.setTrackPan(trackId, values.pan);
1562
1804
  }
1805
+ /**
1806
+ * After an undo, a redo or an abort brought back another mix: the setter
1807
+ * for each field that differs, of each current track that had an entry in
1808
+ * `previousMixer` too. A track that entered with the restore was sent
1809
+ * whole, with its mix in its fields.
1810
+ */
1811
+ sendMixerDiff(previousMixer) {
1812
+ for (const { trackId, values } of mixerChanges(previousMixer, this._arrangement.getMixer())) {
1813
+ this.sendMixerValues(trackId, values);
1814
+ }
1815
+ }
1563
1816
  /**
1564
1817
  * The adapter call for a change to the tracks.
1565
1818
  *
@@ -1690,13 +1943,26 @@ var AdapterSync = class {
1690
1943
  this._loadedMemo.set(track, loaded);
1691
1944
  return loaded;
1692
1945
  }
1693
- /** One track with its current mixer values in its fields (memoized per track object). */
1946
+ /**
1947
+ * True when a take is recording onto the track and it is a current track
1948
+ * (it has an entry in the current mix): it plays muted. A recording id that
1949
+ * is not in the tracks mutes nothing until it enters.
1950
+ */
1951
+ _recordingNow(trackId) {
1952
+ return this._arrangement.recordingTrackIds.has(trackId) && this._arrangement.getMixer().has(trackId);
1953
+ }
1954
+ /**
1955
+ * One track with its current mixer values in its fields, muted while a
1956
+ * take records onto it (memoized per track object).
1957
+ */
1694
1958
  _withMix(track) {
1695
1959
  const values = this._arrangement.getMixer().get(track.id);
1960
+ const recording = this._recordingNow(track.id);
1696
1961
  const memo = this._withMixMemo.get(track);
1697
- if (memo && memo.values === values) return memo.merged;
1698
- const merged = freezeTrackInDevelopment(withMixerValues(track, values));
1699
- this._withMixMemo.set(track, { values, merged });
1962
+ if (memo && memo.values === values && memo.recording === recording) return memo.merged;
1963
+ const playing = recording && values && !values.muted ? { ...values, muted: true } : values;
1964
+ const merged = freezeTrackInDevelopment(withMixerValues(track, playing));
1965
+ this._withMixMemo.set(track, { values, recording, merged });
1700
1966
  return merged;
1701
1967
  }
1702
1968
  };
@@ -2071,9 +2337,13 @@ var PlaylistEngine = class {
2071
2337
  //
2072
2338
  // Each setter writes the value into the arrangement's mixer part (so a later
2073
2339
  // adapter rebuild via setTracks keeps the value, and mixerVersion is bumped);
2074
- // the engine's listener then calls the adapter and emits statechange. For a
2075
- // track the arrangement does not hold nothing changes, and the adapter is
2076
- // still called from here (a consumer may target a track that has not been
2340
+ // the engine's listener then calls the adapter and emits statechange. Each
2341
+ // call is an undo step, unless `options` says otherwise: the same `gesture`
2342
+ // id joins one step, `undoable: false` is no step (`MixerChangeOptions`).
2343
+ //
2344
+ // When the arrangement changes nothing (a track it does not hold, or the
2345
+ // value the track already has), the adapter is still called from here,
2346
+ // with no statechange (a consumer may target a track that has not been
2077
2347
  // ingested yet). These are NOT structural
2078
2348
  // changes, so they deliberately do NOT bump tracksVersion — that would make
2079
2349
  // tracksVersion-gated consumers re-run expensive structural work (audio-graph
@@ -2081,22 +2351,48 @@ var PlaylistEngine = class {
2081
2351
  // cache a track snapshot (e.g. @dawcore/components' <daw-editor>) watch
2082
2352
  // mixerVersion to refresh that cache cheaply, so an unrelated setTracks
2083
2353
  // rebuild no longer reverts live mixer state (#501).
2084
- setTrackVolume(trackId, volume) {
2085
- const changed = this._mixerChanged(() => this._arrangement.setTrackVolume(trackId, volume));
2354
+ setTrackVolume(trackId, volume, options) {
2355
+ const changed = this._mixerChanged(
2356
+ () => this._arrangement.setTrackVolume(trackId, volume, options)
2357
+ );
2086
2358
  if (!changed && !this._disposed) this._adapter?.setTrackVolume(trackId, volume);
2087
2359
  }
2088
- setTrackMute(trackId, muted) {
2089
- const changed = this._mixerChanged(() => this._arrangement.setTrackMute(trackId, muted));
2090
- if (!changed && !this._disposed) this._adapter?.setTrackMute(trackId, muted);
2360
+ setTrackMute(trackId, muted, options) {
2361
+ const changed = this._mixerChanged(
2362
+ () => this._arrangement.setTrackMute(trackId, muted, options)
2363
+ );
2364
+ if (!changed && !this._disposed) this._adapterSync.sendMixerValues(trackId, { muted });
2091
2365
  }
2092
- setTrackSolo(trackId, soloed) {
2093
- const changed = this._mixerChanged(() => this._arrangement.setTrackSolo(trackId, soloed));
2366
+ setTrackSolo(trackId, soloed, options) {
2367
+ const changed = this._mixerChanged(
2368
+ () => this._arrangement.setTrackSolo(trackId, soloed, options)
2369
+ );
2094
2370
  if (!changed && !this._disposed) this._adapter?.setTrackSolo(trackId, soloed);
2095
2371
  }
2096
- setTrackPan(trackId, pan) {
2097
- const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan));
2372
+ setTrackPan(trackId, pan, options) {
2373
+ const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan, options));
2098
2374
  if (!changed && !this._disposed) this._adapter?.setTrackPan(trackId, pan);
2099
2375
  }
2376
+ /**
2377
+ * Mark a track as the one a take is recording onto, or clear it when the
2378
+ * take ends: the arrangement's `setTrackRecording`. That track plays muted
2379
+ * (here and in the adapter) while its mute in the mix stays as it is; the
2380
+ * take's mute is not an undo step. The arrangement's `'recording'`
2381
+ * announcement brings the adapter in step, so there is no adapter call of
2382
+ * its own here; a disposed engine changes the arrangement only.
2383
+ */
2384
+ setTrackRecording(trackId, recording) {
2385
+ this._arrangement.setTrackRecording(trackId, recording);
2386
+ }
2387
+ /**
2388
+ * The tracks a take is recording onto, read from the arrangement (the
2389
+ * owner; no copy). The same set object until it changes; never write to
2390
+ * it, use `setTrackRecording`. Such a track plays muted, so a UI can
2391
+ * disable its mute control while the id is in the set.
2392
+ */
2393
+ get recordingTrackIds() {
2394
+ return this._arrangement.recordingTrackIds;
2395
+ }
2100
2396
  // ---------------------------------------------------------------------------
2101
2397
  // Zoom
2102
2398
  // ---------------------------------------------------------------------------
@@ -2169,6 +2465,14 @@ var PlaylistEngine = class {
2169
2465
  case "mixer":
2170
2466
  this._adapterSync.sendMixerValues(change.trackId, change.values);
2171
2467
  break;
2468
+ case "restoreMix":
2469
+ this._adapterSync.sendMixerDiff(change.previousMixer);
2470
+ break;
2471
+ case "recording": {
2472
+ const values = this._arrangement.getMixer().get(change.trackId);
2473
+ if (values) this._adapterSync.sendMixerValues(change.trackId, { muted: values.muted });
2474
+ break;
2475
+ }
2172
2476
  case "reorder":
2173
2477
  // not audible: adapter nodes are keyed by track id
2174
2478
  case "history":
@@ -2181,6 +2485,7 @@ var PlaylistEngine = class {
2181
2485
  this._followLoad(change, at);
2182
2486
  } else {
2183
2487
  this._adapterSync.follow(change, at);
2488
+ if (change.type === "restore") this._adapterSync.sendMixerDiff(change.previousMixer);
2184
2489
  }
2185
2490
  }
2186
2491
  this._emitStateChange();