Array of identical samples. Minim triggering sample issue.

edited March 2018 in Library Questions

Hi there. I have an array of identical samples for two bouncing orbs which are also in an array. A sample is to play each time a ball falls below a virtual water level. They trigger just fine, but there seems to be constant or random glitches in sample sounds at various other times when it has not crossed this threshold. The line in question where I set this threshold is contained within the orb_class. Perhaps this is completely wrong, but it triggers the samples at least:

  void playSample(int arrayIndex){ 
    if (orby < waterSurfaceHeight){   
      popsound[arrayIndex].rewind();
      popsound[arrayIndex].play();
    } 
  } 

*Edit. The code is shortened to relevant code only an is quoted in a reply below this one.

Answers

  • the problem here is that we can't test your code unless we have osc and netp5. but the problem doesn't sound like it's related to those. you'd get more of an audience if you cut the code down to the bare minimum to reproduce the problem.

    and if you cut out all the superfluous stuff and the problem goes away then you know it's in something you've removed...

  • edited March 2018

    Thanks, koogs. That makes sense. Thanks.

    Here is the code paired back to the essentials.

    The threshold checker is on line 83:

    import ddf.minim.*;
    Minim minim;
    AudioPlayer[] popsound; 
    
    int numberOfOrbs = 3, numberOfFish = 20, numberOfStars = 100;
    float[] incomingOSC = new float[numberOfOrbs];  // declare OSC message variable
    Orb_Class[] many_Orbs = new Orb_Class[numberOfOrbs];
    
    float spaceHeight;
    float waterSurfaceHeight;
    float seaFloor;
    float radius;   
    
    
    void setup() {
      size(1000, 800);
      noStroke();
      smooth(); 
      waterSurfaceHeight = (height/3)*2;  
      spaceHeight = height/3;
      seaFloor = height;  
    
      popsound = new AudioPlayer[numberOfOrbs]; 
      minim = new Minim(this); 
    
      for (int i=0; i<many_Orbs.length; i++) {  
        many_Orbs[i] = new Orb_Class(64, random(1, 3), random(1, 3));
        popsound[i] = minim.loadFile("splash.mp3");
      }//for
    }//setup
    
    
    void draw() {
      background(255);  // erase the window to black 
      fill(0, 41, 158);
      noStroke();
      rect(0, 0, width, waterSurfaceHeight);  // The Ocean  
    
      for (int j=0; j<many_Orbs.length; j++) {   // display red orbs
        many_Orbs[j].move();  
        many_Orbs[j].playSample(j);
        many_Orbs[j].display(j);
      }//for
    
      fill(0, 41, 158, 200);
      noStroke();
      rect(0, height/3*2, width, height/3*2);  // The Ocean
    }//Draw
    
    
    class Orb_Class {
      float orbx; 
      float orby = random(300, height-100); // location
      float xspeed, yspeed; // speed  
      float xtime;  
      float ytime; 
    
      // Constructor
      Orb_Class(float tempR, float tempXt, float tempYt) {
        radius = tempR; 
        xspeed = tempXt;
        if (random(100)<50) {
          xspeed *= -1;
        }//if
        yspeed = tempYt;
        if (random(100)<50) {
          yspeed *= -1;
        }//if
      }
    
      void move() { 
        orbx += xspeed;//*abs(incomingOSC[arrayIndex]*10); // Increment x
        orby += yspeed;//*abs(incomingOSC[arrayIndex]*10); // Increment y
    
        if (orbx > width || orbx < 0) { // Rebound from horizontal edges 
          xspeed *= -1;
        }//if    
        if (orby > height || orby < 0) { // Rebound from vertical edges
          yspeed *= -1;
        }//if
      }//move
    
      void playSample(int arrayIndex) { 
        if (orby < waterSurfaceHeight) {   
          popsound[arrayIndex].rewind();
          popsound[arrayIndex].play();
        }
      }
    
      void display(int arrayIndex) {  
        fill(255, 0, 0);
        ellipse(orbx, orby, radius+(incomingOSC[arrayIndex]*200), radius+(incomingOSC[arrayIndex]*200));
      }
    }
    
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
      for (int i=0; i<popsound.length; i++) {  
        popsound[i].close();
      }
    
      // always stop Minim before exiting.
      minim.stop(); 
      super.stop();
    }
    
  • and the mp3?

  • add println("Playing", arrayIndex); after line 86, and tell me what you see

  • I see that it's always switching between one of the indices? I'm not sure what's going on here tbh. Is it because with the 'orby < waterSurfaceHeight', it will always be iterating through the indices while orby is below that value?

  • It is always playing and only gets a chance to play out the whole duration of sample when the orb dives below the water?

  • it's playing FAR too often

    i'm guessing it should only play the sample when it hits the surface of the water, you have it playing every single frame that it's under the water.

  • Yeah I just want it to play only once for each time an orb initial hits surface of water. I never knew you could do that kind of printing to get info on what an area is doing. Any idea how I can fix this so it triggers only once? If I set it to = waterSurfaceHeight, nothing happens. Thanks.

  • it's probably never exactly waterSurfaceHeight.

    so you need another variable, oldy to Orb_Class, containing the old value of y

    you can remember the old value at the top of move(), before adding the increments, oldy = orby...

    if the previous position was above the water and the new position is under the water then it's crossed the boundary...

  • I never knew you could do that kind of printing to get info on

    it's debugging 101. don't guess what the code is doing, get it to tell you.

  • I'm reading your words, I'ms sure it makes sense, but I really don't know how to implement that in code. What do you mean by 'before adding the increments'?Perhaps in the processing examples folder or elsewhere there is an example I can look at. Does this old/new checking system have a particular name? Thanks.

  • Line 73 updates the position. You add the speed to the current position to give the new position.

    So oldy = orby before that to store the old y position.

    The condition now needs two clauses, and both have to be true. oldy must be above the waterline AND orby must be below for the condition to be true...

  • I'm so far failing. I'm sorry for exhausting your patience. I just don't get how to apply it. Thanks all the same.

  • edited March 2018

    sorry, it got late where i am

    the old test is good as it is but you need to add the new test and they both have to be true

    so something like this

      void playSample(int arrayIndex) { 
        if (oldy > waterSurfaceHeight && orby < waterSurfaceHeight) {   
          // ball has hit surface
          popsound[arrayIndex].rewind();
          popsound[arrayIndex].play();
        }
      }
    

    where oldy is a new float added to the class and set to orby in the frst line of move.

    (currently untested)

  • how can both line 37 and line 47 be The Ocean?

  • yes, that's confusing. the condition needs to be the other way up because y is 0 at the top of the screen and height at the bottom.

    if (oldy < waterSurfaceHeight && orby > waterSurfaceHeight) {
      ...
    
  • Sorry for the delay, koogs. Thanks for your help :)

Sign In or Register to comment.