Search code examples
javascriptweb-audio-api

Web audio API filter is not working


So I'm creating a synthesizer in the browser and want to add few filters to it. I created all the filter stuff and connect my oscillator to the filter and the fiter to audio destination. Now when I choose lowpass filter to play, I get this error in the console and no effects are being added:

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

Snippet of my code:

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);
}

What is this mean?


Solution

  • The error you see is pretty self explanatory. Basically, the filter.type = 3; isn't a valid BiquadFilterNode type which, as MDN lists, "is a string (enum) value defining the kind of filtering algorithm the node is implementing."

    It's values are the strings: 'highpass', 'bandpass', 'lowshelf', 'highshelf', 'peaking', 'notch' and 'allpass'.

    You can find their meanings on the MDN page.