# Sound files making my code run much slower

**URL:** <https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289>\
**Category:** Libraries\
**Created:** [April 30, 2020, 12:25am UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289 "2020-04-30T00:25:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![achandra](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@achandra](https://discourse.processing.org/u/achandra)\
**Post date:** [April 30, 2020, 12:25am UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289/1 "2020-04-30T00:25:57Z")

</div>

Hi,

I’m new to P5 and working on a code for a school project. I have four sound clips in my code but everytime I have them on, the code becomes very visibly slower. I’m using the preload function to load them in, and then they play based on what checkbox is selected. I’m wondering if there is any way around this? Please help!!

Here is the sketch \*\*Type any lower-case letter \*\*

[https://editor.p5js.org/achandra/sketches/flm8aL1Xl](https://editor.p5js.org/achandra/sketches/flm8aL1Xl)

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [April 30, 2020, 1:15am UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289/2 "2020-04-30T01:15:32Z")

</div>

Welcome to the forums! How big are your sound files?

---

<div class="post-metadata">

**Author:** ![achandra](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@achandra](https://discourse.processing.org/u/achandra)\
**Post date:** [April 30, 2020, 4:00am UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289/3 "2020-04-30T04:00:56Z")

</div>

Around 3.5 MB each. There are 4 of them.

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [April 30, 2020, 5:37am UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289/4 "2020-04-30T05:37:34Z")

</div>

Hello [@achandra](https://discourse.processing.org/u/achandra),

So, I’ll try to answer this with a hand full of questions. 😊

When running the sketch and checking one of the checkboxes. Does the music sound like you expect it too? For me, it sounds pretty odd compared to the original mp3. Kind of echo-like and spooky. Why is that?

Right now, inside your draw function, you have code like this:

```auto
  if(sound01){
    s1.play();
  }

```

The logic above says, “if the checkbox for sound 1 is checked, play sound 1”. But, what if the sound is already playing? Have you checked out [isPlaying()](https://p5js.org/reference/#/p5.SoundFile/isPlaying)?

---

<div class="post-metadata">

**Author:** ![achandra](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@achandra](https://discourse.processing.org/u/achandra)\
**Post date:** [May 2, 2020, 7:16pm UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289/5 "2020-05-02T19:16:19Z")

</div>

Hi @Sven

The sounds does sound a bit stretched out and I havent been able to figure out why that is? Can that be because of the size of my sound files? I tried loading them one at a time to see if it makes a difference, and it still doesn’t make a difference.

I didn’t understand what you mean by “if sound is already playing?”

Right now the code is:

```auto
if(sound01){
    s1.play();
  }
  else if(!sound01){
    s1.stop();
  } 

```

So everytime, you uncheck the box, the sound stops. I tried isPlaying() too like this:

```auto
if(sound01){
    s1.play();
  }
  else if(!sound01 && s1.isPlaying()){
    s1.stop();
  } 

```

but this doesnt seem to be working either!

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [May 2, 2020, 8:02pm UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289/6 "2020-05-02T20:02:05Z")

</div>

The logic is inside the `draw` function. `draw` is called several times per second, depending on the frame rate. Probably around 60 times per second.

When a user interacts with your sketch, she waits for a while and then checks the checkbox. Some milliseconds after that the `draw` function executes, the logic finds that the checkbox is checked and starts playing the sound. 16 milliseconds after that, the `draw` function executes again. The checkbox is still checked, so it starts playing the sound again (now two instances of that sound is playing – 16 ms apart). And so it continues.

That’s why it sounds so stretched out. It’s not one instance of the sound playing: it’s hundreds with a little bit delay between them.

So, how do we avoid this cacophony? 🙂

---

<div class="post-metadata">

**Author:** ![achandra](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@achandra](https://discourse.processing.org/u/achandra)\
**Post date:** [May 2, 2020, 8:42pm UTC](https://discourse.processing.org/t/sound-files-making-my-code-run-much-slower/20289/7 "2020-05-02T20:42:17Z")

</div>

@Sven

Ofcourse! Thanks so much! I added this and I think it fixed the code!:

```auto
  if(sound01 && !s1.isPlaying()){
    s1.play();
  }
  
  else if(!sound01 && s1.isPlaying()){
    s1.stop();
  }

```

The sound doesnt sound echoey and is also not making my code run slower! Yay! Thanks a tonne!
