135 lines
3.2 KiB
Plaintext
135 lines
3.2 KiB
Plaintext
// 大小幅度我之後可以調教?
|
|
|
|
int num = 32;
|
|
Walker[] walkers = new Walker[num];
|
|
import processing.sound.*;
|
|
|
|
// Declare the processing sound variables
|
|
SoundFile sample;
|
|
Amplitude rms;
|
|
|
|
// Declare a smooth factor to smooth out sudden changes in amplitude.
|
|
// With a smooth factor of 1, only the last measured amplitude is used for the
|
|
// visualisation, which can lead to very abrupt changes. As you decrease the
|
|
// smooth factor towards 0, the measured amplitudes are averaged across frames,
|
|
// leading to more pleasant gradual changes
|
|
float smoothingFactor = 0.25;
|
|
|
|
// Used for storing the smoothed amplitude value
|
|
float sum;
|
|
|
|
FFT fft;
|
|
int bands = 32;
|
|
float[] fft_sum = new float[bands];
|
|
// Variables for drawing the spectrum:
|
|
// Declare a scaling factor for adjusting the height of the rectangles
|
|
int scale = 5;
|
|
// Declare a drawing variable for calculating the width of the
|
|
float barWidth;
|
|
|
|
void setup() {
|
|
//fullScreen();
|
|
size(600, 600);
|
|
background(255);
|
|
noStroke();
|
|
textSize(30);
|
|
for (int i = 0; i < num; i++)
|
|
{walkers[i] = new Walker(i);}
|
|
|
|
|
|
//Load and play a soundfile and loop it
|
|
sample = new SoundFile(this, "song.aiff");
|
|
sample.play(1,1);
|
|
//sample.loop();
|
|
|
|
//Create and patch the rms tracker
|
|
rms = new Amplitude(this);
|
|
rms.input(sample);
|
|
|
|
|
|
barWidth = width / float(bands);
|
|
fft = new FFT(this, bands);
|
|
fft.input(sample);
|
|
|
|
}
|
|
|
|
|
|
void draw() {
|
|
fill(255, 50); // fading
|
|
rect(0, 0, width, height);
|
|
|
|
noStroke();
|
|
fill(255, 0, 150);
|
|
//smooth the rms data by smoothing factor
|
|
sum += (rms.analyze() - sum) * smoothingFactor;
|
|
|
|
//rms.analyze() return a value between 0 and 1. It's
|
|
//scaled to height/2 and then multiplied by a fixed scale factor
|
|
//float rms_scaled = sum * (height/2) * 5;
|
|
//ellipse(width/2, height/2, rms_scaled, rms_scaled);
|
|
|
|
fft.analyze();
|
|
for (int i = 0; i < bands; i++) {
|
|
// Smooth the FFT spectrum data by smoothing factor
|
|
fft_sum[i] += (fft.spectrum[i] - fft_sum[i]) * smoothingFactor;
|
|
|
|
// Draw the rectangles, adjust their height using the scale factor
|
|
// rect(i*barWidth, height, barWidth, -fft_sum[i]*height*scale);
|
|
}
|
|
|
|
for (int i = 0; i < num; i++)
|
|
walkers[i].update(Math.max(int(fft_sum[i] * height * scale * 2), 30));
|
|
|
|
}
|
|
|
|
class Walker {
|
|
//fields
|
|
String s;
|
|
float x,y, a;
|
|
float tx, ty, ta;
|
|
color c;
|
|
|
|
//constructor
|
|
Walker(int i) {
|
|
s = nf(i);
|
|
x = random(width);
|
|
y = random(height);
|
|
tx = random(1000);
|
|
ty = random(1000);
|
|
ta = random(1000);
|
|
c = color(random(255),random(255),random(255));
|
|
}
|
|
|
|
//method
|
|
void update(int text_size) {
|
|
pushMatrix();
|
|
translate(x,y);
|
|
rotate(a);
|
|
fill(c, 50);
|
|
text(s, 0, 0);
|
|
textSize(text_size);
|
|
popMatrix();
|
|
|
|
tx += 0.01;
|
|
ty += 0.01;
|
|
ta += 0.001;
|
|
|
|
x += noise(tx) * 4 - 2;
|
|
y += noise(ty) * 4 - 2;
|
|
a += noise(ta) * 0.1 - 0.05;
|
|
|
|
if (x < 0) {
|
|
x += width;
|
|
}
|
|
else if (x > width) {
|
|
x -= width;
|
|
}
|
|
if (y < 0) {
|
|
y += height;
|
|
}
|
|
else if (y > height) {
|
|
y -= height;
|
|
}
|
|
}
|
|
}
|