程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++中發聲函數Beep用法

C++中發聲函數Beep用法

編輯:關於C++

C++中發聲函數Beep用法。本站提示廣大學習愛好者:(C++中發聲函數Beep用法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++中發聲函數Beep用法正文


C++中發聲函數Beep用法

作者:zhcs

本文給大家分享的是Beep函數的用法以及會使揚聲器發出簡單的聲音的示例,有需要的小伙伴可以參考下

•以前,我聽過一個神犇用C++函數做的音樂,當時的心裡就十分激動:哇,好厲害啊,好神啊。

•這次,我終於通過自己無助的盲目的摸索、研究,寫出了這篇文章(此時我的內心是雞凍的233)

下面是正文:

•其實啊,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();

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved