收藏 分享(赏)

C语言电子琴设计.doc

上传人:j35w19 文档编号:7806381 上传时间:2019-05-26 格式:DOC 页数:21 大小:339.50KB
下载 相关 举报
C语言电子琴设计.doc_第1页
第1页 / 共21页
C语言电子琴设计.doc_第2页
第2页 / 共21页
C语言电子琴设计.doc_第3页
第3页 / 共21页
C语言电子琴设计.doc_第4页
第4页 / 共21页
C语言电子琴设计.doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

1、C 语言简易电子琴程序设计 王永平 2011-3-22简介:这是一个用 C 语言写的简易电子琴程序。里面写了五首曲子,分别是东方红 、挥剑问情 、 祝你生日快乐 、 回梦游仙和 蝶恋 。可自己弹奏。程序可发ABCDEFG 调,低中高音共 147 个音。本程序经 TurboC2.0 调试通过,确认无任何问题,现将源码公布。对于内置的五首曲子及 8255 发声频率表,可访问网址复制:http:/ copy 这些程序。思维是最重要的,事实上,当你自己一字一字敲下这些代码进行调试时,你会体会更深,比如“;”的重要性,什么时候用 1,什么时候用 0,等等。一、简易电子琴先来看看要做的简易电子琴是个什么样

2、子1、右下角,生成的电子琴 EXE 文件2、双击打开,出现主界面3、按数字键 1-5 可以播放预置的音乐4、播放音乐时可以按数字键 0 随时退出播放,按 0 后出现:5、按数字键 6,自己弹奏下首先提示选择什么音调(按数字键 0可以取消选择,回到主界面)6、然后提示选择节拍(按数字键 0 可以取消选择,回到主界面)7、选好节拍开始弹奏8、按数字键 8 退出程序,先显示“Thanks for using!” ,延时 2s后退出程序9、若是不能正确发音,可以按 7 调节二、设计分析1、要做出这个简易电子琴,首先要解决发音问题。1)用什么发声。调用系统主板 8255 发声。2)发音程序。C 语言里有

3、 sound()函数,sound(Frequency)可发频率为 Frequency 的音。发出一个音后,它不能一直存在,为此需要控制发音持续多长时间。这就需要延时程序。C 语言里带的延时程序有 sleep(s);delay(ms);显然不能用 sleep(s);函数,因为这个程序最少都会延时 1秒,至少每秒才发一个音的歌也太难听了。执行延时程序后,还需调用一个函数 nosound();否则它还是会一直发原来那个音停不下来。这样要发一个音,需要的程序为:sound(Frequency);delay(ms);nosound();3)延时程序。大家都知道 delay();这个函数很不精确,不精确到

4、什么程度呢,delay(10000)原理上说它应该延时 10 秒,事实上,在绝大多数电脑上,这个延时程序不用 1 秒就运行完了。因而不可能用系统自带的 delay()函数来延时。C 语言里有实现精确延时的方法,但总觉得有点麻烦。怎么办?只好自己写个延时程序了。电脑运行速度很快,要让它实现延时,比较简单的就是让电脑做加减法。下面是我写的一个延时子程序,针对这个电子琴编的。它是一个三重循环,a,b,c 都是变量。num 为要实现的延时时间,单位是 ms,比如 200ms。对不同电脑,执行这个程序的时间是不同的,需要调节一下 b,c 的上限值来实现精确定时。在这个电子琴程序里,按键 7 退出时设置是

5、延时 2 秒再退出,可以通过对照标准时间来改变 b,c 上限值,让这个延时程序变精确。void Delay(int num)int a,b,c;for(a=0;a 1;2- 2;3- 5;4- 10;5- 100;6- 1000n“);printf(“ Fine: q- 1;w- 2;e- 5;r- 10;t- 100;n“);这样延时程序需要写成:void Delay(int num)int a,b,c;for(a=0;aint bmax=1000;int cmax=440;/* delay subroutine */void Delay(int num)int a,b,c;for(a=0;

6、a 1;2- 2;3- 5;4- 10;5- 100;6- 1000n“);printf(“ Fine: q- 1;w- 2;e- 5;r- 10;t- 100;n“);c=bioskey(0);if(c=a)printf(“ Accelerate time!n“);c=bioskey(0);switch(c)case 1:bmax=bmax+1;goto Test;case 2:bmax=bmax+2;goto Test;case 3:bmax=bmax+5;goto Test;case 4:bmax=bmax+10;goto Test;case 5:bmax=bmax+100;goto T

7、est;case 6:bmax=bmax+1000;goto Test;case q:cmax=cmax+1;goto Test;case w:cmax=cmax+2;goto Test;case e:cmax=cmax+5;goto Test;case r:cmax=cmax+10;goto Test;case t:cmax=cmax+100;goto Test;default:break;else if(c=d)printf(“ Slow down time!n“);c=bioskey(0);switch(c)case 1:bmax=bmax-1;goto Test;case 2:bmax

8、=bmax-2;goto Test;case 3:bmax=bmax-5;goto Test;case 4:bmax=bmax-10;goto Test;case 5:bmax=bmax-100;goto Test;case 6:bmax=bmax-1000;goto Test;case q:cmax=cmax-1;goto Test;case w:cmax=cmax-2;goto Test;case e:cmax=cmax-5;goto Test;case r:cmax=cmax-10;goto Test;case t:cmax=cmax-100;goto Test;default:brea

9、k;else if(c=0)goto Test;elseprintf(“ Error Input!n“);goto A;Test:sound(262);Delay(200);nosound();sound(294);Delay(200);nosound();sound(330);Delay(200);nosound();sound(350);Delay(200);nosound();sound(393);Delay(200);nosound();sound(441);Delay(200);nosound();sound(495);Delay(200);nosound();sound(525);

10、Delay(200);nosound();printf(“ Is this OK? n“);printf(“ Press n to reset.Or any other key to mainmenu!n“);c=bioskey(0);if (c=n) goto A;elsemainmenu();/* keyboard play */void scaleplay()char c;int i,h,m,l,time;/* Tone Matrix */* order:A-low pitch,A-middle pitch,A-hight pitch,B-low pitch,B-middle pitch

11、,B-hight pitch.*/int tone217=221,248,278,294,330,371,416,441,495,556,589,661,742,833,882,990,1112,1178,1322,1484,1665,248,278,312,330,371,416,467,495,556,624,661,742,833,935,990,1112,1248,1322,1484,1665,1869,131,147,165,175,196,221,248,262,294,330,350,393,441,495,525,589,661,700,786,882,990,147,165,

12、185,196,221,248,278,294,330,371,393,441,495,556,589,661,742,786,882,990,1112,165,185,208,221,248,278,312,330,371,416,441,495,556,624,661,742,833,882,990,1112,1248,175,196,221,234,262,294,330,350,393,441,467,525,589,661,700,786,882,935,1049,1178,1322,196,221,248,262,294,330,371,393,441,495,525,589,66

13、1,742,786,882,990,1049,1178,1322,1484;mainmenu();/* Select Tone */printf(“ please select tone:n“);seltone:printf(“ 1-A;2-B;3-C;4-D;5-E;6-F;7-G;n“);printf(“ or press 0 to cancle.n“);c=bioskey(0);switch(c)case 1:l=0;m=1;h=2;break;case 2:l=3;m=4;h=5;break;case 3:l=6;m=7;h=8;break;case 4:l=9;m=10;h=11;b

14、reak;case 5:l=12;m=13;h=14;break;case 6:l=15;m=16;h=17;break;case 7:l=18;m=19;h=20;break;case 0:mainmenu();printf(“ What do you want to do?n“);printf(“ Please select.n“);goto exit;default:printf(“ Error Input!n“);printf(“ Please select again!n“);goto seltone;/* Select Time */printf(“ Please select t

15、ime.n“);seltime:printf(“ 1-slow,2-middle,3-fastn“);printf(“ or press 0 to cancle.n“);c=bioskey(0);if(c=1)time=350;else if(c=2)time=250;else if(c=3)time=150;else if(c=0)mainmenu();printf(“ What do you want to do?n“);goto exit;elseprintf(“ Error Input.Please select again!n“);goto seltime;/* Play */pri

16、ntf(“ Start to play!(Prees 0 to exit)n“);c=bioskey(0);while(1)c=bioskey(0);switch(c)case 1:sound(tonem0);Delay(time);nosound();break;case 2:sound(tonem1);Delay(time);nosound();break;case 3:sound(tonem2);Delay(time);nosound();break;case 4:sound(tonem3);Delay(time);nosound();break;case 5:sound(tonem4)

17、;Delay(time);nosound();break;case 6:sound(tonem5);Delay(time);nosound();break;case 7:sound(tonem6);Delay(time);nosound();break;case q:sound(toneh0);Delay(time);nosound();break;case w:sound(toneh1);Delay(time);nosound();break;case e:sound(toneh2);Delay(time);nosound();break;case r:sound(toneh3);Delay

18、(time);nosound();break;case t:sound(toneh4);Delay(time);nosound();break;case y:sound(toneh5);Delay(time);nosound();break;case u:sound(toneh6);Delay(time);nosound();break;case a:sound(tonel0);Delay(time);nosound();break;case s:sound(tonel1);Delay(time);nosound();break;case d:sound(tonel2);Delay(time)

19、;nosound();break;case f:sound(tonel3);Delay(time);nosound();break;case g:sound(tonel4);Delay(time);nosound();break;case h:sound(tonel5);Delay(time);nosound();break;case j:sound(tonel6);Delay(time);nosound();break;case 0:mainmenu();printf(“ What do you want to do?n“);printf(“ Please select.n“);goto e

20、xit;default:sound(0);break;exit:return;/* main */main()int i;char c;/* Music Begin */* music1:The East is Red */int freq1=393,393,441,294,262,262,221,294,393,393,441,525,441,393,262,262,221,294,393,294,262,248,221,196,393,294,330,294,262,262,221,294,330,294,262,294,262,248,221,196,1;int dely1=200,10

21、0,100,400,200,100,100,400,200,200,150,50,100,100,200,100,100,400,200,200,200,100,100,200,200,200,100,100,200,100,100,15,50,100,100,100,100,100,100,200,100,50,50,75,25,50,50,50,50,50,50,200;/* music2:an episode of Chinese Pladin */int freq2=330,294,330,330,294,330,330,294,330,441,393,330,294,262,294,

22、330,330,294,330,330,294,330,525,441,393,330,330,393,441,441,525,441,393,330,294,262,294,330,330,393,330,330,262,294,330,294,330,330,294,330,330,294,330,441,393,330,294,330,393,441,525,441,525,393,441,525,390,441,393,330,393,441,330,393,441,330,393,441,525,441,441,393,330,393,441,525,393,330,294,393,

23、294,330,441,393,294,330,294,330,294,262,294,262,221,262,294,330,393,441,330,262,294,330,393,441,525,393,330,294,393,294,330,441,393,330,393,441,525,589,441,393,441,525,661,589,525,393,441,1;int dely2=200,200,400,200,200,400,200,200,200,200,200,200,400,200,200,400,200,200,400,200,200,200,200,200,200,

24、400,200,200,400,200,200,400,200,200,800,200,200,400,200,200,400,200,200,800,200,200,400,200,200,400,200,200,200,200,200,200,400,200,200,300,100,300,100,200,200,200,200,200,200,200,200,800,200,200,800,200,200,200,200,200,800,800,400,400,600,200,400,400,800,400,400,600,200,400,400,800,400,200,200,800,

25、200,200,400,400,400,400,400,400,600,200,800,400,400,600,200,400,400,800,400,400,600,200,400,400,800,400,400,400,400,400,400,800,400,400,1200,1200,1200,400,400,400,400,400,400,2400;/* music3:Happy Birthday to you */int freq3=262,262,294,262,394,330,262,262,294,262,392,349,262,262,523,440,349,262,262,

26、466,466,440,262,392,349,1;int dely3=100,100,200,200,200,200,200,100,100,200,200,200,200,200,100,100,200,200,200,200,200,200,100,100,200,200,200,200,200;/* music4:an episode of Chinese Pladin */int freq4=990,882,990,990,661,882,786,742,661,742,661,589,495,495,589,786,495,589,742,661,589,661,990,882,9

27、90,882,990,990,661,882,786,742,661,742,661,589,495,495,589,786,495,489,882,990,882,589,661,742,661,786,882,990,990,1178,990,786,882,661,786,990,882,786,661,589,589,661,786,882,990,1178,1322,1178,990,786,882,661,786,990,882,786,661,661,990,882,786,661,589,661,661,589,661,786,882,661,786,882,990,0,990

28、,882,990,990,661,882,786,742,661,742,661,589,495,495,589,786,495,589,742,661,589,661,990,882,990,882,990,990,661,882,786,742,661,742,661,589,495,495,589,786,495,589,882,990,882,589,661,742,589,495,661,661,1;int dely4=200,200,400,200,200,600,100,100,300,100,200,200,400,200,200,400,200,200,400,200,200

29、,600,200,400,200,200,400,200,200,600,100,100,300,100,200,200,400,200,200,400,200,200,300,100,200,200,800,1600,400,200,200,800,200,200,200,200,800,400,200,200,400,400,600,200,800,400,200,200,600,200,200,200,200,200,800,400,200,200,400,400,800,200,200,200,200,200,200,200,200,400,200,200,400,200,200,40

30、0,200,200,300,100,200,200,400,200,200,400,200,200,400,200,200,600,200,400,200,200,400,200,200,400,200,200,300,100,200,200,400,200,200,400,200,200,300,100,200,200,800,800,200,200,800,800;/* music5:an episode of Chinese Pladin */int freq5=416,416,416,371,416,371,416,371,371,278,278,312,330,371,330,312

31、,248,278,416,416,416,371,416,556,495,556,495,495,371,371,416,441,495,441,416,371,416,416,556,624,556,495,416,495,416,495,371,556,495,416,371,416,371,495,556,330,556,556,556,556,624,661,624,556,624,495,416,416,556,624,556,495,416,495,441,495,556,624,556,624,495,556,416,371,495,556,330,556,556,556,556

32、,624,661,624,556,624,495,1112,1;int dely5=200,200,200,200,400,200,200,200,400,200,200,400,200,200,300,100,800,200,200,200,200,300,100,200,200,200,200,400,200,200,400,200,200,300,100,800,200,400,200,200,300,100,800,200,200,400,200,200,300,100,800,400,200,600,400,200,200,200,200,200,400,200,200,300,10

33、0,800,200,400,200,200,300,100,800,200,200,400,200,200,400,150,50,800,400,200,600,400,200,200,200,200,200,400,200,200,300,100,800;/* Music End */* Main */mainmenu();while(1)c=bioskey(0);switch(c)case 1:/* play music1 */printf(“ Start playing!(Prees 0 to stop.)n“);for(i=0;freq1i!=1;i+)sound(freq1i);De

34、lay(dely1i);nosound();c=bioskey(1);if(c=0)break;mainmenu();break;case 2:printf(“ Start playing!(Prees 0 to stop.)n“);for(i=0;freq2i!=1;i+)sound(freq2i);Delay(dely2i);nosound();c=bioskey(1);if(c=0)break;mainmenu();break;case 3:printf(“ Start playing!(Prees 0 to stop.)n“);for(i=0;freq3i!=1;i+)sound(fr

35、eq3i);Delay(dely3i);nosound();c=bioskey(1);if(c=0)break;mainmenu();break;case 4:printf(“ Start playing!(Prees 0 to stop.)n“);for(i=0;freq4i!=1;i+)sound(freq4i);Delay(dely4i);nosound();c=bioskey(1);if(c=0)break;mainmenu();break;case 5:printf(“ Start playing!(Prees 0 to stop.)n“);for(i=0;freq5i!=1;i+)

36、sound(freq5i);Delay(dely5i/1.2);nosound();c=bioskey(1);if(c=0)break;mainmenu();break;case 6:scaleplay();break;/* stop playing */case 0:mainmenu();printf(“ What do you want to do?n“);printf(“ Please select.n“);break;/* Time Adjust */case 7:Adjust();break;/* EXIT */case 8:printf(“ Thanks for using!“);Delay(2000);/* Design to delay 2 seconds */goto exit;/* ERROR CASE */default:mainmenu();printf(“ Input Error!n“);printf(“ Please input again.n“);break;exit:return;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报