Skip to main content

getDuckLevel

Added in 6.5.0

Get the level of a target's duck at this moment, from 0 to 1. It reads the duck's gain, so during the attack and the release it returns the level on the way. Once the ramp is done that is the amount you gave duck while a trigger plays, and 1 otherwise.

getDuckLevel(target: string): number;

Parameters​

ParameterTypeDefaultDescription
targetstringrequiredA target name you passed to duck.

Returns​

number: the current level of the duck, or 1 when the target has no duck.

Example​

soundHub.duck('music', { when: 'voice', amount: 0.25 });

soundHub.getDuckLevel('music'); // 1
soundHub.play('voice');
// 50 ms later, after the default attack:
soundHub.getDuckLevel('music'); // 0.25

A meter next to the music​

// The level moves during a ramp, so read it every frame
const draw = () => {
duckMeter.value = soundHub.getDuckLevel('music');
requestAnimationFrame(draw);
};
draw();

Good to know​

  • For where the duck is heading rather than where it is, use isDucked or the duck_started and duck_ended events, which carry the target level in volume.
  • While nothing plays through the target, a browser does not run the duck's gain node, so its gain stays where it was. In 6.5.0 getDuckLevel returns that frozen value. From 6.5.1 it returns the level the duck is heading for, which is the level the target starts at.
  • The level multiplies the target's own volume. A sound at volume 0.8 ducked to 0.25 plays at 0.2, and getSoundVolume still returns 0.8.

See also​