title: Stop playing sound after specified duration in milliseconds using web audio API
body: <p>After starting to play audio from an AudioBuffer using <a href="https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode" rel="noreferrer">AudioBufferSourceNode</a>, how do I make it stop after a predetermined time in <em>milliseconds</em>? My setup is simple:</p>
  
  <pre><code>// create audio source
  var source = audioCtx.createBufferSource();
  
  // play audio from source
  source.start();
  </code></pre>
  
  <p>Using <a href="https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/stop" rel="noreferrer">AudioBufferSourceNode.stop()</a> I can only specify this duration in seconds, but supplying anything non-integer will evaluate to zero for some reason, and make the audio stop <em>immediately</em>:</p>
  
  <pre><code>source.stop(0.25); // should be 250 milliseconds, but audio stops immediately
  </code></pre>
  
  <p>setTimeout is not precise enough, and occasional jittering/drifting occurs, especially if the UI works a lot, or other calculations are being performed elsewhere.</p>
  
  <pre><code>setTimeout(function () {
      source.stop(); // very inaccurate
  }, 250);
  </code></pre>
  
  <p>Is there a trivial solution to stop playing an audio after a certain amount of <strong>milliseconds</strong>, without resorting to hacks like busy wait in a worker thread, or similar?</p>