index.js 13 KB

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