GainNode

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

增益是一个无单位量,会对所有输入声道的音频进行相应的增加。当对 GainNode 进行修改时,新的增益会通过 de-zippering 算法进行应用,以防止出现“咔嗒”的奇怪声响。

 

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"

属性

继承自父级, AudioNode.

GainNode.gain 只读
一个a-rate 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>
  <a class="mute">Mute button</a>
</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.value = 0;
    mute.id = "activated";
    mute.innerHTML = "Unmute";
  } else {
    gainNode.gain.value = 1;
    mute.id = "";
    mute.innerHTML = "Mute";
  }
}

规范

Specification Status Comment
Web Audio API
GainNode
Working Draft  

浏览器兼容

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 10.0webkit 25.0 (25.0)  未实现 15.0webkit
22
6.0webkit
Feature Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? 26.0 1.2 ? ? ? 33.0

参考

文档标签和贡献者