基于乐理和MATLAB自身sound函数,可以实现MATLAB演奏音乐,本文中以邓紫棋《光年之外》为例实现了该功能。
主函数代码为:
Fs = 44100; y1=gen_wave2(3,0.5);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(2,0.25); y5=gen_wave2(1,0.25); y6=gen_wave2(1,0.25); y7=gen_wave2(14,0.25); y8=gen_wave2(14,0.25); y9=gen_wave2(1,0.25); y10=gen_wave2(1,0.25); y11=gen_wave2(22,0.5); y=[y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11]; %感受停在我发端的之间; y1=gen_wave2(12,0.25);%12是5的低音;15是1的高音 y2=gen_wave2(12,0.25); y3=gen_wave2(12,0.75); y4=gen_wave2(3,0.75); y5=gen_wave2(12,0.25); y6=gen_wave2(12,0.25); y7=gen_wave2(12,0.75); y8=gen_wave2(2,0.5); y9=gen_wave2(22,0.5); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9]; %如何瞬间冻结时间; y1=gen_wave2(3,0.25);%12是5的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(2,0.25); y5=gen_wave2(1,0.25); y6=gen_wave2(1,0.25); y7=gen_wave2(14,0.25); y8=gen_wave2(14,0.25); y9=gen_wave2(1,0.25); y10=gen_wave2(1,0.5); y11=gen_wave2(22,0.25); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11]; %记住望着我坚定的双眼 y1=gen_wave2(12,0.25);%12是5的低音;15是1的高音 y2=gen_wave2(12,0.25); y3=gen_wave2(12,0.5); y4=gen_wave2(3,0.75); y5=gen_wave2(12,0.25); y6=gen_wave2(12,0.25); y7=gen_wave2(12,0.5); y8=gen_wave2(2,0.5); y9=gen_wave2(22,0.25); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9]; %也许已经没有明天 y1=gen_wave2(14,0.125);%8是1的低音;15是1的高音 y2=gen_wave2(14,0.125); y3=gen_wave2(14,0.25); y4=gen_wave2(1,0.125); y5=gen_wave2(1,0.125); y6=gen_wave2(1,0.25); y7=gen_wave2(3,0.5); y8=gen_wave2(22,0.5); y=[y,y1,y2,y3,y4,y5,y6,y7,y8]; %面对浩瀚的星海; y1=gen_wave2(14,0.125);%8是1的低音;15是1的高音 y2=gen_wave2(14,0.125); y3=gen_wave2(14,0.125); y4=gen_wave2(14,0.25); y5=gen_wave2(1,0.125); y6=gen_wave2(1,0.125); y7=gen_wave2(1,0.25); y8=gen_wave2(3,0.25) y9=gen_wave2(22,0.5); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9]; %我们微小得像尘埃; y1=gen_wave2(1,0.125);%8是1的低音;15是1的高音 y2=gen_wave2(2,0.125); y3=gen_wave2(3,0.25); y4=gen_wave2(22,0.25); y5=gen_wave2(1,0.25); y6=gen_wave2(3,0.25); y7=gen_wave2(22,0.25); y8=gen_wave2(1,0.25) y9=gen_wave2(2,0.25); y10=gen_wave2(22,1); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10]; %漂浮在 一片无奈 y1=gen_wave2(14,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(1,0.25); y3=gen_wave2(1,0.25); y4=gen_wave2(2,0.25); y5=gen_wave2(2,0.25); y6=gen_wave2(3,0.25); y7=gen_wave2(3,0.375); y8=gen_wave2(5,0.125); y9=gen_wave2(5,0.25); y10=gen_wave2(6,0.25); y11=gen_wave2(6,0.5); y12=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12]; %缘分让我们相遇乱世以外 y1=gen_wave2(1,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(2,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(3,0.25); y5=gen_wave2(3,0.25); y6=gen_wave2(5,0.25); y7=gen_wave2(5,0.375); y8=gen_wave2(1,0.125); y9=gen_wave2(3,0.25); y10=gen_wave2(2,0.25); y11=gen_wave2(2,0.5); y12=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12]; %命运却要我们危难中相爱; y1=gen_wave2(14,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(1,0.25); y3=gen_wave2(1,0.25); y4=gen_wave2(2,0.25); y5=gen_wave2(2,0.25); y6=gen_wave2(3,0.25); y7=gen_wave2(3,0.375); y8=gen_wave2(5,0.125); y9=gen_wave2(5,0.25); y10=gen_wave2(6,0.25); y11=gen_wave2(6,0.5); y12=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12]; %也许未来遥远在光年之外 y1=gen_wave2(1,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(2,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(3,0.25); y5=gen_wave2(3,0.25); y6=gen_wave2(5,0.25); y7=gen_wave2(5,0.375); y8=gen_wave2(1,0.125); y9=gen_wave2(3,0.25); y10=gen_wave2(2,0.25); y11=gen_wave2(2,0.25); %y12=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11]; %我愿守候未知里为你等待 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25); y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(13,0.25); y13=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %我没想到为了你我能疯狂到 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25); y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(14,0.25); y13=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %山崩海啸没有你根本不想逃 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25);%此处6应该不准,仍写为1 y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(13,0.25); y13=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %我的大脑为了你已经疯狂到 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25);% y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(14,0.25); y13=gen_wave2(22,1.25); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %脉搏心跳没有你根本不重要 y1=gen_wave2(14,0.5);%8是1的低音;15是1的高音 y2=gen_wave2(13,0.25); y3=gen_wave2(1,0.25); y4=gen_wave2(14,0.5);% y5=gen_wave2(12,0.5); y6=gen_wave2(14,0.5); y7=gen_wave2(22,0.5); y8=gen_wave2(1,0.25); y9=gen_wave2(14,0.25); y10=gen_wave2(12,0.5); y11=gen_wave2(9,0.5); y12=gen_wave2(12,0.5); y13=gen_wave2(10,0.25); y14=gen_wave2(22,1); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14]; %也许航道以外是醒不来的梦; y1=gen_wave2(1,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(14,0.5); y3=gen_wave2(12,0.5); y4=gen_wave2(2,0.25);% y5=gen_wave2(22,0.5); y6=gen_wave2(1,0.25); y7=gen_wave2(1,0.5); y8=gen_wave2(14,0.5); y9=gen_wave2(14,0.5); y10=gen_wave2(1,0.5); y11=gen_wave2(2,0.25); y12=gen_wave2(22,0.5); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12]; %乱世以外,是纯粹的相拥 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25); y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(13,0.25); y13=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %我没想到为了你我能疯狂到 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25); y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(14,0.25); y13=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %山崩海啸没有你根本不想逃 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25);%此处6应该不准,仍写为1 y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(13,0.25); y13=gen_wave2(22,0.75); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %我的大脑为了你已经疯狂到 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25);% y5=gen_wave2(1,0.125); y6=gen_wave2(5,0.125); y7=gen_wave2(5,0.25); y8=gen_wave2(5,0.25); y9=gen_wave2(5,0.25); y10=gen_wave2(1,0.375); y11=gen_wave2(1,0.25); y12=gen_wave2(14,0.25); y13=gen_wave2(22,1.25); y=[y,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13]; %脉搏心跳没有你根本不重要 y1=gen_wave2(4,0.25);%8是1的低音;15是1的高音 y2=gen_wave2(3,0.25); y3=gen_wave2(2,0.25); y4=gen_wave2(1,0.25);% y5=gen_wave2(22,0.125); y=[y,y1,y2,y3,y4,y5]; %我没想到 sound(y,Fs);
用到的函数代码为:
function y = gen_wave2( tone, rythm )
??? %Fs = 8192; ??? Fs = 44100; ?? %freqs = [ 262, 294,330, 349, 392, 440, 494, 131, 147, 165, 175, 196, 220, 247, 523,587,659,698,784,880,988,0,3520]; ?? freqs=[262, 294,330, 349, 392, 440, 494, 131,147,165,175,196,220,247,523, 587, 659, 698, 783, 880, 988,0,3520] ??? x = linspace(0, 2 * pi * rythm, floor(Fs * rythm));
?? y = sin(freqs(tone) * x) .*(1- x/(rythm * 2 *pi)); ?? ? end
|