基於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函數設計

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