// Chord Test 4 by John Clavin -- August 14, 2016 // Chord Test 4 Fix by John Clavin // August 16, 2017 var numOfOsc; var beatCount, chordCount, chordNumber; var fadeFlag, hiChordsFlag; var fadeCount; var jcOscs = []; var jcBass; var noteArray = [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72]; function setup() { createCanvas(800, 400); background(180); frameRate(30); noStroke(); masterVolume(0.30); numOfOsc = 4; fadeFlag = false; hiChordsFlag = 0; fadeCount = 0; chordCount = 0; chordNumber = floor(random(5, 11)); beatCount = 1; for (var i = 0; i < numOfOsc; i++) { jcOscs[i] = new jcOsc(); } jcBass = new jcOscBass(); } function draw() { if (frameCount % 30 == 15) { if(beatCount === 0) { createChord(); chordCount++; if(chordCount >= chordNumber) { beatCount = 8; chordCount = 0; chordNumber = floor(random(5, 11)); fadeFlag = true; if(random(100) > 70) { hiChordsFlag = 1; } else { hiChordsFlag = 0; } } else if(random(100) > 94) { beatCount = 0; } else { beatCount = 1; } } else { beatCount--; } } if(fadeFlag) { fadeCount++; if(fadeCount > 60 && fadeCount < 130 ) { background(180, 12); } if(fadeCount > 128 && fadeCount < 132 ) { background(180); } if(fadeCount > 160 && fadeCount < 220) { fill(130); rect(width/2 - width/4, height/6, width/2, 30); } if(fadeCount > 218 && fadeCount < 222 ) { background(180); fadeFlag = false; fadeCount = 0; } } } function jcOsc() { this.osc = new p5.SinOsc(); this.envlope = new p5.Env(); this.envlope.setADSR(0.03, 0.26, 0.26, 3.8); this.envlope.setRange(0.40, 0); this.osc.amp(this.envlope); this.osc.start(); this.jamOn = function(xnote) { this.note = xnote; this.freqValue = midiToFreq(this.note); this.osc.freq(this.freqValue); this.envlope.play(); } this.display = function(noteDisplayIndex) { this.note = noteDisplayIndex; fill(240, 0, 0); this.x = map(this.note, 1, 21, 0, width); rect(this.x, 150, 10, 250); } } function jcOscBass() { this.osc1 = new p5.TriOsc(); this.osc2 = new p5.TriOsc(); this.envlope1 = new p5.Env(); this.envlope2 = new p5.Env(); this.envlope1.setADSR(0.03, 0.26, 0.20, 2.4); this.envlope2.setADSR(0.03, 0.26, 0.20, 2.4); this.envlope1.setRange(0.40, 0); this.envlope2.setRange(0.40, 0); this.osc1.amp(this.envlope1); this.osc2.amp(this.envlope2); // this.osc1.start(); // this.osc2.start(); this.jamOnBass = function(xnote) { this.note = xnote - 12; this.freqValue = midiToFreq(this.note); this.osc1.freq(this.freqValue); this.note = xnote - 24; this.freqValue = midiToFreq(this.note); this.osc2.freq(this.freqValue); this.envlope1.play(); this.envlope2.play(); } } function createChord() { background(180); if(hiChordsFlag == 1) { var noteIndex = floor(random(17, 21)); var midiValue = noteArray[noteIndex]; jcOscs[0].jamOn(midiValue); jcOscs[0].display(noteIndex); noteIndex = noteIndex - floor(random(3, 6)); midiValue = noteArray[noteIndex]; jcOscs[1].jamOn(midiValue); jcOscs[1].display(noteIndex); noteIndex = noteIndex - floor(random(1, 4)); midiValue = noteArray[noteIndex]; jcOscs[2].jamOn(midiValue); jcOscs[2].display(noteIndex); noteIndex = noteIndex - floor(random(3, 6)); midiValue = noteArray[noteIndex]; // jcBass.jamOnBass(midiValue); jcOscs[3].jamOn(midiValue); jcOscs[3].display(noteIndex); } else { noteIndex = floor(random(15, 19)); midiValue = noteArray[noteIndex]; jcOscs[0].jamOn(midiValue); jcOscs[0].display(noteIndex); noteIndex = noteIndex - floor(random(3, 6)); midiValue = noteArray[noteIndex]; jcOscs[1].jamOn(midiValue); jcOscs[1].display(noteIndex); noteIndex = noteIndex - floor(random(1, 4)); midiValue = noteArray[noteIndex]; jcOscs[2].jamOn(midiValue); jcOscs[2].display(noteIndex); noteIndex = noteIndex - floor(random(3, 6)); midiValue = noteArray[noteIndex]; // jcBass.jamOnBass(midiValue); jcOscs[3].jamOn(midiValue); jcOscs[3].display(noteIndex); } }