根据声音改变亮度(处理中) [英] Changing brightness depending on sound (Processing)
问题描述
我现在正在学习处理,我正在尝试制作一个草图,当声音改变时,它可能会改变颜色.
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+ )
因为更改颜色不需要像draw()函数一样快地进行更改.那么,我该如何构建一个时钟,以使颜色不会在每次绘制时都发生变化?
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);
}
}
推荐答案
在frameRate中信任以控制定时是可以的,但是当然取决于frameRate.这意味着,如果您的frameRate下降,则timimg也将下降.
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.
为避免这种情况,您可以使用 millis()
并将计时附加到时间上:)
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);
}
这篇关于根据声音改变亮度(处理中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!