Está en la página 1de 13

import processing.serial.*; import cc.arduino.*; import ddf.minim.*; import ddf.minim.signals.*; import ddf.minim.analysis.*; import ddf.minim.effects.

*;

Serial port; Arduino arduino; AudioInput in; Minim minim; FFT fftLin;

float vectormedias[]=new float[6]; // float magnitud=4; float volumen; int volcaptado;

// DECLARACIN DE CANCIONES

AudioPlayer songAmarilloUno; AudioPlayer songAmarilloDos; AudioPlayer songAmarilloTres;

AudioPlayer songAzulUno; AudioPlayer songAzulDos; AudioPlayer songAzulTres;

AudioPlayer songRojoUno; AudioPlayer songRojoDos; AudioPlayer songRojoTres;

// DECLARACIN DE PINES

int amarilloUno=2; int amarilloDos=3; int amarilloTres=4; int azulUno=5; int azulDos=6; int azulTres=7; int rojoUno=8; int rojoDos=9; int rojoTres=10;

int contador1=0; int contador2=0; int contador3=0;

void setup(){

println(Serial.list()); if (port==null) port = new Serial(this, Serial.list()[1], 9600); size(512, 700, P3D); volumen=1;

arduino=new Arduino(this,Arduino.list()[2],57600); minim= new Minim(this); in = minim.getLineIn(Minim.STEREO, 2048); fftLin = new FFT(in.bufferSize(), in.sampleRate()); fftLin.linAverages(240); rectMode(CORNERS);

// INDICAMOS QUE TODOS LOS PINES SON DE ENTRADA

arduino.pinMode(amarilloUno,Arduino.INPUT); arduino.pinMode(amarilloDos,Arduino.INPUT); arduino.pinMode(amarilloTres,Arduino.INPUT);

arduino.pinMode(azulUno,Arduino.INPUT); arduino.pinMode(azulDos,Arduino.INPUT);

arduino.pinMode(azulTres,Arduino.INPUT);

arduino.pinMode(rojoUno,Arduino.INPUT); arduino.pinMode(rojoDos,Arduino.INPUT); arduino.pinMode(rojoTres,Arduino.INPUT);

// INICIALIZAMOS LAS CANCIONES

songAmarilloUno = minim.loadFile("1.mp3"); songAmarilloDos = minim.loadFile("2.mp3"); songAmarilloTres = minim.loadFile("3.mp3");

songAzulUno = minim.loadFile("4.mp3"); songAzulDos = minim.loadFile("5.mp3"); songAzulTres = minim.loadFile("6.mp3");

songRojoUno = minim.loadFile("7.mp3"); songRojoDos = minim.loadFile("8.mp3"); songRojoTres = minim.loadFile("9.mp3");

void draw(){

//ECUALIZADOR

background(0); fftLin.forward(in.mix);

noStroke(); //fill(255); int w = int(width/6);

for(int i = 0; i < 120; i++) { if (i<=1) { if (vectormedias[0]<2*fftLin.getAvg(i)) vectormedias[0]=2*fftLin.getAvg(i); fill(255,0,0); // Each range has a multiplier, to adjust the visual response. This is based on tests, feel free to change it. // I changed them a lot, feel free to change what bars are included too. Some will be OK for certain music, or not. } else if (i<=5) { if (vectormedias[1]<1.2*fftLin.getAvg(i)) vectormedias[1]=1.2*fftLin.getAvg(i);

fill(255,0,0); } else if (i<=12) { if (vectormedias[2]<1.6*fftLin.getAvg(i)) vectormedias[2]=1.6*fftLin.getAvg(i); fill(0,0,255); } else if (i<=22) { if (vectormedias[3]<1.75*fftLin.getAvg(i)) vectormedias[3]=1.75*fftLin.getAvg(i); fill(0,0,255); } else if (i<=55) { if (vectormedias[4]<2*fftLin.getAvg(i)) vectormedias[4]=2*fftLin.getAvg(i); fill(255,255,0); } if (i<=160) { //This is the last one. It is multiplied by the bar index, to give more weight to the final frequencies (that tend to be lower always). //This gives good feedback for noise, high pitch sounds, or percusion instruments. vectormedias[5]=i*fftLin.getAvg(i)/300+vectormedias[5]; fill(255,255,0); }

if (i==119) { for (byte k=0;k<6;k++) { delay(10); //The following number is important. It is the level that is taken as real input, if you catch noise //you will have to make it a bit bigger. If you don't have any noise, perhaps a lower number is better if (in.mix.level()>0.001) { /*This keeps the volume changing with the in.mix level. However, we don't want it to change very fast, because in.mix.level is very variable. Otherwise, you'll see low sounds as big as high sounds. And we don't want it to be constant, otherwise when the volume is low you wont see anything. Feel free to change the values to change faster or slower though (keeping the sum as 1.00, don't mind the *10)*/ volumen=volumen*0.998+in.mix.level()*0.002*10; // the *10 is cause the in.mix.level is usually 0.1. I wanted it to be closer to 1. //println(in.mix.level()); //uncomment this if you want to try to change the volume control volcaptado=int(magnitud*vectormedias[k]/volumen); //This is the last step. It gives the amount of volume to the graphic bars and the arduino. if (volcaptado>300) volcaptado=300; //we don't want it to be VERY big. volcaptado=volcaptado/10; //the arduino will get a value between 0 and 30 as the volume input. A same, but it is necessary as we'll need 3 bits to select the LED, //and we have 5 more (up to 31). I could have used 0 to 31, but I was lazy.

port.write((k<<5)|byte(volcaptado)); //This gives the arduino wich led it is with K, and the value of volcaptado. // it works like this: XXX | XXXXX // first the number of the LED, and last the amount of volume. The serial port only send one byte (eight bits) so it has to be like that. // Another option would be giving a certain number to sync arduino and processing, like 0, v1, v2, v3, v4, v5, v6 , (and repeat). // However, the LED's change so fast that with 30 values I found it enough. Feel free to change it, though. rect(k*w, height, k*w + w, height - magnitud*vectormedias[k]/volumen); //and this draws the rectangles in your processing window If you don't want this feedback, just comment the line above. } else{ //We get here if the in.mix.level is very low. So no sound is received. You'll receive noise only. volumen=0.4; //We put a low volume as a base (so when a sound comes in, the LED will bright more than usual) port.write((k<<5)); // We tell the arduino to put all LED's with a volume of 0. delay(100); // change the delay if you want, or even delete it. }

vectormedias[k]=0; // we delete the previous maximums, to make new ones in the next iteration.

//BOTON AMARILLO 1

if ((arduino.digitalRead(amarilloUno)==Arduino.HIGH)){

contador1++; delay(200); if (contador1%2==1){ songAmarilloUno = minim.loadFile("1.mp3"); songAmarilloUno.play(); } else { songAmarilloUno.close(); } }

//BOTON AMARILLO 2

if ((arduino.digitalRead(amarilloDos)==Arduino.HIGH)){ contador2++; delay(200);

if (contador2%2==1){ songAmarilloDos = minim.loadFile("2.mp3"); songAmarilloDos.play(); } else { songAmarilloDos.close(); } }

//BOTON AMARILLO 3

if ((arduino.digitalRead(amarilloTres)==Arduino.HIGH)){ contador3++; delay(200); if (contador3%2==1){ songAmarilloTres = minim.loadFile("3.mp3"); songAmarilloTres.play(); } else { songAmarilloTres.close(); } }

// BOTON AZUL 1

if ((arduino.digitalRead(azulUno)==Arduino.HIGH)){ songAzulUno.close(); delay(5); songAzulUno = minim.loadFile("4.mp3"); songAzulUno.play(); }

// BOTON AZUL 2

if ((arduino.digitalRead(azulDos)==Arduino.HIGH)){ songAzulDos.close(); delay(10); songAzulDos = minim.loadFile("5.mp3"); songAzulDos.play(); }

// BOTON AZUL 3

if ((arduino.digitalRead(azulTres)==Arduino.HIGH)){ songAzulTres.close(); delay(15); songAzulTres = minim.loadFile("6.mp3");

songAzulTres.play(); }

// BOTON ROJO 1

if ((arduino.digitalRead(rojoUno)==Arduino.HIGH)){ songRojoUno.close(); delay(15); songRojoUno = minim.loadFile("7.mp3"); songRojoUno.play(); }

// BOTON ROJO 2

if ((arduino.digitalRead(rojoDos)==Arduino.HIGH)){ songRojoDos.close(); delay(15); songRojoDos = minim.loadFile("8.mp3"); songRojoDos.play(); }

// BOTON ROJO 3

if ((arduino.digitalRead(rojoTres)==Arduino.HIGH)){ songRojoTres.close(); delay(15); songRojoTres = minim.loadFile("9.mp3"); songRojoTres.play(); }

void stop() { // always close Minim audio classes when you are done with them in.close(); // always stop Minim before exiting minim.stop();

super.stop(); // send led=0 before exiting port.write(0); }

También podría gustarte