latency.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <meta name="GENERATOR" content="Mozilla/4.79 [en] (Windows NT 5.0; U) [Netscape]">
  6. <meta name="Author" content="Phil Burk">
  7. <meta name="Description" content="Internal docs. How a stream is started or stopped.">
  8. <meta name="KeyWords" content="audio, tutorial, library, portable, open-source, DirectSound,sound, music, JSyn, synthesis,">
  9. <title>PortAudio Implementation - Start/Stop</title>
  10. </head>
  11. <body>
  12. &nbsp;
  13. <center><table COLS=1 WIDTH="100%" BGCOLOR="#FADA7A" >
  14. <tr>
  15. <td>
  16. <center>
  17. <h1>
  18. <a href="http://www.portaudio.com">PortAudio</a> Latency</h1></center>
  19. </td>
  20. </tr>
  21. </table></center>
  22. <p>This page discusses the issues of audio latency for <a href="http://www.portaudio.com">PortAudio</a>
  23. . It offers suggestions on how to lower latency to improve the responsiveness
  24. of applications.
  25. <blockquote><b><a href="#what">What is Latency?</a></b>
  26. <br><b><a href="#portaudio">PortAudio and Latency</a></b>
  27. <br><b><a href="#macintosh">Macintosh</a></b>
  28. <br><b><a href="#unix">Unix</a></b>
  29. <br><b><a href="#windows">WIndows</a></b></blockquote>
  30. By Phil Burk, Copyright 2002 Phil Burk and Ross Bencina
  31. <h2>
  32. <a NAME="what"></a>What is Latency?</h2>
  33. Latency is basically longest time that you have to wait before you obtain
  34. a desired result. For digital audio output it is the time between making
  35. a sound in software and finally hearing it.
  36. <p>Consider the example of pressing a key on the ASCII keyboard to play
  37. a note. There are several stages in this process which each contribute
  38. their own latency. First the operating system must respond to the keypress.
  39. Then the audio signal generated must work its way through the PortAudio
  40. buffers. Then it must work its way through the audio card hardware. Then
  41. it must go through the audio amplifier which is very quick and then travel
  42. through the air. Sound travels at abous one foot per millisecond through
  43. air so placing speakers across the room can add 5-20 msec of delay.
  44. <p>The reverse process occurs when recording or responding to audio input.
  45. If you are processing audio, for example if you implement a software guitar
  46. fuzz box, then you have both the audio input and audio output latencies
  47. added together.
  48. <p>The audio buffers are used to prevent glitches in the audio stream.
  49. The user software writes audio into the output buffers. That audio is read
  50. by the low level audio driver or by DMA and sent to the DAC. If the computer
  51. gets busy doing something like reading the disk or redrawing the screen,
  52. then it may not have time to fill the audio buffer. The audio hardware
  53. then runs out of audio data, which causes a glitch. By using a large enough
  54. buffer we can ensure that there is always enough audio data for the audio
  55. hardware to play. But if the buffer is too large then the latency is high
  56. and the system feels sluggish. If you play notes on the keyboard then the
  57. "instrument" will feel unresponsive. So you want the buffers to be as small
  58. as possible without glitching.
  59. <h2>
  60. <a NAME="portaudio"></a>PortAudio and Latency</h2>
  61. The only delay that PortAudio can control is the total length of its buffers.
  62. The Pa_OpenStream() call takes two parameters: numBuffers and framesPerBuffer.
  63. The latency is also affected by the sample rate which we will call framesPerSecond.
  64. A frame is a set of samples that occur simultaneously. For a stereo stream,
  65. a frame is two samples.
  66. <p>The latency in milliseconds due to this buffering&nbsp; is:
  67. <blockquote><tt>latency_msec = 1000 * numBuffers * framesPerBuffer / framesPerSecond</tt></blockquote>
  68. This is not the total latency, as we have seen, but it is the part we can
  69. control.
  70. <p>If you call Pa_OpenStream() with numBuffers equal to zero, then PortAudio
  71. will select a conservative number that will prevent audio glitches. If
  72. you still get glitches, then you can pass a larger value for numBuffers
  73. until the glitching stops. if you try to pass a numBuffers value that is
  74. too small, then PortAudio will use its own idea of the minimum value.
  75. <p>PortAudio decides on the minimum number of buffers in a conservative
  76. way based on the frameRate, operating system and other variables. You can
  77. query the value that PortAudio will use by calling:
  78. <blockquote><tt>int Pa_GetMinNumBuffers( int framesPerBuffer, double sampleRate
  79. );</tt></blockquote>
  80. On some systems you can override the PortAudio minimum if you know your
  81. system can handle a lower value. You do this by setting an environment
  82. variable called PA_MIN_LATENCY_MSEC which is read by PortAudio when it
  83. starts up. This is supported on the PortAudio implementations for Windows
  84. MME, Windows DirectSound, and Unix OSS.
  85. <h2>
  86. <a NAME="macintosh"></a>Macintosh</h2>
  87. The best thing you can do to improve latency on Mac OS 8 and 9 is to turn
  88. off Virtual Memory. PortAudio V18 will detect that Virtual Memory is turned
  89. off and use a very low latency.
  90. <p>For Mac OS X the latency is very low because Apple Core Audio is so
  91. well written. You can set the PA_MIN_LATENCY_MSEC variable using:
  92. <blockquote><tt>setenv PA_MIN_LATENCY_MSEC 4</tt></blockquote>
  93. <h2>
  94. <a NAME="unix"></a>Unix</h2>
  95. PortAudio under Unix currently uses a backgroud thread that reads and writes
  96. to OSS. This gives you decent but not great latency. But if you raise the
  97. priority of the background thread to a very priority then you can get under
  98. 10 milliseconds latency. In order to raise your priority you must run the
  99. PortAudio program as root! You must also set PA_MIN_LATENCY_MSEC using
  100. the appropriate command for your shell.
  101. <h2>
  102. <a NAME="windows"></a>Windows</h2>
  103. Latency under Windows is a complex issue because of all the alternative
  104. operating system versions and device drivers. I have seen latency range
  105. from 8 milliseconds to 400 milliseconds. The worst case is when using Windows
  106. NT. Windows 98 is a little better, and Windows XP can be quite good if
  107. properly tuned.
  108. <p>The underlying audio API also makes a lot of difference. If the audio
  109. device has its own DirectSound driver then DirectSound can often provide
  110. better latency than WMME. But if a real DirectSound driver is not available
  111. for your device then it is emulated using WMME and the latency can be very
  112. high. That's where I saw the 400 millisecond latency. The ASIO implementation
  113. is generally very good and will give the lowest latency if available.
  114. <p>You can set the PA_MIN_LATENCY_MSEC variable to 50, for example, by
  115. entering in MS-DOS:
  116. <blockquote><tt>set PA_MIN_LATENCY_MSEC=50</tt></blockquote>
  117. If you enter this in a DOS window then you must run the PortAudio program
  118. from that same window for the variable to have an effect. You can add that
  119. line to your C:\AUTOEXEC.BAT file and reboot if you want it to affect any
  120. PortAudio based program.
  121. <p>For Windows XP, you can set environment variables as follows:
  122. <ol>
  123. <li>
  124. Select "Control Panel" from the "Start Menu".</li>
  125. <li>
  126. Launch the "System" Control Panel</li>
  127. <li>
  128. Click on the "Advanced" tab.</li>
  129. <li>
  130. Click on the "Environment Variables" button.</li>
  131. <li>
  132. Click "New" button under&nbsp; User Variables.</li>
  133. <li>
  134. Enter PA_MIN_LATENCY_MSEC for the name and some optimistic number for the
  135. value.</li>
  136. <li>
  137. Click OK, OK, OK.</li>
  138. </ol>
  139. <h3>
  140. Improving Latency on Windows</h3>
  141. There are several steps you can take to improve latency under windows.
  142. <ol>
  143. <li>
  144. Avoid reading or writng to disk when doing audio.</li>
  145. <li>
  146. Turn off all automated background tasks such as email clients, virus scanners,
  147. backup programs, FTP servers, web servers, etc. when doing audio.</li>
  148. <li>
  149. Disconnect from the network to prevent network traffic from interrupting
  150. your CPU.</li>
  151. </ol>
  152. <b>Important: </b>Windows XP users can also tune the OS to favor background
  153. tasks, such as audio, over foreground tasks, such as word processing. I
  154. lowered my latency from 40 to 10 milliseconds using this simple technique.
  155. <ol>
  156. <li>
  157. Select "Control Panel" from the "Start Menu".</li>
  158. <li>
  159. Launch the "System" Control Panel</li>
  160. <li>
  161. Click on the "Advanced" tab.</li>
  162. <li>
  163. Click on the "Settings" button in the Performance area.</li>
  164. <li>
  165. Click on the "Advanced" tab.</li>
  166. <li>
  167. Select "Background services" in the Processor Scheduling area.</li>
  168. <li>
  169. Click OK, OK.</li>
  170. </ol>
  171. Please let us know if you have others sugestions for lowering latency.
  172. <br>&nbsp;
  173. <br>&nbsp;
  174. </body>
  175. </html>
粤ICP备19079148号