DSP System Toolbox
This example shows how to measure the latency introduced in audio signals when using the audio player and audio recorder 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.
Parameters in MATLAB/Simulink Affecting Latency
The following discusses the parameters that impact latency for
dsp.AudioRecorder System objects and the corresponding Simulink blocks,
To Audio Device and
From Audio Device.
Buffer Size and Frame Size
BufferSize is a property of both the
dsp.AudioRecorder System objects. It describes the size of the buffer that the sound card reads or writes as it communicates with MATLAB or Simulink.
Frame size is the number of samples passed to the audio player or taken from the audio recorder objects in each call to their step methods. Analogously, it would be the size of the array on input or output lines of the corresponding blocks in Simulink. Set the output frame size for
dsp.AudioRecorder using its SamplesPerFrame property. The frame size for
dsp.AudioPlayer corresponds to the number of rows in the input signal.
Larger frame 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 for to collect a full frame of samples, leading to increased latency. For low latency audio, keep BufferSize and frame size as low as possible without experiencing overruns (at the audio input) or underruns (at the audio output).
dsp.AudioRecorder System objects have the property, QueueDuration. The queue is a storage space between the System object and the sound card buffer. The main use of QueueDuration is to match the throughput of the algorithm in MATLAB and the device. A proper setting for QueueDuration can help prevent buffer underruns and overruns. QueueDuration should be set to a value as small as possible to maintain acceptable latency while not introducing glitches in the audio. The optimal value for the queue size for minimum latency is 0, but that can sometimes lead to overruns or underruns.
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, go to the DSP System Toolbox tab under MATLAB preferences, and select ASIO.
In this example, measure the latency by playing an audio signal through
dsp.AudioPlayer (or the
To Audio Device block), recording the audio through
dsp.AudioRecorder (or the
From Audio Device 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 the set-up involving the audio System objects or blocks: a sum of latencies due to the player and the recorder.
The audioLatencyMeasurementExampleAppaudioLatencyMeasurementExampleApp 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:
frameSize: Number of audio samples that constitute a single frame. This sets the SamplesPerFrame property of the
bufferSize: The value of this parameter is copied to the BufferSize property of the
dsp.AudioPlayer and the
dsp.AudioRecorder System objects.
plotFlag: Set this to true to visualize the input to the
dsp.AudioPlayer object and the output from the
useSimulink: Set this to true to use the Simulink model
audiolatencymeasurement.slx to measure the latency.
The first two inputs are parameters that affect 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 audioLatencyMeasurementExampleAppaudioLatencyMeasurementExampleApp function are:
Initialization: The default values for the 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
dsp.AudioPlayer. Through the loopback cable and audio-in port, the audio is read in using the
dsp.AudioRecorder object. When the useSimulink input is true, the loopback simulation is performed through the Simulink model
audiolatencybasicmeasurement.slx. The signal read from the file and the recorded signal are stored in a signal sink.
Cross-correlation: Using the
xcorr function, the cross-correlation sequence is computed between the audio signal sent to the player and the one received by the recorder. The received signal lags the played signal. The time shift corresponding to the maximum value of the 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 audioLatencyMeasurementExampleAppaudioLatencyMeasurementExampleApp 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 audioLatencyMeasurementExampleAppaudioLatencyMeasurementExampleApp. 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.