index.js 14 KB

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