Spirit Mirror < Mid term code
Spirit
Mirror

Project Members
Ji Sun Lee
Anjali Patel

Related links
ICM class site
 
Mid term Porject Code on Processing
import processing.serial.*;
Serial myPort;
int dia; 
int microphone;
int signal = 0;    // the good looking microphone signal
int off_count = 0;
int time_count = 0;
int sec_count = 0;
int delay_time = 70;     // [msec] 0.1 sec delay to create a fixed data sampling 
int cutoff_signal = 20;  // Specify the minium signal to be "ON"
int cutoff_sec = 4;       // [0.1sec] At every N second, set to draw one more circle

import pitaru.sonia_v2_9.*;
Sample spirit;

import processing.video.*;
Capture video;
int videoScale = 5;

int cols,rows;

// change position of object
float mx;
float my;


void setup() {
  size(700,600);
  background(0);

  myPort = new Serial(this, Serial.list()[0], 9600);

  Sonia.start(this); // Start Sonia engine
  spirit = new Sample("spirit.wav");
  spirit.setVolume(0);
  spirit.repeat(); 

  cols = width/videoScale;
  rows = height/videoScale;
  video = new Capture(this, cols, rows, 15);
  

}

void draw() {
  // Simulated version of myport...
  microphone = int(dia);
  if (microphone < 0) microphone = 0;

  convert( microphone );
  //println(second() + " Analog: " + microphone + " S= " + signal + " Count= "   + sec_count + " Off= " + off_count + " Time= " + time_count); 

  if(video.available()){
    video.read();
  }

  videoDisplay();


  if (signal>3){
    fill(0,150);
    rect(0,0,width,height);
      float randomNumber = random(3);
     if (randomNumber > 2) {
        objectDraw0();
    }
    else if (randomNumber > 1) {
        objectDraw1();
    }
    else {
        objectDraw2();
    }
    
    spirit.setVolume(1);
  } else{

    spirit.setVolume(0);
  }

  // video end
  delay(delay_time); // 0.1 second delay
}

void videoDisplay(){  
  // display video
  for(int i= 0; i < cols; i++){
    for(int j=0; j < rows; j++){
      int x= i*videoScale;
      int y = j* videoScale;
      int loc = (video.width -i-1) + j*video.width;
      color c= video.pixels[loc];
      fill(c);
      noStroke();
      rect(x,y,videoScale, videoScale);
    }
  }
}

void objectDraw0(){
  for(int jj=0; jj<100; jj++){
    smooth();  
    mx=random(-50,50);
    my=random(-50,50);
    //    translate(random(-5,5), random(-5,5));
    fill(255,jj/10);
    beginShape(POLYGON); 
    curveVertex(344+mx,196+my);
    curveVertex(182+mx,175+my);
    curveVertex(333+mx,303+my);
    curveVertex(335+mx,321+my);
    curveVertex(278+mx,382+my);
    curveVertex(317+mx,398+my);
    curveVertex(369+mx,342+my);
    curveVertex(421+mx,291+my);
    curveVertex(446+mx,279+my);
    curveVertex(469+mx,287+my);
    curveVertex(464+mx,271+my);
    curveVertex(452+mx,249+my);
    curveVertex(430+mx,250+my);
    curveVertex(459+mx,201+my);
    curveVertex(468+mx,133+my);
    curveVertex(399+mx,160+my);
    curveVertex(387+mx,244+my);
    curveVertex(349+mx,240+my);
    curveVertex(259+mx,181+my);
    curveVertex(181+mx,174+my);
    endShape(); 
  }
}

void objectDraw1(){
  for(int jj=0; jj<100; jj++){
    smooth();  
    mx=random(-50,10);
    my=random(-50,10);
    translate(random(-2,5),random(-2,5));
    fill(255,jj/10);
      beginShape(POLYGON); 
     curveVertex(161+mx,117+my);
     curveVertex(125+mx,81+my);
     curveVertex(125+mx,65+my);
     curveVertex(132+mx,48+my);
     curveVertex(152+mx,39+my);
     curveVertex(186+mx,47+my);
     curveVertex(195+mx,70+my);
     curveVertex(215+mx,49+my);
     curveVertex(231+mx,48+my);
     curveVertex(252+mx,64+my);
     curveVertex(261+mx,90+my);
     curveVertex(256+mx,102+my);
     curveVertex(219+mx,117+my);
     curveVertex(201+mx,121+my);
     curveVertex(227+mx,146+my);
     curveVertex(234+mx,174+my);
     curveVertex(199+mx,206+my);
     curveVertex(154+mx,201+my);
     curveVertex(151+mx,183+my);
     curveVertex(155+mx,152+my);
     curveVertex(121+mx,164+my);
     curveVertex(82+mx,149+my);
     curveVertex(73+mx,124+my);
     curveVertex(83+mx,87+my);
     curveVertex(106+mx,71+my);
     curveVertex(124+mx,78+my);
     curveVertex(129+mx,79+my);
     endShape(); 
  }
}
void objectDraw2(){
  for(int jj=0; jj<100; jj++){
    smooth();  
    mx=random(-50,50);
    my=random(-50,50);
    fill(255,jj/10);
    ellipse(width/2+mx, height/2+my, 50+jj, 50+jj);
  }
}

// ---------------------------
// Sound function
// ---------------------------
public void stop(){
  Sonia.stop();
  super.stop();
}

// ---------------------------
// Serial communication Function
// ---------------------------

void serialEvent(Serial myPort) {
  //read the serial port and assign it to the x position of the object
  dia = myPort.read();
  // print the serial data
}

// ---------------------------
// Signal Conversion Function
// ---------------------------
void convert(int analog) {
  // Ignore noise
  if (analog <= 10) {
    off_count += 1;
    if (off_count >= 6) 
      signal = 0;
    // Otherwise, analog > 0, good signal
  } 
  else {
    off_count = 0;

    signal = analog/cutoff_signal; // let analog input 100 be the minim
    // thus, if analog = 0 0 50 200 400 0  800 1000 1000 0 0 0 0 
    //     signal = 0 0 0  1    2  2   4    5   5   5 0 0 0 
  }     
  // Signal ON/OFF timing: assume each data is collected every 0.1 second
  if (signal > 0) {
    time_count += 1;      // 10 counts per second 
    sec_count = time_count/cutoff_sec;   // "/ 20" gives 1 every 2 seconds

  } 
  else {
    time_count = 0;
    sec_count = 0;
  }
}


	
Do you want to contact us?
Ji Sun Lee, Anjali Patel