index.js 13 KB

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