// "Click On Jazz" by John Clavin -- Sept 2015 var envelope; var guit; var clear; var playJazz; var melody_1; var melody_2; var melodyIndex; var noteArray = [53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86]; function setup() { frameRate(30); var coj_canvas = createCanvas(800, 400); coj_canvas.parent('coj_container'); coj_canvas.mousePressed(mouseXPressed); background(240,238,220); clear = false; playJazz = false; melody_1 = new melody(10); melody_2 = new melody(22); guit = new jcOsc(); // text( "test 3", width - 100, 30); } function draw() { if(clear) { background(240,238,220); clear = false; } if (frameCount % 4 == 3) { if(playJazz) { if(mouseY >= height/2) { melodyIndex = melody_1.compose(); } else { melodyIndex = melody_2.compose(); } guit.jamOn(noteArray[melodyIndex]); guit.display(); } } } function mouseXPressed() { clear = true; playJazz = true; } function mouseReleased() { playJazz = false; return false; } function jcOsc() { this.osc1 = new p5.Oscillator(); this.osc2 = new p5.Oscillator(); this.osc1.setType( 'sine' ); this.osc2.setType( 'square' ); this.osc1.amp(0.0); this.osc2.amp(0.0); this.envelope1 = new p5.Env(0.008, 0.20, 0.10, 0.12, 0.80, 0.00); this.envelope2 = new p5.Env(0.008, 0.12, 0.10, 0.06, 0.80, 0.00); // this.reverb = new p5.Reverb(); this.osc1.start(); this.osc2.start(); } jcOsc.prototype.jamOn = function(xnote) { this.note = xnote; this.freqValue1 = midiToFreq(this.note); this.freqValue2 = midiToFreq(this.note - 12); this.osc1.freq(this.freqValue1); this.osc2.freq(this.freqValue2); // this.reverb.process(this.osc1, 2); this.envelope1.play(this.osc1); this.envelope2.play(this.osc2); } jcOsc.prototype.display = function() { this.dir = 180 + random(-26, 26); this.notePosX = mouseX + random(-32, 32); this.notePosY = mouseY + random(-32, 32); fill(0); push(); translate(this.notePosX, this.notePosY); rotate(radians(this.dir)); ellipse(0, 0, 12, 8); line(-6, 0, -6, 20); line(-6, 20, -14, 16); pop(); } function melody(noteCenterIndex) { this.noteCenter = noteCenterIndex; this.note2Play; this.johnClavin; this.previousNote = this.noteCenter + round(random(-10, 10)); } melody.prototype.compose = function() { if(this.previousNote < this.noteCenter) { this.note2Play = this.previousNote + round(random(1, 10)); } else { this.note2Play = this.previousNote - round(random(1, 10)); } this.previousNote = this.note2Play; this.johnClavin = 89200; return this.note2Play; }