var nbinstruments, pentanotes;
var center, garadius, tempo;
createCanvas(windowWidth, windowHeight);
96, 99, 101, 102, 103, 106];
colorMode(HSB, 360, 255, 255, 255);
textAlign(CENTER, CENTER);
center=createVector(windowWidth/2, windowHeight/2);
garadius=windowHeight*.45;
if (random(1)>.7&&(2*frameCount*tempo)%PI<.1) {
if (millis()%120000<60000) {
if (instruments.length<8 && random(1)>.75)instruments.push(new Instrument(random(1)>.5?new p5.SinOsc():new p5.TriOsc(), new p5.Envelope(), random(.1), random(1), random(1), random(1)));
var nk=int(random(instruments.length));
instruments.splice(nk, 1, new Instrument(random(1)>.5?new p5.SinOsc():new p5.TriOsc(), new p5.Envelope(), random(.1), random(1), random(1), random(1)));
var nk=int(random(instruments.length));
if (instruments.length>0 && random(1)>.75)instruments.splice(nk, 1);
instruments.splice(nk, 1, new Instrument(random(1)>.5?new p5.SinOsc():new p5.TriOsc(), new p5.Envelope(), random(.1), random(1), random(1), random(1)));
for (var j=0; j<instruments.length; j++) {
for (var i=0; i<ins.keys.length; i++) {
if (abs(ins.keys[i].angle+frameCount*tempo)%TWO_PI<.02)ins.keys[i].play();
text("Click!", windowWidth*.5, windowHeight*.5);
line(center.x, center.y, center.x+garadius*sin(-frameCount*tempo), center.y+garadius*cos(-frameCount*tempo));
function mouseClicked() {
for (var i=0; i<nbinstruments; i++) {
instruments.push(new Instrument(random(1)>.5?new p5.SinOsc():new p5.TriOsc(), new p5.Envelope(), random(.1), random(1), random(1), random(1)));
function Key(freq, volume, instrument, angle, col, radius) {
this.instrument=instrument;
this.position=createVector(center.x+this.radius*garadius*sin(this.angle), center.y+this.radius*garadius*cos(this.angle));
Key.prototype.display=function() {
translate(this.position.x, this.position.y);
if (millis()<this.start+1000*this.instrument.releaseTime) {
ellipse(0, 0, (.5+.5*sin(frameCount))*60*this.volume, (.5+.5*sin(frameCount))*60*this.volume);
ellipse(0, 0, 60*this.volume, 60*this.volume);
Key.prototype.play=function() {
this.instrument.envelope.mult(this.volume);
this.instrument.osc.freq(this.freq);
this.instrument.envelope.play(this.instrument.osc, 0, 0.1);
function Instrument(osc, envelope, attackTime, decayTime, susRatio, releaseTime) {
this.releaseTime=releaseTime;
this.envelope.setADSR(attackTime, decayTime, susRatio, releaseTime);
this.envelope.setRange(1, 0);
this.nkeys=int(random(1, 12));
var col=color(random(360), 255, 255, 200);
this.note=int(random(pentanotes.length));
var freq=midiToFreq(pentanotes[this.note]);
for (var k=0; k<this.nkeys; k++) {
if(random(1)>0)this.keys.push(new Key(freq, vol, this, k*TWO_PI/this.nkeys, col, map(this.note, 0, pentanotes.length, .1, 1)));