I am working on a project to get a string of lights to flash to music when the bass hits primarily. I have the hardware pretty much set, but I am using minim's audio in function to create an fft and look at the low frequencies and display the intensity as brightness to leds. But as play songs, I am getting a noticeable delay between a bass hit and the flash of the led.
Does anyone have any ideas where I could optimize the code? I'm thinking it might be something like fft sample rate, or maybe the line in buffer.
int xmax = 600; //window width int ymax = 300;//window height
void setup() { size(xmax, ymax); minim = new Minim(this);
//line in code song = minim.getLineIn(Minim.STEREO, 1024); arduino = new Arduino(this, Arduino.list()[0], 57600);
//create graph fft = new FFT(song.bufferSize(), 48000); }
void draw() { background(0); fft.forward(song.mix); stroke(127, 255, 0, 200); //I set the color of my lines for(int i = 10; i < fft.specSize(); i++){ line(i, height-30, i, height - (30+fft.getFreq(i*10))); // I create lines that represent the amplitude // of each frequency. text("Min Freq", 10, height-10); text("Max Freq", fft.specSize(), height-10); } ledcontrol(); //I call the function for the arduino }
void ledcontrol(){ //h is the intensity cutoff int h =90;
//if the bass is below h, the lights are on low //peaks above the cutoff will display normally if(int(fft.getFreq(30))< h){ arduino.analogWrite (11, 10); } else{ arduino.analogWrite(11, int(fft.getFreq(30))); println(fft.getFreq(30)); }