This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Real-Time Audio in MATLAB

Audio System Toolbox™ is optimized for real-time audio processing. audioDeviceReader, audioDeviceWriter, audioPlayerRecorder, dsp.AudioFileReader, and dsp.AudioFileWriter 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:

  1. Build objects to input and output audio from your test bench.

  2. Create an audio stream loop that processes your audio frame-by-frame.

  3. Add a scope to visualize both the input and output of your audio stream loop.

  4. 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.

 Completed Test Bench Code

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.

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 your input System object.

For more information on how to use System objects, see What Are System Objects? (MATLAB)

 View Example Code

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.

 View Example Code

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 to the Audio System Toolbox user. Two common scopes are the dsp.TimeScope and the dsp.SpectrumAnalyzer. This tutorial uses the dsp.TimeScope System object to visualize the audio signal.

The dsp.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 dsp.TimeScope System object within your audio stream loop with an audio signal as an argument.

 View Example Code

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 System object to process the signal within your audio stream loop.

Create a reverberator System object, and specify the SampleRate property as the sample rate of your input System object. 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 System object within your audio stream loop with an audio signal as an argument.

 View Example Code

Add Tunability

The Audio System 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 audioPlugin classes and most Audio System Toolbox System objects.

  • Built-in functions – Functions in Audio System 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 System Toolbox System objects include functions that support MIDI controls. You can use the configureMIDI 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 a configureMIDI function, 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.

Quick Start Examples

 Audio Stream from Device to Device

 Audio Stream from Device to File

 Audio Stream from File to Device

Related Topics