function hMainFigure = HelperCreateSoundPressureUI()
% Function to create a UI to select the filter, the audio source and 
% display the rms pressure values before and after weighting.
%
% This function HelperCreateSoundPressureUI is only in support of
% SoundPressureMeasurementExample. It may change in a future release.
%

%   Copyright 2014-2018 The MathWorks, Inc.

%% Handles for UI objects and UDP sender.
% Declare and create all the UI objects in this interface here so that they
% can be used in any functions
hMainFigure     =   figure(...       % the main UI figure
                     'Name', 'Filter Tuning UI', ...
                     'MenuBar','none', ...
                     'Toolbar','none', ...
                     'NumberTitle','off', ...
                     'Color', get(0, 'defaultuicontrolbackgroundcolor'),...
                     'DeleteFcn', @hFigDeleteCallback);
                    
hMainText       =   uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Units','normalized', ...
                     'Position', [0 0.74 1 0.2], ...
                     'String','Sound Pressure Measurement', ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'FontWeight', 'bold');                       %#ok<*NASGU>
                    
hAudioText      =   uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Units','normalized', ...
                     'Position', [0 0.68 1 0.1], ...
                     'String','Audio source', ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'FontWeight', 'bold');                       
                    
hAudioSelect    =   uicontrol(hMainFigure,...   % pop up menu to select an audio source
                     'Style', 'popup', ...                        
                     'Units','normalized', ...
                     'Position', [0.3 0.61 0.4 0.1], ...
                     'String', {'Pink noise', ...
                                'External (mic)', ...
                                'Engine', ...
                                'Turbine', ...
                                'Washing machine', ...
                                'Rock drums', ...
                                'Load file...'}, ...
                     'UserData', 1, ...  % keep track of previous selection
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'Callback', @hAudioSelectCallback, ...
                     'Tag', 'hcspGuiSourceSelecter');
                    
hLevelNoFiltValue = uicontrol(hMainFigure,...
                     'Style','checkbox', ...
                     'Units','normalized', ...
                     'Position', [0.72 0.63 0.2 0.1], ...
                     'String', 'Mute', ...
                     'Enable', 'on', ...
                     'Value', 1, ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'Callback', @hMuteSelectCallback,...
                     'Tag', 'hcspGuiMuteSelecter');
                 
hAudioLoadedText =  uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Units','normalized', ...
                     'Position', [0 0.53 1 0.1], ...
                     'String','Pink noise', ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'Tag', 'StFileName');                     
                    
hAudioPathText =    uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Visible','off', ...
                     'String','Pink noise', ...
                     'Tag', 'StFullPathName');                     
                    
hFilterText     =   uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Units','normalized', ...
                     'Position', [0.15 0.41 0.3 0.1], ...
                     'String','Weighting filter', ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'FontWeight', 'bold');      
                    
hFilterSelect   =   uicontrol(hMainFigure,...   % pop up menu to select a filter
                     'Style', 'popup', ...                        
                     'Units','normalized', ...
                     'Position', [0.15 0.34 0.3 0.1], ...
                     'String', {'Z-Weighting (none)', 'A-Weighting', 'C-Weighting'}, ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'Callback', @hFiltSelectCallback, ...
                     'Tag', 'hcspGuiFilterSelecter');
                   
hBandwidthText  =   uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Units','normalized', ...
                     'Position', [0.55 0.41 0.3 0.1], ...
                     'String','Bandwidth', ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'FontWeight', 'bold');      
                    
hBandwidthSelect =  uicontrol(hMainFigure,...   % pop up menu to select a bandwidth
                     'Style', 'popup', ...                        
                     'Units','normalized', ...
                     'Position', [0.55 0.34 0.3 0.1], ...
                     'String', {'1 octave', '2/3 octave', '1/3 octave'}, ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'Callback', @hBandwidthSelectCallback, ...
                     'Tag', 'hcspGuiBandwidthSelector');
                   
hLevelFiltText  =   uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Units','normalized', ...
                     'Position', [0 0.16 1 0.1], ...
                     'String','Sound pressure level', ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.3, ...
                     'FontWeight', 'bold');                      
                    
hLevelFiltValue =   uicontrol(hMainFigure,...
                     'Style','text', ...
                     'Units','normalized', ...
                     'Position', [0 0.08 1 0.1], ...
                     'String', '0', ...
                     'FontUnits', 'normalized', ...
                     'FontSize', 0.45, ...
                     'Tag', 'StRmsPressureFilt');                     

UDPSender  = dsp.UDPSender; % UDP sender
filterNumber = 0;           % Can be changed to select a different filter
bandwidthNumber = 0;        % Can be changed to select a different bandwidth
changeAudioFile = false;    % New audio source/file
audioSource = 0;            % Audio source ID
audioMute = 1;              % Mute audio
drawnow

%% Callback functions as nested functions
function sendUDP()
    packetUDP = zeros(1,5);
    packetUDP(1) = filterNumber;
    packetUDP(2) = bandwidthNumber;
    packetUDP(3) = changeAudioFile;
    packetUDP(4) = audioSource;
    packetUDP(5) = audioMute;
    UDPSender(packetUDP);
end

function hMuteSelectCallback(cb,~)
    % Called when selecting the checkbox
    audioMute = cb.Value;
    sendUDP();
end

function hAudioSelectCallback(source,~)
    % Called when an audio source is selected from the pop-up menu
    str = get(source, 'String');
    val = get(source, 'Value');
    
    switch str{val}
        case 'External (mic)' 
            audioSource = 1;
            set(hAudioLoadedText, 'String', 'External source (mic)');
        case 'Engine'   
            audioSource = 2;
            set(hAudioLoadedText, 'String', 'Engine-16-44p1-stereo-20sec.wav');
            set(hAudioPathText,   'String', 'Engine-16-44p1-stereo-20sec.wav');
        case 'Turbine'
            audioSource = 2;
            set(hAudioLoadedText, 'String', 'Turbine-16-44p1-mono-22secs.wav');
            set(hAudioPathText,   'String', 'Turbine-16-44p1-mono-22secs.wav');
        case 'Washing machine'
            audioSource = 2;
            set(hAudioLoadedText, 'String', 'WashingMachine-16-44p1-stereo-10secs.wav');            
            set(hAudioPathText,   'String', 'WashingMachine-16-44p1-stereo-10secs.wav');            
        case 'Rock drums'
            audioSource = 2;
            set(hAudioLoadedText, 'String', 'RockDrums-44p1-stereo-11secs.mp3');
            set(hAudioPathText,   'String', 'RockDrums-44p1-stereo-11secs.mp3');
        case 'Load file...' 
            [fileName,pathName] = uigetfile({'*.wav;*.mp3;*.mp4;*.m4a;*.flac;*.ogg;*.wma;*.au', ...
                                'Audio Files (*.wav,*.mp3,*.mp4,*.m4a,*.flac,*.ogg,*.wma,*.au)'}, ...
                                'Select an audio file');
            if (fileName ~=0) 
                audioSource = 2;
                set(hAudioLoadedText, 'String', fileName);
                set(hAudioPathText, 'String', fullfile(pathName, fileName));
            else % User canceled the new file selection
                prevSelect = get(source, 'UserData');
                if strcmp(str{prevSelect}, 'Load file...')
                    audioSource = 2; % go back to previously loaded file
                else % go back to the previous item (not a 'load file...')
                    set(source, 'Value', prevSelect); % set the previous item
                    hAudioSelectCallback(source);     % callback with old value
                    val = prevSelect;   % save last item instead of 'load file'
                end
            end
        otherwise
            audioSource = 0;
            set(hAudioLoadedText, 'String', 'Pink noise');
            set(source, 'Value', 1); % in case otherwise isn't pink noise
            val = 1;
    end
    
    set(source, 'UserData', val); % Keep this selection in case we later cancel a 'load file'
    
    if audioSource == 1
        set(hLevelNoFiltValue, 'Enable', 'off');
        audioMute = true;
    else
        set(hLevelNoFiltValue, 'Enable', 'on');
        audioMute = get(hLevelNoFiltValue, 'Value');
    end
    
    changeAudioFile = true;
    sendUDP();
    changeAudioFile = false;
end

function hFiltSelectCallback(source,~)
    % Called when a filter is selected from the pop-up menu
    str = get(source, 'String');
    val = get(source, 'Value');
    
    switch str{val}
        case 'A-Weighting' 
            filterNumber = 1;
        case 'C-Weighting' 
            filterNumber = 2;
        otherwise % Z-Weighting
            filterNumber = 0;
    end
   
    sendUDP();
end

function hBandwidthSelectCallback(source,~)
    % Called when a bandwidth is selected from the pop-up menu
    str = get(source, 'String');
    val = get(source, 'Value');
    
    switch str{val}
        case '1/3 octave' 
            bandwidthNumber = 2;
        case '2/3 octave' 
            bandwidthNumber = 1;
        otherwise % 1 octave
            bandwidthNumber = 0;
    end
   
    sendUDP();
end

function hFigDeleteCallback(~,~,~)
    % Called when the UI is closed
    release(UDPSender);
end

end

