整理:⽤matlab创作歌曲
(⼀部分)
前两天在⽹上看到了有⼈⽤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是频率
标准⾳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↓
0123456789
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)
(−43)(−31)(−19)(−7)(+5)(+17)(+29)(+41)(+53)(+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);
谢谢收听
(⼆部分)
邢昭林梁洁
(三部分)
%% 《最炫民族风》
% Most shining national wind %最炫民族风 on Matlab