Latency is the time delay between when one initiates something and when it happens. There are several places in the SC architecture where latencies occur:
The Latency class is intended for measuring audio hardware latency, and overall latency, which includes sound travel through air. Latency does its measuring by generating pulses on one or several output channels, and measuring time until each pulse registers again at a given audio input. [ interpreter latency is covered in the examples section below ]
This is the time between entering a command and the time it is executed in the client (sclang). This depends on how much code runs "at once" and how complex the calculations are. For many cases, it is near zero.
sclang and scserver communicate via network (OpenSoundControl, usually UDP, or also TCP/IP); until a network packet sent from sclang to the server arrives, an undetermined time passes. On the same machine, with little other network traffic, this can be as low as 0.02 seconds, depending mainly on the server's hardware buffer size. This can be measured with:
To ensure that messages arrive at the server in the correct order, and with exact relative timing, the server has a latency value s.latency
which can be tuned. By default, this is rather high with 0.2 secs. For tigher interaction, e.g. when playing an instrument-like interface, one can tune this value down until occasional late messages appear, and then go up until they disappear again.
scsynth passes buffers (one block of numbers per channel) to the audio hardware; the size of these buffers determines the time it takes between when a sound is calculated internally and when it actually is heard. The same goes for audio inputs, which are passed to SC as blocks, so there is some latency between when e.g. a trigger happens at the analog audio input, and when it is detected with e.g. a Trig Ugen.
While hardware buffer size and other factors can be set directly with utility software for audiocards, such software often does not consider all the factors: E.g. the operating system itself, firewire or USB connections, and intermediate layers like Jack may introduce extra buffering and thus accumulate delay time. Thus, connecting the first audio output to the first audio input electrically is the most accurate measuring methods, as it includes all such delays.
For multi-speaker setups, distances to any listening position introduce delays, so measuring overall audio latency can be useful. This is done by setting up all loudspeakers as desired, putting a single mic in the "sweet spot", or another typical listening position, and recording responses over all acoustic channels.
main method to measure audio latency
numChans |
the number of output channels to send pulses on |
maxDT |
maximum expected delay time, determines pulse cycle time |
server |
the server to play on, default is Server.default |
inChan |
the input channel on which incoming pulses are measured |
get and set a flag whether to post verbosely
get and set amplitude threshold for pulses on input channel
stop measurement
the latest measured latencies for all channels
post the latest measured latencies
the most recent measurement: [outputIndex, latencyInSamples ]
the server to use
the synth that runs the latency test
the OSCFunc that listens for pulse triggers