// Classical 1 -- An experiment in improvised classical music. // I am using my homemade piano soundfont. // By John Clavin August 29, 2017 var sfnames = [ 'wavfiles/m52.wav', 'wavfiles/m53.wav', 'wavfiles/m54.wav', 'wavfiles/m55.wav', 'wavfiles/m56.wav', 'wavfiles/m57.wav', 'wavfiles/m58.wav', 'wavfiles/m59.wav', 'wavfiles/m60.wav', 'wavfiles/m61.wav', 'wavfiles/m62.wav', 'wavfiles/m63.wav', 'wavfiles/m64.wav', 'wavfiles/m65.wav', 'wavfiles/m66.wav', 'wavfiles/m67.wav', 'wavfiles/m68.wav', 'wavfiles/m69.wav', 'wavfiles/m70.wav', 'wavfiles/m71.wav', 'wavfiles/m72.wav', 'wavfiles/m73.wav', 'wavfiles/m74.wav', 'wavfiles/m75.wav' ] var piano = []; var reverb = []; function preload() { for (n = 0; n < sfnames.length; n++) { piano[n] = loadSound(sfnames[n]); } } 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 minorTemplate = [2, 0, 1, 2, 0, 0, 0, 2, 0, 1, 1, 0, 2, 0, 1, 2, 0, 0, 0, 2, 0, 1, 1, 0, 2, 0, 1, 2, 0, 0, 0, 2, 0, 1, 1, 0, 2, 0 ]; var majorTemplate = [2, 0, 1, 0, 2, 0, 0, 2, 0, 1, 0, 1, 2, 0, 1, 0, 2, 0, 0, 2, 0, 1, 0, 1, 2, 0, 1, 0, 2, 0, 0, 2, 0, 1, 0, 1 ]; var tempKeyArray = [2, 0, 1, 2, 0, 0, 0, 2, 0, 1, 1, 0, 2, 0, 1, 2, 0, 0, 0, 2, 0, 1, 1, 0 ]; var parserDirFlag; var parserIndex; var voiceArray = []; var music; var goMusic = false; var xPos = 0; function setup() { createCanvas(960, 400); frameRate(60); rectMode(CENTER); noStroke(); background(238); masterVolume(0.50); parserDirFlag = true; parserIndex = 4; for (var v = 0; v < 4; v++) { voice = new jcOsc(); voiceArray.push(voice); } for (r = 0; r < piano.length; r++) { reverb[r] = new p5.Reverb(); reverb[r].process(piano[r], 1.74, 0.0); piano[r].setVolume(0.75); reverb[r].amp(1.0); } music = new MusicManagement(); music.composeSpaces(); } function mousePressed() { goMusic = true; } function draw() { if (goMusic) { music.manage(); } } function MusicManagement() { this.timeSpaces = []; this.tsCounter = 0; this.composeSpaces = function() { for (var i = 0; i < 360; i++) { var s = new TimeSpace(); this.timeSpaces.push(s); } for (var j = 0; j < this.timeSpaces.length; j++) { this.timeSpaces[j].compose(); } } this.manage = function() { this.timeSpaces[this.tsCounter].musicGraphics(); if ((this.tsCounter + 1) < this.timeSpaces.length) { if (frameCount % 11 == 1) { this.timeSpaces[this.tsCounter].play(); this.tsCounter++; } } else { goMusic = false; } } } function TimeSpace() { this.restFlag = false; this.noteStartedFlag = false; this.timeFinishedFlag = false; this.noteFoundFlag = false; this.hiNotesFlag = false; this.musicKey = 0; this.notes2Play = []; this.musicTime = 0; this.numberOfNotes = 0; this.compose = function() { if (random(100) < 4) { this.musicKey = floor(random(12)); if (random(100) < 50) { for (var maj = 0; maj < tempKeyArray.length; maj++) { tempKeyArray[maj] = majorTemplate[maj + this.musicKey]; } } else { for (var minor = 0; minor < tempKeyArray.length; minor++) { tempKeyArray[minor] = minorTemplate[minor + this.musicKey]; } } } if (random(100) < 70) { this.numberOfNotes = 1; } else { if (random(100) < 75) { this.numberOfNotes = 2; } else { this.numberOfNotes = 4; } } if (this.numberOfNotes == 1) { this.musicTime = 0; noteFoundFlag = false; while (noteFoundFlag === false) { if (tempKeyArray[parserIndex] > 0) { var note = noteArray[parserIndex]; this.notes2Play[0] = note; noteFoundFlag = true; } this.parserMove(); } if (random(100) < 50) { if (this.hiNotesFlag === true) { parserIndex = floor(random(4, 11)); } else { parserIndex = floor(random(13, 20)); } parserDirFlag = !parserDirFlag; noteFoundFlag = false; while (noteFoundFlag === false) { if (tempKeyArray[parserIndex] == 2) { note = noteArray[parserIndex]; this.notes2Play[0] = note; noteFoundFlag = true; } this.parserMove(); } } } else { this.chordIndex = floor(random(1, 8)); for (var j = 0; j < this.numberOfNotes; j++) { this.noteFoundFlag = false; while (this.noteFoundFlag === false) { if (tempKeyArray[this.chordIndex] == 2) { note = noteArray[this.chordIndex]; this.notes2Play[j] = note; this.noteFoundFlag = true; } this.chordIndex++; } } } } this.parserMove = function() { if (parserIndex >= 23) { parserIndex = parserIndex - 3; parserDirFlag = false; } if (parserIndex <= 1) { parserIndex = parserIndex + 2; parserDirFlag = true; } if (parserDirFlag === true) { parserIndex++; } else { parserIndex--; } if (parserIndex > 12) { this.hiNotesFlag = true; } else { this.hiNotesFlag = false; } } this.play = function() { for (var n = 0; n < this.numberOfNotes; n++) { voiceArray[n].jamOn(this.notes2Play[n]); } } this.musicGraphics = function() { xPos = xPos + 0.25; for (var n = 0; n < this.numberOfNotes; n++) { var yPos = map(this.notes2Play[n], 77, 52, 0, height); noStroke(); if (this.numberOfNotes > 1) { fill(0); } else { fill(200, 0, 0); } rect(xPos, yPos, 6, 12); } } } function jcOsc() { this.jamOn = function(xnote) { this.note = xnote - 53; piano[this.note].play(); } }