C++中发声函数Beep用法
作者:zhcs
本文给大家分享的是Beep函数的用法以及会使扬声器发出简单的声音的示例,有需要的小伙伴可以参考下
•以前,我听过一个神犇用C++函数做的音乐,当时的心里就十分激动:哇,好厉害啊,好神啊。
•这次,我终于通过自己无助的盲目的摸索、研究,写出了这篇文章
下面是正文:
•其实啊,Windows API 就提供了一个这样奇妙的发音函数,它就是Beep函数。Beep可以通过控制主板扬声器的发声频率和节拍来演奏美妙的旋律,本文根据音乐简谱来演奏几首优美的歌曲,用以展现Beep的疯狂魅力。
首先我们来Beep的函数原型:
BOOL Beep( DWORD dwFreq; DWORD dwDuration; );
参数说明:
•dwFreq 指定要发出的频率(HZ)
•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函数演奏的天空之城
#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(); }