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