index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. debugReRenderBtn;
  60. // Initialization code
  61. function init() {
  62. var name, option;
  63. err = document.getElementById("error-td");
  64. error_tr = document.getElementById("error-tr");
  65. zoomDiv = document.getElementById("zoom-str");
  66. custom = document.createElement("option");
  67. selectSample = document.getElementById("selectSample");
  68. selectBounding = document.getElementById("selectBounding");
  69. skylineDebug = document.getElementById("skylineDebug");
  70. bottomlineDebug = document.getElementById("bottomlineDebug");
  71. zoomIn = document.getElementById("zoom-in-btn");
  72. zoomOut = document.getElementById("zoom-out-btn");
  73. canvas = document.createElement("div");
  74. nextCursorBtn = document.getElementById("next-cursor-btn");
  75. resetCursorBtn = document.getElementById("reset-cursor-btn");
  76. showCursorBtn = document.getElementById("show-cursor-btn");
  77. hideCursorBtn = document.getElementById("hide-cursor-btn");
  78. backendSelect = document.getElementById("backend-select");
  79. debugReRenderBtn = document.getElementById("debug-re-render-btn");
  80. // Hide error
  81. error();
  82. // Create select
  83. for (name in samples) {
  84. if (samples.hasOwnProperty(name)) {
  85. option = document.createElement("option");
  86. option.value = samples[name];
  87. option.textContent = name;
  88. }
  89. selectSample.appendChild(option);
  90. }
  91. selectSample.onchange = selectSampleOnChange;
  92. selectBounding.onchange = selectBoundingOnChange;
  93. // Pre-select default music piece
  94. custom.appendChild(document.createTextNode("Custom"));
  95. // Create zoom controls
  96. zoomIn.onclick = function () {
  97. zoom *= 1.2;
  98. scale();
  99. };
  100. zoomOut.onclick = function () {
  101. zoom /= 1.2;
  102. scale();
  103. };
  104. skylineDebug.onclick = function() {
  105. openSheetMusicDisplay.DrawSkyLine = !openSheetMusicDisplay.DrawSkyLine;
  106. }
  107. bottomlineDebug.onclick = function() {
  108. openSheetMusicDisplay.DrawBottomLine = !openSheetMusicDisplay.DrawBottomLine;
  109. }
  110. debugReRenderBtn.onclick = function() {
  111. rerender();
  112. }
  113. // Create OSMD object and canvas
  114. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, {
  115. autoResize: true,
  116. backend: backendSelect.value,
  117. drawingParameters: "default",
  118. disableCursor: false,
  119. });
  120. openSheetMusicDisplay.setLogLevel('info');
  121. document.body.appendChild(canvas);
  122. // Set resize event handler
  123. new Resize(
  124. function(){
  125. if (!sampleLoaded) {
  126. return;
  127. }
  128. disable();
  129. },
  130. function(){
  131. if (!sampleLoaded) {
  132. return;
  133. }
  134. var width = document.body.clientWidth;
  135. canvas.width = width;
  136. try {
  137. openSheetMusicDisplay.render();
  138. } catch (e) {
  139. errorLoadingOrRenderingSheet(e, "rendering");
  140. }
  141. enable();
  142. }
  143. );
  144. window.addEventListener("keydown", function(e) {
  145. var event = window.event ? window.event : e;
  146. if (event.keyCode === 39) {
  147. openSheetMusicDisplay.cursor.next();
  148. }
  149. });
  150. nextCursorBtn.addEventListener("click", function() {
  151. openSheetMusicDisplay.cursor.next();
  152. });
  153. resetCursorBtn.addEventListener("click", function() {
  154. openSheetMusicDisplay.cursor.reset();
  155. });
  156. hideCursorBtn.addEventListener("click", function() {
  157. openSheetMusicDisplay.cursor.hide();
  158. });
  159. showCursorBtn.addEventListener("click", function() {
  160. if (openSheetMusicDisplay.cursor) {
  161. openSheetMusicDisplay.cursor.show();
  162. } else {
  163. console.info("Can't show cursor, as it was disabled (e.g. by drawingParameters).");
  164. }
  165. });
  166. backendSelect.addEventListener("change", function(e) {
  167. var value = e.target.value;
  168. // clears the canvas element
  169. canvas.innerHTML = "";
  170. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, value);
  171. openSheetMusicDisplay.setLogLevel('info');
  172. selectSampleOnChange();
  173. });
  174. }
  175. function Resize(startCallback, endCallback) {
  176. var rtime;
  177. var timeout = false;
  178. var delta = 200;
  179. function resizeEnd() {
  180. timeout = window.clearTimeout(timeout);
  181. if (new Date() - rtime < delta) {
  182. timeout = setTimeout(resizeEnd, delta);
  183. } else {
  184. endCallback();
  185. }
  186. }
  187. window.addEventListener("resize", function () {
  188. rtime = new Date();
  189. if (!timeout) {
  190. startCallback();
  191. rtime = new Date();
  192. timeout = window.setTimeout(resizeEnd, delta);
  193. }
  194. });
  195. window.setTimeout(startCallback, 0);
  196. window.setTimeout(endCallback, 1);
  197. }
  198. function selectBoundingOnChange(evt) {
  199. var value = evt.target.value;
  200. openSheetMusicDisplay.DrawBoundingBox = value;
  201. }
  202. function selectSampleOnChange(str) {
  203. error();
  204. disable();
  205. var isCustom = typeof str === "string";
  206. if (!isCustom) {
  207. str = sampleFolder + selectSample.value;
  208. }
  209. zoom = 1.0;
  210. openSheetMusicDisplay.load(str).then(
  211. function() {
  212. // This gives you access to the osmd object in the console. Do not use in productive code
  213. window.osmd = openSheetMusicDisplay;
  214. return openSheetMusicDisplay.render();
  215. },
  216. function(e) {
  217. errorLoadingOrRenderingSheet(e, "rendering");
  218. }
  219. ).then(
  220. function() {
  221. return onLoadingEnd(isCustom);
  222. }, function(e) {
  223. errorLoadingOrRenderingSheet(e, "loading");
  224. onLoadingEnd(isCustom);
  225. }
  226. );
  227. }
  228. function errorLoadingOrRenderingSheet(e, loadingOrRenderingString) {
  229. var errorString = "Error " + loadingOrRenderingString + " sheet: " + e;
  230. // if (process.env.DEBUG) { // people may not set a debug environment variable for the demo.
  231. // Always giving a StackTrace might give us more and better error reports.
  232. // TODO for a release, StackTrace control could be reenabled
  233. errorString += "\n" + "StackTrace: \n" + e.stack;
  234. // }
  235. console.warn(errorString);
  236. }
  237. function onLoadingEnd(isCustom) {
  238. sampleLoaded = true;
  239. // Remove option from select
  240. if (!isCustom && custom.parentElement === selectSample) {
  241. selectSample.removeChild(custom);
  242. }
  243. // Enable controls again
  244. enable();
  245. }
  246. function logCanvasSize() {
  247. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  248. }
  249. function scale() {
  250. disable();
  251. window.setTimeout(function(){
  252. openSheetMusicDisplay.zoom = zoom;
  253. openSheetMusicDisplay.render();
  254. enable();
  255. }, 0);
  256. }
  257. function rerender() {
  258. disable();
  259. window.setTimeout(function(){
  260. openSheetMusicDisplay.render();
  261. enable();
  262. }, 0);
  263. }
  264. function error(errString) {
  265. if (!errString) {
  266. error_tr.style.display = "none";
  267. } else {
  268. err.textContent = errString;
  269. error_tr.style.display = "";
  270. canvas.width = canvas.height = 0;
  271. enable();
  272. }
  273. }
  274. // Enable/Disable Controls
  275. function disable() {
  276. document.body.style.opacity = 0.3;
  277. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  278. }
  279. function enable() {
  280. document.body.style.opacity = 1;
  281. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  282. logCanvasSize();
  283. }
  284. // Register events: load, drag&drop
  285. window.addEventListener("load", function() {
  286. init();
  287. selectSampleOnChange();
  288. });
  289. window.addEventListener("dragenter", function(event) {
  290. event.preventDefault();
  291. disable();
  292. });
  293. window.addEventListener("dragover", function(event) {
  294. event.preventDefault();
  295. });
  296. window.addEventListener("dragleave", function(event) {
  297. enable();
  298. });
  299. window.addEventListener("drop", function(event) {
  300. event.preventDefault();
  301. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  302. return;
  303. }
  304. // Add "Custom..." score
  305. selectSample.appendChild(custom);
  306. custom.selected = "selected";
  307. // Read dragged file
  308. var reader = new FileReader();
  309. reader.onload = function (res) {
  310. selectSampleOnChange(res.target.result);
  311. };
  312. var filename = event.dataTransfer.files[0].name;
  313. if (filename.toLowerCase().indexOf(".xml") > 0
  314. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  315. reader.readAsText(event.dataTransfer.files[0]);
  316. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  317. reader.readAsBinaryString(event.dataTransfer.files[0]);
  318. }
  319. else {
  320. alert("No vaild .xml/.mxl/.musicxml file!");
  321. }
  322. });
  323. }());