Last active
August 29, 2015 14:03
-
-
Save allenjprice/0607f21daf3b11d1e9a7 to your computer and use it in GitHub Desktop.
Nashy Transposition, Take 2
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
var pitches = {}; | |
//pitch dictionary via numerical index. not zero based. I might regret that. | |
pitches['_1'] = ['C', 'B#']; | |
pitches['_2'] = ['C#', 'Db']; | |
pitches['_3'] = ['D', 'C*']; | |
pitches['_4'] = ['D#', 'Eb']; | |
pitches['_5'] = ['E', 'Fb']; | |
pitches['_6'] = ['F', 'E#']; | |
pitches['_7'] = ['F#', 'Gb']; | |
pitches['_8'] = ['G', 'F*']; | |
pitches['_9'] = ['G#', 'Ab']; | |
pitches['_10'] = ['A', 'G*']; | |
pitches['_11'] = ['A#', 'Bb']; | |
pitches['_12'] = ['B', 'Cb']; | |
function processText(text, original, destination){ | |
//search through the pitches object for the given chord, return its "index" in the chord dictionary | |
function findChordByName(chord){ | |
var counter = 1; | |
for (counter; counter <= 12; counter++){ | |
for(var i=0; i<pitches['_' + counter].length; i++){ | |
if (chord === pitches['_' + counter][i]){ | |
return counter; | |
} | |
} | |
} | |
} | |
//given a chord name, transpose it from original key to destination key. | |
function transpose(chord){ | |
var chordIndex = findChordByName(chord); | |
var interval = original - destination; | |
if (interval > 0) | |
interval -= 12; | |
var invertedInterval = interval + 12; | |
var result = pitches['_' + (chordIndex - interval)]; | |
if (!result){ | |
result = pitches['_' + (chordIndex - invertedInterval)]; | |
} | |
return result[0]; | |
} | |
function processChord(chord){ | |
var processed = ''; | |
if(chord[1] === '#' || chord[1] === 'b'){ | |
processed += transpose(chord.slice(0,2)); | |
} | |
else | |
processed += transpose(chord[0]); | |
var chordEnd = processed.length; | |
if (processed[chordEnd-1] === '#' || processed[chordEnd-1] === 'b'){ | |
chordEnd--; | |
} | |
for(var i=chordEnd; i<chord.length; i++){ | |
switch(chord[i]){ | |
case '/': | |
processed += '/' + transpose(chord.slice(i+1)); | |
i++; | |
break; | |
case '#': | |
//processed += ''; | |
break; | |
case 'b': | |
//processed += ''; | |
break; | |
default: | |
processed += chord[i]; | |
} | |
} | |
return processed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment