index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { OpenSheetMusicDisplay } from '../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay';
  2. /*jslint browser:true */
  3. (function () {
  4. "use strict";
  5. var openSheetMusicDisplay;
  6. var sampleFolder = process.env.STATIC_FILES_SUBFOLDER ? process.env.STATIC_FILES_SUBFOLDER + "/" : "",
  7. samples = {
  8. "Beethoven, L.v. - An die ferne Geliebte": "Beethoven_AnDieFerneGeliebte.xml",
  9. "Clementi, M. - Sonatina Op.36 No.1 Pt.1": "MuzioClementi_SonatinaOpus36No1_Part1.xml",
  10. "Clementi, M. - Sonatina Op.36 No.1 Pt.2": "MuzioClementi_SonatinaOpus36No1_Part2.xml",
  11. "Clementi, M. - Sonatina Op.36 No.3 Pt.1": "MuzioClementi_SonatinaOpus36No3_Part1.xml",
  12. "Clementi, M. - Sonatina Op.36 No.3 Pt.2": "MuzioClementi_SonatinaOpus36No3_Part2.xml",
  13. "Bach, J.S. - Praeludium in C-Dur BWV846 1": "JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml",
  14. "Bach, J.S. - Air": "JohannSebastianBach_Air.xml",
  15. "Gounod, C. - Méditation": "CharlesGounod_Meditation.xml",
  16. "Haydn, J. - Concertante Cello": "JosephHaydn_ConcertanteCello.xml",
  17. "Joplin, S. - Elite Syncopations": "ScottJoplin_EliteSyncopations.xml",
  18. "Joplin, S. - The Entertainer": "ScottJoplin_The_Entertainer.xml",
  19. "Mozart, W.A. - An Chloe": "Mozart_AnChloe.xml",
  20. "Mozart, W.A. - Das Veilchen": "Mozart_DasVeilchen.xml",
  21. "Mozart, W.A.- Clarinet Quintet (Excerpt)": "Mozart_Clarinet_Quintet_Excerpt.mxl",
  22. "Mozart/Holzer - Land der Berge (national anthem of Austria)": "Land_der_Berge.musicxml",
  23. "OSMD Function Test - All": "OSMD_function_test_all.xml",
  24. "OSMD Function Test - Autobeam": "OSMD_function_test_autobeam.musicxml",
  25. "OSMD Function Test - Accidentals": "OSMD_function_test_accidentals.musicxml",
  26. "OSMD Function Test - Drumset": "OSMD_function_test_drumset.musicxml",
  27. "OSMD Function Test - Expressions": "OSMD_function_test_expressions.musicxml",
  28. "OSMD Function Test - Expressions Overlap": "OSMD_function_test_expressions_overlap.musicxml",
  29. "OSMD Function Test - Grace Notes": "OSMD_function_test_GraceNotes.xml",
  30. "OSMD Function Test - NoteHeadShapes": "OSMD_function_test_noteHeadShapes.musicxml",
  31. "OSMD Function Test - Ornaments": "OSMD_function_test_Ornaments.xml",
  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. debugClearBtn;
  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. debugClearBtn = document.getElementById("debug-clear-btn");
  82. // Hide error
  83. error();
  84. // Create select
  85. for (name in samples) {
  86. if (samples.hasOwnProperty(name)) {
  87. option = document.createElement("option");
  88. option.value = samples[name];
  89. option.textContent = name;
  90. }
  91. selectSample.appendChild(option);
  92. }
  93. selectSample.onchange = selectSampleOnChange;
  94. if (selectBounding) {
  95. selectBounding.onchange = selectBoundingOnChange;
  96. }
  97. // Pre-select default music piece
  98. custom.appendChild(document.createTextNode("Custom"));
  99. // Create zoom controls
  100. zoomIn.onclick = function () {
  101. zoom *= 1.2;
  102. scale();
  103. };
  104. zoomOut.onclick = function () {
  105. zoom /= 1.2;
  106. scale();
  107. };
  108. if (skylineDebug) {
  109. skylineDebug.onclick = function() {
  110. openSheetMusicDisplay.DrawSkyLine = !openSheetMusicDisplay.DrawSkyLine;
  111. }
  112. }
  113. if (bottomlineDebug) {
  114. bottomlineDebug.onclick = function() {
  115. openSheetMusicDisplay.DrawBottomLine = !openSheetMusicDisplay.DrawBottomLine;
  116. }
  117. }
  118. if (debugReRenderBtn) {
  119. debugReRenderBtn.onclick = function() {
  120. rerender();
  121. }
  122. }
  123. if (debugClearBtn) {
  124. debugClearBtn.onclick = function() {
  125. openSheetMusicDisplay.clear();
  126. }
  127. }
  128. // Create OSMD object and canvas
  129. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, {
  130. autoResize: true,
  131. backend: backendSelect.value,
  132. disableCursor: false,
  133. drawingParameters: "default", // try compact (instead of default)
  134. drawPartNames: true, // try false
  135. // drawTitle: false,
  136. // drawSubtitle: false,
  137. drawFingerings: true,
  138. fingeringPosition: "auto", // left is default. try right. experimental: auto, above, below.
  139. // fingeringInsideStafflines: "true", // default: false. true draws fingerings directly above/below notes
  140. setWantedStemDirectionByXml: true, // try false, which was previously the default behavior
  141. autoBeam: false, // try true, OSMD Function Test AutoBeam sample
  142. autoBeamOptions: {
  143. beam_rests: false,
  144. beam_middle_rests_only: false,
  145. //groups: [[3,4], [1,1]],
  146. maintain_stem_directions: false
  147. },
  148. // tupletsBracketed: true, // creates brackets for all tuplets except triplets, even when not set by xml
  149. // tripletsBracketed: true,
  150. // tupletsRatioed: true, // unconventional; renders ratios for tuplets (3:2 instead of 3 for triplets)
  151. });
  152. openSheetMusicDisplay.setLogLevel('info');
  153. document.body.appendChild(canvas);
  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 selectBoundingOnChange(evt) {
  192. var value = evt.target.value;
  193. openSheetMusicDisplay.DrawBoundingBox = value;
  194. }
  195. function selectSampleOnChange(str) {
  196. error();
  197. disable();
  198. var isCustom = typeof str === "string";
  199. if (!isCustom) {
  200. str = sampleFolder + selectSample.value;
  201. }
  202. zoom = 1.0;
  203. openSheetMusicDisplay.load(str).then(
  204. function() {
  205. // This gives you access to the osmd object in the console. Do not use in productive code
  206. window.osmd = openSheetMusicDisplay;
  207. return openSheetMusicDisplay.render();
  208. },
  209. function(e) {
  210. errorLoadingOrRenderingSheet(e, "rendering");
  211. }
  212. ).then(
  213. function() {
  214. return onLoadingEnd(isCustom);
  215. }, function(e) {
  216. errorLoadingOrRenderingSheet(e, "loading");
  217. onLoadingEnd(isCustom);
  218. }
  219. );
  220. }
  221. function errorLoadingOrRenderingSheet(e, loadingOrRenderingString) {
  222. var errorString = "Error " + loadingOrRenderingString + " sheet: " + e;
  223. // if (process.env.DEBUG) { // people may not set a debug environment variable for the demo.
  224. // Always giving a StackTrace might give us more and better error reports.
  225. // TODO for a release, StackTrace control could be reenabled
  226. errorString += "\n" + "StackTrace: \n" + e.stack;
  227. // }
  228. console.warn(errorString);
  229. }
  230. function onLoadingEnd(isCustom) {
  231. // Remove option from select
  232. if (!isCustom && custom.parentElement === selectSample) {
  233. selectSample.removeChild(custom);
  234. }
  235. // Enable controls again
  236. enable();
  237. }
  238. function logCanvasSize() {
  239. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  240. }
  241. function scale() {
  242. disable();
  243. window.setTimeout(function(){
  244. openSheetMusicDisplay.zoom = zoom;
  245. openSheetMusicDisplay.render();
  246. enable();
  247. }, 0);
  248. }
  249. function rerender() {
  250. disable();
  251. window.setTimeout(function(){
  252. if (openSheetMusicDisplay.IsReadyToRender()) {
  253. openSheetMusicDisplay.render();
  254. } else {
  255. selectSampleOnChange(); // reload sample e.g. after osmd.clear()
  256. }
  257. enable();
  258. }, 0);
  259. }
  260. function error(errString) {
  261. if (!errString) {
  262. error_tr.style.display = "none";
  263. } else {
  264. err.textContent = errString;
  265. error_tr.style.display = "";
  266. canvas.width = canvas.height = 0;
  267. enable();
  268. }
  269. }
  270. // Enable/Disable Controls
  271. function disable() {
  272. document.body.style.opacity = 0.3;
  273. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  274. }
  275. function enable() {
  276. document.body.style.opacity = 1;
  277. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  278. logCanvasSize();
  279. }
  280. // Register events: load, drag&drop
  281. window.addEventListener("load", function() {
  282. init();
  283. selectSampleOnChange();
  284. });
  285. window.addEventListener("dragenter", function(event) {
  286. event.preventDefault();
  287. disable();
  288. });
  289. window.addEventListener("dragover", function(event) {
  290. event.preventDefault();
  291. });
  292. window.addEventListener("dragleave", function(event) {
  293. enable();
  294. });
  295. window.addEventListener("drop", function(event) {
  296. event.preventDefault();
  297. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  298. return;
  299. }
  300. // Add "Custom..." score
  301. selectSample.appendChild(custom);
  302. custom.selected = "selected";
  303. // Read dragged file
  304. var reader = new FileReader();
  305. reader.onload = function (res) {
  306. selectSampleOnChange(res.target.result);
  307. };
  308. var filename = event.dataTransfer.files[0].name;
  309. if (filename.toLowerCase().indexOf(".xml") > 0
  310. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  311. reader.readAsText(event.dataTransfer.files[0]);
  312. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  313. reader.readAsBinaryString(event.dataTransfer.files[0]);
  314. }
  315. else {
  316. alert("No vaild .xml/.mxl/.musicxml file!");
  317. }
  318. });
  319. }());