gpt4 book ai didi

javascript - Web Audio Api,设置增益

转载 作者:行者123 更新时间:2023-11-30 12:22:23 25 4
gpt4 key购买 nike

我一直在查看网络音频 API,但无法使音频增益正常工作。我这里设置了一个fiddle,可以理解函数的应用:http://jsfiddle.net/mnu70gy3/

我希望在点击事件中动态创建一个音调,但我无法让该音调淡出。下面是相关代码:

var audioCtx = new AudioContext();
var osc = {}; // set up an object for all the oscillators
var gainNodes = {}; // set up an object for all the gains
var now;
function tone(id,freq) {

// create osc / set gain / connect osc
gainNodes.id = audioCtx.createGain();
osc.id = audioCtx.createOscillator();
osc.id.connect(audioCtx.destination);

// set frequency
osc.id.frequency.value = freq;

// set gain at 1 and fade to 0 in one second
gainNodes.id.gain.value = 1.0;
gainNodes.id.gain.setValueAtTime(0, audioCtx.currentTime + 1);

// start and connect
osc.id.start(0);
osc.id.connect(audioCtx.destination);
}

是否可以这样做?

最佳答案

在您的代码中,您将振荡器连接到 destination 两次。而不是连接 oscillator -> gain -> destination

gainNodes.id = audioCtx.createGain();
osc.id = audioCtx.createOscillator();
osc.id.connect(gainNodes.id);

// set frequency and gain
osc.id.frequency.value = freq;
gainNodes.id.gain.value = 1.0;
gainNodes.id.gain.setValueAtTime(0, audioCtx.currentTime + 1);

// start and connect
osc.id.start(0);
gainNodes.id.connect(audioCtx.destination);

关于javascript - Web Audio Api,设置增益,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30564330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com