index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. console.warn(e.stack);
  120. }
  121. enable();
  122. }
  123. );
  124. window.addEventListener("keydown", function(e) {
  125. var event = window.event ? window.event : e;
  126. if (event.keyCode === 39) {
  127. openSheetMusicDisplay.cursor.next();
  128. }
  129. });
  130. nextCursorBtn.addEventListener("click", function() {
  131. openSheetMusicDisplay.cursor.next();
  132. });
  133. resetCursorBtn.addEventListener("click", function() {
  134. openSheetMusicDisplay.cursor.reset();
  135. });
  136. hideCursorBtn.addEventListener("click", function() {
  137. openSheetMusicDisplay.cursor.hide();
  138. });
  139. showCursorBtn.addEventListener("click", function() {
  140. openSheetMusicDisplay.cursor.show();
  141. });
  142. backendSelect.addEventListener("change", function(e) {
  143. var value = e.target.value;
  144. // clears the canvas element
  145. canvas.innerHTML = "";
  146. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, value);
  147. openSheetMusicDisplay.setLogLevel('info');
  148. selectSampleOnChange();
  149. });
  150. }
  151. function Resize(startCallback, endCallback) {
  152. var rtime;
  153. var timeout = false;
  154. var delta = 200;
  155. function resizeEnd() {
  156. timeout = window.clearTimeout(timeout);
  157. if (new Date() - rtime < delta) {
  158. timeout = setTimeout(resizeEnd, delta);
  159. } else {
  160. endCallback();
  161. }
  162. }
  163. window.addEventListener("resize", function () {
  164. rtime = new Date();
  165. if (!timeout) {
  166. startCallback();
  167. rtime = new Date();
  168. timeout = window.setTimeout(resizeEnd, delta);
  169. }
  170. });
  171. window.setTimeout(startCallback, 0);
  172. window.setTimeout(endCallback, 1);
  173. }
  174. function selectBoundingOnChange(evt) {
  175. var value = evt.target.value;
  176. openSheetMusicDisplay.DrawBoundingBox = value;
  177. }
  178. function selectSampleOnChange(str) {
  179. error();
  180. disable();
  181. var isCustom = typeof str === "string";
  182. if (!isCustom) {
  183. str = sampleFolder + selectSample.value;
  184. }
  185. zoom = 1.0;
  186. openSheetMusicDisplay.load(str).then(
  187. function() {
  188. return openSheetMusicDisplay.render();
  189. },
  190. function(e) {
  191. console.warn(e.stack);
  192. error("Error reading sheet: " + e);
  193. }
  194. ).then(
  195. function() {
  196. return onLoadingEnd(isCustom);
  197. }, function(e) {
  198. console.warn(e.stack);
  199. error("Error rendering sheet: " + process.env.DEBUG ? e.stack : e);
  200. onLoadingEnd(isCustom);
  201. }
  202. );
  203. }
  204. function onLoadingEnd(isCustom) {
  205. // Remove option from select
  206. if (!isCustom && custom.parentElement === selectSample) {
  207. selectSample.removeChild(custom);
  208. }
  209. // Enable controls again
  210. enable();
  211. }
  212. function logCanvasSize() {
  213. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  214. }
  215. function scale() {
  216. disable();
  217. window.setTimeout(function(){
  218. openSheetMusicDisplay.zoom = zoom;
  219. openSheetMusicDisplay.render();
  220. enable();
  221. }, 0);
  222. }
  223. function error(errString) {
  224. if (!errString) {
  225. error_tr.style.display = "none";
  226. } else {
  227. err.textContent = errString;
  228. error_tr.style.display = "";
  229. canvas.width = canvas.height = 0;
  230. enable();
  231. }
  232. }
  233. // Enable/Disable Controls
  234. function disable() {
  235. document.body.style.opacity = 0.3;
  236. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  237. }
  238. function enable() {
  239. document.body.style.opacity = 1;
  240. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  241. logCanvasSize();
  242. }
  243. // Register events: load, drag&drop
  244. window.addEventListener("load", function() {
  245. init();
  246. selectSampleOnChange();
  247. });
  248. window.addEventListener("dragenter", function(event) {
  249. event.preventDefault();
  250. disable();
  251. });
  252. window.addEventListener("dragover", function(event) {
  253. event.preventDefault();
  254. });
  255. window.addEventListener("dragleave", function(event) {
  256. enable();
  257. });
  258. window.addEventListener("drop", function(event) {
  259. event.preventDefault();
  260. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  261. return;
  262. }
  263. // Add "Custom..." score
  264. selectSample.appendChild(custom);
  265. custom.selected = "selected";
  266. // Read dragged file
  267. var reader = new FileReader();
  268. reader.onload = function (res) {
  269. selectSampleOnChange(res.target.result);
  270. };
  271. var filename = event.dataTransfer.files[0].name;
  272. if (filename.toLowerCase().indexOf(".xml") > 0
  273. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  274. reader.readAsText(event.dataTransfer.files[0]);
  275. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  276. reader.readAsBinaryString(event.dataTransfer.files[0]);
  277. }
  278. else {
  279. alert("No vaild .xml/.mxl/.musicxml file!");
  280. }
  281. });
  282. }());