vlambda博客
学习文章列表

R语言音乐代码—为母亲节献歌两首

专为大数据分析而生的R语言,功能自然强大,用R语言播放两首歌《世上只有妈妈好》、《母亲》,祝天下母亲健康、快乐!

 

母亲节到了,为天下勤劳、善良的母亲们送上两首歌曲,与普通形式不同,这次是用R语言代码进行播放,希望用一种全新的形式诠释新时期的母爱。

准备:1、安装R语言; 2、加载两个包signal和tuneR。

函数说明:

1、七个函数CDEFGAB,代表音符1-7;每个函数有两个参数long(时长)high(高音=2、中音=1,低音=0.5,其余按倍数缩放)。

2、声音用正弦波来描述,sin(wx); 时间序列采取1/8000间隔来近似连续值,和放电影一个原理。

3、函数会启用windows的播放器自动播放,如果第一次没有配置播放器的可以在自动弹出后,一路默认,配置过的忽略本条。

第一曲:《世上只有妈妈好》

代码如下:

library(signal)library(tuneR)C<-function(long,high){ t = seq(0, long, 1/8000) u =(2^15-1)*sin(2*pi*high*330*t)return(u)}D<-function(long,high){ t = seq(0, long, 1/8000) u =(2^15-1)*sin(2*pi*high*370*t)return(u)}E<-function(long,high){ t = seq(0, long, 1/8000) u = (2^15-1)*sin(2*pi*high*415*t)return(u)}F<-function(long,high){ t = seq(0, long, 1/8000) u =(2^15-1)*sin(2*pi*high*440*t)return(u)}G<-function(long,high){ t = seq(0, long, 1/8000) u =(2^15-1)*sin(2*pi*high*494*t)return(u)}A<-function(long,high){ t = seq(0, long, 1/8000) u =(2^15-1)*sin(2*pi*high*554*t)return(u)}B<-function(long,high){ t = seq(0, long, 1/8000) u =(2^15-1)*sin(2*pi*high*622*t)return(u)}#谨以此歌献给世界上最伟大的人!!!u=c(A(1.5,1),G(0.5,1),E(1,1),G(1,1),C(1,2),A(0.5,1),G(0.5,1),A(2,1),E(1,1),G(0.5,1),A(0.5,1),G(1,1),E(1,1),C(0.5,1),A(0.5,0.5),G(0.5,1),E(0.5,1),D(2,1),D(1.5,1),E(0.5,1),G(1,1),G(0.5,1),A(0.5,1),E(1,1),D(1,1),C(2,1),G(1.5,1),E(0.5,1),D(0.5,1),C(0.5,1),A(0.5,0.5),C(0.5,1),G(3,0.5))w = Wave(u, samp.rate = 8000, bit=16play(w)



 

第二曲:《母亲》-阎维文

代码如下:

u=c(G(0.5,1),G(0.25,1),A(0.25,1),C(0.75,2),B(0.25,1),A(0.5,1),E(0.25,1),D(0.25,1),C(1,1),E(0.5,1),D(0.5,2),D(0.5,2),B(0.25,1),A(0.25,1),E(0.25,1),G(2,1),G(0.5,1),G(0.25,1),A(0.25,1),C(0.75,2),B(0.25,1),A(0.5,1),G(0.25,1),A(0.25,1),E(1,1),#A(0.5,0.5),A(0.5,1),A(0.25,1),G(0.25,1),G(0.25,1),E(0.25,1),D(2,1),E(0.5,1),E(0.25,1),D(0.25,1),G(0.5,1),G(0.25,1),E(0.25,1),D(0.5,1),E(0.25,1),D(0.25,1),C(0.75,1),D(0.25,1),E(0.75,1),G(0.25,1),B(0.25,1),A(0.25,1),G(0.5,1),A(2,1),#C(0.5,2),C(0.25,2),A(0.25,1),C(0.75,2),C(0.25,2),A(0.5,1),G(0.25,1),E(0.25,1),D(1,1),A(0.5,0.5),A(0.5,1),G(0.25,1),E(0.25,1),E(0.25,1),D(0.25,1),C(1.5,1),C(0.25,2),D(0.25,2),E(0.25,2),E(0.5,2),E(0.25,2),D(0.5,2),E(0.25,2),D(0.25,2),C(1.5,2),A(0.25,1),C(0.25,2),#D(0.25,2),D(0.5,2),E(0.25,2),D(0.25,2),B(0.25,1),A(0.5,1),G(2,1),C(0.75,2),A(0.25,1),C(0.5,2),C(0.25,2),A(0.25,1),G(0.5,1),A(0.25,1),G(0.25,1),E(1,1),#A(0.75,1),C(0.25,2),E(0.25,2),D(0.25,2),C(0.5,2),D(1.5,2),C(0.25,2),D(0.25,2),E(0.25,2),E(0.5,2),E(0.25,2),D(0.5,2),E(0.25,2),D(0.25,2),C(1.5,2),A(0.25,1),C(0.25,2),D(0.75,2),E(0.25,2),D(0.25,2),B(0.25,1),A(0.5,1),G(2,1),#A(0.25,1),C(0.25,2),C(0.25,2),A(0.25,1),C(0.5,2),C(0.25,2),A(0.25,1),G(0.5,1),A(0.25,1),G(0.25,1),G(0.01,1),E(1,1),A(0.5,1),E(0.5,2),D(0.25,2),C(0.25,2),A(0.5,1),C(2,2),#G(0.5,1),G(0.25,1),A(0.25,1),C(0.75,2),B(0.25,1),A(0.5,1),E(0.25,1),D(0.25,1),C(1,1),E(0.5,1),D(0.5,2),D(0.5,2),B(0.25,1),A(0.25,1),E(0.25,1),G(2,1),G(0.5,1),G(0.25,1),A(0.25,1),C(0.75,2),B(0.25,1),A(0.5,1),G(0.25,1),A(0.25,1),E(1,1),#A(0.5,0.5),A(0.5,1),A(0.25,1),G(0.25,1),G(0.25,1),E(0.25,1),D(2,1),E(0.5,1),E(0.25,1),D(0.25,1),G(0.5,1),G(0.25,1),E(0.25,1),D(0.5,1),E(0.25,1),D(0.25,1),C(0.75,1),D(0.25,1),E(0.75,1),G(0.25,1),B(0.25,1),A(0.25,1),G(0.5,1),A(2,1),#C(0.5,2),C(0.25,2),A(0.25,1),C(0.75,2),C(0.25,2),A(0.5,1),G(0.25,1),E(0.25,1),D(1,1),A(0.5,0.5),A(0.5,1),G(0.25,1),E(0.25,1),E(0.25,1),D(0.25,1),C(1.5,1),C(0.25,2),D(0.25,2),E(0.25,2),E(0.5,2),E(0.25,2),D(0.5,2),E(0.25,2),D(0.25,2),C(1.5,2),A(0.25,1),C(0.25,2),#D(0.25,2),D(0.5,2),E(0.25,2),D(0.25,2),B(0.25,1),A(0.5,1),G(2,1),C(0.75,2),A(0.25,1),C(0.5,2),C(0.25,2),A(0.25,1),G(0.5,1),A(0.25,1),G(0.25,1),G(0.01,1),E(1,1),#A(0.75,1),C(0.25,2),E(0.25,2),D(0.25,2),C(0.5,2),D(1.5,2),C(0.25,2),D(0.25,2),E(0.25,2),E(0.5,2),E(0.25,2),D(0.25,2),D(0.25,2),E(0.25,2),D(0.25,2),C(1.5,2),A(0.25,1),C(0.25,2),D(0.75,2),E(0.25,2),D(0.25,2),B(0.25,1),A(0.5,1),G(2,1),#A(0.25,1),C(0.25,2),C(0.25,2),A(0.25,1),C(0.5,2),C(0.25,2),A(0.25,1),G(0.5,1),A(0.25,1),G(0.25,1),E(1,1),A(0.5,1),E(0.5,2),D(0.25,2),C(0.25,2),C(0.25,2),A(0.5,1),C(1.5,2),A(0.5,1),G(0.5,2),D(0.25,2),C(0.25,2),C(0.25,2),A(0.5,1),C(1.75,2))w=Wave(u,samp.rate=8000,bit=16)play(w)

 

扩展:在此基础上,大家可以自由发挥,有创作好的歌曲,可以把R语言写的最后歌曲序列u=c()发在留言中,进行交流。

留言格式:

歌名《***》:u=c(G(0.5,1),G(0.25,1),…)