index.js 15 KB

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