Help with Audio Visualizer

Hello! I am trying to create an audio visualizer. I am using live sound. The problem I keep coming into is the nullpointer exception error. Please take a look at this code and give me some tips on how to fix this! Thank you.

import ddf.minim.analysis.*; import ddf.minim.*;

Minim minim; AudioPlayer jingle; AudioInput input; FFT fft; int[][] colo = new int[100][10];

float color1 = 35;> float color2 = 45; float color3 = 65; float color4 = 75;

void setup() { size(512, 400); background(0); colorMode(HSB,100,100,100,100);

minim = new Minim(this);

input = minim.getLineIn();

fft = new FFT(input.bufferSize(), input.sampleRate());

}

void draw() { background(0); noStroke(); fill(0, 5); rect(0, 0, width, height); pushMatrix(); translate(width/2, height/2); rotate(radians(frameCount % 360 * 2));

fft.forward(input.mix);

for(int i = 0; i < fft.specSize(); i++) {

  if(jingle.mix.get(i)*200 > fft.specSize()) {
    stroke(color1,100,100);
  }
  else {
    stroke(color2,100,100);
  }

  line(cos(i)*50, sin(i)*50, cos(i)*abs(jingle.left.get(i))*200 + cos(i)*50, sin(i)*abs(jingle.right.get(i))*200 + sin(i)*50);
}

for(int j = fft.specSize(); j > 0; j--) {

    if(jingle.mix.get(j)*200 > fft.specSize()) {
    stroke(color3,100,100);
  }
  else {
    stroke(color4,100,100);
  }


  line(cos(j)*50, sin((j))*50, cos(j)*abs(jingle.right.get(j))*200 + cos(j)*50, sin(j)*abs(jingle.left.get(j))*200 + sin(j)*50);   
}

popMatrix(); }

void keyPressed() {

if(key == 'r') { color1 = 0; color2 = 5; color3 = 90; color4 = 95; }

if(key == 'g') { color1 = 35; color2 = 45; color3 = 65; color4 = 75; } }

Tagged:

Answers

  • step 1 - code almost unchanged but better formatted

    import ddf.minim.analysis.*; 
    import ddf.minim.*;
    
    Minim minim; 
    AudioPlayer jingle; 
    AudioInput input; 
    FFT fft; 
    int[][] colo = new int[100][10];
    
    float color1 = 35;
    float color2 = 45; 
    float color3 = 65; 
    float color4 = 75;
    
    void setup() {
    
      size(512, 400); 
      background(0); 
      colorMode(HSB, 100, 100, 100, 100);
    
      minim = new Minim(this);
      println("2");
      input = minim.getLineIn();
      println("3");
      fft = new FFT(input.bufferSize(), input.sampleRate());
      println("4");
    }
    
    void draw() {
    
      background(0);
    
      noStroke(); 
      fill(0, 5); 
      rect(0, 0, width, height);
    
      pushMatrix(); 
      translate(width/2, height/2); 
      rotate(radians(frameCount % 360 * 2));
    
      fft.forward(input.mix);
    
      for (int i = 0; i < fft.specSize(); i++) {
    
        if (jingle.mix.get(i)*200 > fft.specSize()) {
          stroke(color1, 100, 100);
        } else {
          stroke(color2, 100, 100);
        }
    
        line(cos(i)*50, sin(i)*50, 
          cos(i)*abs(jingle.left.get(i))*200 + cos(i)*50, sin(i)*abs(jingle.right.get(i))*200 + sin(i)*50);
      }
    
      for (int j = fft.specSize(); j > 0; j--) {
    
        if (jingle.mix.get(j)*200 > fft.specSize()) {
          stroke(color3, 100, 100);
        } else {
          stroke(color4, 100, 100);
        }
    
    
        line(cos(j)*50, sin((j))*50, 
          cos(j)*abs(jingle.right.get(j))*200 + cos(j)*50, 
          sin(j)*abs(jingle.left.get(j))*200 + sin(j)*50);
      }
    
      popMatrix();
    }
    
    void keyPressed() {
    
      if (key == 'r') { 
        color1 = 0; 
        color2 = 5; 
        color3 = 90; 
        color4 = 95;
      }
    
      if (key == 'g') { 
        color1 = 35; 
        color2 = 45; 
        color3 = 65; 
        color4 = 75;
      }
    }
    
  • edited September 2017
    import ddf.minim.analysis.*;
    import ddf.minim.*;
    
    BeatDetect heart = new BeatDetect();
    Minim       minim;
    AudioPlayer jingle;
    FFT         fft;
    
    void setup(){
      size(512, 200, P3D);
    
      minim = new Minim(this);
    
      // specify that we want the audio buffers of the AudioPlayer
      // to be 1024 samples long because our FFT needs to have 
      // a power-of-two buffer size and this is a good size.
      jingle = minim.loadFile("Forbidden Voices.mp3", 1024);
    
      // loop the file indefinitely
      jingle.loop();
    
      // 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 half as large.
      fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
    
    }
    
    void draw()
    {
      background(0);
      heart.detect( jingle.right );  
      if( heart.isOnset() ){
        background(255,0,0);
      }
    
      stroke(255);
    
      // perform a forward FFT on the samples in jingle's mix buffer,
      // which contains the mix of both the left and right channels of the file
      fft.forward( jingle.right );
    
      for(int i = 0; i < fft.specSize(); i++)
      {
        // draw the line for frequency band i, scaling it up a bit so we can see it
        line( i, height, i, height - fft.getBand(i)*8 );
      }
    }
    

    How could I change this into a 3D environment based on the FFT - I want to make a 3D spectral analysis graph of the audio.

  • there is a 3D version of line with xyz to xyz - see reference

    use another property of fft to use as Z-Value

  • I realize that the property would be inserted here:

    line(i, height, ____, height - fft.getBand(i)*8);

    I am wondering what the property would be?

Sign In or Register to comment.