Advertisement
Guest User

Aaron Baker Processing Project - Copy of Code

a guest
Jan 25th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import arb.soundcipher.*;
  2. import gab.opencv.*;
  3. import processing.video.*;
  4.  
  5. OpenCV opencv;
  6. Capture cam;
  7. SoundCipher sc = new SoundCipher(this);
  8.  
  9. PImage img;
  10. ArrayList notes = new ArrayList();
  11. int index, time, playDelay;
  12. PVector last = new PVector(-10,0);
  13. color segColor;
  14. boolean doPlayBack = false;
  15.  
  16. boolean sketchFullScreen(){
  17.      return true;
  18. }
  19.  
  20. void setup(){
  21.   colorMode(HSB,100);
  22.   background(0,0,100);
  23.   frameRate(60);
  24.   smooth();
  25.   size(displayWidth,displayHeight);
  26.   img = loadImage("testmusicimage2.png");
  27.   img.resize(160,120);
  28.   cam = new Capture(this,160,120);
  29.   cam.start();
  30.   opencv = new OpenCV(this,img);
  31.   index = 0;
  32.   time = millis();
  33.   playDelay = millis();
  34.   segColor = color(random(100),10,80);
  35. }
  36.  
  37. void draw(){
  38.   if (cam.available() == true) {
  39.      cam.read();
  40.      img = cam.get();
  41.      opencv.loadImage(img);
  42.  
  43.  
  44.     if(mousePressed){
  45.        background(0,0,100);
  46.        notes = new ArrayList();
  47.        doPlayBack = false;
  48.     }
  49.    
  50.     for (int i = 0; i < notes.size(); i++){
  51.       note n = new note(0,0,0);
  52.       n = (note)notes.get(i);
  53.       n.drawCircle();
  54.     }  
  55.    
  56.  
  57.   PVector bright = opencv.max();
  58.   //print(brightness(opencv.getOutput().get(int(bright.x),int(bright.y))) + "   ");                                     //PRINT BRIGHTEST VALUE - UNCOMMENT IF NEED TO ADJUST
  59.   float x = bright.x;
  60.   float y = bright.y;
  61.   x = img.width - x;
  62.  
  63.   x = map(x, 0, img.width, 0, width);
  64.   y = map(y, 0, img.height, 0, height);
  65.  
  66.   if (brightness(opencv.getOutput().get(int(bright.x),int(bright.y))) > 94.7) {
  67.     lightDraw(x,y);
  68.     } else {
  69.        if (doPlayBack) {
  70.           if(millis() - playDelay > 500) {
  71.              playBack();
  72.            }
  73.         }
  74.     }
  75.      /*pushMatrix();
  76.      scale(-1,1);
  77.      image(opencv.getOutput(),0-img.width,0);
  78.      popMatrix(); */                                              //Display the camera output - uncomment for testing
  79.   }
  80.  }
  81.    
  82. void lightDraw(float x, float y) {
  83.   doPlayBack = true;
  84.   float w;
  85.   if (last.x != -10) {
  86.     w = abs(last.x - x) + abs(last.y - y);
  87.   } else {
  88.     w = 20;
  89.   }
  90.   notes.add(new note(x,y,w));
  91.   note tempNote = (note)notes.get(notes.size() - 1);
  92.   tempNote.drawCircle();
  93.   last = new PVector(x,y);
  94.   playDelay = millis();  
  95. }
  96.  
  97. void playBack() {
  98.   if (index >= notes.size()) {
  99.      index = 0;
  100.      last = new PVector(-10,0);
  101.      doPlayBack = false;
  102.      segColor = color(random(100),10,80);
  103.      
  104.      if(notes.size() > 50){
  105.         notes = new ArrayList();
  106.         background(0,0,100);
  107.      }
  108.    }
  109.      
  110.   if (notes.size() != 0){
  111.      note temp = new note(0,0,0);
  112.      temp = (note)notes.get(index);
  113.      temp.play();  
  114.      
  115.      timing();  
  116.    }
  117. }
  118.  
  119. void timing(){
  120.   if(millis() - time > 5){
  121.      time = millis();
  122.      index++;
  123.   }
  124. }
  125.  
  126.  
  127.  
  128. //////// THE NOTE CLASS //////////
  129.  
  130. class note {  
  131.  
  132.   /*
  133.   FIELDS: X,Y,PITCH,AMPLITUDE,COLOUR,SIZE
  134.   FUNCTIONS: DRAW, PLAY
  135.   */
  136.  
  137.   float x,y,amp,size;
  138.   color c;
  139.   int flash;
  140.  
  141.   note (float x, float y, float z) {
  142.     this.x = x;
  143.     this.y = y;
  144.     this.c = c;
  145.     size = z;
  146.    
  147.     flash = 0;
  148.    
  149.     c = segColor;
  150.     c = color(hue(c),map(z,0,500,0,100),brightness(c));
  151.     amp = constrain(z, 0, 100);
  152.   }
  153.  
  154.   void drawCircle(){
  155.     noStroke();
  156.    
  157.      if (flash == 1){
  158.        if(brightness(c) <= 80){
  159.           flash = 0;
  160.        } else {
  161.            c = color(hue(c),saturation(c),brightness(c)-2);
  162.        }
  163.     }
  164.    
  165.     fill(c);
  166.    
  167.     ellipse(x,y,size,size);  
  168.   }
  169.  
  170.   void play(){
  171.     flash();  
  172.     sc.playNote(100-map(y,0,height,0,100),amp,2.0);
  173.   }
  174.  
  175.   void flash() {
  176.     c = color(hue(c),saturation(c),100);
  177.     //drawCircle();
  178.     flash = 1;
  179.   }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement