3.6 Sound Average and Sound as Button

Below is a sketch that creates a visual sound bar and threshold slider. This sound bar visualizes the average sound per frame. This gives you a single value so you can check if it is over the threshold. If it is you can then execute a block of code. The sound bar is created in a function. To draw the sound bar you call the function: AverageSound(x,y,width,height,s);

x: x position
y: y position
width: width of bar
height: height of bar
s: average sound

import controlP5.*;

Minim minim;
AudioInput sound;

float threshold;

ControlP5 cp5;

void setup(){
  size(900,900);
  
  minim = new Minim(this);
  sound = minim.getLineIn(Minim.STEREO, 1024);
  
  cp5 = new ControlP5(this);
  
  cp5.addSlider("threshold")
    .setPosition(40,40)
    .setRange(0,1)
    .setSize(200,20)
    .setValue(0.5)
    .setColorForeground(color(150,150,150)) 
    .setColorLabel(color(255)) 
    .setColorBackground(color(70,70,70)) 
    .setColorValue(color(0,0,0)) 
    .setColorActive(color(200,200,200))
     ;
}

void draw(){
  background(0);
  
  //get the average sound_sample
  float av_sound = 0;
  for(int i = 0; i < sound.mix.size(); i++){ 
    av_sound = av_sound + abs(sound.mix.get(i)); 
  } 
  av_sound = av_sound/sound.mix.size(); 
  
  AverageSound(0,0,width,height,av_sound);
}

void AverageSound(float xloc, float yloc, int wdth, int hght, float amp) {
  noStroke();
  fill(50);
  rect(xloc,yloc,wdth,hght);
  fill(150);
  rect(xloc,yloc+hght,wdth,-hght*amp);
  stroke(255);
  line(xloc,yloc+hght-threshold*hght,xloc+wdth,yloc+hght-threshold*hght);
}

 

The sketch below uses the sound visualizer along with the physics library Fisica.

import ddf.minim.*;
import controlP5.*;
import fisica.*;

FWorld world;

Minim minim;
AudioInput sound;

float threshold;

ControlP5 cp5;

void setup(){
  size(900,900);
  
  minim = new Minim(this);
  sound = minim.getLineIn(Minim.STEREO, 1024);
  
  cp5 = new ControlP5(this);
  
  cp5.addSlider("threshold")
    .setPosition(40,40)
    .setRange(0,1)
    .setSize(200,20)
    .setValue(0.5)
    .setColorForeground(color(150,150,150))
     .setColorLabel(color(255))
     .setColorBackground(color(70,70,70))
     .setColorValue(color(0,0,0))
     .setColorActive(color(200,200,200))
     ;
     
  Fisica.init(this);

  world = new FWorld();
  world.setGravity(0, 100);
  world.setEdges();
     
}

void draw(){
  background(0);
  
  //get the average sound_sample
  float av_sound = 0;
  for(int i = 0; i < sound.mix.size(); i++){ 
    av_sound = av_sound + abs(sound.mix.get(i)); 
  } 
  av_sound = av_sound/sound.mix.size();           
  AverageSound(40,80,50,200,av_sound);    
  if ( av_sound > threshold) {
    float sz = random(40, 130);
    FCircle b = new FCircle(sz);
    b.setPosition(width/2, 100);
    b.setVelocity(random(-100,100), -40);
    b.setRestitution(0.7);
    b.setDamping(0.01);
    b.setNoStroke();
    b.setFill(random(255), random(255), random(255));
    world.add(b);
  }
  
  world.draw();
  world.step();
}

void contactEnded(FContact c) {  
  if (!c.getBody1().isStatic()) {
    FCircle b = (FCircle)c.getBody1();
    if (b.getSize()>5) {
      b.setSize(b.getSize()*0.98);
    }
  } 

  if (!c.getBody2().isStatic()) {
    FCircle b = (FCircle)c.getBody2();
    if (b.getSize()>5) {
      b.setSize(b.getSize()*0.98);
    }
  }
}


void AverageSound(float xloc, float yloc, int wdth, int hght, float amp) {
  noStroke();
  fill(50);
  rect(xloc,yloc,wdth,hght);
  fill(150);
  rect(xloc,yloc+hght,wdth,-hght*amp);
  stroke(255);
  line(xloc,yloc+hght-threshold*hght,xloc+wdth,yloc+hght-threshold*hght);
}