Sound Structure

Starting a new interactive lighting project and trying to figure out ways to break away from regular structure. Here is a quick processing sketch that just takes simply brownian motion and makes each segment reactive to sound. As the structure grows and the resolution becomes greater you can begin to see the sound travel through the structure. Click continue below for the code.

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

ControlP5 cp5;

Minim minim;
AudioInput sound;

float amplitude;
float threshold;

float hvalue;

float zp = 0;

IntList xpos;
IntList ypos;
IntList zpos;
IntList strw;
IntList trans;

float newpulsex = 0;
float newpulsey = 0;
float newpulsez = 0;

float distcount = 2000;

void setup() {
  size(800,800,P3D);
  
  cp5 = new ControlP5(this);
  minim = new Minim(this);
  
  sound = minim.getLineIn(Minim.STEREO, 1024);
  
  xpos = new IntList();
  ypos = new IntList();
  zpos = new IntList();
  strw = new IntList();
  trans = new IntList();
  
  cp5.addSlider("amplitude")
    .setPosition(50,50)
    .setRange(0,900)
    .setSize(200,20)
    .setValue(100)
    .setColorActive(color(100)) 
    .setColorBackground(color(20)) 
    .setColorForeground(color(60)) 
  ;
  
  cp5.addSlider("threshold")
    .setPosition(50,80)
    .setRange(0,200)
    .setSize(200,20)
    .setValue(100)
    .setColorActive(color(100)) 
    .setColorBackground(color(20)) 
    .setColorForeground(color(60)) 
  ;
  
  xpos.append(0);
  ypos.append(0);
  zpos.append(0);
  strw.append(1);
  strw.append(50);
  
}

void draw(){
  background(0);
  fill(255);
  pushMatrix();
  
  ///simulated camera//////////////////
  translate(width/2,height/2,zp);
  rotateY(PI*2*(mouseX-220)/(width-220));
  rotateX(PI*2*mouseY/height);
  ///end camera//////////////////
  
  
  /////brownian motion + sound///////

  int newx = round(random((hvalue/5*-1),(hvalue/5)));
  int newy = round(random((hvalue/5*-1),(hvalue/5)));
  int newz = round(random((hvalue/5*-1),(hvalue/5)));
  
  int lastx = xpos.get(xpos.size()-1);
  int lasty = ypos.get(ypos.size()-1);
  int lastz = zpos.get(zpos.size()-1);
  
  if(((lastx + newx) > 400) || ((lastx + newx) < -400)){
   newx = newx * -1;
  }
  
  if(((lasty + newy) > 400) || ((lasty + newy) < -400)){
   newy = newy * -1;
  }
  
  if(((lastz + newz) > 400) || ((lastz + newz) < -400)){
   newz = newz * -1;
  }
  
  newx = lastx + newx;
  newy = lasty + newy;
  newz = lastz + newz;
  
  float check = random(1);
  

  
  if(hvalue > threshold){

    newpulsex = random(-400,400);
    newpulsey = random(-400,400);
    newpulsez = random(-400,400);
    distcount = 0;    
  }
  
  xpos.append(newx);
    ypos.append(newy);
    zpos.append(newz);
    strw.append(1);
    trans.append(50);
    
  
  
  for(int i = 0; i < xpos.size() - 1; i++){
     float dist = dist(newpulsex,newpulsey,newpulsez,xpos.get(i), ypos.get(i),zpos.get(i));
     
     if(abs(dist-distcount) < 10){
       strw.set(i,5);
       trans.set(i,255);
     }
     strw.set(i,int(strw.get(i)*0.99999));
     trans.set(i,int(trans.get(i)*0.95));
     
     if(strw.get(i) < 1){
       strw.set(i,1);
     }
     
     if(trans.get(i) < 50){
       trans.set(i,50);
     }
     stroke(255,255,255,trans.get(i));
     strokeWeight(strw.get(i));
     line(xpos.get(i), ypos.get(i),zpos.get(i),xpos.get(i+1), ypos.get(i+1),zpos.get(i+1));
  }
  popMatrix();
  
  ////end brownian motion///////
  
  
  /////get sound wave///////
  stroke(255,255,255,100);
  strokeWeight(1);
  
  hvalue = 0;
  for(int i = 0; i < sound.mix.size() - 1; i++){
    line(i, 700 + sound.mix.get(i)*amplitude, i+1, 700 + sound.mix.get(i+1)*amplitude);
    if(hvalue <  abs(sound.mix.get(i))){
      hvalue = abs(sound.mix.get(i));
    }
  }
  
  hvalue = hvalue * amplitude;
  ////sound bar////
  noStroke();
  rectMode(CORNER);
  fill(20);
  rect(50,110,50,200);
  fill(60);
  rect(50,310,50,(hvalue*-1));
  
  stroke(255);
  line(110,(310-threshold),120,(310-threshold));
  
 
  
  distcount =  distcount+10;
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  zp = zp - e*5;
}