C语言电子琴设计.doc

上传人:飞****2 文档编号:60929678 上传时间:2022-11-19 格式:DOC 页数:21 大小:312.50KB
返回 下载 相关 举报
C语言电子琴设计.doc_第1页
第1页 / 共21页
C语言电子琴设计.doc_第2页
第2页 / 共21页
点击查看更多>>
资源描述

《C语言电子琴设计.doc》由会员分享,可在线阅读,更多相关《C语言电子琴设计.doc(21页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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

2、下角,生成的电子琴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语言里有sound()函数,sound(Frequency

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

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

5、y(int num)int a,b,c;for(a=0;anum;a+)for(b=0;b1000;b+)for(c=0;c 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;anum;a+)for(b=0;bbmax;b+)for(c=0;ccmax;c+);至此,发音程序就完成了。2、如何完整弹奏一首歌曲。一首歌,有音调,有节拍用两个数组,一个存音调,一个存节拍,然后再循环播放即可实现那

6、么,如何判断一首歌播完?用一个特殊的频率即可,本程序用了频率1for(i=0;freq1i!=1;i+)sound(freq1i);Delay(dely1i);nosound();c=bioskey(1);if(c=0)break;mainmenu();break;这里考虑到歌太长,别人不想听的情况,设置了按0可退出。另外,Delay(dely1i);要是觉得一首歌整体节拍要改,可以直接改这里而不影响其他歌曲。如改为:Delay(2dely1i)则整首歌播放时间延长了一倍。3、如何实现键盘弹奏。很简单,按一个键发一个音。我们需要这样一张表,8255发声频率表:一共有7x7x3=147个音。可以

7、写出按键发音的基本程序是:sound(Frequency);Delay(Time);nosound();break;但是重复147句这样的语句显然很罗嗦。很自然的,想到用数组。用一个二维数组把它存起来,然后再调用即可。本程序中,排列顺序是A低音,A中音,A高音,B低音,B中音,B高音G低音,G中音,G高音4、几个细节。1、子函数可以调用子函数,但是被调用的子函数要先于子函数本身声明。2、注意数组的定义。声明要在程序,下标要正确,结尾不要忘记分号。3、界面符合习惯。三、程序源代码/* Simple Electronic Organ */* By xpxxpbmy 2011/3/22 */* Co

8、ntact Me QQ: */* All Rights Reserved */#includeint bmax=1000;int cmax=440;/* delay subroutine */void Delay(int num)int a,b,c;for(a=0;anum;a+)for(b=0;bbmax;b+)for(c=0;c 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=bio

9、skey(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 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、+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=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;c

11、ase 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=0)goto Test;elseprintf( Error Input!n);goto A;Test:sound(262);Delay(200);nosound();sound(294);Delay(200);nosound();sound(330);Del

12、ay(200);nosound();sound(350);Delay(200);nosound();sound(393);Delay(200);nosound();sound(441);Delay(200);nosound();sound(495);Delay(200);nosound();sound(525);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(

13、);/* 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,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,4

14、95,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,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,88

15、2,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,661,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);prin

16、tf( 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;break;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( Plea

17、se select.n);goto exit;default:printf( Error Input!n);printf( Please select again!n);goto seltone;/* Select Time */printf( Please select time.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

18、)mainmenu();printf( What do you want to do?n);goto exit;elseprintf( Error Input.Please select again!n);goto seltime;/* Play */printf( 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

19、();break;case 3:sound(tonem2);Delay(time);nosound();break;case 4:sound(tonem3);Delay(time);nosound();break;case 5:sound(tonem4);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();bre

20、ak;case w:sound(toneh1);Delay(time);nosound();break;case e:sound(toneh2);Delay(time);nosound();break;case r:sound(toneh3);Delay(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;cas

21、e a:sound(tonel0);Delay(time);nosound();break;case s:sound(tonel1);Delay(time);nosound();break;case d:sound(tonel2);Delay(time);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:so

22、und(tonel6);Delay(time);nosound();break;case 0:mainmenu();printf( What do you want to do?n);printf( Please select.n);goto exit;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

23、,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,100,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,5

24、0,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,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,4

25、41,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,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,5

26、25,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,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,2

27、00,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,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,24

28、00;/* 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,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 C

29、hinese 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,990,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,9

30、90,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,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,

31、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,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

32、,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,400,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

33、,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,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,

34、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,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

35、,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,100,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=bio

36、skey(0);switch(c)case 1:/* play music1 */printf( Start playing!(Prees 0 to stop.)n);for(i=0;freq1i!=1;i+)sound(freq1i);Delay(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=bio

37、skey(1);if(c=0)break;mainmenu();break;case 3:printf( Start playing!(Prees 0 to stop.)n);for(i=0;freq3i!=1;i+)sound(freq3i);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

38、=bioskey(1);if(c=0)break;mainmenu();break;case 5:printf( Start playing!(Prees 0 to stop.)n);for(i=0;freq5i!=1;i+)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( Pleas

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁