diff --git a/javascript/JZZ.js b/javascript/JZZ.js index cea36c6..f52dfed 100644 --- a/javascript/JZZ.js +++ b/javascript/JZZ.js @@ -2847,7 +2847,7 @@ // JZZ.UMP function UMP(arg) { - var self = this instanceof UMP ? this : self = new UMP(); + var self = this instanceof UMP ? this : self = new UMP([0, 0, 0, 0]); var i; if (arg instanceof UMP) { self._from = arg._from.slice(); @@ -2857,11 +2857,13 @@ else self._from = []; if (typeof arg == 'undefined') return self; var arr = arg instanceof Array ? arg : arguments; + self.length = 0; for (i = 0; i < arr.length; i++) { n = arr[i]; if (n != parseInt(n) || n < 0 || n > 255) _throw(arr[i]); self.push(n); } + if (self.length != [4, 4, 4, 8, 8, 16, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16][self[0] >> 4]) throw RangeError('Wrong UMP size'); return self; } UMP.prototype = []; @@ -2885,6 +2887,11 @@ ret._gr = g; return ret; }; + UMP.prototype.getGroup = function() { + if (!this[0]) return; + var m = this[0] >> 4; + if (m == 1 || m == 2 || m == 3 || m == 4 || m == 5 || m == 13) return this[0] & 15; + }; UMP.prototype.toString = function() { return _hexx(this); diff --git a/test/common.js b/test/common.js index 7dc0bc1..1783c84 100644 --- a/test/common.js +++ b/test/common.js @@ -763,6 +763,38 @@ describe('MIDI messages', function() { }); }); +describe('UMP messages', function() { + it('throw', function() { + assert.throws(function() { JZZ.UMP([1]); }); + assert.throws(function() { JZZ.UMP([1, 2, 3, 'error']); }); + assert.throws(function() { JZZ.UMP([1, 2, 3, 4, 5, 6, 7, 8]); }); + }); + it('noop', function() { + var msg =JZZ.UMP.noop(); + assert.equal(msg.toString(), '00000000'); + assert.equal(typeof msg.getGroup(), 'undefined'); + }); + it('noteOn', function() { + var s = '21923d7f'; + var msg = JZZ.UMP.noteOn(1, 2, 'C#5'); + assert.equal(msg.getGroup(), 1); + assert.equal(JZZ.UMP.noteOn(1, 2, 'C#5').toString(), s); + assert.equal(JZZ.UMP.ch().noteOn(1, 2, 'C#5').toString(), s); + assert.equal(JZZ.UMP.ch(5).ch().noteOn(1, 2, 'C#5').toString(), s); + assert.equal(JZZ.UMP.gr().noteOn(1, 2, 'C#5').toString(), s); + assert.equal(JZZ.UMP.gr(5).gr().noteOn(1, 2, 'C#5').toString(), s); + assert.equal(JZZ.UMP.gr(1).noteOn(2, 'C#5').toString(), s); + assert.equal(JZZ.UMP.ch(2).noteOn(1, 'C#5').toString(), s); + assert.equal(JZZ.UMP.gr(1).ch(2).noteOn('C#5').toString(), s); + assert.equal(JZZ.UMP.ch(2).gr(1).noteOn('C#5', 127).toString(), s); + assert.equal(JZZ.UMP.ch(2).gr(1).ch(2).gr(1).noteOn('C#5', 127).toString(), s); + }); + it('noteOff', function() { + assert.equal(JZZ.UMP.noteOff(1, 2, 61).toString(), '21823d40'); + assert.equal(JZZ.UMP.noteOff(1, 2, 'C#5', 64).toString(), '21823d40'); + }); +}); + describe('SMF events', function() { it('empty', function() { assert.equal(JZZ.MIDI.smf(1).toString(), 'ff01 -- Text');