ProcessingDMXTests/audio_reactive_3light/audio_reactive_3light.pde
2023-10-04 01:40:58 -04:00

54 lines
1.2 KiB
Plaintext

import processing.sound.*;
import processing.serial.*;
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);
arduino.write("1c" + val + "w");
} else if (i == 1) {
int val = (int)map(spectrum[i], 0, 1, 0, 255);
fill(0, 255, 0);
arduino.write("5c" + val*2 + "w");
} else if (i == 2) {
int val = (int)map(spectrum[i], 0, 1, 0, 255);
fill(0, 0, 255);
arduino.write("9c" + val*3 + "w");
}
rect(i * 200, 0, 200, spectrum[i] * height);
}
}