aboutsummaryrefslogtreecommitdiffhomepage
path: root/desktop/core/io/osc.js
blob: c761ced5a28f02d98a4222b855d183d3bed2c73e (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')

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

  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 }) {
    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 (port < 1000) { console.warn('Unavailable port'); return }
    this.port = port
    this.setup()
    this.update()
  }

  this.update = function () {
    console.log(`OSC Port: ${this.port}`)
    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.kill() }
    this.client = new osc.Client(ip, this.port)
  }
}

module.exports = Osc