We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
play sound once! (Read 5219 times)
play sound once!
Sep 20th, 2006, 3:13pm
 
Hi, I have written a program that reads an analog value through the serial port. According to this value, a specific sound file is loaded. The problem: the file keeps on playing as long as a signal is received. I understand why this happens, ut I can't fix it. I want the sound file to play only once, and then never again until a new value is received. How can I do this?

this is my code:


/*
*  pa_Potentiometer
*  
*  Reads the values which represent the state of a potentiometer
*  from the serial port and draws a graphical representation.
*
*  This file is part of the Arduino meets Processing Project.
*  For more information visit http://www.arduino.cc.
*
*  copyleft 2005 by Melvin Ochsmann for Malmö University
*
*/

// importing the processing serial class
import processing.serial.*;
import pitaru.sonia_v2_9.*;

// the display item draws background and grid
 DisplayItems di;

// definition of window size and framerate
 int xWidth = 768;
 int yHeight = 512;
 int fr = 1000;
 int lf = 3; //linefeed ascii
 
// attributes of the display
 boolean bck = true;
 boolean grid = true;
 boolean g_vert = true;
 boolean g_horiz = false;
 boolean g_values = true;
 boolean output = true;
 
// variables for serial connection, portname and baudrate have to be set
 Serial myPort;
 String portname = "/dev/tty.usbserial-351";  
 int baudrate = 9600;
 int value = 0;
 String buf="";
 int value1=0;  
 Sample sound1, sound2, sound3, sound4, sound5, sound6;

// variables to draw graphics
 int xpos;

// lets user control DisplayItems properties and value output in console
void keyPressed(){
 if (key == 'b' || key == 'B') bck=!bck;  // background black/white
 if (key == 'g' || key == 'G') grid=!grid;  // grid ON/OFF
 if (key == 'v' || key == 'V') g_values=!g_values;  // grid values ON/IFF  
 if (key == 'o' || key == 'O') output=!output;   //turns value output ON/OFF
}

void setup(){
 // set size and framerate
 size(xWidth, yHeight); framerate(fr);
 // establish serial port connection      
 myPort = new Serial(this, portname, baudrate);
 println(myPort);
 myPort.bufferUntil(lf);
 // create DisplayItems object
 di = new DisplayItems();
 //establish Sonia library
 Sonia.start(this,44100);
 sound1=new Sample("geluid1.wav");
 sound2=new Sample("geluid2.wav");
 sound3=new Sample("geluid3.wav");
 sound4=new Sample("geluid4.wav");
 sound5=new Sample("geluid5.wav");
 sound6=new Sample("geluid6.wav");
}

void drawPotiState(){
  noStroke();
  fill(0, 0, 255);
  // draw rectangfle at xpos
   rect(xpos, 0, width/32+(xpos/200), height);
   fill(255);
   // and xpos as text
   text(""+xpos , xpos, height/2);
}

void serialEvent(int serial){
 // if serial event is not a line break
 if(serial!=10) {        
   // add event to buffer
   buf += char(serial);          
   } else {
   // if serial is line break set value1 to buff and clear it
   value1 = int(buf);
   buf="";
   }
   // convert value1 to xpos
   xpos = constrain ( ( ((value1-100)*(width+100))/width )  ,0, width-12);
   if(output) println("xpos: "+xpos);
   // determine values for output
   if(value1<150){
     println("niks");
   } else if(value1<250){
     println("geluid1");
   } else if (value1<350){
     println("geluid2");
     sound2.play();
   } else if(value1<400){
     println("geluid3");
     sound3.play();
   } else if(value1<500){
     println("geluid4");
     sound4.play();
   } else if(value1<600){
     println("geluid5");
     sound5.play();
   } else if(value1<680){
     println("geluid6");
     sound6.play();
   }else{
    println("niks");
   }
   
}

// draw listens to serial port, draw
void draw(){
// listen to serial port and trigger serial event  
 while(myPort.available() > 0){
       value = myPort.read();
       serialEvent(value);
   }
// draw background, then PushButtonState and finally rest of DisplayItems  
   di.drawBack();
   drawPotiState();
   di.drawItems();  
}



Thanks a lot!
Re: play sound once!
Reply #1 - Sep 20th, 2006, 7:35pm
 
To start, you should use the isPlaying() method to determine if the sound is already playing.

As long as the sound is not already playing, play it.
Code:

if(!sound.isPlaying()){
sound.play();
}


Once the sound finishes, however, it will start again (assuming the value is still received via serial).  To protect against this, you either need to change the way your device that is sending serial works (to only send a new value when there is a change) or else implement your own logic to keep track of what was last received.  A boolean variable will do the trick, you might also consider creating a sound "class" that knows what sound file to play, when it last played, etc. . .  Using an array will also help matters.

Dan
Page Index Toggle Pages: 1