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

% Last Modified by GUIDE v2.5 08-Sep-2005 20:03:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @demo_speech_OpeningFcn, ...
                   'gui_OutputFcn',  @demo_speech_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 demo_speech is made visible.
function demo_speech_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 demo_speech (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = demo_speech_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 selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global d_var;
soundsc(d_var.wav,d_var.ofs);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global d_var;
soundsc(d_var.sp,d_var.ofs);


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global d_var;
soundsc(d_var.np,d_var.ofs);


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

% refresh button, load the list of audio files into the popup menu
wavfiles=dir('*.wav');
for cnt=1:size(wavfiles,1)
    current{cnt}=wavfiles(cnt).name;
end
aufiles=dir('*.au');
for cnt=1:size(aufiles,1)
    current{cnt+size(aufiles,1)}=aufiles(cnt).name;
end
mpfiles=dir('*.mp3');
for cnt=1:size(mpfiles,1)
    current{cnt+size(aufiles,1)}=mpfiles(cnt).name;
end
if isempty(current) current=' ';end
set(handles.popupmenu1,'String',current);
handles.soundpath=cd;
guidata(gcbf,handles);


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

% run button, detect speech

global d_var;
soundfile=get(handles.popupmenu1,'String');
soundfile=soundfile{get(handles.popupmenu1,'Value')};
if soundfile(end-2:end)=='wav'
    [d_var.wav,d_var.ofs]=wavread([handles.soundpath '/' soundfile]);
elseif soundfile(end-2:end)=='mp3'
    [d_var.wav,d_var.ofs]=mp3read([handles.soundpath '/' soundfile]);
else
    [d_var.wav,d_var.ofs]=auread([handles.soundpath '/' soundfile]);
end
max_length=str2num(getfield(get(handles.edit1),'String'));
d_var.wav=d_var.wav(1:min(length(d_var.wav),max_length*d_var.ofs));
set(hObject,'String','Busy');
set(hObject,'ForegroundColor',[1 0 0]);
drawnow;
tic;
[d_var.res, d_var.sp, d_var.np]=speechdetector(d_var.wav,d_var.ofs,250,10);
toc;
set(hObject,'String','Run!');
set(hObject,'ForegroundColor',[0 0 0]);
drawnow;
if isempty(d_var.wav) d_var.wav=0;end
if isempty(d_var.sp) d_var.sp=0;end
if isempty(d_var.np) d_var.np=0;end

subplot(handles.axes1);s=spectrogram(d_var.wav,256);
hold off;
imagesc(log(abs(s)+0.1));axis xy;
hold on;
t1=d_var.res;
f=size(s,2)/length(t1);
t2=repmat(t1(:),[1 f])';
plot(t2(:)*size(s,1)/2,'g.');
subplot(handles.axes4);s=spectrogram(d_var.sp,256);
imagesc(log(abs(s)+0.1));axis xy;
subplot(handles.axes5);s=spectrogram(d_var.np,256);
imagesc(log(abs(s)+0.1));axis xy;

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


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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


