Creating an image mosaic that interacts with sound

edited November 2014 in Questions about Code

Hi,

I would like to create tiles out of my image that move freely and when a sound is heard they come to their correct place to show the image. I tried to use Chrisir's image mosaic code and mix it together with minim code. But I was unsuccessful. I am new to programming. I am attaching the code below, I would appreciate any help! (I am on a mac and I think sometimes there are problems with minim and mac but I also tried it on a pc and it did not work. ) Thank you.

// combining the two sketches from
// https://forum.processing.org/topic/image-mosaic-random-x-y-position-particle-movement
// new version with tiles of one color
// ============================================
import ddf.minim.*;
Minim minim;
AudioInput in;

class Fly {
  color c;          // color
  PVector p;        // position
  PVector v;        // velocity
  PVector d;        // destination
  boolean r;        // running yes/no
  //
  // constr
  Fly( color _c, int x, int y ) {
    c = _c;
    d = new PVector( x, y, 0 );
    p = new PVector();
    p.set(d);
    // p = new PVector( random(img.width), random(img.height), 0 );
    // p = new PVector( random(width/2-12, width/2+12), random(height/2-12, height/2+12), 0 );
    // p = new PVector( width/2, height/2, 0 );
    v = new PVector( random(-5, 4), random(-7, 5), 0 );
    // if velocity is == 0, make it > 0
    if (v.x==0)
      v.x=random(1, 2);
    if (v.y==0)
      v.y=random(1, 2);
  } // constr
  //
  void draw() {
    // move and show one tile
    if (!keyPressed) {
      simulate(); // move
    }
    render(); // show
  }
  //
  void simulate() {
    // move one tile
    if ( goHome ) {
      // go home
      float dx=d.x;
      float dy=d.y;
      //
      v.x = dx - p.x;
      v.y = dy - p.y;
      v.limit(4); // max speed
      r = true;
    } // if
    else {
      // random
      if ( r ) {
        v.x = random(-5, 5);
        v.y = random(-5, 5);
        r = false;
      }
    } // else
    //
    // movement
    p.x+=v.x;//+width;
    p.y+=v.y;//+height;
    // boundaries
    if (p.x>width)
      v.x=-1*abs(v.x);
    if (p.x<0)
      v.x=abs(v.x);
    //
    if (p.y>width)
      v.y=-1*abs(v.y);
    if (p.y<0)
      v.y=abs(v.y);
    //    p.x%=width;
    //    p.y%=height;
  }
  void render() {
    // show one tile
    fill(c);
    rect(p.x, p.y, cellSize, cellSize);
  }
} // class
//
// ============================================
//
boolean goHome = false; // go to the image yes / no
ArrayList<Fly> flies = new ArrayList(); // this uses our class
int cellSize = 4;
PImage img;
//
void setup() {
  size(620, 620);
  img = loadImage("mozaik.jpg");  //  loadImage( "http://www.tfguy44.com/MyIcon1.PNG" ); // MyIcon1.PNG" );
  //
  // get the flies with tiles of cellSize x cellSize
  for (int x=0;x<img.width;x+=cellSize) {
    for (int y=0;y<img.height;y+=cellSize) {
      int i = (x+cellSize/2) + (y+cellSize/2)*img.width;
      color c = color(img.pixels[i]);
      Fly currentFly;
      currentFly = new Fly(c, ((width-img.width)/2)+x, ((height-img.height)/2)+y);
      flies.add(currentFly);
    } // for
  } // for
  // func
minim = new Minim(this);
minim.debugOn();
// get a line in from Minim, default bit depth is 16
in = minim.getLineIn(Minim.STEREO, 512);//
}
void draw() {
  background(255);
  // show and move the flies
  for (int i=0;i<flies.size();i++) {
    Fly currentFly;
    currentFly = flies.get(i);
    currentFly.draw();
  } // for
//for(int i = 0; i < in.bufferSize() - 1; i++)
  //goHome = (0 < (in.right.get(i)*200+in.left.get(i)*200));
  //int x = (in.right.get(i)*200);
//  print(x);
println(in.getGain()); 
   }
 // func

void stop()
{
  // always close Minim audio classes when you are done with them
in.close();
minim.stop();
super.stop();
}
Tagged:

Comments

  • you asked me a question I can't see here about that... oh, was that in my inbox...?

Sign In or Register to comment.