【问题标题】:Trying to draw on a canvas from picking up frequencies - processing尝试通过拾取频率在画布上绘制 - 处理
【发布时间】:2018-07-01 13:04:46
【问题描述】:

我在停止播放字符串后无法在画布上保留绘图。

所以我将它映射为从我的麦克风中拾取一个频率,当它听到该频率时,它会在画布上画一条线。唯一的问题是停频后就消失了。

如何在画布上保留线条,然后用其他频率绘制更多线条?

代码如下:

import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioInput in;
FFT fft;
float highestAmp=0,freq,frequency;
float amplitude;
int x = 0;
int y = 0;
int p = x;
int o = y;

void setup(){
        size(512, 200);
        background(0);

        // initialize Minim and catching the output
        minim = new Minim(this);
        in = minim.getLineIn(Minim.MONO, 4096*8, 44100);
        fft = new FFT(in.left.size(), 44100);

}


void draw() {
      highestAmp=0;
      amplitude=0;
      frequency = 0;
      fft.forward(in.left);
      x = constrain(x,0,width);
      y = constrain(y,0,height);


      //searching from 0Hz to 20000Hz. getting the band, and from the band the frequency
     for(int i = 0; i < 20000; i++) {
            amplitude = fft.getFreq(i);
            if (amplitude > highestAmp){
                highestAmp = amplitude;
                frequency = i;
            }
          }
          //write the frequency on the screen
          fill(255);
          background(0);
          text(frequency,200,100);
          text(amplitude,400,100);
          if (frequency > 340.00 & frequency < 360){
            stroke(28,125,85);
            line(x, y, p, o);
            x++;
            y++;

          }

          if (frequency > 320.00 & frequency < 340){
            stroke(255,0,166);
            line(x, y, p, o);
            x++;
            y--;
          }



}

【问题讨论】:

    标签: java audio canvas processing frequency


    【解决方案1】:

    通常,您希望将先前的频率存储在数据结构中,例如数组或 ArrayList。然后,您将遍历该数据结构并绘制每个先前的频率。这是一个简单的例子:

    ArrayList<PVector> previousPoints = new ArrayList<PVector>();
    
    void draw(){
      background(32);
    
      previousPoints.add(new PVector(mouseX, mouseY));
    
      for(PVector point : previousPoints){
        ellipse(point.x, point.y, 20, 20);
      }
    }
    

    这个示例程序存储了以前的鼠标位置,并在ArrayList 上循环以绘制它们。你想对你的频率做类似的事情。

    【讨论】:

    • 您好,感谢您的回复,我看看您的示例,看看我如何在我的代码中实现,再次感谢 :)
    猜你喜欢
    • 2015-01-20
    • 2016-02-23
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2011-09-26
    • 2023-04-08
    • 2011-08-20
    • 2015-04-12
    相关资源
    最近更新 更多