- % Most shining national wind//最炫民族风 on Matlab
- % The Modification is from "canon", not by me
-
- fs = 44100; % sample rate
- dt = 1/fs;
-
- T16 = 0.125;
-
- t16 = [0:dt:T16];
- [temp k] = size(t16);
-
- t4 = linspace(0,4*T16,4*k);
- t8 = linspace(0,2*T16,2*k);
-
- [temp i] = size(t4);
- [temp j] = size(t8);
-
- % Modification functions
- mod4=(t4.^4).*exp(-30*(t4.^0.5));
- mod4=mod4*(1/max(mod4));
- mod8=(t8.^4).*exp(-50*(t8.^0.5));
- mod8=mod8*(1/max(mod8));
- mod16=(t16.^4).*exp(-90*(t16.^0.5));
- mod16=mod16*(1/max(mod16));
-
- f0 = 2*146.8; % reference frequency
-
- ScaleTable = [2/3 3/4 5/6 15/16 ...
- 1 9/8 5/4 4/3 3/2 5/3 9/5 15/8 ...
- 2 9/4 5/2 8/3 3 10/3 15/4 4 ...
- 1/2 9/16 5/8];
-
- % 1/4 notes
- do0f = mod4.*cos(2*pi*ScaleTable(21)*f0*t4);
- re0f = mod4.*cos(2*pi*ScaleTable(22)*f0*t4);
- mi0f = mod4.*cos(2*pi*ScaleTable(23)*f0*t4);
-
- fa0f = mod4.*cos(2*pi*ScaleTable(1)*f0*t4);
- so0f = mod4.*cos(2*pi*ScaleTable(2)*f0*t4);
- la0f = mod4.*cos(2*pi*ScaleTable(3)*f0*t4);
- ti0f = mod4.*cos(2*pi*ScaleTable(4)*f0*t4);
- do1f = mod4.*cos(2*pi*ScaleTable(5)*f0*t4);
- re1f = mod4.*cos(2*pi*ScaleTable(6)*f0*t4);
- mi1f = mod4.*cos(2*pi*ScaleTable(7)*f0*t4);
- fa1f = mod4.*cos(2*pi*ScaleTable(8)*f0*t4);
- so1f = mod4.*cos(2*pi*ScaleTable(9)*f0*t4);
- la1f = mod4.*cos(2*pi*ScaleTable(10)*f0*t4);
- tb1f = mod4.*cos(2*pi*ScaleTable(11)*f0*t4);
- ti1f = mod4.*cos(2*pi*ScaleTable(12)*f0*t4);
- do2f = mod4.*cos(2*pi*ScaleTable(13)*f0*t4);
- re2f = mod4.*cos(2*pi*ScaleTable(14)*f0*t4);
- mi2f = mod4.*cos(2*pi*ScaleTable(15)*f0*t4);
- fa2f = mod4.*cos(2*pi*ScaleTable(16)*f0*t4);
- so2f = mod4.*cos(2*pi*ScaleTable(17)*f0*t4);
- la2f = mod4.*cos(2*pi*ScaleTable(18)*f0*t4);
- ti2f = mod4.*cos(2*pi*ScaleTable(19)*f0*t4);
- do3f = mod4.*cos(2*pi*ScaleTable(20)*f0*t4);
- blkf = zeros(1,i);
-
- % 1/8 notes
- do0e = mod8.*cos(2*pi*ScaleTable(21)*f0*t8);
- re0e = mod8.*cos(2*pi*ScaleTable(22)*f0*t8);
- mi0e = mod8.*cos(2*pi*ScaleTable(23)*f0*t8);
-
- fa0e = mod8.*cos(2*pi*ScaleTable(1)*f0*t8);
- so0e = mod8.*cos(2*pi*ScaleTable(2)*f0*t8);
- la0e = mod8.*cos(2*pi*ScaleTable(3)*f0*t8);
- ti0e = mod8.*cos(2*pi*ScaleTable(4)*f0*t8);
- do1e = mod8.*cos(2*pi*ScaleTable(5)*f0*t8);
- re1e = mod8.*cos(2*pi*ScaleTable(6)*f0*t8);
- mi1e = mod8.*cos(2*pi*ScaleTable(7)*f0*t8);
- fa1e = mod8.*cos(2*pi*ScaleTable(8)*f0*t8);
- so1e = mod8.*cos(2*pi*ScaleTable(9)*f0*t8);
- la1e = mod8.*cos(2*pi*ScaleTable(10)*f0*t8);
- tb1e = mod8.*cos(2*pi*ScaleTable(11)*f0*t8);
- ti1e = mod8.*cos(2*pi*ScaleTable(12)*f0*t8);
- do2e = mod8.*cos(2*pi*ScaleTable(13)*f0*t8);
- re2e = mod8.*cos(2*pi*ScaleTable(14)*f0*t8);
- mi2e = mod8.*cos(2*pi*ScaleTable(15)*f0*t8);
- fa2e = mod8.*cos(2*pi*ScaleTable(16)*f0*t8);
- so2e = mod8.*cos(2*pi*ScaleTable(17)*f0*t8);
- la2e = mod8.*cos(2*pi*ScaleTable(18)*f0*t8);
- ti2e = mod8.*cos(2*pi*ScaleTable(19)*f0*t8);
- do3e = mod8.*cos(2*pi*ScaleTable(20)*f0*t8);
- blke = zeros(1,j);
-
- % 1/16 notes
- do0s = mod16.*cos(2*pi*ScaleTable(21)*f0*t16);
- re0s = mod16.*cos(2*pi*ScaleTable(22)*f0*t16);
- mi0s = mod16.*cos(2*pi*ScaleTable(23)*f0*t16);
-
- fa0s = mod16.*cos(2*pi*ScaleTable(1)*f0*t16);
- so0s = mod16.*cos(2*pi*ScaleTable(2)*f0*t16);
- la0s = mod16.*cos(2*pi*ScaleTable(3)*f0*t16);
- ti0s = mod16.*cos(2*pi*ScaleTable(4)*f0*t16);
- do1s = mod16.*cos(2*pi*ScaleTable(5)*f0*t16);
- re1s = mod16.*cos(2*pi*ScaleTable(6)*f0*t16);
- mi1s = mod16.*cos(2*pi*ScaleTable(7)*f0*t16);
- fa1s = mod16.*cos(2*pi*ScaleTable(8)*f0*t16);
- so1s = mod16.*cos(2*pi*ScaleTable(9)*f0*t16);
- la1s = mod16.*cos(2*pi*ScaleTable(10)*f0*t16);
- tb1s = mod16.*cos(2*pi*ScaleTable(11)*f0*t16);
- ti1s = mod16.*cos(2*pi*ScaleTable(12)*f0*t16);
- do2s = mod16.*cos(2*pi*ScaleTable(13)*f0*t16);
- re2s = mod16.*cos(2*pi*ScaleTable(14)*f0*t16);
- mi2s = mod16.*cos(2*pi*ScaleTable(15)*f0*t16);
- fa2s = mod16.*cos(2*pi*ScaleTable(16)*f0*t16);
- so2s = mod16.*cos(2*pi*ScaleTable(17)*f0*t16);
- la2s = mod16.*cos(2*pi*ScaleTable(18)*f0*t16);
- ti2s = mod16.*cos(2*pi*ScaleTable(19)*f0*t16);
- do3s = mod16.*cos(2*pi*ScaleTable(20)*f0*t16);
- blks = zeros(1,k);
-
-
- % Melody by Schau_mal
- part0 = [mi1f la0e la0e do1f mi1f ...
- re1e re1s mi1s re1e do1e re1e do1e la0f ...
- mi1f la0e la0e do1f mi1f ...
- so1e re1s mi1s re1e do1e re1e do1e ti0e so0e ...
- mi1f la0e la0e do1f mi1f ...
- re1e re1s mi1s re1e do1e re1e do1e la0e so0e ...
- mi1f la0e la0e do1f mi1f ...
- so1e mi1e blkf blkf blkf ...
- ];
-
- part1 = [la0f la0e so0e la0f la0e do1e ...
- do1f re1e do1e la0f la0f ...
- do1f do1e so0e do1e re1e mi1e so1e ...
- so1e mi1e re1f mi1f mi1f ...
- la1e la1e la1e so1e mi1e mi1f do1e ...
- la0e la0e la0e mi1e re1s mi1s re1e re1f ...
- mi1e mi1e so1e mi1e re1e mi1e re1e do1e ...
- la0f so0f la0f la0f ...
- ];
-
- part2 = [mi1e mi1e so1e mi1e mi1e so1e so1e la1e ...
- do2e la1e so1f la1s do2s la1e la1f ...
- la0f la0e so0e la0f do1f ...
- re1e mi1s re1s do1e re1e mi1f mi1f ...
- la0e la1e la1e so1e re1e mi1s re1s do1e re1e ...
- mi1f mi1f blke blke blkf ...
- do1e la0e la0e do1e re1f so0e so0e ...
- mi1e so1e mi1e re1e do1f do1f ...
- la0e do1e re1e mi1e re1e do1e so0e mi0e ...
- la0f la0f blke blke blkf ...
- ];
-
- part3 = [la0f la0e so0e la0f do1f ...
- re1e mi1s re1s do1e re1e mi1f mi1f ...
- la0e la1e la1e so1e re1e mi1s re1s do1e re1e ...
- mi1f mi1f blke blke blkf ...
- do1e la0e la0e do1e re1f so0e so0e ...
- mi1e so1e mi1e re1e do1f do1e do1e ...
- la0e do1e re1e mi1e so1e mi1e mi1e so1e ...
- la1f la1f la1f la1f ...
- ];
-
- part4 = [la1e la1s la1s la1e la1e la1e la1s so1s mi1e re1e ...
- re1e re1s re1s mi1e mi1s so1s mi1e mi1s re1s do1e do1s la0s ...
- la0f la0e so0e la0f la0e do1e ...
- re1e mi1s re1s do1e re1e mi1f mi1f ...
- la1e so1e mi1e re1e so1e mi1e re1e do1e ...
- do1f do1f la0s do1s re1s mi1s re1s do1s la0s do1s
- ];
-
- part5 = [do2e do2s do2s la1e la1s la1s so1e so1s so1s mi1e mi1s mi1s ...
- re1e mi1s re1s do1e la0s so0s la0s so0s do1s re1s mi1s so1s la1s re2s ...
- do2f do2f blks blks blks blks do1e re1e ...
- mi1f mi1f mi1f so1e mi1e ...
- la1f la1f la1e do1e so1e mi1e ...
- re1f re1e re1s re1s re1e re1e do1e re1e ...
- mi1f mi1e mi1s mi1s mi1e re1s do1s ti0e do1s re1s ...
- mi1f mi1f mi1f so1e mi1e ...
- do2f la1f la1f la1e do1e ...
- re1f so1f so1f la1f ...
- ti1f ti1f ti1f ti1f ...
- ];
-
- part6 = [blkf blkf mi1e so1e mi1e so1e ...
- mi1f la0e la0s la0s do1f la0e mi1s la0s ...
- do1e do1s do1s re1e do1s re1s mi1f mi1f ...
- mi1f la0e la0s la0s so1f re1e re1s re1s ...
- mi1f mi1f mi1s re1s do1s la0s mi0s re0s mi0s so0s ...
- do1f la0e la0s la0s re1f so0e so0s so0s ...
- mi0f so0e so0s so0s do1f do1f ...
- la0f do1e do1s la0s mi1e mi1s mi1s re1e re1s mi1s ...
- ];
-
- % Combination, v1 is complete version, v2 is simple version.
- v1 = [part0 part1 part1 part2 part3 part4 part0 part1 part1 part2 part3 part5 part3 part6 part3];
- v2 = [part0 part1 part1 part2 part3 part5 part3 part6 part3];
-
- % Let's rock ^_^
- s = v1;
- s = s/max(s);
- sound(s,fs);
前两天在网上看到了有人用matlab演奏出最炫民族风,我用了这么多年matlab还是第一次知道有这种玩法,于是把他的代码研究了一下,自己制作了一首歌曲,在这里分享一下。
首先介绍matlab函数的玩法:
matlab播放音乐是由sound(Y,fs,bits)函数完成的,该函数的3个参数代表输入信号、采样率、比特率。先说采样率fs的设置,人耳能够听到的声音范围是20~20000Hz。根据采样定理fs只需要大于40000即可。此处采样率的设置采用了MP3的标准,即fs=44.1k.再说输入信号Y,Y一般是一个正弦波,如A*sin(2*pi*w*t)。其中A控制着声音的大小,w控制着声音的高低,t的范围控制着声音的长短,所以理论上利用这个公式可以发出任何声音,只是不能控制音色和音质(音色音质很难用参数量化,我问了一下学语音的同学,这个东西跟音频谱重心,音频扩展度和音频谱平坦度有关系,这里不作讨论)。比特率采用默认值即可,该参数省略。
于是用下面的公式就可以播放出标准音la:(座机电话提示音就是la,可以用来调吉他)
fs=44100;
t=0: 1/fs: 0.5;
la = sin(2*pi*440*t); (下文介绍440是怎么来的)
sound(la, fs)
下面介绍一下简单乐理:
看过柯南的同学都知道,音高和频率是指数的关系,它们满足下面的公式,其中p是音高,f是频率
f=440 * 2(p-69)/12,这就是著名的十二平均率。
标准音la,即钢琴的A4键,定义为p=69。音高每上升一个半音,p加1。
如上图所示,从C4到B4分别对应着do re mi fa sol la xi,它们的p值分别为 60 62 64 65 67 69 71,注意中间有黑键,你也可以根据下表来查询某个音的频率。
Frequency in hertz (semitones above or below middle C) |
Octave→ Note↓ |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
C |
16.352 (−48) |
32.703 (−36) |
65.406 (−24) |
130.81 (−12) |
261.63 (0) |
523.25 (+12) |
1046.5 (+24) |
2093.0 (+36) |
4186.0 (+48) |
8372.0 (+60) |
C♯/D♭ |
17.324 (−47) |
34.648 (−35) |
69.296 (−23) |
138.59 (−11) |
277.18 (+1) |
554.37 (+13) |
1108.7 (+25) |
2217.5 (+37) |
4434.9 (+49) |
8869.8 (+61) |
D |
18.354 (−46) |
36.708 (−34) |
73.416 (−22) |
146.83 (−10) |
293.66 (+2) |
587.33 (+14) |
1174.7 (+26) |
2349.3 (+38) |
4698.6 (+50) |
9397.3 (+62) |
D♯/E♭ |
19.445 (−45) |
38.891 (−33) |
77.782 (−21) |
155.56 (−9) |
311.13 (+3) |
622.25 (+15) |
1244.5 (+27) |
2489.0 (+39) |
4978.0 (+51) |
9956.1 (+63) |
E |
20.602 (−44) |
41.203 (−32) |
82.407 (−20) |
164.81 (−8) |
329.63 (+4) |
659.26 (+16) |
1318.5 (+28) |
2637.0 (+40) |
5274.0 (+52) |
10548 (+64) |
F |
21.827 (−43) |
43.654 (−31) |
87.307 (−19) |
174.61 (−7) |
349.23 (+5) |
698.46 (+17) |
1396.9 (+29) |
2793.8 (+41) |
5587.7 (+53) |
11175 (+65) |
F♯/G♭ |
23.125 (−42) |
46.249 (−30) |
92.499 (−18) |
185.00 (−6) |
369.99 (+6) |
739.99 (+18) |
1480.0 (+30) |
2960.0 (+42) |
5919.9 (+54) |
11840 (+66) |
G |
24.500 (−41) |
48.999 (−29) |
97.999 (−17) |
196.00 (−5) |
392.00 (+7) |
783.99 (+19) |
1568.0 (+31) |
3136.0 (+43) |
6271.9 (+55) |
12544 (+67) |
G♯/A♭ |
25.957 (−40) |
51.913 (−28) |
103.83 (−16) |
207.65 (−4) |
415.30 (+8) |
830.61 (+20) |
1661.2 (+32) |
3322.4 (+44) |
6644.9 (+56) |
13290 (+68) |
A |
27.500 (−39) |
55.000 (−27) |
110.00 (−15) |
220.00 (−3) |
440.00 (+9) |
880.00 (+21) |
1760.0 (+33) |
3520.0 (+45) |
7040.0 (+57) |
14080 (+69) |
A♯/B♭ |
29.135 (−38) |
58.270 (−26) |
116.54 (−14) |
233.08 (−2) |
466.16 (+10) |
932.33 (+22) |
1864.7 (+34) |
3729.3 (+46) |
7458.6 (+58) |
14917 (+70) |
B |
30.868 (−37) |
61.735 (−25) |
123.47 (−13) |
246.94 (−1) |
493.88 (+11) |
987.77 (+23) |
1975.5 (+35) |
3951.1 (+47) |
7902.1 (+59) |
15804 (+71) |
有了上面的基础,下面就可以用matlab创作歌曲了,这里以李健的传奇作为示例:(喜欢李健的可以加我好友~~)
clc
clear
fs=44100;
t=0:1/fs:0.5;
e3_2=key(52, 2, fs); %表示2分音符的e3
a3_2=key(57, 2, fs);
c4_2=key(60, 2, fs);
e4_2=key(52, 2, fs);
g3_2=key(55, 2, fs);
d4_2=key(62, 2, fs);
e4_2=key(64, 2, fs);
g4_2=key(67, 2, fs);
e4_4=key(52, 4, fs);
g3_4=key(55, 4, fs);
a3_4=key(57, 4, fs);
b3_4=key(59, 4, fs);
c4_4=key(60, 4, fs);
d4_4=key(62, 4, fs);
e4_4=key(64, 4, fs);
f4_4=key(65, 4, fs);
g4_4=key(67, 4, fs);
a4_4=key(69, 4, fs);
e3_8=key(52, 8, fs);
g3_8=key(55, 8, fs);
a3_8=key(57, 8, fs);
b3_8=key(59, 8, fs);
c4_8=key(60, 8, fs);
d4_8=key(62, 8, fs);
e4_8=key(64, 8, fs);
f4_8=key(65, 8, fs);
g4_8=key(67, 8, fs);
a4_8=key(69, 8, fs);
b4_8=key(71, 8, fs);
part1=[c4_8 c4_8 c4_4 e4_4 d4_4 d4_8 d4_4 c4_4 c4_8];
part2=[d4_4 d4_4 c4_8 a3_8 a3_2];
part3=[b3_8 b3_8 b3_4 c4_8 d4_8 d4_4];
part4=[b3_4 a3_8 g3_4 e3_8 e3_2 e3_2];
part5=[e4_8 d4_8 e4_4 d4_8 d4_8 d4_4 d4_8 c4_8 c4_2 d4_4 a3_4 a3_8 a3_8 d4_8 c4_8 c4_2];
part6=[d4_4 a3_8 g3_4 g3_8 e3_2 e3_2];
part7=[g4_4 g4_8 d4_8 d4_4 e4_4 g4_4 g4_8 d4_8 d4_4 c4_4 a3_2 a3_2];
part8=[d4_4 d4_8 a3_8 a3_4 e4_4 d4_4 d4_8 c4_8 c4_4];
part9=[c4_8 c4_8 c4_4 g3_8 c4_8 c4_4 g4_4 f4_4 e4_4 d4_4 d4_8 c4_8 c4_2 c4_4];
part10=[c4_8 e4_8 g4_8 a4_4 g4_8 a4_4 g4_4 g4_8 a4_4];
para1=[part1 part2 part3 part4];
para2=[part5 part3 part6];
para3=[part7 part8 c4_4 g3_2 g3_2];
para4=[part7 part8 c4_8 d4_8 d4_2 d4_2];
para5=[part9 part10 g4_8 e4_8 e4_8 d4_4 e4_8 e4_2];
para6=[part9 part10 g4_4 e4_8 g4_4 g4_8 g4_2];
legend=[para1 para2 para3 para4 para5 para6];
sound(legend,fs)
其中有两个函数:
function g=key(p, n, fs)
t=0 : 1/fs : 2/n;
g=sin(2*pi* fre(p) *t);
function f = fre(p)
f=440*2^((p-69)/12);