最小音频输入最大阈值映射

时间:2017-05-11 15:20:58

标签: processing fft minim

我正在尝试弄清楚如何使用Minim在我的Processing sketch中获取AudioInput的最大阈值。我正在尝试准确映射信号,以便我可以调整其他参数。

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();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您可以使用几个变量来跟踪遇到的最高和最低值,然后将其插入以进行映射:

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();
    }
  }
}

注意如果你拍手或发出巨响,最高音将跳跃并留在那里。根据草图的长度,您可能需要在一段时间后重置这些值。此外,您可能还希望为这些值添加缓动,因此从静音到嘈杂的环境并返回时,映射不会感到太刺激。