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:
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 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/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.AudioFileReader
System object™ and specify a file. To reduce latency, set theSamplesPerframe.
财产的财产dsp.AudioFileReader
System object to a small frame size.
Next, create anaudioDeviceWriter
System 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?
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.AudioFileReader
within your audio stream loop, and provide no arguments. To write an audio signal frame-by-frame, call youraudioDeviceWriter
within your audio stream loop with an audio signal as an argument.
All System objects have arelease
功能。作为最佳实践,使用后释放您的系统对象,特别是如果这些系统对象与诸如声卡等硬件设备通信。
3. Add Scopes
There are several scopes available. Two common scopes are thetimescope
和thedsp.SpectrumAnalyzer
. This tutorial usestimescope
to visualize the audio signal.
Thetimescope
System object displays an audio signal in the time domain. Create the System object. To aid visualization, specify values for the时间跨度
,BufferLength.
, andYLimits
properties. To visualize an audio signal frame-by-frame, call thetimescope
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 thereverberator
to process the signal within your audio stream loop.
Create areverberator
System object, and specify theSampleRate
property as the sample rate of your file reader. To adjust the reverberation effect, specify values for the预制
和WetDryMix
properties. To apply the reverberation effect to an audio signal frame-by-frame, call thereverberator
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:
TheAudio Test Bench– UI-based exercises for
audioPlugin
类和大多数音频工具箱系统对象。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 the
function in theconfigureMIDI
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, 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