index.js 11 KB

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