index.js 14 KB

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