Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save waltercruz/5b745ae8037adb1c8980 to your computer and use it in GitHub Desktop.
Save waltercruz/5b745ae8037adb1c8980 to your computer and use it in GitHub Desktop.
// Diatonic Chord Trigger
var notes = ["C", "C#/Db", "D", "D#/Eb", "E", "F", "F#/Gb", "G", "G#/Ab", "A", "A#/Bb", "B"];
var PluginParameters = [
{ name: "Parent Key", type: "menu", valueStrings:notes, defaultValue: 0 }
];
var debug = true;
function rootKey() {
return GetParameter("Parent Key");
}
function playChord(events) {
for (var i = 0; i < events.length; i += 1) {
if (debug) {
events[i].trace();
}
events[i].send();
}
}
function copyNote(event) {
var copy = new event.constructor();
copy.pitch = event.pitch;
copy.velocity = event.velocity;
return copy;
}
function noteAtSemitonesFrom(event, semitones) {
var note = new event.constructor();
note.pitch = event.pitch + semitones;
note.velocity = event.velocity;
return note;
}
function semitoneDifferenceFromRoot(pitch) {
var semitones = (pitch - rootKey()) % 12;
if (debug) {
Trace(pitch);
Trace(semitones);
}
return semitones;
}
function isMajorInterval(event) {
return [0, 5, 7].indexOf(semitoneDifferenceFromRoot(event.pitch)) >= 0;
}
function isMinorInterval(event) {
return [2, 3, 4, 9, 11].indexOf(semitoneDifferenceFromRoot(event.pitch)) >= 0;
}
function majorThirdIntervalFrom(event) {
return noteAtSemitonesFrom(event, 4);
}
function minorThirdIntervalFrom(event) {
return noteAtSemitonesFrom(event, 3);
}
function perfectFifthIntervalFrom(event) {
return noteAtSemitonesFrom(event, 7);
}
function diminishedFifthIntervalFrom(event) {
return noteAtSemitonesFrom(event, 6);
}
function octaveAbove(event) {
return noteAtSemitonesFrom(event, 12);
}
function majorChordWithRoot(event) {
return [
event,
majorThirdIntervalFrom(event),
perfectFifthIntervalFrom(event),
octaveAbove(event)
];
}
function minorChordWithRoot(event) {
var notes = [
event,
minorThirdIntervalFrom(event),
];
if (11 === semitoneDifferenceFromRoot(event.pitch)) {
notes.push(diminishedFifthIntervalFrom(event));
} else {
notes.push(perfectFifthIntervalFrom(event));
}
notes.push(octaveAbove(event));
return notes;
}
function chordForNote(event) {
if (isMajorInterval(event)) {
return majorChordWithRoot(event);
} else if (isMinorInterval(event)) {
return minorChordWithRoot(event);
} else {
return [event];
}
}
function HandleMIDI(event) {
playChord(chordForNote(event));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment