index.js 15 KB

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