title: How can I avoid this 'clicking' sound when I stop playing a sound?
body: <p>I really hope this question stays a programming question and do not end up an Sound Mechanics question... Here goes...</p>
  
  <p>I am doing some experiments in order to figure out how the Web Audio API works.  What I am trying to do is a simple "Hang up phone" sound playing in a loop. The problem is that when the sound ends, you can hear a quite annoying 'clicking' sound. I cannot explain it better, but you can hear it if you test the code.</p>
  
  <p>Is there some way I could avoid this? Some filter I could apply or anything?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var audioContext = new (AudioContext || webkitAudioContext)();
      
      var frequencyOffset = 0
      function boop(){
        // Our sound source is a simple triangle oscillator
        var oscillator = audioContext.createOscillator(); // Create sound source  
        oscillator.type = 'triangle';
        
        // Adding a gain node just to lower the volume a bit and to make the
        // sound less ear-piercing
        var gain = audioContext.createGain();
        oscillator.connect(gain);
        gain.connect(audioContext.destination);
        
        gain.gain.value = 0.1;
        // Just for fun let the frequency increase on each itteration
        oscillator.frequency.value = 200 + frequencyOffset;
        oscillator.start(0);
        
        // The sound should last for 250ms
        setTimeout(function(){
          oscillator.disconnect(); 
          oscillator.stop();
          gain.disconnect();
        }, 250);
        frequencyOffset += 1;
      }
  
      setInterval(boop, 500);</code></pre>
  </div>
  </div>
  </p>