classdef UDPSender < audioPlugin
%UDPSender Send audio, plugin parameters or plugin states via UDP.
%   This audio plugin can be used to send live stereo audio from a Digital
%   Audio Workstation to MATLAB via UDP. The maximum frame/buffer size that
%   can be sent is 4094 samples per channel on a Windows machine or 512
%   samples per channel on a Mac.
%
%   In order to use multiple plugins to send audio at different points in
%   the processing chain, it is necessary to change the UDP port used.
%
% See also: dsp.UDPSender, dsp.UDPReceiver
    
    %   Copyright 2016-2022 The MathWorks, Inc.
    %#codegen
    
    %----------------------------------------------------------------------
    % Public properties
    %----------------------------------------------------------------------
    properties
        % Property to store UDP sender System object
        udpsend
    end
    
    %----------------------------------------------------------------------
    % Constant properties
    %----------------------------------------------------------------------
    properties (Constant)
        % audioPluginInterface manages the number of input/output channels
        % and also instantiates the value class, audioPluginParameter to
        % generate plugin UI parameters.
        PluginInterface = audioPluginInterface(...
            'InputChannels',2,...
            'OutputChannels',2,...
            'PluginName','UDPSender');            
    end
    
    %----------------------------------------------------------------------
    % Private properties
    %----------------------------------------------------------------------
    properties (Access = private, Constant)
        % Maximum samples per channel
        pMaxSamplesPerChannel = getMaxSamples();                 
    end
    
    %----------------------------------------------------------------------
    % Public methods
    %----------------------------------------------------------------------
    methods
        
        function plugin = UDPSender          
            % Construct UDP sender
            plugin.udpsend = dsp.UDPSender('RemoteIPPort', 20000);
        end
        
        function Output = process(plugin, Input)
            % Output is just a passthrough
            Output = Input;
            
            % Send first 512 (on mac) or 4094 (on other platforms) samples
            % of each channel through UDP
            maxIdx = min(size(Input,1), plugin.pMaxSamplesPerChannel);
            plugin.udpsend([Input(1:maxIdx,1);Input(1:maxIdx,2)]);
        end
        function s = saveobj(obj)
            s = saveobj@audioPlugin(obj);
            s.udpsend = matlab.System.saveObject(obj.udpsend);
        end
        function obj = reload(obj,s)
            obj = reload@audioPlugin(obj,s);
            obj.udpsend = matlab.System.loadObject(s.udpsend);
        end
    end
    methods(Static)
        function obj = loadobj(s)
            if isstruct(s)
                obj = audiopluginexample.UDPSender;
                obj = reload(obj,s);
                
            end
        end     
    end
end

function maxSamples = getMaxSamples()
    if ismac()
        maxSamples = 512;
    else
        maxSamples = 4094;
    end
end