Final Processing Code

The final Processing code. To process the sound I used the ESS library that reads the spectrum of a sound source. The website of this library can be found here: Link.
The following code is taken by the library website, and changed by me to make it work with my installation
Here I import the Ess library and the serial one, and initialize all the variables needed for the script to run.

import krister.Ess.*;
import processing.serial.*;

// “bufferSize” defines the buffer used to read and
// process the signal from the microphone
int bufferSize;

// “steps” and “limitDiff” define the scale of the reading
int steps;
float limitDiff;

// numAverages defines the number of averages computed between
// the reading of all the frequency bands
int numAverages=3;

// the value of “Damp” defines on how similar and smooth should be
// the frequency band

float myDamp=.1f;

// “maxLimit” and “minLimit” define the lowest and the highest value
// of the reading scale
float maxLimit,minLimit;

// “bass”, “medium” and “high” are the variable that will store the
// output value to be sent to the Arduino board
int bass;
int medium;
int high;

// “myPort” is dedicated to the Serial port
Serial myPort;

// “myFFT” is dedicated to store the array with all the readings
FFT myFFT;

// “AudioInput” is the variable to store the audio
AudioInput myInput;

At this point I start with the setup of the stage, I initialize the libraries and call all the functions to read the audio

void setup () {
size(700,221);// start up Ess
Ess.start(this);

// set up our AudioInput
bufferSize=32;
myInput=new AudioInput(bufferSize);

// set up our FFT
myFFT=new FFT(bufferSize*2);
myFFT.equalizer(false);

// set up our FFT normalization/dampening
minLimit=.005;
maxLimit=.05;
myFFT.limits(minLimit,maxLimit);
myFFT.damp(myDamp);
myFFT.averages(numAverages);

// get the number of bins per average
steps=bufferSize/numAverages;

// get the distance of travel between minimum and maximum limits
limitDiff=maxLimit-minLimit;

frameRate(25);

myInput.start();
myPort = new Serial(this, Serial.list()[2], 115200);
}

I start by drawing on the stage the waveform and the spectrum

void draw() {
background(0,0,255);

// draw the waveform

noStroke();
fill(255,128);

// draw the spectrum

for (int i=0; i
rect(10+i,10,1,myFFT.spectrum[i]*200);
}

Then we start to calculate the averages. As I set up the numAverages to be 3, the loop will continue as long as the “i” variable will not reach the number of 3. I declare the variable “tuning”, that I always use as a multiplier in case the signals that I get become too low. Than I draw 3 rectangles, each one set to be tall as the average of the first, second and third frequency range.

// draw our averages
for(int i=0; i)

{
int tuning=74;
fill(255,128);
rect(10+i*steps,10,steps,myFFT.averages[i]*200);
fill(255);
rect(10+i*steps,(int)(10+myFFT.maxAverages[i]*200),steps,1);
rect(10+i*steps,10,1,200);

Now, I start to retrieve the final values that I have to send to the board. I transform the value into an integer, as the “myFFT” variable was decleares as a float, and multiply the result by the “tuning” variable, that I can easily adjust if I should need to do so. I print everything on the Processing console for debugging, and then send the values to Arduino. Note that I added 33 to the “bass” variable, 108 to the “medium” and 183 to the “high”. I did so because Arduino will read the range between 33 and 108 and assign the value to the pin dedicated for the bass fan.

int bass = int(myFFT.averages[0]*tuning);
int medium = int(myFFT.averages[1]*tuning);
int high = int(myFFT.averages[2]*tuning);
println(byte(bass+33));
println(medium+108);
println(high+183);
myPort.write(byte(bass+33));
myPort.write(byte(medium+108));
myPort.write(byte(high+183));
}

At the end I call the function that reads the input from the soundcard

public void audioInputData(AudioInput theInput) {
myFFT.getSpectrum(myInput);
}

The Processing code is also finished! Let’s build the structure: Link!

No Comments

No comments yet.

Comments RSS

Sorry, the comment form is closed at this time.