Writing an equalizer using biquad filters

Writing an equalizer using biquad filters

Example 1: an audio equalizer with an <audio> element

Example at JSBin, or you can try it in your browser:

60Hz  0 dB
170Hz  0 dB
350Hz  0 dB
1000Hz  0 dB
3500Hz  0 dB
10000Hz  0 dB

This example uses six BiquadFilter nodes with type=”peaking”.

If you read the description of this filter type: “Frequencies inside the range get a boost or an attenuation; frequencies outside it are unchanged.” This is exactly what we need to write a multi band equalizer! We’re going to use several sliders, each of which boosts one range of frequency values.

The definition says that:

    • the frequency property value of a filter will indicate the middle of the frequency range getting a boost or an attenuation, each slider corresponds to a filter whose frequency will be set to 60Hz, 170Hz, 350Hz, 1000Hz, 3500Hz, or 10000Hz.
    • the gain property value of a filter corresponds to the boost, in dB, to be applied; if negative, it will be an attenuation. We will code the sliders’ event listeners to change the gain value of the corresponding filter.
    • the Q property values control the width of the frequency band. The greater the Q value, the smaller the frequency band. We’ll ignore it for the purposes of this example.

HTML code extract:

  1. <h2>Equalizer made with the Web Audio API</h2>
  2.  class=“eq”>
  3.   
  4.       src=http://mainline.i3s.unice.fr/mooc/drums.mp3&#8221;>
  5.      Your browser does not support the audio tag.
  6.  
  7.   
     class=“controls”>
  8.     60Hz
  9.      type=“range”
  10.            value=“0” step=“1” min=“-30” max=“30”
  11.            oninput=changeGain(this.value, 0);>
  12.    
  13.      id=“gain0”>0 dB
  14.   
  •   
     class=“controls”>
  •    170Hz
  •     type=“range”
  •           value=“0” step=“1” min=“-30” max=“30”
  •           oninput=changeGain(this.value, 1);>
  •    
  •     id=“gain1”>0 dB
  •   
  •   
     class=“controls”>
  •     350Hz
  •      type=“range”
  •            value=“0” step=“1” min=“-30” max=“30”
  •            oninput=changeGain(this.value, 2);>
  •    
  •      id=“gain2”>0 dB
  •