OSMD.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {IXmlElement} from "./../Common/FileIO/Xml";
  2. import {VexFlowMusicSheetCalculator} from "./../MusicalScore/Graphical/VexFlow/VexFlowMusicSheetCalculator";
  3. import {MusicSheetReader} from "./../MusicalScore/ScoreIO/MusicSheetReader";
  4. import {GraphicalMusicSheet} from "./../MusicalScore/Graphical/GraphicalMusicSheet";
  5. import {MusicSheetCalculator} from "./../MusicalScore/Graphical/MusicSheetCalculator";
  6. import {VexFlowMusicSheetDrawer} from "./../MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer";
  7. import {MusicSheet} from "./../MusicalScore/MusicSheet";
  8. import {Cursor} from "./Cursor";
  9. import {openMxl} from "../Common/FileIO/Mxl";
  10. import {Promise} from "es6-promise";
  11. export class OSMD {
  12. /**
  13. * The easy way of displaying a MusicXML sheet music file
  14. * @param container is either the id, or the actual "div" element which will host the music sheet
  15. */
  16. constructor(container: string|HTMLElement) {
  17. // Store container element
  18. if (typeof container === "string") {
  19. this.container = document.getElementById(<string>container);
  20. } else if (container && "appendChild" in <any>container) {
  21. this.container = <HTMLElement>container;
  22. }
  23. if (!this.container) {
  24. throw new Error("Please pass a valid div container to OSMD");
  25. }
  26. // Create the elements inside the container
  27. this.heading = document.createElement("div");
  28. this.canvas = document.createElement("canvas");
  29. this.canvas.style.zIndex = "0";
  30. let inner: HTMLElement = document.createElement("div");
  31. inner.style.position = "relative";
  32. this.container.appendChild(this.heading);
  33. inner.appendChild(this.canvas);
  34. this.container.appendChild(inner);
  35. // Create the drawer
  36. this.drawer = new VexFlowMusicSheetDrawer(this.heading, this.canvas);
  37. // Create the cursor
  38. this.cursor = new Cursor(inner, this);
  39. }
  40. public cursor: Cursor;
  41. public zoom: number = 1.0;
  42. private container: HTMLElement;
  43. private heading: HTMLElement;
  44. private canvas: HTMLCanvasElement;
  45. private sheet: MusicSheet;
  46. private drawer: VexFlowMusicSheetDrawer;
  47. private graphic: GraphicalMusicSheet;
  48. /**
  49. * Load a MusicXML file
  50. * @param content is either the url of a file, or the root node of a MusicXML document, or the string content of a .xml/.mxl file
  51. */
  52. public load(content: string|Document): Promise<void> {
  53. this.reset();
  54. let path: string = "Unknown path";
  55. if (typeof content === "string") {
  56. let str: string = <string>content;
  57. if (str.substr(0, 4) === "http") {
  58. path = str;
  59. str = this.openURL(path);
  60. }
  61. if (str.substr(0, 4) === "\x04\x03\x4b\x50") {
  62. // This is a zip file, unpack it first
  63. return openMxl(str).then(
  64. this.load,
  65. (err: any) => {
  66. throw new Error("extractSheetFromMxl: " + err.message);
  67. }
  68. );
  69. }
  70. if (str.substr(0, 5) === "<?xml") {
  71. // Parse the string representing an xml file
  72. let parser: DOMParser = new DOMParser();
  73. content = parser.parseFromString(str, "text/xml");
  74. }
  75. }
  76. if (!content || !("nodeName" in <any>content)) {
  77. throw new Error("Could not ");
  78. }
  79. let elem: Element = (<Document>content).getElementsByTagName("score-partwise")[0];
  80. if (elem === undefined) {
  81. throw new Error("Invalid partwise MusicXML document");
  82. }
  83. let score: IXmlElement = new IXmlElement(elem);
  84. let calc: MusicSheetCalculator = new VexFlowMusicSheetCalculator();
  85. let reader: MusicSheetReader = new MusicSheetReader();
  86. this.sheet = reader.createMusicSheet(score, path);
  87. this.graphic = new GraphicalMusicSheet(this.sheet, calc);
  88. this.cursor.init(this.sheet.MusicPartManager, this.graphic);
  89. return Promise.resolve();
  90. }
  91. /**
  92. * Render the music sheet in the container
  93. */
  94. public render(): void {
  95. this.resetHeadings();
  96. if (!this.graphic) {
  97. throw new Error("OSMD: Before rendering a music sheet, please load a MusicXML file");
  98. }
  99. let width: number = this.container.offsetWidth;
  100. if (isNaN(width)) {
  101. throw new Error("OSMD: Before rendering a music sheet, please give the container a width");
  102. }
  103. // Set page width
  104. this.sheet.pageWidth = width / this.zoom / 10.0;
  105. // Calculate again
  106. this.graphic.reCalculate();
  107. // Update Sheet Page
  108. let height: number = this.graphic.MusicPages[0].PositionAndShape.BorderBottom * 10.0 * this.zoom;
  109. this.drawer.resize(width, height);
  110. this.drawer.scale(this.zoom);
  111. // Finally, draw
  112. this.drawer.drawSheet(this.graphic);
  113. // Update the cursor position
  114. this.cursor.update();
  115. }
  116. private openURL(url: string): string {
  117. throw new Error("OSMD: Not implemented: Load sheet from URL");
  118. }
  119. private resetHeadings(): void {
  120. // Empty this.headings
  121. while (this.heading.firstChild) {
  122. this.heading.removeChild(this.heading.firstChild);
  123. }
  124. }
  125. /**
  126. * Initialize this object to default values
  127. */
  128. private reset(): void {
  129. this.sheet = undefined;
  130. this.graphic = undefined;
  131. this.zoom = 1.0;
  132. this.resetHeadings();
  133. }
  134. }