0

我试图弄清楚如何使用 Minim 在我的处理草图中获得 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();
    }
  }
}
4

1 回答 1

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

请注意,如果您鼓掌或大声喧哗,最高的将跳并停留在那里。根据草图的长度,您可能希望在一段时间后重置这些值。此外,您可能还想为这些值添加缓动,以便在从静音环境到嘈杂环境再返回时,映射不会感觉太跳跃。

于 2017-05-11T18:05:48.770 回答