Skip to content

Commit

Permalink
Merge pull request #449 from endurance21/processPeaks
Browse files Browse the repository at this point in the history
fixed extra callback calls in while using cue to the soundFiles
  • Loading branch information
endurance21 authored Jun 1, 2020
2 parents b3e61a3 + 37fe4a5 commit 3992740
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/soundfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

};

/**
Expand Down
25 changes: 25 additions & 0 deletions test/tests/p5.SoundFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
});
});
});

0 comments on commit 3992740

Please sign in to comment.