龙空技术网

程序员怎样用最简单代码做出送给女神最浪漫的礼物?

孤单北京城 182

前言:

目前小伙伴们对“beep函数的使用”都比较关怀,看官们都需要学习一些“beep函数的使用”的相关文章。那么小编在网上网罗了一些有关“beep函数的使用””的相关资讯,希望看官们能喜欢,大家一起来了解一下吧!

各位还在加班的程序员们,看着朋友圈各种秀恩爱撒狗粮有没有羡慕嫉妒恨呢?

不用慌不用急,不要担心枯燥的代码无法俘获女神的芳心,其实代码里有很多有趣又很酷的操作,今天就来介绍一种用最简单代码做出送给女神最浪漫的礼物。

有时候会看到神犇用C++函数做的音乐,不用惊叹,其实你也可以做到。Windows API 提供了一个这样奇妙的发音函数,它就是Beep函数。Beep可以通过控制主板扬声器的发声频率和节拍来演奏美妙的旋律,本文根据音乐简谱来演奏出优美的歌曲,用以展现Beep的疯狂魅力。让你再也不用担心送啥礼物给女神了,要送就送专业高级点的,保证让她认识到你的认真的加班工作的魅力,感动到哭有木有[流泪]!!!

首先我们来Beep的函数原型:

BOOL Beep(    DWORD dwFreq;          //指定要发出的频率(HZ)    DWORD dwDuration;  //指定发音的时长,以毫秒为单位);  
Beep的实际应用

比如说我要发do这个音,do应该是523hz,且如果要发一拍的话,就相当于400毫秒。那这个函数就成了这样:Beep(523,400);

但是,如果我们要连续的发出多个音的话,比如说像这样:

Beep(523,400);//do  Beep(578,400);//re  Beep(659,400);//mi  

但是乐谱上音节多了这样写太麻烦了,所以,我建议大家用define函数宏定义一下这些音,比如说这样:#define re 578#define mi 659,这样,上面的三行语句就简化了成这样:

Beep(do,400);Beep(re,400);Beep(mi,400);

下面是收集整理的音阶表,其中,q前缀为低音,1后缀为高音,s前缀为半音阶。

#define qdo 262 #define qre 294#define qmi 330#define qfa 349#define qso 392#define qla 440#define qsi 494#define do 523#define re 578#define mi 659#define fa 698#define so 784#define la 880#define si 988#define do1 1046#define re1 1175#define mi1 1318#define fa1 1480#define so1 1568#define la1 1760#define si1 1976#define sqdo 277#define sqre 311#define sqfa 370#define sqso 415#define sqla 466#define sdo 554#define sre 622#define sfa 740#define sso 831#define sla 932#define sdo1 1046#define sre1 1245#define sfa1 1480#define sso1 1661#define sla1 1865

惊喜彩蛋,用Beep函数演奏#宫崎骏#大师的天空之城。

程序运行的那一刻,c++发出的声音太浪漫了,还不赶紧记下代码,转发给你心中的女生[灵光一闪]!!!保证让她感动到哭[酷拽]

#include <cstdio>#include <windows.h>#define qdo 262 #define qre 294#define qmi 330     //q前缀为低音,1后缀为高音,s前缀为半音阶 #define qfa 349#define qso 392#define qla 440#define qsi 494#define do 523#define re 578#define mi 659#define fa 698#define so 784#define la 880#define si 988#define do1 1046#define re1 1175#define mi1 1318#define fa1 1480#define so1 1568#define la1 1760#define si1 1976#define sqdo 277#define sqre 311#define sqfa 370#define sqso 415#define sqla 466#define sdo 554#define sre 622#define sfa 740#define sso 831#define sla 932#define sdo1 1046#define sre1 1245#define sfa1 1480#define sso1 1661#define sla1 1865 int main(){int pai=400,ban=200;int ting=128; Sleep(1000); Beep(la,ban);Beep(si,ban);Sleep(ting); Beep(do1,pai+ban);Beep(si,ban);Sleep(ting);Beep(do1,pai);Sleep(ting);Beep(mi1,pai);Sleep(ting); Beep(si,3*pai);Sleep(ting);Beep(mi,ban);Beep(mi,ban); Beep(la,ban+pai);Beep(so,ban);Sleep(ting);Beep(la,pai);Sleep(ting);Beep(do1,pai);Sleep(ting); Beep(so,2*pai);Sleep(ting);Sleep(pai);Beep(mi,ban);Sleep(ting/2);Beep(mi,ban);Sleep(ting/2); Beep(fa,pai+ban);Beep(mi,ban);Sleep(ting);Beep(fa,ban);Beep(do1,ban+pai);Sleep(ting); Beep(mi,2*pai);Sleep(ting);Sleep(ban);Beep(do1,ban);Sleep(ting/2);Beep(do1,ban);Sleep(ting/2);Beep(do1,ban);Sleep(ting/2); Beep(si,ban+pai);Beep(sfa,ban);Sleep(ting);Beep(sfa,pai);Beep(si,pai);Sleep(ting); Beep(si,2*pai);Sleep(ting);Sleep(pai);Beep(la,ban);Beep(si,ban);Sleep(ting); Beep(do1,pai+ban);Beep(si,ban);Sleep(ting);Beep(do1,pai);Sleep(ting);Beep(mi1,pai);Sleep(ting); Beep(si,2*pai);Sleep(ting);Sleep(pai);Beep(mi,ban);Sleep(20);Beep(mi,ban);Sleep(ting); Beep(la,pai+ban);Beep(so,ban);Sleep(ting);Beep(la,pai);Sleep(ting);Beep(do1,pai);Sleep(ting); Beep(so,3*pai);Sleep(ting+ban);Beep(mi,ban);Sleep(ting/2); Beep(fa,pai);Sleep(ting);Beep(do1,ban);Beep(si,ban);Sleep(20);Beep(si,pai);Sleep(ting);Beep(do1,pai);Sleep(ting); Beep(re1,ban);Sleep(20);Beep(re1,ban);Sleep(20);Beep(mi1,ban);Sleep(ting/2);Beep(do1,pai);Sleep(ting+pai); Beep(do1,pai);Beep(si,ban);Sleep(ting);Beep(la,ban);Sleep(20);Beep(la,ban);Sleep(ting);Beep(si,pai);Sleep(ting);Beep(sso,pai);Sleep(ting); Beep(sso,2*pai);Sleep(ting+pai);Beep(do1,ban);Beep(re1,ban);Sleep(ting); Beep(mi1,pai+ban);Beep(re1,ban);Sleep(ting);Beep(mi1,pai);Sleep(ting);Beep(fa1,pai);Sleep(ting); Beep(re1,2*pai);Sleep(pai+ting);Beep(so,ban);Sleep(20);Beep(so,ban);Sleep(ting); Beep(do1,ban);Beep(si,ban);Sleep(ting);Beep(do1,pai);Sleep(ting);Beep(mi1,pai);Sleep(ting); Beep(mi1,2*pai);Sleep(ting+2*pai); Beep(la,ban);Beep(si,ban);Sleep(ting);Beep(do1,pai);Sleep(ting);Beep(si,pai);Sleep(ting);Beep(re1,ban);Sleep(20);Beep(re1,ban);Sleep(ting); Beep(do1,pai+ban);Beep(so,ban);Sleep(20);Beep(so,pai);Sleep(pai+ting); Beep(fa1,pai);Sleep(ting);Beep(mi1,pai);Sleep(ting);Beep(re1,pai);Sleep(ting);Beep(do1,pai);Sleep(ting); Beep(mi1,4*pai); Beep(mi1,pai*2);Sleep(pai+ting);Beep(mi1,pai);Sleep(ting); Beep(la1,2*pai);Sleep(ting);Beep(so1,pai);Sleep(ting);Beep(so1,pai);Sleep(ting); Beep(mi1,ban);Sleep(ting/2);Beep(re1,ban);Sleep(ting);Beep(do1,pai);Sleep(ting+ban);Beep(do1,ban);Sleep(ting); Beep(re1,pai);Sleep(ting);Beep(do1,ban);Beep(re1,ban);Sleep(20);Beep(re1,ban);Sleep(ting);Beep(so1,pai);Sleep(ting); Beep(mi1,2*pai);Sleep(ting+pai);Beep(mi,pai);Sleep(ting); Beep(la1,2*pai);Sleep(ting);Beep(so1,2*pai);Sleep(ting); Beep(mi1,ban);Beep(re1,ban);Sleep(ting);Beep(do1,2*pai);Sleep(ting+ban);Beep(do1,ban);Sleep(ting); Beep(re1,pai);Sleep(ting);Beep(do1,ban);Beep(re1,ban);Sleep(20);Beep(re1,ban);Sleep(ting);Beep(si,pai);Sleep(ting); Beep(la,2*pai);Sleep(ting);Beep(la,ban);Beep(si,ban); Beep(do1,pai+ban);Beep(si,ban);Sleep(ting);Beep(do1,pai);Sleep(ting);Beep(mi1,pai);Sleep(ting); Beep(si,3*pai);Sleep(ting);Beep(mi,ban);Beep(mi,ban); Beep(la,ban+pai);Beep(so,ban);Sleep(ting);Beep(la,pai);Sleep(ting);Beep(do1,pai);Sleep(ting); Beep(so,2*pai);Sleep(ting);Sleep(pai);Beep(mi,ban);Sleep(ting/2);Beep(mi,ban);Sleep(ting/2); Beep(fa,pai+ban);Beep(mi,ban);Sleep(ting);Beep(fa,ban);Beep(do1,ban+pai);Sleep(ting); Beep(mi,2*pai);Sleep(ting);Sleep(ban);Beep(do1,ban);Sleep(ting/2);Beep(do1,ban);Sleep(ting/2);Beep(do1,ban);Sleep(ting/2); Beep(si,ban+pai);Beep(sfa,ban);Sleep(ting);Beep(sfa,pai);Beep(si,pai);Sleep(ting); Beep(si,2*pai);Sleep(ting);Sleep(pai);Beep(la,ban);Beep(si,ban);Sleep(ting); Beep(do1,pai+ban);Beep(si,ban);Sleep(ting);Beep(do1,pai);Sleep(ting);Beep(mi1,pai);Sleep(ting); Beep(si,2*pai);Sleep(ting);Sleep(pai);Beep(mi,ban);Sleep(20);Beep(mi,ban);Sleep(ting); Beep(la,pai+ban);Beep(so,ban);Sleep(ting);Beep(la,pai);Sleep(ting);Beep(do1,pai);Sleep(ting); Beep(so,3*pai);Sleep(ting+ban);Beep(mi,ban);Sleep(ting/2); Beep(fa,pai);Sleep(ting);Beep(do1,ban);Beep(si,ban);Sleep(20);Beep(si,pai);Sleep(ting);Beep(do1,pai);Sleep(ting); Beep(re1,ban);Sleep(20);Beep(re1,ban);Sleep(20);Beep(mi1,ban);Sleep(ting/2);Beep(do1,pai);Sleep(ting+pai); Beep(la,4*pai); Sleep(1000); main(); }

//文章代码内容参考了CSDN上作者v1t1p9hvbd的“C++中发声函数Beep详解”

标签: #beep函数的使用