GainNode

GainNode 接口表示音量的变化。它是一个AudioNode音频处理模块,在输出前使用给定增益应用到输入。一个 GainNode 总是只有一个输入和一个输出,两者拥有同样数量的声道。

增益是一个无单位的值,会对所有输入声道的音频进行相应的增加。如果进行了修改,则会立即应用新增益,从而在结果音频中产生奇怪的“咔嗒”声。为了防止这种情况发生,请不要直接更改值,而应在AudioParam接口上使用指数插值方法

The GainNode is increasing the gain of the output.

Number of inputs 1
Number of outputs 1
Channel count mode "max"
Channel count 2 (not used in the default count mode)
Channel interpretation "speakers"

构造函数

GainNode()
创建GainNode对象的新实例不应手动创建增益节点;而应该使用AudioContext.createGain()方法。

属性

从其父类继承属性AudioNode

GainNode.gain 只读

是一个a-rateAudioParam表示应用的增益量。必须设置AudioParam.value或者使用AudioParam的方法改变增益效果。

方法

无指定方法;所有方法继承自父类AudioNode.

示例

The following example shows basic usage of an AudioContext to create a GainNode, which is then used to mute and unmute the audio when a Mute button is clicked by changing the gain property value.

The below snippet wouldn't work as is — for a complete working example, check out our Voice-change-O-matic demo (view source.)

<div>
  <button class="mute">Mute button</button>
</div>
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var gainNode = audioCtx.createGain();
var mute = document.querySelector('.mute');
var source;

if (navigator.mediaDevices.getUserMedia) {
 navigator.mediaDevices.getUserMedia (
   // constraints - only audio needed for this app
   {
     audio: true
   },

   // Success callback
   function(stream) {
     source = audioCtx.createMediaStreamSource(stream);

   },

   // Error callback
   function(err) {
     console.log('The following gUM error occured: ' + err);
   }
  );
} else {
   console.log('getUserMedia not supported on your browser!');
}

source.connect(gainNode);
gainNode.connect(audioCtx.destination);

  ...

mute.onclick = voiceMute;

function voiceMute() {
  if(mute.id == "") {
    gainNode.gain.setValueAtTime(0, audioCtx.currentTime);
    mute.id = "activated";
    mute.innerHTML = "Unmute";
  } else {
    gainNode.gain.setValueAtTime(1, audioCtx.currentTime);
    mute.id = "";
    mute.innerHTML = "Mute";
  }
}

规范

Specification Status Comment
Web Audio API
GainNode
Working Draft

浏览器兼容

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
GainNodeChrome Full support 14Edge Full support YesFirefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support Yes
GainNode() constructorChrome Full support 55
Notes
Full support 55
Notes
Notes Before Chrome 59, the default values were not supported.
Edge ? Firefox Full support 53IE No support NoOpera Full support 42Safari ? WebView Android Full support 55
Notes
Full support 55
Notes
Notes Before Chrome 59, the default values were not supported.
Chrome Android Full support 55
Notes
Full support 55
Notes
Notes Before Chrome 59, the default values were not supported.
Firefox Android Full support 53Opera Android Full support 42Safari iOS ? Samsung Internet Android Full support 6.0
gainChrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

参见