【发布时间】:2017-11-10 02:16:32
【问题描述】:
我一直在尝试使用 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));
};
谢谢!
【问题讨论】:
标签: javascript audio web-audio-api