Skip to content

Commit

Permalink
Add compound messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-soft committed Mar 24, 2024
1 parent 88fba7c commit ee03427
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
31 changes: 26 additions & 5 deletions javascript/JZZ.midi.SMF.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,19 +784,40 @@
}
return a.join('\n ');
};

function _msg(msg) {
if (msg.length || msg.isSMF()) return msg;
_error('Not a MIDI message');
}
MTrk.prototype.add = function(t, msg) {
t = parseInt(t);
if(isNaN(t) || t < 0) _error('Invalid parameter');
msg = JZZ.MIDI(msg);
msg.tt = t;
var i, j, a;
try {
a = [_msg(JZZ.MIDI(msg))];
}
catch (e) {
try {
a = [];
for (i = 0; i < msg.length; i++) {
a.push(_msg(JZZ.MIDI(msg[i])));
}
if (!i) throw e;
}
catch (ee) {
throw i ? ee : e;
}
}
if (this[this._orig.length - 1].tt < t) this[this._orig.length - 1].tt = t; // end of track
if (msg.ff == 0x2f || msg[0] > 0xf0 && msg[0] != 0xf7) return this;
var i;
for (i = 0; i < this._orig.length - 1; i++) {
if (this._orig[i].tt > t) break;
}
this._orig.splice(i, 0, msg);
for (j = 0; j < a.length; j++) {
msg = a[j];
msg.tt = t;
this._orig.splice(i, 0, msg);
i++;
}
return this;
};

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"grunt": "^1.6.1",
"grunt-contrib-jshint": "^3.2.0",
"grunt-contrib-uglify": "^5.2.2",
"jzz-gui-player": "^1.7.1",
"jzz-midi-gm": "^1.3.5",
"jzz-gui-player": "^1.7.2",
"jzz-midi-gm": "^1.3.6",
"jzz-synth-tiny": "^1.3.8",
"mocha": "^10.3.0",
"nyc": "^15.1.0",
"test-midi-files": "^1.0.4"
"test-midi-files": "^1.0.5"
},
"runkitExampleFilename": "runkit.js",
"repository": {
Expand Down
11 changes: 11 additions & 0 deletions test/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,22 @@ describe('functions', function() {
assert.equal(chk.type, 'fake');
assert.equal(trk.type, 'MTrk');
});
it('add', function() {
var t = new JZZ.MIDI.SMF.MTrk();
t.add(10, JZZ.MIDI.bank(0, 1, 2));
assert.equal(t[0].toString(), 'b0 00 01 -- Bank Select MSB');
assert.equal(t[1].toString(), 'b0 20 02 -- Bank Select LSB');
});
it('throw', function() {
assert.throws(function() { trk.add(); });
assert.throws(function() { trk.add(0); });
assert.throws(function() { trk.add(0, 'dummy'); });
assert.throws(function() { trk.add(0, []); });
assert.throws(function() { trk.add(0, [[0x80, 0, 0], []]); });
assert.throws(function() { trk.tick(); });
assert.throws(function() { trk.ch(-1); });
assert.throws(function() { trk.sxId(-1); });
trk.add(0, [[0x80, 0, 0], []])
});
it('validate', function() {
var smf = new JZZ.MIDI.SMF(1);
Expand Down

0 comments on commit ee03427

Please sign in to comment.