#include "conio.h"
#include "bios.h"
#include "process.h"
#include "string.h"
#include "stdio.h"
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
#include <ctype.h>
#include <dos.h>
#define buttonheight 7
#define buttonwidth 2
/*以下变量存放 未按 按钮前的颜色, h高音按钮 l低音按钮 m中音按钮*/
char bufrefresh[66*8*2],bufWHITE[66*8*2]; /*钢琴外边框大小,参见 initpiano()函数*/
/*以下变量存放 按键后闪烁的变色按钮*/
char bufh1[buttonwidth*buttonheight*2],bufh2[buttonwidth*buttonheight*2],
bufh3[buttonwidth*buttonheight*2],bufh4[buttonwidth*buttonheight*2],
bufh5[buttonwidth*buttonheight*2],bufh6[buttonwidth*buttonheight*2],
bufh7[buttonwidth*buttonheight*2],
bufm1[buttonwidth*buttonheight*2],bufm2[buttonwidth*buttonheight*2],
bufm3[buttonwidth*buttonheight*2],bufm4[buttonwidth*buttonheight*2],
bufm5[buttonwidth*buttonheight*2],bufm6[buttonwidth*buttonheight*2],
bufm7[buttonwidth*buttonheight*2],
bufl1[buttonwidth*buttonheight*2],bufl2[buttonwidth*buttonheight*2],
bufl3[buttonwidth*buttonheight*2],bufl4[buttonwidth*buttonheight*2],
bufl5[buttonwidth*buttonheight*2],bufl6[buttonwidth*buttonheight*2],
bufl7[buttonwidth*buttonheight*2];
/**************** 定义函数原型 *********************************/
void initpiano(); /*初始化钢琴的原始状态*/
void refreshbutton();/*恢复钢琴的原始状态*/
/**************** 定义函数原型 *********************************/
main()
{
FILE *fp;
/************以下变量用于select 1*****************/
long key; /*用于存放键盘输入值。如: alt+1、shift+5等*/
float fre; /*音乐频率*/
long delaytime;/*自由演奏时的响声延时时间*/
/*定义变量存储钢琴 按钮 *******************/
int loop;
/****************以下变量用于 select 2*****************/
int rate;
char sound_high[3];
float sound_long;
register int i=0,j;
int sign=0;
float str[2000][2]; /*存放乐谱文件*/
int select;
char filename[100]; /*存放音乐文件名*/
long line=0;
long keyreset;
initpiano(); /* 初始化钢琴*/
while(1)
{
puttext(8,14,73,21,bufWHITE);/*暂时把钢琴掩盖*/
/*设置对话框的阴影*/
window(11,3,73,12); /*定义一个文本窗口*/
textbackground(BLACK); /*设置窗口背景色*/
clrscr();
/*设置对话框 边框*/
window(9,2,71,11); /*定义一个文本窗口*/
textbackground(GREEN); /*设置窗口背景色*/
clrscr();
textcolor(YELLOW);
gotoxy(1,1);
cprintf(" ALT+R----reset");
/*对话框 内框*/
window(10,3,70,10); /*定义一个文本窗口*/
textbackground(BLUE); /*设置窗口背景色*/
clrscr();
textcolor(WHITE);
line=0;
line++;
gotoxy(1,line);
/*在对话框中输出提示:(注:用cprintf才能限制在对话框内打印,若用print会在对话框之外打印)*/
cprintf("To play mucis ,input number 1;To autodisplay music,input mumber 2:\n");
line++;
line++;
gotoxy(1,line);
cprintf("Select:");
scanf("%d",&select);
while(!(select==1||select==2))
{
line++;
gotoxy(1,line);
cprintf("The number you input is wrong! Please input again.\n");
scanf("%d",&select);
}
/*+++++++++++ select 2 +++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
if(select==2)
{
nosound();
for(loop=0;loop<=99;loop++)
{filename[loop]='';}
line++;
gotoxy(1,line);
cprintf("Please input music file\n");
line++;
gotoxy(1,line);
cprintf(" [Note:If the music file is not at the same directory of \n");
line++;
gotoxy(1,line);
cprintf(" music.exe,you should include whole directory path!]\n");
line++;
gotoxy(1,line);
cprintf("Music file name:");
scanf("%s",filename);
fp=fopen(filename,"r");
if(fp==NULL) /* 文件打开失败 */
{
line++;
gotoxy(1,line);
cprintf("Open music file Error!\n");
}
else/*打开文件成功*/
{
fscanf(fp,"%d",&rate); /* 读取音长基数的值 */
while(!feof(fp)&&!sign) /* 文件没有结束并且数据还是乐谱 */
{
fscanf(fp,"%s%f",sound_high,&sound_long); /*得到音频、音长的数值*/
str[i][1]=rate*sound_long; /*音长=音长基数×节拍数*/
switch(sound_high[0])
{
case '*': /*最高音*/
switch(sound_high[1]) /*确定发声的频率*/
{
case '1':
str[i++][0]=1046.5;
break;
case '2':
str[i++][0]=1174.7;
break;
case '3':
str[i++][0]=1318.5;
break;
case '4':
str[i++][0]=1396.9;
break;
case '5':
str[i++][0]=1568;
break;
case '6':
str[i++][0]=1760;
break;
case '7':
str[i++][0]=1975.5;
break;
default:
printf("\n Error in music.doc\n");
exit(1);
}
/*******************************************************************************/
case 'h': /*高音*/
switch(sound_high[1]) /*确定发声的频率*/
{
case '1':
str[i++][0]=523.3;
break;
case '2':
str[i++][0]=587.3;
break;
case '3':
str[i++][0]=659.3;
break;
case '4':
str[i++][0]=698.5;
break;
case '5':
str[i++][0]=784.0;
break;
case '6':
str[i++][0]=880;
break;
case '7':
str[i++][0]=987.8;
break;
default:
printf("\n Error in music.doc\n");
exit(1);
}
/*******************************************************************************/
case 'm': /*中音*/
switch(sound_high[1])
{
case '1':
str[i++][0]=262;
break;
case '2':
str[i++][0]=296;
break;
case '3':
str[i++][0]=329.6;
break;
case '4':
str[i++][0]=349.2;
break;
case '5':
str[i++][0]=392;
break;
case '6':
str[i++][0]=440;
break;
case '7':
str[i++][0]=493.9;
break;
default:
printf("\n Error in music.doc\n");
exit(1);
}
/*******************************************************************************/
case 'l': /*低音*/
switch(sound_high[1])