index.js 13 KB

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