Changing brightness depending on sound (Processing)

房东的猫 提交于 2021-02-08 06:14:33

问题


I am learning processing right now and I am trying to make a sketch that could change colour when the sound changes.

(When Amplitude + , Then Brightness+ )

Because changing colour does not need to change as rapid as the draw() function. So how could I build a clock so that the color would not change in every draw?

This is the code I am using right now:

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer song;
FFT fft; 
BeatDetect beat;

color start=color(0,0,0);
color finish;
float amt = 0.0;

void setup()
{
  frameRate(50);
  size(600,600,P3D);    
  minim = new Minim(this);
  song = minim.loadFile("song2.mp3", 512);
  song.loop();

  fft = new FFT(song.bufferSize(), song.sampleRate());

   beat = new BeatDetect(song.bufferSize(), song.sampleRate());

}

// draw is run many times
void draw()
{  
  float brightness = map( 0, 0, song.bufferSize(), 0, 255 ); 
  background( brightness );
  // println(song.bufferSize());

  stroke(100);

  // draw the waveforms
  for( int i = 0; i < song.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, song.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, song.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + song.left.get(i)*50, x2, 50 + song.left.get(i+1)*50);
    line( x1, 150 + song.right.get(i)*50, x2, 150 + song.right.get(i+1)*50);
  println(x1);
  }

}


回答1:


When you call frameRate(50); you are telling Processing to (try to) update the draw() 50 times a second. You can tell how many frames have passed since the start of the sketch by checking the built-in variable frameCount.

This can then be divided by a number which represents how many frames you want to draw before doing something special - I would use modulus for this, it will divide the numbers and return the remainder. If it equals 0, then that number of frames have passed.

    int updateTriggerCount = 10;        
    void setup() {
    ...        
    }

    void draw()
    {
       if((frameCount % updateTriggerCount) == 0)
       {
            // Another 10 frames have passed! Do something special
       }
       ....
    }



回答2:


Trusting in frameRate to control timing is ok, but is, of course, frameRate dependent. Meaning that if your frameRate drops the timimg will drop together.

To avoid that, you may use millis() and attach your timing to, well time :)

Here a very simple timer example:

PFont font;
String time = "000";
int initialTime;
int interval = 1000;//one second
color bg = color (255);


void setup()
{
  size(300, 300);
  font = createFont("Arial", 30);
  background(255);
  fill(0);
  initialTime = millis();
  frameRate(30);// changed framerate to exemplify
}

void draw()
{
  background(bg);
  if (millis() - initialTime > interval)
  {
    time = nf(int(millis()/1000), 3);
    initialTime = millis();
   bg = color (random(255), random(100), random(255));
  }

  text(time, width/2, height/2);


}


来源:https://stackoverflow.com/questions/31661302/changing-brightness-depending-on-sound-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!