P5 soundfile addCue is called multiple times

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

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:

Schedule events to trigger every time a MediaElement (audio/video) reaches a playback cue point.

Emphasis added by me.

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:

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

Funny, looks like you stumbled over a bug that was actually fixed just a couple of days ago. So you can look forward to this issue disappearing in a future release of the library. :tada: