// JC_Jazz_2 // Machine Jazz -- by John Clavin // September 10, 2017 var noteArray = [52, 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 ]; var dom13b9Template = [2, 2, 0, 2, 1, 0, 0, 2, 0, 1, 2, 0, 2, 2, 0, 2, 1, 0, 0, 2, 0, 1, 2, 0, 2, 2, 0, 2, 1, 0, 0, 2, 0, 1, 2, 0, 2, 2 ]; var dom7b5Template = [2, 0, 1, 0, 2, 0, 2, 0, 1, 0, 2, 0, 2, 0, 1, 0, 2, 0, 2, 0, 1, 0, 2, 0, 2, 0, 1, 0, 2, 0, 2, 0, 1, 0, 2, 0, 2, 0 ]; var tempKeyArray = [2, 0, 1, 2, 0, 1, 0, 2, 0, 1, 2, 0, 2, 0, 1, 2, 0, 1, 0, 2, 0, 1, 2, 0 ]; var chords = [ [53, 59, 64, 67], [54, 60, 65, 68], [55, 61, 66, 69], [56, 62, 64, 70], [57, 63, 65, 71], [58, 64, 66, 72] ]; var bass = [ [31, 38], [32, 39], [33, 40], [34, 28], [35, 29], [36, 30] ]; chordProg = []; var track_1 = []; var track_2 = []; var track_3 = []; var chords2Mea = []; var chords1Mea = []; var chords1MeaE = []; var melody2Mea_1 = []; var melody2Mea_2 = []; var bass2Mea_1 = []; var bass2Mea_2 = []; var bass2Mea_3 = []; var bass1Mea_1 = []; var rest2Mea = [91, 320]; var rest1Mea = [91, 160]; var endMusic = [89]; var oscVoices = []; var parserDirFlag = true; var parserIndex; var hiNotesFlag = false; var goMusic = true; var numOfChords = 8; var trk1_Play; var lastRand; var graphicsFlag = false; function setup() { createCanvas(600, 600); frameRate(60); rectMode(CENTER); noStroke(); background(238); masterVolume(0.35); parserIndex = floor(random(2, 20)); lastRand = floor(random(6)); for (var v = 0; v < 4; v++) { var voice = new oscChord(); oscVoices.push(voice); } soloVoice = new oscSolo(); bassVoice = new oscBass(); composeChords(); formatChords(); form1MeaChords(); formatChordEnd() bass2Mea_1 = composeBass(bass2Mea_1, 1); bass2Mea_2 = composeBass(bass2Mea_2, 2); bass2Mea_3 = composeBass(bass2Mea_3, 2); bass1Mea_1 = comp1MeaBass(bass1Mea_1); melody2Mea_1 = composeMelody(melody2Mea_1); melody2Mea_2 = composeMelody(melody2Mea_2); assembleTrk1(); assembleTrk2(); assembleTrk3(); trk1_Play = new trackManage(track_1); trk2_Play = new trackManage(track_2); trk3_Play = new trackManage(track_3); graphicsManage = new GraphicsManagement(); } function draw() { background(238); if (goMusic) { trk1_Play.playTrack(); graphicsFlag = trk2_Play.playTrackSolo(); trk3_Play.playTrackBass(); } graphicsManage.manage(graphicsFlag); } function composeChords() { for (var i = 0; i < numOfChords; i++) { chordProg[i] = rndNoRepeat(6); } } function formatChords() { var c2m_index = 0; var numbOfNotes = 3; for (var i = 0; i < 8; i++) { chords2Mea[c2m_index] = 90; c2m_index++; for (var j = 0; j < numbOfNotes; j++) { var chordsIndex = chordProg[i]; chords2Mea[c2m_index] = chords[chordsIndex][j]; c2m_index++; } chords2Mea[c2m_index] = 92; c2m_index++; chords2Mea[c2m_index] = 40; c2m_index++; } } function formatChordEnd() { var c2m_index = 0; var numbOfNotes = 3; chords1MeaE[c2m_index] = 90; c2m_index++; for (var j = 0; j < numbOfNotes; j++) { var chordsIndex = chordProg[0]; chords1MeaE[c2m_index] = chords[chordsIndex][j]; c2m_index++; } chords1MeaE[c2m_index] = 92; c2m_index++; chords1MeaE[c2m_index] = 160; c2m_index++; } function form1MeaChords() { var c1m_index = 0; var numbOfNotes = 4; for (var i = 0; i < 4; i++) { chords1Mea[c1m_index] = 90; c1m_index++; for (var j = 0; j < numbOfNotes; j++) { var chordsIndex = chordProg[i]; chords1Mea[c1m_index] = chords[chordsIndex][j]; c1m_index++; } chords1Mea[c1m_index] = 92; c1m_index++; chords1Mea[c1m_index] = 40; c1m_index++; } } function composeBass(destination, complexity) { var bass2Mea = destination; var b2m_index = 0; var numbOfNotes = 2; for (var i = 0; i < 8; i++) { if (complexity == 1) { for (var h = 0; h < numbOfNotes; h++) { bass2Mea[b2m_index] = 90; b2m_index++; var bassIndex = chordProg[i]; bass2Mea[b2m_index] = bass[bassIndex][1]; b2m_index++; bass2Mea[b2m_index] = 92; b2m_index++; bass2Mea[b2m_index] = 20; b2m_index++; } } else { if (random(100) < 4) { bass2Mea[b2m_index] = 90; b2m_index++; bassIndex = chordProg[i]; var root5th = floor(random(2)); bass2Mea[b2m_index] = bass[bassIndex][root5th]; b2m_index++; bass2Mea[b2m_index] = 92; b2m_index++; bass2Mea[b2m_index] = 40; b2m_index++; } else { numbOfNotes = 2; var root5thArray = []; bassIndex = chordProg[i]; if (random(100) < 36) { root5thArray[0] = bass[bassIndex][1]; root5thArray[1] = bass[bassIndex][0]; } else { root5thArray[0] = bass[bassIndex][0]; root5thArray[1] = bass[bassIndex][1]; } for (var j = 0; j < numbOfNotes; j++) { bass2Mea[b2m_index] = 90; b2m_index++; bass2Mea[b2m_index] = root5thArray[j]; b2m_index++; bass2Mea[b2m_index] = 92; b2m_index++; bass2Mea[b2m_index] = 20; b2m_index++; } } } } return bass2Mea; } function comp1MeaBass(destination) { var bass1Mea = destination; var b1m_index = 0; bass1Mea[b1m_index] = 90; b1m_index++; var bassIndex = chordProg[0]; bass1Mea[b1m_index] = bass[bassIndex][0]; b1m_index++; bass1Mea[b1m_index] = 92; b1m_index++; bass1Mea[b1m_index] = 160; b1m_index++; return bass1Mea; } function composeMelody(destination) { var melody2Mea = destination; var m2m_index = 0; var noteFoundFlag; for (var i = 0; i < 8; i++) { var chordNumber = chordProg[i]; var k; if (chordNumber === 0) { for (k = 0; k < tempKeyArray.length; k++) { tempKeyArray[k] = dom13b9Template[k]; } } else if (chordNumber == 1) { for (k = 0; k < tempKeyArray.length; k++) { tempKeyArray[k] = dom13b9Template[k + 11]; } } else if (chordNumber == 2) { for (k = 0; k < tempKeyArray.length; k++) { tempKeyArray[k] = dom13b9Template[k + 10]; } } else if (chordNumber == 3) { for (k = 0; k < tempKeyArray.length; k++) { tempKeyArray[k] = dom7b5Template[k]; } } else if (chordNumber == 4) { for (k = 0; k < tempKeyArray.length; k++) { tempKeyArray[k] = dom7b5Template[k + 11]; } } else if (chordNumber == 5) { for (k = 0; k < tempKeyArray.length; k++) { tempKeyArray[k] = dom7b5Template[k + 10]; } } if (random(100) < 25) { melody2Mea[m2m_index] = 90; m2m_index++; noteFoundFlag = false; var bailout = 0; while (noteFoundFlag === false) { if (tempKeyArray[parserIndex] > 1) { var note = noteArray[parserIndex]; melody2Mea[m2m_index] = note; noteFoundFlag = true; } parserMove(); bailout++; if (bailout > 75) { break; } } m2m_index++; melody2Mea[m2m_index] = 92; m2m_index++; melody2Mea[m2m_index] = 40; m2m_index++; } else { for (var j = 0; j < 4; j++) { melody2Mea[m2m_index] = 90; m2m_index++; noteFoundFlag = false; bailout = 0; while (noteFoundFlag === false) { if (tempKeyArray[parserIndex] > 0) { note = noteArray[parserIndex]; melody2Mea[m2m_index] = note; noteFoundFlag = true; } parserMove(); bailout++; if (bailout > 75) { break; } } if (random(100) < 40) { if (this.hiNotesFlag === true) { parserIndex = int(random(4, 11)); } else { parserIndex = int(random(13, 20)); } parserDirFlag = !parserDirFlag; } m2m_index++; melody2Mea[m2m_index] = 92; m2m_index++; melody2Mea[m2m_index] = 10; m2m_index++; } } } return melody2Mea; } function trackManage(track) { this.track = track; this.trkTime = 0; this.trkIndex = 0; this.playTrack = function() { if (this.track[this.trkIndex] == 89) { goMusic = false; } if (this.trkTime === 0) { if (this.track[this.trkIndex] == 90) { this.trkIndex++; var voiceNumber = 0; while (this.track[this.trkIndex] != 92) { var note = this.track[this.trkIndex]; oscVoices[voiceNumber].jamChord(note); this.trkIndex++; voiceNumber++; if (voiceNumber > 20) { break; } } this.trkIndex++; this.trkTime = this.track[this.trkIndex]; this.trkIndex++; } else { if (this.track[this.trkIndex] == 91) { this.trkIndex++; this.trkTime = this.track[this.trkIndex]; this.trkIndex++; } } } this.trkTime--; } this.playTrackSolo = function() { this.graphicsCue = false; if (this.trkTime === 0) { if (this.track[this.trkIndex] == 90) { this.trkIndex++; var note = this.track[this.trkIndex]; soloVoice.jamSolo(note); this.trkIndex++; this.trkIndex++; this.trkTime = this.track[this.trkIndex]; this.trkIndex++; } else { if (this.track[this.trkIndex] == 91) { this.trkIndex++; this.trkTime = this.track[this.trkIndex]; this.trkIndex++; } } this.graphicsCue = true; } this.trkTime--; return this.graphicsCue; } this.playTrackBass = function() { if (this.trkTime === 0) { if (this.track[this.trkIndex] == 90) { this.trkIndex++; var note = this.track[this.trkIndex]; bassVoice.jamBass(note); this.trkIndex++; this.trkIndex++; this.trkTime = this.track[this.trkIndex]; this.trkIndex++; } else { if (this.track[this.trkIndex] == 91) { this.trkIndex++; this.trkTime = this.track[this.trkIndex]; this.trkIndex++; } } } this.trkTime--; } } parserMove = function() { if (parserIndex >= 23) { parserIndex = parserIndex - 3; parserDirFlag = false; } if (parserIndex <= 0) { parserIndex = parserIndex + 2; parserDirFlag = true; } if (parserDirFlag === true) { parserIndex++; } else { parserIndex--; } if (parserIndex > 12) { hiNotesFlag = true; } else { hiNotesFlag = false; } } // Chords function assembleTrk1() { var trk_i = 0; var source_i; // Intro for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Melody 1 for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Interlude for (source_i = 0; source_i < chords1Mea.length; source_i++) { track_1[trk_i] = chords1Mea[source_i]; trk_i++; } // Melody 1 for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Interlude for (source_i = 0; source_i < chords1Mea.length; source_i++) { track_1[trk_i] = chords1Mea[source_i]; trk_i++; } // Melody 2 for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Interlude for (source_i = 0; source_i < chords1Mea.length; source_i++) { track_1[trk_i] = chords1Mea[source_i]; trk_i++; } // Melody 1 for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Interlude for (source_i = 0; source_i < chords1Mea.length; source_i++) { track_1[trk_i] = chords1Mea[source_i]; trk_i++; } // Bass Solo 1 for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Bass Solo 2 for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Chords alone for (source_i = 0; source_i < chords2Mea.length; source_i++) { track_1[trk_i] = chords2Mea[source_i]; trk_i++; } // Melody 1 Alone for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_1[trk_i] = rest2Mea[source_i]; trk_i++; } // End for (source_i = 0; source_i < chords1MeaE.length; source_i++) { track_1[trk_i] = chords1MeaE[source_i]; trk_i++; } // Stop Music Loop for (source_i = 0; source_i < endMusic.length; source_i++) { track_1[trk_i] = endMusic[source_i]; trk_i++; } } // melody function assembleTrk2() { var trk_i = 0; var source_i; for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_2[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < melody2Mea_1.length; source_i++) { track_2[trk_i] = melody2Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < rest1Mea.length; source_i++) { track_2[trk_i] = rest1Mea[source_i]; trk_i++; } for (source_i = 0; source_i < melody2Mea_1.length; source_i++) { track_2[trk_i] = melody2Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < rest1Mea.length; source_i++) { track_2[trk_i] = rest1Mea[source_i]; trk_i++; } for (source_i = 0; source_i < melody2Mea_2.length; source_i++) { track_2[trk_i] = melody2Mea_2[source_i]; trk_i++; } for (source_i = 0; source_i < rest1Mea.length; source_i++) { track_2[trk_i] = rest1Mea[source_i]; trk_i++; } for (source_i = 0; source_i < melody2Mea_1.length; source_i++) { track_2[trk_i] = melody2Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < rest1Mea.length; source_i++) { track_2[trk_i] = rest1Mea[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_2[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_2[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_2[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < melody2Mea_1.length; source_i++) { track_2[trk_i] = melody2Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < rest1Mea.length; source_i++) { track_2[trk_i] = rest1Mea[source_i]; trk_i++; } } // bass function assembleTrk3() { var trk_i = 0; var source_i; for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_3[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_3[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < bass1Mea_1.length; source_i++) { track_3[trk_i] = bass1Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_3[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < bass1Mea_1.length; source_i++) { track_3[trk_i] = bass1Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_3[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < bass1Mea_1.length; source_i++) { track_3[trk_i] = bass1Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_3[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < bass1Mea_1.length; source_i++) { track_3[trk_i] = bass1Mea_1[source_i]; trk_i++; } for (source_i = 0; source_i < bass2Mea_2.length; source_i++) { track_3[trk_i] = bass2Mea_2[source_i]; trk_i++; } for (source_i = 0; source_i < bass2Mea_3.length; source_i++) { track_3[trk_i] = bass2Mea_3[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_3[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < rest2Mea.length; source_i++) { track_3[trk_i] = rest2Mea[source_i]; trk_i++; } for (source_i = 0; source_i < bass1Mea_1.length; source_i++) { track_3[trk_i] = bass1Mea_1[source_i]; trk_i++; } } function GraphicsManagement() { this.balls = []; this.manage = function(cue) { this.cue = cue; if (this.cue == true) { var b = new Ball(random((width / 2 - 30), (width / 2 + 30)), height - 4); this.balls.push(b); } for (var j = 0; j < this.balls.length; j++) { this.topOut = false; this.balls[j].update(); this.balls[j].display(); this.balls[j].edges(); } } } function Ball(x, y) { this.ballColor = color(random(250), random(120), random(120)); noStroke(); this.pos = createVector(x, y); this.vel = createVector(random(-0.6, 0.6), random(-0.8, 0.0)); this.update = function() { this.pos.add(this.vel); } this.display = function() { fill(this.ballColor); ellipse(this.pos.x, this.pos.y, 12, 12); } this.edges = function() { this.topLimit = false; if (this.pos.y > height) { this.vel.y *= -1; this.pos.y = height; } if (this.pos.x > width) { this.vel.x *= -1; this.pos.x = width; } if (this.pos.y < 0) { this.vel.y *= -1 * 0.05; this.pos.y = 0; } if (this.pos.x < 0) { this.vel.x *= -1; this.pos.x = 0; } } } function oscChord() { this.osc = new p5.TriOsc(); this.oscEnv = new p5.Env(); this.oscEnv.setADSR(0.018, 0.24, 0.2, 0.8); this.oscEnv.setRange(0.30, 0); this.osc.amp(this.oscEnv); this.reverb = new p5.Reverb(); this.reverb.process(this.osc, 1.90, 0.0); this.osc.start(); this.jamChord = function(xnote) { this.note = xnote; this.oscFreq = midiToFreq(this.note); this.osc.freq(this.oscFreq); this.oscEnv.play(); } } function oscSolo() { this.osc1 = new p5.SawOsc(); this.osc2 = new p5.SqrOsc(); this.osc1Env = new p5.Env(); this.osc2Env = new p5.Env(); this.osc1Env.setADSR(0.004, 0.25, 0.20, 0.5); this.osc2Env.setADSR(0.020, 0.25, 0.20, 0.5); this.osc1Env.setRange(0.50, 0); // 60 this.osc2Env.setRange(0.30, 0); // 40 this.osc1.amp(this.osc1Env); this.osc2.amp(this.osc2Env); this.reverb1 = new p5.Reverb(); this.reverb2 = new p5.Reverb(); this.reverb1.process(this.osc1, 1.90, 0.0); this.reverb2.process(this.osc2, 1.90, 0.0); this.osc1.start(); this.osc2.start(); this.jamSolo = function(xnote) { this.note = xnote; this.osc1Freq = midiToFreq(this.note); this.osc2Freq = midiToFreq(this.note); this.osc1.freq(this.osc1Freq); this.osc2.freq(this.osc2Freq * 1.006); this.osc1Env.play(); this.osc2Env.play(); } } function oscBass() { this.osc1 = new p5.SawOsc(); this.osc2 = new p5.SinOsc(); this.oscEnv1 = new p5.Env(); this.oscEnv2 = new p5.Env(); this.oscEnv1.setADSR(0.006, 0.50, 0.50, 0.0); this.oscEnv2.setADSR(0.006, 0.50, 0.50, 0.0); this.oscEnv1.setRange(0.44, 0); this.oscEnv2.setRange(0.32, 0); this.osc1.amp(this.oscEnv1); this.osc2.amp(this.oscEnv2); this.osc1.start(); this.osc2.start(); this.jamBass = function(xnote) { this.note = xnote; this.osc1Freq = midiToFreq(this.note + 12); this.osc2Freq = midiToFreq(this.note + 12); this.osc1.freq(this.osc1Freq); this.osc2.freq(this.osc2Freq); this.oscEnv1.play(); this.oscEnv2.play(); } } function rndNoRepeat(randomField) { var test = floor(random(randomField)); while (test == lastRand) { test = floor(random(randomField)); } lastRand = test; return test; }