歌曲简谱转换为频率、时长数组

#include "stdio.h"
#include "stdint.h"

const uint16_t c_key_scale_freq[] = {
    262,    294,    330,    349,    392,    440,    494,    /* C调低音7个音阶 */
    523,    587,    659,    698,    784,    880,    988,    /* C调中音7个音阶 */
    1064,   1175,   1318,   1397,   1568,   1760,   1967    /* C调高音7个音阶 */
};

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;     /* 1拍时长 */
    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
请按任意键继续. . .
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值