-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive.js
62 lines (51 loc) · 1.74 KB
/
receive.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
const term = require('terminal-kit').terminal;
const midi = require('midi');
const WebSocket = require('ws');
const { VIRTUAL_MIDI_DEVICE_NAME, PORT } = require('./constants.js');
let ip;
module.exports = () => {
term.clear();
term.cyan(`please pick a midi device to send received MIDI to\n`);
// Set up a new input.
const output = new midi.Output();
// Count the available output ports.
const numPorts = output.getPortCount();
// Get the name of a specified input port.
const deviceNames = [];
for(let i = 0; i < numPorts; i++) {
const name = output.getPortName(i);
if (name !== VIRTUAL_MIDI_DEVICE_NAME) {
deviceNames.push(name);
}
}
term.singleColumnMenu([`Virtual MIDI device (${VIRTUAL_MIDI_DEVICE_NAME})`, ...deviceNames], {
cancelable: true,
}, (error, response) => {
term.clear();
// term('\n').eraseLineAfter.green(
// "#%s selected: %s (%s,%s)\n",
// response.selectedIndex,
// response.selectedText,
// response.x,
// response.y
// );
if (response.selectedIndex > 0) {
output.openPort(response.selectedIndex - 1);
} else {
output.openVirtualPort(VIRTUAL_MIDI_DEVICE_NAME);
term.black.bgCyan(`virtual midi device available as ${VIRTUAL_MIDI_DEVICE_NAME}\n`);
}
const wss = new WebSocket.Server({ port: PORT });
wss.on('listening', function listening() {
term.black.bgGreen(`websocket server listening on ${PORT}\n`);
});
wss.on('connection', function connection(ws) {
console.log('new connection');
ws.on('message', function incoming(wsMessage) {
const message = JSON.parse(wsMessage)
console.log('received:', message);
output.sendMessage(message);
});
});
});
};