index.js 13 KB

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