一、简介
因为学习了单片机的无源蜂鸣器,想要播放音乐时,简谱得手动转变为频率数组和声音延时,故随便写了一个简谱转换数组工具,源代码在文末。不过由于没有精力制作GUI界面,所以使用仍然有待优化。
代码仅仅做了低音中音高音的三部分,如果需要转换复杂的曲子则手动添加和修改频率对照数组。
二、输入对照表
输入对应的字符代表对应的简谱符号:

(注意:输入字符均为英文输入法下的字符),用法如下:
简谱 -> 输入
5— -> 5— 延时3拍
5- -> 5- 延时1拍
5· -> 5~ 附点音符,时长延长每拍的二分之一
5 -> 5_ 减时线,时长变为一拍的二分之一
5(顶上一个点) -> 5` 高音
5(底下一个点) -> 5. 低音
5# -> #5 升音(后面紧跟数字)
对于小节线“ | ”可以输入,程序自动忽略
三、参考实例

下面展示要如何输入以上简谱到程序:
|5_5_|651`|705_5_|652`|1`-5_5_|
程序输出:(前面是频率,后面是时长)

再来一个:

|3~_3__3~_3__|2~_1__2_30|1_1_1_6._2_12_|3-00|
程序输出:

四、源代码
#include <stdio.h>
#include <string.h>
int ToNumber(char num);
int CheckNumber(char num);
void MusicAdd(int note, char time);
int TransChar(const char* notation, unsigned int* gap, int* pitch, unsigned int* time);
unsigned char Notation[500]; //输入要转换的简谱
const unsigned int NoteTime = 500; //四分音符时长,单位:ms
unsigned char Note = 4, MusicLen = 0; //Note是每拍的时长
unsigned int Music[1000]; //转换后的音乐,每两个数据为一组,前是频率,后是时长
unsigned int CounterTable[] =
{
0,
63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64524,
64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283
}; // 51单片机定时器计数值
unsigned int FreqTable[] =
{
0,
262,277,294,311,330,349,370,392,415,440,466,494,
523,554,587,622,659,698,740,784,831,880,932,988,
1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976
}; //低音中音高音频率对照表,包含了升音#
unsigned char NormalIndex[] = { 0, 13, 15, 17, 18, 20, 22, 24 };
unsigned char SharpIndex[] = { 0, 14, 16, 0, 19, 21, 23, 0 };
int main(void)
{
unsigned int TranSelect = 0, SharpFlag = 0; //当前字符串处理索引
printf("输入简谱:rn");
gets(Notation);
while (Notation[TranSelect])
{
if (Notation[TranSelect] == '|')
{
TranSelect++;
continue;
}
if (Notation[TranSelect] == '#')
{
SharpFlag = 1;
TranSelect++;
continue;
}
if (CheckNumber(Notation[TranSelect]))
{
int pitch = 0, gap = 1, time = Note, FreqIndex; //pitch:低中高音的标志系数,gap:这个数字到下一个数字之间的序列差值
TransChar(Notation + TranSelect, &gap, &pitch, &time);
if (SharpFlag)
{
SharpFlag = 0;
FreqIndex = SharpIndex[ToNumber(Notation[TranSelect])] + pitch * 12;
}
else
{
FreqIndex = NormalIndex[ToNumber(Notation[TranSelect])] + pitch * 12;
}
MusicAdd(FreqTable[FreqIndex], time);
//MusicAdd(FreqIndex, time);
TranSelect += gap; //跳转到下一个数字
}
else
{
TranSelect++;
}
}
printf("{rn");
int p = 0;
while (p < MusicLen)
{
printf("%d, %d,rn", Music[p], Music[p+1]);
p += 2;
}
printf("0xFFrn}");
getchar();
return 0;
}
int CheckNumber(char num)
{
return (num >= '0' && num <= '7');
}
int ToNumber(char num)
{
if (CheckNumber(num))
return num - '0';
}
int TransChar(const char* notation, unsigned int* gap, int* pitch, unsigned int* time)
{
unsigned int p = 1;
while (notation[p] && !CheckNumber(notation[p])) //不是数字则为延时或升降音
{
switch (notation[p])
{
case '.':
(*pitch)--;
break;
case '`':
(*pitch)++;
break;
case '~':
(*time) *= 1.5f;
break;
case '_':
(*time) *= 0.5f;
break;
case '-':
(*time) += Note;
break;
case '#':
(*gap)--;
break;
}
p++;
(*gap)++;
}
}
void MusicAdd(int note, char time)
{
static unsigned char p = 0;
Music[p++] = note;
Music[p++] = time;
MusicLen = p;
}
思路仅作分享,如有错误欢迎评论指正。
