From ee034271025b809f64b8dcf17a6862e725fa2293 Mon Sep 17 00:00:00 2001 From: Sema Date: Sun, 24 Mar 2024 01:01:03 -0400 Subject: [PATCH] Add compound messages --- javascript/JZZ.midi.SMF.js | 31 ++++++++++++++++++++++++++----- package.json | 6 +++--- test/mocha.js | 11 +++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/javascript/JZZ.midi.SMF.js b/javascript/JZZ.midi.SMF.js index 301f912..5c18822 100644 --- a/javascript/JZZ.midi.SMF.js +++ b/javascript/JZZ.midi.SMF.js @@ -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; }; diff --git a/package.json b/package.json index 6154104..42c179d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/mocha.js b/test/mocha.js index f526fa7..7dd0529 100644 --- a/test/mocha.js +++ b/test/mocha.js @@ -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);