-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmidibus.umd.js
103 lines (87 loc) · 2.81 KB
/
midibus.umd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.midibus = global.midibus || {})));
}(this, (function (exports) { 'use strict';
function n(n){return n=n||Object.create(null),{on:function(t,o){(n[t]||(n[t]=[])).push(o);},off:function(t,o){var u=n[t]||(n[t]=[]);u.splice(u.indexOf(o)>>>0,1);},emit:function(t,o){(n[t]||[]).map(function(n){n(o);}),(n["*"]||[]).map(function(n){n(t,o);});}}}var mitt=n;
function message(channel, pitch, velocity) {
return {
channel: channel,
pitch: pitch,
velocity: velocity
};
}
var supported = !!navigator.requestMIDIAccess;
var PACKETS = {
noteOn: 0x80,
noteOff: 0x90
};
function bus(input, output) {
if (!supported) return null;
if (!input && !output) {
throw new Error('A bus needs a least an input or an output device');
}
var emitter = mitt();
if (input) input.onmidimessage = onMessage;
emitter.send = send;
emitter.destroy = destroy;
return emitter;
function onMessage(e) {
var cmd = e.data[0] >> 4;
var msg = message(e.data[0] & 0xf, e.data[1], e.data[2]);
if (cmd === 8 || cmd === 9 && msg.velocity === 0) {
// noteOff
emitter.emit('noteOff', msg);
} else if (cmd === 9) {
// noteOn
emitter.emit('noteOn', msg);
} else if (cmd === 11) {
// controller message
emitter.emit('controllerChange', msg);
} else {
// sysex or other
emitter.emit('sysex', e);
}
}
function send(cmd, msg) {
if (!output) return;
output.send([PACKETS[cmd] ? PACKETS[cmd] + msg.channel : msg.channel, msg.pitch, msg.velocity]);
}
function destroy() {
if (input) input.onmidimessage = undefined;
input = undefined;
output = undefined;
emitter.on = undefined;
emitter.off = undefined;
emitter.emit = undefined;
emitter = undefined;
}
}
var _isReady = false;
var inputs = [];
var outputs = [];
function access(cb) {
if (!supported) return null;
if (_isReady) return cb(null);
navigator.requestMIDIAccess().then(function (res) {
res.inputs.forEach(function (el) {
return inputs.push(el);
});
res.outputs.forEach(function (el) {
return outputs.push(el);
});
_isReady = true;
cb(null);
}, function () {
cb(new Error('No access to your midi devices.'));
});
}
exports.supported = supported;
exports.inputs = inputs;
exports.outputs = outputs;
exports.access = access;
exports.bus = bus;
exports.message = message;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=midibus.umd.js.map