Skip to content

Commit

Permalink
MIDI 2.0 adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-soft committed Sep 18, 2023
1 parent 45332b8 commit c65e703
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
67 changes: 35 additions & 32 deletions javascript/JZZ.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,12 @@
}
return this._thenable();
};
function _midi(msg) { return msg.isMidi2 ? new UMP(msg) : new MIDI(msg); }
_M.prototype._emit = function(msg) {
var i;
for (i = 0; i < this._handles.length; i++) this._handles[i].apply(this, [MIDI(msg)._stamp(this)]);
for (i = 0; i < this._outs.length; i++) {
var m = MIDI(msg);
var m = _midi(msg);
if (!m._stamped(this._outs[i])) this._outs[i].send(m._stamp(this));
}
};
Expand Down Expand Up @@ -485,6 +486,20 @@
this._push(_kick, [img]);
return img._thenable();
};
_M.prototype.gr = function(g) {
if (g == this._gr || typeof g == 'undefined' && typeof this._gr == 'undefined') return this._thenable();
var img = this._image();
if (typeof g != 'undefined') g = _7b(g);
img._gr = g;
this._push(_kick, [img]);
return img._thenable();
};
_M.prototype.MIDI1 = function() { return this._thenable(); };
_M.prototype.MIDI2 = function() {
var m2 = new _M2(this);
this._push(_kick, [m2]);
return m2._thenable();
};

function _mpe(m, n) {
if (!this._orig._mpe) this._orig._mpe = new MPE();
Expand All @@ -507,41 +522,38 @@
throw RangeError('Bad channel value (must not be less than 0 or more than 15): ' + c);
}

// _M2: MIDI 2.0 object
function _M2() {
// _M2: MIDI 2.0 adaptor
function _M2(sink) {
_R.apply(this);
this._handles = [];
this._outs = [];
this._sink = sink;
this._sxid = sink._sxid;
}
_M2.prototype = new _R();
_M2.prototype._receive = function(msg) { this._emit(msg); };
_M2.prototype._receive = function(msg) { this._sink._receive(msg); };
_M2.prototype.send = function() {
this._push(_receive, [UMP.apply(null, arguments)]);
return this._thenable();
};
_M2.prototype._emit = function(msg) {
var i;
for (i = 0; i < this._handles.length; i++) this._handles[i].apply(this, [UMP(msg)._stamp(this)]);
for (i = 0; i < this._outs.length; i++) {
var m = UMP(msg);
if (!m._stamped(this._outs[i])) this._outs[i].send(m._stamp(this));
}
};
_M2.prototype._emit = function(msg) { this._sink._emit(msg); };
_M2.prototype._image = _M.prototype._image;
_M2.prototype.connect = _M.prototype.connect;
_M2.prototype.disconnect = _M.prototype.disconnect;
_M2.prototype.connected = _M.prototype.connected;
_M2.prototype._sxid = 0x7f;
_M2.prototype.connect = function(arg) {
this._sink.connect(arg);
return this._thenable();
};
_M2.prototype.disconnect = function(arg) {
this._sink.disconnect(arg);
return this._thenable();
};
_M2.prototype.connected = function() { return this._sink.connected(); };
_M2.prototype.sxId = _M.prototype.sxId;
_M2.prototype.ch = _M.prototype.ch;
_M2.prototype.gr = function(g) {
if (g == this._gr || typeof g == 'undefined' && typeof this._gr == 'undefined') return this._thenable();
var img = this._image();
if (typeof g != 'undefined') g = _ch(g);
img._gr = g;
_M2.prototype.gr = _M.prototype.gr;
_M2.prototype.MIDI1 = function() {
var img = this._sink._image();
this._push(_kick, [img]);
return img._thenable();
};
_M2.prototype.MIDI2 = function() { return this._thenable(); };

// _W: Watcher object ~ MIDIAccess.onstatechange
function _W() {
Expand Down Expand Up @@ -1312,15 +1324,6 @@
JZZ.Widget = Widget;
_J.prototype.Widget = JZZ.Widget;

function Widget2(arg) {
var self = new _M2();
if (arg instanceof Object) _for(arg, function(k) { self[k] = arg[k]; });
self._resume();
return self;
}
JZZ.Widget2 = Widget2;
_J.prototype.Widget2 = JZZ.Widget2;

JZZ.addMidiIn = function(name, widget) {
var info = _clone(widget._info || {});
info.name = name;
Expand Down
13 changes: 12 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,10 @@ describe('JZZ.Widget', function() {
port.ch(4).mtc(JZZ.SMPTE(30, 1, 2, 3, 4)).ch().mtc(JZZ.SMPTE(30, 1, 2, 3, 4));
port.note(0, 'B#4', 127, 1).note(9, 0, 1).ch(5).wait(10).note('Dbb5', 127, 1).wait(10).note(0, 1).disconnect().close();
});
it('gr', function() {
var port = JZZ.Widget({ _receive: function(msg) { sample.compare(msg); }});
port.gr(1).gr(1).gr().gr();
});
it('sxId', function(done) {
var sample = new test.Sample(done, [
[0xf0, 0x7f, 0x11, 0x04, 0x04, 0x00, 0x40, 0xf7], [0xf0, 0x7f, 0x11, 0x04, 0x03, 0x00, 0x40, 0xf7],
Expand All @@ -1385,9 +1389,16 @@ describe('JZZ.Widget', function() {
.noteOn('D5').aftertouch('D5', 127).noteOff('C5').note('E5', 127, 1).note(0, 1)
.mpe(0, 4).mpe(0, 4).mpe(0, 3).mpe(0, 0).mpe();
});
it('MIDI1/MIDI2', function(done) {
var sample = new test.Sample(done, [
[0x90, 0x40, 0x7f], [0x20, 0x90, 0x40, 0x7f], [0x90, 0x40, 0x7f]
]);
var port = JZZ.Widget({ _receive: function(msg) { sample.compare(msg); }});
port.noteOn(0, 64).MIDI2().MIDI2().noteOn(0, 0, 64).MIDI1().MIDI1().noteOn(0, 64).close();
});
});

describe('JZZ.Widget2', function() {
describe.skip('JZZ.Widget2', function() {
it('noop', function(done) {
var sample = new test.Sample(done, [[0, 0, 0, 0]]);
var port1 = JZZ.Widget2();
Expand Down

0 comments on commit c65e703

Please sign in to comment.