-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnote.js
42 lines (35 loc) · 1.39 KB
/
note.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
// struct of note data, non-specific to whether it's a note on or off
class Note {
constructor(pitch = 60, velocity = 96, channel = 0) {
// clamp midi values
pitch = Math.floor(Math.max(0, Math.min(pitch, 127)));
velocity = Math.floor(Math.max(0, Math.min(velocity, 127)));
channel = Math.floor(Math.max(0, Math.min(channel, 15)));
this.pitch = pitch;
this.velocity = velocity;
this.channel = channel;
}
// --------------------------------------------------------------- private
// converts a midi note object {pitch, velocity, channel} to either
// a note on or off (as specified in argument) array of midi bytes
toMidiBytesArray(isNoteOn) {
// create status byte
let statusByte = (isNoteOn ? 0b10010000 : 0b10000000) | this.channel;
// and return the midi bytes array
return [statusByte, this.pitch, this.velocity];
}
// ---------------------------------------------------------------- public
toNoteOnMidiBytesArray() {
return this.toMidiBytesArray(true);
}
toNoteOffMidiBytesArray() {
return this.toMidiBytesArray(false);
}
equals(other) {
return (this.pitch === other.pitch) && (this.channel == other.channel);
}
// copy this note to a new object
copySelf() {
return new Note(this.pitch, this.velocity, this.channel);
}
}