Skip to content
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
36 changes: 18 additions & 18 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,6 @@ class Player {
this.inLoop = true;
this.tick = this.getCurrentTick();

if (!dryRun && this.endOfFile()) {
if (this.loop) {
this.resetTracks();
this.setTempo(this.defaultTempo);
this.startTick = 0;
this.startTime = Date.now();
this.scheduledTime = Date.now();
this.tick = 0;
this.triggerPlayerEvent('endOfFile');
} else {
this.stop();
this.triggerPlayerEvent('endOfFile');
}

this.inLoop = false;
return;
}

this.tracks.forEach(function(track, index) {
let result = track.handleEvent(this.tick, dryRun);

Expand Down Expand Up @@ -249,6 +231,24 @@ class Player {

}, this);

if (!dryRun && this.endOfFile()) {
if (this.loop) {
this.resetTracks();
this.setTempo(this.defaultTempo);
this.startTick = 0;
this.startTime = Date.now();
this.scheduledTime = Date.now();
this.tick = 0;
this.triggerPlayerEvent('endOfFile');
} else {
this.stop();
this.triggerPlayerEvent('endOfFile');
}

this.inLoop = false;
return;
}

if (!dryRun && this.isPlaying()) this.triggerPlayerEvent('playing', {tick: this.tick});
this.inLoop = false;
}
Expand Down
33 changes: 33 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,39 @@ describe('MidiPlayerJS', function() {
});
});

describe('#final tick events', function () {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
this.clock.tick(5000); // set start time
});
afterEach(function() {
sinon.restore();
});

it('should emit events at the final tick before endOfFile fires', function () {
// Note On C4 at tick 0, Note Off C4 at tick 96 (= totalTicks), then End of Track
var midi = buildMidi([
0x00, 0x90, 0x3C, 0x7F, // Note On C4 vel 127 at tick 0
0x60, 0x80, 0x3C, 0x00, // Note Off C4 at tick 96
].concat(EOT));
var events = [];
var endOfFileCount = 0;
var Player = new MidiPlayer.Player();
Player.on('midiEvent', function(event) { events.push(event); });
Player.on('endOfFile', function() { endOfFileCount++; });
Player.loadArrayBuffer(midi.buffer);
Player.play();

// Advance well past the song length
this.clock.tick(2000);

var noteOffEvents = events.filter(function(e) { return e.name === 'Note off'; });
assert.ok(noteOffEvents.length > 0, 'Note off at final tick should have been emitted');
assert.equal(noteOffEvents[0].tick, 96, 'Note off should be at the final tick');
assert.equal(endOfFileCount, 1, 'endOfFile should have fired');
});
});

describe('#loop', function () {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
Expand Down
Loading