Loading...
Logo
Processing Forum
Hi,

I am using the following Processing code to produce a score board for a reaction timer game running on an Arduino. Every time the score is increased the new value is sent by the Arduino to Processing via the serial port.It works well and the score is displayed in a big font.

However I would really like to also produce a sound every time the score is updated.  I have briefly looked at Minim but it seems to me that it cannot be used during a draw loop very easily.  I got a sound to play once but not repetitively.  It seems that once the sound is played you need to close the sound before it can be played again?  But how can the software know when the sound has finished in order  to close the sound.  Most attempts ended in crashed sketches!

So perhaps it is not possible.  But if anyone knows how then I would be grateful.  I must point out that I am a very newbie!

Copy code

  1. void draw () 
  2. {
  3.   fill (255, 255, 255);//white fill
  4.   rect (0, 0, 1500, 800);//draw a big box on the screen 1500 x 800 pixels (filled white)

  5.   int line=600;// y offset value - see below

  6.   if (myPort.available()>0) //if there is anything in the serial buffer
  7.   {
  8.     count=myPort.read();//read the value from the port and make it equal to variable "count".
  9.     
  10.     fill(0,0,0);//black fill for font characters
  11.     textFont(f, 800);// define a huge font size!
  12.     text(count, 70, line);// write the value of count, 70 pixels in from the left and 600 down from the top of the box
  13. //I would like a short sound (.WAV or .mp3) to be played HERE!
  14.   }
  15. }   

Replies(2)

It's hard to say what' the reason, becaue you didn't supply parts of your sketch which are sound-related.

Below is your standard way of playing sound in Minim and it should work. 

ps. also it seems like you're problem has nothing to do with arduino, but rather using Minim in regular processing sketch. This question rather belongs to  https://forum.processing.org/core-library-questions (because Minim is included into core libraries now).


Copy code
  1. Minim minim;
  2. AudioPlayer myAudio;
  3. void setup(){
  4.    size(1500, 800);
  5.    minim = new Minim(this);
  6.     myAudio = minim.loadFile("mysound.mp3");
  7. }

  8. void draw () 
  9. {
  10.   fill (255, 255, 255);//white fill
  11.   rect (0, 0, 1500, 800);//draw a big box on the screen 1500 x 800 pixels (filled white)

  12.   int line=600;// y offset value - see below

  13.   if (myPort.available()>0) //if there is anything in the serial buffer
  14.   {
  15.     count=myPort.read();//read the value from the port and make it equal to variable "count".
  16.     
  17.     fill(0,0,0);//black fill for font characters
  18.     textFont(f, 800);// define a huge font size!
  19.     text(count, 70, line);// write the value of count, 70 pixels in from the left and 600 down from the top of the box
  20. //I would like a short sound (.WAV or .mp3) to be played HERE!
  21.    if ( myAudio.isPlaying() ){
  22.       myAudio.rewind(); // we just rewind it to the beginning so that the sound starts from the start
  23.    }
  24.    else{
  25.     myAudio.play();  // we just play as usual
  26.    }
  27.   }
  28. }   

Thanks very much for answering. It worked with a little modification. I now always rewind before playing since the sound is very short (<1s) so it will always be over before the next time it is needed.

For sure the problem was not Arduino related.  However I was hoping that it might attract some good interest if I mentioned Arduino. And it did because you kindly answered.  Hopefully your reply might also be read by other Arduino geeks who are looking to do a similar thing.

Thanks again,

David