【数字信号】基于GUI可编程电音合成器【Matlab 872期】
作者:互联网
一、案例简介
本文基于MATLAB及其GUI界面设计了一个基可编程电音合成器软件。利用MATLAB的GUI控件及相关算法,实现了多种音色电子音的合成,做到了音长、音色、音调可控和显示的多样化。 软件首先设计了Synthesizer界面,将音频的音频显示坐标、频域显示坐标、数据输入输出界面集成到一起,实现电音合成与播放的基本功能。
二、部分源代码
function varargout = MyPiano(varargin)
% MYPIANO MATLAB code for MyPiano.fig
% MYPIANO, by itself, creates a new MYPIANO or raises the existing
% singleton*.
%
% H = MYPIANO returns the handle to a new MYPIANO or the handle to
% the existing singleton*.
%
% MYPIANO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MYPIANO.M with the given input arguments.
%
% MYPIANO('Property','Value',...) creates a new MYPIANO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MyPiano_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MyPiano_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 MyPiano
% Last Modified by GUIDE v2.5 19-Apr-2018 20:58:34
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MyPiano_OpeningFcn, ...
'gui_OutputFcn', @MyPiano_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 MyPiano is made visible.
function MyPiano_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 MyPiano (see VARARGIN)
% Choose default command line output for MyPiano
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MyPiano wait for user response (see UIRESUME)
% uiwait(handles.figure1);
clc;
set(gcf,'name','Synthesizer ');
global signal;
signal.x=[];
signal.fs=8000;
signal.noteamp = 1;
signal.dur=0.6; % 时间默认为0.6s
signal.h_t=[]; % 时域图形句柄
signal.h_f=[]; % 频域图形句柄
signal.color=[0,0,1]; % 颜色默认为蓝
signal.linestyle = '-';
signal.linewidth = 0.5;
signal.marker = 'none';
signal.markeredgecolor = 'auto';
signal.markerfacecolor = 'none';
signal.markersize = 6;
global control;
control.notegroup = 4; % 默认是小字一组
control.timber = '4seg_envelopes_sine'; % 默认是 4seg_envelopes_sine
% 新窗口变量
global midisignal;
midisignal.fs=44100;
midisignal.y=[];
midisignal.len=[];
midisignal.t=[];
midisignal.filename=[];
midisignal.timber = 4; %默认用四段包络正弦波 4seg_envelopes_sine
% 初始化操作
set(handles.edit_notegroup,'string','小字一组');
set(handles.edit_tone,'string',control.timber);
set(handles.edit_turation,'string',num2str(signal.dur));
set(handles.edit_marker,'string',signal.marker);
set(handles.edit_linewidth,'string',num2str(signal.linewidth));
set(handles.edit_markersize,'string','(Value)');
% 最开始高音段是可见的
set(handles.threelinedoctave,'enable','on');
set(handles.fourlinedoctave,'enable','on');
set(handles.fivelinedoctave,'enable','on');
% --- Outputs from this function are returned to the command line.
function varargout = MyPiano_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 pushbutton_C.
function pushbutton_C_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_C (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global signal;
global control;
switch control.notegroup % /Hz
case 0 %大字二组
f=0;
case 1 %大字一组
f=32.703;
case 2 %大字组
f=65.406;
case 3 %小字组
f=130.813;
case 4 %小字一组
f=261.626;
case 5 %小字二组
f=523.251;
case 6 %小字三组
f=1046.502;
case 7 %小字四组
f=2093.005;
case 8 %小字五组
f=4186.009;
end
t=0:1/signal.fs:signal.dur;
x = notesynth(f,signal.dur,signal.fs,signal.noteamp,control.timber);
sound(x);
axes(handles.axes_time);
signal.h_t=drawxt(t,x,signal.color,signal.linestyle,signal.linewidth,...
signal.marker,signal.markeredgecolor,signal.markerfacecolor,...
signal.markersize);
axes(handles.axes_freq);
[~,signal.h_f]=drawfft(x,signal.fs,signal.dur,signal.color,...
signal.linestyle,signal.linewidth,signal.marker,...
signal.markeredgecolor,signal.markerfacecolor,signal.markersize);
set(handles.edit_Value,'String',num2str(f)); %注意set函数的用法
% --- Executes on button press in pushbutton_D.
function pushbutton_D_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_D (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global signal;
global control;
switch control.notegroup % /Hz
case 0 %大字二组
f=0;
case 1 %大字一组
f=36.708;
case 2 %大字组
f=73.416;
case 3 %小字组
f=146.832;
case 4 %小字一组
f=293.665;
case 5 %小字二组
f=587.33;
case 6 %小字三组
f=1174.659;
case 7 %小字四组
f=2349.318;
case 8 %小字五组
f=0;
end
t=0:1/signal.fs:signal.dur;
x = notesynth(f,signal.dur,signal.fs,signal.noteamp,control.timber);
sound(x);
axes(handles.axes_time);
signal.h_t=drawxt(t,x,signal.color,signal.linestyle,signal.linewidth,...
signal.marker,signal.markeredgecolor,signal.markerfacecolor,...
signal.markersize);
axes(handles.axes_freq);
[~,signal.h_f]=drawfft(x,signal.fs,signal.dur,signal.color,...
signal.linestyle,signal.linewidth,signal.marker,...
signal.markeredgecolor,signal.markerfacecolor,signal.markersize);
set(handles.edit_Value,'String',num2str(f)); %注意set函数的用法
% --- Executes on button press in pushbutton_E.
function pushbutton_E_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_E (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global signal;
global control;
switch control.notegroup % /Hz
case 0 %大字二组
f=0;
case 1 %大字一组
f=41.203;
case 2 %大字组
f=41.203;
case 3 %小字组
f=164.814;
case 4 %小字一组
f=329.628;
case 5 %小字二组
f=659.255;
case 6 %小字三组
f=1318.51;
case 7 %小字四组
f=2637.02;
case 8 %小字五组
f=0;
end
t=0:1/signal.fs:signal.dur;
x = notesynth(f,signal.dur,signal.fs,signal.noteamp,control.timber);
sound(x);
axes(handles.axes_time);
signal.h_t=drawxt(t,x,signal.color,signal.linestyle,signal.linewidth,...
signal.marker,signal.markeredgecolor,signal.markerfacecolor,...
signal.markersize);
axes(handles.axes_freq);
[~,signal.h_f]=drawfft(x,signal.fs,signal.dur,signal.color,...
signal.linestyle,signal.linewidth,signal.marker,...
signal.markeredgecolor,signal.markerfacecolor,signal.markersize);
set(handles.edit_Value,'String',num2str(f)); %注意set函数的用法
% --- Executes on button press in pushbutton_F.
function pushbutton_F_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_F (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global signal;
global control;
switch control.notegroup % /Hz
case 0 %大字二组
f=0;
case 1 %大字一组
f=43.654;
case 2 %大字组
f=87.307;
case 3 %小字组
f=174.614;
case 4 %小字一组
f=349.228;
case 5 %小字二组
f=698.456;
case 6 %小字三组
f=1396.913;
case 7 %小字四组
f=2793.826;
case 8 %小字五组
f=0;
end
t=0:1/signal.fs:signal.dur;
x = notesynth(f,signal.dur,signal.fs,signal.noteamp,control.timber);
sound(x);
axes(handles.axes_time);
signal.h_t=drawxt(t,x,signal.color,signal.linestyle,signal.linewidth,...
signal.marker,signal.markeredgecolor,signal.markerfacecolor,...
signal.markersize);
axes(handles.axes_freq);
[~,signal.h_f]=drawfft(x,signal.fs,signal.dur,signal.color,...
signal.linestyle,signal.linewidth,signal.marker,...
signal.markeredgecolor,signal.markerfacecolor,signal.markersize);
set(handles.edit_Value,'String',num2str(f)); %注意set函数的用法
% --- Executes on button press in pushbutton_G.
function pushbutton_G_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_G (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global signal;
global control;
switch control.notegroup % /Hz
case 0 %大字二组
f=0;
case 1 %大字一组
f=48.999;
case 2 %大字组
f=97.999;
case 3 %小字组
f=195.998;
case 4 %小字一组
f=391.995;
case 5 %小字二组
f=783.991;
case 6 %小字三组
f=1567.982;
case 7 %小字四组
f=3135.963;
case 8 %小字五组
f=0;
end
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 沈再阳.精通MATLAB信号处理[M].清华大学出版社,2015.
[2]高宝建,彭进业,王琳,潘建寿.信号与系统——使用MATLAB分析与实现[M].清华大学出版社,2020.
[3]王文光,魏少明,任欣.信号处理与系统分析的MATLAB实现[M].电子工业出版社,2018.
标签:case,...,set,GUI,872,电音,handles,小字,signal 来源: https://blog.csdn.net/m0_54742769/article/details/120375179