-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
41 lines (29 loc) · 982 Bytes
/
main.js
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
const ques = {};
const piano = new Instruments.Instrument.Piano();
window.addEventListener("keydown", event => {
const noteInfo = Instruments.defaultMap[event.code];
if (!noteInfo) return;
const note = piano.createNote(noteInfo[0], 5 + noteInfo[1]);
event.preventDefault();
if (!(note.toString() in ques)) {
piano.play(note).then(noteIds => ques[note.toString()] = noteIds);
}
});
window.addEventListener("keyup", event => {
const noteInfo = Instruments.defaultMap[event.code];
if (!noteInfo) return;
const note = piano.createNote(noteInfo[0], 5 + noteInfo[1]);
event.preventDefault();
if (note.toString() in ques) {
const noteIds = ques[note.toString()];
for (const noteId of noteIds) {
piano.commander.requestCommand("Note.stop", [ noteId, 0 ],
noteInfo => noteInfo.noteId === noteId && noteInfo.duration === 0
).then(noteInfo => {
piano.stop(noteInfo.noteId);
delete ques[note.toString()];
});
}
}
});
/* global Instruments */