This example shows how to measure the latency introduced in audio signals when using the audio device reader and audio device writer in MATLAB or Simulink. The procedure presented here allows you to tune certain parameters that affect latency and observe the effect in a simple measurement. The specific parameters are discussed in detail later in the example.
In general terms, latency is defined as the time from when the audio signal enters a system until it exits. There are multiple factors that influence latency. For example:
Hardware: CPU, memory, and sound card.
Audio drivers that communicate with the system's sound card.
Algorithmic complexity of any audio processing between playback and recording.
Software used to process the audio: MATLAB or Simulink in this example.
This example shows you how to measure the overall rountrip latency with a focus on what parameters determine the latency effect of using MATLAB or Simulink. There are two variants of this example: one uses the audio System objects in MATLAB and the other uses the Simulink audio input and output blocks.
The following discusses the parameters that impact latency for
audioDeviceWriter System objects. Same discussion also holds for the corresponding Simulink blocks,
Audio Device Reader and
Audio Device Writer.
Buffer Size and Frame Size
The sound card maintains a buffer that is used to read or write samples as it communicates with MATLAB or Simulink. For
audioDeviceReader, the size of this buffer is determined by its
SamplesPerFrame property. For
audioDeviceWriter, the buffer size is same as the frame size (number of rows) of the input signal passed to it in each call to its
Analogously in Simulink, the size of the buffer for the
Audio Device Reader block is determined by the value of the
Samples per frame parameter on its dialog. The number of rows of the array on the input line of the
Audio Device Writer block determines the buffer size it will use.
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 lower latency on Windows, ASIO™ drivers can be used. To use ASIO drivers, select
ASIO as the
Driver property value of
audioDeviceWriter objects. The
CoreAudio driver supported by these objects on Mac also has good latency performance.
In this example, measure the 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 that set-up, the hardware interface latency is the same as this set-up: a sum of latencies due to the player and the recorder.
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 - SamplesPerFrame property of
plotFlag: Set this to true to visualize the input to the
audioDeviceWriter object and the output from the
useSimulink: Set this to true to use the Simulink model
audiolatencymeasurement.slx to measure the latency.
The first input 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:
Initialization: The default values for input parameters are assigned. The audio System objects are created with appropriate parameter values.
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. Through the loopback cable and audio-in port, the audio is read in using the
audioDeviceReader object. When useSimulink input is true, loopback simulation is performed through the Simulink model
audiolatencybasicmeasurement.slx. Signal read from the file and the recorded signal are stored for cross-correlation.
Cross-correlation: Using the
xcorr function, cross-correlation sequence 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 value of cross-correlation sequence 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 corresponding message is displayed in the command window.
Here are some results obtained by running audioLatencyMeasurementExampleApp on a Windows® 7 machine with Intel® Xeon® 2.4 GHz processor and 32 GB of RAM. Measurements were made using Steinberg UR22 Audio Interface. The audio is stereo sampled at a range of different commonly used sample rates, from 44.1 KHz to 192 KHz. 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 results in latency column are average of 10 runs of latency value computed using the audioLatencyMeasurementExampleApp. Jitter is calculated as the standard deviation of latency values across those 10 runs.
You can also zoom into the plot to see the delay between the player and recorder waveforms.
This example discussed the concept of latency in audio signals and demonstrated a way in MATLAB and Simulink to quantify latency. The example also described various parameters that influence audio latency. Some sample results obtained on a specific computer were presented. Note that results vary based on machine configuration. You can follow the advice in this example and experiment with different values to find the combination that best suits your system and application.