-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from msawired/OSC-support
OSC support
- Loading branch information
Showing
13 changed files
with
1,380 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
|
||
/oscServer/node_modules | ||
/node_modules | ||
.release-it.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"git": { | ||
"tagName": "${version}", | ||
"commitMessage": "Version Bump ${version}", | ||
"requireCleanWorkingDir": true, | ||
"requireUpstream": false, | ||
"commit": true, | ||
"pushTags": true | ||
}, | ||
"npm": { | ||
"publish": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Launch Server", | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"program": "${workspaceFolder}/oscServer/oscServer.js --verbose" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,105 @@ class OPC { | |
constructor() { | ||
this.options = {}; | ||
this.collapsed = false; | ||
this.osc = null; | ||
} | ||
|
||
static setOSC(server, port = null) { | ||
// you can set the OSC server and port here | ||
// this will be used to send OSC messages when a variable changes | ||
// This will also be used to receive OSC messages to update the variables | ||
|
||
//load the OSC library | ||
if (typeof window.OSC === 'undefined') { | ||
let script = document.createElement('script'); | ||
script.onload = function () { | ||
console.log('OSC library loaded'); | ||
//initialize OSC | ||
OPC.loadOSC(server, port); | ||
}; | ||
script.onerror = function () { | ||
console.log('OSC library failed to load'); | ||
}; | ||
script.src = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/osc-browser.min.js'; | ||
document.head.appendChild(script); | ||
} | ||
} | ||
|
||
static loadOSC(server, port = null) { | ||
//initialize OSC | ||
OPC.osc = new osc.WebSocketPort({ | ||
url: `${server}` + (port ? `:${port}` : ''), // URL to your Web Socket server. | ||
metadata: true | ||
}); | ||
OPC.osc.on('open', function () { | ||
console.log('OSC connection opened'); | ||
}); | ||
OPC.osc.on('close', function () { | ||
console.log('OSC connection closed. Trying again in 5 seconds...'); | ||
//try again in 5 seconds | ||
setTimeout(function () { | ||
OPC.osc.open(); | ||
}, 5000); | ||
|
||
}); | ||
OPC.osc.on('error', function (error) { | ||
console.log('OSC connection error. Trying again in 5 seconds...'); | ||
//try again in 5 seconds | ||
setTimeout(function () { | ||
OPC.osc.open(); | ||
}, 5000); | ||
|
||
}); | ||
OPC.osc.on('message', function (message) { | ||
//set variables based on message | ||
// console.log('OSC message:', message); | ||
if (message.address) { | ||
let variableName = message.address.slice(1); //remove the first slash | ||
if (OPC.options[variableName]) { | ||
if (message.args.length === 1) { | ||
let value = message.args[0]; | ||
if (typeof message.args[0] === 'object') { | ||
value = message.args[0].value; | ||
if (message.args[0].type === 'f') { | ||
value = parseFloat(value); | ||
} else if (message.args[0].type === 'i') { | ||
value = parseInt(value); | ||
} else if (message.args[0].type === 's') { | ||
value = value.split(','); | ||
} | ||
} | ||
OPC._set(variableName, value); | ||
} | ||
} | ||
} | ||
}); | ||
OPC.osc.on('ready', function () { | ||
console.log('OSC ready'); | ||
}); | ||
OPC.osc.open(); | ||
} | ||
static oscSendMessage(varName, value) { | ||
let type; | ||
if (Array.isArray(value)) { | ||
type = 's'; | ||
value = value.join(','); | ||
} else if (typeof value === 'string') { | ||
type = 's'; | ||
} else if (typeof value === 'number') { | ||
type = 'f'; | ||
} else if (typeof value === 'boolean') { | ||
type = 'i'; | ||
value = value ? 1 : 0; | ||
} | ||
OPC.osc.send({ | ||
address: `/${varName}`, | ||
args: [ | ||
{ type, value } | ||
] | ||
}); | ||
} | ||
|
||
|
||
static slider(variableNameOrConfig, value, min = 0, max = null, step = null) { | ||
let variableName, label, description; | ||
if (typeof variableNameOrConfig === 'object') { | ||
|
@@ -201,53 +298,60 @@ class OPC { | |
return OPC.options[option.name].value; | ||
}, | ||
set: function (value) { | ||
OPC.options[option.name].value = value; | ||
OPC.callParentFunction('OPC', OPC.options[option.name]); | ||
if (typeof window.parameterChanged == 'function') { | ||
window.parameterChanged(option.name, value); | ||
OPC._set(option.name, value); | ||
if (OPC.osc) { | ||
OPC.oscSendMessage(option.name, value); | ||
} | ||
} | ||
}); | ||
this.callParentFunction('OPC', option); | ||
return true; | ||
} | ||
|
||
static set (variableName, value) { | ||
static _set(variableName, value) { | ||
OPC.options[variableName].value = value; | ||
OPC.callParentFunction('OPC', OPC.options[variableName]); | ||
if (typeof window.parameterChanged == 'function') { | ||
window.parameterChanged(variableName, value); | ||
} | ||
} | ||
|
||
static set(variableName, value) { | ||
window[variableName] = value; | ||
} | ||
|
||
static buttonPressed (variableName, value) { | ||
static buttonPressed(variableName, value) { | ||
OPC.options[variableName].value = value; | ||
if (typeof window.buttonPressed == 'function') { | ||
window.buttonPressed(variableName, value); | ||
} | ||
|
||
} | ||
static buttonReleased (variableName, value) { | ||
static buttonReleased(variableName, value) { | ||
OPC.options[variableName].value = value; | ||
if (typeof window.buttonReleased == 'function') { | ||
window.buttonReleased(variableName, value); | ||
} | ||
|
||
} | ||
|
||
static collapse () { | ||
static collapse() { | ||
OPC.collapsed = true; | ||
OPC.callParentFunction('OPC_collapsed', OPC.collapsed); | ||
} | ||
static expand () { | ||
static expand() { | ||
OPC.collapsed = false; | ||
OPC.callParentFunction('OPC_collapsed', OPC.collapsed); | ||
} | ||
static delete (variableName) { | ||
if (OPC.options[variableName]){ | ||
static delete(variableName) { | ||
if (OPC.options[variableName]) { | ||
delete OPC.options[variableName]; | ||
delete window.variableName; | ||
} | ||
OPC.callParentFunction('OPC_delete', variableName); | ||
} | ||
|
||
static callParentFunction (functionName, arg = {}) { | ||
static callParentFunction(functionName, arg = {}) { | ||
// console.log(arg); | ||
try { | ||
//try sending as is | ||
|
Oops, something went wrong.