@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.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/v3"
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/v3"
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,39 @@ 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();
1022
+ function insertIndex(index, length) {
1023
+ if (index === void 0) return length;
1024
+ if (typeof index !== "number" || !Number.isFinite(index)) {
1025
+ throw new TypeError(
1026
+ `[waveform-playlist] addTrack: index must be a finite number, got ${String(index)}`
1027
+ );
1028
+ }
1029
+ return Math.max(0, Math.min(Math.trunc(index), length));
1030
+ }
922
1031
  var ArrangementImpl = class {
923
1032
  constructor(options = {}) {
924
1033
  this._tracks = [];
925
1034
  this._tracksVersion = 0;
926
1035
  this._mixerVersion = 0;
927
1036
  this._mixer = EMPTY_MIXER;
1037
+ /**
1038
+ * The ids of the tracks a take is recording onto: outside the history and
1039
+ * every saved state (#843). Replaced on a change, never written to.
1040
+ */
1041
+ this._recordingTrackIds = EMPTY_RECORDING;
928
1042
  this._history = EMPTY_HISTORY;
929
1043
  this._transaction = null;
1044
+ /**
1045
+ * The gesture of the top undo step, while a change of that gesture may
1046
+ * still join it: its id, and the history from before its step was pushed
1047
+ * (put back when the gesture returns to where it began, with the redo
1048
+ * states and any step the undo limit evicted). Set when a gesture change
1049
+ * pushes the step, cleared by anything else that touches the history
1050
+ * (#843). `before` is kept in step with the history: the time base, the
1051
+ * switch to loaded clips and `undoable: false` reach it too.
1052
+ */
1053
+ this._gesture = null;
930
1054
  this._subscribers = /* @__PURE__ */ new Set();
931
1055
  this._listeners = /* @__PURE__ */ new Set();
932
1056
  this._pending = [];
@@ -1029,17 +1153,19 @@ var ArrangementImpl = class {
1029
1153
  }
1030
1154
  const prepare = tickPreparer(timeBase, this._tickPpqn);
1031
1155
  const tracks = prepare(this._tracks);
1032
- const history = {
1033
- undo: this._history.undo.map(prepare),
1034
- redo: this._history.redo.map(prepare)
1156
+ const history = mapHistoryTracks(this._history, prepare);
1157
+ const gesture = this._gesture && {
1158
+ ...this._gesture,
1159
+ before: mapHistoryTracks(this._gesture.before, prepare)
1035
1160
  };
1036
1161
  const transaction = this._transaction && {
1037
1162
  ...this._transaction,
1038
- tracks: prepare(this._transaction.tracks)
1163
+ state: withStateTracks(this._transaction.state, prepare(this._transaction.state.tracks))
1039
1164
  };
1040
1165
  this._timeBase = timeBase;
1041
1166
  this._tickPpqn = timeBase.ppqn;
1042
1167
  this._history = history;
1168
+ this._gesture = gesture;
1043
1169
  this._transaction = transaction;
1044
1170
  if (tracks !== this._tracks) {
1045
1171
  this._tracks = tracks;
@@ -1061,22 +1187,24 @@ var ArrangementImpl = class {
1061
1187
  }
1062
1188
  undo() {
1063
1189
  this._abortOpenTransaction();
1064
- const step = undoStep(this._history, this._tracks);
1190
+ this._gesture = null;
1191
+ const step = undoStep(this._history, this._currentState());
1065
1192
  if (!step) return;
1066
1193
  this._history = step.history;
1067
- this._restoreTracks(step.tracks);
1194
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1068
1195
  }
1069
1196
  redo() {
1070
1197
  this._abortOpenTransaction();
1071
- const step = redoStep(this._history, this._tracks);
1198
+ this._gesture = null;
1199
+ const step = redoStep(this._history, this._currentState());
1072
1200
  if (!step) return;
1073
1201
  this._history = step.history;
1074
- this._restoreTracks(step.tracks);
1202
+ if (!this._restoreState(step.state)) this._emit({ type: "history" });
1075
1203
  }
1076
1204
  clearHistory() {
1077
1205
  const hadHistory = this.canUndo || this.canRedo;
1078
1206
  this._history = EMPTY_HISTORY;
1079
- this._mixer = retainMixer(this._mixer, this._heldStates());
1207
+ this._gesture = null;
1080
1208
  if (hadHistory) this._emit({ type: "history" });
1081
1209
  }
1082
1210
  beginTransaction() {
@@ -1086,7 +1214,8 @@ var ArrangementImpl = class {
1086
1214
  );
1087
1215
  }
1088
1216
  const replaced = this._transaction !== null;
1089
- this._transaction = beginBase(this._tracks);
1217
+ this._transaction = beginBase(this._currentState());
1218
+ this._gesture = null;
1090
1219
  if (replaced) this._resyncSourcesQuietly();
1091
1220
  }
1092
1221
  commitTransaction() {
@@ -1096,8 +1225,9 @@ var ArrangementImpl = class {
1096
1225
  return;
1097
1226
  }
1098
1227
  this._transaction = null;
1099
- if (transaction.changed && !sameTracks(transaction.tracks, this._tracks)) {
1100
- this._pushStep(transaction.tracks);
1228
+ this._gesture = null;
1229
+ if (transaction.changed && !sameState(transaction.state, this._currentState())) {
1230
+ this._pushStep(transaction.state);
1101
1231
  this._emit({ type: "history" });
1102
1232
  }
1103
1233
  }
@@ -1122,19 +1252,20 @@ var ArrangementImpl = class {
1122
1252
  tracks.forEach(assertUniqueClipIds);
1123
1253
  this._transaction = null;
1124
1254
  this._history = EMPTY_HISTORY;
1125
- const kept = retainMixer(this._mixer, [this._tracks]);
1255
+ this._gesture = null;
1126
1256
  this._tracks = tracks.map((track) => this._ingest(track));
1127
- this._mixer = retainMixer(seedMixer(kept, this._tracks), [this._tracks]);
1257
+ this._mixer = mixerOf(this._mixer, this._tracks);
1128
1258
  this._tracksVersion++;
1129
1259
  this._emit({ type: "set" });
1130
1260
  }
1131
- addTrack(track) {
1261
+ addTrack(track, options) {
1262
+ const index = insertIndex(options?.index, this._tracks.length);
1132
1263
  assertClipsOfTrackKind(track);
1133
1264
  assertUniqueClipIds(track);
1134
1265
  if (this._tracks.some((t) => t.id === track.id)) throw duplicateTrackId(track.id);
1135
1266
  this._pushUndoSnapshot();
1136
1267
  const enriched = this._ingest(track);
1137
- this._tracks = [...this._tracks, enriched];
1268
+ this._tracks = [...this._tracks.slice(0, index), enriched, ...this._tracks.slice(index)];
1138
1269
  this._mixer = seedMixer(this._mixer, [enriched]);
1139
1270
  this._tracksVersion++;
1140
1271
  this._emit({ type: "add", trackId: track.id });
@@ -1152,6 +1283,7 @@ var ArrangementImpl = class {
1152
1283
  if (!this._tracks.some((t) => t.id === trackId)) return;
1153
1284
  this._pushUndoSnapshot();
1154
1285
  this._tracks = this._tracks.filter((t) => t.id !== trackId);
1286
+ this._mixer = withoutEntry(this._mixer, trackId);
1155
1287
  this._tracksVersion++;
1156
1288
  this._emit({ type: "remove", trackId });
1157
1289
  }
@@ -1198,7 +1330,6 @@ var ArrangementImpl = class {
1198
1330
  const enriched = this._ingest(track, new Set(current.clips));
1199
1331
  this._tracks = this._tracks.map((t) => t.id === trackId ? enriched : t);
1200
1332
  this._mixer = seedMixer(carryMixerEntry(this._mixer, trackId, enriched.id), [enriched]);
1201
- if (enriched.id !== trackId) this._mixer = retainMixer(this._mixer, this._heldStates());
1202
1333
  this._tracksVersion++;
1203
1334
  this._emit({ type: "update", trackId, previous: current });
1204
1335
  }
@@ -1235,19 +1366,46 @@ var ArrangementImpl = class {
1235
1366
  //
1236
1367
  // Each replaces the Map and one entry, bumps mixerVersion and announces
1237
1368
  // 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 });
1369
+ // tracksVersion stays (#501). Each call is an undo step; inside an open
1370
+ // transaction it is part of it, like any other change. Calls with one
1371
+ // `gesture` id, one after another, are one step; `undoable: false` is no
1372
+ // step and is written into every saved state (#843). A value that does
1373
+ // not change, or an id that is not in the current tracks, does nothing.
1374
+ setTrackVolume(trackId, volume, options) {
1375
+ this._setMixerValues(trackId, { volume }, options);
1242
1376
  }
1243
- setTrackMute(trackId, muted) {
1244
- this._setMixerValues(trackId, { muted });
1377
+ setTrackMute(trackId, muted, options) {
1378
+ this._setMixerValues(trackId, { muted }, options);
1245
1379
  }
1246
- setTrackSolo(trackId, soloed) {
1247
- this._setMixerValues(trackId, { soloed });
1380
+ setTrackSolo(trackId, soloed, options) {
1381
+ this._setMixerValues(trackId, { soloed }, options);
1248
1382
  }
1249
- setTrackPan(trackId, pan) {
1250
- this._setMixerValues(trackId, { pan });
1383
+ setTrackPan(trackId, pan, options) {
1384
+ this._setMixerValues(trackId, { pan }, options);
1385
+ }
1386
+ // ---------------------------------------------------------------------------
1387
+ // Recording mute (the track a take is recording onto)
1388
+ // ---------------------------------------------------------------------------
1389
+ //
1390
+ // The track a take is recording onto plays muted: the engine's projection
1391
+ // and the adapter get `muted: stored || recording`, and the stored mute in
1392
+ // `getMixer()` is not touched. The set is outside the history: the take's
1393
+ // mute is no undo step, and undo, redo, abort, `setTracks` and
1394
+ // `clearHistory` leave it alone (#843).
1395
+ /** The ids of the tracks a take is recording onto. The same Set until it changes; never written to. */
1396
+ get recordingTrackIds() {
1397
+ return this._recordingTrackIds;
1398
+ }
1399
+ /**
1400
+ * Mark a track as the one a take is recording onto, or clear it. An id
1401
+ * that is not in the current tracks is recorded too, and mutes nothing
1402
+ * until a track of that id enters. The same value does nothing.
1403
+ */
1404
+ setTrackRecording(trackId, recording) {
1405
+ if (this._recordingTrackIds.has(trackId) === recording) return;
1406
+ this._recordingTrackIds = recording ? /* @__PURE__ */ new Set([...this._recordingTrackIds, trackId]) : new Set([...this._recordingTrackIds].filter((id) => id !== trackId));
1407
+ if (this._mixer.has(trackId)) this._mixerVersion++;
1408
+ this._emit({ type: "recording", trackId });
1251
1409
  }
1252
1410
  // ---------------------------------------------------------------------------
1253
1411
  // Tempo
@@ -1310,37 +1468,97 @@ var ArrangementImpl = class {
1310
1468
  })
1311
1469
  };
1312
1470
  }
1313
- _setMixerValues(trackId, values) {
1471
+ _setMixerValues(trackId, values, options = {}) {
1314
1472
  if (!this._tracks.some((t) => t.id === trackId)) return;
1315
1473
  const next = setMixerValues(this._mixer, trackId, values);
1316
1474
  if (next === this._mixer) return;
1317
- this._mixer = next;
1475
+ if (options.undoable === false) {
1476
+ this._writeMixerThrough(trackId, values, next);
1477
+ } else {
1478
+ this._recordMixerChange(options.gesture);
1479
+ this._mixer = next;
1480
+ this._dropReturnedGestureStep();
1481
+ }
1318
1482
  this._mixerVersion++;
1319
1483
  this._emit({ type: "mixer", trackId, values });
1320
1484
  }
1321
- /** Every tracks array the arrangement can still return to, and the current one. */
1485
+ /**
1486
+ * The undo step of a mixer change about to be made: part of an open
1487
+ * transaction, the top step when it was pushed by the same gesture and
1488
+ * nothing has touched the history since, or else a new step (which becomes
1489
+ * that gesture's).
1490
+ */
1491
+ _recordMixerChange(gesture) {
1492
+ if (this._transaction) {
1493
+ this._markTransactionChanged();
1494
+ return;
1495
+ }
1496
+ if (gesture !== void 0 && this._gesture?.id === gesture) return;
1497
+ const before = this._history;
1498
+ this._pushStep(this._currentState());
1499
+ if (gesture !== void 0 && this.canUndo) this._gesture = { id: gesture, before };
1500
+ }
1501
+ /**
1502
+ * A gesture that came back to the state its step holds leaves no step, and
1503
+ * the history is the one it found: the redo states its first change
1504
+ * cleared come back (as with a transaction that changes nothing).
1505
+ */
1506
+ _dropReturnedGestureStep() {
1507
+ const gesture = this._gesture;
1508
+ if (!gesture) return;
1509
+ const { undo } = this._history;
1510
+ if (!sameState(undo[undo.length - 1], this._currentState())) return;
1511
+ this._history = dropRepeats(gesture.before, this._currentState());
1512
+ this._gesture = null;
1513
+ }
1514
+ /**
1515
+ * `undoable: false`: the value in the current mix and in every saved state
1516
+ * that has an entry for the track, so that no undo, redo or abort brings
1517
+ * the old one back. A step that no longer differs from the state next to
1518
+ * it goes; when that was the step a gesture joins and no equal step is
1519
+ * left on top, the gesture's next change pushes a new one.
1520
+ */
1521
+ _writeMixerThrough(trackId, values, next) {
1522
+ this._mixer = next;
1523
+ const write = stateWithMixerValues(trackId, values);
1524
+ const { undo } = this._history;
1525
+ const top = undo.length > 0 ? write(undo[undo.length - 1]) : void 0;
1526
+ const written = writeThrough(this._history, this._transaction, this._currentState(), write);
1527
+ this._history = written.history;
1528
+ this._transaction = written.base;
1529
+ if (this._gesture) {
1530
+ const newTop = this._history.undo[this._history.undo.length - 1];
1531
+ this._gesture = newTop !== void 0 && top !== void 0 && sameState(newTop, top) ? { ...this._gesture, before: mapHistoryStates(this._gesture.before, write) } : null;
1532
+ }
1533
+ }
1534
+ /** The tracks and the mix as they are now: what an undo step or a transaction's base saves. */
1535
+ _currentState() {
1536
+ return { tracks: this._tracks, mixer: this._mixer };
1537
+ }
1538
+ /** Every tracks array the arrangement can still return to, and the current one (for the sources). */
1322
1539
  _heldStates() {
1323
1540
  return [
1324
1541
  this._tracks,
1325
- ...this._history.undo,
1326
- ...this._history.redo,
1327
- ...this._transaction ? [this._transaction.tracks] : []
1542
+ ...this._history.undo.map((state) => state.tracks),
1543
+ ...this._history.redo.map((state) => state.tracks),
1544
+ ...this._gesture ? this._gesture.before.undo.map((state) => state.tracks) : [],
1545
+ ...this._gesture ? this._gesture.before.redo.map((state) => state.tracks) : [],
1546
+ ...this._transaction ? [this._transaction.state.tracks] : []
1328
1547
  ];
1329
1548
  }
1330
1549
  /** `removeTrack` with `undoable: false`: out of the current tracks and out of every stored state. */
1331
1550
  _removeTrackEverywhere(trackId) {
1332
1551
  const wasThere = this._tracks.some((t) => t.id === trackId);
1333
- const purged = purgeTrack(this._history, this._tracks, trackId);
1552
+ const base = this._transaction?.state ?? null;
1553
+ const purged = purgeTrack(this._history, this._currentState(), trackId, base);
1334
1554
  const historyChanged = purged.history !== this._history;
1335
1555
  this._history = purged.history;
1336
- if (this._transaction) {
1337
- this._transaction = {
1338
- ...this._transaction,
1339
- tracks: withoutTrack(this._transaction.tracks, trackId)
1340
- };
1556
+ this._gesture = null;
1557
+ if (this._transaction && purged.base) {
1558
+ this._transaction = { ...this._transaction, state: purged.base };
1341
1559
  }
1342
- this._tracks = purged.current;
1343
- this._mixer = retainMixer(this._mixer, this._heldStates());
1560
+ this._tracks = purged.current.tracks;
1561
+ this._mixer = purged.current.mixer;
1344
1562
  if (wasThere) {
1345
1563
  this._tracksVersion++;
1346
1564
  this._emit({ type: "remove", trackId });
@@ -1353,32 +1571,55 @@ var ArrangementImpl = class {
1353
1571
  /** The state before a change: an undo step, or the mark that an open transaction changed. */
1354
1572
  _pushUndoSnapshot() {
1355
1573
  if (this._transaction) {
1356
- if (!this._transaction.changed) {
1357
- this._transaction = { ...this._transaction, changed: true };
1358
- }
1574
+ this._markTransactionChanged();
1359
1575
  return;
1360
1576
  }
1361
- this._pushStep(this._tracks);
1577
+ this._pushStep(this._currentState());
1578
+ }
1579
+ /** An open transaction has changed something. */
1580
+ _markTransactionChanged() {
1581
+ if (this._transaction && !this._transaction.changed) {
1582
+ this._transaction = { ...this._transaction, changed: true };
1583
+ }
1362
1584
  }
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());
1585
+ /** Add an undo step. No gesture joins it (a gesture change that pushes sets its id after). */
1586
+ _pushStep(state) {
1587
+ this._history = pushStep(this._history, state, this.undoLimit);
1588
+ this._gesture = null;
1368
1589
  }
1369
- _restoreTracks(snapshot) {
1590
+ /**
1591
+ * Make a saved state the current one (an undo, a redo, an abort) and
1592
+ * announce it: `'restore'` when the tracks array is another one (with the
1593
+ * mix from before, which may differ too), `'restoreMix'` when only mixer
1594
+ * values differ. `mixerVersion` counts it when a track that is in both
1595
+ * states has other values; an entry that comes or goes with its track is
1596
+ * not a mixer change. Returns false when nothing differed and nothing was
1597
+ * announced.
1598
+ */
1599
+ _restoreState(state) {
1370
1600
  const previous = this._tracks;
1371
- this._tracks = snapshot;
1372
- this._tracksVersion++;
1373
- this._emit({ type: "restore", previous });
1601
+ const previousMixer = this._mixer;
1602
+ const tracksChanged = state.tracks !== previous;
1603
+ const mixChanged = mixerChanges(previousMixer, state.mixer).length > 0;
1604
+ this._tracks = state.tracks;
1605
+ this._mixer = state.mixer;
1606
+ if (mixChanged) this._mixerVersion++;
1607
+ if (tracksChanged) {
1608
+ this._tracksVersion++;
1609
+ this._emit({ type: "restore", previous, previousMixer });
1610
+ } else if (mixChanged) {
1611
+ this._emit({ type: "restoreMix", previousMixer });
1612
+ }
1613
+ return tracksChanged || mixChanged;
1374
1614
  }
1375
- /** Ends an open transaction, restoring the tracks it began from if it changed them. */
1615
+ /** Ends an open transaction, restoring the state it began from if it changed anything. */
1376
1616
  _abortOpenTransaction() {
1377
1617
  const transaction = this._transaction;
1378
1618
  if (!transaction) return;
1379
1619
  this._transaction = null;
1620
+ this._gesture = null;
1380
1621
  if (transaction.changed) {
1381
- this._restoreTracks(transaction.tracks);
1622
+ this._restoreState(transaction.state);
1382
1623
  }
1383
1624
  }
1384
1625
  /** Declare the sources every state names, and switch the clips whose audio is there. */
@@ -1402,12 +1643,19 @@ var ArrangementImpl = class {
1402
1643
  const pass = this._sources.beginPass((startSample) => this._tickAt(startSample));
1403
1644
  const previous = this._tracks;
1404
1645
  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
- };
1646
+ this._history = mapHistoryTracks(this._history, pass.stored);
1647
+ if (this._gesture) {
1648
+ this._gesture = {
1649
+ ...this._gesture,
1650
+ before: mapHistoryTracks(this._gesture.before, pass.stored)
1651
+ };
1652
+ }
1409
1653
  if (this._transaction) {
1410
- this._transaction = { ...this._transaction, tracks: pass.stored(this._transaction.tracks) };
1654
+ const { state } = this._transaction;
1655
+ this._transaction = {
1656
+ ...this._transaction,
1657
+ state: withStateTracks(state, pass.stored(state.tracks))
1658
+ };
1411
1659
  }
1412
1660
  return this._tracks === previous ? null : previous;
1413
1661
  }
@@ -1433,11 +1681,9 @@ var ArrangementImpl = class {
1433
1681
  * always received that error).
1434
1682
  */
1435
1683
  _emit(change) {
1436
- const switched = change.type === "mixer" ? null : this._syncSources();
1684
+ const mixOnly = change.type === "mixer" || change.type === "restoreMix" || change.type === "recording";
1685
+ const switched = mixOnly ? null : this._syncSources();
1437
1686
  freezeTracksInDevelopment(this._tracks);
1438
- if (change.type === "remove" || change.type === "restore") {
1439
- this._mixer = retainMixer(this._mixer, this._heldStates());
1440
- }
1441
1687
  const at = {
1442
1688
  tracksVersion: this._tracksVersion,
1443
1689
  mixerVersion: this._mixerVersion
@@ -1511,8 +1757,8 @@ var AdapterSync = class {
1511
1757
  }
1512
1758
  /**
1513
1759
  * 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
1760
+ * current mixer values in its fields (muted while a take records onto it): what every
1761
+ * adapter track call and `getState().tracks` use. The same
1516
1762
  * array until the tracks or the mix change, and the same track object
1517
1763
  * while neither that track nor its mix changed.
1518
1764
  */
@@ -1553,13 +1799,30 @@ var AdapterSync = class {
1553
1799
  }
1554
1800
  return true;
1555
1801
  }
1556
- /** A mixer value changed: the adapter's setter for it. */
1802
+ /**
1803
+ * A mixer value changed: the adapter's setter for it. The mute sent is the
1804
+ * one the track plays with: muted while a take records onto the track,
1805
+ * whatever the value given.
1806
+ */
1557
1807
  sendMixerValues(trackId, values) {
1558
1808
  if (values.volume !== void 0) this._adapter?.setTrackVolume(trackId, values.volume);
1559
- if (values.muted !== void 0) this._adapter?.setTrackMute(trackId, values.muted);
1809
+ if (values.muted !== void 0) {
1810
+ this._adapter?.setTrackMute(trackId, values.muted || this._recordingNow(trackId));
1811
+ }
1560
1812
  if (values.soloed !== void 0) this._adapter?.setTrackSolo(trackId, values.soloed);
1561
1813
  if (values.pan !== void 0) this._adapter?.setTrackPan(trackId, values.pan);
1562
1814
  }
1815
+ /**
1816
+ * After an undo, a redo or an abort brought back another mix: the setter
1817
+ * for each field that differs, of each current track that had an entry in
1818
+ * `previousMixer` too. A track that entered with the restore was sent
1819
+ * whole, with its mix in its fields.
1820
+ */
1821
+ sendMixerDiff(previousMixer) {
1822
+ for (const { trackId, values } of mixerChanges(previousMixer, this._arrangement.getMixer())) {
1823
+ this.sendMixerValues(trackId, values);
1824
+ }
1825
+ }
1563
1826
  /**
1564
1827
  * The adapter call for a change to the tracks.
1565
1828
  *
@@ -1690,13 +1953,26 @@ var AdapterSync = class {
1690
1953
  this._loadedMemo.set(track, loaded);
1691
1954
  return loaded;
1692
1955
  }
1693
- /** One track with its current mixer values in its fields (memoized per track object). */
1956
+ /**
1957
+ * True when a take is recording onto the track and it is a current track
1958
+ * (it has an entry in the current mix): it plays muted. A recording id that
1959
+ * is not in the tracks mutes nothing until it enters.
1960
+ */
1961
+ _recordingNow(trackId) {
1962
+ return this._arrangement.recordingTrackIds.has(trackId) && this._arrangement.getMixer().has(trackId);
1963
+ }
1964
+ /**
1965
+ * One track with its current mixer values in its fields, muted while a
1966
+ * take records onto it (memoized per track object).
1967
+ */
1694
1968
  _withMix(track) {
1695
1969
  const values = this._arrangement.getMixer().get(track.id);
1970
+ const recording = this._recordingNow(track.id);
1696
1971
  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 });
1972
+ if (memo && memo.values === values && memo.recording === recording) return memo.merged;
1973
+ const playing = recording && values && !values.muted ? { ...values, muted: true } : values;
1974
+ const merged = freezeTrackInDevelopment(withMixerValues(track, playing));
1975
+ this._withMixMemo.set(track, { values, recording, merged });
1700
1976
  return merged;
1701
1977
  }
1702
1978
  };
@@ -1846,8 +2122,8 @@ var PlaylistEngine = class {
1846
2122
  setTracks(tracks) {
1847
2123
  this._arrangement.setTracks(tracks);
1848
2124
  }
1849
- addTrack(track) {
1850
- this._arrangement.addTrack(track);
2125
+ addTrack(track, options) {
2126
+ this._arrangement.addTrack(track, options);
1851
2127
  }
1852
2128
  removeTrack(trackId) {
1853
2129
  this._arrangement.removeTrack(trackId);
@@ -2071,9 +2347,13 @@ var PlaylistEngine = class {
2071
2347
  //
2072
2348
  // Each setter writes the value into the arrangement's mixer part (so a later
2073
2349
  // 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
2350
+ // the engine's listener then calls the adapter and emits statechange. Each
2351
+ // call is an undo step, unless `options` says otherwise: the same `gesture`
2352
+ // id joins one step, `undoable: false` is no step (`MixerChangeOptions`).
2353
+ //
2354
+ // When the arrangement changes nothing (a track it does not hold, or the
2355
+ // value the track already has), the adapter is still called from here,
2356
+ // with no statechange (a consumer may target a track that has not been
2077
2357
  // ingested yet). These are NOT structural
2078
2358
  // changes, so they deliberately do NOT bump tracksVersion — that would make
2079
2359
  // tracksVersion-gated consumers re-run expensive structural work (audio-graph
@@ -2081,22 +2361,48 @@ var PlaylistEngine = class {
2081
2361
  // cache a track snapshot (e.g. @dawcore/components' <daw-editor>) watch
2082
2362
  // mixerVersion to refresh that cache cheaply, so an unrelated setTracks
2083
2363
  // rebuild no longer reverts live mixer state (#501).
2084
- setTrackVolume(trackId, volume) {
2085
- const changed = this._mixerChanged(() => this._arrangement.setTrackVolume(trackId, volume));
2364
+ setTrackVolume(trackId, volume, options) {
2365
+ const changed = this._mixerChanged(
2366
+ () => this._arrangement.setTrackVolume(trackId, volume, options)
2367
+ );
2086
2368
  if (!changed && !this._disposed) this._adapter?.setTrackVolume(trackId, volume);
2087
2369
  }
2088
- setTrackMute(trackId, muted) {
2089
- const changed = this._mixerChanged(() => this._arrangement.setTrackMute(trackId, muted));
2090
- if (!changed && !this._disposed) this._adapter?.setTrackMute(trackId, muted);
2370
+ setTrackMute(trackId, muted, options) {
2371
+ const changed = this._mixerChanged(
2372
+ () => this._arrangement.setTrackMute(trackId, muted, options)
2373
+ );
2374
+ if (!changed && !this._disposed) this._adapterSync.sendMixerValues(trackId, { muted });
2091
2375
  }
2092
- setTrackSolo(trackId, soloed) {
2093
- const changed = this._mixerChanged(() => this._arrangement.setTrackSolo(trackId, soloed));
2376
+ setTrackSolo(trackId, soloed, options) {
2377
+ const changed = this._mixerChanged(
2378
+ () => this._arrangement.setTrackSolo(trackId, soloed, options)
2379
+ );
2094
2380
  if (!changed && !this._disposed) this._adapter?.setTrackSolo(trackId, soloed);
2095
2381
  }
2096
- setTrackPan(trackId, pan) {
2097
- const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan));
2382
+ setTrackPan(trackId, pan, options) {
2383
+ const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan, options));
2098
2384
  if (!changed && !this._disposed) this._adapter?.setTrackPan(trackId, pan);
2099
2385
  }
2386
+ /**
2387
+ * Mark a track as the one a take is recording onto, or clear it when the
2388
+ * take ends: the arrangement's `setTrackRecording`. That track plays muted
2389
+ * (here and in the adapter) while its mute in the mix stays as it is; the
2390
+ * take's mute is not an undo step. The arrangement's `'recording'`
2391
+ * announcement brings the adapter in step, so there is no adapter call of
2392
+ * its own here; a disposed engine changes the arrangement only.
2393
+ */
2394
+ setTrackRecording(trackId, recording) {
2395
+ this._arrangement.setTrackRecording(trackId, recording);
2396
+ }
2397
+ /**
2398
+ * The tracks a take is recording onto, read from the arrangement (the
2399
+ * owner; no copy). The same set object until it changes; never write to
2400
+ * it, use `setTrackRecording`. Such a track plays muted, so a UI can
2401
+ * disable its mute control while the id is in the set.
2402
+ */
2403
+ get recordingTrackIds() {
2404
+ return this._arrangement.recordingTrackIds;
2405
+ }
2100
2406
  // ---------------------------------------------------------------------------
2101
2407
  // Zoom
2102
2408
  // ---------------------------------------------------------------------------
@@ -2169,6 +2475,14 @@ var PlaylistEngine = class {
2169
2475
  case "mixer":
2170
2476
  this._adapterSync.sendMixerValues(change.trackId, change.values);
2171
2477
  break;
2478
+ case "restoreMix":
2479
+ this._adapterSync.sendMixerDiff(change.previousMixer);
2480
+ break;
2481
+ case "recording": {
2482
+ const values = this._arrangement.getMixer().get(change.trackId);
2483
+ if (values) this._adapterSync.sendMixerValues(change.trackId, { muted: values.muted });
2484
+ break;
2485
+ }
2172
2486
  case "reorder":
2173
2487
  // not audible: adapter nodes are keyed by track id
2174
2488
  case "history":
@@ -2181,6 +2495,7 @@ var PlaylistEngine = class {
2181
2495
  this._followLoad(change, at);
2182
2496
  } else {
2183
2497
  this._adapterSync.follow(change, at);
2498
+ if (change.type === "restore") this._adapterSync.sendMixerDiff(change.previousMixer);
2184
2499
  }
2185
2500
  }
2186
2501
  this._emitStateChange();