aboutsummaryrefslogtreecommitdiffhomepage
path: root/desktop/core/io/osc.js
blob: 5afe920c653f054d398633aa4f7a68a1532d3c2f (plain)
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
'use strict'

const osc = require('node-osc')

export default function Osc (terminal) {
  this.stack = []
  this.port = null
  this.options = { default: 49162, tidalCycles: 6010, sonicPi: 4559, superCollider: 57120 }

  this.start = function () {
    console.info('OSC Starting..')
    this.setup()
    this.select()
  }

  this.clear = function () {
    this.stack = []
  }

  this.run = function () {
    for (const id in this.stack) {
      this.play(this.stack[id])
    }
  }

  this.send = function (path, msg) {
    this.stack.push({ path, msg })
  }

  this.play = function ({ path, msg }) {
    if (!this.client) { console.warn('OSC', 'Unavailable client'); return }
    if (!msg) { console.warn('OSC', 'Empty message'); return }
    const oscMsg = new osc.Message(path)
    for (var i = 0; i < msg.length; i++) {
      oscMsg.append(terminal.orca.valueOf(msg.charAt(i)))
    }
    this.client.send(oscMsg, (err) => {
      if (err) { console.warn(err) }
    })
  }

  this.select = function (port = this.options.default) {
    if (isNaN(port) || port < 1000) { console.warn('Unavailable port'); return }
    console.info('OSC', `Selected port: ${port}`)
    this.port = parseInt(port)
    this.setup()
    this.update()
  }

  this.update = function () {
    terminal.controller.clearCat('default', 'OSC')
    for (const id in this.options) {
      terminal.controller.add('default', 'OSC', `${id.charAt(0).toUpperCase() + id.substr(1)}(${this.options[id]}) ${this.port === this.options[id] ? ' — Active' : ''}`, () => { terminal.io.osc.select(this.options[id]) }, '')
    }
    terminal.controller.commit()
  }

  this.setup = function (ip = '127.0.0.1') {
    if (this.client) { this.client.close() }
    this.client = new osc.Client(ip, this.port)
  }
}