How do I transpose music chords using JavaScript?

前端 未结 7 1370
忘掉有多难
忘掉有多难 2021-02-08 08:56

I was wondering how would one create a javascript function for transposing music chords.

Since I don\'t expect everyone to be a musician here, I\'ll try to explain how i

7条回答
  •  忘了有多久
    2021-02-08 09:41

    function transposechord(chord, amount){
       var scale = ["C","Cb","C#","D","Db","D#","E","Eb","E#","F","Fb","F#","G","Gb","G#",
             "A","Ab","A#","B","Bb","B#"];
       var transp = ["Cb","C","C#","Bb","Cb","C","C","C#","D","Db","D","D#","C","Db","D",
                     "D","D#","E","Eb","E","F","D","Eb","E", "E","E#","F#", "E","F","F#",
                     "Eb","Fb","F","F","F#","G","Gb","G","G#","F","Gb","G", "G","G#","A", 
                     "Ab","A","A#","G","Ab","A","A","A#","B","Bb","B","C","A","Bb","B", 
                     "B","B#","C#"];
       var subst = chord.match(/[^b#][#b]?/g);
       for(var ax in subst){
          if(scale.indexOf(subst[ax])!==-1){
             if(amount>0){
                for(ix=0;ixamount;ix--){
                    var pos = scale.indexOf(subst[ax]);
                    var transpos = 3*pos-2+3;
                    subst[ax] = transp[transpos-1];
                    }
                }
           } 
       }
       chord=subst.join("");
    }
    

    chord = C/B, amount = 1: C#/C or chord = Gm7, amount 2: Am7

提交回复
热议问题