----------------解决方案--------------------------------------------------------
以前看到过这样的帖子..留下啦..哈..现在给你.你看看.所谓播放音乐其实就是系统响声.
要播放真正的音乐..就不知道啦
#include<stdio.h>
#include<dos.h>
#include<bios.h>
#include<time.h>
#define N1 32 /*一拍*/
#define N2 16
#define N4 8
#define N8 4
#define N16 2
#define END 0
/*其中:N1,N2,N4,N6,N8,N16分别代表
一拍,二分之一拍,四分之一拍,
八分之一拍,十六分之一拍。*/
enum NOTES
{
C10=131,D10=147,E10=165,F10=175,G10=196,A10=220,B10=247,
/*低度音调分别是:1,2,3,4,5,6,7*/
C0=262, D0=286, E0=330, F0=349, G0=440, A0=440, B0=494,
/*中度音调分别是:1,2,3,4,5,6,7*/
C1=523, D1=158, E1=659, F1=698, G1=784, A1=880, B1=998,
/*高度音调分别是:1,2,3,4,5,6,7*/
C2=1047,D2=1175,E2=1319,F2=1397,G2=1568,A2=1760,B2=1976
/*高八度音调分别是:1,2,3,4,5,6,7*/
}
song1[]={
G0,N16,G0,N8,A0,N8,G0,N8,C1,N8,B0,N4,G0,N16,
G0,N8,A0,N8,G0,N8,D1,N8,C1,N4,G0,N16,G0,N8,G1,N8,
E1,N8,C1,N8,B0,N16,B0,N8,A0,N4,F1,N16,F1,N8,E1,
N8,C1,N8,D1,N8,C1,N4,END,END
/*以上是曲子代码,你可以嵌入自己喜欢的歌曲代码*/
/*注意:数组中的偶数元素代表音符奇数元素代表拍长.*/
};
song2[]={
C10,N1,D10,N1,E10,N1,F10,N1,G10,N1,A10,N1,B10,N1,
C10,N2,D10,N2,E10,N2,F10,N2,G10,N2,A10,N2,B10,N2,
C10,N4,D10,N4,E10,N4,F10,N4,G10,N4,A10,N4,B10,N4,
C10,N8,D10,N8,E10,N8,F10,N8,G10,N8,A10,N8,B10,N8,
C10,N16,D10,N16,E10,N16,F10,N16,G10,N16,A10,N16,B10,N16,
C0,N1,D0,N1,E0,N1,F0,N1,G0,N1,A0,N1,B0,N1,
C0,N2,D0,N2,E0,N2,F0,N2,G0,N2,A0,N2,B0,N2,
C0,N4,D0,N4,E0,N4,F0,N4,G0,N4,A0,N4,B0,N4,
C0,N8,D0,N8,E0,N8,F0,N8,G0,N8,A0,N8,B0,N8,
C0,N16,D0,N16,E0,N16,F0,N16,G0,N16,A0,N16,B0,N16,
C1,N1,D1,N1,E1,N1,F1,N1,G1,N1,A1,N1,B1,N1,
C1,N2,D1,N2,E1,N2,F1,N2,G1,N2,A1,N2,B1,N2,
C1,N4,D1,N4,E1,N4,F1,N4,G1,N4,A1,N4,B1,N4,
C1,N8,D1,N8,E1,N8,F1,N8,G1,N8,A1,N8,B1,N8,
C1,N16,D1,N16,E1,N16,F1,N16,G1,N16,A1,N16,B1,N16,
C2,N1,D2,N1,E2,N1,F2,N1,G2,N1,A2,N1,B2,N1,
C2,N2,D2,N2,E2,N2,F2,N2,G2,N2,A2,N2,B2,N2,
C2,N4,D2,N4,E2,N4,F2,N4,G2,N4,A2,N4,B2,N4,
C2,N8,D2,N8,E2,N8,F2,N8,G2,N8,A2,N8,B2,N8,
C2,N16,D2,N16,E2,N16,F2,N16,G2,N16,A2,N16,B2,N16,END
/*以上是音符测试代码,你可以通过系统发出的音调选择你需要的音符*/
/*注意:数组中的偶数元素代表音符奇数元素代表拍长.*/
};
song3[]= {
C0,N8,C0,N8,G0,N8,G0,N8,A0,N8,A0,N8,G0,N4,
F0,N8,F0,N8,E0,N8,E0,N8,D0,N8,D0,N8,C0,N4,
G0,N8,G0,N8,F0,N4,E0,N8,E0,N8,D0,N4,G0,N8,
G0,N8,F0,N8,F0,N8,E0,N8,E0,N8,D0,N4,C0,N8,
E0,N8,G0,N4,A0,N8,C1,N8,G0,N4,F0,N8,F0,N8,
E0,N8,E0,N4,D0,N8,D0,N8,C0,N4,C0,N8,C0,N8,
G0,N8,G0,N8,A0,N8,A0,N8,G0,N4,F0,N8,F0,N8,
E0,N8,E0,N8,D0,N8,D0,N8,C0,N4,G0,N8,G0,N8,
F0,N4,E0,N8,E0,N8,D0,N4,G0,N8,G0,N8,F0,N8,
F0,N8,E0,N8,E0,N8,D0,N4,C0,N8,E0,N8,G0,N4,
A0,N8,C1,N8,G0,N4,F0,N8,F0,N8,E0,N8,E0,N4,
D0,N8,D0,N8,C0,N4,END
/*以上是曲子代码,你可以嵌入自己喜欢的歌曲代码*/
/*注意:数组中的偶数元素代表音符奇数元素代表拍长.*/
};
song4[]= {
C10,N4,B0,N4,G0,N8,A0,N4,E0,N16,D0,N8,C0,N8,
D0,N8,G0,N8,E0,N8,E0,N4,E0,N8,G0,N8,E0,N8,
D0,N8,D0,N4,A0,N8,A0,N8,G0,N8,A0,N8,B0,N8,
G0,N8,G0,N8,E0,N4,C10,N8,B0,N8,G0,N8,A0,N4,
E0,N8,D0,N8,C0,N8,D0,N8,G0,N8,E0,N4,C0,N8,D0,N8,
E0,N4,G0,N8,E0,N8,D0,N4,C0,N8,B10,N8,C0,N8,B10,N8,
A10,N4,A0,N8,A0,N8,A0,N8,C1,N8,D1,N8, E1,N8,E1,N8,
C1,N8,A0,N8,A0,N8,B0,N16,A0,N16,G0,N8,E0,N8,A0,N8,
A0,N8,C1,N8,D1,N8,E1,N8,E1,N8,C1,N8,A0,N8,A0,N8,
B0,N8,A0,N8,B0,N16,A0,N16,G0,N8,E0,N8,D1,N16,D1,N16,D1,N8,
D1,N8,A0,N16,B0,N16,A0,N8,G0,N8,E0,N8,D1,N16,D1,N16,D1,N8,
A0,N16,B0,N16,A0,N8,G0,N8,E0,N8,A0,N8,A0,N8,A0,N4,A0,N8,
A0,N8,A0,N4,C10,N8,B0,N8,G0,N8,A0,N4,E0,N16,D0,N16,C0,N8,
D0,N8,G0,N8,E0,N4,E0,N4,E0,N16,G0,N16,E0,N16,
D0,N16,D0,N4,A0,N16,A0,N16,G0,N8,A0,N8,B0,N8,
G0,N16,G0,N16,E0,N4,C10,N8,B0,N8,G0,N8,A0,N4,
E0,N16,D0,N16,C0,N8,D0,N8,G0,N8,E0,N4,C0,N16,D0,N16,
E0,N4,G0,N8,E0,N8,D0,N4,C0,N8,B10,N8,C0,N16,B10,N16,
A10,N4,END};
/*以下是播放程序代码*/
void MusicPlay(int *song)
{
int note=0,fre,dur,control;
clock_t goal;
while(song[note]!=0)
{
fre=song[note];
dur=song[note+1];
if(kbhit())break;
/*int kbhit()函数返回最近所敲的按键*/
if(fre)
{
outportb(0x43,0xB6);
/*函数void outportb(int port,char byte)
将字节byte写入指定的输出端口port*/
fre=(unsigned)(1193180L/fre);
outportb(0x42,(char)(fre>>8));
outportb(0x42,(char)(fre>>8));
control=inportb(0x61);
/*函数int inportb(int port)从指定的
输入端口读入一个字节,并返回这个字节*/
outportb(0x61,(control)|0x3);
}
goal=(clock_t)dur+clock();
while(goal>clock());
if(fre)
outportb(0x61,control);
goal=(clock_t)0;
note=note+2;
}
}
/*以下是主函数其中包括异常处理*/
void main(void)
{ int n;
do{
printf("If you want to listen music please input a number(1-4):");
if(scanf("%d",&n))
{
if(n<=4&&0<=n)
switch(n)
{
case 1:MusicPlay(song1);break;
case 2:MusicPlay(song2);break;
case 3:MusicPlay(song3);break;
case 4:MusicPlay(song4);break;
}
else printf("\n\t\tYou can input '0' to exit!\n\n");
}
else {printf("\n\tInput error,please prass any key to exit!\n");break;}
}while(n!=0);
return ;
getch();
}
WIN-TC~!
[此贴子已经被作者于2006-4-4 19:31:33编辑过]
----------------解决方案--------------------------------------------------------
我试了一下,感觉挺不错的 呵呵 不过好象没办法退出
----------------解决方案--------------------------------------------------------
....输入 0为退出 你可以输入0后再按任意键..或者 在
void main(void)
{ int n;
do{
printf("If you want to listen music please input a number(1-4):");
if(scanf("%d",&n))
{
if(n<=4&&0<=n)
switch(n)
{
case 1:MusicPlay(song1);break;
case 2:MusicPlay(song2);break;
case 3:MusicPlay(song3);break;
case 4:MusicPlay(song4);break;
}
else printf("\n\t\tYou can input '0' to exit!\n\n");
}
else {printf("\n\tInput error,please prass any key to exit!\n");break;}
}while(n!=0);
return ; /* 这是因为 getch() 的原因才需要再按一下 加 return 后就不用啦 */
getch();
}
----------------解决方案--------------------------------------------------------
好啊 我喜欢
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
我用.net2005
----------------解决方案--------------------------------------------------------
用.net2005
怎么不行呀?
提示说没有bios.h这个头文件?
怎么回事?
----------------解决方案--------------------------------------------------------
^_^ 剑人你...
----------------解决方案--------------------------------------------------------
可以播放歌曲吗?
----------------解决方案--------------------------------------------------------