# Can't understand why noLoop() is acting strange

**URL:** <https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322>\
**Category:** Libraries\
**Created:** [July 18, 2021, 5:55pm UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322 "2021-07-18T17:55:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![acagastya](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@acagastya](https://discourse.processing.org/u/acagastya)\
**Post date:** [July 18, 2021, 5:55pm UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322/1 "2021-07-18T17:55:54Z")

</div>

Hi, please consider this code [https://dpaste.com/4NTYQ8UP7](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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 19, 2021, 12:23am UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322/2 "2021-07-19T00:23:56Z")

</div>

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:

```auto
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

`:)`

---

<div class="post-metadata">

**Author:** ![acagastya](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@acagastya](https://discourse.processing.org/u/acagastya)\
**Post date:** [July 19, 2021, 4:45am UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322/3 "2021-07-19T04:45:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![acagastya](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@acagastya](https://discourse.processing.org/u/acagastya)\
**Post date:** [July 19, 2021, 6:39am UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322/4 "2021-07-19T06:39:16Z")

</div>

I seem to have figured out how to make it work, but this is really _really_ odd! See line 35 of the [pastebin](https://pastebin.com/K2rvffKe). @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?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 19, 2021, 11:21am UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322/5 "2021-07-19T11:21:53Z")

</div>

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.

> [@acagastya](#):
>
> but could you please tell me the right way to do it?

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.

`:)`

---

<div class="post-metadata">

**Author:** ![acagastya](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@acagastya](https://discourse.processing.org/u/acagastya)\
**Post date:** [July 19, 2021, 4:31pm UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322/6 "2021-07-19T16:31:04Z")

</div>

Surprising, because it works for me. You can even see the [demo gif](https://git.hanabi.in/acagastya/loudness-wave) 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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 20, 2021, 10:45am UTC](https://discourse.processing.org/t/cant-understand-why-noloop-is-acting-strange/31322/7 "2021-07-20T10:45:33Z")

</div>

Hello,

I did a further exploration of this…

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8e12650e468de67fd3c8f30fc06b3859e57f30b3.png)

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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ead5f233ce6b1599fbb527f2122e639ca0b263e4.png)

_ **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`:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f398fa8332190f6564defb08a8185ced4f431133.png)

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

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a76f11a3d537d7f8b3ef00ece71eff44c019fbfc.png)

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

> [@acagastya](#):
>
> Surprising, because it works for me.

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](https://processing.org/reference/libraries/sound/SoundFile.html)

- 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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cfb27e1ddb1b771c79a248c8190b00a0dd1e86e5.png)

- 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:

> **[Issues · processing/processing-sound](https://github.com/processing/processing-sound/issues)**
>
> Audio library for Processing built with JSyn. Contribute to processing/processing-sound development by creating an account on GitHub.

I have not looked through the existing ones yet…

`:)`
