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. Please click here
To view all translated materials including this page, select Japan 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 all 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. Begin by inspecting the anatomy of a completed audio stream processing test bench, then walk through the example for a description of each stage.

Create a Development Test Bench

This tutorial creates a development test bench in five steps. You begin by constructing objects to input an audio signal to your test bench and output an audio signal from your test bench. You then create an audio stream loop that performs frame-based processing on your audio signal. To gain insight about your audio processing, you add scopes to visualize the input to and output from the audio stream loop. You then develop your audio processing algorithm. In the final stage, you make your processing algorithm tunable in real time.

For an overview of how audio stream processing is implemented, inspect the anatomy of the completed audio stream processing test bench. To create this test bench, walk through the example for explanations and step-by-step instructions.

 Completed Test Bench Code

1. Construct Input/Output System Objects

Your audio stream loop can read audio directly from your device or from a file, and can write to a device or file. In this tutorial, you create an audio stream loop that reads audio frame by frame from a file, and outputs frame by frame to a device. See Quick Start Examples for alternative input/output configurations.

Construct a dsp.AudioFileReader System object™ and specify a file. To reduce latency, specify a small frame size as a property of the dsp.AudioFileReader System object.

Also construct an audioDeviceWriter System object. Specify your audioDeviceWriter sample rate if the default of 44,100 Hz is not appropriate. If you do not modify the sample rate between input and output to your audio stream loop, use the sample rate of your input System object.

 View Example Code

2. Create Audio Stream Loop

An audio stream loop refers to a programming loop that iteratively:

  • Reads a frame of an audio signal

  • Processes the audio signal frame

  • Writes the audio signal frame

In this tutorial, the input to the audio stream loop is read from a file. The output from the audio stream loop writes to a device.

Create Audio Stream Loop with File Input and Device Output.  To read a single frame of an audio file, call your dsp.AudioFileReader like a function without arguments. To read successive frames, call your dsp.AudioFileReader in the audio stream loop.

To write a single frame of an audio signal to your audio device, call your audioDeviceWriter like a function with the signal frame to output as the argument. To write successive frames, call your audioDeviceWriter in the audio stream loop.

 View Example Code

All System objects have a release method. As a best practice, release your System object after use, especially when a System object is communicating with a hardware device such as your sound card.

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.

Add Time Scope.  To display an audio signal in the time domain, construct a dsp.TimeScope System object. To aid visualization, specify necessary dsp.TimeScope properties, such as TimeSpan, BufferLength, and YLimits.

To display the current frame of a signal, call your dsp.TimeScope like a function with the signal frame to display as the argument. To display your signal in real time, call your dsp.TimeScope in the audio stream loop.

 View Example Code

4. Develop Processing Algorithm

In most applications, you want to process your audio signal in the audio stream loop. The processing stage can be

  • An inline script in the audio stream loop

  • A separate function called in the audio stream loop

  • A System object called like a function in an audio stream loop

In this tutorial, you call the noiseGate System object like a function to process the signal in the audio stream loop.

Process Signal with noiseGate.  Construct a noiseGate System object. Specify your noiseGate System object sample rate if the default of 44,100 Hz is not appropriate. As a best practice, use the sample rate of your input System object. To achieve the aims of your audio processing, specify necessary noiseGate properties, such as Threshold, AttackTime, ReleaseTime, and HoldTime.

To process the audio signal, call your noiseGate like a function in the audio stream loop.

In this tutorial, you add random Gaussian noise to the audio stream input to show a possible use case of the noiseGate System object.

 View Example Code

5. Add Tunability

MATLAB provides several options to interactively tune your algorithm with stream processing.

Add User Interface.  MATLAB provides several user interfaces (UI) to inspect and interact with your code. You can use:

  • The Audio Test Bench, which provides UI-based exercises for audioPlugin classes and most Audio System Toolbox System objects.

  • The built-in methods of Audio System Toolbox System objects for visualizing key characteristics of your processing algorithms. Then you can tune them in real time with MIDI controls.

  • A custom-built user interface. See GUI Building (MATLAB) for a tutorial.

This tutorial uses the visualize method of the noiseGate System object to observe its static characteristics.

 View Example Code

Add MIDI Controller.  Many Audio System Toolbox System objects include methods that support MIDI controls. This tutorial uses the configureMIDI method of the noiseGate System object to synchronize your System object properties to MIDI controls.


To use MIDI controls with System objects that do not have a configureMIDI method, see Musical Instrument Digital Interface (MIDI).

To control your noiseGate System object properties with a MIDI controller, connect the MIDI device to your computer.

The configureMIDI method enables you to synchronize properties to MIDI controls using a user interface or a script. This example synchronizes properties to a MIDI controller using a user interface.

Before calling your audio stream loop, call the configureMIDI method on your noiseGate System object. When you run your script, it does not advance until you have completed your configuration and closed the user interface. Once the user interface opens:

  1. Select a property to synchronize by choosing it from the drop-down menu.

  2. Move a MIDI control.

The noiseGate property in the drop-down menu and the MIDI control you moved are now synced. Repeat these steps for all properties you want to synchronize. Then click OK.

While your audio is stream processing, use your MIDI controller to adjust the noiseGate parameters in real time. In particular, toggle the MIDI control mapped to the Threshold property to attenuate the additive Gaussian noise in the signal.

 View Example Code

Add UDP.  You can use the User Datagram Protocol (UDP) within MATLAB for connectionless transmission, or to receive or transmit datagrams outside MATLAB. Possible applications include using MATLAB to tune your audio processing algorithm while playing and visualizing your audio in a third-party environment. See Communicate Between a DAW and MATLAB Using UDP for an example application of UDP communication.

Quick Start Examples

 Audio Stream from Device to Device

 Audio Stream from Device to File

 Audio Stream from File to Device

Related Topics

Was this topic helpful?