Visualizer dont work

edited January 2017 in Library Questions

Hey i got a problem with my visualizer

When i use this code I can select a MP3 file but the Visualizer didn´t show the line and only play the Sound

  import ddf.minim.*;
  import ddf.minim.analysis.*;
  import ddf.minim.effects.*;
  import ddf.minim.signals.*;
  import ddf.minim.spi.*;
  import ddf.minim.ugens.*;
  String path;

  Minim minim;
  AudioPlayer Player;
  int farbe;
  PImage playPause;
  PImage hintergrund;

  void setup() {
    size(500,400);
    background(0);
    minim = new Minim(this);
    //Player = minim.loadFile(path); // Spielt die Mp3 datei ab
    //Player.loop(); // Wiederholt den aktuellen Song
    farbe = 0;
    smooth();
    playPause = loadImage("play3.jpg");
    hintergrund = loadImage("hintergrund2.jpg");
  }

  void selected(File song){
    path = song.getAbsolutePath();
    }

  void interrupt(){
    while (path == null){
      loop();
    }
     noLoop();
    }

  void draw()  {
    selectInput("Suche einen Song aus","selected");
    interrupt();
    background(0);
    Player = minim.loadFile(path); // Spielt die Mp3 datei ab
    Player.play();
    image(hintergrund,0,0);
    image(playPause,25,25);
    translate(width/2, height/2);
    rotate(radians(frameCount % 360 * 2));
    colorMode(HSB);
    stroke(farbe, 255, 255);
    colorMode(RGB);

    for(int j = 0; j < 360; j++){

      if(Player.mix.get(j)*200 > 50) {
        colorMode(HSB);
        stroke(farbe, 255,255);
        colorMode(RGB);
      }
      else {
        colorMode(HSB);
        stroke(farbe,255,255);
        colorMode(RGB);
      }
      line(cos(j)*100, sin(j)*100, cos(j)*abs(Player.left.get(j))*250 + cos(j)*100, sin(j)*abs(Player.right.get(j))*250 + sin(j)*100);
    }

      //for(int i = 0; i < fft.avgSize(); i++){
      //line((i * w)*3, height/1.1, (i * w)*3, height/1.1 - fft.getAvg(i) * 0.5);

    farbe += 2;
    if( farbe > 255)
    {
      farbe = 0;
    }
  // #########MOUSE############
      if (mousePressed && (mouseButton == LEFT)) {
      Player.pause();
    } else if (mousePressed && (mouseButton == RIGHT)) {
      Player.play();
    }

    }

But when i use this code Visualizer show me lines and effects why? here the visualizer got a MP3 in the loadFile but i need a way to choose the mp3 file

      import ddf.minim.*;
      import ddf.minim.analysis.*;
      import ddf.minim.effects.*;
      import ddf.minim.signals.*;
      import ddf.minim.spi.*;
      import ddf.minim.ugens.*;
      String path;

      Minim minim;
      AudioPlayer Player;
      int farbe;
      PImage playPause;
      PImage hintergrund;

      void setup() {
        size(500,400);
        background(0);
        minim = new Minim(this);
        Player = minim.loadFile("bla.mp3"); // Spielt die Mp3 datei ab
        Player.loop(); // Wiederholt den aktuellen Song
        farbe = 0;
        smooth();
        playPause = loadImage("play3.jpg");
        hintergrund = loadImage("hintergrund2.jpg");
      }

     /* void selected(File song){
        path = song.getAbsolutePath();
        }

      void interrupt(){
        while (path == null){
          loop();
        }
         noLoop();
        } */

      void draw()  {
        //selectInput("Suche einen Song aus","selected");
        //interrupt();
        background(0);
       // Player = minim.loadFile(path); // Spielt die Mp3 datei ab
      //  Player.play();
        image(hintergrund,0,0);
        image(playPause,25,25);
        translate(width/2, height/2);
        rotate(radians(frameCount % 360 * 2));
        colorMode(HSB);
        stroke(farbe, 255, 255);
        colorMode(RGB);

        for(int j = 0; j < 360; j++){

          if(Player.mix.get(j)*200 > 50) {
            colorMode(HSB);
            stroke(farbe, 255,255);
            colorMode(RGB);
          }
          else {
            colorMode(HSB);
            stroke(farbe,255,255);
            colorMode(RGB);
          }
          line(cos(j)*100, sin(j)*100, cos(j)*abs(Player.left.get(j))*250 + cos(j)*100, sin(j)*abs(Player.right.get(j))*250 + sin(j)*100);
        }

          //for(int i = 0; i < fft.avgSize(); i++){
          //line((i * w)*3, height/1.1, (i * w)*3, height/1.1 - fft.getAvg(i) * 0.5);

        farbe += 2;
        if( farbe > 255)
        {
          farbe = 0;
        }
      // #########MOUSE############
          if (mousePressed && (mouseButton == LEFT)) {
          Player.pause();
        } else if (mousePressed && (mouseButton == RIGHT)) {
          Player.play();
        } 
        }

Please help me guys i need the filepath way for my university plx plx plx :(

Tagged:

Answers

  • selectInput shouldn't be in draw(). draw() is called 60 times a second.

    similarly don't load anything in draw(). calling loadFile() 60 times a second is ridiculous. load it in setup(). make it a global variable, like the example does.

  • edited January 2017

    Hey selectInput is now in setup() my visualizer is still freezing and lines are not showing up

    what do you mean with global variable

    greetings

  • ok, it's already global

    line 10 defines an AudioPlayer (call this player rather than Player, capitalised names in java are used for classes, not members)

    this is outside of setup() and any other method so it is global

    the working version loads the file in setup(). you want the user to pick a file so you need some logic in there to stop everything until he has. this is further complicated by the fact that the user can dismiss the selection box...

    something like this?

    import ddf.minim.*;
    
    Minim minim;
    AudioPlayer player = null;
    
    void setup() {
      size(500,400);
      minim = new Minim(this);
      frameRate(2); // slow, so we can see what's happening
    }
    
    void selected(File song){
      if (song != null) {
        String path = song.getAbsolutePath();
        println("Selected: " + path);
        player = minim.loadFile(path);
        player.play();
      }
      loop(); // restart draw()
    }
    
    void draw() {
      if (player == null) {
        // nothing has been chosen yet, so ask user to pick something
        selectInput("Suche einen Song aus", "selected"); // see above
        noLoop(); // stop draw() until something is selected
      }
      println("playing...", frameCount);
      // line display code here
    }
    
  • edited January 2017

    i did what you say and an error shows up called NullPointer Exception

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    AudioPlayer player = null;
    
    String path;
    int farbe;
    
    PImage playPause;
    PImage hintergrund;
    
    void setup() {
        size(500,400);
        minim = new Minim(this);
        farbe = 0;
        playPause = loadImage("play3.jpg");
        hintergrund = loadImage("hintergrund2.jpg");
    }
    
    void selected(File song){
        if (song != null){
          String path = song.getAbsolutePath();
          println("Selected: " + path);
          player = minim.loadFile(path);
          player.play();
      }
         loop();
    }
    
    void draw()  {
      if (player == null) {
        selectInput("Suche einen Song aus", "selected");
        noLoop();
    }
        println("playing...", frameCount);
        background(0);
        image(hintergrund,0,0);
        image(playPause,25,25);
        translate(width/2, height/2);
        rotate(radians(frameCount % 360 * 2));
        colorMode(HSB);
        stroke(farbe, 255, 255);
        colorMode(RGB);
    
        **for(int j = 0; j < 360; j++){   
    
          Here is the Error
          if(player.mix.get(j)*200 > 50) {
            colorMode(HSB);
            stroke(farbe, 255,255);
            colorMode(RGB);**
    }
          else {
            colorMode(HSB);
            stroke(farbe,255,255);
            colorMode(RGB);
      }
          line(cos(j)*100, sin(j)*100, cos(j)*abs(player.left.get(j))*250 + cos(j)*100, sin(j)*abs(player.right.get(j))*250 + sin(j)*100);
    }
        farbe += 2;
        if( farbe > 255)
        {
          farbe = 0;
    }
       // #########MOUSE############
          if (mousePressed && (mouseButton == LEFT)) {
          player.pause();
        } else if (mousePressed && (mouseButton == RIGHT)) {
          player.play();
      }  
    }
    
  • ah, yes, player can be null there. the draw() loop doesn't stop just because i call noLoop(), it only stops after the current call.

    add a return; after line 38. that way draw() WILL stop if player is still null.

  • btw ctrl-t in the editor will indent it nicely.

    if code is hard to read it's harder to debug.

  • fixxed the problem here is the working code

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    AudioPlayer player;
    
    int farbe;
    PImage playPause;
    PImage hintergrund;
    
    void set_song(File song){
        if ( song != null ) {
            player = minim.loadFile(song.getAbsolutePath());
            player.play();
            player.loop();
        }
        loop();
    } 
    
    void setup() {
        // resize window
        size(500,400);
    
        // init
        minim = new Minim(this);
        player = null;
        farbe = 0;
    
        // load images
        playPause = loadImage("play3.jpg");
        hintergrund = loadImage("hintergrund2.jpg");
    }
    
    
    void draw()  {
        // if we have no song loaded yet, load one...
        if ( player == null ) {
          noLoop();
          selectInput("Suche einen Song aus", "set_song");
        }
    
       // background(0);
    
        // load images only if they have been loaded
        if ( hintergrund != null ) {
            image(hintergrund,0,0);
        }
        if ( playPause != null ) {
            image(playPause,25,25);
        }
    
        translate(width/2, height/2);
        rotate(radians(frameCount % 360 * 2));
        colorMode(HSB);
        stroke(farbe, 255, 255);
        colorMode(RGB);
    
        // only draw if we have a valid player
        if ( player != null ) {
            for(int j = 0; j < 360; j++){
                if ( player.mix.get(j)*200 > 50 ) {
                    colorMode(HSB);
                    stroke(farbe, 255,255,255);
                    colorMode(RGB);
                }
                else {
                    colorMode(HSB);
                    stroke(farbe,255,255,255);
                    colorMode(RGB);
                }
                line(cos(j)*100, sin(j)*100, cos(j)*abs(player.left.get(j))*250 + cos(j)*100, sin(j)*abs(player.right.get(j))*250 + sin(j)*100);
            }
        }
    
        farbe += 2;
        if( farbe > 255)
        {
            farbe = 0;
        }
    
       // #########MOUSE############
        if ( player != null ) {
            if (mousePressed && (mouseButton == LEFT)) {
                player.pause();
            } else if (mousePressed && (mouseButton == RIGHT)) {
                player.play();
            }
        }
    }
    
  • I arranged your code a bit. You will need to change the name of your image files

    Kf

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    AudioPlayer player;
    
    int farbe;
    PImage playPause;
    PImage hintergrund;
    
    void set_song(File song) {
      if ( song != null ) {
        player = minim.loadFile(song.getAbsolutePath());
        player.play();
        player.loop();
      }
      loop();
    } 
    
    void setup() {
      // resize window
      size(500, 400);
    
      // init
      minim = new Minim(this);
      player = null;
      farbe = 0;
    
      // load images
      playPause = loadImage("dance.jpg");
      hintergrund = loadImage("images.jpg");
    
      image(playPause, 25, 25, width-50, height-50);
      colorMode(HSB);
      noLoop();
    }
    
    
    void draw() {
      // if we have no song loaded yet, load one...
      if ( player == null ) {    
        selectInput("Suche einen Song aus", "set_song");
        return;
      }
    
    
      // load images only if they have been loaded
      if ( player.isPlaying() ) {
        image(hintergrund, 25, 25, width-50, height-50);
      } else {
        image(playPause, 25, 25, width-50, height-50);
      }
    
      translate(width/2, height/2);
      rotate(radians(frameCount % 360 * 2));
    
      // only draw if we have a valid player
      if ( player != null ) {
        for (int j = 0; j < 360; j++) {      
          stroke(farbe, 255, 255, 255);      
          line(cos(j)*100, sin(j)*100, cos(j)*abs(player.left.get(j))*250 + cos(j)*100, sin(j)*abs(player.right.get(j))*250 + sin(j)*100);
        }
      }
    
      farbe += 2;
      if ( farbe > 255)
      {
        farbe = 0;
      }
    }
    
    void mousePressed() {
    
      if (player==null)
        return;
    
      if (mouseButton == LEFT) 
        player.pause();
    
      if (mouseButton == RIGHT)      
        player.play();
    }
    
Sign In or Register to comment.