Tuesday, November 30, 2004

Processing Code 11/29

There is a problem with the .wav and timing of the color changes. We are wondering if this is a Processing or Sonia issue. This code uses for loops and arrays to cut down on file size.

// CODE
// SONIA V2.5 -- Audio Live Stream.
//http://www.pitaru.com/sonia/
//Goal is to capture audio data and parse into if/then statements
//Note the Sonia code folder goes into the sketch folder

//web site to scrape: http://www.dhs.gov/dhspublic/display?theme=29
//sounds: http://www.findsounds.com/ISAPI/search.dll

int numOfPics = 5; //number of images in image array for colors
BImage[] colors = new BImage[numOfPics]; //new array of up for displaying color photos
int picDislayTime = 100000; //time image is displayed in milliseconds

int whichImage = 0;
int startTime;
Sample thunder;
float previousLevel = 0;
boolean showing = false;

void setup()
{
Sonia.start(this); // Start Sonia engine.
LiveInput.start(256); // Start LiveInput and return 256 FFT frequency bands.

thunder = new Sample("thunder.wav");

size(640,480);

for(int j=0 ; j<5 ; j++)
{
int m = j+1;
colors[j] = loadImage("color" + m + ".gif");
}
startTime = millis();
}

void loop()
{

showImage(); //Show appropriate image based on input from microphone

for(int i = 5 ; i > 0 ; i-=1)
{
int j = i-1;
if (showing)

if (whichImage == i) {
image(colors[j], 0, 0);
}
if ((millis() - startTime > picDislayTime) || (1500 * LiveInput.getLevel(Sonia.LEFT) > previousLevel))
/* || (1000 * LiveInput.getLevel(Sonia.LEFT) < previousLevel)*/{
showing = false;
}
}
}

void showImage()//Show image based on input from microphone
{
float meterDataLeft = 1500 * LiveInput.getLevel(Sonia.LEFT);//avg this w/ serial in

for(int i = 800 ; i > 100 ; i-=160)
{
int j = i/160;

if (!showing)
{
if (meterDataLeft > i)
{
whichImage = j;
println(j);
thunder.play();
showing = true;
startTime = millis();
previousLevel= meterDataLeft;
}
}
}
}
// Safely close the sound engine upon Browser shutdown.
public void stop(){
Sonia.stop();
super.stop();
}