Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed extra callback calls in while using cue to the soundFiles #449

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
endurance21 marked this conversation as resolved.
Show resolved Hide resolved
done();
});
}
});
});
});