function varargout = Test(varargin)
% TEST M-file for Test.fig
%      TEST, by itself, creates a new TEST or raises the existing
%      singleton*.
%
%      H = TEST returns the handle to a new TEST or the handle to
%      the existing singleton*.
%
%      TEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST.M with the given input arguments.
%
%      TEST('Property','Value',...) creates a new TEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Test_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Test_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 Test

% Last Modified by GUIDE v2.5 23-May-2011 22:44:14

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Test_OpeningFcn, ...
                   'gui_OutputFcn',  @Test_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
% End initialization code - DO NOT EDIT


% --- Executes just before Test is made visible.
function Test_OpeningFcn(hObject, eventdata, handles, varargin)


global IC;
IC=0;
% Choose default command line output for Test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = Test_OutputFcn(hObject, eventdata, handles) 
% Get default command line output from handles structure
varargout{1} = handles.output;

function edit_meno_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function edit_meno_CreateFcn(hObject, eventdata, handles)

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


function edit_priezvisko_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_priezvisko_CreateFcn(hObject, eventdata, handles)

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


% --- Executes on button press in radiobutton_muz.
function radiobutton_muz_Callback(hObject, eventdata, handles)

% --- Executes on button press in radiobutton_zena.
function radiobutton_zena_Callback(hObject, eventdata, handles)

% --- Executes on button press in radiobutton_yes.
function radiobutton_yes_Callback(hObject, eventdata, handles)

% --- Executes on button press in radiobutton_no.
function radiobutton_no_Callback(hObject, eventdata, handles)

% --- Executes on button press in radiobutton_yes2.
function radiobutton_yes2_Callback(hObject, eventdata, handles)

% --- Executes on button press in radiobutton_no2.
function radiobutton_no2_Callback(hObject, eventdata, handles)

function edit_vek_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_vek_CreateFcn(hObject, eventdata, handles)

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

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

global surname;
age=get(handles.edit_vek,'String');
name=get(handles.edit_meno,'String');
surname=get(handles.edit_priezvisko,'String');
female=get(handles.radiobutton_zena,'Value');
male=get(handles.radiobutton_muz,'Value');
stqy=get(handles.radiobutton_yes,'Value');
stqn=get(handles.radiobutton_no,'Value');
ndqy=get(handles.radiobutton_yes2,'Value');
ndqn=get(handles.radiobutton_no2,'Value');
nastroj=get(handles.edit_nastroj,'String');
testy=get(handles.radiobutton_testy,'Value');
testn=get(handles.radiobutton_testn,'Value');
fid = fopen('new_data2.txt', 'a+');
fseek(fid,0,'eof');
fprintf(fid, '%s %s %s muz %d zena %d sluch %d / %d nastroj %d / %d hraje na %s, na učasť na teste %d / %d \n',name, surname, age,male, female, stqy,stqn, ndqy, ndqn, nastroj,testy,testn);
fclose(fid);
set(handles.uipanel4,'Visible','off');
msgbox('Údaje boli zaznamenané, pokračujete SPUSTENÍM TESTU');
set(handles.popupmenu,'Enable','on');

function edit_nastroj_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_nastroj_CreateFcn(hObject, eventdata, handles)

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


% --- Executes on button press in pushbutton_start.
function pushbutton_start_Callback(hObject, eventdata, handles)

global PL;global C;global pocet;global IC;global M;
C=size(PL);
IC=0;
pocet = nchoosek(C(1),2);
set(handles.slider,'max',pocet-1);
set(handles.slider, 'Value',IC);
set(handles.slider,'Visible','on');
set(handles.uipanel8,'Visible','on');
%msgbox(num2str(pocet));
set(handles.pushbutton_start,'enable','off');
set(handles.popupmenu,'Enable','off');
set(handles.pushbutton_next,'enable','off');
set(handles.pushbutton_mos,'enable','off');
M(15,4)=0;

% --- Executes on button press in pushbutton_play1.
function pushbutton_play1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_play1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global PL;global IC;global player;global M;
[x,fs] = wavread(char(PL(M(IC+1,1),:))); % Read in track
player=audioplayer(x,fs);
play(player);

% --- Executes on button press in pushbutton_play2.
function pushbutton_play2_Callback(hObject, eventdata, handles)

global PL;global IC;global player;global M;
[x,fs] = wavread(char(PL(M(IC+1,2),:))); % Read in track
player=audioplayer(x,fs);
play(player);


% --- Executes on slider movement.
function slider_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function slider_CreateFcn(hObject, eventdata, handles)

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in pushbutton_next.
function pushbutton_next_Callback(hObject, eventdata, handles)

%ic=0;
global IC;global pocet;global M;global surname;
M(IC+1,3)=get(handles.radiobutton_first,'Value');
M(IC+1,4)=get(handles.radiobutton_second,'Value');
IC=IC+1;
set(handles.slider, 'Value',IC);
set(handles.radiobutton_first,'Value',0);
set(handles.radiobutton_second,'Value',0);
set(handles.pushbutton_next,'Enable','off');
if IC==pocet;
    set(handles.pushbutton_next,'Enable','off');
    msgbox('Test bol úspešne vyplnený. Vyberte další test alebo ukončite program.');
    set(handles.uipanel8,'Visible','off');
    set(handles.pushbutton_start,'Enable','on');
    set(handles.pushbutton_mos,'enable','on');
    set(handles.popupmenu,'Enable','on');
    msgbox(num2str(M));
    fid = fopen('new_data2.txt', 'a+');
    fseek(fid,0,'eof');
    fprintf(fid, [repmat('%d ', 1, size(M,2)), '\n'], M'); 
    fclose(fid);
end

% --- Executes during object creation, after setting all properties.
function pushbutton_next_CreateFcn(hObject, eventdata, handles)

% --- Executes on selection change in popupmenu.
function popupmenu_Callback(hObject, eventdata, handles)

global PL; global M;global MPL;
fid = fopen('new_data2.txt', 'a+');
fseek(fid,0,'eof');
switch get(handles.popupmenu,'Value')   
    case 1
        set(handles.pushbutton_start,'enable','off');
        set(handles.pushbutton_mos,'enable','off');
    case 2
        M = [1 2; 3 4; 5 6; 1 3; 2 6; 3 5; 1 4; 2 3; 4 5; 1 6; 2 5; 3 6; 2 4; 1 5; 4 6];
        MPL=[1 6 2 5 3 4];
        set(handles.pushbutton_start,'enable','on');
        set(handles.pushbutton_mos,'enable','on');
        fprintf(fid,'BABBLE 6dB\n'); 
        PL(1,:)= 'TEST_SIGNALS\Babble\MUZ_22_3_normalized__clean_sig_';
        PL(2,:)= 'TEST_SIGNALS\Babble\MUZ_22_3_norm_bab_SNR6dB_SpecMa';
        PL(3,:)= 'TEST_SIGNALS\Babble\MUZ_22_3_norm_bab_SNR6dB_SSp0,2';
        PL(4,:)= 'TEST_SIGNALS\Babble\MUZ_22_3_norm_bab_SNR6dB_SSp0,5';
        PL(5,:)= 'TEST_SIGNALS\Babble\MUZ_22_3_norm_bab_SNR6dB_SSp0,8';
        PL(6,:)= 'TEST_SIGNALS\Babble\MUZ_22_3_norm_bab_SNR6dB_babble';
    case 3
        M = [4 6; 1 5; 2 4; 3 6; 2 5; 1 6; 4 5; 2 3; 1 4; 3 5; 2 6; 1 3; 5 6; 3 4; 1 2];
        MPL=[6 1 5 2 4 3];
        set(handles.pushbutton_start,'enable','on');
        set(handles.pushbutton_mos,'enable','on');
        fprintf(fid,'FACTORY 6dB\n'); 
        PL(1,:)= 'TEST_SIGNALS\Factry\MUZ_22_3_normalized__clean_sig_';
        PL(2,:)= 'TEST_SIGNALS\Factry\MUZ_22_3_norm_fac_SNR6dB_SpecMa';
        PL(3,:)= 'TEST_SIGNALS\Factry\MUZ_22_3_norm_fac_SNR6dB_SSp0,2';
        PL(4,:)= 'TEST_SIGNALS\Factry\MUZ_22_3_norm_fac_SNR6dB_SSp0,5';
        PL(5,:)= 'TEST_SIGNALS\Factry\MUZ_22_3_norm_fac_SNR6dB_SSp0,8';
        PL(6,:)= 'TEST_SIGNALS\Factry\MUZ_22_3_norm_fac_SNR6dB_factor';
    case 4
        M = [2 1; 4 3; 6 5; 3 1; 6 2; 5 3; 4 1; 3 2; 5 4; 6 1; 5 2; 6 3; 4 2; 5 1; 6 4];
        MPL=[4 3 5 2 6 1];
        set(handles.pushbutton_start,'enable','on');
        set(handles.pushbutton_mos,'enable','on');
        fprintf(fid,'WHITE NOISE 6dB\n'); 
        PL(1,:)= 'TEST_SIGNALS\WNoise\MUZ_22_3_normalized__clean_sig_';
        PL(2,:)= 'TEST_SIGNALS\WNoise\MUZ_22_3_norm_w_n_SNR6dB_SpecMa';
        PL(3,:)= 'TEST_SIGNALS\WNoise\MUZ_22_3_norm_w_n_SNR6dB_SSp0,2';
        PL(4,:)= 'TEST_SIGNALS\WNoise\MUZ_22_3_norm_w_n_SNR6dB_SSp0,5';
        PL(5,:)= 'TEST_SIGNALS\WNoise\MUZ_22_3_norm_w_n_SNR6dB_SSp0,8';
        PL(6,:)= 'TEST_SIGNALS\WNoise\MUZ_22_3_norm_w_n_SNR6dB_whiten';
    case 5
        M = [6 4; 5 1; 4 2; 6 3; 5 2; 6 1; 5 4; 3 2; 4 1; 5 3; 6 2; 3 1; 6 5; 4 3; 2 1];
        MPL=[3 4 2 5 1 6];
        set(handles.pushbutton_start,'enable','on');
        set(handles.pushbutton_mos,'enable','on');
        fprintf(fid,'UL. ZUBRI 6dB\n'); 
        PL(1,:)= 'TEST_SIGNALS\Zubri_\MUZ_22_3_normalized__clean_sig_';
        PL(2,:)= 'TEST_SIGNALS\Zubri_\MUZ_22_3_norm_zub_SNR6dB_SpecMa';
        PL(3,:)= 'TEST_SIGNALS\Zubri_\MUZ_22_3_norm_zub_SNR6dB_SSp0,2';
        PL(4,:)= 'TEST_SIGNALS\Zubri_\MUZ_22_3_norm_zub_SNR6dB_SSp0,5';
        PL(5,:)= 'TEST_SIGNALS\Zubri_\MUZ_22_3_norm_zub_SNR6dB_SSp0,8';
        PL(6,:)= 'TEST_SIGNALS\Zubri_\MUZ_22_3_norm_zub_SNR6dB_zubri_';
   
    otherwise
end
fclose(fid);
% --- Executes during object creation, after setting all properties.
function popupmenu_CreateFcn(hObject, eventdata, handles)

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


% --- Executes on button press in pushbutton_stop.
function pushbutton_stop_Callback(hObject, eventdata, handles)

global player;
stop(player);


% --- Executes when selected object is changed in uipanel10.
function uipanel10_SelectionChangeFcn(hObject, eventdata, handles)

set(handles.pushbutton_next,'Enable','on');


% --- Executes on button press in pushbutton_mos.
function pushbutton_mos_Callback(hObject, eventdata, handles)

global PL;global C;global pocet;global IC;global M;global MPL;
C=size(PL);
IC=0;
set(handles.uipanel_mos,'Visible','on');
set(handles.slider,'max',C(1));
set(handles.slider, 'Value',0);
set(handles.slider,'Visible','on');
set(handles.pushbutton_start,'enable','off');
set(handles.pushbutton_mos,'enable','off');
set(handles.popupmenu,'Enable','off');
set(handles.pushbutton_mnext,'Enable','off');


% --- Executes on button press in pushbutton_mplay.
function pushbutton_mplay_Callback(hObject, eventdata, handles)

global PL;global IC;global player;global MPL;

[x,fs] = wavread(char(PL(MPL(IC+1),:))); % Read in track
player=audioplayer(x,fs);
play(player);

% --- Executes on button press in pushbutton_mstop.
function pushbutton_mstop_Callback(hObject, eventdata, handles)

global player;
stop(player);

% --- Executes on button press in pushbutton_mnext.
function pushbutton_mnext_Callback(hObject, eventdata, handles)

global IC;global C;
IC=IC+1;
set(handles.slider, 'Value',IC);
set(handles.radiobutton_5,'Value',0);
set(handles.radiobutton_4,'Value',0);
set(handles.radiobutton_3,'Value',0);
set(handles.radiobutton_2,'Value',0);
set(handles.radiobutton_1,'Value',0);
set(handles.pushbutton_mnext,'Enable','off');
if IC==C(1);
    set(handles.pushbutton_mnext,'Enable','off');
    msgbox('Test bol úspešne vyplnený. Vyberte další test alebo ukončite program.');
    set(handles.uipanel_mos,'Visible','off');
    set(handles.slider,'Visible','off');
    set(handles.pushbutton_start,'Enable','on');
    set(handles.pushbutton_mos,'enable','on');
    set(handles.popupmenu,'Enable','on');
end

% --- Executes when selected object is changed in uipanel12.
function uipanel12_SelectionChangeFcn(hObject, eventdata, handles)

global MOS;global C;global MPL;global IC;
MOS(C(1))=0;
set(handles.pushbutton_mnext,'Enable','on');
fid = fopen('new_data2.txt', 'a+');
fseek(fid,0,'eof');
%fprintf(fid,'MOS Score');
switch get(eventdata.NewValue,'Tag')  
    case 'radiobutton_5'
        
        fprintf(fid,'nahrávka %d - známka 5\n',MPL(IC+1));
    case 'radiobutton_4'
        fprintf(fid,'nahrávka %d - známka 4\n',MPL(IC+1));
    case 'radiobutton_3'
        fprintf(fid,'nahrávka %d - známka 3\n',MPL(IC+1));
    case 'radiobutton_2'
        fprintf(fid,'nahrávka %d - známka 2\n',MPL(IC+1));
    case 'radiobutton_1'    
        fprintf(fid,'nahrávka %d - známka 1\n',MPL(IC+1));
    otherwise
        % Code for when there is no match.
end
fclose(fid);


% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)

global player;
[x,fs] = wavread('TEST_SIGNALS\excellent.wav'); % Read in track
player=audioplayer(x,fs);
play(player);
set(handles.pushbutton12,'Enable','off');

% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)

global player;
[x,fs] = wavread('TEST_SIGNALS\fair.wav'); % Read in track
player=audioplayer(x,fs);
play(player);
set(handles.pushbutton13,'Enable','off');

% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)

global player;
[x,fs] = wavread('TEST_SIGNALS\bad.wav'); % Read in track
player=audioplayer(x,fs);
play(player);
set(handles.pushbutton14,'Enable','off');