Matthew Ortega
Matthew Ortega

Reputation: 103

Minim Audio Input Max Threshold Mapping

I’m trying to figure out how to get the max threshold of an AudioInput in my Processing sketch using Minim. I am trying to accurately map the signal so that I can adjust other parameters.

import ddf.minim.*;

Minim minim;
AudioInput in;

void setup()
{
  size(512, 200, P3D);

  minim = new Minim(this);

  // use the getLineIn method of the Minim object to get an AudioInput
  in = minim.getLineIn();
}

void draw()
{
  background(0);
  stroke(255);

  // draw the waveforms so we can see what we are monitoring
  for(int i = 0; i < in.bufferSize() - 1; i++)
  {
    line( i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50 );
    line( i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50 );
  }

  String monitoringState = in.isMonitoring() ? "enabled" : "disabled";
  text( "Input monitoring is currently " + monitoringState + ".", 5, 15 );
}

void keyPressed()
{
  if ( key == 'm' || key == 'M' )
  {
    if ( in.isMonitoring() )
    {
      in.disableMonitoring();
    }
    else
    {
      in.enableMonitoring();
    }
  }
}

Upvotes: 0

Views: 124

Answers (1)

George Profenza
George Profenza

Reputation: 51837

You can use a couple of variable of keep track of the highest and lower values encountered then plug those later for mapping:

import ddf.minim.*;

Minim minim;
AudioInput in;

//keep track of the lowest and highest values 
float minValue = Float.MAX_VALUE;
float maxValue = 0;

void setup()
{
  size(512, 200, P3D);

  minim = new Minim(this);

  // use the getLineIn method of the Minim object to get an AudioInput
  in = minim.getLineIn();
}

void draw()
{
  background(0);
  stroke(255);
  strokeWeight(1);
  // draw the waveforms so we can see what we are monitoring
  for(int i = 0; i < in.bufferSize() - 1; i++)
  {
    line( i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50 );
    line( i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50 );

    //update min,max values
    if(in.left.get(i) < minValue) {
      minValue = in.left.get(i);
    }
    if(in.left.get(i) > maxValue) {
      maxValue = in.left.get(i);
    }
  }
  //simple demo, plot the min/max values, feel free to plug these into map()
  strokeWeight(3);
  stroke(192,0,0);
  line(0,50 + minValue * 50,width,50 + minValue * 50);
  stroke(0,192,0);
  line(0,50 + maxValue * 50,width,50 + maxValue * 50);

  String monitoringState = in.isMonitoring() ? "enabled" : "disabled";
  text( "Input monitoring is currently " + monitoringState + ".", 5, 15 );
}

void keyPressed()
{
  if ( key == 'm' || key == 'M' )
  {
    if ( in.isMonitoring() )
    {
      in.disableMonitoring();
    }
    else
    {
      in.enableMonitoring();
    }
  }
}

Note that if you clap or make a loud noise, the highest will jump and stay there. Depending on the length of your sketch, you might want to reset these values after a while. Additionally you may want to also add easing to these values, so the mapping doesn't feel too jumpy when going from silent to loud environments and back.

Upvotes: 1

Related Questions