Matlab唱歌?尊嘟假嘟?

       在大二的时候,看到我们的智能优化老师用Matlab唱歌,当时就觉得很牛X,没想到Matlab还能当一个乐器,今天对这个我创作的过程做一个记录,也是对这方面有兴趣的同学做一个引导学习,废话不多说,我们开整!!!

今天我们要演凑的音乐是——达拉崩吧   

1.乐理小知识

1.1 十二平均律

      十二平均律又称十二等程律,是将一个八度的音程等分为是分成十二个半音的律制,各相邻两律之间的波长之比完全相等,十二平均律是由中国明朝皇族世子发现的。

Matlab唱歌?尊嘟假嘟?_第1张图片

1.12 计算方法

       一个八音音程,高音频率为基础音的2倍,将这个2连续开12次方,得出1.05946,这就是两个相邻半音之间的概率比。

第一律:440*2^{\frac{1}{12}}

第i律:440*2^{\frac{i}{12}}

G调萧
音名 筒音作5 筒音作2 频率
d1

2_ 293.7
e1 6 3_ 329.6
f1 #6 4_ 349.2
#f1 7_ #4_ 370.0
g1 1 5_ 392.0
a1 2 6_ 440.0
b1 3 7_ 493.9
c2 4 1 523.3
d2 5 2 587.3
e2 6 3 659.3
f2 #6 4 698.5
#f2 7 #4 740.0
g2 高1 5 784.0
a2 高2 6 880.0
b2 高3 7 987.8
c3 高4 高1 1046.5
d3 高5 高2 1174.7
e3 高6 高3 1318.5
f3 高#6 高4 1396.5
#f3 高7 高#4 1480.0
g3 超高1 高5 1568.0

乐理知识学习这么多,就已经我们用的了,现在开我们Matlab的教学片!!!

2.Matlab教学

2.1 sound函数(将信号数据矩阵转换为声音)

语法:

  • sound(y) 以默认采样率8192Hz向扬声器发送音频信号y
  • sound(y,Fs) 以采样率Fs向扬声器发送音频信号y
  • sound(y,Fs,nBits) 对音频信号y使用nBits的采样位数
%tone是音符,rhythm是频率大小
function y=musiclower(tone,rhythm)
Fs=8192;
%低DO 低RE 低M 低FA 低SO 低LA 低SI
freqs=[262,294,330,349,392,440,494];%七个音符对应的频率
x=linspace(0,2*pi*rhythm,floor(Fs*rhythm));
y=sin(freqs(tone)*x).*(exp(-0.7*x/rhythm));
end

>> y=[];
>> for i=1:7
     y=[y,musiclower(i,0.3)];
end
>> plot(y);
>> sound(y)%默认是8192

Matlab唱歌?尊嘟假嘟?_第2张图片

音乐就是因为震动产生的,震动的幅度不同,音色自然而然也就不同

所以自然而然的出现了很多优化方式,具体见:利用包络函数优化MATLAB中sound函数的发声【matlab调音_2】_matlab 编程实现包络和音色_歪卜巴比的博客-CSDN博客

 源代码:

  • musiclower.m
%tone是音符,rhythm是频率大小
function y=musiclower(tone,rhythm)
Fs=8192;
%低DO 低RE 低M 低FA 低SO 低LA 低SI
freqs=[262,294,330,349,392,440,494];%七个音符对应的频率
x=linspace(0,2*pi*rhythm,floor(Fs*rhythm));
y=sin(freqs(tone)*x).*(exp(-0.7*x/rhythm));
end
  • musiclower_mid.m
function y=musiclower_mid(tone,rhythm)
Fs=8192;
%半音 半DO 半RE 半FA 半SO 半LA
freqs=[277,311,370,415,466];
x=linspace(0,2*pi*rhythm,floor(Fs*rhythm));
y=sin(freqs(tone)*x).*((1-x./rhythm.*2*pi));
end
  • musicmid.m
%tone是音符,rhythm是频率大小
%中
function y=musicmid(tone,rhythm)
Fs=8192;
%中DO-1 中SO-5 中LA-6  中SI-7
freqs=[523,784,880,988];
x=linspace(0,2*pi*rhythm,floor(Fs*rhythm));
y=sin(freqs(tone)*x).*(exp(-0.7*x/rhythm));
end
  • musicmid_mid.m
function y=musicmid_mid(tone,rhythm)
Fs=8192;
%中半音 半FA 半SO 半LA
freqs=[740,831,932];
x=linspace(0,2*pi*rhythm,floor(Fs*rhythm));
y=sin(freqs(tone)*x).*(exp(-0.7*x/rhythm));
end
  • music_high.m
function y=music_high(tone,rhythm)
Fs=8192;
%高DO 高RE 高M 高FA
freqs=[1046,1175,1318,1397];%七个音符对应的频率
x=linspace(0,2*pi*rhythm,floor(Fs*rhythm));
y=sin(freqs(tone)*x).*(exp(-0.7*x/rhythm));
end
  • musichigh_mid.m
function y=musichigh_mid(tone,rhythm)
Fs=8192;
%高半音 半DO 半RE 半FA 
freqs=[1109,1245,1480];
x=linspace(0,2*pi*rhythm,floor(Fs*rhythm));
y=sin(freqs(tone)*x).*((1-x./rhythm.*2*pi));
end
  • dalabengba.m
Fs=9000;%采样频率
%0%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第一行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y0=empty(0,0.05);
y8=empty(0,0.01);
%低5 下点
y1=musiclower(2,0.27);
%低6 下点 延长
y2=musiclower(3,0.32);
%低5 下点 延长
y3=musiclower(2,0.32);
%低6 下点
y4=musiclower(3,0.27);
%中2 延长
y5=musicmid(3,0.32);
%中2 
y6=musicmid(3,0.27);
%中3 
y7=musicmid(4,0.27);
y=[y0,y1,y0,y2,y3,y4,y0,y1,y0,y1,y0,y2,y5,y7,y0,y6,y0,y1,y0,y2,y3,y4,y0,y1,y0,y1,y0,y4,y5,y7,y0,y6];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第二行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pause(0.5);%暂定0.5
%低7 
y9=musiclower(4,0.27);
%中 1
y10=musicmid(2,0.27);
%中 1 下划线
y13=musicmid(2,0.27);
%中 2
y11=musicmid(3,0.27);
%中 2 下划线
y14=musicmid(3,0.27);
%中 3 下线
y12=musicmid(4,0.27);
%中 3  连音线
y16=musicmid(4,0.32);
%中 3 双连下划线 
y17=musicmid(4,0.31);
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,...
    y10,y11,y17,y7,y11,y10,y14,y13,y9,y10,y7];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第三行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中 2 双连音线
y18=musicmid(3,0.32);
%中1 延长
y19=musicmid(2,0.32);
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,...
  y10,y11,y7,y0,y18,y19,y9,y6,y7];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第四行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,y19,y11,y7,...
   y7,y11,y10,y14,y13,y9,y10,y7];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%第五行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%双低7 连音
y20=musiclower(4,0.32);
%半中2 
y21=musicmid_mid(3,0.27);
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,y10,y11,y7,...
    y0,y18,y19,y20,y21,y7];
%%%%%%%%%%%%%%%%%%%%%%%%第六行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中6  
y22=musicmid(1,0.27);
%中 3
y23=musicmid(4,0.27);
%中 半2
y24=musicmid_mid(3,0.27);
%中 半4
y25=musicmid(1,0.27);
%中 5
y26=musicmid(1,0.27);
y=[y,y22,y23,y24,y23,y24,y23,y10,y23,y9,y23,y4,y23,y19,y12,y25,y26,y22,y22,y23,y24,y23,y24,y23,...
    y9,y23,y4,y23,y10,y12,y25,y25];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第二行 2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pause(0.5);%暂定0.5
%低7 
y9=musiclower(4,0.27);
%中 1
y10=musicmid(2,0.27);
%中 1 下划线
y13=musicmid(2,0.27);
%中 2
y11=musicmid(3,0.27);
%中 2 下划线
y14=musicmid(3,0.27);
%中 3 下线
y12=musicmid(4,0.27);
%中 3  连音线
y16=musicmid(4,0.32);
%中 3 双连下划线 
y17=musicmid(4,0.31);
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,...
    y10,y11,y17,y7,y11,y10,y14,y13,y9,y10,y7];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第三行 2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中 2 双连音线
y18=musicmid(3,0.32);
%中1 延长
y19=musicmid(2,0.32);
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,...
  y10,y11,y7,y0,y18,y19,y9,y6,y7];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第四行 2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,y19,y11,y7,...
   y7,y11,y10,y14,y13,y9,y10,y7];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%第五行 2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%双低7 连音
y20=musiclower(4,0.32);
%半中2 
y21=musicmid_mid(3,0.27);
y=[y,y0,y16,y2,y9,y10,y11,y7,y0,y16,y2,y9,y10,y11,y10,y0,y16,y2,y9,y10,y11,y7,...
    y0,y18,y19,y20,y21,y7];
%%%%%%%%%%%%%%%%%%%%%%%%第六行 2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中6  
y22=musicmid(1,0.27);
%中 3
y23=musicmid(4,0.27);
%中 半2
y24=musicmid_mid(3,0.27);
%中 半4
y25=musicmid(1,0.27);
%中 5
y26=musicmid(1,0.27);
y=[y,y22,y23,y24,y23,y24,y23,y10,y23,y9,y23,y4,y23,y19,y12,y25,y26,y22,y22,y23,y24,y23,y24,y23,...
    y9,y23,y4,y23,y10,y12,y25,y25];
%%%%%%%%%%%%%%%%%%%%%第七行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%半 中5
y27=empty(620,0.27);
y=[y,y22,y23,y24,y23,y24,y23,y10,y23,y9,y23,y4,y23,y19,y4,y26,y27,y22,y23,y24,y23,y24,y23,...
    y9,y23,y4,y23,y10,y22,y0];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第八行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%长音 8
y28=musicmid(2,1.5);
%长音 7
y29=musicmid(1,1.5);
%长音 0
y30=empty(0,1);
y=[y,y28,y29,y30,y24,y7];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第九行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=[y,y22,y23,y24,y23,y24,y23,y10,y23,y9,y23,y4,y23,y10,y26,y27,y22,y23,y24,y23,y24,y23,...
    y9,y23,y4,y23,y10,y7,y26];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第10行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%高 1
y31=music_high(4,0.27);
%中7
y32=musicmid_mid(1,0.27);
y=[y,y22,y23,y24,y23,y24,y23,y10,y23,y9,y23,y4,y23,y19,y4,y26,y27,y22,y23,y24,y23,y24,y23,...
    y9,y23,y4,y23,y31,y21,y7];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第11行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=[y,y22,y23,y24,y23,y24,y23,y10,y23,y9,y23,y4,y3,y19,y23,y25,y1,y27,y22,y23,y24,y23,y24,y23,...
    y9,y23,y4,y23,y10,y24,y23];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第12行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=[y,y22,y23,y24,y23,y24,y23,y10,y23,y9,y23,y4,y23,y26,y4,y26,y27,y22,y23,y24,y23,y24,y23,...
    y9,y23,y9,y23,y4,y23,y10,y7,y26];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第13行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中 5超级延长 3
y33=musicmid(1,0.6);
y=[y,y22,y22,y31,y22,y22,y26,y22,y26,y22,y23,y22,y33,y22,y22,y22,y31,y22,y22,y26,y22,y26,y26,y23,y22,y26,y26,y31,y22];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第14行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中 5超级延长 2
y34=musicmid(1,0.5);
y=[y,y22,y22,y31,y22,y22,y26,y22,y26,y22,y22,y22,y22,y22,y22,y34,y31,y22,y22,y26,y22,y26,y26,y23,y22,y26,y22,y31,y26,y22,y0];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第15行%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=[y,y0,y1,y0,y2,y26,y0,y6,y4,y0,y0,y0,y0];
plot(y);
filename = 'handel.wav';
audiowrite(filename,y,Fs);
sound(y,Fs)


你可能感兴趣的:(Matlab,matlab)