index.js 12 KB

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