@waveform-playlist/engine 14.0.0-next.2 → 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/README.md CHANGED
@@ -111,7 +111,7 @@ A `setTempo` places every clip that has a tick on the tick grid of the engine it
111
111
 
112
112
  A different tempo is not converted. A tick is the musical position, so an engine built at another tempo than the one the ticks were made under has clips whose `startSample` and `startTick` disagree until the next `setTempo`, which moves `startSample` to follow the tick. Clips added under the new engine get their ticks at its tempo and keep their place at that `setTempo`, while the older ones move.
113
113
 
114
- An arrangement plays through one engine at a time: building a second engine on it while the first has not been disposed logs a warning. Two engines alive on one arrangement is unsupported, in particular with different `ppqn`: the second engine's time base replaces the first's, so ticks the first engine gives later are computed with the second engine's. The engine's own `undoLimit` option is ignored, with a warning, when an arrangement is passed; set it on `createArrangement({ undoLimit })`. The `arrangement` option accepts only an arrangement made by `createArrangement()`. The engine recognises one by a versioned `Symbol.for` key and not by `instanceof`, so an arrangement made by another installed copy of this package (an app and a library that pin different versions) is accepted as long as both copies use the same key version. The key version changed with the mixer undo steps (`/v2`): an engine and an arrangement from copies on either side of that change refuse each other.
114
+ An arrangement plays through one engine at a time: building a second engine on it while the first has not been disposed logs a warning. Two engines alive on one arrangement is unsupported, in particular with different `ppqn`: the second engine's time base replaces the first's, so ticks the first engine gives later are computed with the second engine's. The engine's own `undoLimit` option is ignored, with a warning, when an arrangement is passed; set it on `createArrangement({ undoLimit })`. The `arrangement` option accepts only an arrangement made by `createArrangement()`. The engine recognises one by a versioned `Symbol.for` key and not by `instanceof`, so an arrangement made by another installed copy of this package (an app and a library that pin different versions) is accepted as long as both copies use the same key version. The key version changed with the mixer undo steps (`/v2`) and again with `addTrack`'s `index` (`/v3`): an engine and an arrangement from copies on either side of such a change refuse each other.
115
115
 
116
116
  The engine's track, clip, mixer, recording and history methods call the arrangement's, so a change made through either reaches the adapter and emits `statechange`. The arrangement's `removeTrack` also takes `{ undoable: false }` (no undo step, and the track is taken out of every undo and redo state, so no undo brings it back: for a dropped file that could not be decoded), and its `updateTrack` takes a function of the current track: `arrangement.updateTrack(id, (track) => ({ ...track, name: 'Bass' }))`.
117
117
 
@@ -255,7 +255,7 @@ class PlaylistEngine {
255
255
 
256
256
  // Tracks
257
257
  setTracks(tracks: readonly ClipTrack[]): void; // replaces every track, clears the undo history
258
- addTrack(track: ClipTrack): void;
258
+ addTrack(track: ClipTrack, options?: AddTrackOptions): void; // { index }: where it enters (default: the end)
259
259
  removeTrack(trackId: string): void;
260
260
  updateTrack(trackId: string, track?: ClipTrack): void; // with a track: replace it; without: send the current one to the adapter again
261
261
  reorderTrack(trackId: string, toIndex: number): void;
@@ -333,7 +333,7 @@ interface Arrangement {
333
333
 
334
334
  // tracks
335
335
  setTracks(tracks: readonly ArrangementTrack[]): void;
336
- addTrack(track: ArrangementTrack): void;
336
+ addTrack(track: ArrangementTrack, options?: AddTrackOptions): void; // { index }: where it enters (default: the end)
337
337
  removeTrack(trackId: string, options?: RemoveTrackOptions): void;
338
338
  updateTrack(
339
339
  trackId: string,
package/dist/index.d.mts CHANGED
@@ -245,6 +245,14 @@ interface RemoveTrackOptions {
245
245
  */
246
246
  undoable?: boolean;
247
247
  }
248
+ /** Options of `addTrack`. */
249
+ interface AddTrackOptions {
250
+ /**
251
+ * Where the track enters: its index in the new order (cut to a whole
252
+ * number, then held between 0 and the number of tracks). Left out: at the end.
253
+ */
254
+ index?: number;
255
+ }
248
256
  /** How a mixer setter's change goes into the undo history. */
249
257
  interface MixerChangeOptions {
250
258
  /** Changes with the same id, one after another, are one undo step. */
@@ -295,7 +303,8 @@ interface Arrangement {
295
303
  * transaction. An id that is in the tracks being replaced keeps its mix.
296
304
  */
297
305
  setTracks(tracks: readonly ArrangementTrack[]): void;
298
- addTrack(track: ArrangementTrack): void;
306
+ /** Add a track: an undo step. Throws for an `index` that is not a finite number. */
307
+ addTrack(track: ArrangementTrack, options?: AddTrackOptions): void;
299
308
  removeTrack(trackId: string, options?: RemoveTrackOptions): void;
300
309
  /**
301
310
  * Replace a track, with a track or with a function of the current one.
@@ -399,6 +408,8 @@ interface CreateArrangementOptions {
399
408
  * v2 (#843): the mixer setters take options and are undo steps, the history
400
409
  * holds the mix (`'restoreMix'`), and the arrangement has the take's mute
401
410
  * (`setTrackRecording`, `recordingTrackIds`, `'recording'`).
411
+ *
412
+ * v3 (#848): `addTrack(track, { index })` puts the track at a place in the order.
402
413
  */
403
414
 
404
415
  type GetDecodeContext = () => Promise<BaseAudioContext>;
@@ -635,7 +646,7 @@ declare class PlaylistEngine {
635
646
  abortTransaction(): void;
636
647
  getState(): EngineState;
637
648
  setTracks(tracks: readonly ClipTrack[]): void;
638
- addTrack(track: ClipTrack): void;
649
+ addTrack(track: ClipTrack, options?: AddTrackOptions): void;
639
650
  removeTrack(trackId: string): void;
640
651
  /**
641
652
  * Move a track to a new position in the track order. Purely organizational —
@@ -757,4 +768,4 @@ declare class PlaylistEngine {
757
768
  private _emitStateChange;
758
769
  }
759
770
 
760
- export { type Arrangement, type ArrangementSourceState, type ArrangementSourcesValue, type ClipBounds, type CreateArrangementOptions, type EngineEvents, type EngineState, type LoadSource, type MixerChangeOptions, PlaylistEngine, type PlaylistEngineOptions, type PlayoutAdapter, type RemoveTrackOptions, type TrackMixerValues, attachDecodeContext, calculateDuration, calculateSplitPoint, calculateViewportBounds, calculateZoomScrollPosition, canSplitAt, clampSeekPosition, constrainBoundaryTrim, constrainClipDrag, createArrangement, defaultLoadSource, findClosestZoomIndex, getVisibleChunkIndices, shouldUpdateViewport, splitClip };
771
+ export { type AddTrackOptions, type Arrangement, type ArrangementSourceState, type ArrangementSourcesValue, type ClipBounds, type CreateArrangementOptions, type EngineEvents, type EngineState, type LoadSource, type MixerChangeOptions, PlaylistEngine, type PlaylistEngineOptions, type PlayoutAdapter, type RemoveTrackOptions, type TrackMixerValues, attachDecodeContext, calculateDuration, calculateSplitPoint, calculateViewportBounds, calculateZoomScrollPosition, canSplitAt, clampSeekPosition, constrainBoundaryTrim, constrainClipDrag, createArrangement, defaultLoadSource, findClosestZoomIndex, getVisibleChunkIndices, shouldUpdateViewport, splitClip };
package/dist/index.d.ts CHANGED
@@ -245,6 +245,14 @@ interface RemoveTrackOptions {
245
245
  */
246
246
  undoable?: boolean;
247
247
  }
248
+ /** Options of `addTrack`. */
249
+ interface AddTrackOptions {
250
+ /**
251
+ * Where the track enters: its index in the new order (cut to a whole
252
+ * number, then held between 0 and the number of tracks). Left out: at the end.
253
+ */
254
+ index?: number;
255
+ }
248
256
  /** How a mixer setter's change goes into the undo history. */
249
257
  interface MixerChangeOptions {
250
258
  /** Changes with the same id, one after another, are one undo step. */
@@ -295,7 +303,8 @@ interface Arrangement {
295
303
  * transaction. An id that is in the tracks being replaced keeps its mix.
296
304
  */
297
305
  setTracks(tracks: readonly ArrangementTrack[]): void;
298
- addTrack(track: ArrangementTrack): void;
306
+ /** Add a track: an undo step. Throws for an `index` that is not a finite number. */
307
+ addTrack(track: ArrangementTrack, options?: AddTrackOptions): void;
299
308
  removeTrack(trackId: string, options?: RemoveTrackOptions): void;
300
309
  /**
301
310
  * Replace a track, with a track or with a function of the current one.
@@ -399,6 +408,8 @@ interface CreateArrangementOptions {
399
408
  * v2 (#843): the mixer setters take options and are undo steps, the history
400
409
  * holds the mix (`'restoreMix'`), and the arrangement has the take's mute
401
410
  * (`setTrackRecording`, `recordingTrackIds`, `'recording'`).
411
+ *
412
+ * v3 (#848): `addTrack(track, { index })` puts the track at a place in the order.
402
413
  */
403
414
 
404
415
  type GetDecodeContext = () => Promise<BaseAudioContext>;
@@ -635,7 +646,7 @@ declare class PlaylistEngine {
635
646
  abortTransaction(): void;
636
647
  getState(): EngineState;
637
648
  setTracks(tracks: readonly ClipTrack[]): void;
638
- addTrack(track: ClipTrack): void;
649
+ addTrack(track: ClipTrack, options?: AddTrackOptions): void;
639
650
  removeTrack(trackId: string): void;
640
651
  /**
641
652
  * Move a track to a new position in the track order. Purely organizational —
@@ -757,4 +768,4 @@ declare class PlaylistEngine {
757
768
  private _emitStateChange;
758
769
  }
759
770
 
760
- export { type Arrangement, type ArrangementSourceState, type ArrangementSourcesValue, type ClipBounds, type CreateArrangementOptions, type EngineEvents, type EngineState, type LoadSource, type MixerChangeOptions, PlaylistEngine, type PlaylistEngineOptions, type PlayoutAdapter, type RemoveTrackOptions, type TrackMixerValues, attachDecodeContext, calculateDuration, calculateSplitPoint, calculateViewportBounds, calculateZoomScrollPosition, canSplitAt, clampSeekPosition, constrainBoundaryTrim, constrainClipDrag, createArrangement, defaultLoadSource, findClosestZoomIndex, getVisibleChunkIndices, shouldUpdateViewport, splitClip };
771
+ export { type AddTrackOptions, type Arrangement, type ArrangementSourceState, type ArrangementSourcesValue, type ClipBounds, type CreateArrangementOptions, type EngineEvents, type EngineState, type LoadSource, type MixerChangeOptions, PlaylistEngine, type PlaylistEngineOptions, type PlayoutAdapter, type RemoveTrackOptions, type TrackMixerValues, attachDecodeContext, calculateDuration, calculateSplitPoint, calculateViewportBounds, calculateZoomScrollPosition, canSplitAt, clampSeekPosition, constrainBoundaryTrim, constrainClipDrag, createArrangement, defaultLoadSource, findClosestZoomIndex, getVisibleChunkIndices, shouldUpdateViewport, splitClip };
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/v2"
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/v2"
200
+ "@waveform-playlist/engine/arrangement/time-base/v3"
201
201
  );
202
202
  function attachPoints(value) {
203
203
  return typeof value === "object" && value !== null ? value : {};
@@ -1019,6 +1019,15 @@ function duplicateTrackId(trackId) {
1019
1019
  var DEFAULT_MIN_DURATION_SECONDS = 0.1;
1020
1020
  var DEFAULT_UNDO_LIMIT = 100;
1021
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
+ }
1022
1031
  var ArrangementImpl = class {
1023
1032
  constructor(options = {}) {
1024
1033
  this._tracks = [];
@@ -1249,13 +1258,14 @@ var ArrangementImpl = class {
1249
1258
  this._tracksVersion++;
1250
1259
  this._emit({ type: "set" });
1251
1260
  }
1252
- addTrack(track) {
1261
+ addTrack(track, options) {
1262
+ const index = insertIndex(options?.index, this._tracks.length);
1253
1263
  assertClipsOfTrackKind(track);
1254
1264
  assertUniqueClipIds(track);
1255
1265
  if (this._tracks.some((t) => t.id === track.id)) throw duplicateTrackId(track.id);
1256
1266
  this._pushUndoSnapshot();
1257
1267
  const enriched = this._ingest(track);
1258
- this._tracks = [...this._tracks, enriched];
1268
+ this._tracks = [...this._tracks.slice(0, index), enriched, ...this._tracks.slice(index)];
1259
1269
  this._mixer = seedMixer(this._mixer, [enriched]);
1260
1270
  this._tracksVersion++;
1261
1271
  this._emit({ type: "add", trackId: track.id });
@@ -2112,8 +2122,8 @@ var PlaylistEngine = class {
2112
2122
  setTracks(tracks) {
2113
2123
  this._arrangement.setTracks(tracks);
2114
2124
  }
2115
- addTrack(track) {
2116
- this._arrangement.addTrack(track);
2125
+ addTrack(track, options) {
2126
+ this._arrangement.addTrack(track, options);
2117
2127
  }
2118
2128
  removeTrack(trackId) {
2119
2129
  this._arrangement.removeTrack(trackId);