xxxxxxxxxx
// remember to add the sound library : p5.sound
var osc1, osc2; // this is a variable which we will use for an oscillator
var env1, env2; // this is a variable which we wlll use for the envelope
// MIDI notes are by semitone
// 60 is middle C, 61 would be the C# above middle C, 59 would be the B below middle C, etc.
// octaves move by 12. So 72 is the C above middle C.
var sequence = [60, 63, 65, 62, 60, 67, 68, 70, 72, 60, 58, 62, 74, 63]; // MIDI
var s1 = 0;
var s2 = 0;
var speed1 = 6;
var speed2 = 9;
var fg = 255;
function setup() {
createCanvas(windowWidth, windowHeight);
background(255);
// take 0.1 seconds to get to level 0.3, then 0.5 seconds to get to level 0
env1 = new p5.Envelope(0.01, 0.3, 0.1, 0);
env2 = new p5.Envelope(0.01, 0.3, 0.1, 0);
osc1 = new p5.Oscillator(); // make an oscillator
osc1.setType('sine'); // options are 'sine', 'sawtooth', 'triangle', 'square'
osc1.start(); // start it
osc1.amp(0);
osc2 = new p5.Oscillator(); // make an oscillator
osc2.setType('sine'); // options are 'sine', 'sawtooth', 'triangle', 'square'
osc2.start(); // start it
osc2.amp(0);
textSize(60);
}
function draw() {
background(255-fg);
stroke(fg);
fill(fg);
text(frameCount + ": " + s1 + ": " + sequence[s1], width/2, height/2);
text(frameCount + ": " + s2 + ": " + sequence[s2], width/2, height/2 + 60);
if(frameCount%speed1==0) {
playit1(sequence[s1]); // play a note
s1 = (s1+1) % sequence.length; // increment which note
}
if(frameCount%speed2==0) {
playit2(sequence[s2]); // play a note
s2 = (s2+1) % sequence.length; // increment which note
}
}
function mouseClicked()
{
//playit();
}
function playit1(f)
{
osc1.freq(midiToFreq(f)); // frequency to the argument
env1.play(osc1);
}
function playit2(f)
{
osc2.freq(midiToFreq(f)); // frequency to the argument
env2.play(osc2);
}