Loading...
Logo
Processing Forum
Hello,

I working on a simple program as a part of a sound-reactive installation. Every time the sound velocity is more than 5, a rectangle comes in on the right, and all other objects shift one place to the left. 

The problem: the velocity is the width/height of the rectangles. Now it affects all objects, but I only want to assign the value to the newest, so all rectangles get their own size (and stay that size). Maybe there is something wrong with void show() and I tried things with an arraylist, but I'm afraid I'm a bit lost.

This is a simplified example to make it more clear, in the real version the rectangles are characters (kind of sound-reactive newsticker).

If anyone can point me in the right direction, that would be great :-)



full code:
Copy code
  1. import ddf.minim.*;
  2. Minim minim;
  3. AudioInput in;
  4. AudioRecorder recorder;

  5. Rectangle[] myRectangle;
  6. float[] vs = new float[1000];
  7. float vel;
  8. int count;
  9. int index;
  10. public float y; 
  11. private float v;
  12. String txt;
  13. void setup() {
  14.   size(1024, 400);
  15.   noStroke();
  16.   minim = new Minim(this);
  17.   in = minim.getLineIn(Minim.STEREO, 2048);

  18.   count = 50;
  19.   char[] allChars = new char[count];
  20.   myRectangle = new Rectangle[count];

  21.   for (int i = 0; i < count; i++) {
  22.     myRectangle[i] = new Rectangle();
  23.   }
  24. }

  25. void draw() {
  26.   y = in.left.get(2)*800;
  27.   scale(0.1);
  28.   background(255);
  29.   translate(0, 200);

  30.   if (y>5) {
  31.     show();
  32.   }

  33.   for (int i=0;i<count;i++) {
  34.     vel = y;
  35.     myRectangle[i].display();
  36.   }
  37. }

  38. void show() {
  39.   if (index<count) {
  40.     for (int i=0;i<index;i++) {
  41.       vel = y;
  42.       myRectangle[i].update();
  43.     }
  44.   }
  45.   index++;
  46. }

class
Copy code
  1. class Rectangle { 
  2.   float xpos;
  3.   Rectangle() {
  4.     xpos = width*10;
  5.   }  
  6.   void display() {
  7.     fill(0, 99, 220);
  8.     rectMode(CENTER);
  9.     rect(xpos+40, 0, vel, vel);
  10.   }
  11.   void update() {
  12.     int spacing = 300;
  13.     xpos = xpos - spacing;
  14.   }
  15. }

Replies(8)

The problem is that you are using vel to display all of the rectangles. If you want the rectangles to be size-independent, then you need to make a size value for each of the rectangles (that is, inside the Rectangle class... probably assigned when the value is received and the rectangle is created).
Thanks for your fast response! I tried this:

in draw:
Copy code
  1.  for (int i=0;i<count;i++) {
  2.     vel[i] = y;
  3.     myRectangle[i].display(vel[i]);
  4.   }
in class:
Copy code
  1.   void display(float vel) {
  2.     fill(0, 99, 220);
  3.     rectMode(CENTER);
  4.     rect(xpos+40, 0, vel, vel);
  5.   }

This seems to work with a normal array, but the value y (microphone input) is constantly changing, so I probably need another way to add the received values to the array. Any ideas?
Can you just add a field to the Rectangle class that contains the size of the rectangle... and reference that?
That doesn't seem to work either. Or maybe I didn't understand it right, maybe you can try it and post an example? Thanks!
Quite frankly, I don't understand your code enough in order to generate an example...
Where in the code do you add a new rectangle?
Are the rectangles stored properly?

Basically, in order for each rectangle to retain its own size, it needs a locally store variable containing the size...
Similar to the xpos.

When the rectangle is created, that size is assigned to the current vel.
In setup() I create al rectangles ( myRectangle[i] = new Rectangle(10); ). I show them with  display() in draw. And change the xpos in update();

But I can't assign the value in setup(), because there isn't sound yet in setup. That's why I wanted to assign the sound value with display().

Maybe I need to create the rectangles with an arraylist, so I can create them in the draw? I've tried this already, but maybe in a wrong way.


Oh, and this what I am trying to do: I simplified it to rectangles, but in the end it will be characters consisting of rectangles with a different size for every character (the code to import text and create the characters out of rectangles works already, but isn't relevant for this problem). 


Woohoo, I finally figured it out. Thanks for al the help! I needed to store the rectangles in an arraylist; first time I used that, so it took me a while to find out how it works.

The process and end result (in a few weeks) can be seen on  examen.martijnvanbachum.com (dutch), final project for my study graphic design.


Copy code
  1. ArrayList <Rectangle> Rectangles = new ArrayList <Rectangle> ();
  2. import ddf.minim.*;
  3. Minim minim;
  4. AudioInput in;
  5. AudioRecorder recorder;
  6. float v;

  7. void setup() {
  8.   size(1024, 400);
  9.   noStroke();
  10.   minim = new Minim(this);
  11.   in = minim.getLineIn(Minim.STEREO, 2048);
  12. }

  13. void draw() {
  14.   background(#FFFFFF);
  15.   for (Rectangle lt : Rectangles) {
  16.     lt.display();
  17.   }

  18.   v = in.left.get(2)*2000;
  19.   println(v);
  20.   if (v>5) {
  21.     Rectangles.add(new Rectangle(width, v));
  22.     for (Rectangle lt : Rectangles) {
  23.       lt.update();
  24.     }
  25.   }
  26. }
  27. class Rectangle {
  28.   int x, y;
  29.   float v;
  30.   Rectangle(int x, float v) {
  31.     this.x = x;
  32.     this.v = v;
  33.   }

  34.   // display function
  35.   void display() {
  36.     fill(0, 99, 220);
  37.     rectMode(CENTER);
  38.     rect(x, 100, v, v);
  39.   }
  40.   
  41.   void update() {
  42.     x = x-100;
  43.   }
  44. }
I'm glad you finally figured it out... ArrayList is definitely a good way to do it.

I'll be sure to look at it when it's completed, but perhaps a better way to showcase your work is in the Share Your Work section.