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

% Last Modified by GUIDE v2.5 21-Jan-2007 15:52:26

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @programa_separa_OpeningFcn, ...
                   'gui_OutputFcn',  @programa_separa_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 programa_separa is made visible.
function programa_separa_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 programa_separa (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

clear all;

% --- Outputs from this function are returned to the command line.
function varargout = programa_separa_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 during object creation, after setting all properties.
function edit_s1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_s1 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function edit_s2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_s2 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_s2_Callback(hObject, eventdata, handles)
% hObject    handle to edit_s2 (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 edit_s2 as text
%        str2double(get(hObject,'String')) returns contents of edit_s2 as a double

% La ruta y nombre del segundo .wav fuente sera fuente2
global fuente2;
fuente2=get(gcbo,'String');
% Activamos el radioboton de 2 fuentes y desactivamos el de 3
set(handles.radioGenera2Fuentes,'Value',1);
set(handles.radioGenera3Fuentes,'Value',0);
set(handles.radioCarga,'Value',0);
set(handles.edit_x,'String','');

% --- Executes during object creation, after setting all properties.
function edit_s3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_s3 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_s3_Callback(hObject, eventdata, handles)
% hObject    handle to edit_s3 (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 edit_s3 as text
%        str2double(get(hObject,'String')) returns contents of edit_s3 as a double

% La ruta y nombre del tercer .wav fuente sera fuente3
global fuente3;
fuente3=get(gcbo,'String');

% Activamos el radioboton de 3 fuentes y desactivamos el de 2
set(handles.radioGenera2Fuentes,'Value',0);
set(handles.radioGenera3Fuentes,'Value',1);
set(handles.radioCarga,'Value',0);
set(handles.edit_x,'String','');

% --- Executes on button press in radioGenera2Fuentes.
function radioGenera2Fuentes_Callback(hObject, eventdata, handles)
% hObject    handle to radioGenera2Fuentes (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 radioGenera2Fuentes

% Cuando se activa este boton los otros se desactivan
set(handles.radioGenera2Fuentes,'Value',1);
set(handles.radioGenera3Fuentes,'Value',0);
set(handles.radioCarga,'Value',0);
set(handles.edit_x,'String','');

% --- Executes on button press in radioGenera3Fuentes.
function radioGenera3Fuentes_Callback(hObject, eventdata, handles)
% hObject    handle to radioGenera3Fuentes (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 radioGenera3Fuentes

% Cuando se activa este boton los otros se desactivan
set(handles.radioGenera2Fuentes,'Value',0);
set(handles.radioGenera3Fuentes,'Value',1);
set(handles.radioCarga,'Value',0);
set(handles.edit_x,'String','');

% --- Executes during object creation, after setting all properties.
function edit_x_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_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: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_x_Callback(hObject, eventdata, handles)
% hObject    handle to edit_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,'String') returns contents of edit_x as text
%        str2double(get(hObject,'String')) returns contents of edit_x as a double

% La ruta y nombre del .wav que contiene la mezcla sera mezcla_x
global mezcla_x;
mezcla_x=get(gcbo,'String');

% Activamos el radioboton adecuado y desactivamos los otros 2
set(handles.radioGenera2Fuentes,'Value',0);
set(handles.radioGenera3Fuentes,'Value',0);
set(handles.radioCarga,'Value',1);

% Borro los campos correspondientes
set(handles.edit_s1,'String','');
set(handles.edit_s2,'String','');
set(handles.edit_s3,'String','');

% --- Executes on button press in radioCarga.
function radioCarga_Callback(hObject, eventdata, handles)
% hObject    handle to radioCarga (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 radioCarga

% Cuando se activa este boton los otros se desactivan
set(handles.radioGenera2Fuentes,'Value',0);
set(handles.radioGenera3Fuentes,'Value',0);
set(handles.radioCarga,'Value',1);
set(handles.edit_s1,'String','');
set(handles.edit_s2,'String','');
set(handles.edit_s3,'String','');

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

% Cargamos las voces en s1,s3,s3
global s fs fuente1 fuente2 fuente3 n;
[s1,fs]=wavread(fuente1);
s2=wavread(fuente2);

if get(handles.radioGenera2Fuentes,'Value')
    if length(s1)<length(s2)
        s2=s2(1:length(s1),1);
    else
        s1=s1(1:length(s2),1);
    end
    s=[s1';s2'];
    set(handles.reproducir_s3,'Enable','off');
    set(handles.specgram_s3,'Enable','off');
    set(handles.grafica3,'Visible','off');
    n=2;
else
    global fuente3;
    s3=wavread(fuente3);
    if length(s1)<length(s2)
        if length(s1)<length(s3)
            s2=s2(1:length(s1),1);
            s3=s3(1:length(s1),1);
        else
            s1=s1(1:length(s3),1);
            s2=s2(1:length(s3),1);
        end
    else
        if length(s2)<length(s3)
            s1=s1(1:length(s2),1);
            s3=s3(1:length(s2),1);
        else
            s1=s1(1:length(s3),1);
            s2=s2(1:length(s3),1);
        end
    end
    set(handles.reproducir_s3,'Enable','on');
    set(handles.specgram_s3,'Enable','on');
    set(handles.grafica3,'Visible','on');
    s=[s1';s2';s3'];
    n=3;
end

% Habilito y deshabilito los botones correspondientes
set(handles.reproducir_s1,'Enable','on');
set(handles.reproducir_s2,'Enable','on');
set(handles.specgram_s1,'Enable','on');
set(handles.specgram_s2,'Enable','on');
set(handles.edit_x,'Enable','off');
set(handles.radioCarga,'Enable','off');
set(handles.cargarMezcla,'Enable','off');

% Ahora las graficas
tiempo=(1/fs)*[1:length(s(1,:))];
set(handles.grafica1,'Visible','on');
axes(handles.grafica1);
plot(tiempo,s(1,:));
set(handles.grafica2,'Visible','on');
axes(handles.grafica2);
plot(tiempo,s(2,:));
if get(handles.radioGenera3Fuentes,'Value')
    axes(handles.grafica3);
    plot(tiempo,s(3,:));
end


% --- Executes on button press in cargarMezcla.
function cargarMezcla_Callback(hObject, eventdata, handles)
% hObject    handle to cargarMezcla (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global s fs;
% Deshabilito los botones correspondientes y habilito los demas
set(handles.reproducir_s1,'Enable','on');
set(handles.specgram_s1,'Enable','on');
set(handles.reproducir_s2,'Enable','on');
set(handles.specgram_s2,'Enable','on');
set(handles.reproducir_s3,'Enable','off');
set(handles.specgram_s3,'Enable','off');
set(handles.radioGenera2Fuentes,'Enable','off');
set(handles.radioGenera3Fuentes,'Enable','off');
set(handles.cargarGenerar,'Enable','off');
set(handles.radioCarga,'Value',1);

% Ahora las graficas
set(handles.grafica1,'Visible','on');
set(handles.grafica2,'Visible','on');
set(handles.grafica3,'Visible','off');
tiempo=(1/fs)*[1:length(s(1,:))];
axes(handles.grafica1);
plot(tiempo,s(1,:));
axes(handles.grafica2);
plot(tiempo,s(2,:));

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

global s fs;
wavplay(s(1,:),fs);

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

global s;
specgramdemo(s(1,:));

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

global s fs;
wavplay(s(2,:),fs);


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

global s fs;
wavplay(s(3,:),fs);

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

global s;
specgramdemo(s(3,:));

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

global s;
specgramdemo(s(2,:));

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

close(gcbf);

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

global s fs n;
if get(handles.radioGenera2Fuentes,'Value')==1 || get(handles.radioGenera3Fuentes,'Value')==1
    parametros_simulacion(s,fs,n);
elseif get(handles.radioCarga,'Value')==1
    n=length(s(1,:));
    parametros_separacion(s,fs,n);
end
close(gcbf);


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_s3 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit_s3 (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 edit_s3 as text
%        str2double(get(hObject,'String')) returns contents of edit_s3 as a double


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

global fuente1;
[file,path]=uigetfile('*.wav','Abrir archivo .wav');
fuente1=[path,file];
set(handles.radioCarga,'Value',0);
set(handles.edit_x,'String','');
set(handles.edit_s1,'String',file);

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

global fuente2;
[file,path]=uigetfile('*.wav','Abrir archivo .wav');
fuente2=[path,file];
set(handles.radioGenera2Fuentes,'Value',1);
set(handles.radioGenera3Fuentes,'Value',0);
set(handles.radioCarga,'Value',0);
set(handles.edit_x,'String','');
set(handles.edit_s2,'String',file);

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

global fuente3;
[file,path]=uigetfile('*.wav','Abrir archivo .wav');
fuente3=[path,file];
set(handles.radioGenera2Fuentes,'Value',0);
set(handles.radioGenera3Fuentes,'Value',1);
set(handles.radioCarga,'Value',0);
set(handles.edit_x,'String','');
set(handles.edit_s3,'String',file);


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

global mezcla_x s fs;
[file,path]=uigetfile({'*.wav;*.mat'},'Abrir archivo .wav');
aux=flipud(file')';
set(handles.edit_x,'String',file);
% Si es un fichero .wav
if aux(1:3)=='vaw'
    mezcla_x=[path,file];
    [s,fs]=wavread(mezcla_x);
    s=s';
else
    % Es un fichero .mat
    aux=[path,file];
    load(aux);
    s=x;
end
