# Blog 16 Recitation 9: Sound in Processing
Exercise One:
The problem I met is that I don’t know how to stop the music when I click twice. And I learnt about the “!playSound1” function that can reverse the status.
One really devastating thing is that I saved the code and when I open it again, it doesn’t work any more. I can’t find anything wrong with it so I have to do the coding again.😭😭😭😭😭😭😭
import processing.sound.*; Boolean playSound1 = false; Boolean playSound2 = false; // declare a SoundFile object SoundFile sound1; SoundFile sound2; void setup() { size(640, 480); background(137, 31, 191); sound1 = new SoundFile(this, "dreaampadloop.wav"); sound2 = new SoundFile(this, "pollyunilead1.wav"); } void draw() { } void keyPressed() { if (key == 'A') { playSound1=!playSound1; } if (playSound1==true) { sound1.loop(); fill(214,84,19); rect(200, 100, 100, 100); } else { sound1.stop(); fill(255); rect(200, 100, 100, 100); } if (key == 'L') { playSound2=!playSound2; } if (playSound2==true) { sound2.loop(); fill(62,113,193); rect(300, 200, 100, 100); } else { sound2.stop(); fill(255); rect(300, 200, 100, 100); } }
Exercise Two:
The problem I met is that I forgot to give value to processing_values[0] so Arduino cannot react to the music.
Arduino:
#define NUM_OF_VALUES_FROM_PROCESSING 1 /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ #include <Servo.h> #define ZD 3 /** DO NOT REMOVE THESE **/ int tempValue = 0; int valueIndex = 0; Servo myservo; /* 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(13, OUTPUT); pinMode(ZD,OUTPUT); } void loop() { getSerialData(); if (processing_values[0] > 0) { int vibe = map(processing_values[0], 0, 1, 0, 200); analogWrite(ZD,vibe); //delay(1); // delay in between reads for stability } else { analogWrite(ZD,0); } } //receive serial data from Processing void getSerialData() { while (Serial.available()) { char c = Serial.read(); switch (c) { case '0'...'9': //save the value of char c to tempValue //but simultaneously rearrange the existing values saved in tempValue //for the digits received through char c to remain coherent //if this does not make sense and would like to know more, send an email to me! tempValue = tempValue * 10 + c - '0'; break; //if the char c from Processing is a comma //indicating that the following values of char c is for the next element in the values array case ',': processing_values[valueIndex] = tempValue; //reset tempValue value tempValue = 0; //increment valuesIndex by 1 valueIndex++; break; //if the char c from Processing is character 'n' //which signals that it is the end of data case '\n': //save the tempValue //this will b the last element in the values array processing_values[valueIndex] = tempValue; //reset tempValue and valueIndex values //to clear out the values array for the next round of readings from Processing tempValue = 0; valueIndex = 0; break; } } }
Processing:
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; // declare an AudioIn object //AudioIn sound; // declare an Amplitude analysis object to detect the volume of sounds Amplitude analysis; void setup() { size(640, 480); setupSerial(); sound = new SoundFile(this, "Lost On The Freeway.aif"); // create the AudioIn object and select the mic as the input stream //sound = new AudioIn(this, 0); sound.loop(); analysis = new Amplitude(this); analysis.input(sound); } void draw() { println(analysis.analyze()); background(0); // analyze the audio for its volume level float volume = analysis.analyze(); // map the volume value to a useful scale float diameter = map(volume, 0, 1, 0, width); // draw a circle based on the microphone amplitude (volume) circle(width/2, height/2, diameter); processing_values[0] =volume; sendSerialData(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[2], 9600); myPort.clear(); // Throw out the first reading, // in case we started reading in the middle of a string from the sender. myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII myString = null; } void sendSerialData() { String data = ""; for (int i=0; i<processing_values.length; i++) { data += processing_values[i]; //if i is less than the index number of the last element in the values array if (i < processing_values.length-1) { data += ","; // add splitter character "," between each values element } //if it is the last element in the values array else { data += "\n"; // add the end of data character linefeed "\n" } } //write to Arduino myPort.write(data); print(data); // this prints to the console the values going to arduino }
Homework:
The only small problem is that because I want to draw a sheep, but I don’t know if there are any way I can draw it more delicately, so in the end I just use basic shapes to make a rough sheep.
In the video, I included many songs that I recently LIKE. Hope you also ENJOY them!💗
https://www.bilibili.com/video/BV1mu411y7RF
import processing.sound.*; AudioIn microphone; Amplitude analysis; void setup() { size(640, 480); microphone = new AudioIn(this, 0); microphone.start(); analysis = new Amplitude(this); analysis.input(microphone); } void draw() { println(analysis.analyze()); background(151,211,143); float volume = analysis.analyze(); float diameter = map(volume, 0, 1, 0, 1000); fill(242,221,166); rect(300, 280, 10,diameter); rect(380, 280, 10,diameter); drawSheep(width/2.5, height/2, 50, color(150,183,181)); } void drawSheep(float u, float v, float s, color c){ fill(242,221,166); rect(300, 280, 10,s); rect(380, 280, 10,s); fill(0); circle(u, v+s*0.5, s); circle(u+s, v+s*1.2, s); circle(u+s*2, v+s*1.5, s); circle(u+s*3, v+s*1.2, s); circle(u+s*4, v+s*0.5, s); circle(u, v-s*0.5, s); circle(u+s, v-s*1.2, s); circle(u+s*2, v-s*1.5, s); circle(u+s*3, v-s*1.2, s); circle(u+s*4, v-s*0.5, s); ellipse(width/1.8, height/2, s*4.5,s*3); fill(242,221,166); ellipse(width/2.6, height/2.2, s*1.2,s*1.4); fill(0); ellipse(width/2.6, height/2.6, s*1.4,s*0.6); fill(242,166,208); triangle(u-40,v-40,u-40,v-10,u-60,v-10); triangle(u+20,v-40,u+20,v-10,u+40,v-10); }
April 23, 2022, Jinqiao, Younian Liu