Performance Issues FFT

SkySky
edited July 2014 in Questions about Code

Hi there,

I'm new at Processing (but some knowledge at Java). I try to visualize some music but somehow i'm calling to much functions and if the middle frequencys starting, there are bad performance issues. Maybe someone can explain me where i did the mistake. Maybe i have to cut the unused Frequencys up to 20kHz. But i don't know how. (On my 8GB, Octacore, High-Graphic Computer are no Issues. They start at 4GB and without good graphiccards) Thanks a lot.

Can't use code-tags so uploaded on my server:Code

Sky

Answers

  • Here is the code:

    import ddf.minim.*;
    import ddf.minim.signals.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import controlP5.*;
    
    Minim minim;
    AudioPlayer player;
    FFT fft;
    ControlP5 cp5;
    
    float[] angle;
    float[] y, x;
    float r = PI/6;
    color[] c = new color[10];
    
    color neonColor;
    int colorCountIn, colorCountOut;
    
    int timing;
    
    int opacity = 0;
    // INTERACTION
    boolean isRotate = true;
    boolean isColor = true;
    boolean isForm = true;
    boolean isSec = false;
    
    void setup() {
      //P3D = 3D rendering, zur Ebenendarstellung
      //size(displayWidth/2, displayHeight/2, OPENGL);
      size(800, 600, P3D);
      minim = new Minim(this);
      cp5 = new ControlP5(this);
    
      //toggle für einen an/aus schalter
      cp5.addToggle("rotation")
        .setPosition(10, 35)
          .setSize(50, 20)
            .setValue(true)
              .setMode(ControlP5.SWITCH);
      cp5.addToggle("colorchange")
        .setPosition(10, 75)
          .setSize(50, 20)
            .setValue(true)
              .setMode(ControlP5.SWITCH);
      cp5.addToggle("formchange")
        .setPosition(10, 115)
          .setSize(50, 20)
            .setValue(true)
              .setMode(ControlP5.SWITCH);
      cp5.addToggle("morecircles")
        .setPosition(10, 155)
          .setSize(50, 20)
            .setValue(true)
              .setMode(ControlP5.SWITCH);
    
      // Slider für die Deckkraft der Außenlinien
      cp5.addSlider("opacity")
        .setPosition(10, 20)
          .setRange(0, 100);
    
      //Foto
      cp5.addButton("Foto")
        .setPosition(765, 565)
          .setImages(loadImage("icon.png"), loadImage("icon.png"), loadImage("icon.png"))
            .updateSize();
    
      //512 ist Buffergröße (1024 ist Standart)
      player = minim.loadFile("19. Freak on a leash (Dante Ross Mix).mp3", 512);
    
      //fft?
      fft =new FFT(player.bufferSize(), player.sampleRate());
      y = new float[fft.specSize()];
      x = new float[fft.specSize()];
      angle = new float[fft.specSize()];
      frameRate(40);
    
      player.play();
    
      c[0] = color(255, 153, 51);
      c[1] = color(0, 255, 0);
      c[2] = color(255, 0, 0);
      c[3] = color(0, 0, 255);
      c[4] = color(255, 255, 255);
      c[5] = color(255, 255, 0);
      c[6] = color(0, 255, 255);
      c[7] = color(255, 0, 255);
      c[8] = color(255, 0, 130);
      c[9] = color(0, 130, 255);
    }
    //Schalterfunktionen
    //Rotation
    void rotation(boolean first) {
      if (first==true) {
        isRotate=true;
        println("Drehung fortgesetzt");
      }
      else {
        isRotate=false;
        println("Drehung angehalten");
      }
    }
    //Farbe
    void colorchange(boolean second) {
      if (second==true) {
        isColor=true;
        println("Farbwechsel fortgesetzt");
      }
      else {
        isColor=false;
        println("Farbwechsel angehalten");
      }
    }
    //Form
    void formchange(boolean third) {
      if (third==true) {
        isForm=true;
      }
      else {
        isForm=false;
      }
      println("Formwechsel");
    }
    //mehr Kreise
    void morecircles(boolean fourth) {
      if (fourth==true) {
        isSec=false;
        println("Weniger Kreise");
      }
      else {
        isSec=true;
        println("Mehr Kreise");
      }
    }
    //Bild Speichern
    public void Foto(int theValue) {
      println("Foto geschossen");
      saveFrame("song-####.jpg");
    }
    
    void draw() {
      println(frameRate);
      //Mittelkreis
      noStroke();
      fill(0);
      rect(0, 0, width, height);
    
      pushMatrix();
      translate(width/2, height/2); //position
      if (isRotate) {
        r +=PI/50;
      }
      rotate(r);
    
      if (player.position()-timing > 5000 && isColor) {
        colorCountIn = (int)random(0, 9);
        colorCountOut = (int)random(0, 9);
        timing = player.position(); //damit die 5sec wieder gestartet werden
      }
    //j < menge der linien
      for (int j = 0; j < 200; j++) {
    
        if (player.mix.get(j) > 0.5) {
          rect(-width, -height, width*4, height*4);
        }
        //Farbe der Außenlinien
        if (player.mix.get(j)*200 > 29) {
        }
        else {
          if (isColor)
            neonColor = c[colorCountIn];
    
          stroke(neonColor, opacity);
        }
        //außenlinien
        float x1 = tan(j)*250;
        float y1 = sin(j)*1250;
    
        float x2 = sin(j)*abs(player.left.get(j))*200 + sin(j)*150;
        float y2 = sin(j)*abs(player.right.get(j))*220 + sin(j)*50;
    
        line(x1, y1, x2, y2);
      }
    
      for (int k = 360; k > 0; k--) {
        if (player.mix.get(k)*200 > 25) {
          stroke(neonColor = c[colorCountOut]);
        } 
        else {
          stroke(neonColor = c[colorCountIn]);
        }
    
        float dim = player.mix.get(k)*100;
        //innerer kreis
        //ellipse(sin(k)*50, sin(k)*50, dim, dim);
        float x1 = sin(k)*50;
        float y1 = sin(k)*50;
    
        float x2 = cos(k)*abs(player.left.get(k))*150 + cos(k)*50;
        float y2 = cos(k)*abs(player.right.get(k))*100 + sin(k)*50;
        if (isForm) {
          line(x1, y1, x2, y2 );
        }
        else { 
          ellipse(sin(k)*50, sin(k)*50, dim, dim);
        }
      }
      if (colorCountOut >= 9) { 
        colorCountOut=0;
      }
      if (colorCountIn >= 9) { 
        colorCountIn=0;
      }
      popMatrix();
    
      //äußere Kreise aufruf
      fft.forward(player.mix);
      doubleAtomicSprocket();
    }
    
    //äußere Kreise
    void doubleAtomicSprocket() {
      if (isSec) {
        noStroke();
        pushMatrix();
        translate(width/2, height/2);
        for (int i = 0; i < fft.specSize()/2 ; i++) {
          //y[i] = y[i] + fft.getBand(i)/100;
          x[i] = x[i] + fft.getFreq(i)/100;
          angle[i] = angle[i] + fft.getFreq(i)/1000;
          rotateX(sin(angle[i]/2));
          rotateY(cos(angle[i]/2));
          // stroke(fft.getFreq(i)*2,0,fft.getBand(i)*2);
         fill(fft.getFreq(i)*2, 0, fft.getBand(i)*2);
          pushMatrix();
          //Position der Kreise in Bezug den Umfang des Kreises
          translate((x[i]+250)%width/3, (y[i]+250)%height/3);
          //box(fft.getBand(i)/20+fft.getFreq(i)/15);
          sphere(fft.getBand(i)/20+fft.getFreq(i)/15);
          popMatrix();
        }
        popMatrix();
      }
    }
    
    void stop() {
      player.close();
      minim.stop();
      super.stop();
    }
    
  • Thanks :) know I need help with the mainproblem :D

  • _vk_vk
    edited July 2014

    The code run smooth here. No performance issues that i noticed... I used other music of course. Mac book pro 2.7 ghz i7. 16GB ram. Intel HD Graphics 4000 ...

    .

  • 16MB RAM? Is that from the '80s? ;)

  • hm thanks for the feedback. maybe there is a config issue on the test machine. i7 core with geforce 660M and 8GB Ram. But maybe there is a problem with win 8 and java. I'll give feedback tomorrow.

  • sorry, GB... 16GB of ram... : ) I'll edit above.

Sign In or Register to comment.