function varargout = gui(varargin)
% GUI MATLAB code for gui.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui

% Last Modified by GUIDE v2.5 20-Apr-2020 00:48:12

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end




function gui_OpeningFcn(hObject, eventdata, handles, varargin)


handles.output = hObject;
guidata(hObject, handles);


function varargout = gui_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;




function pushbutton1_Callback(hObject, eventdata, handles)

[filename, pathname] = uigetfile('*.wav', 'Select a wave file');
[y, Fs] = audioread(fullfile(pathname, filename));

handles.y         = y;
handles.Fs        = Fs;

handles.y = handles.y(5786:end,1); %%%%%%%%%%%%%%%%%%%%%%%%% <----- delete
handles.player = audioplayer(handles.y, handles.Fs);

axes(handles.axes1);
handles.t=[0:length(handles.y)-1] / handles.Fs;
handles.t = handles.t*1000;
plot(handles.t,handles.y);
xlabel('Čas [ms]')
ylabel('y [-]')

handles.output = hObject;
guidata(hObject, handles);




function vyber_parametru_Callback(hObject, eventdata, handles)

Znelost_metody = {'ZCR';'STE';'HNR'};
Intonace_metody = {'ACF','NCC','AMDF'};
Podobnost_metody = {'NCC (lin)';'NCC (nelin)';'AMDF (lin)';'AMDF (nelin)';'DTW (lin)';'DTW (nelin)'};
val = get(hObject,'Value');
handles.str = get(hObject,'String');
switch handles.str{val}
    case 'Znělost/Neznělost'
        set(handles.vyber_metody,'String',Znelost_metody,'Value',1);
    case 'Intonace'
        set(handles.vyber_metody,'String',Intonace_metody,'Value',1);
    case 'Podobnost'
        set(handles.vyber_metody,'String',Podobnost_metody,'Value',1);
end
guidata(hObject, handles)
 
  



function vyber_parametru_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




function vyber_metody_Callback(hObject, eventdata, handles)

handles.metoda = get(handles.vyber_metody,'String');

guidata(hObject, handles)





function vyber_metody_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function delka_okna_Callback(hObject, eventdata, handles)

handles.delka = str2double(get(handles.delka_okna, 'String'));

guidata(hObject, handles)



function delka_okna_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in prehrat.
function prehrat_Callback(hObject, eventdata, handles)

play(handles.player);



function start_Callback(hObject, eventdata, handles)

    contents = get(handles.vyber_metody,'String'); 
    metoda = contents{get(handles.vyber_metody,'Value')};
    
    switch metoda
        case 'STE'
        	[y2, znelost, Y] = STE_final(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);             
            plot(handles.t,y2); 
            LOKACE_znelost(znelost,Y);
            clear y2 znelost Y;
            title('STE')
            xlabel('Čas [ms]')
            ylabel('y [-]')

        case 'ZCR'
        	[y2, znelost, Y] = ZCR_final(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);             
            plot(handles.t,y2); 
            LOKACE_znelost(znelost,Y);
            clear y2 znelost Y;
            title('ZCR')
            xlabel('Čas [ms]')
            ylabel('y [-]')
            
        case 'HNR'
        	[y2, znelost, Y] = HNR_final(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);             
            plot(handles.t,y2); 
            LOKACE_znelost(znelost,Y);
            clear y2 znelost Y;
            title('HNR')
            xlabel('Čas [ms]')
            ylabel('y [-]')
                        
        case 'ACF'
        	[F] = Pitch_ACF_final(handles.y, handles.Fs, handles.delka, @STE_final, handles.delka);
            axes(handles.axes2);
            t2=[0:handles.delka:numel(F)*20-1];
            stem(t2,F); 
            LOKACE_pitch(F, handles.prah);
            clear y2 znelost Y;
            title('Intonace - Autokorelace')
            xlabel('Čas [ms]')
            ylabel('F [Hz]')
                                    
        case 'NCC'
        	[F] = Pitch_NCC_final(handles.y, handles.Fs, handles.delka, @STE_final, handles.delka);
            axes(handles.axes2);
            t2=[0:handles.delka:numel(F)*20-1];
            stem(t2,F); 
            LOKACE_pitch(F, handles.prah);
            clear y2 znelost Y;
            title('Intonace - Normalizovaná cross korelace')
            xlabel('Čas [ms]')
            ylabel('F [Hz]')
                                    
        case 'AMDF'
        	[F] = Pitch_AMDF_final(handles.y, handles.Fs, handles.delka, @STE_final, handles.delka);
            axes(handles.axes2);
            t2=[0:handles.delka:numel(F)*20-1];
            stem(t2,F); 
            LOKACE_pitch(F, handles.prah);
            clear y2 znelost Y;
            title('Intonace - AMDF')
            xlabel('Čas [ms]')
            ylabel('F [Hz]')
                                                
        case 'NCC (lin)'
        	[podobnost] = NCC_Periodicita_lin(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);
            stem(podobnost); 
            LOKACE_podobnost(podobnost, handles.delka, handles.y, handles.prah)
            clear y2 znelost Y;
            title('Periodicita (NCC, lineární přizp.)')
            xlabel('perioda [n]')
            ylabel('Podobnost [%]')
                                                            
        case 'NCC (nelin)'
        	[podobnost] = NCC_Periodicita_nelin2(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);
            stem(podobnost); 
            LOKACE_podobnost(podobnost, handles.delka, handles.y, handles.prah)
            clear y2 znelost Y;
            title('Periodicita (NCC, nelineární přizp.)')
            xlabel('perioda [n]')
            ylabel('Podobnost [%]')
                                                                        
        case 'AMDF (lin)'
        	[podobnost] = AMDF_Periodicita_lin3(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);
            stem(podobnost); 
            LOKACE_podobnost(podobnost, handles.delka, handles.y, handles.prah)
            clear y2 znelost Y;
            title('Periodicita (AMDF, lineární přizp.)')
            xlabel('perioda [n]')
            ylabel('Podobnost [%]')
                                                                                    
        case 'AMDF (nelin)'
        	[podobnost] = AMDF_Periodicita_nelin(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);
            stem(podobnost); 
            LOKACE_podobnost(podobnost, handles.delka, handles.y, handles.prah)
            clear y2 znelost Y;
            title('Periodicita (AMDF, nelineární přizp.)')
            xlabel('perioda [n]')
            ylabel('Podobnost [%]')
                                                                                                
        case 'DTW (lin)'
        	[podobnost] = DTW_Periodicita_lin(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);
            stem(podobnost); 
            LOKACE_podobnost(podobnost, handles.delka, handles.y, handles.prah)
            clear y2 znelost Y;
            title('Periodicita (DTW, lineární přizp.)')
            xlabel('perioda [n]')
            ylabel('Podobnost [%]')
                                                                                                            
        case 'DTW (nelin)'
        	[podobnost] = DTW_Periodicita_nelin(handles.y, handles.Fs, handles.delka);
            axes(handles.axes2);
            stem(podobnost); 
            LOKACE_podobnost(podobnost, handles.delka, handles.y, handles.prah)
            clear y2 znelost Y;
            title('Periodicita (DTW, nelineární přizp.)')
            xlabel('perioda [n]')
            ylabel('Podobnost [%]')
            
            
            
            
    end

guidata(hObject, handles)



function prah_Callback(hObject, eventdata, handles)

    handles.prah = str2double(get(handles.prah, 'String'));

guidata(hObject, handles)



function prah_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
