How to know when a sound file has been read in its entirety?

Hi,

Here my program that retrieves a value from the Arduino card and plays a sound based on that value.
The value varies according to the position of a potentiometer: an Arduino program transforms the voltage across the potentiometer into 3 digital values: 1, 2 and 3.
The Processing program must play sound 1 when the recovered value is 1, play sound 2 when the recovered value is 2 and play sound 3 when the value is 3, etc.
It works but I want that when you switch from sound 1 to sound 2 (or from sound 2 to 3, etc.), sound 1 is not stopped but only muted. Once the sound has been played in its entirety, it must be automatically restarted.
I don’t know how to do it, I think I have to know when the sound is done, but I didn’t find a function like that.

import processing.serial.*;
import ddf.minim.*;

Serial port;
Minim minim;
AudioPlayer radio1;
AudioPlayer radio2;
AudioPlayer radio3;
AudioPlayer bruitage;

int value=0;

void setup() {
  background(0, 60, 60);
  size(200,200);
  port = new Serial(this, Serial.list()[0], 9600);
  port.bufferUntil('\n');
  minim = new Minim(this);
  radio1 = minim.loadFile("radio1.mp3");
  radio2 = minim.loadFile("radio2.mp3");
  radio3 = minim.loadFile("radio3.mp3");
  bruitage = minim.loadFile("bruitage.mp3");
}

void draw() {
  radio();
  delay(100);
}

void radio() {
  println("canal " + value);
  if (value==1) {
    bruitage.mute();
    radio1.unmute();
    radio1.play();
  }
  if (!radio1.isPlaying() && value==1) {
    radio1.rewind();
    radio1.play();
  }
  if (value==2) {
    radio1.mute();
    bruitage.unmute();
    bruitage.play();
  }
  if (!bruitage.isPlaying() && value==2) {
    bruitage.rewind();
    bruitage.play();
  //...
}

void serialEvent(Serial port){
    String serialStr = port.readStringUntil('\n');
    serialStr=trim(serialStr);
    int values[]=int(split(serialStr,','));
    if(values.length==1){
      value=values[0];
    }
}

Could someone please help me?

1 Like

starting with the arduino part again
( what actually was solved as i remember )
is only confusing, @GoToLoop
your question is about MINIM library

did you check the MINIM examples?

/ Files / Contributed Libraries / Minim / AudioPlayer /
loop
loopNum

can use

.loop() instead of .play()

also this

text(" Is playing: " + groove.isPlaying()
+ ", Is looping: " + groove.isLooping(), 5, 15);

might help

more:
http://code.compartmental.net/minim/audioplayer_method_loop.html

2 Likes

Sorry for the late answer.

I tried to replace play() with loop() method and and it seems to work for a change in value on the potentiometer because the sound change when i turn the potentiometer one time. The text shows that the first song keeps playing and it’s muted. It’s good, but, when I turn the potentiometer a second time, the second song keeps playing in a loop, whatever the value of the potentiometer.

Could you help me please ?

the basic form of operating ( start stop ) songs in MINIM is

void keyPressed() {   // or any other event...
  toggle_groove();
}

void toggle_groove() {
  if ( groove.isPlaying() )  groove.pause();
  else                       groove.loop();
}

but not sure if that helps in your 4 song logic??

The answer was obvious, I need to put this second loop for each sound :

 if (radio1.isPlaying() && value==1) {
   bruitage.mute();
   radio1.unmute();
 }

I know there’s probably a better way to do this but, it’s worked so it’s okay ahah
Thank you for you big help