├── .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 |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. Runnpm 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 | OSC Message:
12 | 13 |OSC Message:
20 | 21 |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 Message:
37 | 38 || OSC Address | 13 |OSC Argument Type | 14 |Synth Paramter | 15 |
|---|---|---|
| /fader1/out | 18 |float 0.0 - 1.0 | 19 |carrier frequency | 20 |
| /fader2/out | 23 |float 0.0 - 1.0 | 24 |carrier amplitude | 25 |
| /fader3/out | 28 |float 0.0 - 1.0 | 29 |modulator frequency | 30 |
| /fader4/out | 33 |float 0.0 - 1.0 | 34 |modulator amplitude | 35 |
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 | | OSC Address | 14 |OSC Argument Type | 15 |Synth Paramter | 16 |
|---|---|---|
| /fader1/out | 19 |float 0.0 - 1.0 | 20 |carrier frequency | 21 |
| /fader2/out | 24 |float 0.0 - 1.0 | 25 |carrier amplitude | 26 |
| /fader3/out | 29 |float 0.0 - 1.0 | 30 |modulator frequency | 31 |
| /fader4/out | 34 |float 0.0 - 1.0 | 35 |modulator amplitude | 36 |
| OSC Address | 47 |OSC Argument Type | 48 |Synth Paramter | 49 |
|---|---|---|
| /knobs/0 | 52 |float 0.0 - 1.0 | 53 |carrier frequency | 54 |
| /knobs/1 | 57 |float 0.0 - 1.0 | 58 |carrier amplitude | 59 |
| /knobs/2 | 62 |float 0.0 - 1.0 | 63 |modulator frequency | 64 |
| /knobs/3 | 67 |float 0.0 - 1.0 | 68 |modulator amplitude | 69 |
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 | | OSC Address | 14 |OSC Argument Type | 15 |Synth Paramter | 16 |
|---|---|---|
| /fader1/out | 19 |float 0.0 - 1.0 | 20 |carrier frequency | 21 |
| /fader2/out | 24 |float 0.0 - 1.0 | 25 |carrier amplitude | 26 |
| /fader3/out | 29 |float 0.0 - 1.0 | 30 |modulator frequency | 31 |
| /fader4/out | 34 |float 0.0 - 1.0 | 35 |modulator amplitude | 36 |
| OSC Address | 47 |OSC Argument Type | 48 |Synth Paramter | 49 |
|---|---|---|
| /knobs/0 | 52 |float 0.0 - 1.0 | 53 |carrier frequency | 54 |
| /knobs/1 | 57 |float 0.0 - 1.0 | 58 |carrier amplitude | 59 |
| /knobs/2 | 62 |float 0.0 - 1.0 | 63 |modulator frequency | 64 |
| /knobs/3 | 67 |float 0.0 - 1.0 | 68 |modulator amplitude | 69 |
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("