MusicSheetAPI.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 {VexFlowTextMeasurer} from "./MusicalScore/Graphical/VexFlow/VexFlowTextMeasurer";
  9. export class MusicSheetAPI {
  10. constructor() {
  11. this.free();
  12. }
  13. private canvas: HTMLCanvasElement;
  14. private sheet: MusicSheet;
  15. private drawer: VexFlowMusicSheetDrawer;
  16. private graphic: GraphicalMusicSheet;
  17. private width: number;
  18. private zoom: number = 1.0;
  19. private unit: number = 10.0;
  20. /**
  21. * Initialize this object to default values
  22. */
  23. public free(): void {
  24. this.width = undefined;
  25. this.canvas = undefined;
  26. this.sheet = undefined;
  27. this.drawer = undefined;
  28. this.graphic = undefined;
  29. this.zoom = 1.0;
  30. this.unit = 10.0;
  31. }
  32. /**
  33. * Load a MusicXML file
  34. * @param doc is the root node of a MusicXML document
  35. */
  36. public load(doc: Document): void {
  37. let elem: Element = doc.getElementsByTagName("score-partwise")[0];
  38. if (elem === undefined) {
  39. throw new Error("Invalid partwise MusicXML document");
  40. }
  41. let score: IXmlElement = new IXmlElement(elem);
  42. let calc: MusicSheetCalculator = new VexFlowMusicSheetCalculator();
  43. let reader: MusicSheetReader = new MusicSheetReader();
  44. this.sheet = reader.createMusicSheet(score, "*** unknown path ***");
  45. this.graphic = new GraphicalMusicSheet(this.sheet, calc);
  46. this.display();
  47. }
  48. /**
  49. * Set the drawing canvas
  50. * @param canvas
  51. */
  52. public setCanvas(canvas: HTMLCanvasElement): void {
  53. this.canvas = canvas;
  54. this.drawer = new VexFlowMusicSheetDrawer(canvas, new VexFlowTextMeasurer());
  55. }
  56. /**
  57. * Set the canvas width
  58. * @param width
  59. */
  60. public setWidth(width: number): void {
  61. if (width === this.width) {
  62. return;
  63. }
  64. this.width = width;
  65. this.display();
  66. }
  67. /**
  68. * Set the zoom
  69. * @param k
  70. */
  71. public scale(k: number): void {
  72. this.zoom = k;
  73. this.display();
  74. }
  75. // FIXME: make the following private!
  76. public display(): void {
  77. if (this.width === undefined) {
  78. return;
  79. }
  80. if (this.canvas === undefined) {
  81. return;
  82. }
  83. if (this.sheet === undefined) {
  84. return;
  85. }
  86. // Set page width
  87. this.sheet.pageWidth = this.width / this.zoom / this.unit;
  88. // Calculate again
  89. this.graphic.reCalculate();
  90. // Update Sheet Page
  91. let height: number = this.graphic.MusicPages[0].PositionAndShape.BorderBottom * this.unit * this.zoom;
  92. this.drawer.resize(
  93. this.width,
  94. height
  95. );
  96. // Fix the label problem
  97. // this.drawer.translate(0, 100);
  98. this.drawer.scale(this.zoom);
  99. // Finally, draw
  100. this.drawer.drawSheet(this.graphic);
  101. }
  102. }