// Chord Test 3 by John Clavin -- Sept 2015 var envelope, numOfOsc; var beatCount, chordCount, chordNumber; var fadeFlag, fadeCount; var jcOscs = []; 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() { var ctCanvas = createCanvas(800, 400); ctCanvas.parent('ctContainer'); frameRate(30); background(180); noStroke(); numOfOsc = 4; fadeFlag = false; fadeCount = 0; chordCount = 0; chordNumber = round(random(5, 13)); beatCount = round(random(0, 1)); for (var i = 0; i < numOfOsc; i++) { jcOscs[i] = new jcOsc(); } } function draw() { if (frameCount % 30 == 15) { if(beatCount == 0) { createChord(round(random(1, 4))); chordCount++; if(chordCount >= chordNumber) { beatCount = 6; chordCount = 0; chordNumber = round(random(5, 13)); fadeFlag = true; } else if(random(100) > 50) { beatCount = 0; } else { beatCount = 1; } } else { beatCount--; } } if(fadeFlag) { fadeCount++; if(fadeCount > 60 && fadeCount < 130 ) { background(180, 12); } else if(fadeCount > 125) { background(180); fadeFlag = false; fadeCount = 0; } } } function jcOsc() { this.osc = new p5.Oscillator(); this.osc.setType( 'sine' ); this.osc.amp(0.0); this.envelope = new p5.Env(0.015, 0.16, 0.20, 0.12, 3.6, 0.00); this.osc.start(); } jcOsc.prototype.jamOn = function(xnote) { this.note = xnote; this.freqValue = midiToFreq(this.note); this.osc.freq(this.freqValue); this.envelope.play(this.osc); } jcOsc.prototype.display = function(noteDisplayIndex) { this.note = noteDisplayIndex; fill(240, 0, 0); this.x = map(this.note, 0, 23, 0, width); rect(this.x, 150, 10, 250); } function createChord(noteIndex) { background(180); var noteIndex; var midiValue = noteArray[noteIndex]; jcOscs[0].jamOn(midiValue); jcOscs[0].display(noteIndex); noteIndex = noteIndex + round(random(3, 6)); midiValue = noteArray[noteIndex]; jcOscs[1].jamOn(midiValue); jcOscs[1].display(noteIndex); noteIndex = noteIndex + round(random(1, 6)); var midiValue = noteArray[noteIndex]; jcOscs[2].jamOn(midiValue); jcOscs[2].display(noteIndex); noteIndex = noteIndex + round(random(1, 6)); midiValue = noteArray[noteIndex]; jcOscs[3].jamOn(midiValue); jcOscs[3].display(noteIndex); }