Skip to main content

GainNode

The GainNode interface represents the change in volume (amplitude) of the audio signal. It is an AudioNode that applies given gain to processing audio data.

Direct gain modification often results in unpleasant 'clicks'. To avoid this effect, utilize exponential interpolation methods from the AudioParam.

gain-node

AudioNode properties

Number of inputs1
Number of outputs1
Channel count2
Channel count modemax
Channel interpretationspeakers

Envelope - ADSR

Envelope is term widely used in music and sound engineering, it describes how a sound changes over time. The most common way of describing an envelope is ADSR. This acronym stands for: attack, decay, sustain and release.

  • Attack - the time it takes for the sound to reach its peak volume from the beginning.
  • Decay - the time it takes for the sound to reach the sustain level after the peak volume.
  • Sustain - the volume level that the sound will stay at until the key is released.
  • Release - the time it takes for the sound to fade out after the key is released.

ADSR Example

You can read more about envelopes and ADSR on Wikipedia.

ADSR Playground

This is an interactive example demonstrating the ADSR on GainNode. You can adjust the parameters in real-time to hear and see how they affect the generated sound wave.

GainNode interactive playground

ADSR Envelope Example

Attack: 1.00s

Decay: 1.00s

Sustain: 0.50

Release: 2.00s

import { AudioContext } from 'react-native-audio-api';

const ctx = new AudioContext();
const osc = ctx.createOscillator();
const gain = ctx.createGain();

osc.connect(gain);
gain.connect(ctx.destination);

const now = ctx.currentTime;
gain.gain.setValueAtTime(0.00001, now);

// Attack -> peak at 1.00s
gain.gain.exponentialRampToValueAtTime(1, now + 1.00);

// Decay -> to sustain 0.50 at +1.00s
gain.gain.exponentialRampToValueAtTime(0.50001, now + 2.00);

// Immediate release after decay, release duration 2.00s
gain.gain.setValueAtTime(0.50, now + 2.00);
gain.gain.linearRampToValueAtTime(0, now + 4.00);

osc.start(now);
osc.stop(now + 4.00);

Constructor

BaseAudioContext.createGain()

Properties

It inherits all properties from AudioNode.

NameTypeDescription
gainAudioParama-rate AudioParam representing value of gain to apply.
Read only

Methods

GainNode does not define any additional methods. It inherits all methods from AudioNode.

Remarks

gain

  • Default value is 1.0.
  • Nominal range is -∞ to ∞.