GainNode - Web API 接口参考 编辑
GainNode
接口表示音量的变化。它是一个AudioNode
音频处理模块,在输出前使用给定增益应用到输入。一个 GainNode
总是只有一个输入和一个输出,两者拥有同样数量的声道。
增益是一个无单位的值,会对所有输入声道的音频进行相应的增加。如果进行了修改,则会立即应用新增益,从而在结果音频中产生奇怪的“咔嗒”声。为了防止这种情况发生,请不要直接更改值,而应在AudioParam
接口上使用指数插值方法。
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
。
是一个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 == "") {
// 0 means mute. If you still hear something, make sure you haven't
// connected your source into the output in addition to using the GainNode.
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 |
浏览器兼容
BCD tables only load in the browser
参见
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论