This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoteConstructor.js
60 lines (60 loc) · 2.21 KB
/
NoteConstructor.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Note_V2 {
constructor(args) {
this.file = args.path;
this.numType = args.numType;
this.stem = args.stem;
this.gearType = args.gearType || "-";
this.type = args.type || "Unidentified";
this.flat = args.flat || false;
this.sharp = args.sharp || false;
if (args.audioCtx == null)
throw new Error(`Missing context for ${args.path}`);
this.audioCtx = args.audioCtx;
if (this.type != "repeat_begin" && this.type != "repeat_end" && this.type != "blank") {
this.source = this.audioCtx.createBufferSource();
const request = new XMLHttpRequest();
request.open("GET", "./notes/" + this.file);
request.responseType = "arraybuffer";
request.onload = () => {
const audioData = request.response;
audioCtx.decodeAudioData(
audioData,
(buffer) => {
this.source.buffer = buffer;
}, (err) => console.error(err.err),
);
window.dispatchEvent(new CustomEvent("loadedAsset", {
detail: {
file: "./notes/" + this.file
}
}));
}
request.send();
}
this.key = args.key || "?";
}
play(volume = 0.33) {
if (this.type != "repeat_begin" && this.type != "repeat_end" && this.type != "blank") {
var x = this.audioCtx.createBufferSource();
x.buffer = this.source.buffer;
if (volume != 0.33) {
var note_gain = audioCtx.createGain();
note_gain.gain.value = volume;
note_gain.connect(audioCtx.destination);
x.connect(note_gain);
x.addEventListener("ended", () => {note_gain.disconnect(audioCtx.destination);});
} else x.connect(gain);
x.start();
}
}
toString() {
let t = this.gearType;
let k = this.key;
let n = "-";
if (this.type.includes("flat"))
n = "b";
else if (this.type.includes("sharp"))
n = "#";
return t + k + n;
}
}