├── .gitignore ├── electron ├── .npmrc ├── src │ ├── main.js │ ├── client │ │ └── html │ │ │ └── main-window.html │ ├── serialport-logger.js │ └── app.js ├── README.md └── package.json ├── udp-browser ├── web │ ├── package.json │ ├── osc-view.css │ └── index.html ├── testSend.py ├── testReceive.py ├── package.json ├── README.md └── index.js ├── chrome-app ├── package.json ├── js │ ├── launch.js │ ├── example-synth.js │ └── app.js ├── manifest.json ├── css │ └── osc-view.css ├── html │ └── index.html └── README.md ├── browser ├── web │ ├── package.json │ ├── osc-view.css │ ├── index.html │ └── socket-synth.js ├── package.json ├── README.md └── index.js ├── README.md ├── send-to-supercollider ├── supercollider-receive.scd ├── README.md ├── package.json └── index.js ├── utils ├── supercollider-lemur-faderlab-style-client.scd └── arduino │ └── ArduinoOSCSender │ └── ArduinoOSCSender.ino └── nodejs ├── package.json ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | -------------------------------------------------------------------------------- /electron/.npmrc: -------------------------------------------------------------------------------- 1 | target=23.1.3 2 | disturl=https://electronjs.org/headers 3 | runtime=electron 4 | build_from_source=true 5 | -------------------------------------------------------------------------------- /udp-browser/web/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc.js-webudp-app", 3 | "version": "1.0.0", 4 | "ignore": [], 5 | "dependencies": { 6 | "osc": "2.4.4", 7 | "jquery": "3.6.4" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /chrome-app/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc.js-chrome-app", 3 | "version": "1.0.1", 4 | "main": "launch.js", 5 | "ignore": [], 6 | "dependencies": { 7 | "osc": "2.4.4", 8 | "flocking": "2.0.1" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /browser/web/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc.js-web-app", 3 | "version": "1.0.1", 4 | "main": "socket-synth.js", 5 | "ignore": [], 6 | "dependencies": { 7 | "osc": "2.4.4", 8 | "flocking": "2.0.1" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /electron/src/main.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var fluid = require("infusion"); 4 | 5 | require("./serialport-logger.js") 6 | require("./app.js"); 7 | 8 | var electronExamples = fluid.registerNamespace("oscjsExamples.electron"); 9 | electronExamples.app(); 10 | -------------------------------------------------------------------------------- /chrome-app/js/launch.js: -------------------------------------------------------------------------------- 1 | chrome.app.runtime.onLaunched.addListener(function () { 2 | chrome.app.window.create("html/index.html", { 3 | id: "app-window", 4 | bounds: { 5 | width: 640, 6 | height: 640 7 | } 8 | }); 9 | }); 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | osc.js Examples 2 | =============== 3 | 4 | This repository contains examples of how to use osc.js in a variety of situations, including in a browser with Web Sockets, on Node.js, and in a Chrome App. 5 | 6 | For more information, please see [osc.js](https://github.com/colinbdclark/osc.js). 7 | -------------------------------------------------------------------------------- /udp-browser/testSend.py: -------------------------------------------------------------------------------- 1 | import optparse 2 | from OSC import * 3 | from OSC import _readString, _readFloat, _readInt 4 | 5 | c = OSCClient() 6 | c.connect(('127.0.0.1',7400)) 7 | print c 8 | 9 | m = OSCMessage("/test") 10 | m += [44, 11, 4.5, "the white cliffs of dover"] 11 | 12 | c.send(m) 13 | 14 | c.close() 15 | -------------------------------------------------------------------------------- /send-to-supercollider/supercollider-receive.scd: -------------------------------------------------------------------------------- 1 | ( 2 | ~listener = {|msg, time, replyAddr, recvPort| 3 | if (msg[0] != "/status.reply", { 4 | // Log all received messages to the console. 5 | ("Message received on port" + recvPort + "from " + replyAddr.ip + ":" + replyAddr.port + ":" + msg).postln; 6 | }); 7 | }; 8 | 9 | thisProcess.addOSCRecvFunc(~listener); 10 | ) 11 | -------------------------------------------------------------------------------- /electron/src/client/html/main-window.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | osc.js Electron Example 6 | 7 | 8 | 9 | 10 |

osc.js Electron Example

11 | 12 |

Check the electron console to see OSC messages sent via the serial port.

13 | 14 | 15 | -------------------------------------------------------------------------------- /electron/README.md: -------------------------------------------------------------------------------- 1 | # Electron App Example 2 | 3 | This examples illustrates the use of osc.js within an Electron application. It uses [Fluid Infusion](https://github.com/fluid-project/infusion) and [infusion-electron](https://github.com/colinbdclark/infusion-electron) to implement the Electron application. 4 | 5 | ## Installation 6 | 7 | 1. Run npm install 8 | 9 | ## Running the Example 10 | 11 | 1. Run npm run electron-app 12 | -------------------------------------------------------------------------------- /utils/supercollider-lemur-faderlab-style-client.scd: -------------------------------------------------------------------------------- 1 | n = NetAddr.new("127.0.0.1", 57121); 2 | 3 | w = Window.new("osc test"); 4 | 5 | f = w.addFlowLayout(5@5, 5@5); 6 | 7 | [1,2,3,4].do({arg num; 8 | var oscpath = "/fader%/out".format(num); 9 | EZSlider.new( 10 | w, 11 | 30@200, 12 | label: "f%".format(num), 13 | action: {arg tez; 14 | 15 | (oscpath ++ ", " ++ tez.value).postln; 16 | n.sendMsg(oscpath, tez.value); 17 | 18 | }, 19 | layout:'vert' 20 | ); 21 | }); 22 | 23 | w.front; -------------------------------------------------------------------------------- /chrome-app/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "OSC.js Chrome App Demo", 3 | "sockets": { 4 | "udp": { 5 | "bind": "*" 6 | } 7 | }, 8 | "version": "1", 9 | "manifest_version": 2, 10 | "permissions": [ 11 | "serial", 12 | "system.network" 13 | ], 14 | "minimum_chrome_version": "23", 15 | "icons": {}, 16 | "app": { 17 | "background": { 18 | "scripts": ["js/launch.js"], 19 | "transient": true 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /send-to-supercollider/README.md: -------------------------------------------------------------------------------- 1 | # Sending OSC Messages from Node.js to SuperCollider 2 | 3 | This example shows how to send OSC messages from a Node.js server using osc.js to the SuperCollider language process, _sclang_. 4 | 5 | ## Instructions 6 | 7 | 1. Run npm install to install osc.js and all its dependencies. 8 | 2. Start the Node application by running node . 9 | 3. Open supercollider-receive.scd in SuperCollider and evaluate it. 10 | 4. Watch SuperCollider's console for incoming OSC messages. 11 | -------------------------------------------------------------------------------- /udp-browser/web/osc-view.css: -------------------------------------------------------------------------------- 1 | #toolbar { 2 | padding: 0.5em 0.5em 0.75em 0.5em; 3 | } 4 | 5 | #messageArea { 6 | color: #fff; 7 | background-color: #aaa; 8 | border-radius: 10px; 9 | padding-left: 0.75em; 10 | padding-top: 0.1em; 11 | height: 93%; 12 | width: 98%; 13 | } 14 | 15 | #udpStatus { 16 | float: right; 17 | font-size: 75%; 18 | font-style: italic; 19 | padding-top: 0.5em; 20 | } 21 | 22 | #messageLabel { 23 | font-weight: bold; 24 | } 25 | 26 | #message { 27 | font-family: monospace; 28 | font-size: 148%; 29 | } 30 | -------------------------------------------------------------------------------- /chrome-app/css/osc-view.css: -------------------------------------------------------------------------------- 1 | #toolbar { 2 | padding: 0.5em 0.5em 0.75em 0.5em; 3 | } 4 | 5 | #messageArea { 6 | color: #fff; 7 | background-color: #aaa; 8 | border-radius: 10px; 9 | padding-left: 0.75em; 10 | padding-top: 0.1em; 11 | height: 93%; 12 | width: 98%; 13 | } 14 | 15 | #udpStatus { 16 | float: right; 17 | } 18 | 19 | #udpStatus div span{ 20 | font-family: monospace; 21 | font-size: 110% 22 | } 23 | 24 | #messageLabel { 25 | font-weight: bold; 26 | } 27 | 28 | #message { 29 | font-family: monospace; 30 | font-size: 148%; 31 | } 32 | -------------------------------------------------------------------------------- /udp-browser/testReceive.py: -------------------------------------------------------------------------------- 1 | import optparse 2 | from OSC import * 3 | from OSC import _readString, _readFloat, _readInt 4 | 5 | if __name__ == "__main__": 6 | 7 | s = OSCServer(("127.0.0.1", 7500), return_port=7500) 8 | s.addMsgHandler("/hello", s.msgPrinter_handler) 9 | 10 | print s 11 | 12 | st = threading.Thread(target=s.serve_forever) 13 | st.start() 14 | 15 | try: 16 | while True: 17 | time.sleep(30) 18 | 19 | except KeyboardInterrupt: 20 | print "\nClosing OSCServer." 21 | s.close() 22 | print "Waiting for Server-thread to finish" 23 | st.join() 24 | 25 | sys.exit(0) -------------------------------------------------------------------------------- /chrome-app/html/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | : 5 | 7 | 8 |
9 | 10 |
11 |

OSC Message:

12 |

13 | 
14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /electron/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc-electron-example", 3 | "main": "src/main.js", 4 | "description": "An example of how to use osc.js with Electron.", 5 | "version": "1.0.0", 6 | "author": "Colin Clark", 7 | "license": "BSD-3-Clause", 8 | "repository": "git://github.com/colinbdclark/osc.js-examples.git", 9 | "bugs": "http://github.com/colinbdclark/osc.js-examples/issues", 10 | "homepage": "http://github.com/colinbdclark/osc.js-examples", 11 | "readmeFilename": "README.md", 12 | "dependencies": { 13 | "electron": "23.1.3", 14 | "infusion": "4.2.0", 15 | "infusion-electron": "0.10.0", 16 | "osc": "2.4.4" 17 | }, 18 | "scripts": { 19 | "app": "npx electron ." 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /udp-browser/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc.js-examples-udp-ws", 3 | "main": "index.js", 4 | "version": "0.1.1", 5 | "description": "Example of how to bi-directionally send OSC messages between a web browser and a UDP socket.", 6 | "author": "Colin Clark", 7 | "homepage": "http://flockingjs.org/", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/colinbdclark/osc.js-examples.git" 11 | }, 12 | "readmeFilename": "README.md", 13 | "bugs": "https://github.com/colinbdclark/osc.js/issues", 14 | "license": "(MIT OR GPL-2.0)", 15 | "keywords": [ 16 | "Open Sound Control", 17 | "OSC" 18 | ], 19 | "dependencies": { 20 | "osc": "2.4.4", 21 | "ws": "8.13.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /nodejs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc.js-examples-node", 3 | "main": "index.js", 4 | "version": "0.1.1", 5 | "description": "Example of how to use osc.js in Node.js.", 6 | "author": "Colin Clark", 7 | "homepage": "http://flockingjs.org/", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/colinbdclark/osc.js-examples.git" 11 | }, 12 | "readmeFilename": "README.md", 13 | "bugs": "https://github.com/colinbdclark/osc.js/issues", 14 | "license": "(MIT OR GPL-2.0)", 15 | "keywords": [ 16 | "Open Sound Control", 17 | "OSC", 18 | "sound", 19 | "audio", 20 | "music", 21 | "Flocking" 22 | ], 23 | "dependencies": { 24 | "osc": "2.4.4" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /browser/web/osc-view.css: -------------------------------------------------------------------------------- 1 | #toolbar { 2 | padding: 0.5em 0.5em 0.75em 0.5em; 3 | } 4 | 5 | #messageArea { 6 | color: #fff; 7 | background-color: #aaa; 8 | border-radius: 10px; 9 | padding-left: 0.75em; 10 | padding-top: 0.1em; 11 | height: 93%; 12 | width: 98%; 13 | } 14 | 15 | #udpStatus { 16 | float: right; 17 | font-size: 75%; 18 | font-style: italic; 19 | padding-top: 0.5em; 20 | } 21 | 22 | #messageLabel { 23 | font-weight: bold; 24 | } 25 | 26 | #message { 27 | font-family: monospace; 28 | font-size: 148%; 29 | } 30 | 31 | button { 32 | font-size: 150%; 33 | border: none; 34 | border-radius: 0.5em; 35 | padding: 0.4em; 36 | margin-bottom: 1em; 37 | background-color: #000; 38 | color: #fff; 39 | } 40 | -------------------------------------------------------------------------------- /send-to-supercollider/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "send-to-supercollider", 3 | "main": "index.js", 4 | "version": "0.1.1", 5 | "description": "Example of how to send messages to sclang from a Node.js app.", 6 | "author": "Colin Clark", 7 | "homepage": "http://flockingjs.org/", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/colinbdclark/osc.js-examples.git" 11 | }, 12 | "readmeFilename": "README.md", 13 | "bugs": "https://github.com/colinbdclark/osc.js/issues", 14 | "license": "(MIT OR GPL-2.0)", 15 | "keywords": [ 16 | "Open Sound Control", 17 | "OSC", 18 | "sound", 19 | "audio", 20 | "music", 21 | "Flocking" 22 | ], 23 | "dependencies": { 24 | "osc": "2.4.4" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /browser/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "osc.js-examples-web", 3 | "main": "index.js", 4 | "version": "0.1.1", 5 | "description": "Example of how to use osc.js in a web browser.", 6 | "author": "Colin Clark", 7 | "homepage": "http://flockingjs.org/", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/colinbdclark/osc.js-examples.git" 11 | }, 12 | "readmeFilename": "README.md", 13 | "bugs": "https://github.com/colinbdclark/osc.js/issues", 14 | "license": "(MIT OR GPL-2.0)", 15 | "keywords": [ 16 | "Open Sound Control", 17 | "OSC", 18 | "sound", 19 | "audio", 20 | "music", 21 | "Flocking" 22 | ], 23 | "dependencies": { 24 | "osc": "2.4.4", 25 | "express": "4.17.1", 26 | "ws": "7.5.3" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /browser/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | osc.js Web Socket Demo 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |

osc.js Web Socket Demo

15 | 16 | 17 | 18 |
19 |

OSC Message:

20 |

21 |         
22 | 23 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /utils/arduino/ArduinoOSCSender/ArduinoOSCSender.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #if defined(CORE_TEENSY) 5 | #include 6 | #else 7 | #include 8 | #endif 9 | 10 | #if defined(CORE_TEENSY) 11 | SLIPEncodedUSBSerial SLIPSerial(Serial); 12 | #else 13 | SLIPEncodedSerial SLIPSerial(Serial); 14 | #endif 15 | 16 | void setup() { 17 | Serial.begin(9600); // Rate is ignored for USB devices such as the Teensy. 18 | } 19 | 20 | void loop() { 21 | 22 | String address = String("/knobs/0"); 23 | int addressLength = address.length() + 1; 24 | char addressBuffer[addressLength]; 25 | 26 | address.toCharArray(addressBuffer, addressLength); 27 | OSCMessage msg(addressBuffer); 28 | msg.add(random(100)/100.0); 29 | 30 | msg.send(SLIPSerial); 31 | SLIPSerial.endPacket(); 32 | msg.empty(); 33 | 34 | delay(1000); 35 | 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /send-to-supercollider/index.js: -------------------------------------------------------------------------------- 1 | var osc = require("osc"); 2 | 3 | var udpPort = new osc.UDPPort({ 4 | // This is the port we're listening on. 5 | localAddress: "127.0.0.1", 6 | localPort: 57121, 7 | 8 | // This is where sclang is listening for OSC messages. 9 | remoteAddress: "127.0.0.1", 10 | remotePort: 57120, 11 | metadata: true 12 | }); 13 | 14 | // Open the socket. 15 | udpPort.open(); 16 | 17 | // Every second, send an OSC message to SuperCollider 18 | setInterval(function() { 19 | var msg = { 20 | address: "/hello/from/oscjs", 21 | args: [ 22 | { 23 | type: "f", 24 | value: Math.random() 25 | }, 26 | { 27 | type: "f", 28 | value: Math.random() 29 | } 30 | ] 31 | }; 32 | 33 | console.log("Sending message", msg.address, msg.args, "to", udpPort.options.remoteAddress + ":" + udpPort.options.remotePort); 34 | udpPort.send(msg); 35 | }, 1000); 36 | -------------------------------------------------------------------------------- /electron/src/serialport-logger.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var fluid = require("infusion"), 4 | osc = require("osc"), 5 | oscjsExamples = fluid.registerNamespace("oscjsExamples"); 6 | 7 | fluid.defaults("oscjsExamples.electron.serialPortLogger", { 8 | gradeNames: "fluid.component", 9 | 10 | members: { 11 | serialPort: "@expand:oscjsExamples.electron.createSerialPort()" 12 | }, 13 | 14 | events: { 15 | onMessage: null 16 | }, 17 | 18 | listeners: { 19 | "onCreate.bindMessageLogger": { 20 | "this": "{that}.serialPort", 21 | method: "on", 22 | args: ["message", "{that}.events.onMessage.fire"] 23 | }, 24 | 25 | "onCreate.openPort": { 26 | priority: "last", 27 | "this": "{that}.serialPort", 28 | method: "open" 29 | }, 30 | 31 | "onMessage.print": { 32 | "this": "console", 33 | method: "log", 34 | args: "{arguments}.0" 35 | } 36 | } 37 | }); 38 | 39 | oscjsExamples.electron.createSerialPort = function () { 40 | return new osc.SerialPort({ 41 | devicePath: process.argv[2] || "/dev/tty.usbmodem221361" 42 | }); 43 | }; 44 | -------------------------------------------------------------------------------- /electron/src/app.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | 3 | "use strict"; 4 | 5 | var fluid = require("infusion"), 6 | oscjsExamples = fluid.registerNamespace("oscjsExamples"); 7 | 8 | require("infusion-electron"); 9 | 10 | fluid.defaults("oscjsExamples.electron.app", { 11 | gradeNames: "electron.app", 12 | 13 | components: { 14 | mainWindow: { 15 | createOnEvent: "onReady", 16 | type: "oscjsExamples.electron.window" 17 | }, 18 | 19 | serialPortLogger: { 20 | type: "oscjsExamples.electron.serialPortLogger" 21 | } 22 | } 23 | }); 24 | 25 | 26 | fluid.defaults("oscjsExamples.electron.window", { 27 | gradeNames: "electron.browserWindow", 28 | 29 | windowOptions: { 30 | title: "osc.js Electron serial port example", 31 | x: 0, 32 | y: 0, 33 | width: 640, 34 | height: 480 35 | }, 36 | 37 | 38 | model: { 39 | url: { 40 | expander: { 41 | funcName: "fluid.stringTemplate", 42 | args: [ 43 | "%url/src/client/html/main-window.html", 44 | "{app}.env.appRoot" 45 | ] 46 | } 47 | } 48 | } 49 | }); 50 | -------------------------------------------------------------------------------- /udp-browser/README.md: -------------------------------------------------------------------------------- 1 | # Bi-directional UDP <-> web browser example 2 | 3 | This example opens a Web Socket in a web page that communicates with a Node.js server. 4 | The server is responsible for relaying OSC messages bidirectionally between the web page and set of 5 | example Python scripts. 6 | 7 | ## Installation 8 | 9 | From the command line: 10 | 1. Run npm install 11 | 2. In the web folder, run npm install 12 | 3. Use [pip](https://pypi.python.org/pypi/pip) to install pyosc: sudo pip install pyosc --pre 13 | ** On Mac OS X, pip can be easily installed using the command sudo easy_install pip. 14 | 15 | ## Running the Demo 16 | 17 | 1. In the udp-browser folder, start the Node.js server: node . 18 | 2. In web folder, open index.html in a web browser; a log message will be printed to the terminal when you have connected. 19 | 3. To send an OSC message via UDP to the browser, run python testSend.py in a new terminal window; an OSC message should appear in the web browser window. 20 | 4. To send an OSC message from the browser to the UDP socket, run python testReceive.py. This will start a Python-based UDP OSC server. Then in the browser, click the 'Send OSC message' button. An OSC message should appear in the terminal window where you ran testReceive.py. 21 | -------------------------------------------------------------------------------- /udp-browser/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | osc.js Web Socket Demo 5 | 6 | 7 | 8 | 9 | 30 | 31 | 32 | 33 |

osc.js Web Socket Demo

34 | 35 |
36 |

OSC Message:

37 |

38 |         
39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /nodejs/index.js: -------------------------------------------------------------------------------- 1 | var osc = require("osc"); 2 | 3 | /******************* 4 | * OSC Over Serial * 5 | *******************/ 6 | 7 | // Instantiate a new OSC Serial Port. 8 | var serialPort = new osc.SerialPort({ 9 | devicePath: process.argv[2] || "/dev/tty.usbmodem221361" 10 | }); 11 | 12 | serialPort.on("message", function (oscMessage) { 13 | console.log(oscMessage); 14 | }); 15 | 16 | // Open the port. 17 | serialPort.open(); 18 | 19 | 20 | /**************** 21 | * OSC Over UDP * 22 | ****************/ 23 | 24 | var getIPAddresses = function () { 25 | var os = require("os"), 26 | interfaces = os.networkInterfaces(), 27 | ipAddresses = []; 28 | 29 | for (var deviceName in interfaces) { 30 | var addresses = interfaces[deviceName]; 31 | for (var i = 0; i < addresses.length; i++) { 32 | var addressInfo = addresses[i]; 33 | if (addressInfo.family === "IPv4" && !addressInfo.internal) { 34 | ipAddresses.push(addressInfo.address); 35 | } 36 | } 37 | } 38 | 39 | return ipAddresses; 40 | }; 41 | 42 | var udpPort = new osc.UDPPort({ 43 | localAddress: "0.0.0.0", 44 | localPort: 57121 45 | }); 46 | 47 | udpPort.on("ready", function () { 48 | var ipAddresses = getIPAddresses(); 49 | 50 | console.log("Listening for OSC over UDP."); 51 | ipAddresses.forEach(function (address) { 52 | console.log(" Host:", address + ", Port:", udpPort.options.localPort); 53 | }); 54 | }); 55 | 56 | udpPort.on("message", function (oscMessage) { 57 | console.log(oscMessage); 58 | }); 59 | 60 | udpPort.on("error", function (err) { 61 | console.log(err); 62 | }); 63 | 64 | udpPort.open(); 65 | -------------------------------------------------------------------------------- /udp-browser/index.js: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------- 2 | // Bi-Directional OSC messaging Websocket <-> UDP 3 | //-------------------------------------------------- 4 | var osc = require("osc"), 5 | WebSocket = require("ws"); 6 | 7 | var getIPAddresses = function () { 8 | var os = require("os"), 9 | interfaces = os.networkInterfaces(), 10 | ipAddresses = []; 11 | 12 | for (var deviceName in interfaces){ 13 | var addresses = interfaces[deviceName]; 14 | 15 | for (var i = 0; i < addresses.length; i++) { 16 | var addressInfo = addresses[i]; 17 | 18 | if (addressInfo.family === "IPv4" && !addressInfo.internal) { 19 | ipAddresses.push(addressInfo.address); 20 | } 21 | } 22 | } 23 | 24 | return ipAddresses; 25 | }; 26 | 27 | var udp = new osc.UDPPort({ 28 | localAddress: "0.0.0.0", 29 | localPort: 7400, 30 | remoteAddress: "127.0.0.1", 31 | remotePort: 7500 32 | }); 33 | 34 | udp.on("ready", function () { 35 | var ipAddresses = getIPAddresses(); 36 | console.log("Listening for OSC over UDP."); 37 | ipAddresses.forEach(function (address) { 38 | console.log(" Host:", address + ", Port:", udp.options.localPort); 39 | }); 40 | console.log("Broadcasting OSC over UDP to", udp.options.remoteAddress + ", Port:", udp.options.remotePort); 41 | }); 42 | 43 | udp.open(); 44 | 45 | var wss = new WebSocket.Server({ 46 | port: 8081 47 | }); 48 | 49 | wss.on("connection", function (socket) { 50 | console.log("A Web Socket connection has been established!"); 51 | var socketPort = new osc.WebSocketPort({ 52 | socket: socket 53 | }); 54 | 55 | var relay = new osc.Relay(udp, socketPort, { 56 | raw: true 57 | }); 58 | }); 59 | -------------------------------------------------------------------------------- /browser/README.md: -------------------------------------------------------------------------------- 1 | # UDP -> Web Socket Example 2 | 3 | This example illustrates a Node.js server that will relay OSC messages sent via 4 | a UDP socket (listening on port 57121) to a web page using a Web Socket connection. 5 | 6 | By default, it is configured to handle OSC messages via UDP from Lemur's AB Faderlab project. 7 | It maps four parameters of a simple Flocking-based FM synthesizer to the first four 8 | faders in the Lemur project. Here is the mapping: 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 |
OSC AddressOSC Argument TypeSynth Paramter
/fader1/outfloat 0.0 - 1.0carrier frequency
/fader2/outfloat 0.0 - 1.0carrier amplitude
/fader3/outfloat 0.0 - 1.0modulator frequency
/fader4/outfloat 0.0 - 1.0modulator amplitude
37 | 38 | If you don't have a copy of Lemur, you can use the SuperCollider test client created by [Jascha Narveson](https://github.com/jaschanarveson), which is located in the [utils](../utils/supercollider-lemur-faderlab-style-client.scd) directory. 39 | 40 | ## Installation 41 | 42 | 1. Run npm install in the terminal to install all required Node dependencies 43 | 2. In the web directory, run npm install to install all web dependencies 44 | 45 | ## Running the Example 46 | 47 | 1. Run node . in the Terminal 48 | 2. Open http://localhost:8081 in your browser 49 | 3. Control the synth using OSC messages sent from Lemur or another OSC server 50 | -------------------------------------------------------------------------------- /browser/index.js: -------------------------------------------------------------------------------- 1 | var osc = require("osc"), 2 | express = require("express"), 3 | WebSocket = require("ws"); 4 | 5 | var getIPAddresses = function () { 6 | var os = require("os"), 7 | interfaces = os.networkInterfaces(), 8 | ipAddresses = []; 9 | 10 | for (var deviceName in interfaces) { 11 | var addresses = interfaces[deviceName]; 12 | for (var i = 0; i < addresses.length; i++) { 13 | var addressInfo = addresses[i]; 14 | if (addressInfo.family === "IPv4" && !addressInfo.internal) { 15 | ipAddresses.push(addressInfo.address); 16 | } 17 | } 18 | } 19 | 20 | return ipAddresses; 21 | }; 22 | 23 | // Bind to a UDP socket to listen for incoming OSC events. 24 | var udpPort = new osc.UDPPort({ 25 | localAddress: "0.0.0.0", 26 | localPort: 57121 27 | }); 28 | 29 | udpPort.on("ready", function () { 30 | var ipAddresses = getIPAddresses(); 31 | console.log("Listening for OSC over UDP."); 32 | ipAddresses.forEach(function (address) { 33 | console.log(" Host:", address + ", Port:", udpPort.options.localPort); 34 | }); 35 | console.log("To start the demo, go to http://localhost:8081 in your web browser."); 36 | }); 37 | 38 | udpPort.open(); 39 | 40 | // Create an Express-based Web Socket server to which OSC messages will be relayed. 41 | var appResources = __dirname + "/web", 42 | app = express(), 43 | server = app.listen(8081), 44 | wss = new WebSocket.Server({ 45 | server: server 46 | }); 47 | 48 | app.use("/", express.static(appResources)); 49 | wss.on("connection", function (socket) { 50 | console.log("A Web Socket connection has been established!"); 51 | var socketPort = new osc.WebSocketPort({ 52 | socket: socket 53 | }); 54 | 55 | var relay = new osc.Relay(udpPort, socketPort, { 56 | raw: true 57 | }); 58 | }); 59 | -------------------------------------------------------------------------------- /nodejs/README.md: -------------------------------------------------------------------------------- 1 | # Node.js Serial and UDP Example 2 | 3 | This example illustrates a Node.js application that accepts OSC messages 4 | from the serial port and a UDP socket (listening on port 57121). 5 | 6 | ## OSC Messages Over UDP 7 | By default, this example is configured to handle OSC messages via from Lemur's AB Faderlab project. 8 | It maps four parameters of a simple Flocking-based FM synthesizer to the first four 9 | faders in the Lemur project. Here is the mapping: 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 |
OSC AddressOSC Argument TypeSynth Paramter
/fader1/outfloat 0.0 - 1.0carrier frequency
/fader2/outfloat 0.0 - 1.0carrier amplitude
/fader3/outfloat 0.0 - 1.0modulator frequency
/fader4/outfloat 0.0 - 1.0modulator amplitude
38 | 39 | If you don't have a copy of Lemur, you can use the SuperCollider test client created by [Jascha Narveson](https://github.com/jaschanarveson), which is located in the [utils](../utils/supercollider-lemur-faderlab-style-client.scd) directory. 40 | 41 | ## OSC Messages Over Serial 42 | This example also listens for OSC messages over the serial port (which can be specified as the first argument to Node.js when starting the server). Here is the mapping: 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 |
OSC AddressOSC Argument TypeSynth Paramter
/knobs/0float 0.0 - 1.0carrier frequency
/knobs/1float 0.0 - 1.0carrier amplitude
/knobs/2float 0.0 - 1.0modulator frequency
/knobs/3float 0.0 - 1.0modulator amplitude
71 | 72 | ## Installing and Running the Example 73 | 74 | 0. Run npm install in the terminal to install all required dependencies 75 | 1. Run node . to start the Node.js application 76 | 2. Control the synth using OSC messages sent from Lemur, another source of OSC messages sent via UDP, or via a serial device that sends OSC messages (such as an Arduino project) 77 | -------------------------------------------------------------------------------- /chrome-app/js/example-synth.js: -------------------------------------------------------------------------------- 1 | var flock = flock || require("../../nodejs/node_modules/flocking"), 2 | example = example || {}; 3 | 4 | (function () { 5 | 6 | "use strict"; 7 | 8 | flock.init(); 9 | 10 | // A nice little FM synth. 11 | // Incoming OSC messages control the frequency and amplitude 12 | // of both the modulator and the carrier oscillators. 13 | example.synth = flock.synth({ 14 | synthDef: { 15 | id: "carrier", 16 | ugen: "flock.ugen.sin", 17 | freq: { 18 | ugen: "flock.ugen.value", 19 | rate: "audio", 20 | value: 400, 21 | add: { 22 | id: "modulator", 23 | ugen: "flock.ugen.sin", 24 | freq: { 25 | ugen: "flock.ugen.value", 26 | rate: "audio", 27 | value: 124 28 | }, 29 | mul: 100 30 | } 31 | }, 32 | mul: 0.3 33 | } 34 | }); 35 | 36 | var freqTransform = function (value) { 37 | return (value * 6000) + 60; 38 | }; 39 | 40 | var identityTransform = function (value) { 41 | return value; 42 | }; 43 | 44 | var carrierSpec = { 45 | freq: { 46 | inputPath: "carrier.freq.value", 47 | transform: freqTransform 48 | }, 49 | mul: { 50 | inputPath: "carrier.mul", 51 | transform: identityTransform 52 | } 53 | }; 54 | 55 | var modulatorSpec = { 56 | freq: { 57 | inputPath: "modulator.freq.value", 58 | transform: freqTransform 59 | }, 60 | mul: { 61 | inputPath: "modulator.mul", 62 | transform: freqTransform 63 | } 64 | }; 65 | 66 | example.synthValueMap = { 67 | "/knobs/0": carrierSpec.freq, 68 | "/fader1/out": carrierSpec.freq, 69 | 70 | "/knobs/1": carrierSpec.mul, 71 | "/fader2/out": carrierSpec.mul, 72 | 73 | "/knobs/2": modulatorSpec.freq, 74 | "/fader3/out": modulatorSpec.freq, 75 | 76 | "/knobs/3": modulatorSpec.mul, 77 | "/fader4/out": modulatorSpec.mul 78 | }; 79 | 80 | example.mapOSCToSynth = function (oscMessage, synth, valueMap) { 81 | var address = oscMessage.address; 82 | var value = oscMessage.args[0]; 83 | var transformSpec = valueMap[address]; 84 | 85 | if (transformSpec) { 86 | var transformed = transformSpec.transform(value); 87 | synth.set(transformSpec.inputPath, transformed); 88 | } 89 | }; 90 | 91 | // If we're in a require-compatible environment, export ourselves. 92 | if (typeof module !== "undefined" && module.exports) { 93 | module.exports = example; 94 | } 95 | 96 | }()); 97 | -------------------------------------------------------------------------------- /chrome-app/README.md: -------------------------------------------------------------------------------- 1 | # Chrome App Serial and UDP Example 2 | 3 | This example illustrates a Chrome application that accepts OSC messages 4 | from the serial port and a UDP socket (listening on port 57121). 5 | 6 | ## OSC Messages Over UDP 7 | By default, this example is configured to handle OSC messages via from Lemur's AB Faderlab project. 8 | It maps four parameters of a simple Flocking-based FM synthesizer to the first four 9 | faders in the Lemur project. Here is the mapping: 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 |
OSC AddressOSC Argument TypeSynth Paramter
/fader1/outfloat 0.0 - 1.0carrier frequency
/fader2/outfloat 0.0 - 1.0carrier amplitude
/fader3/outfloat 0.0 - 1.0modulator frequency
/fader4/outfloat 0.0 - 1.0modulator amplitude
38 | 39 | If you don't have a copy of Lemur, you can use the SuperCollider test client created by [Jascha Narveson](https://github.com/jaschanarveson), which is located in the [utils](../utils/supercollider-lemur-faderlab-style-client.scd) directory. 40 | 41 | ## OSC Messages Over Serial 42 | This example also listens for OSC messages over the serial port, which can be selected from the dropdown menu in the app. Here is the mapping: 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 |
OSC AddressOSC Argument TypeSynth Paramter
/knobs/0float 0.0 - 1.0carrier frequency
/knobs/1float 0.0 - 1.0carrier amplitude
/knobs/2float 0.0 - 1.0modulator frequency
/knobs/3float 0.0 - 1.0modulator amplitude
71 | 72 | ## Installation 73 | 74 | 1. Run npm install in the Terminal 75 | 2. Open Chrome's _Extensions_ window (_More Tools > Extensions_) 76 | 3. Enable the _Developer Mode_ checkbox 77 | 4. Choose _Load unpacked extension..._ and navigate to the chrome-app directory 78 | 79 | ## Running the Example 80 | 1. In Chrome's _Extensions_ window, click the _Launch_ link for the OSC.js Chrome App Demo extension 81 | 2. Control the synth using OSC messages sent from Lemur, another source of OSC messages sent via UDP, or via a serial device that sends OSC messages (such as an Arduino project). 82 | -------------------------------------------------------------------------------- /chrome-app/js/app.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | "use strict"; 4 | 5 | var oscMessageListener = function (oscMessage) { 6 | example.mapOSCToSynth(oscMessage, example.synth, example.synthValueMap); 7 | $("#message").text(fluid.prettyPrintJSON(oscMessage)); 8 | }; 9 | 10 | /******************* 11 | * OSC Over Serial * 12 | *******************/ 13 | 14 | // Enumerate all the serial port devices and render them to the dropdown. 15 | chrome.serial.getDevices(function (ports) { 16 | var portSelector = $("#portSelector"); 17 | 18 | portSelector.empty(); 19 | ports.forEach(function (port) { 20 | var label = port.displayName || port.path; 21 | var option = $(""); 22 | portSelector.append(option); 23 | }); 24 | 25 | var selectPort = function () { 26 | var selectedDevice = $("#portSelector").val(); 27 | connectToSerialPort(selectedDevice); 28 | }; 29 | 30 | portSelector.change(selectPort); 31 | 32 | if (ports.length === 1) { 33 | selectPort(); 34 | } 35 | 36 | }); 37 | 38 | var connectToSerialPort = function (devicePath) { 39 | // Instantiate a new OSC Serial Port. 40 | var serialPort = new osc.SerialPort({ 41 | devicePath: devicePath 42 | }); 43 | 44 | // Listen for the message event and map the OSC message to the synth. 45 | serialPort.on("message", oscMessageListener); 46 | 47 | // Open the port. 48 | serialPort.open(); 49 | }; 50 | 51 | 52 | /**************** 53 | * OSC Over UDP * 54 | ****************/ 55 | 56 | var getIPAddresses = function () { 57 | var ipAddresses = []; 58 | chrome.system.network.getNetworkInterfaces(function (interfaces) { 59 | interfaces.forEach(function (iface) { 60 | if (iface.prefixLength === 24) { 61 | ipAddresses.push(iface.address); 62 | } 63 | }); 64 | }); 65 | 66 | return ipAddresses; 67 | }; 68 | 69 | // Also bind to a UDP socket. 70 | var udpPort = new osc.UDPPort({ 71 | localAddress: "0.0.0.0", 72 | localPort: 57121 73 | }); 74 | 75 | udpPort.on("ready", function () { 76 | var ipAddresses = getIPAddresses(), 77 | addressPortStrings = []; 78 | 79 | ipAddresses.forEach(function (address) { 80 | addressPortStrings.push(address + ":" + udpPort.options.localPort); 81 | }); 82 | 83 | $("#udpStatus").append("
UDP: " + addressPortStrings.join(",") + "
"); 84 | }); 85 | 86 | udpPort.on("message", oscMessageListener); 87 | udpPort.on("error", function (err) { 88 | throw new Error(err); 89 | }); 90 | 91 | udpPort.open(); 92 | 93 | // Start playing the synth. 94 | flock.init(); 95 | example.synth.play(); 96 | }()); 97 | -------------------------------------------------------------------------------- /browser/web/socket-synth.js: -------------------------------------------------------------------------------- 1 | var example = example || {}; 2 | 3 | (function () { 4 | "use strict"; 5 | 6 | var freqTransform = function (value) { 7 | return (value * 6000) + 60; 8 | }; 9 | 10 | var identityTransform = function (value) { 11 | return value; 12 | }; 13 | 14 | var carrierSpec = { 15 | freq: { 16 | inputPath: "carrier.freq.value", 17 | transform: freqTransform 18 | }, 19 | mul: { 20 | inputPath: "carrier.mul", 21 | transform: identityTransform 22 | } 23 | }; 24 | 25 | var modulatorSpec = { 26 | freq: { 27 | inputPath: "modulator.freq.value", 28 | transform: freqTransform 29 | }, 30 | mul: { 31 | inputPath: "modulator.mul", 32 | transform: freqTransform 33 | } 34 | }; 35 | 36 | example.SocketSynth = function () { 37 | this.oscPort = new osc.WebSocketPort({ 38 | url: "ws://localhost:8081" 39 | }); 40 | 41 | this.listen(); 42 | this.oscPort.open(); 43 | 44 | this.oscPort.socket.onmessage = function (e) { 45 | console.log("message", e); 46 | }; 47 | 48 | this.valueMap = { 49 | "/knobs/0": carrierSpec.freq, 50 | "/fader1/out": carrierSpec.freq, 51 | 52 | "/knobs/1": carrierSpec.mul, 53 | "/fader2/out": carrierSpec.mul, 54 | 55 | "/knobs/2": modulatorSpec.freq, 56 | "/fader3/out": modulatorSpec.freq, 57 | 58 | "/knobs/3": modulatorSpec.mul, 59 | "/fader4/out": modulatorSpec.mul 60 | }; 61 | }; 62 | 63 | example.SocketSynth.prototype.createSynth = function () { 64 | if (this.synth) { 65 | return; 66 | } 67 | 68 | this.synth = flock.synth({ 69 | synthDef: { 70 | id: "carrier", 71 | ugen: "flock.ugen.sin", 72 | freq: { 73 | ugen: "flock.ugen.value", 74 | rate: "audio", 75 | value: 400, 76 | add: { 77 | id: "modulator", 78 | ugen: "flock.ugen.sin", 79 | freq: { 80 | ugen: "flock.ugen.value", 81 | rate: "audio", 82 | value: 124 83 | }, 84 | mul: 100 85 | } 86 | }, 87 | mul: 0.3 88 | } 89 | }); 90 | }; 91 | 92 | example.SocketSynth.prototype.listen = function () { 93 | var that = this; 94 | $("button").click(function () { 95 | that.createSynth(); 96 | that.play(); 97 | }); 98 | 99 | this.oscPort.on("message", this.mapMessage.bind(this)); 100 | this.oscPort.on("message", function (msg) { 101 | console.log("message", msg); 102 | }); 103 | this.oscPort.on("close", this.pause.bind(this)); 104 | }; 105 | 106 | example.SocketSynth.prototype.play = function () { 107 | this.synth.play(); 108 | }; 109 | 110 | example.SocketSynth.prototype.pause = function () { 111 | this.synth.pause(); 112 | }; 113 | 114 | example.SocketSynth.prototype.mapMessage = function (oscMessage) { 115 | $("#message").text(fluid.prettyPrintJSON(oscMessage)); 116 | 117 | var address = oscMessage.address; 118 | var value = oscMessage.args[0]; 119 | var transformSpec = this.valueMap[address]; 120 | 121 | if (transformSpec) { 122 | var transformed = transformSpec.transform(value); 123 | this.synth.set(transformSpec.inputPath, transformed); 124 | } 125 | }; 126 | 127 | }()); 128 | --------------------------------------------------------------------------------