index.js 15 KB

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