xxxxxxxxxx
54
// Click on the canvas to create a random melody
// Uncomment below to play each example
var synth;
function setup() {
createCanvas(620, 200);
synth = new Tone.Synth().toMaster();
}
function draw() {
background(0);
}
function mousePressed(){
//play 440 Hz for 0.1 seconds
// synth.triggerAttackRelease(440, 0.1);
//play A4 (440 Hz, referred to by its note name)
//see frequencies of different notes here: http://www.phy.mtu.edu/~suits/notefreqs.html
// synth.triggerAttackRelease("A4", 0.1);
//play a random frequency
// var frequency = random(100, 10000);
// synth.triggerAttackRelease(frequency, 0.1);
//play a random frequency within an octave
// var frequency = random(440, 880);
// synth.triggerAttackRelease(frequency, 0.1);
//play a random frequency within an equal-tempered semitone
// var frequency = random(329.63, 349.23);
// synth.triggerAttackRelease(frequency, 0.1);
//play a random frequency within a 5-note scale that I made up
// var myScale = [200.32, 350.55, 480, 670, 800];
// var pos = int(random(0, myScale.length));
// var frequency = myScale[pos];
// synth.triggerAttackRelease(frequency, 0.1);
//play a random equal-tempered note between C3 and C5 (three octaves)
//https://en.wikipedia.org/wiki/Scientific_pitch_notation
//these are all within the C major scale, or the A minor scale
//notes range from C0 to B8 (C0, D0, E0, F0, G0, A0, B0; C1, D1, and so on)
var myScale = ["C3", "D3", "E3", "F3", "G3", "A3", "B3",
"C4", "D4", "E4", "F4", "G4", "A4", "B4",
"C5", "D5", "E5", "F5", "G5", "A5", "B5" ];
var pos = int(random(0, myScale.length));
var note = myScale[pos];
synth.triggerAttackRelease(note, 0.1);
}