Skip to main content

getGainNode

Get the GainNode of one sound. Every sound's audio passes through it on the way to the master chain. Reach for it when you want to schedule your own volume automation or read the live gain.

getGainNode(id: string): GainNode | undefined;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of a sound, a stream, an overlapping instance ('laser:2') or a sprite ('game-sounds_jump').

Returns​

GainNode | undefined: the sound's gain node, or undefined for an id that is not loaded.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music', { loop: true });

const gain = soundHub.getGainNode('music');
if (gain) {
const now = soundHub.getContext().currentTime;
// A quick duck and back up
gain.gain.setValueAtTime(gain.gain.value, now);
gain.gain.linearRampToValueAtTime(0.2, now + 0.1);
gain.gain.linearRampToValueAtTime(1, now + 1);
}

Good to know​

  • The hub does not see changes you make to the node. getSoundVolume keeps reporting the volume last set through the hub, and the next setSoundVolume, fade, or play() with a volume overwrites your value.
  • The node is connected to the master input. Do not disconnect it, or the sound goes silent until its next play() connects it again.
  • For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​

  • setSoundVolume: change a sound's volume the supported way.
  • fadeIn: ramp a sound's volume with an event at the end.
  • getSource: the source node that feeds the gain node.
  • getMasterInput: the node every gain node connects to.