This sketch is created with an older version of Processing,
and doesn't work on browsers anymore.
xxxxxxxxxx
int w, h, xpos, ypos, xa, xd; //variables for width, height, mouse position and the left and right of each section
int num = 500; //size of arrays (number of ellipses) for visuals
int[] dx = new int[num];
int[] dy = new int[num]; //arrays for position and size of ellipses
int[] diam = new int[num];
import arb.soundcipher.*; //imports soundcipher library
SoundCipher notes = new SoundCipher(this);
//these create soundcipher instruments for notes and chords
SoundCipher chord = new SoundCipher(this);
void setup()
{
size(1200, 760); //setting up the canvas
background(40);
smooth();
frameRate(10);
w = width; h = height; //assigning width and height to w and h
drawLines(); //draws the lines and words from the start
words();
notes.instrument(39); //bass guitar instrument for notes
chord.instrument(25); //steel string acoustic guitar for broken chords
}
void draw()
{
drawLines(); //draws the dividing lines
xpos = mouseX; ypos = mouseY; //assigns mouse position to xpos and ypos
float vel = map(ypos, 0, h, 30, 90); //maps mouse y position to velocity of the notes
background(40);
drawLines(); //resets background and redraws lines and text every frame to cover up previous ellipses
words();
if (mousePressed == true) {
if (xpos < w/6) {xa = 0; xd = w/6; ellipses(); notes.playNote(38, vel, 1.2);}
else if (xpos < w/3) {xa = w/6; xd = w/3; ellipses(); notes.playNote(40, vel, 1.2);} //this section determines where the mouse is. this defines
else if (xpos < w/2) {xa = w/3; xd = w/2; ellipses(); notes.playNote(41, vel, 1.2);} //values for xa and xd and calls the ellipse function to
else if (xpos < w - w/3) {xa = w/2; xd = w - w/3; ellipses(); notes.playNote(43, vel, 1.2);} //draw the ellipses in the correct box
else if (xpos < w - w/6) {xa = w - w/3; xd = w - w/6; ellipses(); notes.playNote(45, vel, 1.2);}
else {xa = w - w/6; xd = w; ellipses(); notes.playNote(47, vel, 1.2);}
}
saveFrame();
}
void keyPressed()
{
if (key == ' ') markovChords(); //if the space bar is pressed, call the markov function which plays chords according to a markov chain
}
void words() //this function prints all of the text to the canvas
{
fill(200, 0, 100);
textAlign(CENTER);
textSize(20);
text("Click to Play Notes!", w/2, h-35);
text("Hold Space Bar to Play Chords!", w/2, h - 10);
textSize(40);
text("D", w/12, h - 90);
text("E", 3*w/12, h - 90);
text("F", 5*w/12, h - 90);
text("G", 7*w/12, h - 90);
text("A", 9*w/12, h - 90);
text("B", 11*w/12, h - 90);
}
void markovChords() //this function contains the markov chain whiach plays the chords
{
if (frameCount % 5 == 0) //plays a new chord only every 5 frames
{
int last = 1; //variable for last state of the state engine
float r = random(1); //creates the random probabilities
float[] chord1 = {48, 52, 55}; //Cmaj
float[] chord2 = {53, 57, 60}; //Fmaj //arrays for the three chords
float[] chord3 = {55, 59, 60}; //Gmaj
float[] dyn = {65, 70, 75}; //these arrays define the velocity and duration of each respective note in the broken chord
float[] dur = {0.2, 0.2, 0.3};
switch(last) //switches the last variable around
{
case 1:
if (r < 0.2)
{
chord.playPhrase(chord1, dyn, dur);
last = 1;
}
else if (r < 0.4)
{
chord.playPhrase(chord2, dyn, dur);
last = 2;
}
else if (r < 0.8)
{
}
else
{
chord.playPhrase(chord3, dyn, dur);
last = 3;
}
break;
case 2:
if (r < 0.3)
{
chord.playPhrase(chord1, dyn, dur);
last = 1;
}
else if (r < 0.5)
{
chord.playPhrase(chord2, dyn, dur);
last = 2;
}
else if (r < 0.8)
{
}
else
{
chord.playPhrase(chord3, dyn, dur);
last = 3;
};
break;
case 3:
if (r < 0.1)
{
chord.playPhrase(chord1, dyn, dur);
last = 1;
}
else if (r < 0.3)
{
chord.playPhrase(chord2, dyn, dur);
last = 2;
}
else if (r < 0.8)
{
}
else
{
chord.playPhrase(chord3, dyn, dur);
last = 3;
}
break;
}
}
}
void ellipses()
{
for (int i = 0; i < num; i++) {
dy[i] = trianglePass(0, ypos);
diam[i] = lowPass(50);
}
for (int i = 0; i < num; i++) {
dx[i] = trianglePass(xa, xd);
}
for (int j = 0; j < num; j++) {
stroke(random(255), random(255), random(255));
fill(random(155), random(155), random(255), 30);
ellipse(dx[j], dy[j], diam[j], diam[j]);
}
}
void drawLines()
{
for (int i = 0; i < w; i += w/6)
{
stroke(255);
line(i, 0, i, h-60);
}
line(0, h-60, w, h-60);
}
int trianglePass(int a, int d)
{
int aa = int(random(a, d) + random(a, d) + random(a, d) + random(a, d) + random(a, d) + random(a, d) + random(a, d) + random(a, d))/8;
return aa;
}
int lowPass(int b)
{
int bb = min(int(random(5, b)), int(random(5, b)));
return bb;
}