66 lines
1.6 KiB
Plaintext
66 lines
1.6 KiB
Plaintext
import processing.sound.*;
|
|
import processing.serial.*;
|
|
|
|
int NUM_LIGHTS = 3;
|
|
|
|
FFT fft;
|
|
AudioIn in;
|
|
int bands = 4;
|
|
float[] spectrum = new float[bands];
|
|
|
|
Serial arduino;
|
|
|
|
void setup() {
|
|
size(600, 300);
|
|
background(255);
|
|
|
|
frameRate(14);
|
|
// Create an Input stream which is routed into the Amplitude analyzer
|
|
fft = new FFT(this, bands);
|
|
in = new AudioIn(this, 0);
|
|
|
|
// start the Audio Input
|
|
in.start();
|
|
|
|
// patch the AudioIn
|
|
fft.input(in);
|
|
|
|
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
|
|
arduino = new Serial(this, portName, 9600);
|
|
}
|
|
|
|
void draw() {
|
|
background(255);
|
|
fft.analyze(spectrum);
|
|
|
|
for (int i = 0; i < bands; i++) {
|
|
// The result of the FFT is normalized
|
|
// draw the line for frequency band i scaling it up by 5 to get more amplitude.
|
|
if (i == 0) {
|
|
fill(255, 0, 0);
|
|
int val = (int)map(spectrum[i], 0, 1, 0, 255);
|
|
for (int k = 0; k < NUM_LIGHTS; k++){
|
|
int channel = 1 + (k * 3);
|
|
arduino.write(channel+"c" + val + "w");
|
|
}
|
|
|
|
} else if (i == 1) {
|
|
int val = (int)map(spectrum[i], 0, 1, 0, 255);
|
|
val = val*2;
|
|
fill(0, 255, 0);
|
|
for (int k = 0; k < NUM_LIGHTS; k++){
|
|
int channel = 2 + (k * 3);
|
|
arduino.write(channel+"c" + val + "w");
|
|
}
|
|
} else if (i == 2) {
|
|
int val = (int)map(spectrum[i], 0, 1, 0, 255);
|
|
fill(0, 0, 255);
|
|
for (int k = 0; k < NUM_LIGHTS; k++){
|
|
int channel = 3 + (k * 3);
|
|
arduino.write(channel+"c" + val + "w");
|
|
}
|
|
}
|
|
rect(i * 200, 0, 200, spectrum[i] * height);
|
|
}
|
|
}
|