This example creates a random point for each of the 1024 sound samples. First in 2d and then in 3D. Each point is given motion similar to Brownian motion. The sound sample for each point can be used to increase its speed vector as if the sound is heating up gas. In 3d he points are queried to connect points within a given distance creating a network of points driven by a sound delay.

The code below creates a list of random points. The point positions are defined by a vector: (x,y,z). In this case, the sketch is 2d so we are not suing the z coordinate.
import ddf.minim.*;
Minim minim;
AudioInput sound;
//declare and initialize a list of vectors called pts
ArrayList <PVector> pts = new ArrayList<PVector>();
void setup(){
size(900,900);
minim = new Minim(this);
sound = minim.getLineIn(Minim.STEREO, 1024);
//a loop that creates a vector for each sound sample
for(int i = 0; i < sound.mix.size(); i++){
float xp = random(100,width-100);
float yp = random(100,height-100);
pts.add(new PVector(xp,yp,0));
}
}
void draw(){
background(0);
//place a point at each vector position
stroke(255);
strokeWeight(2);
for(int i = 0; i < sound.mix.size(); i++){
PVector p1 = pts.get(i);
point(p1.x,p1.y);
}
}
We will give the points movement by creating another vector list for speed. We will add that speed vector incrementally to the point vectors. We also need to check to see if each point has exceeded the bounds of the sketch. If so we reverse the speed for that vector by multiplying by -1.
import ddf.minim.*;
Minim minim;
AudioInput sound;
//declare and initialize a list of vectors called pts and speed
ArrayList <PVector> pts = new ArrayList<PVector>();
ArrayList <PVector> speed = new ArrayList<PVector>();
void setup(){
size(900,900);
minim = new Minim(this);
sound = minim.getLineIn(Minim.STEREO, 1024);
//a loop that creates a vector for each sound sample
for(int i = 0; i < sound.mix.size(); i++){
//generate random positions for the points
float xp = random(100,width-100);
float yp = random(100,height-100);
pts.add(new PVector(xp,yp,0));
//generate random speed vectors to give the points movement
float xs = random(-1,1);
float ys = random(-1,1);
speed.add(new PVector(xs,ys,0));
}
}
void draw(){
background(0);
//place a point at each vector position
stroke(255);
strokeWeight(2);
for(int i = 0; i < sound.mix.size(); i++){
PVector p1 = pts.get(i);
PVector s1 = speed.get(i);
point(p1.x,p1.y);
//add the speed vector to the current point position
p1.x = p1.x + s1.x;
p1.y = p1.y + s1.y;
//check to see if point vector is out of the sketch boundary
if(p1.x < 0 || p1.x > width){
s1.x = s1.x * -1;
}
if(p1.y < 0 || p1.y > height){
s1.y = s1.y * -1;
}
//increase speed based on sound sample
float sound_sample = abs(sound.mix.get(i));
if(s1.x > 0){
s1.x = s1.x + sound_sample;
}else{
s1.x = s1.x - sound_sample;
}
if(s1.y > 0){
s1.y = s1.y + sound_sample;
}else{
s1.y = s1.y - sound_sample;
}
//slow down the points to offset speed increases from sound (cooling down)
if(abs(s1.x) > 0.1){
s1.x = s1.x *0.99;
}
if(abs(s1.y) > 0.1){
s1.y = s1.y *0.99;
}
//replace point and speed vector with new values
pts.set(i,p1);
speed.set(i,s1);
}
}
The code below simply converts the 2d sketch into 3d with P3D in the size declaration in setup. We are now using the z coordinate and a camera function was added to the end of the sketch that allows the sketch to be rotated using the mouse.
import ddf.minim.*;
Minim minim;
AudioInput sound;
///camera varibles
int oldx = mouseX;
int oldy = mouseY;
float rotx = 0;
float roty = 0;
float zcam = -800;
//declare and initialize a list of vectors called pts and speed
ArrayList <PVector> pts = new ArrayList<PVector>();
ArrayList <PVector> speed = new ArrayList<PVector>();
//declare max and min bounds
float MAX = 500;
float MIN = -500;
void setup(){
size(900,900,P3D);
minim = new Minim(this);
sound = minim.getLineIn(Minim.STEREO, 1024);
//a loop that creates a vector for each sound sample
for(int i = 0; i < sound.mix.size(); i++){
//generate random positions for the points
float xp = random(MIN,MAX);
float yp = random(MIN,MAX);
float zp = random(MIN,MAX);
pts.add(new PVector(xp,yp,zp));
//generate random speed vectors to give the points movement
float xs = random(-1,1);
float ys = random(-1,1);
float zs = random(-1,1);
speed.add(new PVector(xs,ys,zs));
}
}
void draw(){
background(0);
//call camera function
cam();
//place a point at each vector position
stroke(255);
strokeWeight(2);
for(int i = 0; i < sound.mix.size(); i++){
PVector p1 = pts.get(i);
PVector s1 = speed.get(i);
point(p1.x,p1.y,p1.z);
//add the speed vector to the current point position
p1.x = p1.x + s1.x;
p1.y = p1.y + s1.y;
p1.z = p1.z + s1.z;
//check to see if point vector is out of the sketch boundary
if(p1.x < MIN || p1.x > MAX){
s1.x = s1.x * -1;
}
if(p1.y < MIN || p1.y > MAX){
s1.y = s1.y * -1;
}
if(p1.z < MIN || p1.z > MAX){
s1.z = s1.z * -1;
}
//increase speed based on sound sample
float sound_sample = abs(sound.mix.get(i));
if(s1.x > 0){
s1.x = s1.x + sound_sample;
}else{
s1.x = s1.x - sound_sample;
}
if(s1.y > 0){
s1.y = s1.y + sound_sample;
}else{
s1.y = s1.y - sound_sample;
}
if(s1.z > 0){
s1.z = s1.z + sound_sample;
}else{
s1.z = s1.z - sound_sample;
}
//slow down the points to offset speed increases from sound (cooling down)
if(abs(s1.x) > 0.1){
s1.x = s1.x *0.99;
}
if(abs(s1.y) > 0.1){
s1.y = s1.y *0.99;
}
if(abs(s1.z) > 0.1){
s1.z = s1.z *0.99;
}
//replace point and speed vector with new values
pts.set(i,p1);
speed.set(i,s1);
}
}
///camera functions
void cam() {
int newx = mouseX;
int newy = mouseY;
translate(width/2, height/2,zcam);
rotateY(rotx);
rotateX(roty);
//rotateZ(PI);
if ((mousePressed == true)) {
rotx = rotx + (oldx-newx)/50.0;
roty = roty + (oldy-newy)/50.0;
}
oldx = newx;
oldy = newy;
}
void mouseWheel(MouseEvent event) {
float e = event.getCount();
zcam = zcam - e*5;
}
A query has been added that checks the distance between one point and all the other points. If the distance is within a certain threshold the sketch draws a line between those points. In this case that threshold his decided by a slider and a sound delay. The sound delay stores the highest average sound and then slowly decreases it until the average sound surpasses it again.
import ddf.minim.*;
import controlP5.*;
Minim minim;
AudioInput sound;
ControlP5 cp5;
///camera varibles
int oldx = mouseX;
int oldy = mouseY;
float rotx = 0;
float roty = 0;
float zcam = -800;
//declare and initialize a list of vectors called pts and speed
ArrayList <PVector> pts = new ArrayList<PVector>();
ArrayList <PVector> speed = new ArrayList<PVector>();
//declare max and min bounds
float MAX = 500;
float MIN = -500;
float sound_delay = 0;
float point_distance;
void setup(){
size(900,900,P3D);
minim = new Minim(this);
sound = minim.getLineIn(Minim.STEREO, 1024);
cp5 = new ControlP5(this);
cp5.addSlider("point_distance")
.setPosition(40,40)
.setRange(0,100)
.setSize(200,20)
.setValue(50)
.setColorForeground(color(20,200,200))
.setColorLabel(color(255))
.setColorBackground(color(70,70,70))
.setColorValue(color(0,0,0))
.setColorActive(color(0,255,255))
;
//a loop that creates a vector for each sound sample
for(int i = 0; i < sound.mix.size(); i++){
//generate random positions for the points
float xp = random(MIN,MAX);
float yp = random(MIN,MAX);
float zp = random(MIN,MAX);
pts.add(new PVector(xp,yp,zp));
//generate random speed vectors to give the points movement
float xs = random(-1,1);
float ys = random(-1,1);
float zs = random(-1,1);
speed.add(new PVector(xs,ys,zs));
}
}
void draw(){
background(0);
//get the average sound_sample
float average_sound = 0;
for(int i = 0; i < sound.mix.size(); i++){
average_sound = average_sound + abs(sound.mix.get(i));
}
average_sound = average_sound/sound.mix.size();
average_sound = average_sound * 160;
//adding a sound delay
//sound delay will equal the highest average sound
///and then slowly count down
if(average_sound > sound_delay){
sound_delay = average_sound;
}
//slowly count down the sound delay
sound_delay = sound_delay *0.99;
//visualize the average sound per frame and the sound delay
fill(70,70,70);
noStroke();
rect(40,80,40,160);
fill(0,255,255);
rect(40,240,40,-average_sound);
fill(70,70,70);
noStroke();
rect(100,80,40,160);
fill(0,255,255);
rect(100,240,40,-sound_delay);
pushMatrix();
//call camera function
cam();
//place a point at each vector position
stroke(255);
strokeWeight(3);
for(int i = 0; i < sound.mix.size(); i++){
PVector p1 = pts.get(i);
PVector s1 = speed.get(i);
point(p1.x,p1.y,p1.z);
//add the speed vector to the current point position
p1.x = p1.x + s1.x;
p1.y = p1.y + s1.y;
p1.z = p1.z + s1.z;
//check to see if point vector is out of the sketch boundary
if(p1.x < MIN || p1.x > MAX){
s1.x = s1.x * -1;
}
if(p1.y < MIN || p1.y > MAX){
s1.y = s1.y * -1;
}
if(p1.z < MIN || p1.z > MAX){
s1.z = s1.z * -1;
}
//increase speed based on sound sample
float sound_sample = abs(sound.mix.get(i));
if(s1.x > 0){
s1.x = s1.x + sound_sample;
}else{
s1.x = s1.x - sound_sample;
}
if(s1.y > 0){
s1.y = s1.y + sound_sample;
}else{
s1.y = s1.y - sound_sample;
}
if(s1.z > 0){
s1.z = s1.z + sound_sample;
}else{
s1.z = s1.z - sound_sample;
}
//slow down the points to offset speed increases from sound (cooling down)
if(abs(s1.x) > 0.1){
s1.x = s1.x *0.99;
}
if(abs(s1.y) > 0.1){
s1.y = s1.y *0.99;
}
if(abs(s1.z) > 0.1){
s1.z = s1.z *0.99;
}
//replace point and speed vector with new values
pts.set(i,p1);
speed.set(i,s1);
}
///draw lines between points if they are within a certain distance
//this is a nested loop that cycles through the point list and for each point
//cyles through the list agin to query the distance between all of the other points
strokeWeight(1);
stroke(255,100);
for(int i = 0; i < sound.mix.size(); i++){
PVector p1 = pts.get(i);
for(int j = 0; j < sound.mix.size(); j++){
PVector p2 = pts.get(j);
float distance = dist(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z);
if(distance < sound_delay+point_distance){
line(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z);
}
}
}
popMatrix();
}
///camera functions
void cam() {
int newx = mouseX;
int newy = mouseY;
translate(width/2, height/2,zcam);
rotateY(rotx); rotateX(roty);
//rotateZ(PI);
if ((mousePressed == true) && mouseY > 180) {
rotx = rotx + (oldx-newx)/50.0;
roty = roty + (oldy-newy)/50.0;
}
oldx = newx;
oldy = newy;
}
void mouseWheel(MouseEvent event) {
float e = event.getCount();
zcam = zcam - e*5;
}