Main Content

Real-Time Audio inMATLAB

Audio Toolbox™针对实时音频处理进行了优化。audiodevicereader.,audioDeviceWriter,audioPlayerRecorder,dsp.AudioFileReader, anddsp.audiofilewriter.are designed for streaming multichannel audio, and they provide necessary parameters so that you can trade off between throughput and latency.

有关如何优化算法的实时处理和提示的信息,请参阅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.

创建开发测试台

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. 添加范围以可视化音频流循环的输入和输出。

  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/OutputSystem 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. SeeQuick Start Examples用于替代输入/输出配置。

Create adsp.AudioFileReaderSystem object™ and specify a file. To reduce latency, set theSamplesPerframe.财产的财产dsp.AudioFileReaderSystem object to a small frame size.

Next, create anaudioDeviceWriterSystem object and specify its sample rate as the sample rate of the file reader.

For more information on how to use System objects, seeWhat Are System Objects?

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

  • 处理音频信号帧

  • 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 yourdsp.AudioFileReaderwithin your audio stream loop, and provide no arguments. To write an audio signal frame-by-frame, call youraudioDeviceWriterwithin your audio stream loop with an audio signal as an argument.

View Example Code

All System objects have arelease功能。作为最佳实践,使用后释放您的系统对象,特别是如果这些系统对象与诸如声卡等硬件设备通信。

3. Add Scopes

There are several scopes available. Two common scopes are thetimescope和thedsp.SpectrumAnalyzer. This tutorial usestimescopeto visualize the audio signal.

ThetimescopeSystem object displays an audio signal in the time domain. Create the System object. To aid visualization, specify values for the时间跨度,BufferLength., andYLimitsproperties. To visualize an audio signal frame-by-frame, call thetimescopeSystem 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 thereverberatorto process the signal within your audio stream loop.

Create areverberatorSystem object, and specify theSampleRateproperty as the sample rate of your file reader. To adjust the reverberation effect, specify values for the预制WetDryMixproperties. To apply the reverberation effect to an audio signal frame-by-frame, call thereverberatorwithin your audio stream loop with an audio signal as an argument.

View Example Code

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:

  • TheAudio Test Bench– UI-based exercises foraudioPlugin类和大多数音频工具箱系统对象。

  • Built-in functions – Functions in Audio Toolbox for visualizing key aspects of your processing algorithms.

  • A custom-built user interface – SeeReal-Time Parameter Tuningfor a tutorial.

  • A MIDI Controller – Many Audio Toolbox System objects include functions that support MIDI controls. You can use theconfigureMIDIfunction in thereverberatorSystem object to synchronize your System object properties to MIDI controls. To use MIDI controls with System objects that do not have aconfigureMIDIfunction, seeMIDI Control Surface Interface.

  • 用户数据报协议(UDP) - 您可以在MATLAB中使用UDP进行无连接传输。您还可以使用UDP在环境之间接收或传输数据报。可能的应用程序包括使用MATLAB工具在第三方环境中播放和可视化音频时调整音频处理算法。有关UDP通信的示例应用,请参阅Communicate Between a DAW and MATLAB Using UDP.

Quick Start Examples

Audio Stream from Device to Device

Audio Stream from Device to File

从文件到设备的音频流

相关话题