audio i/o: buffering, latency, and throughput
audio toolbox™ is optimized for real-time stream processing. its input and output system objects are efficient, low-latency, and they control all necessary parameters so that you can trade off between throughput and latency.
this tutorial describes how matlab® software implements real-time stream processing. the tutorial presents key terminology and basic techniques for optimizing your stream-processing algorithm. for more detailed technical descriptions and concepts, see the documentation for the audio i/o system objects used in this tutorial.
the concepts presented in this tutorial are described in terms of system objects in the matlab environment. the same concepts can be applied to corresponding blocks in the simulink® environment.
input audio stream
to acquire an audio stream from a file, use the system object™. to acquire an audio stream from a device, use the system object.
this diagram and the description that follows indicate the data flow when
acquiring a monochannel signal with the audiodevicereader
system object.
configuration
properties of your
audiodevicereader
specify the driver, device (sound card), sample rate, bit depth, buffer size, and channel mapping between your device's input channels and columns output from youraudiodevicereader
object. your object communicates these specifications to the driver once atsetup
.
real-time processing loop
the microphone picks up the sound and sends a continuous electrical signal to your sound card.
the sound card performs analog-to-digital conversion at a sample rate, buffer size, and bit depth specified during configuration.
the analog-to-digital converter writes audio samples into the sound card buffer. if the buffer is full, the new samples are dropped. these samples are referred to as overruns.
the
audiodevicereader
uses the driver to pull the oldest frame from the sound card buffer iteratively.
output audio stream
to send an audio stream to a file, use the system object. to send an audio stream to a device, use the system object.
this diagram and the description that follows indicate the data flow when playing
a monochannel signal with the audiodevicewriter
system object.
configuration
properties of your
audiodevicewriter
specify the driver, device (sound card), sample rate, bit depth, buffer size, and channel mapping between your device's output channels and columns input to youraudiodevicewriter
object. your object communicates these specifications to the driver once atsetup
.
real-time processing loop
the processing stage passes a frame of variable length to the
audiodevicewriter
system object.audiodevicewriter
sends the frame to the sound card’s buffer.the sound card pulls the oldest frame from the buffer and performs digital-to-analog conversion. the sound card sends the analog chunk to the speaker. if the buffer is empty when the sound card tries to pull from it, the sound card outputs a region of silence. this is referred to as underrun.
synchronize audio to and from device
to simultaneously read from and write to a single audio device, use the system object.
this diagram and the description that follows indicate the data flow when playing
and recording monochannel signals with the audioplayerrecorder
system object.
configuration
properties of your
audioplayerrecorder
specify the device (sound card), sample rate, bit depth, buffer size, and channel mapping between your device and object. your object communicates these specifications to the driver once atsetup
.
real-time processing loop
the microphone picks up the sound and sends a continuous electrical signal to your sound card. simultaneously, the speaker plays an analog chunk received from the sound card.
the sound card performs analog-to-digital conversion of the acquired audio signal and writes the digital chunk to the input buffer. if the input buffer is full, the new samples are dropped. simultaneously, the sound card pulls the oldest frame from the output buffer and performs digital-to-analog conversion of the next audio chunk to be played. if the output buffer is empty when the sound card tries to retrieve the data, the sound card outputs a region of silence.
the
audioplayerrecorder
object returns the acquired audio signal to the matlab environment for processing. simultaneously, the audio to be played is specified as an argument of theaudioplayerrecorder
for playback in the next i/o cycle.
terminology and techniques to optimize performance
signal drops
underrun refers to output signal silence. output signal silence occurs if the device buffer is empty when it is time for digital-to-analog conversion. this results when the processing loop in matlab does not supply samples at the rate the sound card demands. the number of samples underrun is returned when you call your
audioplayerrecorder
oraudiodevicewriter
object.overrun refers to input signal drops. input signal drops occur when the processing stage does not keep pace with the acquisition of samples. the number of samples overrun is returned when you call your
audioplayerrecorder
oraudiodevicereader
object.
if you encounter overrun or underrun, try improving your i/o system in one or more of the following ways:
identify when the overrun or underrun occurs. if it occurs in the first few iterations, consider calling on your system objects before the loop where real-time processing is required. you can also run the i/o system with dummy data for a few frames before starting the real processing. for more information, see .
if you are using a directsound driver on a windows® platform, consider switching to a wasapi or asio™ driver. asio drivers have the least overhead. if you are using an asio driver, make sure to match the frame size in matlab to the asio buffer size. you can use to open the asio preferences ui from matlab.
if you can afford to add more latency to your application, consider increasing the buffer size of your object. by default, the buffer size is the frame size of the data processed by the audio object.
if you can afford to decrease signal resolution, consider decreasing the sample rate.
close all nonessential processes on your machine, such as mail checkers and file sync utilities. these processes can asynchronously ask for cpu time through interrupts and disturb the audio-processing loop.
to maximize performance, remove all plotting and visualization from your real-time loop. if you require a visualization to update in your processing loop, use a dsp system toolbox™ scope such as , , or . follow the recommendations listed in point 1 to setup and pre-run your scopes. if you require custom graphics or are processing callbacks in the loop, use the command and specify a limited update rate to optimize your event queue.
if the processing loop is algorithm heavy, try profiling your loop to locate the bottlenecks, and then apply appropriate measures:
replace handwritten code with matlab features that have been optimized for speed.
follow best practices for performance: techniques to improve performance.
generating matlab executables (mex files) using matlab coder™ may result in faster execution. see for an example.
you can also generate standalone executables (exe files). see for an example.
if you are considering turning your algorithm into a vst plugin, then try running it as a vst plugin within matlab. vst plugin generation uses c code generation technology under the hood, and running the generated vst plugin within matlab may result in faster execution than with your original matlab code. see and host external audio plugins to learn how to design, generate, and then host a vst plugin.
latency
output latency is measured as the time delay between the time of generation of an audio frame in matlab and the time that audio is heard through the speaker.
input latency is measured as the time delay between the time that audio enters the sound card and the time that the frame is output by the processing stage.
if properties and frame size remain consistent, the ratio of input
latency to output latency is consistent
between calls to an audioplayerrecorder
object.
to minimize latency, you can:
optimize the processing stage. if your processing stage has reached a peak algorithmically, compiling your matlab code into c code using matlab coder may result in faster execution.
increase the sample rate.
decrease the frame size.
for a tutorial on measuring the round-trip latency of your system, see measure audio latency.
see also
| | | | | | | | | |