基于MATLAB中的GUI设计的钢琴界面设计并能发声

我所使用的MATLAB版本——MATLAB R2017a
MATLAB的GUI的操作其他人写的很清楚了,在此不再赘述。
MATLAB的GUI的基本操作可见:GUI基本操作

这次所设计的钢琴有简单的七个琴键,DO RE MI FA SO LA XI
首先在命令行中输入

guide

在这里插入图片描述
即可调出gui设计页面,选择第一个选项的空白GUI,然后点确定即可
在这里插入图片描述
在工具栏的右侧选择按钮,在界面上画出一长方形代表按键,并用ctrl+c,ctrl+v创造出七个琴键,并对齐排列
在这里插入图片描述
双击按键可打开具体的参数设置,其中有前景色、背景色、字体、标志、类型等,可以更加的个性化。
在这里插入图片描述
其中的STRING代表在GUI界面的名字,tag是生成的代码中的标志(在保存后还会生成一代码文件)
在这里插入图片描述
修改后即可看到第一个琴键的名字为DO,同理我们可以把其余其他按键命名,最后我们可以得到如下图所示:
在这里插入图片描述
在保存之后会自动生成一代码,其中大部分我们不需要关心,我们要关注的就是其中的Do_Callback()等回调函数(Do是我们命名的tag使然),也就是我们按一次按键会执行一次回调函数。

function Do_Callback(hObject, eventdata, handles)
% hObject    handle to Do (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

为了发出钢琴声我们还需要编写一发声函数,具体代码如下:

function y = gen_wave( tone, rythm )

%   音调 拍
    Fs = 8192;
    freqs = [523, 587, 659, 698, 783, 880, 988];
    x = linspace(0, 2 * pi * rythm, floor(Fs * rythm));
   y = sin(freqs(tone) * x) .*(1- x/(rythm * 2 *pi));
    
end

函数具体内容参见我的另一博客:MATLAB如何创造音乐
所以在DO按键的回调函数中应该这样写:

function Do_Callback(hObject, eventdata, handles)
% hObject    handle to Do (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(1,1);
sound(y,8192);

同理在此给出所有代码:

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

% Last Modified by GUIDE v2.5 06-Feb-2020 16:54:20

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = piano_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 Do.
function Do_Callback(hObject, eventdata, handles)
% hObject    handle to Do (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(1,1);
sound(y,8192);

% --- Executes on button press in Re.
function Re_Callback(hObject, eventdata, handles)
% hObject    handle to Re (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(2,1);
sound(y,8192);

% --- Executes on button press in Mi.
function Mi_Callback(hObject, eventdata, handles)
% hObject    handle to Mi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(3,1);
sound(y,8192);

% --- Executes on button press in Fa.
function Fa_Callback(hObject, eventdata, handles)
% hObject    handle to Fa (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(4,1);
sound(y,8192);

% --- Executes on button press in So.
function So_Callback(hObject, eventdata, handles)
% hObject    handle to So (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(5,1);
sound(y,8192);

% --- Executes on button press in La.
function La_Callback(hObject, eventdata, handles)
% hObject    handle to La (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(6,1);
sound(y,8192);

% --- Executes on button press in Xi.
function Xi_Callback(hObject, eventdata, handles)
% hObject    handle to Xi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = gen_wave(7,1);
sound(y,8192);

再结合我们的 gen_wave()函数即可发出音乐声(有时候刚开始可能会有一些延迟。。。)
以上就是用MATLAB的GUI如何设计钢琴界面并能发声的教程了,如果感觉有意思,点个赞,O(∩_∩)O哈哈~

参考博客:GUI基本知识
gen_wave函数设计

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章