Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Created March 18, 2015 19:17
Show Gist options
  • Save DanielFGray/c09f16d83c2bd9fab7a9 to your computer and use it in GitHub Desktop.
Save DanielFGray/c09f16d83c2bd9fab7a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var modes = ['Ionian', 'Dorian', 'Phrygian', 'Lydian', 'Mixolydian', 'Aeolian', 'Locrian'];
var steps = [2, 2, 1, 2, 2, 2, 1];
var notes = ['C', 'C#/Db', 'D', 'D#/Eb', 'E', 'F', 'F#/Gb', 'G', 'G#/Ab', 'A', 'A#/Bb', 'B'];
var noteRoots = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
function getNotes(scale, mode) {
if(mode === 'Major') {
mode = 'Ionian';
} else if(mode === 'Minor') {
mode = 'Aeolian';
}
var modeOffset = modes.indexOf(mode);
var currIndex = getValue(scale);
var currRootIndex = noteRoots.indexOf(scale.slice(0, 1));
var outList = [scale];
for(var i = 0; i < steps.length - 1; ++i) {
var toAdd = steps[(i + modeOffset) % steps.length];
currIndex = (parseInt(currIndex) + toAdd) % notes.length;
outList.push(getNextNote(currRootIndex, currIndex));
currRootIndex++;
currRootIndex %= noteRoots.length;
}
return outList.map(function(note){
return note.replace(/b/g, '♭').replace(/#/g, '♯');
});
}
function getValue(note) {
var value = 0;
for(var i in note) {
if(note[i] == '#') {
value++;
} else if(note[i] == 'b') {
value--;
}
}
return (value + notes.indexOf(note.slice(0,1))) % notes.length;
}
function getNextNote(rootIndex, value) {
var outNote = noteRoots[(rootIndex + 1) % noteRoots.length];
var genValue = getValue(outNote);
var possibleDiffs = [
(value - genValue),
(12 + value - genValue),
(-12 + value - genValue)
];
var minIndex = 0;
var diff = possibleDiffs[minIndex];
possibleDiffs.forEach(function(num, idx){
if(Math.abs(num) < Math.abs(possibleDiffs[minIndex])) {
minIndex = idx;
}
});
if(diff > 0) {
while(diff--) {
outNote += '#';
}
} else if(diff < 0) {
while(diff++) {
outNote += 'b';
}
}
return outNote;
}
modes.forEach(function(mode) {
console.log(process.argv[2] + ' ' + mode, getNotes(process.argv[2], mode));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment