|
|
var sharpChords = ['C', 'C♯', 'D', "D♯", 'E', 'F', "F♯", 'G', 'G♯', 'A', 'A♯', 'B']; |
|
|
var flatChords = ['C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭', 'A', 'B♭', 'B']; |
|
|
var sharpChordsFree = ['C', 'C#', 'D', "D#", 'E', 'F', "F#", 'G', 'G#', 'A', 'A#', 'B']; |
|
|
var flatChordsFree = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']; |
|
|
|
|
|
function transposeChordName(chord, steps, preferFlats, freeGCchord) { |
|
|
if (!steps || (steps % 12 === 0)) |
|
|
return chord; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (steps < 0) steps += 12; |
|
|
if (steps > 11) steps = steps % 12; |
|
|
|
|
|
|
|
|
var match = chord.match(/^([A-G][b#♭♯]?)([^\/]+)?\/?([A-G][b#♭♯]?)?(.+)?/) |
|
|
if (!match) |
|
|
return chord; |
|
|
var name = match[1] |
|
|
var extra1 = match[2] |
|
|
var bass = match[3] |
|
|
var extra2 = match[4] |
|
|
var index = sharpChords.indexOf(name) |
|
|
if (index < 0) |
|
|
index = flatChords.indexOf(name) |
|
|
if (index < 0) |
|
|
index = sharpChordsFree.indexOf(name) |
|
|
if (index < 0) |
|
|
index = flatChordsFree.indexOf(name) |
|
|
if (index < 0) |
|
|
return chord; |
|
|
|
|
|
index += steps |
|
|
index = index % 12 |
|
|
|
|
|
if (preferFlats) { |
|
|
if (freeGCchord) chord = flatChordsFree[index] |
|
|
else chord = flatChords[index] |
|
|
} else { |
|
|
if (freeGCchord) chord = sharpChordsFree[index] |
|
|
else chord = sharpChords[index] |
|
|
} |
|
|
|
|
|
if (extra1) |
|
|
chord += extra1 |
|
|
|
|
|
if (bass) { |
|
|
var index = sharpChords.indexOf(bass) |
|
|
if (index < 0) |
|
|
index = flatChords.indexOf(bass) |
|
|
if (index < 0) |
|
|
index = sharpChordsFree.indexOf(bass) |
|
|
if (index < 0) |
|
|
index = flatChordsFree.indexOf(bass) |
|
|
chord += '/' |
|
|
if (index >= 0) { |
|
|
index += steps |
|
|
index = index % 12 |
|
|
if (preferFlats) { |
|
|
if (freeGCchord) chord += flatChordsFree[index] |
|
|
else chord += flatChords[index] |
|
|
} else { |
|
|
if (freeGCchord) chord += sharpChordsFree[index] |
|
|
else chord += sharpChords[index] |
|
|
} |
|
|
} else |
|
|
chord += bass; |
|
|
} |
|
|
|
|
|
if (extra2) |
|
|
chord += extra2 |
|
|
|
|
|
return chord; |
|
|
} |
|
|
|
|
|
module.exports = transposeChordName |