Random Walk with Larger Steps
by Eve Weinberg
HTML
<script src="https://tonejs.github.io/CDN/r6/Tone.min.js"></script>
JavaScript
var synth = new Tone.SimpleSynth({
oscillator : {
type : "sawtooth8"
},
envelope : {
attack : 0.1,
decay : 0.3,
sustain : 0
}
}).toMaster();
var melodyNotes = ["Eb1", "D2", "B2", "C3", "E3", "F#3", "A3", "B4", "G4", "A5"];
var lastIndex = Math.floor(Math.random() * melodyNotes.length);
function nextStep(){
var maxJump = 2; // it can move a total of +/- 3 steps
lastIndex += Math.floor(Math.random() * maxJump * 2 - maxJump);
//make sure it's always a valid number
//wrap around
if (lastIndex < 0){
lastIndex = melodyNotes.length + lastIndex;
} else if (lastIndex >= melodyNotes.length){
lastIndex -= melodyNotes.length - 1;
}
}
for (var i = 0; i < 30; i++){
var note = melodyNotes[lastIndex];
synth.triggerAttack(note, i * 0.3);
nextStep();
}