function HelperAudioEqualizerAndroid(blk, sys, action, paramName, paramNum)
% Helper function for audioequalizerAndroid model

% Copyright 2016-2019 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);
                AndroidInputSelectorParam = maskObj.Parameters(3);
                if strcmp(DeviceChoice, 'Run on host') == 1
                    set_param(strcat(blk, '/Manual Variant Sink'), 'OverrideUsingVariant', 'V_1');
                    AndroidInputSelectorParam.set('Visible', 'off');
                    hostInputSelectorParam.set('Visible', 'on');
                    HelperAudioEqualizerAndroid(blk, sys, 'InputChoice');
                    set_param(sys, 'SimulationMode', 'normal');
                    set_param(sys, 'HardwareBoard', 'None');
                    set_param(sys, 'IsERTTarget','off');
                elseif strcmp(DeviceChoice, 'Run on Android device') == 1
                    set_param(strcat(blk, '/Manual Variant Sink'), 'OverrideUsingVariant', 'V_2');
                    hostInputSelectorParam.set('Visible', 'off');
                    AndroidInputSelectorParam.set('Visible', 'on');
                    HelperAudioEqualizerAndroid(blk, sys, 'InputChoice');
                    set_param(sys, 'SimulationMode', 'external');
                    set_param(sys, 'HardwareBoard', 'Android Device');
                    set_param(sys, 'IsERTTarget','on');
                    
                    % update sample rate and frame size for Android blocks,
                    % since we disable Android block sample rate and frame
                    % size update when user doesn't have Android support
                    % package installed
                    samplingFreq = get_param(blk, 'SamplingFreq');
                    set_param(strcat(sys, '/Audio Capture'), 'sampleFreq', samplingFreq);
                    set_param(strcat(sys, '/Android Equalizer 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'), 'OverrideUsingVariant', 'V_1');
                    elseif strcmp(get_param(blk, 'HostInputSelector'), 'Host Microphone') == 1
                        set_param(strcat(blk, '/Manual Variant Source'), 'OverrideUsingVariant', 'V_2');
                    end
                elseif strcmp(get_param(blk, 'DeviceChoice'), 'Run on Android device') == 1
                    if strcmp(get_param(blk, 'AndroidInputSelector'), 'Signal From Workspace') == 1
                        set_param(strcat(blk, '/Manual Variant Source'), 'OverrideUsingVariant', 'V_1');
                    elseif strcmp(get_param(blk, 'AndroidInputSelector'), 'Android Device Microphone') == 1
                        set_param(strcat(blk, '/Manual Variant Source'), 'OverrideUsingVariant', 'V_3');
                    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(sys, '/Host Equalizer 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 Android device') == 1
                    set_param(strcat(sys, '/Audio Capture'), 'sampleFreq', samplingFreq);
                    set_param(strcat(sys, '/Android Equalizer 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 'VisualInit'
                % get parameters from the equalizer block
                CF1 = str2num(get_param(blk, 'CF1'));
                CF2 = str2num(get_param(blk, 'CF2'));
                CF3 = str2num(get_param(blk, 'CF3'));
                BW1 = str2num(get_param(blk, 'BW1'));
                BW2 = str2num(get_param(blk, 'BW2'));
                BW3 = str2num(get_param(blk, 'BW3'));
                G1 = str2num(get_param(blk, 'G1'));
                G2 = str2num(get_param(blk, 'G2'));
                G3 = str2num(get_param(blk, 'G3'));
                fs = str2num(get_param(strcat(blk, '/Parametric EQ Filter1'), 'SampleRate'));
                
                % construct equalizer object and visualize the initial
                % filter spectrum
                if isempty(get_param(blk, 'UserData'))
                    EQ_obj = multibandParametricEQ;
                else
                    EQ_obj = get_param(blk, 'UserData');
                end
                EQ_obj.Frequencies = [CF1 CF2 CF3];
                EQ_obj.QualityFactors = [CF1/BW1 CF2/BW2 CF3/BW3];
                EQ_obj.PeakGains = [G1 G2 G3];
                EQ_obj.SampleRate = fs;
                visualize(EQ_obj);
                set_param(blk, 'UserData', EQ_obj);  % store the object in UserData
            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 5
        paramNumStr = num2str(paramNum);
        % get stored equalizer object
        EQ_obj = get_param(blk, 'UserData');
        if ~isempty(EQ_obj)  % detect if freq response plot is on show
            % modification
            switch(paramName)
                case 'CF'
                    BW = str2num(get_param(blk, strcat('BW', paramNumStr)));
                    CF = str2num(get_param(blk, strcat('CF', paramNumStr)));
                    EQ_obj.QualityFactors(paramNum) = CF / BW;
                    EQ_obj.Frequencies(paramNum) = CF;
                case 'BW'
                    BW = str2num(get_param(blk, strcat('BW', paramNumStr)));
                    CF = str2num(get_param(blk, strcat('CF', paramNumStr)));
                    EQ_obj.QualityFactors(paramNum) = CF / BW;
                case 'G'
                    G = str2num(get_param(blk, strcat('G', paramNumStr)));
                    EQ_obj.PeakGains(paramNum) = G;
            end
        end
        % store modified object back to UserData
        set_param(blk, 'UserData', EQ_obj);
end

end