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

% Last Modified by GUIDE v2.5 26-May-2014 21:25:03

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SMS_OpeningFcn, ...
                   'gui_OutputFcn',  @SMS_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 SMS is made visible.
function SMS_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to SMS (see VARARGIN)

% global sig1 sig2 sig3 %signals for each input
% global fln1 fln2 fln3
global Fs %sampling rate
global hdls %make all handle as a global
% set the position for each sound sources
global file1_width file1_length file1_height 
global file2_width file2_length file2_height
global file3_width file3_length file3_height
global listener_position_x listener_position_y listener_position_z
global earsize
global roomsize_width roomsize_length roomsize_height
global DPR TINY

%setting the basic values for image source model
SMS_Setup;

% Choose default command line output for SMS
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

hdls=handles; % save all handles to global variable

%Set default Values
Fs = 44100;
earsize = 'Medium';

DPR = 57.29578;            % degrees per radian conversion  
TINY = 0.1^30;             % no zero dividing!  

file1_width = 0; file1_length = 0; file1_height = 0; 
file2_width = 0; file2_length = 0; file2_height = 0;
file3_width = 0; file3_length = 0; file3_height = 0;
listener_position_x = 5; listener_position_y = 5; listener_position_z = 5;
roomsize_width = 10; roomsize_length = 10; roomsize_height = 5;

set(handles.file1_width,'value',file1_width);
set(handles.file1_length,'value',file1_length);
set(handles.file1_height,'value',file1_height);

set(handles.file2_width,'value',file2_width);
set(handles.file2_length,'value',file2_length);
set(handles.file2_height,'value',file2_height);

set(handles.file3_width,'value',file3_width);
set(handles.file3_length,'value',file3_length);
set(handles.file3_height,'value',file3_height);

set(handles.roomsize_width,'value',roomsize_width);
set(handles.roomsize_length,'value',roomsize_length);
set(handles.roomsize_height,'value',roomsize_height);

set(handles.earsize_medium,'String',earsize);

set(handles.listener_position_x,'value',listener_position_x);
set(handles.listener_position_y,'value',listener_position_y);
set(handles.listener_position_z,'value',listener_position_z);

% UIWAIT makes SMS wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = SMS_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

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



function file1_Callback(hObject, eventdata, handles)
% hObject    handle to file1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln1 sig1 Fs
fln1=get(handles.file1,'string');
set(handles.file1_reading,'visible','on');
drawnow;
%audio(fln1,0);
[sig1 Fs] = audioread(fln1);
set(handles.file1_reading,'visible','off');
drawnow;
% Hints: get(hObject,'String') returns contents of file1 as text
%        str2double(get(hObject,'String')) returns contents of file1 as a double


% --- Executes during object creation, after setting all properties.
function file1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file2_Callback(hObject, eventdata, handles)
% hObject    handle to file2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln2 sig2 Fs
fln2=get(handles.file2,'string');
set(handles.file2_reading,'visible','on');
drawnow;
%audio(fln1,0);
[sig2 Fs] = audioread(fln2);
set(handles.file2_reading,'visible','off');
drawnow;
% Hints: get(hObject,'String') returns contents of file2 as text
%        str2double(get(hObject,'String')) returns contents of file2 as a double


% --- Executes during object creation, after setting all properties.
function file2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file3_Callback(hObject, eventdata, handles)
% hObject    handle to file3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln3 sig3 Fs
fln3=get(handles.file3,'string');
set(handles.file3_reading,'visible','on');
drawnow;
%audio(fln1,0);
[sig3 Fs] = audioread(fln3);
set(handles.file3_reading,'visible','off');
drawnow;
% Hints: get(hObject,'String') returns contents of file3 as text
%        str2double(get(hObject,'String')) returns contents of file3 as a double


% --- Executes during object creation, after setting all properties.
function file3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in browse_1.
function browse_1_Callback(hObject, eventdata, handles)
% hObject    handle to browse_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln1 sig1 Fs num_channel_sig1
[FileName,PathName,FilterIndex] = uigetfile({'*.mp3;*.wav;*.wma;*.avi;*.mpg','media files (*.mp3,*.wav,*.wma,*.avi,*.mpg)';'*.*',  'All Files (*.*)'},'File Selector') ;
isok=true;
if length(FileName)==1
     if FileName==0
         isok=false;
     end
end
if isok
    fln1=[PathName FileName];
    set(handles.file1,'string',fln1);
    set(handles.file1_reading,'visible','on');
    drawnow;
    disp(fln1);
    %audio(fln1,'0');
    [sig1 Fs] = audioread(fln1);
    info1 = audioinfo(fln1);
    num_channel_sig1 = info1.NumChannels;
    set(handles.file1_reading,'visible','off');
    drawnow;
    
end




% --- Executes on button press in browse_2.
function browse_2_Callback(hObject, eventdata, handles)
% hObject    handle to browse_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln2 sig2 Fs num_channel_sig2
[FileName,PathName,FilterIndex] = uigetfile({'*.mp3;*.wav;*.wma;*.avi;*.mpg','media files (*.mp3,*.wav,*.wma,*.avi,*.mpg)';'*.*',  'All Files (*.*)'},'File Selector') ;
isok=true;
if length(FileName)==1
     if FileName==0
         isok=false;
     end
end
if isok
    fln2=[PathName FileName];
    set(handles.file2,'string',fln2);
    set(handles.file2_reading,'visible','on');
    drawnow;
    disp(fln2);
    %audio(fln2,'0');
    [sig2 Fs] = audioread(fln2);
    info2 = audioinfo(fln2);
    num_channel_sig2 = info2.NumChannels;
    set(handles.file2_reading,'visible','off');
    drawnow;
    
end


% --- Executes on button press in browse_3.
function browse_3_Callback(hObject, eventdata, handles)
% hObject    handle to browse_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln3 sig3 Fs num_channel_sig3
[FileName,PathName,FilterIndex] = uigetfile({'*.mp3;*.wav;*.wma;*.avi;*.mpg','media files (*.mp3,*.wav,*.wma,*.avi,*.mpg)';'*.*',  'All Files (*.*)'},'File Selector') ;
isok=true;
if length(FileName)==1
     if FileName==0
         isok=false;
     end
end
if isok
    fln3=[PathName FileName];
    set(handles.file3,'string',fln3);
    set(handles.file3_reading,'visible','on');
    drawnow;
    disp(fln3);
    %audio(fln3,'0');
    [sig3 Fs] = audioread(fln3);
    info3 = audioinfo(fln3);
    num_channel_sig3 = info3.NumChannels;
    set(handles.file3_reading,'visible','off');
    drawnow;
    
end


% --- Executes on button press in play1.
function play1_Callback(hObject, eventdata, handles)
% hObject    handle to play1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln1
audio (fln1, 11);

% --- Executes on button press in play2.
function play2_Callback(hObject, eventdata, handles)
% hObject    handle to play2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln2
audio (fln2, 12);

% --- Executes on button press in play3.
function play3_Callback(hObject, eventdata, handles)
% hObject    handle to play3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln3
audio (fln3, 13);

% --- Executes on button press in stop1.
function stop1_Callback(hObject, eventdata, handles)
% hObject    handle to stop1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln1
audio (fln1, 3);

% --- Executes on button press in stop2.
function stop2_Callback(hObject, eventdata, handles)
% hObject    handle to stop2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln2
audio (fln2, 3);

% --- Executes on button press in stop3.
function stop3_Callback(hObject, eventdata, handles)
% hObject    handle to stop3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fln3
audio (fln3, 3);

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton3


%%
% Set Source location

function file1_width_Callback(hObject, eventdata, handles)
% hObject    handle to file1_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file1_width as text
%        str2double(get(hObject,'String')) returns contents of file1_width as a double
global file1_width
file1_width=get(hObject,'Value');


% --- Executes during object creation, after setting all properties.
function file1_width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file1_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file2_width_Callback(hObject, eventdata, handles)
% hObject    handle to file2_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file2_width as text
%        str2double(get(hObject,'String')) returns contents of file2_width as a double
global file2_width
file2_width=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file2_width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file2_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file3_width_Callback(hObject, eventdata, handles)
% hObject    handle to file3_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file3_width as text
%        str2double(get(hObject,'String')) returns contents of file3_width as a double
global file3_width
file3_width=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file3_width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file3_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file1_length_Callback(hObject, eventdata, handles)
% hObject    handle to file1_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file1_length as text
%        str2double(get(hObject,'String')) returns contents of file1_length as a double
global file1_length
file1_length=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file1_length_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file1_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file2_length_Callback(hObject, eventdata, handles)
% hObject    handle to file2_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file2_length as text
%        str2double(get(hObject,'String')) returns contents of file2_length as a double
global file2_length
file2_length=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file2_length_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file2_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file3_length_Callback(hObject, eventdata, handles)
% hObject    handle to file3_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file3_length as text
%        str2double(get(hObject,'String')) returns contents of file3_length as a double
global file3_length
file3_length=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file3_length_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file3_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file1_height_Callback(hObject, eventdata, handles)
% hObject    handle to file1_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file1_height as text
%        str2double(get(hObject,'String')) returns contents of file1_height as a double
global file1_height
file1_height=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file1_height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file1_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file2_height_Callback(hObject, eventdata, handles)
% hObject    handle to file2_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file2_height as text
%        str2double(get(hObject,'String')) returns contents of file2_height as a double

global file2_height
file2_height=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file2_height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file2_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function file3_height_Callback(hObject, eventdata, handles)
% hObject    handle to file3_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of file3_height as text
%        str2double(get(hObject,'String')) returns contents of file3_height as a double

global file3_height
file3_height=get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function file3_height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file3_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%%
% Executes on button press in check_position.
function check_position_Callback(hObject, eventdata, handles)
% hObject    handle to check_position (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

plotQuadrature;




function edit13_Callback(hObject, eventdata, handles)
% hObject    handle to edit13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit13 as text
%        str2double(get(hObject,'String')) returns contents of edit13 as a double


% --- Executes during object creation, after setting all properties.
function edit13_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function roomsize_width_Callback(hObject, eventdata, handles)
% hObject    handle to roomsize_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of roomsize_width as text
%        str2double(get(hObject,'String')) returns contents of roomsize_width as a double
global roomsize_width
roomsize_width = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function roomsize_width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roomsize_width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in step5_store.
function step5_store_Callback(hObject, eventdata, handles)
% hObject    handle to step5_store (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global roomsize_width roomsize_length roomsize_height
global room_data
room_data = [roomsize_width roomsize_length roomsize_height];
save room_data.mat room_data;


function roomsize_length_Callback(hObject, eventdata, handles)
% hObject    handle to roomsize_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of roomsize_length as text
%        str2double(get(hObject,'String')) returns contents of roomsize_length as a double
global roomsize_length
roomsize_length = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function roomsize_length_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roomsize_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function roomsize_height_Callback(hObject, eventdata, handles)
% hObject    handle to roomsize_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of roomsize_height as text
%        str2double(get(hObject,'String')) returns contents of roomsize_height as a double
global roomsize_height
roomsize_height = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function roomsize_height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roomsize_height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in pref_SFX.
function pref_SFX_Callback(hObject, eventdata, handles)
% hObject    handle to pref_SFX (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns pref_SFX contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pref_SFX
global pref_SFX
pref_SFX = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function pref_SFX_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pref_SFX (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Power_off.
function Power_off_Callback(hObject, eventdata, handles)
% hObject    handle to Power_off (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all;


% --- Executes on button press in earsize_small.
function earsize_small_Callback(hObject, eventdata, handles)
% hObject    handle to earsize_small (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global earsize
earsize = get(hObject,'String');
% Hint: get(hObject,'Value') returns toggle state of earsize_small


% --- Executes on button press in earsize_medium.
function earsize_medium_Callback(hObject, eventdata, handles)
% hObject    handle to earsize_medium (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global earsize
earsize = get(hObject,'String');
% Hint: get(hObject,'Value') returns toggle state of earsize_medium


% --- Executes on button press in earsize_large.
function earsize_large_Callback(hObject, eventdata, handles)
% hObject    handle to earsize_large (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global earsize
earsize = get(hObject,'String');
% Hint: get(hObject,'Value') returns toggle state of earsize_large


% --- Executes on button press in step4_store.
function step4_store_Callback(hObject, eventdata, handles)
% hObject    handle to step4_store (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on slider movement.
function listener_position_x_Callback(hObject, eventdata, handles)
% hObject    handle to listener_position_x (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global listener_position_x
listener_position_x = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function listener_position_x_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listener_position_x (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% 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 slider movement.
function listener_position_y_Callback(hObject, eventdata, handles)
% hObject    handle to listener_position_y (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global listener_position_y
listener_position_y = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function listener_position_y_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listener_position_y (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% 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 slider movement.
function listener_position_z_Callback(hObject, eventdata, handles)
% hObject    handle to listener_position_z (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global listener_position_z
listener_position_z = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function listener_position_z_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listener_position_z (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% 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 store.
function store_Callback(hObject, eventdata, handles)
% hObject    handle to store (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global file1_width file1_length file1_height
global file2_width file2_length file2_height
global file3_width file3_length file3_height
global roomsize_width roomsize_length roomsize_height
global listener_position_x listener_position_y listener_position_z

global sig1 sig2 sig3 final_sig Fs
global room_data
%global az el

sig_all = {sig1, sig2, sig3};
file_width = [file1_width;file2_width;file3_width];
file_length = [file1_length;file2_length;file3_length];
file_height = [file1_height;file2_height;file3_height];

%room_data (:,1),(:,2),(:,3) : Source position
%room_data (:,4): Room size
%room_data (:,5): Listener position
% Source Position and Listener position is propotional to room size
% file_width = roomsize_width * (file_width/10);

room_data = [roomsize_width*0.1*file1_width,roomsize_length*0.1*file1_length,roomsize_height*0.1*file1_height;,roomsize_width*0.1*file2_width,roomsize_length*0.1*file2_length,roomsize_height*0.1*file2_height;,roomsize_width*0.1*file3_width,roomsize_length*0.1*file3_length,roomsize_height*0.1*file3_height;roomsize_width roomsize_length roomsize_height;roomsize_width*0.1*listener_position_x,roomsize_length*0.1*listener_position_y,roomsize_height*0.1*listener_position_z];
save room_data.mat room_data;

% Get data from image source model
% result_IM_cref2d.mat
for j = 1:3
    ism(j);
    %pause(2);
end

load result_ISM.mat

for j = 1:3
    hrir_sig = conv_hrir(sig_all{j},file_width(j), file_length(j), file_height(j), j); % j is index to collect angle data
    hrir_sig_all {j} = hrir_sig;
%    sound(hrir_sig,Fs);
end  

% Test for listening with HRTF
%To make sure to fit length
% sound(hrir_sig_all{1},Fs);
% sound(hrir_sig_all{2},Fs);
% sound(hrir_sig_all{3},Fs);
% 
% for i=1:3
%     %temp(:,:,i)=cell2mat(result_ISM{i}{:}();
%     tg(:,i) = 10 .^(result_ISM{1,i}{:}(:,7) ./ 20); % Because the gain value has decibel unit when it was calculated.
%     td(:,i) = round(abs(result_ISM{1,i}{:}(:,6) .* Fs));
% end

% for i=1:3
%     temp(:,:,i)=cell2mat(result_ISM{i});
%     tg(:,i) = 10 .^(temp(:,7,i) ./ 20); % Because the gain value has decibel unit when it was calculated.
%     td(:,i) = round(abs(temp(:,6,i) .* Fs));
% end


% tn = length(tg(:,1));

tn = 5; % number of reflections (taps)
tg = [0.8, 0.5, 0.3, 0.25, 0.2]; % vector of gain factors for the 5 reflections
td = [59, 100, 195, 225, 250]; % vector of delay in samples for the 5 reflections

%using example
cg = [0.805, 0.827, 0.783, 0.764];
cd = [901, 778, 1011, 1123];
ag = 0.7;
ad = [125, 42, 12];
k = 0.5;

%using 4 CPU Cores for schroeder calculating because there are 3 loops to
%calculate the schroeder reverberation and 1 is for main code.
tic;    
matlabpool open 4; 
parfor i = 1:3 %3
    final_sig{i}=schroeder(hrir_sig_all{i},tn,10.^(result_ISM{1,i}{:}(:,7) ./ 20),round(abs(result_ISM{1,i}{:}(:,6) .* Fs)),cg,cd,ag,ad,k);
    %final_sig{i}=schroeder(hrir_sig_all{i},tn,tg,td,cg,cd,ag,ad,k);
end
matlabpool close;
toc;

audiowrite('processed_signal1.wav',final_sig{1},Fs);
audiowrite('processed_signal1.wav',final_sig{2},Fs);
audiowrite('processed_signal1.wav',final_sig{3},Fs);

% --- Executes on button press in play_final.
function play_final_Callback(hObject, eventdata, handles)
% hObject    handle to play_final (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global final_sig
sound(final_sig{1},Fs);
sound(final_sig{2},Fs);
sound(final_sig{3},Fs);
