Advertisement
homeworkhelp111

Major Scale

Oct 22nd, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function note_number_to_name(number) {
  2.     var pitch_class = ['c','d','e','f','g','a','b'];
  3.     var pitch_d = [0,2,4,5,7,9,11];
  4.     var str = ""
  5.    
  6.     var octave = Math.trunc((number/12) - 1);
  7.    
  8.     var pitch = number % 12;
  9.  
  10.     var index = -1;
  11.     for(var i = 0; i< pitch_class.length;i++) {
  12.         if(pitch_d== pitch) {
  13.             index = i;
  14.             break;
  15.         }
  16.     }
  17.  
  18.     if(index != -1) {//It means there is no sharp #
  19.         str = str + pitch_class[index];
  20.         str = str + octave;
  21.     } else{
  22.         pitch = (number - 1) % 12;
  23.         for(var i = 0; i< pitch_class.length;i++) {
  24.             if(pitch_d== pitch) {
  25.                 index = i;
  26.                 break;
  27.             }
  28.         }
  29.         str = str + pitch_class[index];
  30.         str = str + '#';
  31.         str = str + octave;
  32.     }
  33.  
  34.     str = str.toUpperCase();
  35.     return str;
  36. }
  37.  
  38. function note_name_to_number(note) {
  39.     note = note.toLowerCase();
  40.     var pitch_class = ['c','d','e','f','g','a','b'];
  41.     var pitch_d = [0,2,4,5,7,9,11];
  42.  
  43.     var octave = parseInt(note[note.length-1]);
  44.  
  45.     var pitch_index = 0;
  46.     for(pitch_index = 0; pitch_index< pitch_class.length; pitch_index++) {
  47.         if(pitch_class[pitch_index] == note[0]) {
  48.             break;
  49.         }
  50.     }
  51.  
  52.     var acc_index = 0;
  53.     note = note.toUpperCase();
  54.     if(note.length == 3) {
  55.         return((octave+1)*12+pitch_d[pitch_index]+1)
  56.     } else {
  57.         return((octave+1)*12+pitch_d[pitch_index])
  58.     }
  59. }
  60.  
  61. function major_scale(note){
  62.     note_number = note_name_to_number(note);
  63.     var add_note = [0,2,4,5,7,9,11,12];
  64.     var major = [];
  65.     for(var i=0; i<add_note.length;i++) {
  66.         major = note_number_to_name(note_number+add_note);
  67.     }
  68.     return major;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement