Real-Time Audio in MATLAB
Audio Toolbox™ is optimized for real-time audio processing.
dsp.AudioFileWriter are designed for streaming multichannel audio, and they
provide necessary parameters so that you can trade off between throughput and
For information on real-time processing and tips on how to optimize your algorithm, see Audio I/O: Buffering, Latency, and Throughput.
This tutorial describes how you can implement audio stream processing in MATLAB®. It outlines the workflow for creating a development test bench and provides examples for each stage of the workflow.
Create a Development Test Bench
This tutorial creates a development test bench in four steps:
Build objects to input and output audio from your test bench.
Create an audio stream loop that processes your audio frame-by-frame.
Add a scope to visualize both the input and output of your audio stream loop.
Add a processing algorithm for your audio stream loop.
This tutorial also discusses tools for visualizing and tuning your processing algorithm in real time.
For an overview of the processing loop, consider the completed test bench below. You can recreate this test bench by walking step-by-step through this tutorial.
1. Create Input/Output System objects
Your audio stream loop can read from a device or a file, and it can write to a device or a file. In this example, you build an audio stream loop that reads audio frame-by-frame from a file and writes audio frame-by-frame to a device. See Quick Start Examples for alternative input/output configurations.
System object™ and specify a file. To reduce latency, set the
SamplesPerFrame property of the
System object to a small frame size.
Next, create an
System object and specify its sample rate as the sample rate of the file
For more information on how to use System objects, see What Are System Objects?
2. Create Audio Stream Loop
An audio stream loop processes audio iteratively. It does so by:
Reading a frame of an audio signal
Processing that frame of audio signal
Writing that frame of audio signal to a device or file
Moving to the next frame
In this tutorial, the input to the audio stream loop is read from a file. The output is written to a device.
To read an audio file frame-by-frame, call your
dsp.AudioFileReader within your audio stream loop, and provide no
arguments. To write an audio signal frame-by-frame, call your
audioDeviceWriter within your audio stream loop with an
audio signal as an argument.
All System objects have a
release function. As a best practice,
release your System objects after use, especially if those System objects are communicating with hardware devices such as sound
3. Add Scopes
System object displays an audio signal in the time domain. Create the
System object. To aid visualization, specify values
YLimits properties. To visualize an audio signal
frame-by-frame, call the
System object within your audio stream loop with an audio
signal as an argument.
4. Develop Processing Algorithm
In most applications, you want to process your audio signal within your audio stream loop. The processing stage can be:
A block of MATLAB code within your audio stream loop
A separate function called within your audio stream loop
A System object called within your audio stream loop
In this tutorial, you call the
reverberator to process the signal
within your audio stream loop.
System object, and specify the
SampleRate property as the
sample rate of your file reader. To adjust the reverberation effect, specify
values for the
properties. To apply the reverberation effect to an audio signal frame-by-frame,
reverberator within your audio stream loop with an
audio signal as an argument.
The Audio Toolbox user has several options to add real-time tunability to a processing algorithm. To add tunability to your audio stream loop, you can use:
The Audio Test Bench – UI-based exercises for
audioPluginclasses and most Audio Toolbox System objects.
Built-in functions – Functions in Audio Toolbox for visualizing key aspects of your processing algorithms.
A custom-built user interface – See Real-Time Parameter Tuning for a tutorial.
A MIDI Controller – Many Audio Toolbox System objects include functions that support MIDI controls. You can use the
function in the
reverberatorSystem object to synchronize your System object properties to MIDI controls. To use MIDI controls with System objects that do not have a
configureMIDIfunction, see MIDI Control Surface Interface.
The User Datagram Protocol (UDP) – You can use UDP within MATLAB for connectionless transmission. You can also use UDP to receive or transmit datagrams between environments. Possible applications include using MATLAB tools to tune your audio processing algorithm while playing and visualizing your audio in a third-party environment. For an example application of UDP communication, see Communicate Between a DAW and MATLAB Using UDP.