
%--------------------------------------------------------------------
% ------------- Código de inicialización del GUI --------------------
function varargout = DigitalReverb2(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DigitalReverb2_OpeningFcn, ...
                   'gui_OutputFcn',  @DigitalReverb2_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 DigitalReverb2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);

function varargout = DigitalReverb2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;

% ----------- Fin del Código de inicialización del GUI --------------
%--------------------------------------------------------------------


%---- Descripción de las funciones de cada elemento del GUI.
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

%--------------------------------------------------------------------
%---- Cuadro de texto "Archivo de sonido" ----
function songntxt_Callback(hObject, eventdata, handles)
% get(hObject,'String') returns contents of songntxt as text       
audiofile=get(hObject,'String');
handles.audiodata.audiofile = audiofile;
guidata(hObject,handles);
%--------------------------------------------------------------------

%--------------------------------------------------------------------
%---- Cuadro de texto "Tiempo de reverberación" ----
function RT60_Callback(hObject, eventdata, handles)
% Convierte el contenido de RT60 en numero (double)
rt = str2double(get(hObject, 'String'));
if isnan(rt)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.audiodata.rt = rt;
guidata(hObject,handles);
%--------------------------------------------------------------------

%--------------------------------------------------------------------
%---- Cuadro de texto "Archivo de destino" ----
function destext_Callback(hObject, eventdata, handles)

audiofile2=get(hObject,'String');
handles.audiodata.audiofile2 = audiofile2;
guidata(hObject,handles);
%--------------------------------------------------------------------

%--------------------------------------------------------------------
% --- BOTON "Reproducir Original"----
function playortxt_Callback(hObject, eventdata, handles)

audiofile=handles.audiodata.audiofile;
[x,Fs,nbits]=wavread(audiofile);
wavplay(x,Fs);
guidata(hObject,handles);
%--------------------------------------------------------------------

%--------------------------------------------------------------------
% --- BOTON "Transformar"----.
function transformar_Callback(hObject, eventdata, handles)

set(handles.estado,'BackgroundColor',[.9 .2 .2]);
set(handles.estado,'String','Ocupado');

audiofile=handles.audiodata.audiofile;

[X,Fs,nbits]=wavread(audiofile);
siz=int32(wavread(audiofile,'size'));
siz2= siz(:,1);

% Early delay medido desde el inicio del sonido directo
ED_input=[0.006;0.013;0.017;0.023;0.029;0.031];
EG_input=[0.7;0.8;0.6;0.65;0.5;0.4];

% Obtener en la variable Y_ER el resultado de las reflexiones tempranas
Y_ER=earlyreflect(X,Fs,siz2,ED_input,EG_input);

%Ganancia del sonido directo (Direct Sound)
GDS=handles.audiodata.vol1;
%Ganancia de las reflexiones tempranas (Early Reflections)
GER=handles.audiodata.vol2;
%Tiempo de reverberación de las reflexiones tardías
RT=handles.audiodata.rt;
%Ganancia de la reverberacion tardía (Late Reverb)
GLR=handles.audiodata.vol3;

% Obtener en la variable Y_LR el resultado de las reflexiones tardías
Y_LR=latereverb(X,Fs,siz2,RT);

% Calcular resultado Final:
Y = [GDS*X(:,1) GDS*X(:,1)]+ GER*Y_ER + GLR*Y_LR;

handles.audiodata.freq = Fs;
handles.audiodata.reverb = Y;

set(handles.estado,'BackgroundColor',[.2 .9 .2]);
set(handles.estado,'String','Listo');

guidata(hObject,handles);
%--------------------------------------------------------------------

%--------------------------------------------------------------------
% --- BOTON "Reproducir"----
function reproducir_Callback(hObject, eventdata, handles)

y=handles.audiodata.reverb;
Fs= handles.audiodata.freq;
wavplay(y,Fs);
guidata(hObject,handles);
%--------------------------------------------------------------------

%--------------------------------------------------------------------
% --- BOTON "Grabar"----
function grabar_Callback(hObject, eventdata, handles)

audiofile2=handles.audiodata.audiofile2;
y=handles.audiodata.reverb;
Fs= handles.audiodata.freq;
wavwrite(y,Fs,audiofile2);
guidata(hObject,handles);
%--------------------------------------------------------------------


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

a= get(hObject,'Value');
b=round(a*100)/100;
set(handles.vol1,'String',b);
handles.audiodata.vol1 = a;
guidata(hObject,handles); 
%--------------------------------------------------------------------
%--------------------------------------------------------------------
% --- 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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.3 .3 .3]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
%--------------------------------------------------------------------

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

a= get(hObject,'Value');
b=round(a*100)/100;
set(handles.vol2,'String',b);
handles.audiodata.vol2 = a;
guidata(hObject,handles); 
%--------------------------------------------------------------------

%--------------------------------------------------------------------
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.4 .4 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
%--------------------------------------------------------------------





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

a= get(hObject,'Value');
b=round(a*100)/100;
set(handles.vol3,'String',b);
handles.audiodata.vol3 = a;
guidata(hObject,handles); 


% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider3 (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
handles.audiodata.rt=0;
handles.audiodata.vol1=0;
handles.audiodata.vol2=0;
handles.audiodata.vol3=0;
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .4 .4]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
guidata(hObject,handles); 




% --- Executes on button press in previsualizar.
function previsualizar_Callback(hObject, eventdata, handles)

%Lineas para medir la respuesta al impulso 
%----------------------------------------------------

 Fs = 44100 ;%44000 muestras por segundo
 Ts = 1/Fs; % cada muestra dura 1/44100 = 0.000022675 segundos
 Duracion = handles.audiodata.rt; %segundos
 siz = int32(Duracion*Fs);
 X=[1;zeros(siz-1,1)];
 
 
HD= int32(0.0008*Fs); %Head Delay: Retardo de la funcion de transferencia de la cabeza
%----------Definicion del Vector tiempo-----------
t=double(zeros(siz,1));
t(1)=1/Fs;
for i=1:siz-1
    t(i+1)=t(i)+t(1);
end

% Early delay medido desde el inicio del sonido directo
ED_input=[0.006;0.013;0.017;0.023;0.029;0.031];
EG_input=[0.7;0.8;0.6;0.65;0.5;0.4];

% Obtener en la variable Y_ER el resultado de las reflexiones tempranas
Y_ER=earlyreflect(X,Fs,siz,ED_input,EG_input);

%Ganancia del sonido directo (Direct Sound)
GDS=handles.audiodata.vol1;
%Ganancia de las reflexiones tempranas (Early Reflections)
GER=handles.audiodata.vol2;
%Tiempo de reverberación de las reflexiones tardías
RT=handles.audiodata.rt;
%Ganancia de la reverberacion tardía (Late Reverb)
GLR=handles.audiodata.vol3;

% Obtener en la variable Y_LR el resultado de las reflexiones tardías
Y_LR=latereverb(X,Fs,siz,RT);

t0=0;

% Graficar los resultados parciales
plot(handles.grafica,t0,GDS*X(1,1),'k*',t,GER*Y_ER(:,1),'b',t,abs(GLR*Y_LR(:,1)),'r');

plot(handles.graficart,t,abs(GLR*Y_LR(:,1)),'m');
set(handles.graficart,'XGrid','on');
set(handles.graficart,'YGrid','on');
set(handles.graficart,'YScale','log');

set(handles.estado,'BackgroundColor',[.5 1 1]);
set(handles.estado,'String','En Espera');

%-----FIN-----

