real-time audio in matlab
audio toolbox™ is optimized for real-time audio processing. , , , , and are designed for streaming multichannel audio, and they provide necessary parameters so that you can trade off between throughput and latency.
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 example for an alternative input/output configuration.
create a dsp.audiofilereader
system object™ and specify a file. to reduce latency, set the
samplesperframe
property of the dsp.audiofilereader
system object to a small frame size.
next, create an audiodevicewriter
system object and specify its sample rate as the sample rate of the file
reader.
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
cards.
3. add scopes
there are several scopes available. two common scopes are the and the . this tutorial uses to visualize the audio signal.
the timescope
system object displays an audio signal in the time domain. create the
system object. to aid visualization, specify values
for the timespan
, bufferlength
, and
ylimits
properties. to visualize an audio signal
frame-by-frame, call the timescope
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 to process the signal within your audio stream loop.
create a
system object, and specify the samplerate
property as the
sample rate of your file reader. to adjust the reverberation effect, specify
values for the predelay
and wetdrymix
properties. to apply the reverberation effect to an audio signal frame-by-frame,
call the reverberator
within your audio stream loop with an
audio signal as an argument.
add tunability
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 – ui-based exercises for
audioplugin
classes 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
reverberator
system object to synchronize your system object properties to midi controls. to use midi controls with system objects that do not have aconfiguremidi
function, see .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 .
quick start example
audio stream from device to file
this example shows how to acquire an audio signal using your microphone using audiodevicereader
, perform basic signal processing, and write your signal to a file using dsp.audiofilewriter
.
construct input and output objects. use the sample rate of your input as the sample rate of your output.
devicereader = audiodevicereader; filewriter = dsp.audiofilewriter(samplerate=devicereader.samplerate);
specify an audio processing algorithm. for simplicity, only add gain.
process = @(x) x.*5;
place the following steps in a while loop for continuous stream processing:
call your audio device reader like a function with no arguments to acquire one input frame.
perform your signal processing operation on the input frame.
call your audio file writer like a function with the processed frame as an argument.
the file is named output.wav
and written to current folder by default.
disp("begin signal input...")
begin signal input...
tic while toc<5 mysignal = devicereader(); myprocessedsignal = process(mysignal); filewriter(myprocessedsignal) end disp("end signal input")
end signal input
release(devicereader) release(filewriter)