index.js 15 KB

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