index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. //drawFromMeasureNumber: 4,
  144. //drawUpToMeasureNumber: 8,
  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. hideCursorBtn.addEventListener("click", function() {
  180. openSheetMusicDisplay.cursor.hide();
  181. });
  182. showCursorBtn.addEventListener("click", function() {
  183. if (openSheetMusicDisplay.cursor) {
  184. openSheetMusicDisplay.cursor.show();
  185. } else {
  186. console.info("Can't show cursor, as it was disabled (e.g. by drawingParameters).");
  187. }
  188. });
  189. backendSelect.addEventListener("change", function(e) {
  190. var value = e.target.value;
  191. var createNewOsmd = true;
  192. if (createNewOsmd) {
  193. // clears the canvas element
  194. canvas.innerHTML = "";
  195. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, {backend: value});
  196. openSheetMusicDisplay.setLogLevel('info');
  197. } else {
  198. // alternative, doesn't work yet, see setOptions():
  199. openSheetMusicDisplay.setOptions({backend: value});
  200. }
  201. selectSampleOnChange();
  202. });
  203. }
  204. function selectBoundingOnChange(evt) {
  205. var value = evt.target.value;
  206. openSheetMusicDisplay.DrawBoundingBox = value;
  207. }
  208. function selectSampleOnChange(str) {
  209. error();
  210. disable();
  211. var isCustom = typeof str === "string";
  212. if (!isCustom) {
  213. str = sampleFolder + selectSample.value;
  214. }
  215. zoom = 1.0;
  216. // for debugging: draw from a random range of measures
  217. /*let minMeasureToDraw = Math.floor(Math.random() * 20);
  218. let maxMeasureToDraw = Math.floor(Math.random() * 20);
  219. if (minMeasureToDraw > maxMeasureToDraw) {
  220. minMeasureToDraw = maxMeasureToDraw;
  221. let a = minMeasureToDraw;
  222. maxMeasureToDraw = a;
  223. }
  224. //minMeasureToDraw = 17; // set your custom indexes here. Drawing only one measure can be a special case
  225. //maxMeasureToDraw = 17;
  226. console.log("drawing measures in the range: [" + minMeasureToDraw + "," + maxMeasureToDraw + "]");
  227. openSheetMusicDisplay.setOptions({
  228. drawFromMeasureNumber: minMeasureToDraw,
  229. drawUpToMeasureNumber: maxMeasureToDraw
  230. });*/
  231. // Enable Boomwhacker-like coloring for OSMD Function Test - Auto-Coloring (Boomwhacker-like, custom color set)
  232. if (str.includes("auto-custom-coloring")) {
  233. //openSheetMusicDisplay.setOptions({coloringMode: 1}); // Auto-Coloring with pre-defined colors
  234. openSheetMusicDisplay.setOptions({
  235. coloringMode: 2,
  236. coloringSetCustom: ["#d82c6b", "#F89D15", "#FFE21A", "#4dbd5c", "#009D96", "#43469d", "#76429c", "#ff0000"]
  237. // last color value of coloringSetCustom is for rest notes
  238. });
  239. } else {
  240. openSheetMusicDisplay.setOptions({coloringMode: 0});
  241. }
  242. openSheetMusicDisplay.setOptions({autoBeam: str.includes("autobeam")});
  243. openSheetMusicDisplay.setOptions({drawPartAbbreviations: !str.includes("Schubert_An_die_Musik")}); // TODO weird layout bug here. but shouldn't be in score anyways
  244. openSheetMusicDisplay.load(str).then(
  245. function() {
  246. // This gives you access to the osmd object in the console. Do not use in productive code
  247. window.osmd = openSheetMusicDisplay;
  248. return openSheetMusicDisplay.render();
  249. },
  250. function(e) {
  251. errorLoadingOrRenderingSheet(e, "rendering");
  252. }
  253. ).then(
  254. function() {
  255. return onLoadingEnd(isCustom);
  256. }, function(e) {
  257. errorLoadingOrRenderingSheet(e, "loading");
  258. onLoadingEnd(isCustom);
  259. }
  260. );
  261. }
  262. function errorLoadingOrRenderingSheet(e, loadingOrRenderingString) {
  263. var errorString = "Error " + loadingOrRenderingString + " sheet: " + e;
  264. // if (process.env.DEBUG) { // people may not set a debug environment variable for the demo.
  265. // Always giving a StackTrace might give us more and better error reports.
  266. // TODO for a release, StackTrace control could be reenabled
  267. errorString += "\n" + "StackTrace: \n" + e.stack;
  268. // }
  269. console.warn(errorString);
  270. }
  271. function onLoadingEnd(isCustom) {
  272. // Remove option from select
  273. if (!isCustom && custom.parentElement === selectSample) {
  274. selectSample.removeChild(custom);
  275. }
  276. // Enable controls again
  277. enable();
  278. }
  279. function logCanvasSize() {
  280. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  281. }
  282. function scale() {
  283. disable();
  284. window.setTimeout(function(){
  285. openSheetMusicDisplay.zoom = zoom;
  286. openSheetMusicDisplay.render();
  287. enable();
  288. }, 0);
  289. }
  290. function rerender() {
  291. disable();
  292. window.setTimeout(function(){
  293. if (openSheetMusicDisplay.IsReadyToRender()) {
  294. openSheetMusicDisplay.render();
  295. } else {
  296. selectSampleOnChange(); // reload sample e.g. after osmd.clear()
  297. }
  298. enable();
  299. }, 0);
  300. }
  301. function error(errString) {
  302. if (!errString) {
  303. error_tr.style.display = "none";
  304. } else {
  305. err.textContent = errString;
  306. error_tr.style.display = "";
  307. canvas.width = canvas.height = 0;
  308. enable();
  309. }
  310. }
  311. // Enable/Disable Controls
  312. function disable() {
  313. document.body.style.opacity = 0.3;
  314. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  315. }
  316. function enable() {
  317. document.body.style.opacity = 1;
  318. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  319. logCanvasSize();
  320. }
  321. // Register events: load, drag&drop
  322. window.addEventListener("load", function() {
  323. init();
  324. selectSampleOnChange();
  325. });
  326. window.addEventListener("dragenter", function(event) {
  327. event.preventDefault();
  328. disable();
  329. });
  330. window.addEventListener("dragover", function(event) {
  331. event.preventDefault();
  332. });
  333. window.addEventListener("dragleave", function(event) {
  334. enable();
  335. });
  336. window.addEventListener("drop", function(event) {
  337. event.preventDefault();
  338. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  339. return;
  340. }
  341. // Add "Custom..." score
  342. selectSample.appendChild(custom);
  343. custom.selected = "selected";
  344. // Read dragged file
  345. var reader = new FileReader();
  346. reader.onload = function (res) {
  347. selectSampleOnChange(res.target.result);
  348. };
  349. var filename = event.dataTransfer.files[0].name;
  350. if (filename.toLowerCase().indexOf(".xml") > 0
  351. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  352. reader.readAsText(event.dataTransfer.files[0]);
  353. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  354. reader.readAsBinaryString(event.dataTransfer.files[0]);
  355. }
  356. else {
  357. alert("No vaild .xml/.mxl/.musicxml file!");
  358. }
  359. });
  360. }());