給大家分享下51單片機播放音樂簡譜的函數庫:
COPYRIGHT (c) 2005 BY JJJ.
--ALL RIGHTS RESERVED--
File Name: SoundPlay.h
Author: Jiang Jian Jun
Created: 2005/5/16
Modified:NO
Revision: 1.0
說明
曲譜存貯格式 unsigned char code MusicName{音高,音長,音高,音長。..。, 0,0}; 末尾:0,0 表示結束(Important)
音高由三位數字組成:
個位是表示 1~7 這七個音符
十位是表示音符所在的音區:1-低音,2-中音,3-高音;
百位表示這個音符是否要升半音: 0-不升,1-升半音。
音長最多由三位數字組成:
個位表示音符的時值,其對應關系是:
|數值(n):|0 |1 |2 |3 | 4 | 5 | 6
|幾分音符: |1 |2 |4 |8 |16 |32 |64 音符=2^n
十位表示音符的演奏效果(0-2):0-普通,1-連音,2-頓音
百位是符點位: 0-無符點,1-有符點
調用演奏子程序的格式
Play(樂曲名,調號,升降八度,演奏速度);
|樂曲名: 要播放的樂曲指針,結尾以(0,0)結束;
|調號(0-11) : 是指樂曲升多少個半音演奏;
|升降八度(1-3) : 1:降八度, 2:不升不降, 3:升八度;
|演奏速度(1-12000): 值越大速度越快;
***************************************************************************/
#ifndef __SOUNDPLAY_H_REVISION_FIRST__
#define __SOUNDPLAY_H_REVISION_FIRST__
#include
/**************************************************************************
#define SYSTEM_OSC 11059200/12000000 /定義晶振頻率12000000HZ
#define SOUND_SPACE4/5 /定義普通音符演奏的長度分率,/每4分音符間隔
sbit BeepIO = P2^6;/定義輸出管腳
unsigned intcode FreTab[12]= { 262,277,294,311,330,349,369,392,415,440,466,494 }; /原始頻率表
unsigned char code SignTab[7]= { 0,2,4,5,7,9,11 }; /1~7在頻率表中的位置
unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };
unsigned char Sound_Temp_TH0,Sound_Temp_TL0; /音符定時器初值暫存
unsigned char Sound_Temp_TH1,Sound_Temp_TL1; /音長定時器初值暫存
/**************************************************************************
void InitialSound(void)
{
BeepIO = 1;
Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; / 計算TL1應裝入的初值(10ms的初裝值)
Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%6; / 計算TH1應裝入的初值
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TMOD|= 0x11;
ET0 = 1;
ET1 = 0;
TR0 = 0;
TR1 = 0;
EA = 1;
}
void BeepTimer0(void) interrupt 1 /音符發生中斷
{
BeepIO = !BeepIO;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0;
}
/**************************************************************************
void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed)
{
unsigned int NewFreTab[12];/新的頻率表
unsigned char i,j;
unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;
unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;
for(i=0;i12;i) / 根據調號及升降八度來生成新的頻率表
{
j = i Signature;
if(j11)
{
j = j-12;
NewFreTab[i] = FreTab[j]*2;
}
else
NewFreTab[i] = FreTab[j];
if(Octachord == 1)
NewFreTab[i]=2;
else if(Octachord == 3)
NewFreTab[i]=2;
}
SoundLength = 0;
while(Sound[SoundLength] != 0x00) /計算歌曲長度
{
SoundLength =2;
}
Point = 0;
Tone = Sound[Point]; br / Length = Sound[Point 1]; / 讀出第一個音符和它時時值
LDiv0 = 12000/Speed; / 算出1分音符的長度(幾個10ms)
LDiv4 = LDiv0/4; / 算出4分音符的長度
LDiv4 = LDiv4-LDiv4*SOUND_SPACE;/ 普通音最長間隔標準
TR0 = 0;
TR1 = 1;
while(PointSoundLength)
{
SL=Tone; /計算出音符
SM=Tone/10; /計算出高低音
SH=Tone/100; /計算出是否升半
CurrentFre = NewFreTab[SignTab[SL-1] SH];/查出對應音符的頻率
if(SL!=0)
{
if (SM==1) CurrentFre = 2; /低音
if (SM==3) CurrentFre = 2; /高音
Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);/計算計數器初值
Sound_Temp_TH0 = Temp_T/256;
Sound_Temp_TL0 = Temp_T%6;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0 12; /加12是對中斷延時的補償
}
SLen=LengthTab[Length];/算出是幾分音符
XG=Length/10; /算出音符類型(0普通1連音2頓音)
FD=Length/100;
LDiv=LDiv0/SLen; /算出連音音符演奏的長度(多少個10ms)
if (FD==1)
LDiv=LDiv LDiv/2;
if(XG!=1)
if(XG==0) /算出普通音符的演奏長度
if (SLen=4)
LDiv1=LDiv-LDiv4;
else
LDiv1=LDiv*SOUND_SPACE;
else
LDiv1=LDiv/2; /算出頓音的演奏長度
else
LDiv1=LDiv;
if(SL==0) LDiv1=0;
LDiv2=LDiv-LDiv1; /算出不發音的長度
if (SL!=0)
{
TR0=1;
for(i=LDiv1;i0;i--)/發規定長度的音
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
if(LDiv2!=0)
{
TR0=0; BeepIO=1;
for(i=LDiv2;i0;i--)/音符間的間隔
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
Point =2;
Tone=Sound[Point];
Length=Sound[Point 1];
}
BeepIO = 1;
}
/**************************************************************************
#endif
以下為曲譜編碼文件,自己可以根據規則,對照簡譜編寫曲譜編碼表,我已經做好了一個應用程序,只需將簡譜輸入進去,就可以直接輸出曲譜編碼表,省去人工編碼的痛苦。此軟件作為共享軟件發布,此東東絕對是搞電子設計在校大學生泡MM的巨佳手段,呵呵。
▎附錄:
/揮著翅膀的女孩
unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03,
0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03,
0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03,
0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02,
0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03,
0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03,
0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03,
0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03,
0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02,
0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03,
0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03,
0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
bsp; 0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03,
0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03,
0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,
0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E,
0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03,
0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66,
0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03,
0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03,
0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04,
0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03,
0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03,
0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02,
0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02,
0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00};
評論
查看更多