Random Notes

by Eve Weinberg

HTML

<script src="https://tonejs.github.io/CDN/r6/Tone.min.js"></script>

JavaScript

var synth = new Tone.SimpleSynth({
	oscillator : {
  	type : "pwm"
    //or try "triangle" or "square6"
  },envelope : {
  	attack : 0.02,
    decay : 0.1,
    sustain : 0
  }}).toMaster();

var melodyNotes = ["Eb1", "D2", "B2", "C3", "E3", "F#3", "A3", "B4", "G4", "A5"];
var restInterval = 10;

for (var i = 0; i < 20; i++){
//give it some rest
if (i% restInterval !=0){
	synth.triggerAttack(randomMelodyChoice(), i * 0.4);
  }
  
}

function randomMelodyChoice(){
	var index = Math.floor(Math.random() * melodyNotes.length);
  return melodyNotes[index];
}