Created
October 11, 2013 05:17
-
-
Save zhezheng/6929916 to your computer and use it in GitHub Desktop.
A MATLAB Audio Player
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function varargout = audio(varargin) | |
% AUDIO MATLAB code for audio.fig | |
% AUDIO, by itself, creates a new AUDIO or raises the existing | |
% singleton*. | |
% | |
% H = AUDIO returns the handle to a new AUDIO or the handle to | |
% the existing singleton*. | |
% | |
% AUDIO('CALLBACK',hObject,eventData,handles,...) calls the local | |
% function named CALLBACK in AUDIO.M with the given input arguments. | |
% | |
% AUDIO('Property','Value',...) creates a new AUDIO or raises the | |
% existing singleton*. Starting from the left, property value pairs are | |
% applied to the GUI before audio_OpeningFcn gets called. An | |
% unrecognized property name or invalid value makes property application | |
% stop. All inputs are passed to audio_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 audio | |
% Last Modified by GUIDE v2.5 07-Feb-2012 20:31:42 | |
% Begin initialization code - DO NOT EDIT | |
gui_Singleton = 1; | |
gui_State = struct('gui_Name', mfilename, ... | |
'gui_Singleton', gui_Singleton, ... | |
'gui_OpeningFcn', @audio_OpeningFcn, ... | |
'gui_OutputFcn', @audio_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 audio is made visible. | |
function audio_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 audio (see VARARGIN) | |
% Choose default command line output for audio | |
handles.output = hObject; | |
% Update handles structure | |
guidata(hObject, handles); | |
% UIWAIT makes audio wait for user response (see UIRESUME) | |
% uiwait(handles.figure1); | |
% --- Outputs from this function are returned to the command line. | |
function varargout = audio_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 button press in btn_play. | |
function btn_play_Callback(hObject, eventdata, handles) | |
% hObject handle to btn_play (see GCBO) | |
% eventdata reserved - to be defined in a future version of MATLAB | |
% handles structure with handles and user data (see GUIDATA) | |
set(handles.btn_play, 'Enable', 'off'); | |
set(handles.btn_stop, 'Enable', 'on'); | |
set(handles.btn_pause, 'Enable', 'on'); | |
set(handles.btn_play1, 'Enable', 'off'); | |
state = getappdata(handles.main_form, 'state'); | |
if strcmp(state, 'pause') | |
disp('Resuming from previous pause'); | |
setappdata(handles.main_form, 'state', 'play'); | |
uiresume; | |
return; | |
end | |
setappdata(handles.main_form, 'state', 'play'); | |
blk_size = 2048; | |
fname = get(handles.edit_fname, 'String') | |
[x, fs] = wavread(fname); | |
x = mean(x, 2); | |
x_len = length(x); | |
axes(handles.main_form); | |
cla; | |
x_blk_save_1 = 0; | |
x_blk_save_2 = 0; | |
x_blk_save_3 = 0; | |
msound('openwrite', 0, fs, blk_size, 1); | |
try | |
for idx = 1:blk_size:x_len | |
state = getappdata(handles.main_form, 'state'); | |
if strcmp(state, 'stop') | |
break; | |
elseif strcmp(state, 'pause') | |
uiwait; | |
state = getappdata(handles.main_form, 'state'); | |
if strcmp(state, 'stop') | |
break; | |
end | |
end | |
x_blk = x(idx:idx+blk_size-1); | |
msound('putsamples', x_blk); | |
x_blk_save_3 = x_blk_save_2; | |
x_blk_save_2 = x_blk_save_1; | |
x_blk_save_1 = x_blk; | |
plot(x_blk_save_3); | |
% set(handles.main_form,... | |
% 'XTick', [], 'XTickMode', 'manual',... | |
% 'YTick', [], 'YTickMode', 'manual',... | |
% 'XLim', [0, blk_size-1], 'XLimMode', 'manual',... | |
% 'YLim', [-1, 1], 'YLimMode', 'manual'... | |
% ); | |
ylim('manual'); ylim([-1, 1]); | |
drawnow; | |
end | |
msound('close'); | |
catch | |
msound('close'); | |
rethrow(lasterror); | |
end | |
set(handles.btn_play, 'Enable', 'on'); | |
set(handles.btn_pause, 'Enable', 'off'); | |
set(handles.btn_stop, 'Enable', 'off'); | |
set(handles.btn_play1, 'Enable', 'on'); | |
% --- Executes on button press in btn_pause. | |
function btn_pause_Callback(hObject, eventdata, handles) | |
% hObject handle to btn_pause (see GCBO) | |
% eventdata reserved - to be defined in a future version of MATLAB | |
% handles structure with handles and user data (see GUIDATA) | |
setappdata(handles.main_form, 'state', 'pause'); | |
set(handles.btn_pause, 'Enable', 'off'); | |
set(handles.btn_play, 'Enable', 'on'); | |
set(handles.btn_play1, 'Enable', 'on'); | |
% --- Executes on button press in btn_stop. | |
function btn_stop_Callback(hObject, eventdata, handles) | |
% hObject handle to btn_stop (see GCBO) | |
% eventdata reserved - to be defined in a future version of MATLAB | |
% handles structure with handles and user data (see GUIDATA) | |
% state = getappdata(handles.main_form, 'state'); | |
setappdata(handles.main_form, 'state', 'stop'); | |
% if strcmp(state, 'pause') | |
uiresume; | |
% end | |
set(handles.btn_stop, 'Enable', 'off'); | |
set(handles.btn_pause, 'Enable', 'off'); | |
set(handles.btn_play, 'Enable', 'on'); | |
set(handles.btn_play1, 'Enable', 'on'); | |
function edit_fname_Callback(hObject, eventdata, handles) | |
% hObject handle to edit_fname (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_fname as text | |
% str2double(get(hObject,'String')) returns contents of edit_fname as a double | |
% --- Executes during object creation, after setting all properties. | |
function edit_fname_CreateFcn(hObject, eventdata, handles) | |
% hObject handle to edit_fname (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 use_wnd_Callback(hObject, eventdata, handles) | |
% hObject handle to use_wnd (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 use_wnd as text | |
% str2double(get(hObject,'String')) returns contents of use_wnd as a double | |
% --- Executes during object creation, after setting all properties. | |
function use_wnd_CreateFcn(hObject, eventdata, handles) | |
% hObject handle to use_wnd (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 Untitled_1_Callback(hObject, eventdata, handles) | |
% hObject handle to Untitled_1 (see GCBO) | |
% eventdata reserved - to be defined in a future version of MATLAB | |
% handles structure with handles and user data (see GUIDATA) | |
% -------------------------------------------------------------------- | |
function Untitled_2_Callback(hObject, eventdata, handles) | |
% hObject handle to Untitled_2 (see GCBO) | |
% eventdata reserved - to be defined in a future version of MATLAB | |
% handles structure with handles and user data (see GUIDATA) | |
% --- Executes on button press in btn_play1. | |
function btn_play1_Callback(hObject, eventdata, handles) | |
% hObject handle to btn_play1 (see GCBO) | |
% eventdata reserved - to be defined in a future version of MATLAB | |
% handles structure with handles and user data (see GUIDATA) | |
set(handles.btn_play, 'Enable', 'off'); | |
set(handles.btn_play1, 'Enable', 'off'); | |
set(handles.btn_stop, 'Enable', 'on'); | |
set(handles.btn_pause, 'Enable', 'on'); | |
state = getappdata(handles.main_form, 'state'); | |
if strcmp(state, 'pause') | |
disp('Resuming from previous pause'); | |
setappdata(handles.main_form, 'state', 'play'); | |
uiresume; | |
return; | |
end | |
setappdata(handles.main_form, 'state', 'play'); | |
blk_size = 2048; | |
fname = get(handles.edit_fname, 'String') | |
[x, fs] = wavread(fname); | |
x = mean(x, 2); | |
x_len = length(x); | |
axes(handles.main_form); | |
cla; | |
x_blk_save_1 = 0; | |
x_blk_save_2 = 0; | |
x_blk_save_3 = 0; | |
msound('openwrite', 0, fs, blk_size, 1); | |
try | |
for idx = 1:blk_size:x_len | |
state = getappdata(handles.main_form, 'state'); | |
if strcmp(state, 'stop') | |
break; | |
elseif strcmp(state, 'pause') | |
uiwait; | |
state = getappdata(handles.main_form, 'state'); | |
if strcmp(state, 'stop') | |
break; | |
end | |
end | |
x_blk = x(idx:idx+blk_size-1); | |
msound('putsamples', x_blk); | |
x_blk_save_3 = x_blk_save_2; | |
x_blk_save_2 = x_blk_save_1; | |
x_blk_save_1 = freqz(x_blk); | |
bar(x_blk_save_3,0.7); | |
% plot(x_blk_save_3); | |
% set(handles.main_form,... | |
% 'XTick', [], 'XTickMode', 'manual',... | |
% 'YTick', [], 'YTickMode', 'manual',... | |
% 'XLim', [0, blk_size-1], 'XLimMode', 'manual',... | |
% 'YLim', [-1, 1], 'YLimMode', 'manual'... | |
% ); | |
ylim('manual'); ylim([0, 40]); | |
xlim('manual'); xlim([0, 14]); | |
drawnow; | |
end | |
msound('close'); | |
catch | |
msound('close'); | |
rethrow(lasterror); | |
end | |
set(handles.btn_play, 'Enable', 'on'); | |
set(handles.btn_pause, 'Enable', 'off'); | |
set(handles.btn_stop, 'Enable', 'off'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment