source: main/trunk/model-sites-dev/respooled/collect/popup-video-respooled/js/midi/plugin.webmidi.js@ 29863

Last change on this file since 29863 was 29863, checked in by davidb, 9 years ago

First cut at respooled site/collection

File size: 2.3 KB
Line 
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 ///
67 navigator.requestMIDIAccess().then(function(access) {
68 plugin = access;
69 output = plugin.outputs()[0];
70 opts.onsuccess && opts.onsuccess();
71 }, function(err) { // well at least we tried!
72 if (window.AudioContext) { // Chrome
73 opts.api = 'webaudio';
74 } else if (window.Audio) { // Firefox
75 opts.api = 'audiotag';
76 } else { // no support
77 return;
78 }
79 root.loadPlugin(opts);
80 });
81 };
82
83})(MIDI);
Note: See TracBrowser for help on using the repository browser.