aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pilot.moon
blob: 1cbc0702cb20701f92caa1eb85d0e788214e4231 (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
import Const, Op, FnDef from require 'core'
import dns, udp from require 'socket'

conn = udp!
hex = "0123456789abcdef"
encode = (arg) ->
  switch type arg
    when 'number' then
      i = 1 + math.floor arg
      hex\sub i, i
    when 'string' then arg
    else error "invalid type: #{type arg}"

send = (tbl) ->
  str = table.concat [encode v for v in *tbl]
  conn\sendto str, '127.0.0.1', 49161

class play extends Op
  @doc: "(play trig ch oct note [vel [len]]) - play a note when trig is live"

  setup: (@trig, @ch, @oct, @note, @vel, @len) =>

  update: (dt) =>
    vals = for c in *{@trig, @ch, @oct, @note, @vel, @len }
      c\update dt
      c\get!

    trig = table.remove vals, 1

    if trig
      send vals

class effect extends Op
  @doc: "(effect which a b) - set an effect

which is one of 'DIS', 'CHO', 'REV' or 'FEE'"

  setup: (@which, @a, @b) =>

  update: (dt) =>
    @which\update dt
    @a\update dt
    @b\update dt
    which, a, b = @which\get!, @a\get!, @b\get!
    if which and a and b
      send { which, a, b }
{
  :play
  :effect
}