Created
November 15, 2019 16:59
-
-
Save ayaysir/411afca4581eb066c5d5a7a94b76903c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NAME_COMMON = ["", "C", "C+", "D", "E-", "E", "F", "F+", "G", "A-", "A", "B-", "B"]; | |
// C2 ^C2 _E2 =E2 ^F2 G2 A2 _B2 c2| | |
// ^: sharp | |
// =: natural | |
// =: flat | |
function translateToAbcjs(scaleBySemitone){ | |
console.log(scaleBySemitone) | |
const outArr = [] | |
const changedNotes = [] | |
for(let each of scaleBySemitone){ | |
console.log(each) | |
const nameIndex = each > 12 ? each - 12 : each | |
const octave = each > 12 ? 1 : 0 | |
console.log("octave", octave) | |
const note = (_ => { | |
if(octave == 0){ | |
return NAME_COMMON[nameIndex].substr(0, 1) | |
} else if(octave == 1){ | |
return NAME_COMMON[nameIndex].substr(0, 1).toLowerCase() | |
} | |
})() | |
const postfix = NAME_COMMON[nameIndex].substr(NAME_COMMON[nameIndex].length-1, 1) | |
console.log(note, postfix) | |
if(note.toUpperCase() != postfix){ | |
const prefix = postfix == "+" ? "^" : "_" | |
changedNotes.push(note) | |
outArr.push(prefix + note + "2") | |
} else { | |
if( changedNotes.indexOf(note) != -1 ){ | |
outArr.push("=" + note + "2") | |
} else { | |
outArr.push(note + "2") | |
} | |
} | |
} | |
console.log(outArr) | |
return outArr.join(" ") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment