clear;clc
Fs=8192; %sound函数的采样频率
%说明:tone为七节白键,rythm默认为4/4拍,keynote_type为更改基调,
%说明:rythm_change为节拍改变,文中有一部份为3/4拍,Soundzone_change为低中高音设定
%1234567分别对应do,re,mi,fa,so,la,si
tone1=[5,5,6,5,1,7,5,5,6,5,2,...
1,5,5,5,3,1,7,6,4,4,3,1,3,2,5,5,6,5,1,...
7,5,5,6,5,2,1,5,5,5,3,1,7,6];
tone2=[4,4,3,1,2,1,5,5,...
6,5,1,7,5,5,6,5,2,1,5,5,...
5,3,1,7,6,4,4,3,1,3,2,5,5,...
6,5,1,7,5,5,6,5,2,1,5,5,5,3,1,...
7,6,4,4,3,1,2,1];
tone=[tone1,tone2];
rythm1=[0.5,0.5,1,1,1,2,0.5,0.5,1,1,1,...
2,0.5,0.5,1,1,1,1,1,0.5,0.5,1,1,1,2,0.5,0.5,1,1,1,...
2,0.5,0.5,1,1,1,2,0.5,0.5,1,1,1,1,5];
rythm2=[1,1,2,2,2,7,0.5,0.5,...
1.5,1.5,1,3,0.5,0.5,1.5,1.5,1,3,0.5,0.5,...
1.5,1.5,1,1.5,1.5,0.5,0.5,1.5,1.5,1,3,0.5,0.5,...
1.5,1.55,1,3,0.5,0.5,1.5,1.5,1,3,0.5,0.5,1.5,1.5,1,...
1.5,5.5,0.5,0.5,2,2,4,8];
rythm=[rythm1,rythm2];
length1=length(tone1);
length2=length(tone2);
rythm_change_1=ones(1,length1)*3/4/2;
rythm_change_2=ones(1,length2)*1/4;
rythm_change=[rythm_change_1,rythm_change_2];
rythm=rythm.*rythm_change;
y=[];
len=length(tone);
%A调为1,B调为2,C调为3,D调为4,E调为5,F调为6,E调为7;
keynote_type=6;
%3为高音,2为中音,1为低音
Soundzone_change=[1,1,1,1,2,1,1,1,1,1,2,...
2,1,1,2,2,2,1,1,2,2,2,2,2,2,1,1,1,1,2,...
1,1,1,1,1,2,2,1,1,2,2,2,1,1,...
2,2,2,2,2,2,1,1,...
1,1,2,1,1,1,1,1,2,2,1,1,...
2,2,2,1,1,2,2,2,2,2,2,1,1,...
1,1,2,1,1,1,1,1,2,2,1,1,2,2,2,...
1,1,2,2,2,2,2,2];
%3为上升一个全音,2.5为上升一个半音,2为原音,1.5为下调一个半音,1为下调一个全音
up_down=2;
for i=1:1:len
y=[y,get_ware(tone(i),rythm(i),keynote_type,Soundzone_change(i),up_down)];
end
%plot(y)
sound(y,Fs) %按照给定的波,发出声音的函数
上述的音乐绘制还有一些值得研究的地方,比如说,如何发出同时发出多个音,实现更为复杂的乐曲的演奏,以及如何模拟出钢琴按键轻重不同导致的尾音长短的不同。还有就是在转换乐谱上边,如果有更加智能的方式生成就更加好了,因为基础的演奏的函数其实很简单。