Midterm Project

Introduction

Create a graphical user interface (GUI) to process the music signal easily. There are two sliders that user can use them to choose the interval which we want to process, one axes that shows signals ( magnitude versus time) , and eight buttons that do processing such as playing/stopping music , making music loud/soft/fast/slow/ reverse, and clear the waveform view.

If the start time is after finish time,the signal will reverse. Thus, start time being after finish time and pressing Reverse button will make music as same as initial.

If we don't press Clear button after pressing others many times, there will be a stack of signals in the view. It looks mess sometimes, but we can compare with signals.

Music

Demo Video

Code

function varargout = player(varargin)

%PLAYER MATLAB code file for player.fig

% PLAYER, by itself, creates a new PLAYER or raises the existing

% singleton*.

%

% H = PLAYER returns the handle to a new PLAYER or the handle to

% the existing singleton*.

%

% PLAYER('Property','Value',...) creates a new PLAYER using the

% given property value pairs. Unrecognized properties are passed via

% varargin to player_OpeningFcn. This calling syntax produces a

% warning when there is an existing singleton*.

%

% PLAYER('CALLBACK') and PLAYER('CALLBACK',hObject,...) call the

% local function named CALLBACK in PLAYER.M with the given input

% arguments.

%

% *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 player


% Last Modified by GUIDE v2.5 08-May-2019 00:57:08


% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name', mfilename, ...

'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @player_OpeningFcn, ...

'gui_OutputFcn', @player_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 player is made visible.

function player_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 unrecognized PropertyName/PropertyValue pairs from the

% command line (see VARARGIN)


% initial view

axes(handles.axes1);

title('Waveform');

xlabel('Time(s)');

ylabel('Magnitude');

grid on;hold on;



% Choose default command line output for player

handles.output = hObject;


% Update handles structure

guidata(hObject, handles);


% UIWAIT makes player wait for user response (see UIRESUME)

% uiwait(handles.figure1);



% --- Outputs from this function are returned to the command line.

function varargout = player_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;



%slider1:start time

% --- Executes on slider movement.

function slider1_Callback(hObject, eventdata, handles)

% hObject handle to slider1 (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,'Value') returns position of slider

% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


%save and show slider's value

a = get(handles.slider1, 'Value');

set(handles.text2, 'String', a);



% --- 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.

if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

set(hObject,'BackgroundColor',[.9 .9 .9]);

end



%slider2:finish time

% --- Executes on slider movement.

function slider2_Callback(hObject, eventdata, handles)

% hObject handle to slider2 (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,'Value') returns position of slider

% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


%save and show slider's value

a = get(handles.slider2, 'Value');

set(handles.text3, 'String', a);



% --- 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.

if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

set(hObject,'BackgroundColor',[.9 .9 .9]);

end



%play

% --- 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)


%read and play .mp3

[y1 fs1]=audioread('豆豆龍.mp3');

t0 = get(handles.slider1, 'Value');

t1= get(handles.slider2, 'Value');

t0=length(y1)*t0/203;

t1=length(y1)*t1/203;

if t0>t1

yy=y1(t1:t0);

yy=flipud(yy');

else

yy=y1(t0:t1);

end

sound(yy,fs1);


%draw the signal

axes(handles.axes1);

if t0>t1

t=(0:t0-t1)/fs1;

else

t=(0:t1-t0)/fs1;

end

plot(t,yy);

grid on;hold on;



%stop

% --- 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)


%stop the music

clear sound;



%volume +

% --- 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)

[y1 fs1]=audioread('豆豆龍.mp3');

t0 = get(handles.slider1, 'Value');

t1= get(handles.slider2, 'Value');

t0=length(y1)*t0/203;

t1=length(y1)*t1/203;

if t0>t1

yy=y1(t1:t0);

yy=flipud(yy');

else

yy=y1(t0:t1);

end

sound(3*yy,fs1);


%draw the signal

axes(handles.axes1);

if t0>t1

t=(0:t0-t1)/fs1;

else

t=(0:t1-t0)/fs1;

end

plot(t,3*yy);

grid on;hold on;



%volume -

% --- 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)

[y1 fs1]=audioread('豆豆龍.mp3');

t0 = get(handles.slider1, 'Value');

t1= get(handles.slider2, 'Value');

t0=length(y1)*t0/203;

t1=length(y1)*t1/203;

if t0>t1

yy=y1(t1:t0);

yy=flipud(yy');

else

yy=y1(t0:t1);

end

sound(0.5*yy,fs1);


%draw the signal

axes(handles.axes1);

if t0>t1

t=(0:t0-t1)/fs1;

else

t=(0:t1-t0)/fs1;

end

plot(t,0.5*yy);

grid on;hold on;



%fast

% --- 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)

[y1 fs1]=audioread('豆豆龍.mp3');

t0 = get(handles.slider1, 'Value');

t1= get(handles.slider2, 'Value');

t0=length(y1)*t0/203;

t1=length(y1)*t1/203;

if t0>t1

yy=y1(t1:t0);

yy=flipud(yy');

else

yy=y1(t0:t1);

end

sound(yy,3*fs1);


%draw the signal

axes(handles.axes1);

if t0>t1

t=(0:t0-t1)/(3*fs1);

else

t=(0:t1-t0)/(3*fs1);

end

plot(t,yy);

grid on;hold on;



%slow

% --- Executes on button press in pushbutton6.

function pushbutton6_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton6 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

[y1 fs1]=audioread('豆豆龍.mp3');

t0 = get(handles.slider1, 'Value');

t1= get(handles.slider2, 'Value');

t0=length(y1)*t0/203;

t1=length(y1)*t1/203;

if t0>t1

yy=y1(t1:t0);

yy=flipud(yy');

else

yy=y1(t0:t1);

end

sound(yy,0.5*fs1);


%draw the signal

axes(handles.axes1);

if t0>t1

t=(0:t0-t1)/(0.5*fs1);

else

t=(0:t1-t0)/(0.5*fs1);

end

plot(t,yy);

grid on;hold on;


%reverse

% --- Executes on button press in pushbutton7.

function pushbutton7_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton7 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

[y1 fs1]=audioread('豆豆龍.mp3');

t0 = get(handles.slider1, 'Value');

t1= get(handles.slider2, 'Value');

t0=length(y1)*t0/203;

t1=length(y1)*t1/203;

if t0>t1

yy=y1(t1:t0);

yy=flipud(yy');

else

yy=(y1(t0:t1))';

end

sound(flipud(yy),fs1);


%draw the signal

axes(handles.axes1);

if t0>t1

t=(0:t0-t1)/fs1;

else

t=(0:t1-t0)/fs1;

end

plot(t,flipud(yy));

grid on;hold on;



%clear

% --- Executes on button press in pushbutton8.

function pushbutton8_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton8 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)


%clear view

hold off;

plot(0);

title('Waveform');

xlabel('Time(s)');

ylabel('Magnitude');

grid on;hold on;