Cong Box v1. Another example of sound interacting with shape generation.

Feb 18, 2011   //   by J.Boston   //   Interactive Programs, Portfolio, Processing Sketches  //  No Comments

This example is using OPENGL to generate a cube.
Using the minim library I have integrated sound to make it decrease and increase in size.
Below is the source code:

/**I created a cube that is colored 
using RGB values when settings the vertex positions. 
I am using the minim library to change the size of the cube.
I also textured the cube in Normalized mode.
On top of this I left two sides untextured to give a cool glitchy seethrough effect.
LIKE A G6.

This 3d shape interacts with sound and is textured. 
**/


import processing.opengl.*;
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

int si = 30;
PFont fontA;
int var1 = 200; 
PImage img;
Minim minim;
AudioOutput out;
SineWave sine;
AudioPlayer music;


float xmag, ymag = 0;
float newXmag, newYmag = 0, i;
float il = 0;
float il2 = 0;
 
void setup() 
{ 
  size(500, 500, OPENGL); 
  strokeWeight(3);
  stroke(0); 
  textureMode(NORMALIZED);
  img = loadImage("img.jpg");
  minim = new Minim(this);
  music = minim.loadFile("music.mp3", 2048);
  // get a line out from Minim, default sample rate is 44100, default bit depth is 16
  out = minim.getLineOut(Minim.STEREO, 2048);
  fontA = loadFont("HERO.vlw");
 
  // create a sine wave Oscillator, set to 440 Hz, 
//at 0.5 amplitude, sample rate 44100 to match the line out
  //sine = new SineWave(440, 0.5, out.sampleRate());
  // add the oscillator to the line out
  //out.addSignal(sine);
  music.play();
} 
 
void draw() 
{ 
  textFont(fontA, 32);
  stroke(int(random(255)), int(random(255)), int(random(255)));
  
  if(frameCount % 5 == 0){
    background(255);
  }
  if(frameCount % 1700 < 20 && frameCount > 1700){
    //this creates a really awesome feedback glitch
    music = minim.loadFile("music.mp3", 2048);
    music.play();
  }
  println(frameCount);
  pushMatrix(); 
 
  translate(width/2, height/2, -30); 
  
  newXmag = mouseX/float(width) * TWO_PI;
  newYmag = mouseY/float(height) * TWO_PI;
  
  float diff = xmag-newXmag;
  if (abs(diff) >  0.01) { xmag -= diff/4.0; }
  
  diff = ymag-newYmag;
  if (abs(diff) >  0.01) { ymag -= diff/4.0; }
  
  rotateX(-xmag); 
  rotateY(-ymag); 
  
  
  il = music.left.get(1)*100;
  il2 = music.right.get(1)*100;
  float outs = il+il2;
  if(outs < 40){
    outs = 40;
  }
  
  scale(outs);
  beginShape(QUADS);
  texture(img);
  vertex(-1,  1,  1, 0, 0);
  vertex( 1,  1,  1, 1, 0);
  vertex( 1, -1,  1, 1, 1);
  vertex(-1, -1,  1, 0, 1);

  vertex( 1,  1,  1, 0, 0);
  vertex( 1,  1, -1, 1, 0);
  vertex( 1, -1, -1, 1, 1);
  vertex( 1, -1,  1, 0, 1);

  vertex( 1,  1, -1, 0, 0);
  vertex(-1,  1, -1, 1, 0);
  vertex(-1, -1, -1, 1, 1);
  vertex( 1, -1, -1, 0, 1);

  vertex(-1,  1, -1, 0, 0);
  vertex(-1,  1,  1, 1, 0);
  vertex(-1, -1,  1, 1, 1);
  vertex(-1, -1, -1, 0, 1);

  vertex(-1,  1, -1);
  vertex( 1,  1, -1);
  vertex( 1,  1,  1);
  vertex(-1,  1,  1);

  vertex(-1, -1, -1);
  vertex( 1, -1, -1);
  vertex( 1, -1,  1);
  vertex(-1, -1,  1);

  endShape();
  
  
  popMatrix(); 
  
  fill( int(random(255)), int(random(255)), int(random(255)));
  stroke( int(random(255)), int(random(255)), int(random(255)));
  text("Cong   Box        v1.0", width/2-100, 30 );
}



void stop()
{
  // always close Minim audio classes when you are done with them
  out.close();
  minim.stop();
 
  super.stop();
}


void musicalVar()
{
  stroke(255);
  // draw the waveforms
  for(int i = 0; i < out.bufferSize() - 1; i++)
  {
    line(i, 50 + out.left.get(i)*50, i+1, 50 + out.left.get(i+1)*50);
    line(i, 150 + out.right.get(i)*50, i+1, 150 + out.right.get(i+1)*50);
  }
}



Leave a comment

You must be logged in to post a comment.

Get In touch!

contact@jaboston.com
Add me on LinkedIn
Check out my Illustrations

Long cat