当前位置:   article > 正文

51单片机之蜂鸣器模拟钢琴(代码详解)——起风了_蜂鸣器音乐代码起风了

蜂鸣器音乐代码起风了

目录

前言

正文

乐理

程序

补充


前言

        最近心血来潮,想要用蜂鸣器播放音乐(全损音质),于是最初的想法诞生了,但是我总不能每次想听歌都敲一遍蜂鸣器的代码吧,有没有什么办法只需要敲一遍的代码便可以实现听歌自由呢(相对自由)?也就是每次写歌只需要将歌曲的谱子录入即可,于是我决定把蜂鸣器模拟成任何一种乐器,将乐器的每一个音都写到程序中,如此,只需要将所获得的谱子添加到程序中便可,极大地减少了工作量。

        既然如此,我为何选择模拟钢琴呢?由于鄙人缺乏乐理知识,只好向朋友或者网友请教,刚好有认识会钢琴的网友,于是确定了下来模拟钢琴。在此,感谢网友的帮助,让我的学习进展得十分顺利!

        本文以《起风了》这首歌来为大家讲解相关的知识和程序,先听听歌(全损音质)。

起风了(全损音质)

正文

乐理

温馨提示:此处为未懂得乐理的网友提供一点知识,懂得乐理的大神们可以跳过。本人也才疏学浅,有讲解不对的地方,请不吝赐教!

        首先我们来看一小段钢琴的简谱,如图

         谱子中有数字,有下划线,有双下划线,有弧,还有点,这些都代表什么呢?首先,数字表示这个音的音调,这是我们再熟悉不过的了,比如1对应的音就是do,2对应的音就是re,以此类推,接下来的3、4、5、6、7对应的音就是mi、fa、so、la、xi……

        对了,还有一个就是有的时候会碰到谱子上的有写着“#1”或者其他前面带有“#”的数字,这个表示的是这个音比1高但比2低,即介于1和2之间。而数字0表示的则是一个空拍,就是没有声音的意思就是了。

        接下来先讲下面的一点,比如7下面带着这样的一点“·”,表示的是这个带点的“7”这个音比不带点的“7”的音调低一轮。那如果是两个点呢?那就是音调低两轮。相反地,如果这个点是放在7的上头的,那么一个点就是音调高一轮,两个点就是音调高两轮。

        而下划线呢?一个下划线和两个下划线有什么不一样?还有右边的那一横杠是什么?我们应该知道,音乐在演奏的过程中,每个音都有对应的时间,也就是这个音要“响”多久,所以呢,下划线和右边一横杠便表示相应的音延时多久。它们的规律是这样的——单下划线表示这个音延时一个八分音符,双下划线表示这个音延时一个十六分音符(即八分音符时间的一半),以此类推,后面三十二分音符就是三个下划线。而横杠,则有一个横杠表示一个二分音符,两个横杠表示一个全音符。说了这么多,怎么不见四分音符?其实四分音符的表示方法就是一个音不带下划线也不带横杠。

        至于两个3之间的弧……那是钢琴的一种演奏方法,我们这里不作深究。毕竟我们的目标是蜂鸣器。

        还有一种符号,如图

         仔细看我们发现,这两个“2”后面跟了一个小点点,这个小点点又是什么意思呢?其实它也是延时的意思,表示前面的这个音再延长原来时间的一半。这么说多少有点抽象,就举个例子吧,就上图的这个例子,音调“2”下带了一个下划线,表示这个音延时了一个八分音符,然后我在这延时之后再延时一个十六分音符(八分音符的一半),也就是总共延时了(八分音符+十六分音符)。

        还有,细心的读者可能会发现,这幅图后面的两个音——“6”,下面带了两个点,这就是我上面所说的它的音调低了两轮。

        写蜂鸣器所需要的乐理大致如此,接下来开始讲解程序。

程序

先看看我书写的程序框架:

         图中主要的程序文件为main.c、music.c和wind.c文件music.c文件里存放着我上部分讲解的乐理部分,而wind.c文件则存放着《起风了》这首歌的谱子。

        废话少说,先看主程序:

  1. #include<stc89c5xrc.h>
  2. #include<music.h>
  3. #include<moon.h>
  4. sbit led = P2^7;
  5. void main()
  6. {
  7. const float time = 0.75;
  8. unsigned int num;
  9. musicInit(time);
  10. while(1)
  11. {
  12. num++;
  13. if(num >= 20000)
  14. {
  15. num = 0;
  16. led = !led;
  17. }
  18. }
  19. }

        main函数中首先定义了两个变量,之后初始化musicInit函数,变量num想必读者们都懂,它是用于延时使灯led闪烁的,这里不作介绍。于是我们进入music.c此文件中看看musicInit等函数的定义。如下:

  1. #include<music.h>
  2. //音调(晶振频率:12M)(定时器的装载值)
  3. code unsigned int tone[8][12] =
  4. {
  5. {35233,36965,38509,39895,41146,42809,43797,45128,46305,47354,48295,49407},
  6. {50151,51043,51837,52715,53341,54042,54666,55332,55921,56445,56989,57406},
  7. {57902,58342,58733,59126,59475,59822,60131,60434,60728,60991,61244,61487},
  8. {61719,61926,62135,62321,62506,62671,62833,62985,63126,63263,63390,63512},
  9. {63624,63731,63832,63928,64019,64103,64185,64260,64333,64400,64463,64524},
  10. {64580,64634,64685,64733,64777,64820,64860,64898,64934,64968,65000,65030},
  11. {65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283},
  12. {65297,65311,65323,65335,65346,65357,65367,65377,65386,65394,65402,65409},
  13. };
  14. unsigned int fre = 0; //定时器的装载值
  15. unsigned int delay[7]; //延时:全音符,二分音符,四分音符,八分音符,十六分音符,三十二分音符
  16. /*
  17. 初始化
  18. 参数t:设置一拍(四分音符)的时间(单位:s)
  19. */
  20. void musicInit(float t)
  21. {
  22. unsigned char x,y;
  23. TMOD = 0x11; //定时器设置16位模式
  24. TH1 = 0xfc;
  25. TL1 = 0x18; //定时器0每1ms中断一次
  26. TH0 = 0xff;
  27. TL0 = 0x00;
  28. ET1 = 1;
  29. ET0 = 1;
  30. EA = 1;
  31. PT0 = 1; //定时器0的优先级高
  32. PT1 = 0; //定时器1的优先级低,可被定时器0中断打断
  33. t *= 4; //全音符时长(单位:s)
  34. for(x = 0;x < 7;x++)
  35. {
  36. delay[x] = t *1000; //t *1000;全音符时长(单位:ms)
  37. for(y = 0;y < x;y++)
  38. {
  39. delay[x] /= 2; //全音符,二分音符,四分音符,八分音符,十六分音符,三十二分音符
  40. }
  41. }
  42. delay[6] /= 8; //相同音延时,只需一瞬间(经我测试这个延时是最好的)
  43. TR1 = 1;
  44. TR0 = 1;
  45. }
  46. /*
  47. 定时器0中断函数,主要负责蜂鸣器的振动频率
  48. 中断优先级高于定时器1,防止定时器1的中断影响其振动频率而导致音调不对
  49. */
  50. void rate() interrupt 1
  51. {
  52. if(fre != 0)
  53. {
  54. TH0 = (fre >> 8);
  55. TL0 = (fre & 0x00ff);
  56. beep = !beep;
  57. }
  58. else //空拍
  59. {
  60. TH0 = 0xff;
  61. TL0 = 0xe0;
  62. beep = 0;
  63. }
  64. }
  65. /*
  66. 定时器1中断,主要负责每个音的延时时间(音符),优先级低于定时器0中断
  67. 每1ms进入一次中断
  68. */
  69. void time() interrupt 3
  70. {
  71. static unsigned int j,n;
  72. TH1 = 0xfc;
  73. TL1 = 0x18; //定时器0每1ms中断一次
  74. if((++j) >= delay[MUSIC[n][2]])
  75. {
  76. n++; //切换到下一个音
  77. j = 0;
  78. }
  79. if(n >= LENGTH) //LENGTH为音乐的长度,该部分为防止n超出数组的范围
  80. {
  81. n = 0;
  82. }
  83. if(MUSIC[n][0] != NONE) //判断是不是空拍
  84. {
  85. fre = tone[MUSIC[n][0]][MUSIC[n][1]];
  86. }
  87. else //空拍
  88. {
  89. fre = 0;
  90. }
  91. }

        首先看musicInit函数,里面我初始化了两个定时器,为什么要用到两个定时器呢?我们不妨思考一下,钢琴的音调是如何控制的,初中的物理知识告诉我们是通过频率控制它音调高低的,因此这里面一个重要因素就是频率,不妨看到定时器0中断函数,可以知道我利用了定时器在不同的装载值下,进入中断的时间(即频率)不同,那么蜂鸣器的振动频率也随之不同。所以,我们只需要控制定时器0的重装载值即可控制蜂鸣器的音调高低。

        至于定时器1,还记得我在乐理部分讲过每一个音都有一定的延时吗?定时器1就是为其延时而来的。而参数t和数组delay的处理我将在后面延时部分细讲,请读者留意。

        不妨先看到数组tone,顾名思义,tone所装载的内容十有八九跟音调高低有关,没错,它装的就是定时器0的重装载值(晶振为12MHz),每一行从左往右分别对应音调:1、#1、2、#2、3、4、#4、5、#5、6、#6、7。从上往下,上一行的音调要比下一行的音调低(这个其实就是我在乐理部分为大家讲解的数字上头或下方带小点点的部分,比如我选择“1”这个音在第一行,则“1”上面加一点这个音就在第二行,加两个点就在第三行)。

        因此,我们只需要将谱子上的每一音分解成两个数字,将这两个数字对应到数组中,即可使蜂鸣器发出的声音为我们所想要的音调。这两组数字我将在头文件部分定义,读者稍安勿躁。

        那么读者肯定好奇,我是如何知道它每个调的频率的,这还不简单,当然是知之为知之,不知“百度知”啦。如图:

         以上便是每个音调所对应的频率,请读者自行计算!

        定时器中断0我在前面有简单讲述,其主要负责蜂鸣器发出声音的音调,过于简单,我在此不作过多赘述。

        接下来看定时器中断函数1,我也有说过它主要用于声音的延时。由重装载值可知,每过1ms进入一次中断函数。讲到这里,先插入讲一下数组delaydelay数组里所存放的内容为延时时间,即全音符、二分音符、四分音符……说的比较直白点就是存放了需要进入多少次定时器1中断来延时(也就是需要延时多少个1ms)。不妨看定时器1中断函数的第一个条件判断语句if,每当一个音延长到相应的时间之后变通过变量n切换到下一个音。

        再看看我们刚刚在musicInit函数里落下的变量tdelay处理部分。此时读者对这段代码的理解是否更加深刻了呢?变量t为传入参数,表示一拍(即一个四分音符)所占的时长(单位:s),我们可以根据歌曲的节奏快慢改变t的数值,而数组delay由delay[0]到delay[6]则是对应全音符、二分音符 ……二百五十六分音符(为什么要一个二百五十六分音符我会在后续遇到问题上作讲解)的时长(单位:ms)。

        接着继续看定时器1中断函数的第三个条件判断语句if,其中fre为定时器0的重装载值,即它的大小决定着蜂鸣器的音调高低。而它的值则来自数组tone,由此我们知道,数组MUSIC的第0列和第1列肯定存放着确定音高低的数字,通过这两个数字对应到数组tone中便是了。

        下面我将带领读者瞧一瞧music.h头文件,如下

  1. #ifndef _MUSIC_H
  2. #define _MUSIC_H
  3. #include<stc89c5xrc.h>
  4. #include<wind.h>
  5. sbit beep = P1^5;
  6. //高低音,但谱子需要升降调时,可调节其数字来升降调,但此处只能使用C调
  7. #define L 4
  8. #define M 5
  9. #define H 6
  10. //音调,do,re,mi,fa,so,la,xi
  11. #define T1_0 0 //C
  12. #define T1_5 1 //C#
  13. #define T2_0 2 //D
  14. #define T2_5 3 //Eb
  15. #define T3_0 4 //E
  16. #define T4_0 5 //F
  17. #define T4_5 6 //F#
  18. #define T5_0 7 //G
  19. #define T5_5 8 //G#
  20. #define T6_0 9 //A
  21. #define T6_5 10 //Bb
  22. #define T7_0 11 //B
  23. #define NONE 12 //空拍
  24. //音符(延时)
  25. #define D_1 0 //全音符
  26. #define D_2 1 //二分音符
  27. #define D_4 2 //四分音符
  28. #define D_8 3 //八分音符
  29. #define D_16 4 //十六分音符
  30. #define D_32 5 //三十二分音符
  31. #define D_256 6 //二百五十六分音符(用于两个相同的音按两次时中间的间断)
  32. extern unsigned int delay[7];
  33. void musicInit(float t);
  34. #define MUSIC wind //起风了
  35. #define LENGTH windLength //歌曲长度
  36. #endif

        程序不长,我挑主要部分讲(其实我也在代码里面都注释好了doge)。可以看到我所定义的"L"、"M"、"H"即对应.c文件中数组tone的行数,而”T1_0“、”T1_5“、”T2_0“等等则是对应着数组tone的列数,通过这两组数组来确定音调在tone中的位置(当我们对歌曲的整体音调有要求时,只需改变定义的”L“、”M“、”H“的数值即可改变成我们所想要的音调,当然,数值的大小不可超过数组tone的范围)。而下面的”D_1“、”D_2“、”D_3“等则是对应到数组delay中去。有了这些定义之后,我们只需要在一个数组中,按着谱子的顺序放入它们,便可以将歌曲放出来了。如程序中定义的MUSIC,它其实就是一个数组,里面按照一定的规律放着谱的信息,而LENGTH则是数组MUSIC的长度。

        以上便是主要的程序部分,那么接下来我们来看看wind.c文件,也就是存放歌曲的谱的信息的文件

  1. #include<wind.h>
  2. /*
  3. 曲谱,3代表构成它的三个要素
  4. 第一个为高低音,第二个为高低音中对应的音调,第三个为音调对应的音符(时长)
  5. */
  6. code unsigned char wind[][3] =
  7. {
  8. {NONE,NONE,D_4},
  9. //前奏
  10. {M,T7_0,D_16},
  11. {H,T1_0,D_16},
  12. {H,T2_0,D_16},
  13. {H,T3_0,D_16},
  14. {M,T3_0,D_8},
  15. {H,T5_0,D_16},
  16. {H,T3_0,D_16},
  17. {NONE,NONE,D_256},
  18. {H,T3_0,D_2},
  19. {M,T7_0,D_16},
  20. {H,T1_0,D_16},
  21. {H,T2_0,D_16},
  22. {H,T3_0,D_16},
  23. {M,T2_0,D_8},
  24. {H,T5_0,D_16},
  25. {H,T3_0,D_16},
  26. {H,T2_0,D_16},
  27. {H,T3_0,D_16},
  28. {H,T1_0,D_16},
  29. {H,T2_0,D_16},
  30. {M,T7_0,D_16},
  31. {H,T1_0,D_16},
  32. {M,T5_0,D_8},
  33. {M,T7_0,D_16},
  34. {H,T1_0,D_16},
  35. {H,T2_0,D_16},
  36. {H,T3_0,D_16},
  37. {M,T3_0,D_8},
  38. {H,T5_0,D_16},
  39. {H,T3_0,D_16},
  40. {NONE,NONE,D_512},
  41. {H,T3_0,D_2},
  42. {M,T7_0,D_16},
  43. {H,T1_0,D_16},
  44. {H,T2_0,D_16},
  45. {H,T3_0,D_16},
  46. {M,T2_0,D_8},
  47. {H,T5_0,D_16},
  48. {H,T3_0,D_16},
  49. {H,T2_0,D_16},
  50. {H,T3_0,D_16},
  51. {H,T1_0,D_16},
  52. {H,T2_0,D_16},
  53. {M,T7_0,D_16},
  54. {H,T1_0,D_16},
  55. {M,T5_0,D_4},
  56. {NONE,NONE,D_16},
  57. //这一路上走走停停,顺着少年漂流的痕迹
  58. {M,T2_0,D_8},
  59. {M,T2_0,D_16},
  60. {M,T1_0,D_16},
  61. {M,T2_0,D_8},
  62. {M,T2_0,D_16},
  63. {M,T1_0,D_16},
  64. {M,T2_0,D_8},
  65. {M,T3_0,D_8},
  66. {M,T5_0,D_8},
  67. {M,T3_0,D_8},
  68. {M,T2_0,D_8},
  69. {M,T2_0,D_16},
  70. {M,T1_0,D_16},
  71. {M,T2_0,D_8},
  72. {M,T2_0,D_16},
  73. {M,T1_0,D_16},
  74. {M,T2_0,D_16},
  75. {M,T3_0,D_16},
  76. {M,T2_0,D_16},
  77. {M,T1_0,D_16},
  78. {L,T5_0,D_4},
  79. //迈出车站的前一刻竟有些犹豫
  80. {M,T2_0,D_8},
  81. {M,T2_0,D_16},
  82. {M,T1_0,D_16},
  83. {M,T2_0,D_8},
  84. {M,T2_0,D_16},
  85. {M,T1_0,D_16},
  86. {M,T2_0,D_8},
  87. {M,T3_0,D_8},
  88. {M,T5_0,D_8},
  89. {M,T3_0,D_8},
  90. {M,T2_0,D_8},
  91. {M,T2_0,D_16},
  92. {M,T3_0,D_16},
  93. {M,T2_0,D_8},
  94. {M,T1_0,D_16},
  95. {M,T2_0,D_16},
  96. {NONE,NONE,D_256},
  97. {M,T2_0,D_4},
  98. // {NONE,NONE,D_4}, //此处照着谱子应该是四分音符,但个人认为在这里四分音符过长,所以改成八分音符
  99. {NONE,NONE,D_8},
  100. //不禁笑这近乡情怯仍无可避免
  101. {M,T2_0,D_8},
  102. {M,T2_0,D_16},
  103. {M,T1_0,D_16},
  104. {M,T2_0,D_8},
  105. {M,T2_0,D_16},
  106. {M,T1_0,D_16},
  107. {M,T2_0,D_8},
  108. {M,T3_0,D_8},
  109. {M,T5_0,D_8},
  110. {M,T3_0,D_8},
  111. {M,T2_0,D_8},
  112. {M,T2_0,D_16},
  113. {M,T3_0,D_16},
  114. {M,T2_0,D_8},
  115. {M,T1_0,D_16},
  116. {L,T6_0,D_16},
  117. {NONE,NONE,D_256},
  118. {L,T6_0,D_4},
  119. //而长野的天依旧那么暖,吹起了从前
  120. {M,T3_0,D_16},
  121. {M,T2_0,D_16},
  122. {M,T1_0,D_16},
  123. {M,T2_0,D_16},
  124. {M,T1_0,D_4},
  125. {M,T3_0,D_16},
  126. {M,T2_0,D_16},
  127. {M,T1_0,D_16},
  128. {M,T2_0,D_16},
  129. {M,T1_0,D_4},
  130. {M,T3_0,D_16},
  131. {M,T2_0,D_16},
  132. {M,T1_0,D_16},
  133. {M,T2_0,D_16},
  134. {M,T1_0,D_4},
  135. // {NONE,NONE,D_4}, //同上
  136. {NONE,NONE,D_8},
  137. //从前初识这世间,万般流连,看着天边似在眼前
  138. {M,T1_0,D_8},
  139. {M,T2_0,D_8},
  140. {M,T3_0,D_8},
  141. {M,T1_0,D_8},
  142. {M,T6_0,D_8},
  143. {M,T5_0,D_16},
  144. {M,T6_0,D_16},
  145. {NONE,NONE,D_256},
  146. {M,T6_0,D_8},
  147. {M,T6_0,D_16},
  148. {M,T1_0,D_16},
  149. {M,T7_0,D_8},
  150. {M,T6_0,D_16},
  151. {M,T7_0,D_16},
  152. {NONE,NONE,D_256},
  153. {M,T7_0,D_4},
  154. {NONE,NONE,D_256},
  155. {M,T7_0,D_8},
  156. {M,T6_0,D_16},
  157. {M,T7_0,D_16},
  158. {NONE,NONE,D_256},
  159. {M,T7_0,D_16},
  160. {M,T3_0,D_16},
  161. {NONE,NONE,D_256},
  162. {M,T3_0,D_8},
  163. {H,T1_0,D_16},
  164. {H,T2_0,D_16},
  165. {H,T1_0,D_16},
  166. {M,T7_0,D_16},
  167. {M,T6_0,D_8},
  168. //也甘愿赴汤蹈火去走它一遍
  169. {M,T5_0,D_8},
  170. {M,T6_0,D_8},
  171. {M,T5_0,D_16},
  172. {M,T6_0,D_16},
  173. {NONE,NONE,D_256},
  174. {M,T6_0,D_16},
  175. {M,T5_0,D_16},
  176. {M,T6_0,D_16},
  177. {M,T5_0,D_16},
  178. {M,T6_0,D_8},
  179. {M,T5_0,D_16},
  180. {M,T2_0,D_16},
  181. {NONE,NONE,D_256},
  182. {M,T2_0,D_16},
  183. {M,T5_0,D_8},
  184. {M,T5_0,D_16},
  185. {M,T3_0,D_4},
  186. // {NONE,NONE,D_4}, //同上
  187. {NONE,NONE,D_16},
  188. //如今走过这世间,万般流连,翻过岁月不同侧脸
  189. {M,T1_0,D_8},
  190. {M,T2_0,D_8},
  191. {M,T3_0,D_8},
  192. {NONE,NONE,D_256},
  193. {M,T1_0,D_8},
  194. {M,T6_0,D_8},
  195. {M,T5_0,D_16},
  196. {M,T6_0,D_16},
  197. {NONE,NONE,D_256},
  198. {M,T6_0,D_8},
  199. {M,T6_0,D_16},
  200. {M,T1_0,D_16},
  201. {M,T7_0,D_8},
  202. {M,T6_0,D_16},
  203. {M,T7_0,D_16},
  204. {NONE,NONE,D_256},
  205. {M,T7_0,D_4},
  206. {NONE,NONE,D_256},
  207. {M,T7_0,D_8},
  208. {M,T6_0,D_16},
  209. {M,T7_0,D_16},
  210. {NONE,NONE,D_256},
  211. {M,T7_0,D_16},
  212. {M,T3_0,D_16},
  213. {NONE,NONE,D_256},
  214. {M,T3_0,D_8},
  215. {H,T1_0,D_16},
  216. {H,T2_0,D_16},
  217. {H,T1_0,D_16},
  218. {M,T7_0,D_16},
  219. {M,T6_0,D_8},
  220. //措不及防闯入你的笑颜
  221. {M,T5_0,D_8},
  222. {M,T6_0,D_8},
  223. {H,T3_0,D_16},
  224. {NONE,NONE,D_256},
  225. {H,T3_0,D_16},
  226. {NONE,NONE,D_256},
  227. {H,T3_0,D_8},
  228. {M,T5_0,D_8},
  229. {M,T6_0,D_8},
  230. {H,T3_0,D_16},
  231. {NONE,NONE,D_256},
  232. {H,T3_0,D_16},
  233. {NONE,NONE,D_256},
  234. {H,T3_0,D_16},
  235. {M,T5_0,D_8},
  236. {M,T6_0,D_16},
  237. {NONE,NONE,D_256},
  238. {M,T6_0,D_2},
  239. {NONE,NONE,D_4},
  240. //我曾难自拔于世界之大,也沉溺于其中梦话
  241. {H,T1_0,D_8},
  242. {H,T2_0,D_8},
  243. {H,T3_0,D_8},
  244. {H,T6_0,D_16},
  245. {H,T5_0,D_16},
  246. {NONE,NONE,D_256},
  247. {H,T5_0,D_8},
  248. {H,T6_0,D_16},
  249. {H,T5_0,D_16},
  250. {NONE,NONE,D_256},
  251. {H,T5_0,D_8},
  252. {H,T6_0,D_16},
  253. {H,T5_0,D_16},
  254. {NONE,NONE,D_256},
  255. {H,T5_0,D_8},
  256. {H,T2_0,D_16},
  257. {H,T3_0,D_16},
  258. {NONE,NONE,D_256},
  259. {H,T3_0,D_8},
  260. {H,T6_0,D_16},
  261. {H,T5_0,D_16},
  262. {NONE,NONE,D_256},
  263. {H,T5_0,D_8},
  264. {H,T6_0,D_16},
  265. {H,T5_0,D_16},
  266. {NONE,NONE,D_256},
  267. {H,T5_0,D_8},
  268. {H,T6_0,D_16},
  269. {H,T5_0,D_16},
  270. {NONE,NONE,D_256},
  271. {H,T5_0,D_16},
  272. {H,T3_0,D_8},
  273. {H,T3_0,D_16},
  274. //不得真假不做挣扎不惧笑话
  275. {H,T2_0,D_8},
  276. {H,T1_0,D_16},
  277. {M,T6_0,D_16},
  278. {NONE,NONE,D_256},
  279. {M,T6_0,D_16},
  280. {H,T1_0,D_8},
  281. {NONE,NONE,D_256},
  282. {H,T1_0,D_16},
  283. {H,T2_0,D_8},
  284. {H,T1_0,D_16},
  285. {M,T6_0,D_16},
  286. {NONE,NONE,D_256},
  287. {M,T6_0,D_16},
  288. {H,T1_0,D_8},
  289. {H,T3_0,D_4},
  290. {NONE,NONE,D_256},
  291. {H,T3_0,D_16},
  292. {H,T4_0,D_16},
  293. {H,T3_0,D_16},
  294. {H,T2_0,D_16},
  295. {H,T3_0,D_16},
  296. {H,T2_0,D_8},
  297. {H,T2_0,D_16},
  298. //我曾将青春翻涌成她,也曾指尖弹出盛夏
  299. {H,T1_0,D_8},
  300. {H,T2_0,D_8},
  301. {H,T3_0,D_8},
  302. {H,T6_0,D_16},
  303. {H,T5_0,D_16},
  304. {NONE,NONE,D_256},
  305. {H,T5_0,D_8},
  306. {H,T6_0,D_16},
  307. {H,T5_0,D_16},
  308. {NONE,NONE,D_256},
  309. {H,T5_0,D_8},
  310. {H,T6_0,D_16},
  311. {H,T5_0,D_16},
  312. {NONE,NONE,D_256},
  313. {H,T5_0,D_8},
  314. {H,T2_0,D_8},
  315. {H,T3_0,D_8},
  316. {H,T6_0,D_16},
  317. {H,T5_0,D_16},
  318. {NONE,NONE,D_256},
  319. {H,T5_0,D_8},
  320. {H,T6_0,D_16},
  321. {H,T5_0,D_16},
  322. {NONE,NONE,D_256},
  323. {H,T5_0,D_8},
  324. {H,T6_0,D_16},
  325. {H,T5_0,D_16},
  326. {NONE,NONE,D_256},
  327. {H,T5_0,D_16},
  328. //心之所动且就随缘去吧
  329. {H,T3_0,D_8},
  330. {H,T3_0,D_16},
  331. {H,T2_0,D_8},
  332. {H,T1_0,D_16},
  333. {M,T6_0,D_16},
  334. {NONE,NONE,D_256},
  335. {M,T6_0,D_16},
  336. {H,T3_0,D_8},
  337. {H,T3_0,D_16},
  338. {H,T2_0,D_8},
  339. {H,T1_0,D_16},
  340. {M,T6_0,D_16},
  341. {NONE,NONE,D_256},
  342. {M,T6_0,D_16},
  343. {NONE,NONE,D_256},
  344. {M,T6_0,D_16},
  345. {H,T1_0,D_8},
  346. {NONE,NONE,D_256},
  347. // {H,T1_0,D_1}, //同上
  348. {H,T1_0,D_2},
  349. //逆着光行走任风吹雨打
  350. {M,T6_0,D_16},
  351. {H,T3_0,D_8},
  352. {H,T3_0,D_16},
  353. {H,T2_0,D_8},
  354. {H,T1_0,D_16},
  355. {M,T6_0,D_16},
  356. {NONE,NONE,D_256},
  357. {M,T6_0,D_16},
  358. {H,T3_0,D_8},
  359. {H,T3_0,D_16},
  360. {H,T2_0,D_8},
  361. {H,T1_0,D_16},
  362. {M,T6_0,D_16},
  363. {NONE,NONE,D_256},
  364. {M,T6_0,D_16},
  365. {H,T1_0,D_8},
  366. {H,T1_0,D_16},
  367. {NONE,NONE,D_256},
  368. // {H,T1_0,D_1}, //同上
  369. {H,T1_0,D_2},
  370. {NONE,NONE,D_4}, //结束延时
  371. };
  372. //曲谱长度
  373. code unsigned int windLength = sizeof(wind)/3;

        先不看其他的,总体浏览一下,知道前两列的变量决定着此时音调的高低,后一列的变量决定着这个音调需要的延时。而在着其中掺着不少的{NONE,NONE,...},对于{NONE,NONE,D_4}之类的,不难知道,这些就是对应着谱中的空拍(即0),我在这里不过多解释。对于{NONE,NONE,D_256}这个呢,为什么要插入这么短的一个空音?这是由于我在写歌的时候遇到了一个问题,就是如果前后两个音的音调是相同的,如果直接这样体现在蜂鸣器上,势必只能听到一次这个音,这于钢琴不同,钢琴如果遇到这两个音的弹奏过程是手指按下,然后起来,然后再按下,所以我在这里加多这一空音就是为了达到这种效果。

        等等,数组中为什么有的连续两个音相同但是它们中间却没有用{NONE,NONE,D_256}隔开呢?读者可否还记得谱子里有的音会在数字后面加一个小点,如图

         还记得这个是什么意思吗?不加一个短促的空音是因为这本来就是一个音,只是它被我分解成两个部分了。

补充

        以上部分是最最最主要的程序部分,接下来的是使这个模拟的钢琴更加完善。

        以上的代码部分只能演奏C调的歌曲,而我们所听的歌曲并不仅仅局限于C调,还有C#调、D调、Eb调、E调、F调、F#调、G调、G#调、A调、Bb调、B调等,那么该如何实现用不同的调来奏响曲子呢?其实方法有很多,比如在不同的调下使确定音调的变量发生适当的偏移,或者利用十二平均律来调节蜂鸣器振动的频率,或者……

        而由于鄙人比较懒惰,我使用了不需要动脑子的方法,但是相应的代码量就增加了(doge虽说是增加了,但是都是cv操作)。看看music.c文件的音调数组部分

  1. //音调(晶振频率:12M)(定时器的装载值)
  2. #ifdef C //C调
  3. code unsigned int tone[8][12] =
  4. {
  5. {35233,36965,38509,39895,41146,42809,43797,45128,46305,47354,48295,49407},
  6. {50151,51043,51837,52715,53341,54042,54666,55332,55921,56445,56989,57406},
  7. {57902,58342,58733,59126,59475,59822,60131,60434,60728,60991,61244,61487},
  8. {61719,61926,62135,62321,62506,62671,62833,62985,63126,63263,63390,63512},
  9. {63624,63731,63832,63928,64019,64103,64185,64260,64333,64400,64463,64524},
  10. {64580,64634,64685,64733,64777,64820,64860,64898,64934,64968,65000,65030},
  11. {65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283},
  12. {65297,65311,65323,65335,65346,65357,65367,65377,65386,65394,65402,65409},
  13. };
  14. #endif
  15. #ifdef C_ //C#调
  16. code unsigned int tone[8][12] =
  17. {
  18. {36965,38509,39895,41146,42809,43797,45128,46305,47354,48295,49407,50151},
  19. {51043,51837,52715,53341,54042,54666,55332,55921,56445,56989,57406,57902},
  20. {58342,58733,59126,59475,59822,60131,60434,60728,60991,61244,61487,61719},
  21. {61926,62135,62321,62506,62671,62833,62985,63126,63263,63390,63512,63624},
  22. {63731,63832,63928,64019,64103,64185,64260,64333,64400,64463,64524,64580},
  23. {64634,64685,64733,64777,64820,64860,64898,64934,64968,65000,65030,65058},
  24. {65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,65297},
  25. // {65311,65323,65335,65346,65357,65367,65377,65386,65394,65402,65409,00000},
  26. };
  27. #endif
  28. #ifdef D //D调
  29. code unsigned int tone[8][12] =
  30. {
  31. {38509,39895,41146,42809,43797,45128,46305,47354,48295,49407,50151,51043},
  32. {51837,52715,53341,54042,54666,55332,55921,56445,56989,57406,57902,58342},
  33. {58733,59126,59475,59822,60131,60434,60728,60991,61244,61487,61719,61926},
  34. {62135,62321,62506,62671,62833,62985,63126,63263,63390,63512,63624,63731},
  35. {63832,63928,64019,64103,64185,64260,64333,64400,64463,64524,64580,64634},
  36. {64685,64733,64777,64820,64860,64898,64934,64968,65000,65030,65058,65085},
  37. {65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,65297,65311},
  38. // {65323,65335,65346,65357,65367,65377,65386,65394,65402,65409,00000,00000},
  39. };
  40. #endif
  41. #ifdef Eb //Eb调
  42. code unsigned int tone[8][12] =
  43. {
  44. {39895,41146,42809,43797,45128,46305,47354,48295,49407,50151,51043,51837},
  45. {52715,53341,54042,54666,55332,55921,56445,56989,57406,57902,58342,58733},
  46. {59126,59475,59822,60131,60434,60728,60991,61244,61487,61719,61926,62135},
  47. {62321,62506,62671,62833,62985,63126,63263,63390,63512,63624,63731,63832},
  48. {63928,64019,64103,64185,64260,64333,64400,64463,64524,64580,64634,64685},
  49. {64733,64777,64820,64860,64898,64934,64968,65000,65030,65058,65085,65110},
  50. {65134,65157,65178,65198,65217,65235,65252,65268,65283,65297,65311,65323},
  51. // {65335,65346,65357,65367,65377,65386,65394,65402,65409,00000,00000,00000},
  52. };
  53. #endif
  54. #ifdef E //E调
  55. code unsigned int tone[8][12] =
  56. {
  57. {41146,42809,43797,45128,46305,47354,48295,49407,50151,51043,51837,52715},
  58. {53341,54042,54666,55332,55921,56445,56989,57406,57902,58342,58733,59126},
  59. {59475,59822,60131,60434,60728,60991,61244,61487,61719,61926,62135,62321},
  60. {62506,62671,62833,62985,63126,63263,63390,63512,63624,63731,63832,63928},
  61. {64019,64103,64185,64260,64333,64400,64463,64524,64580,64634,64685,64733},
  62. {64777,64820,64860,64898,64934,64968,65000,65030,65058,65085,65110,65134},
  63. {65157,65178,65198,65217,65235,65252,65268,65283,65297,65311,65323,65335},
  64. // {65346,65357,65367,65377,65386,65394,65402,65409,00000,00000,00000,00000},
  65. };
  66. #endif
  67. #ifdef F //F调
  68. code unsigned int tone[8][12] =
  69. {
  70. {42809,43797,45128,46305,47354,48295,49407,50151,51043,51837,52715,53341},
  71. {54042,54666,55332,55921,56445,56989,57406,57902,58342,58733,59126,59475},
  72. {59822,60131,60434,60728,60991,61244,61487,61719,61926,62135,62321,62506},
  73. {62671,62833,62985,63126,63263,63390,63512,63624,63731,63832,63928,64019},
  74. {64103,64185,64260,64333,64400,64463,64524,64580,64634,64685,64733,64777},
  75. {64820,64860,64898,64934,64968,65000,65030,65058,65085,65110,65134,65157},
  76. {65178,65198,65217,65235,65252,65268,65283,65297,65311,65323,65335,65346},
  77. // {65357,65367,65377,65386,65394,65402,65409,00000,00000,00000,00000,00000},
  78. };
  79. #endif
  80. #ifdef F_ //F#调
  81. code unsigned int tone[8][12] =
  82. {
  83. {43797,45128,46305,47354,48295,49407,50151,51043,51837,52715,53341,54042},
  84. {54666,55332,55921,56445,56989,57406,57902,58342,58733,59126,59475,59822},
  85. {60131,60434,60728,60991,61244,61487,61719,61926,62135,62321,62506,62671},
  86. {62833,62985,63126,63263,63390,63512,63624,63731,63832,63928,64019,64103},
  87. {64185,64260,64333,64400,64463,64524,64580,64634,64685,64733,64777,64820},
  88. {64860,64898,64934,64968,65000,65030,65058,65085,65110,65134,65157,65178},
  89. {65198,65217,65235,65252,65268,65283,65297,65311,65323,65335,65346,65357},
  90. // {65367,65377,65386,65394,65402,65409,00000,00000,00000,00000,00000,00000},
  91. };
  92. #endif
  93. #ifdef G //G调
  94. code unsigned int tone[8][12] =
  95. {
  96. {45128,46305,47354,48295,49407,50151,51043,51837,52715,53341,54042,54666},
  97. {55332,55921,56445,56989,57406,57902,58342,58733,59126,59475,59822,60131},
  98. {60434,60728,60991,61244,61487,61719,61926,62135,62321,62506,62671,62833},
  99. {62985,63126,63263,63390,63512,63624,63731,63832,63928,64019,64103,64185},
  100. {64260,64333,64400,64463,64524,64580,64634,64685,64733,64777,64820,64860},
  101. {64898,64934,64968,65000,65030,65058,65085,65110,65134,65157,65178,65198},
  102. {65217,65235,65252,65268,65283,65297,65311,65323,65335,65346,65357,65367},
  103. // {65377,65386,65394,65402,65409,00000,00000,00000,00000,00000,00000,00000},
  104. };
  105. #endif
  106. #ifdef G_ //G#调
  107. code unsigned int tone[8][12] =
  108. {
  109. {46305,47354,48295,49407,50151,51043,51837,52715,53341,54042,54666,55332},
  110. {55921,56445,56989,57406,57902,58342,58733,59126,59475,59822,60131,60434},
  111. {60728,60991,61244,61487,61719,61926,62135,62321,62506,62671,62833,62985},
  112. {63126,63263,63390,63512,63624,63731,63832,63928,64019,64103,64185,64260},
  113. {64333,64400,64463,64524,64580,64634,64685,64733,64777,64820,64860,64898},
  114. {64934,64968,65000,65030,65058,65085,65110,65134,65157,65178,65198,65217},
  115. {65235,65252,65268,65283,65297,65311,65323,65335,65346,65357,65367,65377},
  116. // {65386,65394,65402,65409,00000,00000,00000,00000,00000,00000,00000,00000},
  117. };
  118. #endif
  119. #ifdef A //A调
  120. code unsigned int tone[8][12] =
  121. {
  122. {47354,48295,49407,50151,51043,51837,52715,53341,54042,54666,55332,55921},
  123. {56445,56989,57406,57902,58342,58733,59126,59475,59822,60131,60434,60728},
  124. {60991,61244,61487,61719,61926,62135,62321,62506,62671,62833,62985,63126},
  125. {63263,63390,63512,63624,63731,63832,63928,64019,64103,64185,64260,64333},
  126. {64400,64463,64524,64580,64634,64685,64733,64777,64820,64860,64898,64934},
  127. {64968,65000,65030,65058,65085,65110,65134,65157,65178,65198,65217,65235},
  128. {65252,65268,65283,65297,65311,65323,65335,65346,65357,65367,65377,65386},
  129. // {65394,65402,65409,00000,00000,00000,00000,00000,00000,00000,00000,00000},
  130. };
  131. #endif
  132. #ifdef Bb //Bb调
  133. code unsigned int tone[8][12] =
  134. {
  135. {48295,49407,50151,51043,51837,52715,53341,54042,54666,55332,55921,56445},
  136. {56989,57406,57902,58342,58733,59126,59475,59822,60131,60434,60728,60991},
  137. {61244,61487,61719,61926,62135,62321,62506,62671,62833,62985,63126,63263},
  138. {63390,63512,63624,63731,63832,63928,64019,64103,64185,64260,64333,64400},
  139. {64463,64524,64580,64634,64685,64733,64777,64820,64860,64898,64934,64968},
  140. {65000,65030,65058,65085,65110,65134,65157,65178,65198,65217,65235,65252},
  141. {65268,65283,65297,65311,65323,65335,65346,65357,65367,65377,65386,65394},
  142. // {65402,65409,00000,00000,00000,00000,00000,00000,00000,00000,00000,00000},
  143. };
  144. #endif
  145. #ifdef B //B调
  146. code unsigned int tone[8][12] =
  147. {
  148. {49407,50151,51043,51837,52715,53341,54042,54666,55332,55921,56445,56989},
  149. {57406,57902,58342,58733,59126,59475,59822,60131,60434,60728,60991,61244},
  150. {61487,61719,61926,62135,62321,62506,62671,62833,62985,63126,63263,63390},
  151. {63512,63624,63731,63832,63928,64019,64103,64185,64260,64333,64400,64463},
  152. {64524,64580,64634,64685,64733,64777,64820,64860,64898,64934,64968,65000},
  153. {65030,65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268},
  154. {65283,65297,65311,65323,65335,65346,65357,65367,65377,65386,65394,65402},
  155. // {65409,00000,00000,00000,00000,00000,00000,00000,00000,00000,00000,00000},
  156. };
  157. #endif

        额……应该都能看出来吧,我用最直接的方法就是不同的调定义内容不同的tone数组,也就是在C调数组的基础上不断向前挤,从而改变歌曲的音调。相应的我们在头文件里也应添加一部分代码

  1. //选择音调,可选C调,C#调(C_),D调,Eb调,E调,F调,F#调(F_),G调,G#调(G_),A调,Bb调,B调
  2. #define F_

        由此我们实现了用不同的调奏出我们所想要的曲子!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/554274
推荐阅读
相关标签
  

闽ICP备14008679号