Tone.js Follower to Create Side-Chain Volume Control

Viewed 308

I would like to control the gain of one source via another one to create gain automation on source 2 based on the negated envelope of source one.

Here's my logic:

 playerOne.connect(follower)
 follower.connect(signal)
 signal.connect(negate)
 negate.connect(gainTwo.gain)

where gainTwo is a gain node connected to playerTwo. Here is a codepen example: https://codepen.io/adam_fr/pen/BazVEwe

The expected outcome is that the gainTwo.gain gets ducked every time playerOne is triggered, however what I get is the opposite, the second source plays only when the first source is playing.

1 Answers

I've figured it out: need to invert the follower signal and push it up by 1, 1 being the maximum value the signal can reach. The codepen above is updated with the correct wiring:

// Flip the values and shift them up by the max value of the sidechain signal
const negate = new Tone.Multiply(-sideChainRatio)
const shift = new Tone.Add(1)

playerOne.connect(gainOne)
playerTwo.connect(gainTwo)        

playerOne.connect(follower)
follower.connect(signal)
signal.connect(negate)
negate.connect(shift)
shift.connect(gainTwo.gain)
Related