index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 - NoteHeadShapes": "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. coloringEnabled: true,
  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. autoBeam: false, // try true, OSMD Function Test AutoBeam sample
  144. autoBeamOptions: {
  145. beam_rests: false,
  146. beam_middle_rests_only: false,
  147. //groups: [[3,4], [1,1]],
  148. maintain_stem_directions: false
  149. },
  150. // tupletsBracketed: true, // creates brackets for all tuplets except triplets, even when not set by xml
  151. // tripletsBracketed: true,
  152. // tupletsRatioed: true, // unconventional; renders ratios for tuplets (3:2 instead of 3 for triplets)
  153. });
  154. openSheetMusicDisplay.setLogLevel('info');
  155. document.body.appendChild(canvas);
  156. window.addEventListener("keydown", function(e) {
  157. var event = window.event ? window.event : e;
  158. if (event.keyCode === 39) {
  159. openSheetMusicDisplay.cursor.next();
  160. }
  161. });
  162. nextCursorBtn.addEventListener("click", function() {
  163. openSheetMusicDisplay.cursor.next();
  164. });
  165. resetCursorBtn.addEventListener("click", function() {
  166. openSheetMusicDisplay.cursor.reset();
  167. });
  168. hideCursorBtn.addEventListener("click", function() {
  169. openSheetMusicDisplay.cursor.hide();
  170. });
  171. showCursorBtn.addEventListener("click", function() {
  172. if (openSheetMusicDisplay.cursor) {
  173. openSheetMusicDisplay.cursor.show();
  174. } else {
  175. console.info("Can't show cursor, as it was disabled (e.g. by drawingParameters).");
  176. }
  177. });
  178. backendSelect.addEventListener("change", function(e) {
  179. var value = e.target.value;
  180. var createNewOsmd = true;
  181. if (createNewOsmd) {
  182. // clears the canvas element
  183. canvas.innerHTML = "";
  184. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, {backend: value});
  185. openSheetMusicDisplay.setLogLevel('info');
  186. } else {
  187. // alternative, doesn't work yet, see setOptions():
  188. openSheetMusicDisplay.setOptions({backend: value});
  189. }
  190. selectSampleOnChange();
  191. });
  192. }
  193. function selectBoundingOnChange(evt) {
  194. var value = evt.target.value;
  195. openSheetMusicDisplay.DrawBoundingBox = value;
  196. }
  197. function selectSampleOnChange(str) {
  198. error();
  199. disable();
  200. var isCustom = typeof str === "string";
  201. if (!isCustom) {
  202. str = sampleFolder + selectSample.value;
  203. }
  204. zoom = 1.0;
  205. openSheetMusicDisplay.load(str).then(
  206. function() {
  207. // This gives you access to the osmd object in the console. Do not use in productive code
  208. window.osmd = openSheetMusicDisplay;
  209. return openSheetMusicDisplay.render();
  210. },
  211. function(e) {
  212. errorLoadingOrRenderingSheet(e, "rendering");
  213. }
  214. ).then(
  215. function() {
  216. return onLoadingEnd(isCustom);
  217. }, function(e) {
  218. errorLoadingOrRenderingSheet(e, "loading");
  219. onLoadingEnd(isCustom);
  220. }
  221. );
  222. }
  223. function errorLoadingOrRenderingSheet(e, loadingOrRenderingString) {
  224. var errorString = "Error " + loadingOrRenderingString + " sheet: " + e;
  225. // if (process.env.DEBUG) { // people may not set a debug environment variable for the demo.
  226. // Always giving a StackTrace might give us more and better error reports.
  227. // TODO for a release, StackTrace control could be reenabled
  228. errorString += "\n" + "StackTrace: \n" + e.stack;
  229. // }
  230. console.warn(errorString);
  231. }
  232. function onLoadingEnd(isCustom) {
  233. // Remove option from select
  234. if (!isCustom && custom.parentElement === selectSample) {
  235. selectSample.removeChild(custom);
  236. }
  237. // Enable controls again
  238. enable();
  239. }
  240. function logCanvasSize() {
  241. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  242. }
  243. function scale() {
  244. disable();
  245. window.setTimeout(function(){
  246. openSheetMusicDisplay.zoom = zoom;
  247. openSheetMusicDisplay.render();
  248. enable();
  249. }, 0);
  250. }
  251. function rerender() {
  252. disable();
  253. window.setTimeout(function(){
  254. if (openSheetMusicDisplay.IsReadyToRender()) {
  255. openSheetMusicDisplay.render();
  256. } else {
  257. selectSampleOnChange(); // reload sample e.g. after osmd.clear()
  258. }
  259. enable();
  260. }, 0);
  261. }
  262. function error(errString) {
  263. if (!errString) {
  264. error_tr.style.display = "none";
  265. } else {
  266. err.textContent = errString;
  267. error_tr.style.display = "";
  268. canvas.width = canvas.height = 0;
  269. enable();
  270. }
  271. }
  272. // Enable/Disable Controls
  273. function disable() {
  274. document.body.style.opacity = 0.3;
  275. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  276. }
  277. function enable() {
  278. document.body.style.opacity = 1;
  279. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  280. logCanvasSize();
  281. }
  282. // Register events: load, drag&drop
  283. window.addEventListener("load", function() {
  284. init();
  285. selectSampleOnChange();
  286. });
  287. window.addEventListener("dragenter", function(event) {
  288. event.preventDefault();
  289. disable();
  290. });
  291. window.addEventListener("dragover", function(event) {
  292. event.preventDefault();
  293. });
  294. window.addEventListener("dragleave", function(event) {
  295. enable();
  296. });
  297. window.addEventListener("drop", function(event) {
  298. event.preventDefault();
  299. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  300. return;
  301. }
  302. // Add "Custom..." score
  303. selectSample.appendChild(custom);
  304. custom.selected = "selected";
  305. // Read dragged file
  306. var reader = new FileReader();
  307. reader.onload = function (res) {
  308. selectSampleOnChange(res.target.result);
  309. };
  310. var filename = event.dataTransfer.files[0].name;
  311. if (filename.toLowerCase().indexOf(".xml") > 0
  312. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  313. reader.readAsText(event.dataTransfer.files[0]);
  314. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  315. reader.readAsBinaryString(event.dataTransfer.files[0]);
  316. }
  317. else {
  318. alert("No vaild .xml/.mxl/.musicxml file!");
  319. }
  320. });
  321. }());