书上的钢琴例子,但运行结果不对
程序代码:
#include "stdio.h" #include "dos.h" #include "bios.h" #include "graphics.h" int ioport=0x388; /*FM端口*/ void writeFM(char reg,char data); /*写FM端口函数*/ char readFM(); /*读FM端口函数*/ void FMsound(int frequency,int block); /*用FM发声,frequency:频率,block:音阶*/ void delayFM(int time); void FMdelay(int time); void FMsoundoff(int frequency,int block); /*关闭声音*/ void bar3Dpress(int left,int top,int right,int bottom); /*3D窗口函数*/ void bar3D(int left,int top,int right,int bottom); void initpiano(); /*初始化钢琴音色,C程序员可以改变音色*/ int key[21][2]={131,0x7a,147,0x78,165,0x63,175, 0x76,196,0x62,220,0x6e,247,0x6d, 262,0x61,294,0x73,330,0x64,349,0x66, 392,0x67,440,0x68,494,0x6a,524,0x71, 587,0x77,659,0x65,698,0x72,784, 0x74,880,0x79,998,0x75}; /*为Z~M,A~J,Q~U这21个键定义音高*/ void main() { int gd=DETECT,gm,cursound=0,i; char input; /*按下的键的键码*/ char * mask; /*图像缓冲*/ /*初始化BGI图形模式*/ initgraph(&gd,&gm,""); cleardevice(); /*绘制背景以及电子钢琴*/ setfillstyle(XHATCH_FILL,BLUE); bar(0,0,639,479); mask=(char *)malloc(imagesize(0,0,28,200)); getimage(0,0,28,200,mask); bar3D(0,90,639,320); for(i=0;i<21;i++) { bar3D(i*30+5,200,i*30+32,300); } bar3D(5,100,630,190); settextstyle(0,0,3); /*绘制版权说明和作者介绍*/ setcolor(BLUE); outtextxy(172,122,"FM PIANO DELUX"); setcolor(LIGHTBLUE); outtextxy(170,120,"FM PIANO DELUX"); settextstyle(0,0,1); setcolor(BLACK); outtextxy(220,150,"Produced by suprman 99.9.6"); outtextxy(220,165," Copyleft Software"); /*初始化声卡上的FM合成器,并定义为钢琴音色*/ initpiano(); do { input=getch(); for(i=0;i<21;i++) { if(key[i][1]==input) { /*putimage(i*30+5,200,mask,XOR_PUT);*/ bar3Dpress(i*30+5,200,i*30+32,300); FMsoundoff(cursound,4); FMsound(key[i][0],4); cursound=key[i][0]; FMdelay(4); bar3D(i*30+5,200,i*30+32,300); /*putimage(i*30+5,200,mask,XOR_PUT);*/ } } }while(input!=27); /*按Esc键退出*/ closegraph(); /*关闭图形模式*/ free(mask); /*清除缓冲*/ } void writeFM(char reg,char data) { outportb(ioport,reg); delay(1000); outportb(ioport+1,data); delay(2000); } char readFM() { char data; data=inportb(ioport); return(data); } void FMsound(int frequency,int block) {int fnh,fnl,block2,blfnh,kblfnh,key; fnl=frequency&0x00ff; fnh=frequency&0xff00; fnh=fnh>>8; block2=block<<2; blfnh=fnh|block2; key=0x20; kblfnh=blfnh|key; writeFM(0xa0,fnl); writeFM(0xb0,kblfnh); } void delayFM(int time) { unsigned char sl,i; writeFM(0x04,0x80); writeFM(0x03,216); writeFM(0x04,0x42); for(i=1;i<time;i++) { do {sl=readFM(); sl=sl&0x00e0; }while(sl<0xa0); writeFM(0x04,0x82); } } void FMdelay(int time) { int i; for(i=time;i>=0;i--) delayFM(5); } void FMsoundoff(int frequency,int block) {int fnh,fnl,block2,blfnh,kblfnh,key; fnh=frequency&0x00ff; fnh=fnh>>8; block2=block<<2; blfnh=fnh|block2; key=0x00; kblfnh=blfnh|key; writeFM(0xb0,kblfnh); } void bar3Dpress(int left,int top,int right,int bottom) {setfillstyle(SOLID_FILL,LIGHTGRAY); bar(left,top,right,bottom); setcolor(BLACK); line(left,top,left,bottom); line(left,top,right,top); setcolor(WHITE); line(right,top,right,bottom); line(left,bottom,right,bottom); } void bar3D(int left,int top,int right,int bottom) {setfillstyle(SOLID_FILL,LIGHTGRAY); bar(left,top,right,bottom); setcolor(WHITE); line(left,top,left,bottom); line(left,top,right,top); setcolor(BLACK); line(right,top,right,bottom); line(left,bottom,right,bottom); } void initpiano() { writeFM(1,0); writeFM(8,0); writeFM(0xbd,0x00); writeFM(0x20+0,0x21); writeFM(0x20+3,0x11); writeFM(0x40+0,0x4c); writeFM(0x40+3,0x00); writeFM(0x60+0,0xd2); writeFM(0x60+3,0xd2); writeFM(0x80+0,0x32); writeFM(0x80+3,0x11); writeFM(0xe0+0,0x00); writeFM(0xe0+3,0x00); writeFM(0xc0+0,0x04); }运行出来的没有声音哦,不知道是哪里不对,大大们帮我看看。