index.js 11 KB

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