Exercise One: VIRTUAL MUSIC LAUNCHBOX
I tried to add a melody I wanted but found that the size of the file was not suitable for the program, so I used the four loops provided in the loop folder and tried to control the music at different points in time by pressing the up, down, left, right and center keys and trying to match the new music.
import processing.sound.*; Boolean playSound1 = false; Boolean playSound2 = false; Boolean playSound3 = false; Boolean playSound4 = false; // declare three SoundFile objects SoundFile A; SoundFile B; SoundFile C; SoundFile D; int value1 = color(180, 180, 180); int value2 = color(180, 180, 180); int value3 = color(180, 180, 180); int value4 = color(180, 180, 180); void setup() { size(520, 520); background(255); // create the objects and load sounds into them A = new SoundFile(this, "crashbreak2rideout.wav"); B = new SoundFile(this, "monsterwobbleloop.wav"); C = new SoundFile(this, "plunkysynthloop.wav"); D = new SoundFile(this, "somekindofvocal.wav"); } void draw() { fill(value1); square(40, 40, 200); fill(value2); square(280, 40, 200); fill(value3); square(40, 280, 200); fill(value4); square(280, 280, 200); } void keyPressed() { if (key == CODED) { if (keyCode == UP) { value1 = color(252, 224, 5); A.play(); } if (keyCode == DOWN) { value2 = color(5, 5, 5); B.play(); } if (keyCode == LEFT) { value3 = color(5, 5, 5); C.play(); } if (keyCode == RIGHT) { value4 = color(252, 224, 5); D.play(); } } }
Exercise Two: HAPTIC SOUND WEARABLE
In this step, for the processing part, I use amplitude analysis to analyze the volume of the music file and send the volume evaluation as an input to the Arduino. the moving circle during processing is used as an output for visualization. the Arduino materializes the volume as vibration as another output. And at the beginning, I had a short circuit because I connected the wrong power supply, but later I found that after adjusting it, it could vibrate.
Processing code
import processing.serial.*; import processing.sound.*; SoundFile sound; int NUM_OF_VALUES_FROM_PROCESSING = 1; float processing_values[] = new float[NUM_OF_VALUES_FROM_PROCESSING]; Serial myPort; String myString; Amplitude analysis; void setup() { size(780, 640); setupSerial(); sound = new SoundFile(this, "plunkysynthloop.wav"); sound.loop(); analysis = new Amplitude(this); analysis.input(sound); } void draw() { println(analysis.analyze()); background(0); float volume = analysis.analyze(); float diameter = map(volume, 0, 1, 0, width); circle(width/2, height/2, diameter); processing_values[0] =volume; sendSerialData(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); myString = null; } void sendSerialData() { String data = ""; for (int i=0; i<processing_values.length; i++) { data += processing_values[i]; if (i < processing_values.length-1) { data += ","; } else { data += "\n"; } } myPort.write(data); print(data); }
Arduino code
#define NUM_OF_VALUES_FROM_PROCESSING 3 #define ZD 3 /** DO NOT REMOVE THESE **/ int tempValue = 0; int valueIndex = 0; /* This is the array of values storing the data from Processing. */ int processing_values[NUM_OF_VALUES_FROM_PROCESSING]; void setup() { Serial.begin(9600); pinMode(3, OUTPUT); } void loop() { getSerialData(); if (processing_values[0] == 1) { analogWrite(ZD, 200); delay(100); } if (processing_values[0] == 0) { analogWrite(ZD, 0); delay(0); } } //receive serial data from Processing void getSerialData() { while (Serial.available()) { char c = Serial.read(); //for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase switch (c) { //if the char c from Processing is a number between 0 and 9 case '0'...'9': tempValue = tempValue * 10 + c - '0'; break; case ',': processing_values[valueIndex] = tempValue; //reset tempValue value tempValue = 0; //increment valuesIndex by 1 valueIndex++; break; case '\n': processing_values[valueIndex] = tempValue; tempValue = 0; valueIndex = 0; break; } } }
HOMEWORK: VOICE CONTROLLED PUPPET
I used the name of my own favorite star band group as my character, the user can use voice to control the letter“T” ‘s transformation with music as visualized outputs of sound volume. With the help of my roommates, I changed the sound map from 0-1 to 0-0.05 since my microphone input is low due to a technical problem.
code
import processing.sound.*; AudioIn input; Amplitude loudness; PFont font; void setup() { size(640, 480); background(255); input = new AudioIn(this,0); input.start(); loudness = new Amplitude(this); loudness.input(input); } void draw() { float inputLevel = map(mouseY, 0, height, 1.0, 0.0); input.amp(inputLevel); float volume = loudness.analyze(); float diameter= map(volume,0,1,0,width); int size = int(map(volume, 0, 0.5, 0,width)); background(250, 2410, 135); noStroke(); circle(60, 100, diameter); circle(50, 400, diameter); circle(600, 350, diameter); circle(400, 450, diameter); circle(500, 80, diameter); circle(300, 40, diameter); circle(300, 450, diameter); circle(40, 350, diameter); circle(30, 100, diameter); circle(400, 70, diameter); fill(255, 100, 150); font = createFont("Black", 135); textFont(font); fill(0); text("N", width/2-52, height/2+78); // We draw a circle whose size is coupled to the audio analysis drawAudrey(width/2+40, height/2-90, size, size); } void drawAudrey(float u, float v, float s, color c){ noStroke(); fill(0); rect(200, 230, s-10,100); rect(220,220,s-70,10); rect(440, 230, s-10,100); rect(450,220,s-70,10); }