index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import { OpenSheetMusicDisplay } from '../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay';
  2. /*jslint browser:true */
  3. (function () {
  4. "use strict";
  5. var openSheetMusicDisplay;
  6. var sampleLoaded = false;
  7. // folder of the sample files
  8. var sampleFolder = process.env.STATIC_FILES_SUBFOLDER ? process.env.STATIC_FILES_SUBFOLDER + "/" : "",
  9. samples = {
  10. "Beethoven, L.v. - An die ferne Geliebte": "Beethoven_AnDieFerneGeliebte.xml",
  11. "Clementi, M. - Sonatina Op.36 No.1 Pt.1": "MuzioClementi_SonatinaOpus36No1_Part1.xml",
  12. "Clementi, M. - Sonatina Op.36 No.1 Pt.2": "MuzioClementi_SonatinaOpus36No1_Part2.xml",
  13. "Clementi, M. - Sonatina Op.36 No.3 Pt.1": "MuzioClementi_SonatinaOpus36No3_Part1.xml",
  14. "Clementi, M. - Sonatina Op.36 No.3 Pt.2": "MuzioClementi_SonatinaOpus36No3_Part2.xml",
  15. "Bach, J.S. - Praeludium in C-Dur BWV846 1": "JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml",
  16. "Bach, J.S. - Air": "JohannSebastianBach_Air.xml",
  17. "Gounod, C. - Méditation": "CharlesGounod_Meditation.xml",
  18. "Haydn, J. - Concertante Cello": "JosephHaydn_ConcertanteCello.xml",
  19. "Joplin, S. - Elite Syncopations": "ScottJoplin_EliteSyncopations.xml",
  20. "Joplin, S. - The Entertainer": "ScottJoplin_The_Entertainer.xml",
  21. "Mozart, W.A. - An Chloe": "Mozart_AnChloe.xml",
  22. "Mozart, W.A. - Das Veilchen": "Mozart_DasVeilchen.xml",
  23. "Mozart, W.A.- Clarinet Quintet (Excerpt)": "Mozart_Clarinet_Quintet_Excerpt.mxl",
  24. "Mozart/Holzer - Land der Berge (national anthem of Austria)": "Land_der_Berge.musicxml",
  25. "OSMD Function Test - All": "OSMD_function_test_all.xml",
  26. "OSMD Function Test - Grace Notes": "OSMD_function_test_GraceNotes.xml",
  27. "OSMD Function Test - Ornaments": "OSMD_function_test_Ornaments.xml",
  28. "OSMD Function Test - Accidentals": "OSMD_function_test_accidentals.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 - NoteHeadShapes": "OSMD_function_test_noteHeadShapes.musicxml",
  32. "OSMD Function Test - Drumset": "OSMD_function_test_drumset.musicxml",
  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. // Initialization code
  62. function init() {
  63. var name, option;
  64. err = document.getElementById("error-td");
  65. error_tr = document.getElementById("error-tr");
  66. zoomDiv = document.getElementById("zoom-str");
  67. custom = document.createElement("option");
  68. selectSample = document.getElementById("selectSample");
  69. selectBounding = document.getElementById("selectBounding");
  70. skylineDebug = document.getElementById("skylineDebug");
  71. bottomlineDebug = document.getElementById("bottomlineDebug");
  72. zoomIn = document.getElementById("zoom-in-btn");
  73. zoomOut = document.getElementById("zoom-out-btn");
  74. canvas = document.createElement("div");
  75. nextCursorBtn = document.getElementById("next-cursor-btn");
  76. resetCursorBtn = document.getElementById("reset-cursor-btn");
  77. showCursorBtn = document.getElementById("show-cursor-btn");
  78. hideCursorBtn = document.getElementById("hide-cursor-btn");
  79. backendSelect = document.getElementById("backend-select");
  80. debugReRenderBtn = document.getElementById("debug-re-render-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. selectBounding.onchange = selectBoundingOnChange;
  94. // Pre-select default music piece
  95. custom.appendChild(document.createTextNode("Custom"));
  96. // Create zoom controls
  97. zoomIn.onclick = function () {
  98. zoom *= 1.2;
  99. scale();
  100. };
  101. zoomOut.onclick = function () {
  102. zoom /= 1.2;
  103. scale();
  104. };
  105. skylineDebug.onclick = function() {
  106. openSheetMusicDisplay.DrawSkyLine = !openSheetMusicDisplay.DrawSkyLine;
  107. }
  108. bottomlineDebug.onclick = function() {
  109. openSheetMusicDisplay.DrawBottomLine = !openSheetMusicDisplay.DrawBottomLine;
  110. }
  111. debugReRenderBtn.onclick = function() {
  112. rerender();
  113. }
  114. // Create OSMD object and canvas
  115. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, {
  116. autoResize: true,
  117. backend: backendSelect.value,
  118. disableCursor: false,
  119. drawingParameters: "default", // try compact (instead of default)
  120. drawPartNames: true, // try false
  121. // drawTitle: false,
  122. // drawSubtitle: false,
  123. drawFingerings: true,
  124. fingeringPosition: "auto", // left is default. try right. experimental: auto, above, below.
  125. // fingeringInsideStafflines: "true", // default: false. true draws fingerings directly above/below notes
  126. setWantedStemDirectionByXml: true, // try false, which was previously the default behavior
  127. // tupletsBracketed: true, // creates brackets for all tuplets except triplets, even when not set by xml
  128. // tripletsBracketed: true,
  129. // tupletsRatioed: true, // unconventional; renders ratios for tuplets (3:2 instead of 3 for triplets)
  130. });
  131. openSheetMusicDisplay.setLogLevel('info');
  132. document.body.appendChild(canvas);
  133. // Set resize event handler
  134. new Resize(
  135. function(){
  136. if (!sampleLoaded) {
  137. return;
  138. }
  139. disable();
  140. },
  141. function(){
  142. if (!sampleLoaded) {
  143. return;
  144. }
  145. var width = document.body.clientWidth;
  146. canvas.width = width;
  147. try {
  148. openSheetMusicDisplay.render();
  149. } catch (e) {
  150. errorLoadingOrRenderingSheet(e, "rendering");
  151. }
  152. enable();
  153. }
  154. );
  155. window.addEventListener("keydown", function(e) {
  156. var event = window.event ? window.event : e;
  157. if (event.keyCode === 39) {
  158. openSheetMusicDisplay.cursor.next();
  159. }
  160. });
  161. nextCursorBtn.addEventListener("click", function() {
  162. openSheetMusicDisplay.cursor.next();
  163. });
  164. resetCursorBtn.addEventListener("click", function() {
  165. openSheetMusicDisplay.cursor.reset();
  166. });
  167. hideCursorBtn.addEventListener("click", function() {
  168. openSheetMusicDisplay.cursor.hide();
  169. });
  170. showCursorBtn.addEventListener("click", function() {
  171. if (openSheetMusicDisplay.cursor) {
  172. openSheetMusicDisplay.cursor.show();
  173. } else {
  174. console.info("Can't show cursor, as it was disabled (e.g. by drawingParameters).");
  175. }
  176. });
  177. backendSelect.addEventListener("change", function(e) {
  178. var value = e.target.value;
  179. var createNewOsmd = true;
  180. if (createNewOsmd) {
  181. // clears the canvas element
  182. canvas.innerHTML = "";
  183. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, {backend: value});
  184. openSheetMusicDisplay.setLogLevel('info');
  185. } else {
  186. // alternative, doesn't work yet, see setOptions():
  187. openSheetMusicDisplay.setOptions({backend: value});
  188. }
  189. selectSampleOnChange();
  190. });
  191. }
  192. function Resize(startCallback, endCallback) {
  193. var rtime;
  194. var timeout = false;
  195. var delta = 200;
  196. function resizeEnd() {
  197. timeout = window.clearTimeout(timeout);
  198. if (new Date() - rtime < delta) {
  199. timeout = setTimeout(resizeEnd, delta);
  200. } else {
  201. endCallback();
  202. }
  203. }
  204. window.addEventListener("resize", function () {
  205. rtime = new Date();
  206. if (!timeout) {
  207. startCallback();
  208. rtime = new Date();
  209. timeout = window.setTimeout(resizeEnd, delta);
  210. }
  211. });
  212. window.setTimeout(startCallback, 0);
  213. window.setTimeout(endCallback, 1);
  214. }
  215. function selectBoundingOnChange(evt) {
  216. var value = evt.target.value;
  217. openSheetMusicDisplay.DrawBoundingBox = value;
  218. }
  219. function selectSampleOnChange(str) {
  220. error();
  221. disable();
  222. var isCustom = typeof str === "string";
  223. if (!isCustom) {
  224. str = sampleFolder + selectSample.value;
  225. }
  226. zoom = 1.0;
  227. openSheetMusicDisplay.load(str).then(
  228. function() {
  229. // This gives you access to the osmd object in the console. Do not use in productive code
  230. window.osmd = openSheetMusicDisplay;
  231. return openSheetMusicDisplay.render();
  232. },
  233. function(e) {
  234. errorLoadingOrRenderingSheet(e, "rendering");
  235. }
  236. ).then(
  237. function() {
  238. return onLoadingEnd(isCustom);
  239. }, function(e) {
  240. errorLoadingOrRenderingSheet(e, "loading");
  241. onLoadingEnd(isCustom);
  242. }
  243. );
  244. }
  245. function errorLoadingOrRenderingSheet(e, loadingOrRenderingString) {
  246. var errorString = "Error " + loadingOrRenderingString + " sheet: " + e;
  247. // if (process.env.DEBUG) { // people may not set a debug environment variable for the demo.
  248. // Always giving a StackTrace might give us more and better error reports.
  249. // TODO for a release, StackTrace control could be reenabled
  250. errorString += "\n" + "StackTrace: \n" + e.stack;
  251. // }
  252. console.warn(errorString);
  253. }
  254. function onLoadingEnd(isCustom) {
  255. sampleLoaded = true;
  256. // Remove option from select
  257. if (!isCustom && custom.parentElement === selectSample) {
  258. selectSample.removeChild(custom);
  259. }
  260. // Enable controls again
  261. enable();
  262. }
  263. function logCanvasSize() {
  264. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  265. }
  266. function scale() {
  267. disable();
  268. window.setTimeout(function(){
  269. openSheetMusicDisplay.zoom = zoom;
  270. openSheetMusicDisplay.render();
  271. enable();
  272. }, 0);
  273. }
  274. function rerender() {
  275. disable();
  276. window.setTimeout(function(){
  277. openSheetMusicDisplay.render();
  278. enable();
  279. }, 0);
  280. }
  281. function error(errString) {
  282. if (!errString) {
  283. error_tr.style.display = "none";
  284. } else {
  285. err.textContent = errString;
  286. error_tr.style.display = "";
  287. canvas.width = canvas.height = 0;
  288. enable();
  289. }
  290. }
  291. // Enable/Disable Controls
  292. function disable() {
  293. document.body.style.opacity = 0.3;
  294. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  295. }
  296. function enable() {
  297. document.body.style.opacity = 1;
  298. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  299. logCanvasSize();
  300. }
  301. // Register events: load, drag&drop
  302. window.addEventListener("load", function() {
  303. init();
  304. selectSampleOnChange();
  305. });
  306. window.addEventListener("dragenter", function(event) {
  307. event.preventDefault();
  308. disable();
  309. });
  310. window.addEventListener("dragover", function(event) {
  311. event.preventDefault();
  312. });
  313. window.addEventListener("dragleave", function(event) {
  314. enable();
  315. });
  316. window.addEventListener("drop", function(event) {
  317. event.preventDefault();
  318. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  319. return;
  320. }
  321. // Add "Custom..." score
  322. selectSample.appendChild(custom);
  323. custom.selected = "selected";
  324. // Read dragged file
  325. var reader = new FileReader();
  326. reader.onload = function (res) {
  327. selectSampleOnChange(res.target.result);
  328. };
  329. var filename = event.dataTransfer.files[0].name;
  330. if (filename.toLowerCase().indexOf(".xml") > 0
  331. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  332. reader.readAsText(event.dataTransfer.files[0]);
  333. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  334. reader.readAsBinaryString(event.dataTransfer.files[0]);
  335. }
  336. else {
  337. alert("No vaild .xml/.mxl/.musicxml file!");
  338. }
  339. });
  340. }());