plugin.webmidi.js 2.6 KB

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