Reply by shanaka nishantha October 8, 20082008-10-08
hi, im new to matlab. im doing speech lie detector project. main part is record, save, load recorded voice from wave file ..etc...

i did recording , stop and paly back parts and wave signal display in plot as well.
but i have problem in saving recorded voice and loading it again and plotting it. can some one pls help me to fix that error..
_______________________________________________

function varargout = Main(varargin)

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

handles.output = hObject;
handles.status=0;
handles.stat=0;
handles.dat=0;
handles.stp=0;
handles.data=0;
handles.FileName='audio1';
handles.samplerateD100;
handles.bitrate;
handles.chanles=1;
set(handles.figure1,'CloseRequestFcn',@closeGUI);
% Update handles structure
guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = Main_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;
% --- Executes on button press in btnRec.
function btnRec_Callback(hObject, eventdata, handles)
if handles.status==0
handles.status=1;
handles.stat=0;
handles.dat=0;
handles.stp=0;
handles.data=1;
handles.r=audiorecorder(handles.samplerate,handles.bitrate,handles.chanles);
record(handles.r);
else
disp('Warning: Recording Is Already In Progress.')
disp(' To Begin A New Recording You Must First Stop The Current Sesion.')
end
guidata(hObject,handles);
% --- Executes on button press in btnStop.
function btnStop_Callback(hObject, eventdata, handles)

if handles.status==1
handles.status=0;
handles.stat=0;
handles.dat=1;
handles.stp=1;
stop(handles.r);
handles.y=getaudiodata(handles.r,'double');
if handles.chanles==2
m2=max(handles.y(:,2));
n2=max(-handles.y(:,2));
n2s(n2);
if m2>n2
p2=m2;
else
p2=n2;
end
scale=0.95/p2;
handles.y(:,2)=scale*handles.y(:,2)
end
m=max(handles.y(:,1));
n=max(-handles.y(:,1));
ns(n);
if m>n
p=m;
else
p=n;
end
scale=0.95/p;
handles.y(:,1)=scale*handles.y(:,1)
plot(handles.y)
else
disp('Warning: To Stop, Recording Must Be In Progress Or Paused.')
end
guidata(hObject,handles);

% --- Executes on button press in btnPlay.
function btnPlay_Callback(hObject, eventdata, handles)
% hObject handle to pl (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%play(handles.r);
if (handles.dat==1)&&((handles.stp==1)||(handles.stat==1))
handles.y=getaudiodata(handles.r,'double');
if handles.chanles==2
m2=max(handles.y(:,2));
n2=max(-handles.y(:,2));
n2s(n2)
if m2>n2
p2=m2;
else
p2=n2;
end
scale=0.95/p2;
handles.y(:,2)=scale*handles.y(:,2)
end
m=max(handles.y(:,1));
n=max(-handles.y(:,1));
ns(n)
if m>n
p=m;
else
p=n;
end
scale=0.95/p;
handles.y(:,1)=scale*handles.y(:,1)
sound(handles.y,handles.samplerate,handles.bitrate);
else if handles.data==0
disp('There Is Currently No Audio Saved In Audio Recorder')
else
disp('Worning: To Play, Recorder Must Be Paused Or Stoped')
end
end
guidata(hObject,handles);
% --- Executes on button press in btnLoad.
function btnLoad_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile('*.wav','Select Data File');

if filename ~= 0

cd(pathname);

% Get data and sampling rate
[data,R_fs] = wavread([pathname filename]);
if min(size(data))>1
error('Can''t load stereo data')
end

end
% --- Executes on button press in btnSave.
function btnSave_Callback(hObject, eventdata, handles)
[filename, pathname] = uiputfile('*.wav', 'Save Data to Wave File');
if filename ~= 0
wavwrite(z_data,hObject,[pathname filename])
end
% --- Executes on button press in btnExit.
function btnExit_Callback(hObject, eventdata, handles)
selection = questdlg('Do you want to Exit?',...
'Lie Detector',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end