Making a Sound Meter

Hello!

I have a problem. I am a newbie with processing and I'm trying to make a sound meter similar to one that you would see on a lie detecter test/or a Joy Division album cover. I've only been using processing for a month now, and the problem I'm running into is that every time I make a sound my image jumps and looks like squares instead of curved lines.

Hoping I could get some help....

-Clinton

import processing.sound.*; AudioIn in; Amplitude amp; float x=50; float y=10; float c; void setup() { frameRate (300); size (1500, 500); amp=new Amplitude(this); in=new AudioIn(this, 0); in.start(); amp.input(in); background(255); }

void draw() { c=amp.analyze()*50; beginShape(); stroke(25);

line(x, y, x, y-c); line(x, y, x, y+c); x++; if (x>width) { y=y+10; x=0; } }

Answers

  • edited March 13

    Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Demo below.

    Kf

    import processing.sound.*; 
    
    final int STEPX=4;
    final int STEPY=30;
    
    AudioIn in; 
    Amplitude amp; 
    float x=50; 
    float y=STEPY; 
    float c; 
    PVector prevPoint;
    
    
    void setup() { 
      size (displayWidth, 500); 
      amp=new Amplitude(this); 
      in=new AudioIn(this, 0); 
      in.start(); 
      amp.input(in); 
      background(255);
      prevPoint=new PVector(0, y);
    }
    
    void draw() { 
      c=amp.analyze()*50; 
      beginShape(); 
      stroke(25);
    
      line(prevPoint.x,prevPoint.y, x+STEPX, y+c);
    
    
    
      if (x>width) { 
        y=y+STEPY; 
        x=0;
      }
    
      prevPoint.set( x=x+STEPX, y+c);
    
    }
    

    P.S. The code for the line() function above could be replace with:

      beginShape();
      vertex(prevPoint.x, prevPoint.y);
      vertex( x+STEPX, y+c);
      endShape();
    
  • Hey KF,

    Thanks for this, extremely helpful. The only problem that I'm running into with your code is that when the meter goes to the next line, it keeps running (connecting the line to the next with a massive diagonal line through the screen) . Wondering if it's possible to get it to skip to the next line in the Y axis. Let me know if this doesn't make sense, and thanks again!

  • Answer ✓

    Right, it is a bug in my code. Here is a revised version of the relevant section. You need to change one line. I edited my code above.

    Kf

  • Thanks for this!

  • a Joy Division album cover

    For those not familiar with this:

    download

Sign In or Register to comment.