??// Setting for the Serial Baud Rate Box ??private void BaudComboBox(int x, int y) ??{ ?? …… ??} ?? ????// Setting for the Serial Port Box ??// If an Arduino board is communicating with Processing, the Serial Port Box will detect the Serial Port ???// automatically in this function. ???private void DetectDevices(int x, int y) ??{ ?? …… }
??MusicBox(370, 60); // Set the size of the music box in Setup () ?????? ??????loadFilenames(); // Load the names of the songs in the music list ?? ??// Freshing action to make the music box play songs in the music list one by one; ????//Also, if the last song in the music list is finished, the music box ???//will start to play from the first song again freshMusic();
?????// Setting for the Music Box ??public void MusicBox(int x, int y) ??{ ?? …… ??????}
???// Freshing action to make the music box play songs in the music list one by one ???void freshMusic() ???{ ?? …… ???} ???// Load the names of the songs in the music list ???void loadFilenames() ???{ ?? …… ???} ??? ???/** This class implements the FilenameFilter interface. ??? * The Accept method returns true for .java, .class and .jar files. ?????? */ ??????class OnlyJava implements FilenameFilter ???{ ??? …… ???} ??? ???//The ActionListener can make the music box play the song you select from the list ???public class MusicListEvent implements ActionListener ???{ ??? public void actionPerformed(ActionEvent e) { ??? JComboBox cb = (JComboBox)e.getSource(); ??? String MusicName = (String)cb.getSelectedItem(); ??? PlayingFileName = MusicName; ??? //updateLabel(petName); ??? Player.close(); ??? //minim.stop(); ??? //super.stop(); ??? println(MusicName); ??? freshMusic(); ??? ??? for(i=0;i
c) Display the information of a song which is playing
Variable declaration:
Minim minim;
AudioMetaData meta;
Function:
?? void freshMusic() ?????{ ????? …… ??????try { ????? …… ????? meta = Player.getMetaData(); ????? …… ????? } ??}
Draw ():
????//Display the information of a song which is playing ???? //You can set the following information of the songs in “Properties” ???? textFont(megaFont); text("File Name : " + meta.fileName(), 5, 25); //text("Length (in milliseconds): " + meta.length(), 5, 40); text("Length : " + meta.length()/60000+ ":" + meta.length()%60000/1000, 5, 45); text("Title : " + meta.title(), 5, 65); text("Author : " + meta.author(), 5, 85); text("Album : " + meta.album(), 5, 105);
?? void freshMusic() ?????{ ????? …… ??????try { ????? …… ????? meta = Player.getMetaData(); ????? …… ????? } ??}
Draw ():
???? //Display the information of a song which is playing ???? //You can set the following information of the songs in “Properties” ???? textFont(megaFont); text("File Name : " + meta.fileName(), 5, 25); //text("Length (in milliseconds): " + meta.length(), 5, 40); text("Length : " + meta.length()/60000+ ":" + meta.length()%60000/1000, 5, 45); text("Title : " + meta.title(), 5, 65); text("Author : " + meta.author(), 5, 85); text("Album : " + meta.album(), 5, 105);
?? void freshMusic() ?????{ ????? …… ??????try { ????? …… ????? fft = new FFT(Player.bufferSize(), Player.sampleRate()); ????? fft.linAverages(100); ????? …… ????? } ?????}
Draw ():
???? float centerFrequency = 0; ???? try { ???? fft.forward(Player.mix); ???? ???? int w = int(fft.specSize()/100); ???? for (int i = 0; i < fft.avgSize(); i++) ???? { ???? if ( mouseX >= i*w && mouseX < i*w + w ) ???? { ???? centerFrequency = fft.getAverageCenterFrequency(i); ???? textFont(font); ???? textSize( 18 ); ???? fill(27, 128); ???? text("Linear Average Center Frequency: " + centerFrequency, 27, height45 - 110); ???? text("Frequency:" + fft.getAvg(i)*3, 27, height45 - 150); ???? fill(255, 64, 64); ???? } ???? else ???? { ???? if (i == GetWave[0] || i == GetWave[1] || i == GetWave[2]) ???? { ???? //print("HeightVal:" + fft.getAvg(i) * 3 + " "); ???? fill(154,192,205); ???? }else{ ???? fill(27); ???? } ???? } ???? noStroke(); ???? rect(i*w, height45, i*w + w/2, height45 - fft.getAvg(i)*spectrumScale); ???? } ???? ???? //localcolor[0]&localcolor[1]&localcolor[3]are used in CommWriter() ???? //These three values are the music signals sent to Arduino ???? localcolor[0] = constrain(int(fft.getAvg(GetWave[0]) *3), 0, 255); ???? localcolor[1] = constrain(int(fft.getAvg(GetWave[1]) * 3), 0, 255); ???? localcolor[2] = constrain(int(fft.getAvg(GetWave[2]) * 3), 0, 255); ???? print(localcolor[0]); ???? print(" "); ???? print(localcolor[1]); ???? print(" "); ???? println(localcolor[2]); ???? } ???? catch(Throwable err) ???? { ???? println("Play Music Error!"); ???? };
??void freshMusic() ?????{ ????? …… ??????try { ????? …… ????? clock.reset(); ????? …… ????? } ??}
??timer.setValue(clock.toString()); ?? timer.draw(this); ?? timer.setPosition(450,275);
???? try { ????? …… ???????//The volume slider can adjust the value of gain ???????setVol=Volume-35; ??????Player .setGain(setVol); ???????…… ???? }
Make Processing send music signal to Arduino
??// Switching between the buttons: ??//playButton/playoverButton/stopButton/stopoverButton ??//Response to the mousePressed() function ??boolean StartState = false; ??boolean ButtonRegion = false; ??????public void ButtonImage(int x, int y) ??????{ ??????if (mouseX > x && mouseX < (x + PicSizeX) && mouseY > y && mouseY < (y + PicSizeY)) { ??????ButtonRegion = true; ??????} else{ ?????? ButtonRegion = false; ??????} ?????? if (StartState) ?????? { ?????? if (ButtonRegion) image(StopOverButton, x, y); ?????? else image(StopButton, x, y); ?????? } ?????? else ?????? { ?????? if (ButtonRegion) image(PlayOverButton, x, y); ?????? else image(PlayButton, x, y); ?????? } ?????} ????? ?????// Use mouse to control whether send music signal to Arduino ?????String portChoise; ?????int BaudChoise; ?????void mousePressed() ?????{ ????? try { ????? if (ButtonRegion) //image button region ????? { ????? //portChoise = Serial.list()[1]; ????? if (!StartState) ????? { ????? StartState = true; ????? portChoise = PortBox.getSelectedItem().toString(); ????? BaudChoise = Integer.parseInt(BaudBox.getSelectedItem().toString()); ????? ????? println(portChoise + " " + BaudChoise); ????? ????? ArduinoPort = new Serial(this, portChoise, BaudChoise); ????? } ????? else ????? { ????? StartState = false; ????? ArduinoPort.stop(); ????? } ????? } ????? } ????? catch(Throwable err) ????? { ????? println("Error!"); ????? } ?????}
?????int[] WriteByte = new int[20]; ??void CommWriter(int command) ??{ ?? //int[] WriteByte = new int[20]; ?? int Length = 4; ?? ?? switch(command) { ?? case 'B': ?? WriteByte[0] = 'B'; ?? WriteByte[1] = 0; ?? WriteByte[2] = 0; ?? WriteByte[3] = 0; ?? WriteByte[4] = 0x0a; ?? Length = 4; ?? break; ?? ?? //localcolor[0]/localcolor[1]/localcolor[3] stores the music signal sent to Arduino ?? case 'C': ?? WriteByte[0] = 'C'; ?? WriteByte[1] = localcolor[0]; ?? WriteByte[2] = localcolor[1]; ?? WriteByte[3] = localcolor[2]; ?? WriteByte[4] = 0x0a; ?? ?? Length = 4; ?? break; ?? ?? default: ?? break; ?? } ?? //println("TransCommand!"); ?? ?? ?? if (StartState) ?? { ?? try { ?? for (int i = 0;i <= Length ;i++) ArduinoPort.write(WriteByte[i]); ?? } ?? catch(Throwable err) ?? { ?? //println("Error!"); ?? } ?? } }
#include //version 0.3 #include #include PumpSignal pump = PumpSignal(); colorLed pins = colorLed(9,10,11); Metro ledMetro = Metro(2000); byte newData[3]; int E1 = 5; //M1 Speed Control int M1 = 4; //M1 Direction Control int E2 = 6; //M2 Speed Control int M2 = 7; //M2 Direction Control void stop(void) //Stop { digitalWrite(E1,LOW); digitalWrite(E2,LOW); Serial.begin(57600); randomSeed(analogRead(3)); } void advance(char a, char b) //Move forward { analogWrite (E1,a); //PWM Speed Control digitalWrite(M1,HIGH); analogWrite (E2,b); digitalWrite(M2,HIGH); } void setup(void) { pins.InitPins(); int i; for(i=4;i<=7;i++) pinMode(i, OUTPUT); } int val[3]; int i = 0; void loop(void) { pump.Run(); //Receive the music signal sent from Processing if(pump.newCmd('C')){ pump.getSerialCmd('C',newData); //Control two pumps to make them change the water flow according to the music signal for(i=0;i<=1;i++){ if(newData[i]>0&&newData[i]<=47){ val[0] = map(newData[0],0,47,47,52); val[1] = map(newData[2],0,50,50,55); }else{ val[0] = newData[0]; val[1] = newData[2]; } } } advance (val[0],val[1]); //Light six LEDs to make the water colorful if (ledMetro.check() == 1) { pins.colorRGB(random(0,255),random(0,255),random(0,255)); } }