title: Recorder.js calculate and offset recording for latency
body: <p>I'm using Recorder.js to record audio from Google Chrome desktop and mobile browsers. In my specific use case I need to record exactly 3 seconds of audio, starting and ending at a specific time.</p>
  
  <p>Now I know that when recording audio, your soundcard cannot work in realtime due to hardware delays, so there is always a memory buffer which allows you to keep up recording without hearing jumps/stutters.</p>
  
  <p>Recorder.js allows you to configure the bufferLen variable exactly for this, while sampleRate is taken automatically from the audio context object. Here is a simplified version of how it works:</p>
  
  <pre><code>var context = new AudioContext();
  var recorder;
  navigator.getUserMedia({audio: true}, function(stream) {
      recorder = new Recorder(context.createMediaStreamSource(stream), {
          bufferLen: 4096
      });
  });
  
  function recordLoop() {
      recorder.record();
      window.setTimeout(function () {
          recorder.stop();
      }, 3000);
  }
  </code></pre>
  
  <p>The issue i'm facing is that record() does not offset for the buffer latency and neither does stop(). So instead of getting a three second sound, it's 2.97 seconds and the start is cut off.</p>
  
  <p>This means my recordings don't start in the same place, and also when I loop them, the loops are different lengths depending on your device latency!!</p>
  
  <p>There are two potentially solutions I see here:</p>
  
  <ul>
  <li>Adjust Recorder.js code to offset the buffer automatically against your start/stop times (maybe add new startSync/stopSync functions)</li>
  <li>Calculate the latency and create two offset timers to start and stop Recorder.js at the correct points in time.</li>
  </ul>
  
  <p>I'm trying solution 2, because solution 1 requires knowledge of buffer arrays which I don't have :( I believe the calculation for latency is:</p>
  
  <pre><code>var bufferSize = 4096;
  var sampleRate = 44100
  var latency = (bufferSize / sampleRate) * 2; // 0.18575963718820862 secs
  </code></pre>
  
  <p>However when I run these calculations in a real test I get:</p>
  
  <pre><code>var duration = 2.972154195011338 secs
  var latency = 0.18575963718820862 secs
  var total = duration + latency // 3.1579138321995464 secs
  </code></pre>
  
  <p>Something isn't right, it doesn't make 3 seconds and it's beginning to confuse me now! I've created a working fork of Recorder.js demo with a log:</p>
  
  <p><a href="http://kmturley.github.io/Recorderjs/" rel="nofollow">http://kmturley.github.io/Recorderjs/</a></p>
  
  <p>Any help would be greatly appreciated. Thanks!</p>