index.js 13 KB

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