我正在用 WebMIDI 控制构建一个简单的合成器。增益节点对振荡器没有影响,它一直处于全音量状态。此外,当我弹奏和弦时,频率是正确的,但会有摇晃和刺耳的效果。使用我的 MIDI 控制器播放以及使用控制台启动和停止合成器时会出现问题。
这是我的合成器代码:
var synth = {
voices: {},
start: function (note, vol) {
this.voices[note] = {
gain: audio.createGain(),
osc: audio.createOscillator()
}
this.voices[note].gain.connect(audio.destination);
this.voices[note].osc.frequency.value = noteToFreq(note);
this.voices[note].osc.connect(this.voices[note].gain);
this.voices[note].osc.start(0);
this.voices[note].gain.gain.setTargetAtTime(vol, audio.currentTime, 0.5);
},
stop: function (note) {
this.voices[note].gain.gain.setTargetAtTime(0, audio.currentTime, 2);
this.voices[note].osc.stop(audio.currentTime + 2);
}
}