function HelperAudioMultibandCompressoriOS(blk, sys, action, compressorNum, paramName)
% Helper function for audiomultibandcompressoriOS model

% Copyright 2016 The MathWorks, Inc.

switch nargin
    case 3
        switch action
            
            case 'DeviceChoice'
                DeviceChoice = get_param(blk, 'DeviceChoice');
                maskObj = Simulink.Mask.get(blk);
                hostInputSelectorParam = maskObj.Parameters(2);
                iOSInputSelectorParam = maskObj.Parameters(3);
                
                if strcmp(DeviceChoice, 'Run on host') == 1
                    set_param(strcat(blk, '/Manual Variant Sink'), 'OverrideUsingVariant', 'V_1');
                    set_param(strcat(blk, '/Manual Variant Source'), 'OverrideUsingVariant', 'V_1');
                    set_param(strcat(sys, '/Display/Manual Variant Sink'), 'OverrideUsingVariant', 'V_1');
                    set_param(strcat(sys, '/Display/Manual Variant Sink1'), 'OverrideUsingVariant', 'V_1');
                    iOSInputSelectorParam.set('Visible', 'off');
                    hostInputSelectorParam.set('Visible', 'on');
                    HelperAudioMultibandCompressoriOS(blk, sys, 'InputChoice');
                    set_param(sys, 'SimulationMode', 'normal');
                    set_param(sys, 'HardwareBoard', 'None');
                    set_param(sys, 'IsERTTarget','off');
                elseif strcmp(DeviceChoice, 'Run on Apple iOS device') == 1
                    set_param(strcat(blk, '/Manual Variant Sink'), 'OverrideUsingVariant', 'V_2');
                    set_param(strcat(blk, '/Manual Variant Source'), 'OverrideUsingVariant', 'V_2');
                    set_param(strcat(sys, '/Display/Manual Variant Sink'), 'OverrideUsingVariant', 'V_2');
                    set_param(strcat(sys, '/Display/Manual Variant Sink1'), 'OverrideUsingVariant', 'V_2');
                    hostInputSelectorParam.set('Visible', 'off');
                    iOSInputSelectorParam.set('Visible', 'on');
                    HelperAudioMultibandCompressoriOS(blk, sys, 'InputChoice');
                    set_param(sys, 'SimulationMode', 'external');
                    set_param(sys, 'HardwareBoard', 'Apple iOS Device');
                    set_param(sys, 'IsERTTarget','on');
                    
                    % update sample rate and frame size for iOS blocks, since we disable iOS
                    % block sample rate and frame size update when user doesn't have iOS
                    % support package installed
                    samplingFreq = get_param(blk, 'SamplingFreq');
                    set_param(strcat(sys, '/Audio Capture'), 'sampleFreq_', samplingFreq);
                    set_param(strcat(blk, '/iOS Compressor and Playback/Audio Playback'), 'sampleFreq_', samplingFreq);
                    if strcmp(samplingFreq, '44100') == 1
                        set_param(strcat(sys, '/Audio Capture'), 'frameSize_', '2205');
                    elseif strcmp(samplingFreq, '48000') == 1
                        set_param(strcat(sys, '/Audio Capture'), 'frameSize_', '2400');
                    end
                end
                
            case 'InputChoice'
                if strcmp(get_param(blk, 'DeviceChoice'), 'Run on host') == 1
                    if strcmp(get_param(blk, 'HostInputSelector'), 'Signal From Workspace') == 1
                        set_param(strcat(blk, '/Manual Variant Source for Input'), 'OverrideUsingVariant', 'V_1');
                    elseif strcmp(get_param(blk, 'HostInputSelector'), 'Host Microphone') == 1
                        set_param(strcat(blk, '/Manual Variant Source for Input'), 'OverrideUsingVariant', 'V_2');
                    end
                elseif strcmp(get_param(blk, 'DeviceChoice'), 'Run on Apple iOS device') == 1
                    if strcmp(get_param(blk, 'iOSInputSelector'), 'Signal From Workspace') == 1
                        set_param(strcat(blk, '/Manual Variant Source for Input'), 'OverrideUsingVariant', 'V_1');
                        set_param(strcat(blk, '/iOS Compressor and Playback/Manual Variant Sink'), 'OverrideUsingVariant', 'V_1');
                        set_param(strcat(blk, '/iOS Compressor and Playback/Manual Variant Source'), 'OverrideUsingVariant', 'V_1');
                    elseif strcmp(get_param(blk, 'iOSInputSelector'), 'iOS Device Microphone') == 1
                        set_param(strcat(blk, '/Manual Variant Source for Input'), 'OverrideUsingVariant', 'V_3');
                        set_param(strcat(blk, '/iOS Compressor and Playback/Manual Variant Sink'), 'OverrideUsingVariant', 'V_2');
                        set_param(strcat(blk, '/iOS Compressor and Playback/Manual Variant Source'), 'OverrideUsingVariant', 'V_2');
                    end
                end
                
            case 'SetSampleRate'
                samplingFreq = get_param(blk, 'SamplingFreq');
                set_param(strcat(sys, '/Signal From Workspace'), 'Fs', samplingFreq);
                set_param(strcat(sys, '/Audio Device Reader'), 'SampleRate', samplingFreq);
                set_param(strcat(blk, '/Crossover Filter'), 'SampleRate', samplingFreq);
                set_param(strcat(blk, '/Host Compressor and Playback/Compressor Bank/Compressor1'), 'SampleRate', samplingFreq);
                set_param(strcat(blk, '/Host Compressor and Playback/Compressor Bank/Compressor2'), 'SampleRate', samplingFreq);
                set_param(strcat(blk, '/Host Compressor and Playback/Compressor Bank/Compressor3'), 'SampleRate', samplingFreq);
                set_param(strcat(blk, '/Host Compressor and Playback/Compressor Bank/Compressor4'), 'SampleRate', samplingFreq);
                set_param(strcat(blk, '/Host Compressor and Playback/Audio Device Writer'), 'SampleRate', samplingFreq);
                
                if strcmp(samplingFreq, '44100') == 1
                    set_param(strcat(sys, '/Signal From Workspace'), 'nsamps', '2205');
                    set_param(strcat(sys, '/Audio Device Reader'), 'SamplesPerFrame', '2205');
                elseif strcmp(samplingFreq, '48000') == 1
                    set_param(strcat(sys, '/Signal From Workspace'), 'nsamps', '2400');
                    set_param(strcat(sys, '/Audio Device Reader'), 'SamplesPerFrame', '2400');
                end
                
                deviceChoice = get_param(blk, 'DeviceChoice');
                if strcmp(deviceChoice, 'Run on Apple iOS device') == 1
                    set_param(strcat(sys, '/Audio Capture'), 'sampleFreq_', samplingFreq);
                    set_param(strcat(blk, '/iOS Compressor and Playback/Audio Playback'), 'sampleFreq_', samplingFreq);
                    if strcmp(samplingFreq, '44100') == 1
                        set_param(strcat(sys, '/Audio Capture'), 'frameSize_', '2205');
                    elseif strcmp(samplingFreq, '48000') == 1
                        set_param(strcat(sys, '/Audio Capture'), 'frameSize_', '2400');
                    end
                end
                
            case 'Bypass'
                bypass = get_param(blk, 'Bypass');
                switch bypass
                    case 'on'
                        set_param(strcat(sys, '/Bypass'), 'Gain', '1');
                    case 'off'
                        set_param(strcat(sys, '/Bypass'), 'Gain', '0');
                end
                
        end
        
    case 4
        compressorNumStr = num2str(compressorNum);
        
        T = str2num(get_param(blk, strcat('T', compressorNumStr)));
        R = str2num(get_param(blk, strcat('R', compressorNumStr)));
        AT = str2num(get_param(blk, strcat('AT', compressorNumStr)));
        RT = str2num(get_param(blk, strcat('RT', compressorNumStr)));
        SampleRate = str2num(get_param(strcat(blk, '/Compressor1'), 'SampleRate'));
        
        if isempty(get_param(blk, 'UserData'))
            compressorObjCellArray = {0, 0, 0, 0};
        else
            compressorObjCellArray = get_param(blk, 'UserData');
        end
        
        switch compressorNum
            case 1
                compressor1Obj = compressor('Threshold', T, 'Ratio', R,...
                    'AttackTime', AT, 'ReleaseTime', RT,...
                    'SampleRate', SampleRate);
                visualize(compressor1Obj);
                compressorObjCellArray(1) = {compressor1Obj};
            case 2
                compressor2Obj = compressor('Threshold', T, 'Ratio', R,...
                    'AttackTime', AT, 'ReleaseTime', RT,...
                    'SampleRate', SampleRate);
                visualize(compressor2Obj);
                compressorObjCellArray(2) = {compressor2Obj};
            case 3
                compressor3Obj = compressor('Threshold', T, 'Ratio', R,...
                    'AttackTime', AT, 'ReleaseTime', RT,...
                    'SampleRate', SampleRate);
                visualize(compressor3Obj);
                compressorObjCellArray(3) = {compressor3Obj};
            case 4
                compressor4Obj = compressor('Threshold', T, 'Ratio', R,...
                    'AttackTime', AT, 'ReleaseTime', RT,...
                    'SampleRate', SampleRate);
                visualize(compressor4Obj);
                compressorObjCellArray(4) = {compressor4Obj};
        end
        
        set_param(blk, 'UserData', compressorObjCellArray);  % store the object in UserData
        
        
    case 5
        compressorNumStr = num2str(compressorNum);
        
        compressorObjCellArray = get_param(blk, 'UserData');
        
        if ~isempty(compressorObjCellArray)
            if strcmp(paramName, 'T') == 1
                T = str2num(get_param(blk, strcat('T', compressorNumStr)));
                compressorObjCellArray{compressorNum}.Threshold = T;
            elseif strcmp(paramName, 'R') == 1
                R = str2num(get_param(blk, strcat('R', compressorNumStr)));
                compressorObjCellArray{compressorNum}.Ratio = R;
            elseif strcmp(paramName, 'AT') == 1
                AT = str2num(get_param(blk, strcat('AT', compressorNumStr)));
                compressorObjCellArray{compressorNum}.AttackTime = AT;
            elseif strcmp(paramName, 'RT') == 1
                RT = str2num(get_param(blk, strcat('RT', compressorNumStr)));
                compressorObjCellArray{compressorNum}.ReleaseTime = RT;
            end
        end
        
        % store modified object back to UserData
        set_param(blk, 'UserData', compressorObjCellArray);
end

end