We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › 3D Music Map
Page Index Toggle Pages: 1
3D Music Map (Read 2742 times)
3D Music Map
Jan 14th, 2009, 6:43am
 
Hello,
Though this is my first post, I am not new to processing. I first started processing 4 months ago and absolutely love it!! Here is a 3D visualizer for music. The next step i'm embarking on is making a flying plane that you must help through the maze of mountains and valleys (created by the visualizer). Level difficulty will be based on how close you are to the ground.
I need and comments on optimization (or different libraries). Minim is awesome but SLOWWWWW. THanx!
Just copy paste to sketch and put a .mp3 file in the sketch folder and rename "UntrustUs.mp3" to your mp3 file name


Code:


//Libraries (OPENGL better than P3D)
import processing.opengl.*;
import ddf.minim.analysis.*;
import ddf.minim.*;
//Global Variables
Minim minim;          //Minim object
AudioPlayer jingle;   //AudioPlayer object that holds sound
FFT fft;              //FFT (Ferrier Form Transformation) Object that hold band data
int numS = 70;        //Const that dictates how many bands will get read
System s;             //An arraylist object that holds all of the WaveForm's and parses through them
float zoom = 100;     //A variable that controls the starting zoom
float sf = 0;         //a variable used in garbage control

//Setup
void setup() {
 size(1000, 600, OPENGL);
 minim = new Minim(this);                        //instantiate the Minim master object  
 jingle = minim.loadFile("UntrustUs.mp3", 1024); //load song file
 jingle.loop();                                  //loop song file
 fft = new FFT(jingle.bufferSize(), jingle.sampleRate()); // create an FFT object that has a time-domain buffer the same size as jingle's sample buffer
                                                          // note that this needs to be a power of two and that it means the size of the spectrum
                                                          // will be 512.
 s = new System();                              //make a new system
}

void draw()
{
 WaveForm w = new WaveForm(numS);              //make a new WaveForm object every time you draw. This object is then passed to the system which draws it and all of the existing waveforms
 background(200);  
 pointLight(51, 102, 255, 65, 60, 100);        //Some 3D lightning for neat effect
 pointLight(200, 40, 60, -65, -60, -150);  
 ambientLight(70, 70, 10);                     // Raise overall light in scene
 
 translate(width/2,height/2,-zoom);            //Translation and rotation stuff. A lot of this is just experimenting and tweaking
 rotateY((0.0-((float)mouseX/(float)width)+0.5)*PI);
 rotateX(PI/2.3);
 rotateX((((float)mouseY/(float)width)-0.5)*(PI/2));
 
 fft.forward(jingle.mix);                      //get the data
 for(int i = 0; i < numS; i++)  {
   w.importBand(fft.getBand(i), i);            //pass the data to the WaveForm
 }
 s.addWave(w);                                 //Add the WaveForm to the System
 //fill(200,0,0);
 noStroke();                                  
 s.render();
 //Garbage collection
 sf += .01;
 if (sf > .75) {
   s.remWave(s.waves.size() - 1);
   sf = 1.01;
 }
 //end of Draw  
}

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

//Zooming control. o = zoomout   i = zoomin
void keyPressed() {
 if (key == 'o') {
   zoom += 50;
 }
 if (key == 'i') {
   zoom -= 50;
 }
}

//WaveForm class
class WaveForm {
 float[] bandMap; //holds all of the getBand() numbers from FFT
 int sz;          //size of the array
 //Constructor
 WaveForm(int _sz) {
   sz = _sz;
   bandMap = new float[sz];
 }
 //place value to a position in the array
 void importBand(float i, int place) {
   bandMap[place] = i;
 }
}


//System Class
class System {
 ArrayList waves;
 float xyScale = 20.0f; //Contracts or expands flowing shape

 System() {  
   waves = new ArrayList();
 }
 
 //Render waves
 void render() {
   pushMatrix();
   //colorMode(HSB, 255);
   translate(-xyScale * 8, 0, -100);
   //Basically, this takes two waves (going through the whole array of course and creates and bunch of
   //rectangles between them to synthesize a flowing shape. It really is a lot scarier than it looks.
   for (int y = 1; y < waves.size(); y++) {
     WaveForm w1 = (WaveForm) waves.get(y-1); //The two waves
     WaveForm w2 = (WaveForm) waves.get(y);
     beginShape(QUADS);                       //Making sure th rendered creates squares (i think this code could be optimized to use TRIANGLE_STRIP)
     for(int x=1; x < w1.sz; x++) {
       //fill(w1.bandMap[x]  * 6, 255, 50);
       vertex(x*xyScale,y*xyScale, w1.bandMap[x-1]);
       vertex(x*xyScale,(y+1)*xyScale,w2.bandMap[x-1]);
       vertex((x+1)*xyScale,(y+1)*xyScale,w2.bandMap[x]);
       vertex((x+1)*xyScale,y*xyScale,w1.bandMap[x]);
     }
     endShape();
   }
   popMatrix();
   
 }

 //adds a WaveForm object to the front of the list
 void addWave(WaveForm w) {
   waves.add(0,w);
 }
 
 //removes a WaveForm object from the index i (this is used only in the Garbage collection area in draw)
 void remWave(int i) {
   waves.remove(i);
 }
}
//The End
Re: 3D Music Map
Reply #1 - Jan 14th, 2009, 7:35am
 
Don't be lazy! It's pretty cool when you try it!
Re: 3D Music Map
Reply #2 - Jan 14th, 2009, 9:35pm
 
I like it. Thanks for sharing.I'll dig into the code when I have some time.

No criticism, but might I recommend a little more control over the 3D position and a more interesting background than grey Smiley

Tim
Re: 3D Music Map
Reply #3 - Jan 15th, 2009, 6:26am
 
Thanx for the comments!
Yeah, im implementing some moving average algorithms to smooth the peaks. Otherwise, ive use this fill (inside the begin shape method :
fill(w1.bandMap[x]  * 6, 255, 50);
Next step is (like youve said) to improve the camera movement (btw you can zoom in and out by pressing 'i' or 'o' respectively).
Thanx again!
Re: 3D Music Map
Reply #4 - Jan 22nd, 2009, 6:09pm
 
it's pretty great, i like it.
Re: 3D Music Map
Reply #5 - Jan 24th, 2009, 7:24pm
 
I get the following error when I try to run your code:

Semantic Error: No constructor with signature "Minim(Temporary_1028_9852)" was found in type "ddf.minim.Minim".

I think it's something basic, but I'm really new to processing, so any help is appreciated!
Re: 3D Music Map
Reply #6 - Feb 12th, 2009, 11:35pm
 
do you have minim properly linked? You have to put it in your libraries (not lib) folder. If this doesn't work redownload minim. Make sure you are using Processing 1.0!
Hope it works!!!!
Re: 3D Music Map
Reply #7 - Mar 14th, 2009, 9:50pm
 
Hey. Love the audio visualizer. Looks great. I'm doing some test to try to export a frame of the visualizer, but I'm having some trouble. Do you have any ideas. I've tried using beginRaw() and endRaw(), but it doesn't save any of the color information. It just spits out an outline of the wave form. Any help would be great. Thanks.

A.M.
Re: 3D Music Map
Reply #8 - Mar 14th, 2009, 10:20pm
 
I'm just now getting into looking at your code after playing with this when you first posted. This is hugely helpful because I want to do stuff that uses this idea as a starting point.

Thanks!
Page Index Toggle Pages: 1