Mapping microphone input to a boolean

edited December 2013 in Questions about Code

Am I barking up the wrong tree here?

I'm not even sure if this is possible but I'm trying to map an integer from a microphone input to a boolean to spawn in two classes.

Previously they were spawning in on mouse and keyboard inputs and I was hoping at the back of my mind that this would be a simple case of swapping out one with the other.

Here's what i have in my draw function:--------------------------------------------------------------

  // w = new Walker();
for(int =map(-1,1,0,1,boolean i = 0; i < in.left.size()-1; i++)){
    Walker w = in.left.get(i);
    w.step();
    w.display();
  }


   // d = new Diamond(); 
for(int =map(-1,1,0,1,boolean d = 0; d < in.right.size()-1; d++)){
  Diamond o = in.right.get(d);
  o.step();
  o.display();
}

Here is my complete code:-----------------------------------------------------------------------

import ddf.minim.*;

Minim minim;
AudioInput in;
//

class Walker { //class

  float x, y;
  float tx, ty;
  float r;
  int timer;

  // constructor
  Walker(  ) {
    x = random(width);
    y = random(height);

    tx = random (110);
    ty = random (10000);

    r = 255;
    timer = millis();

  } // constructor

  void display() {

    //Cross 1
    pushMatrix();
    translate (x, y);
    stroke (255,255,255,r);
    strokeWeight ( 5 );
    line ( 10, 10, 20, 20 );
    strokeWeight ( 5 );
    line ( 20, 10, 10, 20 );
    popMatrix();

  }

  void step() {
    x = map( noise(tx), 0, 1, 0, width ); // x- & y-location mapped from noise
    y = map( noise(ty), 0, 1, 0, height );

    tx += 0.01; //move forward through "time."
    ty += 0.01;

    if (millis() - timer >= 10000){
    r = map(10000 - (millis() - timer), 0, 30000, 0, 255);

    }
  }
} // class

class Diamond {

  float x, y;
  float tx, ty;
  float r;

  int timer;

  // constructor
  Diamond() {
    x = random(width);
    y = random(height);

    tx = random (110);
    ty = random (10000);

    r = 255;
    timer = millis();

  } // constructor

  void display() {


   //Diamond
    pushMatrix();
    noStroke();
    fill (255,255,255,r);
    translate (x,y);
    //rotate (random (10,180) );
    beginShape();
    vertex(50,50);
    vertex(100,75);
    vertex(50,100);
    vertex(0,75);
    endShape(CLOSE);
    popMatrix();

  }

  void step() {
    x = map( noise(tx), 0, 1, 0, width ); // x- & y-location mapped from noise
    y = map( noise(ty), 0, 1, 0, height );

    tx += 0.0025; //move forward through "time."
    ty += 0.0025;

      if (millis() - timer >= 5000){
      r = map(10000 - (millis() - timer), 0, 10000, 0, 255);
      }
  }
} //class

//

// Walker w;
ArrayList<Walker> listW = new ArrayList();

//Diamond d;
ArrayList<Diamond> listD = new ArrayList();

void mousePressed(){
  listW.add ( new Walker () );
}
void keyPressed(){
  listD.add ( new Diamond () );
}

void setup() {
  size (1200, 700);

  minim = new Minim(this);
  in = minim.getLineIn(Minim.STEREO, 2048);


  background(0);
} // func

void draw() {

  fill(255);
  text ("please press mouse to spawn crosses, spacebar to spawn Rhombuses", 20, 13);


  // w = new Walker();
for(int =map(-1,1,0,1,boolean i = 0; i < in.left.size()-1; i++)){
    Walker w = in.left.get(i);
    w.step();
    w.display();
  }


   // d = new Diamond(); 
for(int =map(-1,1,0,1,boolean d = 0; d < in.right.size()-1; d++)){
  Diamond o = in.right.get(d);
  o.step();
  o.display();
}

  //background fade effect
  pushMatrix();
  fill(0, 0, 0, 55);
  strokeWeight( 0 );
  stroke (0);
  translate( 0, 0 );
  rect (0, 0, width, height);
  popMatrix();


} // func
//

Answers

  • edited December 2013

    for(int =map(-1,1,0,1,boolean i = 0; i < in.left.size()-1; i++)){

    AFAIK, Java doesn't allow declaring variables inside functions. 'Cause that can be interpreted as declaring a function! b-(

    If you want to convert a number to a boolean, try this snippet out:

    static final boolean isItTrue(double num) {
      return num == 0? false : true;
    }
    

    And the invert operation:

    static final int howTrueIsIt(boolean bool) {
      return bool? 1 : 0;
    }
    
  • Try as I might I can't figure out how to implement that to my code :/

    Very new to Processing sorry.

  • edited December 2013

    I believe you know what a boolean is, right?
    http://processing.org/reference/boolean.html

    You pass a value to isItTrue(), and gets either true or false from it.
    For howTrueIsIt(), you pass either true or false and get either 1 or 0.

    However, for the reason why you'd need such convert functions is still not clear to me! @-)

  • I need either an on or an off signal coming from a microphone input so that my sketch will know when and when not to create particles.

  • The microphone would be sending me signals between -1 and 1 apparently so I'm trying to set it up so that anything below 0=0 and anything above =1.

  • Perhaps what you're trying to do is mapping a range of values as true and the other range as false??? #-o

  • precisely yes.

  • That microphone sends int or float data-type? [..]

  • int apparently

  • edited December 2013

    I've adapted isItTrue() to return false if passed value is < 0 and true if it's >= 0:
    Dunno if you want the opposite of it?! :P

    static final boolean toCreateParticle(int num) {
      return num >= 0;
    }
    
  • Thanks so much, where would I put this in my code though? :S

  • I think I figured it out

  • edited December 2013

    A test case example: :-B

    static final boolean toCreateParticle(int num) {
      return num >= 0;
    }
    
    void setup() {
      frameRate(1);
    }
    
    void draw() {
      final int mic = (int) random(-2, 2);
    
      if (toCreateParticle(mic))   println("Created: " + mic);
      else                         println("None: " + mic);
    }
    
  • nope, I have not understood this, are you sure this will work with the code I posted above?

  • edited December 2013

    I haven't looked much at your code. But it's easy to notice that it won't even compile!!! :-q
    Try to create a mini example, similar to mine, that only captures the signal value coming from the mic.
    In my test unit, I've simulated the incoming value as a random number!

  • I don't think your solution is going to work with my code. Thanks anyway.

  • I'm still struggling with this if anybody has any pointers? -Cheers.

Sign In or Register to comment.