📄 5-5.c
字号:
/*************************************************/
/* FMPIANO */
/***********************************************/
#include <stdio.h>
#include <dos.h>
#include <bios.h>
#include "stdlib.h"
#include "math.h"
#include "conio.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 Bar3D(int left, int top, int right, int bottom); /*3D窗口函数*/
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 gdriver=DETECT,gmode,cursound=0,i,ErrorCode;
char input; /*按下的键的键码*/
char * mask; /*图像缓冲*/
/*初始化BGI图形模式*/
initgraph( &gdriver, &gmode, "");
ErrorCode=graphresult();
if(ErrorCode!=grOk)
{
printf("Graphics system error: %s\n", grapherrormsg(ErrorCode));
exit(1);
}
/*绘制背景以及电子钢琴*/
setfillstyle(XHATCH_FILL,BLUE);
bar(0, 0, 499, 399);
mask=(char *)malloc(imagesize(0,0,20,100));
getimage(0,0,20,100,mask);
Bar3D(10,90,489,320);
for(i=0;i<21;i++)
{
Bar3D(i*22+18,200,i*22+35,300);
}
Bar3D(15,100,480,190);
settextstyle(1,0,2); /*绘制版权说明和作者介绍*/
setcolor(RED);
outtextxy(141,121,"FM PIANO DEMO");
setcolor(LIGHTRED);
outtextxy(140,120,"FM PIANO DEMO");
settextstyle(3,0,1);
outtextxy(120,150,"Designed by Programmer in C.");
outtextxy(220,165,"Copyright (C) 2006 Software");
/*初始化声卡上的FM合成器,并定义为钢琴音色。*/
InitPiano();
do
{
input=getch();
for(i=0; i<21; i++)
{
if(key[i][1]==input)
{
putimage(i*22+16, 200, mask, XOR_PUT);
FMSoundOFF(cursound, 4);
FMSound(key[i][0], 4);
cursound=key[i][0];
FMDelay(1);
putimage(i*22+18, 200, mask, XOR_PUT);
}
}
}while(input!=27); /*按ESC键退出*/
closegraph(); /*关闭图形模式*/
free(mask); /*清除缓冲*/
}
void WriteFM(char reg, char data)
{
outportb(ioport, reg);
delay(1);
outportb(ioport+1, data);
delay(2);
}
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=0x00e0&sl;
}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 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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -