title: Linux: Timing during recording/playing sound
body: <p>I have a more general question, regarding timing in a standard Linux-OS dealing with playing sound and receiving data over a serial port.</p>
  
  <p>In the moment, I'm reading a PCM-Signal arriving over a USB-to-Serial Bridge (pl2303) which is recorded, encoded and sent from a FPGA.</p>
  
  <p>Now, I need to create "peaks" at a known position in the recorded soundstream, and plan to play a soundfile from the same machine which is recording at a known moment. The peak has to begin and stop inside windows of maximal 50ms, it's length could be ~200ms...</p>
  
  <p>Now, my question is: How precise can I expect the timing to be? I know, that several components add "unkown lag", jitter:</p>
  
  <ul>
  <li>USB-to-Serial Bridge collects ~20 bytes from the serial side before sending them to the USB-side (at 230400Baud this results in ~1ms)</li>
  <li>If I call "`sleep 1; mpg123 $MP3FILE` &amp;" directly before my recording software, the Linux-Kernel will schedule them differenty (maybe this causes a few 10ms, depending on system load?)</li>
  <li>The soundcard/driver will maybe add some more unkown lag...</li>
  <li>Will tricks like "nice" or "sched_setscheduler" add value in my case?</li>
  <li>I could build an additional thread inside my recording sofware, which plays the sound. Doing this, the timing may be more precise, but I have a lot more work to do ...</li>
  </ul>
  
  <p>Thanks a lot.</p>
  
  <p>I will try it anyway, but I'm looking for some background toughts to understand and solve my upcoming problems better.</p>