diff --git a/src/soundfile.js b/src/soundfile.js index 137e2ae5..8f549b2f 100644 --- a/src/soundfile.js +++ b/src/soundfile.js @@ -1772,17 +1772,17 @@ define(function (require) { var cue = this._cues[i]; var callbackTime = cue.time; var val = cue.val; + var leftLimit = this._prevUpdateTime || 0 ; + var rightLimit = playbackTime ; + if ( leftLimit <= callbackTime && callbackTime <= rightLimit ) { - if ( - ~~this._prevUpdateTime <= callbackTime && - callbackTime <= playbackTime - ) { // pass the scheduled callbackTime as parameter to the callback cue.callback(val); } } this._prevUpdateTime = playbackTime; + }; /** diff --git a/test/tests/p5.SoundFile.js b/test/tests/p5.SoundFile.js index 1b7c1400..c251d363 100644 --- a/test/tests/p5.SoundFile.js +++ b/test/tests/p5.SoundFile.js @@ -115,5 +115,30 @@ define(['chai'], function (chai) { done(); }, 100); }); + it('calls the cueCallbacks correct number of times', function (done) { + this.timeout(2000); + let sf = new p5.SoundFile('./testAudio/drum', onloaded); + + function onloaded() { + let audioLength = sf.duration(); + let numberOfCuesCalls = 0; + + let callback = () => { + numberOfCuesCalls++; + }; + + sf.addCue(audioLength / 5, callback); + sf.addCue((audioLength * 2) / 5, callback); + sf.addCue((audioLength * 3) / 5, callback); + sf.addCue((audioLength * 4) / 5, callback); + + sf.play(); + + sf.onended(() => { + expect(numberOfCuesCalls).to.equal(4); + done(); + }); + } + }); }); });