title: Sample-accurate punch-in and punch-out recording using tone.js
body: <p>Using the code below, I attempt to record exactly one bar of audio using <a href="https://github.com/Tonejs/Tone.js/blob/dev/Tone/component/channel/Recorder.ts" rel="nofollow noreferrer">tonejs` Recorder</a> (a wrapper around MediaRecorder).</p>
  
  <pre><code>const userMedia = new Tone.UserMedia();
  await userMedia.open();
  const recorder = new Recorder();
  userMedia.connect(recorder);
  
  Tone.Transport.scheduleOnce(t =&gt; {
    console.log('starting recorder', t);
    recorder.start();
  }, "4:0:0");
  
  Tone.Transport.scheduleOnce(async t =&gt; {
    const data = await recorder.stop();
    // ... when I load the audio data into a SamplePlayer and inspect the buffer:
    //  * the length in seconds is 1.97 (I expect 2.0 for a bar when the tempo is 120 bpm)
    //  * the length in samples is 87317 (I expect 88200 for 2 secs w/ 44.1 sample rate) 
  
  }, "5:0:0");
  
  </code></pre>
  
  <p>After recording, I load the audio data into a <code>SamplePlayer</code> and inspect the buffer:</p>
  
  <ul>
  <li>the length in seconds is 1.97 (I expect 2.0 for a bar when the tempo is 120 bpm)</li>
  <li>the length in samples is 87317 (I expect 88200 for 2 secs w/ 44.1 sample rate)</li>
  </ul>
  
  <p>I am looking for advice on how to record exactly one bar of audio (i.e. 88200 samples w/ 120 bpm and 44.1 sample rate), with the start of the recorded audio being exactly on the measure.</p>