|
@@ -5,106 +5,134 @@ import {GraphicalMusicSheet} from "./MusicalScore/Graphical/GraphicalMusicSheet"
|
|
import {MusicSheetCalculator} from "./MusicalScore/Graphical/MusicSheetCalculator";
|
|
import {MusicSheetCalculator} from "./MusicalScore/Graphical/MusicSheetCalculator";
|
|
import {VexFlowMusicSheetDrawer} from "./MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer";
|
|
import {VexFlowMusicSheetDrawer} from "./MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer";
|
|
import {MusicSheet} from "./MusicalScore/MusicSheet";
|
|
import {MusicSheet} from "./MusicalScore/MusicSheet";
|
|
-import {VexFlowTextMeasurer} from "./MusicalScore/Graphical/VexFlow/VexFlowTextMeasurer";
|
|
|
|
|
|
+import {Fraction} from "./Common/DataObjects/fraction";
|
|
|
|
+import {OutlineAndFillStyleEnum} from "./MusicalScore/Graphical/DrawingEnums";
|
|
|
|
|
|
export class MusicSheetAPI {
|
|
export class MusicSheetAPI {
|
|
- constructor() {
|
|
|
|
- this.free();
|
|
|
|
|
|
+ constructor(container: HTMLElement) {
|
|
|
|
+ this.container = container;
|
|
|
|
+ this.titles = document.createElement("div");
|
|
|
|
+ this.canvas = document.createElement("canvas");
|
|
|
|
+ this.container.appendChild(this.titles);
|
|
|
|
+ this.container.appendChild(this.canvas);
|
|
|
|
+ this.drawer = new VexFlowMusicSheetDrawer(this.titles, this.canvas);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private container: HTMLElement;
|
|
|
|
+ private titles: HTMLElement;
|
|
private canvas: HTMLCanvasElement;
|
|
private canvas: HTMLCanvasElement;
|
|
private sheet: MusicSheet;
|
|
private sheet: MusicSheet;
|
|
private drawer: VexFlowMusicSheetDrawer;
|
|
private drawer: VexFlowMusicSheetDrawer;
|
|
private graphic: GraphicalMusicSheet;
|
|
private graphic: GraphicalMusicSheet;
|
|
- private width: number;
|
|
|
|
private zoom: number = 1.0;
|
|
private zoom: number = 1.0;
|
|
private unit: number = 10.0;
|
|
private unit: number = 10.0;
|
|
|
|
|
|
- /**
|
|
|
|
- * Initialize this object to default values
|
|
|
|
- */
|
|
|
|
- public free(): void {
|
|
|
|
- this.width = undefined;
|
|
|
|
- this.canvas = undefined;
|
|
|
|
- this.sheet = undefined;
|
|
|
|
- this.drawer = undefined;
|
|
|
|
- this.graphic = undefined;
|
|
|
|
- this.zoom = 1.0;
|
|
|
|
- this.unit = 10.0;
|
|
|
|
- }
|
|
|
|
|
|
+ private fraction: Fraction = new Fraction(0, 4);
|
|
|
|
|
|
/**
|
|
/**
|
|
* Load a MusicXML file
|
|
* Load a MusicXML file
|
|
* @param doc is the root node of a MusicXML document
|
|
* @param doc is the root node of a MusicXML document
|
|
*/
|
|
*/
|
|
- public load(doc: Document): void {
|
|
|
|
- let elem: Element = doc.getElementsByTagName("score-partwise")[0];
|
|
|
|
- if (elem === undefined) {
|
|
|
|
- throw new Error("Invalid partwise MusicXML document");
|
|
|
|
|
|
+ public load(content: string|Document): void {
|
|
|
|
+ this.reset();
|
|
|
|
+ let elem: Element;
|
|
|
|
+ let path: string = "Unknown path";
|
|
|
|
+ if (typeof content === "string") {
|
|
|
|
+ if ((<string>content).substr(0, 4) === "http") {
|
|
|
|
+ path = <string>content;
|
|
|
|
+ content = this.loadURL(path);
|
|
|
|
+ }
|
|
|
|
+ //if (<string>content.substr() === "")
|
|
|
|
+ }
|
|
|
|
+ if ("nodeName" in <any>content) {
|
|
|
|
+ elem = (<Document>content).getElementsByTagName("score-partwise")[0];
|
|
|
|
+ if (elem === undefined) {
|
|
|
|
+ throw new Error("Invalid partwise MusicXML document");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
let score: IXmlElement = new IXmlElement(elem);
|
|
let score: IXmlElement = new IXmlElement(elem);
|
|
let calc: MusicSheetCalculator = new VexFlowMusicSheetCalculator();
|
|
let calc: MusicSheetCalculator = new VexFlowMusicSheetCalculator();
|
|
let reader: MusicSheetReader = new MusicSheetReader();
|
|
let reader: MusicSheetReader = new MusicSheetReader();
|
|
- this.sheet = reader.createMusicSheet(score, "*** unknown path ***");
|
|
|
|
|
|
+ this.sheet = reader.createMusicSheet(score, path);
|
|
this.graphic = new GraphicalMusicSheet(this.sheet, calc);
|
|
this.graphic = new GraphicalMusicSheet(this.sheet, calc);
|
|
- this.display();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Set the drawing canvas
|
|
|
|
- * @param canvas
|
|
|
|
|
|
+ * Set the zoom
|
|
|
|
+ * @param factor is the zooming factor
|
|
*/
|
|
*/
|
|
- public setCanvas(canvas: HTMLCanvasElement): void {
|
|
|
|
- this.canvas = canvas;
|
|
|
|
- this.drawer = new VexFlowMusicSheetDrawer(canvas, new VexFlowTextMeasurer());
|
|
|
|
|
|
+ public scale(factor: number): void {
|
|
|
|
+ this.zoom = factor;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Set the canvas width
|
|
|
|
- * @param width
|
|
|
|
|
|
+ * Render the music sheet in the container
|
|
*/
|
|
*/
|
|
- public setWidth(width: number): void {
|
|
|
|
- if (width === this.width) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- this.width = width;
|
|
|
|
- this.display();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Set the zoom
|
|
|
|
- * @param k
|
|
|
|
- */
|
|
|
|
- public scale(k: number): void {
|
|
|
|
- this.zoom = k;
|
|
|
|
- this.display();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // FIXME: make the following private!
|
|
|
|
- public display(): void {
|
|
|
|
- if (this.width === undefined) {
|
|
|
|
- return;
|
|
|
|
|
|
+ public render(): void {
|
|
|
|
+ this.resetTitle();
|
|
|
|
+ if (!this.graphic) {
|
|
|
|
+ throw new Error("OSMD: Before rendering a music sheet, please load a MusicXML file");
|
|
}
|
|
}
|
|
- if (this.canvas === undefined) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- if (this.sheet === undefined) {
|
|
|
|
- return;
|
|
|
|
|
|
+ let width: number = this.container.offsetWidth;
|
|
|
|
+ if (isNaN(width)) {
|
|
|
|
+ throw new Error("OSMD: Before rendering a music sheet, please set the width of the container");
|
|
}
|
|
}
|
|
// Set page width
|
|
// Set page width
|
|
- this.sheet.pageWidth = this.width / this.zoom / this.unit;
|
|
|
|
|
|
+ this.sheet.pageWidth = width / this.zoom / this.unit;
|
|
// Calculate again
|
|
// Calculate again
|
|
this.graphic.reCalculate();
|
|
this.graphic.reCalculate();
|
|
// Update Sheet Page
|
|
// Update Sheet Page
|
|
let height: number = this.graphic.MusicPages[0].PositionAndShape.BorderBottom * this.unit * this.zoom;
|
|
let height: number = this.graphic.MusicPages[0].PositionAndShape.BorderBottom * this.unit * this.zoom;
|
|
- this.drawer.resize(
|
|
|
|
- this.width,
|
|
|
|
- height
|
|
|
|
- );
|
|
|
|
|
|
+ this.drawer.resize(width, height);
|
|
// Fix the label problem
|
|
// Fix the label problem
|
|
// this.drawer.translate(0, 100);
|
|
// this.drawer.translate(0, 100);
|
|
this.drawer.scale(this.zoom);
|
|
this.drawer.scale(this.zoom);
|
|
// Finally, draw
|
|
// Finally, draw
|
|
this.drawer.drawSheet(this.graphic);
|
|
this.drawer.drawSheet(this.graphic);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public next(): void {
|
|
|
|
+ //calculateCursorLineAtTimestamp
|
|
|
|
+ //let iterator: MusicPartManagerIterator = this.sheet.MusicPartManager.getIterator();
|
|
|
|
+ //while (!iterator.EndReached && iterator.CurrentVoiceEntries !== undefined) {
|
|
|
|
+ // for (let idx: number = 0, len: number = iterator.CurrentVoiceEntries.length; idx < len; ++idx) {
|
|
|
|
+ // let voiceEntry: VoiceEntry = iterator.CurrentVoiceEntries[idx];
|
|
|
|
+ // for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
|
|
|
|
+ // let note: Note = voiceEntry.Notes[idx2];
|
|
|
|
+ // note.state = NoteState.Normal;
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ // iterator.moveToNext();
|
|
|
|
+ //}
|
|
|
|
+ this.graphic.Cursors.length = 0;
|
|
|
|
+ this.graphic.Cursors.push(this.graphic.calculateCursorLineAtTimestamp(this.fraction, OutlineAndFillStyleEnum.PlaybackCursor));
|
|
|
|
+ this.fraction.Add(new Fraction(1, 8));
|
|
|
|
+ this.render();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private loadURL(url: string): Document {
|
|
|
|
+ return undefined;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //private loadMXL(content: string): Document {
|
|
|
|
+ // return undefined;
|
|
|
|
+ //}
|
|
|
|
+
|
|
|
|
+ private resetTitle(): void {
|
|
|
|
+ // Empty this.titles
|
|
|
|
+ while (this.titles.firstChild) {
|
|
|
|
+ this.titles.removeChild(this.titles.firstChild);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Initialize this object to default values
|
|
|
|
+ */
|
|
|
|
+ private reset(): void {
|
|
|
|
+ this.sheet = undefined;
|
|
|
|
+ this.graphic = undefined;
|
|
|
|
+ this.zoom = 1.0;
|
|
|
|
+ this.unit = 10.0;
|
|
|
|
+ this.resetTitle();
|
|
|
|
+ }
|
|
}
|
|
}
|