-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.js
107 lines (86 loc) · 2.93 KB
/
send.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
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 = () => {
// Set up a new input.
const input = new midi.Input();
// Count the available input ports.
const numInputPorts = input.getPortCount();
// Get the name of a specified input port.
const inputDeviceNames = [];
for(let i = 0; i < numInputPorts; i++) {
const name = input.getPortName(i);
if (name !== VIRTUAL_MIDI_DEVICE_NAME) {
inputDeviceNames.push(name);
}
}
// Set up a new ouput.
const output = new midi.Output();
// Count the available ouput ports.
const numOutputPorts = output.getPortCount();
// Get the name of a specified input port.
const outputDeviceNames = [];
for(let i = 0; i < numOutputPorts; i++) {
const name = output.getPortName(i);
if (name !== VIRTUAL_MIDI_DEVICE_NAME) {
outputDeviceNames.push(name);
}
}
term.cyan(`please enter the IP of the recipient\n`);
term.inputField((error, response) => {
term.clear();
ip = response;
term.cyan(`opening websocket connection to ${ip}:${PORT}\n`);
const ws = new WebSocket(`ws://${ip}:${PORT}`);
ws.on('open', () => {
term.clear();
term.cyan(`opened websocket connection to ${ip}:${PORT}\n\n`);
term.cyan(`please pick a midi input device to send to ${ip}:${PORT}\n`);
term.singleColumnMenu(inputDeviceNames, {
cancelable: true,
}, (error, response) => {
term.clear();
// term('\n').eraseLineAfter.green(
// "#%s selected: %s (%s,%s)\n",
// response.selectedIndex,
// response.selectedText,
// response.x,
// response.y
// );
const midiInputDeviceId = response.selectedIndex;
term.cyan(`opened websocket connection to ${ip}:${PORT}\n\n`);
term.cyan(`please pick a midi output device to route locally`);
term.singleColumnMenu(outputDeviceNames, {
cancelable: true,
}, (error, response) => {
term.clear();
// term('\n').eraseLineAfter.green(
// "#%s selected: %s (%s,%s)\n",
// response.selectedIndex,
// response.selectedText,
// response.x,
// response.y
// );
input.openPort(midiInputDeviceId);
output.openPort(response.selectedIndex);
// Configure a callback.
input.on('message', (deltaTime, message) => {
console.log(message);
ws.send(JSON.stringify(message));
output.sendMessage(message);
});
});
});
});
ws.on('close', () => {
term.clear();
term.black.bgYellow(`websocket connection to server closed`);
});
ws.on('error', () => {
term.clear();
term.red.bgYellow(`websocket connection to server errored`);
});
});
}