How can I put microphone value into an array and also filter microphone value

I want to do a project which is about when saying something through microphone, and then can draw something (on Processing).

l watch Daniel Shiffman's video "17.9: Sound Visualization: Graphing Amplitude - p5.js", https://youtube.com/watch?v=jEwAMgcCgOA&t=370s, but what I use is processing and pure data, I have a question about how to put those microphone value into an array in Processing, like the tutorial in the video, (the way of receive sound from microphone I use is Pure data, and I'm successfully connect pd and Processing), but I just don't know how to put those value into an array in processing, and I guess the value also need to greater than a certain value because microphone will also receive value when is not saying.Thank you for your help!

My Pure data and Processing sketch is here https://github.com/nancyjou/Sound-Visualization

and here is only my Processing sketch.

float global1;
float global2;
float adc;
float fiddle;
float colorY;
float alphaX;

import netP5.*;
import oscP5.*;


OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(800, 800);
  background(255);
  frameRate(25);
  colorMode(HSB, 360, 100, 100);
  noStroke();
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this, 12001);

  myRemoteLocation = new NetAddress("127.0.0.1", 12000);
}

void draw() {

  fiddle = global2;
  colorY= map(global2, 25, 150, 0, 360);
  alphaX= map(global1, 40, 250, 20, 255);
  fill(colorY, 100, 100);
  fill(colorY, 100, fiddle+50, alphaX);
  for (int y=200; y<(height/4)+1; y+=10) {
    ellipse(width/2, random(0, 400), global1, global1);
    println(y);
  }
  adc = global1 + 10;
}


void mousePressed() {
  OscMessage myMessage = new OscMessage("/first");


  myMessage.add(123);
  myMessage.add(12.34);
  myMessage.add("some text");

  oscP5.send(myMessage, myRemoteLocation);
}  

void oscEvent(OscMessage theOscMessage) {

  if (theOscMessage.checkAddrPattern("/first")==true) {



    float firstValue = theOscMessage.get(0).floatValue();
    global1 = firstValue;
  }

  if (theOscMessage.checkAddrPattern("/second")==true) {



    float secondValue = theOscMessage.get(0).floatValue();
    global2 = secondValue;
  }
  println("### received an osc message. with address pattern "+theOscMessage.addrPattern());
}

Answers

  • Are you familiar with the minim library? You can install it using the library manager i the Processing IDE. After you install the library, you can check the examples as it shows some filtering techniques. This next post should get you started with minim. Notice you can access the mic directly from there.

    https://forum.processing.org/one/topic/how-can-i-detect-sound-with-my-mic-in-my-computer.html

    You can check the documentation here: http://code.compartmental.net/minim/audioinput_class_audioinput.html

    Kf

  • @kfrajer

    I try the minim library, and it works, but I want it is draw only when I saying something through microphone, now it receive all the values, so even if I did't talk, it will also draw. So I think I need to filter the value, when the value is greater than a certain value, it can draw only when talking. How can I do that or is there any other ways to deal with that? Thanks!!

    import ddf.minim.*;
    
    Minim minim;
    AudioInput in;
    color white;
    
    void setup()
    {
      size(512, 200, P2D);
      white = color(255);
      colorMode(HSB,100);
      minim = new Minim(this);
      minim.debugOn();
    
      // get a line in from Minim, default bit depth is 16
      in = minim.getLineIn(Minim.STEREO, 512);
      background(0);
    }
    
    void draw()
    {
      background(0);
      // draw the waveforms
      beginShape();
      for(int i = 0; i < in.bufferSize() - 1; i++)
      {
    
        stroke(white);
        noFill();
        float y = map (in.left.get(i),0,1,height/2,0);
        vertex(i, y);
        println(i);
      }
     endShape();
    }
    
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
      in.close();
      minim.stop();
      super.stop();
    }
    
  • edited November 2017

    @nancyjou -- Do you want to check .getVolume() on an AudioInput before deciding whether to draw or not?

    Edit: looks like using .getVolume() is wrong -- instead, loop over each bin in the buffer and check if it is greater than a threshhold value, e.g. using AudioInput.left.get(i) -- see @kfrajer answer below

  • edited November 2017

    @jeremydouglass

    sorry I have no idea how to use .getVolume() :-S :-S it say:

    === Minim Error === === Volume is not supported.

    import ddf.minim.*;
    
    Minim minim;
    AudioInput in;
    color white;
    
    void setup()
    {
      size(512, 200, P2D);
      white = color(255);
      colorMode(HSB,100);
      minim = new Minim(this);
      minim.debugOn();
    
      // get a line in from Minim, default bit depth is 16
      in = minim.getLineIn(Minim.STEREO, 512);
      background(0);
    }
    
    void draw()
    {
      background(0);
      in.getVolume();
     println(in.getVolume());
    }
    
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
      in.close();
      minim.stop();
      super.stop();
    }
    
  • Answer ✓

    Consider this example.

    Kf

    import ddf.minim.*;
    
    
    final float THRESH=0.25;
    Minim minim;
    AudioInput in;
    color white;
    
    void setup()
    {
      size(512, 200, P2D);
      white = color(255);
      colorMode(HSB, 100);
      minim = new Minim(this);
      in = minim.getLineIn(Minim.STEREO, 512);
    
    }
    
    void draw()
    {
      //===========
      //STEP ONE: Check for silence
    
      boolean silenceFlag=true;
    
      for (int i = 0; i < in.bufferSize() - 1; i++){
        if(in.left.get(i)>THRESH){
          silenceFlag=false;
          break;
        }    
      }
    
      if(silenceFlag)
      return;   //Skips draw()
    
    
      //=================
      //STEP 2: Draw the wave
    
      background(0);
      // draw the waveforms
      beginShape();
      for (int i = 0; i < in.bufferSize() - 1; i++)
      {
        stroke(white);
        noFill();
        float y = map (in.left.get(i), 0, 1, height/2, 0);
        vertex(i, y);
        println(i);
      }
      endShape();
    }
    
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
      in.close();
      minim.stop();
      super.stop();
    }
    
  • @kfrajer

    Thanks a lot!! This example is really good, it works!! but when l set the canvas longer, I found another problem that the line can't draw continuously to the right, it stop drawing to the right at some point on the canvas, I want it can keep drawing to the right like the Daniel Shiffman's video do, and I check my code, I cant't find the problem. #-o

    And there is another small question, when I use this minim library to receive mic value, when I press RUN button, it need to wait about a minute before the window will jump out, is that normal? or just my computer's problem?

    import ddf.minim.*;
    
    
    final float THRESH=0.15;
    Minim minim;
    AudioInput in;
    color white;
    
    void setup()
    {
      size(800, 200, P2D);
      white = color(255);
      colorMode(HSB, 100);
      minim = new Minim(this);
      in = minim.getLineIn(Minim.STEREO, 512);
    
    }
    
    void draw()
    {
      //===========
      //STEP ONE: Check for silence
    
      boolean silenceFlag=true;
    
      for (int i = 0; i < in.bufferSize() - 1; i++){
        if(in.left.get(i)>THRESH){
          silenceFlag=false;
          break;
        }    
      }
    
      if(silenceFlag)
      return;   //Skips draw()
    
    
      //=================
      //STEP 2: Draw the wave
    
      background(0);
      // draw the waveforms
      beginShape();
      for (int i = 0; i < in.bufferSize() - 1; i++)
      {
        stroke(white);
        noFill();
        float y = map (in.left.get(i), 0, 1, height/2, 0);
        vertex(i, y);
        println(i);
      }
      endShape();
    }
    
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
      in.close();
      minim.stop();
      super.stop();
    }
    
  • Taking a minute to start? That is odd. I suggest removing the return statement in line 34. Do you observe the same problem? What operating system btw?

    I can't see the video right now. Not sure what you mean with keep drawing to the line (I will need to see the video). You mean the data slides to the left? Or do you mean that after running the code for certain time, the program stops refreshing?

    Kf

  • @kfrajer

    Actually, the first time I run the code in the link you give me will need to taking a minute to start(https://forum.processing.org/one/topic/how-can-i-detect-sound-with-my-mic-in-my-computer.html), so if remove the return statement can't solve, I think it is either the minim library's problem, or is my computer's problem... I use PC and processing3.3.6 ( is that the operating system you mean?)

    And about keep drawing line problem, sorry just temporarily ignore me, I will try it again later, maybe I can deal with it. Thanks!

  • Are you a mac or Windows user? Very odd about the delay... So I understand it is only the first time? It is not that bad then, I would say.

    Kf

  • @kfrajer
    I am a Windows user, not only the first time, every time I want to check the code how it looks like, I need to wait for a minute ha ha, but it does not matte, the program can work just fine, thanks for your help, the example really help me!!

Sign In or Register to comment.