处理:如何仅每x帧绘制一次? [英] Processing: How can I draw only every x frames?

查看:96
本文介绍了处理:如何仅每x帧绘制一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码:

//3D Spectrogram with Microphone Input
//Modified by kylejanzen 2011 - https://kylejanzen.wordpress.com
//Based on script written by John Locke 2011 - http://gracefulspoon.com

//Output .DXF file at any time by pressing "r" on the keyboard

import processing.dxf.*;
import ddf.minim.analysis.*;
import ddf.minim.*;
import peasy.*;

PeasyCam cam;

FFT fftLin;
FFT fftLog;

Waveform audio3D;

Minim minim;
AudioInput microphone;

boolean record;

PFont font;

float camzoom;
float maxX = 0;float maxY = 0;float maxZ = 0;
float minX = 0;float minY = 0;float minZ = 0;

void setup()
{
  size(1250,750,P3D); //screen proportions
  noStroke();
  minim = new Minim(this);
  microphone = minim.getLineIn(Minim.STEREO, 4096); //repeat the song

  cam = new PeasyCam(this, 0, 0, 0, 50);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(500);

  background(255);

  fftLog = new FFT(microphone.bufferSize(),microphone.sampleRate());
  fftLog.logAverages(1,2);  //adjust numbers to adjust spacing;
  float w = float (width/fftLog.avgSize());
  print(fftLog.avgSize());
  float x = w;
  float y = 0;
  float z = 10;
  float radius = 100;
  audio3D = new Waveform(x,y,z,radius);
}
void draw()

{
  background(0);
  // ambientLight(102,102,102);

  if (frameCount>0)
  {
    for(int i = 0; i < fftLog.avgSize(); i++){
      float zoom = 1;
      float jitter = (fftLog.getAvg(i)*2);
      //println(jitter);
      PVector foc = new PVector(audio3D.x+jitter, audio3D.y+jitter, 0);
      PVector cam = new PVector(zoom, zoom, -zoom);
      // camera(foc.x+cam.x+50,foc.y+cam.y+50,foc.z+cam.z+100,foc.x+30,foc.y+30,foc.z+100,0,0,1);
    }
  }
  //play the song
  fftLog.forward(microphone.mix);

  audio3D.update();
  // audio3D.textdraw();

  if(record)
  {
    beginRaw(DXF, "output.dxf");
  }
  audio3D.plotTrace();

  if(record)
  {
    endRaw();
    record = false;
    println("It's Done Bitches! Find your DXF!");
  }
}
void stop()
{
  // always close Minim audio classes when you finish with them
  microphone.close();
  // always stop Minim before exiting
  minim.stop();
  super.stop();
}
class Waveform
{
  float x,y,z;
  float radius;

  PVector[] pts = new PVector[fftLog.avgSize()];

  PVector[] trace = new PVector[0];

  Waveform(float incomingX, float incomingY, float incomingZ, float incomingRadius)
  {
    x = incomingX;
    y = incomingY;
    z = incomingZ;
    radius = incomingRadius;
  }
  void update()
  {
    plot();
  }
  void plot()
  {
    for(int i = 1; i < fftLog.avgSize(); i++)
    {
      int w = int(width/fftLog.avgSize());

      x = i*w;
      y = frameCount*5; // CHANGE THE SPEED
      z = height/4-fftLog.getAvg(i)*4; //change multiplier to reduces height default '10'

      stroke(0);
      point(x, y, z);
      pts[i] = new PVector(x, y, z);
      //increase size of array trace by length+1
      trace = (PVector[]) expand(trace, trace.length+1);
      //always get the next to last
      trace[trace.length-1] = new PVector(pts[i].x, pts[i].y, pts[i].z);
    }
  }
  /* void textdraw()
  {
    for(int i =0; i<fftLog.avgSize(); i++){
      pushMatrix();
      translate(pts[i].x, pts[i].y, pts[i].z);
      rotateY(PI/2);
      rotateZ(PI/2);

      fill(255,255);
      text(round(fftLog.getAvg(i)*100),0,0,0);
      popMatrix();
    }
  } */
  void plotTrace()
  {
    stroke(255,100);
    int inc = fftLog.avgSize();

    for(int i=1; i<trace.length-inc; i++)
    {
      if(i%inc != 0)
      {
        beginShape(POINTS);
        strokeWeight(2);
        fill(0, 0, 0, 100);
        vertex(trace[i].x, trace[i].y, trace[i].z);
        vertex(trace[i-1].x, trace[i-1].y, trace[i-1].z);
        vertex(trace[i+inc].x, trace[i+inc].y, trace[i+inc].z);
        vertex(trace[i-1+inc].x, trace[i-1+inc].y, trace[i-1+inc].z);
        endShape(CLOSE);
      }
    }
  }
}
void keyPressed()
{
  if (key == 'r') record = true;
}

我目前试图实现的目标是减少(移动)(y-)轴上生成点的数量.似乎每一帧都产生一个点. 因此,我的问题非常简单:如何让它仅绘制例如每5帧?我只是找不到管理它的价值.

What I currently try to achieve is to reduce the count of generated points on the (moving) (y-)axis. It seems that every frame, generates a point. My question therefore is fairly simple: How can I let it draw only e.g. every 5 frames? I just can't find the value, which manages that.

非常感谢.

推荐答案

您有三个选择:

选项1:调用 frameRate() 函数减少每秒绘制的帧数.

Option 1: Call the frameRate() function to reduce the number of frames that get drawn per second.

void setup(){
  size(500, 500);
  frameRate(5);
}

void draw(){
  background(0);
  ellipse(mouseX, mouseY, 20, 20);
}

选项2:使用 frameCount 变量和 modulus % operator 来确定X帧何时经过.

Option 2: Use the frameCount variable and the modulus % operator to determine when X frames have elapsed.

void setup(){
  size(500, 500);
}

void draw(){
  if(frameCount % 5 == 0){
    background(0);
    ellipse(mouseX, mouseY, 20, 20);
  }
}

选项3:.您可以创建自己的变量,用于存储已过去的帧数.

Option 3: You could create your own variable that stores the number of frames that have elapsed.

int framesElapsed = 0;

void setup(){
  size(500, 500);
}

void draw(){
  framesElapsed++;

  if(framesElapsed == 5){
    background(0);
    ellipse(mouseX, mouseY, 20, 20);
    framesElapsed = 0;
  }
}

请注意,对于简单的情况,这只是在执行选项2中的模运算符.在这种情况下,模量可能更好.但是,如果您希望例如在不同时间发生不同的事情,这将很有用.在这种情况下,您将有多个变量来跟踪您要跟踪的对象的生命周期".

Note that for the simple case, this is just doing what the modulus operator in option 2 is doing. In that case, modulus is probably better. But this becomes useful if you want, for example, different things to happen at different times. In that case you'd have multiple variables keeping track of the "lifetime" of whatever you want to track.

但是对于您的示例来说,选项2 可能是最好的选择.

But for your example, option 2 is probably the best option.

这篇关于处理:如何仅每x帧绘制一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆