使用Web Audio API进行弹出和点击

时间:2017-11-09 16:24:33

标签: javascript audio web-audio-api

我一直在尝试使用Web Audio API制作数字合成器,但我遇到了一些障碍。我正在尝试为复音合成器添加一个信封,我似乎无法正确使用它。我几乎尝试过linearRamptoValueAtTime,exponentialRamptoValueAtTime,setTargetAtTime等的每一个组合......而我仍然最终得到了这些烦人的流行音乐和点击。我觉得我已经阅读了每个教程并发布了这个主题,但我显然做错了什么。

这是我目前攻击和释放的代码。

攻击:

let osc1Vol;
keyboard.keyDown = function(note, freq) {
    let now = audioCtx.currentTime;
    const osc1 = audioCtx.createOscillator();
    if (gainNodeTable[freq]) {
    osc1Vol = gainNodeTable[freq];
    osc1Vol.gain.cancelScheduledValues(now);
    osc1Vol.gain.setValueAtTime(osc1Vol.gain.value, now);
  } else {
    osc1Vol = audioCtx.createGain();
    osc1Vol.gain.setValueAtTime(0, now);
  }
    osc1.connect(osc1Vol);
    osc1.type = osc1wave.value;
    osc1.frequency.value = (freq * octaveTable[osc1octave.value]);
    oscillators[freq] = osc1;
    gainNodeTable[freq] = osc1Vol;
    osc1Vol.connect(audioCtx.destination);
    osc1Vol.gain.linearRampToValueAtTime(1.0, (now + parseInt(attack.value)));
  osc1.start();
};

释放:

keyboard.keyUp = function (note, freq) {
    const now = audioCtx.currentTime;
    const gain = gainNodeTable[freq].gain.value;
    gainNodeTable[freq].gain.cancelScheduledValues(now);
    gainNodeTable[freq].gain.setValueAtTime(gain, now)
    gainNodeTable[freq].gain.exponentialRampToValueAtTime(0.0001, now + parseInt(decay.value));
    oscillators[freq].stop(now + parseInt(decay.value));
};

谢谢!

1 个答案:

答案 0 :(得分:0)

结束只是通过过滤器运行它来摆脱这些频率。