Search This Blog

Wednesday, June 26, 2013

Chord-changer Javascript

I have written a Javascript program to transpose the guitar chords for a song into a different key. In the lyrics of the song, the guitar chords are supposed to be between <sup> and </sup>. You can try it at Myanmar Lyrics by clicking the Key Up and Key Down buttons. The codes are shown below.
<script type="text/javascript">
//Author: YAN NAING AYE 
//WebSite: http://cool-emerald.blogspot.com/
//License: Chord-changer Javascript by Yan Naing Aye is licensed
//         under a Creative Commons Attribution 3.0 Unported License.
//         http://creativecommons.org/licenses/by/3.0/
//  You are free:
//    to Share — to copy, distribute and transmit the work
//    to Remix — to adapt the work
//    to make commercial use of the work
//  Under the following conditions:
//    Attribution — You must attribute Chord-changer Javascript  to Yan Naing Aye (with link).
//-----------------------------------------------------------------------------
function ChangeKey(d)
{
  var y = document.getElementsByTagName("sup");
  var kUp = 0;
  var keyA = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
  var ki=0;
  var t;
  for (i = 0; i < y.length; i++) 
  {      
      nCh = 1;
      var kUp = 0;
      t= y[i].innerHTML;
      if (t.indexOf('b') == 1) 
      {
          kUp=-1;
          nCh =2;          
      }
      else if (t.indexOf('#') == 1)
      {
          kUp=1;
          nCh =2;
      }
      ki=keyA.indexOf(t.charAt(0));
      if(ki!=-1)
      {
             ki+=kUp+d+12;
             ki=ki%12;
             y[i].innerHTML=keyA[ki].concat(t.substr(nCh));
      }        
  }
}
</script>
<input type="button" value="Key Up" onclick="ChangeKey(1)"/>
<input type="button" value="Key Down" onclick="ChangeKey(-1)"/>

No comments:

Post a Comment