player.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. "undefined" === typeof MIDI && (MIDI = {});
  2. "undefined" === typeof MIDI.Player && (MIDI.Player = {});
  3. (function() {
  4. var b = MIDI.Player;
  5. b.callback = void 0;
  6. b.currentTime = 0;
  7. b.endTime = 0;
  8. b.restart = 0;
  9. b.playing = !1;
  10. b.timeWarp = 1;
  11. b.start = b.resume = function() {
  12. -1 > b.currentTime && (b.currentTime = -1);
  13. l(b.currentTime)
  14. }
  15. ;
  16. b.pause = function() {
  17. var a = b.restart;
  18. n();
  19. b.restart = a
  20. }
  21. ;
  22. b.stop = function() {
  23. n();
  24. b.restart = 0;
  25. b.currentTime = 0
  26. }
  27. ;
  28. b.addListener = function(a) {
  29. k = a
  30. }
  31. ;
  32. b.removeListener = function() {
  33. k = void 0
  34. }
  35. ;
  36. b.clearAnimation = function() {
  37. b.interval && window.clearInterval(b.interval)
  38. }
  39. ;
  40. b.setAnimation = function(a) {
  41. var c = "function" === typeof a ? a : a.callback;
  42. a = a.interval || 30;
  43. var e = 0
  44. , k = 0
  45. , f = 0;
  46. b.clearAnimation();
  47. b.interval = window.setInterval(function() {
  48. if (0 !== b.endTime) {
  49. b.playing ? (e = f === b.currentTime ? k - (new Date).getTime() : 0,
  50. e = 0 === b.currentTime ? 0 : b.currentTime - e,
  51. f !== b.currentTime && (k = (new Date).getTime(),
  52. f = b.currentTime)) : e = b.currentTime;
  53. var a = e / 1E3
  54. , l = a / 60
  55. , a = 60 * l + (a - 60 * l)
  56. , l = b.endTime / 1E3;
  57. -1 > l - a || c({
  58. now: a,
  59. end: l,
  60. events: d
  61. })
  62. }
  63. }, a)
  64. }
  65. ;
  66. b.loadMidiFile = function() {
  67. b.replayer = new Replayer(MidiFile(b.currentData),b.timeWarp);
  68. b.data = b.replayer.getData();
  69. b.endTime = f()
  70. }
  71. ;
  72. b.loadFile = function(a, d) {
  73. b.stop();
  74. if (-1 !== a.indexOf("base64,")) {
  75. var c = window.atob(a.split(",")[1]);
  76. b.currentData = c;
  77. b.loadMidiFile();
  78. d && d(c)
  79. } else
  80. c = new XMLHttpRequest,
  81. c.open("GET", a),
  82. c.overrideMimeType("text/plain; charset=x-user-defined"),
  83. c.onreadystatechange = function() {
  84. if (4 === this.readyState && 200 === this.status) {
  85. for (var a = this.responseText || "", c = [], e = a.length, k = String.fromCharCode, f = 0; f < e; f++)
  86. c[f] = k(a.charCodeAt(f) & 255);
  87. a = c.join("");
  88. b.currentData = a;
  89. b.loadMidiFile();
  90. d && d(a)
  91. }
  92. }
  93. ,
  94. c.send()
  95. }
  96. ;
  97. var a = [], c, e = 0, d = {}, k = void 0, m = function(a, e, f, n, g, m) {
  98. return window.setTimeout(function() {
  99. var n = {
  100. channel: a,
  101. note: e,
  102. now: f,
  103. end: b.endTime,
  104. message: g,
  105. velocity: m
  106. };
  107. 128 === g ? delete d[e] : d[e] = n;
  108. k && k(n);
  109. b.currentTime = f;
  110. b.currentTime === c && c < b.endTime && l(c, !0)
  111. }, f - n)
  112. }, g = function() {
  113. if ("WebAudioAPI" === MIDI.lang)
  114. return MIDI.Player.ctx;
  115. b.ctx || (b.ctx = {
  116. currentTime: 0
  117. });
  118. return b.ctx
  119. }, f = function() {
  120. for (var a = b.data, d = a.length, c = 0.5, e = 0; e < d; e++)
  121. c += a[e][1];
  122. return c
  123. }, l = function(d, k) {
  124. if (b.replayer) {
  125. k || ("undefined" === typeof d && (d = b.restart),
  126. b.playing && n(),
  127. b.playing = !0,
  128. b.data = b.replayer.getData(),
  129. b.endTime = f());
  130. var l, s = 0, t = 0, v = b.data, w = g(), F = v.length;
  131. c = 0.5;
  132. e = w.currentTime;
  133. for (var B = 0; B < F; B++)
  134. if (l = v[B],
  135. l = l[1] || 1E-11,
  136. c + l <= d)
  137. s = c += l;
  138. else
  139. break;
  140. for (; B < F && 100 > t; B++) {
  141. l = v[B];
  142. c += l[1] || 1E-11;
  143. d = c - s;
  144. var x = l[0].event;
  145. if ("channel" === x.type) {
  146. var u = 0;
  147. // var u = x.channel;
  148. switch (x.subtype) {
  149. case "noteOn":
  150. if (MIDI.channels[u].mute)
  151. break;
  152. l = x.noteNumber - (b.MIDIOffset || 0);
  153. a.push({
  154. event: x,
  155. source: MIDI.noteOn(u, x.noteNumber, x.velocity, d / 1E3 + w.currentTime),
  156. interval: m(u, l, c, s, 144, x.velocity)
  157. });
  158. t++;
  159. break;
  160. case "noteOff":
  161. if (MIDI.channels[u].mute)
  162. break;
  163. l = x.noteNumber - (b.MIDIOffset || 0);
  164. a.push({
  165. event: x,
  166. source: MIDI.noteOff(u, x.noteNumber, d / 1E3 + w.currentTime),
  167. interval: m(u, l, c, s, 128)
  168. })
  169. }
  170. }
  171. }
  172. }
  173. }, n = function() {
  174. var c = g();
  175. b.playing = !1;
  176. for (b.restart += 1E3 * (c.currentTime - e); a.length; )
  177. c = a.pop(),
  178. window.clearInterval(c.interval),
  179. c.source && ("number" === typeof c.source ? window.clearTimeout(c.source) : c.source.disconnect(0));
  180. for (var f in d)
  181. c = d[f],
  182. 144 === d[f].message && k && k({
  183. channel: c.channel,
  184. note: c.note,
  185. now: c.now,
  186. end: c.end,
  187. message: 128,
  188. velocity: c.velocity
  189. });
  190. d = {}
  191. }
  192. }
  193. )();