index.js 11 KB

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