How to loop the sound ?

edited October 2013 in Library Questions

Hi, Can anyone suggest me how to do looping of the sound. I know there is method called rewind and I used it but the problem is I have tried placing player.rewind() every possible place in the code but it is not looping the audio.

Click on Download for application

        import Blobscanner.*; 
        import java.util.*;
        import java.util.Map.*;
        //import java.util.Map.Entry;
        PGraphics testLayer;
        PImage img, colimg;
        Detector bs;
        PVector myinterest_key;
        import ddf.minim.*;
        Minim minim;
        AudioPlayer[] player = new AudioPlayer[16];
        LinkedHashMap<PVector, Integer> blobValues = new LinkedHashMap< PVector, Integer>();
        void setup() {
          size(400, 400);
          testLayer = createGraphics(width, height);
          bs = new Detector(this, 0, 0, 400, 400, 255);
          testLayer.beginDraw();
          testLayer.endDraw();
          //----------------------------------------------------
          minim = new Minim(this); // initialaizing minim object
          for (int i=0; i<player.length;i++) {
            //String str = "Incredibox.com_"+i+".mp3"; // file name & path
            String str = i+".mp3";
            player[i] = minim.loadFile(str); // load file in audio player array loadFile ( "FILE NAME");
          }  
          //----------------------------------------------------
        }
        color lastcol = color(255);
        color c;
        int R, G, B;
        void draw() {
          background(255);
          //--------- RANDOM COLOR GENERATOR ---------
          if (keyPressed) {
            c = color(random(255), random(255), random(255));
          }
          fill(c);
          noStroke();
          ellipse(mouseX, mouseY, 10, 10);
          testLayer.beginDraw();
          if (mousePressed) {
            testLayer.stroke(c);
            testLayer.strokeWeight(10);
            testLayer.line(mouseX, mouseY, pmouseX, pmouseY);
          }
          testLayer.endDraw();
          //---------------------------------
          img = testLayer.get(0, 0, width, height);
          img = testLayer.get(0, 0, width, height);
          colimg = img.get();
          img.filter(THRESHOLD, 0.2);
          img.loadPixels();
          bs.imageFindBlobs(img);
          bs.loadBlobsFeatures();
          bs.weightBlobs(false); 
          //---------------------------------------
          for (int i=0;i<bs.getBlobsNumber();i++) {
            PVector[] pixloc = bs.getBlobPixelsLocation(i);
            PVector p = new PVector(pixloc[0].x, pixloc[0].y);
            blobValues.put(p, i);
          }
          if (blobValues.size()>0) {
            final Set<Entry<PVector, Integer>> mapValues = blobValues.entrySet();
            final int maplength = mapValues.size();
            final Entry<PVector, Integer>[] test = new Entry[maplength];
            mapValues.toArray(test);
            System.out.print("First Key:"+test[0].getKey());
            System.out.println(" First Value:"+test[0].getValue());
            System.out.print("Last Key:"+test[maplength-1].getKey());
            System.out.println(" Last Value:"+test[maplength-1].getValue());
            myinterest_key = test[maplength-1].getKey();
            //-------------------------------------------------------------------------
            int blobcol = colimg.pixels[(int)myinterest_key.x + width*(int)myinterest_key.y]; // getting blob color from testLayer
            if (blobcol!=lastcol) {
              int r = (blobcol & 0x00ff0000) >> 16; // red channel (0-255)
              int g = (blobcol & 0x0000ff00) >> 8;  // green channel (0-255)
              int b = (blobcol & 0x000000ff);       // blue channel (0-255)
              int maxChannelValue = max(r, g, b);  
              if (maxChannelValue == r) {
                R = (int)random(0, 5);
                println("R:" + R);
                player[R].play();
                if (!player[R].isPlaying()) player[R].rewind();
              }
              if (maxChannelValue == g) {
                G = (int)random(5, 10);
                println("G:" + G);
                player[G].play();
                if (!player[G].isPlaying()) player[G].rewind();
              }  
              if (maxChannelValue == b) {
                B = (int)random(10, 16);
                println("B:" + B);
                player[B].play();
                if (!player[B].isPlaying()) player[B].rewind();
              }

              lastcol =blobcol;
            }

            //    if (!player[R].isPlaying()) {
            //      //player[R].pause();
            //      player[R].rewind();
            //    }
            //    if (!player[G].isPlaying()) {
            //      //player[G].pause();
            //      player[G].rewind();
            //    }
            //    if (!player[B].isPlaying()) {
            //      //player[B].pause();
            //      player[B].rewind();
            //    }
          }
          image(colimg, 0, 0);
        }

Answers

  • void loop() Sets looping to continuous.

  • Sorry Chrisir ! but I think I didn't get you. Maybe you are saying the Draw loop should play automatically the audios in the loop so if this is what you are saying then it is not working here. please help ! I have also given the full application downloading link in the previous post so you can try it by yourself.

  • edited October 2013 Answer ✓

    player.loop();

    tells the player to play continously - http://code.compartmental.net/minim-beta/javadoc/ddf/minim/AudioPlayer.html

  • :D :D :D thanks Chrisir

  • edited October 2013

    this works (without using player.loop();) - I had to take the player[0].play command within the if-condition (if ( !player[0].isPlaying()) { ) and not before it

    //
    import ddf.minim.*;
    Minim minim;
    AudioPlayer[] player = new AudioPlayer[1];
    
    void setup() {
      size(400, 400);
      //----------------------------------------------------
      minim = new Minim(this); // initialaizing minim object
      player[0] = minim.loadFile("test33.mp3"); // load file in audio player array loadFile ( "FILE NAME");
      println ("song length is approx. " + player[0].length() / 1000 + " s.");
    }
    
    void draw() {
      background(255);
      if (!player[0].isPlaying()) {
        player[0].rewind();
        player[0].play();
      }
    } // draw 
    //
    
  • edited October 2013

    @Chrisir your second suggestion, I had tried but it is not working I have tried putting the player[R].play(); under if statement but none of them was working. (Similarly for others)

    Chirsir can you please also check that the random number which is generating before player[R].play() statement, sometimes generates two or more random number can you fix this.

    Brief of the application : It recognizes the color of the stroke drawn on the canvas and detects it color components (R,G, B) values and generates a number which remains unchanged until a different color gets detected.

    I have given my application downloading link in my first post and in case if you require blobscanner library click here

  • edited October 2013 Answer ✓

    hi, I don't have time to look at your code. You wrote:

    I have tried putting the player[R].play(); under if statement but none of them was working

    Did you use { } around the code?

    You wrote:

    check that the random number which is generating before player[R].play() statement, sometimes generates two or more random number can you fix this.

    instead of

      if (keyPressed) {
        c = color(random(255), random(255), random(255));
      }
    

    please use the function (outside of draw())

    void keyPressed() {
        c = color(random(255), random(255), random(255));
    }
    

    Greetings, Chrisir

  • edited October 2013 Answer ✓

    Shorter random color version: o=>

    void keyPressed() {
        c = (color) random(#000000);
    }
    
  • edited October 2013

    @Chrisir & @GoToLoop : Yes Chrisir, I did put them inside the curly brackets.

    About the random Number generation code: I was asking about the code line 80 -95 where R, G, B, are those numbers which gets generated randomly and sometimes these number execute more than once which should not happen. And sorry if I was not clear about my question so there is no problem with random color generator which both of you have mentioned. GoToLoop: Thanks :D I am still learning syntax :D

    The real problem is here mentioned in code with comments:

                //-------------------------------------------------------------------------
                int blobcol = colimg.pixels[(int)myinterest_key.x + width*(int)myinterest_key.y]; // getting blob color from testLayer
                if (blobcol!=lastcol) {
                  int r = (blobcol & 0x00ff0000) >> 16; // red channel (0-255)
                  int g = (blobcol & 0x0000ff00) >> 8;  // green channel (0-255)
                  int b = (blobcol & 0x000000ff);       // blue channel (0-255)
                  int maxChannelValue = max(r, g, b);  
                  if (maxChannelValue == r) {
                    R = (int)random(0, 5); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE
                    println("R:" + R);
                   //player[R].play();
                    if (!player[R].isPlaying()) {
                      player[R].rewind();  
                      player[R].play();
                    }
                  }
                  if (maxChannelValue == g) {
                    G = (int)random(5, 10); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE
                    println("G:" + G);
                    //player[G].play();
                    if (!player[G].isPlaying()) { 
                      player[G].rewind();  
                      player[G].play();
                    }
                  }  
                  if (maxChannelValue == b) {
                    B = (int)random(10, 16); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE
                    println("B:" + B);
                    //player[B].play();
                    if (!player[B].isPlaying()) {
                      player[B].rewind();  
                      player[B].play();
                    }
                  }
    
                  lastcol =blobcol;
                }
              }
    
  • edited October 2013

    gets executed more than once..

    please use the function (outside of draw()) 1 2 3 void keyPressed() {

    I wrote that because when you use the boolean keyPressed in draw() it gets executed more than once. With the function keyPressed outside of draw() only once.

    now you don't have keyPreessed anymore... why?

  • edited October 2013 Answer ✓

    no seriously, in which cases do you want to have it changed?

    until a different color gets detected.

    so just have a flag newColorDetetcted and set it to false and only change color randomly when newColorDetetcted gets true ( if (newColorDetetcted) { R = (int)random(0, 5);} ).

    Set newColorDetetcted to true when a new color is drawn or so...

  • Answer ✓
    int maxChannelValue = max(r, g, b);
    
    if (maxChannelValue == r) { // ...
    if (maxChannelValue == g) { // ...
    if (maxChannelValue == b) { // ...
    

    Have you thought about that 2 or all of them be equal to each other??? So those 3 if blocks can be true at the same time??? :-?

  • edited October 2013

    @Chirsir : I have done as you said but still there is problem. Actually there is no problem with the random color generation. Problem is in line number 80-99 with R,G,B random value generation :(

              import Blobscanner.*; 
              import java.util.*;
              import java.util.Map.*;
              //import java.util.Map.Entry;
              PGraphics testLayer;
              PImage img, colimg;
              Detector bs;
              PVector myinterest_key;
              import ddf.minim.*;
              Minim minim;
              AudioPlayer[] player = new AudioPlayer[16];
              LinkedHashMap<PVector, Integer> blobValues = new LinkedHashMap< PVector, Integer>();
              void setup() {
                size(400, 400);
                testLayer = createGraphics(width, height);
                bs = new Detector(this, 0, 0, 400, 400, 255);
                testLayer.beginDraw();
                testLayer.endDraw();
                //----------------------------------------------------
                minim = new Minim(this); // initialaizing minim object
                for (int i=0; i<player.length;i++) {
                  //String str = "Incredibox.com_"+i  +".mp3"; // file name & path
                  String str = i+".mp3";
                  player[i] = minim.loadFile(str); // load file in audio player array loadFile ( "FILE NAME");
                }  
                //----------------------------------------------------
              }
              color lastcol = color(255);
              color c;
              int R, G, B;
              void keyPressed() {
                c = (color) random(#000000);
              }
              void draw() {
                background(255);
                //--------- RANDOM COLOR GENERATOR ---------
                fill(c);
                noStroke();
                ellipse(mouseX, mouseY, 10, 10);
                testLayer.beginDraw();
                if (mousePressed) {
                  testLayer.stroke(c);
                  testLayer.strokeWeight(10);
                  testLayer.line(mouseX, mouseY, pmouseX, pmouseY);
                }
                testLayer.endDraw();
                //---------------------------------
                img = testLayer.get(0, 0, width, height);
                img = testLayer.get(0, 0, width, height);
                colimg = img.get();
                img.filter(THRESHOLD, 0.2);
                img.loadPixels();
                bs.imageFindBlobs(img);
                bs.loadBlobsFeatures();
                bs.weightBlobs(false); 
                //---------------------------------------
                for (int i=0;i<bs.getBlobsNumber();i++) {
                  PVector[] pixloc = bs.getBlobPixelsLocation(i);
                  PVector p = new PVector(pixloc[0].x, pixloc[0].y);
                  blobValues.put(p, i);
                }
                if (blobValues.size()>0) {
                  final Set<Entry<PVector, Integer>> mapValues = blobValues.entrySet();
                  final int maplength = mapValues.size();
                  final Entry<PVector, Integer>[] test = new Entry[maplength];
                  mapValues.toArray(test);
                 // System.out.print("First Key:"+test[0].getKey());
                 // System.out.println(" First Value:"+test[0].getValue());
                 // System.out.print("Last Key:"+test[maplength-1].getKey());
                 // System.out.println(" Last Value:"+test[maplength-1].getValue());
                  myinterest_key = test[maplength-1].getKey();
                  //-------------------------------------------------------------------------
                  int blobcol = colimg.pixels[(int)myinterest_key.x + width*(int)myinterest_key.y]; // getting blob color from testLayer
                  if (blobcol!=lastcol) {
                    int r = (blobcol & 0x00ff0000) >> 16; // red channel (0-255)
                    int g = (blobcol & 0x0000ff00) >> 8;  // green channel (0-255)
                    int b = (blobcol & 0x000000ff);       // blue channel (0-255)
                    int maxChannelValue = max(r, g, b);  
                    if (maxChannelValue == r) {
                      R = (int)random(0, 5); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE
                      println("R:" + R);
                      player[R].loop();
                      //        if (!player[R].isPlaying()) {
                      //          player[R].rewind();  
                      //          player[R].play();
                      //        }
                    }
                    if (maxChannelValue == g) {
                      G = (int)random(5, 10);
                      println("G:" + G); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE
                      player[G].loop();
                      //        if (!player[G].isPlaying()) { 
                      //          player[G].rewind();  
                      //          player[G].play();
                      //        }
                    }  
                    if (maxChannelValue == b) {
                      B = (int)random(10, 16);
                      println("B:" + B); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE
                      player[B].loop();
                      //        if (!player[B].isPlaying()) {
                      //          player[B].rewind();  
                      //          player[B].play();
                      //        }
                    }
                    lastcol =blobcol;
                  }
                }
    
                image(colimg, 0, 0);
              } 
    
  • edited October 2013

    @Chrisir : " no seriously, in which cases do you want to have it changed?" has been explained in reason 2.

    This application is a basically creates music with color. So what it does, it detects the stroke of the color drawn on the canvas with the help of blobscanner. Blobscanner detects the blob of each color. Here LinkedHashMap stores the latest values in the entry and updates it in each frame. So every new stroke drawn on the canvas would be consider as a new entry in the LinkedHashMap.

    Why I am using HashMap ?

    • Reason 1: The application is gonna integrate with kinect for multiple user drawing application.
    • Reason 2: Using LinkedHashMap can help me avoid the old blob detect by blobscanner otherwise each blob will get detected per frame and would generate a random number for each blob again and again which will create problems while calling the player[R].play. Actually it will call so many player at a time the music produced would be like mixture, cacophonic.

    @GoToLoop: Yes I know there would be some cases but for a while I have just ignored them to check whether application works or not. :P

  • Did you use the flag as I suggested?

  • edited November 2013

    @Chrisir: Yes, I have already used similar method as you have suggested. In my code you can see if (blobcol!=lastcol) where I am saving old blob color in lastcol variable and checking it with the current detected blob color variable which is blobcol.

    I think my variables names are confusing. So let me make them clear here

    • Variable R,G,B are not generating any random color these are just used to call audio players. Since the file names are (0,1,...15.mp3) and I have grouped them in set of five like 0-5 ,5-10 & 10 -16. I have used R,G,B variable as the name just because distinguish between which color component is max.

    • Here this line

      int blobcol = colimg.pixels[(int)myinterest_key.x + width*(int)myinterest_key.y];
      

    detects the blob color at myinterest position then I pick the r,g,b component of blob color

        int r = (blobcol & 0x00ff0000) >> 16; // red channel (0-255)
        int g = (blobcol & 0x0000ff00) >> 8;  // green channel (0-255)
        int b = (blobcol & 0x000000ff);       // blue channel (0-255)
    

    and then if (maxChannelValue == r) { is used to check which component has maximum value and then int variable R,G,B gets executed.

          R = (int)random(0, 5); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE
          println("R:" + R);
          player[R].loop(); // R is player number. It is not the color component 
    
  • I don't really get what you mean

    when this still applies:

    R = (int)random(0, 5); // PROBLEM IS HERE IT GETS EXECUTE MORE THAN ONCE

    try not to set R just anywhere in draw() but set it only once when the case occurs that it needs to be changed (e.g. when a blob is detected)

  • edited November 2013

    Hi Chrisir, Yes, I did exactly what you said. You can see this logic between line 62- 106. Line 62 checks if number of blobs is not zero if (blobValues.size()>0) and then it store all the blobs in a HashMap with their IDs and location. After this you can see it takes out the latest entry from the HashMap and checked it with previous value. If they are not same then it will perform the actions written inside the if(blobcol!=lastcol) .

    Chrisir, as I explained in previous post this application detects the color of the stroke drawn on the canvas and plays a new sound every new color. It is basically a music creating application and with this one can create music with colors.

    Chrisir, can you suggest me if there is another way to achieve the same effect which I am trying?

  • Answer ✓

    i have no idea...

  • Okay Chrisir ...Thanks a lot :)

Sign In or Register to comment.