]> Shamusworld >> Repos - ardour-manual/blob - include/latency-and-latency-compensation.html
531c977ada68302c09a8a5158522b84cb1cfe047
[ardour-manual] / include / latency-and-latency-compensation.html
1
2 <p>
3   <a
4   href="http://en.wikipedia.org/wiki/Latency_%28audio%29"><dfn>Latency</dfn></a> 
5   is a system's reaction time to a given stimulus. There are many factors that 
6   contribute to the total latency of a system. In order to achieve exact time 
7   synchronization all sources of latency need to be taken into account and 
8   compensated for.
9 </p>
10
11 <h2>Sources of Latency</h2>
12
13 <h3>Sound propagation through the air</h3>
14 <p>
15   Since sound is a mechanical perturbation in a fluid, it travels at 
16   comparatively slow <a href="http://en.wikipedia.org/wiki/Speed_of_sound">speed</a> 
17   of about 340 m/s. As a consequence, your acoustic guitar or piano has a 
18   latency of about 1&ndash;2 ms, due to the propagation time of the sound 
19   between your instrument and your ear. 
20 </p>
21 <h3>Digital-to-Analog and Analog-to-Digital conversion</h3>
22 <p>
23   Electric signals travel quite fast (on the order of the speed of light), 
24   so their propagation time is negligible in this context. But the conversions 
25   between the analog and digital domain take a comparatively long time to perform, 
26   so their contribution to the total latency may be considerable on
27   otherwise very low-latency systems. Conversion delay is usually below 1&nbsp;ms.
28 </p>
29 <h3>Digital Signal Processing</h3>
30 <p>
31   Digital processors tend to process audio in chunks, and the size of that chunk 
32   depends on the needs of the algorithm and performance/cost considerations. 
33   This is usually the main cause of latency when you use a computer and one you 
34   can try to predict and optimize.
35 </p>
36 <h3>Computer I/O Architecture</h3>
37 <p>
38   A computer is a general purpose processor, not a digital audio processor. 
39   This means our audio data has to jump a lot of fences in its path from the 
40   outside to the CPU and back, contending in the process with some other parts 
41   of the system vying for the same resources (CPU time, bus bandwidth, etc.) 
42 </p>
43
44 <h2>The Latency chain</h2>
45
46 <img src="/images/latency-chain.png"  title="Latency chain" alt="Latency chain" />
47 <p>
48   <em>Figure: Latency chain.</em> 
49   The numbers are an example for a typical PC. With professional gear and an 
50   optimized system the total roundtrip latency is usually lower. The important 
51   point is that latency is always additive and a sum of many independent factors.
52 </p>
53
54 <p>
55   Processing latency is usually divided into <dfn>capture latency</dfn> (the time 
56   it takes for the digitized audio to be available for digital processing, usually 
57   one audio period), and <dfn>playback latency</dfn> (the time it takes for
58   In practice, the combination of both matters. It is called <dfn>roundtrip 
59   latency</dfn>: the time necessary for a certain audio event to be captured, 
60   processed and played back.
61 </p>
62 <p class="note">
63   It is important to note that processing latency in a jackd is a matter of
64   choice. It can be lowered within the limits imposed by the hardware (audio 
65   device, CPU and bus speed) and audio driver. Lower latencies increase the 
66   load on the system because it needs to process the audio in smaller chunks 
67   which arrive much more frequently. The lower the latency, the more likely 
68   the system will fail to meet its processing deadline and the dreaded
69   <dfn>xrun</dfn> (short for buffer over- or under-run) will make its 
70   appearance more often, leaving its merry trail of clicks, pops and crackles.
71 </p>
72
73 <p>
74   The digital I/O latency is usually negligible for integrated or 
75   <abbr title="Periphal Component Interface">PCI</abbr> audio devices, but 
76   for USB or FireWire interfaces the bus clocking and buffering can add some 
77   milliseconds.
78 </p>
79
80
81 <h2>Low Latency usecases</h2>
82 <p>
83   Low latency is <strong>not</strong> always a feature you want to have. It 
84   comes with a couple of drawbacks: the most prominent is increased power 
85   consumption because the CPU needs to process many small chunks of audio data,
86   it is constantly active and can not enter power-saving mode (think fan-noise). 
87   Since each application that is part of the signal chain must run in every 
88   audio cycle, low-latency systems will undergo<dfn>context switches</dfn> 
89   between applications more often, which incur a significant overhead.  
90   This results in a much higher system load and an increased chance of xruns. 
91 </p>
92 <p>
93   For a few applications, low latency is critical:
94 </p>
95 <h3>Playing virtual instruments</h3>
96 <p>
97   A large delay between the pressing of the keys and the sound the instrument 
98   produces will throw-off the timing of most instrumentalists (save church 
99   organists, whom we believe to be awesome latency-compensation organic systems.)
100 </p>
101 <h3>Software audio monitoring</h3>
102 <p>
103   If a singer is hearing her own voice through two different paths, her head 
104   bones and headphones, even small latencies can be very disturbing and
105   manifest as a tinny, irritating sound.
106 </p>
107 <h3>Live effects</h3>
108 <p>
109   Low latency is important when using the computer as an effect rack for
110   inline effects such as compression or EQ. For reverbs, slightly higher
111   latency might be tolerable, if the direct sound is not routed through the
112   computer. 
113 </p>
114 <h3>Live mixing</h3> 
115 <p>
116   Some sound engineers use a computer for mixing live performances. 
117   Basically that is a combination of the above: monitoring on stage, 
118   effects processing and EQ. 
119 </p>
120 <p>
121   In many other cases, such as playback, recording, overdubbing, mixing, 
122   mastering, etc. latency is not important, since it can easily be 
123   compensated for.<br />
124   To explain that statement: During mixing or mastering you don&#039;t care 
125   if it takes 10ms or 100ms between the instant you press the play button
126   and sound coming from the speaker. The same is true when recording with a count in.
127 </p>
128
129 <h2>Latency compensation</h2>
130 <p>
131   During tracking it is important that the sound that is currently being 
132   played back is internally aligned with the sound that is being recorded.
133 </p>
134 <p>
135   This is where latency-compensation comes into play. There are two ways to 
136   compensate for latency in a DAW, <dfn>read-ahead</dfn> and
137   <dfn>write-behind</dfn>. The DAW starts playing a bit early (relative to 
138   the playhead), so that when the sound arrives at the speakers a short time 
139   later, it is exactly aligned with the material that is being recorded.
140   Since we know that play-back has latency, the incoming audio can be delayed 
141   by the same amount to line things up again.
142 </p>
143 <p>
144   As you may see, the second approach is prone to various implementation 
145   issues regarding timecode and transport synchronization. Ardour uses read-ahead 
146   to compensate for latency. The time displayed in the Ardour clock corresponds 
147   to the audio-signal that you hear on the speakers (and is not where Ardour 
148   reads files from disk).
149 </p>
150 <p>
151   As a side note, this is also one of the reasons why many projects start at 
152   timecode <samp>01:00:00:00</samp>. When compensating for output latency the 
153   DAW will need to read data from before the start of the session, so that the 
154   audio arrives in time at the output when the timecode hits <samp>01:00:00:00</samp>. 
155   Ardour3 does handle the case of <samp>00:00:00:00</samp> properly but not all 
156   systems/software/hardware that you may inter-operate with may behave the same.
157 </p>
158
159 <h2>Latency Compensation And Clock Sync</h2>
160
161 <p>
162   To achieve sample accurate timecode synchronization, the latency introduced 
163   by the audio setup needs to be known and compensated for.
164 </p>
165 <p>
166   In order to compensate for latency, JACK or JACK applications need to know 
167   exactly how long a certain signal needs to be read-ahead or delayed:
168 </p>
169 <img src="/images/jack-latency-excerpt.png"  title="Jack Latency Compensation" alt="Jack Latency Compensation" />
170 <p>
171   <em>Figure: Jack Latency Compensation.</em>  
172 </p>
173 <p>
174   In the figure above, clients A and B need to be able to answer the following 
175   two questions:
176 </p>
177 <ul>
178   <li>
179     How long has it been since the data read from port Ai or Bi arrived at the
180     edge of the JACK graph (capture)?
181   </li>
182   <li>
183     How long will it be until the data writen to port Ao or Bo arrives at the
184     edge of the JACK graph (playback)?
185   </li>
186 </ul>
187
188 <p>
189   JACK features an <abbr title="Application Programming Interface">API</abbr> 
190   that allows applications to determine the answers to above questions. 
191   However JACK can not know about the additional latency that is introduced 
192   by the computer architecture, operating system and soundcard. These values 
193   can be specified by the JACK command line parameters <kbd class="input">-I</kbd> 
194   and <kbd class="input">-O</kbd> and vary from system 
195   to system but are constant on each. On a general purpose computer system 
196   the only way to accurately learn about the total (additional) latency is to 
197   measure it.
198 </p>
199
200
201 <h2>Calibrating JACK Latency</h2>
202 <p>
203   Linux DSP guru Fons Adriaensen wrote a tool called <dfn>jack_delay</dfn> 
204   to accurately measure the roundtrip latency of a closed loop audio chain, 
205   with sub-sample accuracy. JACK itself includes a variant of this tool 
206   called <dfn>jack_iodelay</dfn>.
207 </p>
208 <p>
209   Jack_iodelay allows you to measure the total latency of the system, 
210   subtracts the known latency of JACK itself and suggests values for 
211   jackd's audio-backend parameters.
212 </p>
213 <p>
214   jack_[io]delay works by emitting some rather annoying tones, capturing 
215   them again after a round trip through the whole chain, and measuring the 
216   difference in phase so it can estimate with great accuracy the time taken. 
217 </p>
218 <p>
219   You can close the loop in a number of ways:
220 </p>
221 <ul>
222   <li>
223     Putting a speaker close to a microphone. This is rarely done, as air 
224     propagation latency is well known so there is no need to measure it.
225   </li>
226   <li>
227     Connecting the output of your audio interface to its input using a 
228     patch cable. This can be an analog or a digital loop, depending on 
229     the nature of the input/output you use. A digital loop will not factor 
230     in the <abbr title="Analog to Digital, Digital to Analog">AD/DA</abbr> 
231     converter latency.
232   </li>
233 </ul>
234 <p>
235   Once you have closed the loop you have to:
236 </p>
237 <ol>
238   <li>Launch jackd with the configuration you want to test.</li>
239   <li>Launch <kbd class="input">jack_delay</kbd> on the commandline.</li>
240   <li>Make the appropriate connections between your jack ports so the loop is closed.</li>
241   <li>Adjust the playback and capture levels in your mixer.</li>
242 </ol>
243
244