Can't understand why noLoop() is acting strange

Hi, please consider this code https://dpaste.com/4NTYQ8UP7. I cannot seem to understand why line 57-59 is causing the render to stop completely. I want the draw() to stop right when the sound is over, but can’t seem to make it work. Please advice how to fix it.

Hello,

If I start sample.play() in setup() then sample.isPlaying() in draw() goes to false soon after it starts playing.

If I start sample.play() with keyPressed() I can use sample.isPlaying() in draw() and it behaves as expected.

I used keyPressed() in example below to start audio.

println() is your friend when debugging code!

Example to try:

import processing.sound.*;
AudioSample sample;

boolean playing = false;

void setup() 
  {
  size(300, 200);
  background(0);

  // Create a new audiosample and play it
  sample = new SoundFile(this, "beat.aiff");
  //sample.play();
  }    

void draw()
  {
  background(0);
  textSize(48);
  fill(255, 0, 0);
  textAlign(CENTER, CENTER);
  text(frameCount/60, width/2, height/2);
    
  if (sample.isPlaying()) //isPlaying == true
    {
    println(frameCount, "Sample IS playing");
    playing = true;
    }
  else                    //isPlaying == false
    {
    println(frameCount, "Sample NOT playing");
    if (playing) 
      {
      playing = false;
      }
    }
  }    
  
void keyPressed()
  {
  if (!playing)
    {
    sample.play();
    }
  println(frameCount);
  }    

I added an extra boolean variable playing for more control:

  • There are delays in starting audio sample
  • Multiple keypresses will play multiple audio samples

:)

Thanks for that, @glv, however, I am still not sure how to make use of noLoop. Consider this for example: Code on Pastebin – I don’t understand why when using draw1, noLoop works as expected, yet it seems to be acting strange for draw.

I seem to have figured out how to make it work, but this is really really odd! See line 35 of the pastebin. @glv any clue why textSize(48); changes this behaviour? Is this expected behaviour, xor is this some bug? I can use this hack for time being, but could you please tell me the right way to do it?

Hello,

I was focused on seeing the status of sample.isPlayed() in my example before acting on that condition (TRUE or FALSE). If I add noLoop() to my example it stops draw() from looping at end of play.

In my case I will get a FALSE before a TRUE and adapted code for that.

In your last example you added exit() along with textSize(48) which exits right away and does not play my sound file.
The textSize(48) can be commented out to see if it has any effect.

I do not have an immediate answer for you and was exploring this along with you.
There may be others in the forum that can help.

:)

Surprising, because it works for me. You can even see the demo gif in the repo created using that. textSize(48) is the line which precisely helps animate, without which, it was not animating at all. You can comment out exit and see, the textSize(48) is causing the animation to actually render and then stop.

Hello,

I did a further exploration of this…

I tried this out with the “BeatDetection” example that comes with Processing:

I was able to resolve my sample.isPlaying() issue with a delay() between loading and playing the sound file.
The length of delay was trial and error; 200 was the threshold and I bumped it up to 250 for this example. Your mileage (kilometrage?) may vary.

Without a delay I get 3 true then false event though it is still playing!

The false triggers the noLoop() in this code example:

With a delay it behaves as I would expect it to and I get a true from the start until it stops playing and I get a false:

And it displays this when it is false and triggers the noLoop():

image

On my PC this is consistent behavior and I needed to add a delay for the examples I tried.

Code will not always behave as expected for a number of reasons:

  • bug in library code or Processing

  • the logic and flow in the users code

  • loading files can take time and times can differ loading from hard drive, solid state drive or network

  • sound file format:
    SoundFile / Libraries / Processing.org

  • version of sound library. I am using 2.3.1

  • version of Processing. I am using 3.5.4

  • the PC hardware (Intel, RPI, video card, drives, etc.)

  • PC OS. I have W10 Pro.

  • library specific details such as:
    image

  • I am sure there are more items to add to this list…

Adding a line of code such as textSize(48) may have added the delay (every line of code takes time to execute) necessary for you in your case based on my exploration of this; I can’t comment further on this as it made no difference here on my PC.

I may open an issue on GitHub for this:

I have not looked through the existing ones yet…

:)