VC 用发音函数Beep 播放简谱音乐

                  Win32 API中有个非常有意思的发音函数Beep(),可以用它在扬声器上播放简单的音调,如果按简谱来设置播放的内容,就可以播放一些简单的音乐了。

 

函数原型:

BOOL WINAPI Beep(

  __in          DWORD dwFreq,

  __in          DWORD dwDuration

);

函数说明:

第一个参数表示频率,用Hz为单位,可以在0x25到0x7FFF之间。

第二个参数表示持续的时间,以毫秒为单位。

 

与这个函数类似的是:

BOOL WINAPI MessageBeep(

  __in          UINT uType

);

这个函数使用要广泛的多。相当多的程序在MessageBox()之前都会调用MessageBeep()来发出一些提示音(实际上是播放系统音乐文件)。

可供选择的参数有:

MB_ICONASTERISK
0x00000040L

SystemAsterisk

MB_ICONEXCLAMATION
0x00000030L

SystemExclamation

MB_ICONHAND
0x00000010L

SystemHand 警告音

MB_ICONQUESTION
0x00000020L

SystemQuestion

MB_OK
0x00000000L

SystemDefault

 

 

再附上本人收藏的几个用Beep()播放音乐的程序,虽然音质无法与音频文件媲美,但做个小小的娱乐还是可以的:

生日快乐歌曲

// 播放  生日快乐#include <windows.h>void main(void)   {       unsigned FREQUENCY[] = {392,392,440,392,523,494,     392,392,440,392,587,523,     392,392,784,659,523,494,440,     689,689,523,587,523};       unsigned DELAY[] = {375,125,500,500,500,1000,     375,125,500,500,500,1000,     375,125,500,500,500,500,1000,     375,125,500,500,500,1000};       int i;       for (i = 0; i < 25; i++)       {     Beep(FREQUENCY[i], DELAY[i]);       }   } 

小酒窝

// 播放  小酒窝#include<iostream.h>#include<windows.h>enum fy{ d1 = 262, d1_ = 277, d2 = 294, d2_ = 311, d3 = 330, d4 = 349, d5 = 392, d5_= 415, d6 = 440, d6_= 466, d7 = 494, z1 = 523, z1_ = 554, z2 = 578, z2_ = 622, z3 = 659, z4 = 698, z4_ = 740, z5 = 784, z5_ = 831, z6 = 880, z6_ = 932, z7 = 988, g1 = 1046, g1_ = 1109, g2 = 1175, g2_ = 1245, g3 = 1318, g4 = 1397, g4_ = 1480, g5 = 1568, g5_ = 1661, g6 = 1760, g6_ = 1865, g7 = 1976, yaya = 0};struct yf{ enum fy s; int t;};int main()int n = 0struct yf a[1000]={{z3,125},{z3,50},{z4,100},{z3,50},{z2,50}, //5 {z2,125},{z2,50},{z5,100},{z2,50},{z1,50}, //10 {z1,125},{z1,50},{z6,100},{z5,100},    //14 {z3,125},{z4,25},{z3,25},{z2,100},{z2,100}, //19 {z3,125},{z3,50},{z4,100},{z5,50},{z3,50}, //24 {z5,125},{z5,50},{g2,100},{z7,50},{g1,50}, //29 {g1,200},{g1,50},{z7,50},{z6,50},{z7,50}, //34 {g1,125},{z7,50},{g1,200},      //37 {g1,50},{z7,50},{g1,50},{z7,50},{g1,100},{z3,50},{z5,50}, //44 {z5,250},{z6,50},{z7,50},      //47 {g1,50},{z7,50},{g1,50},{z7,50},{g1,100},{g2,100},{g3,100}, //54 {g3,300},{z6,50},{z7,50},   //57 {g1,50},{z7,50},{g1,50},{z7,50},{g1,100},{g3,100}, //63 {z7,50},{z6,50},{z7,50},{z6,50},{z7,50},{z5,50},{z5,50},{g1,50}, //71 {g1,200},{g3,75},{g4,25},{g4,50},{g3,50}, //76 {g2,200},{yaya,50},{z5,50},{g1,50},{g2,50}, //81 {g3,50},{g1,50},{g1,50},{z5,50},{z5,100},{z5,50},{g3,50}, //88 {g2,50},{g3,50},{g2,50},{g1,50},{z5,50},{z5,50},{z6,50},{z7,50}, //96 {g1,50},{z6,50},{z6,50},{z3,50},{z3,100},{yaya,50},{z7,50}, //103 {g1,50},{z7,50},{g1,50},{g2,50},{z6,50},{z5,50},{z5,100}, //110 {z6,100},{z5,50},{z6,50},{z7,100},{g3,50},{g4,50}, //116 {g3,50},{g4,50},{g3,50},{g2,50},{g2,50},{g1,100},{z5,50}, //123 {z5,50},{z4,50},{z4,50},{g1,50},{g1,50},{g3,50},{g3,50},{g1,50}, //131 {g2,200},{yaya,50},{z5,50},{g1,50},{g2,50}, //136 {g3,50},{g1,50},{g1,50},{z5,50},{z5,100},{z5,50},{g3,50}, //143 {g2,50},{g3,50},{g2,50},{g1,50},{z5,50},{z5,50},{z6,50},{z7,50}, //151 {g1,50},{z6,50},{z6,50},{z3,50},{z3,100},{z3,50},{z7,50}, //158 {g1,50},{z7,50},{g1,50},{g2,50},{z6,50},{z5,50},{z5,100}, //165 {z6,50},{z7,50},{g1,50},{g2,100},{g3,50},{g4,50}, //171 {g3,50},{g4,50},{g2,50},{g1,50},{g2,50},{g1,50},{g1,100}, //178 {g2,50},{g1,50},{g2,50},{z6,50},{g1,100},{g2,50},{g1,50}, //185 {g1,400},   //186 {g2,50},{g1,50},{g2,50},{z6,50},{z7,100},{z7,50},{g1,50}, //193 {g1,400},   //194 }; struct yf *atop; atop = a; n = 194while(n--) {  Beep(atop->s,atop->t*5);  atop++; } return 0;}
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值