网站导航: 首页 > 设计参考 > 正文 文章搜索
蜂鸣器发声工具-输入简谱就能自动生成音乐程序的工具Music Encode
 
文章编号:
100623160319
文章分类: 单片机 51系列
点 击:
...
关 键 词: 蜂鸣器,简谱,音乐
文章来源:
互联网
摘 要:
相信您在作蜂鸣器发声实验时肯定为复杂的频率与音调之间的转换而伤脑筋,这里向大家介绍一款工具可以大大简化转换操作,只需要输入简谱就能自动生成音乐播放程序代码。

  相信您在作蜂鸣器发声实验时肯定为复杂的频率与音调之间的转换而伤脑筋,这里向大家介绍一款工具可以大大简化转换操作,只需要输入简谱就能自动生成音乐播放程序代码。

以下此程序的主界面:

程序范例可以点主界面上的“关于”按钮后就能找到,但是代码也不全,需要加入对音乐程序的调用代码,其它范例里也写的很清楚了,只需要调用Play函数就行了,其调用格式如下:

 
  1. Play(乐曲名,调号,升降八度,演奏速度);  

经过实践,此函数的的后三个参数分别设为0,2和350时演奏出的音乐比较符合“原味”,下面给出本站的入门型51实验板结合此工具作的音乐程序:

 
  1. //51单片机播放音乐库文件   
  2. /**************************************************************************  
  3.      
  4.                         SOUND PLAY FOR 51MCU  
  5.     
  6.                     COPYRIGHT (c)   2005 BY JJJ.  
  7.                             --  ALL RIGHTS RESERVED  --  
  8.     
  9.    File Name:       SoundPlay.h  
  10.    Author:          Jiang Jian Jun  
  11.    Created:         2005/5/16  
  12.    Modified:        NO  
  13.    Revision:        1.0  
  14.     
  15. *******************************************************************************/  
  16.   
  17. /*说明**************************************************************************  
  18.  曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长...., 0,0}; 末尾:0,0 表示结束(Important)  
  19.  
  20.  音高由三位数字组成:  
  21.         个位是表示 1~7 这七个音符   
  22.         十位是表示音符所在的音区:1-低音,2-中音,3-高音;  
  23.         百位表示这个音符是否要升半音: 0-不升,1-升半音。  
  24.    
  25.  音长最多由三位数字组成:   
  26.         个位表示音符的时值,其对应关系是:   
  27.             |数值(n):  |0 |1 |2 |3 | 4 | 5 | 6   
  28.             |几分音符: |1 |2 |4 |8 |16 |32 |64      音符=2^n  
  29.         十位表示音符的演奏效果(0-2):  0-普通,1-连音,2-顿音  
  30.         百位是符点位: 0-无符点,1-有符点  
  31.  
  32.  调用演奏子程序的格式  
  33.         Play(乐曲名,调号,升降八度,演奏速度);  
  34.     |乐曲名           : 要播放的乐曲指针,结尾以(0,0)结束;  
  35.     |调号(0-11)       :   是指乐曲升多少个半音演奏;  
  36.     |升降八度(1-3)    : 1:降八度, 2:不升不降, 3:升八度;  
  37.     |演奏速度(1-12000): 值越大速度越快;  
  38.  
  39. ***************************************************************************/  
  40. #ifndef __SOUNDPLAY_H_REVISION_FIRST__   
  41. #define __SOUNDPLAY_H_REVISION_FIRST__   
  42.   
  43. #include <REG51.H>   
  44.   
  45. //**************************************************************************   
  46.   
  47. #define SYSTEM_OSC      11059200//12000000  //定义晶振频率12000000HZ   
  48. #define SOUND_SPACE     4/5         //定义普通音符演奏的长度分率,//每4分音符间隔   
  49. sbit    BeepIO    =     P3^5;       //定义输出管脚   
  50.   
  51. unsigned int  code FreTab[12]  = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表   
  52. unsigned char code SignTab[7]  = { 0,2,4,5,7,9,11 };                                  //1~7在频率表中的位置   
  53. unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };                         
  54. unsigned char Sound_Temp_TH0,Sound_Temp_TL0;    //音符定时器初值暂存    
  55. unsigned char Sound_Temp_TH1,Sound_Temp_TL1;    //音长定时器初值暂存   
  56. //**************************************************************************   
  57. void InitialSound(void)   
  58. {   
  59.     BeepIO = 1;   
  60.     Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256;   // 计算TL1应装入的初值  (10ms的初装值)   
  61.     Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256;   // 计算TH1应装入的初值    
  62.     TH1 = Sound_Temp_TH1;   
  63.     TL1 = Sound_Temp_TL1;   
  64.     TMOD  |= 0x11;   
  65.     ET0    = 1;   
  66.     ET1    = 0;   
  67.     TR0    = 0;   
  68.     TR1    = 0;   
  69.     EA     = 1;   
  70. }   
  71.   
  72. void BeepTimer0(void) interrupt 1   //音符发生中断   
  73. {   
  74.     BeepIO = !BeepIO;   
  75.     TH0    = Sound_Temp_TH0;   
  76.     TL0    = Sound_Temp_TL0;   
  77. }   
  78. //**************************************************************************   
  79. void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed)   
  80. {   
  81.     unsigned int NewFreTab[12];     //新的频率表   
  82.     unsigned char i,j;   
  83.     unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;   
  84.     unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;   
  85.     for(i=0;i<12;i++)               // 根据调号及升降八度来生成新的频率表    
  86.     {   
  87.         j = i + Signature;   
  88.         if(j > 11)   
  89.         {   
  90.             j = j-12;   
  91.             NewFreTab[i] = FreTab[j]*2;   
  92.         }   
  93.         else  
  94.             NewFreTab[i] = FreTab[j];   
  95.   
  96.         if(Octachord == 1)   
  97.             NewFreTab[i]>>=2;   
  98.         else if(Octachord == 3)   
  99.             NewFreTab[i]<<=2;   
  100.     }                                      
  101.        
  102.     SoundLength = 0;   
  103.     while(Sound[SoundLength] != 0x00)   //计算歌曲长度   
  104.     {   
  105.         SoundLength+=2;   
  106.     }   
  107.   
  108.     Point = 0;   
  109.     Tone   = Sound[Point];     
  110.     Length = Sound[Point+1];            // 读出第一个音符和它时时值   
  111.        
  112.     LDiv0 = 12000/Speed;                // 算出1分音符的长度(几个10ms)       
  113.     LDiv4 = LDiv0/4;                    // 算出4分音符的长度    
  114.     LDiv4 = LDiv4-LDiv4*SOUND_SPACE;    // 普通音最长间隔标准    
  115.     TR0   = 0;   
  116.     TR1   = 1;   
  117.     while(Point < SoundLength)   
  118.     {   
  119.         SL=Tone%10;                                 //计算出音符    
  120.         SM=Tone/10%10;                              //计算出高低音    
  121.         SH=Tone/100;                                //计算出是否升半    
  122.         CurrentFre = NewFreTab[SignTab[SL-1]+SH];   //查出对应音符的频率        
  123.         if(SL!=0)   
  124.         {   
  125.             if (SM==1) CurrentFre >>= 2;        //低音    
  126.             if (SM==3) CurrentFre <<= 2;        //高音   
  127.             Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//计算计数器初值   
  128.             Sound_Temp_TH0 = Temp_T/256;    
  129.             Sound_Temp_TL0 = Temp_T%256;    
  130.             TH0 = Sound_Temp_TH0;     
  131.             TL0 = Sound_Temp_TL0 + 12; //加12是对中断延时的补偿    
  132.         }   
  133.         SLen=LengthTab[Length%10];  //算出是几分音符   
  134.         XG=Length/10%10;            //算出音符类型(0普通1连音2顿音)    
  135.         FD=Length/100;   
  136.         LDiv=LDiv0/SLen;            //算出连音音符演奏的长度(多少个10ms)   
  137.         if (FD==1)    
  138.             LDiv=LDiv+LDiv/2;   
  139.         if(XG!=1)      
  140.             if(XG==0)               //算出普通音符的演奏长度    
  141.                 if (SLen<=4)       
  142.                     LDiv1=LDiv-LDiv4;   
  143.                 else  
  144.                     LDiv1=LDiv*SOUND_SPACE;   
  145.             else  
  146.                 LDiv1=LDiv/2;       //算出顿音的演奏长度    
  147.         else  
  148.             LDiv1=LDiv;   
  149.         if(SL==0) LDiv1=0;   
  150.             LDiv2=LDiv-LDiv1;       //算出不发音的长度    
  151.         if (SL!=0)   
  152.         {   
  153.             TR0=1;   
  154.             for(i=LDiv1;i>0;i--)    //发规定长度的音    
  155.             {   
  156.                 while(TF1==0);   
  157.                 TH1 = Sound_Temp_TH1;   
  158.                 TL1 = Sound_Temp_TL1;   
  159.                 TF1=0;   
  160.             }   
  161.         }   
  162.         if(LDiv2!=0)   
  163.         {   
  164.             TR0=0; BeepIO=1;   
  165.             for(i=LDiv2;i>0;i--)    //音符间的间隔   
  166.             {   
  167.                 while(TF1==0);   
  168.                 TH1 = Sound_Temp_TH1;   
  169.                 TL1 = Sound_Temp_TL1;   
  170.                 TF1=0;   
  171.             }   
  172.         }   
  173.         Point+=2;    
  174.         Tone=Sound[Point];   
  175.         Length=Sound[Point+1];   
  176.     }   
  177.     BeepIO = 1;   
  178. }   
  179. //**************************************************************************   
  180. #endif   
  181.   
  182.   
  183.   
  184. //挥着翅膀的女孩   
  185. unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03,   
  186.                                   0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03,   
  187.                                   0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03,   
  188.                                   0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02,   
  189.                                   0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03,   
  190.                                   0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03,   
  191.                                   0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03,   
  192.                                   0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,   
  193.                                   0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03,   
  194.                                   0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02,   
  195.                                   0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,   
  196.                                   0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,   
  197.                                   0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,   
  198.                                   0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03,   
  199.                                   0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03,   
  200.                                   0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,   
  201.                                   0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,   
  202.                                   0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,   
  203.                                   0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,   
  204.                                   0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03,   
  205.                                   0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03,   
  206.                                   0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,   
  207.                                   0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E,   
  208.                                   0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03,   
  209.                                   0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66,   
  210.                                   0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03,   
  211.                                   0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03,   
  212.                                   0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,   
  213.                                   0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04,   
  214.                                   0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,   
  215.                                   0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03,   
  216.                                   0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03,   
  217.                                   0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,   
  218.                                   0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02,   
  219.                                   0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02,   
  220.                                   0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00};   
  221. //同一首歌   
  222. unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03,   
  223.                                   0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02,   
  224.                                   0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02,   
  225.                                   0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66,   
  226.                                   0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00,   
  227.                                   0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03,   
  228.                                   0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02,   
  229.                                   0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03,   
  230.                                   0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03,   
  231.                                   0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C,   
  232.                                   0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66,   
  233.                                   0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03,   
  234.                                   0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01,   
  235.                                   0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02,   
  236.                                   0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03,   
  237.                                   0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70,   
  238.                                   0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66,   
  239.                                   0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02,   
  240.                                   0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C,   
  241.                                   0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66,   
  242.                                   0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00,   
  243.                                   0x00,0x00 };   
  244. //两只蝴蝶                                     
  245. unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,   
  246.                                   0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,   
  247.                                   0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,   
  248.                                   0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03,   
  249.                                   0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01,   
  250.                                   0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,   
  251.                                   0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01,   
  252.                                   0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,   
  253.                                   0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,   
  254.                                   0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,   
  255.                                   0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03,   
  256.                                   0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01,   
  257.                                   0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,   
  258.                                   0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00,   
  259.                                   0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03,   
  260.                                   0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03,   
  261.                                   0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03,   
  262.                                   0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03,   
  263.                                   0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03,   
  264.                                   0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E,   
  265.                                   0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E,   
  266.                                   0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03,   
  267.                                   0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03,   
  268.                                   0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03,   
  269.                                   0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03,   
  270.                                   0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02,   
  271.                                   0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01,   
  272.                                   0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03,   
  273.                                   0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,   
  274.                                   0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,   
  275.                                   0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,   
  276.                                   0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02,   
  277.                                   0x10,0x03, 0x15,0x00, 0x00,0x00 };   
  278.   
  279.   
  280. void main()   
  281. {   
  282.        
  283.         InitialSound();           
  284.   
  285.            
  286.         while(1)   
  287.         {                      
  288.             Play(Music_Girl,0,2,350);   
  289.             Play(Music_Same,0,2,350);   
  290.             Play(Music_Two,0,2,350);   
  291.         }   
  292. }   
  293.   
  294.   

 

点击下载Music Encode

 
相关文章:

 
最新开源项目
 
 
  查看更多...  
 
本站相关产品   淘宝网店
 




 
  查看更多...  

 

本站程序由百合电子工作室开发和维护
Copyright @ baihe electric studio
渝ICP备09006681号-4