index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 - NoteHeadShapes": "OSMD_function_test_noteHeadShapes.musicxml",
  31. "OSMD Function Test - Drumset": "OSMD_function_test_drumset.musicxml",
  32. "Schubert, F. - An Die Musik": "Schubert_An_die_Musik.xml",
  33. "Actor, L. - Prelude (Sample)": "ActorPreludeSample.xml",
  34. "Anonymous - Saltarello": "Saltarello.mxl",
  35. "Debussy, C. - Mandoline": "Debussy_Mandoline.xml",
  36. "Levasseur, F. - Parlez Mois": "Parlez-moi.mxl",
  37. "Schumann, R. - Dichterliebe": "Dichterliebe01.xml",
  38. "Telemann, G.P. - Sonate-Nr.1.1-Dolce": "TelemannWV40.102_Sonate-Nr.1.1-Dolce.xml",
  39. "Telemann, G.P. - Sonate-Nr.1.2-Allegro": "TelemannWV40.102_Sonate-Nr.1.2-Allegro-F-Dur.xml",
  40. },
  41. zoom = 1.0,
  42. // HTML Elements in the page
  43. err,
  44. error_tr,
  45. canvas,
  46. selectSample,
  47. selectBounding,
  48. skylineDebug,
  49. bottomlineDebug,
  50. zoomIn,
  51. zoomOut,
  52. zoomDiv,
  53. custom,
  54. nextCursorBtn,
  55. resetCursorBtn,
  56. showCursorBtn,
  57. hideCursorBtn,
  58. backendSelect;
  59. // Initialization code
  60. function init() {
  61. var name, option;
  62. err = document.getElementById("error-td");
  63. error_tr = document.getElementById("error-tr");
  64. zoomDiv = document.getElementById("zoom-str");
  65. custom = document.createElement("option");
  66. selectSample = document.getElementById("selectSample");
  67. selectBounding = document.getElementById("selectBounding");
  68. skylineDebug = document.getElementById("skylineDebug");
  69. bottomlineDebug = document.getElementById("bottomlineDebug");
  70. zoomIn = document.getElementById("zoom-in-btn");
  71. zoomOut = document.getElementById("zoom-out-btn");
  72. canvas = document.createElement("div");
  73. nextCursorBtn = document.getElementById("next-cursor-btn");
  74. resetCursorBtn = document.getElementById("reset-cursor-btn");
  75. showCursorBtn = document.getElementById("show-cursor-btn");
  76. hideCursorBtn = document.getElementById("hide-cursor-btn");
  77. backendSelect = document.getElementById("backend-select");
  78. // Hide error
  79. error();
  80. // Create select
  81. for (name in samples) {
  82. if (samples.hasOwnProperty(name)) {
  83. option = document.createElement("option");
  84. option.value = samples[name];
  85. option.textContent = name;
  86. }
  87. selectSample.appendChild(option);
  88. }
  89. selectSample.onchange = selectSampleOnChange;
  90. selectBounding.onchange = selectBoundingOnChange;
  91. // Pre-select default music piece
  92. custom.appendChild(document.createTextNode("Custom"));
  93. // Create zoom controls
  94. zoomIn.onclick = function () {
  95. zoom *= 1.2;
  96. scale();
  97. };
  98. zoomOut.onclick = function () {
  99. zoom /= 1.2;
  100. scale();
  101. };
  102. skylineDebug.onclick = function() {
  103. openSheetMusicDisplay.DrawSkyLine = !openSheetMusicDisplay.DrawSkyLine;
  104. }
  105. bottomlineDebug .onclick = function() {
  106. openSheetMusicDisplay.DrawBottomLine = !openSheetMusicDisplay.DrawBottomLine;
  107. }
  108. // Create OSMD object and canvas
  109. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, backendSelect.value);
  110. openSheetMusicDisplay.setLogLevel('info');
  111. document.body.appendChild(canvas);
  112. // Set resize event handler
  113. new Resize(
  114. function(){
  115. if (!sampleLoaded) {
  116. return;
  117. }
  118. disable();
  119. },
  120. function(){
  121. if (!sampleLoaded) {
  122. return;
  123. }
  124. var width = document.body.clientWidth;
  125. canvas.width = width;
  126. try {
  127. openSheetMusicDisplay.render();
  128. } catch (e) {
  129. console.warn(e.stack);
  130. }
  131. enable();
  132. }
  133. );
  134. window.addEventListener("keydown", function(e) {
  135. var event = window.event ? window.event : e;
  136. if (event.keyCode === 39) {
  137. openSheetMusicDisplay.cursor.next();
  138. }
  139. });
  140. nextCursorBtn.addEventListener("click", function() {
  141. openSheetMusicDisplay.cursor.next();
  142. });
  143. resetCursorBtn.addEventListener("click", function() {
  144. openSheetMusicDisplay.cursor.reset();
  145. });
  146. hideCursorBtn.addEventListener("click", function() {
  147. openSheetMusicDisplay.cursor.hide();
  148. });
  149. showCursorBtn.addEventListener("click", function() {
  150. openSheetMusicDisplay.cursor.show();
  151. });
  152. backendSelect.addEventListener("change", function(e) {
  153. var value = e.target.value;
  154. // clears the canvas element
  155. canvas.innerHTML = "";
  156. openSheetMusicDisplay = new OpenSheetMusicDisplay(canvas, false, value);
  157. openSheetMusicDisplay.setLogLevel('info');
  158. selectSampleOnChange();
  159. });
  160. }
  161. function Resize(startCallback, endCallback) {
  162. var rtime;
  163. var timeout = false;
  164. var delta = 200;
  165. function resizeEnd() {
  166. timeout = window.clearTimeout(timeout);
  167. if (new Date() - rtime < delta) {
  168. timeout = setTimeout(resizeEnd, delta);
  169. } else {
  170. endCallback();
  171. }
  172. }
  173. window.addEventListener("resize", function () {
  174. rtime = new Date();
  175. if (!timeout) {
  176. startCallback();
  177. rtime = new Date();
  178. timeout = window.setTimeout(resizeEnd, delta);
  179. }
  180. });
  181. window.setTimeout(startCallback, 0);
  182. window.setTimeout(endCallback, 1);
  183. }
  184. function selectBoundingOnChange(evt) {
  185. var value = evt.target.value;
  186. openSheetMusicDisplay.DrawBoundingBox = value;
  187. }
  188. function selectSampleOnChange(str) {
  189. error();
  190. disable();
  191. var isCustom = typeof str === "string";
  192. if (!isCustom) {
  193. str = sampleFolder + selectSample.value;
  194. }
  195. zoom = 1.0;
  196. openSheetMusicDisplay.load(str).then(
  197. function() {
  198. // This gives you access to the osmd object in the console. Do not use in productive code
  199. window.osmd = openSheetMusicDisplay;
  200. return openSheetMusicDisplay.render();
  201. },
  202. function(e) {
  203. console.warn(e.stack);
  204. error("Error reading sheet: " + e);
  205. }
  206. ).then(
  207. function() {
  208. return onLoadingEnd(isCustom);
  209. }, function(e) {
  210. console.warn(e.stack);
  211. error("Error rendering sheet: " + process.env.DEBUG ? e.stack : e);
  212. onLoadingEnd(isCustom);
  213. }
  214. );
  215. }
  216. function onLoadingEnd(isCustom) {
  217. sampleLoaded = true;
  218. // Remove option from select
  219. if (!isCustom && custom.parentElement === selectSample) {
  220. selectSample.removeChild(custom);
  221. }
  222. // Enable controls again
  223. enable();
  224. }
  225. function logCanvasSize() {
  226. zoomDiv.innerHTML = Math.floor(zoom * 100.0) + "%";
  227. }
  228. function scale() {
  229. disable();
  230. window.setTimeout(function(){
  231. openSheetMusicDisplay.zoom = zoom;
  232. openSheetMusicDisplay.render();
  233. enable();
  234. }, 0);
  235. }
  236. function error(errString) {
  237. if (!errString) {
  238. error_tr.style.display = "none";
  239. } else {
  240. err.textContent = errString;
  241. error_tr.style.display = "";
  242. canvas.width = canvas.height = 0;
  243. enable();
  244. }
  245. }
  246. // Enable/Disable Controls
  247. function disable() {
  248. document.body.style.opacity = 0.3;
  249. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "disabled";
  250. }
  251. function enable() {
  252. document.body.style.opacity = 1;
  253. selectSample.disabled = zoomIn.disabled = zoomOut.disabled = "";
  254. logCanvasSize();
  255. }
  256. // Register events: load, drag&drop
  257. window.addEventListener("load", function() {
  258. init();
  259. selectSampleOnChange();
  260. });
  261. window.addEventListener("dragenter", function(event) {
  262. event.preventDefault();
  263. disable();
  264. });
  265. window.addEventListener("dragover", function(event) {
  266. event.preventDefault();
  267. });
  268. window.addEventListener("dragleave", function(event) {
  269. enable();
  270. });
  271. window.addEventListener("drop", function(event) {
  272. event.preventDefault();
  273. if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
  274. return;
  275. }
  276. // Add "Custom..." score
  277. selectSample.appendChild(custom);
  278. custom.selected = "selected";
  279. // Read dragged file
  280. var reader = new FileReader();
  281. reader.onload = function (res) {
  282. selectSampleOnChange(res.target.result);
  283. };
  284. var filename = event.dataTransfer.files[0].name;
  285. if (filename.toLowerCase().indexOf(".xml") > 0
  286. || filename.toLowerCase().indexOf(".musicxml") > 0) {
  287. reader.readAsText(event.dataTransfer.files[0]);
  288. } else if (event.dataTransfer.files[0].name.toLowerCase().indexOf(".mxl") > 0){
  289. reader.readAsBinaryString(event.dataTransfer.files[0]);
  290. }
  291. else {
  292. alert("No vaild .xml/.mxl/.musicxml file!");
  293. }
  294. });
  295. }());