index.js 15 KB

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