This repository was archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathControllerMidi.js
285 lines (238 loc) · 9.51 KB
/
ControllerMidi.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import midi from 'easymidi'
import debug from 'debug'
import { throttle } from 'throttle-debounce'
import { EventEmitter } from 'inf-ee'
const log = debug('sio:midi')
const CONNECTION_TIMEOUT = 5000 // Ms
const CONNECTION_RETRY_INTERVAL = 4000 // Ms
const THROTTLE_BUTTON_UPDATE = 250 // Ms
const THROTTLE_CONTROLLER_UPDATE = 250 // Ms
export const ConnectionState = {
Closed: 0x00,
Connected: 0x01,
}
export class ControllerMidi extends EventEmitter {
constructor(options = {}) {
super()
console.log('Constructing MIDI Controller')
this.componentName = 'MIDI Controller'
this.config = options.config || undefined
this.sessionId = -1
this._reconnectTimer = undefined
this._lastReceivedAt = undefined
this._inputDeviceName = undefined
this._outputDeviceName = undefined
this._outputChannel = undefined
this._input = undefined
this._output = undefined
}
startTimers() {
if (!this._reconnectTimer) {
this._reconnectTimer = setInterval(async () => {
if (this._lastReceivedAt + CONNECTION_TIMEOUT > Date.now()) {
// Console.log(`MIDI Connection Check: No need to check since we just heard form stuff`)
// We heard from the midi controller recently
return
}
if (!this.isConnected()) {
this.log('info', 'MIDI Connection Check Failed: sending for connection restart')
try {
await this.restartConnection()
} catch (error) {
this.log('info', `MIDI Reconnect failed: ${error}`)
}
}
}, CONNECTION_RETRY_INTERVAL)
}
// NOTE: not sure if I need this? Maybe for future reliability?
// https://github.com/nrkno/tv-automation-atem-connection/blob/74ddb0e2f9d609d16bd673db075bdc143f691eae/src/lib/atemSocketChild.ts#L349
// Check for retransmits every 10 milliseconds
// if (!this._retransmitTimer) {
// this._retransmitTimer = setInterval(() => this._checkForRetransmit(), 10)
// }
}
async connect(options = {}) {
this._inputDeviceName = options.inputDeviceName || 'X-TOUCH MINI'
this._outputDeviceName = options.outputDeviceName || 'X-TOUCH MINI'
this._outputChannel = options.outputChannel || 10
this.startTimers()
await this.restartConnection()
}
async disconnect() {
// Stop timers, as they just cause pointless work now.
if (this._reconnectTimer) {
clearInterval(this._reconnectTimer)
this._reconnectTimer = undefined
}
if (this._input) {
this._input.close()
}
if (this._output) {
this._output.close()
}
this._connectionState = ConnectionState.Closed
this.emit('disconnect', { sessionId: this.sessionId })
return { sessionId: this.sessionId }
}
async restartConnection() {
// This includes a 'disconnect'
if (this._connectionState === ConnectionState.Connected) {
if (this._input) {
this._input.close()
this._input = undefined
}
if (this._output) {
this._output.close()
this._output = undefined
}
this._connectionState = ConnectionState.Closed
this.emit('disconnect', { sessionId: this.sessionId })
}
this.log('warn', 'Trying to Reconnect')
// Try doing reconnect
const midiInputsByName = midi.getInputs()
const midiOutputsByName = midi.getOutputs()
if (!midiInputsByName.includes(this._inputDeviceName)) {
this.log('info', `Reconnect failed: input device '${this._inputDeviceName}' not found`)
return
}
if (!midiOutputsByName.includes(this._outputDeviceName)) {
this.log('info', `Reconnect failed: output device '${this._outputDeviceName}' not found`)
return
}
this.connectOutput()
this.connectInput()
this._connectionState = ConnectionState.Connected
this.sessionId += 1
this.emit('connect', { isReconnect: this.sessionId > 0, sessionId: this.sessionId })
}
connectOutput() {
this._output = new midi.Output(this._outputDeviceName)
this.log('info', `Connected Output: ${this._outputDeviceName}`)
}
connectInput() {
this._input = new midi.Input(this._inputDeviceName)
this.log('info', `Connected Input: ${this._inputDeviceName}`)
this._input.on('message', (message) => {
this._lastReceivedAt = Date.now()
this.log('info', `${this._inputDeviceName}:`, message)
})
this._input.on('noteoff', (parameters) => this.emit('noteoff', parameters))
this._input.on('noteoff', (parameters) => this.emit('noteOff', parameters))
this._input.on('noteon', (parameters) => this.emit('noteon', parameters))
this._input.on('noteon', (parameters) => this.emit('noteOn', parameters))
this._input.on('poly aftertouch', (parameters) => this.emit('poly aftertouch', parameters))
this._input.on('poly aftertouch', (parameters) => this.emit('polyAftertouch', parameters))
this._input.on('cc', (parameters) => this.emit('cc', parameters))
this._input.on('cc', (parameters) => this.emit('controllerChange', parameters))
this._input.on('program', (parameters) => this.emit('program', parameters))
this._input.on('channel aftertouch', (parameters) => this.emit('channel aftertouch', parameters))
this._input.on('channel aftertouch', (parameters) => this.emit('channelAftertouch', parameters))
this._input.on('pitch', (parameters) => this.emit('pitch', parameters))
this._input.on('position', (parameters) => this.emit('position', parameters))
this._input.on('select', (parameters) => this.emit('select', parameters))
this._input.on('clock', (parameters) => this.emit('clock', parameters))
this._input.on('start', (parameters) => this.emit('start', parameters))
this._input.on('continue', (parameters) => this.emit('continue', parameters))
this._input.on('stop', (parameters) => this.emit('stop', parameters))
this._input.on('reset', (parameters) => this.emit('reset', parameters))
}
isConnected() {
const midiInputsByName = midi.getInputs()
const midiOutputsByName = midi.getOutputs()
const connectionCheckFailed = false
if (typeof this._input === 'undefined') {
// This.log('info', 'Connection Check Failed: no connection established')
return false
}
if (!connectionCheckFailed && typeof this._output === 'undefined') {
// This.log('info', 'Connection Check Failed: no connection established')
return false
}
if (!connectionCheckFailed && !midiInputsByName.includes(this._inputDeviceName)) {
// This.log('info', `Connection Check Failed: input device '${this._inputDeviceName}' not found`)
return false
}
if (!connectionCheckFailed && !midiOutputsByName.includes(this._outputDeviceName)) {
// This.log('info', `Connection Check Failed: output device '${this._outputDeviceName}' not found`)
return false
}
return true
}
send(messageType, options) {
const optionsDefault = {
note: undefined,
velocity: undefined,
value: undefined,
channel: this._outputChannel,
}
options = { ...optionsDefault, ...options }
if (this.isConnected()) {
this._output.send(messageType || 'noteoff', options)
} else {
this.log('info', 'NOT CONNECTED & COMMAND NOT SENT')
}
}
sendControllerChange(options) {
this.send('cc', options)
}
updateButtonsViaStateInstant(buttonStates) {
for (const btn of buttonStates) {
let state = (btn.state || 'noteoff').toLowerCase()
if (['flashingon', 'flashingoff'].includes(state)) {
btn.state = state === 'flashingon' ? 'flashingoff' : 'flashingon'
state = state === 'flashingon' ? 'noteoff' : 'noteon'
}
this.send(state, {
note: btn.note,
velocity: btn.value || btn.defaultValue || 0,
channel: btn.channel || this.config?.midi?.outputChannel || 10,
})
}
}
updateButtonsViaState(buttonStates, instant = false) {
// This.log('debug', 'updateButtonsViaState')
if (instant) {
// This.log('debug', 'updateButtonsViaStateInstant')
this.updateButtonsViaStateInstant(buttonStates)
} else {
if (!this.updateButtonsViaStateThrottled) {
this.updateButtonsViaStateThrottled = throttle(THROTTLE_BUTTON_UPDATE, false, (buttonStates) =>
// This.log('debug', 'updateButtonsViaStateThrottled')
this.updateButtonsViaStateInstant(buttonStates),
)
}
this.updateButtonsViaStateThrottled(buttonStates)
}
}
updateControllersViaStateInstant(controllerStates) {
for (const controller of controllerStates) {
const { note, value, channel } = controller
this.sendControllerChange({
controller: note,
value,
channel: channel || this.config?.midi?.outputChannel || 10,
})
}
}
updateControllersViaState(controllerStates, instant = false) {
// This.log('debug', 'updateControllersViaState')
if (instant) {
// This.log('debug', 'updateControllersViaStateInstant')
this.updateControllersViaStateInstant(controllerStates)
} else {
if (!this.updateControllersViaStateThrottled) {
this.updateControllersViaStateThrottled = throttle(THROTTLE_CONTROLLER_UPDATE, false, (controllerStates) =>
// This.log('debug', 'updateControllersViaStateThrottled')
this.updateControllersViaStateInstant(controllerStates),
)
}
this.updateControllersViaStateThrottled(controllerStates)
}
}
log(level, ...args) {
this.emit('log', { component: this.componentName, level: level.toLowerCase(), message: args })
log(`${level.toLowerCase()}: ${args}`)
}
}
export default ControllerMidi