1 | /*
|
---|
2 | ----------------------------------------------------------------------
|
---|
3 | Web MIDI API - Native Soundbanks
|
---|
4 | ----------------------------------------------------------------------
|
---|
5 | http://webaudio.github.io/web-midi-api/
|
---|
6 | ----------------------------------------------------------------------
|
---|
7 | */
|
---|
8 |
|
---|
9 | (function(root) { 'use strict';
|
---|
10 |
|
---|
11 | var plugin = null;
|
---|
12 | var output = null;
|
---|
13 | var channels = [];
|
---|
14 | var midi = root.WebMIDI = {api: 'webmidi'};
|
---|
15 | midi.send = function(data, delay) { // set channel volume
|
---|
16 | output.send(data, delay * 1000);
|
---|
17 | };
|
---|
18 |
|
---|
19 | midi.setController = function(channel, type, value, delay) {
|
---|
20 | output.send([channel, type, value], delay * 1000);
|
---|
21 | };
|
---|
22 |
|
---|
23 | midi.setVolume = function(channel, volume, delay) { // set channel volume
|
---|
24 | output.send([0xB0 + channel, 0x07, volume], delay * 1000);
|
---|
25 | };
|
---|
26 |
|
---|
27 | midi.programChange = function(channel, program, delay) { // change patch (instrument)
|
---|
28 | output.send([0xC0 + channel, program], delay * 1000);
|
---|
29 | };
|
---|
30 |
|
---|
31 | midi.pitchBend = function(channel, program, delay) { // pitch bend
|
---|
32 | output.send([0xE0 + channel, program], delay * 1000);
|
---|
33 | };
|
---|
34 |
|
---|
35 | midi.noteOn = function(channel, note, velocity, delay) {
|
---|
36 | output.send([0x90 + channel, note, velocity], delay * 1000);
|
---|
37 | };
|
---|
38 |
|
---|
39 | midi.noteOff = function(channel, note, delay) {
|
---|
40 | output.send([0x80 + channel, note, 0], delay * 1000);
|
---|
41 | };
|
---|
42 |
|
---|
43 | midi.chordOn = function(channel, chord, velocity, delay) {
|
---|
44 | for (var n = 0; n < chord.length; n ++) {
|
---|
45 | var note = chord[n];
|
---|
46 | output.send([0x90 + channel, note, velocity], delay * 1000);
|
---|
47 | }
|
---|
48 | };
|
---|
49 |
|
---|
50 | midi.chordOff = function(channel, chord, delay) {
|
---|
51 | for (var n = 0; n < chord.length; n ++) {
|
---|
52 | var note = chord[n];
|
---|
53 | output.send([0x80 + channel, note, 0], delay * 1000);
|
---|
54 | }
|
---|
55 | };
|
---|
56 |
|
---|
57 | midi.stopAllNotes = function() {
|
---|
58 | output.cancel();
|
---|
59 | for (var channel = 0; channel < 16; channel ++) {
|
---|
60 | output.send([0xB0 + channel, 0x7B, 0]);
|
---|
61 | }
|
---|
62 | };
|
---|
63 |
|
---|
64 | midi.connect = function(opts) {
|
---|
65 | root.setDefaultPlugin(midi);
|
---|
66 | var errFunction = function(err) { // well at least we tried!
|
---|
67 | if (window.AudioContext) { // Chrome
|
---|
68 | opts.api = 'webaudio';
|
---|
69 | } else if (window.Audio) { // Firefox
|
---|
70 | opts.api = 'audiotag';
|
---|
71 | } else { // no support
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | root.loadPlugin(opts);
|
---|
75 | };
|
---|
76 | ///
|
---|
77 | navigator.requestMIDIAccess().then(function(access) {
|
---|
78 | plugin = access;
|
---|
79 | var pluginOutputs = plugin.outputs;
|
---|
80 | if (typeof pluginOutputs == 'function') { // Chrome pre-43
|
---|
81 | output = pluginOutputs()[0];
|
---|
82 | } else { // Chrome post-43
|
---|
83 | output = pluginOutputs[0];
|
---|
84 | }
|
---|
85 | if (output === undefined) { // nothing there...
|
---|
86 | errFunction();
|
---|
87 | } else {
|
---|
88 | opts.onsuccess && opts.onsuccess();
|
---|
89 | }
|
---|
90 | }, errFunction);
|
---|
91 | };
|
---|
92 |
|
---|
93 | })(MIDI);
|
---|