How to use PC sounds to FFT for LEDs control with arduino?

edited December 2015 in Arduino

Hello, i'm new to Processing and all of this.

I have been trying to make some LEDs react to sound. At the begining i started to use mp3 files and i got it to work, now i want to use real time sound coming from the PC to control those LEDs. So i used AudioInput instead of AudioPlayer and when i try to run it gives me the following error:

`==== JavaSound Minim Error ==== ====Unable to return TargetDataline: unsupported format - PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian

=== Minim Error === === Minim.getLineIn: attempt failed, could not secure an AudioInput. `

After some search i tried to enable Stereo Mix through the Sound options of Windows, and the error persists. Then for no much reason i connected my headphones to the front audio jack of my PC and hit run again, no errors were shown and everything was working as expected (besides the fact that i had to scale all the frequency bands so i didn't have to have the volume almost at max). But when i disconnect the headphones the LEDs stop reacting, and if i restart the program the errors are showed again.

Can anyone help me with this? Thank your very much.

P.S.: Don't know if it matters, but i have on my PC a Realtek integrated sound card a Nvidea graphics card connected with a HDMI cable to an Asus LCD monitor which has integrated speakers (so the sound comes from the monitor). The code i'm using is the following;

`import ddf.minim.*;
import ddf.minim.analysis.*; import processing.serial.*; import cc.arduino.*;

Arduino arduino;

Minim minim; AudioInput sound; FFT fft;

int redPin1 = 8; int greenPin1 = 7; int bluePin1 = 6;

int redPin2 = 2; int greenPin2 = 3; int bluePin2 = 4;

int redPin3 = 11; int greenPin3 = 12; int bluePin3 = 13;

int color_id = 0;

int common_cathode = 1;

void setup() { size(800, 600);

arduino = new Arduino(this, Arduino.list()[1], 57600);
for (int i = 0; i <= 13; i++) arduino.pinMode(i, Arduino.OUTPUT);
for (int i = 0; i <= 13; i++) arduino.digitalWrite(i,arduino.HIGH);

minim = new Minim(this);
sound = minim.getLineIn();
fft = new FFT(sound.bufferSize(), sound.sampleRate());

}

void draw() {
background(#151515);

fft.forward(sound.mix);

for (int i = 0; i < 513; i++){
  fft.scaleBand(i, 15);
}

strokeWeight(1.3);
stroke(#FFF700);

// frequency
pushMatrix();
  translate(250, 0);

  for(int i = 0; i < 0+fft.specSize(); i++) {
    line(i, height*4/5, i, height*4/5 - fft.getBand(i)*4); 
    if(i%100==0) text(fft.getBand(i), i, height*4/5+20);
    if(i==200) {
      if(fft.getBand(i)>2) {
        setColor1(255,255,0);
        setColor3(255,255,0);
      }
      else if(fft.getBand(i)>1) {
        setColor1(255,0,255);
        setColor3(255,0,255);
      } else {
        setColor1(255,255,255);
        setColor3(255,255,255);
      }
    }
    if(i==50) {
      if(fft.getBand(i)>5) {
        color_id = (color_id+1)%4;
      } else if(fft.getBand(i)>3) {
        if(color_id==0) setColor2(0,255,0);
        else if(color_id==1) setColor2(0,255,255);
        else if(color_id==2) setColor2(0,0,255);
        else setColor2(255,0,0);
      } else if (fft.getBand(i) > 1){
       setColor1(0,255,255);
       setColor3(0,255,255);
      }
      else {
        setColor2(255,255,255);
      }
    } 
  }  
popMatrix();

stroke(#FF0000);

//waveform
for(int i = 250; i < sound.left.size() - 1; i++) {
  line(i, 50 + sound.left.get(i)*50, i+1, 50 + sound.left.get(i+1)*50);
  line(i, 150 + sound.right.get(i)*50, i+1, 150 + sound.right.get(i+1)*50);
  line(i, 250 + sound.mix.get(i)*50, i+1, 250 + sound.mix.get(i+1)*50);
}

noStroke();
fill(#111111);
rect(0, 0, 250, height);

textSize(24);
fill(#046700);
text("left amplitude", 10, 50); 
text("right amplitude", 10, 150); 
text("mixed amplitude", 10, 250); 
text("frequency", 10, height*4/5); 

}

void stop() { for (int i = 0; i <= 13; i++) arduino.digitalWrite(i,arduino.HIGH); sound.close();
minim.stop(); super.stop(); } void setColor1(int red, int green, int blue) { if(common_cathode==1) { red = 255-red; green = 255-green; blue = 255-blue; } arduino.digitalWrite(redPin1, red); arduino.digitalWrite(greenPin1, green); arduino.digitalWrite(bluePin1, blue);
} void setColor2(int red, int green, int blue) { if(common_cathode==1) { red = 255-red; green = 255-green; blue = 255-blue; } arduino.digitalWrite(redPin2, red); arduino.digitalWrite(greenPin2, green); arduino.digitalWrite(bluePin2, blue);
} void setColor3(int red, int green, int blue) { if(common_cathode==1) { red = 255-red; green = 255-green; blue = 255-blue; } arduino.digitalWrite(redPin3, red); arduino.digitalWrite(greenPin3, green); arduino.digitalWrite(bluePin3, blue);
}`

Sign In or Register to comment.