-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (65 loc) · 1.88 KB
/
index.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
var osc = require("osc");
/****************
* OSC Over UDP *
****************/
var getIPAddresses = function () {
var os = require("os"),
interfaces = os.networkInterfaces(),
ipAddresses = [];
for (var deviceName in interfaces) {
var addresses = interfaces[deviceName];
for (var i = 0; i < addresses.length; i++) {
var addressInfo = addresses[i];
if (addressInfo.family === "IPv4" && !addressInfo.internal) {
ipAddresses.push(addressInfo.address);
}
}
}
return ipAddresses;
};
var udpPort = new osc.UDPPort({
localAddress: "0.0.0.0",
localPort: 9000,
// sonic pi receives on port 4560 if this port is not used.
remoteAddress: "127.0.0.1",
remotePort: 4560,
metadata: true
});
udpPort.on("ready", function () {
var ipAddresses = getIPAddresses();
console.log("Listening for OSC over UDP.");
ipAddresses.forEach(function (address) {
console.log(" Host:", address + ", Port:", udpPort.options.localPort);
});
});
udpPort.on("message", function (oscMessage) {
console.log(oscMessage);
});
udpPort.on("error", function (err) {
console.log(err);
});
udpPort.open();
/*********** send to sonic pi *****/
// Every second, send an OSC message to Sonic Pi - 1000 ms = 1s :-)
setInterval(function() {
var msg = {
address: "/play/note",
args: [
{
// midi note value
type: "i",
value: 60+24
},
{ // amp value a float to send decimal numbers
type: "f",
value: 1.5
},
{ // sustain value
type: "f",
value: 2
}
]
};
console.log("Sending message", msg.address, msg.args, "to", udpPort.options.remoteAddress + ":" + udpPort.options.remotePort);
udpPort.send(msg);
}, 1000);