# P5 soundfile addCue is called multiple times

**URL:** <https://discourse.processing.org/t/p5-soundfile-addcue-is-called-multiple-times/21521>\
**Category:** Libraries\
**Created:** [June 3, 2020, 7:31am UTC](https://discourse.processing.org/t/p5-soundfile-addcue-is-called-multiple-times/21521 "2020-06-03T07:31:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casperleerink](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/casperleerink/32/9237_2.png) [@casperleerink](https://discourse.processing.org/u/casperleerink)\
**Post date:** [June 3, 2020, 7:31am UTC](https://discourse.processing.org/t/p5-soundfile-addcue-is-called-multiple-times/21521/1 "2020-06-03T07:31:48Z")

</div>

Hi everyone!

I am using the addCue method on a p5.soundFile. I invoke this method in the setup function. The callback function i provide is called multiple times (around 170) instead of just 1 time (which I expected). Does anyone know more about this? I know I can add a check myself so the critical part of the callback only runs once, but this seems a bit tedious.

Thank you!  
Casper

---

<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:** [June 3, 2020, 7:43am UTC](https://discourse.processing.org/t/p5-soundfile-addcue-is-called-multiple-times/21521/2 "2020-06-03T07:43:19Z")

</div>

Hello @casperleerink!

I think you have to share some code with us; it’s hard to see what’s going on without it. But, could it be that you’re looping the sound so that the cue point is reached more than once? From the [`addCue()` documentation](https://p5js.org/reference/#/p5.SoundFile/addCue):

> Schedule events to trigger _every time_ a MediaElement (audio/video) reaches a playback cue point.

Emphasis added by me.

---

<div class="post-metadata">

**Author:** ![casperleerink](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/casperleerink/32/9237_2.png) [@casperleerink](https://discourse.processing.org/u/casperleerink)\
**Post date:** [June 3, 2020, 5:34pm UTC](https://discourse.processing.org/t/p5-soundfile-addcue-is-called-multiple-times/21521/3 "2020-06-03T17:34:28Z")

</div>

Hi Sven,

Thank you for your reply. I don’t think i’m looping the sound. Here is the simplest version which produces the same unwanted result:

libraries:

```auto
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/addons/p5.sound.min.js"></script>

```

```auto
let audio;
let cueCount = 0;
function preload() {
    audio = loadSound("assets/audio/storm.mp3");
}
function setup() {
    createCanvas(400, 400);
    audio.play();
    audio.addCue(2, cueCallback);
}

function cueCallback() {
    cueCount++;
    console.log(`Callback is called: ${cueCount} times`);
}

```

running this code on my live server calls the cueCallback 173 times

---

<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:** [June 3, 2020, 8:08pm UTC](https://discourse.processing.org/t/p5-soundfile-addcue-is-called-multiple-times/21521/4 "2020-06-03T20:08:46Z")

</div>

Funny, looks like you stumbled over [a bug that was actually fixed](https://github.com/processing/p5.js-sound/pull/449) just a couple of days ago. So you can look forward to this issue disappearing in a future release of the library. 🎉
