@waveform-playlist/engine 14.0.0-next.4 → 14.0.0-next.6

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
@@ -117,7 +117,7 @@ The engine's track, clip, mixer, recording and history methods call the arrangem
117
117
 
118
118
  ### Mixer moves are undo steps
119
119
 
120
- `setTrackVolume`, `setTrackMute`, `setTrackSolo` and `setTrackPan` (on the arrangement and on the engine) are undo steps: an undo puts the fader back. Their last argument, `options?: MixerChangeOptions`, says how a change goes into the history:
120
+ `setTrackVolume`, `setTrackMute`, `setTrackSolo`, `setTrackPan` and `setTrackPanLaw` (on the arrangement and on the engine) are undo steps: an undo puts the fader back. Their last argument, `options?: MixerChangeOptions`, says how a change goes into the history:
121
121
 
122
122
  - nothing: each call is a step of its own;
123
123
  - `{ gesture: id }`: calls with the same id, one after another, are one step (a fader drag from press to release);
@@ -188,7 +188,7 @@ What happens to a loading clip:
188
188
  - **It is held like any clip.** It is copied on its way in, is the same object through every operation on its track, keeps its place among the track's clips and is part of the undo steps. It has no `startTick`.
189
189
  - **It cannot be edited.** `moveClip`, `trimClip` and `splitClip` on it log a warning (`… is still loading and cannot be edited until its audio has loaded`) and change nothing; `getClipBounds` returns `null` and `constrainTrimDelta` returns 0. Loaded clips do not collide with it.
190
190
  - **It is audio.** A loading clip on a `kind: 'midi'` track is refused: `createArrangement({ tracks })`, `setTracks`, `addTrack` and `updateTrack` throw the error for a track whose clips do not match its kind, and nothing changes.
191
- - **An engine does not play or report it.** The tracks an adapter is sent and `engine.getState().tracks` are each track without its loading clips, and `getState().duration` counts loaded clips only. A change that leaves a track's loaded clips (the same objects in the same order) and its own fields as they were makes no adapter call for that track; the `statechange` is still emitted. `engine.updateTrack(id)` always sends.
191
+ - **An engine does not play or report it.** The tracks an adapter is sent and `engine.getState().tracks` are each track without its loading clips, and `getState().duration` counts loaded clips only. A change that leaves a track's loaded clips (the same objects in the same order) and its own fields other than the view fields (`TRACK_VIEW_FIELDS`) as they were makes no adapter call for that track; the `statechange` is still emitted. `engine.updateTrack(id)` always sends.
192
192
 
193
193
  Which sources are loaded: every source a loading clip names in the current tracks, in an undo or redo state or in the base of an open transaction. So an undo never fetches a source again, and a source is dropped (its load aborted, its audio let go) when no state names it any more; named again later, it is fetched again. A loaded clip that carries both `src` and `audioBuffer` counts too: its buffer is used for that source and nothing is fetched. A loaded clip with `src` and no `audioBuffer` is not fetched and is not in `sources`: it never gets audio from the arrangement.
194
194
 
@@ -236,9 +236,10 @@ One context at a time: attaching a second while one is attached logs a warning a
236
236
 
237
237
  Rules that hold for the engine and the arrangement alike:
238
238
 
239
- - **Nothing handed out is a copy.** `arrangement.getTracks()`, `engine.getState().tracks` and the tracks an adapter receives are shared with the undo history. Never write to the array, a track or a clip: copy first (`{ ...track, clips: [...] }`) and pass the copy to `updateTrack`. `arrangement.updateTrack`, given a track object or a function, keeps each clip object the track already holds and copies every other clip, so the clips you did not replace are the same objects afterwards. Outside production builds (`process.env.NODE_ENV` is not `'production'`; a page that loads the module with no bundler, and so has no `process`, counts as production) the tracks array, each track, its `clips` array and each clip are frozen, so a write throws in strict code (every module is strict). What a clip refers to (its `AudioBuffer`, peaks, fades, notes) is not frozen. Tracks are copied on their way in, so the objects you pass stay yours.
239
+ - **Nothing handed out is a copy.** `arrangement.getTracks()` and `engine.getState().tracks` are shared with the undo history. The tracks an adapter receives are copies without the view fields (core's `TRACK_VIEW_FIELDS`, typed `PlayoutTrack`) that share these clips, frozen the same way. Never write to the array, a track or a clip: copy first (`{ ...track, clips: [...] }`) and pass the copy to `updateTrack`. `arrangement.updateTrack`, given a track object or a function, keeps each clip object the track already holds and copies every other clip, so the clips you did not replace are the same objects afterwards. Outside production builds (`process.env.NODE_ENV` is not `'production'`; a page that loads the module with no bundler, and so has no `process`, counts as production) the tracks array, each track, its `clips` array and each clip are frozen, so a write throws in strict code (every module is strict). What a clip refers to (its `AudioBuffer`, peaks, fades, notes) is not frozen. Tracks are copied on their way in, so the objects you pass stay yours.
240
240
  - **A change replaces only what changed.** The tracks array and the changed track and clip objects are new; every other track is the same object as before, so a comparison by identity finds what changed. The exception is `setTracks`, which copies every track it is given: after it no track is the object it was.
241
- - **The mix is separate from the tracks.** A track's own `volume`, `muted`, `soloed` and `pan` fields are the values it enters with. They seed its mixer entry when its id has none. `setTracks` keeps the mix of an id that is in the tracks it replaces, and `updateTrack` keeps the track's mix (also when it gives the track a new id); in those cases the fields are not read. `addTrack` seeds the entry from the new track's fields, also for the id of a track removed earlier. `getMixer()` holds the entries of the current tracks; each undo and redo state keeps its own. Change the mix with `setTrackVolume`, `setTrackMute`, `setTrackSolo` and `setTrackPan`, which act on a track that is in the current tracks. `engine.getState().tracks` and the tracks an adapter receives carry the current mix in those fields; `arrangement.getTracks()` does not (read `getMixer()`).
241
+ - **The mix is separate from the tracks.** A track's own `volume`, `muted`, `soloed`, `pan` and `panLaw` fields are the values it enters with. They seed its mixer entry when its id has none. `setTracks` keeps the mix of an id that is in the tracks it replaces, and `updateTrack` keeps the track's mix (also when it gives the track a new id); in those cases the fields are not read. `addTrack` seeds the entry from the new track's fields, also for the id of a track removed earlier. `getMixer()` holds the entries of the current tracks; each undo and redo state keeps its own. Change the mix with `setTrackVolume`, `setTrackMute`, `setTrackSolo`, `setTrackPan` and `setTrackPanLaw`, which act on a track that is in the current tracks. `engine.getState().tracks` and the tracks an adapter receives carry the current mix in those fields; `arrangement.getTracks()` does not (read `getMixer()`).
242
+ - **The pan law is a mixer value that the first audio decides.** `getMixer().get(id).panLaw` is `'auto'`, `'mono'` or `'stereo'`. A track enters with its own `panLaw` field when that is `'mono'` or `'stereo'`, else with the law of the decoded audio it holds (`'stereo'` if a clip's buffer has two or more channels), else with `'auto'`. A track that is `'auto'` gets the law of the first decoded audio it holds, in the same change that brings the audio: the same undo step for a clip added decoded, no step for a clip that finishes loading (every state the arrangement can return to gets the law of its own tracks; an undo or redo step that this makes equal to the state next to it is dropped, so a finished load can turn `canUndo` or `canRedo` false). From then on the clips do not change it: not a clip of the other kind, not the removal of every clip, not `setTracks` with the same id. The law belongs to the id, like the rest of the mix: an app that reuses a track id for other material gets the law the id had (use a new id, pass through `setTracks([])`, which drops every entry, or set the law). `setTrackPanLaw(id, 'mono' | 'stereo')` changes it and may be called before the track has audio; any other value, `'auto'` included, throws a `TypeError`. A MIDI track stays `'auto'` (it plays with the mono law), and the setter does nothing for it. The law converts no audio: a stereo clip on a `'mono'` track stays stereo. `engine.getState().tracks` and the tracks an adapter receives carry the law in `panLaw` (a track that carries no field and is `'auto'` is sent without one). A change of the law alone is one call of the adapter's optional `setTrackPanLaw`, also when an undo returns a track without audio to `'auto'`; an adapter without the method is sent the track again. Unlike the other four engine setters, `engine.setTrackPanLaw` makes no adapter call when the arrangement changed nothing (a track it does not hold, the law the track already has).
242
243
  - **Undo, redo and an aborted transaction restore the tracks, the clips and the mix** of the state they return to. Undoing a `removeTrack` brings the track back with the mix it had, and undoing an `updateTrack` that gave a track a new id brings back the old id with the mix it had then.
243
244
  - **Ids are unique.** `setTracks`, `addTrack` and `updateTrack` throw an `Error` for a duplicate track id, for two clips with one id on the same track, and for a track whose clips do not match its `kind` (a loading clip is an audio clip), before anything changes.
244
245
  - **Without an engine** clips are given no `startTick`, and a clip that came with one loses it when it is moved, trimmed on the left or split. Every clip without a tick, in the current tracks and in every undo and redo state, gets one from its `startSample` when an engine attaches. The shortest a clip may be is 0.1 s at the clip's own sample rate.
@@ -286,6 +287,7 @@ class PlaylistEngine {
286
287
  setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
287
288
  setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
288
289
  setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
290
+ setTrackPanLaw(trackId: string, panLaw: 'mono' | 'stereo', options?: MixerChangeOptions): void;
289
291
  setTrackRecording(trackId: string, recording: boolean): void; // plays muted during the take; not an undo step
290
292
  readonly recordingTrackIds: ReadonlySet<string>; // the arrangement's set (the same object), read-only
291
293
  zoomIn(): void;
@@ -354,6 +356,7 @@ interface Arrangement {
354
356
  setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
355
357
  setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
356
358
  setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
359
+ setTrackPanLaw(trackId: string, panLaw: 'mono' | 'stereo', options?: MixerChangeOptions): void;
357
360
 
358
361
  // the take's mute: the tracks a take is recording onto (outside the history)
359
362
  readonly recordingTrackIds: ReadonlySet<string>;
@@ -419,10 +422,13 @@ interface PlayoutAdapter {
419
422
  setPpqn?(ppqn: number): void;
420
423
 
421
424
  init(): Promise<void>;
422
- setTracks(tracks: readonly ClipTrack[]): void; // the engine's own objects: read them, never write to them. Loaded clips only
423
- addTrack?(track: ClipTrack): void;
425
+ // PlayoutTrack (core): a ClipTrack without the view fields of TRACK_VIEW_FIELDS (name,
426
+ // color, height, renderMode, spectrogramConfig, spectrogramColorMap); a change of those
427
+ // alone makes no adapter call. Read the tracks, never write to them. Loaded clips only
428
+ setTracks(tracks: readonly PlayoutTrack[]): void;
429
+ addTrack?(track: PlayoutTrack): void;
424
430
  removeTrack?(trackId: string): void;
425
- updateTrack?(trackId: string, track: ClipTrack): void;
431
+ updateTrack?(trackId: string, track: PlayoutTrack): void;
426
432
 
427
433
  play(startTime: number, endTime?: number): void;
428
434
  pause(): void;
@@ -436,6 +442,9 @@ interface PlayoutAdapter {
436
442
  setTrackMute(trackId: string, muted: boolean): void;
437
443
  setTrackSolo(trackId: string, soloed: boolean): void;
438
444
  setTrackPan(trackId: string, pan: number): void;
445
+ // Optional. 'auto': from the track's clips (core's trackPanLaw). Without it the engine sends
446
+ // the track again (updateTrack, else setTracks), with the law in track.panLaw.
447
+ setTrackPanLaw?(trackId: string, panLaw: PanLaw): void;
439
448
  setLoop(enabled: boolean, start: number, end: number): void;
440
449
 
441
450
  setTempo?(bpm: number, atTick?: number): boolean | void;
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AudioClip, ClipTrack, ClipSource, ArrangementTrack, ValueSource } from '@waveform-playlist/core';
1
+ import { AudioClip, ClipTrack, ClipSource, PanLaw, ArrangementTrack, DecidedPanLaw, ValueSource, PlayoutTrack } from '@waveform-playlist/core';
2
2
 
3
3
  /**
4
4
  * Clip Operations
@@ -209,8 +209,8 @@ interface ArrangementSourcesValue {
209
209
  }
210
210
 
211
211
  /**
212
- * The mixer part of an arrangement: volume, mute, solo and pan by track id,
213
- * as pure functions over an immutable Map.
212
+ * The mixer part of an arrangement: volume, mute, solo, pan and pan law by
213
+ * track id, as pure functions over an immutable Map.
214
214
  *
215
215
  * The values live here and not on the track objects, so a fader move
216
216
  * replaces no track. The current Map holds an entry for each current track;
@@ -218,6 +218,11 @@ interface ArrangementSourcesValue {
218
218
  * `volume` / `muted` / `soloed` / `pan` fields are the values it ENTERED
219
219
  * with; they seed its entry when its id has none, and are not read while it
220
220
  * has one.
221
+ *
222
+ * The pan law (#684) is `'auto'` until the track holds decoded audio; that
223
+ * audio decides it (`decidePanLaws`), and from then on the track's clips do
224
+ * not change it. `'auto'` is only ever the law of a track with no decoded
225
+ * audio, or of a MIDI track (which always plays with the mono law).
221
226
  */
222
227
 
223
228
  interface TrackMixerValues {
@@ -225,6 +230,8 @@ interface TrackMixerValues {
225
230
  muted: boolean;
226
231
  soloed: boolean;
227
232
  pan: number;
233
+ /** `'auto'` until the track's first decoded audio decides it (#684). */
234
+ panLaw: PanLaw;
228
235
  }
229
236
 
230
237
  /**
@@ -341,6 +348,13 @@ interface Arrangement {
341
348
  setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
342
349
  setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
343
350
  setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
351
+ /**
352
+ * Set a track's pan law (#684): `'mono'` or `'stereo'`. Any other value,
353
+ * `'auto'` included, throws a `TypeError`. A mixer value like the four
354
+ * above: `'auto'` until the track's first decoded audio decides it, after
355
+ * which the track's clips do not change it. A MIDI track has none to set.
356
+ */
357
+ setTrackPanLaw(trackId: string, panLaw: DecidedPanLaw, options?: MixerChangeOptions): void;
344
358
  /**
345
359
  * The ids of the tracks a take is recording onto. The same Set until one
346
360
  * is added or removed; never write to it.
@@ -463,14 +477,18 @@ interface PlayoutAdapter {
463
477
  /** Set the adapter's tick resolution. Optional — adapters with fixed PPQN ignore this. */
464
478
  setPpqn?(ppqn: number): void;
465
479
  init(): Promise<void>;
466
- /** The tracks are the engine's own objects: read them, never write to them. */
467
- setTracks(tracks: readonly ClipTrack[]): void;
480
+ /**
481
+ * Copies of the engine's tracks without their view fields (core's
482
+ * `TRACK_VIEW_FIELDS`), sharing its clips: read them, never write to them.
483
+ * A change of view fields alone causes no adapter call.
484
+ */
485
+ setTracks(tracks: readonly PlayoutTrack[]): void;
468
486
  /** Incrementally add a single track without rebuilding the entire playout. */
469
- addTrack?(track: ClipTrack): void;
487
+ addTrack?(track: PlayoutTrack): void;
470
488
  /** Incrementally remove a single track without rebuilding the entire playout. */
471
489
  removeTrack?(trackId: string): void;
472
490
  /** Update a single track's clips (removes old, adds new). */
473
- updateTrack?(trackId: string, track: ClipTrack): void;
491
+ updateTrack?(trackId: string, track: PlayoutTrack): void;
474
492
  play(startTime: number, endTime?: number): void;
475
493
  pause(): void;
476
494
  stop(): void;
@@ -487,6 +505,13 @@ interface PlayoutAdapter {
487
505
  setTrackMute(trackId: string, muted: boolean): void;
488
506
  setTrackSolo(trackId: string, soloed: boolean): void;
489
507
  setTrackPan(trackId: string, pan: number): void;
508
+ /**
509
+ * A track's pan law changed (#684). `'auto'` means "from the track's
510
+ * clips" (core's `trackPanLaw`); it is sent when an undo returns a track
511
+ * without audio to it. Optional: an adapter without it is sent the track
512
+ * again (`updateTrack`, else `setTracks`), with the law in `track.panLaw`.
513
+ */
514
+ setTrackPanLaw?(trackId: string, panLaw: PanLaw): void;
490
515
  setLoop(enabled: boolean, start: number, end: number): void;
491
516
  /** Set tempo at a tick position. A defaulted (no atTick) call sets the base
492
517
  * tempo on a single-entry map; adapters MAY refuse it when their tempo map
@@ -740,6 +765,14 @@ declare class PlaylistEngine {
740
765
  setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
741
766
  setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
742
767
  setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
768
+ /**
769
+ * Set a track's pan law (#684): the arrangement's `setTrackPanLaw`.
770
+ * Unlike the four setters above, nothing is sent to the adapter when the
771
+ * arrangement changed nothing (a track it does not hold, the law the
772
+ * track already has): for an adapter without `setTrackPanLaw` the call
773
+ * would be a whole-track `updateTrack`.
774
+ */
775
+ setTrackPanLaw(trackId: string, panLaw: DecidedPanLaw, options?: MixerChangeOptions): void;
743
776
  /**
744
777
  * Mark a track as the one a take is recording onto, or clear it when the
745
778
  * take ends: the arrangement's `setTrackRecording`. That track plays muted
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AudioClip, ClipTrack, ClipSource, ArrangementTrack, ValueSource } from '@waveform-playlist/core';
1
+ import { AudioClip, ClipTrack, ClipSource, PanLaw, ArrangementTrack, DecidedPanLaw, ValueSource, PlayoutTrack } from '@waveform-playlist/core';
2
2
 
3
3
  /**
4
4
  * Clip Operations
@@ -209,8 +209,8 @@ interface ArrangementSourcesValue {
209
209
  }
210
210
 
211
211
  /**
212
- * The mixer part of an arrangement: volume, mute, solo and pan by track id,
213
- * as pure functions over an immutable Map.
212
+ * The mixer part of an arrangement: volume, mute, solo, pan and pan law by
213
+ * track id, as pure functions over an immutable Map.
214
214
  *
215
215
  * The values live here and not on the track objects, so a fader move
216
216
  * replaces no track. The current Map holds an entry for each current track;
@@ -218,6 +218,11 @@ interface ArrangementSourcesValue {
218
218
  * `volume` / `muted` / `soloed` / `pan` fields are the values it ENTERED
219
219
  * with; they seed its entry when its id has none, and are not read while it
220
220
  * has one.
221
+ *
222
+ * The pan law (#684) is `'auto'` until the track holds decoded audio; that
223
+ * audio decides it (`decidePanLaws`), and from then on the track's clips do
224
+ * not change it. `'auto'` is only ever the law of a track with no decoded
225
+ * audio, or of a MIDI track (which always plays with the mono law).
221
226
  */
222
227
 
223
228
  interface TrackMixerValues {
@@ -225,6 +230,8 @@ interface TrackMixerValues {
225
230
  muted: boolean;
226
231
  soloed: boolean;
227
232
  pan: number;
233
+ /** `'auto'` until the track's first decoded audio decides it (#684). */
234
+ panLaw: PanLaw;
228
235
  }
229
236
 
230
237
  /**
@@ -341,6 +348,13 @@ interface Arrangement {
341
348
  setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
342
349
  setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
343
350
  setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
351
+ /**
352
+ * Set a track's pan law (#684): `'mono'` or `'stereo'`. Any other value,
353
+ * `'auto'` included, throws a `TypeError`. A mixer value like the four
354
+ * above: `'auto'` until the track's first decoded audio decides it, after
355
+ * which the track's clips do not change it. A MIDI track has none to set.
356
+ */
357
+ setTrackPanLaw(trackId: string, panLaw: DecidedPanLaw, options?: MixerChangeOptions): void;
344
358
  /**
345
359
  * The ids of the tracks a take is recording onto. The same Set until one
346
360
  * is added or removed; never write to it.
@@ -463,14 +477,18 @@ interface PlayoutAdapter {
463
477
  /** Set the adapter's tick resolution. Optional — adapters with fixed PPQN ignore this. */
464
478
  setPpqn?(ppqn: number): void;
465
479
  init(): Promise<void>;
466
- /** The tracks are the engine's own objects: read them, never write to them. */
467
- setTracks(tracks: readonly ClipTrack[]): void;
480
+ /**
481
+ * Copies of the engine's tracks without their view fields (core's
482
+ * `TRACK_VIEW_FIELDS`), sharing its clips: read them, never write to them.
483
+ * A change of view fields alone causes no adapter call.
484
+ */
485
+ setTracks(tracks: readonly PlayoutTrack[]): void;
468
486
  /** Incrementally add a single track without rebuilding the entire playout. */
469
- addTrack?(track: ClipTrack): void;
487
+ addTrack?(track: PlayoutTrack): void;
470
488
  /** Incrementally remove a single track without rebuilding the entire playout. */
471
489
  removeTrack?(trackId: string): void;
472
490
  /** Update a single track's clips (removes old, adds new). */
473
- updateTrack?(trackId: string, track: ClipTrack): void;
491
+ updateTrack?(trackId: string, track: PlayoutTrack): void;
474
492
  play(startTime: number, endTime?: number): void;
475
493
  pause(): void;
476
494
  stop(): void;
@@ -487,6 +505,13 @@ interface PlayoutAdapter {
487
505
  setTrackMute(trackId: string, muted: boolean): void;
488
506
  setTrackSolo(trackId: string, soloed: boolean): void;
489
507
  setTrackPan(trackId: string, pan: number): void;
508
+ /**
509
+ * A track's pan law changed (#684). `'auto'` means "from the track's
510
+ * clips" (core's `trackPanLaw`); it is sent when an undo returns a track
511
+ * without audio to it. Optional: an adapter without it is sent the track
512
+ * again (`updateTrack`, else `setTracks`), with the law in `track.panLaw`.
513
+ */
514
+ setTrackPanLaw?(trackId: string, panLaw: PanLaw): void;
490
515
  setLoop(enabled: boolean, start: number, end: number): void;
491
516
  /** Set tempo at a tick position. A defaulted (no atTick) call sets the base
492
517
  * tempo on a single-entry map; adapters MAY refuse it when their tempo map
@@ -740,6 +765,14 @@ declare class PlaylistEngine {
740
765
  setTrackMute(trackId: string, muted: boolean, options?: MixerChangeOptions): void;
741
766
  setTrackSolo(trackId: string, soloed: boolean, options?: MixerChangeOptions): void;
742
767
  setTrackPan(trackId: string, pan: number, options?: MixerChangeOptions): void;
768
+ /**
769
+ * Set a track's pan law (#684): the arrangement's `setTrackPanLaw`.
770
+ * Unlike the four setters above, nothing is sent to the adapter when the
771
+ * arrangement changed nothing (a track it does not hold, the law the
772
+ * track already has): for an adapter without `setTrackPanLaw` the call
773
+ * would be a whole-track `updateTrack`.
774
+ */
775
+ setTrackPanLaw(trackId: string, panLaw: DecidedPanLaw, options?: MixerChangeOptions): void;
743
776
  /**
744
777
  * Mark a track as the one a take is recording onto, or clear it when the
745
778
  * take ends: the arrangement's `setTrackRecording`. That track plays muted
package/dist/index.js CHANGED
@@ -194,7 +194,7 @@ function clampSeekPosition(time, duration) {
194
194
  }
195
195
 
196
196
  // src/arrangement/Arrangement.ts
197
- var import_core7 = require("@waveform-playlist/core");
197
+ var import_core8 = require("@waveform-playlist/core");
198
198
 
199
199
  // src/arrangement/attach.ts
200
200
  var ARRANGEMENT_ATTACH_DECODE = /* @__PURE__ */ Symbol.for(
@@ -748,6 +748,7 @@ function sameValue(a, b) {
748
748
  }
749
749
 
750
750
  // src/arrangement/mixer.ts
751
+ var import_core5 = require("@waveform-playlist/core");
751
752
  var EMPTY_MIXER = /* @__PURE__ */ new Map();
752
753
  var DEFAULT_VOLUME = 1;
753
754
  var DEFAULT_PAN = 0;
@@ -756,9 +757,33 @@ function mixerValuesOf(track) {
756
757
  volume: track.volume ?? DEFAULT_VOLUME,
757
758
  muted: track.muted ?? false,
758
759
  soloed: track.soloed ?? false,
759
- pan: track.pan ?? DEFAULT_PAN
760
+ pan: track.pan ?? DEFAULT_PAN,
761
+ panLaw: enteringPanLaw(track)
760
762
  };
761
763
  }
764
+ function decidedByAudio(track) {
765
+ if ((0, import_core5.trackKind)(track) === "midi") return null;
766
+ return (0, import_core5.audioPanLaw)({
767
+ clips: track.clips.filter((clip) => !(0, import_core5.isLoadingClip)(clip))
768
+ });
769
+ }
770
+ function enteringPanLaw(track) {
771
+ if ((0, import_core5.trackKind)(track) === "midi") return import_core5.DEFAULT_PAN_LAW;
772
+ if ((0, import_core5.isDecidedPanLaw)(track.panLaw)) return track.panLaw;
773
+ return decidedByAudio(track) ?? import_core5.DEFAULT_PAN_LAW;
774
+ }
775
+ function decidePanLaws(mixer, tracks) {
776
+ let next = null;
777
+ for (const track of tracks) {
778
+ const entry = mixer.get(track.id);
779
+ if (!entry || entry.panLaw !== import_core5.DEFAULT_PAN_LAW) continue;
780
+ const panLaw = decidedByAudio(track);
781
+ if (!panLaw) continue;
782
+ if (!next) next = new Map(mixer);
783
+ next.set(track.id, { ...entry, panLaw });
784
+ }
785
+ return next ?? mixer;
786
+ }
762
787
  function seedMixer(mixer, tracks) {
763
788
  let next = null;
764
789
  for (const track of tracks) {
@@ -795,7 +820,7 @@ function withoutEntry(mixer, trackId) {
795
820
  return next;
796
821
  }
797
822
  function sameValues(a, b) {
798
- return a.volume === b.volume && a.muted === b.muted && a.soloed === b.soloed && a.pan === b.pan;
823
+ return a.volume === b.volume && a.muted === b.muted && a.soloed === b.soloed && a.pan === b.pan && a.panLaw === b.panLaw;
799
824
  }
800
825
  function sameMixerValues(a, b) {
801
826
  if (a === b) return true;
@@ -817,12 +842,14 @@ function mixerChanges(before, after) {
817
842
  if (was.muted !== now.muted) values.muted = now.muted;
818
843
  if (was.soloed !== now.soloed) values.soloed = now.soloed;
819
844
  if (was.pan !== now.pan) values.pan = now.pan;
845
+ if (was.panLaw !== now.panLaw) values.panLaw = now.panLaw;
820
846
  if (Object.keys(values).length > 0) changes.push({ trackId, values });
821
847
  }
822
848
  return changes;
823
849
  }
824
850
  function withMixerValues(track, values) {
825
- if (!values || track.volume === values.volume && track.muted === values.muted && track.soloed === values.soloed && track.pan === values.pan) {
851
+ if (!values || track.volume === values.volume && track.muted === values.muted && track.soloed === values.soloed && track.pan === values.pan && // A track that carries no law is an 'auto' track: no copy for that alone.
852
+ (track.panLaw ?? import_core5.DEFAULT_PAN_LAW) === values.panLaw) {
826
853
  return track;
827
854
  }
828
855
  return { ...track, ...values };
@@ -983,6 +1010,27 @@ function stateWithMixerValues(trackId, patch) {
983
1010
  return next;
984
1011
  };
985
1012
  }
1013
+ function stateWithDecidedPanLaws() {
1014
+ const decided = /* @__PURE__ */ new Map();
1015
+ const states = /* @__PURE__ */ new Map();
1016
+ return (state) => {
1017
+ const known = states.get(state);
1018
+ if (known) return known;
1019
+ let byTracks = decided.get(state.mixer);
1020
+ if (!byTracks) {
1021
+ byTracks = /* @__PURE__ */ new Map();
1022
+ decided.set(state.mixer, byTracks);
1023
+ }
1024
+ let mixer = byTracks.get(state.tracks);
1025
+ if (!mixer) {
1026
+ mixer = decidePanLaws(state.mixer, state.tracks);
1027
+ byTracks.set(state.tracks, mixer);
1028
+ }
1029
+ const next = mixer === state.mixer ? state : { ...state, mixer };
1030
+ states.set(state, next);
1031
+ return next;
1032
+ };
1033
+ }
986
1034
  function writeThrough(history, base, current, write) {
987
1035
  const nextBase = base && { ...base, state: write(base.state) };
988
1036
  return {
@@ -992,9 +1040,9 @@ function writeThrough(history, base, current, write) {
992
1040
  }
993
1041
 
994
1042
  // src/arrangement/ticks.ts
995
- var import_core5 = require("@waveform-playlist/core");
1043
+ var import_core6 = require("@waveform-playlist/core");
996
1044
  function forLoadedClips(change) {
997
- return (clip) => (0, import_core5.isLoadingClip)(clip) ? clip : change(clip);
1045
+ return (clip) => (0, import_core6.isLoadingClip)(clip) ? clip : change(clip);
998
1046
  }
999
1047
  function withClips(track, change) {
1000
1048
  let changed = false;
@@ -1037,12 +1085,12 @@ function rescaleTick(tick, fromPpqn, toPpqn) {
1037
1085
  }
1038
1086
 
1039
1087
  // src/arrangement/validate.ts
1040
- var import_core6 = require("@waveform-playlist/core");
1088
+ var import_core7 = require("@waveform-playlist/core");
1041
1089
  function assertClipsOfTrackKind(track) {
1042
1090
  const clips = track.clips.map(
1043
- (clip) => (0, import_core6.isLoadingClip)(clip) ? { id: clip.id, name: clip.name } : clip
1091
+ (clip) => (0, import_core7.isLoadingClip)(clip) ? { id: clip.id, name: clip.name } : clip
1044
1092
  );
1045
- (0, import_core6.assertTrackKind)({ ...track, clips });
1093
+ (0, import_core7.assertTrackKind)({ ...track, clips });
1046
1094
  }
1047
1095
  function assertUniqueClipIds(track) {
1048
1096
  const seen = /* @__PURE__ */ new Set();
@@ -1451,6 +1499,23 @@ var ArrangementImpl = class {
1451
1499
  setTrackPan(trackId, pan, options) {
1452
1500
  this._setMixerValues(trackId, { pan }, options);
1453
1501
  }
1502
+ /**
1503
+ * Set a track's pan law (#684): a mixer value like the four above. It
1504
+ * takes a decided law; anything else, `'auto'` included, throws before
1505
+ * anything changes (a decided track does not go back to `'auto'`). May be
1506
+ * called before the track holds audio: the law is then decided, and the
1507
+ * first audio does not change it. A MIDI track has no law to set.
1508
+ */
1509
+ setTrackPanLaw(trackId, panLaw, options) {
1510
+ if (!(0, import_core8.isDecidedPanLaw)(panLaw)) {
1511
+ throw new TypeError(
1512
+ `setTrackPanLaw: panLaw must be 'mono' or 'stereo', got ${JSON.stringify(panLaw) ?? String(panLaw)}`
1513
+ );
1514
+ }
1515
+ const track = this._tracks.find((t) => t.id === trackId);
1516
+ if (!track || (0, import_core8.trackKind)(track) === "midi") return;
1517
+ this._setMixerValues(trackId, { panLaw }, options);
1518
+ }
1454
1519
  // ---------------------------------------------------------------------------
1455
1520
  // Recording mute (the track a take is recording onto)
1456
1521
  // ---------------------------------------------------------------------------
@@ -1531,7 +1596,7 @@ var ArrangementImpl = class {
1531
1596
  ...track,
1532
1597
  clips: track.clips.map((clip) => {
1533
1598
  if (held?.has(clip)) return clip;
1534
- if ((0, import_core7.isLoadingClip)(clip)) return { ...clip };
1599
+ if ((0, import_core8.isLoadingClip)(clip)) return { ...clip };
1535
1600
  return { ...clip, startTick: clip.startTick ?? this._tickAt(clip.startSample) };
1536
1601
  })
1537
1602
  };
@@ -1748,8 +1813,58 @@ var ArrangementImpl = class {
1748
1813
  state: withStateTracks(state, pass.stored(state.tracks))
1749
1814
  };
1750
1815
  }
1816
+ this._decidePanLaws();
1751
1817
  return this._tracks === previous ? null : previous;
1752
1818
  }
1819
+ /**
1820
+ * First audio decides a track's pan law (#684): in the current state and
1821
+ * in every state the arrangement can return to, a track whose law is
1822
+ * `'auto'` and that holds decoded audio gets the law of that audio, each
1823
+ * state from its own tracks. Part of the change that brought the audio:
1824
+ * not an undo step. `mixerVersion` counts it when the current mix changed.
1825
+ *
1826
+ * A saved step that no longer differs from the state next to it is
1827
+ * dropped, as `_writeMixerThrough` does (a law set on a track whose only
1828
+ * clip was loading, and the clip turns out to have that law): its undo, or
1829
+ * its redo, would do nothing. A gesture whose own step went has no step to
1830
+ * join, and its next change pushes a new one; a gesture whose step is still
1831
+ * the top one goes on.
1832
+ */
1833
+ _decidePanLaws() {
1834
+ const decide = stateWithDecidedPanLaws();
1835
+ const current = this._currentState();
1836
+ const decided = decide(current);
1837
+ if (decided !== current) {
1838
+ this._mixer = decided.mixer;
1839
+ this._mixerVersion++;
1840
+ }
1841
+ const changedIn = (before, after) => after.undo.some((state, i) => state !== before.undo[i]) || after.redo.some((state, i) => state !== before.redo[i]);
1842
+ let base = this._transaction?.state ?? null;
1843
+ if (this._transaction && base) {
1844
+ const decidedBase = decide(base);
1845
+ if (decidedBase !== base) {
1846
+ base = decidedBase;
1847
+ this._transaction = { ...this._transaction, state: decidedBase };
1848
+ }
1849
+ }
1850
+ const mapped = mapHistoryStates(this._history, decide);
1851
+ const savedChanged = changedIn(this._history, mapped);
1852
+ if (savedChanged || decided !== current) {
1853
+ const top = mapped.undo[mapped.undo.length - 1];
1854
+ const kept = dropRepeats(mapped, base ?? decided);
1855
+ if (savedChanged || kept !== mapped) this._history = kept;
1856
+ if (this._gesture && kept !== mapped) {
1857
+ const newTop = kept.undo[kept.undo.length - 1];
1858
+ if (!(newTop !== void 0 && top !== void 0 && sameState(newTop, top))) {
1859
+ this._gesture = null;
1860
+ }
1861
+ }
1862
+ }
1863
+ if (this._gesture) {
1864
+ const before = mapHistoryStates(this._gesture.before, decide);
1865
+ if (changedIn(this._gesture.before, before)) this._gesture = { ...this._gesture, before };
1866
+ }
1867
+ }
1753
1868
  /** A load finished or failed by itself (not inside one of this arrangement's methods). */
1754
1869
  _onSourceSettled() {
1755
1870
  const previous = this._switchLoaded();
@@ -1819,7 +1934,7 @@ function createArrangement(options = {}) {
1819
1934
  }
1820
1935
 
1821
1936
  // src/adapterSync.ts
1822
- var import_core8 = require("@waveform-playlist/core");
1937
+ var import_core9 = require("@waveform-playlist/core");
1823
1938
  var AdapterSync = class {
1824
1939
  constructor(_arrangement, _adapter) {
1825
1940
  this._arrangement = _arrangement;
@@ -1865,6 +1980,14 @@ var AdapterSync = class {
1865
1980
  this._projection = { tracksVersion, mixerVersion, tracks };
1866
1981
  return tracks;
1867
1982
  }
1983
+ /**
1984
+ * `tracks()` as the adapter is given them: without the view fields. Made
1985
+ * for each send; nothing depends on the adapter's tracks keeping their
1986
+ * identity between sends.
1987
+ */
1988
+ _playoutTracks() {
1989
+ return freezeTracksInDevelopment(this.tracks().map((track) => this._playout(track)));
1990
+ }
1868
1991
  /** When an engine attaches: send the tracks that are there. With none, the adapter is in step as it is. */
1869
1992
  sendInitialTracks() {
1870
1993
  if (this._arrangement.getTracks().length > 0) {
@@ -1876,7 +1999,7 @@ var AdapterSync = class {
1876
1999
  /** Send the adapter every track, and remember up to which change that covers. */
1877
2000
  sendAllTracks() {
1878
2001
  const version = this._arrangement.tracksVersion;
1879
- this._adapter?.setTracks(this.tracks());
2002
+ this._adapter?.setTracks(this._playoutTracks());
1880
2003
  this._syncedTracksVersion = version;
1881
2004
  }
1882
2005
  /** Send the arrangement's current track to the adapter. False when there is no such track. */
@@ -1884,7 +2007,7 @@ var AdapterSync = class {
1884
2007
  const track = this._arrangement.getTracks().find((t) => t.id === trackId);
1885
2008
  if (!track) return false;
1886
2009
  if (this._adapter?.updateTrack) {
1887
- this._adapter.updateTrack(trackId, this._project(track));
2010
+ this._adapter.updateTrack(trackId, this._playout(this._project(track)));
1888
2011
  } else {
1889
2012
  this.sendAllTracks();
1890
2013
  }
@@ -1902,6 +2025,13 @@ var AdapterSync = class {
1902
2025
  }
1903
2026
  if (values.soloed !== void 0) this._adapter?.setTrackSolo(trackId, values.soloed);
1904
2027
  if (values.pan !== void 0) this._adapter?.setTrackPan(trackId, values.pan);
2028
+ if (values.panLaw !== void 0) this._sendPanLaw(trackId, values.panLaw);
2029
+ }
2030
+ /** The adapter's setter for a pan law, or, for an adapter without one, the track again (#684). */
2031
+ _sendPanLaw(trackId, panLaw) {
2032
+ if (!this._adapter) return;
2033
+ if (this._adapter.setTrackPanLaw) this._adapter.setTrackPanLaw(trackId, panLaw);
2034
+ else this.sendTrack(trackId);
1905
2035
  }
1906
2036
  /**
1907
2037
  * After an undo, a redo or an abort brought back another mix: the setter
@@ -1985,7 +2115,7 @@ var AdapterSync = class {
1985
2115
  _addTrack(trackId) {
1986
2116
  const track = this._arrangement.getTracks().find((t) => t.id === trackId);
1987
2117
  if (track && this._adapter?.addTrack) {
1988
- this._adapter.addTrack(this._project(track));
2118
+ this._adapter.addTrack(this._playout(this._project(track)));
1989
2119
  } else {
1990
2120
  this.sendAllTracks();
1991
2121
  }
@@ -2025,9 +2155,10 @@ var AdapterSync = class {
2025
2155
  if (before.clips.some((clip, i) => clip !== now.clips[i])) return false;
2026
2156
  const keys = /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(now)]);
2027
2157
  keys.delete("clips");
2158
+ for (const field of import_core9.TRACK_VIEW_FIELDS) keys.delete(field);
2028
2159
  return [...keys].every((key) => Object.is(before[key], now[key]));
2029
2160
  }
2030
- /** What an adapter and `getState()` are given for one of the arrangement's tracks. */
2161
+ /** What `getState()` is given for one of the arrangement's tracks (an adapter: `_playout` of it). */
2031
2162
  _project(track) {
2032
2163
  return this._withMix(this._loaded(track));
2033
2164
  }
@@ -2039,7 +2170,7 @@ var AdapterSync = class {
2039
2170
  _loaded(track) {
2040
2171
  const known = this._loadedMemo.get(track);
2041
2172
  if (known) return known;
2042
- const clips = track.clips.filter((clip) => !(0, import_core8.isLoadingClip)(clip));
2173
+ const clips = track.clips.filter((clip) => !(0, import_core9.isLoadingClip)(clip));
2043
2174
  const loaded = clips.length === track.clips.length ? track : { ...track, clips };
2044
2175
  this._loadedMemo.set(track, loaded);
2045
2176
  return loaded;
@@ -2052,6 +2183,12 @@ var AdapterSync = class {
2052
2183
  _recordingNow(trackId) {
2053
2184
  return this._arrangement.recordingTrackIds.has(trackId) && this._arrangement.getMixer().has(trackId);
2054
2185
  }
2186
+ /** A projected track without its view fields: a copy that shares its clips. */
2187
+ _playout(track) {
2188
+ const copy = { ...track };
2189
+ for (const field of import_core9.TRACK_VIEW_FIELDS) delete copy[field];
2190
+ return freezeTrackInDevelopment(copy);
2191
+ }
2055
2192
  /**
2056
2193
  * One track with its current mixer values in its fields, muted while a
2057
2194
  * take records onto it (memoized per track object).
@@ -2474,6 +2611,16 @@ var PlaylistEngine = class {
2474
2611
  const changed = this._mixerChanged(() => this._arrangement.setTrackPan(trackId, pan, options));
2475
2612
  if (!changed && !this._disposed) this._adapter?.setTrackPan(trackId, pan);
2476
2613
  }
2614
+ /**
2615
+ * Set a track's pan law (#684): the arrangement's `setTrackPanLaw`.
2616
+ * Unlike the four setters above, nothing is sent to the adapter when the
2617
+ * arrangement changed nothing (a track it does not hold, the law the
2618
+ * track already has): for an adapter without `setTrackPanLaw` the call
2619
+ * would be a whole-track `updateTrack`.
2620
+ */
2621
+ setTrackPanLaw(trackId, panLaw, options) {
2622
+ this._arrangement.setTrackPanLaw(trackId, panLaw, options);
2623
+ }
2477
2624
  /**
2478
2625
  * Mark a track as the one a take is recording onto, or clear it when the
2479
2626
  * take ends: the arrangement's `setTrackRecording`. That track plays muted