Last active
August 29, 2015 14:05
-
-
Save grifdail/32b158025ea66e063e4b to your computer and use it in GitHub Desktop.
Markov music improved
This file contains 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
var transpose = 24; | |
var bpm = 240; // Beats per minute | |
var spb = 60/bpm; // Second per beat | |
function note(n, octave){ | |
n += transpose; | |
return Math.pow(2, (n - 33 + (12 * (octave || 0))) / 12) * 440; | |
} | |
var melodies = [1]; | |
var counter; | |
//C Major random | |
var MajorRandom = [ | |
[1,0,1,0,1,1,0,1,0,1,0,1], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[1,0,1,0,1,1,0,1,0,1,0,1], | |
[0,0,0,0,1,0,0,0,0,0,0,0], | |
[1,0,1,0,1,1,0,1,0,1,0,1], | |
[1,0,1,0,1,1,0,1,0,1,0,1], | |
[0,0,0,0,0,0,0,1,0,0,0,0], | |
[1,0,1,0,1,1,0,1,0,1,0,1], | |
[0,0,0,0,0,0,0,0,0,1,0,0], | |
[1,0,1,0,1,1,0,1,0,1,0,1], | |
[0,0,0,0,0,0,0,0,0,0,0,1], | |
[1,0,1,0,1,1,0,1,0,1,0,1], | |
] | |
//C Major chromatic | |
var MajorChromatic = [ | |
[1,0,1,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[1,0,1,0,1,0,0,0,0,0,0,0], | |
[0,0,0,0,1,0,0,0,0,0,0,0], | |
[0,0,1,0,1,1,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,1,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,1,0,1,0,1,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,1,0,1,0,1], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,1,0,1], | |
] | |
var MajorChord = [ | |
[0,0,0,0,1,1,0,1,0,1,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,1,0,0,0,1], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,1,0,0], | |
[1,0,1,0,0,0,0,1,0,0,0,1], | |
[0,0,0,0,0,0,0,0,0,0,0,0], | |
[1,0,0,0,0,0,0,0,0,1,0,0], | |
[0,0,0,0,0,0,0,0,0,1,0,0], | |
[0,0,1,0,0,1,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,1], | |
[1,0,0,0,0,0,0,1,0,0,0,0], | |
] | |
function calcMarkovChain(chain,models,length) { | |
function calcNext(table) { | |
var current = 0; | |
var proba = []; | |
for (var i = 0; i < table.length; i++) { | |
current+=table[i]; | |
proba[i] = current; | |
} | |
var r = Math.random(); | |
for (i = 0; i < proba.length; i++) { | |
proba[i]*= 1/current; | |
if (r<proba[i]) { | |
return i; | |
} | |
} | |
return 0 | |
} | |
chain.slice(0); | |
chain.push(0); | |
for (var i = 0; i < length; i++) { | |
var currentModel = models[chain[i] || 0]; | |
chain.push(calcNext(currentModel)) | |
} | |
return chain | |
} | |
function buildMelodie() { | |
var pattern = calcMarkovChain([] ,MajorChromatic,8); | |
melodies = []; | |
melodies.push.apply(melodies,pattern); | |
pattern[(Math.random()*8)|0] += 1; | |
pattern[(Math.random()*8)|0] += 1; | |
melodies.push.apply(melodies,pattern); | |
melodies.push.apply(melodies,melodies.map(function(x){return x+12})); | |
return melodies; | |
} | |
buildMelodie(); | |
var chord = calcMarkovChain([] ,MajorChord,8); | |
function sin(t,f,a) { | |
return a * Math.sin( 2 * Math.PI * t * f); | |
} | |
function sqr(t,f,a) { | |
return ((sin(t,f,a)>0) *2-1) *a; | |
} | |
function playChord(t,n,f,a,transpose) { | |
transpose = transpose || 0; | |
var scale = { | |
0: [0,4,7], | |
2: [2,5,9], | |
4: [4,7,11], | |
5: [5,9,12], | |
7: [7,11,14], | |
9: [9,12,16], | |
11: [11,14,17] | |
} | |
return (0 | |
+f(t,note(scale[n][0]+transpose),a) | |
+f(t,note(scale[n][1]+transpose),a) | |
+f(t,note(scale[n][2]+transpose),a) | |
) | |
} | |
function dsp(t) { | |
counter = Math.floor(t/spb); //How many beats have passed | |
var noteprogression = (t%spb)/spb | |
return (0 | |
+ sqr(t,note(melodies[counter%melodies.length]),0.2)*sin(t,note(melodies[counter%melodies.length])*2,1) | |
+ playChord(t,chord[((counter/4)|0)%chord.length],sin,0.1,-12) | |
//+ sin(t,note(chord[((counter/4)|0)%chord.length]-24),0.15) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment