measure audio latency -凯发k8网页登录
this example shows how to measure the latency of an audio device. the example uses audiolatencymeasurementexampleapp which in turn uses audioplayerrecorder along with a test signal and cross correlation to determine latency. to avoid disk access interference, the test signal is loaded into a dsp.asyncbuffer object first, and frames are streamed from that object through the audio device.
introduction
in general terms, latency is defined as the time from when the audio signal enters a system until it exits. in a digital audio processing chain, there are multiple parameters that cause latency:
hardware (including a/d and d/a conversion)
audio drivers that communicate with the system's sound card
sampling rate
samples per frame (buffer size)
algorithmic latency (e.g. delay introduced by a filter or audio effect)
this example shows how to measure round trip latency. that is, the latency incurred when playing audio through a device, looping back the audio with a physical loopback cable, and recording the loopback audio with the same audio device. in order to compute latency for your own audio device, you need to connect the audio out and audio in ports using a loopback cable.
roundtrip latency does not break down the measurement between output latency and input latency. it measures only the combined effect of the two. also, most practical applications will not use a loopback setup. typically the processing chain consists of recording audio, processing it, and playing the processed audio. however, the latency involved should be the same either way provided the other factors (frame size, sampling rate, algorithm latency) don't change.
hardware latency
smaller frame sizes and higher sampling rates reduce the roundtrip latency. however, the tradeoff is a higher chance of dropouts occurring (overruns/underruns).
in addition to potentially increasing latency, the amount of processing involved in the audio algorithm can also cause dropouts.
measuring latency with audiolatencymeasurementexampleapp.m
the function audiolatencymeasurementexampleapp computes roundtrip latency in milliseconds for a given setup. overruns and underruns are also presented. if the overruns/underruns are not zero, the results are likely invalid. for example:
audiolatencymeasurementexampleapp('samplesperframe',64,'samplerate',48e3) % the measurements in this example were done on macos. for most % measurements, a steinberg ur22 external usb device was used. for the % measurements with custom i/o channels, an rme fireface ufx device was % used. this rme device has lower latency than the steinberg device for a % given sample rate/frame size combination. measurements on windows using % asio drivers should result in similar values.
trial(s) done for framesize 64. ans = 1×5 table samplesperframe samplerate_khz latency_ms overruns underruns _______________ ______________ __________ ________ _________ 64 48 8.3125 0 0
some tips when measuring latency
real-time processing on a general purpose operating system is only possible if you minimize other tasks being performed by the computer. it is recommended to:
close all other programs
ensure no underruns/overruns occur
use a large enough buffer size (samplesperframe) to ensure consistent dropout-free behavior
ensure your hardware settings (buffer size, sampling rate) match the inputs to measurelatency
on windows, you can use the asiosettings function to launch the dialog to control the hardware settings. on macos, you should launch the audio midi setup.
when using asio (or coreaudio with mac os), the latency measurements are consistent as long as no dropouts occur. for small buffer sizes, it is possible to get a clean measurement in one instance and dropouts the next. the ntrials option can be used to ensure consistent dropout behavior when measuring latency. for example, to perform 3 measurements, use:
audiolatencymeasurementexampleapp('samplesperframe',96,... 'samplerate',48e3,'ntrials',3)
trial(s) done for framesize 96. ans = 3×5 table samplesperframe samplerate_khz latency_ms overruns underruns _______________ ______________ __________ ________ _________ 96 48 10.312 0 0 96 48 10.312 0 0 96 48 10.312 0 0
measurements for different buffer sizes
on macos, it is also possible to try different frame sizes without changing the hardware settings. to make this convenient, you can specify a vector of samplesperframe:
buffersizes = [64;96;128]; t = audiolatencymeasurementexampleapp('samplesperframe',buffersizes) % notice that for every sample increment in the buffer size, the additional % latency is 3*samplesperframeincrement/samplerate (macos only).
trial(s) done for framesize 64. trial(s) done for framesize 96. trial(s) done for framesize 128. t = 3×5 table samplesperframe samplerate_khz latency_ms overruns underruns _______________ ______________ __________ ________ _________ 64 48 8.3125 0 0 96 48 10.312 0 0 128 48 12.312 0 0
specifically, in the previous example, the increment is
3*[128-96, 96-64]/48e3 % in addition, notice that the actual buffering latency is also determined % by 3*samplesperframe/samplerate. subtracting this value from the measured % latency gives a measure of the latency introduced by the device (combined % effect of a/d conversion, d/a conversion, and drivers). the numbers above % indicate about 4.3125 ms latency due to device-specific factors. t.latency_ms - 3*buffersizes/48
ans = 0.0020 0.0020 ans = 4.3125 4.3125 4.3125
specifying custom input/output channels
the measurements performed so far assume that channel #1 is used for both input and output. if your device has a loopback cable connected to other channels, you can specify them using the iochannels option to measurelatency. this is specified as a 2-element vector, corresponding to the input and output channels to be used (the measurement is always on a mono signal). for example for an rme fireface ufx :
audiolatencymeasurementexampleapp('samplesperframe',[32 64 96],... 'samplerate',96e3,'device','fireface ufx (23767940)',... 'iochannels',[1 3])
trial(s) done for framesize 32. trial(s) done for framesize 64. trial(s) done for framesize 96. ans = 3×5 table samplesperframe samplerate_khz latency_ms overruns underruns _______________ ______________ __________ _________ __________ 32 96 2.6458 0 32 64 96 3.6458 0 0 96 96 4.6458 0 0
algorithmic latency
the measurements so far have not included algorithm latency. therefore, they represent the minimal roundtrip latency that can be achieved for a given device, buffer size, and sampling rate. you can add a linear phase fir filter the processing chain to verify that the latency measurements are as expected. moreover, it provides a way of verifying robustness of the real-time audio processing under a given workload. for example,
l = 961; fs = 48e3; audiolatencymeasurementexampleapp('samplesperframe',128,... 'samplerate',fs,'filterlength',l,'ntrials',3) % the latency introduced by the filter is given by the filter's % group-delay. groupdelay = (l-1)/2/fs % the group delay accounts for the 10 ms of additional latency when using a % 961-tap linear-phase fir filter vs. the minimal achievable latency.
trial(s) done for framesize 128. ans = 3×6 table samplesperframe samplerate_khz filterlength latency_ms overruns underruns _______________ ______________ ____________ __________ ________ _________ 128 48 961 22.312 0 0 128 48 961 22.312 0 0 128 48 961 22.312 0 0 groupdelay = 0.0100
plotting the original and recorded signal
%the latency measurements are determined by cross-correlating a source %audio signal with a delayed version of the signal that results after %loopback through the audio device. you can use the plot option in %measurelatency to plot the original and delayed signal along with the %cross correlation: audiolatencymeasurementexampleapp('samplesperframe',128,'plot',true) % if the optional fir filtering is used, the waveforms are not affected % because the filter used has a broader bandwidth than the test audio % signal.
trial(s) done for framesize 128. plotting... ans = 1×5 table samplesperframe samplerate_khz latency_ms overruns underruns _______________ ______________ __________ ________ _________ 128 48 12.312 0 0