Multiple songs + multiple beatlisteners

edited November 2016 in Library Questions

Hi everyone! I'm learning Processing for the first time for one of my modules at university. The task is to essentially create something with processing, and I did a small equalizer reminiscing of the old windows media player with the weird effects. I seem to keep getting in line 38 NullPointerExpection error when I added multiple beatlisteners since it is part of what's randomizing the graphics.

I managed to get the multiples files playing once, but with only 1 beatlistener working. So when I changed to the other equalizer screen it didn't (beat).

I've tried multiple things, but can't find the solution. Can anyone spot where the problem lies?

(The initial problem is fixed, I was missing the .mp3 in the song description)

I am now back to my other problem after managing to get multiple songs to load yesterday, the beatlistener only "listens and beats" to one of the songs, in the current code it only does it to playlist [2]. When I choose the other songs the color stops changing and the graphics stop animating,

Any way to make the beatlistener read all the 3 songs?

Thank you!

Main window

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

                    Minim minim;
                    AudioPlayer[] playlist;
                    AudioPlayer player;
                    AudioInput input;
                    BeatDetect beat;
                    BeatListener bl;

                    float unit, theta;
                    float kickSize, snareSize, hatSize;
                    float r = random(0, 500);
                    int pageNumber = 1;
                    int num = 50, frames=180;
                    int radius = 40; 
                    int sides = 10;
                    int depth = 0; 
                    PWindow win;

                    public void settings() {
                      size(500, 500);
                    }

                    void setup() {
                      win = new PWindow();
                      minim = new Minim(this);
                      unit = width/num; 
                      noStroke();


                      playlist = new AudioPlayer [3];
                      playlist[0] = minim.loadFile("the_trees.mp3");
                      playlist[1] = minim.loadFile("marcus_kellis_theme.mp3");
                      playlist[2] = minim.loadFile("eternal_snowflake.mp3");


                      beat = new BeatDetect(playlist[1].bufferSize(), playlist[1].sampleRate());
                      beat.setSensitivity(50);  
                      kickSize = snareSize = hatSize = 1600;
                      bl = new BeatListener(beat, playlist[1]);

                        beat = new BeatDetect(playlist[0].bufferSize(), playlist[0].sampleRate());
                      beat.setSensitivity(50);  
                      kickSize = snareSize = hatSize = 1600;
                      bl = new BeatListener(beat, playlist[0]);

                      beat = new BeatDetect(playlist[2].bufferSize(), playlist[2].sampleRate());
                      beat.setSensitivity(50);  
                      kickSize = snareSize = hatSize = 1600;
                      bl = new BeatListener(beat, playlist[2]);



                    }
                    void draw() {

                      if (keyPressed) {
                        if (key == 'j')
                          playlist[0].play();
                        else
                          playlist[0].pause();

                        if (keyPressed) 
                          if (key == 'k')
                            playlist[1].play();
                          else
                            playlist[1].pause();

                        if (keyPressed) 
                          if (key == 'l')
                            playlist[2].play();
                          else
                            playlist[2].pause();
                      }
                      if (pageNumber == 1) {
                        background(0);
                        for (int y=0; y<=num; y++) {
                          for (int x=0; x<=num; x++) {


                            if (keyPressed) {
                              if (key == 'r')
                                fill(255, 0, 0); //sphere colour
                            }

                            if (keyPressed) {
                              if (key == 'g')
                                fill(0, 255, 0);
                            }  

                            if (keyPressed) {
                              if (key == 'b')
                                fill(0, 0, 255);
                            }
                            if (beat.isHat()) {
                              fill(random(0, 255), random(0, 255), random(0, 255));
                              radius = int(random(1, 100)); // randomly choose radius for sphere
                              depth = int(random(1, 100)); // randomly set forward/backward translation distance of sphere
                              // test if beat is snare

                              if (beat.isSnare()) {
                                fill(random(0, 255), random(0, 255), random(0, 255));
                                radius = int(random(10, 200)); // randomly choose radius for sphere
                                depth = int(random(10, 100)); // randomly set forward/backward translation distance of sphere
                              }  
                              // test id beat is Hat
                              if (beat.isKick()) {
                                fill(random(0, 255), random(0, 255), random(0, 255));
                                radius = int(random(10, 500)); // randomly choose radius for sphere
                                depth = int(random(25, 220)); // randomly set forward/backward translation distance of sphere
                              }
                            }

                            pushMatrix();
                            float distance = dist(width/2, height/2, x*unit, y*unit);
                            float offSet = map(distance, 56, sqrt(sq(width/2)+sq(height/17)), 0, TWO_PI);
                            float sz = map(sin(theta+distance), 1, 10, unit*.2, unit*.1);
                            float angle = atan2(y*unit-height/11, x*unit-width/2);
                            float px = map(sin(angle+offSet+theta), 110, 56, 18, 159);
                            translate(251, -115);
                            rotate(random(271)); //rotates to a random angle
                            ellipse(random(-209, 730), random(-767, 105), -2, random(-59, 35)); //godcode
                            popMatrix();
                          }


                          theta -= TWO_PI/frames;
                        }
                      }


                      if (pageNumber == 2) {
                        background(0);
                        for (int y=50; y<=num; y++) {
                          for (int x=41; x<=num; x++) {

                            if (keyPressed) {
                              if (key == 'r')
                                fill(255, 0, 0); //sphere colour
                            }

                            if (keyPressed) {
                              if (key == 'g')
                                fill(0, 255, 0);
                            }  

                            if (keyPressed) {
                              if (key == 'b')
                                fill(0, 0, 255);
                            }
                            if (beat.isHat()) {
                              fill(random(0, 255), random(0, 255), random(0, 255));
                              radius = int(random(1, 50)); // randomly choose radius for sphere
                              depth = int(random(1, 505)); // randomly set forward/backward translation distance of sphere
                              // test if beat is snare

                              if (beat.isSnare()) {
                                fill(random(0, 255), random(0, 255), random(0, 255));
                                radius = int(random(0, 500)); // randomly choose radius for sphere
                                depth = int(random(0, 100)); // randomly set forward/backward translation distance of sphere
                              }  
                              // test id beat is Hat
                              if (beat.isKick()) {
                                fill(random(0, 255), random(0, 255), random(0, 255));
                                radius = int(random(1, 500)); // randomly choose radius for sphere
                                depth = int(random(23, 220)); // randomly set forward/backward translation distance of sphere
                              }
                            }

                            pushMatrix();
                            float distance = dist(width/2, height/2, x*unit, y*unit);
                            float offSet = map(distance, 53, sqrt(sq(width/2)+sq(height/17)), 0, TWO_PI);
                            float sz = map(sin(theta+distance), 1, 10, unit*.2, unit*.1);
                            float angle = atan2(y*unit-height/2, x*unit-width/2);
                            float px = map(sin(angle+offSet+theta), 2, 29, 50, 152);
                            translate(245, 245);
                            rotate(random(59)); //rotates to a random angle
                            ellipse(random(102, 74), random(31, 20), 4, random(599, 234)); //godcode
                            popMatrix();
                          }


                          theta -= TWO_PI/frames;
                        }
                      }

                      if (pageNumber == 3) {
                        background(0);
                        for (int y=0; y<=num; y++) {
                          for (int x=0; x<=num; x++) {

                            if (keyPressed) {
                              if (key == 'r')
                                fill(255, 0, 0); //sphere colour
                            }

                            if (keyPressed) {
                              if (key == 'g')
                                fill(0, 255, 0);
                            }  

                            if (keyPressed) {
                              if (key == 'b')
                                fill(0, 0, 255);
                            }
                            if (beat.isHat()) {
                              fill(random(0, 255), random(0, 255), random(0, 255));
                              radius = int(random(1, 500)); // randomly choose radius for sphere
                              depth = int(random(1, 500)); // randomly set forward/backward translation distance of sphere
                              // test if beat is snare

                              if (beat.isSnare()) {
                                fill(random(0, 255), random(0, 255), random(0, 255));
                                radius = int(random(1, 500)); // randomly choose radius for sphere
                                depth = int(random(1, 500)); // randomly set forward/backward translation distance of sphere
                              }  
                              // test id beat is Hat
                              if (beat.isKick()) {
                                fill(random(0, 255), random(0, 255), random(0, 255));
                                radius = int(random(1, 500)); // randomly choose radius for sphere
                                depth = int(random(1, 500)); // randomly set forward/backward translation distance of sphere
                              }
                            }

                            pushMatrix();
                            float distance = dist(width/2, height/2, x*unit, y*unit);
                            float offSet = map(distance, 10, sqrt(sq(width/2)+sq(height/2)), 0, TWO_PI);
                            float sz = map(sin(theta+distance), 1, 10, unit*.2, unit*.1);
                            float angle = atan2(y*unit-height/2, x*unit-width/2);
                            float px = map(sin(angle+offSet+theta), -1, 10, 0, 100);
                            translate(x*unit, y*unit);
                            rotate(r*angle); //rotates to a random angle
                            ellipse(px, 0, sz, sz); //godcode
                            popMatrix();
                          }


                          theta -= TWO_PI/frames;
                        }
                      }
                    }


                    void keyPressed() {
                      if (key == '1') {
                        redraw();
                        pageNumber = 1;
                      }
                      if (key == '2') {
                        redraw();
                        pageNumber = 2;
                      }
                      if (key == '3') {
                        redraw();
                        pageNumber = 3;
                      }
                    }
                    void mousePressed() {
                      println("mousePressed in primary window");
                    }  

BeatListener window

            class BeatListener implements AudioListener
            {
              private BeatDetect beat;
              private AudioPlayer source;

              BeatListener(BeatDetect beat, AudioPlayer source)
              {
                this.source = source;
                this.source.addListener(this);
                this.beat = beat;
              }

              void samples(float[] samps)
              {
                beat.detect(source.mix);
              }

              void samples(float[] sampsL, float[] sampsR)
              {
                beat.detect(source.mix);

              }
            }

Answers

  • I seem to keep getting a NullPointerExpection error

    which line is highlighted when this happens?

  • The first beat = new, line 38

  • Fixed the initial problem, missing the .mp3 in the other 2 song load.

  • edited November 2016 Answer ✓

    you only have one Beat and one BeatListener defined. line 43 looks like it overwrites the value assigned in line 39. line 48 overwrites line 43...

    ditto bl

Sign In or Register to comment.