专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

音乐音符的VHDL程序

作者:佚名   来源:本站原创   点击数:  更新时间:2013年11月08日   【字体:

一、以20M的时钟频率编写的高中低音符,每个音符响一秒钟(乐谱知识见后面文字部分介绍):
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_arith.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY music_act IS
 PORT(CLK,rst:in std_logic;    
         out_bit:OUT STD_LOGIC);
END music_act;
ARCHITECTURE PVJ OF music_act IS
  SIGNAL time:STD_LOGIC_VECTOR(24 DOWNTO 0);
  signal music21:STD_LOGIC_VECTOR(8 DOWNTO 0);
  SIGNAL L1,L2,L3,L4,L5,L6,L7:STD_LOGIC_VECTOR(16 DOWNTO 0);
  SIGNAL M1,M2,M3,M4,M5,M6,M7:STD_LOGIC_VECTOR(16 DOWNTO 0);
  SIGNAL H1,H2,H3,H4,H5,H6,H7:STD_LOGIC_VECTOR(16 DOWNTO 0);
  SIGNAL LL1,LL2,LL3,LL4,LL5,LL6,LL7,C:STD_LOGIC;
  SIGNAL MM1,MM2,MM3,MM4,MM5,MM6,MM7:STD_LOGIC;
  SIGNAL HH1,HH2,HH3,HH4,HH5,HH6,HH7,X0:STD_LOGIC;
    BEGIN
      process(clk,rst)
        begin
        X0<='1';
        if clk'event and clk='1' then time<=time+1;                  --将20M频率6分频
                 if L1=76444  then L1<="00000000000000000";
                    else    L1<=L1+1;                 
                 end if;
                 IF L1<38222 THEN LL1<='1';ELSE LL1<='0';END IF;
                 if L2=68104  then L2<="00000000000000000";
                    else    L2<=L2+1;                 
                 end if;
                 IF L2<34052 THEN LL2<='1';ELSE LL2<='0';END IF;
                 if L3=60674  then L3<="00000000000000000";
                    else    L3<=L3+1;                 
                 end if;
                 IF L3<30337 THEN LL3<='1';ELSE LL3<='0';END IF;
                 if L4=57268  then L4<="00000000000000000";
                    else    L4<=L4+1;                 
                 end if;
                 IF L4<28634 THEN LL4<='1';ELSE LL4<='0';END IF;
                 if L5=51020  then L5<="00000000000000000";
                    else    L5<=L5+1;                 
                 end if;
                 IF L5<25510 THEN LL5<='1';ELSE LL5<='0';END IF;
                 if L6=50000  then L6<="00000000000000000";
                    else    L6<=L6+1;                 
                 end if;
                 IF L6<25000 THEN LL6<='1';ELSE LL6<='0';END IF;
                 if L7=40494  then L7<="00000000000000000";
                    else    L7<=L7+1;                 
                 end if;
                 IF L7<20247 THEN LL7<='1';ELSE LL7<='0';END IF;
      if M1=38222  then M1<="00000000000000000";
                    else    M1<=M1+1;                 
                 end if;
                 IF M1<19111 THEN MM1<='1';ELSE MM1<='0';END IF;
                 if M2=34051  then M2<="00000000000000000";
                    else    M2<=M2+1;                 
                 end if;
                 IF M2<17026 THEN MM2<='1';ELSE MM2<='0';END IF;
                 if M3=33036  then M3<="00000000000000000";
                    else    M3<=M3+1;                 
                 end if;
                 IF M3<16518 THEN MM3<='1';ELSE MM3<='0';END IF;
                 if M4=28634  then M4<="00000000000000000";
                    else    M4<=M4+1;                 
                 end if;
                 IF M4<14217 THEN MM4<='1';ELSE MM4<='0';END IF;
                 if M5=25510  then M5<="00000000000000000";
                    else    M5<=M5+1;                 
                 end if;
                 IF M5<12755 THEN MM5<='1';ELSE MM5<='0';END IF;
                 if M6=25000  then M6<="00000000000000000";
                    else    M6<=M6+1;                 
                 end if;
                 IF M6<12500 THEN MM6<='1';ELSE MM6<='0';END IF;
                 if M7=20247  then M7<="00000000000000000";
                    else    M7<=M7+1;                 
                 end if;
                 IF M7<10124 THEN MM7<='1';ELSE MM7<='0';END IF;
     if H1=19110  then H1<="00000000000000000";
                    else    H1<=H1+1;                 
                 end if;
                 IF H1<9555 THEN HH1<='1';ELSE HH1<='0';END IF;
                 if H2=17025  then H2<="00000000000000000";
                    else    H2<=H2+1;                 
                 end if;
                 IF H2<8513 THEN HH2<='1';ELSE HH2<='0';END IF;
                 if H3=15168  then H3<="00000000000000000";
                    else    H3<=H3+1;                 
                 end if;
                 IF H3<7584 THEN HH3<='1';ELSE HH3<='0';END IF;
                 if H4=14316  then H4<="00000000000000000";
                    else    H4<=H4+1;                 
                 end if;
                 IF H4<7158 THEN HH4<='1';ELSE HH4<='0';END IF;
                 if H5=12754  then H5<="00000000000000000";
                    else   H5<=H5+1;                 
                 end if;
                 IF H5<6377 THEN HH5<='1';ELSE HH5<='0';END IF;
                 if H6=11764  then H6<="00000000000000000";
                    else    H6<=H6+1;                 
                 end if;
                 IF H6<5882 THEN HH6<='1';ELSE HH6<='0';END IF;
                 if H7=10124  then H7<="00000000000000000";
                    else    H7<=H7+1;                 
                 end if;
                 IF H7<5062 THEN HH7<='1';ELSE HH7<='0';END IF;
                 if time=2499999 then time<="0000000000000000000000000";c<='1';
                 else c<='0';
                 end if;               
           end if;
        end process;
   PROCESS(c,RST)
       BEGIN
        if c'event and c='1' then music21<=music21+1;
              if music21=263 then music21<="000000000";
              end if;
        end if;--(国歌)
        if music21>=0 and music21<=1 then out_bit<=LL5;END IF;
        IF music21>=2 and music21<=5 then out_bit<=MM1;END IF;
        IF music21>=6 and music21<=7  then out_bit<=MM1;END IF;
        IF music21>=8 and music21<=10  then out_bit<=mm1;END IF;
        IF music21=11 then out_bit<=MM1;END IF;
        IF music21>=12 and music21<=14  then out_bit<=LL5;END IF;
        IF music21=15 then out_bit<=LL6;END IF;
        if music21=16 then out_bit<=LL7;END IF;
        IF music21>=17 and music21<=20  then out_bit<=MM1;END IF;
        IF music21>=21 and music21<=24  then out_bit<=MM1;END IF;
        IF music21>=25 and music21<=26  then out_bit<=X0;END IF;
        IF music21>=27 and music21<=28  then out_bit<=MM3;END IF;
        IF music21>=29 and music21<=30  then out_bit<=MM1;END IF;
        IF music21=31 then out_bit<=MM2;END IF;
        if music21=32 then out_bit<=MM3;END IF;
        IF music21>=33 and music21<=38  then out_bit<=MM5;END IF;
        IF music21=39 then out_bit<=MM5;END IF;
        IF music21>=40 and music21<=48  then out_bit<=MM5;END IF;
        IF music21>=49 and music21<=53  then out_bit<=MM3;END IF;
        IF music21>=54 and music21<=56  then out_bit<=MM1;END IF;
        IF music21=57 then out_bit<=MM3;END IF;
        IF music21>=58 and music21<=60  then out_bit<=MM5;END IF;
        IF music21=61  then out_bit<=MM3;END IF;
        IF music21>=62 and music21<=71  then out_bit<=MM2;END IF;
        IF music21>=72 and music21<=75  then out_bit<=MM6;END IF;
        IF music21>=76 and music21<=79  then out_bit<=MM5;END IF;
        IF music21>=80 and music21<=83  then out_bit<=MM2;END IF;
        IF music21>=84 and music21<=87  then out_bit<=MM3;END IF;
        IF music21>=88 and music21<=89  then out_bit<=MM5;END IF;
        IF music21>=90 and music21<=93  then out_bit<=MM3;END IF;
        IF music21>=94 and music21<=95  then out_bit<=MM5;END IF;
        IF music21>=96 and music21<=97  then out_bit<=MM3;END IF;
        IF music21>=98  then out_bit<=MM2;END IF;
        IF music21>=93  then out_bit<=MM3;END IF;
        IF music21>=100 and music21<=102  then out_bit<=MM1;END IF;
        IF music21>=103  then out_bit<=MM2;END IF;
        IF music21>=104 and music21<=107  then out_bit<=MM3;END IF;
        IF music21>=108 and music21<=111  then out_bit<=X0;END IF;
        IF music21>=112 and music21<=114  then out_bit<=MM5;END IF;
        IF music21=115  then out_bit<=LL6;END IF;
        IF music21>=116 and music21<=119  then out_bit<=MM1;END IF;
        IF music21>=120 and music21<=123  then out_bit<=MM3;END IF;
        IF music21>=124 and music21<=127  then out_bit<=MM5;END IF;
        IF music21>=128 and music21<=131  then out_bit<=MM2;END IF;
        IF music21>=132 and music21<=135  then out_bit<=LL6;END IF;
        IF music21>=136 and music21<=141  then out_bit<=MM2;END IF;
        IF music21>=142 and music21<=143  then out_bit<=LL5;END IF;
        IF music21>=144 and music21<=151  then out_bit<=MM1;END IF;
        IF music21>=152 and music21<=159  then out_bit<=MM3;END IF;
        IF music21>=160 and music21<=175  then out_bit<=MM5;END IF;
        IF music21>=176 and music21<=178  then out_bit<=MM1;END IF;
        IF music21=179  then out_bit<=MM3;END IF;
        IF music21>=180 and music21<=183  then out_bit<=MM5;END IF;
        IF music21>=184 and music21<=187  then out_bit<=MM6;END IF;
        IF music21>=188 and music21<=191  then out_bit<=MM5;END IF;
        IF music21>=192 and music21<=194  then out_bit<=MM3;END IF;
        IF music21=195  then out_bit<=MM1;END IF;
        IF music21>=196 and music21<=199  then out_bit<=MM5;END IF;
        IF music21>=200 and music21<=201  then out_bit<=MM3;END IF;
        IF music21>=202 and music21<=203  then out_bit<=X0;END IF;
        IF music21>=204 and music21<=205  then out_bit<=MM1;END IF;
        IF music21>=206 and music21<=207  then out_bit<=X0;END IF;
        IF music21>=208 and music21<=211  then out_bit<=LL5;END IF;
        IF music21>=212 and music21<=215  then out_bit<=MM1;END IF;
        IF music21>=216 and music21<=218  then out_bit<=MM3;END IF;
        IF music21=219  then out_bit<=MM1;END IF;
        IF music21>=220 and music21<=223  then out_bit<=MM5;END IF;
        IF music21>=224 and music21<=225  then out_bit<=MM3;END IF;
        IF music21>=226 and music21<=227  then out_bit<=X0;END IF;
        IF music21>=228 and music21<=229  then out_bit<=MM1;END IF;
        IF music21>=230 and music21<=231  then out_bit<=X0;END IF;
        IF music21>=232 and music21<=235  then out_bit<=LL5;END IF;
        IF music21>=236 and music21<=239  then out_bit<=MM1;END IF;
        IF music21>=240 and music21<=243  then out_bit<=LL5;END IF;
        IF music21>=244 and music21<=247  then out_bit<=MM1;END IF;
        IF music21>=248 and music21<=251  then out_bit<=LL5;END IF;
        IF music21>=252 and music21<=255  then out_bit<=MM1;END IF;              
        IF music21>=256 and music21<=259  then out_bit<=MM1;END IF;
        IF music21>=260 and music21<=263  then out_bit<=X0;END IF;
IF RST='0' THEN MUSIC21<="000000000";END IF;   
    END PROCESS;
end;
二、以50M的时钟频率编写:
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_arith.all;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY music IS
 PORT(CLK,rst:in std_logic;    
         out_bit:OUT STD_LOGIC);
END music;
ARCHITECTURE PVJ OF music IS
  SIGNAL clk_div1:STD_LOGIC_VECTOR(2 DOWNTO 0);
  SIGNAL clk_div2:STD_LOGIC_VECTOR(12 DOWNTO 0);
  SIGNAL cnt:STD_LOGIC_VECTOR(20 DOWNTO 0);
  signal  state:STD_LOGIC_VECTOR(2 DOWNTO 0);
  constant duo    :STD_LOGIC_VECTOR(12 DOWNTO 0):="0111011101110";--3822频率为1308.2Hz
  constant lai    :STD_LOGIC_VECTOR(12 DOWNTO 0):="0110101001101";--3405频率为1468.4Hz
  constant mi     :STD_LOGIC_VECTOR(12 DOWNTO 0):="0101111011010";--3034频率为1647.9Hz
  constant fa     :STD_LOGIC_VECTOR(12 DOWNTO 0):="0101100110001";--2865频率为1745.2Hz
  constant suo    :STD_LOGIC_VECTOR(12 DOWNTO 0):="0100111110111";--2551频率为1960.0Hz
  constant la     :STD_LOGIC_VECTOR(12 DOWNTO 0):="0100011100001";--2273频率为2199.7Hz
  constant xi     :STD_LOGIC_VECTOR(12 DOWNTO 0):="0011111101000";--2024频率为2470.3Hz
  constant duo1   :STD_LOGIC_VECTOR(12 DOWNTO 0):="0011101110111";--1911频率为2616.4Hz
  signal out_bit_tmp: STD_LOGIC;
    BEGIN 
    out_bit<=out_bit_tmp;
      process(clk,rst)
        begin
          if  rst='0' then
            clk_div1<="000";
             elsif clk'event and clk='1' then                   --将20M频率6分频
                 if clk_div1=5  then clk_div1<="000";
                    else    clk_div1<=clk_div1+1;                 
                 end if;
             end if;
        end process;
     process(clk,rst)
      begin
          if rst='0' then
               clk_div2<="0000000000000";
               state<="000";
               cnt<="000000000000000000000";--决定音符长短
               out_bit_tmp<='0';
           elsif clk'event and clk='1' then
               if clk_div1=5 then
                  case state is
                     when "000"=> cnt<=cnt+1;                       
                         if cnt="111111111111111111111" then state<="001";                          
                         end if;
                         if clk_div2=duo then
                           clk_div2<="0000000000000";
                           out_bit_tmp<=not out_bit_tmp; 
                         else
                             clk_div2<=clk_div2+1;                          
                         end if;
                      when "001"=>cnt<=cnt+1;                        
                         if cnt="111111111111111111111" then state<="010";                          
                         end if;
                         if clk_div2=lai then
                            clk_div2<="0000000000000";
                            out_bit_tmp<=not out_bit_tmp;                             
                           else
                            clk_div2<=clk_div2+1;
                          end if;
                      when "010"=>cnt<=cnt+1;
                         if cnt="111111111111111111111" then state<="011";                          
                         end if;
                         if clk_div2=mi then
                             clk_div2<="0000000000000";
                             out_bit_tmp<=not out_bit_tmp; 
                         else                          
                            clk_div2<=clk_div2+1;
                         end if;
                      when "011"=>cnt<=cnt+1;                        
                         if cnt="111111111111111111111" then state<="100";
                         end if;
                         if clk_div2=fa then
                            clk_div2<="0000000000000"; 
                            out_bit_tmp<=not out_bit_tmp;          
                         else                          
                            clk_div2<=clk_div2+1;
                         end if;
                      when "100"=> cnt<=cnt+1;                       
                         if cnt="111111111111111111111" then  state<="101";
                         end if;
                         if clk_div2=suo then
                            clk_div2<="0000000000000";
                            out_bit_tmp<=not out_bit_tmp;
                         else                         
                            clk_div2<=clk_div2+1;
                         end if;
                      when "101"=> cnt<=cnt+1;
                         if cnt="111111111111111111111" then state<="110";                           
                         end if;
                         if clk_div2=la then
                            out_bit_tmp<=not out_bit_tmp;  
                            clk_div2<="0000000000000";
                         else
                              clk_div2<=clk_div2+1;
                         end if;
                      when "110"=>cnt<=cnt+1;                        
                         if cnt="111111111111111111111" then  state<="111";
                         end if;
                         if clk_div2=xi then
                           out_bit_tmp<=not out_bit_tmp;
                           clk_div2<="0000000000000";
                         else                          
                             clk_div2<=clk_div2+1;
                         end if;
                      when "111"=> cnt<=cnt+1;                       
                         if cnt="111111111111111111111" then state<="010";                          
                         end if;
                         if clk_div2=duo1 then
                            out_bit_tmp<=not out_bit_tmp;
                            clk_div2<="0000000000000";
                         else
                              clk_div2<=clk_div2+1;
                         end if;
                       when others=>null;
                   end case;
               end if;
           end if;
       end process;      
end;
音 乐 和 简 谱 知 识

  和语言一样,不同民族都有过自己创立并传承下来的记录音乐的方式---记谱法。各民族的记谱方式各有千秋,但是目前被更广泛使用的是五线谱和简谱(据说简谱是由法国思想家卢梭于1742年发明的)。
  
  简谱应该说是一种比较简单易学的音乐记谱法。它的最大好处是仅用7个阿拉伯数字----1234567,就能将万千变化的音乐曲子记录并表示出来,并能使人很快记住而终身不忘;同时涉及其他的音乐元素也基本可以正确显示。简谱虽然不是出现在中国,但是好象只有在中国得到非常广泛的传播。

  一般来说,所有音乐的构成有四个基本要素,而其中最重要的是“音的高低”和“音的长短”:

    1 音的高低:任何一首曲子都是高低相间的音组成的,从钢琴上直观看就是越往左面的键盘音越低,越往右面的键盘音越高。

    2 音的长短:除了音的高低外,还有一个重要的因素就是音的长短。音的高低和长短的标住决定了该首曲子有别于另外的曲子,因此成为构成音乐的最重要的基础元素。

    3 音的力度:音乐的力度很容易理解,也叫强度。一首音乐作品总会有一些音符的力度比教强一些,有些地方弱一些。而力度的变化是音乐作品中表达情感的因素之一。

    4 音质:也可以称音色。也就是发出音乐的乐器或人声。同样的旋律音高男生和女声唱就不一样的音色;小提琴和钢琴的音色就不一样。

    上述四项构成了任一首乐曲的基础元素。应该说简谱基本可以将这些基础性元素正确标住。

关闭窗口

相关文章