Skip to content

Instantly share code, notes, and snippets.

@munckymagik
Created January 26, 2020 09:48
Show Gist options
  • Save munckymagik/9b00461fcc99423f58180e800a200731 to your computer and use it in GitHub Desktop.
Save munckymagik/9b00461fcc99423f58180e800a200731 to your computer and use it in GitHub Desktop.
// run with node --harmony
(() => {
'use strict'
function zip(... arrays) {
return arrays[0].map(function(_,i){
return arrays.map(function(array){return array[i]})
});
}
Array.prototype.flatMap = function(lambda) {
return Array.prototype.concat.apply([], this.map(lambda));
};
class Scale {
constructor(name, degrees) {
this.name = name;
this.degrees = degrees;
}
count_identical_degrees(other) {
return zip(this.degrees, other.degrees)
.filter((x) => x[0] === x[1])
.length
};
score_differences(other) {
return zip(this.degrees, other.degrees)
.reduce((acc, x) => {
return acc - Math.abs(x[0] - x[1])
}, 0)
}
}
let majorModes = [
new Scale('Ionian', [0, 0, 0, 0, 0, 0, 0]),
new Scale('Dorian', [0, 0, -1, 0, 0, 0, -1]),
new Scale('Phrygian', [0, -1, -1, 0, 0, -1, -1]),
new Scale('Lydian', [0, 0, 0, 1, 0, 0, 0]),
new Scale('Mixolydian', [0, 0, 0, 0, 0, 0, -1]),
new Scale('Aeolian', [0, 0, -1, 0, 0, -1, -1]),
new Scale('Locrian', [0, -1, -1, 0, -1, -1, -1]),
]
let harmonicMinorModes = [
new Scale('Harmonic Minor', [0, 0, -1, 0, 0, 0, 0]),
new Scale('Dorian ♭2', [0, -1, -1, 0, 0, 0, -1]),
new Scale('Lydian Augmented', [0, 0, 0, 1, 1, 0, 0]),
new Scale('Lydian Dominant', [0, 0, 0, 1, 0, 0, -1]),
new Scale('Mixolydian ♭6', [0, 0, 0, 0, 0, -1, -1]),
new Scale('Locrian ♮2', [0, 0, -1, 0, -1, -1, -1]),
new Scale('Altered Scale', [0, -1, -1, -1, -1, -1, -1]),
]
// console.log(majorModes)
function calcSimilarity(scales) {
return scales.flatMap((scale) => {
return scales
.filter((other) => scale !== other)
.map((other) => {
let c = scale.count_identical_degrees(other)
let d = scale.score_differences(other)
return [[scale.name, other.name], c + d]
})
.sort((a, b) => a[1] - b[1])
.reverse()
}).filter((a) => a[1] >= 1)
}
// console.log(calcSimilarity(majorModes))
console.log(calcSimilarity(harmonicMinorModes))
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment