forked from michaelpalumbo/allhands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_human.js
318 lines (259 loc) · 9.16 KB
/
app_human.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
let wss;
let bpm = 60
let secondsPerBeat = 60 / bpm * 1000.
let ppqn = secondsPerBeat / 24
let t = ppqn + 'm'
let beatsPerBar = 4
let beat = 1
let count = 0
console.log(t, ppqn * 24)
console.log(bpm, secondsPerBeat, ppqn)
var NanoTimer = require('nanotimer');
function clock(){
var timer = new NanoTimer();
timer.setInterval(timingClock, '', t);
}
function timingClock(){
//for(i = 0; i < 24; i++){
// console.log(11111000);
let msg = JSON.stringify({
cmd: 'MIDI_Timing',
data: 11111000
})
send(msg)
if (count === 23){
count = 0
} if (count === 0){
let msg = JSON.stringify({
cmd: 'beat',
data: beat
})
send(msg)
beat++
if(beat > beatsPerBar){
beat = 1
}
}
count++
// }
}
function send(msg){
wss.clients.forEach(function each(client) {
// if (client == ignore) return;
try {
client.send(msg);
} catch (e) {
console.error(e);
};
});
}
// this script runs at either side
// it's function as either a server or client is determined by a cli arg
console.log(process.argv[2] + ' mode')
const mode = process.argv[2]
if (mode === 'client' && !process.argv[3]){
console.log('error: client mode requires a 2nd argument to specify server IP address\nexample:\n\nnpm start client localhost')
process.exit()
}
// ***** Local UDP Send & Receive Config ******* //
let localReceivePort;
let localSendPort;
if(mode === 'server'){
localReceivePort = 7402
localSendPort = 7401
} else if (mode === 'client'){
localReceivePort = 7404
localSendPort = 7403
}
// ***** Local UDP-Sender ******* //
// this is used by either mode!
function init(){
}
// both modes require these libs
const { Client, Server } = require('node-osc');
const WebSocket = require('ws');
let ws; // keep this here
if (mode === "server"){
// run the serverconst WebSocket = require('ws');
const app = require('express')()
const http = require('http').createServer(app);;
// app.get('/', function(req, res) {
// res.sendFile('index.html');
// })
app.get('/', function(req, res){
res.sendfile('index.html', { root: __dirname } );
});
let listenPort = (process.env.PORT || 8081)
wss = new WebSocket.Server({ 'server': http, clientTracking: true });
http.listen(listenPort, function(){
})
// start the midi clock
clock();
wss.on('connection', function connection(ws, req, client) {
console.log('new connection established ')
const localSend = new Client('127.0.0.1', localSendPort);
console.log('Configure your local pd patch(es) to listen on UDP Port ' + localSendPort)
const localReceive = new Server(localReceivePort, '0.0.0.0');
// once running, inform user
localReceive.on('listening', () => {
console.log('Configure your local pd patch(es) to send on UDP Port ' + localReceivePort)
})
// handle message:
localReceive.on('message', (msg) => {
// validate correct OSC address pattern syntax
// console.log(msg)
if(msg[0].charAt(0) === '/'){
// get the address pattern
ap = msg[0]
// trim the address pattern
msg.shift()
// construct object to send over websocket
message = {
// cmd allows us to send other types of messages, ask Michael for more info if curious!
cmd: 'OSC',
date: new Date().toUTCString(),
addressPattern: ap,
// this is the data!
typeTagString: msg
}
// inform user
// console.log('sending to remote:\n', message)
// package data for the web, send it!
// if(ws){
ws.send(JSON.stringify(message))
// }
} else {
// if incoming OSC message does not have an address pattern, refuse to handle it
console.log('error, OSC Message must lead with an addressPattern\n\ni.e. /bioData')
}
});
ws.on('message', function incoming(message) {
msg = JSON.parse(message)
// console.log(msg)
switch (msg.cmd){
// in case you want to receive other data and route it elsewhere
case 'OSC':
localSend.send(msg.addressPattern, msg.typeTagString, (err) => {
if (err) console.error(err);
});
break;
default:
console.log('client sent message with unknown cmd: ' + msg)
// ws.send('server received message but did not understand: ' + msg)
break;
}
});
ws.on('close', function(code, reason) {
localSend.close();
localReceive.close();
})
});
// we can use this if we want to send to multiple clients!
function broadcast(msg){
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(msg);
}
});
}
} else if (mode === 'client'){
const localSend = new Client('127.0.0.1', localSendPort);
console.log('Configure your local pd patch(es) to listen on UDP Port ' + localSendPort)
// run the app in client mode
// ***** Websocket ******* //
// WebSocket that will automatically attempt to reconnect if the connection is closed, or if the remote server goes down
const ReconnectingWebSocket = require('reconnecting-websocket');
const serverIP = process.argv[3]
const serverPort = '8081';
const serverWSAddress = `ws://${serverIP}:${serverPort}`;
// options for the reconnecting websocket
const rwsOptions = {
// make rws use the webSocket module implementation
WebSocket: WebSocket,
// ms to try reconnecting:
connectionTimeout: 1000,
//debug:true,
}
// create a websocket
// console.log(`attempting to connect to server at ${serverWSAddress}`)
ws = new ReconnectingWebSocket(serverWSAddress, [], rwsOptions);
// if the server responds with an error
ws.addEventListener('error', () => {
console.log(`connection error: ${serverIP}`);
});
// on successful connection to server:
ws.addEventListener('open', () => {
console.log (`connected to server at ${serverWSAddress}`)
});
// on close:
ws.addEventListener('close', () => {
console.log("server connection closed");
localSend.close();
localReceive.close();
});
// handle messages
ws.addEventListener('message', (data) => {
let msg = JSON.parse(data.data);
// console.log(msg)
switch (msg.cmd){
// in case you want to receive other data and route it elsewhere
case 'OSC':
// send formatted OSC message locally (i.e. a pd patch)
localSend.send(msg.addressPattern, msg.typeTagString, (err) => {
if (err) console.error(err);
});
break;
default:
// inform user that unknown message commang used
console.log('client sent message with unknown cmd: ' + msg)
break;
}
});
// ***** Local UDP-Receiver ******* //
// this is used by either mode!
let localReceivePort;
if(mode === 'server'){
localReceivePort = 7402
} else if (mode === 'client'){
localReceivePort = 7404
}
const localReceive = new Server(localReceivePort, '0.0.0.0');
// once running, inform user
localReceive.on('listening', () => {
console.log('Configure your local pd patch(es) to send on UDP Port ' + localReceivePort)
})
// handle message:
localReceive.on('message', (msg) => {
// validate correct OSC address pattern syntax
// console.log(msg)
if(msg[0].charAt(0) === '/'){
// get the address pattern
ap = msg[0]
// trim the address pattern
msg.shift()
// construct object to send over websocket
message = {
// cmd allows us to send other types of messages, ask Michael for more info if curious!
cmd: 'OSC',
date: new Date().toUTCString(),
addressPattern: ap,
// this is the data!
typeTagString: msg
}
// inform user
// console.log('sending to remote:\n', message)
// package data for the web, send it!
// if(ws){
ws.send(JSON.stringify(message))
// }
} else {
// if incoming OSC message does not have an address pattern, refuse to handle it
console.log('error, OSC Message must lead with an addressPattern\n\ni.e. /bioData')
}
});
} else {
// app needs to know what mode to run in
console.log('error! first argument must be either \'client\' or \'server\'')
// stop node process
process.exit()
}