%% Communicate Between a DAW and MATLAB Using UDP
% This example shows how to communicate between a digital audio workstation (DAW) and MATLAB using the user datagram protocol (UDP). The information shared between the DAW and MATLAB can used to perform visualization in real time in MATLAB on parameters that are being changed in the DAW.
%% User Datagram Protocol (UDP)
% UDP is a core member of the Internet protocol suite. It is a simple connectionless transmission that does not employ any methods for error checking. Because it does not check for errors, UDP is a fast but unreliable alternative to the transmission control protocol (TCP) and stream control transmission protocol (SCTP). UDP is widely used in applications that are willing to trade fidelity for high-speed transmission, such as video conferencing and real-time computer games. If you use UDP for communication within a single machine, packets are less likely to drop. The tutorials outlined here work best when executed on a single machine.
%% UDP and MATLAB
% These System objects enable you to use UDP with MATLAB:
% dsp.UDPReceiver - Receive UDP packets from network
% dsp.UDPSender - Send UDP packets to network
% To communicate between a DAW and MATLAB using UDP, place a UDP sender in the plugin used in the DAW, and run a corresponding UDP receiver in MATLAB.
%
% The dsp.UDPSender and dsp.UDPReceiver System objects use prebuilt library files that are included with MATLAB.
%% Example Plugins
% These Audio Toolbox™ example plugins use UDP:
% audiopluginexample.UDPSender - Send an audio signal from a DAW to the network. If you generate this plugin and deploy it to a DAW, the plugin sends frames of a stereo signal to the network. The frame size is determined by the DAW. You can modify the example plugin to send any information you want to analyze in MATLAB.
% audiopluginexample.ParametricEqualizerWithUDP - Send a plugin's filter coefficients from a DAW to the network. If you generate this plugin and run it in a DAW, the plugin sends the coefficients of the parametric equalizer you tune in the DAW to the network. The HelperUDPPluginVisualizer function contains a UDP receiver that receives the datagram, and uses it to plot the magnitude response of the filter you are tuning in a DAW.
%% Send Audio from DAW to MATLAB
% Step 1: Generate a VST Plugin
% To generate a VST plugin from audiopluginexample.UDPSender, use the generateAudioPlugin function. It is a best practice to move to a directory that can store the generated plugin before executing this command:

generateAudioPlugin audiopluginexample.UDPSender

% The generated plugin is saved to your current folder and named UDPSender.
%
% Step 2: Open DAW with Appropriate Environment Variables Set
% To run the UDP sender outside of MATLAB, you must open the DAW from a command terminal with the appropriate environment variables set. Setting environment variables enables the deployed UDP sender to use the necessary library files in MATLAB. To learn how to set the environment variables, see the tutorial specific to your system:
% Windows®
% Mac®
% After you set the environment variables, open your DAW from the same command terminal, such as in this example terminal from a Windows system.
%
% Step 3: Receive and Process an Audio Signal
% a. In the DAW, open the generated UDPSender file.
% b. In MATLAB, run this function: HelperUDPPluginReceiver
% The audio signal is displayed on the dsp.SpectrumAnalyzer for analysis.
%
%% Send Coefficients from DAW to MATLAB
% 1. Follow steps 1-2 from Send Audio from DAW to MATLAB, replacing audiopluginexample.UDPSender with audiopluginexample.ParametricEqualizerWithUDP.
% 2. Receive and process filter coefficients
% a. In the DAW, open the generated ParameterEqualizerWithUDP file. The plugin display name is ParametricEQ.
% b. In MATLAB, run this command: HelperUDPPluginVisualizer
% The HelperUDPPluginVisualizer function uses a dsp.UDPReceiver to receive the filter coefficients and then displays the magnitude response for 60 seconds. You can modify the code to extend or reduce the amount of time. The plotted magnitude response corresponds to the parametric equalizer plugin you tune in the DAW.
%
% Copyright 2015-2018 The MathWorks, Inc.