Skip to content

基于HMM的和弦生成工具(支持基于OpenGL的GPU加速)

License

Notifications You must be signed in to change notification settings

SingingRivulet/chcpy

Repository files navigation

基于 hmm 的自动和弦生成工具:

使用:

CPU 端:

    chcpy::seq2id::dict_t dict_seq;//旋律字典
    chcpy::chord2id::dict_t dict_chord;//和弦字典
    chcpy::melody2chord::chordMap chordmap;//和弦分割表
    chcpy::hmm::hmm_predict_t model;//hmm模型

    chcpy::seq2id::load(dict_seq, "../data/melodydict.txt");//加载旋律字典
    chcpy::chord2id::load(dict_chord, "../data/chorddict.txt");//加载和弦字典
    chcpy::hmm::load_text(model, "../data/model");//加载模型

    midiSearch::chord_t chord;
    for (auto line : midiSearch::lineReader("test.txt")) {//逐条载入样本
        midiSearch::melody_t melody;
        midiSearch::str2melody(line->c_str(), melody);//加载样本文本格式
        chcpy::predict::gen(chordmap, dict_seq, dict_chord, model, melody, chord);//生成和弦
        //和弦在chord中
    }

GPU 端:

GPU 端基于 OpenGL ES 3 的计算着色器。如须使用该功能,在上述代码该位置添加chcpy::gpu::hmm_t,将模型复制到 GPU 端

    ......
    chcpy::seq2id::load(dict_seq, "../data/melodydict.txt");//加载旋律字典
    chcpy::chord2id::load(dict_chord, "../data/chorddict.txt");//加载和弦字典
    chcpy::hmm::load_text(model, "../data/model");//加载模型

    chcpy::gpu::hmm_t model_gpu(&context, &model);  //将模型复制到gpu端
    ....

复制完成后,像在 CPU 端一样使用 model_gpu 即可。

测试:

执行./test/chordGen./test/chordGen_gpu可查看效果
实际效果:

输入

[0,0,0,0,45,45,45,45,48,48,48,48,48,48,41,41,43,43,43,43,43,43,43,43,0,0,0,0,0,0,0,0,0,0,0,0,43,43,43,43,45,45,45,45,45,45,43,43,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,45,45,45,45,45,45,45,45,0,0,0,0,0,0,0,0,0,0,0,0,45,45,45,45,50,50,50,50,50,50,50,50,50,50,46,46,46,46,46,46,0,0,0,0,0,0,0,0,0,0,0,0,43,43,43,43,48,48,48,48,48,48,50,50,48,48,48,48,48,48,48,48,0,0,0,0,0,0,0,0,0,0,48,48,48,48,48,48,48,48,48,48,50,50,45,45,45,45,45,45,45,45,45,45,0,0,0,0,0,0,0,0,45,45,45,45,45,45,45,45,0,0,0,0,0,0,0,0,0,0,45,45,45,45,45,45,50,50,50,50,50,50,50,50,50,50,46,46,46,46,46,46,0,0,0,0,0,0,0,0,0,0,0,0,43,43,43,43,48,48,48,48,48,48,50,48,48,48,48,48,48,48,48,48,0,0,0,0,0,0,0,0,0,0,0,0,48,48,48,48,48,48,48,48,48,48,48,50,50,50,50,50,50,50,50,50,0,0,0,0,0,0,0,0,0,0,0,0,50,50,50,50,53,53,53,53,53,53,50,50,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,0,0,0,0,50,50,50,50,53,53,53,53,53,53,50,50,52,52,52,52,52,52,52,52,0,0,0,0,0,0,0,0,0,0,0,0,52,52,52,52,52,52,52,52,52,52,52,52,53,53,53,53,53,53,50,50,50,50,50,50,48,48,46,46,46,46,46,46,46,46,46,46,53,53,46,46,46,46,43,43,45,45,45,45,45,45,45,45,0,0,0,0,0,0,0,0]

输出

CPU 版


用时0.607999秒
41_M3(w=0.000000) len(seq)=4
旋律		模型输出	实际结果
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[48 48 48 48 ]	[0 4 8 ]	(29 33 36 )
[48 48 41 41 ]	[0 4 8 ]	(29 33 36 )

用时1.231054秒
43_-5(w=2.000000) len(seq)=6
旋律		模型输出	实际结果
[43 43 43 43 ]	[0 4 8 ]	(31 35 38 )
[43 43 43 43 ]	[0 4 8 ]	(31 35 38 )
[0 0 0 0 ]	[0 4 8 ]	(31 35 38 )
[0 0 0 0 ]	[0 4 8 ]	(31 35 38 )
[0 0 0 0 ]	[0 4 8 ]	(31 35 38 )
[43 43 43 43 ]	[0 4 8 ]	(31 35 38 )

用时2.441387秒
41_-5(w=3.500000) len(seq)=12
旋律		模型输出	实际结果
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[45 45 43 43 ]	[0 4 8 ]	(29 33 36 )
[41 41 41 41 ]	[0 4 8 ]	(29 33 36 )
[41 41 41 41 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )

用时1.242936秒
43_m3(w=0.000000) len(seq)=8
旋律		模型输出	实际结果
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 46 46 ]	[0 3 8 ]	(31 34 38 )
[46 46 46 46 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[43 43 43 43 ]	[0 3 8 ]	(31 34 38 )

用时4.343543秒
45_m3(w=4.500000) len(seq)=20
旋律		模型输出	实际结果
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 50 50 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[0 0 0 0 ]	[4 9 12 ]	(36 40 43 )
[0 0 0 0 ]	[4 9 12 ]	(36 40 43 )
[0 0 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[50 50 45 45 ]	[4 9 12 ]	(36 40 43 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )

用时1.207562秒
43_m3(w=0.000000) len(seq)=8
旋律		模型输出	实际结果
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 46 46 ]	[0 3 8 ]	(31 34 38 )
[46 46 46 46 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[43 43 43 43 ]	[0 3 8 ]	(31 34 38 )

用时2.412206秒
48_-5(w=3.500000) len(seq)=10
旋律		模型输出	实际结果
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 50 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[0 0 0 0 ]	[0 4 8 ]	(36 40 43 )
[0 0 0 0 ]	[0 4 8 ]	(36 40 43 )
[0 0 0 0 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 50 ]	[0 4 8 ]	(36 40 43 )

用时3.459711秒
50_m3(w=1.000000) len(seq)=16
旋律		模型输出	实际结果
[50 50 50 50 ]	[0 4 8 ]	(38 41 45 )
[50 50 50 50 ]	[0 4 8 ]	(38 41 45 )
[0 0 0 0 ]	[0 4 8 ]	(38 41 45 )
[0 0 0 0 ]	[0 4 8 ]	(38 41 45 )
[0 0 0 0 ]	[0 4 8 ]	(38 41 45 )
[50 50 50 50 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 50 50 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[0 0 0 0 ]	[10 0 4 ]	(35 38 41 )
[50 50 50 50 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 50 50 ]	[10 0 4 ]	(35 38 41 )

用时1.987380秒
52_-5(w=2.000000) len(seq)=8
旋律		模型输出	实际结果
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[0 0 0 0 ]	[0 4 8 ]	(40 43 47 )
[0 0 0 0 ]	[0 4 8 ]	(40 43 47 )
[0 0 0 0 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )

用时0.727281秒
43_m7(w=2.500000) len(seq)=8
旋律		模型输出	实际结果
[53 53 53 53 ]	[3 8 12 ]	(34 38 41 )
[53 53 50 50 ]	[3 8 12 ]	(34 38 41 )
[50 50 50 50 ]	[3 8 12 ]	(34 38 41 )
[48 48 46 46 ]	[3 8 12 ]	(34 38 41 )
[46 46 46 46 ]	[3 8 12 ]	(34 38 41 )
[46 46 46 46 ]	[3 8 12 ]	(34 38 41 )
[53 53 46 46 ]	[3 8 12 ]	(34 38 41 )
[46 46 43 43 ]	[3 8 12 ]	(34 38 41 )

用时0.865151秒
45_-5(w=2.000000) len(seq)=4
旋律		模型输出	实际结果
[45 45 45 45 ]	[0 4 8 ]	(33 36 40 )
[45 45 45 45 ]	[0 4 8 ]	(33 36 40 )
[0 0 0 0 ]	[0 4 8 ]	(33 36 40 )
[0 0 0 0 ]	[0 4 8 ]	(33 36 40 )
=========================================================

GPU 版


用时0.003832秒
41_M3(w=0.000000) len(seq)=4
旋律		模型输出	实际结果
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[48 48 48 48 ]	[0 4 8 ]	(29 33 36 )
[48 48 41 41 ]	[0 4 8 ]	(29 33 36 )

用时0.005096秒
43_-5(w=2.000000) len(seq)=6
旋律		模型输出	实际结果
[43 43 43 43 ]	[0 4 8 ]	(31 35 38 )
[43 43 43 43 ]	[0 4 8 ]	(31 35 38 )
[0 0 0 0 ]	[0 4 8 ]	(31 35 38 )
[0 0 0 0 ]	[0 4 8 ]	(31 35 38 )
[0 0 0 0 ]	[0 4 8 ]	(31 35 38 )
[43 43 43 43 ]	[0 4 8 ]	(31 35 38 )

用时0.009922秒
41_-5(w=3.500000) len(seq)=12
旋律		模型输出	实际结果
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[45 45 43 43 ]	[0 4 8 ]	(29 33 36 )
[41 41 41 41 ]	[0 4 8 ]	(29 33 36 )
[41 41 41 41 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[0 0 0 0 ]	[0 4 8 ]	(29 33 36 )
[45 45 45 45 ]	[0 4 8 ]	(29 33 36 )

用时0.007204秒
43_m3(w=0.000000) len(seq)=8
旋律		模型输出	实际结果
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 46 46 ]	[0 3 8 ]	(31 34 38 )
[46 46 46 46 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[43 43 43 43 ]	[0 3 8 ]	(31 34 38 )

用时0.016720秒
45_m3(w=4.500000) len(seq)=20
旋律		模型输出	实际结果
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 50 50 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[0 0 0 0 ]	[4 9 12 ]	(36 40 43 )
[0 0 0 0 ]	[4 9 12 ]	(36 40 43 )
[0 0 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[48 48 48 48 ]	[4 9 12 ]	(36 40 43 )
[50 50 45 45 ]	[4 9 12 ]	(36 40 43 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 0 0 ]	[6 11 12 0 ]	(26 30 31 33 )
[0 0 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )
[45 45 45 45 ]	[6 11 12 0 ]	(26 30 31 33 )

用时0.006242秒
43_m3(w=0.000000) len(seq)=8
旋律		模型输出	实际结果
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 50 50 ]	[0 3 8 ]	(31 34 38 )
[50 50 46 46 ]	[0 3 8 ]	(31 34 38 )
[46 46 46 46 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[0 0 0 0 ]	[0 3 8 ]	(31 34 38 )
[43 43 43 43 ]	[0 3 8 ]	(31 34 38 )

用时0.007571秒
48_-5(w=3.500000) len(seq)=10
旋律		模型输出	实际结果
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 50 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[0 0 0 0 ]	[0 4 8 ]	(36 40 43 )
[0 0 0 0 ]	[0 4 8 ]	(36 40 43 )
[0 0 0 0 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 48 ]	[0 4 8 ]	(36 40 43 )
[48 48 48 50 ]	[0 4 8 ]	(36 40 43 )

用时0.012121秒
50_m3(w=1.000000) len(seq)=16
旋律		模型输出	实际结果
[50 50 50 50 ]	[0 4 8 ]	(38 41 45 )
[50 50 50 50 ]	[0 4 8 ]	(38 41 45 )
[0 0 0 0 ]	[0 4 8 ]	(38 41 45 )
[0 0 0 0 ]	[0 4 8 ]	(38 41 45 )
[0 0 0 0 ]	[0 4 8 ]	(38 41 45 )
[50 50 50 50 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 50 50 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[0 0 0 0 ]	[10 0 4 ]	(35 38 41 )
[50 50 50 50 ]	[10 0 4 ]	(35 38 41 )
[53 53 53 53 ]	[10 0 4 ]	(35 38 41 )
[53 53 50 50 ]	[10 0 4 ]	(35 38 41 )

用时0.003820秒
52_-5(w=2.000000) len(seq)=8
旋律		模型输出	实际结果
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[0 0 0 0 ]	[0 4 8 ]	(40 43 47 )
[0 0 0 0 ]	[0 4 8 ]	(40 43 47 )
[0 0 0 0 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )
[52 52 52 52 ]	[0 4 8 ]	(40 43 47 )

用时0.003228秒
43_m7(w=2.500000) len(seq)=8
旋律		模型输出	实际结果
[53 53 53 53 ]	[3 8 12 ]	(34 38 41 )
[53 53 50 50 ]	[3 8 12 ]	(34 38 41 )
[50 50 50 50 ]	[3 8 12 ]	(34 38 41 )
[48 48 46 46 ]	[3 8 12 ]	(34 38 41 )
[46 46 46 46 ]	[3 8 12 ]	(34 38 41 )
[46 46 46 46 ]	[3 8 12 ]	(34 38 41 )
[53 53 46 46 ]	[3 8 12 ]	(34 38 41 )
[46 46 43 43 ]	[3 8 12 ]	(34 38 41 )

用时0.003611秒
45_-5(w=2.000000) len(seq)=4
旋律		模型输出	实际结果
[45 45 45 45 ]	[0 4 8 ]	(33 36 40 )
[45 45 45 45 ]	[0 4 8 ]	(33 36 40 )
[0 0 0 0 ]	[0 4 8 ]	(33 36 40 )
[0 0 0 0 ]	[0 4 8 ]	(33 36 40 )
=========================================================