function HelperUDPPluginReceiver
%HelperUDPPluginReceiver Receive and process data from DAW
%
%   This file  is used only in support of UDPDeployedAudioPluginExample and
%   may be modified or removed in the future.

% Copyright 2015-2018 The MathWorks, Inc.

% The UDPSender plugin sends frames of audio to the network at whatever
% frame size the DAW uses. Specify a reasonable upper bound as the maximum
% message length supported by the dsp.UDPReceiver, 8188.
receiver = dsp.UDPReceiver(...
    'MessageDataType','double',...  % Data type used in DAW
    'LocalIPPort',20000,...         % Remote IP port of UDP sender
    'MaximumMessageLength',8188,...
    'ReceiveBufferSize',8188*8);    % (Max message length * data type size)

scope = dsp.SpectrumAnalyzer(...
    'SampleRate',44100,...          % Sample rate of audio in DAW
    'PlotAsTwoSidedSpectrum',false,...
    'FrequencyScale','log',...
    'YLimits',[-70 10]);

tic
while toc < 10
    audioIn = receiver();
    if ~isempty(audioIn)
        
        % The UDPSender plugin converts the audio it sends from two
        % channels to one channel by stacking the channels. Convert the
        % signal back to stereo.
        x = numel(audioIn)/2;
        left = audioIn(1:x);
        right = audioIn(x+1:end);
        
        % Convert the stereo signal to a mono for visualization.
        mono = 0.5*sum([left,right],2);
        scope(mono)   
    end  
end

release(scope)
release(receiver)