Play a sample when an ellipse disappears

edited June 2015 in Library Questions

Hi there,

This is probably something very simple, but I just can't make it work grrrr. So hopefully someone can help me out!

I have a client patch who registrates the mic input. At a certain amount of mic input, there appear ellipses (bubbles) in the server patch. When they 'float' out the screen, I want to play a sample of a popping sound (like the bubble pops when it isn't in sight anymore). But for some reason this isn't working as I would want to.

Scenario 1: I only use "sample1.play" > the sample sounds 1 time when the first bubble disappears. But if other bubbles disappear, the sample does nothing.

Scenario 2: So I thought maybe I should first say "sample1.rewind" and then "sample1.play" > but then, as soon as bubble 1 disappears the sample keeps rewinding itself from the first second so you have this very annoying crackling sound ALL the time.

So I guess I have to find out how I can let the sample play completely untill it's done and THEN let it start over again as soon as another bubble floats out the screen.

Ik tried to explain my problem as clear as I could! Here is the code:

 
 /*
  * Example of a server in Processing that receives OSC messages and can
  * send a reply
  *
  * 2012, Marc Groenewegen, Hogeschool voor de Kunsten Utrecht
  *
  * oscP5 and controlP5 created by Andreas Schlegel
  * oscP5 -- http://www.sojamo.de/oscP5
  * controlP5 -- http://www.sojamo.de/libraries/controlP5/
  */
  
 import oscP5.*;
 import netP5.*;
   
 import ddf.minim.*;  
 
 Minim minim;
 AudioInput input;
 
 AudioPlayer sample1;
 AudioPlayer sample2;
 
 OscP5 oscP5;
 NetAddress myRemoteLocation;
 
 ArrayList bub;
 
 int count= 0;
 int count2=0;
 
 float power;
 float x,y;
 
 PImage bg; 
 
 color c = color(random(167, 174),random(189, 167), 219, 63);
 
 
 void setup()
 {
   size(640,480);
   bg = loadImage("Clear_sky.JPG");
   x=width/2;
   y=height/2;
   frameRate(25);
   noStroke(); 
 
   // start oscP5 and listen for incoming messages at port 12000
   oscP5 = new OscP5(this,12000);
   
   // use the following IP/port combination to send messages to
   myRemoteLocation = new NetAddress("localhost",12001);
   
   //audio
> >   minim = new Minim(this);
> >   
> >   sample1 = minim.loadFile("plop.wav");
> >   sample2 = minim.loadFile("plop2.wav");
> >   
> >   
> >   //ArrayList for bubbles
> >   bub = new ArrayList();
> >   
> > } // setup()
> > 
> > 
> > /*
> >  * Draw loop must be present for this program to work, even though we don't
> >  * do a lot of magic here
> >  */
> > void draw()
> > {
> >   background(bg);
> > 
> > //adding bubbles
> > if (abs(power) > 50){
> >   count2++;
> > } 
> > 
> > //making bubbles
> > for(count = 0; count < count2; count++){
> >   bubble bubbles = new bubble(power,random(10, 50));
> >   bub.add(bubbles);
> > }
> >   
> > for(count = 0; count < count2; count++){
> >   bubble bubbles = (bubble) bub.get(count);
> >   bubbles.run();
> > }  
> >  
> > }
> > 
> > 
> > /*
> >  * The function oscEvent is triggered when a message comes in
> >  */
> > void oscEvent(OscMessage theOscMessage)
> > {
> >   // Show some info about the received message
> >   print("Received an osc message");
> >   String msgType =  theOscMessage.addrPattern();
> >   float msgValue = theOscMessage.get(0).floatValue(); // assuming a float
> > 
> >   print("Message name: " + msgType);
> >   print(" value: " + msgValue);
> >   println(" type: "+theOscMessage.typetag());
> > 
> >   // handle a certain type of message
> >   if(msgType.equals("/power"))
> >   {
> >     power=msgValue; // move the ellipse
> >   }
> >   
> >  
> > 
> >   // Send a reply to the client to say that we received the message
> >   OscMessage myMessage = new OscMessage("/ack");
> >   myMessage.add(0);
> >     oscP5.send(myMessage, myRemoteLocation); 
> > } // oscEvent()
> > 
> > 
> > 
> > 
> > ------ the bubble class: --------
> > 
> > class bubble{
> >  
> >   
> >  //declaring variables
> >  float kracht;
> >  float groote;
> >  
> >  float keer;
> >  
> >  int rand;
> >  int rand2;
> >  
> >  float bubbleX = random(10, 390);
> >  float bubbleY = 400;
> >  
> >  int count = 0; 
> > 
> >  
> >  void run(){
> >    drawing();
> >    move();
> >    outOfBox(); 
> > 
> >    
> >  } 
> >  
> >  
> >  bubble(float _power,float _size){
> >   kracht = _power;
> >   groote = _size;
> >    
> >  }
> >  
> >  //moving bubbles
> >  void move(){
> >  
> >  //mass determens speed 
> >  bubbleY = (bubbleY - (0.05 + groote * 0.05));
> >  bubbleX = (bubbleX + (0.001 + groote * 0.05) * keer);
> >  
> >  
> >  count = count + 1;
> >  
> >  rand2 = int(random(10, 20));
> >  
> >  if (count == rand2){
> >     
> >  rand = int(random(1, 3));
> >  count = 0;
> >    
> >  }
> >  
> >  if(rand == 1){
> >    
> >   keer = -1;
> >  } 
> >   if(rand == 2){
> >    
> >    keer = 1; 
> >   }
> >    
> >  } 
> >  
> >  //drawing bubbles
> >  void drawing(){
> >     
> >  ellipse(bubbleX,bubbleY,groote, groote);
> >  fill(c);
> >    
> >  }
> >  
> >  void outOfBox(){
> >    
> >  if (bubbleX < 0){
> >  sample1.rewind();
> >  sample1.play();
> > }
> > 
> >   if (bubbleX > 400){
> >   sample1.rewind();
> >   sample1.play(); 
> >    
> >   }
> >  
> >  if (bubbleY < 0){
> >  sample1.rewind();
> >  sample1.play();
> >    
> >  }
> >   
> >    
> >   }
> >   

Answers

  • edited June 2015

    (not tested)

    have boolean nowPlaying = false;

    when starting the song say

    nowPlaying = true; 
    sample1.play(); 
    

    then

    if (nowPlaying && ! sample1.isPlaying())  {
        sample1.rewind();
        nowPlaying = false; 
    }
    

    that should make it possible to rewind only when the song is over

  • When I put it like below, it still doesn't play again when the next bubble goes away unfortunately..

    
    void outOfBox(){
       
       boolean nowPlaying = false;
       
     if (bubbleX < 0){
       sample1.play();
       
       if (nowPlaying && ! sample1.isPlaying())
       sample1.rewind();
       sample1.play();
    }
     }
    
    
  • when starting the song say nowPlaying = true;

  • have this

      boolean nowPlaying = false;
    

    before setup()

  • Like this? That doesn't change a thing (sorry if I'm misunderstanding you, I'm a noob you see..)

     
     void outOfBox(){
       
       boolean nowPlaying = false;
       
     if (bubbleX < 0){
       nowPlaying = true;
       sample1.play();
       
       
       if (nowPlaying && ! sample1.isPlaying()) {
         sample1.rewind();
         nowPlaying = false;
         sample1.play();
         
    }
     }
      } 
    
    
  • Oh wait

  • With that line before setup it still doesn't replay ..

  • hm... I should test this someday

    but isn't the idea:

    • when a bubbles goes away play a sound. Sounds ends.

    • Next bubble, next sound (rewind, play).

    • when a bubbles goes away play a sound. When the next bubbles goes off while the sound is still playing, stop, rewind, play from start

    ?

    ;-)

  • before setup()

       boolean nowPlaying = false;
    

    then

        void outOfBox(){
    
    
    
         if (bubbleX < 0) {
    
           if (nowPlaying && ! sample1.isPlaying()) {
             sample1.rewind();
             nowPlaying = false;
             sample1.play();
    
            } // if
             else ( ! nowPlaying) {
               nowPlaying = true;
               sample1.play();
            } // else 
    
        } // if
        } // func
    
  • Yeah that is exactly the idea. But then just with 1 sample.

    Am I typing something in the wrong order?

  • before setup()

    boolean nowPlaying = false;
    

    then

    void outOfBox(){
    
       if (bubbleX < 0) {
    
         sample1.rewind();
    
         sample1.play();
         } // if
    } // func
    
  • I don't know

  • when you put .rewind first he's going to keep starting the sample over and over again. Annoying problem! It should be so easy, but for some reason it's not -.-'

  • when you put .rewind first he's going to keep starting the sample over and over again.

    wait, but outOfBox() is called only once, when the bubble is exploding or is it called again and again?

  • did you try my code from 03:45?

  • edited June 2015
    void outOfBox(){
    
    
    
     if (bubbleX < 0) {
    
       if (nowPlaying && ! sample1.isPlaying()) {
         sample1.rewind();
         nowPlaying = true;
         sample1.play();
    
        } // if
    
       else if (nowPlaying && sample1.isPlaying()) {
         sample1.rewind();
         nowPlaying = true;
         sample1.play();
    
        } // if
    
         else if ( ! nowPlaying) {
           nowPlaying = true;
           sample1.play();
        } // else 
    
    } // if
    } // func
    
  • outOfBox() is being called again and again. And yes I definitely tried your code from 03:45.

    I get this error when I try your last code

    
    else ( ! nowPlaying) {
    
    unexpected token: {
    
    

    I tried to put '()' after nowPlaying, but that didn't help.

  •  else if ( ! nowPlaying) {
           nowPlaying = true;
           sample1.play();
        } // else 
    
  • Aaaah! I swapped a few lines and now it works! :D

    Thank you so much!

  • great !

  • can you post the code - might be useful for others

    ;-)

  • Yes of course!

    Above void setup()

    boolean nowPlaying = false;
       

    And then

    
    void outOfBox(){ 
     
     if (bubbleX < 0) {
     
       if (nowPlaying && ! sample1.isPlaying()) {
         nowPlaying = true;
         sample1.play();
     
        } // if
     
       else if (nowPlaying && sample1.isPlaying()) {
         nowPlaying = true;
         sample1.play();
     
        } // if
     
         else if ( ! nowPlaying) {
           nowPlaying = true;
           sample1.rewind();
           sample1.play();
        } // else 
     
    } // if
    
    } // func  
    
    

    Thanks again Chrisir! This made my day ;)

  • thank you!

    ;-)

Sign In or Register to comment.