-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
71 lines (65 loc) · 2.21 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { OpenSheetMusicDisplay, Cursor, VoiceEntry, Note, StemDirectionType } from "opensheetmusicdisplay";
let osmd: OpenSheetMusicDisplay;
/*
* Create a container element for OpenSheetMusicDisplay...
*/
let container: HTMLElement = <HTMLElement>document.createElement("div");
/*
* ... and attach it to our HTML document's body. The document itself is a HTML5
* stub created by Webpack, so you won't find any actual .html sources.
*/
document.body.appendChild(container);
/*
* Create a new instance of OpenSheetMusicDisplay and tell it to draw inside
* the container we've created in the steps before.
* The second parameter is an IOSMDOptions object.
* autoResize tells OSMD not to redraw on resize.
*/
osmd = new OpenSheetMusicDisplay(container, {autoResize: false});
osmd.setLogLevel('info');
/*
* Load our MusicXMl and display it. The file is renamed by Webpack during bundling, it's
* Muzio Clementi's Sonatina Opus 36 No 1, Part 1, which you can find next to this file.
*/
loadMusicXML("musicXmlSample.xml");
/** Some example code to use OSMD classes after rendering a score. */
function afterRender() {
let cursor: Cursor = osmd.cursor;
cursor.show();
cursor.next();
const cursorVoiceEntry: VoiceEntry = cursor.Iterator.CurrentVoiceEntries[0];
const baseNote: Note = cursorVoiceEntry.Notes[0];
console.log("Stem direction of VoiceEntry under Cursor: " + StemDirectionType[cursorVoiceEntry.StemDirection]);
console.log("base note of Voice Entry at second cursor position: " + baseNote.Pitch.ToString());
osmd.setOptions( { autoResize: true });
}
/**
* Load a MusicXml file via xhttp request, and display its contents.
*/
function loadMusicXML(url: string) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
switch (xhttp.readyState) {
case 0 : // UNINITIALIZED
case 1 : // LOADING
case 2 : // LOADED
case 3 : // INTERACTIVE
break;
case 4 : // COMPLETED
osmd
.load(xhttp.responseXML)
.then(
() => {
osmd.render();
afterRender();
},
(err) => console.log(err)
);
break;
default:
throw("Error loading MusicXML file.");
}
}
xhttp.open("GET", url, true);
xhttp.send();
}