tone 14.8.45 → 14.8.46

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.
@@ -1,6 +1,8 @@
1
1
  import { TimeClass } from "../../core/type/Time";
2
2
  import { PlaybackState } from "../../core/util/StateTimeline";
3
3
  import { TimelineValue } from "../../core/util/TimelineValue";
4
+ import { ToneAudioNode } from "../../core/context/ToneAudioNode";
5
+ import { Pow } from "../../signal/Pow";
4
6
  import { Signal } from "../../signal/Signal";
5
7
  import {
6
8
  onContextClose,
@@ -58,7 +60,7 @@ type TransportEventNames =
58
60
  interface SyncedSignalEvent {
59
61
  signal: Signal;
60
62
  initial: number;
61
- ratio: Gain;
63
+ nodes: ToneAudioNode<any>[];
62
64
  }
63
65
 
64
66
  type TransportCallback = (time: Seconds) => void;
@@ -707,25 +709,45 @@ export class Transport
707
709
  * Otherwise it will be computed based on their current values.
708
710
  */
709
711
  syncSignal(signal: Signal<any>, ratio?: number): this {
712
+ const now = this.now();
713
+ let source : TickParam<"bpm"> | ToneAudioNode<any> = this.bpm;
714
+ let sourceValue = 1 / (60 / source.getValueAtTime(now) / this.PPQ);
715
+ let nodes : ToneAudioNode<any>[] = [];
716
+ // If the signal is in the time domain, sync it to the reciprocal of
717
+ // the tempo instead of the tempo.
718
+ if (signal.units === "time") {
719
+ // The input to Pow should be in the range [1 / 4096, 1], where
720
+ // where 4096 is half of the buffer size of Pow's waveshaper.
721
+ // Pick a scaling factor based on the initial tempo that ensures
722
+ // that the initial input is in this range, while leaving room for
723
+ // tempo changes.
724
+ const scaleFactor = 1 / 64 / sourceValue;
725
+ const scaleBefore = new Gain(scaleFactor);
726
+ const reciprocal = new Pow(-1);
727
+ const scaleAfter = new Gain(scaleFactor);
728
+ // @ts-ignore
729
+ source.chain(scaleBefore, reciprocal, scaleAfter);
730
+ source = scaleAfter;
731
+ sourceValue = 1 / sourceValue;
732
+ nodes = [scaleBefore, reciprocal, scaleAfter];
733
+ }
710
734
  if (!ratio) {
711
735
  // get the sync ratio
712
- const now = this.now();
713
736
  if (signal.getValueAtTime(now) !== 0) {
714
- const bpm = this.bpm.getValueAtTime(now);
715
- const computedFreq = 1 / (60 / bpm / this.PPQ);
716
- ratio = signal.getValueAtTime(now) / computedFreq;
737
+ ratio = signal.getValueAtTime(now) / sourceValue;
717
738
  } else {
718
739
  ratio = 0;
719
740
  }
720
741
  }
721
742
  const ratioSignal = new Gain(ratio);
722
743
  // @ts-ignore
723
- this.bpm.connect(ratioSignal);
744
+ source.connect(ratioSignal);
724
745
  // @ts-ignore
725
746
  ratioSignal.connect(signal._param);
747
+ nodes.push(ratioSignal);
726
748
  this._syncedSignals.push({
727
749
  initial: signal.value,
728
- ratio: ratioSignal,
750
+ nodes: nodes,
729
751
  signal,
730
752
  });
731
753
  signal.value = 0;
@@ -740,7 +762,7 @@ export class Transport
740
762
  for (let i = this._syncedSignals.length - 1; i >= 0; i--) {
741
763
  const syncedSignal = this._syncedSignals[i];
742
764
  if (syncedSignal.signal === signal) {
743
- syncedSignal.ratio.dispose();
765
+ syncedSignal.nodes.forEach((node) => node.dispose());
744
766
  syncedSignal.signal.value = syncedSignal.initial;
745
767
  this._syncedSignals.splice(i, 1);
746
768
  }
@@ -480,6 +480,15 @@ describe("Signal", () => {
480
480
  }, 10);
481
481
  });
482
482
 
483
+ it("keeps the ratio of a time signal when the bpm changes", () => {
484
+ return ConstantOutput(({ transport }) => {
485
+ transport.bpm.value = 120;
486
+ const sig = new Signal("4n", "time").toDestination();
487
+ transport.syncSignal(sig);
488
+ transport.bpm.value = 240;
489
+ }, 0.25);
490
+ });
491
+
483
492
  it("outputs 0 when the signal is 0", () => {
484
493
  return ConstantOutput(({ transport }) => {
485
494
  transport.bpm.value = 120;
package/Tone/version.ts CHANGED
@@ -1 +1 @@
1
- export const version: string = "14.8.45";
1
+ export const version: string = "14.8.46";