index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import { OpenSheetMusicDisplay } from '../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay';
  2. /*jslint browser:true */
  3. (function () {
  4. "use strict";
  5. var openSheetMusicDisplay;
  6. // folder of the sample files
  7. var sampleFolder = process.env.STATIC_FILES_SUBFOLDER ? process.env.STATIC_FILES_SUBFOLDER + "/" : "",
  8. samples = {
  9. "Beethoven, L.v. - An die ferne Geliebte": "Beethoven_AnDieFerneGeliebte.xml",
  10. "Clementi, M. - Sonatina Op.36 No.1 Pt.1": "MuzioClementi_SonatinaOpus36No1_Part1.xml",
  11. "Clementi, M. - Sonatina Op.36 No.1 Pt.2": "MuzioClementi_SonatinaOpus36No1_Part2.xml",
  12. "Clementi, M. - Sonatina Op.36 No.3 Pt.1": "MuzioClementi_SonatinaOpus36No3_Part1.xml",
  13. "Clementi, M. - Sonatina Op.36 No.3 Pt.2": "MuzioClementi_SonatinaOpus36No3_Part2.xml",
  14. "Bach, J.S. - Praeludium in C-Dur BWV846 1": "JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml",
  15. "Bach, J.S. - Air": "JohannSebastianBach_Air.xml",
  16. "Gounod, C. - Meditation": "CharlesGounod_Meditation.xml",
  17. "Haydn, J. - Concertante Cello": "JosephHaydn_ConcertanteCello.xml",
  18. "Joplin, S. - Elite Syncopations": "ScottJoplin_EliteSyncopations.xml",
  19. "Joplin, S. - The Entertainer": "ScottJoplin_The_Entertainer.xml",
  20. "Mozart, W.A. - An Chloe": "Mozart_AnChloe.xml",
  21. "Mozart, W.A. - Das Veilchen": "Mozart_DasVeilchen.xml",
  22. "Mozart, W.A.- Clarinet Quintet (Excerpt)": "Mozart_Clarinet_Quintet_Excerpt.mxl",
  23. "OSMD Function Test - All": "OSMD_function_test_all.xml",
  24. "OSMD Function Test - Grace Notes": "OSMD_function_test_GraceNotes.xml",
  25. "OSMD Function Test - Ornaments": "OSMD_function_test_Ornaments.xml",
  26. "OSMD Function Test - Accidentals": "OSMD_function_test_accidentals.musicxml",
  27. "Schubert, F. - An Die Musik": "Schubert_An_die_Musik.xml",
  28. "Actor, L. - Prelude (Sample)": "ActorPreludeSample.xml",
  29. "Anonymous - Saltarello": "Saltarello.mxl",
  30. "Debussy, C. - Mandoline": "Debussy_Mandoline.xml",
  31. "Levasseur, F. - Parlez Mois": "Parlez-moi.mxl",
  32. "Schumann, R. - Dichterliebe": "Dichterliebe01.xml",
  33. "Telemann, G.P. - Sonate-Nr.1.1-Dolce": "TelemannWV40.102_Sonate-Nr.1.1-Dolce.xml",
  34. "Telemann, G.P. - Sonate-Nr.1.2-Allegro": "TelemannWV40.102_Sonate-Nr.1.2-Allegro-F-Dur.xml",
  35. },
  36. zoom = 1.0,
  37. // HTML Elements in the page
  38. err,
  39. error_tr,
  40. canvas,
  41. selectSample,
  42. selectBounding,
  43. skylineDebug,
  44. bottomlineDebug,
  45. zoomIn,
  46. zoomOut,
  47. zoomDiv,
  48. custom,
  49. nextCursorBtn,
  50. resetCursorBtn,
  51. showCursorBtn,
  52. hideCursorBtn,
  53. backendSelect;
  54. // Initialization code
  55. function init() {
  56. var name, option;
  57. err = document.getElementById("error-td");
  58. error_tr = document.getElementById("error-tr");
  59. zoomDiv = document.getElementById("zoom-str");
  60. custom = document.createElement("option");
  61. selectSample = document.getElementById("selectSample");
  62. selectBounding = document.getElementById("selectBounding");
  63. skylineDebug = document.getElementById("skylineDebug");
  64. bottomlineDebug = document.getElementById("bottomlineDebug");
  65. zoomIn = document.getElementById("zoom-in-btn");
  66. zoomOut = document.getElementById("zoom-out-btn");
  67. canvas = document.createElement("div");
  68. nextCursorBtn = document.getElementById("next-cursor-btn");
  69. resetCursorBtn = document.getElementById("reset-cursor-btn");
  70. showCursorBtn = document.getElementById("show-cursor-btn");
  71. hideCursorBtn = document.getElementById("hide-cursor-btn");
  72. backendSelect = document.getElementById("backend-select");
  73. // Hide error
  74. error();
  75. // Create select
  76. for (name in samples) {
  77. if (samples.hasOwnProperty(name)) {
  78. option = document.createElement("option");
  79. option.value = samples[name];
  80. option.textContent = name;
  81. }
  82. selectSample.appendChild(option);
  83. }
  84. selectSample.onchange = selectSampleOnChange;
  85. selectBounding.onchange = selectBoundingOnChange;
  86. // Pre-select default music piece
  87. custom.appendChild(document.createTextNode("Custom"));
  88. // Create zoom controls
  89. zoomIn.onclick = function () {
  90. zoom *= 1.2;
  91. scale();
  92. };
  93. zoomOut.onclick = function () {
  94. zoom /= 1.2;
  95. scale();
  96. };
  97. skylineDebug.onclick = function() {
  98. openSheetMusicDisplay.DrawSkyLine = !openSheetMusicDisplay.DrawSkyLine;
  99. }
  100. bottomlineDebug .onclick = function() {
  101. openSheetMusicDisplay.DrawBottomLine = !openSheetMusicDisplay.DrawBottomLine;
  102. }
  103. // Create OSMD object and canvas
  104. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, backendSelect.value);
  105. openSheetMusicDisplay.setLogLevel('info');
  106. document.body.appendChild(canvas);
  107. // Set resize event handler
  108. new Resize(
  109. function(){
  110. disable();
  111. },
  112. function() {
  113. var width = document.body.clientWidth;
  114. canvas.width = width;
  115. try {
  116. openSheetMusicDisplay.render();
  117. } catch (e) {}
  118. enable();
  119. }
  120. );
  121. window.addEventListener("keydown", function(e) {
  122. var event = window.event ? window.event : e;
  123. if (event.keyCode === 39) {
  124. openSheetMusicDisplay.cursor.next();
  125. }
  126. });
  127. nextCursorBtn.addEventListener("click", function() {
  128. openSheetMusicDisplay.cursor.next();
  129. });
  130. resetCursorBtn.addEventListener("click", function() {
  131. openSheetMusicDisplay.cursor.reset();
  132. });
  133. hideCursorBtn.addEventListener("click", function() {
  134. openSheetMusicDisplay.cursor.hide();
  135. });
  136. showCursorBtn.addEventListener("click", function() {
  137. openSheetMusicDisplay.cursor.show();
  138. });
  139. backendSelect.addEventListener("change", function(e) {
  140. var value = e.target.value;
  141. // clears the canvas element
  142. canvas.innerHTML = "";
  143. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, value);
  144. openSheetMusicDisplay.setLogLevel('info');
  145. selectSampleOnChange();
  146. });
  147. }
  148. function Resize(startCallback, endCallback) {
  149. var rtime;
  150. var timeout = false;
  151. var delta = 200;
  152. function resizeEnd() {
  153. timeout = window.clearTimeout(timeout);
  154. if (new Date() - rtime < delta) {
  155. timeout = setTimeout(resizeEnd, delta);
  156. } else {
  157. endCallback();
  158. }
  159. }
  160. window.addEventListener("resize", function () {
  161. rtime = new Date();
  162. if (!timeout) {
  163. startCallback();
  164. rtime = new Date();
  165. timeout = window.setTimeout(resizeEnd, delta);
  166. }
  167. });
  168. window.setTimeout(startCallback, 0);
  169. window.setTimeout(endCallback, 1);
  170. }
  171. function selectBoundingOnChange(evt) {
  172. var value = evt.target.value;
  173. openSheetMusicDisplay.DrawBoundingBox = value;
  174. }
  175. function selectSampleOnChange(str) {
  176. error();
  177. disable();
  178. var isCustom = typeof str === "string";
  179. if (!isCustom) {
  180. str = sampleFolder + selectSample.value;
  181. }
  182. zoom = 1.0;
  183. openSheetMusicDisplay.load(str).then(
  184. function() {
  185. return openSheetMusicDisplay.render();
  186. },
  187. function(e) {
  188. console.warn(e.stack);
  189. error("Error reading sheet: " + e);
  190. }
  191. ).then(
  192. function() {
  193. return onLoadingEnd(isCustom);
  194. }, function(e) {
  195. console.warn(e.stack);
  196. error("Error rendering sheet: " + process.env.DEBUG ? e.stack : e);
  197. onLoadingEnd(isCustom);
  198. }
  199. );
  200. }
  201. function onLoadingEnd(isCustom) {
  202. // Remove option from select
  203. if (!isCustom && custom.parentElement === selectSample) {
  204. selectSample.removeChild(custom);
  205. }
  206. // Enable controls again
  207. enable();
  208. }
  209. function logCanvasSize() {
  210. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  211. }
  212. function scale() {
  213. disable();
  214. window.setTimeout(function(){
  215. openSheetMusicDisplay.zoom = zoom;
  216. openSheetMusicDisplay.render();
  217. enable();
  218. }, 0);
  219. }
  220. function error(errString) {
  221. if (!errString) {
  222. error_tr.style.display = "none";
  223. } else {
  224. err.textContent = errString;
  225. error_tr.style.display = "";
  226. canvas.width = canvas.height = 0;
  227. enable();
  228. }
  229. }
  230. // Enable/Disable Controls
  231. function disable() {
  232. document.body.style.opacity = 0.3;
  233. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  234. }
  235. function enable() {
  236. document.body.style.opacity = 1;
  237. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  238. logCanvasSize();
  239. }
  240. // Register events: load, drag&drop
  241. window.addEventListener("load", function() {
  242. init();
  243. selectSampleOnChange();
  244. });
  245. window.addEventListener("dragenter", function(event) {
  246. event.preventDefault();
  247. disable();
  248. });
  249. window.addEventListener("dragover", function(event) {
  250. event.preventDefault();
  251. });
  252. window.addEventListener("dragleave", function(event) {
  253. enable();
  254. });
  255. window.addEventListener("drop", function(event) {
  256. event.preventDefault();
  257. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  258. return;
  259. }
  260. // Add "Custom..." score
  261. selectSample.appendChild(custom);
  262. custom.selected = "selected";
  263. // Read dragged file
  264. var reader = new FileReader();
  265. reader.onload = function (res) {
  266. selectSampleOnChange(res.target.result);
  267. };
  268. var filename = event.dataTransfer.files[0].name;
  269. if (filename.toLowerCase().indexOf(".xml") > 0
  270. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  271. reader.readAsText(event.dataTransfer.files[0]);
  272. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  273. reader.readAsBinaryString(event.dataTransfer.files[0]);
  274. }
  275. else {
  276. alert("No vaild .xml/.mxl/.musicxml file!");
  277. }
  278. });
  279. }());