-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidi.js
64 lines (59 loc) · 1.87 KB
/
midi.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
61
62
63
64
import Synthesizer from './synthesizer.js'
export default class Midi {
constructor() {
navigator.requestMIDIAccess().then(this.midi)
self = this
this.synthesizer = Synthesizer.getInstance()
}
midi(response) {
for (let inputPort of response.inputs.values()) {
console.log(
'input ports:',
inputPort.type,
inputPort.name,
inputPort.state
)
connect(inputPort)
}
response.onstatechange = midiOnStateChange
function midiOnStateChange(event) {
if (
event.port.type == 'input' &&
event.port.state == 'connected' &&
!event.port.onmidimessage
) {
connect(event.port)
}
}
function connect(port) {
port.onmidimessage = midiMessage
}
let midiStatusByte, midiEvent, midiChannel, midiKey, midiVelocity
function midiMessage(event) {
midiStatusByte = event.data[0].toString(16)
midiEvent = midiStatusByte.substring(0, 1)
midiChannel = midiStatusByte.substring(1)
midiKey = event.data[1]
midiVelocity = event.data[2]
console.log(
event.currentTarget.name,
'-',
'midiEvent:',
midiEvent,
' midiChannel:',
midiChannel,
' midiKey:',
midiKey,
'midiVelocity',
midiVelocity
)
if (midiEvent == '9') {
//console.log('9')
self.synthesizer.noteOn(midiKey, midiChannel, midiVelocity)
} else {
//console.log('8')
self.synthesizer.noteOff(midiKey, midiChannel)
}
}
}
}