网络音频 API 过滤器不起作用

Web audio API filter is not working

所以我正在浏览器中创建一个合成器,并想向其中添加一些过滤器。我创建了所有滤波器,并将我的振荡器连接到滤波器,将滤波器连接到音频目的地。现在当我选择低通滤波器播放时,我在控制台中收到此错误并且没有添加任何效果:

The provided value '3' is not a valid enum value of type BiquadFilterType.

我的代码片段:

function init() {
  octaveNumber = document.getElementById("octaveNum");
  audioCtx = new (window.AudioContext || window.webkitAudioContext);
  osc = audioCtx.createOscillator();
  volume = audioCtx.createGain();
  filter = audioCtx.createBiquadFilter();
  osc.connect(filter);
  volume.connect(audioCtx.destination);
  booleanVal = false;
  osc.frequency.value = freqSlider.value
  osc.start();
  gainDisplay.innerHTML = gainSlider.value;
}

lowpass.addEventListener("click", function(event) {
    filter.type = 3;  // In this case it's a lowshelf filter
    filter.frequency.value = 440;
    filter.Q.value = 0;
    filter.gain.value = 0;
})

function start() {
    UI('start');
    volume.gain.value = gainSlider.value;
    filter.connect(volume);
}

这是什么意思?

您看到的错误很容易解释。基本上,filter.type = 3; 不是有效的 BiquadFilterNode 类型,如 MDN 列表,"is a string (enum) value defining the kind of filtering algorithm the node is implementing."

它的值是字符串:'highpass'、'bandpass'、'lowshelf'、'highshelf'、'peaking'、'notch' 和 'allpass'.

您可以在 MDN 页面上找到它们的含义。