Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/docs/latency.html

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