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

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/v3"
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/v3"
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,39 @@ 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();
981
+ function insertIndex(index, length) {
982
+ if (index === void 0) return length;
983
+ if (typeof index !== "number" || !Number.isFinite(index)) {
984
+ throw new TypeError(
985
+ `[waveform-playlist] addTrack: index must be a finite number, got ${String(index)}`
986
+ );
987
+ }
988
+ return Math.max(0, Math.min(Math.trunc(index), length));
989
+ }
881
990
  var ArrangementImpl = class {
882
991
  constructor(options = {}) {
883
992
  this._tracks = [];
884
993
  this._tracksVersion = 0;
885
994
  this._mixerVersion = 0;
886
995
  this._mixer = EMPTY_MIXER;
996
+ /**
997
+ * The ids of the tracks a take is recording onto: outside the history and
998
+ * every saved state (#843). Replaced on a change, never written to.
999
+ */
1000
+ this._recordingTrackIds = EMPTY_RECORDING;
887
1001
  this._history = EMPTY_HISTORY;
888
1002
  this._transaction = null;
1003
+ /**
1004
+ * The gesture of the top undo step, while a change of that gesture may
1005
+ * still join it: its id, and the history from before its step was pushed
1006
+ * (put back when the gesture returns to where it began, with the redo
1007
+ * states and any step the undo limit evicted). Set when a gesture change
1008
+ * pushes the step, cleared by anything else that touches the history
1009
+ * (#843). `before` is kept in step with the history: the time base, the
1010
+ * switch to loaded clips and `undoable: false` reach it too.
1011
+ */
1012
+ this._gesture = null;
889
1013
  this._subscribers = /* @__PURE__ */ new Set();
890
1014
  this._listeners = /* @__PURE__ */ new Set();
891
1015
  this._pending = [];
@@ -988,17 +1112,19 @@ var ArrangementImpl = class {
988
1112
  }
989
1113
  const prepare = tickPreparer(timeBase, this._tickPpqn);
990
1114
  const tracks = prepare(this._tracks);
991
- const history = {
992
- undo: this._history.undo.map(prepare),
993
- redo: this._history.redo.map(prepare)
1115
+ const history = mapHistoryTracks(this._history, prepare);
1116
+ const gesture = this._gesture && {
1117
+ ...this._gesture,
1118
+ before: mapHistoryTracks(this._gesture.before, prepare)
994
1119
  };
995
1120
  const transaction = this._transaction && {
996
1121
  ...this._transaction,
997
- tracks: prepare(this._transaction.tracks)
1122
+ state: withStateTracks(this._transaction.state, prepare(this._transaction.state.tracks))
998
1123
  };
999
1124
  this._timeBase = timeBase;
1000
1125
  this._tickPpqn = timeBase.ppqn;
1001
1126
  this._history = history;
1127
+ this._gesture = gesture;
1002
1128
  this._transaction = transaction;
1003
1129
  if (tracks !== this._tracks) {
1004
1130
  this._tracks = tracks;
@@ -1020,22 +1146,24 @@ var ArrangementImpl = class {
1020
1146
  }
1021
1147
  undo() {
1022
1148
  this._abortOpenTransaction();
1023
- const step = undoStep(this._history, this._tracks);
1149
+ this._gesture = null;
1150
+ const step = undoStep(this._history, this._currentState());
1024
1151
  if (!step) return;
1025
1152
  this._history = step.history;
1026
- this._restoreTracks(step.tracks);
1153
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1027
1154
  }
1028
1155
  redo() {
1029
1156
  this._abortOpenTransaction();
1030
- const step = redoStep(this._history, this._tracks);
1157
+ this._gesture = null;
1158
+ const step = redoStep(this._history, this._currentState());
1031
1159
  if (!step) return;
1032
1160
  this._history = step.history;
1033
- this._restoreTracks(step.tracks);
1161
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1034
1162
  }
1035
1163
  clearHistory() {
1036
1164
  const hadHistory = this.canUndo || this.canRedo;
1037
1165
  this._history = EMPTY_HISTORY;
1038
- this._mixer = retainMixer(this._mixer, this._heldStates());
1166
+ this._gesture = null;
1039
1167
  if (hadHistory) this._emit({ type: "history" });
1040
1168
  }
1041
1169
  beginTransaction() {
@@ -1045,7 +1173,8 @@ var ArrangementImpl = class {
1045
1173
  );
1046
1174
  }
1047
1175
  const replaced = this._transaction !== null;
1048
- this._transaction = beginBase(this._tracks);
1176
+ this._transaction = beginBase(this._currentState());
1177
+ this._gesture = null;
1049
1178
  if (replaced) this._resyncSourcesQuietly();
1050
1179
  }
1051
1180
  commitTransaction() {
@@ -1055,8 +1184,9 @@ var ArrangementImpl = class {
1055
1184
  return;
1056
1185
  }
1057
1186
  this._transaction = null;
1058
- if (transaction.changed && !sameTracks(transaction.tracks, this._tracks)) {
1059
- this._pushStep(transaction.tracks);
1187
+ this._gesture = null;
1188
+ if (transaction.changed && !sameState(transaction.state, this._currentState())) {
1189
+ this._pushStep(transaction.state);
1060
1190
  this._emit({ type: "history" });
1061
1191
  }
1062
1192
  }
@@ -1081,19 +1211,20 @@ var ArrangementImpl = class {
1081
1211
  tracks.forEach(assertUniqueClipIds);
1082
1212
  this._transaction = null;
1083
1213
  this._history = EMPTY_HISTORY;
1084
- const kept = retainMixer(this._mixer, [this._tracks]);
1214
+ this._gesture = null;
1085
1215
  this._tracks = tracks.map((track) => this._ingest(track));
1086
- this._mixer = retainMixer(seedMixer(kept, this._tracks), [this._tracks]);
1216
+ this._mixer = mixerOf(this._mixer, this._tracks);
1087
1217
  this._tracksVersion++;
1088
1218
  this._emit({ type: "set" });
1089
1219
  }
1090
- addTrack(track) {
1220
+ addTrack(track, options) {
1221
+ const index = insertIndex(options?.index, this._tracks.length);
1091
1222
  assertClipsOfTrackKind(track);
1092
1223
  assertUniqueClipIds(track);
1093
1224
  if (this._tracks.some((t) => t.id === track.id)) throw duplicateTrackId(track.id);
1094
1225
  this._pushUndoSnapshot();
1095
1226
  const enriched = this._ingest(track);
1096
- this._tracks = [...this._tracks, enriched];
1227
+ this._tracks = [...this._tracks.slice(0, index), enriched, ...this._tracks.slice(index)];
1097
1228
  this._mixer = seedMixer(this._mixer, [enriched]);
1098
1229
  this._tracksVersion++;
1099
1230
  this._emit({ type: "add", trackId: track.id });
@@ -1111,6 +1242,7 @@ var ArrangementImpl = class {
1111
1242
  if (!this._tracks.some((t) => t.id === trackId)) return;
1112
1243
  this._pushUndoSnapshot();
1113
1244
  this._tracks = this._tracks.filter((t) => t.id !== trackId);
1245
+ this._mixer = withoutEntry(this._mixer, trackId);
1114
1246
  this._tracksVersion++;
1115
1247
  this._emit({ type: "remove", trackId });
1116
1248
  }
@@ -1157,7 +1289,6 @@ var ArrangementImpl = class {
1157
1289
  const enriched = this._ingest(track, new Set(current.clips));
1158
1290
  this._tracks = this._tracks.map((t) => t.id === trackId ? enriched : t);
1159
1291
  this._mixer = seedMixer(carryMixerEntry(this._mixer, trackId, enriched.id), [enriched]);
1160
- if (enriched.id !== trackId) this._mixer = retainMixer(this._mixer, this._heldStates());
1161
1292
  this._tracksVersion++;
1162
1293
  this._emit({ type: "update", trackId, previous: current });
1163
1294
  }
@@ -1194,19 +1325,46 @@ var ArrangementImpl = class {
1194
1325
  //
1195
1326
  // Each replaces the Map and one entry, bumps mixerVersion and announces
1196
1327
  // 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 });
1328
+ // tracksVersion stays (#501). Each call is an undo step; inside an open
1329
+ // transaction it is part of it, like any other change. Calls with one
1330
+ // `gesture` id, one after another, are one step; `undoable: false` is no
1331
+ // step and is written into every saved state (#843). A value that does
1332
+ // not change, or an id that is not in the current tracks, does nothing.
1333
+ setTrackVolume(trackId, volume, options) {
1334
+ this._setMixerValues(trackId, { volume }, options);
1201
1335
  }
1202
- setTrackMute(trackId, muted) {
1203
- this._setMixerValues(trackId, { muted });
1336
+ setTrackMute(trackId, muted, options) {
1337
+ this._setMixerValues(trackId, { muted }, options);
1204
1338
  }
1205
- setTrackSolo(trackId, soloed) {
1206
- this._setMixerValues(trackId, { soloed });
1339
+ setTrackSolo(trackId, soloed, options) {
1340
+ this._setMixerValues(trackId, { soloed }, options);
1207
1341
  }
1208
- setTrackPan(trackId, pan) {
1209
- this._setMixerValues(trackId, { pan });
1342
+ setTrackPan(trackId, pan, options) {
1343
+ this._setMixerValues(trackId, { pan }, options);
1344
+ }
1345
+ // ---------------------------------------------------------------------------
1346
+ // Recording mute (the track a take is recording onto)
1347
+ // ---------------------------------------------------------------------------
1348
+ //
1349
+ // The track a take is recording onto plays muted: the engine's projection
1350
+ // and the adapter get `muted: stored || recording`, and the stored mute in
1351
+ // `getMixer()` is not touched. The set is outside the history: the take's
1352
+ // mute is no undo step, and undo, redo, abort, `setTracks` and
1353
+ // `clearHistory` leave it alone (#843).
1354
+ /** The ids of the tracks a take is recording onto. The same Set until it changes; never written to. */
1355
+ get recordingTrackIds() {
1356
+ return this._recordingTrackIds;
1357
+ }
1358
+ /**
1359
+ * Mark a track as the one a take is recording onto, or clear it. An id
1360
+ * that is not in the current tracks is recorded too, and mutes nothing
1361
+ * until a track of that id enters. The same value does nothing.
1362
+ */
1363
+ setTrackRecording(trackId, recording) {
1364
+ if (this._recordingTrackIds.has(trackId) === recording) return;
1365
+ this._recordingTrackIds = recording ? /* @__PURE__ */ new Set([...this._recordingTrackIds, trackId]) : new Set([...this._recordingTrackIds].filter((id) => id !== trackId));
1366
+ if (this._mixer.has(trackId)) this._mixerVersion++;
1367
+ this._emit({ type: "recording", trackId });
1210
1368
  }
1211
1369
  // ---------------------------------------------------------------------------
1212
1370
  // Tempo
@@ -1269,37 +1427,97 @@ var ArrangementImpl = class {
1269
1427
  })
1270
1428
  };
1271
1429
  }
1272
- _setMixerValues(trackId, values) {
1430
+ _setMixerValues(trackId, values, options = {}) {
1273
1431
  if (!this._tracks.some((t) => t.id === trackId)) return;
1274
1432
  const next = setMixerValues(this._mixer, trackId, values);
1275
1433
  if (next === this._mixer) return;
1276
- this._mixer = next;
1434
+ if (options.undoable === false) {
1435
+ this._writeMixerThrough(trackId, values, next);
1436
+ } else {
1437
+ this._recordMixerChange(options.gesture);
1438
+ this._mixer = next;
1439
+ this._dropReturnedGestureStep();
1440
+ }
1277
1441
  this._mixerVersion++;
1278
1442
  this._emit({ type: "mixer", trackId, values });
1279
1443
  }
1280
- /** Every tracks array the arrangement can still return to, and the current one. */
1444
+ /**
1445
+ * The undo step of a mixer change about to be made: part of an open
1446
+ * transaction, the top step when it was pushed by the same gesture and
1447
+ * nothing has touched the history since, or else a new step (which becomes
1448
+ * that gesture's).
1449
+ */
1450
+ _recordMixerChange(gesture) {
1451
+ if (this._transaction) {
1452
+ this._markTransactionChanged();
1453
+ return;
1454
+ }
1455
+ if (gesture !== void 0 && this._gesture?.id === gesture) return;
1456
+ const before = this._history;
1457
+ this._pushStep(this._currentState());
1458
+ if (gesture !== void 0 && this.canUndo) this._gesture = { id: gesture, before };
1459
+ }
1460
+ /**
1461
+ * A gesture that came back to the state its step holds leaves no step, and
1462
+ * the history is the one it found: the redo states its first change
1463
+ * cleared come back (as with a transaction that changes nothing).
1464
+ */
1465
+ _dropReturnedGestureStep() {
1466
+ const gesture = this._gesture;
1467
+ if (!gesture) return;
1468
+ const { undo } = this._history;
1469
+ if (!sameState(undo[undo.length - 1], this._currentState())) return;
1470
+ this._history = dropRepeats(gesture.before, this._currentState());
1471
+ this._gesture = null;
1472
+ }
1473
+ /**
1474
+ * `undoable: false`: the value in the current mix and in every saved state
1475
+ * that has an entry for the track, so that no undo, redo or abort brings
1476
+ * the old one back. A step that no longer differs from the state next to
1477
+ * it goes; when that was the step a gesture joins and no equal step is
1478
+ * left on top, the gesture's next change pushes a new one.
1479
+ */
1480
+ _writeMixerThrough(trackId, values, next) {
1481
+ this._mixer = next;
1482
+ const write = stateWithMixerValues(trackId, values);
1483
+ const { undo } = this._history;
1484
+ const top = undo.length > 0 ? write(undo[undo.length - 1]) : void 0;
1485
+ const written = writeThrough(this._history, this._transaction, this._currentState(), write);
1486
+ this._history = written.history;
1487
+ this._transaction = written.base;
1488
+ if (this._gesture) {
1489
+ const newTop = this._history.undo[this._history.undo.length - 1];
1490
+ this._gesture = newTop !== void 0 && top !== void 0 && sameState(newTop, top) ? { ...this._gesture, before: mapHistoryStates(this._gesture.before, write) } : null;
1491
+ }
1492
+ }
1493
+ /** The tracks and the mix as they are now: what an undo step or a transaction's base saves. */
1494
+ _currentState() {
1495
+ return { tracks: this._tracks, mixer: this._mixer };
1496
+ }
1497
+ /** Every tracks array the arrangement can still return to, and the current one (for the sources). */
1281
1498
  _heldStates() {
1282
1499
  return [
1283
1500
  this._tracks,
1284
- ...this._history.undo,
1285
- ...this._history.redo,
1286
- ...this._transaction ? [this._transaction.tracks] : []
1501
+ ...this._history.undo.map((state) => state.tracks),
1502
+ ...this._history.redo.map((state) => state.tracks),
1503
+ ...this._gesture ? this._gesture.before.undo.map((state) => state.tracks) : [],
1504
+ ...this._gesture ? this._gesture.before.redo.map((state) => state.tracks) : [],
1505
+ ...this._transaction ? [this._transaction.state.tracks] : []
1287
1506
  ];
1288
1507
  }
1289
1508
  /** `removeTrack` with `undoable: false`: out of the current tracks and out of every stored state. */
1290
1509
  _removeTrackEverywhere(trackId) {
1291
1510
  const wasThere = this._tracks.some((t) => t.id === trackId);
1292
- const purged = purgeTrack(this._history, this._tracks, trackId);
1511
+ const base = this._transaction?.state ?? null;
1512
+ const purged = purgeTrack(this._history, this._currentState(), trackId, base);
1293
1513
  const historyChanged = purged.history !== this._history;
1294
1514
  this._history = purged.history;
1295
- if (this._transaction) {
1296
- this._transaction = {
1297
- ...this._transaction,
1298
- tracks: withoutTrack(this._transaction.tracks, trackId)
1299
- };
1515
+ this._gesture = null;
1516
+ if (this._transaction && purged.base) {
1517
+ this._transaction = { ...this._transaction, state: purged.base };
1300
1518
  }
1301
- this._tracks = purged.current;
1302
- this._mixer = retainMixer(this._mixer, this._heldStates());
1519
+ this._tracks = purged.current.tracks;
1520
+ this._mixer = purged.current.mixer;
1303
1521
  if (wasThere) {
1304
1522
  this._tracksVersion++;
1305
1523
  this._emit({ type: "remove", trackId });
@@ -1312,32 +1530,55 @@ var ArrangementImpl = class {
1312
1530
  /** The state before a change: an undo step, or the mark that an open transaction changed. */
1313
1531
  _pushUndoSnapshot() {
1314
1532
  if (this._transaction) {
1315
- if (!this._transaction.changed) {
1316
- this._transaction = { ...this._transaction, changed: true };
1317
- }
1533
+ this._markTransactionChanged();
1318
1534
  return;
1319
1535
  }
1320
- this._pushStep(this._tracks);
1536
+ this._pushStep(this._currentState());
1537
+ }
1538
+ /** An open transaction has changed something. */
1539
+ _markTransactionChanged() {
1540
+ if (this._transaction && !this._transaction.changed) {
1541
+ this._transaction = { ...this._transaction, changed: true };
1542
+ }
1321
1543
  }
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());
1544
+ /** Add an undo step. No gesture joins it (a gesture change that pushes sets its id after). */
1545
+ _pushStep(state) {
1546
+ this._history = pushStep(this._history, state, this.undoLimit);
1547
+ this._gesture = null;
1327
1548
  }
1328
- _restoreTracks(snapshot) {
1549
+ /**
1550
+ * Make a saved state the current one (an undo, a redo, an abort) and
1551
+ * announce it: `'restore'` when the tracks array is another one (with the
1552
+ * mix from before, which may differ too), `'restoreMix'` when only mixer
1553
+ * values differ. `mixerVersion` counts it when a track that is in both
1554
+ * states has other values; an entry that comes or goes with its track is
1555
+ * not a mixer change. Returns false when nothing differed and nothing was
1556
+ * announced.
1557
+ */
1558
+ _restoreState(state) {
1329
1559
  const previous = this._tracks;
1330
- this._tracks = snapshot;
1331
- this._tracksVersion++;
1332
- this._emit({ type: "restore", previous });
1560
+ const previousMixer = this._mixer;
1561
+ const tracksChanged = state.tracks !== previous;
1562
+ const mixChanged = mixerChanges(previousMixer, state.mixer).length > 0;
1563
+ this._tracks = state.tracks;
1564
+ this._mixer = state.mixer;
1565
+ if (mixChanged) this._mixerVersion++;
1566
+ if (tracksChanged) {
1567
+ this._tracksVersion++;
1568
+ this._emit({ type: "restore", previous, previousMixer });
1569
+ } else if (mixChanged) {
1570
+ this._emit({ type: "restoreMix", previousMixer });
1571
+ }
1572
+ return tracksChanged || mixChanged;
1333
1573
  }
1334
- /** Ends an open transaction, restoring the tracks it began from if it changed them. */
1574
+ /** Ends an open transaction, restoring the state it began from if it changed anything. */
1335
1575
  _abortOpenTransaction() {
1336
1576
  const transaction = this._transaction;
1337
1577
  if (!transaction) return;
1338
1578
  this._transaction = null;
1579
+ this._gesture = null;
1339
1580
  if (transaction.changed) {
1340
- this._restoreTracks(transaction.tracks);
1581
+ this._restoreState(transaction.state);
1341
1582
  }
1342
1583
  }
1343
1584
  /** Declare the sources every state names, and switch the clips whose audio is there. */
@@ -1361,12 +1602,19 @@ var ArrangementImpl = class {
1361
1602
  const pass = this._sources.beginPass((startSample) => this._tickAt(startSample));
1362
1603
  const previous = this._tracks;
1363
1604
  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
- };
1605
+ this._history = mapHistoryTracks(this._history, pass.stored);
1606
+ if (this._gesture) {
1607
+ this._gesture = {
1608
+ ...this._gesture,
1609
+ before: mapHistoryTracks(this._gesture.before, pass.stored)
1610
+ };
1611
+ }
1368
1612
  if (this._transaction) {
1369
- this._transaction = { ...this._transaction, tracks: pass.stored(this._transaction.tracks) };
1613
+ const { state } = this._transaction;
1614
+ this._transaction = {
1615
+ ...this._transaction,
1616
+ state: withStateTracks(state, pass.stored(state.tracks))
1617
+ };
1370
1618
  }
1371
1619
  return this._tracks === previous ? null : previous;
1372
1620
  }
@@ -1392,11 +1640,9 @@ var ArrangementImpl = class {
1392
1640
  * always received that error).
1393
1641
  */
1394
1642
  _emit(change) {
1395
- const switched = change.type === "mixer" ? null : this._syncSources();
1643
+ const mixOnly = change.type === "mixer" || change.type === "restoreMix" || change.type === "recording";
1644
+ const switched = mixOnly ? null : this._syncSources();
1396
1645
  freezeTracksInDevelopment(this._tracks);
1397
- if (change.type === "remove" || change.type === "restore") {
1398
- this._mixer = retainMixer(this._mixer, this._heldStates());
1399
- }
1400
1646
  const at = {
1401
1647
  tracksVersion: this._tracksVersion,
1402
1648
  mixerVersion: this._mixerVersion
@@ -1470,8 +1716,8 @@ var AdapterSync = class {
1470
1716
  }
1471
1717
  /**
1472
1718
  * 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
1719
+ * current mixer values in its fields (muted while a take records onto it): what every
1720
+ * adapter track call and `getState().tracks` use. The same
1475
1721
  * array until the tracks or the mix change, and the same track object
1476
1722
  * while neither that track nor its mix changed.
1477
1723
  */
@@ -1512,13 +1758,30 @@ var AdapterSync = class {
1512
1758
  }
1513
1759
  return true;
1514
1760
  }
1515
- /** A mixer value changed: the adapter's setter for it. */
1761
+ /**
1762
+ * A mixer value changed: the adapter's setter for it. The mute sent is the
1763
+ * one the track plays with: muted while a take records onto the track,
1764
+ * whatever the value given.
1765
+ */
1516
1766
  sendMixerValues(trackId, values) {
1517
1767
  if (values.volume !== void 0) this._adapter?.setTrackVolume(trackId, values.volume);
1518
- if (values.muted !== void 0) this._adapter?.setTrackMute(trackId, values.muted);
1768
+ if (values.muted !== void 0) {
1769
+ this._adapter?.setTrackMute(trackId, values.muted || this._recordingNow(trackId));
1770
+ }
1519
1771
  if (values.soloed !== void 0) this._adapter?.setTrackSolo(trackId, values.soloed);
1520
1772
  if (values.pan !== void 0) this._adapter?.setTrackPan(trackId, values.pan);
1521
1773
  }
1774
+ /**
1775
+ * After an undo, a redo or an abort brought back another mix: the setter
1776
+ * for each field that differs, of each current track that had an entry in
1777
+ * `previousMixer` too. A track that entered with the restore was sent
1778
+ * whole, with its mix in its fields.
1779
+ */
1780
+ sendMixerDiff(previousMixer) {
1781
+ for (const { trackId, values } of mixerChanges(previousMixer, this._arrangement.getMixer())) {
1782
+ this.sendMixerValues(trackId, values);
1783
+ }
1784
+ }
1522
1785
  /**
1523
1786
  * The adapter call for a change to the tracks.
1524
1787
  *
@@ -1649,13 +1912,26 @@ var AdapterSync = class {
1649
1912
  this._loadedMemo.set(track, loaded);
1650
1913
  return loaded;
1651
1914
  }
1652
- /** One track with its current mixer values in its fields (memoized per track object). */
1915
+ /**
1916
+ * True when a take is recording onto the track and it is a current track
1917
+ * (it has an entry in the current mix): it plays muted. A recording id that
1918
+ * is not in the tracks mutes nothing until it enters.
1919
+ */
1920
+ _recordingNow(trackId) {
1921
+ return this._arrangement.recordingTrackIds.has(trackId) && this._arrangement.getMixer().has(trackId);
1922
+ }
1923
+ /**
1924
+ * One track with its current mixer values in its fields, muted while a
1925
+ * take records onto it (memoized per track object).
1926
+ */
1653
1927
  _withMix(track) {
1654
1928
  const values = this._arrangement.getMixer().get(track.id);
1929
+ const recording = this._recordingNow(track.id);
1655
1930
  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 });
1931
+ if (memo && memo.values === values && memo.recording === recording) return memo.merged;
1932
+ const playing = recording && values && !values.muted ? { ...values, muted: true } : values;
1933
+ const merged = freezeTrackInDevelopment(withMixerValues(track, playing));
1934
+ this._withMixMemo.set(track, { values, recording, merged });
1659
1935
  return merged;
1660
1936
  }
1661
1937
  };
@@ -1805,8 +2081,8 @@ var PlaylistEngine = class {
1805
2081
  setTracks(tracks) {
1806
2082
  this._arrangement.setTracks(tracks);
1807
2083
  }
1808
- addTrack(track) {
1809
- this._arrangement.addTrack(track);
2084
+ addTrack(track, options) {
2085
+ this._arrangement.addTrack(track, options);
1810
2086
  }
1811
2087
  removeTrack(trackId) {
1812
2088
  this._arrangement.removeTrack(trackId);
@@ -2030,9 +2306,13 @@ var PlaylistEngine = class {
2030
2306
  //
2031
2307
  // Each setter writes the value into the arrangement's mixer part (so a later
2032
2308
  // 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
2309
+ // the engine's listener then calls the adapter and emits statechange. Each
2310
+ // call is an undo step, unless `options` says otherwise: the same `gesture`
2311
+ // id joins one step, `undoable: false` is no step (`MixerChangeOptions`).
2312
+ //
2313
+ // When the arrangement changes nothing (a track it does not hold, or the
2314
+ // value the track already has), the adapter is still called from here,
2315
+ // with no statechange (a consumer may target a track that has not been
2036
2316
  // ingested yet). These are NOT structural
2037
2317
  // changes, so they deliberately do NOT bump tracksVersion — that would make
2038
2318
  // tracksVersion-gated consumers re-run expensive structural work (audio-graph
@@ -2040,22 +2320,48 @@ var PlaylistEngine = class {
2040
2320
  // cache a track snapshot (e.g. @dawcore/components' <daw-editor>) watch
2041
2321
  // mixerVersion to refresh that cache cheaply, so an unrelated setTracks
2042
2322
  // rebuild no longer reverts live mixer state (#501).
2043
- setTrackVolume(trackId, volume) {
2044
- const changed = this._mixerChanged(() => this._arrangement.setTrackVolume(trackId, volume));
2323
+ setTrackVolume(trackId, volume, options) {
2324
+ const changed = this._mixerChanged(
2325
+ () => this._arrangement.setTrackVolume(trackId, volume, options)
2326
+ );
2045
2327
  if (!changed && !this._disposed) this._adapter?.setTrackVolume(trackId, volume);
2046
2328
  }
2047
- setTrackMute(trackId, muted) {
2048
- const changed = this._mixerChanged(() => this._arrangement.setTrackMute(trackId, muted));
2049
- if (!changed && !this._disposed) this._adapter?.setTrackMute(trackId, muted);
2329
+ setTrackMute(trackId, muted, options) {
2330
+ const changed = this._mixerChanged(
2331
+ () => this._arrangement.setTrackMute(trackId, muted, options)
2332
+ );
2333
+ if (!changed && !this._disposed) this._adapterSync.sendMixerValues(trackId, { muted });
2050
2334
  }
2051
- setTrackSolo(trackId, soloed) {
2052
- const changed = this._mixerChanged(() => this._arrangement.setTrackSolo(trackId, soloed));
2335
+ setTrackSolo(trackId, soloed, options) {
2336
+ const changed = this._mixerChanged(
2337
+ () => this._arrangement.setTrackSolo(trackId, soloed, options)
2338
+ );
2053
2339
  if (!changed && !this._disposed) this._adapter?.setTrackSolo(trackId, soloed);
2054
2340
  }
2055
- setTrackPan(trackId, pan) {
2056
- const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan));
2341
+ setTrackPan(trackId, pan, options) {
2342
+ const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan, options));
2057
2343
  if (!changed && !this._disposed) this._adapter?.setTrackPan(trackId, pan);
2058
2344
  }
2345
+ /**
2346
+ * Mark a track as the one a take is recording onto, or clear it when the
2347
+ * take ends: the arrangement's `setTrackRecording`. That track plays muted
2348
+ * (here and in the adapter) while its mute in the mix stays as it is; the
2349
+ * take's mute is not an undo step. The arrangement's `'recording'`
2350
+ * announcement brings the adapter in step, so there is no adapter call of
2351
+ * its own here; a disposed engine changes the arrangement only.
2352
+ */
2353
+ setTrackRecording(trackId, recording) {
2354
+ this._arrangement.setTrackRecording(trackId, recording);
2355
+ }
2356
+ /**
2357
+ * The tracks a take is recording onto, read from the arrangement (the
2358
+ * owner; no copy). The same set object until it changes; never write to
2359
+ * it, use `setTrackRecording`. Such a track plays muted, so a UI can
2360
+ * disable its mute control while the id is in the set.
2361
+ */
2362
+ get recordingTrackIds() {
2363
+ return this._arrangement.recordingTrackIds;
2364
+ }
2059
2365
  // ---------------------------------------------------------------------------
2060
2366
  // Zoom
2061
2367
  // ---------------------------------------------------------------------------
@@ -2128,6 +2434,14 @@ var PlaylistEngine = class {
2128
2434
  case "mixer":
2129
2435
  this._adapterSync.sendMixerValues(change.trackId, change.values);
2130
2436
  break;
2437
+ case "restoreMix":
2438
+ this._adapterSync.sendMixerDiff(change.previousMixer);
2439
+ break;
2440
+ case "recording": {
2441
+ const values = this._arrangement.getMixer().get(change.trackId);
2442
+ if (values) this._adapterSync.sendMixerValues(change.trackId, { muted: values.muted });
2443
+ break;
2444
+ }
2131
2445
  case "reorder":
2132
2446
  // not audible: adapter nodes are keyed by track id
2133
2447
  case "history":
@@ -2140,6 +2454,7 @@ var PlaylistEngine = class {
2140
2454
  this._followLoad(change, at);
2141
2455
  } else {
2142
2456
  this._adapterSync.follow(change, at);
2457
+ if (change.type === "restore") this._adapterSync.sendMixerDiff(change.previousMixer);
2143
2458
  }
2144
2459
  }
2145
2460
  this._emitStateChange();