Visual Music

I chose to make music based on visuals, rather than make visuals based on music.

import processing.video.*;
import arb.soundcipher.*;
import java.applet.*;

Capture cam;
SoundCipher sc;

float currentRed = 0;
float currentGreen = 0;
float currentBlue = 0;
float currentBright = 0;
float currentBrightPercent;
float noteSpeed = 7;
float stillPixels = 0;
float counter = 0;
int timer = 0;
int bufferCams = 10;
color currentPixel;
color prevPixel = 255;
PImage currentCam;
PImage prevCam;
PImage [] middleCams = new PImage [bufferCams];

void setup () {

size (640, 480);

cam = new Capture (this, 640, 480);
sc = new SoundCipher (this);
currentCam = new PImage (80,60);
prevCam = new PImage (80,60);
for (int i = 0; i < bufferCams; i++)
middleCams [i] = new PImage (80,60);
background (0);
cam.start ();
}

void draw () {

if (cam.available () == true) // only read camera if it’s available
cam.read ();

image (cam, 0, 0, currentCam.width, currentCam.height); // draw what the camera reads

//currentCam.copy();

//image(currentCam, 80, 0);
//image(prevCam,0,0);

for (int i = 0; i < 80; i+= 1) { // get the average colors and brightnesses
for (int j = 0; j < 60; j+= 1) { // of every pixel currently being drawn
currentPixel = get(i,j);
prevPixel = prevCam.get(i,j);
currentBright += brightness (currentPixel)/100;
currentRed += red (currentPixel)/100;
currentGreen += green (currentPixel)/100;
currentBlue += blue (currentPixel)/100;
counter++;
//if ( brightness (currentPixel) > ( brightness (prevPixel) -30) &&
// brightness (currentPixel) < ( brightness (prevPixel) + 30))
// stillPixels += 1;
if ( red(currentPixel) > ( red (prevPixel) -30) &&
red(currentPixel) < ( red (prevPixel) +30)){
if ( green(currentPixel) > ( green (prevPixel) -30) &&
green(currentPixel) < ( green (prevPixel) +30)){
if ( blue(currentPixel) > ( blue (prevPixel) -30) &&
blue(currentPixel) < ( blue (prevPixel) +30))
stillPixels += 1;
}
}
}
}

prevCam.copy(cam, 0, 0, cam.width, cam.height,
0, 0, prevCam.width, prevCam.height);
image (cam, 0, 0);

currentBright /= counter; // get the current brightness
currentBrightPercent = ((currentBright*100)/255);
currentRed /= counter; // get the current average color
currentGreen /= counter;
currentBlue /= counter;

if (currentRed > currentGreen && currentRed > currentBlue)
sc.instrument(sc.PIANO); // depending on current dominant color,
else if (currentGreen > currentRed && currentGreen > currentBlue)
sc.instrument(sc.GLOCK); // change instruments.
else if (currentBlue > currentRed && currentBlue > currentRed)
sc.instrument(sc.EL_BASS);

noteSpeed = (pow(stillPixels,10)/pow(counter,10))*100;

if (timer >= noteSpeed){ // note plays every noteSpeed or greater runs of void draw
sc.playNote( ((currentBrightPercent*20)+60) , 60, .2);
timer = 0;
}
else
timer++;

//text(currentBrightPercent*100, 10, 20);
//text(“(” + stillPixels + “/” + counter + “)*” + 20 + “=”, 10, 40);
//text(noteSpeed,10,60);
//println(noteSpeed);
//prevCam = middleCams[bufferCams-1];
//for (int i = bufferCams-1; i > 0; i–)
// middleCams [i] = middleCams [i-1];
//middleCams[0] = currentCam;

currentRed = 0; // reset all values
currentGreen = 0;
currentBlue = 0;
currentBright = 0;
currentBrightPercent = 0;
counter = 0;
stillPixels = 0;
}

Advertisements

About barberne

an animation student at alfred state from rochester
This entry was posted in Time-Based and tagged , , . Bookmark the permalink.