This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Measure Audio Latency

This example shows how to measure the latency introduced when using the audio device reader and audio device writer in MATLAB or Simulink. The procedure presented here allows you to tune parameters that affect latency and observe the effect in a simple measurement.


In general terms, latency is defined as the time from when the audio signal enters a system until it exits. There are multiple parameters that influence latency. For example:

  1. Hardware: CPU, memory, and sound card.

  2. Audio drivers that communicate with the system's sound card.

  3. Algorithmic complexity of any audio processing between playback and recording.

  4. Software used to process the audio: MATLAB or Simulink in this example.

This example shows you how to measure overall rountrip latency, with attention to the parameters determine the latency in MATLAB or Simulink.

Parameters in MATLAB/Simulink Affecting Latency

The following discusses the parameters that impact latency for the audioDeviceReader and audioDeviceWriter System objects in MATLAB, and the Audio Device Reader and Audio Device Writer blocks in Simulink.

Buffer Size and Frame Size

The sound card maintains a buffer to read or write samples as it communicates with MATLAB or Simulink. For audioDeviceReader, the size of the buffer is determined by its SamplesPerFrame property. For audioDeviceWriter, the size of the buffer is the same as the frame size (number of rows) of the input signal passed to it in each call to its play or step method.

In Simulink, for the Audio Device Reader block, the size of the buffer is determined by the value of the Samples per frame parameter on its dialog. For the Audio Device Writer block, the size of the buffer is the same as the number of rows of the input array to the block.

Larger buffer sizes can lead to enhanced performance because they reduce system overhead by processing large chunks of data at each step. However, you must also wait longer to collect a full buffer of samples, leading to increased latency. For low latency audio, keep the buffer size as low as possible without experiencing overruns (at the audio input) or underruns (at the audio output).

Sound Card and Drivers

Latency characteristics vary for different sound cards and vendors. For low latency on Windows, ASIO™ drivers can be used. To use ASIO drivers in MATLAB, select ASIO as the Driver property value of audioDeviceReader and audioDeviceWriter objects. To use ASIO drivers in Simulink, select ASIO as the Driver parameter value in the dialog of Audio Device Reader and Audio Device Writer blocks. The CoreAudio driver supported by these objects on Mac also has good latency performance.

Set-up Experiment

In this example, you measure latency by playing an audio signal through audioDeviceWriter (or the Audio Device Writer block), recording the audio through audioDeviceReader (or the Audio Device Reader block), and measuring the delay by the cross-correlation of the two signals. No other processing is done on the audio signal. A loopback cable is used to physically connect the audio-out port of the sound card to its audio-in port. The set-up is shown in the following illustration.

The set-up for another common application is to record live audio with a microphone, process the audio on the computer, and play the audio back in a streaming fashion. In both set-ups, the hardware interface latency is the same - a sum of latencies due to the player and the recorder.

Measure Latency

The audioLatencyMeasurementExampleApp function measures the latency for the set-up where an audio signal is played and recorded with System objects or Simulink blocks. Inputs to this function are:

  • bufferSize: Size of buffer used by sound card, in samples. This is the number of audio samples that constitute a single audio frame. The buffer size sets the SamplesPerFrame property of dsp.AudioFileReader and audioDeviceReader objects.

  • plotFlag: Set this to true to visualize the input to the audioDeviceWriter object and the output from the audioDeviceReader object.

  • useSimulink: Set this to true to use the Simulink model audiolatencymeasurement.slx to measure the latency.

The bufferSize affects audio latency in MATLAB or Simulink. You can try different values and measure the latency for each set. The various sections in the MATLAB code for the audioLatencyMeasurementExampleApp function are:

  1. Initialization: The default values for input parameters are assigned. The audio System objects are created with appropriate parameter values.

  2. Loopback simulation: For a duration of 10 seconds, audio is read from a file and sent to the computer's audio-out port through audioDeviceWriter. The audio signal travels through the loopback cable to the audio-in port, where it is read in using the audioDeviceReader object. When useSimulink is true, loopback simulation is performed through the Simulink model audiolatencybasicmeasurement.slx. The signal read from the file and the recorded signal are stored for cross-correlation.

  3. Cross-correlation: Using the xcorr function, cross-correlation is computed between the audio signal sent to device output and the one received by device input. The received signal lags the played signal. The time shift corresponding to the maximum cross-correlation is the latency. Additionally, if the plotFlag input is true, the two audio signals are plotted in a figure so you can observe the lag.

If the queue is overrun or underrun, a message stating the number of dropped samples is displayed in the command window.

Sample Results

The table summarizes results obtained by running audioLatencyMeasurementExampleApp on a Windows® 7 machine with Intel® Xeon® 2.4 GHz processor, 32 GB of RAM, and using Steinberg UR22 Audio Interface. For these measurements, frame size was kept equal to the buffer size and both were incrementally decreased until the audio queue started to underrun or overrun.

The latency values in the table are the average of 10 runs. Jitter is calculated as the standard deviation of latency values across those 10 runs.

Was this topic helpful?