plugin.audiotag.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ----------------------------------------------------------------------
  3. AudioTag <audio> - OGG or MPEG Soundbank
  4. ----------------------------------------------------------------------
  5. http://dev.w3.org/html5/spec/Overview.html#the-audio-element
  6. ----------------------------------------------------------------------
  7. */
  8. (function(root) { 'use strict';
  9. window.Audio && (function() {
  10. var midi = root.AudioTag = { api: 'audiotag' };
  11. var noteToKey = {};
  12. var volume = 127; // floating point
  13. var buffer_nid = -1; // current channel
  14. var audioBuffers = []; // the audio channels
  15. var notesOn = []; // instrumentId + noteId that is currently playing in each 'channel', for routing noteOff/chordOff calls
  16. var notes = {}; // the piano keys
  17. for (var nid = 0; nid < 12; nid ++) {
  18. audioBuffers[nid] = new Audio();
  19. }
  20. var playChannel = function(channel, note) {
  21. if (!root.channels[channel]) return;
  22. var instrument = root.channels[channel].instrument;
  23. var instrumentId = root.GM.byId[instrument].id;
  24. var note = notes[note];
  25. if (note) {
  26. var instrumentNoteId = instrumentId + '' + note.id;
  27. var nid = (buffer_nid + 1) % audioBuffers.length;
  28. var audio = audioBuffers[nid];
  29. notesOn[ nid ] = instrumentNoteId;
  30. if (!root.Soundfont[instrumentId]) {
  31. if (root.DEBUG) {
  32. console.log('404', instrumentId);
  33. }
  34. return;
  35. }
  36. audio.src = root.Soundfont[instrumentId][note.id];
  37. audio.volume = volume / 127;
  38. audio.play();
  39. buffer_nid = nid;
  40. }
  41. };
  42. var stopChannel = function(channel, note) {
  43. if (!root.channels[channel]) return;
  44. var instrument = root.channels[channel].instrument;
  45. var instrumentId = root.GM.byId[instrument].id;
  46. var note = notes[note];
  47. if (note) {
  48. var instrumentNoteId = instrumentId + '' + note.id;
  49. for (var i = 0, len = audioBuffers.length; i < len; i++) {
  50. var nid = (i + buffer_nid + 1) % len;
  51. var cId = notesOn[nid];
  52. if (cId && cId == instrumentNoteId) {
  53. audioBuffers[nid].pause();
  54. notesOn[nid] = null;
  55. return;
  56. }
  57. }
  58. }
  59. };
  60. midi.audioBuffers = audioBuffers;
  61. midi.send = function(data, delay) { };
  62. midi.setController = function(channel, type, value, delay) { };
  63. midi.setVolume = function(channel, n) {
  64. volume = n; //- should be channel specific volume
  65. };
  66. midi.programChange = function(channel, program) {
  67. root.channels[channel].instrument = program;
  68. };
  69. midi.pitchBend = function(channel, program, delay) { };
  70. midi.noteOn = function(channel, note, velocity, delay) {
  71. var id = noteToKey[note];
  72. if (!notes[id]) return;
  73. if (delay) {
  74. return setTimeout(function() {
  75. playChannel(channel, id);
  76. }, delay * 1000);
  77. } else {
  78. playChannel(channel, id);
  79. }
  80. };
  81. midi.noteOff = function(channel, note, delay) {
  82. // var id = noteToKey[note];
  83. // if (!notes[id]) return;
  84. // if (delay) {
  85. // return setTimeout(function() {
  86. // stopChannel(channel, id);
  87. // }, delay * 1000)
  88. // } else {
  89. // stopChannel(channel, id);
  90. // }
  91. };
  92. midi.chordOn = function(channel, chord, velocity, delay) {
  93. for (var idx = 0; idx < chord.length; idx ++) {
  94. var n = chord[idx];
  95. var id = noteToKey[n];
  96. if (!notes[id]) continue;
  97. if (delay) {
  98. return setTimeout(function() {
  99. playChannel(channel, id);
  100. }, delay * 1000);
  101. } else {
  102. playChannel(channel, id);
  103. }
  104. }
  105. };
  106. midi.chordOff = function(channel, chord, delay) {
  107. for (var idx = 0; idx < chord.length; idx ++) {
  108. var n = chord[idx];
  109. var id = noteToKey[n];
  110. if (!notes[id]) continue;
  111. if (delay) {
  112. return setTimeout(function() {
  113. stopChannel(channel, id);
  114. }, delay * 1000);
  115. } else {
  116. stopChannel(channel, id);
  117. }
  118. }
  119. };
  120. midi.stopAllNotes = function() {
  121. for (var nid = 0, length = audioBuffers.length; nid < length; nid++) {
  122. audioBuffers[nid].pause();
  123. }
  124. };
  125. midi.connect = function(opts) {
  126. root.setDefaultPlugin(midi);
  127. ///
  128. for (var key in root.keyToNote) {
  129. noteToKey[root.keyToNote[key]] = key;
  130. notes[key] = {id: key};
  131. }
  132. ///
  133. opts.onsuccess && opts.onsuccess();
  134. };
  135. })();
  136. })(MIDI);