Skip to content

Instantly share code, notes, and snippets.

@allenjprice
Created July 2, 2014 15:06
Show Gist options
  • Save allenjprice/b350225ab3e698250e66 to your computer and use it in GitHub Desktop.
Save allenjprice/b350225ab3e698250e66 to your computer and use it in GitHub Desktop.
Nashy Transposition, Take 3
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'];
// pitches by name [0 index group]
PITCHES['c'] = PITCHES['_1'][0];
PITCHES['cSharp'] = PITCHES._2[0];
PITCHES['d'] = PITCHES._3[0];
PITCHES['dSharp'] = PITCHES._4[0];
PITCHES['e'] = PITCHES._5[0];
PITCHES['f'] = PITCHES._6[0];
PITCHES['fSharp'] = PITCHES._7[0];
PITCHES['g'] = PITCHES._8[0];
PITCHES['gSharp'] = PITCHES._9[0];
PITCHES['a'] = PITCHES._10[0];
PITCHES['aSharp'] = PITCHES._11[0];
PITCHES['b'] = PITCHES._12[0];
// pitches by name [1 index group]
PITCHES['bSharp'] = PITCHES._1[1];
PITCHES['dFlat'] = PITCHES._2[1];
PITCHES['cDoubleSharp'] = PITCHES._3[1];
PITCHES['eFlat'] = PITCHES._4[1];
PITCHES['fFlat'] = PITCHES._5[1];
PITCHES['eSharp'] = PITCHES._6[1];
PITCHES['gFlat'] = PITCHES._7[1];
PITCHES['fDoubleSharp'] = PITCHES._8[1];
PITCHES['aFlat'] = PITCHES._9[1];
PITCHES['gDoubleSharp'] = PITCHES._10[1];
PITCHES['bFlat'] = PITCHES._11[1];
PITCHES['cFlat'] = PITCHES._12[1];
//SCALES: Dictionary containing the diatonic pitches for each key.
var SCALES = {
c: {},
cSharp: {},
dFlat: {},
d: {},
dSharp: {},
eFlat: {},
e: {},
f: {},
fSharp: {},
gFlat: {},
g: {},
gSharp: {},
aFlat: {},
a: {},
aSharp: {},
bFlat: {},
b: {}
};
SCALES['c']['_1'] = PITCHES['c'];
SCALES['c']['_2'] = PITCHES['cSharp'];
SCALES['c']['_3'] = PITCHES['d'];
SCALES['c']['_4'] = PITCHES['eFlat'];
SCALES['c']['_5'] = PITCHES['e'];
SCALES['c']['_6'] = PITCHES['f'];
SCALES['c']['_7'] = PITCHES['fSharp'];
SCALES['c']['_8'] = PITCHES['g'];
SCALES['c']['_9'] = PITCHES['aFlat'];
SCALES['c']['_10'] = PITCHES['a'];
SCALES['c']['_11'] = PITCHES['bFlat'];
SCALES['c']['_12'] = PITCHES['b'];
SCALES['cSharp']['_1'] = PITCHES['bSharp'];
SCALES['cSharp']['_2'] = PITCHES['cSharp'];
SCALES['cSharp']['_3'] = PITCHES['cDoubleSharp'];
SCALES['cSharp']['_4'] = PITCHES['dSharp'];
SCALES['cSharp']['_5'] = PITCHES['e'];
SCALES['cSharp']['_6'] = PITCHES['eSharp'];
SCALES['cSharp']['_7'] = PITCHES['fSharp'];
SCALES['cSharp']['_8'] = PITCHES['g'];
SCALES['cSharp']['_9'] = PITCHES['gSharp'];
SCALES['cSharp']['_10'] = PITCHES['a'];
SCALES['cSharp']['_11'] = PITCHES['aSharp'];
SCALES['cSharp']['_12'] = PITCHES['b'];
//...and so on, through all the scales.
function processText(text, originalKeyX, destinationKeyX){
function findIndexX(key){
var keyIndex;
switch (key){
case 'c':
keyIndex = 1;
break;
case 'cSharp':
keyIndex = 2;
break;
case 'dFlat':
keyIndex = 2;
break;
case 'd':
keyIndex = 3;
break;
case 'dSharp':
keyIndex = 4;
break;
case 'eFlat':
keyIndex = 4;
break;
case 'e':
keyIndex = 5;
break;
case 'f':
keyIndex = 6;
break;
case 'fSharp':
keyIndex = 7;
break;
case 'gFlat':
keyIndex = 7;
break;
case 'g':
keyIndex = 8;
break;
case 'gSharp':
keyIndex = 9;
break;
case 'aFlat':
keyIndex = 9;
break;
case 'a':
keyIndex = 10;
break;
case 'aSharp':
keyIndex = 11;
break;
case 'bFlat':
keyIndex = 11;
break;
case 'b':
keyIndex = 12;
break;
}
return keyIndex;
}
//search through the PITCHES object for the given chord, return its index in the PITCHES 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;
}
}
}
}
function transposeX(chord){
var interval = findIndexX(originalKeyX) - findIndexX(destinationKeyX);
if (interval>0)
interval -= 12;
var invertedInterval = interval + 12;
// var chordIndexX = findChordByName(chord);
var difference = findChordByName(chord) - interval;
var differenceInverted = findChordByName(chord) - invertedInterval;
var result = SCALES[destinationKeyX]['_' + difference];
if (!result)
result = SCALES[destinationKeyX]['_' + differenceInverted];
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment