This processing project basically animates the shoe up and down using sound as the input instead of a mouse position. I have used the microphone input to measure the sound level and add it to the image position in each loop that has been made. (similar to the last one) as the sound increases or continues the image moves higher and when it disappears off the top it changes and drifts back . if the sound stops the shoe will drift back and if the sound isn't high enough the shoe will hover and drift around un changed. the program has two modes, one is listen to the sound and move the shoe the other is when the shoe image has changed ignore the sound and just drift back to the home position before switching back to the listen mode.
This is based on an example that uses a microphone input with in the project.
An important thing with this project is that it initialises the sound objects correctly and when the program stops it calls the Stop function which shuts down the sound objects.
minim is library that is used to in put and out put sound. im only interested in the input for this project.
There is a class called MaxAmplitudeDetector based on an example that inherits/implements AudioListener this is taken from examples and is use to detect the sound recorded during each frame.
CODE EXAMPLE -
import ddf.minim.*;
float sensitivity=5; //any number greater than 0 and less than 10
Minim minim;
AudioInput in;
MaxAmplitudeDetector mad;
float lastAmplitude=10;
float amplutudeThreshold=20;
float driftBackFactor=32;
float driftBackFactorForNewShoe=8;
PImage bgShoe;
PImage fgShoe;
String[] Shoes;
int ShoeNum = 4;
int canChangeShoeCounter=24;
boolean listenMode=true;
void setup() {
size(1000, 600,P3D);
frameRate(24);
minim = new Minim(this);
minim.debugOn();
// get a line in from Minim, default bit depth is 16
in = minim.getLineIn(Minim.MONO);
mad= new MaxAmplitudeDetector();
in.addListener(mad);
bgShoe = loadImage("bg.jpg");
fgShoe = loadImage("shoe1.png");
Shoes = new String[10];
Shoes[0]="shoe1.png";
Shoes[1]="shoe2.png";
Shoes[2]="shoe3.png";
Shoes[3]="shoe4.png";
Shoes[4]="shoe5.png";
Shoes[5]="shoe6.png";
Shoes[6]="shoe7.png";
Shoes[7]="shoe8.png";
Shoes[8]="shoe9.png";
Shoes[9]="shoe10.png";
}
void draw()
{
println(listenMode);
if (listenMode)
{
listenMode();
}
else
{
newShoeMode();
}
}
void newShoeMode()
{
float newAmplitude =lastAmplitude ;
newAmplitude = lastAmplitude - (lastAmplitude / driftBackFactorForNewShoe);
if (canChangeShoeCounter>0)
{
canChangeShoeCounter--;
}
if (newAmplitude <=10)
{
listenMode=true;
}
background(0);
DrawShoes(newAmplitude);
lastAmplitude=newAmplitude;
}
void listenMode()
{
float peakAmplitude=mad.Amplitude();
peakAmplitude=map(peakAmplitude,0,sensitivity, 0,height);
float newAmplitude =lastAmplitude ;
if (peakAmplitude > amplutudeThreshold)
{
newAmplitude =lastAmplitude + peakAmplitude ;
if(newAmplitude>height)
{
ChangeShoe();
return;
}
}
else
{
//drift back
newAmplitude = lastAmplitude - (lastAmplitude / driftBackFactor);
if (canChangeShoeCounter>0)
{
canChangeShoeCounter--;
}
}
if(newAmplitude>(height *1.1))
{
newAmplitude=(height *1.1);
}
background(0);
DrawShoes(newAmplitude);
lastAmplitude=newAmplitude;
}
void DrawShoes(float yOffset)
{
image(bgShoe, 0, 0);
image(fgShoe, 0, yOffset * -1);
}
void ChangeShoe()
{
if (canChangeShoeCounter>0)
{
//Stops double hits can only change image again after drift back has started.
return;
}
canChangeShoeCounter=48;
if (ShoeNum < 0 ) {ShoeNum=9;}
fgShoe=loadImage(Shoes[ShoeNum]);
ShoeNum--;
listenMode=false;
}
void stop()
{
// always close Minim audio classes when you finish with them
minim.stop();
super.stop();
}
class MaxAmplitudeDetector implements AudioListener
{
private float[] left;
private float[] right;
float maximumApmlitude;
MaxAmplitudeDetector()
{
left = null;
right = null;
}
synchronized void samples(float[] samp)
{
left = samp;
}
synchronized void samples(float[] sampL, float[] sampR)
{
left = sampL;
right = sampR;
}
synchronized float Amplitude()
{
maximumApmlitude=0;
// we've got a stereo signal if right or left are not null
if ( left != null && right != null )
{
for ( int i = 0; i < left.length; i++ )
{
if (abs(left[i])>maximumApmlitude)
{
maximumApmlitude=abs(left[i]);
}
}
for ( int i = 0; i < right.length; i++ )
{
if (abs(right[i])>maximumApmlitude)
{
maximumApmlitude=abs(right[i]);
}
}
}
else if ( left != null )
{
for ( int i = 0; i < left.length; i++ )
{
if (abs(left[i])>maximumApmlitude)
{
maximumApmlitude=abs(left[i]);
}
}
}
return maximumApmlitude;
}
}