H a r b i n I n s t i t u t e o f T e c h n o l o g y课程设计说明书(论文)课程名称: C语言课程设计设计题目:音乐程序与波特图院系:航天学院控制科学与工程系班级:设计者:学号:指导教师:设计时间:哈尔滨工业大学教务处哈尔滨工业大学课程设计任务书题目一1.1题目详细描述:播放音乐程序,实现了自选音乐曲目和直接使用键盘弹奏,而且可以在曲目播放结束后循环选择乐曲。
1.2程序设计思路及流程图:1.3#include <stdio.h>#include <dos.h>#include <stdlib.h>#include <time.h>#include <bios.h>#include <conio.h>#define N1 16#define N2 8#define N4 4#define N8 2#define N16 1#define END 0void playmusic(int n,int *c);void typemusic();enum NOTES{C10=131,D10=147,E10=165,F10=175,G10=196,A10=220,B10=247,C0=262,D0=296,E0=330,F0=349,G0=392,A0=440,B0=494,C1=523,D1=587,E1=659,F1=698,G1=784,A1=880,B1=988,C2=1047,D2=1175,E2=1319,F2=1397,G2=1568,A2=1760,B2=1976,S=10};typedef enum NOTES SONG;SONG song1[]={C0,N4+N2,E0,N4,G0,N2,G0,N2,A0,N1,G0,N1,E0,N4+N2,C0,N4,G0,N1/3,G0,N1/3,G0,N1/3,E0,N1,C0,N1,G10,N1/3,G10,N1/3,G10,N1/3,G10,N1/3,G10,N1/3,G10,N1/3,C0,N1,END,END};SONG song2[]={A0,N2,B0,N2,C1,N1+N2,B0,N2,C1,N1,E1,N1,B0,N1+N1,S,N1,E0,N1,A0,N1+N2,G0,N2,A0,N1,C1,N1,G0,N1+N1,S,N1,E0,N2,E0,N2,F0,N1+N2,E0,N2,F0,N1,C0,N1,E0,N1+N1,S,N1,C1,N2,C1,N2,B0,N1+N2,370, N2,F0,N1,B0,N1,B0,N1+N2,S,N1,A0,N2,B0,N2,C1,N1+N2,B0,N2,C1,N1, E1,N1,B0,N1+N2,END,END};int main(){int m,n,c=1;char b='y';while(b=='y'){printf("Hello,what do you want to do by this program?\n");printf("1.Listen to music.-------Press 1\n");printf("2.Play music by yourself.-------Press 2\n");scanf("%d",&m);while(m!=1&&m!=2&&m!=3) /*选择方式*/{printf("You typed wrong!Do not push me around.:(\n");scanf("%d",&m);}if(m==1){printf("I have two musics,choose one!(Press 1/2)\n");scanf("%d",&n);while(n!=1&&n!=2){printf("You typed wrong!Do not push me around.:(\n");scanf("%d",&n);}playmusic(n,&c);}else if(m==2){typemusic();}getchar();printf("Do you want to continue?(y/n)\n");scanf("%c",&b);}return 0;}void playmusic(int n,int *c){int note=0,fre,dur,control;char d;clock_t goal;if(n==1){while(song1[note]!=END){fre=*c*song1[note];dur=song1[note+1];if(kbhit()) break;if(fre){ /*若频率值fre非0*/outportb(0x43,0xb6); /*初始化2号定时器 */fre=(unsigned)(1193180L/fre); /*计算计数初值 */outportb(0x42,(char)fre); /*先写计数初值的低8位*/outportb(0x42,(char)(fre>>8)); /*再写计数初值的高8位*/control=inportb(0x61); /*读0x61的状态*/outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/ }goal=(clock_t)dur+clock( );while(goal>clock( ));if(dur)outportb(0x61,control);note+=2;}}else{while(song2[note]!=END){fre=*c*song2[note];dur=song2[note+1];if(kbhit()) break;if(fre){ /*若频率值fre非0*/outportb(0x43,0xb6); /*初始化2号定时器 */fre=(unsigned)(1193180L/fre); /*计算计数初值 */outportb(0x42,(char)fre); /*先写计数初值的低8位*/outportb(0x42,(char)(fre>>8)); /*再写计数初值的高8位*/control=inportb(0x61); /*读0x61的状态*/outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/ }goal=(clock_t)dur+clock( );while(goal>clock( ));if(dur)outportb(0x61,control);note+=2;}}getchar();printf("Do you want to raise/lower the frequency? Press r/l\nIf you do not want press n\n"); scanf("%c",&d);if(d=='r')*c*=2;else if(d=='l')*c*=0.5;else if(d=='n')*c=*c;}void typemusic(){char ch;clock_t goal;int fre=0,control,dur=2;printf("Play your own music!\n(You can play on the keyboard by pressing Z-M,A-J,Q-U,1-7)\n"); printf("If you are bored press /\n");do{ch=getch();if(ch=='/');else{switch(ch){case 'z':fre=C10;break;case 'x':fre=D10;break;case 'c':fre=E10;break;case 'v':fre=F10;break;case 'b':fre=G10;break;case 'n':fre=A10;break;case 'm':fre=B10;break;case 'a':fre=C0;break;case 's':fre=D0;break;case 'd':fre=E0;break;case 'f':fre=F0;break;case 'g':fre=G0;break;case 'h':fre=A0;break;case 'j':fre=B0;break;case 'q':fre=C1;break;case 'w':fre=D1;break;case 'e':fre=E1;break;case 'r':fre=F1;break;case 't':fre=G1;break;case 'y':fre=A1;break;case 'u':fre=B1;break;case '1':fre=C2;break;case '2':fre=D2;break;case '3':fre=E2;break;case '4':fre=F2;break;case '5':fre=G2;break;case '6':fre=A2;break;case '7':fre=B2;break;default:printf("You typed wrong!Do not push me around.:(\n");}}if(fre){ /*若频率值fre非0*/outportb(0x43,0xb6); /*初始化2号定时器 */fre=(unsigned)(1193180L/fre); /*计算计数初值 */outportb(0x42,(char)fre); /*先写计数初值的低8位*/outportb(0x42,(char)(fre>>8)); /*再写计数初值的高8位*/control=inportb(0x61); /*读0x61的状态*/outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/}goal=(clock_t)dur+clock( ); /* clock()是计时函数,而与其相关的数据类型是clock_t,返回当前程序占用的CPU时间,用clock()返回的值除于CLOCK_PER_SEC常量可以得到当前程序的运行时间,单位是秒 */while(goal>clock( ));if(dur)outportb(0x61,control);}while (ch!='/');}1.4程序运行结果及自评:1.5程序调试中遇到过的问题及解决办法在编辑弹奏的程序时,没有弄明白延时函数,无法让按键发声,后来明白了延时函数的原理,把时长设成4,就可以了,还有就是按/时直接退出,后来把if放到前面,就解决了。