- 简谱基本知识介绍:点这里查看
- 测试简谱《five hundred miles》:点这里查看
- 代码实现及测试例程如下
#include "stdio.h"
#include "stdint.h"
const uint16_t c_key_scale_freq[] = {
262, 294, 330, 349, 392, 440, 494,
523, 587, 659, 698, 784, 880, 988,
1064, 1175, 1318, 1397, 1568, 1760, 1967
};
void translate(const char notation[], uint16_t freq_time[])
{
uint16_t i = 0, j = 0;
char symbol = 0;
int8_t scale_spacing = 0;
bool time_counting = false;
const uint16_t one_beat_duration = 250;
uint16_t time = one_beat_duration;
for (i = 0; notation[i] != '\0'; i++)
{
symbol = notation[i];
if (symbol == '|' || symbol == ' ')
{
continue;
}
if (symbol == ',' || symbol == '`')
{
if (symbol == ',')
{
scale_spacing = -7;
}
else if (symbol == '`')
{
scale_spacing = 7;
}
}
if (symbol >= '1' && symbol <= '7')
{
freq_time[j++] = c_key_scale_freq[symbol - '1' + 7 + scale_spacing];
scale_spacing = 0;
time = one_beat_duration;
while (notation[i+1] != '\0')
{
symbol = notation[++i];
if (symbol == '-' || symbol == '_' || symbol == '.')
{
switch (symbol)
{
case '-': time += one_beat_duration; break;
case '_': time /= 2; break;
case '.': time += time / 2; break;
default: break;
}
}
else
{
freq_time[j++] = time;
i--;
break;
}
}
}
}
}
int main (void)
{
const char notation[] = {
"|,513.3_2_1.3-|2_1.2.3_2_1.,6-|,6_1_2.3_2_1.,6_,5.,5_,5_,6_1_1-|1_113.3_2_1.3.3_2_1.2.3_2_1.,6-|\
,6_1_2.3_2_1.,6_,5.,5_,5_,6_1_1-|113-2_1.3-2_1.2.3_2_1.,6-|,6_1_2.3_2_1.,6_,5.,5_,5_,6_1_1-|\
113-2_1.3-2_1.2.3_2_1.,6-|,6_1_2.3_2_1.,6_,5.,5_,5_,6_1_1-|"
};
uint16_t song[500] = {0};
translate(notation, song);
for (int i = 0; song[i] != 0; i++)
{
if ((i % 20) == 0)
{
printf("\n");
}
printf("%4d,", song[i]);
}
return 0;
}
392, 250, 523, 250, 659, 375, 659, 125, 587, 125, 523, 375, 659, 500, 587, 125, 523, 375, 587, 375,
659, 125, 587, 125, 523, 375, 440, 500, 440, 125, 523, 125, 587, 375, 659, 125, 587, 125, 523, 375,
440, 125, 392, 375, 392, 125, 392, 125, 440, 125, 523, 125, 523, 500, 523, 125, 523, 250, 523, 250,
659, 375, 659, 125, 587, 125, 523, 375, 659, 375, 659, 125, 587, 125, 523, 375, 587, 375, 659, 125,
587, 125, 523, 375, 440, 500, 440, 125, 523, 125, 587, 375, 659, 125, 587, 125, 523, 375, 440, 125,
392, 375, 392, 125, 392, 125, 440, 125, 523, 125, 523, 500, 523, 250, 523, 250, 659, 500, 587, 125,
523, 375, 659, 500, 587, 125, 523, 375, 587, 375, 659, 125, 587, 125, 523, 375, 440, 500, 440, 125,
523, 125, 587, 375, 659, 125, 587, 125, 523, 375, 440, 125, 392, 375, 392, 125, 392, 125, 440, 125,
523, 125, 523, 500, 523, 250, 523, 250, 659, 500, 587, 125, 523, 375, 659, 500, 587, 125, 523, 375,
587, 375, 659, 125, 587, 125, 523, 375, 440, 500, 440, 125, 523, 125, 587, 375, 659, 125, 587, 125,
523, 375, 440, 125, 392, 375, 392, 125, 392, 125, 440, 125, 523, 125, 523, 500,
--------------------------------
Process exited after 0.1434 seconds with return value 0
请按任意键继续. . .