-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmaxCommunication.js
More file actions
95 lines (85 loc) · 2.58 KB
/
maxCommunication.js
File metadata and controls
95 lines (85 loc) · 2.58 KB
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
var MaxCommunication = (function(){
var instantiated;
function init(options){
var socket = Websocket.getInstance({'host' : options.socketHost});
var udpPort = options.udpPort;
var byteConverter = ByteConverter.getInstance();
var oscLib = LibOSC.getInstance();
var controls = {
'default' : function(msg){console.log(msg);}
};
// add Max message listener and handling to socket
socket.receiveMsg("max",function(data){
var oscMsg = oscLib.parseOSCMsg(data);
var functionId = oscMsg.address;
if(controls[functionId] != undefined){
controls[functionId](oscMsg.typeFlags,oscMsg.values);
} else{
controls["default"](oscMsg);
}
});
// private function to send a message to Max
function sendMessageToMax(address,typeArray,valueArray){
socket.sendMsg("max",{"port" : udpPort, "mssg" : oscLib.createOSCMsg(address,typeArray,valueArray)});
}
return {
// SERVER ONLY COMMUNICATION
sendMsgToServer : function(msgType,data){
socket.sendMsg(msgType,data);
},
receiveMsgFromServer : function(msgType,operation){
socket.receiveMsg(msgType,operation);
},
// RECEIVING MSGS FROM MAX
setDefaultBehaviour : function(fct){
controls["default"] = fct;
},
addMaxMsgHandler : function(address,operation){
controls[address] = operation;
},
// SENDING MSGS TO MAX
sendMsgToMax : function(address,typeArray,valueArray){
sendMessageToMax(address,typeArray,valueArray);
},
sendStringToMax : function(address,value){
sendMessageToMax(address,["s"],[value]);
},
sendStringsToMax : function(address,valueArray){
var typeArray = new Array();
for(var i = 0; i < valueArray.length; i++){
typeArray.push("s");
}
sendMessageToMax(address,typeArray,valueArray);
},
sendIntToMax : function(address,value){
sendMessageToMax(address,["i"],[value])
},
sendIntsToMax : function(address,valueArray){
var typeArray = new Array();
for(var i = 0; i < valueArray.length; i++){
typeArray.push("i");
}
sendMessageToMax(address,typeArray,valueArray);
},
sendFloatToMax : function(address,value){
sendMessageToMax(address,["f"],[value]);
},
sendFloatsToMax : function(address,valueArray){
var typeArray = new Array();
for(var i = 0; i < valueArray.length; i++){
typeArray.push("f");
}
sendMessageToMax(address,typeArray,valueArray);
}
}; // END OF PUBLIC SPACE
}
// Singleton behaviour
return {
getInstance : function(options){
if (!instantiated){
instantiated = init(options);
}
return instantiated;
}
};
})();