A Pen by Jake Albaugh on CodePen.
Created
February 14, 2020 07:34
-
-
Save sbusso/a015baa676f3d1ab32f13c95b36ec366 to your computer and use it in GitHub Desktop.
Musical Arpeggio Pattern Generator
This file contains hidden or 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
/** | |
ArpeggioPatterns | |
returns arrays of arpeggio patterns for a given length of notes | |
@param steps {Integer} number of steps | |
@return {Object} | |
patterns: {Array} of arpeggiated index patterns | |
*/ | |
class ArpeggioPatterns { | |
constructor(params) { | |
this.steps = params.steps; | |
this._loadPatterns(); | |
this.updatePatterns = this.pubUpdatePatterns; | |
}; | |
pubUpdatePatterns(params) { | |
this.steps = params.steps; | |
this._loadPatterns(); | |
}; | |
_loadPatterns() { | |
this.arr = []; | |
this.patterns = []; | |
for(let i = 0; i < this.steps; i++) { this.arr.push(i); } | |
this._used = []; | |
this.permutations = this._permute(this.arr); | |
this.looped = this._loop(); | |
this.patterns = { | |
straight: this.permutations, | |
looped: this.looped | |
}; | |
}; | |
_permute(input, permutations) { | |
permutations = permutations || []; | |
var i, ch; | |
for (i = 0; i < input.length; i++) { | |
ch = input.splice(i, 1)[0]; | |
this._used.push(ch); | |
if (input.length === 0) { | |
permutations.push(this._used.slice()); | |
} | |
this._permute(input, permutations); | |
input.splice(i, 0, ch); | |
this._used.pop(); | |
} | |
return permutations; | |
}; | |
_loop() { | |
let looped = []; | |
for(let p = 0; p < this.permutations.length; p++) { | |
let perm = this.permutations[p]; | |
let arr = Array.from(perm); | |
for(let x = 1; x < perm.length - 1; x++) { | |
arr.push(perm[perm.length - 1 - x]); | |
} | |
looped.push(arr); | |
} | |
return looped; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment