@schmooky/zvuk 1.11.1 → 1.12.0

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.d.ts CHANGED
@@ -1332,6 +1332,21 @@ declare class Snapshot {
1332
1332
  private getParam;
1333
1333
  constructor(name: string, state: SnapshotState, getBus: (name: string) => Bus | undefined, getParam: (name: string) => Parameter | undefined);
1334
1334
  apply(opts?: ApplyOptions): Promise<void>;
1335
+ /**
1336
+ * Snap the live mix to a linear interpolation between this snapshot and
1337
+ * `other`. `t` is clamped to [0, 1]: `t = 0` matches this snapshot,
1338
+ * `t = 1` matches `other`, and intermediate values lerp every shared
1339
+ * bus level and parameter.
1340
+ *
1341
+ * Designed to be called continuously (e.g. from a `Parameter` subscriber)
1342
+ * — each call snaps instantly. For a one-shot crossfade with a fade
1343
+ * duration, use `apply({ fade })` instead.
1344
+ *
1345
+ * Buses or parameters present in only one of the two snapshots are
1346
+ * skipped. Mute flags are not interpolated: `t < 0.5` uses this
1347
+ * snapshot's mute, `t >= 0.5` uses `other`'s.
1348
+ */
1349
+ blendWith(other: Snapshot, t: number): void;
1335
1350
  }
1336
1351
 
1337
1352
  /**
@@ -1491,6 +1506,20 @@ interface Engine<TBusName extends string = string> {
1491
1506
  captureSnapshot(name: string): Snapshot;
1492
1507
  /** Build a Snapshot from explicit state without capturing the live engine. */
1493
1508
  snapshot(name: string, state: SnapshotState): Snapshot;
1509
+ /**
1510
+ * Snap the live mix to a linear interpolation between two snapshots.
1511
+ * `t` is clamped to [0, 1]: `t = 0` matches `a`, `t = 1` matches `b`,
1512
+ * intermediate values lerp every shared bus level and parameter.
1513
+ *
1514
+ * Drive a `Parameter` to animate over time — each call snaps instantly,
1515
+ * so a per-frame subscriber gives a continuous blend without scheduling
1516
+ * fades. Use `snapshot.apply({ fade })` for one-shot crossfades.
1517
+ *
1518
+ * const tension = engine.parameter('tension', 0);
1519
+ * tension.bindTo((t) => engine.blendSnapshots(calm, combat, t));
1520
+ * tension.set(0.4); // mix is now 40% of the way to combat
1521
+ */
1522
+ blendSnapshots(a: Snapshot, b: Snapshot, t: number): void;
1494
1523
  }
1495
1524
  interface CrossfadeOptions {
1496
1525
  /** Crossfade duration in seconds. Default 1.5. */
package/dist/index.js CHANGED
@@ -2788,6 +2788,39 @@ var Snapshot = class {
2788
2788
  }
2789
2789
  return Promise.all(tasks).then(() => void 0);
2790
2790
  }
2791
+ /**
2792
+ * Snap the live mix to a linear interpolation between this snapshot and
2793
+ * `other`. `t` is clamped to [0, 1]: `t = 0` matches this snapshot,
2794
+ * `t = 1` matches `other`, and intermediate values lerp every shared
2795
+ * bus level and parameter.
2796
+ *
2797
+ * Designed to be called continuously (e.g. from a `Parameter` subscriber)
2798
+ * — each call snaps instantly. For a one-shot crossfade with a fade
2799
+ * duration, use `apply({ fade })` instead.
2800
+ *
2801
+ * Buses or parameters present in only one of the two snapshots are
2802
+ * skipped. Mute flags are not interpolated: `t < 0.5` uses this
2803
+ * snapshot's mute, `t >= 0.5` uses `other`'s.
2804
+ */
2805
+ blendWith(other, t) {
2806
+ const u = t < 0 ? 0 : t > 1 ? 1 : t;
2807
+ const v = 1 - u;
2808
+ for (const [name, busA] of Object.entries(this.state.buses)) {
2809
+ const busB = other.state.buses[name];
2810
+ if (!busB) continue;
2811
+ const bus = this.getBus(name);
2812
+ if (!bus) continue;
2813
+ bus.muted = u < 0.5 ? busA.muted : busB.muted;
2814
+ bus.level = busA.level * v + busB.level * u;
2815
+ }
2816
+ for (const [name, valA] of Object.entries(this.state.parameters)) {
2817
+ const valB = other.state.parameters[name];
2818
+ if (valB === void 0) continue;
2819
+ const param = this.getParam(name);
2820
+ if (!param) continue;
2821
+ param.set(valA * v + valB * u);
2822
+ }
2823
+ }
2791
2824
  };
2792
2825
 
2793
2826
  // src/mixer/engine.ts
@@ -3171,6 +3204,9 @@ var EngineImpl = class {
3171
3204
  (paramName) => this.parameters.get(paramName)
3172
3205
  );
3173
3206
  }
3207
+ blendSnapshots(a, b, t) {
3208
+ a.blendWith(b, t);
3209
+ }
3174
3210
  /**
3175
3211
  * Lazily instantiate the bus graph against the live AudioContext. Called
3176
3212
  * from any path that touches the context (unlock, loadSound, bus, ...).